/**
  * Method that renders Entity values through Field Handler Factory.
  *
  * @param  Cake\ORM\Entity       $entity    Entity instance
  * @param  Cake\ORM\Table|string $table     Table instance
  * @param  array                 $fields    Fields to prettify
  * @return void
  */
 protected function _prettify(Entity $entity, $table, array $fields = [])
 {
     if (!$this->__fhf instanceof FieldHandlerFactory) {
         $this->__fhf = new FieldHandlerFactory();
     }
     if (empty($fields)) {
         $fields = array_keys($entity->toArray());
     }
     foreach ($fields as $field) {
         // handle belongsTo associated data
         if ($entity->{$field} instanceof Entity) {
             $tableName = $table->association($entity->{$field}->source())->className();
             $this->_prettify($entity->{$field}, $tableName);
         }
         // handle hasMany associated data
         if (is_array($entity->{$field})) {
             if (empty($entity->{$field})) {
                 continue;
             }
             foreach ($entity->{$field} as $associatedEntity) {
                 if (!$associatedEntity instanceof Entity) {
                     continue;
                 }
                 $tableName = $table->association($associatedEntity->source())->className();
                 $this->_prettify($associatedEntity, $tableName);
             }
         }
         $renderOptions = ['entity' => $entity];
         $entity->{$field} = $this->__fhf->renderValue($table instanceof Table ? $table->registryAlias() : $table, $field, $entity->{$field}, $renderOptions);
     }
 }
Example #2
0
 /**
  * Check if there is some files to upload and modify the entity before
  * it is saved.
  *
  * At the end, for each files to upload, unset their "virtual" property.
  *
  * @param Event  $event  The beforeSave event that was fired.
  * @param Entity $entity The entity that is going to be saved.
  *
  * @throws \LogicException When the path configuration is not set.
  * @throws \ErrorException When the function to get the upload path failed.
  *
  * @return void
  */
 public function beforeSave(Event $event, Entity $entity)
 {
     $config = $this->_config;
     foreach ($config['fields'] as $field => $fieldOption) {
         $data = $entity->toArray();
         $virtualField = $field . $config['suffix'];
         if (!isset($data[$virtualField]) || !is_array($data[$virtualField])) {
             continue;
         }
         $file = $entity->get($virtualField);
         if ((int) $file['error'] === UPLOAD_ERR_NO_FILE) {
             continue;
         }
         if (!isset($fieldOption['path'])) {
             throw new \LogicException(__('The path for the {0} field is required.', $field));
         }
         if (isset($fieldOption['prefix']) && (is_bool($fieldOption['prefix']) || is_string($fieldOption['prefix']))) {
             $this->_prefix = $fieldOption['prefix'];
         }
         $extension = (new File($file['name'], false))->ext();
         $uploadPath = $this->_getUploadPath($entity, $fieldOption['path'], $extension);
         if (!$uploadPath) {
             throw new \ErrorException(__('Error to get the uploadPath.'));
         }
         $folder = new Folder($this->_config['root']);
         $folder->create($this->_config['root'] . dirname($uploadPath));
         if ($this->_moveFile($entity, $file['tmp_name'], $uploadPath, $field, $fieldOption)) {
             if (!$this->_prefix) {
                 $this->_prefix = '';
             }
             $entity->set($field, $this->_prefix . $uploadPath);
         }
         $entity->unsetProperty($virtualField);
     }
 }
 /**
  * Get resource attributes.
  *
  * @param \Cake\ORM\Entity $resource Entity resource
  * @return array
  */
 public function getAttributes($resource)
 {
     if ($resource->has($this->idField)) {
         $hidden = array_merge($resource->hiddenProperties(), [$this->idField]);
         $resource->hiddenProperties($hidden);
     }
     return $resource->toArray();
 }
Example #4
0
 /**
  * @param Entity $page
  *
  * @return \Symfony\Component\Form\Form
  * @throws \Rad\DependencyInjection\Exception\ServiceNotFoundException
  */
 public static function getForm($page = null)
 {
     $data = null;
     if ($page) {
         $data = $page->toArray();
     }
     $action = $page ? Container::get('router')->generateUrl(['pages', $data['slug']]) : Container::get('router')->generateUrl(['pages']);
     $formFactory = Forms::createFormFactory();
     $options = ['action' => $action, 'method' => $page ? 'PUT' : 'POST'];
     return $formFactory->createBuilder('form', $data, $options)->add('title', 'text', ['required' => true, 'attr' => ['class' => 'form-control']])->add('slug', 'text', ['required' => true, 'attr' => ['class' => 'form-control']])->add('body', 'textarea', ['required' => true, 'attr' => ['class' => 'form-control wysiwyg']])->add('submit', 'submit')->getForm();
 }
Example #5
0
 /**
  * Test that toArray respects hidden properties.
  *
  * @return void
  */
 public function testToArrayHiddenProperties()
 {
     $data = ['secret' => 'sauce', 'name' => 'mark', 'id' => 1];
     $entity = new Entity($data);
     $entity->hiddenProperties(['secret']);
     $this->assertEquals(['name' => 'mark', 'id' => 1], $entity->toArray());
 }
