Esempio n. 1
0
 /**
  * {@inheritDoc}
  */
 public function beforeSave(Field $field, $post)
 {
     $value = $post;
     if (is_array($value)) {
         $value = implode(' ', array_values($value));
     }
     $field->set('value', $value);
     $field->set('extra', $post);
 }
Esempio n. 2
0
 /**
  * {@inheritDoc}
  *
  * - extra: Holds string date incoming from POST
  * - value: Holds datetime information
  */
 public function beforeSave(Field $field, $post)
 {
     if (!empty($post['date']) && !empty($post['format'])) {
         $date = $post['date'];
         $format = $post['format'];
         if ($date = DateToolbox::createFromFormat($format, $date)) {
             $field->set('extra', $post['date']);
         } else {
             $field->metadata->entity->errors($field->name, __d('field', 'Invalid date/time, it must match the the pattern: {0}', $format));
             return false;
         }
         $field->set('value', date_timestamp_get($date));
     } else {
         $field->set('value', null);
     }
     return true;
 }
Esempio n. 3
0
 /**
  * We catch all field rendering request (from CMS\View\View) here, then we
  * dispatch to their corresponding FieldHandler.
  *
  * If the field object being rendered has been set to "hidden" for the current
  * view mode it will not be rendered.
  *
  * @param \Cake\Event\Event $event The event that was triggered
  * @param \Field\Model\Entity\Field $field Mock entity
  * @param array $options Additional array of options
  * @return string The rendered field
  */
 public function renderField(Event $event, $field, $options = [])
 {
     $viewMode = $this->viewMode();
     if (isset($field->metadata->view_modes[$viewMode]) && !$field->metadata->view_modes[$viewMode]['hidden']) {
         $event->stopPropagation();
         // We don't want other plugins to catch this
         $result = (string) $field->render($event->subject());
         if (!$field->metadata->view_modes[$viewMode]['shortcodes']) {
             $result = $event->subject()->stripShortcodes($result);
         }
         return $result;
     }
     return '';
 }
Esempio n. 4
0
 /**
  * {@inheritDoc}
  */
 public function afterSave(Field $field)
 {
     $entity = $field->get('metadata')->get('entity');
     $table = TableRegistry::get($entity->source());
     $pk = $table->primaryKey();
     if ($entity->has($pk)) {
         $TermsCache = TableRegistry::get('Taxonomy.EntitiesTerms');
         $tableAlias = Inflector::underscore($table->alias());
         $extra = !is_array($field->extra) ? [$field->extra] : $field->extra;
         $TermsCache->deleteAll(['entity_id' => $entity->get($pk), 'table_alias' => $tableAlias, 'field_instance_id' => $field->metadata->instance_id]);
         foreach ($extra as $termId) {
             Cache::delete("t{$termId}", 'terms_count');
             $cacheEntity = $TermsCache->newEntity(['entity_id' => $entity->get($pk), 'term_id' => $termId, 'table_alias' => $tableAlias, 'field_instance_id' => $field->metadata->instance_id]);
             $TermsCache->save($cacheEntity);
         }
     }
 }
