From ae27b007efa5922fd056e8c13723ad49181af527 Mon Sep 17 00:00:00 2001 From: Mihail Date: Mon, 28 Sep 2015 17:36:34 +0300 Subject: [PATCH] add ajax handler for deleting files from server --- backend/assets/CheckPriceAsset.php | 26 -------------------------- backend/assets/ParserAsset.php | 26 ++++++++++++++++++++++++++ backend/controllers/ParserController.php | 45 ++++++++++++++++++++++++++++----------------- backend/views/check-price/index.php | 8 ++++---- backend/views/parser/server-files.php | 28 +++++++++++++--------------- backend/web/js/check-price.js | 9 --------- backend/web/js/parser.js | 35 +++++++++++++++++++++++++++++++++++ 7 files changed, 106 insertions(+), 71 deletions(-) delete mode 100644 backend/assets/CheckPriceAsset.php create mode 100644 backend/assets/ParserAsset.php delete mode 100644 backend/web/js/check-price.js create mode 100644 backend/web/js/parser.js diff --git a/backend/assets/CheckPriceAsset.php b/backend/assets/CheckPriceAsset.php deleted file mode 100644 index 876f02d..0000000 --- a/backend/assets/CheckPriceAsset.php +++ /dev/null @@ -1,26 +0,0 @@ -where(['in', 'id', $arr_id])->orderBy(['upload_time' => SORT_DESC]); + Yii::$app->cache->set( 'files_to_delete',json_encode( $arr_id_files ) ); + $query = ImporterFiles::find()->where(['in', 'id', $arr_id_files])->orderBy(['upload_time' => SORT_DESC]); $provider = new ActiveDataProvider([ 'query' => $query, @@ -289,24 +289,35 @@ class ParserController extends BaseController 'dataProvider' => $provider]); } - public function actionDelete ($id) + public function actionDelete () { - if(Yii::$app->request->isAjax){ - CustomVarDamp::dumpAndDie(1); - } - - $files_model = new ImporterFiles(); - try { + if ( Yii::$app->request->isAjax ) { - $files_model->delete($id); - unlink(Yii::getAlias('@auto_upload') . '/' . $id . '.csv' ); - - } catch (ErrorException $e) { + $files_model = new ImporterFiles(); + if ( isset(Yii::$app->request->post()['id'] )) { + $id = Yii::$app->request->post()['id']; + try { + $files_model->delete($id); + unlink(Yii::getAlias('@auto_upload') . '/' . $id . '.csv' ); + + // удалим этот id и из кэша + if( $arr_id_files = Yii::$app->cache->get( 'files_to_delete' ) ){ + $arr_id_files = json_decode($arr_id_files); + if (isset( $arr_id_files[$id] ) ) { + unset( $arr_id_files[$id] ); + // положем уже обновленный массив + Yii::$app->cache->set( 'files_to_delete',json_encode( $arr_id_files ) ); + } + } + // сообщим скрипту что все ОК + echo 1; + } catch (ErrorException $e) { - CustomVarDamp::dump($e->getMessage()); + CustomVarDamp::dump($e->getMessage()); + } + } } - $this->redirect('server-files'); } } diff --git a/backend/views/check-price/index.php b/backend/views/check-price/index.php index 4070d96..dce29bb 100644 --- a/backend/views/check-price/index.php +++ b/backend/views/check-price/index.php @@ -2,11 +2,10 @@ use yii\helpers\Html; use yii\grid\GridView; use yii\grid\SerialColumn; -use yii\grid\ActionColumn; use yii\widgets\Pjax; use yii\bootstrap\Modal; use yii\helpers\Url; -use backend\assets\CheckPriceAsset; +use backend\assets\ParserAsset; /* @var $this yii\web\View */ @@ -16,7 +15,8 @@ use backend\assets\CheckPriceAsset; $this->title = 'Проверка прайсов'; $this->params['breadcrumbs'][] = $this->title; // зарегистрируем скрипт для обработки загрузки модального окна -CheckPriceAsset::register($this); +ParserAsset::register($this); +Pjax::begin(); ?>
@@ -30,7 +30,6 @@ CheckPriceAsset::register($this); ['content' => function ($model) { $url = Url::to(['view', 'id' => $model->id, 'date_update' => $model->price_date_update]); return Html::a(' Просмотреть ', '#', [ - 'title' => Yii::t('yii', 'Просмотреть прайс'), 'class' => 'modalButton', 'value' => $url, ]); @@ -76,6 +75,7 @@ CheckPriceAsset::register($this); echo "
"; Modal::end(); + Pjax::end(); ?> diff --git a/backend/views/parser/server-files.php b/backend/views/parser/server-files.php index 486f6bc..2bc638a 100644 --- a/backend/views/parser/server-files.php +++ b/backend/views/parser/server-files.php @@ -4,15 +4,17 @@ use yii\grid\GridView; use yii\grid\SerialColumn; use yii\grid\ActionColumn; use yii\widgets\Pjax; +use backend\assets\ParserAsset; /* @var $this yii\web\View */ /* @var $searchModel backend\models\CatalogSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ -$this->title = 'Проверка прайсов'; +$this->title = 'Файлы на сервере'; $this->params['breadcrumbs'][] = $this->title; -Pjax::begin(); +ParserAsset::register($this); +Pjax::begin(['id' => 'server_files_grid']); ?>
@@ -30,19 +32,15 @@ Pjax::begin(); ['label' =>'Дата загрузки', 'attribute' => 'upload_time' ], - ['class' => ActionColumn::className(), - 'template'=>'{delete}', - 'buttons' => [ - 'delete' => function ($url, $model, $key) { - return Html::a('', $url, [ - 'title' => Yii::t('yii', 'Удалить файл'), - 'data-confirm' => 'Вы уверены что хотите удалить этот файл?', - 'data-method' => 'post', - 'data-pjax' => '0', - ]); - }, - ], - ] + ['content' => function ($model) { + //Url::to(['delete', 'id' => $model->id, 'date_update' => $model->price_date_update]); + return Html::a(' Удалить ', '#', [ + 'value' => $model->id, + 'class' => 'deleteLink', + ]); + }, + ], + ]] );?> diff --git a/backend/web/js/check-price.js b/backend/web/js/check-price.js deleted file mode 100644 index ec4fc62..0000000 --- a/backend/web/js/check-price.js +++ /dev/null @@ -1,9 +0,0 @@ -$(function(){ - - // get the click of the create button - $('.modalButton').click(function (){ - $('#modal').modal('show') - .find('#modalContent') - .load($(this).attr('value')); - }); -}); diff --git a/backend/web/js/parser.js b/backend/web/js/parser.js new file mode 100644 index 0000000..bc81bd2 --- /dev/null +++ b/backend/web/js/parser.js @@ -0,0 +1,35 @@ +$(function(){ + + // для каждой строки обрабатываем клик по ссылке + $('.modalButton').click(function (){ + // находим контейнер с модальным контентом и подгружаем в него по оджаксу результат + $('#modal').modal('show') + .find('#modalContent') + .load($(this).attr('value')); // в 'value' - у нас указан путь с гет параметрами к конроллеру + }); + + $('.deleteLink').click(function () + { + if ( confirm('Вы уверены что хотите удалить этот файл?') ){ + var data = $(this).attr('value');// здесь у нас id удаляемой модели + + $.post( 'delete',{'id':data} ) + .done(function(result) { + if(result == 1) + { + $.pjax.reload({container:'#server_files_grid'}); + }else + { + $("#message").html(result); + } + }).fail(function() + { + console.log("server error"); + }); + } + + + + +}); +}); -- libgit2 0.21.4