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());
 }
 public function edit($id)
 {
     if ($this->request->is('post')) {
         $data = $this->request->data;
         $position = $this->Positions->get($data['position_id']);
         if ($position->type == 'image') {
             $uploader = new FileUploader();
             $uploader->allowTypes('image/jpg', 'image/jpeg', 'image/png', 'image/gif')->setDestination(TMP . 'uploads');
             $uploadedImage = $uploader->upload($data['image']);
             if ($uploadedImage) {
                 $image = new Image($uploadedImage);
                 $image->resizeTo($position->width, $position->height, $position->mode);
                 $banner = $image->save(WWW_ROOT . 'img/banners/');
                 if ($banner) {
                     $data['image'] = 'banners/' . $banner->getFilename();
                 } else {
                     $data['image'] = '';
                 }
                 $file = new File($uploadedImage);
                 $file->delete();
                 $image->close();
             } else {
                 $data['image'] = '';
             }
         } else {
             $data['image'] = '';
         }
         if (empty($data['image'])) {
             unset($data['image']);
         }
         $banner = $this->Banners->get($id);
         $banner = $this->Banners->patchEntity($banner, $data);
         if ($this->Banners->save($banner)) {
             $this->Flash->set('Banner salvo!', ['element' => 'ControlPanel.alert_success']);
         } else {
             $this->Flash->set($banner->getErrorMessages(), ['element' => 'ControlPanel.alert_danger']);
         }
     }
     $banner = $this->Banners->get($id);
     $options = Configure::read('MswAgencia.Plugins.BannersManager.Settings.Options');
     $positionsList = $this->Positions->getPositionsAvailableList();
     $this->set('options', $options);
     $this->set('positionsList', $positionsList, $banner->position_id);
     $this->set('banner', $banner);
 }
 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/");
 }