/** * Get the excerpt of a given piece of text. * * @param int $length * @param bool $includeTitle * @param array|string|null $focus * * @return string|null */ public function getExcerpt($length = 200, $includeTitle = false, $focus = null) { $title = null; if ($includeTitle && $this->title !== null) { $title = Html::trimText(strip_tags($this->title), $length); $length = $length - strlen($title); } if ($this->body instanceof Content) { $this->body = $this->body->getValues(); } if (is_array($this->body)) { // Assume it's an array, strip some common fields that we don't need, implode the rest. $stripKeys = ['id', 'slug', 'datecreated', 'datechanged', 'username', 'ownerid', 'title', 'contenttype', 'status', 'taxonomy', 'templatefields']; foreach ($stripKeys as $key) { unset($this->body[$key]); } $excerpt = implode(' ', $this->body); } elseif (is_string($this->body) || is_object($this->body) && method_exists($this->body, '__toString')) { // otherwise we just use the string. $excerpt = (string) $this->body; } else { // Nope, got nothing. $excerpt = ''; } $excerpt = str_replace('>', '> ', $excerpt); if (empty($focus)) { $excerpt = Html::trimText(strip_tags($excerpt), $length); } else { $excerpt = $this->extractRelevant($focus, strip_tags($excerpt), $length); } if ($title !== null) { $excerpt = '<b>' . $title . '</b> ' . $excerpt; } return $excerpt; }
public function testSetupWithRecord() { $app = $this->getApp(); $content = new Content($app, 'pages'); $content->setValue('id', 5); $event = new StorageEvent($content); $this->assertEquals(5, $event->getId()); $this->assertEquals('pages', $event->getContentType()); }
public function testEditable() { $app = $this->getApp(); $handler = new HtmlHandler($app); $content = new Content($app); $content->setValues(['id' => 42, 'contenttype' => ['slug' => 'snail']]); $result = $handler->editable('<blink>Drop Bear Warning!</blink>', $content, 'paddock', false); $this->assertSame('<div class="Bolt-editable" data-id="42" data-contenttype="" data-field="paddock"><blink>Drop Bear Warning!</blink></div>', $result); }
public function testgetValues() { $app = $this->getApp(); $content = new Content($app, 'pages'); $content->setValue('title', 'Test Page'); $content->setValue('image', ['file' => 'image1.jpg', 'title' => 'Test image']); $values = $content->getValues(true); $this->assertEquals('Test Page', $values['title']); $this->assertEquals('{"file":"image1.jpg","title":"Test image"}', $values['image']); }
public function testIsCreate() { $app = $this->getApp(); $content = new Content($app, 'pages'); $content->setValue('id', 5); $event = new StorageEvent($content); $event->setArgument('create', true); $this->assertTrue($event->isCreate()); $event->setArgument('create', false); $this->assertFalse($event->isCreate()); }
public function testHomepageContent() { $app = $this->getApp(); $this->setRequest(Request::create('/')); $storage = $this->getMock('Bolt\\Storage', ['getContent'], [$app]); $content1 = new Content($app); $content1->setValue('id', 42); $storage->expects($this->once())->method('getContent')->will($this->returnValue($content1)); $this->setService('storage', $storage); $response = $this->controller()->homepage($this->getRequest()); $globals = $response->getGlobalContext(); $this->assertTrue($response instanceof BoltResponse); $this->assertSame([42 => $content1], $globals['records']); }
public function testRecord() { $contentType = $this->getService('storage')->getContentType('pages'); $request = Request::create('/pages/test'); $this->setRequest($request); $content = new Content($this->getApp(), $contentType); $content->setValues(['slug' => 'test', 'title' => 'test']); $this->getService('storage')->saveContent($content); $response = $this->controller()->record($request, 'pages', 'test'); $this->assertTrue($response instanceof TemplateResponse); $this->assertSame('page.twig', $response->getTemplate()->getTemplateName()); $this->assertNotEmpty($response->getGlobals()); }
/** * Create a list of fields types used in regular, template and virtual fields. * * @param array $contenttype * @param Content $content * @param array $has * * @return array */ private function getUsedFieldtypes(array $contenttype, Content $content, array $has) { $fieldtypes = ['meta' => true]; foreach ([$contenttype['fields'], $content->get('templatefields')->contenttype['fields'] ?: []] as $fields) { foreach ($fields as $field) { $fieldtypes[$field['type']] = true; } } if ($has['relations'] || $has['incoming_relations']) { $fieldtypes['relationship'] = true; } if ($has['taxonomy'] || is_array($contenttype['groups']) && in_array('taxonomy', $contenttype['groups'])) { $fieldtypes['taxonomy'] = true; } if ($has['templatefields'] || is_array($contenttype['groups']) && in_array('template', $contenttype['groups'])) { $fieldtypes['template'] = true; } return array_keys($fieldtypes); }
/** * Check if a given name is a valid column, and if it can be used in queries. * * @param string $name * @param array $contenttype * @param boolean $allowVariants * * @return boolean */ private function isValidColumn($name, $contenttype, $allowVariants = false) { $isValidColumn = false; if ($this->isMultiOrderQuery($name)) { $separatedOrders = $this->getOrderBys($name); } else { $separatedOrders = [$name]; } //Loop through each term if multiple foreach ($separatedOrders as $index => $name) { $name = trim($name); // Strip the minus in '-title' if allowed. if ($allowVariants) { if (strlen($name) > 0 && $name[0] == "-") { $name = substr($name, 1); } $name = $this->getFieldName($name); } // Check if the $name is in the contenttype's fields. if (isset($contenttype['fields'][$name])) { $isValidColumn = true; } if (in_array($name, Content::getBaseColumns())) { $isValidColumn = true; } // Repeating fields are handled differently so skip here if (isset($contenttype['fields'][$name]) && $contenttype['fields'][$name]['type'] == 'repeater') { $isValidColumn = false; } if (!$isValidColumn) { return false; } } return true; }
/** * Check if a given name is a valid column, and if it can be used in queries. * * @param string $name * @param array $contenttype * @param boolean $allowVariants * * @return boolean */ private function isValidColumn($name, $contenttype, $allowVariants = false) { // Strip the minus in '-title' if allowed. if ($allowVariants) { if (strlen($name) > 0 && $name[0] == "-") { $name = substr($name, 1); } $name = $this->getFieldName($name); } // Check if the $name is in the contenttype's fields. if (isset($contenttype['fields'][$name])) { return true; } if (in_array($name, Content::getBaseColumns())) { return true; } return false; }
/** * Set a ContentType record's individual value. * * @param string $key * @param mixed $value */ public function setValue($key, $value) { // Don't set templateFields if not a real contenttype if ($key === 'templatefields' && !$this->isRootType) { return; } // Check if the value need to be unserialized. if (is_string($value) && substr($value, 0, 2) === 'a:') { try { $unserdata = Lib::smartUnserialize($value); } catch (\Exception $e) { $unserdata = false; } if ($unserdata !== false) { $value = $unserdata; } } if ($key === 'id') { $this->id = $value; } // Set the user in the object. if ($key === 'ownerid' && !empty($value)) { $this->user = $this->app['users']->getUser($value); } // Only set values if they have are actually a field. $allowedcolumns = self::getBaseColumns(); $allowedcolumns[] = 'taxonomy'; if (!isset($this->contenttype['fields'][$key]) && !in_array($key, $allowedcolumns)) { return; } /** * This Block starts introducing new-style hydration into the legacy content object. * To do this we fetch the new field from the manager and hydrate a temporary entity. * * We don't return at this point so continue to let other transforms happen below so the * old behaviour will still happen where adjusted. */ if (isset($this->contenttype['fields'][$key]['type']) && $this->app['storage.field_manager']->hasCustomHandler($this->contenttype['fields'][$key]['type'])) { $newFieldType = $this->app['storage.field_manager']->getFieldFor($this->contenttype['fields'][$key]['type']); $newFieldType->mapping['fieldname'] = $key; $entity = new Content(); $newFieldType->hydrate([$key => $value], $entity); $value = $entity->{$key}; } if (in_array($key, ['datecreated', 'datechanged', 'datepublish', 'datedepublish'])) { if (!preg_match("/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/", $value)) { // @todo Try better date-parsing, instead of just setting it to // 'now' (or 'the past' for datedepublish) if ($key === 'datedepublish') { $value = null; } else { $value = date('Y-m-d H:i:s'); } } } if ($key === 'templatefields') { if (is_string($value) || is_array($value)) { if (is_string($value)) { try { $unserdata = Lib::smartUnserialize($value); } catch (\Exception $e) { $unserdata = false; } } else { $unserdata = $value; } if (is_array($unserdata)) { if (is_a($this, Legacy\Content::class)) { $value = new static($this->app, $this->getTemplateFieldsContentType(), [], false); } else { $value = new Legacy\Content($this->app, $this->getTemplateFieldsContentType(), [], false); } $value->setValues($unserdata); } else { $value = null; } } } if (!isset($this->values['datechanged']) || !preg_match("/(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})/", $this->values['datechanged'])) { $this->values['datechanged'] = date('Y-m-d H:i:s'); } $this->values[$key] = $value; }