Commit 23fcf85c by dianc

Category Files Created

parent 71b55384
......@@ -2,14 +2,15 @@
namespace backend\modules\theme\controllers;
use common\models\CategoryFiles;
use Yii;
use common\models\Category;
use backend\modules\theme\models\search\CategorySearch;
use yii\behaviors\SluggableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\Model;
use yii\helpers\ArrayHelper;
/**
* CategoryController implements the CRUD actions for Category model.
......@@ -125,4 +126,118 @@ class CategoryController extends Controller
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
* @return mixed
*/
public function actionFiles($id)
{
// $this->layout = '@backend/modules/theme/views/category/layout.php';
$model = $this->findModel($id);
$modelFiles = $model->categoryFiles;
if ($model->load(Yii::$app->request->post())) { //&& $model->save()
$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 ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) {
CategoryFiles::deleteAll(['id' => $deletedIDs]);
}
foreach ($modelFiles as $modelFiles) {
$modelFiles->customer_id = $model->id;
if (! ($flag = $modelFiles->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['file', 'category_id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('file', [
'model' => $model,
'modelFiles' => (empty($modelFiles)) ? [new CategoryFiles()] : $modelFiles
]);
}
public function actionCreatee()
{
$model = new Category();
$modelFiles = [new CategoryFiles()];
if ($model->load(Yii::$app->request->post())) {
$modelFiles = Model::createMultiple(CategoryFiles::classname());
Model::loadMultiple($modelFiles, 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::validate($model)
);
}
// validate all models
$valid = $model->validate();
$valid = Model::validateMultiple($modelFiles) && $valid;
if ($valid) {
$transaction = \Yii::$app->db->beginTransaction();
try {
if ($flag = $model->save(false)) {
foreach ($modelFiles as $modelFile) {
$modelFile->category_id = $model->id;
if (! ($flag = $modelFile->save(false))) {
$transaction->rollBack();
break;
}
}
}
if ($flag) {
$transaction->commit();
return $this->redirect(['file', 'category_id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
}
}
}
return $this->render('createe', [
'model' => $model,
'modelFiles' => (empty($modelFiles)) ? [new CategoryFiles()] : $modelFiles
]);
}
}
......@@ -28,7 +28,7 @@ use trntv\filekit\widget\Upload;
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade in active" id="info">
<div class="page-header">
<h3>Info</h3>
<h3> <?= Yii::t('backend', 'Info') ?> </h3>
</div>
<div class="row">
<div class="col-md-6">
......
<?php
/* @var $this yii\web\View */
/* @var $model common\models\Category */
/* @var $modelFiles common\models\CategoryFiles */
$this->params['categoryId'] = false;
$this->title = 'Create Category File';
$this->params['breadcrumbs'][] = ['label' => Yii::t('backend', 'Categories'), 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="category-create">
<?= $this->render('..\file\_form', [
'model' => $model,
'modelFiles' => $modelFiles,
]) ?>
</div>
......@@ -11,17 +11,15 @@ Menu::widget([
'items' => [
[
'label' => 'Info',
'url' => $this->params['categoryId'] ? [
'category/update',
'id' => $this->params['categoryId']
] : ['category/create'],
'url' => $this->params['categoryId'] ?
['category/update', 'id' => $this->params['categoryId']]
: ['category/create'],
],
[
'label' => 'Files',
'url' => $this->params['categoryId'] ? [
'category/files',
'category_id' => $this->params['categoryId']
] : '#',
'url' => $this->params['categoryId'] ?
['category/files', 'id' => $this->params['categoryId']]
: '#',
'options' => ['class' => $this->params['categoryId'] ? '' : 'disabled']
],
],
......
<?php
?>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
?>
<div class="customer-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'description')->textInput(['maxlength' => true]) ?>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading">
<h4><i class="glyphicon glyphicon-envelope"></i> Addresses</h4>
</div>
<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
'model' => $modelFiles[0],
'formId' => 'dynamic-form',
'formFields' => [
'name',
'code',
'category_id',
],
]); ?>
<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>
<div class="pull-right">
<button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>
<button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
</div>
<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-5">
<?= $form->field($modelFile, "[{$i}]name")->textInput(['maxlength' => true]) ?>
</div>
<div class="col-sm-5">
<?= $form->field($modelFile, "[{$i}]code")->textInput(['maxlength' => true]) ?>
</div>
</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(); ?>
</div>
<?php
?>
<div id="panel-option-values" class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><i class="fa fa-check-square-o"></i> Option values</h3>
</div>
<?php DynamicFormWidget::begin([
'widgetContainer' => 'dynamicform_wrapper',
'widgetBody' => '.form-options-body',
'widgetItem' => '.form-options-item',
'min' => 1,
'insertButton' => '.add-item',
'deleteButton' => '.delete-item',
'model' => $modelsOptionValue[0],
'formId' => 'dynamic-form',
'formFields' => [
'name',
'img'
],
]); ?>
<table class="table table-bordered table-striped margin-b-none">
<thead>
<tr>
<th style="width: 90px; text-align: center"></th>
<th class="required">Option value name</th>
<th style="width: 188px;">Image</th>
<th style="width: 90px; text-align: center">Actions</th>
</tr>
</thead>
<tbody class="form-options-body">
<?php foreach ($modelsOptionValue as $index => $modelOptionValue): ?>
<tr class="form-options-item">
<td class="sortable-handle text-center vcenter" style="cursor: move;">
<i class="fa fa-arrows"></i>
</td>
<td class="vcenter">
<?= $form->field($modelOptionValue, "[{$index}]name")->label(false)->textInput(['maxlength' => 128]); ?>
</td>
<td>
<?php if (!$modelOptionValue->isNewRecord): ?>
<?= Html::activeHiddenInput($modelOptionValue, "[{$index}]id"); ?>
<?= Html::activeHiddenInput($modelOptionValue, "[{$index}]image_id"); ?>
<?= Html::activeHiddenInput($modelOptionValue, "[{$index}]deleteImg"); ?>
<?php endif; ?>
<?php
$modelImage = Image::findOne(['id' => $modelOptionValue->image_id]);
$initialPreview = [];
if ($modelImage) {
$pathImg = Yii::$app->fileStorage->baseUrl . '/' . $modelImage->path;
$initialPreview[] = Html::img($pathImg, ['class' => 'file-preview-image']);
}
?>
<?= $form->field($modelOptionValue, "[{$index}]img")->label(false)->widget(FileInput::classname(), [
'options' => [
'multiple' => false,
'accept' => 'image/*',
'class' => 'optionvalue-img'
],
'pluginOptions' => [
'previewFileType' => 'image',
'showCaption' => false,
'showUpload' => false,
'browseClass' => 'btn btn-default btn-sm',
'browseLabel' => ' Pick image',
'browseIcon' => '<i class="glyphicon glyphicon-picture"></i>',
'removeClass' => 'btn btn-danger btn-sm',
'removeLabel' => ' Delete',
'removeIcon' => '<i class="fa fa-trash"></i>',
'previewSettings' => [
'image' => ['width' => '138px', 'height' => 'auto']
],
'initialPreview' => $initialPreview,
'layoutTemplates' => ['footer' => '']
]
]) ?>
</td>
<td class="text-center vcenter">
<button type="button" class="delete-item btn btn-danger btn-xs"><i class="fa fa-minus"></i></button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
<tfoot>
<tr>
<td colspan="3"></td>
<td><button type="button" class="add-item btn btn-success btn-sm"><span class="fa fa-plus"></span> New</button></td>
</tr>
</tfoot>
</table>
<?php DynamicFormWidget::end(); ?>
</div>
......@@ -28,7 +28,7 @@ class m160830_105840_category extends Migration
'id' => $this->primaryKey(),
'category_id' => $this->integer()->notNull(),
'code' => $this->string(32)->notNull(),
'type' => $this->string(32)->notNull(),
'type' => $this->integer()->notNull(),
'name' => $this->string(32)->notNull(),
'created_at' => $this->integer(),
'updated_at' => $this->integer()
......
......@@ -19,6 +19,10 @@ use Yii;
*/
class CategoryFiles extends \yii\db\ActiveRecord
{
const TYPE_JS = 1;
const TYPE_CSS = 2;
const TYPE_HTML = 3;
const TYPE_HTML_BLOCK = 4;
/**
* @inheritdoc
*/
......@@ -72,4 +76,14 @@ class CategoryFiles extends \yii\db\ActiveRecord
{
return new \common\models\query\CategoryFilesQuery(get_called_class());
}
public static function getfiles()
{
return [
self::TYPE_JS => Yii::t('common', 'JS'),
self::TYPE_CSS => Yii::t('common', 'CSS'),
self::TYPE_HTML => Yii::t('common', 'HTML'),
self::TYPE_HTML_BLOCK => Yii::t('common', 'HTML BLOCK'),
];
}
}
<?php
/**
* Created by PhpStorm.
* User: dianc
* Date: 03-09-2016
* Time: 11:21
*/
namespace common\models;
use Yii;
use yii\helpers\ArrayHelper;
class Model extends \yii\base\Model
{
/**
* Creates and populates a set of models.
*
* @param string $modelClass
* @param array $multipleModels
* @return array
*/
public static function createMultiple($modelClass, $multipleModels = [])
{
$model = new $modelClass;
$formName = $model->formName();
$post = Yii::$app->request->post($formName);
$models = [];
if (! empty($multipleModels)) {
$keys = array_keys(ArrayHelper::map($multipleModels, 'id', 'id'));
$multipleModels = array_combine($keys, $multipleModels);
}
if ($post && is_array($post)) {
foreach ($post as $i => $item) {
if (isset($item['id']) && !empty($item['id']) && isset($multipleModels[$item['id']])) {
$models[] = $multipleModels[$item['id']];
} else {
$models[] = new $modelClass;
}
}
}
unset($model, $formName, $post);
return $models;
}
}
......@@ -47,7 +47,8 @@
"bower-asset/html5shiv": "^3.0",
"bower-asset/jquery-slimscroll": "^1.3",
"bower-asset/flot": "^0.8",
"symfony/process": "^3.0"
"symfony/process": "^3.0",
"wbraganca/yii2-dynamicform": "*"
},
"require-dev": {
"yiisoft/yii2-debug": "^2.0.0",
......
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