actions = array( array( 'identifier' => 'id_object' 'action' => 'http://...' 'img' => 'img/' 'label' => 'update button' ), ... ) */ public $_actions = array(); /* $this->_header = array( array( 'name' => 'col1', 'label' => 'column 1' 'width' => '40' ), array( 'name' => 'col2', 'label' => 'column 2' ), ... );*/ public $_header = array(); /* $this->_rows = array( array( 'col1' => array( 'value' => 'cell 1' 'class' => 'highlight' ), 'col2' => array( 'value' => 'cell 2' ), ... ), array( 'col1' => array( ... ) ), ... ); */ public $_rows = array(); public function renderList($display = true){ $this->_html = ''; $this->displayTable(); if ($this->_have_filters) $this->addFilterJs(); $list = $this->_html; if (!$display) return $list; echo $list; } public function addFilterJs(){ $this->_js .= ' '; $this->_html = $this->_js . $this->_html; } public function displayTable(){ ## ADD NEW FIELDSET AND TABLE if (!$this->_img) $this->_img = '../img/admin/cms.gif'; if (!$this->_label) $this->_label = 'List'; $this->setHeader(); $this->_html .= '
' . $this->_label . ' ' . $this->displayPagination() . ' _table_id ? ' id="' . $this->_table_id . '"' : '') . ' class="table' . ($this->_have_filters ? ' filter_table' : '') . '" style="width: 100%; clear:both; margin-top: 15px;">'; ## ORDER ROWS if ($this->_orderby) $this->orderRows(); ## ADD HEADER $this->displayHeader(); ## ADD BODY $this->displayBody(); ## CLOSE TABLME AND FIELDSET $this->_html .= ' ' . $this->displayPagination() . '
'; } public function displayPagination(){ if (!$this->_pagination) return; $base_uri = ($this->_current_index ? $this->_current_index : ''); $base_uri .= (strpos($base_uri, '?') === FALSE) ? '?' : '&'; $nb_pages = ceil($this->_pagination['count'] / $this->_pagination['step']); $range = isset($this->_pagination['range']) ? $this->_pagination['range'] : 2; $current = $this->_pagination['current']; $display_all = isset($this->_pagination['all']) ? $this->_pagination['all'] : false; $all_text = isset($this->_pagination['all_text']) ? $this->_pagination['all_text'] : 'Display all'; if ($current == 'all'){ $all = true; $current = 1; } else{ $all = false; $current = (int) $current; } $href = $base_uri . ($this->_table_id ? $this->_table_id : '') . '_pagination='; $html = ''; return $html; } public function displayHeader(){ $this->_html .= $this->_html_header; } public function setHeader(){ ## NEW HEADER ROW $this->_filters = ''; $this->_html_header .= ' '; ## ADD HEADER DATA foreach ($this->_header as $field_header) { $width = ''; if (isset($field_header['width']) && $field_header['width']) $width = 'style="width:' . $field_header['width'] . 'px;"'; $this->_html_header .= '' . $field_header['label'] . ''; $this->setFilter($field_header); } ## ADD ACTIONS COLUMN if (isset($this->_actions) && is_array($this->_actions) && count($this->_actions)){ if (isset($this->_actions_header)){ $width = (isset($this->_actions_header['width']) && $this->_actions_header['width'] ? 'width=' . $this->_actions_header['width'] : ''); } $this->_html_header .= '' . (isset($this->_actions['label']) ? $this->_actions['label'] : 'Actions') . ''; } ## CLOSE HEADER ROW $this->_html_header .= ' '; ## ADD FILTERS if ($this->_have_filters){ $this->_html_header .= ''; $this->_html_header .= $this->_filters; if (isset($this->_actions) && is_array($this->_actions) && count($this->_actions)) $this->_html_header .= 'refresh'; $this->_html_header .= ''; } $this->_html_header .= ' '; $this->_html_header .= ''; } public function setFilter($header_row){ if (!isset($header_row['filter'])) return $this->_filters .= '--'; $this->_have_filters = true; $filter = $header_row['filter']; $filter_type = isset($filter['type']) ? $filter['type'] : 'text'; switch($filter_type){ case 'text': default: $this->_filters .= ''; break; } } public function displayBody(){ $this->_html .= ''; ## NEW ROW foreach ($this->_rows as $row) { $this->_html .= ''; ## ADD ROW DATA foreach ($this->_header as $field_header) { $switch = (isset($field_header['type']) && $field_header['type'] == 'bool'); $row_value = (isset($row[$field_header['name']]) ? $row[$field_header['name']] : '--'); $class = ($switch ? ($row_value ? 'switch_on' : 'switch_off') : ''); $class .= (isset($field_header['class']) && $field_header['class']) ? $field_header['class'] : ''; $this->_html .= '' . ($switch ? ($row_value ? '' : '') : $row_value) . ''; } ## ADD ACTIONS if (isset($this->_actions) && $this->_actions){ $this->_html .= ''; $base_uri = ($this->_current_index ? $this->_current_index : ''); $base_uri .= (strpos($base_uri, '?') === FALSE) ? '?' : '&'; foreach ($this->_actions as $action) { $action_link = (isset($action['identifier']) && $action['identifier'] && isset($row[$action['identifier']]) && $row[$action['identifier']] ? 'id=' . $row[$action['identifier']] . '&' . $action['action']: $action['action'] ); if (isset($action['params'])){ foreach ($action['params'] as $param_key => $param_field) { if (isset($row[$param_field])) $action_link .= '&' . $param_key . '=' . $row[$param_field]; } } $this->_html .= ' ' . $action['label'] . ' '; } $this->_html .= ''; } } $this->_html .= ''; } public function orderRows(){ if (!$this->_orderby || !$this->_rows) return false; $_tmp_rows = array(); foreach ($this->_rows as $row) { if (!isset($row[$this->_orderby])) continue; $_tmp_rows[$row[$this->_orderby]] = $row; } if (strtolower($this->_orderway) == 'desc') krsort($_tmp_rows); else ksort($_tmp_rows); $this->_rows = $_tmp_rows; } }