/**
  * get appropriate fields with files
  * move uploaded file and set path to entity field, but only if a file was selected
  *
  * @param Entity $entity
  */
 public function uploadFile(Entity $entity)
 {
     $config = $this->config();
     if (!is_array($config['field'])) {
         $field = $entity->get($config['field']);
         if (empty($field['tmp_name'])) {
             $entity->unsetProperty($config['field']);
         } else {
             if ($entity->get($config['field']) != $entity->getOriginal($config['field'])) {
                 $originalFilePath = $entity->getOriginal($config['field']);
                 $this->_delete($originalFilePath);
             }
             $filePath = $this->_moveFile($field);
             $entity->set($config['field'], $filePath);
         }
     } else {
         foreach ($config['field'] as $value) {
             $field = $entity->get($value);
             if (empty($field['tmp_name'])) {
                 $entity->unsetProperty($config['field']);
             } else {
                 if ($entity->get($config['field']) != $entity->getOriginal($config['field'])) {
                     $originalFilePath = $entity->getOriginal($config['field']);
                     $this->_delete($originalFilePath);
                 }
                 $filePath = $this->_moveFile($field);
                 $entity->set($config['field'], $filePath);
             }
         }
     }
 }
Пример #2
0
 /**
  * Tests unsetProperty whith multiple properties
  *
  * @return void
  */
 public function testUnsetMultiple()
 {
     $entity = new Entity(['id' => 1, 'name' => 'bar', 'thing' => 2]);
     $entity->unsetProperty(['id', 'thing']);
     $this->assertFalse($entity->has('id'));
     $this->assertTrue($entity->has('name'));
     $this->assertFalse($entity->has('thing'));
 }
Пример #3
0
 /**
  * [afterSave description]
  * @param  Event       $event   [description]
  * @param  Entity      $entity  [description]
  * @param  ArrayObject $options [description]
  * @return void
  */
 public function afterSave(Event $event, Entity $entity, ArrayObject $options)
 {
     if (!empty($entity->_images)) {
         foreach ($entity->_images as $imageEntity) {
             $this->generatePresets($imageEntity);
         }
         $entity->unsetProperty('_images');
     }
 }
Пример #4
0
 /**
  * Hashing the password and whitelisting
  *
  * @param \Cake\Event\Event $event
  * @param \Cake\ORM\Entity $entity
  * @throws \Exception
  * @return void
  */
 public function beforeSave(Event $event, Entity $entity)
 {
     $formField = $this->_config['formField'];
     $field = $this->_config['field'];
     if ($entity->get($formField) !== null) {
         $cost = !empty($this->_config['hashCost']) ? $this->_config['hashCost'] : 10;
         $options = ['cost' => $cost];
         /** @var \Cake\Auth\AbstractPasswordHasher $PasswordHasher */
         $PasswordHasher = $this->_getPasswordHasher($this->_config['passwordHasher'], $options);
         $entity->set($field, $PasswordHasher->hash($entity->get($formField)));
         if (!$entity->get($field)) {
             throw new Exception('Empty field');
         }
         $entity->unsetProperty($formField);
         //$entity->set($formField, null);
         if ($this->_config['confirm']) {
             $formFieldRepeat = $this->_config['formFieldRepeat'];
             $entity->unsetProperty($formFieldRepeat);
             //unset($Model->data[$table->alias()][$formFieldRepeat]);
         }
         if ($this->_config['current']) {
             $formFieldCurrent = $this->_config['formFieldCurrent'];
             $entity->unsetProperty($formFieldCurrent);
             //unset($Model->data[$table->alias()][$formFieldCurrent]);
         }
     }
 }
Пример #5
0
 /**
  * Unsets the temporary `_i18n` property after the entity has been saved
  *
  * @param \Cake\Event\Event $event The beforeSave event that was fired
  * @param \Cake\ORM\Entity $entity The entity that is going to be saved
  * @return void
  */
 public function afterSave(Event $event, Entity $entity)
 {
     $entity->unsetProperty('_i18n');
 }
 /**
  * @param Entity $entity
  * @return void
  */
 public function encodeBitmaskData(Entity $entity)
 {
     $field = $this->_config['field'];
     if (!($mappedField = $this->_config['mappedField'])) {
         $mappedField = $field;
     }
     $default = null;
     $schema = $this->_table->schema()->column($field);
     if ($schema && isset($schema['default'])) {
         $default = $schema['default'];
     }
     if ($this->_config['defaultValue'] !== null) {
         $default = $this->_config['defaultValue'];
     }
     if ($entity->get($mappedField) !== null) {
         $entity->set($field, $this->encodeBitmask($entity->get($mappedField), $default));
     }
     if ($field !== $mappedField) {
         $entity->unsetProperty($mappedField);
     }
 }
Пример #7
0
 /**
  * Ensures that content content has the correct publishing status based in content
  * type restrictions.
  *
  * If it's a new content it will set the correct status. However if it's an
  * existing content and user has no publishing permissions this method will not
  * change content's status, so it will remain published if it was already
  * published by an administrator.
  *
  * @param \Cake\ORM\Entity $entity The content
  * @return void
  */
 protected function _ensureStatus(Entity $entity)
 {
     if (!$entity->has('status')) {
         return;
     }
     if (!$entity->has('content_type') && ($entity->has('content_type_id') || $entity->has('content_type_slug'))) {
         if ($entity->has('content_type_id')) {
             $type = $this->ContentTypes->get($entity->get('content_type_id'));
         } else {
             $type = $this->ContentTypes->find()->where(['content_type_slug' => $entity->get('content_type_id')])->limit(1)->first();
         }
     } else {
         $type = $entity->get('content_type');
     }
     if ($type && !$type->userAllowed('publish')) {
         if ($entity->isNew()) {
             $entity->set('status', false);
         } else {
             $entity->unsetProperty('status');
         }
     }
 }
 /**
  * Unsets the temporary `__version` property after the entity has been saved
  *
  * @param \Cake\Event\Event $event The beforeSave event that was fired
  * @param \Cake\ORM\Entity $entity The entity that is going to be saved
  * @return void
  */
 public function afterSave(Event $event, Entity $entity)
 {
     $property = $this->versionAssociation()->property();
     $entity->unsetProperty($property);
 }
 /**
  * Hashing the password and whitelisting
  *
  * @param Event $event
  * @return void
  */
 public function beforeSave(Event $event, Entity $entity)
 {
     $formField = $this->_config['formField'];
     $field = $this->_config['field'];
     if ($entity->get($formField) !== null) {
         $cost = !empty($this->_config['hashCost']) ? $this->_config['hashCost'] : 10;
         $options = ['cost' => $cost];
         $PasswordHasher = $this->_getPasswordHasher($this->_config['passwordHasher']);
         $entity->set($field, $PasswordHasher->hash($entity->get($formField), $options));
         if (!$entity->get($field)) {
             throw new \Exception('Empty field');
         }
         $entity->unsetProperty($formField);
         //$entity->set($formField, null);
         if ($this->_config['confirm']) {
             $formFieldRepeat = $this->_config['formFieldRepeat'];
             $entity->unsetProperty($formFieldRepeat);
             //unset($Model->data[$table->alias()][$formFieldRepeat]);
         }
         if ($this->_config['current']) {
             $formFieldCurrent = $this->_config['formFieldCurrent'];
             $entity->unsetProperty($formFieldCurrent);
             //unset($Model->data[$table->alias()][$formFieldCurrent]);
         }
     }
     // Update whitelist
     $this->_modifyWhitelist($entity, true);
     return true;
 }