primary sku photo working with janky toggle workaround but w/e lol
This commit is contained in:
@@ -11,7 +11,6 @@ use Cake\ORM\Table;
|
||||
use Cake\ORM\TableRegistry;
|
||||
use CakeProducts\Controller\AppController;
|
||||
use CheeseCake\Controller\Traits\OverrideTableTrait;
|
||||
;
|
||||
use CakeProducts\Model\Enum\ProductCategoryAttributeTypeId;
|
||||
use CakeProducts\Model\Table\ProductCategoryAttributesTable;
|
||||
|
||||
|
||||
@@ -74,8 +74,8 @@ class ProductPhotosController extends AppController
|
||||
$product = $productPhotosTable->Products->get($this->request->getData('product_id'));
|
||||
$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;
|
||||
$productSku = $productPhotosTable->ProductSkus->find()->where(['ProductSkus.id' => $this->request->getData('product_sku_id'), 'ProductSkus.product_id' => $product->id])->first();
|
||||
$path = $productSku ? $path . DS . 'skus' . DS . $productSku->id : $path;
|
||||
}
|
||||
/**
|
||||
* @var UploadedFileInterface $photoObject
|
||||
@@ -98,14 +98,13 @@ class ProductPhotosController extends AppController
|
||||
$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']);
|
||||
}
|
||||
dd($productPhoto->product_category_id);
|
||||
// dd(print_r($productPhoto->getErrors(), true));
|
||||
$this->Flash->error(__('The product photo could not be saved. Please, try again.'));
|
||||
}
|
||||
$productCategory = $productPhoto->product_category_id ? $productPhotosTable->ProductCategories->find()->where(['internal_id' => $productPhoto->product_category_id ?? '-1'])->first() : null;
|
||||
|
||||
57
src/Model/Behavior/ThirdToggleBehavior.php
Normal file
57
src/Model/Behavior/ThirdToggleBehavior.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace CakeProducts\Model\Behavior;
|
||||
|
||||
use ArrayObject;
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\Event\EventInterface;
|
||||
use Cake\ORM\Behavior;
|
||||
use LogicException;
|
||||
use Tools\Model\Behavior\ToggleBehavior;
|
||||
|
||||
/**
|
||||
* ToggleBehavior
|
||||
*
|
||||
* An implementation of a unique field toggle per table or scope.
|
||||
* This will ensure that on a set of records only one can be a "primary" one, setting the others to false then.
|
||||
* On delete it will give the primary status to another record if applicable.
|
||||
*
|
||||
* @author Mark Scherer
|
||||
* @license MIT
|
||||
*/
|
||||
class ThirdToggleBehavior extends ToggleBehavior {
|
||||
|
||||
/**
|
||||
* Default config
|
||||
*
|
||||
* @var array<string, mixed>
|
||||
*/
|
||||
protected array $_defaultConfig = [
|
||||
'field' => 'primary',
|
||||
'on' => 'afterSave', // afterSave (without transactions) or beforeSave (with transactions)
|
||||
'scopeFields' => [],
|
||||
'scope' => [],
|
||||
'findOrder' => null, // null = autodetect modified/created, false to disable
|
||||
'implementedMethods' => [], // to prevent conflict with public toggleField method
|
||||
];
|
||||
|
||||
/**
|
||||
* @param \Cake\Datasource\EntityInterface $entity
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function buildConditions(EntityInterface $entity) {
|
||||
$conditions = $this->getConfig('scope');
|
||||
$scopeFields = (array)$this->getConfig('scopeFields');
|
||||
|
||||
foreach ($scopeFields as $scopeField) {
|
||||
if ($entity->get($scopeField) === null) {
|
||||
continue;
|
||||
}
|
||||
$conditions[$scopeField] = $entity->get($scopeField);
|
||||
}
|
||||
// dd($conditions);
|
||||
|
||||
return $conditions;
|
||||
}
|
||||
}
|
||||
@@ -17,6 +17,7 @@ use Cake\ORM\Entity;
|
||||
* @property string $photo_filename
|
||||
* @property bool $primary_photo
|
||||
* @property bool $primary_category_photo
|
||||
* @property bool $primary_sku_photo
|
||||
* @property int $photo_position
|
||||
* @property bool $enabled
|
||||
* @property DateTime $created
|
||||
@@ -46,6 +47,7 @@ class ProductPhoto extends Entity
|
||||
'photo_filename' => true,
|
||||
'primary_photo' => true,
|
||||
'primary_category_photo' => true,
|
||||
'primary_sku_photo' => true,
|
||||
'photo_position' => true,
|
||||
'enabled' => true,
|
||||
'created' => true,
|
||||
|
||||
@@ -58,6 +58,7 @@ class ProductPhotosTable extends Table
|
||||
);
|
||||
|
||||
$this->addBehavior('Timestamp');
|
||||
|
||||
$this->addBehavior('Tools.Toggle', [
|
||||
'field' => 'primary_photo',
|
||||
'scopeFields' => ['product_id'],
|
||||
@@ -74,6 +75,14 @@ class ProductPhotosTable extends Table
|
||||
'product_category_id IS NOT' => null,
|
||||
],
|
||||
]);
|
||||
$this->addBehavior('CakeProducts.ThirdToggle', [
|
||||
'field' => 'primary_sku_photo',
|
||||
'scopeFields' => ['product_sku_id'],
|
||||
'scope' => [
|
||||
'deleted IS' => null,
|
||||
'product_sku_id IS NOT' => null,
|
||||
],
|
||||
]);
|
||||
$this->belongsTo('Products', [
|
||||
'foreignKey' => 'product_id',
|
||||
'joinType' => 'LEFT',
|
||||
|
||||
Reference in New Issue
Block a user