Exemple #1
0
 /**
  * Save the image block and create a cropped image if necessary
  * @param \Zend\EventManager\Event $event
  * @return array|\DotsBlock\Db\Entity\Block|object|string
  */
 public function saveBlock(Event $event)
 {
     $request = $event->getParam('request');
     $locator = Registry::get('service_locator');
     $appConfig = $locator->get('ApplicationConfig');
     $modelImageBlock = $locator->get('DotsImageBlock\\Db\\Model\\ImageBlock');
     $block = $event->getTarget();
     $form = new MultiForm(array('image_content' => new ImageContentForm()));
     $data = array('image_content' => $request->getPost());
     $files = $request->getFiles();
     $data['image_content']['original_src'] = $files['original_src']['name'];
     $form->setData($data);
     if ($form->isValid()) {
         $data = $form->getInputFilter()->getValues();
         if ($block->id) {
             $imageBlock = $modelImageBlock->getByBlockId($block->id);
         } else {
             $block->save();
             $imageBlock = new ImageBlock();
             $imageBlock->block_id = $block->id;
         }
         $imageBlock->alt = $data['image_content']['alt'];
         $imageBlock->display_width = $data['image_content']['display_width'];
         $imageBlock->display_height = $data['image_content']['display_height'];
         $editedCrop = $imageBlock->crop_x1 != $data['image_content']['crop_x1'] || $imageBlock->crop_y1 != $data['image_content']['crop_y1'] || $imageBlock->crop_x2 != $data['image_content']['crop_x2'] || $imageBlock->crop_y2 != $data['image_content']['crop_y2'];
         $imageBlock->crop_x1 = $data['image_content']['crop_x1'];
         $imageBlock->crop_y1 = $data['image_content']['crop_y1'];
         $imageBlock->crop_x2 = $data['image_content']['crop_x2'];
         $imageBlock->crop_y2 = $data['image_content']['crop_y2'];
         if (!empty($files['original_src']['tmp_name'])) {
             $upload = new Upload(array('path' => 'data/uploads/', 'destination' => $appConfig['public_path'] . '/'));
             $path = $upload->process($files);
             $data['image_content']['original_src'] = $path['original_src'];
         }
         if (!($imageBlock->id && empty($data['image_content']['original_src']))) {
             // success - do something with the uploaded file
             $fullFilePath = $data['image_content']['original_src'];
             if ($imageBlock->original_src) {
                 unlink($appConfig['public_path'] . '/' . $imageBlock->original_src);
             }
             if ($imageBlock->src != $imageBlock->original_src) {
                 unlink($appConfig['public_path'] . '/' . $imageBlock->src);
             }
             $imageBlock->original_src = $fullFilePath;
             $imageBlock->src = $fullFilePath;
             $thumb = PhpThumbFactory::create($appConfig['public_path'] . '/' . $imageBlock->original_src);
             $dimensions = $thumb->getCurrentDimensions();
             $imageBlock->width = $dimensions['width'];
             $imageBlock->height = $dimensions['height'];
         }
         if ($editedCrop) {
             if ($imageBlock->src != $imageBlock->original_src) {
                 unlink($appConfig['public_path'] . '/' . $imageBlock->src);
             }
             if ($imageBlock->crop_x1 !== "" && $imageBlock->crop_y1 !== "" && $imageBlock->crop_x2 !== "" && $imageBlock->crop_y2 !== "") {
                 $thumb = PhpThumbFactory::create($appConfig['public_path'] . '/' . $imageBlock->original_src);
                 if ($imageBlock->width && $imageBlock->height) {
                     $w = $imageBlock->width;
                     $h = $imageBlock->height;
                 } else {
                     $dimensions = $thumb->getCurrentDimensions();
                     $imageBlock->width = $w = $dimensions['width'];
                     $imageBlock->height = $h = $dimensions['height'];
                 }
                 $x1 = round($imageBlock->crop_x1 * $w / 100);
                 $y1 = round($imageBlock->crop_y1 * $h / 100);
                 $x2 = round($imageBlock->crop_x2 * $w / 100);
                 $y2 = round($imageBlock->crop_y2 * $h / 100);
                 $thumb->crop($x1, $y1, $x2 - $x1, $y2 - $y1);
                 $filename = basename($imageBlock->original_src);
                 $filename = substr($filename, 0, strrpos($filename, '.')) . '.jpg';
                 $filename = 'data/uploads/edited/' . uniqid(rand()) . '_' . $filename;
                 $thumb->save($appConfig['public_path'] . '/' . $filename, 'jpg');
                 $imageBlock->src = $filename;
             } else {
                 $imageBlock->src = $imageBlock->original_src;
             }
         }
         $imageBlock->save();
         return $block;
     }
     $event->stopPropagation();
     $errors = $form->getMessages();
     return $errors;
 }
Exemple #2
0
 public function saveAction()
 {
     $locator = $this->getServiceLocator();
     $appConfig = $locator->get('ApplicationConfig');
     $modelPage = $locator->get('DotsPages\\Db\\Model\\Page');
     $modelBlock = $locator->get('DotsBlock\\Db\\Model\\Block');
     $modelLinkBlock = $locator->get('DotsLinkBlock\\Db\\Model\\LinkBlock');
     $POST = $this->getRequest()->getPost()->toArray();
     $FILES = $this->getRequest()->getFiles()->toArray();
     $form = new LinkContentForm();
     if (!empty($FILES)) {
         $POST['file'] = $FILES['file'];
     }
     $form->setData($POST);
     if ($form->isValid()) {
         $data = $form->getInputFilter()->getValues();
         $page = $modelPage->getByAlias($POST['alias']);
         if ($POST['block_id']) {
             $block = $modelBlock->getById($POST['block_id']);
         } else {
             $block = new Block();
             $block->page_id = $page->id;
             $block->section = $POST['section'];
             $block->type = self::TYPE;
             $block->position = $POST['position'];
             $block->save();
         }
         if ($data['id']) {
             $linkBlock = $modelLinkBlock->getById($data['id']);
         } else {
             $linkBlock = new LinkBlock();
             $linkBlock->block_id = $block->id;
         }
         if ($linkBlock->type == 'file' && $data['type'] != 'file' && $linkBlock->href) {
             unset($linkBlock->href);
         }
         $linkBlock->type = $data['type'];
         $linkBlock->title = $data['title'];
         $linkBlock->position = $data['position'];
         switch ($linkBlock->type) {
             case 'link':
                 $linkBlock->href = $data['link'];
                 break;
             case 'file':
                 $upload = new Upload(array('path' => 'data/uploads/', 'destination' => $appConfig['public_path'] . '/'));
                 $path = $upload->process(array('file' => $POST['file']));
                 $data['file'] = $path['file'];
                 if ($data['file']) {
                     if ($linkBlock->href) {
                         unset($linkBlock->href);
                     }
                     $linkBlock->href = $data['file'];
                 }
                 break;
             case 'page':
                 $selectedPage = $modelPage->getById($data['entity_id']);
                 $linkBlock->entity_id = $data['entity_id'];
                 $linkBlock->href = $selectedPage->alias;
                 break;
         }
         if (!is_numeric($data['position'])) {
             $linkBlock->position = 1;
         }
         $linkBlock->save();
         return $this->jsonResponse(array('success' => true, 'data' => $linkBlock->toArray()));
     }
     return $this->jsonResponse(array('success' => false, 'errors' => $form->getMessages()));
 }