App_SideBlock_Obj::PAGE_ALL, 'linkType' => App_SideBlock_Obj::LINK_NONE, 'blockType' => App_SideBlock_Obj::TYPE_HTML, 'backgroundColor' => 'color-none', 'show' => 'y' ); protected $_backgroundColors = array( 'color-none' => 'None', 'color-1' => 'Color 1', 'color-2' => 'Color 2', 'color-3' => 'Color 3', ); public function init() { $this->setDfRelations($this->getConfigArray('dynamicFormRelations')); return parent::init(); } protected function _getBackgroundOptions() { $options = array(); foreach ($this->_backgroundColors as $color => $title) { $class = 'side-block-bg' . (($color) ? ' ' . $color : ''); $options[$color] = '' . htmlspecialchars($title); } return $options; } protected function _initElements() { $this->addElement('text', 'title', array('label' => 'Title', 'required' => true)); $this->addElement( 'checkbox', 'hideTitle', array('label' => 'Hide Title on user end', 'decoration' => 'simple') ); $this->addElement( 'radio', 'backgroundColor', array( 'label' => 'Background Color', 'multiOptions' => $this->_getBackgroundOptions(), 'escape' => false, 'separator' => '     ' ) ); if (App_SideBlock_View::$blockTypes) { $this->addElement( 'select', 'blockType', array( 'label' => 'Block Type', 'multiOptions' => array('' => '- Select Type -') + $this->_getBlocksTypes(), 'required' => true, ) ); } $this->addElement( 'htmlEditor', 'content', array( 'label' => 'Content', 'required' => !$this->isSubmitted() || $this->isVisible('content'), 'height' => 350, 'hasMsWordNote' => true ) ); $this->addElement( 'select', 'pageId', array( 'label' => 'Parent Page', 'escapeLabel' => false, 'required' => !$this->isSubmitted() || $this->isVisible('pageId'), 'multiOptions' => array('' => '- Select Page -') + App_SideBlock_Admin_Obj::getPages4Select() ) ); $this->addElement( 'select', 'pageType', array( 'label' => 'Show on Pages', 'multiOptions' => App_SideBlock_View::$pageTypeTitles, ) ); $this->addElement( 'checkboxTree', 'idPage', array( 'label' => '', 'required' => !$this->isSubmitted() || $this->isVisible('idPage'), 'multiOptions' => $this->_getPages4MultiSelect(), 'treeViewOptions' => array('collapsed' => true), 'scriptOptions' => array( 'toggleDialog' => array( 'message' => 'Do you want to %action% all pages within this section of the website or just this page?', 'buttonNo' => 'Just %action% this page', 'buttonYes' => '%action% all pages in this section' ) ) ) ); $this->addElement( 'select', 'linkType', array( 'label' => 'Link Type', 'multiOptions' => App_SideBlock_View::$linkTypeTitles, ) ); $this->addElement( 'text', 'linkTitle', array( 'label' => 'Link Title', 'maxlength' => 255, 'required' => !$this->isSubmitted() || $this->isVisible('linkTitle') ) ); $this->addElement( 'select', 'linkPageId', array( 'label' => 'Site Page', 'escapeLabel' => false, 'required' => !$this->isSubmitted() || $this->isVisible('linkPageId'), 'multiOptions' => array('' => '- Select Page -') + App_SideBlock_Admin_Obj::getPages4Select() ) ); $this->addElement( 'text', 'linkUrl', array( 'label' => 'Url', 'required' => !$this->isSubmitted() || $this->isVisible('linkUrl'), 'description' => 'Example: ' . constant('EXAMPLE_URL'), 'maxlength' => 255, 'filters' => array('StringTrim'), 'validators' => ['Url'] ) ); $this->addElement('checkbox', 'show', array('label' => 'Show on user end', 'decoration' => 'simple')); $this->addFormRule(array($this, 'validateForm')); return $this; } public function validateForm($data) { $errors = array(); $blockType = Qs_Array::get($data, 'blockType'); $id = Qs_Array::get($data, 'id'); if (null !== $blockType && !in_array($blockType, [App_SideBlock_Obj::TYPE_HTML, App_SideBlock_Obj::TYPE_SITEMAP]) && 0 < $this->_getBlockCountByType($blockType, $id) ) { $errors['blockType'] = '"' . Qs_Array::get(App_SideBlock_View::$blockTypeTitles, $blockType, 'Block') . '"' . ' side block already exists'; } return (empty($errors)) ? true : $errors; } protected function _getPages4MultiSelect() { $result = array(); $siteMap = App_Cms_Obj::getInstance()->getSiteMap(); $this->_prepareSiteMap($siteMap, $result); return $result; } protected function _prepareSiteMap($siteMap, &$result) { $additional = array(); foreach ($siteMap as $page) { if (!array_key_exists('id', $page) || 'y' != $page['showSideBlock']) { continue; // skip sitemap.xml items and pages without side blocks // @TODO: тут проблема, якщо в сторінки showSideBlock = "n", а в неї є підсторінка в якої showSideBlock = "y", то підсторіка в дерево не попаде } if (('y' === $page['isAdditional'])) { $destination = &$additional; } else { $destination = &$result; } $destination[$page['id']] = $this->_getNode($page); if (isset($page['sub']) && $page['sub']) { $destination[$page['id']]['sub'] = array(); $this->_prepareSiteMapSub($page['sub'], $destination[$page['id']]['sub']); } } $result = array_merge($result, $additional); return $this; } protected function _prepareSiteMapSub($siteMap, &$sub) { foreach ($siteMap as $page) { if (!array_key_exists('id', $page) || 'y' != $page['showSideBlock']) { continue; // skip sitemap.xml items } $sub[$page['id']] = $this->_getNode($page); if (isset($page['sub']) && $page['sub']) { $sub[$page['id']]['sub'] = array(); $this->_prepareSiteMapSub($page['sub'], $sub[$page['id']]['sub']); } } return $this; } protected function _getNode($page) { $node = array( 'id' => $page['id'], 'title' => $page['menuTitle'], ); return $node; } protected function _getBlockCountByType($type, $id = null) { $db = Qs_Db::getInstance(); $select = $db->select(); $select->from(Qs_Db::getPair('SideBlock'), array('count' => 'COUNT(*)')); $select->where('`blockType` = ?', $type); if (null !== $id) { $select->where('`id` != ?', $id); } $select->limit(1); return $db->fetchOne($select); } protected function _getBlocksTypes() { $types = App_SideBlock_View::$blockTypeTitles; if (!Qs_SiteMap::findFirst(null, array('type' => 'Form_Contact_Admin_'), null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_CONTACT_FORM]); } if (!Qs_SiteMap::findFirst(null, array('type' => 'Testimonial_Admin_'), null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_TESTIMONIAL]); } if (!Qs_SiteMap::findFirst(null, array('type' => 'Blog\\Admin\\'), null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_FEATURED_POSTS]); } asort($types); return $types; } }