Esempio n. 5
0
 /**
  * {@inheritDoc}
  *
  * - extra: Holds a list (array) of files and their in formation (mime-icon,
  *   file name, etc).
  *
  * - value: Holds a text containing all file names separated by space.
  */
 public function beforeSave(Field $field, $post)
 {
     // FIX Removes the "dummy" input from extra if exists, the "dummy" input is
     // used to force Field Handler to work when empty POST information is sent
     $extra = [];
     foreach ((array) $field->extra as $k => $v) {
         if (is_integer($k)) {
             $extra[] = $v;
         }
     }
     $field->set('extra', $extra);
     $files = (array) $post;
     if (!empty($files)) {
         $value = [];
         foreach ($files as $k => $file) {
             if (!is_integer($k)) {
                 unset($files[$k]);
                 continue;
             } else {
                 $file = array_merge(['mime_icon' => '', 'file_name' => '', 'file_size' => '', 'description' => ''], (array) $file);
             }
             $value[] = trim("{$file['file_name']} {$file['description']}");
         }
         $field->set('value', implode(' ', $value));
         $field->set('extra', $files);
     }
     if ($field->metadata->value_id) {
         $newFileNames = Hash::extract($files, '{n}.file_name');
         try {
             $prevFiles = (array) TableRegistry::get('Eav.EavValues')->get($field->metadata->value_id)->extra;
         } catch (\Exception $ex) {
             $prevFiles = [];
         }
         foreach ($prevFiles as $f) {
             if (!in_array($f['file_name'], $newFileNames)) {
                 $file = normalizePath(WWW_ROOT . "/files/{$field->metadata->settings['upload_folder']}/{$f['file_name']}", DS);
                 $file = new File($file);
                 $file->delete();
             }
         }
     }
     return true;
 }
 /**
  * {@inheritDoc}
  */
 public function beforeSave(Field $field, $post)
 {
     $values = [];
     $extra = ['from' => ['string' => null, 'timestamp' => null], 'to' => ['string' => null, 'timestamp' => null]];
     foreach (['from', 'to'] as $type) {
         if (!empty($post[$type]['string']) && !empty($post[$type]['format'])) {
             $date = $post[$type]['string'];
             $format = $post[$type]['format'];
             if ($date = DateToolbox::createFromFormat($format, $date)) {
                 $extra[$type]['string'] = $post[$type]['string'];
                 $extra[$type]['timestamp'] = date_timestamp_get($date);
                 $values[] = $extra[$type]['timestamp'] . ' ' . $post[$type]['string'];
             } else {
                 $typeLabel = $type == 'from' ? __d('field', 'Start') : __d('field', 'Finish');
                 $field->metadata->entity->errors($field->name, __d('field', 'Invalid date/time range, "{0}" date must match the the pattern: {1}', $typeLabel, $format));
                 return false;
             }
         }
     }
     $field->set('value', implode(' ', $values));
     $field->set('extra', $extra);
     return true;
 }
Esempio n. 7
0
 /**
  * {@inheritDoc}
  */
 public function beforeSave(Field $field, $post)
 {
     $field->set('extra', null);
     $field->set('value', $post);
 }
 /**
  * Creates a new Virtual "Field" to be attached to the given entity.
  *
  * This mock Field represents a new property (table column) of the entity.
  *
  * @param \Cake\Datasource\EntityInterface $entity The entity where the
  *  generated virtual field will be attached
  * @param \Cake\Datasource\EntityInterface $attribute The attribute where to get
  *  the information when creating the mock field.
  * @return \Field\Model\Entity\Field
  */
 protected function _prepareMockField(EntityInterface $entity, EntityInterface $attribute)
 {
     $type = $this->_toolbox->mapType($attribute->get('type'));
     if (!$attribute->has(':value')) {
         $bundle = $this->_resolveBundle($entity);
         $conditions = ['EavAttribute.table_alias' => $this->_table->table(), 'EavAttribute.name' => $attribute->get('name'), 'EavValues.entity_id' => $entity->get((string) $this->_table->primaryKey())];
         if ($bundle) {
             $conditions['EavAttribute.bundle'] = $bundle;
         }
         $storedValue = TableRegistry::get('Eav.EavValues')->find()->contain(['EavAttribute'])->select(['id', "value_{$type}", 'extra'])->where($conditions)->limit(1)->first();
     } else {
         $storedValue = $attribute->get(':value');
     }
     $mockField = new Field(['name' => $attribute->get('name'), 'label' => $attribute->get('instance')->get('label'), 'value' => null, 'extra' => null, 'metadata' => new Entity(['value_id' => null, 'instance_id' => $attribute->get('instance')->get('id'), 'attribute_id' => $attribute->get('id'), 'entity_id' => $this->_toolbox->getEntityId($entity), 'table_alias' => $attribute->get('table_alias'), 'type' => $type, 'bundle' => $attribute->get('bundle'), 'handler' => $attribute->get('instance')->get('handler'), 'required' => $attribute->get('instance')->required, 'description' => $attribute->get('instance')->description, 'settings' => $attribute->get('instance')->settings, 'view_modes' => $attribute->get('instance')->view_modes, 'entity' => $entity, 'errors' => []])]);
     if ($storedValue) {
         $mockField->set('value', $this->_toolbox->marshal($storedValue->get("value_{$type}"), $type));
         $mockField->set('extra', $storedValue->get('extra'));
         $mockField->metadata->set('value_id', $storedValue->id);
     }
     $mockField->isNew($entity->isNew());
     return $mockField;
 }