Commit e3ec491ce7d09d1ed4f28a0f7335ab11cac9ef21

Authored by andryeyev
1 parent b15a9aec

+ Catalog в backend по структуре semena.in.ua

backend/controllers/CatalogController.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace backend\controllers;
  4 +
  5 +use Yii;
  6 +use common\models\Catalog;
  7 +use backend\models\SearchCatalog;
  8 +use yii\web\Controller;
  9 +use yii\web\NotFoundHttpException;
  10 +use yii\filters\VerbFilter;
  11 +use common\models\CatalogLang;
  12 +
  13 +/**
  14 + * CatalogController implements the CRUD actions for Catalog model.
  15 + */
  16 +class CatalogController extends Controller
  17 +{
  18 + public function behaviors()
  19 + {
  20 + return [
  21 + 'verbs' => [
  22 + 'class' => VerbFilter::className(),
  23 + 'actions' => [
  24 + 'delete' => ['post'],
  25 + ],
  26 + ],
  27 + ];
  28 + }
  29 +
  30 + /**
  31 + * Lists all Catalog models.
  32 + * @return mixed
  33 + */
  34 + public function actionIndex()
  35 + {
  36 + $searchModel = new SearchCatalog();
  37 + $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  38 +
  39 + return $this->render('index', [
  40 + 'searchModel' => $searchModel,
  41 + 'dataProvider' => $dataProvider,
  42 + ]);
  43 + }
  44 +
  45 + /**
  46 + * Displays a single Catalog model.
  47 + * @param integer $id
  48 + * @return mixed
  49 + */
  50 + public function actionView($id)
  51 + {
  52 + return $this->render('view', [
  53 + 'model' => $this->findModel($id),
  54 + ]);
  55 + }
  56 +
  57 + /**
  58 + * Creates a new Catalog model.
  59 + * If creation is successful, the browser will be redirected to the 'view' page.
  60 + * @return mixed
  61 + */
  62 + public function actionCreate()
  63 + {
  64 + $model = new Catalog();
  65 +
  66 + if ($model->load(Yii::$app->request->post()) && $model->save())
  67 + {
  68 + return $this->redirect(['view', 'id' => $model->id]);
  69 + }
  70 + else
  71 + {
  72 + return $this->render('create', [
  73 + 'model' => $model,
  74 + ]);
  75 + }
  76 + }
  77 +
  78 + /**
  79 + * Updates an existing Catalog model.
  80 + * If update is successful, the browser will be redirected to the 'view' page.
  81 + * @param integer $id
  82 + * @return mixed
  83 + */
  84 + public function actionUpdate($id)
  85 + {
  86 + $model = $this->findModel($id);
  87 + $model_lang = CatalogLang::find()
  88 + ->where(['catalog' => $id, 'lang_id' => 1])
  89 + ->one();
  90 +//die();
  91 + if ($model->load(Yii::$app->request->post()))
  92 + {
  93 +
  94 + //var_dump($model->title);die();
  95 + echo $model_lang->title = $model->title;
  96 +
  97 + //var_dump($model->save());die();
  98 + $model->save();
  99 + $model_lang->save();
  100 +
  101 + return $this->redirect(['view', 'id' => $model->id]);
  102 + }
  103 + else
  104 + {
  105 + return $this->render('update', [
  106 + 'model' => $model,
  107 + ]);
  108 + }
  109 + }
  110 +
  111 + /**
  112 + * Deletes an existing Catalog model.
  113 + * If deletion is successful, the browser will be redirected to the 'index' page.
  114 + * @param integer $id
  115 + * @return mixed
  116 + */
  117 + public function actionDelete($id)
  118 + {
  119 + $this->findModel($id)->delete();
  120 +
  121 + return $this->redirect(['index']);
  122 + }
  123 +
  124 + /**
  125 + * Finds the Catalog model based on its primary key value.
  126 + * If the model is not found, a 404 HTTP exception will be thrown.
  127 + * @param integer $id
  128 + * @return Catalog the loaded model
  129 + * @throws NotFoundHttpException if the model cannot be found
  130 + */
  131 + protected function findModel($id)
  132 + {
  133 + if (($model = Catalog::findOne($id)) !== null)
  134 + {
  135 + return $model;
  136 + }
  137 + else
  138 + {
  139 + throw new NotFoundHttpException('The requested page does not exist.');
  140 + }
  141 + }
  142 +}
