'text.tpl', 'DB_LIST_CELL_TEXT_CENTER' => 'text_center.tpl', 'DB_LIST_CELL_TEXT_YES_NO' => 'text_yes_no.tpl', 'DB_LIST_CELL_EDIT' => 'edit.tpl', 'DB_LIST_CELL_DEL' => 'del.tpl', 'DB_LIST_CELL_EDIT_DEL' => 'edit_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_EMAIL' => 'email.tpl', 'DB_LIST_CELL_DATE' => 'date.tpl', 'DB_LIST_CELL_SORTER' => 'sorter.tpl', 'DB_LIST_CELL_ENUM' => 'enum.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 { const DEFAULT_IPP = 50; 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 $pager_link = null; var $pager_query_string = null; var $itemName = NULL; var $tableAttribs = array('class' => 'list_tbl'); var $sort_link = NULL; var $pageNum = NULL; var $ipp = 20; var $paginationOption = array(50 => 50, 100 => 100, 500 => 500, 1000 => 1000); var $showPaginationOptions = false; var $overrideIpp; protected $_pluginPath = 'class/DB/List/plugins'; /** * @var Doc */ var $Doc; /** * @var DBObj */ var $DBObj; var $additionalOptions = array(); function DB_List(&$Doc, &$DBObj, $functionalFields = array()) { $this->Doc = &$Doc; $this->DBObj = &$DBObj; $this->functionalFields = $functionalFields; $this->_init(); } function _init() { } 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']); } if ($this->overrideIpp) { $this->ipp = $this->overrideIpp; } 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); } if (null === Qs_Array::get($params, 'attribs[class]')) { Qs_Array::set($params, 'attribs[class]', 'c_' . $name); } $this->_column[$name] = $params; $this->_columnSort[$name] = $this->_columnSort ? (max((array)$this->_columnSort) + 1) : 1; } function addOptionsColumn($name, array $options = array()) { $this->_prepareOptionsColumn($options); $this->_column[$name] = $options; $this->_columnSort[$name] = $this->_columnSort ? (max((array)$this->_columnSort) + 1) : 1; } function removeCol($name) { unset($this->_column[$name]); unset($this->_columnSort[$name]); } function _prepageUrlVarNames() { $request = array(); foreach ($this->urlVarNames as $name) { if (array_key_exists($name, $_REQUEST)) { $request[$name] = $_REQUEST[$name]; } } $query = http_build_query($request) . '&'; return $query; } function _getBaseLink() { $link = Constant::get('BASE_URL') . '/' . CURR_PAGE_FULL . '?' . $this->_prepageUrlVarNames(); 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 setPagerLink($link) { $this->pager_link = $link; return $this; } function getPagerLink() { if (null == $this->pager_link) { $order_by = $this->order_by; if (null !== $this->def_order_by && $order_by == $this->def_order_by) { $order_by = ''; } $pager_link = $this->_getBaseLink(); if (!empty($order_by)) { $pager_link .= 'order_by=' . urlencode($order_by) . '&'; } } else { $glue = '?'; if (false !== strpos($glue, $this->pager_link)) { $glue = '&'; } $this->pager_query_string = $this->_prepageUrlVarNames(); if (!empty($order_by)) { $this->pager_query_string .= 'order_by=' . urlencode($order_by) . '&'; } if (!empty($this->pager_query_string)) { $this->pager_query_string = $glue . $this->pager_query_string; } $pager_link = $this->pager_link; } return $pager_link; } function exec() { $item = $this->toArray(); $this->Doc->addContent($item); } 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->getPagerLink(); $item['pager_query_string'] = $this->pager_query_string; $item['items'] = $list['list']; unset($list['list']); $item = array_merge($item, $list); if (!in_array($this->_pluginPath, $this->Doc->tplPlugins)) { $this->Doc->tplPlugins[] = $this->_pluginPath; } return $item; } 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]; if (null === Qs_Array::get($sortedColumn[$name], 'headerAttribs[id]')) { Qs_Array::set($sortedColumn[$name], 'headerAttribs[id]', 'th-' . $name); } if (array_key_exists('width', $sortedColumn[$name]) && null === Qs_Array::get($sortedColumn[$name], 'headerAttribs[width]') ) { Qs_Array::set($sortedColumn[$name], 'headerAttribs[width]', $sortedColumn[$name]['width']); unset($sortedColumn[$name]['width']); } } return $sortedColumn; } function updateCol($field, $key, $value) { $idFld =& $this->getCol($field); $idFld[$key] = $value; $this->setCol($field, $idFld); } protected function _prepareOptionsColumn(&$options) { if (!isset($options['actions'])) { $options['actions'] = array('edit' => array(), 'delete' => array()); } elseif (is_string($options['actions'])) { $actions = split('; ?', $options['actions']); $options['actions'] = array_combine($actions, array_fill(0, count($actions), array())); } elseif(is_array($options['actions']) && !Qs_Array::isAssoc($options['actions'])) { $options['actions'] = array_combine($options['actions'], array_fill(0, count($options['actions']), array())); } foreach ($options['actions'] as $action => &$actionOptions) { if (!isset($actionOptions['link'])) { $actionOptions['link'] = BASE_URL . '/' . CURR_PAGE . '?' . http_build_query(array('action' => $action)); } if (!isset($actionOptions['title'])) { $actionOptions['title'] = ucfirst(preg_replace('/([a-z])([A-Z])/', '$1 $2', $action)); } } if (!isset($options['params'])) { $options['params'] = array('id' => 'id'); } if (!isset($options['title'])) { $options['title'] = 'Options'; } if (!isset($options['tpl'])) { $options['tpl'] = SiteMap::getPath2ClassTpl('list', null, 'cells/options.tpl'); } $defaultAttribs = array( 'class' => 'grid_options', 'align' => 'center' ); if (!isset($options['attribs'])) { $options['attribs'] = array(); } $options['attribs'] = array_merge($defaultAttribs, $options['attribs']); return $this; } }