CakeProducts/src/Controller/ProductPhotosController.php

180 lines
6.5 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace CakeProducts\Controller;
use Cake\Core\Configure;
2025-08-22 05:43:06 +00:00
use Cake\Datasource\Exception\RecordNotFoundException;
use Cake\Http\Exception\ForbiddenException;
2025-08-22 05:43:06 +00:00
use Cake\Http\Response;
use Cake\Utility\Text;
use CakeProducts\Controller\AppController;
2025-08-22 05:43:06 +00:00
use CakeProducts\Model\Table\ProductPhotosTable;
use CheeseCake\Controller\Traits\OverrideTableTrait;
use Psr\Http\Message\UploadedFileInterface;
/**
* ProductPhotos Controller
*
2025-08-22 05:43:06 +00:00
* @property ProductPhotosTable $ProductPhotos
*/
class ProductPhotosController extends AppController
{
use OverrideTableTrait;
/**
* Index method
*
2025-08-22 05:43:06 +00:00
* @return Response|null|void Renders view
*/
public function index()
{
$query = $this->getTable()->find()
2025-08-22 05:43:06 +00:00
->contain(['Products', 'ProductSkus']);
$productPhotos = $this->paginate($query);
$this->set(compact('productPhotos'));
}
/**
* View method
*
* @param string|null $id Product Photo id.
2025-08-22 05:43:06 +00:00
* @return Response|null|void Renders view
* @throws RecordNotFoundException When record not found.
*/
public function view($id = null)
{
2025-10-12 08:04:36 +00:00
$productPhoto = $this->getTable()->get($id, contain: ['Products', 'ProductSkus', 'ProductCategories']);
$this->set(compact('productPhoto'));
}
/**
* Add method
*
2025-08-22 05:43:06 +00:00
* @return Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$productPhotosTable = $this->getTable();
$productPhoto = $productPhotosTable->newEmptyEntity();
if ($this->request->is('post')) {
if (!$this->request->getData('photo')) {
$this->Flash->error('Photo is required. Nothing was uploaded. Please try again.');
$products = $productPhotosTable->Products->find('list', limit: 200)->all();
$this->set(compact('productPhoto', 'products'));
return;
}
$uuid = Text::uuid();
$postData = $this->request->getData();
$postData['id'] = $uuid;
$baseDir = Configure::readOrFail('CakeProducts.photos.directory');
$product = $productPhotosTable->Products->get($this->request->getData('product_id'));
2025-08-22 07:41:57 +00:00
$path = $product->id;
if ($this->request->getData('product_sku_id')) {
$productSku = $productPhotosTable->ProductSkus->get($this->request->getData('product_sku_id'));
$path .= DS . 'skus' . DS . $productSku->id;
}
/**
* @var UploadedFileInterface $photoObject
*/
$photoObject = $this->request->getData('photo');
2025-08-22 07:41:57 +00:00
$fullPath = $baseDir . $path;
if (!file_exists($fullPath)) {
if (!mkdir($fullPath, 0777, true)) {
throw new ForbiddenException('Failed to create the required folders. Please check the folder permissions and try again.');
}
}
2025-08-22 07:41:57 +00:00
$destination = $fullPath . DS . $uuid;
// Existing files with the same name will be replaced.
$photoObject->moveTo($destination);
if (!file_exists($destination)) {
throw new ForbiddenException('Failed to move the uploaded image to the appropriate folder. Please try again.');
}
2025-10-12 08:04:36 +00:00
$postData['product_category_id'] = $product->product_category_id ?? null;
$postData['photo_dir'] = $path;
$postData['photo_filename'] = $uuid;
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData);
if ($productPhotosTable->save($productPhoto)) {
$this->Flash->success(__('The product photo has been saved.'));
return $this->redirect(['action' => 'index']);
}
2025-10-12 08:04:36 +00:00
dd($productPhoto->product_category_id);
// dd(print_r($productPhoto->getErrors(), true));
$this->Flash->error(__('The product photo could not be saved. Please, try again.'));
}
$products = $productPhotosTable->Products->find('list', limit: 200)->all();
$productSkus = $productPhotosTable->ProductSkus->find('list', limit: 200)->all();
2025-08-22 05:43:06 +00:00
$this->set(compact('productPhoto', 'products', 'productSkus'));
}
/**
* Edit method
*
* @param string|null $id Product Photo id.
2025-08-22 05:43:06 +00:00
* @return Response|null|void Redirects on successful edit, renders view otherwise.
* @throws RecordNotFoundException When record not found.
*/
public function edit($id = null)
{
$productPhotosTable = $this->getTable();
$productPhoto = $productPhotosTable->get($id, contain: []);
if ($this->request->is(['patch', 'post', 'put'])) {
$postData = $this->request->getData();
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData);
if ($productPhotosTable->save($productPhoto)) {
$this->Flash->success(__('The product photo has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product photo could not be saved. Please, try again.'));
}
$products = $productPhotosTable->Products->find('list', limit: 200)->all();
$productSkus = $productPhotosTable->ProductSkus->find('list', limit: 200)->all();
2025-08-22 05:43:06 +00:00
$this->set(compact('productPhoto', 'products', 'productSkus'));
}
/**
* Delete method
*
* @param string|null $id Product Photo id.
2025-08-22 05:43:06 +00:00
* @return Response|null Redirects to index.
* @throws RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$productPhotosTable = $this->getTable();
$productPhoto = $productPhotosTable->get($id);
if ($productPhotosTable->delete($productPhoto)) {
$this->Flash->success(__('The product photo has been deleted.'));
} else {
$this->Flash->error(__('The product photo could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
2025-08-22 07:41:57 +00:00
/**
* @param $id
* @return Response
*/
public function image($id = null)
{
$productPhoto = $this->getTable()->get($id);
$fullPath = Configure::readOrFail('CakeProducts.photos.directory') . $productPhoto->photo_dir . DS . $productPhoto->photo_filename;
return $this->response->withFile($fullPath, [
'download' => $this->request->getQuery('download', false) === '1'
]);
2025-08-22 07:41:57 +00:00
}
}