Commit 010258b8 by Junaid Rahman pv

edited theme module

parent f962472a
...@@ -140,49 +140,74 @@ class ThemeController extends Controller ...@@ -140,49 +140,74 @@ class ThemeController extends Controller
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());
$deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelFiles, 'id', 'id'))); // $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelFiles, 'id', 'id')));
//
// ajax validation // // ajax validation
if (Yii::$app->request->isAjax) { // if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON; // Yii::$app->response->format = Response::FORMAT_JSON;
return ArrayHelper::merge( // return ArrayHelper::merge(
ActiveForm::validateMultiple($modelFiles), // ActiveForm::validateMultiple($modelFiles),
ActiveForm::validate($model) // ActiveForm::validate($model)
); // );
} // }
//
// validate all models // // validate all models
$valid = $model->validate(); // $valid = $model->validate();
$valid = Model::validateMultiple($modelFiles) && $valid; // $valid = Model::validateMultiple($modelFiles) && $valid;
//
if ($valid) { // if ($valid) {
$transaction = \Yii::$app->db->beginTransaction(); // $transaction = \Yii::$app->db->beginTransaction();
try { // try {
if ($flag = $model->save(false)) { // if ($flag = $model->save(false)) {
if (! empty($deletedIDs)) { // if (! empty($deletedIDs)) {
ThemeFiles::deleteAll(['id' => $deletedIDs]); // ThemeFiles::deleteAll(['id' => $deletedIDs]);
} // }
foreach ($modelFiles as $modelFiles) { // foreach ($modelFiles as $modelFiles) {
$modelFiles->customer_id = $model->id; // $modelFiles->customer_id = $model->id;
if (! ($flag = $modelFiles->save(false))) { // if (! ($flag = $modelFiles->save(false))) {
$transaction->rollBack(); // $transaction->rollBack();
break; // break;
} // }
} // }
} // }
if ($flag) { // if ($flag) {
$transaction->commit(); // $transaction->commit();
return $this->redirect(['file', 'theme_id' => $model->id]); // return $this->redirect(['files', 'id' => $model->id]);
} // }
} catch (Exception $e) { // } catch (Exception $e) {
$transaction->rollBack(); // $transaction->rollBack();
// }
// }
// get the uploaded file instance. for multiple file uploads
// the following data will return an array
$theme_file = UploadedFile::getInstance($model, 'theme_file');
// the path to save file, you can set an uploadPath
// in Yii::$app->params (as used in example below)
$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
} }
}else{
echo 'aaa';
echo $model->file;
exit();
} }
} }
$categoryFiles = $this->findCategoryFiles($model->category_id); $categoryFiles = $this->findCategoryFiles($model->category_id);
return $this->render('file', [ return $this->render('file', [
'model' => $model, 'model' => $model,
...@@ -233,6 +258,10 @@ class ThemeController extends Controller ...@@ -233,6 +258,10 @@ class ThemeController extends Controller
$transaction->rollBack(); $transaction->rollBack();
} }
} }
} }
return $this->render('file', [ return $this->render('file', [
...@@ -240,20 +269,20 @@ class ThemeController extends Controller ...@@ -240,20 +269,20 @@ class ThemeController extends Controller
'modelFiles' => (empty($modelFiles)) ? [new ThemeFiles()] : $modelFiles 'modelFiles' => (empty($modelFiles)) ? [new ThemeFiles()] : $modelFiles
]); ]);
} }
public function actionUpload() // public function actionUpload()
{ // {
$model = new UploadForm(); // $model = new UploadForm();
//
if (Yii::$app->request->isPost) { // if (Yii::$app->request->isPost) {
$model->imageFile = UploadedFile::getInstance($model, 'imageFile'); // $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
if ($model->upload()) { // if ($model->upload()) {
echo 'file is uploaded successfully'; // echo 'file is uploaded successfully';
return true; // return true;
} // }
} // }
//
return $this->render('view', ['model' => $model]); // return $this->render('file', ['model' => $model]);
} // }
protected function findCategoryFiles($id){ protected function findCategoryFiles($id){
......
...@@ -26,19 +26,15 @@ use yii\widgets\ActiveForm; ...@@ -26,19 +26,15 @@ use yii\widgets\ActiveForm;
<td><?= $value['name'] ?></td> <td><?= $value['name'] ?></td>
<?php <?php
$theme_code = $model->code;
$filePath = yii::getAlias('@storage/private/theme-assets/'); $file_code = $value['code'];
$folderName = $model->code . '/';//theme code if ($a = $modelFiles[0]::isFileExist($theme_code,$file_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> <td><?= 'edit' ?></td>
<?php } else { ?> <?php } else { ?>
<td><?= $form->field($model, 'themeFile')->fileInput() ?></td> <td><?= $form->field($model, 'theme_file')->fileInput() ?></td>
<?php } ?> <?php } ?>
......
...@@ -28,7 +28,11 @@ class Theme extends \yii\db\ActiveRecord ...@@ -28,7 +28,11 @@ class Theme extends \yii\db\ActiveRecord
/* /*
* @upload file * @upload file
*/ */
public $themeFile; public $theme_file;
/*
* @upload file
*/
public $file;
/** /**
* @inheritdoc * @inheritdoc
...@@ -49,6 +53,8 @@ class Theme extends \yii\db\ActiveRecord ...@@ -49,6 +53,8 @@ class Theme extends \yii\db\ActiveRecord
[['code', 'slug', 'name'], 'string', 'max' => 32], [['code', 'slug', 'name'], 'string', 'max' => 32],
[['description'], 'string', 'max' => 512], [['description'], 'string', 'max' => 512],
[['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']], [['category_id'], 'exist', 'skipOnError' => true, 'targetClass' => Category::className(), 'targetAttribute' => ['category_id' => 'id']],
[['theme_file'], 'safe'],
[['theme_file'], 'file', 'extensions'=>'js, css, html'],
]; ];
} }
......
...@@ -14,6 +14,7 @@ use Yii; ...@@ -14,6 +14,7 @@ use Yii;
* @property string $base_path * @property string $base_path
* @property integer $created_at * @property integer $created_at
* @property integer $updated_at * @property integer $updated_at
* @property string $filePath
* *
* @property CategoryFiles $fileCode * @property CategoryFiles $fileCode
* @property Theme $themeCode * @property Theme $themeCode
...@@ -75,6 +76,12 @@ class ThemeFiles extends \yii\db\ActiveRecord ...@@ -75,6 +76,12 @@ 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)
{
return yii::getAlias('@storage/private/theme-assets/') .$theme_code . '/' .$file_code;
}
/** /**
* @inheritdoc * @inheritdoc
* @return \common\models\query\ThemeFilesQuery the active query used by this AR class. * @return \common\models\query\ThemeFilesQuery the active query used by this AR class.
...@@ -83,4 +90,9 @@ class ThemeFiles extends \yii\db\ActiveRecord ...@@ -83,4 +90,9 @@ class ThemeFiles extends \yii\db\ActiveRecord
{ {
return new \common\models\query\ThemeFilesQuery(get_called_class()); return new \common\models\query\ThemeFilesQuery(get_called_class());
} }
public static function isFileExist($theme_code,$file_code)
{
return file_exists(self::getFilePath($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