'text.tpl' , 'DB_LIST_CELL_TEXT_CENTER' => 'text_center.tpl' , 'DB_LIST_CELL_TEXT_YES_NO' => 'text_yes_no.tpl' , 'DB_LIST_CELL_EMAIL' => 'email.tpl' , 'DB_LIST_CELL_EDIT' => 'edit.tpl' , 'DB_LIST_CELL_VIEW' => 'view.tpl' , 'DB_LIST_CELL_DEL' => 'del.tpl' , 'DB_LIST_CELL_EDIT_DEL' => 'edit_del.tpl' , 'DB_LIST_CELL_EDIT_DEL_REORDER' => 'edit_del_reorder.tpl' , 'DB_LIST_CELL_VIEW_DEL' => 'view_del.tpl' , 'DB_LIST_CELL_IMG' => 'img.tpl' , 'DB_LIST_CELL_FILE' => 'file.tpl' , 'DB_LIST_CELL_LINK' => 'link.tpl' , 'DB_LIST_CELL_LINK_IMG' => 'link_img.tpl', 'DB_LIST_CELL_MONEY' => 'money.tpl', 'DB_LIST_CELL_DATE' => 'date.tpl', 'DB_LIST_CELL_SORTER' => 'sorter.tpl', 'DB_LIST_CELL_LINK_ENUM' => 'link_enum.tpl', ); foreach ($tpls as $key => $file) { define($key, SiteMap::getPath2ClassTpl('list', null, 'cells/'.$file)); } return true; } DB_List_init_tpls(); class DB_List { var $urlVarNames = array('ipp', 'query'); var $_column = array(); var $_columnSort = array(); var $functionalFields = array(); var $name; var $tpl; var $def_order_by = NULL; var $order_by = NULL; var $itemName = NULL; var $sort_link = NULL; var $pageNum = NULL; var $ipp = 20; var $Doc; var $DBObj; function DB_List(&$Doc, &$DBObj, $functionalFields = array()) { $this->Doc = &$Doc; $this->DBObj = &$DBObj; $this->functionalFields = $functionalFields; } function bindCols() { if (count($this->_column) == 0){ $this->_column = array(); $count = 1; foreach ($this->DBObj->getFieldList() as $field) { $fieldName = $field['field']; $fieldTitle = str_replace(array('-', '_'), ' ', $fieldName); if (!in_array($fieldName, $this->functionalFields )){ $this->insertColLast($fieldName, array('title' => ucwords($fieldTitle), 'order_by' => $fieldName, 'width' => '150', 'tpl' => DB_LIST_CELL_TEXT, 'default_text' => "empty $fieldTitle", 'params' => array($fieldName), )); } } $this->insertColLast('options', array('title' => 'Options', 'width' => '150', 'tpl' => DB_LIST_CELL_EDIT_DEL, 'edit_link' => Constant::get('BASE_URL').'/'.CURR_PAGE.'?action=edit&id=', 'del_link' => Constant::get('BASE_URL').'/'.CURR_PAGE.'?action=del&id=', 'params' => array('id'), )); } return true; } function bind() { $this->bindCols(); if (is_null($this->itemName)) { $this->itemName = $this->DBObj->itemName; } if (is_null($this->order_by)) { $this->order_by = $this->getOrderBy($_GET['order_by']); } if (is_null($this->sort_link)) { $this->sort_link = $this->_getBaseLink(); } if (is_null($this->pageNum)) { $this->pageNum = intval($_GET['page']); } if (isset($_GET['ipp']) && $_GET['ipp'] > 0 ){ $this->ipp = intval($_GET['ipp']); } return true; } function getCol($name) { return $this->_column[$name]; } function setCol($name, $params) { if (!key_exists($name, $this->_column)) { return false; } $this->_column[$name] = $params; } function insertColFirst($name, $params) { if (key_exists($name, $this->_column)) { $this->removeCol($name); } $this->_column[$name] = $params; $this->_columnSort[$name] = $this->_columnSort ? (min($this->_columnSort) - 1) : 1; } function insertColBefore($name, $params, $beforeName) { if (key_exists($name, $this->_column)) { $this->removeCol($name); } $this->_column[$name] = $params; $moveFlag = false; asort($this->_columnSort); foreach ($this->_columnSort as $key => $sorter) { if ($key == $beforeName) { $moveFlag = true; $new_arr[$name] = $sorter; } if ($moveFlag == true) { if ($key != $name) { $new_arr[$key] = $sorter + 1; } } else { $new_arr[$key] = $sorter; } } asort($new_arr); $this->_columnSort = $new_arr; } function insertColAfter($name, $params, $afterName) { if (key_exists($name, $this->_column)) { $this->removeCol($name); } $this->_column[$name] = $params; $moveFlag = false; asort($this->_columnSort); foreach ($this->_columnSort as $key => $sorter) { if ($key == $afterName) { $moveFlag = true; $new_arr[$name] = $sorter + 1; } if ($moveFlag == true) { if ($key != $afterName) { $new_arr[$key] = $sorter + 1; } else { $new_arr[$key] = $sorter; } } else { $new_arr[$key] = $sorter; } } asort($new_arr); $this->_columnSort = $new_arr; } function insertColLast($name, $params) { if (key_exists($name, $this->_column)) { $this->removeCol($name); } $this->_column[$name] = $params; $this->_columnSort[$name] = $this->_columnSort ? (max((array)$this->_columnSort) + 1) : 1; } function removeCol($name) { unset($this->_column[$name]); unset($this->_columnSort[$name]); } function _getBaseLink() { $link = Constant::get('BASE_URL').'/'.CURR_PAGE.'?'; foreach ($this->urlVarNames as $name) { $qs = array(); if (isset($_REQUEST[$name]) ){ $qs[$name] = $_REQUEST[$name]; } if (!empty($qs)) { $link .= http_build_query($qs) . '&'; } } return $link; } function getOrderBy($order_by) { /*ßêøî $order_by íåâàë³äíå òî ßêùî ïóñòå çà÷åííÿ $this->def_order_by òî ñîðòóºìî ïî ïåðø³é êîëîíö³ ïî ÿê³é ìîæíà ñîðòóâàòè */ list($field, $order) = explode(' ', $order_by); $validField = false; $def_order_by = $this->def_order_by; foreach ($this->_column as $column) { if ($field == $column['order_by'] && !empty($field) ){ // ïåðåâ³ðêà íà âàë³äí³ñòü $validField = true; break; } if ( empty($def_order_by) && !empty($column['order_by']) ){ $def_order_by = $column['order_by']; } } if (!$validField){ return $def_order_by; }else { if ('DESC' != $order){ $order = ''; }else { $order = 'DESC'; } return trim("$field $order"); } } function _getList() { $opt = array( 'order_by' => $this->order_by, 'start' => $this->pageNum * $this->ipp, 'ipp' => $this->ipp, ); $list = $this->DBObj->getList4Grid($opt); if (($this->pageNum + 1) > $list['num_pages'] && $list['num_pages'] > 0 ){ $this->pageNum = $list['num_pages'] - 1; $opt['start'] = $this->pageNum * $this->ipp; $list = $this->DBObj->getList4Grid($opt); } return $list; } function toArray() { $this->bind(); $list = $this->_getList(); $item = get_object_vars($this); $item['tpl'] = $this->getTpl(); $item['column'] = $this->getColumns(); unset($item['Doc']); unset($item['DBObj']); $item['pager_link'] = $this->_getBaseLink().'order_by='.urlencode($this->order_by).'&'; $item['num_rows'] = $list['num_rows']; $item['num_pages'] = $list['num_pages']; $item['items'] = $list['list']; return $item; } function initPlugins() { $this->Doc->tplPlugins[] = 'class/DB/List/plugins'; } function exec() { $item = $this->toArray(); $this->initPlugins(); $this->Doc->addContent($item); return true; } function getTpl() { return SiteMap::getPath2ClassTpl('list', $this->tpl, 'list.tpl', $this->name); } function getColumns() { $sortedColumn = array(); asort($this->_columnSort); foreach ($this->_columnSort as $name => $sorter) { $sortedColumn[$name] = $this->_column[$name]; } return $sortedColumn; } function updateCol($field, $key, $value) { $idFld =& $this->getCol($field); $idFld[$key] = $value; $this->setCol($field, $idFld); } } ?>