/** * 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(); }
/** * Corrigir os campos de data ou timestamp * @param Entity $entity Uma instância * @return void */ private function __ajustarDatas(Entity &$entity) { foreach ($this->campos[$this->_table->alias()] as $campo) { if ($entity->has($campo) && $entity->get($campo) !== "") { // DATA E HORA if ($this->__isDataHora($entity->get($campo))) { $this->__ajustarDataHora($entity, $campo); } elseif (preg_match('/\\d{1,2}\\/\\d{1,2}\\/\\d{2,4}/', $entity->get($campo))) { // DATA $this->__ajustarData($entity, $campo); } } } }
/** * beforeSave method * * @param Event $event The event * @param Entity $entity The entity * @param ArrayObject $options Array of options * @param ProfferPathInterface $path Inject an instance of ProfferPath * @return true * @throws Exception */ public function beforeSave(Event $event, Entity $entity, ArrayObject $options, ProfferPathInterface $path = null) { foreach ($this->config() as $field => $settings) { if ($entity->has($field) && is_array($entity->get($field)) && $entity->get($field)['error'] === UPLOAD_ERR_OK) { // Allow path to be injected or set in config if (!empty($settings['pathClass'])) { $path = new $settings['pathClass']($this->_table, $entity, $field, $settings); } elseif (!isset($path)) { $path = new ProfferPath($this->_table, $entity, $field, $settings); } $event = new Event('Proffer.afterPath', $entity, ['path' => $path]); $this->_table->eventManager()->dispatch($event); if (!empty($event->result)) { $path = $event->result; } $path->createPathFolder(); if ($this->moveUploadedFile($entity->get($field)['tmp_name'], $path->fullPath())) { $entity->set($field, $path->getFilename()); $entity->set($settings['dir'], $path->getSeed()); // Only generate thumbnails for image uploads if (getimagesize($path->fullPath()) !== false && isset($settings['thumbnailSizes'])) { // Allow the transformation class to be injected if (!empty($settings['transformClass'])) { $imageTransform = new $settings['transformClass']($this->_table, $path); } else { $imageTransform = new ImageTransform($this->_table, $path); } $imageTransform->processThumbnails($settings); } } else { throw new Exception('Cannot upload file'); } } unset($path); } return true; }
/** * 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')); }
/** * Get old order and scope values. * * @param \Cake\ORM\Entity $entity Entity. * * @return array */ protected function _getOldValues(Entity $entity) { $config = $this->config(); $fields = array_merge($config['scope'], [$config['order']]); $values = []; foreach ($fields as $field) { if ($entity->dirty($field)) { $values[$field] = $entity->getOriginal($field); } elseif ($entity->has($field)) { $values[$field] = $entity->get($field); } } if (count($fields) != count($values)) { $primaryKey = $entity->get($this->_table->primaryKey()); $values = $this->_table->get($primaryKey, ['fields' => $fields, 'limit' => 1])->toArray(); } $order = $values[$config['order']]; unset($values[$config['order']]); return [$order, $values]; }
/** * Restore original (unencrypted) values after saving to DB * @param Event $event Event object * @param Entity $entity Entity object * @param ArrayObject $options Save options array * @return void */ public function afterSave(Event $event, Entity $entity, ArrayObject $options) { if ($entity->has('_cyphered') && is_array($entity->_cyphered)) { foreach ($entity->_cyphered as $field => $value) { $entity->set($field, $value); $entity->clean(); } unset($entity->_cyphered); } }
/** * Get Title * * @param Cake\ORM\Entity $uri A complete uri entity * @return string */ public function getTitle(Entity $uri) { $title = $this->config('defaults.title'); $prefix = $this->config('defaults.prefix'); $suffix = $this->config('defaults.suffix'); if ($uri->has('seo_title') && $uri->seo_title) { $title = $uri->seo_title->title; } return $prefix . $title . $suffix; }
/** * 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'); } } }
/** * Generate a slug for the given string and entity. The generated slug is * unique across the entire table. * * @param string $string String from where to generate slug * @param \Cake\ORM\Entity $entity The entity for which generate the slug * @return string Slug for given string */ protected function _slug($string, $entity) { $string = $this->_mbTrim(mb_strtolower($string)); $config = $this->config(); $slug = Inflector::slug($string, $config['separator']); $pk = $this->_table->primaryKey(); if (mb_strlen($slug) > $config['length']) { $slug = mb_substr($slug, 0, $config['length']); } $conditions = ["{$config['slug']} LIKE" => "{$slug}%"]; if ($entity->has($pk)) { $conditions["{$pk} NOT IN"] = [$entity->{$pk}]; } $same = $this->_table->find()->where($conditions)->all()->extract($config['slug'])->toArray(); if (!empty($same)) { $initialSlug = $slug; $index = 1; while ($index > 0) { $nextSlug = "{$initialSlug}{$config['separator']}{$index}"; if (!in_array($nextSlug, $same)) { $slug = $nextSlug; $index = -1; } $index++; } } return $slug; }
/** * Internal method to recursively generate the menu. * * @param \Cake\ORM\Query $items Items to render * @param int $depth Current iteration depth * @return string HTML */ protected function _render($items, $depth = 0) { $content = ''; $formatter = $this->config('formatter'); foreach ($items as $item) { $children = ''; if (is_array($item)) { $item = new Entity($item); } if ($item->has('children') && !empty($item->children) && $item->expanded) { $children = $this->formatTemplate('parent', ['attrs' => $this->templater()->formatAttributes(['class' => $this->config('dropdown') ? 'dropdown-menu multi-level' : '', 'role' => 'menu']), 'content' => $this->_render($item->children, $depth + 1)]); } $this->_index++; $info = ['index' => $this->_index, 'total' => $this->_count, 'active' => $this->Link->isActive($item), 'depth' => $depth, 'hasChildren' => !empty($children), 'children' => $children]; $content .= $formatter($item, $info); } return $content; }
/** * Format Template * * Return a formated template to use as content for a tag * ex : * Hello {{name}} -> Hello John * {{name}} is the property entity * * @param string $pattern a pattern to match * @param Cake\ORM\Entity $entity The entity * @return string * @todo Allow {{model.field}} syntax */ protected function _formatTemplate($pattern, $entity) { $template = preg_replace_callback('/{{([\\w\\.]+)}}/', function ($matches) use($entity) { if (preg_match('/^([_\\w]+)\\.(\\w+)$/', $matches[1], $x)) { $table = TableRegistry::get(Inflector::tableize($x[1])); $matchEntity = $table->get($entity->{$x[1] . '_id'}); return $matchEntity->{$x[2]}; } if ($entity->has($matches[1])) { return $entity->{$matches[1]}; } }, $pattern); return trim($template); }
/** * Here we set default values for plugin's settings. * * Triggers the `Plugin.<PluginName>.settingsDefaults` event, event listeners * should catch the event and return an array as `key` => `value` with default * values. * * @param \Cake\Event\Event $event The event that was triggered * @param \Cake\ORM\Entity $plugin The plugin entity where to put those values * @return array */ public function settingsDefaultValues(Event $event, Entity $plugin) { if ($plugin->has('name')) { return (array) $this->trigger("Plugin.{$plugin->name}.settingsDefaults", $plugin)->result; } return []; }