PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/atk4/lib/Lister.php

https://github.com/mahimarathore/mahi
PHP | 161 lines | 68 code | 15 blank | 78 comment | 9 complexity | 4b637ca6d2af61cfbcc0f915175aa4f0 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php // vim:ts=4:sw=4:et:fdm=marker
  2. /*
  3. * Lister implements a very simple and fast way to output series
  4. * of data by applying template formatting
  5. *
  6. * @link http://agiletoolkit.org/doc/lister
  7. *
  8. * Use:
  9. * $list=$this->add('Lister');
  10. * $list->setModel('User');
  11. *
  12. * Template (view/users.html):
  13. * <h4><?$name?></h4>
  14. * <p><?$desc?></p>
  15. *
  16. * @license See http://agiletoolkit.org/about/license
  17. *
  18. * @link http://agiletoolkit.org/
  19. *//*
  20. ==ATK4===================================================
  21. This file is part of Agile Toolkit 4
  22. http://agiletoolkit.org/
  23. (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
  24. Distributed under Affero General Public License v3 and
  25. commercial license.
  26. See LICENSE or LICENSE_COM for more information
  27. =====================================================ATK4=*/
  28. class Lister extends View {
  29. /** If lister data is retrieed from the SQL database, this will contain dynamic query. */
  30. public $dq=null;
  31. /** For other iterators, this variable will be used */
  32. public $iter=null;
  33. /** Points to current row before it's being outputted. Used in formatRow() */
  34. public $current_row=array();
  35. /** Similar to $current_row, but will use for direct HTML output, no escaping. Use with care. */
  36. public $current_row_html=array();
  37. /** Contains ID of current record */
  38. public $current_id=null;
  39. /** Name of the ID field in this lister */
  40. public $id_field=null;
  41. /** Data set fetched from source */
  42. public $iterator=array();
  43. /*
  44. * Sets source data for the lister. If source is a model, use setModel() instead.
  45. *
  46. * @link http://agiletoolkit.org/doc/lister
  47. *
  48. * Examples:
  49. * $l=$this->add('Lister');
  50. * $l->setSource( array('a','b','c') ); // associative array
  51. *
  52. * or // array of hashes
  53. * $l->setSource( array(
  54. * array('id'=>1,'name'=>'John','surname'=>'Smith'),
  55. * array('id'=>2,'name'=>'Joe','surname'=>'Blogs')
  56. * ));
  57. *
  58. * or // dsql
  59. * $l->setSource( $this->api->db->dsql()
  60. * ->table('user')
  61. * ->where('age>',3)
  62. * ->field('*')
  63. * );
  64. *
  65. * or // sql table
  66. * $l->setSource( 'user', array('name','surname'));
  67. */
  68. function setSource($source,$fields=null){
  69. // Set DSQL
  70. if($source instanceof DB_dsql){
  71. $this->dq=$source;
  72. return $this;
  73. }
  74. // SimpleXML and other objects
  75. if(is_object($source)){
  76. if($source instanceof Model)throw $this->exception('Use setModel() for Models');
  77. if($source instanceof Controller)throw $this->exception('Use setController() for Controllers');
  78. if($source instanceof Iterator){
  79. $this->iter=$source;
  80. return $this;
  81. }
  82. // Cast non-iterable objects into array
  83. $source=(array)$source;
  84. }
  85. // Set Array as a data source
  86. if(is_array($source)){
  87. $m=$this->setModel('Model',$fields);
  88. if(is_array(reset($source))){
  89. $m->setSource('Array',$source);
  90. }else{
  91. $m->setSource('ArrayAssoc',$source);
  92. }
  93. return $this;
  94. }
  95. // Set manually
  96. $this->dq=$this->api->db->dsql();
  97. $this->dq
  98. ->table($source)
  99. ->field($fields?:'*');
  100. return $this;
  101. }
  102. /** @obsolete set array source */
  103. function setStaticSource($data){
  104. return $this->setSource($data);
  105. }
  106. /** Redefine and change $this->current_row to format data before it appears */
  107. function formatRow(){
  108. $this->hook('formatRow');
  109. }
  110. /** Renders single row. If you use for formatting then interact with template->set() directly prior to calling parent */
  111. function rowRender($template) {
  112. foreach($this->current_row as $key=>$val){
  113. if(isset($this->current_row_html[$key]))continue;
  114. $template->trySet($key,$val);
  115. }
  116. $template->setHTML($this->current_row_html);
  117. $template->trySet('id',$this->current_id);
  118. return $template->render();
  119. }
  120. function getIterator(){
  121. if(is_null($i=$this->model?:$this->dq?:$this->iter))
  122. throw $this->exception('Please specify data source with setSource or setModel');
  123. return $i;
  124. }
  125. /*
  126. function prepareIterator(){
  127. $this->iterator = $this->getIterator();
  128. $data = array();
  129. foreach($this->iterator as $id=>$row){
  130. $data[$id] = $row;
  131. }
  132. $this->iterator = $data;
  133. }
  134. */
  135. function render(){
  136. foreach($this->getIterator() as $this->current_id=>$this->current_row){
  137. $this->formatRow();
  138. $this->output($this->rowRender($this->template));
  139. }
  140. }
  141. function defaultTemplate(){
  142. return array('view/lister');
  143. }
  144. }