Commit 2fb5a7579e679c3920ce8d5ddd68e51b13faa69e
1 parent
31c39e98
add menu and check price view and action (simple version)
Showing
9 changed files
with
76 additions
and
17 deletions
Show diff stats
backend/components/parsers/CustomConverter.php
| ... | ... | @@ -9,7 +9,7 @@ class CustomConverter extends Converter { |
| 9 | 9 | * @param $key_array - ключи для вложенного массива |
| 10 | 10 | * @return array - таблица с проименованными колонками |
| 11 | 11 | */ |
| 12 | - public static function convertToAssocArray ($value_arr, $key_array, $key_prefix = '') | |
| 12 | + public static function convertToAssocArray ( array $value_arr, array $key_array, $key_prefix = '') | |
| 13 | 13 | { |
| 14 | 14 | // очистка служебного префикса в массиве заголовков |
| 15 | 15 | if ($key_prefix) { |
| ... | ... | @@ -44,7 +44,12 @@ class CustomConverter extends Converter { |
| 44 | 44 | return $result; |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - public function addColumns ($value_arr, $add_array) | |
| 47 | + /** | |
| 48 | + * @param $value_arr - двумерный массив к которому нужно добавить колонки | |
| 49 | + * @param $add_array - массив с колонками (ключи) и занчениями колонок | |
| 50 | + * @return mixed | |
| 51 | + */ | |
| 52 | + public function addColumns ( array $value_arr , array $add_array ) | |
| 48 | 53 | { |
| 49 | 54 | $i = 0; |
| 50 | 55 | while ($i < count($value_arr)) { | ... | ... |
backend/components/parsers/CustomCsvParser.php
| ... | ... | @@ -11,7 +11,7 @@ namespace backend\components\parsers; |
| 11 | 11 | |
| 12 | 12 | class CustomCsvParser extends \yii\multiparser\CsvParser { |
| 13 | 13 | |
| 14 | - public $last_line = 30; | |
| 14 | + public $last_line = 10; | |
| 15 | 15 | //public $hasHeaderRow = true; |
| 16 | 16 | // public $keys = ['first','second', 'third', 'forth', 'fifth']; |
| 17 | 17 | public function setupConverter() | ... | ... |
backend/controllers/ParserController.php
| ... | ... | @@ -2,6 +2,7 @@ |
| 2 | 2 | namespace backend\controllers; |
| 3 | 3 | |
| 4 | 4 | use Yii; |
| 5 | +use yii\data\ActiveDataProvider; | |
| 5 | 6 | use yii\filters\AccessControl; |
| 6 | 7 | use backend\components\base\BaseController; |
| 7 | 8 | use yii\filters\VerbFilter; |
| ... | ... | @@ -14,6 +15,7 @@ use backend\models\Details; |
| 14 | 15 | use backend\models\ImporterFiles; |
| 15 | 16 | use backend\models\Importer; |
| 16 | 17 | use yii\base\ErrorException; |
| 18 | +use yii\db\Query; | |
| 17 | 19 | |
| 18 | 20 | use common\components\CustomVarDamp; |
| 19 | 21 | |
| ... | ... | @@ -34,7 +36,7 @@ class ParserController extends BaseController |
| 34 | 36 | 'class' => AccessControl::className(), |
| 35 | 37 | 'rules' => [ |
| 36 | 38 | [ |
| 37 | - 'actions' => ['index', 'results', 'write'], | |
| 39 | + 'actions' => ['index', 'results', 'write','check_price'], | |
| 38 | 40 | 'allow' => true, |
| 39 | 41 | 'roles' => ['@'], |
| 40 | 42 | ], |
| ... | ... | @@ -77,11 +79,11 @@ class ParserController extends BaseController |
| 77 | 79 | $model->file = UploadedFile::getInstance($model, 'file'); |
| 78 | 80 | // первый проход - валидируем, сохраняем файл, ложим в кеш отпарсенные данные и параметры модели (потом при записи в базу данных они пригодятся) |
| 79 | 81 | if ($model->validate()) { |
| 80 | - $filePath = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; | |
| 82 | + $model->file_path = Yii::getAlias('@webroot') . '/uploads/' . $model->file->baseName . '.' . $model->file->extension; | |
| 81 | 83 | |
| 82 | - $model->file->saveAs($filePath); | |
| 84 | + $model->file->saveAs($model->file_path); | |
| 83 | 85 | //запускаем парсинг |
| 84 | - $data = $model->readFile($filePath); | |
| 86 | + $data = $model->readFile(); | |
| 85 | 87 | // сохраняем в кеш отпарсенные даные |
| 86 | 88 | Yii::$app->getCache()->set('parser_data', json_encode($data)); |
| 87 | 89 | // сохраняем в кеш модель - в ней настройки для дальнейшей обработки данных |
| ... | ... | @@ -188,13 +190,14 @@ class ParserController extends BaseController |
| 188 | 190 | if (!$imp_model->save()) { |
| 189 | 191 | CustomVarDamp::dumpAndDie( $imp_model->getErrors() ); |
| 190 | 192 | } |
| 191 | - | |
| 193 | + $configuration['success'] = true; | |
| 192 | 194 | // все прошло успешно - очищаем кеш |
| 193 | 195 | Yii::$app->getCache()->delete('parser_data'); |
| 194 | 196 | Yii::$app->getCache()->delete('parser_configuration'); |
| 195 | 197 | // @todo - Delete the file |
| 196 | 198 | |
| 197 | - CustomVarDamp::dumpAndDie('!!!'); | |
| 199 | + // все успешно - возвращаемся в начало | |
| 200 | + return $this->render('index', ['model' => $configuration]); | |
| 198 | 201 | } catch (ErrorException $e) { |
| 199 | 202 | CustomVarDamp::dump($e->getMessage()); |
| 200 | 203 | } |
| ... | ... | @@ -215,4 +218,18 @@ class ParserController extends BaseController |
| 215 | 218 | |
| 216 | 219 | } |
| 217 | 220 | |
| 221 | + public function actionCheck_price () | |
| 222 | + { | |
| 223 | + $query = (new Query())->select('*')->from('{{%importer_files}}')->where(['not', ['time_end' => null]])->orderBy(['upload_time' => SORT_DESC]); | |
| 224 | + | |
| 225 | + $provider = new ActiveDataProvider([ | |
| 226 | + 'query' => $query, | |
| 227 | + 'pagination' => [ | |
| 228 | + 'pageSize' => 10, | |
| 229 | + ], | |
| 230 | + ]); | |
| 231 | + return $this->render('checkPrice', | |
| 232 | + [ | |
| 233 | + 'dataProvider' => $provider]); | |
| 234 | + } | |
| 218 | 235 | } | ... | ... |
backend/models/Importer.php
| ... | ... | @@ -47,8 +47,8 @@ class Importer extends BaseActiveRecord |
| 47 | 47 | public function rules() |
| 48 | 48 | { |
| 49 | 49 | return [ |
| 50 | - [['code', 'name', 'currency_id', 'delivery', 'info', 'price_date_update'], 'required'], | |
| 51 | - [['name_price', 'email', 'PARSER_FIELD_SIGN'], 'safe'], | |
| 50 | + [['code', 'name', 'currency_id', 'delivery', 'price_date_update'], 'required'], | |
| 51 | + [['name_price', 'email', 'PARSER_FIELD_SIGN', 'info'], 'safe'], | |
| 52 | 52 | [['currency_id', 'active', 'PARSER_IS_ACTIVE', 'PARSER_COLUMN_COUNT', 'PARSER_FIELD_BRAND', 'PARSER_FIELD_ARTICLE', 'PARSER_FIELD_ARTICLE_PREFIX', 'PARSER_FIELD_PRICE', 'PARSER_FIELD_DESCR', 'PARSER_FIELD_BOX', 'PARSER_FIELD_ADD_BOX', 'PARSER_FIELD_GROUP_RG'], 'integer'], |
| 53 | 53 | [['info'], 'string'], |
| 54 | 54 | [['PARSER_FIELD_MULTIPLIER'], 'number'], | ... | ... |
backend/models/UploadFileParsingForm.php
| ... | ... | @@ -14,13 +14,17 @@ class UploadFileParsingForm extends Model |
| 14 | 14 | /** |
| 15 | 15 | * @var UploadedFile file attribute |
| 16 | 16 | */ |
| 17 | + // атрибуты формы | |
| 17 | 18 | public $file; |
| 18 | 19 | public $importer_id; |
| 19 | 20 | public $action; |
| 20 | 21 | public $delimiter; |
| 21 | 22 | public $delete_price; |
| 22 | 23 | public $delete_prefix; |
| 23 | - public $update_date; | |
| 24 | + | |
| 25 | + // служебные атрибуты | |
| 26 | + public $file_path; | |
| 27 | + public $success; | |
| 24 | 28 | |
| 25 | 29 | /** |
| 26 | 30 | * @return array the validation rules. |
| ... | ... | @@ -34,10 +38,10 @@ class UploadFileParsingForm extends Model |
| 34 | 38 | [['file'], 'file'],// 'extensions' => ['csv', 'xml'] ], |
| 35 | 39 | // 'wrongMimeType' => 'Указан неподдерживаемый тип файла. Можно выбирать csv, xml файлы.' ], |
| 36 | 40 | ['importer_id', 'integer','max' => 999999, 'min' => 0 ], |
| 37 | - [['action','delete_prefix', 'delete_price'], 'boolean'], | |
| 41 | + [['action','delete_prefix', 'delete_price', 'success'], 'boolean'], | |
| 38 | 42 | ['delimiter', 'string', 'max' => 1], |
| 39 | 43 | ['delimiter', 'default', 'value' => ';'], |
| 40 | - ['update_date', 'safe'] | |
| 44 | + [ 'success', 'default', 'value' => false] | |
| 41 | 45 | |
| 42 | 46 | ]; |
| 43 | 47 | } |
| ... | ... | @@ -51,9 +55,9 @@ class UploadFileParsingForm extends Model |
| 51 | 55 | ]; |
| 52 | 56 | } |
| 53 | 57 | |
| 54 | - public function readFile($filePath){ | |
| 58 | + public function readFile(){ | |
| 55 | 59 | |
| 56 | - $data = Yii::$app->multiparser->parse($filePath); | |
| 60 | + $data = Yii::$app->multiparser->parse( $this->file_path ); | |
| 57 | 61 | if( !is_array($data) ){ |
| 58 | 62 | $data = ['No results']; |
| 59 | 63 | } | ... | ... |
backend/views/layouts/column.php
| ... | ... | @@ -282,7 +282,11 @@ $this->beginContent('@app/views/layouts/main.php'); |
| 282 | 282 | echo Menu::widget([ |
| 283 | 283 | 'options' => ['class' => 'sidebar-menu'], |
| 284 | 284 | 'items' => [ |
| 285 | - ['label' => "Прайс парсер", 'url' => ['parser/index']], | |
| 285 | + ['label' => "Загрузка файлов", 'url' => ['#'], 'items' => [ | |
| 286 | + ['label' => 'Ручная загрузка', 'url' => ['parser/index']], | |
| 287 | + ['label' => 'Проверка прайс файлов', 'url' => ['parser/check_price']], | |
| 288 | + ], | |
| 289 | + ], | |
| 286 | 290 | ['label' => 'Управление ролями', 'url' => ['#'], 'items' => [ |
| 287 | 291 | ['label' => 'Покупатели', 'url' => '#'], |
| 288 | 292 | ['label' => 'Поставщики', 'url' => '#'], | ... | ... |
| 1 | +<?php | |
| 2 | +use yii\helpers\Html; | |
| 3 | +use yii\grid\GridView; | |
| 4 | +use yii\grid\SerialColumn; | |
| 5 | +use yii\grid\DataColumn; | |
| 6 | + | |
| 7 | + | |
| 8 | +/* @var $this yii\web\View */ | |
| 9 | +/* @var $searchModel backend\models\CatalogSearch */ | |
| 10 | +/* @var $dataProvider yii\data\ActiveDataProvider */ | |
| 11 | + | |
| 12 | +$this->title = 'Results'; | |
| 13 | +$this->params['breadcrumbs'][] = $this->title; | |
| 14 | +?> | |
| 15 | +<div class="catalog-index"> | |
| 16 | + | |
| 17 | + <h1><?= Html::encode($this->title) ?></h1> | |
| 18 | + | |
| 19 | + <?= GridView::widget( ['dataProvider' => $dataProvider, | |
| 20 | + 'columns' => [['class' => SerialColumn::className()], | |
| 21 | + 'importer_id', | |
| 22 | + 'upload_time' ]] );?> | |
| 23 | + | |
| 24 | +</div> | |
| 0 | 25 | \ No newline at end of file | ... | ... |
backend/views/parser/index.php
| ... | ... | @@ -11,6 +11,10 @@ use yii\helpers\ArrayHelper; |
| 11 | 11 | if (!$model->action) { |
| 12 | 12 | $model->action = 1; |
| 13 | 13 | } |
| 14 | + if ($model->success) { // вернулись после успешной загрузки данного файла | |
| 15 | + //echo "<h4>Файл успешно загружен</h4>"; | |
| 16 | + echo Html::tag('h3', 'Файл успешно загружен',['class'=>'bg-success']); | |
| 17 | + } | |
| 14 | 18 | ?> |
| 15 | 19 | <h3>Загрузка прайсов поставщиков</h3> |
| 16 | 20 | ... | ... |