예제 #1
0
 /**
  * Lists all Image models.
  * @param int $id product id
  * @return mixed
  *
  * @throws NotFoundHttpException
  */
 public function actionIndex($id)
 {
     $form = new MultipleUploadForm();
     if (!Product::find()->where(['id' => $id])->exists()) {
         throw new NotFoundHttpException();
     }
     $searchModel = new ImageSearch();
     $searchModel->product_id = $id;
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     if (Yii::$app->request->isPost) {
         $form->files = UploadedFile::getInstances($form, 'files');
         if ($form->files && $form->validate()) {
             foreach ($form->files as $file) {
                 //
                 // UPLOADING THE IMAGE:
                 //
                 $image = new Image();
                 $image->product_id = $id;
                 if ($image->save()) {
                     // Save an original image;
                     $path = $image->getPath();
                     $file->saveAs($path);
                     // Original size:
                     $size = getimagesize($path);
                     $height = $size[1];
                     $width = $size[0];
                     // IMAGINE
                     ImagineImage::$driver = [ImagineImage::DRIVER_GD2];
                     $imagine = new Imagine();
                     $picture = $imagine->open($path);
                     //---------------------------
                     //                        $size = new Box(self::IMAGE_WIDTH, self::IMAGE_HEIGHT);
                     //                        $center = new Center($size);
                     //---------------------------
                     //                        $picture->crop(new Point(0, 0),
                     //                            new Box(self::IMAGE_WIDTH, self::IMAGE_HEIGHT))->save($path);
                     /**
                      * If the image's height is bigger than needed, it must be cut.
                      * Otherwise it must be resized.
                      */
                     if ($height >= self::IMAGE_HEIGHT) {
                         $picture->thumbnail(new Box(self::IMAGE_WIDTH, self::IMAGE_HEIGHT))->save($path, ['quality' => 100]);
                         // re-save cropped image;
                     } else {
                         $picture->resize(new Box(self::IMAGE_WIDTH, self::IMAGE_HEIGHT))->save($path);
                     }
                     sleep(1);
                     //                        $background = new Color('#FFF');
                     //                        $topLeft    = new Point(0, 0);
                     //                        $canvas = $imagine->create(new Box(450, 450), $background);
                     //                        $canvas->paste($picture, $topLeft)->save($path);
                 }
             }
         }
     }
     return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'uploadForm' => $form]);
 }
예제 #2
0
 public function saveImage()
 {
     $imagePost = new Image();
     $nameImg = Yii::$app->security->generateRandomString(9);
     $this->img->saveAs($this->getPath() . $nameImg . "." . $this->img->extension);
     $imagePost->url = $nameImg . "." . $this->img->extension;
     $imagePost->type = Image::POST_STATUS;
     $imagePost->object_id = $this->id;
     $imagePost->save();
 }
예제 #3
0
 public function actionUpload()
 {
     $newsID = preg_replace('/\\D+/', '', $_SERVER['HTTP_REFERER']);
     $news = News::findOne($newsID);
     $filename = $news ? $news->link . rand(0, 10000) : \Yii::$app->security->generateRandomString();
     $file = UploadHelper::__upload($_FILES['file'], ['filename' => $filename]);
     $image = new Image(['userID' => \Yii::$app->user->id, 'newsID' => $newsID, 'link' => \Yii::$app->params['cdn'] . '/images/stories/' . $file]);
     $image->save(false);
     if (\Yii::$app->request->isAjax) {
         \Yii::$app->response->format = 'json';
         return ['filelink' => $image->link, 'id' => "img{$image->id}"];
     }
     return $image;
 }
예제 #4
0
 public function upload()
 {
     if ($this->validate()) {
         foreach ($this->imageFiles as $file) {
             $this->file_name = $this->table_name . '_' . $this->table_id . '_' . trim($file->baseName) . '.' . $file->extension;
             $full_path = \Yii::$app->basePath . "/../" . $this->filePath;
             $full_path = strtr($full_path, '/\\', DIRECTORY_SEPARATOR . DIRECTORY_SEPARATOR);
             if ($file->saveAs($full_path)) {
                 $model = new Image();
                 $model->table_name = $this->table_name;
                 $model->table_id = $this->table_id;
                 $model->path = $this->path;
                 $model->file_name = $this->file_name;
                 $model->save(false);
             }
         }
         return true;
     } else {
         return false;
     }
 }
