From 6b81ced4bafb258afcf33b72d85d1ef02231c05c Mon Sep 17 00:00:00 2001 From: rocho Date: Mon, 21 Sep 2015 10:35:15 +0200 Subject: [PATCH] =?UTF-8?q?add=20product=20category=20crud=20+=20model=20(?= =?UTF-8?q?=20term=C3=A9k=20kateg=C3=B3ria=20)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/components/AdminMenuStructure.php | 1 + .../controllers/ProductCategoryController.php | 109 ++++++++++++++++++ backend/models/ProductCategorySearch.php | 69 +++++++++++ backend/views/product-category/_form.php | 26 +++++ backend/views/product-category/_search.php | 34 ++++++ backend/views/product-category/create.php | 21 ++++ backend/views/product-category/index.php | 39 +++++++ backend/views/product-category/update.php | 23 ++++ backend/views/product-category/view.php | 44 +++++++ .../messages/hu/common/product_category.php | 36 ++++++ common/models/BaseFitnessActiveRecord.php | 21 ++++ common/models/ProductCategory.php | 72 ++++++++++++ ...21_080440_add__table__product_category.php | 42 +++++++ 13 files changed, 537 insertions(+) create mode 100644 backend/controllers/ProductCategoryController.php create mode 100644 backend/models/ProductCategorySearch.php create mode 100644 backend/views/product-category/_form.php create mode 100644 backend/views/product-category/_search.php create mode 100644 backend/views/product-category/create.php create mode 100644 backend/views/product-category/index.php create mode 100644 backend/views/product-category/update.php create mode 100644 backend/views/product-category/view.php create mode 100644 common/messages/hu/common/product_category.php create mode 100644 common/models/BaseFitnessActiveRecord.php create mode 100644 common/models/ProductCategory.php create mode 100644 console/migrations/m150921_080440_add__table__product_category.php diff --git a/backend/components/AdminMenuStructure.php b/backend/components/AdminMenuStructure.php index 1ff13bb..baac8d3 100644 --- a/backend/components/AdminMenuStructure.php +++ b/backend/components/AdminMenuStructure.php @@ -38,6 +38,7 @@ class AdminMenuStructure{ $items[] = ['label' => 'Raktárak', 'url' =>['/warehouse/index']]; $items[] = ['label' => 'Kasszák', 'url' =>['/account/index']]; $items[] = ['label' => 'Kedvezmények', 'url' => ['/discount/index'] ]; + $items[] = ['label' => 'Termék kategóriák', 'url' => ['/product-category/index'] ]; if ( count($items) > 0 ){ $userMainMenu = ['label' => 'Beállítások', 'url' => null, diff --git a/backend/controllers/ProductCategoryController.php b/backend/controllers/ProductCategoryController.php new file mode 100644 index 0000000..5bf3544 --- /dev/null +++ b/backend/controllers/ProductCategoryController.php @@ -0,0 +1,109 @@ + [ + 'class' => VerbFilter::className(), + 'actions' => [ + 'delete' => ['post'], + ], + ], + ]; + } + + /** + * Lists all ProductCategory models. + * @return mixed + */ + public function actionIndex() + { + $searchModel = new ProductCategorySearch(); + $dataProvider = $searchModel->search(Yii::$app->request->queryParams); + + return $this->render('index', [ + 'searchModel' => $searchModel, + 'dataProvider' => $dataProvider, + ]); + } + + /** + * Displays a single ProductCategory model. + * @param integer $id + * @return mixed + */ + public function actionView($id) + { + return $this->render('view', [ + 'model' => $this->findModel($id), + ]); + } + + /** + * Creates a new ProductCategory model. + * If creation is successful, the browser will be redirected to the 'view' page. + * @return mixed + */ + public function actionCreate() + { + $model = new ProductCategory(); + $model->status = ProductCategory::STATUS_ACTIVE; + if ($model->load(Yii::$app->request->post()) && $model->save()) { + return $this->redirect(['view', 'id' => $model->id_product_category]); + } else { + return $this->render('create', [ + 'model' => $model, + ]); + } + } + + /** + * Updates an existing ProductCategory 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_product_category]); + } else { + return $this->render('update', [ + 'model' => $model, + ]); + } + } + + + /** + * Finds the ProductCategory model based on its primary key value. + * If the model is not found, a 404 HTTP exception will be thrown. + * @param integer $id + * @return ProductCategory the loaded model + * @throws NotFoundHttpException if the model cannot be found + */ + protected function findModel($id) + { + if (($model = ProductCategory::findOne($id)) !== null) { + return $model; + } else { + throw new NotFoundHttpException('The requested page does not exist.'); + } + } +} diff --git a/backend/models/ProductCategorySearch.php b/backend/models/ProductCategorySearch.php new file mode 100644 index 0000000..ff747f3 --- /dev/null +++ b/backend/models/ProductCategorySearch.php @@ -0,0 +1,69 @@ + $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_product_category' => $this->id_product_category, + 'status' => $this->status, + 'created_at' => $this->created_at, + 'updated_at' => $this->updated_at, + ]); + + $query->andFilterWhere(['like', 'name', $this->name]); + + return $dataProvider; + } +} diff --git a/backend/views/product-category/_form.php b/backend/views/product-category/_form.php new file mode 100644 index 0000000..ca9f350 --- /dev/null +++ b/backend/views/product-category/_form.php @@ -0,0 +1,26 @@ + + +
+ + + + field($model, 'name')->textInput(['maxlength' => true]) ?> + + field($model, 'status')->checkbox( ['value' => 10, 'label' => Yii::t('common/product_category', "Active") ]) ?> + + +
+ isNewRecord ? Yii::t('common/product_category', 'Create') : Yii::t('common/product_category', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> +
+ + + +
diff --git a/backend/views/product-category/_search.php b/backend/views/product-category/_search.php new file mode 100644 index 0000000..8878ddc --- /dev/null +++ b/backend/views/product-category/_search.php @@ -0,0 +1,34 @@ + + + diff --git a/backend/views/product-category/create.php b/backend/views/product-category/create.php new file mode 100644 index 0000000..19898e4 --- /dev/null +++ b/backend/views/product-category/create.php @@ -0,0 +1,21 @@ +title = Yii::t('common/product_category', 'Create Product Category'); +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/product_category', 'Product Categories'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/views/product-category/index.php b/backend/views/product-category/index.php new file mode 100644 index 0000000..600c78b --- /dev/null +++ b/backend/views/product-category/index.php @@ -0,0 +1,39 @@ +title = Yii::t('common/product_category', 'Product Categories'); +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ render('_search', ['model' => $searchModel]); ?> + +

+ 'btn btn-success']) ?> +

+ + $dataProvider, + 'columns' => [ + 'name', + [ + 'attribute' => 'status', + 'value' => 'statusHuman', + ], + 'created_at:datetime', + 'updated_at:datetime', + [ + 'class' => 'yii\grid\ActionColumn', + 'template' => '{view} {update}' + ], + ], + ]); ?> + +
diff --git a/backend/views/product-category/update.php b/backend/views/product-category/update.php new file mode 100644 index 0000000..74d3635 --- /dev/null +++ b/backend/views/product-category/update.php @@ -0,0 +1,23 @@ +title = Yii::t('common/product_category', 'Update {modelClass}: ', [ + 'modelClass' =>Yii::t('common/product_category', 'Product Category'), +]) . ' ' . $model->name; +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/product_category', 'Product Categories'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id_product_category]]; +$this->params['breadcrumbs'][] = Yii::t('common/product_category', 'Update'); +?> +
+ +

title) ?>

+ + render('_form', [ + 'model' => $model, + ]) ?> + +
diff --git a/backend/views/product-category/view.php b/backend/views/product-category/view.php new file mode 100644 index 0000000..741d37e --- /dev/null +++ b/backend/views/product-category/view.php @@ -0,0 +1,44 @@ +title = $model->name; +$this->params['breadcrumbs'][] = ['label' => Yii::t('common/product_category', 'Product Categories'), 'url' => ['index']]; +$this->params['breadcrumbs'][] = $this->title; +?> +
+ +

title) ?>

+ +

+ $model->id_product_category], ['class' => 'btn btn-primary']) ?> + $model->id_product_category], [ + 'class' => 'btn btn-danger', + 'data' => [ + 'confirm' => Yii::t('common/product_category', 'Are you sure you want to delete this item?'), + 'method' => 'post', + ], + ]) + */ + ?> +

+ + $model, + 'attributes' => [ + 'id_product_category', + 'name', + 'status', + 'created_at', + 'updated_at', + ], + ]) ?> + +
diff --git a/common/messages/hu/common/product_category.php b/common/messages/hu/common/product_category.php new file mode 100644 index 0000000..e804f72 --- /dev/null +++ b/common/messages/hu/common/product_category.php @@ -0,0 +1,36 @@ + 'Aktív', + 'Are you sure you want to delete this item?' => 'Biztosan törölni szeretné a termék kategóriát?', + 'Create' => 'Mentés', + 'Create Product Category' => 'Új termék kategória', + 'Created At' => 'Létrehozás ideje', + 'Delete' => 'Törlés', + 'Id Product Category' => 'Azonosító', + 'Inactive' => 'Inaktív', + 'Name' => 'Név', + 'Product Categories' => 'Termék kategóriák', + 'Product Category' => 'Termék kategória', + 'Search' => 'Keresés', + 'Status' => 'Státusz', + 'Update' => 'Módosítás', + 'Update {modelClass}: ' => '{modelClass} módosítása:', + 'Updated At' => 'Módosítás ideje', +]; diff --git a/common/models/BaseFitnessActiveRecord.php b/common/models/BaseFitnessActiveRecord.php new file mode 100644 index 0000000..8482072 --- /dev/null +++ b/common/models/BaseFitnessActiveRecord.php @@ -0,0 +1,21 @@ + TimestampBehavior::className(), + 'value' => function(){ return date('Y-m-d H:i:s' ); } + ] + ], parent::behaviors()); + } +} \ No newline at end of file diff --git a/common/models/ProductCategory.php b/common/models/ProductCategory.php new file mode 100644 index 0000000..70b255a --- /dev/null +++ b/common/models/ProductCategory.php @@ -0,0 +1,72 @@ + 64] + ]; + } + + /** + * @inheritdoc + */ + public function attributeLabels() + { + return [ + 'id_product_category' => Yii::t('common/product_category', 'Id Product Category'), + 'name' => Yii::t('common/product_category', 'Name'), + 'status' => Yii::t('common/product_category', 'Status'), + 'created_at' => Yii::t('common/product_category', 'Created At'), + 'updated_at' => Yii::t('common/product_category', 'Updated At'), + ]; + } + + static function statuses() { + return [ + self::STATUS_ACTIVE => Yii::t('common/product_category', 'Active'), + self::STATUS_DELETED => Yii::t('common/product_category', 'Inactive'), + ]; + } + + public function getStatusHuman(){ + $result = null; + $s = self::statuses($this->status); + if ( array_key_exists($this->status, $s)){ + $result = $s[$this->status]; + } + return $result; + } + +} diff --git a/console/migrations/m150921_080440_add__table__product_category.php b/console/migrations/m150921_080440_add__table__product_category.php new file mode 100644 index 0000000..4628bd6 --- /dev/null +++ b/console/migrations/m150921_080440_add__table__product_category.php @@ -0,0 +1,42 @@ +db->driverName === 'mysql') { + // http://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci + $tableOptions = 'CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE=InnoDB'; + } + + $this->createTable('{{%product_category}}', [ + 'id_product_category' => $this->primaryKey(), + 'name' => $this->string(64)->notNull(), + 'status' => $this->smallInteger()->notNull()->defaultValue(10), + 'created_at' => $this->timestamp()->notNull(), + 'updated_at' => $this->timestamp()->notNull(), + ], $tableOptions); + } + + public function down() + { + echo "m150921_080440_add__table__product_category cannot be reverted.\n"; + + return false; + } + + /* + // Use safeUp/safeDown to run migration code within a transaction + public function safeUp() + { + } + + public function safeDown() + { + } + */ +}