... ...
backend/models/SearchCatalog.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace backend\models;
  4 +
  5 +use Yii;
  6 +use yii\base\Model;
  7 +use yii\data\ActiveDataProvider;
  8 +use common\models\Catalog;
  9 +
  10 +/**
  11 + * SearchCatalog represents the model behind the search form about `common\models\Catalog`.
  12 + */
  13 +class SearchCatalog extends Catalog
  14 +{
  15 + public $title;
  16 +
  17 + /**
  18 + * @inheritdoc
  19 + */
  20 + public function rules()
  21 + {
  22 + return [
  23 + [['id', 'parent_id', 'type', 'subtype', 'status', 'sort'], 'integer'],
  24 + [['cover', 'options'], 'safe'],
  25 + // + поиск по title
  26 + [['title'], 'safe']
  27 + ];
  28 + }
  29 +
  30 + /**
  31 + * @inheritdoc
  32 + */
  33 + public function scenarios()
  34 + {
  35 + // bypass scenarios() implementation in the parent class
  36 + return Model::scenarios();
  37 + }
  38 +
  39 + /**
  40 + * Creates data provider instance with search query applied
  41 + *
  42 + * @param array $params
  43 + *
  44 + * @return ActiveDataProvider
  45 + */
  46 + public function search($params)
  47 + {
  48 + $query = Catalog::find();
  49 +
  50 + $dataProvider = new ActiveDataProvider([
  51 + 'query' => $query,
  52 + ]);
  53 +
  54 + // + поиск по title
  55 + $dataProvider->setSort([
  56 + 'attributes' => [
  57 + 'id',
  58 + 'title' => [
  59 + 'asc' => ['title' => SORT_ASC],
  60 + 'desc' => ['title' => SORT_DESC],
  61 + 'label' => 'title',
  62 + 'default' => SORT_ASC
  63 + ]
  64 + ]
  65 + ]);
  66 +
  67 + $this->load($params);
  68 +
  69 + if (!$this->validate())
  70 + {
  71 + // uncomment the following line if you do not want to return any records when validation fails
  72 + // $query->where('0=1');
  73 +
  74 + // + поиск по title
  75 + $query->joinWith(['catalog_i18n']);
  76 +
  77 + return $dataProvider;
  78 + }
  79 +
  80 + $query->andFilterWhere([
  81 + 'id' => $this->id,
  82 + 'parent_id' => $this->parent_id,
  83 + 'type' => $this->type,
  84 + 'subtype' => $this->subtype,
  85 + 'status' => $this->status,
  86 + 'sort' => $this->sort,
  87 + ]);
  88 +
  89 + $query->andFilterWhere(['like', 'cover', $this->cover])
  90 + ->andFilterWhere(['like', 'options', $this->options]);
  91 +
  92 + // + поиск по title
  93 + $query->joinWith(['relationTable' => function ($q)
  94 + {
  95 + //$q->where("catalog_i18n.title LIKE '%". $this->title . "%'");
  96 + $q->where(['like', 'catalog_i18n.title', $this->title]);
  97 + }]);
  98 +
  99 + return $dataProvider;
  100 + }
  101 +}
