Пример #1
0
 public function add($id)
 {
     $gallery = $this->Galleries->get($id);
     if ($this->request->is('post')) {
         $data = $this->request->data;
         $uploader = new FileUploader();
         $uploader->allowTypes('image/jpg', 'image/jpeg', 'image/png')->setDestination(TMP . 'uploads');
         $uploadedPhotosCounter = 0;
         $photos = [];
         foreach ($data['image'] as $image) {
             $uploadedPhotosCounter++;
             $thumbs = [];
             $uploadedImage = $uploader->upload($image);
             $image = new Image($uploadedImage);
             $image->resizeTo($gallery->photo_width, $gallery->photo_height, $gallery->photo_resize_mode);
             $photo = $image->save(WWW_ROOT . 'img/galleries/photos/');
             if (Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.apply_watermark_on_photos')) {
                 $watermark = new Image(Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.watermark_filepath'));
                 $photo = $watermark->placeOver($photo, 20, $photo->getHeight() - (20 + $watermark->getHeight()));
                 $watermark->close();
             }
             foreach (Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Image.Photos.Thumbnails') as $ref => $config) {
                 $image->resizeTo($config['width'], $config['height'], $config['mode']);
                 $thumb = $image->save(WWW_ROOT . 'img/galleries/photos/', 'thumb_' . $image->getFilename());
                 $thumbs[] = ['ref' => $ref, 'path' => 'galleries/photos/' . $thumb->getFilename()];
             }
             $photos[] = ['photo' => 'galleries/photos/' . $photo->getFilename(), 'thumbnails' => $thumbs];
         }
         if ($this->Photos->addNewPhotosToGallery($gallery->id, $photos)) {
             $this->Flash->set("{$uploadedPhotosCounter} foto(s) foram adicionadas", ['element' => 'AppCore.alert_success']);
         } else {
             $this->Flash->set('Houve um erro ao tentar adicionar novas fotos.', ['element' => 'AppCore.alert_danger']);
         }
     }
     $this->redirect("/interno/galeria-de-fotos/galerias/{$id}/fotos/");
 }
 public function edit($id)
 {
     if ($this->request->is('post')) {
         $data = $this->request->data;
         if (Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.use_image')) {
             $uploader = new FileUploader();
             $uploader->allowTypes('image/jpg', 'image/jpeg', 'image/png')->setDestination(TMP . 'uploads');
             $uploadedImage = $uploader->upload($data['cover']);
             unset($data['cover']);
             if ($uploadedImage) {
                 $image = new Image($uploadedImage);
                 $image->resizeTo(Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.gallery_cover_width'), Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.gallery_cover_height'), Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.gallery_cover_resize_mode'));
                 $cover = $image->save(WWW_ROOT . 'img/galleries/');
                 if ($cover) {
                     $data['cover'] = 'galleries/' . $cover->getFilename();
                     $image->resizeTo(Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.gallery_cover_thumbnail_width'), Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.gallery_cover_thumbnail_height'), Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.gallery_cover_thumbnail_resize_mode'));
                     $thumbnailImageName = 'thumb_' . Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.gallery_cover_thumbnail_width') . '_' . Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.gallery_cover_thumbnail_height') . '_' . $image->getFilename();
                     $coverThumbnail = $image->save(WWW_ROOT . 'img/galleries/', $thumbnailImageName);
                     if ($coverThumbnail) {
                         $data['cover_thumbnail'] = 'galleries/' . $coverThumbnail->getFilename();
                     }
                 } else {
                     unset($data['cover']);
                 }
                 $image->close();
                 $uploadedImage = new File($uploadedImage);
                 $uploadedImage->delete();
             }
         } else {
             unset($data['cover']);
         }
         $gallery = $this->Galleries->get($id);
         $gallery = $this->Galleries->patchEntity($gallery, $data);
         if ($gallery->hasErrors()) {
             if (Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options.use_image')) {
                 $file = new File($cover->getFilepath());
                 $file->delete();
                 $file = new File($coverThumbnail->getFilepath());
                 $file->delete();
             }
             $this->Flash->set($gallery->getErrorMessages(), ['element' => 'alert_danger']);
         } elseif ($this->Galleries->save($gallery)) {
             $this->Flash->set('Galeria editada!', ['element' => 'alert_success']);
         }
     }
     $gallery = $this->Galleries->get($id);
     $this->set('gallery', $gallery);
     $this->set('options', Configure::read('WebImobApp.Plugins.PhotoGallery.Settings.Options'));
     $this->set('categoriesList', $this->Categories->getCategoriesAsList());
 }