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($other = false) { if (!$this->dataObj->isDefaultLanguage()) { Qs_Http::redirect(Qs_Request::getFinalUrl()); } $defaults = array('meta' => array()); if (isset($_REQUEST['idParent'])) { $defaults['meta']['idParent'] = intval($_REQUEST['idParent']); } $defaults['meta']['isOther'] = ((!$other) ? 'n' : 'y'); $id = $this->dataObj->insertDefaultPage($defaults); Qs_Http::redirect($this->url(array('action' => 'edit', 'id' => $id))); } protected function _doAdd() { $this->_doNew(true); } protected function _doEdit() { if (!$this->dataObj->draftExists()) { try { $this->dataObj->createDraft(); } 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 ($data['meta']['isOther'] == 'y') { $data['meta']['showInMenu'] = 'n'; } $form->setDefaults($data); $this->_renderMainForm($form); } protected function _getBaseForm() { $form = parent::_getBaseForm(); $form->setAjaxValidation(false); return $form; } protected function _getMetaSubForm() { $meta = new App_Cms_Form_SubForm_Meta(array( 'language' => $this->dataObj->getLanguage(), 'doc' => $this->_doc, 'viewController' => $this )); if ($meta->idParent) { //$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); } } 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'); if (in_array($this->dataObj->getData('meta[isOther]', true), array('y', null))) { $form->meta->removeElement('showInMenu'); $form->meta->addElement('hidden', 'showInMenu', array('value' => 'n')); } else { if (!$this->doc->getAuth()->getSuMode() && in_array($this->dataObj->getData('meta[id]', true), array(27, 22, 23, 24, 25)) ) { $form->meta->removeElement('showInMenu'); $form->meta->addElement( 'hidden', 'showInMenu', array('value' => $this->dataObj->getData('meta[isOther]', true)) ); } } 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; } $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(''), array(''), $content); return $content; } protected function _renderMainForm($form) { $options = array('id_form' => $form->getId()); $itemGroups = $this->dataObj->getItemGroupNames(); foreach ($itemGroups as $groupName => $groupTitle) { if ($itemSubGroup = $form->getSubForm($groupName)) { // init block options (view forms) foreach ($itemSubGroup as $index => $itemForm) { if (($configForm = $itemForm->getSubForm('config'))) { $configVeiwForm = clone $configForm; $configVeiwForm->getDecorator('DtDdWrapper')->setDdAttrib('class', 'noMargin item_config_view'); $item = $this->dataObj->getDraftData("{$groupName}[{$index}]"); $itemObj = $this->_getItemView($item); $html = $itemObj->renderConfigForm($configVeiwForm); $itemForm->view->setValue($html); } } // end of init block options (view forms) // replace form tag to div tag in all html blocks foreach ($itemSubGroup->getSubforms() as $itemSubForm) { if (! ($itemSubForm instanceof App_Cms_Form_SubForm_ItemHtmlBlock)) { continue; } $content = $this->_getFilteredHtmlBlockViewContent($itemSubForm->data->content->getValue()); $itemSubForm->data->content->setValue($content); } // end replace form tag to div tag in all html blocks $order = $itemSubGroup->getElementsOrder(); $order = array_keys($order); foreach ($order as $k => &$v) { $v = intval(str_replace('i', '', $v)); } $options['order_' . $groupName] = $order; } } $options['meta'] = array(); $options['metaMode'] = $form->meta->getMode(); $options['items'] = $this->_getItems($form); $options['config'] = $this->getConfig(); $options['su'] = $this->doc->getAuth()->getSuMode(); $options['languages'] = $this->dataObj->getLanguageNames(); $this->doc->addInitFunction('initCmsForm', array($options)); $this->doc->addScript('js/jquery.scrollTo.js'); $this->doc->addScript('js/lib/form.js'); $this->doc->addScript('js/lib/cms.js'); $this->doc->addScript('js/ckeditor/ckeditor.js'); $this->doc->addScript('js/jquery-ui.js'); $this->doc->addStylesheet('css/jquery-ui.css'); parent::_renderMainForm($form); } protected function _getItemActions($item, $form) { $actions = array(); if (($form->{$item['groupName']} && $form->{$item['groupName']}->{'i' . $item['id']} && $form->{$item['groupName']}->{'i' . $item['id']}->config) || $this->doc->getAuth()->getSuMode() || $item['type'] == 'HtmlBlock_' ) { $actions[] = 'edit'; $actions[] = 'save'; $actions[] = 'cancel'; } $actions[] = 'moveUp'; $actions[] = 'moveDown'; if ($this->doc->getAuth()->getSuMode() || ($this->getConfig('item[allowDelete]') && $item['allowDelete'] == 'y') ) { $actions[] = 'delete'; } return $actions; } protected function _getItems($form) { if (empty($this->_items)) { $this->_items = array(); $_items = $this->dataObj->getItemsList($this->dataObj->getPrimaryKey(), true); $_item = array_fill_keys(array('id', 'type', 'groupName', 'allowDelete'), ''); foreach ($_items as $data) { $item = array_intersect_key($data, $_item); if (!$form->{$item['groupName']} || !$form->{$item['groupName']}->{'i' . $item['id']}) { continue; } $item['actions'] = $this->_getItemActions($data, $form); $item['mode'] = $form->{$item['groupName']}->{'i' . $item['id']}->getMode(); $this->_items[] = $item; } } return $this->_items; } protected function _doUpdateMetaAjax() { $meta = $this->_getMetaSubForm(); $result = array(); if ($meta->validate()) { $data = $meta->getValues(true); $this->dataObj->updateDraftMeta($data); } else { $result['errors'] = $meta->getMessages(null, true); } $this->_displayJson($result); } protected function _doUpdateItemAjax() { $item = Qs_Request::getPost(); unset($item['action']); $item['id'] = $item['idItem']; $itemSubForm = $this->_getItemSubForm($item); $result = array('isValid' => true); if ($configForm = $itemSubForm->getSubForm('config')) { $configVeiwForm = clone $configForm; $configVeiwForm->getDecorator('DtDdWrapper')->setDdAttrib('class', 'noMargin item_config_view'); $result = $this->_getValidateResult($configVeiwForm); if (!$result['isValid']) { $this->_displayJson($result); } else { $result['viewHtml'] = $this->_getItemView($item)->renderConfigForm($configVeiwForm); } } if ($itemSubForm instanceof App_Cms_Form_SubForm_ItemHtmlBlock) { $result['contentViewHtml'] = $this->_getFilteredHtmlBlockViewContent(Qs_Array::get($item, 'data[content]')); } // TODO ������� ������� ����� ��� HTML Block'a $this->dataObj->updateDraftItem($item['idItem'], $item['type'], $item); $this->_displayJson($result); } protected function _doLoadDialogItemTypeAjax() { $item = array(); $item['typeOptions'] = $this->dataObj->getDItemType4Select(); $item['groupNameOptions'] = $this->dataObj->getItemGroupNames(); $this->_doc->assign('item', new Qs_Doc_Item($item)); $this->_doc->setLayoutTemplate($this->getTemplate('Dialog/item-type.tpl')); echo $this->_doc->fetch(); exit; } protected function _doAddItemAjax() { $id = (int)Qs_Request::getPostValue('id'); $groupName = Qs_Request::getPostValue('groupName'); $renderGroup = (bool)Qs_Request::getPostValue('renderGroup'); $type = Qs_Request::getPostValue('type'); $itemData = array('idPage' => $id, 'type' => $type); if (!($itemData['idGroup'] = $this->dataObj->getGroupIdByName($groupName))) { $itemData['idGroup'] = 1; } $idItem = $this->dataObj->insertDraftItem($itemData); $data = $this->dataObj->getDraftItem($idItem); $this->dataObj->initDraftData(); $form = $this->_getEditForm(); $result = array('idItem' => $idItem); $result['actions'] = $this->_getItemActions($data, $form); if (in_array('edit', $result['actions'])) { $result['mode'] = 'edit'; } $this->doc->clearScripts(); $this->doc->clearStylesheets(); $this->doc->clearInlineScripts(); $this->doc->clearInitFunctions(); if ($renderGroup) { if ($subFormItemsGroup = $form->getSubForm($groupName)) { $subFormItemsGroup->setView(Qs_View::getInstance()); $result['html'] = $subFormItemsGroup->render(); } } else { //$subFormItem = $this->_getItemSubForm(array('type' => $type, 'groupName' => $groupName, 'id' => $idItem)); $subFormItem = $form->$groupName->{"i{$idItem}"}; $subFormItem->setDefaults($data); $subFormItem->setAttrib('id', 'i' . $idItem); $subFormItem->setElementsBelongTo($groupName . '[i' . $idItem . ']'); $subFormItem->setView(Qs_View::getInstance()); $result['html'] = $subFormItem->render(); } $result['javascript'] = $this->_getItemJavaScript(); $this->_displayJson($result); } protected function _getItemJavaScript() { $script = ''; $head = $this->doc->getTemplateVariable('head'); foreach($head['link'] as $file) { $script .= "\$.getCSS('{$file['href']}');\n"; } if (!empty($head['script'])) { $script .= "\$.ajaxSetup({async: false});\n"; foreach ($head['script'] as $file) { $script .= "\$.getScript('{$file['src']}');\n"; } $script .= "\$.ajaxSetup({async: true});\n"; } foreach ($head['inlineScript'] as $inlineScript) { $script .= "{$inlineScript}\n"; } foreach ($head['initFunctions'] as $function) { $script .= "{$function['name']}.apply(this, {$function['encodedParams']});\n"; } return $script; } protected function _doDeleteItemAjax() { $idItem = (int)Qs_Request::getPostValue('idItem'); $this->dataObj->deleteDraftItem($idItem); die('{}'); } protected function _doReorderItemsAjax() { $id = (int)Qs_Request::getPostValue('id'); $groupName = Qs_Request::getPostValue('groupName'); $order = Qs_Request::getPostValue('order'); $this->dataObj->reorderDraftItemsGroup($order); asort($order); $result = array( 'groupName' => $groupName, 'order' => array_keys($order) ); $this->_displayJson($result); } public function getCancelMessage() { return $this->_cancelMessage; } public function setCancelMessage($message) { $this->_cancelMessage = $message; } protected function _doPublish() { $this->dataObj->setPrimaryKey(Qs_Request::getRequestValue('meta[id]')); $this->dataObj->initDraftData(); $data = $this->dataObj->getData(); if (empty($data)) { $this->_setBackError('The page draft you edited has been already published. ' . 'You changes are not applied. ' . 'Try again.'); $this->_doBack(); } $this->dataObj->idParent = (int) $data['meta']['idParent']; $form = $this->_getEditForm(); if ($form->validate($data)) { $this->dataObj->publish(); $this->dataObj->deleteDraft(); $this->_doBack(); } else { $messages = $form->getMessages(); foreach ($messages as $groupName => $groupItems) { if ($groupName == 'meta') { $form->meta->setMode('edit'); continue; } if (!$form->{$groupName}) { continue; } $items = array_keys($groupItems); foreach ($items as $item) { if (!$form->{$groupName}->{$item}) { continue; } $form->{$groupName}->{$item}->setMode('edit'); } } $this->_renderMainForm($form); } } protected function _doCancel() { $this->dataObj->deleteDraft(); $meta = $this->dataObj->getMeta($this->dataObj->getPrimaryKey()); if (null == $meta['alias']) { $this->dataObj->delete(); } $this->_doBack(); } protected function _doDelete() { $this->dataObj->deleteDraft(); $this->dataObj->delete(); $this->_setBackMessage($this->dataObj->itemName.' has been deleted'); $this->_doBack(); } protected function _doShowSiteMap() { $this->_addLinksItem(); $item = array(); $item['siteMap'] = $this->dataObj->getSiteMap(); App_Sitemap_Virtual::prepare($item['siteMap']); $item['siteMapPagesCount'] = $this->dataObj->getSiteMapPageCount($item['siteMap']); $item['tpl'] = $this->getTemplate('sitemap.tpl'); $this->_addItem($item); } protected function _getReorderForm() { $form = parent::_getReorderForm(); $form->addElement('hidden', 'idParent', array('value' => $this->dataObj->idParent)); return $form; } protected function _getDefaultLinks() { $links = array( 'new' => 'Add top-level ' . $this->dataObj->itemName, 'reorder' => 'Reorder top-level ' . $this->dataObj->itemsName ); if (!$this->doc->getAuth()->getSuMode()) { if (false !== ($key = array_search('new', $this->_actions))) { unset($links['new']); unset($links['reorder']); } } return $links; } protected function _doCheckContentTranslation() { $this->_addLinks(array('back' => 'Show Site Map')); $item = array( 'links' => $this->_getLinks(), 'siteMap' => $this->dataObj->getNotTranslatedPages(), 'languages' => Qs_Array::exclude(Qs_Db_Language::getList(), DEFAULT_LANGUAGE), 'tpl' => $this->getTemplate('ContentTranslation/sitemap.tpl') ); $item['siteMapPagesCount'] = $this->dataObj->getSiteMapPageCount($item['siteMap']); $this->_addItem($item); } protected function _getData($field = false, $default = null, $method = 'POST') { if ($_SERVER['REQUEST_METHOD'] == strtoupper($method)) { if (false === $field) { return $_POST; } return Qs_Request::getPostValue($field, $default); } $key = $this->dataObj->getPrimaryKey(); if (empty($key)) { return $default; } return $this->dataObj->getDraftData($field); } protected function _doGetBodyTemplatesAjax() { $handler = Qs_Request::getRequestValue('handler'); $options = $this->getBodyTemplate4Select($handler); $this->_displayJson(array('options' => $options)); } protected function _doGetHtmlBlockContentAjax() { $idItem = Qs_Request::getRequestValue('idItem'); $item = $this->dataObj->getDraftItem($idItem); die(Qs_Array::get($item, 'data[content]')); } }