... ...
backend/views/catalog/_form.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\helpers\Html;
  4 +use yii\widgets\ActiveForm;
  5 +
  6 +/* @var $this yii\web\View */
  7 +/* @var $model common\models\Catalog */
  8 +/* @var $form yii\widgets\ActiveForm */
  9 +?>
  10 +
  11 +<div class="catalog-form">
  12 +
  13 + <?php $form = ActiveForm::begin(); ?>
  14 +
  15 + <?= $form->field($model, 'title')->textInput() ?>
  16 +
  17 + <?= $form->field($model, 'parent_id')->textInput() ?>
  18 +
  19 + <?= $form->field($model, 'type')->textInput() ?>
  20 +
  21 + <?= $form->field($model, 'subtype')->textInput() ?>
  22 +
  23 + <?= $form->field($model, 'cover')->textInput(['maxlength' => true]) ?>
  24 +
  25 + <?= $form->field($model, 'options')->textInput() ?>
  26 +
  27 + <?= $form->field($model, 'status')->textInput() ?>
  28 +
  29 + <?= $form->field($model, 'sort')->textInput() ?>
  30 +
  31 + <div class="form-group">
  32 + <?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
  33 + </div>
  34 +
  35 + <?php ActiveForm::end(); ?>
  36 +
  37 +</div>
... ...
backend/views/catalog/_search.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\helpers\Html;
  4 +use yii\widgets\ActiveForm;
  5 +
  6 +/* @var $this yii\web\View */
  7 +/* @var $model backend\models\SearchCatalog */
  8 +/* @var $form yii\widgets\ActiveForm */
  9 +?>
  10 +
  11 +<div class="catalog-search">
  12 +
  13 + <?php $form = ActiveForm::begin([
  14 + 'action' => ['index'],
  15 + 'method' => 'get',
  16 + ]); ?>
  17 +
  18 + <?= $form->field($model, 'id') ?>
  19 +
  20 + <?= $form->field($model, 'parent_id') ?>
  21 +
  22 + <?= $form->field($model, 'type') ?>
  23 +
  24 + <?= $form->field($model, 'subtype') ?>
  25 +
  26 + <?= $form->field($model, 'cover') ?>
  27 +
  28 + <?php // echo $form->field($model, 'options') ?>
  29 +
  30 + <?php // echo $form->field($model, 'status') ?>
  31 +
  32 + <?php // echo $form->field($model, 'sort') ?>
  33 +
  34 + <div class="form-group">
  35 + <?= Html::submitButton(Yii::t('app', 'Search'), ['class' => 'btn btn-primary']) ?>
  36 + <?= Html::resetButton(Yii::t('app', 'Reset'), ['class' => 'btn btn-default']) ?>
  37 + </div>
  38 +
  39 + <?php ActiveForm::end(); ?>
  40 +
  41 +</div>
... ...
backend/views/catalog/create.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\helpers\Html;
  4 +
  5 +
  6 +/* @var $this yii\web\View */
  7 +/* @var $model common\models\Catalog */
  8 +
  9 +$this->title = Yii::t('app', 'Create Catalog');
  10 +$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Catalogs'), 'url' => ['index']];
  11 +$this->params['breadcrumbs'][] = $this->title;
  12 +?>
  13 +<div class="catalog-create">
  14 +
  15 + <h1><?= Html::encode($this->title) ?></h1>
  16 +
  17 + <?= $this->render('_form', [
  18 + 'model' => $model,
  19 + ]) ?>
  20 +
  21 +</div>
