<?php
namespace App\Controller\Resources;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\Routing\Annotation\Route;
use Pimcore\Localization\LocaleServiceInterface;
use Pimcore\Model\DataObject\ClassDefinition\Service;
use App\Model\Product\AbstractProduct;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Model\DataObject\Resources;
use Pimcore\Model\DataObject\Resourcecategory;
use Pimcore\Model\DataObject\Userloyaltyprogram;
class resourcesController extends FrontendController
{
public function resourcesAction(Request $request){
$get_param = $request->query->all();
$sub_category_ids = '';
$query = '';
$resources = new Resources\Listing();
if(!empty($get_param)){
foreach($get_param as $key => $value){
if($key == 'sub_category_id'){
$sub_category_ids .= str_replace('_',',', $value);
}
if($key == 'query'){
$query = $value;
}
}
}
$category_ids = (string)$this->getCategoryId($request->get('url'));
// Get parent Category
if($category_ids){
$current_category = $this->getCategory($category_ids);
}else{
$current_category = array();
}
$category_ids = (string)$this->getCategoryId($request->get('url'));
if(!empty($category_ids)){
$resources->addConditionParam("category__id IN (?)", [$category_ids]);
}
if(!empty($sub_category_ids)){
$resources->addConditionParam("subcategory REGEXP CONCAT('(^|,)(', REPLACE('$sub_category_ids',',', '|'), ')(,|$)')");
}
if(!empty($query)){
$resources->addConditionParam("name LIKE " . $resources->quote("%$query%"));
}
if($request->get('most')){
$resources->addConditionParam("mostpopular = 1");
}
$resources->load();
$all_recource_Ids=[];
foreach($resources as $Resources){
$all_recource_Ids[] = $Resources->getId();
}
$uid = $this->get('session')->get('loginUID');
$Liked_all_recource = new Userloyaltyprogram\Listing();
$Liked_all_recource->setCondition("op_name = ? and u_id = ? and obj_mod_name = ? and obj_data__id IN (?)",['Like',$uid,'Article',$all_recource_Ids]);
$Liked_all_recource->load();
// dd($Liked_all_recource);
$Liked_all_recource_Id = [];
foreach($Liked_all_recource as $liked_all_recource){
$Liked_all_recource_Id[]=$liked_all_recource->getObj_id();}
// dd($Liked_all_recource_Id);
// Resource Sub Category
$category = new Resourcecategory\Listing();
$category->setCondition("o_parentId IN (?)", [$category_ids]);
$category->load();
// More Topics
$more_topics = new Resourcecategory\Listing();
$more_topics->setCondition("o_parentId = 269");
$more_topics->load();
// Filter sub category
$filters = array(
'category_ids' => $sub_category_ids,
'query' => $query,
);
// Popuplar Artical
$most = new Resources\Listing();
$most->setCondition('mostpopular IN (?)', '1');
if(!empty($category_ids)){
$most->addConditionParam("category__id IN (?)", [$category_ids]);
}
$most->setLimit('10');
$most->load();
$most_popular_Ids =[];
foreach($most as $Most){
$most_popular_Ids[]=$Most->getId();
}
//dd( $most_popular_Ids);
$uid = $this->get('session')->get('loginUID');
$Liked_most_popular = new Userloyaltyprogram\Listing();
$Liked_most_popular->setCondition("op_name = ? and u_id = ? and obj_mod_name = ? and obj_data__id IN (?)",['Like',$uid,'Article',$most_popular_Ids]);
$Liked_most_popular->load();
$Liked_most_popular_Id = [];
foreach($Liked_most_popular as $liked_most_popular){
$Liked_most_popular_Id[]=$liked_most_popular->getObj_id();}
//dd($Liked_most_popular_Id);
return $this->render('Resources/resources.html.twig', [
'categories' => $category,
'more_topics' => $more_topics,
'resources' => $resources,
'filters' => $filters,
'most' => $most,
'current_category' => $current_category,
'Liked_most_popular_Id'=>$Liked_most_popular_Id,
'Liked_all_recource_Id'=>$Liked_all_recource_Id,
]);
}
public function getCategoryId($url = ''){
$categories = new Resourcecategory\Listing();
$categories->setCondition("url IN (?)", [[$url]]);
$categories->load();
foreach($categories as $category){
$category_id = $category->getId();
break;
}
if(isset($category_id)){
return $category_id;
}
return '';
}
public function getCategory($id = ''){
$categories = new Resourcecategory\Listing();
$categories->setCondition("o_id IN (?)", [[$id]]);
$categories->load();
foreach($categories as $category){
break;
}
if(isset($category)){
return $category;
}
return '';
}
}
?>