'doList', 'cancel' => 'doBack', 'new' => 'doNew', 'insert' => 'doInsert', 'edit' => 'doEdit', 'update' => 'doUpdate', 'del' => 'doDelete', 'delete' => 'doDelete', 'chopt' => 'doChangeOption', 'reorder' => 'doReorderForm', 'reorder_save' => 'doReorderSave', ); var $filterName; var $compressJSON = null; // null -> auto detect var $fldReorderTitle = 'title'; var $reorderListSize = 15; var $reorderTitleSize = 80; var $concat_form_title = true; var $_yesNoFilterOptions = array('' => 'Yes or No', 'y' => 'Yes', 'n' => 'No'); function DB_Grid(&$Doc, &$DBObj) { $this->Doc = &$Doc; $this->DBObj = &$DBObj; } function setAction($action) { $this->_action = $action; return $this; } function setDefaultAction($action) { $this->_defaultAction = $action; return $this; } function exec() { $this->_initAction(); $this->_callAction(); } protected function _initAction() { if (empty($this->_action)) { if (isset($_REQUEST['action']) && !empty($_REQUEST['action'])) { $this->_action = $_REQUEST['action']; } if (empty($this->_action) || !array_key_exists($this->_action, $this->actions)) { $this->_action = $this->_defaultAction; } } if (!array_key_exists($this->_action, $this->actions)){ $this->_action = reset(array_keys($this->actions)); } $this->_actionMethod = $this->actions[$this->_action]; if (Qs_Request::isXmlHttpRequest()) { $this->_actionMethod .= 'Ajax'; } return $this; } protected function _callAction() { if (method_exists($this, $this->_actionMethod)) { $this->{$this->_actionMethod}(); } else if (Qs_Request::isXmlHttpRequest()) { return false; } else { throw new Zend_Exception('Action method not found: ' . get_class($this) . '->' . $this->_actionMethod); } return $this; } function exec_() { $action = $_REQUEST['action']; if ( !key_exists($action, $this->actions) ) { $actionNames = array_keys($this->actions); $action = $actionNames[0]; } $function = $this->actions[$action]; if ($this->isXmlHttpRequest()) { $function .= 'Ajax'; } if (method_exists($this, $function)) { $this->$function(); } else { exit; } } function getFilterForm() { $form = $this->_getBaseForm('get'); $form->tpl = SiteMap::getPath2ClassTpl('grid', null, 'filter.tpl', $this->filterName); $form->setRendType(FORM_RENDERER_ARRAY_SMARTY); $form->addElement('text', 'query'); $form->addElement('submit', 'submit_btn', 'Search', array( 'class' => 'btn') ); $form->addElement('button', 'cancel_btn', 'Cancel', array( 'class' => 'btn' , 'onclick' => "document.location.href='".Constant::get('BASE_URL').'/'.CURR_PAGE."'" )); return $form; } function _getBaseForm($method = 'post') { require_once 'class/Form/Form.class.php'; $form = new Form($this->Doc, 'form_'.strtolower(get_class($this->DBObj)), $method, '', '_self', array('class'=>'form form_'.strtolower(get_class($this->DBObj)))); if (null !== ($sessionName = Qs_Request::getRequestValue('sessionName'))) { $form->addElement('hidden', 'sessionName', $sessionName); $url = $this->getBackUrl(Qs_Request::getRequestValue('sessionName')); if (!empty($url)) { $form->cancelUrl = $url; } } return $form; } /** * @param $form Form * @param $title * @return bool */ function setFormTitle(&$form, $title) { if (isset($this->opt['form_title_off'])) { return false; } if ($this->concat_form_title) { $this->Doc->content['DOC']['title'] .= ' > '.$title; return false; } $form->setTitle($title); return true; } function _getNewForm() { $form = $this->_getBaseForm(); $form = $this->_bindFormFields($form); $this->setFormTitle($form, 'Add '.$this->DBObj->itemName); $form->addElement('hidden', 'action', 'insert') ; return $form; } function setBackMessage($message, $sessionName = null) { Session::setData(null === $sessionName ? $this->_getPage4SaveMessage() : $sessionName, 'msg', $message); } function doInsert() { $form = $this->_getNewForm(); if ($form->validate()) { $this->DBObj->initFromForm($form); $this->DBObj->insert(); $this->postInsert(); Session::setData($this->_getPage4SaveMessage(), 'msg', $this->DBObj->itemName.' added'); $this->doBack(); } else { $this->renderForm($form); } return true; } protected function _setBackMessage($message) { Session::setData($this->_getPage4SaveMessage(), 'msg', $message); } protected function _setBackError($error) { Session::setData($this->_getPage4SaveMessage(), 'msg_error', $error); } function postInsert() {} function doUpdate() { $form = $this->_getEditForm(); if ($form->validate()){ $this->DBObj->initFromForm($form); $this->DBObj->update(); $this->postUpdate(); Session::setData($this->_getPage4SaveMessage(), 'msg', $this->DBObj->itemName.' updated'); $this->doBack(); }else { $this->renderForm($form); } return true; } function postUpdate() {} function doDelete() { Session::setData($this->_getPage4SaveMessage(), 'msg', $this->DBObj->itemName.' deleted'); $this->DBObj->delete(); $this->doBack(); return true; } function doNew() { $form = $this->_getNewForm(); $this->renderForm($form); } function renderForm(&$form) { $form->exec(); } function _getEditForm() { $form = $this->_getBaseForm(); $form = $this->_bindFormFields($form); $this->setFormTitle($form, 'Edit '.$this->DBObj->itemName); $form->action = CURR_PAGE.'?action=update'; $form->addElement('hidden', 'action', 'update') ; $form->addElement('hidden', 'id', $this->DBObj->getData('id')) ; return $form; } function doEdit() { $this->DBObj->initFromDB(); $form = $this->_getEditForm(); $this->renderForm($form); } function _bindFormFields($form) { foreach ($this->DBObj->getFieldList() as $field) { $name = $field['field']; if (!in_array($name, $this->functionalFields )){ $form->addElement('text', $name, str_replace('_', ' ', ucwords($name) ), array('value' => $this->DBObj->getData($name)) ); } } return $form; } function addLink() { $item = array( 'tpl' => 'center_link.tpl', 'link' => Constant::get('BASE_URL') . '/' . CURR_PAGE . '?action=new', 'title' => 'Add new '.$this->DBObj->itemName ); $this->Doc->addContent($item); } function _saveBackUrl() { $this->setBackUrl(Constant::get('BASE_URL').'/'.CURR_PAGE_FULL.'?'.$_SERVER['QUERY_STRING']); } function setBackUrl($url, $sessionName = null) { if (is_null($sessionName)){ $sessionName = CURR_PAGE; } Session::setData($sessionName, 'query_string', $url); } function getBackUrl($page = '') { $page = (string)$page; if ($page == '') { $page = CURR_PAGE; } return Session::getData($page, 'query_string'); } function clearBackUrl() { return Session::clearData(CURR_PAGE, 'query_string'); } function doBack() { require_once 'class/HTTP.php'; $backUrl = $this->getBackUrl(Qs_Request::getRequestValue('sessionName')); if (empty($backUrl)) { skHTTP::redirect(Constant::get('BASE_URL').'/'.CURR_PAGE_FULL); } else { $this->clearBackUrl(); skHTTP::redirect($backUrl); } } public function fullUrl($options = array(), $isSecure = null) { return $this->url($options, $isSecure, true); } public function url($options = array(), $isSecure = null, $full = false) { $parts = array(); if (null === $isSecure) { $parts[] = REAL_BASE_URL . '/'; } else if (true === $isSecure) { $parts[] = BASE_URL_HTTPS . '/'; } else { $parts[] = BASE_URL_HTTP . '/'; } $parts[] = $full ? CURR_PAGE_FULL : CURR_PAGE; if (!empty($options)) { $parts[] = '?' . http_build_query($options); } return implode('', $parts); } public function redirect($url) { if (is_array($url)) { $url = $this->url($url); } require_once 'class/HTTP.php'; skHTTP::redirect($url); } /** * @param $DB_List DB_List * @return bool */ function _doListBind(&$DB_List) { $DB_List->bind(); return true; } function doList() { $this->_saveBackUrl(); $this->addLink(); require_once('class/DB/List/List.php'); $DB_List = new DB_List($this->Doc, $this->DBObj, $this->functionalFields); $this->_doListBind($DB_List); $DB_List->exec(); return true; } function getHeader($header) { if (empty($header)) { return false; } // Try to get it from the $_SERVER array first $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header)); if (!empty($_SERVER[$temp])) { return $_SERVER[$temp]; } // This seems to be the only way to get the Authorization header on // Apache if (function_exists('apache_request_headers')) { $headers = apache_request_headers(); if (!empty($headers[$header])) { return $headers[$header]; } } return false; } function isXmlHttpRequest() { return ($this->getHeader('X_REQUESTED_WITH') == 'XMLHttpRequest'); } function initAcceptEncoding() { if (is_bool($this->compressJSON)) { return false; } if (strpos($this->getHeader('ACCEPT-ENCODING'), 'gzip') === false) { $this->compressJSON = false; } else { $this->compressJSON = true; } return true; } function displayJSON($data) { $this->initAcceptEncoding(); header('Cache-Control: no-store, no-cache, must-revalidate, private'); header('Pragma: no-cache'); header('Content-Type: application/x-javascript; charset=utf-8'); $json = array2json($data); if ($this->compressJSON) { header('Content-Encoding: gzip'); $json = gzencode($json, 9); } die($json); } function formAddPhoneRule(&$form, $elName) { if (is_array($elName) && !empty($elName)) { foreach($elName as $k=>$v) { $this->formAddPhoneRule($form, $v); } return; } $el = &$form->getElement($elName); $regex = "/^\(\d{3}\)\s*\d{3}\s*-\s*\d{4}(\s.*)?$/"; $form->addRule($elName, $el->getLabel().' is in wrong format', 'regex', $regex); $form->addRule($elName, $el->getLabel().' is in wrong format', 'regex', $regex, 'client'); } function doChangeOption() { $this->DBObj->changeOption(@$_REQUEST['opt']); $this->doBack(); } function doReorderForm() { $form = $this->_getReorderForm(); $this->setFormTitle($form, 'Reorder '.((isset($this->DBObj->itemsName))?$this->DBObj->itemsName:$this->DBObj->itemName)); $form->addElement('hidden', 'action', 'reorder_save'); $form->exec(); } function doReorderSave() { $form=$this->_getReorderForm(); $this->DBObj->reorder($form->getElementValue('order_list')); Session::setData($this->_getPage4SaveMessage(), 'msg', $this->DBObj->itemName.' reordered'); $this->doBack(); } function _getReorderForm() { $form=$this->_getBaseForm(); $list=array(); $list4Grid=$this->DBObj->getList4Grid(array('order_by'=>'sorter')); foreach ($list4Grid['list'] as $val) { $list[$val['id']] = (strlen($val[$this->fldReorderTitle])>$this->reorderTitleSize)?substr($val[$this->fldReorderTitle], 0, $this->reorderTitleSize-3).'...':$val[$this->fldReorderTitle]; } $orderList = Form::createElement('select_edit', 'order_list', false, $list); $orderList->updateAttributes(array('size' => $this->reorderListSize)); $orderList->delAction('add'); $orderList->delAction('del'); $orderList->delAction('edit'); $form->addElement($orderList); $form->addElement('hidden', 'id_parent', $this->DBObj->id_parent); return $form; } function _getPage4SaveMessage() { if (null !== ($sessionName = Qs_Request::getRequestValue('sessionName'))) { return $sessionName; } $backUrl = $this->getBackUrl(); if (empty($backUrl)) return CURR_PAGE; else { $backUrl=str_replace(Constant::get('BASE_URL').'/','',$backUrl); $aBackUrl=split('\?',$backUrl); return $aBackUrl[0]; } } protected function _getData($field = false, $default = null, $method = 'POST') { if ($_SERVER['REQUEST_METHOD'] == strtoupper($method)) { if (false === $field) { return $_POST; } require_once('Qs/Request.php'); return Qs_Request::getPostValue($field, $default); } if (empty($this->DBObj->id)) { return $default; } return $this->DBObj->getData($field); } protected function _createMail(array $options) { require_once('lib/htmlMimeMail/htmlMimeMail.php'); $mail = new htmlMimeMail(); foreach ($options as $name => $value) { $method = 'set' . ucfirst($name); if (method_exists($mail, $method)) { call_user_func(array($mail, $method), $value); } } return $mail; } protected function _sendMail(array $options) { $mail = $this->_createMail($options); foreach ((array)$options['to'] as $email) { $mail->send(array($email)); } } protected function _exportCsv(MDB2_BufferedResult_mysql $statement, $columns, $fileName) { $handle = fopen('php://output', 'w'); header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); header ('Last-Modified: ' . gmdate('D,d M YH:i:s') . ' GMT'); header ('Cache-Control: no-cache, must-revalidate'); header ('Pragma: no-cache'); header ('Content-type: text/comma-separated-values'); header ('Content-Disposition: attachment; filename="' . $fileName . '"'); fputcsv($handle, $columns); $blankRow = array_combine(array_map('strtolower', array_keys($columns)), array_fill(0, count($columns), '')); while ($row = $statement->fetchRow()) { $row = str_replace(array("\n", "\r"), ' ', $row); $row = array_intersect_key($row, $blankRow); $row = array_merge($blankRow, $row); fputcsv($handle, $row); } fclose($handle); exit; } }