diff --git a/generators/crud/Generator.php b/generators/crud/Generator.php new file mode 100644 index 0000000..dde2dbc --- /dev/null +++ b/generators/crud/Generator.php @@ -0,0 +1,90 @@ + + * @package omcrn\gii\generators\crud + */ +class Generator extends \yii\gii\generators\crud\Generator +{ + public $possibleCreatedAtAttributes = ['create_date', 'created_at']; + public $possibleUpdatedAtAttributes = ['update_date', 'updated_at']; + + public function init() + { + $timestampBehavior = new TimestampBehavior(); + if (!in_array($timestampBehavior->createdAtAttribute, $this->possibleCreatedAtAttributes)){ + $this->possibleCreatedAtAttributes[] = $timestampBehavior->createdAtAttribute; + } + if (!in_array($timestampBehavior->updatedAtAttribute, $this->possibleUpdatedAtAttributes)){ + $this->possibleUpdatedAtAttributes[] = $timestampBehavior->updatedAtAttribute; + } + parent::init(); + } + + /** + * + * + * @author Zura Sekhniashvili + * @return \yii\db\ColumnSchema[] + */ + public function getColumns() + { + return $this->tableSchema->columns; + } + + public function getColumnsForGeneration() + { + $columns = []; + $table = $this->getTableSchema(); + foreach ($this->getColumns() as $attribute => $column) { +// \centigen\base\helpers\UtilHelper::vardump($column); + if ((in_array($attribute, $this->possibleCreatedAtAttributes) || in_array($attribute, $this->possibleUpdatedAtAttributes)) + && Column::isUnixTimestampColumn($table, $attribute)){ + continue; + } + $columns[$attribute] = $column; + } + return $columns; + } + + public function generateActiveField($attribute) + { + $tableSchema = $this->getTableSchema(); + if ($tableSchema && isset($tableSchema->columns[$attribute])){ + $column = $tableSchema->columns[$attribute]; + if ($column->type === 'datetime' || $column->type === 'timestamp'){ + return "\$form->field(\$model, '$attribute')->widget('trntv\yii\datetime\DateTimeWidget', [ + 'momentDatetimeFormat' => Yii::\$app->formatter->getMomentDatetimeFormat() ?: 'yyyy-MM-dd\'T\'HH:mm:ssZZZZZ', + ]) "; + } else if (in_array($attribute, ['status', 'is_active'])){ + return "\$form->field(\$model, '$attribute')->checkbox()"; + } + } + return parent::generateActiveField($attribute); + } + + /** + * @inheritdoc + */ + public function generateColumnFormat($columnSchema) + { + if ($columnSchema->type === 'datetime' || $columnSchema->type === 'timestamp' || + ((in_array($columnSchema->name, $this->possibleCreatedAtAttributes) || in_array($columnSchema->name, $this->possibleUpdatedAtAttributes)) + && Column::isUnixTimestampColumn($columnSchema))){ + return 'datetime'; + } + return parent::generateColumnFormat($columnSchema); + } + +} diff --git a/generators/crud/default/controller.php b/generators/crud/default/controller.php new file mode 100644 index 0000000..89f1eed --- /dev/null +++ b/generators/crud/default/controller.php @@ -0,0 +1,176 @@ +controllerClass); +$modelClass = StringHelper::basename($generator->modelClass); +$searchModelClass = StringHelper::basename($generator->searchModelClass); +if ($modelClass === $searchModelClass) { + $searchModelAlias = $searchModelClass . 'Search'; +} + +/* @var $class ActiveRecordInterface */ +$class = $generator->modelClass; +$pks = $class::primaryKey(); +$urlParams = $generator->generateUrlParams(); +$actionParams = $generator->generateActionParams(); +$actionParamComments = $generator->generateActionParamComments(); + +echo " + +namespace controllerClass, '\\')) ?>; + +use Yii; +use modelClass, '\\') ?>; +searchModelClass)): ?> +use searchModelClass, '\\') . (isset($searchModelAlias) ? " as $searchModelAlias" : "") ?>; + +use yii\data\ActiveDataProvider; + +use baseControllerClass, '\\') ?>; +use yii\web\NotFoundHttpException; +use yii\filters\VerbFilter; + +/** + * implements the CRUD actions for model. + */ +class extends baseControllerClass) . "\n" ?> +{ + /** + * @inheritdoc + */ + public function behaviors() + { + return [ + 'verbs' => [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['POST'], + ], + ], + ]; + } + + /** + * Lists all models. + * @return mixed + */ + public function actionIndex() + { +searchModelClass)): ?> + $searchModel = new (); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + + $dataProvider = new ActiveDataProvider([ + 'query' => ::find(), + ]); + + return $this->render('index', [ + 'dataProvider' => $dataProvider, + ]); + + } + + /** + * Displays a single model. + * + * @return mixed + */ + public function actionView() + { + return $this->render('view', [ + 'model' => $this->findModel(), + ]); + } + + /** + * Creates a new model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new (); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', ]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing model. + * If update is successful, the browser will be redirected to the 'view' page. + * + * @return mixed + */ + public function actionUpdate() + { + $model = $this->findModel(); + + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', ]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + /** + * Deletes an existing model. + * If deletion is successful, the browser will be redirected to the 'index' page. + * + * @return mixed + */ + public function actionDelete() + { + $this->findModel()->delete(); + + return $this->redirect(['index']); + } + + /** + * Finds the model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * + * @return the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel() + { + \$$pk"; + } + $condition = '[' . implode(', ', $condition) . ']'; +} +?> + if (($model = ::findOne()) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/generators/crud/default/search.php b/generators/crud/default/search.php new file mode 100644 index 0000000..88edbb1 --- /dev/null +++ b/generators/crud/default/search.php @@ -0,0 +1,87 @@ +modelClass); +$searchModelClass = StringHelper::basename($generator->searchModelClass); +if ($modelClass === $searchModelClass) { + $modelAlias = $modelClass . 'Model'; +} +$rules = $generator->generateSearchRules(); +$labels = $generator->generateSearchLabels(); +$searchAttributes = $generator->getSearchAttributes(); +$searchConditions = $generator->generateSearchConditions(); + +echo " + +namespace searchModelClass, '\\')) ?>; + +use Yii; +use yii\base\Model; +use yii\data\ActiveDataProvider; +use modelClass, '\\') . (isset($modelAlias) ? " as $modelAlias" : "") ?>; + +/** + * represents the model behind the search form about `modelClass ?>`. + */ +class extends + +{ + /** + * @inheritdoc + */ + public function rules() + { + return [ + , + ]; + } + + /** + * @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 = ::find(); + + // add conditions that should always apply here + + $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; + } + + // grid filtering conditions + + + return $dataProvider; + } +} diff --git a/generators/crud/default/views/_form.php b/generators/crud/default/views/_form.php new file mode 100644 index 0000000..874e474 --- /dev/null +++ b/generators/crud/default/views/_form.php @@ -0,0 +1,46 @@ +modelClass(); +$safeAttributes = $model->safeAttributes(); +if (empty($safeAttributes)) { + $safeAttributes = $model->attributes(); +} + +echo " + +use yii\helpers\Html; +use yii\widgets\ActiveForm; + +/* @var $this yii\web\View */ +/* @var $model modelClass, '\\') ?> */ +/* @var $form yii\widgets\ActiveForm */ +?> + +
+ + $form = ActiveForm::begin(); ?> + + +getColumnNames()); ?> +getColumns()); exit; ?> +getColumnsForGeneration() as $attribute => $column) { + /** @var $column \yii\db\ColumnSchema */ + if (in_array($attribute, $safeAttributes)) { + echo " generateActiveField($attribute) . " ?>\n\n"; + } +} ?> +
+ Html::submitButton($model->isNewRecord ? generateString('Create') ?> : generateString('Update') ?>, ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + ActiveForm::end(); ?> + +
diff --git a/generators/crud/default/views/_search.php b/generators/crud/default/views/_search.php new file mode 100644 index 0000000..fc45d2e --- /dev/null +++ b/generators/crud/default/views/_search.php @@ -0,0 +1,44 @@ + + +use yii\helpers\Html; +use yii\widgets\ActiveForm; + +/* @var $this yii\web\View */ +/* @var $model searchModelClass, '\\') ?> */ +/* @var $form yii\widgets\ActiveForm */ +?> + + diff --git a/generators/crud/default/views/create.php b/generators/crud/default/views/create.php new file mode 100644 index 0000000..10b00b9 --- /dev/null +++ b/generators/crud/default/views/create.php @@ -0,0 +1,30 @@ + + +use yii\helpers\Html; + + +/* @var $this yii\web\View */ +/* @var $model modelClass, '\\') ?> */ + +$this->title = generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>; +$this->params['breadcrumbs'][] = ['label' => generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

Html::encode($this->title) ?>

+ + $this->render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/generators/crud/default/views/index.php b/generators/crud/default/views/index.php new file mode 100644 index 0000000..f3ee8aa --- /dev/null +++ b/generators/crud/default/views/index.php @@ -0,0 +1,78 @@ +generateUrlParams(); +$nameAttribute = $generator->getNameAttribute(); + +echo " + +use yii\helpers\Html; +use indexWidgetType === 'grid' ? "yii\\grid\\GridView" : "yii\\widgets\\ListView" ?>; +enablePjax ? 'use yii\widgets\Pjax;' : '' ?> + +/* @var $this yii\web\View */ +searchModelClass) ? "/* @var \$searchModel " . ltrim($generator->searchModelClass, '\\') . " */\n" : '' ?> +/* @var $dataProvider yii\data\ActiveDataProvider */ + +$this->title = generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

Html::encode($this->title) ?>

+searchModelClass)): ?> +indexWidgetType === 'grid' ? "// " : "") ?>echo $this->render('_search', ['model' => $searchModel]); ?> + + +

+ Html::a(generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>, ['create'], ['class' => 'btn btn-success']) ?> +

+enablePjax ? '' : '' ?> +indexWidgetType === 'grid'): ?> + GridView::widget([ + 'dataProvider' => $dataProvider, + searchModelClass) ? "'filterModel' => \$searchModel,\n 'columns' => [\n" : "'columns' => [\n"; ?> + ['class' => 'yii\grid\SerialColumn'], + +getTableSchema()) === false) { + foreach ($generator->getColumnNames() as $name) { + if (++$count < 6) { + echo " '" . $name . "',\n"; + } else { + echo " // '" . $name . "',\n"; + } + } +} else { + foreach ($tableSchema->columns as $column) { + $format = $generator->generateColumnFormat($column); + if (++$count < 6) { + echo " '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; + } else { + echo " // '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; + } + } +} +?> + + ['class' => 'yii\grid\ActionColumn'], + ], + ]); ?> + + ListView::widget([ + 'dataProvider' => $dataProvider, + 'itemOptions' => ['class' => 'item'], + 'itemView' => function ($model, $key, $index, $widget) { + return Html::a(Html::encode($model->), ['view', ]); + }, + ]) ?> + +enablePjax ? '' : '' ?> +
diff --git a/generators/crud/default/views/update.php b/generators/crud/default/views/update.php new file mode 100644 index 0000000..ce12cd6 --- /dev/null +++ b/generators/crud/default/views/update.php @@ -0,0 +1,32 @@ +generateUrlParams(); + +echo " + +use yii\helpers\Html; + +/* @var $this yii\web\View */ +/* @var $model modelClass, '\\') ?> */ + +$this->title = generateString('Update {modelClass}: ', ['modelClass' => Inflector::camel2words(StringHelper::basename($generator->modelClass))]) ?> . $model->getNameAttribute() ?>; +$this->params['breadcrumbs'][] = ['label' => generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->getNameAttribute() ?>, 'url' => ['view', ]]; +$this->params['breadcrumbs'][] = generateString('Update') ?>; +?> +
+ +

Html::encode($this->title) ?>

+ + $this->render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/generators/crud/default/views/view.php b/generators/crud/default/views/view.php new file mode 100644 index 0000000..00ee1d9 --- /dev/null +++ b/generators/crud/default/views/view.php @@ -0,0 +1,57 @@ +generateUrlParams(); + +echo " + +use yii\helpers\Html; +use yii\widgets\DetailView; + +/* @var $this yii\web\View */ +/* @var $model modelClass, '\\') ?> */ + +$this->title = $model->getNameAttribute() ?>; +$this->params['breadcrumbs'][] = ['label' => generateString(Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass)))) ?>, 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

Html::encode($this->title) ?>

+ +

+ Html::a(generateString('Update') ?>, ['update', ], ['class' => 'btn btn-primary']) ?> + Html::a(generateString('Delete') ?>, ['delete', ], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => generateString('Are you sure you want to delete this item?') ?>, + 'method' => 'post', + ], + ]) ?> +

+ + DetailView::widget([ + 'model' => $model, + 'attributes' => [ +getTableSchema()) === false) { + foreach ($generator->getColumnNames() as $name) { + echo " '" . $name . "',\n"; + } +} else { + foreach ($generator->getTableSchema()->columns as $column) { + $format = $generator->generateColumnFormat($column); + echo " '" . $column->name . ($format === 'text' ? "" : ":" . $format) . "',\n"; + } +} +?> + ], + ]) ?> + +
diff --git a/generators/crud/form.php b/generators/crud/form.php new file mode 100644 index 0000000..4f59b8b --- /dev/null +++ b/generators/crud/form.php @@ -0,0 +1,17 @@ +field($generator, 'modelClass'); +echo $form->field($generator, 'searchModelClass'); +echo $form->field($generator, 'controllerClass'); +echo $form->field($generator, 'viewPath'); +echo $form->field($generator, 'baseControllerClass'); +echo $form->field($generator, 'indexWidgetType')->dropDownList([ + 'grid' => 'GridView', + 'list' => 'ListView', +]); +echo $form->field($generator, 'enableI18N')->checkbox(); +echo $form->field($generator, 'enablePjax')->checkbox(); +echo $form->field($generator, 'messageCategory');