PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/atk4/lib/Grid/Basic.php

https://github.com/intuititve/lostandfound
PHP | 198 lines | 135 code | 36 blank | 27 comment | 18 complexity | d4d2d01facdbd46af1a759cc018290c0 MD5 | raw file
Possible License(s): AGPL-3.0
  1. <?php // vim:ts=4:sw=4:et:fdm=marker
  2. /**
  3. * This is a Basic Grid implementation, which produces fully
  4. * functional HTML grid capable of filtering, sorting, paginating
  5. * and using multiple column formatters. Basic Grid no longer
  6. * implements the column formatters, instead they have been
  7. * moved into Grid_Advanced
  8. *
  9. * @link http://agiletoolkit.org/doc/grid
  10. *
  11. * Use:
  12. * $grid=$this->add('Grid');
  13. * $grid->setModel('User');
  14. *
  15. * @license See http://agiletoolkit.org/about/license
  16. *
  17. **/
  18. class Grid_Basic extends CompleteLister {
  19. public $columns=array();
  20. public $default_controller='Controller_MVCGrid';
  21. public $sort_icons=array(
  22. 'ui-icon ui-icon-arrowthick-2-n-s',
  23. 'ui-icon ui-icon-arrowthick-1-n',
  24. 'ui-icon ui-icon-arrowthick-1-s',
  25. );
  26. function init(){
  27. parent::init();
  28. $this->initWidget();
  29. }
  30. /** You might want Grid to be enganced with a widget. Initialize it here or define this as empty function to avoid */
  31. function initWidget(){
  32. }
  33. function defaultTemplate(){
  34. return array('grid');
  35. }
  36. function importFields($model,$fields=undefined){
  37. $this->add('Controller_MVCGrid')->importFields($model,$fields);
  38. }
  39. function addColumn($formatters,$name=null,$descr=null){
  40. if($name===null){
  41. $name=$formatters;
  42. $formatters='text';
  43. }
  44. if($descr===null)$descr=ucwords(str_replace('_',' ',$name));
  45. $this->columns[$name]=array('type'=>$formatters);
  46. if(is_array($descr)){
  47. $this->columns[$name]=array_merge($this->columns[$name],$descr);
  48. } else {
  49. $this->columns[$name]['descr']=$descr;
  50. }
  51. $this->last_column=$name;
  52. if(is_callable($formatters)){
  53. $this->columns[$name]['fx']=$formatters;
  54. return $this;
  55. }
  56. $subtypes=explode(',',$formatters);
  57. foreach($subtypes as $subtype){
  58. if(!$this->hasMethod($m='init_'.$subtype)){
  59. if(!$this->hasMethod($m='format_'.$subtype)){
  60. throw $this->exception('No such formatter')->addMoreInfo('formater',$subtype);
  61. }
  62. }else $this->$m($name,$descr);
  63. }
  64. return $this;
  65. }
  66. function precacheTemplate(){
  67. // pre-cache our template for row
  68. // $full=false used for certain row init
  69. $row = $this->row_t;
  70. $col = $row->cloneRegion('col');
  71. // tbody -> column
  72. $row->setHTML('row_id','<?$id?>');
  73. $row->trySetHTML('odd_even','<?$odd_even?>');
  74. $row->del('cols');
  75. // thead -> column
  76. $header = $this->template->cloneRegion('header');
  77. $header_col = $header->cloneRegion('col');
  78. $header_sort = $header_col->cloneRegion('sort');
  79. // Totals -> column
  80. if($t_row = $this->totals_t){
  81. $t_col = $t_row->cloneRegion('col');
  82. $t_row->del('cols');
  83. }
  84. $header->del('cols');
  85. foreach($this->columns as $name=>$column){
  86. $col->del('content');
  87. $col->setHTML('content','<?$'.$name.'?>');
  88. if(isset($t_row)){
  89. $t_col->del('content');
  90. $t_col->setHTML('content','<?$'.$name.'?>');
  91. $t_col->trySetHTML('tdparam','<?tdparam_'.$name.'?>style="white-space: nowrap"<?/?>');
  92. $t_row->appendHTML('cols',$t_col->render());
  93. }
  94. // some types needs control over the td
  95. $col->setHTML('tdparam','<?tdparam_'.$name.'?>style="white-space: nowrap"<?/?>');
  96. $row->appendHTML('cols',$col->render());
  97. $header_col->set('descr',$column['descr']);
  98. $header_col->trySet('type',$column['type']);
  99. // TODO: rewrite this (and move into Advanced)
  100. if(isset($column['sortable'])){
  101. $s=$column['sortable'];
  102. // calculate sortlink
  103. $l = $this->api->getDestinationURL(null,array($this->name.'_sort'=>$s[1]));
  104. $header_sort->trySet('order',$column['sortable'][0]);
  105. $header_sort->trySet('sorticon',$this->sort_icons[$column['sortable'][0]]);
  106. $header_sort->set('sortlink',$l);
  107. $header_col->setHTML('sort',$header_sort->render());
  108. }else{
  109. $header_col->del('sort');
  110. $header_col->tryDel('sort_del');
  111. }
  112. if($column['thparam']){
  113. $header_col->trySetHTML('thparam',$column['thparam']);
  114. }else{
  115. $header_col->tryDel('thparam');
  116. }
  117. $header->appendHTML('cols',$header_col->render());
  118. }
  119. $this->row_t = $this->api->add('SMlite');
  120. $this->row_t->loadTemplateFromString($row->render());
  121. if(isset($t_row)){
  122. $this->totals_t = $this->api->add('SMlite');
  123. $this->totals_t->loadTemplateFromString($t_row->render());
  124. }
  125. $this->template->setHTML('header',$header->render());
  126. }
  127. function formatRow(){
  128. if(!$this->columns)throw $this->exception('No columns defined for grid');
  129. foreach($this->columns as $tmp=>$column){
  130. $this->current_row[$tmp.'_original']=@$this->current_row[$tmp];
  131. $formatters = explode(',',$column['type']);
  132. foreach($formatters as $formatter){
  133. if(!$formatter)continue;
  134. if(method_exists($this,$m="format_".$formatter)){
  135. $this->$m($tmp,$column);
  136. }else throw new BaseException("Grid does not know how to format type: ".$formatter);
  137. }
  138. // setting cell parameters (tdparam)
  139. $this->applyTDParams($tmp);
  140. if($this->current_row[$tmp]=='')$this->current_row[$tmp]=' ';
  141. }
  142. return $this->current_row;
  143. }
  144. function renderRows(){
  145. $this->precacheTemplate();
  146. parent::renderRows();
  147. if(!$this->totals['row_count']){
  148. $def_template = $this->defaultTemplate();
  149. $this->totals=false;
  150. $this->template->del('full_table');
  151. }else{
  152. $this->template->del('not_found');
  153. }
  154. }
  155. function format_shorttext($field){
  156. $text=$this->current_row[$field];
  157. //TODO counting words, tags and trimming so that tags are not garbaged
  158. if(strlen($text)>60)$text=substr($text,0,28).' ~~~ '.substr($text,-28);;
  159. $this->current_row[$field]=$text;
  160. $this->tdparam[$this->getCurrentIndex()][$field]['title']=$this->current_row[$field.'_original'];
  161. }
  162. }