getRestAlias())) { $language = Qs_Constant::get('CURRENT_LANGUAGE'); } $this->_getDataObj()->setLanguage($language); $this->_getDataObj()->idParent = intval(Qs_Request::getRequestValue('idParent')); if ($this->_doc->getAuth()->getSuMode()) { $this->_getDataObj()->setSuMode(); } else { $this->_getDataObj()->addFilter(array('system' => 'n')); } $this->_getDataObj()->setRedirection($this->getConfig('hasRedirection', false)); return $this; } protected function _doNew() { if (!$this->_getDataObj()->isDefaultLanguage()) { Qs_Http::redirect(Qs_Request::getFinalUrl()); } $defaults = array('meta' => array(), 'options' => array()); if (isset($_REQUEST['idParent'])) { $defaults['meta']['idParent'] = intval($_REQUEST['idParent']); } if (true === $this->getConfig('additionalPages')) { $isParentAdditional = false; $idParent = (int) Qs_Request::getRequestValue('idParent'); if ($idParent) { $isParentAdditional = $this->_getDataObj()->isAdditional($idParent); $isParentPWX = $this->_getDataObj()->isPWX($idParent); if ($isParentPWX) { $defaults['options']['pwx'] = array( 'name' => 'pwx', 'show' => 'y', 'value' => 'y', ); $defaults['options']['showInPwxMenu'] = array( 'name' => 'showInPwxMenu', 'show' => 'y', 'value' => 'y', ); $defaults['options']['showInPwxFooter'] = array( 'name' => 'showInPwxFooter', 'show' => 'y', 'value' => 'y', ); } } if ('y' == Qs_Request::getRequestValue('isAdditional') || $isParentAdditional) { $defaults['options']['isAdditional'] = array( 'name' => 'isAdditional', 'show' => 'n', 'value' => 'y', ); $defaults['options']['showInMenu'] = array( 'name' => 'showInMenu', 'show' => 'n', 'value' => 'n', ); $defaults['options']['showInFooter'] = array( 'name' => 'showInFooter', 'show' => 'n', 'value' => 'n', ); } } $id = $this->_getDataObj()->insertDefaultPage($defaults); Qs_Http::redirect($this->url(array('action' => 'edit', 'id' => $id))); } protected function _doEdit() { if (!$this->_lock()) { $this->_setBackAttention($this->_createMessage(static::MSG_LOCKED, $this->_getLocker())); $this->_doBack(); } if (!$this->_getDataObj()->draftExists()) { try { $this->_getDataObj()->createDraft(); } catch (App_Cms_Exception $e) { $this->_doc->displayError($e->getMessage()); return $this; } } if (!$this->_getDataObj()->draftPageOptionExists()) { try { $this->_getDataObj()->createPageOptionDraft(); } catch (App_Cms_Exception $e) { $this->_doc->displayError($e->getMessage()); return $this; } } $data = $this->_getDataObj()->getDraftData(); if (!App_Admin_Auth::getInstance()->getSuMode() && $this->_dataObj->isParentDisabled($data['meta']['id'])) { $data['meta']['enabled'] = 'n'; // якщо "задісейблений" один з парентів, то скидаємо чекбокс не залежно від його значення } if (empty($data)) { $this->_setBackError('Invalid Page ID'); $this->_doBack(); } $form = $this->_getEditForm(); if (!empty($data['meta']['alias']) && !empty($data['meta']['menuTitle'])) { $form->meta->setMode('view'); } $form->setDefaults($data); $this->_addFormItem($form); if (null === $data['meta']['alias']) { $this->_action = 'new'; } $this->_postEdit(); return $this; } /** * @param array $options * @return App_Cms_Form_Edit */ protected function _getEditForm(array $options = array()) { if (!array_key_exists('defaults', $options)) { $options['defaults'] = $this->_getDataObj()->getDraftData(); $options['primaryKey'] = $this->_getDataObj()->getPrimaryKeyArray(); } return parent::_getEditForm($options); } protected function _doUpdateMetaAjax() { $result = array(); if ($this->_isLocked()) { $locker = $this->_getLocker(); $result = array('isLocked' => true, 'error' => $this->_createMessage(static::MSG_LOCKED, $locker)); } else { $form = $this->_getEditForm(); /** @var $meta Qs_Form_SubForm */ $meta = $form->getSubForm('meta'); if ($meta->validate()) { $data = $meta->getValues(true); /** * Якщо прийшла порожня стрічка (""), то це сабміт з "dummy" моду і парента апдейтити не треба. * В решті випадків парент має апдейтитись */ if ($data['idParent'] == '') { unset($data['idParent']); } $enabledElement = $meta->getElement('enabled'); if ($enabledElement) { if (($enabledElement->getAttrib('disabled') || $enabledElement->getAttrib('disable')) && empty($data['enabled']) ) { unset($data['enabled']); } } $this->_getDataObj()->updateDraftMeta($data); } else { $result['errors'] = $meta->getMessages(null, true); } } $this->_displayJson($result); } protected function _doUpdateItem() { $this->_do404(); } protected function _doUpdateItemAjax() { $this->_getDataObj()->setPrimaryKey(Qs_Request::getRequestValue('idPage')); if ($this->_isLocked()) { $locker = $this->_getLocker(); $result = array('isLocked' => true, 'error' => $this->_createMessage(static::MSG_LOCKED, $locker)); } else { $this->_getDataObj()->initDraftData(); $item = Qs_Request::getPost(); unset($item['action']); $item['id'] = $item['idItem']; $form = $this->_getEditForm(); /** @var App_Cms_Form_SubForm_Item $itemSubForm */ $itemSubForm = $form->getSubForm($item['groupName'])->getSubForm('i' . $item['idItem']); $result = array('isValid' => true); if ($configForm = $itemSubForm->getSubForm('config')) { /** @var $configViewForm Qs_Form_Config */ $configViewForm = clone $configForm; /** @var $decorator Qs_Form_Decorator_DtDdWrapper */ $decorator = $configViewForm->getDecorator('DtDdWrapper'); $decorator->setDdAttrib('class', 'item_config_view'); $result = $configViewForm->validateAjax(); if (!$result['isValid']) { $this->_displayJson($result); } else { $result['viewHtml'] = $configViewForm->renderView(); } } if ($itemSubForm instanceof App_Cms_Form_SubForm_ItemHtmlBlock) { $result['contentViewHtml'] = $this->_dataObj->getFilteredHtmlBlockViewContent( Qs_Array::get($item, 'data[content]') ); } $this->_getDataObj()->updateDraftItem($item['idItem'], $item['type'], $item); } $this->_displayJson($result); } protected function _doLoadDialogItemTypeAjax() { $item = array(); $item['typeOptions'] = $this->_getDataObj()->getDItemType4Select(); $item['groupNameOptions'] = $this->_getDataObj()->getItemGroupNames(); $this->_doc->assign('item', new Qs_Doc_Item($item)); $this->_doc->setLayoutTemplate($this->getTemplate('Dialog/item-type.tpl')); echo $this->_doc->fetch(); exit; } protected function _doAddItemAjax() { if ($this->_isLocked()) { $locker = $this->_getLocker(); $result = array('isLocked' => true, 'error' => $this->_createMessage(static::MSG_LOCKED, $locker)); $this->_displayJson($result); } $id = (int)Qs_Request::getPostValue('id'); $groupName = Qs_Request::getPostValue('groupName'); $renderGroup = (bool)Qs_Request::getPostValue('renderGroup'); $type = Qs_Request::getPostValue('type'); $itemData = array('idPage' => $id, 'type' => $type); if (!($itemData['idGroup'] = $this->_getDataObj()->getGroupIdByName($groupName))) { $itemData['idGroup'] = 1; } $idItem = $this->_getDataObj()->insertDraftItem($itemData); $data = $this->_getDataObj()->getDraftItem($idItem); $this->_getDataObj()->initDraftData(); $form = $this->_getEditForm(); $result = array('idItem' => $idItem); $result['actions'] = $form->getItemActions($data); if (in_array('edit', $result['actions'])) { $result['mode'] = 'edit'; } $this->_doc->clearScripts(); $this->_doc->clearStylesheets(); $this->_doc->clearInlineScripts(); $this->_doc->clearInitFunctions(); /** @var $subFormItemsGroup Zend_Form_SubForm */ if ($renderGroup) { if ($subFormItemsGroup = $form->getSubForm($groupName)) { $subFormItemsGroup->setView(Qs_View::getInstance()); $result['html'] = $subFormItemsGroup->render(); } } else { /** @var $subFormItem Qs_Form_SubForm*/ if (isset($form->$groupName) && isset($form->$groupName->{"i{$idItem}"})) { $subFormItem = $form->$groupName->{"i{$idItem}"}; } else { $subFormItem = $form->getItemSubForm(array('type' => $type, 'groupName' => $groupName, 'id' => $idItem)); } $form->render(); $subFormItem->setDefaults($data); $subFormItem->setView(Qs_View::getInstance()); $result['html'] = $subFormItem->render(); } // TODO: add already loaded resources check, onLoadCallback // $result['javascript'] = $this->_getItemJavaScript(); $this->_displayJson($result); } protected function _getItemJavaScript() { $script = ''; $head = $this->_doc->getTemplateVariable('head'); foreach($head['stylesheet'] as $file) { $script .= "qs.resource.load('{$file['href']}');\n"; } foreach ($head['script'] as $file) { $script .= "qs.resource.load('{$file['src']}');\n"; } foreach ($head['inlineScript'] as $inlineScript) { $script .= "{$inlineScript}\n"; } $script .= "\n" . $this->_doc->renderReadyFunctions(); $script .= "\n" . $this->_doc->renderInitFunctions(); return $script; } protected function _doDeleteItemAjax() { if ($this->_isLocked()) { $locker = $this->_getLocker(); $result = array('isLocked' => true, 'error' => $this->_createMessage(static::MSG_LOCKED, $locker)); } else { $idItem = (int)Qs_Request::getPostValue('idItem'); $this->_getDataObj()->deleteDraftItem($idItem); $result = array(); } $this->_displayJson($result); } protected function _doReorderItemsAjax() { if ($this->_isLocked()) { $locker = $this->_getLocker(); $result = array('isLocked' => true, 'error' => $this->_createMessage(static::MSG_LOCKED, $locker)); } else { $groupName = Qs_Request::getPostValue('groupName'); $order = Qs_Request::getPostValue('order'); $this->_getDataObj()->reorderDraftItemsGroup($order); asort($order); $result = array( 'groupName' => $groupName, 'order' => array_keys($order) ); } $this->_displayJson($result); } protected function _handleFiles(Qs_Form $form) { $data = array(); foreach ($this->_getDataObj()->getFileFields() as $fieldName) { if (($value = $form->getValue($fieldName))) { $data[$fieldName] = $value; } else if (Qs_Request::getPostValue($fieldName . '[del]') == 'on') { $data[$fieldName] = null; $adapter = new Qs_File_Transfer_Adapter_Db(); $adapter->delete(Qs_Request::getPostValue($fieldName . '[oldFile]')); unset($adapter); } } if (!empty($data)) { $this->_getDataObj()->updateDraftMeta($data); } return $this; } protected function _doPublish() { if ($this->_isLocked()) { $this->_setBackAttention($this->_createMessage(static::MSG_LOCKED, $this->_getLocker())); $this->_doBack(); } $this->_getDataObj()->setPrimaryKey(Qs_Request::getRequestValue('meta[id]')); $this->_getDataObj()->initDraftData(); $data = $this->_getDataObj()->getDraftData(); if (empty($data)) { $this->_setBackError('The page draft you edited has been already published. ' . 'You changes are not applied. ' . 'Try again.'); $this->_doBack(); } $post = Qs_Request::getPost(); unset($post['meta'], $post['ITEMS']); // мета та айтеми зберігаються аяксьм і при публікуванні вже збережені $data = array_merge($data, $post); $this->_getDataObj()->setPrimaryKey($data['meta']['id']); $this->_getDataObj()->idParent = (int) $data['meta']['idParent']; $form = $this->_getEditForm(); if ($form->validate($data)) { $this->_handleFiles($form); $this->_getDataObj()->publish(); $this->_getDataObj()->deleteDraft(); $this->_getDataObj()->initData(); $this->_unlock(); $this->_doBack(); } else { $messages = $form->getMessages(); if (array_key_exists('meta', $messages)) { $form->meta->setMode('edit'); } else { $form->meta->setMode('view'); } foreach ($messages as $groupName => $groupItems) { if ($groupName == 'meta') { continue; } if (!$form->{$groupName}) { continue; } $items = array_keys($groupItems); foreach ($items as $item) { if (!$form->{$groupName}->{$item}) { continue; } $form->{$groupName}->{$item}->setMode('edit'); } } $this->_addFormItem($form); } return $this; } protected function _doRemoveDraft() { $this->_unlock(); $this->_getDataObj()->deleteDraft(); $meta = $this->_getDataObj()->getMeta($this->_getDataObj()->getPrimaryKey()); if ($meta && null === $meta['alias']) { $this->_getDataObj()->delete(); } $this->_doBack(); } protected function _doShowSiteMap() { $this->_addLinksItem(); $item = array(); $options = array(); if (true === $this->getConfig('additionalPages')) { $item['additionalPages'] = true; $options = array('select' => $this->_getDataObj()->getAdditionalSelect4SiteMap()); } $item['siteMap'] = $this->_getDataObj()->getSiteMap($options); App_Sitemap_Virtual::prepare($item['siteMap']); $item['siteMapPagesCount'] = $this->_getDataObj()->getSiteMapPageCount($item['siteMap']); if (true === $this->getConfig('additionalPages')) { $item['additionalPagesTopLevelCount'] = $this->_getAdditionalPagesTopLevelCount($item['siteMap']); } $item['tpl'] = $this->getTemplate('sitemap.tpl'); $this->_addItem($item); return $this; } protected function _getAdditionalPagesTopLevelCount($sitemap) { $count = 0; foreach ($sitemap as $page) { if ('y' == $page['isAdditional']) { $count++; } } return $count; } protected function _getReorderForm(array $options = array()) { if (true === $this->getConfig('additionalPages') && 'y' == Qs_Request::getRequestValue('isAdditional')) { $isAdditional = true; } else { $isAdditional = false; } $options = array( 'idParent' => $this->_getDataObj()->idParent, 'orderOptions' => $this->_getDataObj()->getReorderOptions(null, null, $isAdditional), ); $form = $this->_getFormInstance('reorder', $options); return $form; } protected function _getDefaultLinks() { $links = array(); if ($this->getConfig('allowAddTopLevelPage') || $this->_doc->getAuth()->getSuMode()) { $links['new'] = 'Add top-level ' . $this->getConfig('itemName'); } if ($this->getConfig('allowReorderTopLevelPages') || $this->_doc->getAuth()->getSuMode()) { $links['reorder'] = 'Reorder top-level ' . $this->getConfig('itemsName'); } if ($this->_doc->getAuth()->getSuMode()) { $links[] = array( 'title' => 'Export ' . $this->getConfig('itemsName'), 'url' => Qs_SiteMap::findFirst(null, array('type' => 'Cms\\Export\\'), null, 'url') ); } return $links; } protected function _addGoogleAnalyticsLinkItem() { $url = $this->getConfig('analytics')->url; $code = App_Settings_Obj::get('analyticsCode'); if (!empty($code) && !empty($url)) { $item = array(); $item['attribs'] = array( 'id' => str_replace('_', '-', substr(strtolower(get_class($this)), 0, -4) . 'links-google-analytics'), 'class' => 'list_links' ); $item['links'] = array( array( 'title' => 'Launch Google Analytics', 'attribs' => array('target' => '_blank'), 'url' => $url, ) ); $item['tpl'] = $this->getTemplate('list-links.tpl'); $this->_addItem($item); } return $this; } protected function _addLinksItem(array $links = null, $item = array()) { parent::_addLinksItem($links); $this->_addGoogleAnalyticsLinkItem(); return $this; } protected function _getData($field = null, $default = null, $method = 'POST') { if ($_SERVER['REQUEST_METHOD'] == strtoupper($method)) { if (false === $field) { return $_POST; } return Qs_Request::getPostValue($field, $default); } $key = $this->_getDataObj()->getPrimaryKey(); if (empty($key)) { return $default; } return $this->_getDataObj()->getDraftData($field); } protected function _doGetBodyTemplatesAjax() { $handler = Qs_Request::getRequestValue('handler'); $options = $this->_getDataObj()->getBodyTemplate4Select($handler); $this->_displayJson(array('options' => $options)); } protected function _doGetHtmlBlockContentAjax() { $idItem = Qs_Request::getRequestValue('idItem'); $item = $this->_getDataObj()->getDraftItem($idItem); die(Qs_Array::get($item, 'data[content]')); } }