Example #6
0
 /**
  * Email constructor.
  *
  * @param Entity $entity
  */
 public function __construct(Entity $entity)
 {
     $config = PluginConfig::getInstance()->getGroup('union_core');
     $this->_entity = $entity;
     $this->config = $config->params();
     $this->_entityId = $entity->get('id');
     $this->_arrayData = $entity->toArray();
     $this->_serverName = Union::serverName();
     $this->mailer = $this->config->get('mailer', 'mail');
     $this->fromName = $this->config->get('mailfrom', 'Union CMS');
     $this->fromEmail = $this->config->get('mailname', '*****@*****.**');
 }
Example #7
0
 /**
  * Bind data.
  *
  * @param Entity $data
  */
 public function bind(Entity $data)
 {
     $this->_data[$this->name] = $data->toArray();
 }
Example #8
0
 /**
  * converts a Entity into a flat Array
  * properties are inserted in first row
  *
  * @param Entity $entity
  * @return array
  */
 public function prepareEntityData(Entity $entity = null)
 {
     $entityArray = $entity->toArray();
     $data = [array_keys($entityArray)];
     $data[] = array_values($entityArray);
     return $data;
 }
 /**
  * Check if there is some files to upload and modify the entity before
  * it is saved.
  *
  * For each files to upload, unset their "virtual" property.
  *
  * @param Event  $event  The beforeSave event that was fired.
  * @param Entity $entity The entity that is going to be saved.
  *
  * @throws \ErrorException
  */
 public function beforeSave(Event $event, Entity $entity)
 {
     $config = $this->_config;
     /**
      * Foreach file set in the configuration, do the upload.
      */
     foreach ($config['fields'] as $field => $fieldOption) {
         $data = $entity->toArray();
         $virtualField = $field . $config['suffix'];
         /**
          * Check if the "virtual" field is in the data array and if this
          * field have a name param. (This is only for check if its a file type.)
          */
         if (isset($data[$virtualField]) && !empty($data[$virtualField]['name'])) {
             //Get the array (name, tmp_name etc) of the virtual field.
             $file = $entity->get($virtualField);
             /**
              * If the tmp_name is empty, that mean there was an error while uploading
              * the file in the temporary directory.
              */
             if (empty($file['tmp_name'])) {
                 continue;
             }
             if (!isset($fieldOption['path'])) {
                 throw new \ErrorException(__('Error to get the path for the {0} field.', $field));
             }
             /**
              * Check if the user has set an option for the prefix, else use the default prefix config.
              */
             if (isset($fieldOption['prefix']) && (is_bool($fieldOption['prefix']) || is_string($fieldOption['prefix']))) {
                 $this->_prefix = $fieldOption['prefix'];
             }
             /**
              * Get the upload path with identifiers replaced by their corresponding folders.
              */
             $uploadPath = $this->getUploadPath($entity, $fieldOption['path'], (new File($file['name'], false))->ext());
             if (!$uploadPath) {
                 throw new \ErrorException(__('Error to get the uploadPath.'));
             }
             /**
              * Create the folders if it doesn't exist.
              */
             $this->_folder->create(dirname($uploadPath), 0755);
             /**
              * Try to move the temporary file to the correct upload path.
              */
             if ($this->moveFile($entity, $file['tmp_name'], $uploadPath, $fieldOption, $field)) {
                 if (!$this->_prefix) {
                     $this->_prefix = '';
                 }
                 /**
                  * Set the new value for the current field.
                  */
                 $entity->set($field, $this->_prefix . $uploadPath);
             }
         }
         /**
          * Unset the virtual field from the Entity.
          */
         $entity->unsetProperty($virtualField);
     }
 }
 /**
  * Convert Entity resource values to strings.
  * Temporary fix for bug with resources and json_encode() (see link).
  *
  * @param  \Cake\ORM\Entity $entity Entity
  * @return void
  * @link   https://github.com/cakephp/cakephp/issues/9658
  */
 protected function _resourceToString(Entity $entity)
 {
     $fields = array_keys($entity->toArray());
     foreach ($fields as $field) {
         // handle belongsTo associated data
         if ($entity->{$field} instanceof Entity) {
             $this->_resourceToString($entity->{$field});
         }
         // handle hasMany associated data
         if (is_array($entity->{$field})) {
             if (empty($entity->{$field})) {
                 continue;
             }
             foreach ($entity->{$field} as $associatedEntity) {
                 if (!$associatedEntity instanceof Entity) {
                     continue;
                 }
                 $this->_resourceToString($associatedEntity);
             }
         }
         if (is_resource($entity->{$field})) {
             $entity->{$field} = stream_get_contents($entity->{$field});
         }
     }
 }
 /**
  * Validates a resource and returns the validation errors.
  *
  * @param Cake\ORM\Entity $resource
  *
  * @return array
  */
 public function validateResource(Entity &$resource)
 {
     $errors = $this->getResourceValidator()->errors($resource->toArray());
     $resource->errors($errors, null, true);
     return $errors;
 }