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();
|
||||
$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')) {
|
||||
$product = $productPhotosTable->Products
|
||||
->find()
|
||||
|
|
@ -90,9 +93,13 @@ class ProductPhotosController extends AppController
|
|||
])
|
||||
->first();
|
||||
$path = $product ? $product->id : $path;
|
||||
$postData['product_category_id'] = $product->product_category->internal_id ?? null;
|
||||
|
||||
} else if ($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
|
||||
->find()
|
||||
->where([
|
||||
|
|
@ -106,6 +113,12 @@ class ProductPhotosController extends AppController
|
|||
* @var UploadedFileInterface $photoObject
|
||||
*/
|
||||
$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;
|
||||
if (!file_exists($fullPath)) {
|
||||
|
|
@ -122,13 +135,16 @@ class ProductPhotosController extends AppController
|
|||
}
|
||||
|
||||
$postData['photo_dir'] = $path;
|
||||
$postData['photo_filename'] = $uuid;
|
||||
$postData['photo_filename'] = $uuid . '.' . $ext;
|
||||
// dd($postData);
|
||||
|
||||
$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->getErrors());
|
||||
$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;
|
||||
|
|
|
|||
|
|
@ -277,6 +277,7 @@ class ProductPhotosControllerTest extends BaseControllerTest
|
|||
'enabled' => 1,
|
||||
];
|
||||
$this->post($url, $data);
|
||||
// dd($this->_response);
|
||||
$this->assertResponseCode(302);
|
||||
$this->assertRedirectContains('product-photos');
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue