handle internal id vs integer id category in product photos
This commit is contained in:
parent
f5da89cb19
commit
4ffafb781d
|
|
@ -81,6 +81,9 @@ class ProductPhotosController extends AppController
|
||||||
])
|
])
|
||||||
->first();
|
->first();
|
||||||
$path = $productSku ? $productSku->product_id . DS . 'skus' . DS . $productSku->id : $path;
|
$path = $productSku ? $productSku->product_id . DS . 'skus' . DS . $productSku->id : $path;
|
||||||
|
|
||||||
|
$postData['product_id'] = $productSku->product->id ?? null;
|
||||||
|
$postData['product_category_id'] = $productSku->product->product_category->internal_id ?? null;
|
||||||
} else if ($this->request->getData('product_id')) {
|
} else if ($this->request->getData('product_id')) {
|
||||||
$product = $productPhotosTable->Products
|
$product = $productPhotosTable->Products
|
||||||
->find()
|
->find()
|
||||||
|
|
@ -90,9 +93,13 @@ class ProductPhotosController extends AppController
|
||||||
])
|
])
|
||||||
->first();
|
->first();
|
||||||
$path = $product ? $product->id : $path;
|
$path = $product ? $product->id : $path;
|
||||||
|
$postData['product_category_id'] = $product->product_category->internal_id ?? null;
|
||||||
|
|
||||||
} else if ($this->request->getData('product_category_id')) {
|
} else if ($this->request->getData('product_category_id')) {
|
||||||
$categoryId = $this->request->getData('product_category_id');
|
$categoryId = $this->request->getData('product_category_id');
|
||||||
$field = is_integer($categoryId) ? 'ProductCategories.id' : 'ProductCategories.internal_id';
|
// @link https://developer.wordpress.org/reference/functions/wp_is_uuid/
|
||||||
|
$regex = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/';
|
||||||
|
$field = preg_match($regex, $categoryId) ? 'ProductCategories.internal_id' : 'ProductCategories.id';
|
||||||
$productCategoryPosted = $productPhotosTable->ProductCategories
|
$productCategoryPosted = $productPhotosTable->ProductCategories
|
||||||
->find()
|
->find()
|
||||||
->where([
|
->where([
|
||||||
|
|
@ -106,12 +113,18 @@ class ProductPhotosController extends AppController
|
||||||
* @var UploadedFileInterface $photoObject
|
* @var UploadedFileInterface $photoObject
|
||||||
*/
|
*/
|
||||||
$photoObject = $this->request->getData('photo');
|
$photoObject = $this->request->getData('photo');
|
||||||
|
$ext = substr(strtolower($photoObject->getClientFilename()), -4);
|
||||||
|
$ext = str_starts_with($ext, '.') ? substr($ext, 1) : $ext;
|
||||||
|
$allowedFileTypes = ['png', 'jpeg', 'jpg'];
|
||||||
|
if (!in_array($ext, $allowedFileTypes)) {
|
||||||
|
throw new ForbiddenException('Invalid file type. Only PNG and JPG types are allowed.');
|
||||||
|
}
|
||||||
|
|
||||||
$fullPath = $baseDir . $path;
|
$fullPath = $baseDir . $path;
|
||||||
if (!file_exists($fullPath)) {
|
if (!file_exists($fullPath)) {
|
||||||
if (!mkdir($fullPath, 0777, true)) {
|
if (!mkdir($fullPath, 0777, true)) {
|
||||||
throw new ForbiddenException('Failed to create the required folders. Please check the folder permissions and try again.');
|
throw new ForbiddenException('Failed to create the required folders. Please check the folder permissions and try again.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$destination = $fullPath . DS . $uuid;
|
$destination = $fullPath . DS . $uuid;
|
||||||
|
|
||||||
|
|
@ -122,13 +135,16 @@ class ProductPhotosController extends AppController
|
||||||
}
|
}
|
||||||
|
|
||||||
$postData['photo_dir'] = $path;
|
$postData['photo_dir'] = $path;
|
||||||
$postData['photo_filename'] = $uuid;
|
$postData['photo_filename'] = $uuid . '.' . $ext;
|
||||||
|
// dd($postData);
|
||||||
|
|
||||||
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData);
|
$productPhoto = $productPhotosTable->patchEntity($productPhoto, $postData);
|
||||||
if ($productPhotosTable->save($productPhoto)) {
|
if ($productPhotosTable->save($productPhoto)) {
|
||||||
$this->Flash->success(__('The product photo has been saved.'));
|
$this->Flash->success(__('The product photo has been saved.'));
|
||||||
|
|
||||||
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;
|
||||||
|
|
|
||||||
|
|
@ -277,6 +277,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