getRestAlias())) { $language = Qs_Constant::get('CURRENT_LANGUAGE'); } $this->dataObj->setLanguage($language); $this->dataObj->idParent = intval(Qs_Request::getRequestValue('idParent')); if (!$this->doc->getAuth()->getSuMode()) { $this->dataObj->addFilter(array('system' => 'n')); } } public function getConfig($field = false) { if (null === $this->_config) { $this->_config = Zend_Registry::get('config')->app->cms->toArray(); if ($this->doc->getAuth()->getSuMode()) { $this->_config['mode'] = 'advanced'; } $modeConfig = $this->_config['modes'][$this->_config['mode']]; unset($this->_config['modes']); $this->_config = array_merge($this->_config, $modeConfig); } return Qs_Array::get($this->_config, $field); } protected function _doNew() { if (!$this->dataObj->isDefaultLanguage()) { Qs_Http::redirect(Qs_Request::getFinalUrl()); } $defaults = array('meta' => array(), 'options' => array()); if (isset($_REQUEST['idParent'])) { $defaults['meta']['idParent'] = intval($_REQUEST['idParent']); } if (true === $this->getConfig('additionalPages') && !$this->doc->getAuth()->getSuMode()) { if (!isset($_REQUEST['idParent']) || 0 == $_REQUEST['idParent']) { $defaults['options']['isAdditional'] = array( 'name' => 'isAdditional', 'show' => 'n', 'value' => 'y', ); $defaults['options']['showInMenu'] = array( 'name' => 'showInMenu', 'show' => 'n', 'value' => 'n', ); } else { $options = $this->dataObj->getMetaOptions(intval($_REQUEST['idParent']), false, true); if (is_array($options['isAdditional'])) { $defaults['options']['isAdditional'] = $options['isAdditional']; if ('y' == $options['isAdditional']['value']) { $defaults['options']['showInMenu'] = array( 'name' => 'showInMenu', 'show' => 'n', 'value' => 'n', ); } } } } $id = $this->dataObj->insertDefaultPage($defaults); Qs_Http::redirect($this->url(array('action' => 'edit', 'id' => $id))); } protected function _doEdit() { if (!$this->dataObj->draftExists()) { try { $this->dataObj->createDraft(); } catch (App_Cms_Exception $e) { $this->_doc->displayError($e->getMessage()); return; } } if (!$this->dataObj->draftPageOptionExists()) { try { $this->dataObj->createPageOptionDraft(); } catch (App_Cms_Exception $e) { $this->_doc->displayError($e->getMessage()); return; } } $data = $this->dataObj->getDraftFromDb($this->dataObj->getPrimaryKey()); if (empty($data)) { $this->_setBackError('Invalid Page ID'); $this->_doBack(); } $form = $this->_getEditForm(); if (!empty($data['meta']['alias']) && !empty($data['meta']['title'])) { $form->meta->setMode('view'); } if(isset($data['meta']['image'])) { $data['image'] = $data['meta']['image']; } $form->setDefaults($data); $this->_renderMainForm($form); } protected function _getBaseForm() { $form = parent::_getBaseForm(); $form->setAjaxValidation(false); return $form; } protected function _getMetaSubForm() { $idPage = intval(Qs_Request::getRequestValue('id')); $meta = new App_Cms_Form_SubForm_Meta(array( 'language' => $this->dataObj->getLanguage(), 'doc' => $this->_doc, 'viewController' => $this, 'metaOptions' => $this->dataObj->getDraftMetaOptions($idPage), 'isCustomOptions' => $this->dataObj->isCustomOptions(), 'isRedirectFields' => $this->dataObj->isRedirectFields(), )); if ($meta->idParent) { if ($meta->idParent instanceOf Zend_Form_Element_Select) { $meta->idParent->setMultiOptions(array(0 => 'Root') + (array)$this->dataObj->getParent4Select()); } $aliasUniqueValidator = new Qs_Validate_Unique($this->dataObj->table, 'alias', $this->dataObj->getPrimaryKey()); $aliasUniqueValidator->setWhere('idParent = ' . (int) $this->dataObj->idParent); $aliasUniqueValidator->setMessage('Alias must be unique', 'notUnique'); $meta->alias->addValidator($aliasUniqueValidator, true); $meta->addElementPrefixPath('App_Cms_Validate', 'App/Cms/Validate', 'validate'); if (Qs_Request::isXmlHttpRequest()) { $idParent = $this->_getData('idParent'); } else { $idParent = $this->_getData('meta[idParent]'); } $parentAlias = Qs_SiteMap::findFirst(array('id' => $idParent), null, null, 'fullAlias'); $meta->alias->addValidator('XmlAliasUnique', true, array('parentAlias' => $parentAlias)); } if ($meta->handler && $meta->handler instanceof Zend_Form_Element_Select) { $meta->handler->setMultiOptions($this->dataObj->getDPageHandler4Select()); } if ($meta->bodyTemplate) { if (Qs_Request::isXmlHttpRequest()) { $handler = $this->_getData('handler'); } else { $handler = $this->_getData('meta[handler]'); } $meta->bodyTemplate->setMultiOptions($this->getBodyTemplate4Select($handler)); } return $meta; } public function getBodyTemplate4Select($handler = 'site') { $handlerClass = 'App_Doc_' . ucfirst($handler); $handlerFile = BASE_PATH . '/App/Doc/' . ucfirst($handler) . '.php'; if (!file_exists($handlerFile)) { throw new App_Cms_Exception('Handler file not found: ' . $handlerFile); } $handlerObj = new $handlerClass(array('authentication' => false)); $paths = $handlerObj->getTemplatePath(); unset($handlerObj); $files = array(); foreach ($paths as $path) { $dir = BASE_PATH . '/tpl/' . $path . '/Body'; if (file_exists($dir) && is_dir($dir)) { $pattern = $dir . '/*.tpl'; $_files = glob($pattern); $files = array_merge($files, $_files); } $dir = BASE_PATH . '/tpl/_sys/' . $path . '/Body'; if (file_exists($dir) && is_dir($dir)) { $pattern = $dir . '/*.tpl'; $_files = glob($pattern); $files = array_merge($files, $_files); } } $templates = array(); foreach ($files as $file) { $templates[basename($file)] = basename($file); } return $templates; } protected function _bindItems($form) { $data = $this->dataObj->getDraftData(); $itemGroupNames = $this->dataObj->getItemGroupNames(); foreach ($itemGroupNames as $groupName => $groupTitle) { if (!isset($data[$groupName])) { continue; } $pageItems = $data[$groupName]; $this->_filterItems($pageItems); if (empty($pageItems)) { continue; } $decoratorFieldset = array('decorator' => 'Fieldset', 'options' => array('class' => 'cms_item_group')); $options = array('decorators' => array('FormElements', $decoratorFieldset)); $items = new Qs_Form_SubForm($options); $items->setLegend($groupTitle); foreach ($pageItems as $index => $item) { $itemSubForm = $this->_getItemSubForm($item); $items->addSubForm($itemSubForm, $index); } $form->addSubForm($items, $groupName); } $addImage = true; $pageData = $this->dataObj->getData(false, true); if (!array_key_exists('ITEMS', $pageData) || !is_array($pageData['ITEMS'])) { $pageData['ITEMS'] = array(); } foreach ($pageData['ITEMS'] as $itemData) { if ($itemData['type'] == 'Slide_') { $addImage = false; break; } } if ($addImage) { $form->addElement( 'extendedImage', 'image', array( 'label' => 'Top picture', 'required' => false, 'description' => 'Preferred Image Size is ' . self::TOP_IMAGE_WIDTH . 'x' . self::TOP_IMAGE_HEIGHT . 'px' ) ); } } protected function _filterItems(&$items) { $filter = $this->getConfig('item[filter]'); if (empty($filter)) { return false; } foreach ($items as $index => $data) { foreach ($filter as $field => $value) { if (!array_key_exists($field, $data)) { unset($items[$index]); continue; } if (is_array($value)) { if (!in_array($data[$field], $value)) { unset($items[$index]); continue; } } else { if ($data[$field] != $value) { unset($items[$index]); continue; } } } } } protected function _getItemView($item) { if (!array_key_exists($item['id'], $this->_itemView)) { $class = 'App_' . $item['type'] . 'View'; $file = BASE_PATH . '/' . str_replace('_', '/', $class) . '.php'; if (file_exists($file)) { $itemObj = new $class($item); $itemObj->setDoc($this->doc); $this->_itemView[$item['id']] = $itemObj; } else { $this->_itemView[$item['id']] = null; } } return $this->_itemView[$item['id']]; } protected function _getItemSubForm($item) { $type = rtrim($item['type'], '_'); $file = Qs_Constant::get('BASE_PATH') . '/App/Cms/Form/SubForm/Item' . $type . '.php'; $class = 'App_Cms_Form_SubForm_Item' . $type; $options = array( 'groupName' => $item['groupName'], 'itemId' => $item['id'], 'type' => $item['type'], 'legend' => $this->dataObj->getItemTypeTitle($item['type']) ); if (file_exists($file)) { $itemSubForm = new $class($options); } else { $itemSubForm = new App_Cms_Form_SubForm_Item($options); } $itemSubForm->setMode('view'); $itemObj = $this->_getItemView($item); if (null !== $itemObj && $itemObj->hasConfigForm()) { $configForm = new Qs_Form_SubForm(); $configForm->getDecorator('DtDdWrapper')->setDdAttrib('class', 'noMargin item_config'); $configForm->setLegend('Config'); $configForm->setAttrib('id', $item['groupName'] . '-i' . $item['id'] . '-config'); $itemObj->fillConfigForm($configForm); $itemSubForm->addElement('html', 'view'); $itemSubForm->addSubForm($configForm, 'config'); } return $itemSubForm; } protected function _bindFormFields($form) { $languages = $this->dataObj->getLanguages(); if (count($languages) > 1) { $decorators = array('ViewHelper'); $elements = array(); foreach ($languages as $language) { $elements[] = $language['name']; $options = array( 'value' => $language['shortTitle'], 'decorators' => $decorators ); if ($language['name'] == $this->dataObj->getLanguage()) { $options['class'] = 'current'; $options['disabled'] = 'disabled'; } $options['id'] = 'lng-' . $language['name']; $form->addElement('button', $language['name'], $options); } $form->addDisplayGroup($elements, 'languageGroup', array('disableLoadDefaultDecorators' => true)); $form->languageGroup->addDecorator('FormElements') ->addDecorator('Fieldset'); } $meta = $this->_getMetaSubForm(); $form->addSubForm($meta, 'meta'); $this->_bindItems($form); } protected function _getEditForm() { $form = $this->_getBaseForm(); $this->_bindFormFields($form); $this->_bindFormButtons($form); $form->addElement('hidden', 'action', array('value' => 'publish', 'decorators' => array('ViewHelper'))); $form->getDecorator('HtmlTag')->setOption('tag', 'div'); return $form; } protected function _bindFormButtons($form) { $url = $this->url(array('action' => 'cancel', 'id' => $this->dataObj->getPrimaryKey())); $form->setCancelUrl($url); $form->addElement('submit', 'btnSubmit', array( 'label' => 'Publish', 'attribs' => array('class' => 'btn'), 'decorators' => array('ViewHelper') ) ); $form->btnSubmit->getDecorator('ViewHelper')->setAdditionalHtmlAfterElement(' '); $form->addElement('button', 'btnCancel', array( 'label' => 'Cancel All Changes', 'attribs' => array( 'class' => 'btn', 'onclick' => "if (confirm('{$this->getCancelMessage()}')) " . "document.location = '" . htmlspecialchars($form->getCancelUrl()) . "';" ), 'decorators' => array('ViewHelper') ) ); $form->addDisplayGroup(array('btnSubmit', 'btnCancel'), 'submitGroup', array( 'decorators' => array( 'FormElements', array('decorator' => 'HtmlTag'), 'Fieldset' ) ) ); } protected function _getFilteredHtmlBlockViewContent($content) { if (empty($content)) { return $content; } $content = mb_convert_encoding($content, 'HTML-ENTITIES', 'UTF-8'); $dom = new Zend_Dom_Query(); $dom->setDocumentHtml($content); $results = $dom->query('form'); foreach ($results as $tag) { foreach (array('action', 'autocomplete', 'enctype', 'method', 'name', 'target') as $attribute) { $tag->removeAttribute($attribute); } } $document = new DOMDocument(); $body = $results->getDocument()->documentElement->firstChild; for ($i = 0; $i < $body->childNodes->length; $i++) { $document->appendChild($document->importNode($body->childNodes->item($i), true)); } $content = $document->saveHtml(); $content = str_replace(array('