Пример #1
0
 public function actionUpload()
 {
     if (Yii::$app->request->isAjax) {
         Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         switch (Yii::$app->request->post('action')) {
             case "addGroup":
                 $group = new GlGroups();
                 $type = GlTypes::findOne(['type' => Yii::$app->request->post('type')]);
                 $group->types_id = $type->id;
                 $group->save();
                 return ['success' => true, 'groups_id' => $group->id];
                 break;
             case "uploadImage":
                 $group = new GlGroups();
                 if (Yii::$app->request->post('groups_id')) {
                     $group = GlGroups::findOne(Yii::$app->request->post('groups_id'));
                     $type = GlTypes::findOne($group->types_id);
                 } else {
                 }
                 $file = $_FILES['input_file_name'];
                 if (GlImgs::findOne(['groups_id' => $group->id, 'basename_src' => $file['name']])) {
                     return ['success' => false, 'index' => Yii::$app->request->post('index'), 'error' => 'Вы уже загружали эту фотографию'];
                 }
                 $src_image = Yii::getAlias('@webroot') . self::TMP_PATH . '/' . $file['name'];
                 $dst_path = Yii::getAlias('@webroot') . $type->dir_dst;
                 move_uploaded_file($file['tmp_name'], $src_image);
                 $images = (new GlImgs())->convertImg($type->id, $src_image, $dst_path);
                 unlink($src_image);
                 $last_image = GlImgs::find()->where(['groups_id' => $group->id])->orderBy(['seq' => SORT_DESC])->one();
                 $image = new GlImgs();
                 $image->img_small = self::IMAGE_PATH . '/' . $type->type . '/' . $images['img_small'];
                 $image->img_large = self::IMAGE_PATH . '/' . $type->type . '/' . $images['img_large'];
                 $image->basename_src = $file['name'];
                 $image->seq = $last_image ? $last_image->seq + 1 : 1;
                 $image->groups_id = $group->id;
                 $image->save();
                 if (!$group->imgs_id) {
                     $group->imgs_id = $image->id;
                     $group->update();
                 }
                 return ['success' => true, 'index' => Yii::$app->request->post('index'), 'id' => $image->id, 'basename_src' => $image->basename_src, 'img_small' => $image->img_small];
                 break;
             case "deleteImage":
                 $f = fopen(Yii::getAlias('@webroot' . '/img/tmp/upload.log'), 'a');
                 $image = GlImgs::findOne(['id' => Yii::$app->request->post('id'), 'basename_src' => Yii::$app->request->post('basename_src')]);
                 if (!$image) {
                     return ['success' => false, 'error' => 'Файл не найден'];
                 }
                 $image->delete();
                 (new GlImgs())->reSort(Yii::$app->request->post('groups_id'));
                 $image = GlImgs::findOne(['groups_id' => Yii::$app->request->post('groups_id'), 'seq' => 1]);
                 $group = GlGroups::findOne(Yii::$app->request->post('groups_id'));
                 if (!$image) {
                     $group->delete();
                 }
                 if ($image && $group->imgs_id != $image->id) {
                     $group->imgs_id = $image->id;
                     $group->update();
                 }
                 fclose($f);
                 return ['success' => true, 'groups_id' => $image ? $group->id : false];
                 break;
             case "reSortGallery":
                 foreach (Yii::$app->request->post('images') as $imgs_id => $seq) {
                     $image = GlImgs::findOne($imgs_id);
                     $image->seq = $seq;
                     $image->save();
                     if ($seq == 1) {
                         $group = GlGroups::findOne($image->groups_id);
                         if ($group->imgs_id != $image->id) {
                             $group->imgs_id = $image->id;
                             $group->update();
                         }
                     }
                 }
                 return ['success' => true];
                 break;
         }
         return ['success' => false, 'error' => 'На сервер передан неверный параметр'];
     }
 }
Пример #2
0
 private function addImage($links_id, $item)
 {
     $link_imgs_id = Links::findOne($links_id)->gl_imgs_id;
     $gl_groups = array();
     foreach ($item as $image_sxe) {
         $basename_src = basename(strval($image_sxe->{'ПутьКИзображению'}));
         if (!$basename_src) {
             continue;
         }
         preg_match('/_(.+)\\s\\[(.+)\\]/', $basename_src, $matches);
         if ($matches) {
             $type = $matches[1];
             $title = $matches[2];
         } else {
             $type = Yii::$app->params['defaultShGlType'];
             $title = '';
         }
         if (!GlImgs::findOne(['basename_src' => $basename_src])) {
             $gl_type = GlTypes::findOne(['type' => $type]);
             if (!isset($gl_groups[$type])) {
                 $gl_groups[$type] = GlGroups::findOne(['links_id' => $links_id, 'types_id' => $gl_type->id]);
                 if (!$gl_groups[$type]) {
                     $gl_group = new GlGroups();
                     $gl_group->types_id = GlTypes::findOne(['type' => $type])->id;
                     $gl_group->links_id = $links_id;
                     $gl_group->save();
                     $gl_groups[$type] = $gl_group;
                 }
             }
             $src_image = pathinfo($this->import_file)['dirname'] . '/' . strval($image_sxe->{'ПутьКИзображению'});
             $dst_path = Yii::getAlias('@frontend') . '/web' . $gl_type->dir_dst;
             $images = (new GlImgs())->convertImg($gl_type->id, addslashes($src_image), $dst_path);
             $gl_img = new GlImgs();
             $gl_img->groups_id = $gl_groups[$type]->id;
             $gl_img->img_small = $gl_type->dir_dst . '/' . $images['img_small'];
             $gl_img->img_large = $gl_type->dir_dst . '/' . $images['img_large'];
             $gl_img->basename_src = $basename_src;
             $gl_img->title = $title;
             $gl_img->seq = GlImgs::findLastSequence($gl_groups[$type]->id) + 1;
             $gl_img->save();
             if (!$link_imgs_id) {
                 $link = Links::findOne($links_id);
                 $link->gl_imgs_id = $gl_img->id;
                 $link->save();
                 $link_imgs_id = $gl_img->id;
                 $gl_groups[$type]->imgs_id = $gl_img->id;
                 $gl_groups[$type]->save();
             }
             if ($image_sxe->{'ОсновноеИзображение'} == 1 && $link_imgs_id != $gl_img->id) {
                 $link = Links::findOne($links_id);
                 $link->gl_imgs_id = $gl_img->id;
                 $link->save();
                 $link_imgs_id = $gl_img->id;
                 $gl_groups[$type]->imgs_id = $gl_img->id;
                 $gl_groups[$type]->save();
             }
         }
     }
 }