Commit 804166af by jomon

code review changes

parent 04be5278
......@@ -6,6 +6,7 @@ use common\models\CategoryFiles;
use Yii;
use common\models\Category;
use backend\modules\theme\models\search\CategorySearch;
use yii\helpers\Html;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
......@@ -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.
* If record exists, the browser will be redirected to the 'view' page.
* @param integer $id
......@@ -135,49 +120,43 @@ class CategoryController extends Controller
*/
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);
$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');
$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
$valid = $model->validate();
$valid = Model::validateMultiple($modelFiles) && $valid;
if (Model::loadMultiple($modelFiles, Yii::$app->request->post())) {
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelFiles, 'id', 'id')));
if ($valid) {
if (Model::validateMultiple($modelFiles)) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
$flag = true;
if (!empty($deletedIDs)) {
CategoryFiles::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelFiles as $modelFiles) {
$modelFiles->customer_id = $model->id;
if (! ($flag = $modelFiles->save(false))) {
if (!($flag = $modelFiles->save(false))) {
$transaction->rollBack();
echo '<pre>';
var_dump($modelFiles);
echo '</pre>';
exit();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['file', 'category_id' => $model->id]);
}
} catch (Exception $e) {
......@@ -185,13 +164,31 @@ class CategoryController extends Controller
}
}
}
}
return $this->render('file', [
'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()
{
$model = new Category();
......@@ -204,6 +201,7 @@ class CategoryController extends Controller
// ajax validation
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge(
ActiveForm::validateMultiple($modelFiles),
ActiveForm::validate($model)
......@@ -220,7 +218,7 @@ class CategoryController extends Controller
if ($flag = $model->save(false)) {
foreach ($modelFiles as $modelFile) {
$modelFile->category_id = $model->id;
if (! ($flag = $modelFile->save(false))) {
if (!($flag = $modelFile->save(false))) {
$transaction->rollBack();
break;
}
......@@ -228,6 +226,7 @@ class CategoryController extends Controller
}
if ($flag) {
$transaction->commit();
return $this->redirect(['file', 'category_id' => $model->id]);
}
} catch (Exception $e) {
......
......@@ -3,10 +3,13 @@
/* @var $model common\models\Category */
/* @var $modelFiles common\models\CategoryFiles */
$this->params['categoryId'] = false;
$this->title = 'Create Category File';
$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;
?>
<div class="category-create">
......
......@@ -20,7 +20,7 @@ Menu::widget([
'url' => $this->params['categoryId'] ?
['category/files', 'id' => $this->params['categoryId']]
: '#',
'options' => ['class' => $this->params['categoryId'] ? '' : 'disabled']
'options' => $this->params['categoryId'] ? [] : ['class' => 'disabled'],
],
],
'options' => [
......
<?php
/* @var $this yii\web\View */
/* @var $model common\models\Category */
$this->params['categoryId'] = $model->id;
$this->title = Yii::t('backend', 'Update {modelClass}: ', [
'modelClass' => 'Category',
]) . ' ' . $model->name;
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = ['label' => $model->name, 'url' => ['view', 'id' => $model->id]];
$this->params['breadcrumbs'][] = Yii::t('backend', 'Update');
$this->params['categoryId'] = $model->id;
?>
<div class="category-update">
......
......@@ -14,13 +14,20 @@ use wbraganca\dynamicform\DynamicFormWidget;
<div class="panel panel-default">
<div class="panel-body">
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items', // required: css class selector
'widgetItem' => '.item', // required: 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
'widgetContainer' => 'dynamicform_wrapper',
// required: only alphanumeric characters plus "_" [A-Za-z0-9_]
'widgetBody' => '.container-items',
// required: css class selector
'widgetItem' => '.item',
// required: 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],
'formId' => 'dynamic-form',
'formFields' => [
......@@ -31,7 +38,9 @@ use wbraganca\dynamicform\DynamicFormWidget;
]); ?>
<div class="container-items"><!-- widgetContainer -->
<?php foreach ($modelFiles as $i => $modelFile): ?>
<div class="item panel panel-default"><!-- widgetBody -->
<div class="panel-heading">
<h3 class="panel-title pull-left">Category Files</h3>
......@@ -44,34 +53,53 @@ use wbraganca\dynamicform\DynamicFormWidget;
<div class="clearfix"></div>
</div>
<div class="panel-body">
<?php
// necessary for update action.
if (!$modelFile->isNewRecord) {
echo Html::activeHiddenInput($modelFile, "[{$i}]category_id");
}
?>
<div class="row">
<div class="col-sm-4">
<?= $form->field($modelFile, "[{$i}]name")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-4">
<?= $form->field($modelFile, "[{$i}]code")->textInput(['maxlength' => true]) ?>
</div>
<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 class="hidden">
<?= $form->field($modelFile, "[{$i}]category_id")->hiddenInput() ?>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
</div>
</div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
......
......@@ -17,13 +17,13 @@ if (YII_DEBUG) {
$config['bootstrap'][] = 'debug';
$config['modules']['debug'] = [
'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) {
$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