Commit 010258b8 by Junaid Rahman pv

edited theme module

parent f962472a
......@@ -140,49 +140,74 @@ class ThemeController extends Controller
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());
$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)) {
ThemeFiles::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', 'theme_id' => $model->id]);
}
} catch (Exception $e) {
$transaction->rollBack();
// $oldIDs = ArrayHelper::map($modelFiles, 'id', 'id');
// $modelFiles = Model::createMultiple(ThemeFiles::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)) {
// ThemeFiles::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(['files', 'id' => $model->id]);
// }
// } catch (Exception $e) {
// $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);
return $this->render('file', [
'model' => $model,
......@@ -233,6 +258,10 @@ class ThemeController extends Controller
$transaction->rollBack();
}
}
}
return $this->render('file', [
......@@ -240,20 +269,20 @@ 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]);
}
// 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('file', ['model' => $model]);
// }
protected function findCategoryFiles($id){
......
......@@ -26,19 +26,15 @@ use yii\widgets\ActiveForm;
<td><?= $value['name'] ?></td>
<?php
$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)) { ?>
$theme_code = $model->code;
$file_code = $value['code'];
if ($a = $modelFiles[0]::isFileExist($theme_code,$file_code)) { ?>
<td><?= 'edit' ?></td>
<?php } else { ?>
<td><?= $form->field($model, 'themeFile')->fileInput() ?></td>
<td><?= $form->field($model, 'theme_file')->fileInput() ?></td>
<?php } ?>
......
......@@ -28,7 +28,11 @@ class Theme extends \yii\db\ActiveRecord
/*
* @upload file
*/
public $themeFile;
public $theme_file;
/*
* @upload file
*/
public $file;
/**
* @inheritdoc
......@@ -49,6 +53,8 @@ class Theme extends \yii\db\ActiveRecord
[['code', 'slug', 'name'], 'string', 'max' => 32],
[['description'], 'string', 'max' => 512],
[['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;
* @property string $base_path
* @property integer $created_at
* @property integer $updated_at
* @property string $filePath
*
* @property CategoryFiles $fileCode
* @property Theme $themeCode
......@@ -75,6 +76,12 @@ class ThemeFiles extends \yii\db\ActiveRecord
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
* @return \common\models\query\ThemeFilesQuery the active query used by this AR class.
......@@ -83,4 +90,9 @@ class ThemeFiles extends \yii\db\ActiveRecord
{
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