/** * @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, ); }
/** * 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; } }