/var/www/vhosts/ihelp.ro/httpdocs/src/Controller/Admin
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Controller/Admin/ProductsController.php (5642B)
paginate = [
'limit' => ADMIN_PAGE_LIMIT,
'conditions' => $conditions,
'contain' => ['Users', 'Causes', 'Users.UserAddresses', 'Users.Profiles', 'Tags'],
'sortableFields' => ['Products.uuid', 'Products.user_id', 'Products.cause_id', 'Products.name', 'Products.price_min', 'Products.price', 'Products.stock', 'Products.is_price_fixed', 'Products.is_active', 'Products.created']
];
$products = $this->paginate($this->Products);
$this->set(compact('products'));
}
/**
* View method
*/
public function view($id = null)
{
if (!empty($id) && is_numeric($id)) {
$this->loadModel('OrderProducts');
$this->paginate = [
'limit' => ADMIN_PAGE_LIMIT,
'contain' => ['Orders'],
'sortableFields' => ['Orders.uuid']
];
$orderProducts = $this->paginate($this->OrderProducts->find()
->contain(['Orders', 'Orders.Users', 'Orders.UserAddresses', 'Products', 'Products.Users', 'Products.UserAddresses'])
->where(['OrderProducts.product_id' => $id]));
$productId = $id;
$this->set(compact('orderProducts', 'productId'));
} else {
$this->Flash->error(__('Oops. Please, try again.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Products', 'action' => 'index']);
}
}
/**
* Add method
*/
public function add()
{
$product = $this->Products->newEmptyEntity();
if ($this->request->is('post')) {
$product = $this->Products->patchEntity($product, $this->request->getData());
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Products', 'action' => 'index']);
}
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$users = $this->Products->Users->find('list', ['limit' => 200]);
$causes = $this->Products->Causes->find('list', ['limit' => 200]);
$tags = $this->Products->Tags->find('list', ['limit' => 200]);
$categories = $this->Products->Categories->find('list', ['limit' => 200])->where(['Categories.parent_id IS NOT' => null, 'Categories.is_active' => true]);
$this->set(compact('product', 'users', 'causes', 'tags', 'categories'));
}
/**
* Edit method
*/
public function edit($id = null)
{
if (!empty($id) && is_numeric($id)) {
$product = $this->Products->get($id, [
'contain' => ['Tags', 'Categories'],
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$product = $this->Products->patchEntity($product, $this->request->getData(), ['associated' => ['Categories', 'Tags', 'UserAddresses']]);
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been saved.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Products', 'action' => 'edit', $id]);
}
$this->Flash->error(__('The product could not be saved. Please, try again.'));
}
$users = $this->Products->Users->find('list', ['limit' => 200]);
$causes = $this->Products->Causes->find('list', ['limit' => 200]);
$userAddresses = $this->Products->Users->UserAddresses->find()
->where(['UserAddresses.user_id' => $product->user_id])->all();
if (!empty($userAddresses)) {
$addresses = [];
foreach ($userAddresses as $i => $address) {
$addresses[$address->id] = $address->full_address;
}
}
} else {
$this->Flash->error(__('Oops. Please, try again.'));
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Products', 'action' => 'index']);
}
$tags = $this->Products->Tags->find('list', ['limit' => 200])->where(['Tags.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', 'users', 'causes', 'tags', 'categories', 'addresses'));
}
/**
* Disable/Activate
*/
public function delete($id = null)
{
if (!empty($id) && is_numeric($id)) {
$this->request->allowMethod(['post', 'get']);
$product = $this->Products->get($id);
$product->is_active = ($product->is_active === true) ? false : true;
if ($this->Products->save($product)) {
$this->Flash->success(__('The product has been modified.'));
} else {
$this->Flash->error(__('The product could not be modified. Please try again later the shower has been modified.'));
}
} else {
$this->Flash->error(__('Oops. Please, try again.'));
}
return $this->redirect(['prefix' => 'Admin', 'controller' => 'Products', 'action' => 'index']);
}
}