Commit 5c3ec24b by Junaid Rahman pv

edited theme module

parent 13cf417b
......@@ -9,6 +9,12 @@ use backend\modules\theme\models\search\ThemeSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
use common\models\Model;
use yii\httpclient\Response;
use yii\widgets\ActiveForm;
use app\models\UploadForm;
use yii\web\UploadedFile;
/**
* ThemeController implements the CRUD actions for Theme model.
......@@ -126,9 +132,9 @@ class ThemeController extends Controller
}
}
public function actionFiles($id)
{
$model = $this->findModel($id);
$modelFiles = $model->themeFiles;
......@@ -177,9 +183,11 @@ class ThemeController extends Controller
}
}
$categoryFiles = $this->findCategoryFiles($model->category_id);
return $this->render('file', [
'model' => $model,
'modelFiles' => (empty($modelFiles)) ? [new ThemeFiles()] : $modelFiles
'modelFiles' => (empty($modelFiles)) ? [new ThemeFiles()] : $modelFiles,
'categoryFiles' =>$categoryFiles
]);
}
......@@ -232,4 +240,25 @@ class ThemeController extends Controller
'modelFiles' => (empty($modelFiles)) ? [new ThemeFiles()] : $modelFiles
]);
}
public function actionUpload()
{
$model = new UploadForm();
if (Yii::$app->request->isPost) {
$model->imageFile = UploadedFile::getInstance($model, 'imageFile');
if ($model->upload()) {
echo 'file is uploaded successfully';
return true;
}
}
return $this->render('view', ['model' => $model]);
}
protected function findCategoryFiles($id){
$fileDetails = Yii::$app->db->createCommand('SELECT * FROM lw_category_files WHERE category_id=' . $id)
->queryAll();
return $fileDetails;
}
}
<?php
/**
* Created by PhpStorm.
* User: Junaid Rahman
* Date: 20-09-2016
* Time: 05:08 PM
*/
namespace app\models;
use yii;
use yii\base\Model;
use yii\web\UploadedFile;
class UploadForm extends Model
{
/**
* @var UploadedFile
*/
public $themeFile;
public function rules()
{
return [
[['themeFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'js, css, html'],
];
}
public function upload()
{
if ($this->validate()) {
$this->themeFile->saveAs(Yii::$app->params['uploadPath']);
return true;
} else {
return false;
}
}
}
\ No newline at end of file
......@@ -2,6 +2,7 @@
/* @var $this yii\web\View */
/* @var $model common\models\Theme */
/* @var $modelFiles common\models\ThemeFiles */
/* @var $categoryFiles array */
$this->params['themeId'] = false;
$this->title = 'Create Theme File';
......@@ -14,6 +15,7 @@ $this->params['breadcrumbs'][] = $this->title;
<?= $this->render('..\theme_file\_form', [
'model' => $model,
'modelFiles' => $modelFiles,
'categoryFiles' =>$categoryFiles,
]) ?>
</div>
......@@ -18,7 +18,7 @@ Menu::widget([
[
'label' => 'Files',
'url' => $this->params['themeId'] ?
['theme/file', 'id' => $this->params['themeId']]
['theme/files', 'id' => $this->params['themeId']]
: '#',
'options' => ['class' => $this->params['themeId'] ? '' : 'disabled']
],
......
<?php
/* @var $modelFiles common\models\ThemeFiles */
/* @var $categoryFiles array */
?>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use wbraganca\dynamicform\DynamicFormWidget;
?>
<div class="customer-form">
<?php $form = ActiveForm::begin(['id' => 'dynamic-form']); ?>
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'],'id' => 'dynamic-form']); ?>
<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
'model' => $modelFiles[0],
'formId' => 'dynamic-form',
'formFields' => [
'theme_code',
'file_code',
'base_url',
'base_path',
],
]); ?>
<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">
<table>
<tr>
<th>File Name</th>
<th>Options</th>
</tr>
<?php foreach ($categoryFiles as $value) { ?>
<tr>
<td><?= $value['name'] ?></td>
<?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::getTheme(), ['prompt' => '']) ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php DynamicFormWidget::end(); ?>
$filePath = yii::getAlias('@storage/private/theme-assets/');
$folderName = $model->code . '/';//theme code
$fileCode = $value['code'];//file code
$file = $filePath . $folderName . $fileCode;
Yii::$app->params['uploadPath'] = $file;//saving to global parameter
if (file_exists($file)) { ?>
<td><?= 'edit' ?></td>
<?php } else { ?>
<td><?= $form->field($model, 'themeFile')->fileInput() ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
</div>
</div>
......
......@@ -3,8 +3,6 @@
namespace common\models;
use Yii;
use yii\behaviors\SluggableBehavior;
use yii\behaviors\TimestampBehavior;
/**
* This is the model class for table "{{%theme}}".
......@@ -21,11 +19,17 @@ use yii\behaviors\TimestampBehavior;
*
* @property Category $category
* @property ThemeFiles[] $themeFiles
* @property Website[] $websites
*/
class Theme extends \yii\db\ActiveRecord
{
const STATUS_ACTIVE = 1;
const STATUS_IN_ACTIVE = 2;
const STATUS_IN_ACTIVE = 0;
/*
* @upload file
*/
public $themeFile;
/**
* @inheritdoc
*/
......@@ -40,7 +44,7 @@ class Theme extends \yii\db\ActiveRecord
public function rules()
{
return [
[['category_id', 'code', 'name', 'status'], 'required'],
[['category_id', 'code', 'slug', 'name', 'status'], 'required'],
[['category_id', 'status', 'created_at', 'updated_at'], 'integer'],
[['code', 'slug', 'name'], 'string', 'max' => 32],
[['description'], 'string', 'max' => 512],
......@@ -48,37 +52,21 @@ class Theme extends \yii\db\ActiveRecord
];
}
public function behaviors()
{
return [
[
'class' => SluggableBehavior::className(),
'attribute' => 'name',
],
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at'
]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'category_id' => Yii::t('app', 'Category'),
'code' => Yii::t('app', 'Code'),
'slug' => Yii::t('app', 'Slug'),
'name' => Yii::t('app', 'Name'),
'description' => Yii::t('app', 'Description'),
'status' => Yii::t('app', 'Status'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
'id' => Yii::t('common', 'ID'),
'category_id' => Yii::t('common', 'Category ID'),
'code' => Yii::t('common', 'Code'),
'slug' => Yii::t('common', 'Slug'),
'name' => Yii::t('common', 'Name'),
'description' => Yii::t('common', 'Description'),
'status' => Yii::t('common', 'Status'),
'created_at' => Yii::t('common', 'Created At'),
'updated_at' => Yii::t('common', 'Updated At'),
];
}
......@@ -95,7 +83,15 @@ class Theme extends \yii\db\ActiveRecord
*/
public function getThemeFiles()
{
return $this->hasMany(ThemeFiles::className(), ['theme_id' => 'id']);
return $this->hasMany(ThemeFiles::className(), ['theme_code' => 'id']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getWebsites()
{
return $this->hasMany(Website::className(), ['theme_id' => 'id']);
}
/**
......@@ -106,7 +102,10 @@ class Theme extends \yii\db\ActiveRecord
{
return new \common\models\query\ThemeQuery(get_called_class());
}
/**
* List all status
* @return array
*/
public static function statuses()
{
return [
......
......@@ -8,17 +8,19 @@ use Yii;
* This is the model class for table "{{%theme_files}}".
*
* @property integer $id
* @property integer $theme_id
* @property string $file_id
* @property integer $theme_code
* @property integer $file_code
* @property string $base_url
* @property string $base_path
* @property integer $created_at
* @property integer $updated_at
*
* @property Theme $theme
* @property CategoryFiles $fileCode
* @property Theme $themeCode
*/
class ThemeFiles extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
......@@ -33,11 +35,11 @@ class ThemeFiles extends \yii\db\ActiveRecord
public function rules()
{
return [
[['theme_id', 'file_id'], 'required'],
[['theme_id', 'created_at', 'updated_at'], 'integer'],
[['file_id'], 'string', 'max' => 32],
[['theme_code', 'file_code'], 'required'],
[['theme_code', 'file_code', 'created_at', 'updated_at'], 'integer'],
[['base_url', 'base_path'], 'string', 'max' => 1024],
[['theme_id'], 'exist', 'skipOnError' => true, 'targetClass' => Theme::className(), 'targetAttribute' => ['theme_id' => 'id']],
[['file_code'], 'exist', 'skipOnError' => true, 'targetClass' => CategoryFiles::className(), 'targetAttribute' => ['file_code' => 'id']],
[['theme_code'], 'exist', 'skipOnError' => true, 'targetClass' => Theme::className(), 'targetAttribute' => ['theme_code' => 'id']],
];
}
......@@ -47,22 +49,30 @@ class ThemeFiles extends \yii\db\ActiveRecord
public function attributeLabels()
{
return [
'id' => Yii::t('app', 'ID'),
'theme_id' => Yii::t('app', 'Theme ID'),
'file_id' => Yii::t('app', 'File ID'),
'base_url' => Yii::t('app', 'Base Url'),
'base_path' => Yii::t('app', 'Base Path'),
'created_at' => Yii::t('app', 'Created At'),
'updated_at' => Yii::t('app', 'Updated At'),
'id' => Yii::t('common', 'ID'),
'theme_code' => Yii::t('common', 'Theme Code'),
'file_code' => Yii::t('common', 'File Code'),
'base_url' => Yii::t('common', 'Base Url'),
'base_path' => Yii::t('common', 'Base Path'),
'created_at' => Yii::t('common', 'Created At'),
'updated_at' => Yii::t('common', 'Updated At'),
];
}
/**
* @return \yii\db\ActiveQuery
*/
public function getTheme()
public function getFileCode()
{
return $this->hasOne(CategoryFiles::className(), ['id' => 'file_code']);
}
/**
* @return \yii\db\ActiveQuery
*/
public function getThemeCode()
{
return $this->hasOne(Theme::className(), ['id' => 'theme_id']);
return $this->hasOne(Theme::className(), ['id' => 'theme_code']);
}
/**
......
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