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); }
/** * Do the edit form for a record. * * @param Content $content A content record * @param array $contenttype The contenttype data * @param integer $id The record ID * @param boolean $new If TRUE this is a new record * @param boolean $duplicate If TRUE create a duplicate record * * @return array */ public function handleEditRequest($content, array $contenttype, $id, $new, $duplicate) { $contenttypeslug = $contenttype['slug']; $oldStatus = $content['status']; $allStatuses = ['published', 'held', 'draft', 'timed']; $allowedStatuses = []; foreach ($allStatuses as $status) { if ($this->app['users']->isContentStatusTransitionAllowed($oldStatus, $status, $contenttypeslug, $id)) { $allowedStatuses[] = $status; } } // For duplicating a record, clear base field values. if ($duplicate) { $content->setValues(['id' => '', 'slug' => '', 'datecreated' => '', 'datepublish' => '', 'datedepublish' => null, 'datechanged' => '', 'username' => '', 'ownerid' => '']); $this->app['logger.flash']->info(Trans::__('contenttypes.generic.duplicated-finalize', ['%contenttype%' => $contenttypeslug])); } // Set the users and the current owner of this content. if ($new || $duplicate) { // For brand-new and duplicated items, the creator becomes the owner. $contentowner = $this->app['users']->getCurrentUser(); } else { // For existing items, we'll just keep the current owner. $contentowner = $this->app['users']->getUser($content['ownerid']); } // Test write access for uploadable fields. $contenttype['fields'] = $this->setCanUpload($contenttype['fields']); if (!empty($content['templatefields']) && !empty($content['templatefields']->contenttype['fields'])) { $content['templatefields']->contenttype['fields'] = $this->setCanUpload($content['templatefields']->contenttype['fields']); } // Build context for Twig. $contextCan = ['upload' => $this->app['users']->isAllowed('files:uploads'), 'publish' => $this->app['users']->isAllowed('contenttype:' . $contenttypeslug . ':publish:' . $content['id']), 'depublish' => $this->app['users']->isAllowed('contenttype:' . $contenttypeslug . ':depublish:' . $content['id']), 'change_ownership' => $this->app['users']->isAllowed('contenttype:' . $contenttypeslug . ':change-ownership:' . $content['id'])]; $contextHas = ['incoming_relations' => is_array($content->relation), 'relations' => isset($contenttype['relations']), 'tabs' => $contenttype['groups'] !== false, 'taxonomy' => isset($contenttype['taxonomy']), 'templatefields' => $content->hasTemplateFields()]; $contextValues = ['datepublish' => $this->getPublishingDate($content['datepublish'], true), 'datedepublish' => $this->getPublishingDate($content['datedepublish'])]; $context = ['contenttype' => $contenttype, 'content' => $content, 'allowed_status' => $allowedStatuses, 'contentowner' => $contentowner, 'fields' => $this->app['config']->fields->fields(), 'fieldtemplates' => $this->getTempateFieldTemplates($contenttype, $content), 'fieldtypes' => $this->getUsedFieldtypes($contenttype, $content, $contextHas), 'groups' => $this->createGroupTabs($contenttype, $contextHas), 'can' => $contextCan, 'has' => $contextHas, 'values' => $contextValues]; return $context; }
/** * Save a record. * * @param Content $content * @param string $comment * * @throws \Bolt\Exception\StorageException * * @return int */ public function saveContent(Content $content, $comment = null) { $contenttype = $content->contenttype; $fieldvalues = $content->values; if (empty($contenttype)) { $this->app['logger.system']->error('Contenttype is required for ' . __FUNCTION__, ['event' => 'exception']); throw new StorageException('Contenttype is required for ' . __FUNCTION__); } // Test to see if this is a new record, or an update if (empty($fieldvalues['id'])) { $create = true; } else { $create = false; } // We need to verify if the slug is unique. If not, we update it. $getId = $create ? null : $fieldvalues['id']; $fieldvalues['slug'] = $this->getUri($fieldvalues['slug'], $getId, $contenttype['slug'], false, false); // Update the content object $content->setValues($fieldvalues); // Dispatch pre-save event if (!$this->inDispatcher && $this->app['dispatcher']->hasListeners(StorageEvents::PRE_SAVE)) { $event = new StorageEvent($content, ['contenttype' => $contenttype, 'create' => $create]); $this->app['dispatcher']->dispatch(StorageEvents::PRE_SAVE, $event); } // Decide whether to insert a new record, or update an existing one. if ($create) { $this->insertContent($content, $comment); } else { $this->updateContent($content, $comment); } // Update taxonomy and record relationships $this->updateTaxonomy($contenttype, $content->values['id'], $content->taxonomy); $this->updateRelation($contenttype, $content->values['id'], $content->relation); // Dispatch post-save event if (!$this->inDispatcher && $this->app['dispatcher']->hasListeners(StorageEvents::POST_SAVE)) { // Block loops $this->inDispatcher = true; $event = new StorageEvent($content, ['contenttype' => $contenttype, 'create' => $create]); $this->app['dispatcher']->dispatch(StorageEvents::POST_SAVE, $event); // Re-enable the dispatcher $this->inDispatcher = false; } return $content->values['id']; }
/** * 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; }
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()); }