function actionUpload()
 {
     $keys = $this->getKeys();
     if (!$keys) {
         return false;
     }
     $key = $keys['file_key'];
     $t = time();
     $img_dir = Yii::$app->params[$keys['params_key']];
     $date = date($img_dir['date_fmt'], $t);
     if (Yii::$app->user->isGuest) {
         $id_user = Yii::$app->session->getHasSessionId();
     } else {
         $id_user = Yii::$app->user->id;
     }
     if (!Yii::$app->user->isGuest) {
         if (empty($_POST['id_item']) && empty(Yii::$app->session[$keys['key_id_item']])) {
             $item = new Product();
             $item->attributes = $_POST['Product'];
             $item->item_state = Product::STATE_ROUGH_COPY & Product::STATE_PUBLISH;
             $item->id_cat = Category::checkCategory($_POST['Product']['category']);
             $item->save(false);
             $id_item = $item->id;
             Yii::$app->session[$keys['key_id_item']] = $id_item;
             if (!empty($_POST['ProductInfo']['full_text'])) {
                 $item_i = new ProductInfo();
                 $item_i->id = $id_item;
                 $item_i->attributes = $_POST['ProductInfo'];
                 $item_i->save(false);
             }
         } else {
             if (!empty(Yii::$app->session[$keys['key_id_item']])) {
                 $id_item = Yii::$app->session[$keys['key_id_item']];
                 //$item = Product::model()->findByPk(Yii::$app->session[$keys['key_id_item']]);
             } else {
                 if (!empty($_POST['id_item'])) {
                     //$item = Product::model()->findByPk($_POST['id_item']);
                     $id_item = $_POST['id_item'];
                 }
             }
         }
         $mainImg = Yii::$app->db->createCommand('SELECT prod_images.id, prod_images.date_create, prod_images.img_url FROM {{%product}} product INNER JOIN {{%prod_images}} prod_images ON product.id_mainImg=prod_images.id WHERE product.id=:id')->bindParam(':id', $id_item)->queryOne();
         $c_images = Yii::$app->db->createCommand('SELECT COUNT(id) FROM {{%prod_images}} WHERE id_prod=:prod')->bindParam(':prod', $id_item)->queryScalar();
     } else {
         $mainImg = Yii::$app->db->createCommand('SELECT id, date_create, img_url FROM {{%prod_temp_images}} images WHERE id_session=:id AND is_main=1')->bindParam(':id', $id_user)->queryOne();
         $c_images = Yii::$app->db->createCommand('SELECT COUNT(id) FROM {{%prod_temp_images}} WHERE id_session=:prod')->bindParam(':prod', $id_user)->queryScalar();
     }
     foreach ($img_dir as $k => &$dir) {
         if ($k == 'date_fmt') {
             continue;
         }
         if (is_array($dir)) {
             foreach ($dir as &$path) {
                 $path = strtr($path, array('{id_user}' => $id_user, '{id_item}' => $id_item));
                 CLib::createFolders($path);
             }
         } else {
             $dir = strtr($dir, array('{id_user}' => $id_user, '{id_item}' => $id_item));
             CLib::createFolders($dir);
         }
     }
     $targetFolder = $img_dir['origin'];
     $bigFolder = $img_dir['big'];
     $tmpFolder = $img_dir['tmp'];
     $previewFolder = $img_dir['preview'];
     if (!count($_FILES[$key])) {
         return false;
     }
     $info = array();
     $size = 1200;
     // px
     $max_files = Yii::$app->params['max_images'];
     $tempFile = $_FILES[$key]['tmp_name'];
     $fileName = md5($id_user . $_FILES[$key]['name'] . $t . rand(0, 1000));
     $type = explode('/', $_FILES[$key]['type']);
     //$fileName.='.'.$type[1];
     $fileName .= '.jpeg';
     $newFile = $tmpFolder . $fileName;
     // перенести во временную папку
     move_uploaded_file($tempFile, $newFile);
     if (!file_exists($newFile)) {
         $vr['err'] = CLib::param('FILE_TO_BIG');
         $vr['no_image'] = true;
         echo json_encode(array('files' => array($vr)));
         return;
     }
     $f_size = filesize($newFile);
     // TODO
     if ($max_files <= $c_images) {
         unlink($newFile);
         $vr['err'] = CLib::param('FILE_IMAGE_LIMIT');
         $vr['err_limit'] = true;
     } else {
         if ($f_size > MAX_SIZE) {
             unlink($newFile);
             $vr['err'] = CLib::param('FILE_TO_BIG');
             $vr['no_image'] = true;
         } else {
             $mime = explode('/', FileHelper::getMimeType($newFile));
             if ($mime[1] != 'jpeg' && $mime[1] != 'jpg') {
                 unlink($newFile);
                 $vr['err'] = CLib::param('FILE_NO_IMAGE');
                 $vr['no_image'] = true;
             } else {
                 $w_norm = $h_norm = $size;
                 copy($newFile, $targetFolder . $fileName);
                 CLib::resizeImgToSmall($w_norm, $h_norm, $targetFolder . $fileName, false, 80, false, true);
                 //
                 $size = 1200;
                 $w_norm = $h_norm = $size;
                 $t_start = microtime(true);
                 unlink($newFile);
                 $newFile = $targetFolder . $fileName;
                 copy($newFile, $bigFolder . $fileName);
                 CLib::resizeImgToSmall($w_norm, $h_norm, $bigFolder . $fileName, false, 80);
                 //
                 $t_end = microtime(true);
                 $vr['time']['diff2'][] = $t_end - $t_start;
                 foreach ($previewFolder as $size => $folder) {
                     $w_norm = $h_norm = (int) $size;
                     if (!file_exists($folder)) {
                         mkdir($folder);
                     }
                     $img = $folder . $fileName;
                     copy($newFile, $img);
                     CLib::resizeImgToSmall($w_norm, $h_norm, $img, $size == 300 || $size == 100, 80);
                 }
                 //unlink($newFile);
                 //$command->bindParam(':filename',$fileName)->execute();
                 //$id = Yii::$app->db->getLastInsertID();
                 if (!Yii::$app->user->isGuest) {
                     $sql = 'INSERT INTO {{%prod_images}} SET id_prod=:prod, img_url=:url, date_create=:date';
                     Yii::$app->db->createCommand($sql)->bindParam(':prod', $id_item)->bindParam(':url', $fileName)->bindParam(':date', $t)->execute();
                 } else {
                     $sql = 'INSERT INTO {{%prod_temp_images}} SET id_session=:id_user, img_url=:url, date_create=:date';
                     Yii::$app->db->createCommand($sql)->bindParam(':id_user', $id_user)->bindParam(':url', $fileName)->bindParam(':date', $t)->execute();
                 }
                 /*
                 $img_model = new ProdImages();
                 $img_model->id_prod = $id_item;
                 $img_model->img_url = $fileName;
                 $img_model->date_create = $t;
                 $img_model->save(false);
                 */
                 $id_img = Yii::$app->db->lastInsertID;
                 if (!Yii::$app->user->isGuest) {
                     if (empty($mainImg['date_create']) || !CLib::checkProdImg(Yii::$app->user->id, $id_item, $mainImg['img_url'])) {
                         $mainImg['id'] = $id_img;
                         Yii::$app->db->createCommand('UPDATE {{%product}} SET id_mainImg=:img WHERE id=:id')->bindParam(':id', $id_item)->bindParam(':img', $id_img)->execute();
                         /*
                         $item->id_mainImg = $img_model->id;
                         $item->save(false);
                         */
                     }
                 } else {
                     if (!isset(Yii::$app->session['prod_images'])) {
                         $arr = array();
                     } else {
                         $arr = Yii::$app->session['prod_images'];
                     }
                     $arr[] = $id_img;
                     Yii::$app->session['prod_images'] = $arr;
                     if (empty($mainImg['date_create']) || !CLib::checkProdImg($id_user, $id_item, $mainImg['img_url'])) {
                         $mainImg['id'] = $id_img;
                         Yii::$app->db->createCommand('UPDATE {{%prod_temp_images}} SET is_main=1 WHERE id=:id')->bindParam(':id', $id_img)->execute();
                     }
                 }
                 $vr['thumbUrl'] = Url::to($previewFolder['100'] . $fileName, true);
                 $vr['thumbUrl_100'] = Url::to($previewFolder['100'] . $fileName, true);
                 $vr['thumbUrl_300'] = Url::to($previewFolder['300'] . $fileName, true);
                 $vr['id'] = $id_img;
                 $vr['id_main'] = $mainImg['id'];
                 $vr['time']['start'] = $t_start;
                 $vr['time']['end'] = $t_end;
                 $vr['time']['diff'] = $t_end - $t_start;
             }
         }
     }
     $info[] = $vr;
     return json_encode(array('files' => $info));
 }