Commit 8e5dab8a by dianc

Theme CRUD

parent 78b74de1
<?php
namespace backend\modules\theme\controllers;
use Yii;
use common\models\Theme;
use backend\modules\theme\models\search\ThemeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* ThemeController implements the CRUD actions for Theme model.
*/
class ThemeController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['post'],
],
],
];
}
/**
* Lists all Theme models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new ThemeSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Theme model.
* @param integer $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Theme model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Theme();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
/**
* Updates an existing Theme model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
/**
* Deletes an existing Theme model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
*/
public function actionDelete($id)
{
$this->findModel($id)->delete();
return $this->redirect(['index']);
}
/**
* Finds the Theme model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Theme the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Theme::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
<?php
namespace backend\modules\theme\models\search;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Theme;
/**
* ThemeSearch represents the model behind the search form about `common\models\Theme`.
*/
class ThemeSearch extends Theme
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'category_id', 'status', 'created_at', 'updated_at'], 'integer'],
[['code', 'slug', 'name', 'description'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Theme::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'category_id' => $this->category_id,
'status' => $this->status,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'code', $this->code])
->andFilterWhere(['like', 'slug', $this->slug])
->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'description', $this->description]);
return $dataProvider;
}
}
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\models\Theme */
/* @var $form yii\bootstrap\ActiveForm */
?>
<div class="theme-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->errorSummary($model); ?>
<?= $form->field($model, 'category_id')->textInput() ?>
<?= $form->field($model, 'code')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'slug')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'status')->textInput() ?>
<?= $form->field($model, 'created_at')->textInput() ?>
<?= $form->field($model, 'updated_at')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('backend', 'Create') : Yii::t('backend', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
/* @var $this yii\web\View */
/* @var $model backend\modules\theme\models\search\ThemeSearch */
/* @var $form yii\bootstrap\ActiveForm */
?>
<div class="theme-search">
<?php $form = ActiveForm::begin([
'action' => ['index'],
'method' => 'get',
]); ?>
<?= $form->field($model, 'id') ?>
<?= $form->field($model, 'category_id') ?>
<?= $form->field($model, 'code') ?>
<?= $form->field($model, 'slug') ?>
<?= $form->field($model, 'name') ?>
<?php // echo $form->field($model, 'description') ?>
<?php // echo $form->field($model, 'status') ?>
<?php // echo $form->field($model, 'created_at') ?>
<?php // echo $form->field($model, 'updated_at') ?>
<div class="form-group">
<?= Html::submitButton(Yii::t('backend', 'Search'), ['class' => 'btn btn-primary']) ?>
<?= Html::resetButton(Yii::t('backend', 'Reset'), ['class' => 'btn btn-default']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Theme */
$this->title = Yii::t('backend', 'Create {modelClass}', [
'modelClass' => 'Theme',
]);
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Themes'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="theme-create">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\grid\GridView;
/* @var $this yii\web\View */
/* @var $searchModel backend\modules\theme\models\search\ThemeSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
$this->title = Yii::t('backend', 'Themes');
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="theme-index">
<?php // echo $this->render('_search', ['model' => $searchModel]); ?>
<p>
<?= Html::a(Yii::t('backend', 'Create {modelClass}', [
'modelClass' => 'Theme',
]), ['create'], ['class' => 'btn btn-success']) ?>
</p>
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
'category_id',
'code',
'slug',
'name',
// 'description',
// 'status',
// 'created_at',
// 'updated_at',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
</div>
<?php
use yii\helpers\Html;
/* @var $this yii\web\View */
/* @var $model common\models\Theme */
$this->title = Yii::t('backend', 'Update {modelClass}: ', [
'modelClass' => 'Theme',
]) . ' ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Themes'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = Yii::t('backend', 'Update');
?>
<div class="theme-update">
<?= $this->render('_form', [
'model' => $model,
]) ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\DetailView;
/* @var $this yii\web\View */
/* @var $model common\models\Theme */
$this->title = $model->name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Themes'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="theme-view">
<p>
<?= Html::a(Yii::t('backend', 'Update'), ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
<?= Html::a(Yii::t('backend', 'Delete'), ['delete', 'id' => $model->id], [
'class' => 'btn btn-danger',
'data' => [
'confirm' => Yii::t('backend', 'Are you sure you want to delete this item?'),
'method' => 'post',
],
]) ?>
</p>
<?= DetailView::widget([
'model' => $model,
'attributes' => [
'id',
'category_id',
'code',
'slug',
'name',
'description',
'status',
'created_at',
'updated_at',
],
]) ?>
</div>
<?php
use yii\db\Migration;
class m160903_101233_theme extends Migration
{
// Use safeUp/safeDown to run migration code within a transaction
public function safeUp()
{
$tableOptions = null;
if ($this->db->driverName === 'mysql') {
$tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB';
}
$this->createTable('{{%theme}}', [
'id' => $this->primaryKey(),
'category_id' => $this->integer()->notNull(),
'code' => $this->string(32)->notNull(),
'slug' => $this->string(32)->notNull(),
'name' => $this->string(32)->notNull(),
'description' => $this->string(512),
'status' => $this->smallInteger()->notNull(),//->defaultValue(Category::STATUS_ACTIVE),
'created_at' => $this->integer(),
'updated_at' => $this->integer()
], $tableOptions);
$this->createTable('{{%theme_files}}', [
'id' => $this->primaryKey(),
'theme_id' => $this->integer()->notNull(),
'file_id' => $this->string(32)->notNull(),
'base_url' => $this->string(1024),
'base_path' => $this->string(1024),
'created_at' => $this->integer(),
'updated_at' => $this->integer()
], $tableOptions);
$this->addForeignKey('fk_theme_category', '{{%theme}}', 'category_id', '{{%category}}', 'id', 'cascade', 'cascade');
$this->addForeignKey('fk_theme_files_theme', '{{%theme_files}}', 'theme_id', '{{%theme}}', 'id', 'cascade', 'cascade');
}
public function safeDown()
{
$this->dropForeignKey('fk_theme_files_theme', '{{%theme_files}}');
$this->dropForeignKey('fk_theme_category', '{{%theme}}');
$this->dropTable('{{%theme_files}}');
$this->dropTable('{{%theme}}');
}
}
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "{{%theme}}".
*
* @property integer $id
* @property integer $category_id
* @property string $code
* @property string $slug
* @property string $name
* @property string $description
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
*
* @property Category $category
* @property ThemeFiles[] $themeFiles
*/
class Theme extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%theme}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['category_id', 'code', 'slug', 'name', 'status'], 'required'],
[['category_id', 'status', 'created_at', 'updated_at'], 'integer'],
[['code', 'slug', 'name'], 'string', 'max' => 32],
[['description'], 'string', 'max' => 512],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'category_id' => Yii::t('app', 'Category ID'),
'code' => Yii::t('app', 'Code'),
'slug' => Yii::t('app', 'Slug'),
'name' => Yii::t('app', 'Name'),
'description' => Yii::t('app', 'Description'),
'status' => Yii::t('app', 'Status'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getCategory()
{
return $this->hasOne(Category::className(), ['id' => 'category_id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getThemeFiles()
{
return $this->hasMany(ThemeFiles::className(), ['theme_id' => 'id']);
}
/**
* @inheritdoc
* @return \common\models\query\ThemeQuery the active query used by this AR class.
*/
public static function find()
{
return new \common\models\query\ThemeQuery(get_called_class());
}
}
<?php
namespace common\models;
use Yii;
/**
* This is the model class for table "{{%theme_files}}".
*
* @property integer $id
* @property integer $theme_id
* @property string $file_id
* @property string $base_url
* @property string $base_path
* @property integer $created_at
* @property integer $updated_at
*
* @property Theme $theme
*/
class ThemeFiles extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%theme_files}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['theme_id', 'file_id'], 'required'],
[['theme_id', 'created_at', 'updated_at'], 'integer'],
[['file_id'], 'string', 'max' => 32],
[['base_url', 'base_path'], 'string', 'max' => 1024],
[['theme_id'], 'exist', 'skipOnError' => true, 'targetClass' => Theme::className(), 'targetAttribute' => ['theme_id' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'theme_id' => Yii::t('app', 'Theme ID'),
'file_id' => Yii::t('app', 'File ID'),
'base_url' => Yii::t('app', 'Base Url'),
'base_path' => Yii::t('app', 'Base Path'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTheme()
{
return $this->hasOne(Theme::className(), ['id' => 'theme_id']);
}
/**
* @inheritdoc
* @return \common\models\query\ThemeFilesQuery the active query used by this AR class.
*/
public static function find()
{
return new \common\models\query\ThemeFilesQuery(get_called_class());
}
}
<?php
namespace common\models\query;
/**
* This is the ActiveQuery class for [[\common\models\ThemeFiles]].
*
* @see \common\models\ThemeFiles
*/
class ThemeFilesQuery extends \yii\db\ActiveQuery
{
/*public function active()
{
return $this->andWhere('[[status]]=1');
}*/
/**
* @inheritdoc
* @return \common\models\ThemeFiles[]|array
*/
public function all($db = null)
{
return parent::all($db);
}
/**
* @inheritdoc
* @return \common\models\ThemeFiles|array|null
*/
public function one($db = null)
{
return parent::one($db);
}
}
<?php
namespace common\models\query;
/**
* This is the ActiveQuery class for [[\common\models\Theme]].
*
* @see \common\models\Theme
*/
class ThemeQuery extends \yii\db\ActiveQuery
{
/*public function active()
{
return $this->andWhere('[[status]]=1');
}*/
/**
* @inheritdoc
* @return \common\models\Theme[]|array
*/
public function all($db = null)
{
return parent::all($db);
}
/**
* @inheritdoc
* @return \common\models\Theme|array|null
*/
public function one($db = null)
{
return parent::one($db);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment