Commit 16402987 by Junaid Rahman pv

theme module: added file upload option

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