/** * Get uploaded image URL. * Return wrong URL if path config is not under webroot. * UrlHelper::build generates URL. * * @param \Cake\ORM\Entity $entity The entity * @param string $field The image field name * @return string */ public function url(Entity $entity, $field) { $table = TableRegistry::get($entity->source()); $folder = $table->getUploadFolder($entity, $field); $path = str_replace(WWW_ROOT, '', $folder); $path .= $entity->get($field); return $this->Url->build("/{$path}"); }
public function afterSave(Event $event, Entity $entity) { $model = substr(strrchr($entity->source(), '.'), 1); if (!$model) { $model = $entity->source(); } $type_id = self::HISTORIES_CREATE; if (!$entity->isNew()) { $type_id = self::HISTORIES_UPDATE; } $user_id = 0; // Unknow debug($this->request->session()->read('Auth.User.id')); die; $this->Histories = TableRegistry::get('Histories'); $history = $this->Histories->newEntity(['model' => $model, 'foreign_key' => $entity->id, 'type_id' => $type_id, 'user_id' => $user_id, 'created' => new Time()]); $this->Histories->save($history); }
/** * Format nested list element * * Callback method used by The NestableHelper * * @param array $data NestableHelper configuration * @param \Cake\ORM\Entity $entity Entity to format */ public function nestedListFormat(array $data, Entity $entity) { $displayField = TableRegistry::get($entity->source())->displayField(); $html = '<span class="pull-right p10 pb5">'; $html .= $this->Html->link('<i class="fa fa-edit"></i> ', ['action' => 'edit', $entity->id], ['class' => '', 'escape' => false, 'title' => __d('admin', 'Edit')]); $html .= $this->Form->postLink('<i class="fa fa-trash-o"></i>', ['action' => 'delete', $entity['id']], ['class' => '', 'title' => __d('admin', 'Delete {0}', Inflector::humanize($entity->source)), 'escape' => false, 'confirm' => __d('admin', 'Are you sure you want to delete #{0} {1}?', $entity->id, $entity->{$displayField})]); $html .= '</span>'; $html .= '<div class="dd-handle" data-id="' . $entity->id . '">'; if ($entity->active === false) { $html .= '<span class="text-muted">' . $entity->{$displayField} . '</span>'; $html .= ' <span class="text-danger"><i class="fa fa-ban"></i><em class="sr-only">Non active</em></span>'; } else { $html .= $entity->{$displayField}; } $html .= '</div>'; return $html; }
/** * Tests that the table can be derived from the entity source if it is present * * @return void */ public function testTableFromEntitySource() { $entity = new Entity(); $entity->source('Articles'); $context = new EntityContext($this->request, ['entity' => $entity]); $expected = ['id', 'author_id', 'title', 'body', 'published']; $this->assertEquals($expected, $context->fieldNames()); }
/** * Tests the source method * * @return void */ public function testSource() { $entity = new Entity(); $this->assertNull($entity->source()); $entity->source('foos'); $this->assertEquals('foos', $entity->source()); }
public function getUploadPath(Entity $entity, $path, $extension) { $replace = array('%uuid' => String::uuid(), '%id' => $entity->id, '%y' => date('Y'), '%m' => date('m'), '/' => DS); $path = $this->_config['assets_dir'] . DS . Inflector::tableize($entity->source()) . DS . strtr($path, $replace) . '.' . $extension; return $path; }
/** * Test iteration after serialization * * @return void */ public function testIteratorAfterSerializationHydrated() { $query = $this->table->find('all'); $results = unserialize(serialize($query->all())); // Use a loop to test Iterator implementation foreach ($results as $i => $row) { $expected = new \Cake\ORM\Entity($this->fixtureData[$i]); $expected->isNew(false); $expected->source($this->table->alias()); $expected->clean(); $this->assertEquals($expected, $row, "Row {$i} does not match"); } }
/** * Get the path formatted without its identifiers to upload the file. * @param \Cake\ORM\Entity $entity The entity that is going to be saved. * @param array $file The file array * @param string $path The path * @return string */ private function path(Entity $entity, $file = [], $path = false) { // get extension & path $extension = (new File($file['name'], false))->ext(); $path = trim(str_replace(['/', '\\'], DS, $path), DS); // handle identifiers $identifiers = [':model' => strtolower($entity->source()), ':md5' => md5(rand() . uniqid() . time())]; // output return strtr($path, $identifiers) . '.' . strtolower($extension); }