public function run($id, $title) { $id = (int) $id; $cacheKey = __NAMESPACE__ . __CLASS__ . 'adver.view' . $id; $cache = Yii::$app->getCache(); if (!($model = $cache->get($cacheKey))) { $model = Adver::find()->with(['attachment' => function ($query) { $query->select(['id', 'adver_id', 'name', 'title']); }, 'gallery' => function ($query) { $query->select(['id', 'adver_id', 'name', 'title']); }, 'category' => function ($query) { $query->select(['id', 'name']); }, 'country' => function ($query) { $query->select(['id', 'name']); }, 'province' => function ($query) { $query->select(['id', 'name']); }, 'city' => function ($query) { $query->select(['id', 'name']); }])->where(['id' => $id, 'status' => Adver::STATUS_ACTIVE, 'lang' => ['*', Yii::$app->language]])->asArray()->one(); if (!$model) { throw new \yii\web\NotFoundHttpException(Yii::t('app', 'The requested page does not exist.')); } $model['description'] = HtmlPurifier::process($model['description']); $cache->set($cacheKey, $model, 2592000, new \yii\caching\DbDependency(['sql' => "SELECT [[updated_at]] FROM {{%adver}} WHERE [[id]] = :id AND [[status]] = :status", 'params' => [':id' => $id, ':status' => Adver::STATUS_ACTIVE]])); } return $this->controller->render('view', ['model' => $model]); }
public function search($params) { $query = Adver::find(); $query->joinWith(['category' => function ($query) { $query->from(['c' => 'categories']); }, 'country' => function ($query) { $query->from(['co' => 'country']); }, 'province' => function ($query) { $query->from(['p' => 'province']); }, 'city' => function ($query) { $query->from(['ci' => 'city']); }]); $query->from(['a' => $this->tableName()]); $query->andWhere(['a.user_id' => Yii::$app->getUser()->id]); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['category.name'] = ['asc' => ['c.name' => SORT_ASC], 'desc' => ['c.name' => SORT_DESC]]; $dataProvider->sort->attributes['country.name'] = ['asc' => ['co.name' => SORT_ASC], 'desc' => ['co.name' => SORT_DESC]]; $dataProvider->sort->attributes['province.name'] = ['asc' => ['p.name' => SORT_ASC], 'desc' => ['p.name' => SORT_DESC]]; $dataProvider->sort->attributes['city.name'] = ['asc' => ['ci.name' => SORT_ASC], 'desc' => ['ci.name' => SORT_DESC]]; $dataProvider->sort->defaultOrder = ['status' => SORT_ASC, 'id' => SORT_DESC]; // load the seach form data and validate if (!($this->load($params) && $this->validate())) { return $dataProvider; } // adjust the query by adding the filters $query->andFilterWhere(['a.id' => $this->id]); $query->andFilterWhere(['like', 'a.title', $this->title])->andFilterWhere(['a.status' => $this->status])->andFilterWhere(['like', 'a.lang', $this->lang]); $query->andFilterWhere(['LIKE', 'c.name', $this->getAttribute('category.name')]); $query->andFilterWhere(['LIKE', 'co.name', $this->getAttribute('country.name')]); $query->andFilterWhere(['LIKE', 'p.name', $this->getAttribute('province.name')]); $query->andFilterWhere(['LIKE', 'ci.name', $this->getAttribute('city.name')]); return $dataProvider; }
public function run($ids) { $cacheKey = __NAMESPACE__ . __CLASS__ . 'infowindow' . $ids; $ids = explode('-', $ids); $makeCacheParam = []; foreach ($ids as $id) { $makeCacheParam[':p' . $id] = $id; } $makeCacheParam[':status'] = Adver::STATUS_ACTIVE; $cacheWhere = implode(',', array_keys($makeCacheParam)); $cache = Yii::$app->getCache(); if (!($model = $cache->get($cacheKey))) { $model = Adver::find()->select(['id', 'category_id', 'country_id', 'province_id', 'city_id', 'city_id', 'user_id', 'title', 'address', 'lang'])->with(['gallery' => function ($query) { $query->select(['id', 'adver_id', 'name', 'title']); }, 'category' => function ($query) { $query->select(['id', 'name']); }, 'country' => function ($query) { $query->select(['id', 'name']); }, 'province' => function ($query) { $query->select(['id', 'name']); }, 'city' => function ($query) { $query->select(['id', 'name']); }])->andWhere(['id' => $ids])->andWhere(['status' => Adver::STATUS_ACTIVE])->asArray()->all(); $cache->set($cacheKey, $model, 2592000, new \yii\caching\DbDependency(['sql' => "SELECT MAX([[updated_at]]) FROM {{%adver}} WHERE [[id]] IN ({$cacheWhere})", 'params' => $makeCacheParam])); } $newModel = []; foreach ($model as $key => $value) { $address = ''; if (isset($model[$key]['country']['name']) && $model[$key]['country']['name'] != '') { $address .= $model[$key]['country']['name'] . ', '; } if (isset($model[$key]['province']['name']) && $model[$key]['province']['name'] != '') { $address .= $model[$key]['province']['name'] . ', '; } if (isset($model[$key]['city']['name']) && $model[$key]['city']['name'] != '') { $address .= $model[$key]['city']['name'] . ', '; } $address = rtrim($address, ', '); $newModel[$key]['full_address'] = Html::encode($address); $newModel[$key]['address'] = Html::encode($model[$key]['address']); $newModel[$key]['title'] = Html::encode($model[$key]['title']); $newModel[$key]['category'] = Html::encode($model[$key]['category']['name']); $newModel[$key]['url'] = Adver::generateLink($model[$key]['id'], $model[$key]['title'], $model[$key]['category']['name'], $model[$key]['country']['name'], $model[$key]['province']['name'], $model[$key]['city']['name'], $model[$key]['address'], $model[$key]['lang']); $newGallery = []; foreach ($value['gallery'] as $gallery) { $newGallery[] = ['url' => Gallery::getImageUrlFromOutside($gallery['name'], $gallery['adver_id'], 160, 105), 'title' => Html::encode($gallery['title']), 'adver_id' => $gallery['adver_id']]; } $newModel[$key]['gallery'] = $newGallery; } return Json::encode($newModel); }
public function getModel($limit = null, $cat = 0) { $advers = Adver::find()->with(['gallery' => function ($query) { $query->select(['id', 'adver_id', 'name']); }, 'category' => function ($query) { $query->select(['id', 'name']); }, 'country' => function ($query) { $query->select(['id', 'name']); }, 'province' => function ($query) { $query->select(['id', 'name']); }, 'city' => function ($query) { $query->select(['id', 'name']); }])->where(['status' => Adver::STATUS_ACTIVE, 'lang' => ['*', Yii::$app->language]])->orderBy(['id' => SORT_DESC])->limit($limit); if ($cat > 0) { $advers->andWhere(['category_id' => $cat]); } return $advers->asArray()->all(); }
public function search($params) { $query = Adver::find()->with(['gallery' => function ($query) { $query->select(['id', 'adver_id', 'name', 'title']); }, 'category' => function ($query) { $query->select(['id', 'name']); }, 'country' => function ($query) { $query->select(['id', 'name']); }, 'province' => function ($query) { $query->select(['id', 'name']); }, 'city' => function ($query) { $query->select(['id', 'name']); }])->where(['status' => Adver::STATUS_ACTIVE, 'lang' => ['*', Yii::$app->language]]); $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => array('pageSize' => 10)]); /*$dataProvider->setSort([ 'attributes' => [ 'id' => [ 'asc' => ['id' => SORT_ASC], 'desc' => ['id' => SORT_DESC], 'label' => Yii::t('app', 'ID'), //'default' => SORT_ASC ], ] ]);*/ // load the seach form data and validate if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['category_id' => $this->category_id]); $query->andFilterWhere(['country_id' => $this->country_id]); $query->andFilterWhere(['province_id' => $this->province_id]); $query->andFilterWhere(['city_id' => $this->city_id]); $query->andFilterWhere(['title' => $this->title]); $query->andFilterWhere(['address' => $this->address]); return $dataProvider; }
public function actionIndex() { return new ActiveDataProvider(['query' => Adver::find()]); }