/** * Form Handler to save a content quick create. * * @param Form $form * * @return string|bool */ public static function QuickDraftSave(Form $form) { if (!$form->getElementValue('title')) { \Core\set_message('All pages must have titles.', 'error'); return false; } /** @var $model ContentModel */ $model = new ContentModel(); /** @var $page PageModel Page object for this model, already linked up! */ $page = $model->getLink('Page'); // The content nickname is derived from the page title. $model->set('nickname', $form->getElementValue('title')); $model->save(); $ins = new InsertableModel(); $ins->set('site', $page->get('site')); $ins->set('baseurl', '/content/view/' . $model->get('id')); $ins->set('name', 'body'); $ins->set('value', '<p>' . nl2br($form->getElementValue('content')) . '</p>'); $ins->save(); $page->set('title', $form->getElementValue('title')); $page->set('published_status', 'draft'); $page->set('editurl', '/content/edit/' . $model->get('id')); $page->set('deleteurl', '/content/delete/' . $model->get('id')); $page->set('component', 'content'); $page->save(); return true; }
<?php /** * Upgrade file to update the page insertables on 2.6.0 * * @package Core */ // Give me the page system! if(!class_exists('PageModel')) require_once(ROOT_PDIR . 'core/models/PageModel.class.php'); if(!class_exists('PageMetaModel')) require_once(ROOT_PDIR . 'core/models/PageMetaModel.class.php'); // If this site was not in multisite mode.... the insertables and metadata may not have matched up 1-to-1 with the page's site. // In 2.6.0, this relationship is a little more strictly enforced. if(!(Core::IsComponentAvailable('enterprise') && MultiSiteHelper::IsEnabled())){ // Get every page that currently exists $pages = PageModel::FindRaw(); foreach($pages as $page){ // Find the insertables that belong to this page and update their site id. $insertables = InsertableModel::Find(['baseurl = ' . $page['baseurl']]); foreach($insertables as $ins){ /** @var $ins InsertableModel */ $ins->set('site', $page['site']); $ins->save(); } } }
/** * @return null|int */ public function pageinsertables_update(){ $request = $this->getPageRequest(); $view = $this->getView(); $view->mode = View::MODE_AJAX; $view->contenttype = View::CTYPE_JSON; $view->record = false; // This is an ajax-only request. if(!$request->isAjax()){ return View::ERROR_BADREQUEST; } $formid = $request->getPost('___formid'); if(!$formid){ return View::ERROR_NOTFOUND; } // Lookup that form! if(\Core\Session::Get('FormData/' . $formid) === null){ return View::ERROR_NOTFOUND; } /** @var $form Form */ $form = unserialize(\Core\Session::Get('FormData/' . $formid)); if(!$form){ return View::ERROR_NOTFOUND; } // Run though each element submitted and try to validate it. if (strtoupper($form->get('method')) == 'POST') $src =& $_POST; else $src =& $_GET; $form->loadFrom($src, true); // Now that the form has been loaded with the data, reinitialize the page's insertable elements. foreach($form->getModels() as $prefix => $model){ if($model instanceof PageModel && $form->getElement($prefix . '[page_template]')){ $pagetemplate = $form->getElement($prefix . '[page_template]'); // Get all insertables currently present and remove them from the form. // (They will be added back shortly) foreach($form->getElements(true, false) as $el){ $name = $el->get('name'); if(strpos($name, $prefix . '[insertables]') === 0){ $form->removeElement($name); } } // Now that the previous insertables are removed, update the value on the model and add the new insertables. // This block of logic is required because the template systems look at the last template set in the database. // Since what's in the databse isn't what we want here, we need to spoof it so the correct template is // used for retrieving form elements. $model->set('page_template', $pagetemplate->get('value')); // Set the last_template so that the traditional queries to getTemplate work without reverting back to the default template. $t = $model->getBaseTemplateName(); // Allow the specific template to be overridden. if (($override = $model->get('page_template'))){ $t = substr($t, 0, -4) . '/' . $override; $model->set('last_template', $t); } else{ $model->set('last_template', null); } $tpl = Core\Templates\Template::Factory($model->getTemplateName()); if($tpl){ // My counter for which element was added last... I need this because I have "addElementAfter"... // so if I just kept adding the stack after a single element, they'd be in reverse order. // ie: stack: [a, b, c] -> {ref_el}, c, b, a $lastelementadded = $pagetemplate; $insertables = $tpl->getInsertables(); foreach($insertables as $key => $dat){ $type = $dat['type']; $dat['name'] = $prefix . '[insertables][' . $key . ']'; // This insertable may already have content from the database... if so I want to pull that! $i = InsertableModel::Construct($model->get('baseurl'), $key); if ($i->get('value') !== null){ $dat['value'] = $i->get('value'); } $dat['class'] = 'insertable'; $insertableelement = FormElement::Factory($type, $dat); $form->addElementAfter($insertableelement, $lastelementadded); $lastelementadded = $insertableelement; } } // Since there are new elements here, there may be old values that correspond to the new elements too. $form->loadFrom($src, true); // Don't forget to re-save these form updates back to the session! $form->persistent = true; $form->saveToSession(); $view->jsondata = array( 'status' => '1', 'message' => 'Switched templatename successfully', 'formid' => $form->get('uniqueid'), ); return null; } // if($model instanceof PageModel && $form->getElement($prefix . '[page_template]')) } // foreach($form->getModels() as $prefix => $model) // Ummmm..... $view->jsondata = array( 'status' => '0', 'message' => 'No page found :/', 'formid' => null, ); }
/** * Save the elements back to the database for the bound base_url. */ public function save() { // This is similar to the getModel method of the Form, but is done across multiple records instead of just one. $baseurl = $this->get('baseurl'); $els = $this->getElements(true, false); $prefix = $this->get('name') ? $this->get('name') : 'insertables'; foreach ($els as $e) { if (!preg_match('/^' . $prefix . '\[(.*?)\].*/', $e->get('name'), $matches)) continue; $i = new InsertableModel($baseurl, $matches[1]); $i->set('value', $e->get('value')); $i->save(); } }
//} $fac = new ModelFactory('BlogArticleModel'); foreach ($fac->get() as $model) { /** @var BlogArticleModel $model */ /** @var PageModel $page */ $page = $model->getLink('Page'); $page->setMeta('description', $model->getTeaser()); $page->setMeta('image', $model->get('image')); $page->set('published_status', $model->get('status')); $page->set('published', $model->get('published')); // Clone this to a content page. $content = new ContentModel(); $content->set('nickname', $model->get('title')); $content->set('created', $model->get('created')); $content->set('updated', $model->get('updated')); $content->save(); $page->set('component', 'content'); $page->set('baseurl', '/content/view/' . $content->get('id')); $page->set('editurl', '/content/edit/' . $content->get('id')); $page->set('deleteurl', '/content/delete/' . $content->get('id')); $page->set('page_template', 'blog-article.tpl'); $page->save(); $insertable = new InsertableModel(); $insertable->set('site', $page->get('site')); $insertable->set('baseurl', '/content/view/' . $content->get('id')); $insertable->set('name', 'body'); $insertable->set('value', $model->get('body')); $insertable->save(); $model->resetLink('Page'); $model->delete(); }
/** * Primary method for a block of user-customizable content inside a template. * * Insertables are the core method of injecting blocks of user-customizable content into a template. * * An insertable must be on a template that has a registered page URL, as the baseurl is what is tracked as one of the main primary keys. * The other PK is the insertable's name, which must be unique on that one template. * * #### Smarty Parameters * * * name * * The key name of this input value, must be present and unique on this template. * * assign * * Assign the value instead of outputting to the screen. * * title * * When editing the insertable, the title displayed along side the input field. * * type * * #### Example Usage * * <pre> * {insertable name="body" title="Body Content"} * <p> * This is some example content! * </p> * {/insertable} * </pre> * * <pre> * {insertable name="img1" title="Large Image" assign="img1"} * {img src="`$img1`" placeholder="generic" dimensions="800x400"} * {/insertable} * </pre> * * @param array $params Associative (and/or indexed) array of smarty parameters passed in from the template * @param string|null $content Null on opening pass, rendered source of the contents inside the block on closing pass * @param Smarty $smarty Parent Smarty template object * @param boolean $repeat True at the first call of the block-function (the opening tag) and * false on all subsequent calls to the block function (the block's closing tag). * Each time the function implementation returns with $repeat being TRUE, * the contents between {func}...{/func} are evaluated and the function implementation * is called again with the new block contents in the parameter $content. * * @return string */ function smarty_block_insertable($params, $content, $smarty, &$repeat){ $assign = (isset($params['assign']))? $params['assign'] : false; // This only needs to be called once. // If a value is being assigned, then it's on the first pass so the value will be assigned by the time the content is hit. if($assign){ if($repeat){ // Running the first time with an assign variable, OK! } else{ return $content; } } else{ // No assign requested, run on the second only. if($repeat){ return ''; } else{ // Continue! } } $page = PageRequest::GetSystemRequest()->getPageModel(); // I need to use the parent to lookup the current base url. $baseurl = PageRequest::GetSystemRequest()->getBaseURL(); if(!isset($params['name'])) return ''; $i = InsertableModel::Construct($page->get('site'), $baseurl, $params['name']); if($i->exists()){ $value = $i->get('value'); } else{ $value = $content; } if(isset($params['type']) && $params['type'] == 'markdown'){ // Convert this markdown code to HTML via the built-in Michielf library. $value = Core\MarkdownProcessor::defaultTransform($value); //$value = Michelf\MarkdownExtra::defaultTransform($value); } else{ // Coreify the string $value = \Core\parse_html($value); } if($assign){ $smarty->assign($assign, $value); } else{ return $value; } }