/** * 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 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']); }
/** * Update a Bolt contenttype record. * * @param \Bolt\Legacy\Content $content The content object to be updated * @param string $comment Add a comment to save with change. * * @throws \Bolt\Exception\StorageException * * @return bool */ private function updateContent(Content $content, $comment = null) { $tablename = $this->getContenttypeTablename($content->contenttype); // Set the date the record was changed $content->setValue('datechanged', date('Y-m-d H:i:s')); // Test that the record exists in the database $oldContent = $this->findContent($tablename, $content['id']); if (empty($oldContent)) { throw new StorageException('Attempted to update a non-existent record'); } // Get the JSON database prepared values and make sure it's valid $fieldvalues = $this->getValidSaveData($content->getValues(true), $content->contenttype); // Do the actual update, and log it. $res = $this->app['db']->update($tablename, $fieldvalues, ['id' => $content['id']]); if ($res > 0) { $this->logUpdate($content->contenttype['slug'], $content['id'], $fieldvalues, $oldContent, $comment); return true; } }
/** * Update a Bolt contenttype record. * * @param \Bolt\Legacy\Content $content The content object to be updated * @param string $comment Add a comment to save with change. * * @throws \Bolt\Exception\StorageException * * @return bool */ private function updateContent(Content &$content, $comment = null, $extra = []) { $synced = $content->contenttype['blimp_mode'] === 'sync'; // $store = $content->contenttype['blimp_type'] === 'store'; $collection = $this->getContentTypeCollection($content->contenttype); $id = $synced ? $content['blimp_id'] : $content['id']; $oldContent = $this->findContent($collection, $id); // TODO: Verify if blimp_id changed // $oldContentDb = parent::findContent(parent::getContenttypeTablename($contenttype), $id); // if (empty($oldContent)) { // if($synced) { // return $this->insertContent($content, $comment); // } // throw new StorageException('Attempted to update a non-existent record'); // } // Get the JSON database prepared values and make sure it's valid $fieldvalues = $this->getValidSaveData($content->getValues(false), $content->contenttype); $fieldvalues = array_merge($fieldvalues, $extra); $uri = $collection . '/' . $id; $res = $this->app['blimp_client.request']('PUT', $uri, null, $fieldvalues); if ($res['status'] == 200) { $this->logUpdate($content->contenttype['slug'], $content['id'], $fieldvalues, $oldContent, $comment); return true; } }