/** * @inheritdoc */ public function init() { parent::init(); if ($this->key) { $model = Content::findOne(['key' => $this->key]); if (null !== $model) { echo $model->content; } else { return null; // throw new InvalidValueException('Content not found'); } } else { throw new InvalidParamException('Invalid content key'); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Content::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->setSort(['attributes' => ['id', 'status', 'key', 'created_at', 'updated_at', 'name' => ['asc' => ['name' => SORT_ASC], 'desc' => ['name' => SORT_DESC]]]]); $this->load($params); if (!$this->validate()) { $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status' => $this->status]); if ($this->createdRange) { $dateArray = explode('-', $this->createdRange); $dateFrom = trim($dateArray[0]); $dateTo = trim($dateArray[1]); $query->andWhere('created_at <= :dateTo', [':dateTo' => $dateTo])->andWhere('created_at >= :dateFrom', [':dateFrom' => $dateFrom]); } else { $query->andFilterWhere(['created_at' => $this->created_at]); } if ($this->updatedRange) { $dateArray = explode('-', $this->updatedRange); $dateFrom = trim($dateArray[0]); $dateTo = trim($dateArray[1]); $query->andWhere('updated_at <= :dateTo', [':dateTo' => $dateTo])->andWhere('updated_at >= :dateFrom', [':dateFrom' => $dateFrom]); } else { $query->andFilterWhere(['updated_at' => $this->updated_at]); } $query->andFilterWhere(['like', 'key', $this->key]); $query->joinWith(['translations' => function ($query) { if ($this->name) { $query->andWhere(['like', 'name', $this->name]); } if ($this->content) { $query->andWhere(['like', 'content', $this->content]); } }]); return $dataProvider; }
<?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'key')->textInput(['maxlength' => true]); ?> <?php foreach (Yii::$app->urlManager->languages as $language => $details) { echo $form->field($model->translate($language), "[{$language}]name")->textInput(); echo $form->field($model->translate($language), "[{$language}]content")->widget(Redactor::classname(), ['clientOptions' => ['plugins' => ['clips', 'fontcolor', 'imagemanager', 'fullscreen'], 'replaceDivs' => false]]); } ?> <?php echo $form->field($model, 'status')->dropDownList(Content::getStatuses()); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? Yii::t('content-manager', 'Create') : Yii::t('content-manager', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>
/** * Finds the Content model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Content the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Content::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
<h2><?php echo Html::encode($this->title); ?> </h2> <p> <?php echo Html::a(Yii::t('content-manager', 'Add new content block'), ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'pager' => ['firstPageLabel' => true, 'lastPageLabel' => true], 'rowOptions' => function ($model, $key, $index, $grid) { return ['id' => $model['id'], 'style' => 'text-align: center;']; }, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'key', 'name', ['attribute' => 'status', 'format' => 'html', 'value' => function ($data) { if ($data->status === Content::STATUS_BLOCKED) { return Html::tag('span', $data->StatusName, ['class' => 'label label-danger']); } if ($data->status === Content::STATUS_ACTIVE) { return Html::tag('span', $data->StatusName, ['class' => 'label label-success']); } return $data->StatusName; }, 'filter' => Content::getStatuses()], ['attribute' => 'created_at', 'format' => ['date', 'php:d.m.Y H:i:s'], 'filter' => DateRangePicker::widget(['useWithAddon' => true, 'presetDropdown' => true, 'hideInput' => true, 'model' => $searchModel, 'attribute' => 'createdRange', 'convertFormat' => true, 'pluginOptions' => ['timePicker' => true, 'timePickerIncrement' => 1, 'format' => 'U', 'opens' => 'left'], 'containerTemplate' => '<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i> </span><span class="form-control text-right"><span class="pull-left"> <span class="range-value" style="display: none;">{value}</span></span><b class="caret"></b> {input}</span>']), 'options' => ['class' => 'col-sm-1']], ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div>