... ...
backend/views/catalog/index.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\helpers\Html;
  4 +use yii\grid\GridView;
  5 +
  6 +/* @var $this yii\web\View */
  7 +/* @var $searchModel backend\models\SearchCatalog */
  8 +/* @var $dataProvider yii\data\ActiveDataProvider */
  9 +
  10 +$this->title = Yii::t('app', 'Catalogs');
  11 +$this->params['breadcrumbs'][] = $this->title;
  12 +?>
  13 +<div class="catalog-index">
  14 +
  15 + <h1><?= Html::encode($this->title) ?></h1>
  16 + <?php // echo $this->render('_search', ['model' => $searchModel]); ?>
  17 +
  18 + <p>
  19 + <?= Html::a(Yii::t('app', 'Create Catalog'), ['create'], ['class' => 'btn btn-success']) ?>
  20 + </p>
  21 +
  22 + <?= GridView::widget([
  23 + 'dataProvider' => $dataProvider,
  24 + 'filterModel' => $searchModel,
  25 + 'columns' => [
  26 + ['class' => 'yii\grid\SerialColumn'],
  27 +
  28 + 'id',
  29 + 'parent_id',
  30 + 'title',
  31 + 'type',
  32 + 'subtype',
  33 + // 'options',
  34 + // 'status',
  35 + // 'sort',
  36 +
  37 + ['class' => 'yii\grid\ActionColumn'],
  38 + ],
  39 + ]); ?>
  40 +
  41 +</div>
... ...
backend/views/catalog/update.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\helpers\Html;
  4 +
  5 +/* @var $this yii\web\View */
  6 +/* @var $model common\models\Catalog */
  7 +
  8 +$this->title = Yii::t('app', 'Update {modelClass}: ', [
  9 + 'modelClass' => 'Catalog',
  10 +]) . ' ' . $model->id;
  11 +$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Catalogs'), 'url' => ['index']];
  12 +$this->params['breadcrumbs'][] = ['label' => $model->id, 'url' => ['view', 'id' => $model->id]];
  13 +$this->params['breadcrumbs'][] = Yii::t('app', 'Update');
  14 +?>
  15 +<div class="catalog-update">
  16 +
  17 + <h1><?= Html::encode($this->title) ?></h1>
  18 +
  19 + <?= $this->render('_form', [
  20 + 'model' => $model,
  21 + ]) ?>
  22 +
  23 +</div>
