Source for file EmbeddedArrayResultPrinter.class.php

Documentation is available at EmbeddedArrayResultPrinter.class.php

  1. <?php
  2. /**
  3. * @package polyphony.resultprinter
  4. *
  5. * @copyright Copyright &copy; 2005, Middlebury College
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  7. *
  8. * @version $Id: EmbeddedArrayResultPrinter.class.php,v 1.12 2007/09/19 14:04:49 adamfranco Exp $
  9. */
  10.  
  11. require_once(dirname(__FILE__)."/ResultPrinter.abstract.php");
  12.  
  13. /**
  14. * Print out an Array of items in rows and columns in a TABLE HTML element.
  15. *
  16. * @package polyphony.resultprinter
  17. *
  18. * @copyright Copyright &copy; 2005, Middlebury College
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  20. *
  21. * @version $Id: EmbeddedArrayResultPrinter.class.php,v 1.12 2007/09/19 14:04:49 adamfranco Exp $
  22. */
  23.  
  24. class EmbeddedArrayResultPrinter
  25. extends ResultPrinter
  26. {
  27.  
  28. var $_array;
  29. var $_numColumns;
  30. var $_pageSize;
  31. var $_overridePage = -1;
  32. var $_callbackFunction;
  33. var $_tableStyle = '';
  34. var $_tdStyle = 'padding: 3px;';
  35. var $_trStyle = '';
  36. var $_currentItem;
  37. /**
  38. * Constructor
  39. *
  40. * @param object $iterator The iterator to print.
  41. * @param integer $numColumns The number of result columns to print on each page.
  42. * @param integer $numResultsPerPage The number of iterator items to print on a page.
  43. * @param string $callbackFunction The name of the function that will be called to
  44. * to print each result.
  45. * @param optional mixed $callbackArgs Any additional arguements will be stored
  46. * and passed on to the callback function.
  47. * @access public
  48. * @date 8/5/04
  49. */
  50. function EmbeddedArrayResultPrinter ($array, $numColumns,
  51. $numResultsPerPage, $callbackFunction) {
  52. ArgumentValidator::validate($array, ArrayValidatorRule::getRule());
  53. ArgumentValidator::validate($numColumns, IntegerValidatorRule::getRule());
  54. ArgumentValidator::validate($numResultsPerPage, IntegerValidatorRule::getRule());
  55. // ArgumentValidator::validate($callbackFunction, StringValidatorRule::getRule());
  56.  
  57. $this->_array =$array;
  58. $this->_numColumns =$numColumns;
  59. $this->_pageSize =$numResultsPerPage;
  60. $this->_callbackFunction =$callbackFunction;
  61.  
  62. $this->_callbackParams = array();
  63. $args = func_get_args();
  64. for ($i=4; $i<count($args); $i++) {
  65. $this->_callbackParams[] =$args[$i];
  66. }
  67. }
  68.  
  69. /**
  70. * Sets the style of the TD elements of this table.
  71. *
  72. * @return void
  73. ***/
  74. function setTDStyle($style)
  75. {
  76. $this->_tdStyle = $style;
  77. }
  78.  
  79. /**
  80. * Sets the style of the TR elements of this table.
  81. *
  82. * @return void
  83. ***/
  84. function setTRStyle($style)
  85. {
  86. $this->_trStyle = $style;
  87. }
  88.  
  89. /**
  90. * Sets the style of the TABLE element.
  91. *
  92. * @return void
  93. ***/
  94. function setTableStyle($style)
  95. {
  96. $this->_tableStyle = $style;
  97. }
  98.  
  99. /**
  100. * Creates the HTML table element(s) for the passed content.
  101. * @param string $content
  102. * @access protected
  103. *
  104. * @return void
  105. ***/
  106. function createItemElement($content)
  107. {
  108. return $this->createTDElement($content);
  109. }
  110. /**
  111. * Builds the header row of this result table.
  112. *
  113. * @return string
  114. ***/
  115. function createHeaderRow()
  116. {
  117. return '';
  118. }
  119. /**
  120. * Builds the footer row of this result table.
  121. *
  122. * @return string
  123. ***/
  124. function createFooterRow()
  125. {
  126. return '';
  127. }
  128. /**
  129. * Creates a table TD element with the passed content.
  130. * @param string $content
  131. * @param optional integer $colspan The number of columns for this element to span.
  132. * @param optional string $align The text alignment.
  133. *
  134. * @return string
  135. ***/
  136. function createTDElement($content, $colspan = 0, $align='left')
  137. {
  138. $tdStyle = "style='".addslashes($this->_tdStyle)."'";
  139. $colspanAttr = '';
  140. if ($colspan > 0) $colspanAttr = " colspan='$colspan'";
  141. return "<td align='$align' $tdStyle$colspanAttr>$content</td>\n";
  142. }
  143. /**
  144. * Returns the starting tag of a TR element.
  145. *
  146. * @return string
  147. ***/
  148. function createTRElement()
  149. {
  150. $trStyle = "style='".addslashes($this->_trStyle)."'";
  151. return "<tr $trStyle>\n";
  152. }
  153.  
  154. /**
  155. * Called if it is desired to override the page number that is displayed.
  156. * @param integer $number
  157. *
  158. * @return void
  159. ***/
  160. function overridePageNumber($number)
  161. {
  162. $this->_overridePage = ($number-1) * $this->_pageSize + 1;
  163. }
  164.  
  165. /**
  166. * Returns a block of HTML markup.
  167. *
  168. * @param optional string $shouldPrintFunction The name of a function that will
  169. * return a boolean specifying whether or not to filter a given result.
  170. * If null, all results are printed.
  171. * @return string
  172. * @access public
  173. * @date 8/5/04
  174. */
  175. function getMarkup ($shouldPrintFunction = NULL) {
  176. $defaultTextDomain = textdomain("polyphony");
  177. $harmoni = Harmoni::instance();
  178.  
  179. $startingNumber = $this->getStartingNumber();
  180. if ($this->_overridePage > 0) $startingNumber = $this->_overridePage;
  181.  
  182. $markup = '';
  183. $currentCol = 0;
  184.  
  185. $tableStyle = "style='".addslashes($this->_tableStyle)."'";
  186.  
  187. $table = "<table $tableStyle width='100%' border='0' cellpadding='0' cellspacing='0'>\n";
  188. $table .= $this->createHeaderRow();
  189. $table .= $this->createTRElement();
  190.  
  191. $endingNumber = $startingNumber+$this->_pageSize-1;
  192. $numItems = 0;
  193. $shouldPrintEval = $shouldPrintFunction?"\$shouldPrint = ".$shouldPrintFunction."(\$item);":"\$shouldPrint = true;";
  194. if (count($this->_array)) {
  195.  
  196. reset($this->_array);
  197.  
  198. // trash the items before our starting number
  199. while ($numItems+1 < $startingNumber && $numItems < count($this->_array)) {
  200. $item =$this->_array[key($this->_array)];
  201. next($this->_array);
  202.  
  203. // Ignore this if it should be filtered.
  204. eval($shouldPrintEval);
  205. if ($shouldPrint)
  206. $numItems++;
  207. }
  208.  
  209. // print up to $this->_pageSize items
  210. $pageItems = 0;
  211. while ($numItems < $endingNumber && $numItems < count($this->_array)) {
  212. $item =$this->_array[key($this->_array)];
  213. next($this->_array);
  214.  
  215. // Only Act if this item isn't to be filtered.
  216.  
  217. eval($shouldPrintEval);
  218. if ($shouldPrint) {
  219. $this->_currentItem =$item;
  220. $numItems++;
  221. $pageItems++;
  222.  
  223. $itemArray = array ($item);
  224. $params = array_merge($itemArray, $this->_callbackParams);
  225. // Add in our starting number to the end so that that it is accessible.
  226. $params[] = $numItems;
  227. $itemMarkup = call_user_func_array(
  228. $this->_callbackFunction, $params);
  229. if ($currentCol == $this->_numColumns) {
  230. $markup .= "</tr>" . $this->createTRElement();
  231. $currentCol = 0;
  232. }
  233. $markup .= $this->createItemElement($itemMarkup);
  234. $currentCol++;
  235. $this->_currentItem = null;
  236. }
  237. }
  238.  
  239. //if we have a partially empty last row, add more empty layouts
  240. // to better-align the columns
  241. while($currentCol < $this->_numColumns) {
  242. $currentCol++;
  243. $markup .= $this->createTDElement("&nbsp;");
  244. }
  245. // find the count of items
  246. while (true) {
  247. $item =$this->_array[key($this->_array)];
  248. if (!$item) break;
  249. next($this->_array);
  250. // Ignore this if it should be filtered.
  251. eval($shouldPrintEval);
  252. if ($shouldPrint)
  253. $numItems++;
  254. }
  255. } else {
  256. $markup .= $this->createTDElement("<ul><li>"._("No items are available.")."</li></ul>\n", $this->_numColumns);
  257. }
  258. $markup .= "</tr>\n";
  259.  
  260. $table .= $markup;
  261. /*********************************************************
  262. * Page Links
  263. * ------------
  264. * print out links to skip to more items if the number of Items is greater
  265. * than the number we display on the page
  266. *********************************************************/
  267. if ($pageLinks = $this->getPageLinks($startingNumber, $numItems)) {
  268. $table .= $this->createTRElement().$this->createTDElement($pageLinks, $this->_numColumns, "right")."</tr>";
  269. }
  270. $table .= $this->createFooterRow();
  271. $table .= "</table>\n";
  272.  
  273. textdomain($defaultTextDomain);
  274. return $table;
  275. }
  276. }
  277.  
  278. ?>

Documentation generated on Wed, 19 Sep 2007 10:23:01 -0400 by phpDocumentor 1.3.0RC3