'doList', 'cancel' => 'doBack', 'new' => 'doNew', 'insert' => 'doInsert', 'edit' => 'doEdit', 'update' => 'doUpdate', 'del' => 'doDelete', 'chopt' => 'doChangeOption', 'reorder' => 'doReorderForm', 'reorder_save' => 'doReorderSave', 'move_up' => 'doMoveUp', 'move_down' => 'doMoveDown', ); var $filterName; var $compressJSON = null; // null -> auto detect var $fldReorderTitle = 'title'; var $reorderListSize = 15; var $reorderTitleSize = 80; function DB_Grid(&$Doc, &$DBObj) { $this->Doc = &$Doc; $this->DBObj = &$DBObj; } function exec() { $this->initAction(); $this->callAction(); } protected function initAction() { if (empty($this->action)) { if (isset($_REQUEST['action']) && !empty($_REQUEST['action'])) { $this->action= $_REQUEST['action']; } else { $this->action = $this->defaultAction; } } if (key_exists($this->action, $this->actions)){ $this->actionMethod = $this->actions[$this->action]; } else { $this->action = key($this->actions); $this->actionMethod = current($this->actions); } if ($this->isXmlHttpRequest()) { $this->actionMethod .= 'Ajax'; } } protected function callAction() { if (method_exists($this, $this->actionMethod)) { $this->{$this->actionMethod}(); } } 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)))); return $form; } function setFormTitle(&$form, $title) { if (isset($this->opt['form_title_off'])) { return false; } $form->setTitle($title); } function _getNewForm() { $form = $this->_getBaseForm(); $form = $this->_bindFormFields($form); $this->setFormTitle($form, 'Add '.$this->DBObj->itemName); $form->addElement('hidden', 'action', 'insert') ; return $form; } function doInsert() { $form = $this->_getNewForm(); if ($form->validate()){ $this->DBObj->initFromForm($form ); $this->DBObj->insert(); Session::setData($this->_getPage4SaveMessage(), 'msg', $this->DBObj->itemName.' has been added'); $this->doBack(); }else { $this->renderForm($form); } return true; } function doUpdate() { $form = $this->_getEditForm(); if ($form->validate()){ $this->DBObj->initFromForm($form); $this->DBObj->update(); Session::setData($this->_getPage4SaveMessage(), 'msg', $this->DBObj->itemName.' has been updated'); $this->doBack(); }else { $this->renderForm($form); } return true; } function doDelete() { Session::setData($this->_getPage4SaveMessage(), 'msg', $this->DBObj->itemName.' has been 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(); $form->setDefaults($this->DBObj->getData()); $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() { $link = BASE_URL . '/' . CURR_PAGE; $link_list = array(); if ($this->DBObj->itemsName) { $link_list[] = array('title' => 'Manage ' . $this->DBObj->itemsName . ''); } $link_list[] = array('title' => 'Add New ' . $this->DBObj->itemName, 'link' => $link . '?action=new'); $addLink = array( 'tpl' => 'center_link.tpl', 'link_list' => $link_list, ); $this->Doc->addContent($addLink); return true; } function _saveBackUrl() { $this->setBackUrl(Constant::get('BASE_URL').'/'.CURR_PAGE.'?'.$_SERVER['QUERY_STRING']); } function setBackUrl($url, $sess_name = null) { if (is_null($sess_name)){ $sess_name = CURR_PAGE; } Session::setData($sess_name, '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(); if (empty($backUrl)){ skHTTP::redirect(Constant::get('BASE_URL').'/'.CURR_PAGE); }else { $this->clearBackUrl(); skHTTP::redirect($backUrl); } } 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; } } 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 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', ((isset($this->DBObj->itemsName))?$this->DBObj->itemsName:$this->DBObj->itemName).' has been 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() { $backUrl = $this->getBackUrl(); if (empty($backUrl)) { return CURR_PAGE; } else { $backUrl = str_replace(Constant::get('BASE_URL') . '/', '', $backUrl); $aBackUrl = split('\?', $backUrl); return $aBackUrl[0]; } } function doMoveUp() { $this->DBObj->move('up'); $this->doBack(); return true; } function doMoveDown() { $this->DBObj->move('down'); $this->doBack(); return true; } }