... ...
backend/views/catalog/view.php 0 → 100644
  1 +<?php
  2 +
  3 +use yii\helpers\Html;
  4 +use yii\widgets\DetailView;
  5 +
  6 +/* @var $this yii\web\View */
  7 +/* @var $model common\models\Catalog */
  8 +
  9 +$this->title = $model->id;
  10 +$this->params['breadcrumbs'][] = ['label' => Yii::t('app', 'Catalogs'), 'url' => ['index']];
  11 +$this->params['breadcrumbs'][] = $this->title;
  12 +?>
  13 +<div class="catalog-view">
  14 +
  15 + <h1><?= Html::encode($this->title) ?></h1>
  16 +
  17 + <p>
  18 + <?= Html::a(Yii::t('app', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
  19 + <?= Html::a(Yii::t('app', 'Delete'), ['delete', 'id' => $model->id], [
  20 + 'class' => 'btn btn-danger',
  21 + 'data' => [
  22 + 'confirm' => Yii::t('app', 'Are you sure you want to delete this item?'),
  23 + 'method' => 'post',
  24 + ],
  25 + ]) ?>
  26 + </p>
  27 +
  28 + <?= DetailView::widget([
  29 + 'model' => $model,
  30 + 'attributes' => [
  31 + 'id',
  32 + 'parent_id',
  33 + 'type',
  34 + 'subtype',
  35 + 'cover',
  36 + 'options',
  37 + 'status',
  38 + 'sort',
  39 + ],
  40 + ]) ?>
  41 +
  42 +</div>
... ...
common/models/Catalog.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\models;
  4 +
  5 +use Yii;
  6 +
  7 +/**
  8 + * This is the model class for table "catalog".
  9 + *
  10 + * @property integer $id
  11 + * @property integer $parent_id
  12 + * @property integer $type
  13 + * @property integer $subtype
  14 + * @property string $cover
  15 + * @property string $options
  16 + * @property integer $status
  17 + * @property integer $sort
  18 + */
  19 +class Catalog extends \yii\db\ActiveRecord
  20 +{
  21 + /**
  22 + * @inheritdoc
  23 + */
  24 + public static function tableName()
  25 + {
  26 + return 'catalog';
  27 + }
  28 +
  29 + /**
  30 + * @inheritdoc
  31 + */
  32 + public function rules()
  33 + {
  34 + return [
  35 + [['parent_id', 'type', 'subtype', 'status', 'sort'], 'integer'],
  36 + [['options'], 'string'],
  37 + [['cover'], 'string', 'max' => 32],
  38 + ];
  39 + }
  40 +
  41 + /**
  42 + * @inheritdoc
  43 + */
  44 + public function attributeLabels()
  45 + {
  46 + return [
  47 + 'id' => Yii::t('app', 'ID'),
  48 + 'parent_id' => Yii::t('app', 'Parent ID'),
  49 + 'type' => Yii::t('app', 'Type'),
  50 + 'subtype' => Yii::t('app', 'Subtype'),
  51 + 'cover' => Yii::t('app', 'Cover'),
  52 + 'options' => Yii::t('app', 'Options'),
  53 + 'status' => Yii::t('app', 'Status'),
  54 + 'sort' => Yii::t('app', 'Sort'),
  55 + ];
  56 + }
  57 +
  58 + /**
  59 + * @return \yii\db\ActiveQuery
  60 + */
  61 + public function getRelationTable()
  62 + {
  63 + return $this->hasOne(CatalogLang::className(), ['catalog' => 'id']);
  64 + }
  65 +
  66 + public function getTitle()
  67 + {
  68 + return $this->relationTable->title;
  69 + }
  70 +
  71 +}
... ...
common/models/CatalogLang.php 0 → 100644
  1 +<?php
  2 +
  3 +namespace common\models;
  4 +
  5 +use Yii;
  6 +
  7 +/**
  8 + * This is the model class for table "catalog_i18n".
  9 + *
  10 + * @property integer $catalog
  11 + * @property integer $lang_id
  12 + * @property string $title
  13 + * @property string $alias
  14 + * @property string $meta_title
  15 + * @property string $meta_keywords
  16 + * @property string $meta_description
  17 + * @property string $full_alias
  18 + */
  19 +class CatalogLang extends \yii\db\ActiveRecord
  20 +{
  21 + /**
  22 + * @inheritdoc
  23 + */
  24 + public static function tableName()
  25 + {
  26 + return 'catalog_i18n';
  27 + }
  28 +
  29 + /**
  30 + * @inheritdoc
  31 + */
  32 + public function rules()
  33 + {
  34 + return [
  35 + [['catalog', 'lang_id', 'title', 'alias'], 'required'],
  36 + [['catalog', 'lang_id'], 'integer'],
  37 + [['title'], 'string', 'max' => 1024],
  38 + [['alias'], 'string', 'max' => 128],
  39 + [['meta_title', 'meta_keywords', 'meta_description', 'full_alias'], 'string', 'max' => 255],
  40 + [['alias', 'lang_id'], 'unique', 'targetAttribute' => ['alias', 'lang_id'], 'message' => 'The combination of Lang ID and Alias has already been taken.']
  41 + ];
  42 + }
  43 +
  44 + /**
  45 + * @inheritdoc
  46 + */
  47 + public function attributeLabels()
  48 + {
  49 + return [
  50 + 'catalog' => Yii::t('app', 'Catalog'),
  51 + 'lang_id' => Yii::t('app', 'Lang ID'),
  52 + 'title' => Yii::t('app', 'Title'),
  53 + 'alias' => Yii::t('app', 'Alias'),
  54 + 'meta_title' => Yii::t('app', 'Meta Title'),
  55 + 'meta_keywords' => Yii::t('app', 'Meta Keywords'),
  56 + 'meta_description' => Yii::t('app', 'Meta Description'),
  57 + 'full_alias' => Yii::t('app', 'Full Alias'),
  58 + ];
  59 + }
  60 +}
... ...