PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/recordlist/class.atkcustomrecordlist.inc

https://github.com/ibuildingsnl/ATK
PHP | 246 lines | 132 code | 31 blank | 83 comment | 31 complexity | 9e13a27b12055cccdd59628bfc74e842 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, LGPL-3.0
  1. <?php
  2. /**
  3. * This file is part of the Achievo ATK distribution.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package atk
  9. * @subpackage recordlist
  10. *
  11. * @copyright (c)2000-2004 Ibuildings.nl BV
  12. * @license http://www.achievo.org/atk/licensing ATK Open Source License
  13. *
  14. * @version $Revision: 6323 $
  15. * $Id$
  16. */
  17. /** @internal include base class */
  18. atkimport("atk.recordlist.atkrecordlist");
  19. /**
  20. * Custom recordlist renderer.
  21. *
  22. * @author Paul Verhoef <paul@ibuildings.nl>
  23. * @package atk
  24. * @subpackage recordlist
  25. *
  26. */
  27. class atkCustomRecordList extends atkRecordList
  28. {
  29. var $m_exportcsv = true;
  30. protected $m_mode;
  31. /**
  32. * Creates a special Recordlist that can be used for exporting to files or to make it printable
  33. * @param atkNode $node The node to use as definition for the columns.
  34. * @param array $recordset The records to render
  35. * @param String $sol String to use at start of each row
  36. * @param String $sof String to use at start of each field
  37. * @param String $eof String to use at end of each field
  38. * @param String $eol String to use at end of each row
  39. * @param int $type 0=Render rows in simple html tabl; 1= raw export
  40. * @param string $compression Compression technique (bzip / gzip)
  41. * @param array $suppressList List of attributes from $node that should be ignored
  42. * @param array $outputparams Key-Value parameters for output. Currently existing:
  43. * filename - the name of the file (without extension .csv)
  44. * @param String $mode The mode that is passed to attributes' display() method
  45. * (for overrides). Defaults to 'list'.
  46. * @param Boolean $titlerow Should titlerow be rendered or not
  47. * @param Boolean $decode Should data be decoded or not (for exports)
  48. * @param String $fsep String to use between fields
  49. * @param String $rfeplace String for replacing line feeds in recordset field values (null = do not replace)
  50. */
  51. function render(&$node, $recordset, $sol, $sof, $eof, $eol, $type="0", $compression="", $suppressList="",
  52. $outputparams=array(), $mode="list", $titlerow=true, $decode=false, $fsep="", $rfeplace=null)
  53. {
  54. $this->setNode($node);
  55. $this->m_mode = $mode;
  56. // example html csv
  57. // $sol = '<tr>' or ''
  58. // $sof = '<td>' or '"'
  59. // $eof = '</td>' or '"'
  60. // $eol = '</tr>' or '\r\n'
  61. // $fsep = '' or ';'
  62. //$empty om lege tabelvelden op te vullen;
  63. // stuff for the totals row..
  64. $totalisable = false;
  65. $totals = array();
  66. if ($type=="0")
  67. {
  68. $empty = "&nbsp;";
  69. }
  70. if ($type=="1")
  71. {
  72. $output="";
  73. $empty="";
  74. }
  75. if ($titlerow)
  76. {
  77. $output .= $sol;
  78. // display a headerrow with titles.
  79. // Since we are looping the attriblist anyway, we also check if there
  80. // are totalisable collumns.
  81. foreach (array_keys($this->m_node->m_attribList) as $attribname)
  82. {
  83. $p_attrib = &$this->m_node->m_attribList[$attribname];
  84. $musthide=(is_array($suppressList)&&count($suppressList)>0&&in_array($attribname,$suppressList));
  85. if (!$this->isHidden($p_attrib) && !$musthide)
  86. {
  87. $output.=$sof.$this->eolreplace(atktext($p_attrib->fieldName(), $this->m_node->m_module, $this->m_node->m_type), $rfeplace).$eof.$fsep;
  88. // the totalisable check..
  89. if ($p_attrib->hasFlag(AF_TOTAL))
  90. {
  91. $totalisable = true;
  92. }
  93. }
  94. }
  95. $output.=$eol;
  96. }
  97. // Display the values
  98. for ($i = 0, $_i = count($recordset); $i < $_i; $i++)
  99. {
  100. $output.=$sol;
  101. foreach (array_keys($this->m_node->m_attribList) as $attribname)
  102. {
  103. $p_attrib = &$this->m_node->m_attribList[$attribname];
  104. $musthide=(is_array($suppressList)&&count($suppressList)>0&&in_array($attribname,$suppressList));
  105. if (!$this->isHidden($p_attrib) && !$musthide)
  106. {
  107. // An <attributename>_display function may be provided in a derived
  108. // class to display an attribute.
  109. $funcname = $p_attrib->m_name."_display";
  110. if (method_exists($this->m_node,$funcname))
  111. {
  112. $value=$this->eolreplace($this->m_node->$funcname($recordset[$i], $this->m_mode), $rfeplace);
  113. }
  114. else
  115. {
  116. // otherwise, the display function of the particular attribute
  117. // is called.
  118. $value = $this->eolreplace($p_attrib->display($recordset[$i], $this->m_mode), $rfeplace);
  119. }
  120. if (atkGetCharset() != "" && $decode) $value = atk_html_entity_decode(atk_htmlentities($value, ENT_NOQUOTES), ENT_NOQUOTES);
  121. $output.=$sof.($value==""?$empty:$value).$eof.$fsep;
  122. // Calculate totals..
  123. if ($p_attrib->hasFlag(AF_TOTAL))
  124. {
  125. $totals[$attribname] = $p_attrib->sum($totals[$attribname], $recordset[$i]);
  126. }
  127. }
  128. }
  129. $output.=$eol;
  130. }
  131. // totalrow..
  132. if ($totalisable)
  133. {
  134. $totalRow = $sol;
  135. // Third loop.. this time for the totals row.
  136. foreach (array_keys($this->m_node->m_attribList) as $attribname)
  137. {
  138. $p_attrib = &$this->m_node->m_attribList[$attribname];
  139. $musthide=(is_array($suppressList)&&count($suppressList)>0&&in_array($attribname,$suppressList));
  140. if (!$this->isHidden($p_attrib) && !$musthide)
  141. {
  142. if ($p_attrib->hasFlag(AF_TOTAL))
  143. {
  144. $value = $this->eolreplace($p_attrib->display($totals[$attribname], $this->m_mode), $rfeplace);
  145. $totalRow.=$sof.($value=="" ? $empty : $value).$eof.$fsep;
  146. }
  147. else
  148. {
  149. $totalRow.= $sof.$empty.$eof.$fsep;
  150. }
  151. }
  152. }
  153. $totalRow .= $eol;
  154. $output .= $totalRow;
  155. }
  156. // html requires table tags
  157. if ($type == "0")
  158. {
  159. $output = '<table border="1" cellspacing="0" cellpadding="2">'.$output."</table>";
  160. }
  161. atkdebug(atk_html_entity_decode($output));
  162. // To a File
  163. if (!array_key_exists("filename", $outputparams))
  164. $outputparams["filename"] = "achievo";
  165. if ($this->m_exportcsv)
  166. {
  167. $ext = ($type == "0" ? "html" : "csv");
  168. $exporter = &atknew("atk.utils.atkfileexport");
  169. $exporter->export($output, $outputparams["filename"], $ext, $ext, $compression);
  170. }
  171. else
  172. {
  173. return $output;
  174. }
  175. }
  176. /**
  177. * Is this attribute hidden?
  178. *
  179. * @param atkAttribute $attribute
  180. * @return bool Boolean to indicate if attribute is hidden or not
  181. */
  182. protected function isHidden(atkAttribute $attribute)
  183. {
  184. if ($attribute->hasFlag(AF_HIDE)) return true;
  185. if ($attribute->hasFlag(AF_HIDE_SELECT) && $this->m_node->m_action==='select') return true;
  186. if ($attribute->hasFlag(AF_HIDE_LIST) && ($this->m_node->m_action==='export' || $this->m_mode==='export')) return true;
  187. return false;
  188. }
  189. /**
  190. * Set exporting csv to file
  191. *
  192. * @param bool $export
  193. */
  194. function setExportingCSVToFile($export=true)
  195. {
  196. if (is_bool($export)) $this->m_exportcsv = $export;
  197. }
  198. /**
  199. * Replace any eol character(s) by something else
  200. *
  201. * @param String $string The string to process
  202. * @param String $replacement The replacement string for '\r\n', '\n' and/or '\r'
  203. */
  204. function eolreplace($string, $replacement)
  205. {
  206. if (!is_null($replacement))
  207. {
  208. $string = str_replace("\r\n", $replacement, $string); // prevent double replacement in the next lines!
  209. $string = str_replace("\n", $replacement, $string);
  210. $string = str_replace("\r", $replacement, $string);
  211. }
  212. return $string;
  213. }
  214. }
  215. ?>