category id not null but product id can be null same with sku id - product photos
This commit is contained in:
parent
d239a98e8e
commit
dc2f422247
|
|
@ -12,7 +12,7 @@ class AllowProductIdToBeNullInProductPhotos extends BaseMigration
|
||||||
* https://book.cakephp.org/migrations/4/en/migrations.html#the-change-method
|
* https://book.cakephp.org/migrations/4/en/migrations.html#the-change-method
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public function change(): void
|
public function up(): void
|
||||||
{
|
{
|
||||||
$table = $this->table('product_photos');
|
$table = $this->table('product_photos');
|
||||||
$table->changeColumn('product_id', 'uuid', [
|
$table->changeColumn('product_id', 'uuid', [
|
||||||
|
|
@ -20,6 +20,34 @@ class AllowProductIdToBeNullInProductPhotos extends BaseMigration
|
||||||
'limit' => 11,
|
'limit' => 11,
|
||||||
'null' => true,
|
'null' => true,
|
||||||
]);
|
]);
|
||||||
|
$table->changeColumn('product_category_id', 'uuid', [
|
||||||
|
'default' => null,
|
||||||
|
'limit' => 11,
|
||||||
|
'null' => false,
|
||||||
|
]);
|
||||||
|
$table->update();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Change Method.
|
||||||
|
*
|
||||||
|
* More information on this method is available here:
|
||||||
|
* https://book.cakephp.org/migrations/4/en/migrations.html#the-change-method
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
$table = $this->table('product_photos');
|
||||||
|
$table->changeColumn('product_id', 'uuid', [
|
||||||
|
'default' => null,
|
||||||
|
'limit' => 11,
|
||||||
|
'null' => false,
|
||||||
|
]);
|
||||||
|
$table->changeColumn('product_category_id', 'uuid', [
|
||||||
|
'default' => null,
|
||||||
|
'limit' => 11,
|
||||||
|
'null' => true,
|
||||||
|
]);
|
||||||
$table->update();
|
$table->update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,11 +71,33 @@ class ProductPhotosController extends AppController
|
||||||
$postData = $this->request->getData();
|
$postData = $this->request->getData();
|
||||||
$postData['id'] = $uuid;
|
$postData['id'] = $uuid;
|
||||||
$baseDir = Configure::readOrFail('CakeProducts.photos.directory');
|
$baseDir = Configure::readOrFail('CakeProducts.photos.directory');
|
||||||
$product = $productPhotosTable->Products->get($this->request->getData('product_id'));
|
$path = '';
|
||||||
$path = $product->id;
|
|
||||||
if ($this->request->getData('product_sku_id')) {
|
if ($this->request->getData('product_sku_id')) {
|
||||||
$productSku = $productPhotosTable->ProductSkus->find()->where(['ProductSkus.id' => $this->request->getData('product_sku_id'), 'ProductSkus.product_id' => $product->id])->first();
|
$productSku = $productPhotosTable->ProductSkus
|
||||||
$path = $productSku ? $path . DS . 'skus' . DS . $productSku->id : $path;
|
->find()
|
||||||
|
->contain(['Products', 'Products.ProductCategories'])
|
||||||
|
->where([
|
||||||
|
'ProductSkus.id' => $this->request->getData('product_sku_id'),
|
||||||
|
])
|
||||||
|
->first();
|
||||||
|
$path = $productSku ? $productSku->product_id . DS . 'skus' . DS . $productSku->id : $path;
|
||||||
|
} else if ($this->request->getData('product_id')) {
|
||||||
|
$product = $productPhotosTable->Products
|
||||||
|
->find()
|
||||||
|
->contain(['ProductCategories'])
|
||||||
|
->where([
|
||||||
|
'Products.id' => $this->request->getData('product_id'),
|
||||||
|
])
|
||||||
|
->first();
|
||||||
|
$path = $product ? $product->id : $path;
|
||||||
|
} else if ($this->request->getData('product_category_id')) {
|
||||||
|
$productCategoryPosted = $productPhotosTable->ProductCategories
|
||||||
|
->find()
|
||||||
|
->where([
|
||||||
|
'ProductCategories.internal_id' => $this->request->getData('product_category_id'),
|
||||||
|
])
|
||||||
|
->first();
|
||||||
|
$path = $productCategoryPosted ? 'categories' : $path;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* @var UploadedFileInterface $photoObject
|
* @var UploadedFileInterface $photoObject
|
||||||
|
|
@ -95,7 +117,7 @@ class ProductPhotosController extends AppController
|
||||||
if (!file_exists($destination)) {
|
if (!file_exists($destination)) {
|
||||||
throw new ForbiddenException('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['product_category_id'] = $product->product_category_id ?? null;
|
|
||||||
$postData['photo_dir'] = $path;
|
$postData['photo_dir'] = $path;
|
||||||
$postData['photo_filename'] = $uuid;
|
$postData['photo_filename'] = $uuid;
|
||||||
|
|
||||||
|
|
@ -105,6 +127,7 @@ class ProductPhotosController extends AppController
|
||||||
|
|
||||||
return $this->redirect(['action' => 'index']);
|
return $this->redirect(['action' => 'index']);
|
||||||
}
|
}
|
||||||
|
dd($productPhoto->getErrors());
|
||||||
$this->Flash->error(__('The product photo could not be saved. Please, try again.'));
|
$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;
|
$productCategory = $productPhoto->product_category_id ? $productPhotosTable->ProductCategories->find()->where(['internal_id' => $productPhoto->product_category_id ?? '-1'])->first() : null;
|
||||||
|
|
|
||||||
|
|
@ -34,4 +34,24 @@ class SecondToggleBehavior extends ToggleBehavior {
|
||||||
'findOrder' => null, // null = autodetect modified/created, false to disable
|
'findOrder' => null, // null = autodetect modified/created, false to disable
|
||||||
'implementedMethods' => [], // to prevent conflict with public toggleField method
|
'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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,13 @@ class ProductPhotosTable extends Table
|
||||||
$this->addBehavior('Timestamp');
|
$this->addBehavior('Timestamp');
|
||||||
|
|
||||||
$this->addBehavior('Tools.Toggle', [
|
$this->addBehavior('Tools.Toggle', [
|
||||||
|
'field' => 'primary_category_photo',
|
||||||
|
'scopeFields' => ['product_category_id'],
|
||||||
|
'scope' => [
|
||||||
|
'deleted IS' => null,
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
$this->addBehavior('CakeProducts.SecondToggle', [
|
||||||
'field' => 'primary_photo',
|
'field' => 'primary_photo',
|
||||||
'scopeFields' => ['product_id'],
|
'scopeFields' => ['product_id'],
|
||||||
'scope' => [
|
'scope' => [
|
||||||
|
|
@ -67,14 +74,6 @@ class ProductPhotosTable extends Table
|
||||||
'product_id IS NOT' => null,
|
'product_id IS NOT' => null,
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
$this->addBehavior('CakeProducts.SecondToggle', [
|
|
||||||
'field' => 'primary_category_photo',
|
|
||||||
'scopeFields' => ['product_category_id'],
|
|
||||||
'scope' => [
|
|
||||||
'deleted IS' => null,
|
|
||||||
'product_category_id IS NOT' => null,
|
|
||||||
],
|
|
||||||
]);
|
|
||||||
$this->addBehavior('CakeProducts.ThirdToggle', [
|
$this->addBehavior('CakeProducts.ThirdToggle', [
|
||||||
'field' => 'primary_sku_photo',
|
'field' => 'primary_sku_photo',
|
||||||
'scopeFields' => ['product_sku_id'],
|
'scopeFields' => ['product_sku_id'],
|
||||||
|
|
@ -92,7 +91,7 @@ class ProductPhotosTable extends Table
|
||||||
$this->belongsTo('ProductCategories', [
|
$this->belongsTo('ProductCategories', [
|
||||||
'foreignKey' => 'product_category_id',
|
'foreignKey' => 'product_category_id',
|
||||||
'bindingKey' => 'internal_id',
|
'bindingKey' => 'internal_id',
|
||||||
'joinType' => 'LEFT',
|
'joinType' => 'INNER',
|
||||||
'className' => 'CakeProducts.ProductCategories',
|
'className' => 'CakeProducts.ProductCategories',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
|
@ -121,7 +120,8 @@ class ProductPhotosTable extends Table
|
||||||
|
|
||||||
$validator
|
$validator
|
||||||
->uuid('product_category_id')
|
->uuid('product_category_id')
|
||||||
->allowEmptyString('product_category_id');
|
->requirePresence('product_category_id', 'create')
|
||||||
|
->notEmptyString('product_category_id');
|
||||||
|
|
||||||
$validator
|
$validator
|
||||||
->scalar('photo_dir')
|
->scalar('photo_dir')
|
||||||
|
|
|
||||||
|
|
@ -166,6 +166,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
|
||||||
if (!copy($file, $toUseFile)) {
|
if (!copy($file, $toUseFile)) {
|
||||||
$this->fail('Failed to copy test image');
|
$this->fail('Failed to copy test image');
|
||||||
}
|
}
|
||||||
|
$categoryId = '6d223283-361b-4f9f-a7f1-c97aa0ca4c23';
|
||||||
$productId = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
|
$productId = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
|
||||||
$primaryPhotosCountBefore = $this->ProductPhotos->find()->where(['product_id' => $productId, 'primary_photo' => true])->count();
|
$primaryPhotosCountBefore = $this->ProductPhotos->find()->where(['product_id' => $productId, 'primary_photo' => true])->count();
|
||||||
$primaryPhotoBefore = $this->ProductPhotos->find()->where(['product_id' => $productId, 'primary_photo' => true])->first();
|
$primaryPhotoBefore = $this->ProductPhotos->find()->where(['product_id' => $productId, 'primary_photo' => true])->first();
|
||||||
|
|
@ -186,6 +187,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
|
||||||
]);
|
]);
|
||||||
$data = [
|
$data = [
|
||||||
'product_id' => $productId,
|
'product_id' => $productId,
|
||||||
|
'product_category_id' => $categoryId,
|
||||||
'primary_photo' => 1,
|
'primary_photo' => 1,
|
||||||
'primary_category_photo' => 0,
|
'primary_category_photo' => 0,
|
||||||
'primary_sku_photo' => 0,
|
'primary_sku_photo' => 0,
|
||||||
|
|
@ -265,7 +267,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
$data = [
|
$data = [
|
||||||
'product_id' => $productId,
|
'product_id' => '',
|
||||||
'product_category_id' => $categoryId,
|
'product_category_id' => $categoryId,
|
||||||
'product_sku_id' => '',
|
'product_sku_id' => '',
|
||||||
'primary_photo' => 0,
|
'primary_photo' => 0,
|
||||||
|
|
@ -327,7 +329,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
|
||||||
}
|
}
|
||||||
$productId = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
|
$productId = 'cfc98a9a-29b2-44c8-b587-8156adc05317';
|
||||||
$skuId = '3a477e3e-7977-4813-81f6-f85949613979';
|
$skuId = '3a477e3e-7977-4813-81f6-f85949613979';
|
||||||
|
$categoryId = '6d223283-361b-4f9f-a7f1-c97aa0ca4c23';
|
||||||
$primarySkuPhotosCountBefore = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId, 'primary_sku_photo' => true])->count();
|
$primarySkuPhotosCountBefore = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId, 'primary_sku_photo' => true])->count();
|
||||||
$primarySkuPhotoBefore = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId, 'primary_sku_photo' => true])->first();
|
$primarySkuPhotoBefore = $this->ProductPhotos->find()->where(['product_sku_id' => $skuId, 'primary_sku_photo' => true])->first();
|
||||||
|
|
||||||
|
|
@ -349,6 +351,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
|
||||||
$data = [
|
$data = [
|
||||||
'product_id' => $productId,
|
'product_id' => $productId,
|
||||||
'product_sku_id' => $skuId,
|
'product_sku_id' => $skuId,
|
||||||
|
'product_category_id' => $categoryId,
|
||||||
'primary_photo' => 0,
|
'primary_photo' => 0,
|
||||||
'primary_category_photo' => 0,
|
'primary_category_photo' => 0,
|
||||||
'primary_sku_photo' => 1,
|
'primary_sku_photo' => 1,
|
||||||
|
|
@ -356,7 +359,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
|
||||||
'enabled' => 1,
|
'enabled' => 1,
|
||||||
];
|
];
|
||||||
$this->post($url, $data);
|
$this->post($url, $data);
|
||||||
|
// dd($this->_response);
|
||||||
$this->assertResponseCode(302);
|
$this->assertResponseCode(302);
|
||||||
$this->assertRedirectContains('product-photos');
|
$this->assertRedirectContains('product-photos');
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue