Commit 32ec38bf by jomon

code review changes

parent 3725cdac
......@@ -121,17 +121,17 @@ class CategoryController extends Controller
{
$this->layout = '@backend/modules/theme/views/category/layout.php';
$model = $this->findModel($id);
$modelFiles = $model->categoryFiles;
$categoryModel = $this->findModel($id);
$models = $categoryModel->categoryFiles;
if (Yii::$app->request->isPost) {
$oldIDs = ArrayHelper::map($modelFiles, 'id', 'id');
$modelFiles = Model::createMultiple(CategoryFiles::classname(), $modelFiles);
$oldIDs = ArrayHelper::map($models, 'id', 'id');
$models = Model::createMultiple(CategoryFiles::classname(), $models);
if (Model::loadMultiple($modelFiles, Yii::$app->request->post())) {
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelFiles, 'id', 'id')));
if (Model::loadMultiple($models, Yii::$app->request->post())) {
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($models, 'id', 'id')));
if (Model::validateMultiple($modelFiles)) {
if (Model::validateMultiple($models)) {
$transaction = \Yii::$app->db->beginTransaction();
try {
......@@ -141,21 +141,17 @@ class CategoryController extends Controller
CategoryFiles::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelFiles as $modelFiles) {
if (!($flag = $modelFiles->save(false))) {
foreach ($models as $model) {
$model->category_id = $categoryModel->id;
if (!($flag = $model->save(false))) {
$transaction->rollBack();
echo '<pre>';
var_dump($modelFiles);
echo '</pre>';
exit();
break;
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['index', 'category_id' => $model->id]);
return $this->redirect(['index']);
}
} catch (Exception $e) {
$transaction->rollBack();
......@@ -165,8 +161,8 @@ class CategoryController extends Controller
}
return $this->render('file', [
'model' => $model,
'modelFiles' => (empty($modelFiles)) ? [new CategoryFiles(['category_id' => $model->id])] : $modelFiles
'categoryModel' => $categoryModel,
'models' => (empty($models)) ? [new CategoryFiles(['category_id' => $categoryModel->id])] : $models,
]);
}
......@@ -190,31 +186,31 @@ class CategoryController extends Controller
// public function actionCreatee()
// {
// $model = new Category();
// $modelFiles = [new CategoryFiles()];
// $models = [new CategoryFiles()];
// if ($model->load(Yii::$app->request->post())) {
//
// $modelFiles = Model::createMultiple(CategoryFiles::classname());
// Model::loadMultiple($modelFiles, Yii::$app->request->post());
// $models = Model::createMultiple(CategoryFiles::classname());
// Model::loadMultiple($models, Yii::$app->request->post());
//
// // ajax validation
// if (Yii::$app->request->isAjax) {
// Yii::$app->response->format = Response::FORMAT_JSON;
//
// return ArrayHelper::merge(
// ActiveForm::validateMultiple($modelFiles),
// ActiveForm::validateMultiple($models),
// ActiveForm::validate($model)
// );
// }
//
// // validate all models
// $valid = $model->validate();
// $valid = Model::validateMultiple($modelFiles) && $valid;
// $valid = Model::validateMultiple($models) && $valid;
//
// if ($valid) {
// $transaction = \Yii::$app->db->beginTransaction();
// try {
// if ($flag = $model->save(false)) {
// foreach ($modelFiles as $modelFile) {
// foreach ($models as $modelFile) {
// $modelFile->category_id = $model->id;
// if (!($flag = $modelFile->save(false))) {
// $transaction->rollBack();
......@@ -235,7 +231,7 @@ class CategoryController extends Controller
//
// return $this->render('file', [
// 'model' => $model,
// 'modelFiles' => (empty($modelFiles)) ? [new CategoryFiles()] : $modelFiles
// 'modelFiles' => (empty($models)) ? [new CategoryFiles()] : $models
// ]);
// }
}
<?php
/* @var $this yii\web\View */
/* @var $model common\models\Category */
/* @var $modelFiles common\models\CategoryFiles */
/* @var $categoryModel common\models\Category */
/* @var $models common\models\CategoryFiles[] */
$this->title = Yii::t('admin', 'Create Category File');
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
$this->params['categoryId'] = $model->id;
$this->params['categoryId'] = $categoryModel->id;
?>
<div class="category-create">
<?= $this->render('..\file\_form', [
'model' => $model,
'modelFiles' => $modelFiles,
'categoryModel' => $categoryModel,
'models' => $models,
]) ?>
</div>
<?php
?>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
/* @var $this yii\web\View */
/* @var $categoryModel common\models\Category */
/* @var $models common\models\CategoryFiles[] */
?>
<div class="customer-form">
......@@ -28,7 +29,7 @@ use wbraganca\dynamicform\DynamicFormWidget;
// css class
'deleteButton' => '.remove-item',
// css class
'model' => $modelFiles[0],
'model' => $models[0],
'formId' => 'dynamic-form',
'formFields' => [
'name',
......@@ -39,7 +40,7 @@ use wbraganca\dynamicform\DynamicFormWidget;
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelFiles as $i => $modelFile): ?>
<?php foreach ($models as $i => $model): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
......@@ -56,34 +57,29 @@ use wbraganca\dynamicform\DynamicFormWidget;
<?php
// necessary for update action.
if (!$modelFile->isNewRecord) {
echo Html::activeHiddenInput($modelFile, "[{$i}]category_id");
if (!$model->isNewRecord) {
echo Html::activeHiddenInput($model, "[{$i}]category_id");
}
?>
<div class="row">
<div class="col-sm-4">
<?= $form->field($modelFile, "[{$i}]name")->textInput(['maxlength' => true]) ?>
<?= $form->field($model, "[{$i}]name")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelFile, "[{$i}]code")->textInput(['maxlength' => true]) ?>
<?= $form->field($model, "[{$i}]code")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelFile, "[{$i}]type")->dropDownList($modelFile::getfiles(),
<?= $form->field($model, "[{$i}]type")->dropDownList($model::getfiles(),
['prompt' => '']) ?>
</div>
</div>
<div class="hidden">
<?= $form->field($modelFile, "[{$i}]category_id")->hiddenInput() ?>
</div>
</div>
</div>
......
......@@ -23,6 +23,7 @@ class CategoryFiles extends \yii\db\ActiveRecord
const TYPE_CSS = 2;
const TYPE_HTML = 3;
const TYPE_HTML_BLOCK = 4;
/**
* @inheritdoc
*/
......@@ -37,10 +38,16 @@ class CategoryFiles extends \yii\db\ActiveRecord
public function rules()
{
return [
[['category_id', 'code'], 'required'],
[['code'], 'required'],
[['category_id', 'created_at', 'updated_at'], 'integer'],
[['code', 'type', 'name'], 'string', 'max' => 32],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
[
['category_id'],
'exist',
'skipOnError' => true,
'targetClass' => Category::className(),
'targetAttribute' => ['category_id' => 'id']
],
];
}
......
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