/**
  * Action upload anh
  */
 public function actionAdditemimage()
 {
     // Cac thong so mac dinh cua image
     // Kieu upload
     $type = Yii::$app->request->post('type');
     // Module upload
     $module = Yii::$app->request->post('module');
     // Cac truong cua image
     $columns = \yii\helpers\Json::decode(Yii::$app->request->post('columns'));
     // danh sach cac anh duoc upload
     $gallery = [];
     // Id cua gallery
     $id = uniqid('g_');
     // Begin upload image
     if ($type == Gallery::TYPE_UPLOAD) {
         // Upload anh khi chon type la upload
         $images = \yii\web\UploadedFile::getInstancesByName('image');
         if (empty($images)) {
             return;
         }
         foreach ($images as $image) {
             // Tao lai id khi upload nhieu anh
             $id = uniqid('g_');
             $ext = FileHelper::getExtention($image);
             if (!empty($ext)) {
                 $fileDir = strtolower($module) . '/' . date('Y/m/d/');
                 $fileName = uniqid() . '.' . $ext;
                 $folder = Yii::getAlias(Yii::$app->getModule('gallery')->syaDirPath) . Yii::$app->getModule('gallery')->syaDirUpload . '/' . $fileDir;
                 FileHelper::createDirectory($folder);
                 $image->saveAs($folder . $fileName);
                 $gallery[$id] = ['url' => $fileDir . $fileName, 'type' => $type];
             }
         }
     } elseif ($type == Gallery::TYPE_URL) {
         // Lay ra duong dan anh khi type la url
         $image = Yii::$app->request->post('image');
         if (empty($image)) {
             return;
         }
         $gallery[$id] = ['url' => $image, 'type' => $type];
     } elseif ($type == Gallery::TYPE_PATH) {
         $image = Yii::$app->request->post('image');
         if (empty($image)) {
             return;
         }
         $images = explode(',', $image);
         if (!empty($image) && is_array($images)) {
             foreach ($images as $img) {
                 $id = uniqid('g_');
                 $gallery[$id] = ['url' => $img, 'type' => $type];
             }
         }
     }
     // End upload image
     echo \sya\gallery\models\Gallery::generateGalleryTemplate($gallery, $module, $columns);
 }
Example #2
0
 /**
  * Action upload anh
  */
 public function actionAdditemimage()
 {
     // Cac thong so mac dinh cua image
     // Kieu upload
     $type = Yii::$app->request->post('type');
     // Module upload
     $module = Yii::$app->request->post('module');
     // Attribute name
     $attribute = Yii::$app->request->post('attribute');
     // Cac truong cua image
     $columns = Json::decode(Yii::$app->request->post('columns'));
     $templateInfomationImage = Yii::$app->request->post('templateInfomationImage');
     $templateInfomationImageDetail = Yii::$app->request->post('templateInfomationImageDetail');
     // danh sach cac anh duoc upload
     $gallery = [];
     // Column defalt image
     $columnsDefault = ['title' => '', 'caption' => '', 'alt_text' => ''];
     // Id cua gallery
     $id = uniqid('g_');
     // Begin upload image
     if ($type == Gallery::TYPE_UPLOAD) {
         // Upload anh khi chon type la upload
         $images = UploadedFile::getInstancesByName('image');
         if (empty($images)) {
             return;
         }
         foreach ($images as $image) {
             // Tao lai id khi upload nhieu anh
             $id = uniqid('g_');
             $ext = FileHelper::getExtention($image);
             if (!empty($ext)) {
                 $fileDir = strtolower($module) . '/' . date('Y/m/d/');
                 $fileName = pathinfo($image, PATHINFO_BASENAME);
                 $folder = Yii::getAlias(Yii::$app->getModule('gallery')->syaDirPath) . Yii::$app->getModule('gallery')->syaDirUpload . '/' . $fileDir;
                 FileHelper::createDirectory($folder);
                 $image->saveAs($folder . $fileName);
                 $columnsDefault['title'] = reset(explode('.', $fileName));
                 $gallery[$id] = ArrayHelper::merge(['url' => $fileDir . $fileName, 'type' => $type], $columnsDefault);
             }
         }
         $template = Gallery::generateGalleryTemplateByPath($gallery);
     } elseif ($type == Gallery::TYPE_URL) {
         // Lay ra duong dan anh khi type la url
         $image = Yii::$app->request->post('image');
         $columnsDefault = Json::decode($image);
         if (empty($image)) {
             return;
         }
         $gallery[$id] = ArrayHelper::merge(['type' => $type], $columnsDefault);
         $template = Gallery::generateGalleryTemplate($gallery, $columns, $module, $attribute, $templateInfomationImage, $templateInfomationImageDetail);
     } elseif ($type == Gallery::TYPE_PATH) {
         $image = Yii::$app->request->post('image');
         if (empty($image)) {
             return;
         }
         $images = explode(';', $image);
         if (!empty($image) && is_array($images)) {
             foreach ($images as $img) {
                 $columnsDefault = Json::decode($img);
                 $id = uniqid('g_');
                 $gallery[$id] = ArrayHelper::merge(['type' => $type], $columnsDefault);
             }
         }
         $template = Gallery::generateGalleryTemplate($gallery, $columns, $module, $attribute, $templateInfomationImage, $templateInfomationImageDetail);
     }
     // End upload image
     echo $template;
 }