_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; } /** * @param string|null $field * @param mixed|null $default * @return Qs_Config|mixed */ 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 = ['ViewHelper']; $elements = []; foreach ($languages as $language) { $elements[] = $language['name']; $options = [ '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', ['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', [ '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 = [ 'decorators' => [ 'FormElements', ['Fieldset', ['class' => 'cms_item_group']], ['HtmlTag', ['tag' => 'dd', 'id' => 'ITEMS-element']], [['label' => 'HtmlTag'], ['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 = [ '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(); /** @var Qs_Form_Decorator_DtDdWrapper $dtDdWrapper */ $dtDdWrapper = $configForm->getDecorator('DtDdWrapper'); $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) { /** @var Zend_Config $item */ $item = $this->getConfig('item'); $filter = $item->get('filter'); if (null === $filter) { $filter = []; } 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(['action' => 'removeDraft', 'id' => $this->_dataObj->getPrimaryKey()]); $this->setCancelUrl($url); $disablePreview = $this->_getData('meta[customOptions][disablePreview]', 'false'); if ('false' == $disablePreview) { $this->addElement( 'submit', 'btnPreview', [ 'label' => 'Preview Changes', 'attribs' => [ 'class' => 'btn btn-info' . ((!$this->_dataObj->getDraftData('meta[hasChanges]')) ? ' hidden' : ''), 'helper' => 'formInputButton', ], ] ); } $this->addElement( 'submit', 'btnSubmit', ['label' => 'Publish', 'attribs' => ['class' => 'btn btn-primary']] ); $this->addElement( 'submit', 'btnCancel', [ 'label' => 'Cancel All Changes', 'attribs' => [ 'class' => 'btn', 'helper' => 'formInputButton', 'onclick' => "if (confirm('{$this->getConfig('cancelMessage')}')) " . "document.location = '" . htmlspecialchars($this->getCancelUrl()) . "';", ], ] ); $this->addDisplayGroup(['btnPreview', 'btnSubmit', 'btnCancel'], 'submitGroup'); return $this; } protected function _getMetaSubForm() { $idPage = (int) Qs_Request::getRequestValue('id', $this->_dataObj->getPrimaryKey()); $meta = new App_Cms_Form_SubForm_Meta([ '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; } protected function getPreviewUrl() { if (null === $this->_previewUrl) { $this->_previewUrl = Qs_Constant::get('BASE_URL_LANGUAGE') . '/' . Qs_Constant::get('CURRENT_PAGE') . '?action=preview&pageId=' . $this->_dataObj->getPrimaryKey(); } return $this->_previewUrl; } public function render(Zend_View_Interface $view = null) { /** @var $doc Qs_Doc */ $doc = Zend_Registry::get('doc'); $options = [ 'id_form' => $this->getId(), 'btnPreviewId' => null, 'previewUrl' => $this->getPreviewUrl(), ]; if (($btnPreview = $this->getElement('btnPreview'))) { $options['btnPreviewId'] = $btnPreview->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; /** @var Qs_Form_Decorator_DtDdWrapper $dtDdWrapper */ $dtDdWrapper = $configViewForm->getDecorator('DtDdWrapper'); $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; } } /** @var App_Cms_Form_SubForm_Meta $metaSubForm */ $metaSubForm = $this->getSubForm('meta'); $options['meta'] = []; $options['metaMode'] = $metaSubForm->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', [$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 = []; $_items = $this->_dataObj->getItemsList($this->_getData('meta[id]'), true); $_item = array_fill_keys(['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 = []; 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 = []) { $defaults = Qs_Array::mergeRecursive($this->_getDefaults(), $defaults); if ($this->getConfig('hasHeaderImage')) { $defaults['headerImage'] = $defaults['meta']['headerImage']; } return parent::setDefaults($defaults); } }