Ejemplo n.º 1
0
 /**
  * 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(0, null, $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
             $parentId = 0;
             $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'] = BackendPagesModel::getMaximumPageId() + 1;
             $page['user_id'] = BackendAuthentication::getUser()->getUserId();
             $page['parent_id'] = $parentId;
             $page['template_id'] = (int) $this->frm->getField('template_id')->getValue();
             $page['meta_id'] = (int) $this->meta->save();
             $page['language'] = BackendLanguage::getWorkingLanguage();
             $page['type'] = 'root';
             $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();
             $page['created_on'] = BackendModel::getUTCDate();
             $page['edited_on'] = BackendModel::getUTCDate();
             $page['allow_move'] = 'Y';
             $page['allow_children'] = 'Y';
             $page['allow_edit'] = 'Y';
             $page['allow_delete'] = 'Y';
             $page['sequence'] = BackendPagesModel::getMaximumSequence($parentId) + 1;
             $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::insert($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_add', $page);
             // save tags
             BackendTagsModel::saveTags($page['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule());
             // build the 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=added&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['revision_id'] . '&draft=' . $page['revision_id']);
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * 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'));
         }
         // set callback for generating an unique URL
         $this->meta->setURLCallback('BackendPagesModel', 'getURL', array(0, null, $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
             $parentId = 0;
             $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'] = BackendPagesModel::getMaximumPageId() + 1;
             $page['user_id'] = BackendAuthentication::getUser()->getUserId();
             $page['parent_id'] = $parentId;
             $page['template_id'] = (int) $this->frm->getField('template_id')->getValue();
             $page['meta_id'] = (int) $this->meta->save();
             $page['language'] = BackendLanguage::getWorkingLanguage();
             $page['type'] = 'root';
             $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();
             $page['created_on'] = BackendModel::getUTCDate();
             $page['edited_on'] = BackendModel::getUTCDate();
             $page['allow_move'] = 'Y';
             $page['allow_children'] = 'Y';
             $page['allow_edit'] = 'Y';
             $page['allow_delete'] = 'Y';
             $page['sequence'] = BackendPagesModel::getMaximumSequence($parentId) + 1;
             $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::insert($page);
             // init var
             $hasBlock = false;
             // build blocks
             $blocks = array();
             // loop blocks in template
             for ($i = 0; $i < $this->templates[$page['template_id']]['num_blocks']; $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 = $this->frm->getField('block_html_' . $i)->getValue();
                 } else {
                     // type of block
                     if (isset($this->extras[$extraId]['type']) && $this->extras[$extraId]['type'] == 'block') {
                         // set error
                         if ($hasBlock) {
                             $this->frm->getField('block_extra_id_' . $i)->addError('Can\'t add 2 blocks');
                         }
                         // reset var
                         $hasBlock = true;
                     }
                 }
                 // build block
                 $block = array();
                 $block['id'] = BackendPagesModel::getMaximumBlockId() + ($i + 1);
                 $block['revision_id'] = $page['revision_id'];
                 $block['extra_id'] = $extraId;
                 $block['html'] = $html;
                 $block['status'] = 'active';
                 $block['created_on'] = BackendModel::getUTCDate();
                 $block['edited_on'] = $block['created_on'];
                 // add block
                 $blocks[] = $block;
             }
             // insert the blocks
             BackendPagesModel::insertBlocks($blocks, $hasBlock);
             // trigger an event
             BackendModel::triggerEvent($this->getModule(), 'after_add', $page);
             // save tags
             BackendTagsModel::saveTags($page['id'], $this->frm->getField('tags')->getValue(), $this->URL->getModule());
             // build the cache
             BackendPagesModel::buildCache(BL::getWorkingLanguage());
             // active
             if ($page['status'] == 'active') {
                 // add search index
                 if (is_callable(array('BackendSearchModel', 'addIndex'))) {
                     // init var
                     $text = '';
                     // build search-text
                     foreach ($blocks as $block) {
                         $text .= ' ' . $block['html'];
                     }
                     // add
                     BackendSearchModel::addIndex($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=added&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['revision_id'] . '&draft=' . $page['revision_id']);
             }
         }
     }
 }