'itemName', 'itemsName' => 'itemsName' ); protected $_messageTemplates = array( 'msgAdded' => '%itemName% has been added', 'msgUpdated' => '%itemName% has been updated', 'msgReordered' => '%itemsName% have been reordered', 'msgDeleted' => '%itemName% has been deleted', ); protected $_pageHeaderTemplates = array( 'headerNew' => 'Add a new %itemName%', 'headerEdit' => 'Edit %itemName%', 'headerReorder' => 'Reorder %itemsName%', 'headerView' => 'View %itemName%', ); protected $_formDefaults = array(); protected static $_translatorDefault; public function __construct($options = array()) { $this->setOptions($options); $this->_init(); } public function __get($name) { if ($name == 'dataObj') { return $this->_getDataObj(); } else if ($name == 'list') { return $this->_getList(); } $propertyName = '_' . $name; if (property_exists($this, $propertyName)) { return $this->{$propertyName}; } if (in_array($name, array('itemName', 'itemsName'))) { return $this->_getDataObj()->$name; } throw new Qs_ViewController_Exception('Unknown property ' . $name); } protected function _init() { if (Zend_Registry::isRegistered('Zend_Translate')) { $translate = Zend_Registry::get('Zend_Translate'); if (!$translate->isSection($this->_getTranslationSection(), CURRENT_LOCALE)) { $file = BASE_PATH . '/languages/' . CURRENT_LOCALE . '/' . $this->_getTranslationSection() . '.mo'; if (file_exists($file)) { $translate->addTranslation($file, CURRENT_LOCALE); $translate->setSection($this->_getTranslationSection()); } } } return $this; } public function exec() { $this->_initAction(); $this->_callAction(); return $this; } protected function _addMessageTemplate($key, $text) { $this->_messageTemplates[$key] = $text; return $this; } protected function _getListOptions() { foreach (array('itemName', 'itemsName') as $name) { if (!array_key_exists($name, $this->_listOptions)) { if (property_exists($this->dataObj, $name)) { $this->_listOptions[$name] = $this->dataObj->$name; } } } if (!array_key_exists('idItem', $this->_listOptions)) { $this->_listOptions['idItem'] = $this->getIdItem(); } if (!array_key_exists('idContainer', $this->_listOptions)) { $this->_listOptions['idContainer'] = $this->_getIdContainer(); } return $this->_listOptions; } /** * @return Qs_ViewController_List */ protected function _getList() { if (null === $this->_list) { $this->_list = new Qs_ViewController_List($this->_getListOptions()); $this->_list->setViewController($this); } return $this->_list; } protected function _setMessage($messageString, $messageKey = null) { if ($messageKey === null) { $keys = array_keys($this->_messageTemplates); $messageKey = current($keys); } if (!isset($this->_messageTemplates[$messageKey])) { throw new Qs_ViewController_Exception("No message template exists for key '$messageKey'"); } $this->_messageTemplates[$messageKey] = $messageString; return $this; } protected function _setMessages(array $messages) { foreach ($messages as $key => $message) { $this->_setMessage($message, $key); } return $this; } protected function _getMessageVariables() { return array_keys($this->_messageVariables); } protected function _setHeader() { if ($this->_hasDataObj() && $this->doc->hasAuth() && get_class($this->doc->getAuth()) == 'App_Admin_Auth') { $key = 'header' . ucfirst($this->_action); $header = $this->_createHeader($key); if ($header) { $this->doc->setHeader($header); } } return $this; } protected function _callAction() { if (method_exists($this, $this->_actionMethod)) { if (Qs_Version::compareVersion('2.2.0') <= 0) { $this->_setHeader(); } $this->{$this->_actionMethod}(); } else if (Qs_Request::isXmlHttpRequest()) { Qs_Debug::log('Action method not found: ' . get_class($this) . '->' . $this->_actionMethod); return false; } else { throw new Zend_Exception('Action method not found: ' . get_class($this) . '->' . $this->_actionMethod); } return $this; } public function setIdItem($id) { $this->_idItem = (int) $id; return $this; } public function getIdItem() { return $this->_idItem; } protected function _initAction() { if (empty($this->_action)) { if (isset($_REQUEST['action']) && !empty($_REQUEST['action'])) { if (isset($_REQUEST['__idItem']) && !empty($_REQUEST['__idItem'])) { if ($_REQUEST['__idItem'] == $this->getIdItem()) { $this->_action = $_REQUEST['action']; } } else { $this->_action = $_REQUEST['action']; } } if (empty($this->_action) || !in_array($this->_action, $this->_actions)) { $this->_action = $this->_defaultAction; } } if (!in_array($this->_action, $this->_actions)){ $this->_action = reset($this->_actions); } $this->_actionMethod = '_do' . ucfirst($this->_action); if (Qs_Request::isXmlHttpRequest()) { if (isset($_REQUEST['__idItem']) && !empty($_REQUEST['__idItem']) && $_REQUEST['__idItem'] == $this->getidItem()) { $this->_actionMethod .= 'Ajax'; } else if (!isset($_REQUEST['__idItem'])) { $this->_actionMethod .= 'Ajax'; } } return $this; } protected function _initPrimaryKey() { $_primaryKey = array(); $primary = $this->_dataObj->getPrimary(); if (is_array($primary) && !empty($primary)) { foreach ($primary as $name) { $_primaryKey[$name] = Qs_Request::getRequestValue($name); } } $this->_primaryKey = $_primaryKey; $this->_dataObj->setPrimaryKey($_primaryKey); return $this; } protected function _setCompressJson($flag) { $this->_compressJson = (bool) $flag; return $this; } protected function _getCompressJson() { if (null === $this->_compressJson) { if (strpos(Qs_Request::getHeader('ACCEPT-ENCODING'), 'gzip') === false) { $this->_setCompressJson(false); } else { $this->_setCompressJson(true); } } return $this->_compressJson; } protected function _getDataObjClass() { $class = get_class($this); if (false !== ($pos = strrpos($class, 'View'))) { $class = substr($class, 0, $pos) . 'Obj'; } return $class; } protected function _getDataObjClassFile($class = null) { if (null === $class) { $class = $this->_getDataObjClass(); } return BASE_PATH . '/' . str_replace('_', '/', $class) . '.php'; } protected function _hasDataObj() { if (null !== $this->_dataObj) { return true; } $file = $this->_getDataObjClassFile(); if (file_exists($file)) { return true; } return false; } protected function _getDataObj() { if (null === $this->_dataObj) { $class = $this->_getDataObjClass(); $file = $this->_getDataObjClassFile($class); if (file_exists($file)) { $this->setDataObj(new $class()); $this->_initPrimaryKey(); } else { throw new Qs_ViewController_Exception('Data object is not defined ' . $class); } } return $this->_dataObj; } public function setDataObj($dataObj) { if (!($dataObj instanceof Qs_DataObj_Interface)) { throw new Qs_ViewController_Exception('Data object is not instance of Qs_DataObj_Interface'); } $this->_dataObj = $dataObj; return $this; } protected function _displayJson($data) { header('Cache-Control: no-store, no-cache, must-revalidate, private'); header('Pragma: no-cache'); header('Content-Type: application/x-javascript; charset=utf-8'); $json = json_encode($data); if ($this->_getCompressJson()) { header('Content-Encoding: gzip'); $json = gzencode($json, 9); } die($json); } protected function _saveBackUrl() { $this->_setBackUrl(Qs_Request::getUrl()); return $this; } protected function _setBackUrl($backUrl, $sessionName = null) { if (is_null($sessionName)){ $sessionName = CURRENT_PAGE; } $session = new Qs_Session_Namespace($sessionName); $session->backUrl = $backUrl; return $this; } public function setDoc($doc) { $this->_doc = $doc; return $this; } public function setConfig($config, $value = null) { $globalConfig = (array) $this->getConfig(); if (is_array($config)) { $this->_config = array_merge($globalConfig, $config); } else { $this->_config[$config] = $value; } return $this; } public function getConfig($field = false, $default = null) { if (null === $this->_config) { $configObj = Zend_Registry::get('config')->app; $section = $this->getApplicationName(); if (isset($configObj->$section)) { $this->_config = $configObj->$section->toArray(); } } if (false === $field) { return $this->_config; } if (array_key_exists($field, (array)$this->_config)) { return $this->_config[$field]; } return $default; } public function setOptions($options) { if (isset($options['options'])) { unset($options['options']); } foreach ($options as $key => $value) { $method = 'set' . ucfirst($key); if (method_exists($this, $method)) { $this->$method($value); } else { $this->_options[$key] = $value; } } return $this; } public function getOption($name) { if (isset($this->_options[$name])) { return $this->_options[$name]; } return null; } public function setOption($name, $value) { $this->_options[$name] = $value; return $this; } public function setRestAlias($restAlias, $updateRestParams = true) { $this->_restAlias = $restAlias; if ($updateRestParams && !empty($restAlias)) { $restParams = preg_split($this->_getRestParamsPattern(), $restAlias); foreach ($restParams as &$param) { $param = rawurldecode($param); } $this->setRestParams($restParams); } return $this; } protected function _setRestParamsPattern($pattern) { $this->_restParamsPattern = $pattern; return $this; } protected function _getRestParamsPattern() { if (null === $this->_restParamsPattern) { $this->_setRestParamsPattern('/\//'); } return $this->_restParamsPattern; } public function getRestParam($name) { if (array_key_exists($name, $this->_restParams)) { return $this->_restParams[$name]; } return null; } public function getRestParams() { return $this->_restParams; } public function setRestParams(array $restParams) { $this->_restParams = $restParams; return $this; } public function getRestAlias() { return $this->_restAlias; } protected function _getBackUrl($sessionName = null) { if (is_null($sessionName)){ $sessionName = CURRENT_PAGE; } $session = new Qs_Session_Namespace($sessionName); if (isset($session->backUrl)) { return $session->backUrl; } return $this->url(); } protected function _getDefaultLinks() { $links = array( 'new' => 'Add New ' . $this->dataObj->itemName, 'reorder' => 'Reorder ' . $this->dataObj->itemsName ); return $links; } protected function _getLinks() { if (empty($this->_links)) { $links = $this->_getDefaultLinks(); $this->_addLinks($links); } return $this->_links; } protected function _addLinks(array $links) { foreach ($links as $action => $spec) { if (!is_numeric($action)) { if (!in_array($action, $this->_actions)) { continue; } if ('reorder' == $action && (!($list = $this->_getList()) || 2 > $list->getItemsCount())) { continue; } } if (is_string($spec)) { $this->_addLink($spec, $this->url(array('action' => $action))); } elseif (is_array($spec)) { $attribs = array(); $options = array(); if (!array_key_exists('url', $spec)) { $url = $this->url(array('action' => $action)); } if (!array_key_exists('title', $spec)) { $title = ucfirst($action); } extract($spec); $this->_addLink($title, $url, $attribs, $options); } } return $this; } protected function _addLink($title, $url, $attribs = array(), $_options = array()) { $defaultOptions = array( 'delimiter' => ' | ' ); $options = array_merge($defaultOptions, $_options); if (null === $this->_links) { $this->_links = array(); } $this->_links[] = array( 'title' => $title, 'url' => $url, 'attribs' => $attribs, 'options' => $options, ); return $this; } protected function _bindListColumns(Qs_ViewController_List $list) { return $this; } public function getTemplate($template, $logNotFound = true) { $_template = Qs_SiteMap::getTemplate($template, $this->_getTemplatePath()); if (false === $_template && $logNotFound) { $message = 'Template "' . $template . '" not found in paths: "' . implode('", "', (array)$this->_getTemplatePath()) . '".'; Qs_Debug::log($message); } return $_template; } protected function _getTemplatePath() { if (null === $this->_templatePath) { $class = get_class($this); $this->_templatePath = substr($class, 0, -4); if (0 === strpos($this->_templatePath, 'App_')) { $this->_templatePath = substr($this->_templatePath, 4); } $this->_templatePath = rtrim($this->_templatePath, '_'); $this->_templatePath = array(str_replace('_', '/', $this->_templatePath), 'ViewController'); } return $this->_templatePath; } protected function _addTemplatePath($path) { $paths = $this->_getTemplatePath(); array_unshift($paths, $path); $paths = array_unique($paths); $this->_setTemplatePath($paths); return $this; } protected function _setTemplatePath($path) { $this->_templatePath = $path; return $this; } protected function _getPage4SaveMessage() { $backUrl = $this->_getBackUrl(); if (empty($backUrl)) { return Qs_Constant::get('CURRENT_PAGE'); } $backUrl = str_replace(Qs_Constant::get('BASE_URL_LANGUAGE') . '/', '', $backUrl); $aBackUrl = preg_split('/\?/', $backUrl); return $aBackUrl[0]; } protected function _getBaseFormOptions() { $options = array( 'method' => 'post', 'action' => $this->url(), 'attribs' => array( 'id' => strtolower($this->getApplicationName()) . '-form', 'class' => strtolower($this->getApplicationName()) . '_form', ) ); return $options; } protected function _getBaseForm() { $form = new Qs_Form($this->_getBaseFormOptions()); if ($this->getIdItem()) { $form->addElement('hidden', '__idItem', array('value' => $this->getIdItem())); } return $form; } protected function _bindFormButtons(Qs_Form $form) { $form->addElement('submit', 'btnSubmit', array( 'label' => 'Save', 'attribs' => array('class' => 'btn'), 'decorators' => array('ViewHelper') ) ); $form->btnSubmit->getDecorator('ViewHelper')->setAdditionalHtmlAfterElement('  '); $btnCancel = new Zend_Form_Element_Button('btnCancel', array( 'label' => 'Cancel', 'attribs' => array( 'class' => 'btn', 'onclick' => "document.location = '{$form->getCancelUrl()}';" ), 'decorators' => array('ViewHelper') ) ); $form->addElement($btnCancel); $decorators = array('FormElements'); $decorators[] = array('decorator' => 'HtmlTag', 'options' => array('tag' => 'div')); $decorators[] = 'Fieldset'; $decorators[] = 'DtDdWrapper'; $form->addDisplayGroup(array('btnSubmit', 'btnCancel'), 'submitGroup', array('decorators' => $decorators)); return $this; } protected function _initFromForm(Qs_Form $form) { $data = $form->getValues(); $this->dataObj->initFromForm($data); return $this; } protected function _getNewForm() { $form = $this->_getBaseForm(); $this->_bindFormFields($form); $this->_bindFormButtons($form); $form->addElement('hidden', 'action', array('value' => 'insert')); return $form; } protected function _getEditForm() { $form = $this->_getBaseForm(); $this->_bindFormFields($form); $this->_bindFormButtons($form); $form->addElement('hidden', 'action', array('value' => 'update')); if (null !== ($primaryKey = $this->getPrimaryKey())) { if (is_array($primaryKey) && !empty($primaryKey)) { foreach ($primaryKey as $name => $value) { $form->addElement('hidden', $name, array('value' => $value)); } } } return $form; } protected function _doNew() { $form = $this->_getNewForm(); $form->setDefaults($this->getFormDefaults()); $this->_renderMainForm($form); return $this; } protected function _doEdit() { $form = $this->_getEditForm(); $this->dataObj->initData(); $data = $this->_getData4Form(); $form->setDefaults($data); $this->_renderMainForm($form); return $this; } public function getFormDefaults($field = false) { return Qs_Array::get($this->_formDefaults, $field); } public function setFormDefaults($field, $value = null) { if (is_array($field)) { $this->_formDefaults = $field; } else if (is_string($field)) { Qs_Array::set($this->_formDefaults, $field, $value); } else { throw new Qs_ViewController_Exception('Invalid argument type'); } return $this; } protected function _getData4Form() { return $this->dataObj->getData(); } protected function _postInsert() { return $this; } protected function _postDelete() { return $this; } protected function _doInsert() { $form = $this->_getNewForm(); if ($form->validate()) { $this->_initFromForm($form); if (($e = $this->dataObj->insert()) instanceof Exception) { if ($e instanceof Qs_Db_Obj_Exception) { $this->_setBackError($e->getMessage()); } else { Qs_Debug::processException($e); } } else { $this->_postInsert(); $this->_setBackMessage(Qs_ViewController::MSG_ADDED); } $this->_doBack(); } else { $this->_renderMainForm($form); } return $this; } protected function _doInsertAjax() { $form = $this->_getNewForm(); $this->_validateAjax($form); } protected function _formGetTitles($form, $errors, $prefix = '') { $titles = array(); foreach ($errors as $name => $errors) { if (!isset($form->$name)) { continue; } if (is_array(current($errors))) { if ($form->$name instanceof Zend_Form) { $prefix .= ((empty($prefix)) ? '' : '. ' ) . $form->$name->getLegend(); $titles[$name] = $this->_formGetTitles($form->$name, $errors, $prefix); } } else { if ($form->$name instanceof Zend_Form_Element) { $titles[$name] = $prefix . ((empty($prefix)) ? '' : '. ' ) . $form->$name->getLabel(); } } } return $titles; } protected function _getValidateResult($form, $method = 'validate') { $result = array(); if (false === ($result['isValid'] = $form->{$method}())) { $result['errors'] = $form->getMessages(null, true); $result['elements'] = array(); foreach ($form->getElements() as $name => $element) { if (isset($result['errors'][$name])) { $result['elements'][$name] = $element->getLabel(); if ($element instanceof Zend_Form_Element_Captcha) { $captcha = $element->getCaptcha(); $result['captcha'][$name] = array( 'id' => $captcha->generate(), 'src' => $captcha->getImgUrl() . $captcha->getId() . $captcha->getSuffix() ); if ($captcha->getRefreshUrl()) { $result['captcha'][$name]['href'] = $captcha->getRefreshUrl() . '?id=' . $captcha->getId(); } } } } } return $result; } protected function _getValidateAjaxResult($form) { return $this->_getValidateResult($form, 'validateAjax'); } protected function _validateAjax($form) { $result = $this->_getValidateAjaxResult($form); $this->_displayJson($result); } protected function _postUpdate() { return $this; } protected function _postUpdateOrder() { return $this; } protected function _postChangeOption() { return $this; } protected function _doUpdateAjax() { $form = $this->_getEditForm(); $this->_validateAjax($form); } protected function _doUpdate() { $form = $this->_getEditForm(); if ($form->validate()) { $this->_initFromForm($form); if (($e = $this->dataObj->update()) instanceof Exception ) { if ($e instanceof Qs_Db_Obj_Exception) { $this->_setBackError($e->getMessage()); } else { Qs_Debug::processException($e); } } else { $this->_postUpdate(); $this->_setBackMessage(Qs_ViewController::MSG_UPDATED); } $this->_doBack(); } else { $this->_renderMainForm($form); } return $this; } protected function _setDisableTranslator($flag) { $this->_translatorDisabled = (bool) $flag; return $this; } protected function _translatorIsDisabled() { return $this->_translatorDisabled; } protected function _getTranslator() { if ($this->_translatorIsDisabled()) { return null; } if (null === $this->_translator) { return Qs_ViewController::getDefaultTranslator(); } return $this->_translator; } public static function getDefaultTranslator() { if (null === self::$_translatorDefault) { if (Zend_Registry::isRegistered('Zend_Translate')) { $translator = Zend_Registry::get('Zend_Translate'); if ($translator instanceof Zend_Translate_Adapter) { return $translator; } elseif ($translator instanceof Zend_Translate) { return $translator->getAdapter(); } } } return self::$_translatorDefault; } protected function _renderMessage($template) { foreach ($this->_messageVariables as $placeholder => $property) { $template = str_replace("%{$placeholder}%", (string) $this->{$property}, $template); } return $template; } protected function _createMessage($messageKey) { if (!isset($this->_messageTemplates[$messageKey])) { return null; } return $this->_renderMessage($this->_messageTemplates[$messageKey]); } protected function _createHeader($headerKey) { if (!isset($this->_pageHeaderTemplates[$headerKey])) { return null; } return $this->_renderMessage($this->_pageHeaderTemplates[$headerKey]); } protected function _setBackMessage($messageKey) { if (array_key_exists($messageKey, $this->_messageTemplates)) { $text = $this->_createMessage($messageKey); } else { $text = $messageKey; } if ($translator = $this->_getTranslator()) { $text = $translator->translate($text); } $sessionName = $this->_getPage4SaveMessage(); $session = new Qs_Session_Namespace($sessionName); if (isset($session->message)) { if (is_array($session->message)) { $session->message[] = $text; } else { $session->message = array($session->message, $text); } } else { $session->message = $text; } $session->setExpirationHops(1, 'message'); return $this; } protected function _setBackError($messageKey) { if (array_key_exists($messageKey, $this->_messageTemplates)) { $text = $this->_createMessage($messageKey); } else { $text = $messageKey; } if ($translator = $this->_getTranslator()) { $text = $translator->translate($text); } $sessionName = $this->_getPage4SaveMessage(); $session = new Qs_Session_Namespace($sessionName); if (isset($session->error)) { if (is_array($session->error)) { $session->error[] = $text; } else { $session->error = array($session->error, $text); } } else { $session->error = $text; } $session->setExpirationHops(1, 'error'); return $this; } protected function _renderForm($form, $template = null, $item = array()) { if (null !== $template && false !== ($item['tpl'] = $this->getTemplate($template, false))) { $form->initRender(); $form->removeContainers(); $item['form'] = $form; } else { $item['tpl'] = $this->getTemplate('text.tpl'); $item['text'] = $form->render(); } $this->_addItem($item); return $this; } protected function _renderMainForm(Qs_Form $form) { $this->_renderForm($form, 'form.tpl'); return $this; } protected function _renderReorderForm(Qs_Form $form) { $this->_renderForm($form, 'reorder-form.tpl'); return $this; } protected function _getReorderForm() { $form = $this->_getBaseForm(); $form->setAjaxValidation(false); $options = $this->dataObj->getReorderOptions($this->getReorderValueColumn(), $this->getReorderTitleColumn()); $form->addElement('select', 'order', array( 'helper' => 'formOrderSelect', 'class' => 'order_select', 'size' => 10, 'multiple' => true, 'multiOptions' => $options ) ); $form->addElement('hidden', 'action', array('value' => 'updateOrder')); Qs_ViewController::_bindFormButtons($form); return $form; } protected function _doReorder() { $form = $this->_getReorderForm(); $this->_renderReorderForm($form); return $this; } protected function _doUpdateOrder() { $form = $this->_getReorderForm(); $form->validate(); $order = $form->getValue('order'); $this->dataObj->updateOrder($order); $this->_postUpdateOrder(); $this->_setBackMessage(Qs_ViewController::MSG_REORDERED); $this->_doBack(); } protected function _doChangeOption() { $name = Qs_Request::getRequestValue('name'); if (empty($name)) { throw new Qs_ViewController_Exception('Option name is required'); } $this->dataObj->changeEnumOption($name); $this->_postChangeOption(); $this->_doBack(); } public function setReorderTitleColumn($name) { $this->_reorderTitleColumn = $name; return $this; } public function getReorderTitleColumn() { if (null === $this->_reorderTitleColumn) { $this->setReorderTitleColumn('title'); } return $this->_reorderTitleColumn; } public function setReorderValueColumn($name) { $this->_reorderValueColumn = $name; return $this; } public function getReorderValueColumn() { if (null === $this->_reorderValueColumn) { $this->setReorderValueColumn('id'); } return $this->_reorderValueColumn; } protected function _getIdContainer() { return str_replace('_', '-', $this->getApplicationName()); } protected function _addLinksItem($idContainer = null, array $links = null) { if (null === $idContainer) { $idContainer = $this->_getIdContainer(); } if (null === $links) { $links = $this->_getLinks(); } $item = array(); $item['idContainer'] = $idContainer; $item['links'] = $links; $item['tpl'] = $this->getTemplate('list-links.tpl'); $this->_addItem($item); return $this; } protected function _addItem($item, $groupName = null) { $item['templatePath'] = $this->_getTemplatePath(); $this->doc->addItem($item, $groupName); return $this; } protected function _addListItem($list = null, $template = 'list.tpl') { if (null == $list) { $list = $this->list; } $this->_bindListColumns($list); $item = $list->toArray(); if (array_key_exists('paginator', $item)) { if ($item['paginator']['last'] < $list->getPageNumber() && ($item['paginator']['pageCount'] != 0 || $list->getPageNumber() > 1) ) { $redirectUrl = str_replace($list->getPaginatorPagePlaceholder(), 1, $list->getPaginatorUrl()); $this->redirect($redirectUrl, 307); } } $item['tpl'] = $this->getTemplate($template); $this->_addItem($item); return $this; } protected function _addFilterItem() { if ($this->dataObj->hasFilter() && $this->_hasFilter) { $form = $this->_getFilterForm(); if ($form->validate()) { $filter = $form->getValues(); $this->list->addUrlVariables(array_keys($filter)); $this->dataObj->addFilter($filter); } $this->_renderForm($form, Qs_Constant::get(array($this, 'FILTER_TEMPLATE'))); } return $this; } protected function _doList() { $this->_saveBackUrl(); $this->_addFilterItem(); $this->_addLinksItem(); $this->_addListItem(); return $this; } protected function _getFilterForm() { $form = $this->_getBaseForm(); $form->setMethod('get') ->setAttrib('id', strtolower($this->getApplicationName()) . '-filter-form') ->setAttrib('class', 'filter_form ' . strtolower($this->getApplicationName()) . '_filter_form') ->setAjaxValidation(false); $this->_bindFilterFields($form); $this->_bindFilterButtons($form); return $form; } protected function _bindFilterFields(Qs_Form $form) { $form->addElement( 'text', 'query', array( 'label' => 'Search', 'size' => 50, 'filters' => array(new Zend_Filter_StringTrim()) ) ); $placeholder = '%fields%'; $description = $this->_filterQueryDescriptionFormat; if (false !== strpos($description, $placeholder)) { $fields = $this->_getFilterFields(); if (!empty($this->_filterQueryDescriptionFormat) && !empty($fields)) { if (is_array($fields)) { $fields = '"' . Qs_Array::implodeLast('", "', '" or "', $fields) . '"'; } $description = str_replace($placeholder, $fields, $this->_filterQueryDescriptionFormat); } } $form->query->setDescription($description); return $this; } protected function _getFilterFields() { if (null === $this->_filterFields) { $this->_filterFields = array(); $filterFields = Qs_Array::collapse($this->dataObj->getFilterFields()); $filterFields = array_unique(array_values($filterFields)); foreach (array_keys($filterFields) as $index) { if (is_scalar($filterFields[$index])) { $this->_filterFields[] = ucfirst( preg_replace('/([a-z])([A-Z])/', '$1 $2', $filterFields[$index]) ); } } } return $this->_filterFields; } protected function _bindFilterButtons($form) { $form->addElement( 'submit', 'btnSearch', array( 'label' => 'Search', 'ignore' => true, 'attribs' => array('class' => 'btn'), 'decorators' => array('ViewHelper') ) ); $form->addElement( 'button', 'btnCancel', array( 'label' => 'Cancel', 'ignore' => true, 'decorators' => array('ViewHelper'), 'attribs' => array( 'class' => 'btn', 'onclick' => "document.location='" . BASE_URL . '/' . CURRENT_PAGE . "';", ) ) ); $decorators = array('FormElements'); $decorators[] = array('decorator' => 'HtmlTag', 'options' => array('tag' => 'dl')); $decorators[] = 'Fieldset'; $form->addDisplayGroup(array('btnSearch', 'btnCancel'), 'submitGroup', array('decorators' => $decorators)); return $this; } protected function _doDelete() { if (($e = $this->dataObj->delete()) instanceof Exception) { if ($e instanceof Qs_Db_Obj_Exception) { $this->_setBackError($e->getMessage()); } else { Qs_Debug::processException($e); } } else { $this->_postDelete(); $this->_setBackMessage(Qs_ViewController::MSG_DELETED); } $this->_doBack(); } protected function _doCancel() { $this->_doBack(); } protected function _doBack() { $backUrl = $this->_getBackUrl(); if (empty($backUrl)) { Qs_Http::redirect($this->url()); } else { $this->_clearBackUrl(); Qs_Http::redirect($backUrl); } } protected function _clearBackUrl($sessionName = null) { if (is_null($sessionName)){ $sessionName = CURRENT_PAGE; } $session = new Qs_Session_Namespace($sessionName); $session->backUrl = null; return $this; } public function getPrimaryKey() { if (null === $this->_primaryKey) { $this->_getDataObj(); // init Data Obj if (null !== $this->_dataObj) { $this->_initPrimaryKey(); } } return $this->_primaryKey; } public function setPrimaryKey($key) { $this->_primaryKey = $key; return $this; } protected function _getTranslationSection() { if (null === $this->_translationSection) { $this->_translationSection = $this->getApplicationName(); } return $this->_translationSection; } public function getApplicationName() { if (null === $this->_applicationName) { $this->_applicationName = get_class($this); $this->_applicationName = substr($this->_applicationName, 4, strrpos($this->_applicationName, '_') - 4); $this->_applicationName = strtolower($this->_applicationName); } return $this->_applicationName; } public function url($options = array()) { $parts = array(); $parts[] = Qs_Constant::get('BASE_URL_LANGUAGE') . '/'; $parts[] = Qs_Constant::get('CURRENT_PAGE'); if (!empty($options)) { $parts[] = '?' . http_build_query($options); } return implode($parts); } public function redirect($url, $code = 302) { if (is_array($url)) { $url = $this->url($url); } Qs_Http::redirect($url, $code); } public function finalUrl($options = array()) { $parts = array(); $parts[] = Qs_Constant::get('BASE_URL_LANGUAGE') . '/'; $parts[] = Qs_Constant::get('CURRENT_PAGE_FINAL'); if (!empty($options)) { $parts[] = '?' . http_build_query($options); } return implode($parts); } public function setDefaultOrderBy($order) { $this->_defaultOrderBy = $order; return $this; } public function setAction($action) { if (!in_array($action, $this->_actions)) { throw new Qs_ViewController_Exception('Unknown action "' . $action . '"'); } $this->_action = $action; return $this; } public function setDefaultAction($action) { if (!in_array($action, $this->_actions)) { throw new Qs_ViewController_Exception('Unknown action "' . $action . '"'); } $this->_defaultAction = $action; return $this; } protected function _getData($field = false, $default = null, $method = 'POST') { $result = null; if ($_SERVER['REQUEST_METHOD'] == strtoupper($method)) { if (false === $field) { $result = $_POST; } else { $result = Qs_Request::getPostValue($field); } } else { $key = $this->dataObj->getPrimaryKey(); if (!empty($key)) { $result = $this->dataObj->getData($field); } } if (null === $result) { $result = $this->getFormDefaults($field); } return (null === $result) ? $default : $result; } public function renderConfigForm($form) { $form->setDisabledInitRender(true); $form->setAttrib('id', 'view-' . $form->getId()); $form->setDecorators(array( array('decorator' => 'RenderTable', 'options' => array('class' => 'block_config')), 'Fieldset', $form->getDecorator('DtDdWrapper') )); foreach ($form as $item) { if ($item instanceof Qs_Form) { $item->setDecorators(array( array('decorator' => 'RenderTable', 'options' => array('class' => 'block_config')), //'Fieldset', $item->getDecorator('DtDdWrapper') )); } } return $form->render(); } public function hasConfigForm() { return ($this->_hasConfigForm && method_exists($this, 'fillConfigForm')); } protected function _sendMail($options) { $requiredOptions = array('from', 'to', 'subject', 'body'); $diff = array_diff_key(array_combine($requiredOptions, array_fill(0, count($requiredOptions), '')), $options); if (!empty($diff)) { throw new Qs_ViewController_Exception('Qs_ViewController::_sendMail. Missed required options: ' . implode(', ', array_keys($diff))); } extract($options); $mail = new Qs_Mail(); $mail->setFrom($from); $mail->setSubject($subject); Qs_Mail::cutImageBaseUrl($body, BASE_URL); $mail->setHtml($body, null, WWW_PATH); $mail->addTo($to); return $mail->send(); } }