Minor updates and enhancements to Table()

This commit is contained in:
Deon George
2013-05-14 23:55:30 +10:00
parent b65ddab2d0
commit 783964ee59
9 changed files with 159 additions and 22 deletions

View File

@@ -9,7 +9,7 @@
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Controller_Default extends Controller {
abstract class lnApp_Controller_Default extends Kohana_Controller {
/**
* The variable that our output is stored in
*/

View File

@@ -9,7 +9,7 @@
* @copyright (c) 2009-2013 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Controller_TemplateDefault extends Controller_Template {
abstract class lnApp_Controller_TemplateDefault extends Kohana_Controller_Template {
/**
* @var object meta object information as per [meta]
*/
@@ -76,7 +76,7 @@ abstract class lnApp_Controller_TemplateDefault extends Controller_Template {
// Check user auth and role
if ($this->_auth_required()) {
if (Kohana::$is_cli)
if (PHP_SAPI === 'cli')
throw new Kohana_Exception('Cant run :method, authentication not possible',array(':method'=>$this->request->action()));
// If auth is required and the user is logged in, then they dont have access.

View File

@@ -48,6 +48,10 @@ abstract class lnApp_Form extends Kohana_Form {
return $output;
}
public static function button($name,$body,array $attributes=NULL) {
return sprintf(static::_controlgroup($name,$attributes),parent::button($name,$body,$attributes));
}
/**
* Wrap our Form() functions with boostrap HTML
*

View File

@@ -102,7 +102,7 @@ abstract class lnApp_HTMLRender {
throw new Kohana_Exception('Missing key :key for image',array(':key'=>$key));
// Check for unique keys
if (static::$_unique_vals)
if (isset(static::$_unique_vals) AND static::$_unique_vals)
foreach (static::$_unique_vals as $v=>$u)
foreach (static::$_data as $d)
if (isset($d[$u]) && $d['data'] == $item['data'])

View File

@@ -12,6 +12,8 @@
abstract class lnApp_Table {
private $data = NULL;
private $columns = array(); // Our columns that we need to display
private $filters = array(); // Our datafomrating that we need to use
private $filters_added = FALSE;
private $jssort = FALSE; // Use the JS pager and sorter
private $other = ''; // If we are only listing x items, this will be the other summary line
private $other_col = NULL; // If we are only listing x items, this will be the other summary line
@@ -19,6 +21,8 @@ abstract class lnApp_Table {
private $page = FALSE; // If we should page the results
private $page_rows = 0; // Number of rows per page
private $prepend = array(); // Our datafomrating that we need to use
private $prepend_vals = array(); // Our datafomrating that we need to use
private $select = array(); // Our select form details
public function __toString() {
return (string) $this->render();
@@ -37,6 +41,12 @@ abstract class lnApp_Table {
}
private function evaluate($d,$key) {
if (count($this->filters) AND ! $this->filters_added) {
$d->display_filters($this->filters);
// So we dont keep extending our display_filters
$this->filters_added = TRUE;
}
if (is_array($d) AND isset($d[$key]))
$x = $d[$key];
@@ -53,8 +63,18 @@ abstract class lnApp_Table {
if (isset($this->prepend[$key])) {
foreach ($this->prepend[$key] as $act => $data) {
switch ($act) {
case 'checkbox': $x = sprintf('<label class="checkbox">%s %s</label>',Form::checkbox($data,$x,FALSE,array('nocg'=>TRUE)),$x); // @todo Check those not yet checked
break;
case 'input':
if (preg_match('/__VALUE__/',$data['key']))
$data['key'] = preg_replace('/__VALUE__/',$x,$data['key']);
$x = Form::input($data['key'],isset($data['values'][$x]) ? $data['values'][$x] : '',array('placeholder'=>$x,'label'=>$x));
break;
case 'url': $x = HTML::anchor($data.$x,$x);
break;
break;
}
}
}
@@ -66,8 +86,17 @@ abstract class lnApp_Table {
return new Table;
}
public function jssort($bool) {
$this->jssort = $bool;
/**
* Apply additional display filters to the results
*/
public function filters($cols) {
$this->filters = $cols;
return $this;
}
public function jssort($table) {
$this->jssort = $table;
return $this;
}
@@ -92,6 +121,8 @@ abstract class lnApp_Table {
foreach ($this->data as $o) {
$row++;
$c = 0;
// So our filters are added for this record.
$this->filters_added = FALSE;
// If we are HTML paging
if ($this->page) {
@@ -103,7 +134,7 @@ abstract class lnApp_Table {
// If we are just listing page_rows items and a summary line as a result of exceeding that
if ($this->other_col AND $row>$this->page_rows) {
$this->other += Table::resolve($o,$this->other_col);
$this->other += $this->evaluate($o,$this->other_col);
// Otherwise rendering our rows
} else {
@@ -118,7 +149,7 @@ abstract class lnApp_Table {
return $result;
}
public function render() {
private function render() {
$output = '';
// If we need to page the results
@@ -132,8 +163,11 @@ abstract class lnApp_Table {
}
if ($this->select)
$output .= Form::open($this->select['form']);
$output .= View::factory('table')
->set('jssort',$this->jssort AND ! $this->page)
->set('jssort',($this->jssort AND ! $this->page) ? $this->jssort : FALSE)
->set('other',$this->other)
->set('th',$this->columns)
->set('td',$this->process());
@@ -162,7 +196,7 @@ $(document).ready(function() {
odd : "" // even row zebra striping
});
$("#list-table").tablesorter({
$("#list-table'.($this->jssort ? '-'.$this->jssort : '').'").tablesorter({
theme: "bootstrap",
widthFixed : true,
headerTemplate : "{content} {icon}", // Add icon for jui theme; new in v2.7!
@@ -170,7 +204,7 @@ $(document).ready(function() {
}).tablesorterPager({
// target the pager markup - see the HTML block below
container : $(".pager"),
container : $(".pager'.($this->jssort ? '-'.$this->jssort : '').'"),
output : "{startRow} - {endRow} / {filteredRows} ({totalRows})",
fixedHeight: true,
removeRows : false,
@@ -200,10 +234,27 @@ $(document).ready(function() {
->data('media/vendor/mottie-tablesorter/css/jquery.tablesorter.pager.css');
}
if ($this->select) {
$output .= Form::button('submit','Submit',array('class'=>'btn btn-primary','value'=>$this->select['submit']));
foreach ($this->select['hidden'] as $k=>$v)
$output .= Form::hidden($k,$v);
$output .= Form::close();
}
return $output;
}
//ZZ
public function select($form,$submit='',array $hidden=array()) {
$this->select['form'] = $form;
$this->select['submit'] = $submit;
$this->select['hidden'] = $hidden;
return $this;
}
// @deprecated
public static function display($data,$rows,array $cols,array $option) {
$prepend = $headers = array();
@@ -222,11 +273,6 @@ $(document).ready(function() {
->prepend($prepend);
/*
if (! empty($option['button']))
$button = implode('',$option['button']);
else
$button = Form::button('Submit','View/Edit',array('class'=>'form_button','type'=>'submit'));
// This JS is failing, any pressing of select all, unselect all, toggle is propaging up and submitting the form
Script::factory()