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', ); 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() { $pageType = $this->_getData('pageType', $this->_defaults['pageType']); $blockType = $this->_getData('blockType', $this->_defaults['blockType']); $linkType = $this->_getData('linkType', $this->_defaults['linkType']); $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' => true, 'height' => 350, 'hasMsWordNote' => true) ); if ($blockType !== App_SideBlock_Obj::TYPE_HTML) { $this->getElement('content')->setRequired(false); $this->getElement('content')->getDecorator('Label')->setTagOption('class', 'hidden'); $this->getElement('content')->getDecorator('HtmlTag')->setOption('class', 'hidden'); } $this->addElement( 'select', 'pageType', array( 'label' => 'Show on Pages', 'multiOptions' => App_SideBlock_View::$pageTypeTitles, ) ); $this->addElement( 'checkboxTree', 'idPage', array( 'label' => '', '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' ) ) ) ); if ($pageType == App_SideBlock_Obj::PAGE_CUSTOM) { $this->getElement('idPage')->setRequired(); } $this->addElement( 'select', 'linkType', array( 'label' => 'Link Type', 'multiOptions' => App_SideBlock_View::$linkTypeTitles, ) ); $this->addElement( 'text', 'linkTitle', array( 'label' => 'Link Title', 'maxlength' => 255, 'required' => (App_SideBlock_Obj::LINK_NONE != $linkType), ) ); $this->addElement( 'select', 'linkPageId', array( 'label' => 'Site Page', 'escapeLabel' => false, 'required' => (App_SideBlock_Obj::LINK_PAGE == $linkType), 'multiOptions' => array('' => '- Select Page -') + App_SideBlock_Admin_Obj::getPages4Select() ) ); $this->addElement( 'text', 'linkUrl', array( 'label' => 'Url', 'required' => (App_SideBlock_Obj::LINK_URL == $linkType), 'description' => 'Example: http://www.adaptainc.com', 'maxlength' => 255, 'filters' => array('StringTrim'), ) ); if ('url' == $this->_getData('linkType')) { $this->getElement('linkUrl')->addValidator('Url'); } $this->addElement('checkbox', 'show', array('label' => 'Show on user end', 'decoration' => 'simple')); foreach (array('content', 'idPage', 'linkTitle', 'linkPageId', 'linkUrl') as $elementName) { $this->getElement($elementName)->getDecorator('label')->setOption('class', 'required'); } $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 && App_SideBlock_Obj::TYPE_HTML !== $blockType && 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; } public function initRender() { /** @var Qs_Doc $doc */ $doc = Zend_Registry::get('doc'); $doc->addScript('js/app/admin-sideBlock.js'); $params = array( 'formId' => $this->getId(), 'TYPE_HTML' => App_SideBlock_Obj::TYPE_HTML, 'PAGE_CUSTOM' => App_SideBlock_Obj::PAGE_CUSTOM, 'LINK_NONE' => App_SideBlock_Obj::LINK_NONE, 'LINK_PAGE' => App_SideBlock_Obj::LINK_PAGE, 'LINK_URL' => App_SideBlock_Obj::LINK_URL, ); $doc->addInitObject('SideBlockAdmin', array($params)); return parent::initRender(); } 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 } 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' => 'Form_Newsletter_Admin_'), null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_SUBSCRIPTION]); } 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]); } if (!Qs_SiteMap::findFirst(null, array('type' => 'Newsletter_Admin_'), null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_NEWSLETTERS]); } if (!Qs_SiteMap::findFirst(null, array('type' => 'News_Admin_'), null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_FEATURED_NEWS], $types[App_SideBlock_Obj::TYPE_RECENT_NEWS]); } else { if (true !== Qs_Application::getConfig('news')->hasFeatured) { unset($types[App_SideBlock_Obj::TYPE_FEATURED_NEWS]); } } asort($types); return $types; } }