File indexing completed on 2024-05-19 05:59:10

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class ExploreController extends Local_Controller_Action_DomainSwitch
0024 {
0025     const DEFAULT_ORDER = 'latest';
0026     const TAG_ISORIGINAL = 'original-product';
0027 
0028     /** @var  string */
0029     protected $_browserTitlePrepend;
0030 
0031     public function init()
0032     {
0033         parent::init();
0034         $this->_auth = Zend_Auth::getInstance();
0035     }
0036 
0037     public function categoriesAction()
0038     {
0039         // Filter-Parameter
0040         $inputFilterParams['category'] = (int)$this->getParam('cat', null);
0041         $inputFilterParams['filter'] = (int)$this->getParam('fil', null);
0042         $inputFilterParams['order'] = preg_replace('/[^-a-zA-Z0-9_]/', '', $this->getParam('ord', self::DEFAULT_ORDER));
0043         $inputFilterParams['selected'] = (int)$this->getParam('sel', $inputFilterParams['category']);
0044 
0045         $modelCategories = new Default_Model_DbTable_ProjectCategory();
0046         $children = $modelCategories->fetchImmediateChildren($inputFilterParams['selected']);
0047         $selChild = $modelCategories->fetchElement($inputFilterParams['filter']);
0048 
0049         $response = $this->generateResponseMsg($children, $inputFilterParams, $selChild);
0050         $this->_helper->json($response);
0051     }
0052 
0053     private function generateResponseMsg($children, $inputParams, $selChild)
0054     {
0055         $result = array();
0056 
0057         if (count($children) == 0) {
0058             return $result;
0059         }
0060 
0061         $helperBuildExploreUrl = new Default_View_Helper_BuildExploreUrl();
0062         foreach ($children as $child) {
0063             $nodeSelectedState = ($inputParams['filter'] == $child['project_category_id']) ? true : false;
0064             if (1 == ($child['rgt'] - $child['lft'])) {
0065                 $result[] = array(
0066                     'title'    => $child['title'],
0067                     'key'      => $child['project_category_id'],
0068                     'href'     => $helperBuildExploreUrl->buildExploreUrl($inputParams['category'], $child['project_category_id'],
0069                         $inputParams['order']),
0070                     'target'   => '_top',
0071                     'selected' => $nodeSelectedState
0072                 );
0073             } else {
0074                 $nodeHasChildren = (1 == ($child['rgt'] - $child['lft'])) ? false : true;
0075                 $nodeIsSelectedSubCat = (($selChild['lft'] > $child['lft']) AND ($selChild['rgt'] < $child['rgt'])) ? true : false;
0076                 $nodeExpandedState = false;
0077                 $nodeChildren = null;
0078                 if ($nodeHasChildren AND $nodeIsSelectedSubCat) {
0079                     $nodeExpandedState = true;
0080                     $modelCategories = new Default_Model_DbTable_ProjectCategory();
0081                     $immChildren = $modelCategories->fetchImmediateChildren($child['project_category_id']);
0082                     $nodeChildren = $this->generateResponseMsg($immChildren, $inputParams, $selChild);
0083                 }
0084                 $result[] = array(
0085                     'title'    => $child['title'],
0086                     'key'      => $child['project_category_id'],
0087                     'folder'   => true,
0088                     'lazy'     => true,
0089                     'selected' => $nodeSelectedState,
0090                     'expanded' => $nodeExpandedState,
0091                     'href'     => $helperBuildExploreUrl->buildExploreUrl($inputParams['category'], $child['project_category_id'],
0092                         $inputParams['order']),
0093                     'target'   => '_top',
0094                     'children' => $nodeChildren
0095                 );
0096             }
0097         }
0098 
0099         return $result;
0100     }
0101 
0102     /**
0103      * @throws Zend_Cache_Exception
0104      * @throws Zend_Db_Select_Exception
0105      * @throws Zend_Exception
0106      * @throws Zend_Loader_PluginLoader_Exception
0107      * @throws Zend_Paginator_Exception
0108      */
0109     public function indexAction()
0110     {
0111 
0112 
0113 
0114         // Filter-Parameter
0115         /*$inputFilterOriginal = $this->getParam('filteroriginal', $this->getFilterOriginalFromCookie());
0116         $this->storeFilterOriginalInCookie($inputFilterOriginal);
0117         $this->view->inputFilterOriginal = $inputFilterOriginal;
0118 */
0119         
0120         $isShowOnlyFavs = (int)$this->getParam('fav', 0);
0121         
0122         
0123         $inputCatId = (int)$this->getParam('cat', null);
0124         if ($inputCatId) {
0125 //            $this->view->isFilterCat = true;
0126 //            $this->view->filterCat = $inputCatId;
0127             $this->view->catabout = $this->getCategoryAbout($inputCatId);
0128 
0129             $helperFetchCategory = new Default_View_Helper_CatTitle();
0130             $catTitle = $helperFetchCategory->catTitle($inputCatId);
0131 
0132             //$this->view->headTitle($catTitle . ' - ' . $_SERVER['HTTP_HOST'], 'SET');
0133             $this->view->headTitle($catTitle . ' - ' . $this->getHeadTitle(), 'SET');
0134         }
0135 
0136         $this->view->cat_id = $inputCatId;
0137         $this->view->showAddProduct = 1;
0138         
0139         $storeCatIds = Zend_Registry::isRegistered('store_category_list') ? Zend_Registry::get('store_category_list') : null;
0140 
0141         $filter = array();
0142         $filter['category'] = $inputCatId ? $inputCatId : $storeCatIds;
0143         $filter['order'] = preg_replace('/[^-a-zA-Z0-9_]/', '', $this->getParam('ord', self::DEFAULT_ORDER));
0144         // removed filter original 20191007
0145         //$filter['original'] = $inputFilterOriginal == 1 ? self::TAG_ISORIGINAL : null;
0146 
0147        
0148         if($isShowOnlyFavs == 1) {
0149             if($this->_authMember->member_id)
0150             {
0151                 $filter['favorite'] = $this->_authMember->member_id;
0152                 $this->view->authMember = $this->_authMember;                                   
0153             }else{
0154                 $this->redirect('/browse');
0155             }
0156         }else
0157         {            
0158             $isShowOnlyFavs = 0;            
0159         }
0160 
0161        
0162         
0163         $filter['tag'] = Zend_Registry::isRegistered('config_store_tags') ?  Zend_Registry::get('config_store_tags') : null;
0164         if (APPLICATION_ENV == "development") {
0165             Zend_Registry::get('logger')->debug(__METHOD__ . ' - ' . json_encode($filter));
0166         }
0167         
0168         $tagFilter  = Zend_Registry::isRegistered('config_store_tags') ? Zend_Registry::get('config_store_tags') : null;
0169         
0170         
0171         $tagGroupFilter  = Zend_Registry::isRegistered('config_store_taggroups') ? Zend_Registry::get('config_store_taggroups') : null;
0172         if(!empty($tagGroupFilter)) {
0173             $filterArray = array();
0174             foreach ($tagGroupFilter as $tagGroupId) {
0175                 $inputFilter = $this->getFilterTagFromCookie($tagGroupId);
0176                 $filterArray[$tagGroupId] = $inputFilter;
0177                 if(!empty($inputFilter)) {
0178                     $filter['tag'][] = $inputFilter;
0179                 }
0180             }
0181             $this->view->tag_group_filter = $filterArray;
0182         }
0183         
0184         $storeConfig = Zend_Registry::isRegistered('store_config') ? Zend_Registry::get('store_config') : null;
0185 
0186         $page = (int)$this->getParam('page', 1);
0187 
0188         $index = null;
0189         $browseListType = null;
0190         
0191         //Old: index as Param
0192         $index = $this->getParam('index');
0193         
0194         if($index) {
0195             switch ($index) {
0196                 case 2:
0197                     $browseListType = 'picture';
0198                     break;
0199                 case 3:
0200                     $browseListType = 'picture';
0201                     break;
0202                 case 4:
0203                     $browseListType = 'phone-pictures';
0204                     break;
0205                 case 8:
0206                     $browseListType = 'apps';
0207                     break;
0208                 case 9:
0209                     $browseListType = 'icons';
0210                     break;
0211             }                        
0212         }
0213         
0214         if($storeConfig->browse_list_type) {
0215             $listTypeTable = new Default_Model_DbTable_BrowseListType();
0216             $listType = $listTypeTable->findBrowseListType($storeConfig->browse_list_type);
0217             if(isset($listType)) {
0218                $browseListType =  $listType['name'];
0219                $index = 2;
0220             }
0221         }
0222 
0223         //Browse List config in Category set?
0224         if(!$index) {
0225             //Now the list type is in backend categories set
0226             $tableCat = new Default_Model_DbTable_ProjectCategory();
0227             $cat = $tableCat->findCategory($this->view->cat_id);
0228             if(isset($cat) && isset($cat['browse_list_type'])) {
0229                 $indexListType = $cat['browse_list_type'];
0230                 $listTypeTable = new Default_Model_DbTable_BrowseListType();
0231                 $listType = $listTypeTable->findBrowseListType($indexListType);
0232                 if(isset($listType)) {
0233                    $browseListType =  $listType['name'];
0234                    $index = 2;
0235                 }
0236             }
0237         }
0238         
0239         // my favourite list filter & list layout
0240         if($isShowOnlyFavs == 1) {
0241             $index=7;
0242             $browseListType =  'myfav';
0243         }
0244 
0245         
0246         
0247         if($index)
0248         {
0249             // only switch view via index=2 parameter
0250             $pageLimit = 50;
0251             if($index==7) $pageLimit = 10;
0252             $requestedElements = $this->fetchRequestedElements($filter, $pageLimit, ($page - 1) * $pageLimit);
0253             $this->view->productsJson = Zend_Json::encode($requestedElements['elements']);
0254             $this->view->filtersJson = Zend_Json::encode($filter);
0255             $this->view->cat_idJson = Zend_Json::encode($inputCatId);
0256             $modelInfo = new Default_Model_Info();
0257             $topprods = $modelInfo->getMostDownloaded(100, $inputCatId, $tagFilter);
0258             $this->view->topprodsJson = Zend_Json::encode($topprods);
0259             $comments = $modelInfo->getLatestComments(5, $inputCatId, $tagFilter);
0260             $this->view->commentsJson = Zend_Json::encode($comments);
0261             $modelCategory = new Default_Model_ProjectCategory();
0262             $this->view->categoriesJson = Zend_Json::encode($modelCategory->fetchTreeForView());
0263             $this->view->browseListType = $browseListType;
0264             
0265             $this->view->pageLimit = $pageLimit;
0266                   
0267             $this->_helper->viewRenderer('index-react'.$index);
0268 
0269         }
0270         else if ($storeConfig->layout_explore && $storeConfig->isRenderReact()) {
0271             $pageLimit = 50;
0272             $requestedElements = $this->fetchRequestedElements($filter, $pageLimit, ($page - 1) * $pageLimit);
0273             $this->view->productsJson = Zend_Json::encode($requestedElements['elements']);
0274             $this->view->filtersJson = Zend_Json::encode($filter);
0275             $this->view->cat_idJson = Zend_Json::encode($inputCatId);
0276             $modelInfo = new Default_Model_Info();
0277             $topprods = $modelInfo->getMostDownloaded(100, $inputCatId, $tagFilter);
0278             $this->view->topprodsJson = Zend_Json::encode($topprods);
0279             $comments = $modelInfo->getLatestComments(5, $inputCatId, $tagFilter);
0280             $this->view->commentsJson = Zend_Json::encode($comments);
0281             $modelCategory = new Default_Model_ProjectCategory();
0282             $this->view->categoriesJson = Zend_Json::encode($modelCategory->fetchTreeForView());
0283             $this->_helper->viewRenderer('index-react');
0284             $this->view->pageLimit = $pageLimit;
0285             
0286         } else {
0287             $pageLimit = 10;
0288             $requestedElements = $this->fetchRequestedElements($filter, $pageLimit, ($page - 1) * $pageLimit);
0289             $this->view->pageLimit = $pageLimit;
0290             
0291         }
0292         if($storeConfig) {
0293             $this->view->storeabout = $this->getStoreAbout($storeConfig->store_id);
0294         }
0295 
0296         $paginator = Local_Paginator::factory($requestedElements['elements']);
0297         $paginator->setItemCountPerPage($pageLimit);
0298         $paginator->setCurrentPageNumber($page);
0299         $paginator->setTotalItemCount($requestedElements['total_count']);
0300 
0301         $this->view->products = $paginator;
0302         $this->view->totalcount = $requestedElements['total_count'];
0303         $this->view->filters = $filter;
0304         $this->view->page = $page;
0305         $this->view->package_type = Zend_Registry::isRegistered('config_store_tags') ? Zend_Registry::get('config_store_tags') : null;
0306         $this->view->tags = $tagFilter;
0307     }
0308 
0309     
0310 
0311     /**
0312      * @param $inputCatId
0313      *
0314      * @return string|null
0315      * @throws Zend_Exception
0316      */
0317     protected function getCategoryAbout($inputCatId)
0318     {
0319         $config = Zend_Registry::get('config');
0320         $static_config = $config->settings->static;
0321 
0322         $include_path_cat = $static_config->include_path . 'category_about/' . $inputCatId . '.phtml';
0323         if (file_exists($include_path_cat)) {
0324             return $include_path_cat;
0325         }
0326 
0327         return null;
0328     }
0329     
0330     
0331 
0332     /**
0333      * @param array $inputFilterParams
0334      * @param int $limit
0335      * @param int $offset
0336      *
0337      * @return array
0338      * @throws Zend_Cache_Exception
0339      * @throws Zend_Db_Select_Exception
0340      * @throws Zend_Exception
0341      */
0342     private function fetchRequestedElements($inputFilterParams, $limit = null, $offset = null)
0343     {
0344         $modelProject = new Default_Model_Project();
0345         $requestedElements = $modelProject->fetchProjectsByFilter($inputFilterParams, $limit, $offset);
0346 
0347         return $requestedElements;
0348     }
0349 
0350     /**
0351      * @throws Zend_Exception
0352      * @throws Zend_Paginator_Exception
0353      */
0354     public function searchAction()
0355     {
0356         ini_set('memory_limit', '3072M');
0357 
0358         $allDomainCatIds = Zend_Registry::isRegistered('store_category_list') ? Zend_Registry::get('store_category_list') : null;
0359         if (count($allDomainCatIds) == 0) {
0360             $allDomainCatIds = null;
0361         }
0362 
0363         if (isset($allDomainCatIds)) {
0364             $this->view->categories = $allDomainCatIds;
0365         } else {
0366             $modelCategories = new Default_Model_DbTable_ProjectCategory();
0367             $this->view->categories = $modelCategories->fetchMainCatIdsOrdered();
0368         }
0369 
0370         // Filter-Parameter
0371         $filterInput = new Zend_Filter_Input(array('*' => 'StringTrim', 'projectSearchText' => 'StripTags', 'page' => 'digits'), array(
0372                 'projectSearchText' => array(
0373                     new Zend_Validate_StringLength(array('min' => 3, 'max' => 100)),
0374                     'presence' => 'required'
0375                 ),
0376                 'page'              => 'digits'
0377             ), $this->getAllParams());
0378 
0379         if ($filterInput->hasInvalid()) {
0380             $this->_helper->viewRenderer('searchError');
0381             $this->view->messages = $filterInput->getMessages();
0382 
0383             return;
0384         }
0385 
0386         $inputFilterParams['projectSearchText'] = $filterInput->getUnescaped('projectSearchText');
0387         $page = (int)$filterInput->getEscaped('page');
0388 
0389         $config = Zend_Registry::get('config');
0390         Zend_Search_Lucene_Search_QueryParser::setDefaultEncoding('utf-8');
0391         Zend_Search_Lucene_Analysis_Analyzer::setDefault(new Zend_Search_Lucene_Analysis_Analyzer_Common_Utf8_CaseInsensitive());
0392 
0393         $dataPath = $config->settings->search->path;
0394         $dataPath .= $this->getNameForStoreClient() . DIRECTORY_SEPARATOR;
0395         $index = Zend_Search_Lucene::open($dataPath);
0396         try {
0397             $hits = $index->find($inputFilterParams['projectSearchText'] . '*');
0398         } catch (Zend_Search_Lucene_Exception $e) {
0399             Zend_Registry::get('logger')->err(__METHOD__ . ' - ' . $e->getMessage() . PHP_EOL . $e->getTraceAsString());
0400             $hits = array();
0401         }
0402 
0403         if (count($hits) == 0) {
0404             $this->_helper->viewRenderer('searchError');
0405             $this->view->inputFilter = $inputFilterParams;
0406             $this->view->searchText = $inputFilterParams['projectSearchText'];
0407 
0408             return;
0409         }
0410 
0411         $results = $this->copyToArray($hits);
0412         $paginator = Zend_Paginator::factory($results);
0413         $paginator->setDefaultItemCountPerPage(10);
0414         $paginator->setCurrentPageNumber($page);
0415 
0416         $this->view->hitsCount = count($hits);
0417         $this->view->hits = $paginator;
0418         $this->view->page = $page;
0419         $this->view->inputFilter = $inputFilterParams;
0420         $this->view->searchText = $inputFilterParams['projectSearchText'];
0421     }
0422 
0423     /**
0424      * @param array $hits
0425      *
0426      * @return array
0427      */
0428     protected function copyToArray($hits)
0429     {
0430         $returnArray = array();
0431         /** @var $hit Zend_Search_Lucene_Search_QueryHit */
0432         foreach ($hits as $hit) {
0433             $returnArray[] = $hit->getDocument();
0434         }
0435 
0436         return $returnArray;
0437     }
0438 
0439     protected function _initResponseHeader()
0440     {
0441         $duration = 1800; // in seconds
0442         $expires = gmdate("D, d M Y H:i:s", time() + $duration) . " GMT";
0443 
0444         $this->getResponse()
0445              ->setHeader('X-FRAME-OPTIONS', 'ALLOWALL', true)
0446 //           ->setHeader('Last-Modified', $modifiedTime, true)
0447              ->setHeader('Expires', $expires, true)->setHeader('Pragma', 'no-cache', true)
0448              ->setHeader('Cache-Control', 'private, no-cache, must-revalidate', true)
0449         ;
0450     }
0451 
0452     /**
0453      * @param Zend_Config $static_config
0454      * @return string|null
0455      */
0456     protected function getStoreAbout($storeId)
0457     {
0458         $config = Zend_Registry::get('config');
0459         $static_config = $config->settings->static;
0460 
0461         $include_path_cat = $static_config->include_path . 'store_about/' . $storeId . '.phtml';
0462         if (file_exists($include_path_cat)) {
0463             return $include_path_cat;
0464         }
0465 
0466         return null;
0467     }
0468 
0469     /**
0470      * @param array $elements
0471      *
0472      * @return array with additional info's
0473      * @deprecated
0474      */
0475     private function fetchAdditionalData($elements)
0476     {
0477         $modelProject = new Default_Model_Project();
0478         $requestedElements = Array();
0479 
0480         foreach ($elements as $project) {
0481             $info = $modelProject->fetchProductInfo($project['project_id']);
0482             $requestedElements[] = $info;
0483         }
0484 
0485         return $requestedElements;
0486     }
0487 
0488     protected function setLayout()
0489     {
0490         $layoutName = 'flat_ui_template';
0491         $storeConfig = Zend_Registry::isRegistered('store_config') ? Zend_Registry::get('store_config') : null;      
0492         if($storeConfig  && $storeConfig->layout_explore)
0493         {
0494              $this->_helper->layout()->setLayout($storeConfig->layout_explore);
0495         }else{
0496             $this->_helper->layout()->setLayout($layoutName);
0497         }        
0498     }
0499 
0500    /* private function storeFilterOriginalInCookie($inputFilterOriginal)
0501     {
0502         $storedInCookie = $this->getFilterOriginalFromCookie();
0503 
0504         if (isset($inputFilterOriginal) AND ($inputFilterOriginal != $storedInCookie)) {
0505                 $config = Zend_Registry::get('config');
0506                 $cookieName = $config->settings->session->filter_browse_original;
0507                 $remember_me_seconds = $config->settings->session->remember_me->cookie_lifetime;
0508                 $cookieExpire = time() + $remember_me_seconds;
0509                 setcookie($cookieName, $inputFilterOriginal, $cookieExpire, '/');
0510         }
0511     }
0512 
0513     private function getFilterOriginalFromCookie()
0514     {
0515         $config = Zend_Registry::get('config');
0516         $cookieName = $config->settings->session->filter_browse_original;
0517 
0518         $storedInCookie = isset($_COOKIE[$cookieName]) ? $_COOKIE[$cookieName] : NULL;
0519 
0520         return $storedInCookie;
0521     }*/
0522     
0523     
0524     private function storeFilterTagInCookie($group, $tag)
0525     {
0526         $storedInCookie = $this->getFilterTagFromCookie($group);
0527 
0528         if (isset($tag) AND ($tag != $storedInCookie)) {
0529                 $config = Zend_Registry::get('config');
0530                 $cookieName = $config->settings->session->filter_browse_original.$group;
0531                 $remember_me_seconds = $config->settings->session->remember_me->cookie_lifetime;
0532                 $cookieExpire = time() + $remember_me_seconds;
0533                 setcookie($cookieName, $tag, $cookieExpire, '/');
0534         }
0535     }
0536 
0537     private function getFilterTagFromCookie($group)
0538     {
0539         $config = Zend_Registry::get('config');
0540         $cookieName = $config->settings->session->filter_browse_original.$group;
0541 
0542         $storedInCookie = isset($_COOKIE[$cookieName]) ? $_COOKIE[$cookieName] : NULL;
0543 
0544         return $storedInCookie;
0545     }
0546     
0547     
0548     public function savetaggroupfilterAction()
0549     {
0550         // Filter-Parameter
0551         $tagGroupId = (int)$this->getParam('group_id', null);
0552         $tagId = (int)$this->getParam('tag_id', null);
0553         
0554         $this->storeFilterTagInCookie($tagGroupId, $tagId);
0555 
0556         $response = array();
0557         $response['Result'] = 'OK';
0558         $this->_helper->json($response);
0559     }
0560 
0561 }