/**
  * 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>'));
 }
 public function change(Event $event, ProfferPath $path)
 {
     // Detect and select the right file extension
     $type = $event->subject()[$path->getField()]['type'];
     switch ($type) {
         default:
         case "image/jpeg":
             $ext = '.jpg';
             break;
         case "image/png":
             $ext = '.png';
             break;
         case "image/gif":
             $ext = '.gif';
             break;
     }
     $newFilename = md5(uniqid(true)) . $ext;
     $path->setFilename($newFilename);
     $event->subject()[$path->getField()]['name'] = $newFilename;
     return $path;
 }
 /**
  * afterDelete method
  *
  * Remove images from records which have been deleted, if they exist
  *
  * @param Event $event The passed event
  * @param Entity $entity The entity
  * @param ArrayObject $options Array of options
  * @param ProfferPathInterface $path Inject and instance of ProfferPath
  * @return bool
  */
 public function afterDelete(Event $event, Entity $entity, ArrayObject $options, ProfferPathInterface $path = null)
 {
     foreach ($this->config() as $field => $settings) {
         $dir = $entity->get($settings['dir']);
         if (!empty($entity) && !empty($dir)) {
             if (!$path) {
                 $path = new ProfferPath($this->_table, $entity, $field, $settings);
             }
             $path->deleteFiles($path->getFolder(), true);
         }
         $path = null;
     }
     return true;
 }
 public function testCreatingPathFolderWhichExists()
 {
     $table = $this->getMockBuilder('Cake\\ORM\\Table')->setMethods(['alias'])->getMock();
     $table->method('alias')->willReturn('ProfferTest');
     $entity = new Entity(['photo' => 'image_640x480.jpg', 'photo_dir' => 'proffer_test']);
     $settings = ['root' => TMP . 'ProfferTests', 'dir' => 'photo_dir', 'thumbnailSizes' => ['square' => ['w' => 100, 'h' => 100], 'squareCrop' => ['w' => 100, 'h' => 100, 'crop' => true]]];
     $path = new ProfferPath($table, $entity, 'photo', $settings);
     mkdir(TMP . 'ProfferTests' . DS . 'proffertest' . DS . 'photo' . DS . 'proffer_test' . DS, 0777, true);
     $result = $path->createPathFolder();
     $this->assertEquals(true, $result);
 }