/var/www/vhosts/ihelp.ro/httpdocs/src/Controller
Edit: /var/www/vhosts/ihelp.ro/httpdocs/src/Controller/CategoriesController.php (4897B)
Authentication->addUnauthenticatedActions(['index', 'view']);
}
/**
* index method
*/
public function index()
{
$categories = $this->Categories->find()
->order(['Categories.lft' => 'ASC'])
->all();
parent::getFavorites();
$this->set(compact('categories'));
}
/**
* view method
*/
public function view($slug = false)
{
$filters = $this->Categories->getFilters();
$prices = $this->Categories->getFilters()['price'];
$priceFilter = Hash::get($this->request->getQueryParams(), 'price');
if ($priceFilter != null) {
list($minPrice, $maxPrice) = explode('-', $priceFilter);
}
$pictures = $this->Categories->getFilters()['picture'];
$pictureFilter = Hash::get($this->request->getQueryParams(), 'picture');
parent::getFavorites();
if (!empty($slug)) {
$url = $this->request->getRequestTarget();
$category = $this->Categories->findBySlugAndIsActive($slug, true)->first();
if (!empty($category)) {
$conditions = [];
//category is child
if (!empty($category->parent_id)) {
$parentCategory = $this->Categories->find()
->where(['Categories.id' => $category->parent_id, 'Categories.is_active' => true])
->first();
} else {
$parentCategory = $category;
}
//if not parent category remove child
if (empty($parentCategory)) {
$this->Flash->error(__('Această categorie nu a fost găsită.'));
return $this->redirect(['prefix' => false, 'controller' => 'Categories', 'action' => 'index']);
}
$childrenCategories = $this->Categories->find()
->where(['Categories.parent_id' => $parentCategory->id, 'Categories.is_active' => true])
->all();
// create conditions for load products by
$conditions['Products.is_active'] = true;
if (!empty($category->parent_id)) {
//if is child
$conditions['Products.category_id'] = $category->id;
} else {
//if is parent
$categoryIds = [];
foreach ($childrenCategories as $childCategory) {
$categoryIds[] = $childCategory->id;
}
$categoryIds[] = $parentCategory->id;
$conditions['Products.category_id IN '] = $categoryIds;
}
if ($priceFilter != null) {
if (!empty($maxPrice)) {
$conditions += [
'Products.price >' => $minPrice,
'Products.price <=' => $maxPrice,
];
} else {
$conditions += [
'Products.price >' => $minPrice,
];
}
}
if ($pictures != null) {
if ($pictureFilter == 1) {
$conditions += [
'FeaturedImage.is_featured' => true,
];
}
}
$this->paginate = [
'limit' => CATEGORY_LIST_PAGE_LIMIT,
];
$this->loadModel('Products');
$products = $this->paginate($this->Products->find('all')
->contain(['Tags', 'Causes', 'FeaturedImage', 'Causes.FeaturedImage', 'Categories'])
->where($conditions)
->order(['Products.id' => 'DESC']));
$this->set(compact('products', 'slug', 'category', 'childrenCategories', 'parentCategory', 'filters', 'prices', 'priceFilter', 'pictureFilter'));
} else {
$this->Flash->error(__('Această categorie nu a fost găsită.'));
return $this->redirect(['prefix' => false, 'controller' => 'Categories', 'action' => 'index']);
}
} else {
$this->Flash->error(__('Această categorie nu a fost găsită.'));
return $this->redirect(['prefix' => false, 'controller' => 'Categories', 'action' => 'index']);
}
}
}