예제 #5
0
 /**
  * Lists all Image models.
  * @param int $id product id
  * @return mixed
  *
  * @throws NotFoundHttpException
  */
 public function actionIndex($id)
 {
     if (!Product::find()->where(['id' => $id])->exists()) {
         throw new NotFoundHttpException();
     }
     $form = new MultipleUploadForm();
     $searchModel = new ImageSearch();
     $searchModel->product_id = $id;
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
     if (Yii::$app->request->isPost) {
         $form->files = UploadedFile::getInstances($form, 'files');
         if ($form->files && $form->validate()) {
             foreach ($form->files as $file) {
                 $image = new Image();
                 $image->product_id = $id;
                 if ($image->save()) {
                     $file->saveAs($image->getPath());
                 }
             }
         }
     }
     return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider, 'uploadForm' => $form]);
 }
예제 #6
0
 /**
  * Updates an existing Hotel model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     //$this->allowAccess(Hotel::checkAccess($id));
     $model = $this->findModel($id);
     $languages = Lang::getAll();
     $roomTypes = RoomType::getRoomTypes();
     $department = Department::getAll();
     $modelHotelDepartmentMapping = new HotelDepartmentMapping();
     $modelHotelData = HotelData::getByHotelId($id);
     if (!$modelHotelData) {
         $modelHotelData = new HotelData();
     }
     $availableRooms = HotelRoomTypeMapping::getRoomTypesIdByHotelId($id);
     $hotelDepartments = HotelDepartmentMapping::getByHotelId($id);
     $modelHotelRoomTypeMapping = new HotelRoomTypeMapping();
     $countries = Country::getAllCountries();
     $savePath = Yii::getAlias($model::FILE_PATH);
     $imageDataProvider = new ArrayDataProvider(['models' => Image::getImagesByHotelId($id)]);
     if ($model->load(Yii::$app->request->post())) {
         //            $modelCity = City::getOrCreateByNameAndCountryId($_POST['Hotel']['cityName'], $model->country_id);
         //            $model->city_id = $modelCity->id;
         if ($model->save()) {
             foreach ($_POST['HotelRoomTypeMapping']['room_type_id'] as $roomTypeId) {
                 $hotelRoomTypeMappings = HotelRoomTypeMapping::getRoomTypeByHotelIdAndRoomTypeId($id, $roomTypeId);
                 if (!$hotelRoomTypeMappings) {
                     $modelHotelRoomTypeMapping = new HotelRoomTypeMapping();
                     $modelHotelRoomTypeMapping->setAttribute('hotel_id', $model->getAttribute('id'));
                     $modelHotelRoomTypeMapping->setAttribute('room_type_id', $roomTypeId);
                     $modelHotelRoomTypeMapping->save();
                 } elseif ($hotelRoomTypeMappings->getAttribute('deleted') === ActiveRecord::STATUS_DELETED) {
                     $hotelRoomTypeMappings->setAttribute('deleted', ActiveRecord::STATUS_NOT_DELETED);
                     $hotelRoomTypeMappings->save();
                 }
             }
             foreach ($languages as $key => $language) {
                 $modelHotelData = HotelData::getByLanguageIdAndHotelId($language->local, $id);
                 if ($modelHotelData) {
                     $modelHotelData->setAttribute('lang_id', Yii::$app->request->post('HotelData')['lang_id'][$key]);
                     $modelHotelData->setAttribute('description', Yii::$app->request->post('HotelData')['description'][$key]);
                     $modelHotelData->setAttribute('location', Yii::$app->request->post('HotelData')['location'][$key]);
                     $modelHotelData->setAttribute('price_information', Yii::$app->request->post('HotelData')['price_information'][$key]);
                     $modelHotelData->setAttribute('other', Yii::$app->request->post('HotelData')['other'][$key]);
                     $modelHotelData->save();
                 } else {
                     $modelHotelData = new HotelData();
                     $modelHotelData->setAttribute('hotel_id', $id);
                     $modelHotelData->setAttribute('lang_id', Yii::$app->request->post('HotelData')['lang_id'][$key]);
                     $modelHotelData->setAttribute('description', Yii::$app->request->post('HotelData')['description'][$key]);
                     $modelHotelData->setAttribute('location', Yii::$app->request->post('HotelData')['location'][$key]);
                     $modelHotelData->setAttribute('price_information', Yii::$app->request->post('HotelData')['price_information'][$key]);
                     $modelHotelData->setAttribute('other', Yii::$app->request->post('HotelData')['other'][$key]);
                     $modelHotelData->save();
                 }
             }
             foreach ($availableRooms as $room) {
                 if (!in_array($room, $_POST['HotelRoomTypeMapping']['room_type_id'])) {
                     $hotelRoomTypeMapping = HotelRoomTypeMapping::getRoomTypeByHotelIdAndRoomTypeId($id, $room);
                     $hotelRoomTypeMapping->setAttribute('deleted', ActiveRecord::STATUS_DELETED);
                     $hotelRoomTypeMapping->save();
                 }
             }
             foreach ($_POST['HotelDepartmentMapping']['department_id'] as $departmentId) {
                 $hotelDepartmentMapping = HotelDepartmentMapping::getByHotelIdAndDepartmentId($id, $departmentId);
                 if (!$hotelDepartmentMapping) {
                     $modelHotelDepartmentMapping = new HotelDepartmentMapping();
                     $modelHotelDepartmentMapping->setAttribute('hotel_id', $model->getAttribute('id'));
                     $modelHotelDepartmentMapping->setAttribute('department_id', $departmentId);
                     $modelHotelDepartmentMapping->save();
                 } elseif ($hotelDepartmentMapping->getAttribute('deleted') === ActiveRecord::STATUS_DELETED) {
                     $hotelDepartmentMapping->setAttribute('deleted', ActiveRecord::STATUS_NOT_DELETED);
                     $hotelDepartmentMapping->save();
                 }
             }
             foreach ($hotelDepartments as $department) {
                 if (!in_array($department, $_POST['HotelDepartmentMapping']['department_id'])) {
                     $modelHotelDepartmentMapping = HotelDepartmentMapping::getByHotelIdAndDepartmentId($id, $department);
                     $modelHotelDepartmentMapping->setAttribute('deleted', ActiveRecord::STATUS_DELETED);
                     $modelHotelDepartmentMapping->save();
                 }
             }
             $files = $model->loadFiles($model->fileAttribute);
             if ($files !== false) {
                 foreach ($files as $file) {
                     if ($file->saveAs($savePath . $file->name)) {
                         $image = new Image();
                         $image->setAttribute('name', $file->name);
                         $image->setAttribute('hotel_id', $id);
                         $image->setAttribute('path', $model::FILE_PATH . $file->name);
                         $image->save();
                     }
                 }
             }
             if ($model->preview_image_id === 0 || $model->preview_image_id === null) {
                 $model->setAttribute('preview_image_id', !empty($image) ? $image->id : $model::IMAGE_NOT_SET);
                 $model->save();
             }
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     return $this->render('update', ['model' => $model, 'modelHotelData' => $modelHotelData, 'modelHotelRoomTypeMapping' => $modelHotelRoomTypeMapping, 'roomTypes' => $roomTypes, 'countries' => $countries, 'availableRooms' => $availableRooms, 'hotelDepartments' => $hotelDepartments, 'imageDataProvider' => $imageDataProvider, 'department' => $department, 'modelHotelDepartmentMapping' => $modelHotelDepartmentMapping, 'languages' => $languages]);
 }
예제 #7
0
 /**
  * Updates an existing Product model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post())) {
         $model->image = UploadedFile::getInstance($model, 'image');
         $model->images = UploadedFile::getInstances($model, 'images');
         if ($model->validate() && $model->save()) {
             $dir = Yii::getAlias('@frontend/web/uploads/product/' . $model->id);
             if ($model->image) {
                 $model->image->saveAs($dir . '/main.jpg');
             }
             if ($model->images) {
                 $imageModels = Image::findAll(['model_id' => $model->id]);
                 if ($imageModels) {
                     foreach ($imageModels as $image) {
                         $file = $dir . '/' . $image->id . '.jpg';
                         if (file_exists($file)) {
                             unlink($file);
                         }
                         $image->delete();
                     }
                 }
                 foreach ($model->images as $image) {
                     $imageModel = new Image();
                     $imageModel->model_id = $model->id;
                     $imageModel->save();
                     $image->saveAs($dir . '/' . $imageModel->id . '.jpg');
                 }
             }
         }
         return $this->redirect(['view', 'id' => $model->id]);
     }
     return $this->render('update', ['model' => $model]);
 }