Commit 16402987 by Junaid Rahman pv

theme module: added file upload option

parent 1adbe559
...@@ -138,7 +138,6 @@ class ThemeController extends Controller ...@@ -138,7 +138,6 @@ class ThemeController extends Controller
$modelFiles = $model->themeFiles; $modelFiles = $model->themeFiles;
if ($model->load(Yii::$app->request->post())) { //&& $model->save() if ($model->load(Yii::$app->request->post())) { //&& $model->save()
$oldIDs = ArrayHelper::map($modelFiles, 'id', 'id'); $oldIDs = ArrayHelper::map($modelFiles, 'id', 'id');
$modelFiles = Model::createMultiple(ThemeFiles::classname(), $modelFiles); $modelFiles = Model::createMultiple(ThemeFiles::classname(), $modelFiles);
Model::loadMultiple($modelFiles, Yii::$app->request->post()); Model::loadMultiple($modelFiles, Yii::$app->request->post());
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace backend\modules\theme\controllers; namespace backend\modules\theme\controllers;
use common\models\ThemeFiles;
use yii; use yii;
use common\models\Theme; use common\models\Theme;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
...@@ -23,42 +24,38 @@ class ThemeFileController extends \yii\web\Controller ...@@ -23,42 +24,38 @@ class ThemeFileController extends \yii\web\Controller
*/ */
protected function findModel($id) protected function findModel($id)
{ {
if (($model = Theme::findOne($id)) !== null) { if (($model = ThemeFiles::findOne($id)) !== null) {
return $model; return $model;
} else { } else {
throw new NotFoundHttpException('The requested page does not exist.'); throw new NotFoundHttpException('The requested page does not exist.');
} }
} }
public function actionUpload($id){ public function actionUpload()
{
$model = $this->findModel($id); $model = new ThemeFiles();
$modelFiles = $model->themeFiles;
if ($model->load(Yii::$app->request->post())) { //&& $model->save() if ($model->load(Yii::$app->request->post())) { //&& $model->save()
// get the uploaded file instance. for multiple file uploads // get the uploaded file instance. for multiple file uploads
// the following data will return an array // the following data will return an array
$theme_file = UploadedFile::getInstance($model, 'theme-file'); $theme_file = UploadedFile::getInstance($model, 'file');
// the path to save file, you can set an uploadPath // the path to save file,
$path = $model->file; $folderPath = yii::getAlias('@storage/private/theme-assets/') .$model->theme_code;
if ($path) { if ( ! is_dir($folderPath)) {
mkdir($folderPath);
echo 'a';
if ($model->save()) {
$theme_file->saveAs($path);
return $this->redirect(['view', 'id' => $model->id]);
} else {
// error in saving model
} }
$path = $folderPath.'/'.$model->file_code;
if ($model->save(false)) {
$theme_file->saveAs($path);
} else { } else {
echo 'aaa'; echo 'Error';
echo $theme_file->id;
exit(); exit();
} }
} }
return $this->render('upload');
return $this->redirect(['theme/index']);
} }
} }
...@@ -12,8 +12,6 @@ use yii\helpers\Url; ...@@ -12,8 +12,6 @@ use yii\helpers\Url;
<div class="customer-form"> <div class="customer-form">
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], 'id' => 'dynamic-form']); ?>
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-body"> <div class="panel-body">
<table> <table>
...@@ -56,11 +54,10 @@ use yii\helpers\Url; ...@@ -56,11 +54,10 @@ use yii\helpers\Url;
?> ?>
<div id='modalContent' .<?= $value['id'] ?> style="align-content: center"> <div id='modalContent' .<?= $value['id'] ?> style="text-align: center">
<?= $this->render('upload', [ <?= $this->render('upload', [
'model' => $model, 'model' => $modelFiles[0],
'modelFiles' => $modelFiles,
]) ?> ]) ?>
</div> </div>
...@@ -84,10 +81,5 @@ use yii\helpers\Url; ...@@ -84,10 +81,5 @@ use yii\helpers\Url;
</div> </div>
</div> </div>
<div class="form-group">
<?= Html::submitButton('Save', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div> </div>
...@@ -6,30 +6,47 @@ ...@@ -6,30 +6,47 @@
* Time: 01:15 PM * Time: 01:15 PM
*/ */
/* @var $modelFiles common\models\ThemeFiles */ /* @var $model common\models\ThemeFiles */
use yii\widgets\ActiveForm; use yii\widgets\ActiveForm;
use yii\bootstrap\Html; use yii\bootstrap\Html;
?> ?>
<?php $form = ActiveForm::begin(); ?> <?php $form = ActiveForm::begin([
'options' => ['enctype' => 'multipart/form-data'],
'id' => 'dynamic-form',
'action' => '../theme-file/upload'
]); ?>
<div class="conatiner">
<div id="myTabContent" class="tab-content">
<div class="row"> <div class="row">
<div class="col-md-6"> <div class="col-md-11">
<p>Choose your file to upload</p>
<?= $form->field($model, 'file')->fileInput(['class' => 'btn btn-default']) ?>
</div> </div>
<div class="col-md-11">
<?= $form->field($model, 'file_code')->hiddenInput(['value' => $model->file_code])->label(false) ?>
</div> </div>
<div class="row"> <div class="col-md-11">
<div class="col-md-6">
<?= $form->field($modelFiles[0], 'file')->fileInput(['class'=>'btn btn-primary']) ?> <?= $form->field($model, 'theme_code')->hiddenInput(['value' => $model->theme_code])->label(false) ?>
<?= $modelFiles[0]->file_code?>
</div> </div>
</div> </div>
<div class="form-group"> <div class="form-group">
<?= Html::submitButton() ?> <?= Html::submitButton(Yii::t('backend', 'Upload'),
['class' => 'btn btn-primary']) ?>
</div> </div>
</div>
<?php ActiveForm::end(); ?> <?php ActiveForm::end(); ?>
\ No newline at end of file
...@@ -27,7 +27,7 @@ class m160830_105840_category extends Migration ...@@ -27,7 +27,7 @@ class m160830_105840_category extends Migration
$this->createTable('{{%category_files}}', [ $this->createTable('{{%category_files}}', [
'id' => $this->primaryKey(), 'id' => $this->primaryKey(),
'category_id' => $this->integer()->notNull(), 'category_id' => $this->integer()->notNull(),
'code' => $this->string(32)->notNull(), 'code' => $this->string(11)->notNull(),
'type' => $this->integer()->notNull(), 'type' => $this->integer()->notNull(),
'name' => $this->string(32)->notNull(), 'name' => $this->string(32)->notNull(),
'created_at' => $this->integer(), 'created_at' => $this->integer(),
......
...@@ -15,7 +15,7 @@ class m160903_101233_theme extends Migration ...@@ -15,7 +15,7 @@ class m160903_101233_theme extends Migration
$this->createTable('{{%theme}}', [ $this->createTable('{{%theme}}', [
'id' => $this->primaryKey(), 'id' => $this->primaryKey(),
'category_id' => $this->integer()->notNull(), 'category_id' => $this->integer()->notNull(),
'code' => $this->string(32)->notNull(), 'code' => $this->string(11)->notNull(),
'slug' => $this->string(32)->notNull(), 'slug' => $this->string(32)->notNull(),
'name' => $this->string(32)->notNull(), 'name' => $this->string(32)->notNull(),
'description' => $this->string(512), 'description' => $this->string(512),
...@@ -26,16 +26,16 @@ class m160903_101233_theme extends Migration ...@@ -26,16 +26,16 @@ class m160903_101233_theme extends Migration
$this->createTable('{{%theme_files}}', [ $this->createTable('{{%theme_files}}', [
'id' => $this->primaryKey(), 'id' => $this->primaryKey(),
'theme_code' => $this->integer()->notNull(), 'theme_code' => $this->string(11)->notNull(),
'file_code' => $this->integer()->notNull(), 'file_code' => $this->string(11)->notNull(),
'created_at' => $this->integer(), 'created_at' => $this->integer(),
'updated_at' => $this->integer() 'updated_at' => $this->integer()
], $tableOptions); ], $tableOptions);
$this->addForeignKey('fk_theme_category', '{{%theme}}', 'category_id', '{{%category}}', 'id', 'cascade', 'cascade'); $this->addForeignKey('fk_theme_category', '{{%theme}}', 'category_id', '{{%category}}', 'id', 'cascade', 'cascade');
$this->addForeignKey('fk_theme_files_theme', '{{%theme_files}}', 'theme_code', '{{%theme}}', 'id', 'cascade', 'cascade'); $this->addForeignKey('fk_theme_files_theme', '{{%theme_files}}', 'theme_code', '{{%theme}}', 'code', 'cascade', 'cascade');
$this->addForeignKey('fk_theme_category_files', '{{%theme_files}}', 'file_code', '{{%category_files}}', 'id', 'cascade', 'cascade'); $this->addForeignKey('fk_theme_category_files', '{{%theme_files}}', 'file_code', '{{%category_files}}', 'code', 'cascade', 'cascade');
} }
public function safeDown() public function safeDown()
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace common\models; namespace common\models;
use Yii; use Yii;
use yii\behaviors\TimestampBehavior;
/** /**
* This is the model class for table "{{%theme_files}}". * This is the model class for table "{{%theme_files}}".
...@@ -33,15 +34,25 @@ class ThemeFiles extends \yii\db\ActiveRecord ...@@ -33,15 +34,25 @@ class ThemeFiles extends \yii\db\ActiveRecord
return '{{%theme_files}}'; return '{{%theme_files}}';
} }
public function behaviors()
{
return [
[
'class' => TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at'
]
];
}
/** /**
* @inheritdoc * @inheritdoc
*/ */
public function rules() public function rules()
{ {
return [ return [
[['theme_code', 'file_code'], 'required'], [['theme_code', 'file_code','file'], 'required'],
[['theme_code', 'file_code', 'created_at', 'updated_at'], 'integer'], [['theme_code', 'file_code'], 'string'],
[['base_url', 'base_path'], 'string', 'max' => 1024], [['created_at', 'updated_at'], 'integer'],
[['file_code'], 'exist', 'skipOnError' => true, 'targetClass' => CategoryFiles::className(), 'targetAttribute' => ['file_code' => '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']], [['theme_code'], 'exist', 'skipOnError' => true, 'targetClass' => Theme::className(), 'targetAttribute' => ['theme_code' => 'id']],
[['file'],'file'], [['file'],'file'],
...@@ -61,6 +72,7 @@ class ThemeFiles extends \yii\db\ActiveRecord ...@@ -61,6 +72,7 @@ class ThemeFiles extends \yii\db\ActiveRecord
'base_path' => Yii::t('common', 'Base Path'), 'base_path' => Yii::t('common', 'Base Path'),
'created_at' => Yii::t('common', 'Created At'), 'created_at' => Yii::t('common', 'Created At'),
'updated_at' => Yii::t('common', 'Updated At'), 'updated_at' => Yii::t('common', 'Updated At'),
'file' => Yii::t('common','Choose your file to upload')
]; ];
} }
...@@ -80,7 +92,7 @@ class ThemeFiles extends \yii\db\ActiveRecord ...@@ -80,7 +92,7 @@ class ThemeFiles extends \yii\db\ActiveRecord
return $this->hasOne(Theme::className(), ['id' => 'theme_code']); return $this->hasOne(Theme::className(), ['id' => 'theme_code']);
} }
public function getFilePath($theme_code,$file_code) public static function getFilePath($theme_code,$file_code)
{ {
return yii::getAlias('@storage/private/theme-assets/') .$theme_code . '/' .$file_code; return yii::getAlias('@storage/private/theme-assets/') .$theme_code . '/' .$file_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