Ejemplo n.º 1
0
 /**
  * Load a table, get it's config and then regenerate the thumbnails for that tables upload fields.
  *
  * @param string $table The name of the table
  * @return void
  */
 public function generate($table)
 {
     $this->checkTable($table);
     $config = $this->Table->behaviors()->Proffer->config();
     foreach ($config as $field => $settings) {
         $records = $this->{$this->Table->alias()}->find()->select([$this->Table->primaryKey(), $field, $settings['dir']])->where(["{$field} IS NOT NULL", "{$field} != ''"]);
         foreach ($records as $item) {
             if ($this->param('verbose')) {
                 $this->out(__('Processing ' . $this->Table->alias() . ' ' . $item->get($this->Table->primaryKey())));
             }
             if (!empty($this->param('path-class'))) {
                 $class = (string) $this->param('path-class');
                 $path = new $class($this->Table, $item, $field, $settings);
             } else {
                 $path = new ProfferPath($this->Table, $item, $field, $settings);
             }
             if (!empty($this->param('image-class'))) {
                 $class = (string) $this->param('image_class');
                 $transform = new $class($this->Table, $path);
             } else {
                 $transform = new ImageTransform($this->Table, $path);
             }
             $transform->processThumbnails($settings);
             if ($this->param('verbose')) {
                 $this->out(__('Thumbnails regenerated for ' . $path->fullPath()));
             } else {
                 $this->out(__('Thumbnails regenerated for ' . $this->Table->alias() . ' ' . $item->get($field)));
             }
         }
     }
     $this->out($this->nl(0));
     $this->out(__('<info>Completed</info>'));
 }
 /**
  * beforeSave method
  *
  * @param Event $event The event
  * @param Entity $entity The entity
  * @param ArrayObject $options Array of options
  * @param ProfferPathInterface $path Inject an instance of ProfferPath
  * @return true
  * @throws Exception
  */
 public function beforeSave(Event $event, Entity $entity, ArrayObject $options, ProfferPathInterface $path = null)
 {
     foreach ($this->config() as $field => $settings) {
         if ($entity->has($field) && is_array($entity->get($field)) && $entity->get($field)['error'] === UPLOAD_ERR_OK) {
             // Allow path to be injected or set in config
             if (!empty($settings['pathClass'])) {
                 $path = new $settings['pathClass']($this->_table, $entity, $field, $settings);
             } elseif (!isset($path)) {
                 $path = new ProfferPath($this->_table, $entity, $field, $settings);
             }
             $event = new Event('Proffer.afterPath', $entity, ['path' => $path]);
             $this->_table->eventManager()->dispatch($event);
             if (!empty($event->result)) {
                 $path = $event->result;
             }
             $path->createPathFolder();
             if ($this->moveUploadedFile($entity->get($field)['tmp_name'], $path->fullPath())) {
                 $entity->set($field, $path->getFilename());
                 $entity->set($settings['dir'], $path->getSeed());
                 // Only generate thumbnails for image uploads
                 if (getimagesize($path->fullPath()) !== false && isset($settings['thumbnailSizes'])) {
                     // Allow the transformation class to be injected
                     if (!empty($settings['transformClass'])) {
                         $imageTransform = new $settings['transformClass']($this->_table, $path);
                     } else {
                         $imageTransform = new ImageTransform($this->_table, $path);
                     }
                     $imageTransform->processThumbnails($settings);
                 }
             } else {
                 throw new Exception('Cannot upload file');
             }
         }
         unset($path);
     }
     return true;
 }