/** * Search products * @param $data StoreCategory|string * @param string $view */ public function doSearch($data, $view) { $this->query = new StoreProduct(null); $this->query->attachBehaviors($this->query->behaviors()); $this->query->applyAttributes($this->activeAttributes)->active(); if ($data instanceof StoreCategory) { $this->query->applyCategories($this->model); } else { $cr = new CDbCriteria(); $cr->with = array('translate' => array('together' => true)); $cr->addSearchCondition('translate.name', $data); $this->query->getDbCriteria()->mergeWith($cr); } // Filter by manufacturer if (Yii::app()->request->getQuery('manufacturer')) { $manufacturers = explode(';', Yii::app()->request->getParam('manufacturer', '')); $this->query->applyManufacturers($manufacturers); } // Create clone of the current query to use later to get min and max prices. $this->currentQuery = clone $this->query->getDbCriteria(); // Filter products by price range if we have min_price or max_price in request $this->applyPricesFilter(); $per_page = $this->allowedPageLimit[0]; if (isset($_GET['per_page']) && in_array((int) $_GET['per_page'], $this->allowedPageLimit)) { $per_page = (int) $_GET['per_page']; } $this->provider = new CActiveDataProvider($this->query, array('id' => false, 'pagination' => array('pageSize' => $per_page))); $this->provider->sort = StoreProduct::getCSort(); $this->render($view, array('provider' => $this->provider, 'itemView' => isset($_GET['view']) && $_GET['view'] === 'wide' ? '_product_wide' : '_product')); }
/** * Display products by manufacturer * * @param $url * @throws CHttpException */ public function actionIndex($url) { $this->model = StoreManufacturer::model()->findByAttributes(array('url' => $url)); if (!$this->model) { throw new CHttpException(404, Yii::t('StoreModule.core', 'Производитель не найден.')); } $query = new StoreProduct(null); $query->attachBehaviors($query->behaviors()); $query->active(); $query->applyManufacturers($this->model->id); $provider = new CActiveDataProvider($query, array('id' => false, 'pagination' => array('pageSize' => $this->allowedPageLimit[0]))); $this->render('index', array('provider' => $provider)); }
/** * Duplicate one product and return model * * @param StoreProduct $model * @return StoreProduct */ public function duplicateProduct(StoreProduct $model) { $product = new StoreProduct(); $product->attributes = $model->attributes; $behaviors = $model->behaviors(); foreach ($behaviors['STranslateBehavior']['translateAttributes'] as $attr) { $product->{$attr} = $model->{$attr}; } $product->name .= $this->getSuffix(); if ($product->save()) { foreach ($this->duplicate as $feature) { $method_name = 'copy' . ucfirst($feature); if (method_exists($this, $method_name)) { $this->{$method_name}($model, $product); } } return $product; } else { return false; } }
/** * @return array of category manufacturers */ public function getCategoryManufacturers() { $cr = new CDbCriteria(); $cr->select = 't.manufacturer_id, t.id'; $cr->group = 't.manufacturer_id'; $cr->addCondition('t.manufacturer_id IS NOT NULL'); //@todo: Fix manufacturer translation $manufacturers = StoreProduct::model()->active()->applyCategories($this->model, null)->with(array('manufacturer' => array('with' => array('productsCount' => array('scopes' => array('active', 'applyCategories' => array($this->model, null), 'applyAttributes' => array($this->getOwner()->activeAttributes), 'applyMinPrice' => array($this->convertCurrency(Yii::app()->request->getQuery('min_price'))), 'applyMaxPrice' => array($this->convertCurrency(Yii::app()->request->getQuery('max_price')))))))))->findAll($cr); $data = array('title' => Yii::t('StoreModule.core', 'Производитель'), 'selectMany' => true, 'filters' => array()); if ($manufacturers) { foreach ($manufacturers as $m) { $m = $m->manufacturer; if ($m) { $model = new StoreProduct(null); $model->attachBehaviors($model->behaviors()); $model->active()->applyCategories($this->model)->applyMinPrice($this->convertCurrency(Yii::app()->request->getQuery('min_price')))->applyMaxPrice($this->convertCurrency(Yii::app()->request->getQuery('max_price')))->applyAttributes($this->getOwner()->activeAttributes)->applyManufacturers($m->id); $data['filters'][] = array('title' => $m->name, 'count' => $model->count(), 'queryKey' => 'manufacturer', 'queryParam' => $m->id); } } } return $data; }