App_SideBlock_Obj::PAGE_ALL, 'linkType' => App_SideBlock_Obj::LINK_NONE, 'blockType' => App_SideBlock_Obj::TYPE_HTML, 'backgroundColor' => 'color-none', 'show' => 'y', ]; protected $_backgroundColors = [ 'color-none' => 'None', 'color-1' => 'Color 1', 'color-2' => 'Color 2', ]; public function init() { $this->_dfRelations = $this->getConfigArray('dynamicFormRelations'); return parent::init(); } protected function _getBackgroundOptions() { $options = []; 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', ['label' => 'Title', 'required' => true]); $this->addElement( 'checkbox', 'hideTitle', ['label' => 'Hide Title on user end', 'decoration' => 'simple'] ); $this->addElement( 'radio', 'backgroundColor', [ 'label' => 'Background Color', 'multiOptions' => $this->_getBackgroundOptions(), 'escape' => false, 'separator' => '     ', ] ); if (App_SideBlock_View::$blockTypes) { $this->addElement( 'select', 'blockType', [ 'label' => 'Block Type', 'multiOptions' => ['' => '- Select Type -'] + $this->_getBlocksTypes(), 'required' => true, ] ); } $this->addElement( 'htmlEditor', 'content', [ 'label' => 'Content', 'required' => !$this->isSubmitted() || $this->isVisible('content'), 'height' => 350, 'hasMsWordNote' => true, ] ); $this->addElement( 'select', 'pageId', [ 'label' => 'Parent Page', 'escapeLabel' => false, 'required' => !$this->isSubmitted() || $this->isVisible('pageId'), 'multiOptions' => ['' => '- Select Page -'] + App_SideBlock_Admin_Obj::getPages4Select(), ] ); $this->addElement( 'select', 'pageType', [ 'label' => 'Show on Pages', 'multiOptions' => App_SideBlock_View::$pageTypeTitles, ] ); $this->addElement( 'checkboxTree', 'idPage', [ 'label' => '', 'required' => !$this->isSubmitted() || $this->isVisible('idPage'), 'multiOptions' => $this->_getPages4MultiSelect(), 'treeViewOptions' => ['collapsed' => true], 'scriptOptions' => [ 'toggleDialog' => [ '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', [ 'label' => 'Link Type', 'multiOptions' => App_SideBlock_View::$linkTypeTitles, ] ); $this->addElement( 'text', 'linkTitle', [ 'label' => 'Link Title', 'maxlength' => 255, 'required' => !$this->isSubmitted() || $this->isVisible('linkTitle'), ] ); $this->addElement( 'select', 'linkPageId', [ 'label' => 'Site Page', 'escapeLabel' => false, 'required' => !$this->isSubmitted() || $this->isVisible('linkPageId'), 'multiOptions' => ['' => '- Select Page -'] + App_SideBlock_Admin_Obj::getPages4Select(), ] ); $this->addElement( 'text', 'linkUrl', [ 'label' => 'Url', 'required' => !$this->isSubmitted() || $this->isVisible('linkUrl'), 'description' => 'Example: http://www.adaptainc.com', 'maxlength' => 255, 'filters' => ['StringTrim'], 'validators' => ['Url'], ] ); $this->addElement('checkbox', 'show', ['label' => 'Show on user end', 'decoration' => 'simple']); $this->addFormRule([$this, 'validateForm']); return $this; } public function validateForm($data) { $errors = []; $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 = []; $siteMap = App_Cms_Obj::getInstance()->getSiteMap(); $this->_prepareSiteMap($siteMap, $result); return $result; } protected function _prepareSiteMap($siteMap, &$result) { $additional = []; 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'] = []; $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'] = []; $this->_prepareSiteMapSub($page['sub'], $sub[$page['id']]['sub']); } } return $this; } protected function _getNode($page) { $node = [ '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'), ['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, ['type' => 'Form_Contact_Admin_'], null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_CONTACT_FORM]); } if (!Qs_SiteMap::findFirst(null, ['type' => 'Form_Newsletter_Admin_'], null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_SUBSCRIPTION]); } if (!Qs_SiteMap::findFirst(null, ['type' => 'ECommerce_Product_'], null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_FEATURED_PRODUCTS]); } if (!Qs_SiteMap::findFirst(null, ['type' => 'Event\\'], null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_UPCOMING_EVENTS]); } if (!Qs_SiteMap::findFirst(null, ['type' => 'Testimonial_Admin_'], null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_TESTIMONIAL]); } if (!Qs_SiteMap::findFirst(null, ['type' => 'Blog\\Admin\\'], null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_FEATURED_POSTS]); } if (!Qs_SiteMap::findFirst(null, ['type' => 'Newsletter_Admin_'], null, 'url')) { unset($types[App_SideBlock_Obj::TYPE_NEWSLETTERS]); } if (!Qs_SiteMap::findFirst(null, ['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; } }