_initDataObj(); parent::init(); return $this; } protected function _initDataObj() { $this->_dataObj = new App_Cms_Obj(); $this->_dataObj->setSuMode(App_Admin_Auth::getInstance()->getSuMode()); $this->_dataObj->setLanguage($this->_language); return $this; } public function setLanguage($language) { $this->_language = $language; return $this; } public function getConfig($field = null, $default = null) { if (null === $this->_config) { $this->_config = Qs_Config::getByInstance($this); } if (null === $field) { return $this->_config; } return $this->_config->get($field, $default); } protected function _initElements() { $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']; $this->addElement('button', $language['name'], $options); } $this->addDisplayGroup($elements, 'languageGroup', array('disableLoadDefaultDecorators' => true)); $this->getDisplayGroup('languageGroup')->addDecorator('FormElements')->addDecorator('Fieldset'); } $meta = $this->_getMetaSubForm(); $this->addSubForm($meta, 'meta'); $this->_bindItems(); if ($this->getConfig('hasHeaderImage') && !in_array($this->_dataObj->getPrimaryKey(), App_Cms_Obj::getImageHeaderExcludePageIds()) ) { $resize = $this->getConfig('headerImageWidth') . 'x' . $this->getConfig('headerImageHeight') . Qs_ImageFs::getResizeMethodAlias($this->getConfig('headerImageResizeMethod')); $this->addElement( 'extendedImage', 'headerImage', array( 'label' => 'Page Header Image', 'resize' => $resize, 'required' => (bool) $this->getConfig('headerImageIsRequired'), ) ); } return $this; } protected function _bindItems() { $itemGroupNames = $this->_dataObj->getItemGroupNames(); foreach ($itemGroupNames as $groupName => $groupTitle) { if (null === ($pageItems = $this->_getData($groupName))) { continue; } $this->_filterItems($pageItems); if (empty($pageItems)) { continue; } $options = array( 'decorators' => array( 'FormElements', array('Fieldset', array('class' => 'cms_item_group')), array('HtmlTag', array('tag' => 'dd', 'id' => 'ITEMS-element')), array(array('label' => 'HtmlTag'), array('tag' => 'dt', 'id' => 'ITEMS-label', 'placement' => 'prepend')), ), 'hasButtons' => false ); $items = new Qs_Form_SubForm($options); $items->setLegend($groupTitle); foreach ($pageItems as $index => $item) { $itemSubForm = $this->getItemSubForm($item); $items->addSubForm($itemSubForm, $index); } $this->addSubForm($items, $groupName); } return $this; } public 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'); /** @var $itemObj Qs_ViewController */ $itemObj = $this->_dataObj->getItemView($item); if (null !== $itemObj && $itemObj->hasConfigForm()) { $configForm = $itemObj->getConfigForm(); $configForm->getDecorator('DtDdWrapper')->setDdAttrib('class', 'item_config'); $configForm->setAttrib('id', $item['groupName'] . '-i' . $item['id'] . '-config'); $itemSubForm->addElement('html', 'view'); $itemSubForm->addSubForm($configForm, 'config'); } return $itemSubForm; } protected function _filterItems(&$items) { $filter = $this->getConfig('item')->get('filter'); if (null === $filter) { $filter = array(); } else if ($filter instanceof Zend_Config) { /** @var $filter Zend_Config */ $filter = $filter->toArray(); } foreach ($items as $index => $data) { // відфільтрував порожні ітеми (сабмітяться при публікації сторіки) if (!array_key_exists('type', $data)) { unset($items[$index]); continue; } 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; } } } } return $this; } protected function _initButtons() { $url = Qs_Request::url(array('action' => 'removeDraft', 'id' => $this->_dataObj->getPrimaryKey())); $this->setCancelUrl($url); $this->addElement( 'submit', 'btnSubmit', array('label' => 'Publish', 'attribs' => array('class' => 'btn btn-primary')) ); $this->addElement( 'submit', 'btnCancel', array( 'label' => 'Cancel All Changes', 'attribs' => array( 'class' => 'btn', 'helper' => 'formInputButton', 'onclick' => "if (confirm('{$this->getConfig('cancelMessage')}')) " . "document.location = '" . htmlspecialchars($this->getCancelUrl()) . "';" ), ) ); $this->addDisplayGroup(array('btnSubmit', 'btnCancel'), 'submitGroup'); return $this; } protected function _getMetaSubForm() { $idPage = (int) Qs_Request::getRequestValue('id', $this->_dataObj->getPrimaryKey()); $meta = new App_Cms_Form_SubForm_Meta(array( 'language' => $this->_dataObj->getLanguage(), 'metaOptions' => $this->_dataObj->getDraftMetaOptions($idPage), 'isRedirectFields' => $this->_dataObj->isRedirectFields(), )); /** @var $handler Zend_Form_Element_Select */ $handler = $meta->getElement('handler'); if ($handler && $handler instanceof Zend_Form_Element_Select) { $handler->setMultiOptions($this->_dataObj->getDPageHandler4Select()); } /** @var $bodyTemplate Zend_Form_Element_Select */ $bodyTemplate = $meta->getElement('bodyTemplate'); if ($bodyTemplate) { $handler = $this->_getData('handler'); if (empty($handler)) { $handler = $this->_getData('meta[handler]'); } if ($handler && $bodyTemplate instanceof Zend_Form_Element_Select) { // Suppress Fatal Error on PageItemUpdate Ajax action $bodyTemplate->setMultiOptions($this->_dataObj->getBodyTemplate4Select($handler)); } } if (null !== ($element = $meta->getElement('redirectPageId'))) { /** @var Zend_Form_Element_Select $element */ $element->addMultiOptions($this->_dataObj->getPages4Select()); } return $meta; } public function render(Zend_View_Interface $view = null) { /** @var $doc Qs_Doc */ $doc = Zend_Registry::get('doc'); $options = array('id_form' => $this->getId()); $itemGroups = $this->_dataObj->getItemGroupNames(); foreach ($itemGroups as $groupName => $groupTitle) { /** @var $itemSubGroup Qs_Form_SubForm */ if ($itemSubGroup = $this->getSubForm($groupName)) { // init block options (view forms) /** @var $itemForm Qs_Form_SubForm */ foreach ($itemSubGroup as $itemForm) { if (($configForm = $itemForm->getSubForm('config'))) { /** @var $configViewForm Qs_Form_Config */ $configViewForm = clone $configForm; $configViewForm->getDecorator('DtDdWrapper')->setDdAttrib('class', 'item_config_view'); $html = $configViewForm->renderView(); $itemForm->getElement('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->_dataObj->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 &$v) { $v = intval(str_replace('i', '', $v)); } $options['order_' . $groupName] = $order; } } $options['meta'] = array(); $options['metaMode'] = $this->getSubForm('meta')->getMode(); $options['items'] = $this->_getItems(); $options['config'] = $this->getConfig()->toArray(); $options['su'] = App_Admin_Auth::getInstance()->getSuMode(); $options['languages'] = $this->_dataObj->getLanguageNames(); $doc->addInitFunction('initCmsForm', array($options)); $doc->addScript('js/jquery.scrollTo.js'); $doc->addScript('js/lib/form.js'); $doc->addScript('js/lib/cms.js'); $doc->addScript('js/ckeditor/ckeditor.js'); $doc->addScript('js/jquery-ui.js'); $doc->addStylesheet('css/thirdpart/jquery-ui.css'); return parent::render($view); } protected function _getItems() { if (empty($this->_items)) { $this->_items = array(); $_items = $this->_dataObj->getItemsList($this->_getData('meta[id]'), true); $_item = array_fill_keys(array('id', 'type', 'groupName', 'allowDelete'), ''); foreach ($_items as $data) { $item = array_intersect_key($data, $_item); if (!$this->{$item['groupName']} || !$this->{$item['groupName']}->{'i' . $item['id']}) { continue; } $item['id'] = (int) $item['id']; $item['actions'] = $this->getItemActions($data); $item['mode'] = $this->{$item['groupName']}->{'i' . $item['id']}->getMode(); $this->_items[] = $item; } } return $this->_items; } public function getItemActions($item) { $actions = array(); if (($this->{$item['groupName']} && $this->{$item['groupName']}->{'i' . $item['id']} && $this->{$item['groupName']}->{'i' . $item['id']}->config) || App_Admin_Auth::getInstance()->getSuMode() || $item['type'] == 'HtmlBlock_' ) { $actions[] = 'edit'; $actions[] = 'save'; $actions[] = 'cancel'; } $actions[] = 'moveUp'; $actions[] = 'moveDown'; if (App_Admin_Auth::getInstance()->getSuMode() || ($this->getConfig('item[allowDelete]') && $item['allowDelete'] == 'y') ) { $actions[] = 'delete'; } return $actions; } public function setDefaults(array $defaults = array()) { $defaults = Qs_Array::mergeRecursive($this->_getDefaults(), $defaults); if ($this->getConfig('hasHeaderImage')) { $defaults['headerImage'] = $defaults['meta']['headerImage']; } return parent::setDefaults($defaults); } }