<?php
class DataPager {
protected $dataSource = array();
protected $headers = array();
protected $headerTitleMap = array();
protected $enableHeaders = true;
protected $dataNavigations = array();
protected $pageSize = 0;
protected $cssClassName;
protected $id;
protected $onHeaderRenderCallBack;
protected $onRowRenderCallBack;
public function __construct(){
$this->onHeaderRenderCallBack = array($this, 'renderHeader');
$this->onRowRenderCallBack = array($this, 'renderRow');
$this->onPagerRenderCallBack = array($this, 'renderPager');
}
public function setCssClassName($cssClassName){
$this->cssClassName = $cssClassName;
}
public function setId($id){
$this->id = $id;
}
public function setDataSource(array $dataSource){
$this->dataSource = $dataSource;
}
public function setHeaders(array $headers){
$this->headers = $headers;
}
public function setHeaderTitles(array $titles){
$this->headerTitleMap = $titles;
}
public function enableHeaders($bool){
$this->enableHeaders = $bool;
}
public function setPageSize($size){
$this->pageSize = $size;
}
public function onHeaderRender($callback){
$this->onHeaderRenderCallBack = $callback;
}
public function onRowRender($callback){
$this->onRowRenderCallBack = $callback;
}
public function onPagerRender($callback){
$this->onPagerRenderCallBack = $callback;
}
public function addDataNavigateUrl($field, $navigation){
$this->dataNavigations[$field] = $navigation;
}
public function render(){
if( count($this->dataSource) > 0){
$fieldCount = count($this->dataSource[0]);
$control = '<table class="' . $this->cssClassName . '" id="' . $this->id . '">';
if(count($this->headers) == 0){
$this->headers = array_keys($this->dataSource[0]);
}
foreach($this->dataSource as $idx=>$row){
if(is_object($row)){
$row = get_object_vars($row);
}
$rowData = array();
foreach($this->headers as $headerField){
if(array_key_exists($headerField, $row)){
$rowData[$headerField] = $row[$headerField];
}else{
unset($this->headers[$headerField]);
}
}
$row = $rowData;
if($this->enableHeaders && $idx == 0){
$control .='<thead>';
$control .= call_user_func_array($this->onHeaderRenderCallBack, array($this->headers));
$control .='</thead><tbody>';
}
$control .= call_user_func_array($this->onRowRenderCallBack, array($row));
}
$control .= '</tbody>';
$control .= '<tfoot>';
$control .= '<tr><td colspan="' . $fieldCount . '">' . call_user_func_array($this->onPagerRenderCallBack, array($this->pageSize)) . '</td></tr>';
$control .= '</tfoot>';
$control .= '</table>';
return $control;
}
}
protected function renderHeader($fields){
$control = '<tr>';
foreach($fields as $header){
$title = isset($this->headerTitleMap[$header]) ? $this->headerTitleMap[$header] : $header;
$control .= '<th>' . $title . '</th>';
}
$control .= '</tr>';
return $control;
}
protected function renderRow($row){
$control = '<tr>';
foreach($row as $field=>$value){
$value = ($value != '') ? $value : ' ';
if(isset($this->dataNavigations[$field])){
$link = $this->dataNavigations[$field];
foreach($this->headers as $headerField){
$link = str_replace('{@' . $headerField . '}', $row[$headerField], $link);
}
$link = '<a href="' . $link . '">' . $value . '</a>';
$value = $link;
}
$control .= '<td>' . $value . '</td>';
}
$control .= '</tr>';
return $control;
}
protected function renderPager($pageSize){
$control = '';
for($i=1; $i <= $pageSize; $i++){
$control .= '<a href="?page=' . $i . '">' . $i . '</a>';
}
return $control;
}
}
?>
No comments:
Post a Comment