Esempio n. 1
0
 /**
  * Implementation of the beforesave event, handles uploading / saving and overwriting of image records
  * @param  \Cake\Event\Event       $event   [description]
  * @param  \Cake\ORM\Entity      $entity  [description]
  * @param  ArrayObject $options [description]
  * @return void
  */
 public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
 {
     $fields = $this->config('fields');
     $alias = $this->_table->registryAlias();
     $newOptions = [$this->_imagesTable->alias() => ['validate' => false]];
     $options['associated'] = $newOptions + $options['associated'];
     $entities = [];
     foreach ($fields as $_fieldName => $fieldType) {
         $uploadedImages = [];
         $field = $entity->{$_fieldName};
         $field = $fieldType == 'one' ? [$field] : $field;
         if (!$field) {
             continue;
         }
         foreach ($field as $index => $image) {
             $uploadeImage = null;
             if (!empty($image['tmp_name'])) {
                 // server based file uploads
                 $uploadeImage = $this->_upload($image['name'], $image['tmp_name'], false);
             } elseif (is_string($image)) {
                 // any other 'path' based uploads
                 $uploadeImage = $this->_upload($image, $image, true);
             }
             if (!empty($uploadeImage)) {
                 $uploadedImages[$index] = $uploadeImage + ['field_index' => $index, 'model' => $alias, 'field' => $_fieldName];
             }
         }
         if (!empty($uploadedImages)) {
             if (!$entity->isNew()) {
                 $imagesTableAlias = $this->_imagesTable->alias();
                 $preexisting = $this->_imagesTable->find()->where(['model' => $alias, 'field' => $_fieldName, 'foreign_key' => $entity->{$this->_table->primaryKey()}])->order(['field_index' => 'ASC']);
                 foreach ($preexisting as $image) {
                     if (isset($uploadedImages[$image->field_index])) {
                         $entities[$image->field_index] = $this->_imagesTable->patchEntity($image, $uploadedImages[$image->field_index]);
                     } elseif ($fieldType == 'one') {
                         $this->_imagesTable->delete($image);
                     }
                 }
             }
             $new = array_diff_key($uploadedImages, $entities);
             foreach ($new as $image) {
                 $entities[] = $this->_imagesTable->newEntity($image);
             }
         }
         $entity->dirty($_fieldName, false);
     }
     $entity->set('_images', $entities);
 }
Esempio n. 2
0
 /**
  * [_regenerate description]
  * @param  [type] $table [description]
  * @return void
  */
 protected function _regenerate($table)
 {
     $alias = $table->alias();
     $imagesTable = $table->imagesTable();
     $images = $imagesTable->find()->where(['model' => $table->registryAlias()]);
     if (isset($this->params['id'])) {
         $images->andWhere(['foreign_key' => $this->params['id']]);
     }
     $total = $images->count();
     $this->out(sprintf("<info>[%s]\t Regenerating presets for %s images</info>", $alias, $total), 0);
     $this->out('');
     $x = 1;
     foreach ($images as $image) {
         $table->generatePresets($image, $this->params['force']);
         $this->io()->overwrite(sprintf("<question>[%s]\t Creating presets... [%s/%s]</question>", $alias, $x, $total), 0);
         $x++;
     }
     $this->out('');
     $this->out(sprintf("<success>[%s]\t FINISHED</success>", $alias));
     $this->hr();
 }