Commit 804166af by jomon

code review changes

parent 04be5278
...@@ -6,6 +6,7 @@ use common\models\CategoryFiles; ...@@ -6,6 +6,7 @@ use common\models\CategoryFiles;
use Yii; use Yii;
use common\models\Category; use common\models\Category;
use backend\modules\theme\models\search\CategorySearch; use backend\modules\theme\models\search\CategorySearch;
use yii\helpers\Html;
use yii\web\Controller; use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
...@@ -112,22 +113,6 @@ class CategoryController extends Controller ...@@ -112,22 +113,6 @@ class CategoryController extends Controller
} }
/** /**
* Finds the Category model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Category the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Category::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
/**
* Files of an existing Category model. * Files of an existing Category model.
* If record exists, the browser will be redirected to the 'view' page. * If record exists, the browser will be redirected to the 'view' page.
* @param integer $id * @param integer $id
...@@ -135,49 +120,43 @@ class CategoryController extends Controller ...@@ -135,49 +120,43 @@ class CategoryController extends Controller
*/ */
public function actionFiles($id) public function actionFiles($id)
{ {
// $this->layout = '@backend/modules/theme/views/category/layout.php'; $this->layout = '@backend/modules/theme/views/category/layout.php';
$model = $this->findModel($id); $model = $this->findModel($id);
$modelFiles = $model->categoryFiles; $modelFiles = $model->categoryFiles;
// $modelFiles->type = $modelFiles::TYPE_JS;
if ($model->load(Yii::$app->request->post())) { //&& $model->save()
if (Yii::$app->request->isPost) {
$oldIDs = ArrayHelper::map($modelFiles, 'id', 'id'); $oldIDs = ArrayHelper::map($modelFiles, 'id', 'id');
$modelFiles = Model::createMultiple(CategoryFiles::classname(), $modelFiles); $modelFiles = Model::createMultiple(CategoryFiles::classname(), $modelFiles);
Model::loadMultiple($modelFiles, Yii::$app->request->post());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelFiles, 'id', 'id')));
// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelFiles),
ActiveForm::validate($model)
);
}
// validate all models if (Model::loadMultiple($modelFiles, Yii::$app->request->post())) {
$valid = $model->validate(); $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelFiles, 'id', 'id')));
$valid = Model::validateMultiple($modelFiles) && $valid;
if ($valid) { if (Model::validateMultiple($modelFiles)) {
$transaction = \Yii::$app->db->beginTransaction(); $transaction = \Yii::$app->db->beginTransaction();
try { try {
if ($flag = $model->save(false)) { $flag = true;
if (! empty($deletedIDs)) {
if (!empty($deletedIDs)) {
CategoryFiles::deleteAll(['id' => $deletedIDs]); CategoryFiles::deleteAll(['id' => $deletedIDs]);
} }
foreach ($modelFiles as $modelFiles) { foreach ($modelFiles as $modelFiles) {
$modelFiles->customer_id = $model->id; if (!($flag = $modelFiles->save(false))) {
if (! ($flag = $modelFiles->save(false))) {
$transaction->rollBack(); $transaction->rollBack();
echo '<pre>';
var_dump($modelFiles);
echo '</pre>';
exit();
break; break;
} }
} }
}
if ($flag) { if ($flag) {
$transaction->commit(); $transaction->commit();
return $this->redirect(['file', 'category_id' => $model->id]); return $this->redirect(['file', 'category_id' => $model->id]);
} }
} catch (Exception $e) { } catch (Exception $e) {
...@@ -185,13 +164,31 @@ class CategoryController extends Controller ...@@ -185,13 +164,31 @@ class CategoryController extends Controller
} }
} }
} }
}
return $this->render('file', [ return $this->render('file', [
'model' => $model, 'model' => $model,
'modelFiles' => (empty($modelFiles)) ? [new CategoryFiles()] : $modelFiles 'modelFiles' => (empty($modelFiles)) ? [new CategoryFiles(['category_id' => $model->id])] : $modelFiles
]); ]);
} }
/**
* Finds the Category model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Category the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Category::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
public function actionCreatee() public function actionCreatee()
{ {
$model = new Category(); $model = new Category();
...@@ -204,6 +201,7 @@ class CategoryController extends Controller ...@@ -204,6 +201,7 @@ class CategoryController extends Controller
// ajax validation // ajax validation
if (Yii::$app->request->isAjax) { if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON; Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge( return ArrayHelper::merge(
ActiveForm::validateMultiple($modelFiles), ActiveForm::validateMultiple($modelFiles),
ActiveForm::validate($model) ActiveForm::validate($model)
...@@ -220,7 +218,7 @@ class CategoryController extends Controller ...@@ -220,7 +218,7 @@ class CategoryController extends Controller
if ($flag = $model->save(false)) { if ($flag = $model->save(false)) {
foreach ($modelFiles as $modelFile) { foreach ($modelFiles as $modelFile) {
$modelFile->category_id = $model->id; $modelFile->category_id = $model->id;
if (! ($flag = $modelFile->save(false))) { if (!($flag = $modelFile->save(false))) {
$transaction->rollBack(); $transaction->rollBack();
break; break;
} }
...@@ -228,6 +226,7 @@ class CategoryController extends Controller ...@@ -228,6 +226,7 @@ class CategoryController extends Controller
} }
if ($flag) { if ($flag) {
$transaction->commit(); $transaction->commit();
return $this->redirect(['file', 'category_id' => $model->id]); return $this->redirect(['file', 'category_id' => $model->id]);
} }
} catch (Exception $e) { } catch (Exception $e) {
......
...@@ -3,10 +3,13 @@ ...@@ -3,10 +3,13 @@
/* @var $model common\models\Category */ /* @var $model common\models\Category */
/* @var $modelFiles common\models\CategoryFiles */ /* @var $modelFiles common\models\CategoryFiles */
$this->params['categoryId'] = false; $this->title = Yii::t('admin', 'Create Category File');
$this->title = 'Create Category File';
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title; $this->params['breadcrumbs'][] = $this->title;
$this->params['categoryId'] = $model->id;
?> ?>
<div class="category-create"> <div class="category-create">
......
...@@ -20,7 +20,7 @@ Menu::widget([ ...@@ -20,7 +20,7 @@ Menu::widget([
'url' => $this->params['categoryId'] ? 'url' => $this->params['categoryId'] ?
['category/files', 'id' => $this->params['categoryId']] ['category/files', 'id' => $this->params['categoryId']]
: '#', : '#',
'options' => ['class' => $this->params['categoryId'] ? '' : 'disabled'] 'options' => $this->params['categoryId'] ? [] : ['class' => 'disabled'],
], ],
], ],
'options' => [ 'options' => [
......
<?php <?php
/* @var $this yii\web\View */ /* @var $this yii\web\View */
/* @var $model common\models\Category */ /* @var $model common\models\Category */
$this->params['categoryId'] = $model->id;
$this->title = Yii::t('backend', 'Update {modelClass}: ', [ $this->title = Yii::t('backend', 'Update {modelClass}: ', [
'modelClass' => 'Category', 'modelClass' => 'Category',
]) . ' ' . $model->name; ]) . ' ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']]; $this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]]; $this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = Yii::t('backend', 'Update'); $this->params['breadcrumbs'][] = Yii::t('backend', 'Update');
$this->params['categoryId'] = $model->id;
?> ?>
<div class="category-update"> <div class="category-update">
......
...@@ -14,13 +14,20 @@ use wbraganca\dynamicform\DynamicFormWidget; ...@@ -14,13 +14,20 @@ use wbraganca\dynamicform\DynamicFormWidget;
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-body"> <div class="panel-body">
<?php DynamicFormWidget::begin([ <?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_] 'widgetContainer' => 'dynamicform_wrapper',
'widgetBody' => '.container-items', // required: css class selector // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetItem' => '.item', // required: css class 'widgetBody' => '.container-items',
'limit' => 4, // the maximum times, an element can be cloned (default 999) // required: css class selector
'min' => 1, // 0 or 1 (default 1) 'widgetItem' => '.item',
'insertButton' => '.add-item', // css class // required: css class
'deleteButton' => '.remove-item', // css class 'limit' => 4,
// the maximum times, an element can be cloned (default 999)
'min' => 1,
// 0 or 1 (default 1)
'insertButton' => '.add-item',
// css class
'deleteButton' => '.remove-item',
// css class
'model' => $modelFiles[0], 'model' => $modelFiles[0],
'formId' => 'dynamic-form', 'formId' => 'dynamic-form',
'formFields' => [ 'formFields' => [
...@@ -31,7 +38,9 @@ use wbraganca\dynamicform\DynamicFormWidget; ...@@ -31,7 +38,9 @@ use wbraganca\dynamicform\DynamicFormWidget;
]); ?> ]); ?>
<div class="container-items"><!-- widgetContainer --> <div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelFiles as $i => $modelFile): ?> <?php foreach ($modelFiles as $i => $modelFile): ?>
<div class="item panel panel-default"><!-- widgetBody --> <div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading"> <div class="panel-heading">
<h3 class="panel-title pull-left">Category Files</h3> <h3 class="panel-title pull-left">Category Files</h3>
...@@ -44,34 +53,53 @@ use wbraganca\dynamicform\DynamicFormWidget; ...@@ -44,34 +53,53 @@ use wbraganca\dynamicform\DynamicFormWidget;
<div class="clearfix"></div> <div class="clearfix"></div>
</div> </div>
<div class="panel-body"> <div class="panel-body">
<?php <?php
// necessary for update action. // necessary for update action.
if (!$modelFile->isNewRecord) { if (!$modelFile->isNewRecord) {
echo Html::activeHiddenInput($modelFile, "[{$i}]category_id"); echo Html::activeHiddenInput($modelFile, "[{$i}]category_id");
} }
?> ?>
<div class="row"> <div class="row">
<div class="col-sm-4"> <div class="col-sm-4">
<?= $form->field($modelFile, "[{$i}]name")->textInput(['maxlength' => true]) ?> <?= $form->field($modelFile, "[{$i}]name")->textInput(['maxlength' => true]) ?>
</div> </div>
<div class="col-sm-4"> <div class="col-sm-4">
<?= $form->field($modelFile, "[{$i}]code")->textInput(['maxlength' => true]) ?> <?= $form->field($modelFile, "[{$i}]code")->textInput(['maxlength' => true]) ?>
</div> </div>
<div class="col-sm-4"> <div class="col-sm-4">
<!-- <?//= $form->field($modelFile, "[{$i}]type")->textInput(['maxlength' => true]) ?> -->
<?= $form->field($modelFile, "[{$i}]type")->dropDownList($modelFile::getfiles(), ['prompt' => '']) ?> <?= $form->field($modelFile, "[{$i}]type")->dropDownList($modelFile::getfiles(),
['prompt' => '']) ?>
</div> </div>
</div> </div>
<div class="hidden">
<?= $form->field($modelFile, "[{$i}]category_id")->hiddenInput() ?>
</div>
</div> </div>
</div> </div>
<?php endforeach; ?> <?php endforeach; ?>
</div> </div>
<?php DynamicFormWidget::end(); ?> <?php DynamicFormWidget::end(); ?>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?> <?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
</div> </div>
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>
......
...@@ -17,13 +17,13 @@ if (YII_DEBUG) { ...@@ -17,13 +17,13 @@ if (YII_DEBUG) {
$config['bootstrap'][] = 'debug'; $config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [ $config['modules']['debug'] = [
'class' => 'yii\debug\Module', 'class' => 'yii\debug\Module',
'allowedIPs' => ['127.0.0.1', '::1', '192.168.33.1', '172.17.42.1', '172.17.0.1'], 'allowedIPs' => ['127.0.0.1', '::1','192.168.1.123'],
]; ];
} }
if (YII_ENV_DEV) { if (YII_ENV_DEV) {
$config['modules']['gii'] = [ $config['modules']['gii'] = [
'allowedIPs' => ['127.0.0.1', '::1', '192.168.33.1', '172.17.42.1', '172.17.0.1'], 'allowedIPs' => ['127.0.0.1', '::1','192.168.1.123'],
]; ];
} }
......
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