/var/www/vhosts/ihelp.ro/httpdocs/src/Controller
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Controller/ProductsController.php (7135B)
Authentication->addUnauthenticatedActions(['index', 'view', 'indexPromotions', 'indexPromotionsDay', 'productsUser']);
}
/**
* Index method
*/
public function index()
{
$this->paginate = [
'contain' => ['Users', 'Causes', 'Categories', 'Tags', 'UserAddresses', 'FeaturedImage', 'Causes.FeaturedImage'],
];
$causeId = $this->request->getQuery('cause_id');
$conditions = [
'Products.is_active' => true,
'Products.stock >' => 0,
'Causes.is_active' => true,
];
if ($causeId !== null && $causeId !== '') {
$conditions += [
'Products.cause_id' => $causeId
];
}
$products = $this->paginate($this->Products->find()
->where($conditions));
parent::getFavorites();
$this->set(compact('products'));
}
/**
* productsCause method
*/
public function productsCause($uuidCause)
{
$this->paginate = [
'contain' => ['Users', 'Causes', 'Categories', 'Tags', 'UserAddresses', 'FeaturedImage', 'Causes.FeaturedImage'],
];
$causeId = $this->request->getQuery('cause_id');
$conditions = [
'Products.is_active' => true,
'Products.stock >' => 0,
'Causes.is_active' => true,
'Causes.uuid' => $uuidCause,
];
if ($causeId !== null && $causeId !== '') {
$conditions += [
'Products.cause_id' => $causeId
];
}
$cause = $this->Products->Causes->find()
->where(['Causes.uuid' => $uuidCause])->first();
$products = $this->paginate($this->Products->find()
->where($conditions));
parent::getFavorites();
$this->set(compact('products', 'cause'));
}
/**
* IndexPromotions method
*/
public function indexPromotions()
{
$this->paginate = [
'contain' => ['Causes', 'Tags', 'FeaturedImage', 'Causes.FeaturedImage'],
];
$productsTagsPromotion = $this->Products->ProductsTags->find()
->contain(['Tags'])
->where(['ProductsTags.tag_id' => 1, 'Tags.is_active' => true])
->toArray();
if (!empty($productsTagsPromotion)) {
$idsProduct = [];
foreach ($productsTagsPromotion as $idProduct) {
$idsProduct[] = $idProduct->product_id;
}
$productsPromotion = $this->paginate(
$this->Products->find()
->where(['Products.is_active' => true, 'Products.stock >' => 0, 'Products.id IN ' => $idsProduct, 'Causes.is_active' => true])
);
}
parent::getFavorites();
$this->set(compact('productsPromotion'));
}
/**
* IndexPromotionsDay method
*/
public function indexPromotionsDay()
{
$productsPromotionDay = null;
$this->paginate = [
'contain' => ['Causes', 'Tags', 'FeaturedImage', 'Causes.FeaturedImage'],
];
$productsTagsPromotionDay = $this->Products->ProductsTags->find()
->contain(['Tags'])
->where(['ProductsTags.tag_id' => 2, 'Tags.is_active' => true])
->toArray();
if (!empty($productsTagsPromotionDay)) {
$idsProduct = [];
foreach ($productsTagsPromotionDay as $idProduct) {
$idsProduct[] = $idProduct->product_id;
}
$productsPromotionDay = $this->paginate(
$this->Products->find()
->where(['Products.is_active' => true, 'Products.stock >' => 0, 'Products.id IN ' => $idsProduct, 'Causes.is_active' => true])
);
}
parent::getFavorites();
$this->set(compact('productsPromotionDay'));
}
/**
* setProductsVisited method
*/
public function setProductsVisited($uuid = null)
{
$session = $this->request->getSession();
if (!$session->check('products_visited')) {
$session->write('products_visited', []);
}
if ($session->check('products_visited')) {
$products_visited = $session->read('products_visited');
if (!in_array($uuid, $products_visited)) {
$session->write(
'products_visited',
array_merge(
$products_visited,
[$uuid]
)
);
}
}
return;
}
/**
* View method
*/
public function view($slug = null, $uuid = null)
{
parent::getFavorites();
$this->setProductsVisited($uuid);
if (!empty($uuid)) {
$product = $this->Products->find()
->contain([
'Users', 'Causes',
'Categories', 'Categories.ParentCategories', 'Tags', 'FeaturedImage', 'Causes.FeaturedImage', 'Users.Profiles',
'GalleryImages' => function ($q) {
return $q->order(['is_featured' => 'DESC']);
}
])
->where(['Products.uuid' => $uuid, 'Products.is_active' => true])->first();
if ($product) {
if ($product->slug != $slug) {
return $this->redirect(['action' => 'view', 'uuid' => $uuid, 'slug' => $product->slug], 301);
}
$this->loadModel('Carts');
$cartEmptyEntity = $this->Carts->newEmptyEntity();
$this->set(compact('product', 'cartEmptyEntity'));
} else {
$this->Flash->error(__('Acest produs nu a fost gasit.'));
return $this->redirect(['action' => 'index']);
}
} else {
$this->Flash->error(__('Acest produs nu a fost gasit'));
return $this->redirect(['action' => 'index']);
}
}
/**
* View method
*/
public function productsUser($uuid = null)
{
if ($uuid) {
$this->paginate = [
'contain' => ['Users', 'Users.Profiles', 'Causes', 'Categories', 'FeaturedImage', 'Causes.FeaturedImage'],
];
$products = $this->paginate($this->Products->find()
->where(['Users.uuid' => $uuid, 'Products.is_active' => true, 'Products.stock >' => 0]));
$name = $this->Products->Users->find()
->where(['Users.uuid' => $uuid, 'Users.is_active' => true])
->first()->name;
parent::getFavorites();
$this->set(compact('products', 'name'));
}
}
}