/** * Creates a new WebTag model. * * @return mixed */ public function actionCreate() { $model = new WebTag(); $baseModel = null; if (Yii::$app->request->isPost) { $post = Yii::$app->request->post(); $transaction = Yii::$app->db->beginTransaction(); try { // Mise à jour du WebTag et du BaseTag associé if (!($baseModel = BaseTag::findOne($post['WebTag']['base_id']))) { // Création du BaseTag à la volée & mise à jour de base_id dans $post if ($post['BaseTag']['code']) { $baseModel = new BaseTag(); if (!($baseModel->load($post) && $baseModel->save())) { throw new Exception("Erreur à la création du BaseTag"); } $post['WebTag']['base_id'] = $baseModel->id; } else { $msg = "Il faut choisir ou créer un BaseTag"; Yii::$app->session->addFlash('flash-danger', $msg); throw new Exception($msg); } } if (!($model->load($post) && $model->save())) { throw new Exception("Erreur à la création du WebTag"); } $transaction->commit(); return $this->redirectAfterCreateSuccess($model); } catch (Exception $x) { $transaction->rollBack(); Yii::$app->session->addFlash('flash-warning', HLib::t('messages', 'There are errors in your form')); } } if (!$baseModel) { $baseModel = new BaseTag(); } // Affichage initial ou ré-affichage en cas d'erreur $baseTags = BaseTag::find()->orderBy('code')->all(); $languages = Language::find()->orderBy('iso_639_code')->all(); return $this->render('create', compact('model', 'baseTags', 'languages', 'baseModel')); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = WebTag::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'base_id' => $this->base_id, 'language_id' => $this->language_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'label', $this->label]); return $dataProvider; }
/** * @inheritdoc */ public function displayActiveFilters($sep = ' - ') { $filters = []; if ($this->title) { $filters[] = Yii::t('labels', '(title)') . ' ' . $this->title; } if ($this->body) { $filters[] = Yii::t('labels', '(text)') . ' ' . $this->body; } if ($this->tagId) { /** @var WebTag $tag */ $tag = WebTag::findOne($this->tagId); $filters[] = $tag ? $tag->label : '???'; } return implode($sep, $filters); }
/** * @return \yii\db\ActiveQuery */ public function getWebTags() { return $this->hasMany(WebTag::className(), ['base_id' => 'id']); }
/** * Renvoie la liste des tags associés à $this * * @param bool $useCache utilise un cache interne. false => on relance la requête à chaque fois. Vaut true par défaut. * @return array [WebTag] */ public function getTags($useCache = true) { if (!$useCache || is_null($this->tags)) { $this->tags = WebTag::find()->forWebNews($this->id)->addOrderBy('web_tags.label ASC')->all(); } return $this->tags; }
/** * Affichage de la liste des objets, avec ou sans filtre selon le contenu de la session * (frontend) * * @param int $page Numéro de la page courante (commence à 1) * @return mixed */ public function actionDisplaySearchResults($page) { // Récupération d'une liste éventuellement filtrée selon les critères du moteur de recherche $searchModel = new WebNewsSearch(); $advancedSearchFilters = $searchModel->retrieveFiltersFromSession(); $dataProvider = $searchModel->search($advancedSearchFilters); // Détermination de l'ordre de tri $sortClausesSessionKey = WebNews::class . '.display-search-results.sort'; $dataProvider->query = ListSorter::updateQuery($dataProvider->query, $sortClausesSessionKey); Yii::$app->session->set(WebNews::class . '.index.page', $page); $dataProvider->pagination->page = --$page; /** @var HCms $module */ $module = $this->module; if (isset($module->frontendLayout)) { $this->layout = $module->frontendLayout; } /** @noinspection PhpUndefinedMethodInspection */ return $this->render('displaySearchResults', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'tags' => ArrayHelper::map(WebTag::find()->byLanguageCode(h::getIso639Code())->orderByLabel()->all(), 'id', 'label'), 'sortClausesSessionKey' => $sortClausesSessionKey]); }