diff --git a/backend/controllers/CartController.php b/backend/controllers/CartController.php index dcaf984..332883a 100755 --- a/backend/controllers/CartController.php +++ b/backend/controllers/CartController.php @@ -8,7 +8,6 @@ use common\models\CartBillsSearch; use backend\components\base\BaseController; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; -use yii\filters\AccessControl; /** * CartController implements the CRUD actions for CartBills model. @@ -16,34 +15,7 @@ use yii\filters\AccessControl; class CartController extends BaseController { public $layout = "/column"; - /** - * @inheritdoc - */ - public function behaviors() - { - return [ - 'access' => [ - 'class' => AccessControl::className(), - 'rules' => [ - [ - 'actions' => ['login', 'error', 'download-photo','delete-image' ], - 'allow' => true, - ], - [ - 'actions' => ['logout', 'index','create','update','view','delete',], - 'allow' => true, - 'roles' => ['@'], - ], - ], - ], - 'verbs' => [ - 'class' => VerbFilter::className(), - 'actions' => [ - 'logout' => ['post'], - ], - ], - ]; - } + /** * Lists all CartBills models. * @return mixed diff --git a/backend/controllers/CrossingUploadController.php b/backend/controllers/CrossingUploadController.php index 0f3fe04..c89b273 100755 --- a/backend/controllers/CrossingUploadController.php +++ b/backend/controllers/CrossingUploadController.php @@ -8,10 +8,13 @@ namespace backend\controllers; +use backend\components\base\BaseActiveRecord; use backend\components\base\BaseController; use common\components\CustomArrayHelper; use common\components\CustomVarDamp; +use yii\base\Model; use yii\data\ArrayDataProvider; +use yii\db\ActiveRecord; use yii\filters\VerbFilter; use yii\filters\AccessControl; use backend\models\UploadFileCrossingForm; @@ -82,7 +85,7 @@ class CrossingUploadController extends BaseController //запускаем парсинг $data = $model->readFile(); // сохраняем в кеш отпарсенные даные - $this->cacheHandler( true, $data, $model ); + $this->cacheHandler( 1, $data, $model ); } else if (Yii::$app->getCache()->get('parser_data')) { $data = json_decode(Yii::$app->getCache()->get('parser_data'), true); } @@ -128,7 +131,7 @@ class CrossingUploadController extends BaseController $arr = $model->toArray(); // получим данные из кеша - $this->cacheHandler( false, $data, $configuration ); + $this->cacheHandler( 0, $data, $configuration ); // соотнесем отпарсенные данные с соответствием полученным от пользователя // для этого преобразуем массив отпарсенных данных - назначим ключи согласно соответствию @@ -138,16 +141,20 @@ class CrossingUploadController extends BaseController $data = $this->convertDataByConfiguration( $data, $configuration ); $crosses_model = new DetailsCrosses(); - $crosses_model->ManualInsertWithIgnore($data); - Yii::$app->session->setFlash('success', 'Файл кроссов успешно загружен'); - // все прошло успешно - очищаем кеш - Yii::$app->getCache()->delete('parser_data'); - Yii::$app->getCache()->delete('parser_configuration'); + if ( $this->validateModel( $crosses_model , $data ) && $crosses_model->ManualInsertWithIgnore( $data ) ) { + + Yii::$app->session->setFlash('success', 'Файл кроссов успешно загружен'); + + // очистим кеш + $this->cacheHandler( 2 ); + + if (file_exists($configuration['file_path'])) + unlink($configuration['file_path']); + return $this->render('index', ['model' => $configuration]); + + } - if (file_exists($configuration['file_path'])) - unlink($configuration['file_path']); - return $this->render('index', ['model' => $configuration]); } else { // не прошла валидация форма загрузки файлов @@ -188,8 +195,8 @@ class CrossingUploadController extends BaseController $options ['configuration'] = ["string" => ['ARTICLE', 'CROSS_ARTICLE'],]; } - foreach ($data as &$row) { - $row = Yii::$app->converter->convertByConfiguration($row, $options); + foreach ( $data as &$row ) { + $row = Yii::$app->converter->convertByConfiguration( $row, $options ); } return $data; @@ -197,30 +204,47 @@ class CrossingUploadController extends BaseController } /** - * @param $mode - bool - true - put in cache, otherwise - fetch from cache + * @param $mode - int: 0 - fetch from cache, - 1 - put in cache, <2 - delete from cache * @param $data - array * @param $configuration - array * @throws \ErrorException */ - protected function cacheHandler( $mode, &$data, &$configuration ){ - - if ( $mode ) { - - Yii::$app->getCache()->set('parser_data', json_encode($data), 1800); - // сохраняем в кеш модель - в ней настройки для дальнейшей обработки данных - Yii::$app->getCache()->set('parser_configuration', serialize($configuration), 1800); + protected function cacheHandler( $mode, &$data = [], &$configuration = [] ){ + switch ( $mode ) { + case 0: + if (Yii::$app->getCache()->get('parser_data') && Yii::$app->getCache()->get('parser_configuration')) { + $data = json_decode(Yii::$app->getCache()->get('parser_data'), true); + $configuration = unserialize(Yii::$app->getCache()->get('parser_configuration')); + } else { + throw new \ErrorException('Ошибка кеша'); + } + break; + + case 1: + Yii::$app->getCache()->set('parser_data', json_encode($data), 1800); + // сохраняем в кеш модель - в ней настройки для дальнейшей обработки данных + Yii::$app->getCache()->set('parser_configuration', serialize($configuration), 1800); + break; + + default: + if( Yii::$app->getCache()->exists('parser_data') ) + Yii::$app->getCache()->delete('parser_data'); + + if( Yii::$app->getCache()->exists('parser_configuration') ) + Yii::$app->getCache()->delete('parser_configuration'); + } - } else { + } - if (Yii::$app->getCache()->get('parser_data') && Yii::$app->getCache()->get('parser_configuration')) { - $data = json_decode(Yii::$app->getCache()->get('parser_data'), true); - $configuration = unserialize(Yii::$app->getCache()->get('parser_configuration')); - } else { - throw new \ErrorException('Ошибка кеша'); - } + protected function validateModel( BaseActiveRecord $model, array $data ){ + foreach ( $data as $row ) { + if ( !$model->validate( $row ) ) { + $model->throwStringErrorException( key( $data ) ); + }; } + return true; } } \ No newline at end of file diff --git a/backend/controllers/DetailsDescriptionController.php b/backend/controllers/DetailsDescriptionController.php index 2ce31ba..2a271f1 100644 --- a/backend/controllers/DetailsDescriptionController.php +++ b/backend/controllers/DetailsDescriptionController.php @@ -11,7 +11,6 @@ use yii\data\ActiveDataProvider; use yii\web\HttpException; use yii\web\NotFoundHttpException; use yii\filters\VerbFilter; -use yii\filters\AccessControl; /** * DetailsDescriptionController implements the CRUD actions for DetailsDescription model. @@ -20,30 +19,13 @@ class DetailsDescriptionController extends BaseController { public $layout = "/column"; - /** - * @inheritdoc - */ public function behaviors() { return [ - 'access' => [ - 'class' => AccessControl::className(), - 'rules' => [ - [ - 'actions' => ['login', 'error', 'download-photo','delete-image' ], - 'allow' => true, - ], - [ - 'actions' => ['logout', 'index','create','update','view','delete',], - 'allow' => true, - 'roles' => ['@'], - ], - ], - ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ - 'logout' => ['post'], + 'delete' => ['post'], ], ], ]; diff --git a/backend/controllers/ParserController.php b/backend/controllers/ParserController.php index 4cef0a1..4b5cf87 100755 --- a/backend/controllers/ParserController.php +++ b/backend/controllers/ParserController.php @@ -38,22 +38,17 @@ class ParserController extends BaseController 'class' => AccessControl::className(), 'rules' => [ [ - 'actions' => ['login', 'error', 'download-photo','delete-image' ], - 'allow' => true, - ], - [ - 'actions' => ['logout', 'index','create','update','view','delete',], 'allow' => true, 'roles' => ['@'], ], ], ], - 'verbs' => [ - 'class' => VerbFilter::className(), - 'actions' => [ - 'logout' => ['post'], - ], - ], +// 'verbs' => [ +// 'class' => VerbFilter::className(), +// 'actions' => [ +// 'logout' => ['post'], +// ], +// ], ]; } diff --git a/backend/controllers/RgGrupController.php b/backend/controllers/RgGrupController.php index 340e63f..073256b 100755 --- a/backend/controllers/RgGrupController.php +++ b/backend/controllers/RgGrupController.php @@ -14,7 +14,6 @@ use common\components\CustomVarDamp; use common\components\parsers\MailAttachmentsSaver; use common\models\Margins; use common\models\MarginsGroups; -use yii\filters\VerbFilter; use yii\filters\AccessControl; use Yii; use yii\web\UploadedFile; @@ -36,22 +35,17 @@ class RgGrupController extends BaseController 'class' => AccessControl::className(), 'rules' => [ [ - 'actions' => ['login', 'error', 'download-photo','delete-image' ], - 'allow' => true, - ], - [ - 'actions' => ['logout', 'index','create','update','view','delete',], 'allow' => true, 'roles' => ['@'], ], ], ], - 'verbs' => [ - 'class' => VerbFilter::className(), - 'actions' => [ - 'logout' => ['post'], - ], - ], +// 'verbs' => [ +// 'class' => VerbFilter::className(), +// 'actions' => [ +// 'logout' => ['post'], +// ], +// ], ]; } diff --git a/backend/models/DetailsCrosses.php b/backend/models/DetailsCrosses.php index b10ff57..12ccb06 100755 --- a/backend/models/DetailsCrosses.php +++ b/backend/models/DetailsCrosses.php @@ -78,8 +78,16 @@ class DetailsCrosses extends \backend\components\base\BaseActiveRecord $query = Yii::$app->db->createCommand()->batchInsert($table_name, $keys_arr, $current_batch_array)->sql; // добавим ключевое слово - ignore $query = preg_replace('/INSERT/','INSERT IGNORE', $query); - Yii::$app->db->createCommand($query)->execute(); + $rows = Yii::$app->db->createCommand($query)->execute(); + + // если нет результата вернемся с ошибкой + if ( $rows == 0 ) { + return false; + } } + + return true; + } } diff --git a/tests/_support/_generated/UnitTesterActions.php b/tests/_support/_generated/UnitTesterActions.php index a23ad43..1077bc9 100755 --- a/tests/_support/_generated/UnitTesterActions.php +++ b/tests/_support/_generated/UnitTesterActions.php @@ -1,4 +1,4 @@ -converter = new Converter(); - $this->configuration = ['configuration' => - ["encode" => 'encode', - "string" => ['string1', 'string2' ], - "float" => 'float', - "integer" => ['integer1', 'integer2' ], + $this->configuration = ['hasKey' => true, + 'configuration' => + ['encode' => 'encode', + 'string' => ['string1', 'string2' ], + 'float' => 'float', + 'integer' => ['integer1', 'integer2' ], ]]; $this->wrong_configuration = ['config' => - ["encode" => 'encode', - "string" => 'string', - "float" => 'float', - "integer" => 'integer', + ['encode' => 'encode', + 'string' => 'string', + 'float' => 'float', + 'integer' => 'integer', ]]; $this->data_in = [ @@ -49,7 +50,7 @@ class BaseConverterTest extends \Codeception\TestCase\Test public function testConvertByConfig(){ - $this->data_out = $this->converter->convertByConfiguration($this->data_in, $this->configuration ); + $this->data_out = $this->converter->convertByConfiguration( $this->data_in, $this->configuration ); $this->assertEquals( $this->data_out['encode'], iconv( 'windows-1251', 'UTF-8', 'test encode string' ), 'Encoding failed' ); $this->assertInternalType( 'float', $this->data_out['float'], 'Convert to float is failed' ); @@ -58,7 +59,7 @@ class BaseConverterTest extends \Codeception\TestCase\Test public function testConvertToException(){ $this->setExpectedException('\Exception'); - $this->data_out = $this->converter->convertByConfiguration($this->data_in, $this->wrong_configuration ); + $this->data_out = $this->converter->convertByConfiguration( $this->data_in, $this->wrong_configuration ); } diff --git a/tests/unit/CrossesParsingTest.php b/tests/unit/CrossesParsingTest.php index 8283f13..0e24cd7 100755 --- a/tests/unit/CrossesParsingTest.php +++ b/tests/unit/CrossesParsingTest.php @@ -17,7 +17,7 @@ class CrossesParsingTest extends \Codeception\TestCase\Test public function _before() { $this->options[ 'mode' ] = 'crosses'; - $this->options[ 'converter_conf' ] = [ 'configuration' => [ "string" => ['ARTICLE', 'CROSS_ARTICLE'], ] ]; + $this->options[ 'converter_conf' ] = [ 'configuration' => [ "string" => ['ARTICLE', 'CROSS_ARTICLE'] ], 'hasKey' => 1 ]; $this->file_path = Yii::getAlias('@data_parser') . '\crosses\test1.csv'; } -- libgit2 0.21.4