/** * 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__, array('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, array('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($content, $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, array('contenttype' => $contenttype, 'create' => $create)); $this->app['dispatcher']->dispatch(StorageEvents::POST_SAVE, $event); // Re-enable the dispather $this->inDispatcher = false; } return $content->values['id']; }
public function getEmptyContent($contenttypeslug) { $contenttype = $this->getContentType($contenttypeslug); $content = new Bolt\Content('', $contenttypeslug); $values = array('id' => '', 'slug' => '', 'datecreated' => '', 'datechanged' => '', 'datepublish' => '', 'username' => '', 'status' => ''); foreach ($contenttype['fields'] as $key => $field) { $values[$key] = ''; // Set the default values. if (isset($field['default'])) { $values[$key] = $field['default']; } else { $values[$key] = ''; } } $content->setValues($values); // echo "<pre>\n" . util::var_dump($content, true) . "</pre>\n"; return $content; }
/** * 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; } 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)) { $templateContent = new Content($this->app, $this->getTemplateFieldsContentType(), [], false); $value = $templateContent; $this->populateTemplateFieldsContenttype($value); $templateContent->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; }
/** * 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']); } // Determine which templates will result in templatefields $templateFieldTemplates = $this->getTempateFieldTemplates($contenttype, $content); // Information flags about what the record contains $info = ['hasIncomingRelations' => is_array($content->relation), 'hasRelations' => isset($contenttype['relations']), 'hasTabs' => $contenttype['groups'] !== false, 'hasTaxonomy' => isset($contenttype['taxonomy']), 'hasTemplateFields' => $content->hasTemplateFields()]; // Generate tab groups $groups = $this->createGroupTabs($contenttype, $info); // Build context for Twig $context = ['contenttype' => $contenttype, 'content' => $content, 'allowed_status' => $allowedStatuses, 'contentowner' => $contentowner, 'fields' => $this->app['config']->fields->fields(), 'fieldtemplates' => $templateFieldTemplates, 'can_upload' => $this->app['users']->isAllowed('files:uploads'), 'groups' => $groups, 'has' => ['incoming_relations' => $info['hasIncomingRelations'], 'relations' => $info['hasRelations'], 'tabs' => $info['hasTabs'], 'taxonomy' => $info['hasTaxonomy'], 'templatefields' => $info['hasTemplateFields']]]; return $context; }