/var/www/vhosts/ihelp.ro/httpdocs/src/Controller/Dashboard
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Controller/Dashboard/ProductsController.php (6310B)
paginate = [];
$products = $this->paginate($this->Products->find()
->contain(['Users', 'Causes', 'Categories', 'Tags', 'OrderProducts', 'OrderProducts.Orders', 'OrderProducts.Orders.OrderStatuses', 'FeaturedImage'])
->where(['Products.user_id' => $this->userAuth->id]));
$this->set(compact('products'));
}
/**
* Add method
*/
public function add()
{
$session = $this->request->getSession();
if (!$session->check('Dashboard.product')) {
$product = $this->Products->newEmptyEntity();
} else {
$product = $session->read('Dashboard.product');
}
$product->user_id = $this->userAuth->id;
$auth = $this->userAuth->id;
$data = $this->request->getData();
$data['featured_image']['model'] = 'Products';
$data['featured_image']['is_featured'] = true;
if ($this->request->is('post')) {
$product = $this->Products->patchEntity($product, $data, ['associated' => [
'Categories', 'Tags', 'UserAddresses',
'FeaturedImage' => ['validate' => 'FeaturedImage']
]]);
if ($this->Products->save($product)) {
$session->delete('Dashboard.product');
$this->Flash->success(__('Produsul a fost salvat.'));
return $this->redirect(['controller' => 'Products', 'action' => 'editGallery', $product->uuid]);
}
$this->Flash->error(__('Produsul nu a putut fi salvat. Vă rugăm să încercați din nou.'));
}
$causes = $this->Products->Causes->find('list')
->where(['Causes.is_active' => true]);
$categories = $this->Products->Categories->find('list', ['limit' => 200])
->where(['Categories.parent_id IS NOT' => null, 'Categories.is_active' => true]);
$tags = $this->Products->Tags->find('list', ['limit' => 200])
->where(['Tags.is_admin_only' => false, 'Tags.is_active' => true]);
$this->set(compact('product', 'causes', 'categories', 'tags', 'auth'));
}
/**
* Edit method
*/
public function edit($uuid = null)
{
if (!empty($uuid)) {
$auth = $this->userAuth->id;
$product = $this->Products->find()
->contain(['Categories', 'Tags', 'FeaturedImage'])
->where(['Products.uuid' => $uuid, 'Products.user_id' => $this->userAuth->id])
->first();
if (!$product) {
$this->Flash->success(__('Produsul nu mai exista.'));
return $this->redirect($this->referer());
}
if ($this->request->is(['patch', 'post', 'put'])) {
$product->user_id = $this->userAuth->id;
$product = $this->Products->patchEntity($product, $this->request->getData(), ['associated' => ['Categories', 'Tags', 'UserAddresses', 'FeaturedImage' => ['validate' => 'FeaturedImage']]]);
if ($this->Products->save($product)) {
$this->Flash->success(__('Produsul a fost salvat.'));
return $this->redirect($this->referer());
}
$this->Flash->error(__('Produsul nu a putut fi salvat. Vă rugăm să încercați din nou.'));
}
} else {
$this->Flash->error(__('A apărut o problemă. Vă rugăm să încercați din nou'));
return $this->redirect(['prefix' => 'Dashboard', 'controller' => 'Products', 'action' => 'index']);
}
$tags = $this->Products->Tags->find('list', ['limit' => 200])
->where(['Tags.is_admin_only' => false, 'Tags.is_active' => true]);
$causes = $this->Products->Causes->find('list', ['limit' => 200])
->where(['Causes.is_active' => true]);;
$categories = $this->Products->Categories->find('list', ['limit' => 200])->where(['Categories.parent_id IS NOT' => null, 'Categories.is_active' => true]);
$this->set(compact('product', 'tags', 'causes', 'categories', 'auth'));
}
/**
* editGallery
*/
public function editGallery($uuid = null)
{
if (!empty($uuid)) {
$product = $this->Products->find()
->where(['Products.uuid' => $uuid, 'Products.user_id' => $this->userAuth->id])
->first();
$foreignUuid = $product->uuid;
$model = $this->modelClass;
$this->loadModel('GalleryImages');
$images = $this->GalleryImages->find()
->where(['foreign_key' => $product->id, 'model' => $model])->order(['is_featured' => 'desc', 'created' => 'asc']);
} else {
$this->Flash->error(__('A apărut o problemă. Vă rugăm să încercați din nou'));
return $this->redirect(['prefix' => 'Dashboard', 'controller' => 'Products', 'action' => 'index']);
}
$this->set(compact('product', 'images', 'model', 'foreignUuid'));
}
/**
* Disable/Activate
*/
public function changeStatus($uuid = null)
{
if (!empty($uuid)) {
$this->request->allowMethod(['post', 'delete']);
$product = $this->Products->find('all')
->where(['Products.uuid' => $uuid, 'Products.user_id' => $this->userAuth->id])->first();
$product->is_active = ($product->is_active === true) ? false : true;
if ($this->Products->save($product)) {
($product->is_active === true) ? $this->Flash->success(__('Produsul a fost activat.')) : $this->Flash->success(__('Produsul a fost dezactivat.'));
} else {
$this->Flash->error(__('A apărut o problemă. Vă rugăm să încercați din nou.'));
}
} else {
$this->Flash->error(__('A apărut o problemă. Vă rugăm să încercați din nou'));
}
return $this->redirect(['prefix' => 'Dashboard', 'controller' => 'Products', 'action' => 'index']);
}
}