product photos first commit - only upload base photo

This commit is contained in:
2025-08-10 02:35:25 -07:00
parent 82b3ca59ed
commit a1012b4054
15 changed files with 1021 additions and 1 deletions

View File

@@ -0,0 +1,149 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Controller;
use Cake\Core\Configure;
use Cake\Http\Exception\ForbiddenException;
use Cake\Utility\Text;
use CakeProducts\Controller\AppController;
use CheeseCake\Controller\Traits\OverrideTableTrait;
use Psr\Http\Message\UploadedFileInterface;
/**
* ProductPhotos Controller
*
* @property \CakeProducts\Model\Table\ProductPhotosTable $ProductPhotos
*/
class ProductPhotosController extends AppController
{
use OverrideTableTrait;
/**
* Index method
*
* @return \Cake\Http\Response|null|void Renders view
*/
public function index()
{
$query = $this->getTable()->find()
->contain(['Products']);
$productPhotos = $this->paginate($query);
$this->set(compact('productPhotos'));
}
/**
* View method
*
* @param string|null $id Product Photo id.
* @return \Cake\Http\Response|null|void Renders view
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$productPhoto = $this->getTable()->get($id, contain: ['Products']);
$this->set(compact('productPhoto'));
}
/**
* Add method
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$productPhotosTable = $this->getTable();
$productPhoto = $productPhotosTable->newEmptyEntity();
if ($this->request->is('post')) {
$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'));
$path = $baseDir . $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');
if (!file_exists($path)) {
if (!mkdir($path, 0777, true)) {
dd('Failed to create the required folders. Please check the folder permissions and try again. PATH: ' . $path);
throw new ForbiddenException('Failed to create the required folders. Please check the folder permissions and try again.');
}
}
$destination = $path . DS . $uuid;
// Existing files with the same name will be replaced.
$photoObject->moveTo($destination);
if (!file_exists($destination)) {
dd('Failed to move the uploaded image to the appropriate folder. Please try again.');
throw new ForbiddenException('Failed to move the uploaded image to the appropriate folder. Please try again.');
}
$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']);
}
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();
$this->set(compact('productPhoto', 'products'));
}
/**
* Edit method
*
* @param string|null $id Product Photo id.
* @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Datasource\Exception\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'])) {
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $this->request->getData());
if ($productPhotosTable->save($productPhoto)) {
$this->Flash->success(__('The product photo has been saved.'));
return $this->redirect(['action' => 'index']);
}
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();
$this->set(compact('productPhoto', 'products'));
}
/**
* Delete method
*
* @param string|null $id Product Photo id.
* @return \Cake\Http\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\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']);
}
}

View File

@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Entity;
use Cake\ORM\Entity;
/**
* ProductPhoto Entity
*
* @property string $id
* @property string $product_id
* @property string|null $product_sku_id
* @property string $photo_dir
* @property string $photo_filename
* @property bool $primary_photo
* @property int $photo_position
* @property bool $enabled
* @property \Cake\I18n\DateTime $created
* @property \Cake\I18n\DateTime|null $modified
* @property \Cake\I18n\DateTime|null $deleted
*
* @property \CakeProducts\Model\Entity\Product $product
*/
class ProductPhoto extends Entity
{
/**
* Fields that can be mass assigned using newEntity() or patchEntity().
*
* Note that when '*' is set to true, this allows all unspecified fields to
* be mass assigned. For security purposes, it is advised to set '*' to false
* (or remove it), and explicitly make individual fields accessible as needed.
*
* @var array<string, bool>
*/
protected array $_accessible = [
'product_id' => true,
'product_sku_id' => true,
'photo_dir' => true,
'photo_filename' => true,
'primary_photo' => true,
'photo_position' => true,
'enabled' => true,
'created' => true,
'modified' => true,
'deleted' => true,
'product' => true,
];
}

View File

@@ -0,0 +1,119 @@
<?php
declare(strict_types=1);
namespace CakeProducts\Model\Table;
use Cake\ORM\Query\SelectQuery;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
/**
* ProductPhotos Model
*
* @property \CakeProducts\Model\Table\ProductsTable&\Cake\ORM\Association\BelongsTo $Products
*
* @method \CakeProducts\Model\Entity\ProductPhoto newEmptyEntity()
* @method \CakeProducts\Model\Entity\ProductPhoto newEntity(array $data, array $options = [])
* @method array<\CakeProducts\Model\Entity\ProductPhoto> newEntities(array $data, array $options = [])
* @method \CakeProducts\Model\Entity\ProductPhoto get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
* @method \CakeProducts\Model\Entity\ProductPhoto findOrCreate($search, ?callable $callback = null, array $options = [])
* @method \CakeProducts\Model\Entity\ProductPhoto patchEntity(\Cake\Datasource\EntityInterface $entity, array $data, array $options = [])
* @method array<\CakeProducts\Model\Entity\ProductPhoto> patchEntities(iterable $entities, array $data, array $options = [])
* @method \CakeProducts\Model\Entity\ProductPhoto|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method \CakeProducts\Model\Entity\ProductPhoto saveOrFail(\Cake\Datasource\EntityInterface $entity, array $options = [])
* @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto>|false saveMany(iterable $entities, array $options = [])
* @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto> saveManyOrFail(iterable $entities, array $options = [])
* @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto>|false deleteMany(iterable $entities, array $options = [])
* @method iterable<\CakeProducts\Model\Entity\ProductPhoto>|\Cake\Datasource\ResultSetInterface<\CakeProducts\Model\Entity\ProductPhoto> deleteManyOrFail(iterable $entities, array $options = [])
*
* @mixin \Cake\ORM\Behavior\TimestampBehavior
*/
class ProductPhotosTable extends Table
{
/**
* Initialize method
*
* @param array<string, mixed> $config The configuration for the Table.
* @return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('product_photos');
$this->setDisplayField('photo_filename');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER',
'className' => 'CakeProducts.Products',
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator): Validator
{
$validator
->uuid('product_id')
->notEmptyString('product_id');
$validator
->uuid('product_sku_id')
->allowEmptyString('product_sku_id')
->add('product_sku_id', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator
->scalar('photo_dir')
->maxLength('photo_dir', 255)
->requirePresence('photo_dir', 'create')
->notEmptyString('photo_dir');
$validator
->scalar('photo_filename')
->maxLength('photo_filename', 255)
->requirePresence('photo_filename', 'create')
->notEmptyString('photo_filename');
$validator
->boolean('primary_photo')
->notEmptyString('primary_photo');
$validator
->integer('photo_position')
->notEmptyString('photo_position');
$validator
->boolean('enabled')
->notEmptyString('enabled');
$validator
->dateTime('deleted')
->allowEmptyDateTime('deleted');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules): RulesChecker
{
$rules->add($rules->isUnique(['product_sku_id'], ['allowMultipleNulls' => true]), ['errorField' => '0']);
$rules->add($rules->existsIn(['product_id'], 'Products'), ['errorField' => '1']);
return $rules;
}
}