bebeboutik/adm/helpers/HelperList.php
2016-09-06 15:32:52 +02:00

333 lines
10 KiB
PHP

<?php
class HelperList{
public $_html = '';
public $_img = false;
public $_js = false;
public $_label = false;
public $_current_index = false;
public $_table_id = false;
public $_orderby = false;
public $_orderway = 'asc';
public $_have_filters = false;
public $_pagination = false;
public $_filters = array();
public $_filter_ajax_url = '../../ajax.php';
/*
$this->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 .= '
<script type="text/javascript">
$(document).delegate(".refresh_filters", "click", function(){
$table = $(this).parent().parent().parent().parent();
$table.html($filterCacheContent[$table.attr("id")]);
});
$(document).ready(function(){
$filterCacheContent = [];
$(".filter_table").each(function(){
$filterCacheContent[$(this).attr("id")] = $(this).html();
});
$(".filter_header input.filter_input").live("keyup", function(){
$this = $(this);
$table = $(this).parent().parent().parent().parent();
if (typeof $timer !== "undefined")
clearTimeout($timer);
$timer = setTimeout(function(){
search($table);
}, 500);
});
});
function search(table_filter){
$table = table_filter;
$filters = $table.find("input.filter_input");
$filter_value = [];
$filters.each(function(){
if ($(this).val() != ""){
$filter_value.push({"filter":$(this).attr("name"),"value":$(this).val()});
}
});
if ($filter_value.length == 0)
$table.html($filterCacheContent[$table.attr("id")]);
else{
$.ajax({
url: "' . $this->_filter_ajax_url . '",
data:{
current_index: "' . $this->_current_index . '",
filters: $filter_value
},
dataType: "json",
success(data, textStatus, jqXHR){
$data = data;
if (!$data.hasError && $data.response){
$table.children("tbody").html($data.response.html_body);
if ($data.response.html_header)
$table.children("thead").html($data.response.html_header);
}
}
});
}
}
</script>';
$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 .= '
<fieldset class="space">
<legend><img src="' . $this->_img . '" alt="" title="" /> ' . $this->_label . '</legend>
' . $this->displayPagination() . '
<table' . ($this->_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 .= '
</table>
' . $this->displayPagination() . '
</fieldset>';
}
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 = '<div style="display: inline-block; margin-top: 10px" class="pagination" id="' . ($this->_table_id ? $this->_table_id : '') . '_pagination">';
if ($current - $range > 1){
$html .= '<a style="margin: 0 5px;" class="button" href="' . $href . 1 . '">' . 1 . '</a>';
if ($current - $range > 2)
$html .= '<span style="margin: 0 5px;">...</span>';
}
for ($i=1; $i <= $nb_pages; $i++){
if ($i < $current - $range || $i > $current + $range)
continue;
if ($i == $current && !$all)
$html .= '<span style="margin: 0 5px;">' . (int) $i . '</span>';
else
$html .= '<a style="margin: 0 5px;" class="button" href="' . $href . (int) $i . '">' . (int) $i . '</a>';
}
if ($current + $range < $nb_pages){
if ($current + $range < $nb_pages -1)
$html .= '<span style="margin: 0 5px;">...</span>';
$html .= '<a style="margin: 0 5px;" class="button" href="' . $href . (int) $nb_pages . '">' . (int) $nb_pages . '</a>';
}
if ($display_all)
if ($all)
$html .= '<span style="margin: 0 5px;">' . $all_text . '</span>';
else
$html .= '<a style="margin: 0 5px;" class="button" href="' . $href . 'all' . '">' . $all_text . '</a>';
$html .= '</div>';
return $html;
}
public function displayHeader(){
$this->_html .= $this->_html_header;
}
public function setHeader(){
## NEW HEADER ROW
$this->_filters = '';
$this->_html_header .= '
<thead>
<tr>';
## 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 .= '<th id="' . $field_header['name'] . '" ' . $width . '>' . $field_header['label'] . '</th>';
$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 .= '<th id="header_actions" ' . $width . '>' . (isset($this->_actions['label']) ? $this->_actions['label'] : 'Actions') . '</th>';
}
## CLOSE HEADER ROW
$this->_html_header .= '
</tr>';
## ADD FILTERS
if ($this->_have_filters){
$this->_html_header .= '<tr>';
$this->_html_header .= $this->_filters;
if (isset($this->_actions) && is_array($this->_actions) && count($this->_actions))
$this->_html_header .= '<th><a class="button refresh_filters">refresh</a></th>';
$this->_html_header .= '</tr>';
}
$this->_html_header .= '
</thead>';
$this->_html_header .= '';
}
public function setFilter($header_row){
if (!isset($header_row['filter']))
return $this->_filters .= '<th>--</th>';
$this->_have_filters = true;
$filter = $header_row['filter'];
$filter_type = isset($filter['type']) ? $filter['type'] : 'text';
switch($filter_type){
case 'text':
default:
$this->_filters .= '<th class="filter_header"><input type="text" class="filter_input filter_' . $filter['name'] . '" name="filter_' . $filter['name'] . '"' . (isset($filter['label']) ? 'placeholder="' . $filter['label'] . '"' : '') . ' /></th>';
break;
}
}
public function displayBody(){
$this->_html .= '<tbody>';
## NEW ROW
foreach ($this->_rows as $row) {
$this->_html .= '<tr>';
## 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 .= '<td' . ($class ? ' class="' . $class . '"' : '') . '>' . ($switch ? ($row_value ? '<img src="../img/admin/enabled.gif" />' : '<img src="../img/admin/disabled.gif" />') : $row_value) . '</td>';
}
## ADD ACTIONS
if (isset($this->_actions) && $this->_actions){
$this->_html .= '<td>';
$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 .= '
<a href="' . $base_uri . $action_link . '"' . ($action['onclick'] ? 'onclick="' . $action['onclick'] . '"' : '') . '>
<img src="' . $action['img'] . '" alt="' . $action['label'] . '" title="'. $action['label'] .'" />
</a>';
}
$this->_html .= '</td>';
}
}
$this->_html .= '</tbody>';
}
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;
}
}