/** * Validate the form */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // get the status $status = SpoonFilter::getPostValue('status', array('active', 'draft'), 'active'); // validate redirect $redirectValue = $this->frm->getField('redirect')->getValue(); if ($redirectValue == 'internal') { $this->frm->getField('internal_redirect')->isFilled(BL::err('FieldIsRequired')); } if ($redirectValue == 'external') { $this->frm->getField('external_redirect')->isURL(BL::err('InvalidURL')); } // set callback for generating an unique URL $this->meta->setURLCallback('BackendPagesModel', 'getURL', array($this->record['id'], $this->record['parent_id'], $this->frm->getField('is_action')->getChecked())); // cleanup the submitted fields, ignore fields that were added by hackers $this->frm->cleanupFields(); // validate fields $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired')); // validate meta $this->meta->validate(); // no errors? if ($this->frm->isCorrect()) { // init var $data = null; // build data if ($this->frm->getField('is_action')->isChecked()) { $data['is_action'] = true; } if ($redirectValue == 'internal') { $data['internal_redirect'] = array('page_id' => $this->frm->getField('internal_redirect')->getValue(), 'code' => '301'); } if ($redirectValue == 'external') { $data['external_redirect'] = array('url' => $this->frm->getField('external_redirect')->getValue(), 'code' => '301'); } // build page record $page['id'] = $this->record['id']; $page['user_id'] = BackendAuthentication::getUser()->getUserId(); $page['parent_id'] = $this->record['parent_id']; $page['template_id'] = (int) $this->frm->getField('template_id')->getValue(); $page['meta_id'] = (int) $this->meta->save(); $page['language'] = BackendLanguage::getWorkingLanguage(); $page['type'] = $this->record['type']; $page['title'] = $this->frm->getField('title')->getValue(); $page['navigation_title'] = $this->frm->getField('navigation_title')->getValue() != '' ? $this->frm->getField('navigation_title')->getValue() : $this->frm->getField('title')->getValue(); $page['navigation_title_overwrite'] = $this->frm->getField('navigation_title_overwrite')->isChecked() ? 'Y' : 'N'; $page['hidden'] = $this->frm->getField('hidden')->getValue(); $page['status'] = $status; $page['publish_on'] = BackendModel::getUTCDate(null, $this->record['publish_on']); $page['created_on'] = BackendModel::getUTCDate(null, $this->record['created_on']); $page['edited_on'] = BackendModel::getUTCDate(); $page['allow_move'] = $this->record['allow_move']; $page['allow_children'] = $this->record['allow_children']; $page['allow_edit'] = $this->record['allow_edit']; $page['allow_delete'] = $this->record['allow_delete']; $page['sequence'] = $this->record['sequence']; $page['data'] = $data !== null ? serialize($data) : null; if ($this->isGod) { $page['allow_move'] = in_array('move', (array) $this->frm->getField('allow')->getValue()) ? 'Y' : 'N'; $page['allow_children'] = in_array('children', (array) $this->frm->getField('allow')->getValue()) ? 'Y' : 'N'; $page['allow_edit'] = in_array('edit', (array) $this->frm->getField('allow')->getValue()) ? 'Y' : 'N'; $page['allow_delete'] = in_array('delete', (array) $this->frm->getField('allow')->getValue()) ? 'Y' : 'N'; } // set navigation title if ($page['navigation_title'] == '') { $page['navigation_title'] = $page['title']; } // insert page, store the id, we need it when building the blocks $page['revision_id'] = BackendPagesModel::update($page); // loop blocks foreach ($this->blocksContent as $i => $block) { // add page revision id to blocks $this->blocksContent[$i]['revision_id'] = $page['revision_id']; // validate blocks, only save blocks for valid positions if (!in_array($block['position'], $this->templates[$this->frm->getField('template_id')->getValue()]['data']['names'])) { unset($this->blocksContent[$i]); } } // insert the blocks BackendPagesModel::insertBlocks($this->blocksContent); // trigger an event BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $page)); // save tags BackendTagsModel::saveTags($page['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule()); // build cache BackendPagesModel::buildCache(BL::getWorkingLanguage()); // active if ($page['status'] == 'active') { // init var $text = ''; // build search-text foreach ($this->blocksContent as $block) { $text .= ' ' . $block['html']; } // add to search index BackendSearchModel::saveIndex($this->getModule(), $page['id'], array('title' => $page['title'], 'text' => $text)); // everything is saved, so redirect to the overview $this->redirect(BackendModel::createURLForAction('edit') . '&id=' . $page['id'] . '&report=edited&var=' . urlencode($page['title']) . '&highlight=row-' . $page['id']); } elseif ($page['status'] == 'draft') { // everything is saved, so redirect to the edit action $this->redirect(BackendModel::createURLForAction('edit') . '&id=' . $page['id'] . '&report=saved-as-draft&var=' . urlencode($page['title']) . '&highlight=row-' . $page['id'] . '&draft=' . $page['revision_id']); } } } }
/** * Validate the form * * @return void */ private function validateForm() { // is the form submitted? if ($this->frm->isSubmitted()) { // get the status $status = SpoonFilter::getPostValue('status', array('active', 'draft'), 'active'); // validate redirect $redirectValue = $this->frm->getField('redirect')->getValue(); if ($redirectValue == 'internal') { $this->frm->getField('internal_redirect')->isFilled(BL::err('FieldIsRequired')); } if ($redirectValue == 'external') { $this->frm->getField('external_redirect')->isURL(BL::err('InvalidURL')); } // init var $templateId = (int) $this->frm->getField('template_id')->getValue(); // loop blocks in template for ($i = 0; $i < $this->templates[$templateId]['num_blocks']; $i++) { // get the extra id $extraId = (int) $this->frm->getField('block_extra_id_' . $i)->getValue(); // reset some stuff if ($extraId > 0) { // type of block if (isset($this->extras[$extraId]['type']) && $this->extras[$extraId]['type'] == 'block') { // home can't have blocks if ($this->record['id'] == 1) { $this->frm->getField('block_html_' . $i)->addError(BL::err('HomeCantHaveBlocks')); $this->frm->addError(BL::err('HomeCantHaveBlocks')); } } } } // set callback for generating an unique URL $this->meta->setURLCallback('BackendPagesModel', 'getURL', array($this->record['id'], $this->record['parent_id'], $this->frm->getField('is_action')->getChecked())); // cleanup the submitted fields, ignore fields that were edited by hackers $this->frm->cleanupFields(); // validate fields $this->frm->getField('title')->isFilled(BL::err('TitleIsRequired')); // validate meta $this->meta->validate(); // no errors? if ($this->frm->isCorrect()) { // init var $data = null; // build data if ($this->frm->getField('is_action')->isChecked()) { $data['is_action'] = true; } if ($redirectValue == 'internal') { $data['internal_redirect'] = array('page_id' => $this->frm->getField('internal_redirect')->getValue(), 'code' => '301'); } if ($redirectValue == 'external') { $data['external_redirect'] = array('url' => $this->frm->getField('external_redirect')->getValue(), 'code' => '301'); } // build page record $page['id'] = $this->record['id']; $page['user_id'] = BackendAuthentication::getUser()->getUserId(); $page['parent_id'] = $this->record['parent_id']; $page['template_id'] = (int) $this->frm->getField('template_id')->getValue(); $page['meta_id'] = (int) $this->meta->save(); $page['language'] = BackendLanguage::getWorkingLanguage(); $page['type'] = $this->record['type']; $page['title'] = $this->frm->getField('title')->getValue(); $page['navigation_title'] = $this->frm->getField('navigation_title')->getValue() != '' ? $this->frm->getField('navigation_title')->getValue() : $this->frm->getField('title')->getValue(); $page['navigation_title_overwrite'] = $this->frm->getField('navigation_title_overwrite')->isChecked() ? 'Y' : 'N'; $page['hidden'] = $this->frm->getField('hidden')->getValue(); $page['status'] = $status; $page['publish_on'] = BackendModel::getUTCDate(null, $this->record['publish_on']); $page['created_on'] = BackendModel::getUTCDate(null, $this->record['created_on']); $page['edited_on'] = BackendModel::getUTCDate(); $page['allow_move'] = $this->record['allow_move']; $page['allow_children'] = $this->record['allow_children']; $page['allow_edit'] = $this->record['allow_edit']; $page['allow_delete'] = $this->record['allow_delete']; $page['sequence'] = $this->record['sequence']; $page['data'] = $data !== null ? serialize($data) : null; // set navigation title if ($page['navigation_title'] == '') { $page['navigation_title'] = $page['title']; } // insert page, store the id, we need it when building the blocks $page['revision_id'] = BackendPagesModel::update($page); // init var $hasBlock = false; // build blocks $blocks = array(); // no blocks should go to waste; even if the new template has fewer blocks, retain existing content $maxNumBlocks = max(count($this->blocksContent), $this->templates[$page['template_id']]['num_blocks']); // loop blocks in template for ($i = 0; $i < $maxNumBlocks; $i++) { // check if this block has been submitted if (isset($_POST['block_extra_id_' . $i])) { // get the extra id $extraId = (int) $this->frm->getField('block_extra_id_' . $i)->getValue(); // reset some stuff if ($extraId <= 0) { $extraId = null; } // init var $html = null; // extra-type is HTML if ($extraId === null) { // reset vars $extraId = null; $html = (string) $this->frm->getField('block_html_' . $i)->getValue(); } else { // type of block if (isset($this->extras[$extraId]['type']) && $this->extras[$extraId]['type'] == 'block') { // home can't have blocks if ($this->record['id'] == 1) { throw new BackendException('Home can\'t have any blocks.'); } // set error if ($hasBlock) { throw new BackendException('Can\'t add 2 blocks'); } // reset var $hasBlock = true; } } // build block $block = array(); $block['id'] = isset($this->blocksContent[$i]['id']) ? $this->blocksContent[$i]['id'] : BackendPagesModel::getMaximumBlockId() + ($i + 1); $block['revision_id'] = $page['revision_id']; $block['extra_id'] = $extraId; $block['html'] = $html; $block['status'] = 'active'; $block['created_on'] = isset($this->blocksContent[$i]['created_on']) ? BackendModel::getUTCDate(null, $this->blocksContent[$i]['created_on']) : BackendModel::getUTCDate(); $block['edited_on'] = BackendModel::getUTCDate(); } else { $block = $this->blocksContent[$i]; $block['revision_id'] = $page['revision_id']; } // add block $blocks[] = $block; } // update the blocks BackendPagesModel::updateBlocks($blocks, $hasBlock); // trigger an event BackendModel::triggerEvent($this->getModule(), 'after_edit', array('item' => $page)); // save tags BackendTagsModel::saveTags($page['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule()); // build cache BackendPagesModel::buildCache(BL::getWorkingLanguage()); // active if ($page['status'] == 'active') { // edit search index if (is_callable(array('BackendSearchModel', 'editIndex'))) { // init var $text = ''; // build search-text foreach ($blocks as $block) { $text .= ' ' . $block['html']; } // add BackendSearchModel::editIndex($this->getModule(), $page['id'], array('title' => $page['title'], 'text' => $text)); } // build URL $redirectUrl = BackendModel::createURLForAction('edit') . '&id=' . $page['id'] . '&report=edited&var=' . urlencode($page['title']) . '&highlight=row-' . $page['id']; } elseif ($page['status'] == 'draft') { // everything is saved, so redirect to the edit action $redirectUrl = BackendModel::createURLForAction('edit') . '&id=' . $page['id'] . '&report=saved-as-draft&var=' . urlencode($page['title']) . '&highlight=row-' . $page['id'] . '&draft=' . $page['revision_id']; } // everything is saved, so redirect to the overview $this->redirect($redirectUrl); } } }
/** * Switch templates for all existing pages * * @param int $oldTemplateId The id of the new template to replace. * @param int $newTemplateId The id of the new template to use. * @param bool[optional] $overwrite Overwrite all pages with default blocks. */ public static function updatePagesTemplates($oldTemplateId, $newTemplateId, $overwrite = false) { $newTemplateId = (int) $newTemplateId; $oldTemplateId = (int) $oldTemplateId; $overwrite = (bool) $overwrite; // fetch new template data $newTemplate = BackendExtensionsModel::getTemplate($newTemplateId); $newTemplate['data'] = @unserialize($newTemplate['data']); // fetch all pages $pages = (array) BackendModel::getDB()->getRecords('SELECT * FROM pages WHERE template_id = ? AND status IN (?, ?)', array($oldTemplateId, 'active', 'draft')); // there is no active/draft page with the old template id if (empty($pages)) { return; } // loop pages foreach ($pages as $page) { // fetch blocks $blocksContent = BackendPagesModel::getBlocks($page['id'], $page['revision_id'], $page['language']); // unset revision id unset($page['revision_id']); // change template $page['template_id'] = $newTemplateId; // save new page revision $page['revision_id'] = BackendPagesModel::update($page); // overwrite all blocks with current defaults if ($overwrite) { // init var $blocksContent = array(); // fetch default blocks for this page $defaultBlocks = array(); if (isset($newTemplate['data']['default_extras_' . $page['language']])) { $defaultBlocks = $newTemplate['data']['default_extras_' . $page['language']]; } elseif (isset($newTemplate['data']['default_extras'])) { $defaultBlocks = $newTemplate['data']['default_extras']; } // loop positions foreach ($defaultBlocks as $position => $blocks) { // loop blocks foreach ($blocks as $extraId) { // build block $block = array(); $block['revision_id'] = $page['revision_id']; $block['position'] = $position; $block['extra_id'] = $extraId; $block['html'] = ''; $block['created_on'] = BackendModel::getUTCDate(); $block['edited_on'] = $block['created_on']; $block['visible'] = 'Y'; $block['sequence'] = count($defaultBlocks[$position]) - 1; // add to the list $blocksContent[] = $block; } } } else { // set new page revision id foreach ($blocksContent as &$block) { $block['revision_id'] = $page['revision_id']; } } // insert the blocks BackendPagesModel::insertBlocks($blocksContent); } }
/** * Switch templates for all existing pages * * @return void * @param int $oldTemplateId The id of the new template to replace. * @param int $newTemplateId The id of the new template to use. */ public static function updatePagesTemplates($oldTemplateId, $newTemplateId) { // fetch new template data $newTemplate = BackendPagesModel::getTemplate($newTemplateId); $newTemplate['data'] = @unserialize($newTemplate['data']); // fetch all pages $pages = BackendModel::getDB()->getRecords('SELECT * FROM pages WHERE template_id = ? AND status IN (?, ?)', array($oldTemplateId, 'active', 'draft')); // there is no active/draft page with the old template id if (empty($pages)) { return; } // loop pages foreach ($pages as $page) { // fetch blocks $blocksContent = BackendPagesModel::getBlocksRevision($page['id'], $page['revision_id'], $page['language']); // unset revision id unset($page['revision_id']); // change template $page['template_id'] = $newTemplateId; // save new page revision $page['revision_id'] = BackendPagesModel::update($page); // init blocks array $blocks = array(); // loop missing blocks for ($i = 0; $i < max(count($blocksContent), (int) $newTemplate['num_blocks']); $i++) { $block = array(); // block already exists if (isset($blocksContent[$i])) { $block = $blocksContent[$i]; $block['created_on'] = BackendModel::getUTCDate(null, $block['created_on']); $block['edited_on'] = BackendModel::getUTCDate(null, $block['edited_on']); $block['revision_id'] = $page['revision_id']; } else { $block['id'] = BackendPagesModel::getMaximumBlockId() + $i + 1; $block['revision_id'] = $page['revision_id']; $block['html'] = ''; $block['status'] = 'active'; $block['created_on'] = BackendModel::getUTCDate(); $block['edited_on'] = $block['created_on']; $block['html'] = ''; $block['extra_id'] = null; $block['has_extra'] = 'N'; } // verify that there is no existing content and that we actually have new default content if (!isset($blocksContent[$i]) || !$blocksContent[$i]['html'] && !$blocksContent[$i]['extra_id'] && $i < $newTemplate['num_blocks']) { // get default extras in this language if (isset($newTemplate['data']['default_extras_' . $page['language']])) { // check if a default extra has been defined if ($newTemplate['data']['default_extras_' . $page['language']][$i] != 'editor') { $page['has_extra'] = 'Y'; $block['extra_id'] = $newTemplate['data']['default_extras_' . $page['language']][$i]; } else { $block['extra_id'] = null; } } else { // check if a default extra has been defined if ($newTemplate['data']['default_extras'][$i] != 'editor') { $page['has_extra'] = 'Y'; $block['extra_id'] = $newTemplate['data']['default_extras'][$i]; } else { $block['extra_id'] = null; } } } // add block $blocks[] = $block; } // insert the blocks BackendPagesModel::updateBlocks($blocks, $page['has_extra'] == 'Y'); } }