Exemplo n.º 1
0
 /**
  * @uses autocomplete
  */
 public function suggestionAction()
 {
     /* @var $request \Zend\Http\Request */
     $request = $this->getRequest();
     $data = array();
     if (!($q = urldecode(trim($request->getQuery('q'))))) {
         return new JsonModel($data);
     }
     $ps = new \Product\Model\Store();
     $ps->setServiceLocator($this->getServiceLocator());
     /* @var $psMapper \Product\Model\StoreMapper */
     $psMapper = $this->getServiceLocator()->get('Product\\Model\\StoreMapper');
     /* @var $categoryMapper \Product\Model\CategoryMapper */
     $categoryMapper = $this->getServiceLocator()->get('Product\\Model\\CategoryMapper');
     $data['searchOptions'] = $ps->prepareSearch();
     $limit = trim($request->getQuery('limit'));
     $options['limit'] = $limit > 0 ? $limit : 20;
     $products = $psMapper->search($ps, $options);
     if (is_array($products) && count($products)) {
         $cIds = [];
         /* @var $ps \Product\Model\Store */
         foreach ($products as $ps) {
             $data['products'][] = $ps->toStd();
             if ($ps->getCategoryId()) {
                 $cIds[$ps->getCategoryId()] = $ps->getCategoryId();
             }
         }
         if ($request->getQuery('showMore') && $request->getQuery('showMore') == 'category') {
             $category = new \Product\Model\Category();
             $category->setStoreId($ps->getStoreId());
             $category->setChilds($cIds);
             $categories = $categoryMapper->search($category, ['limit' => 5]);
             if (count($categories)) {
                 foreach ($categories as $c) {
                     $data['categories'][] = $c->toStd();
                 }
             }
         }
     }
     return new JsonModel($data);
 }