Source for file TableIteratorResultPrinter.class.php

Documentation is available at TableIteratorResultPrinter.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: TableIteratorResultPrinter.class.php,v 1.17 2007/09/19 14:04:49 adamfranco Exp $
  9. */
  10. require_once(dirname(__FILE__)."/ResultPrinter.abstract.php");
  11. /**
  12. * Print out an Iterator of items in rows and columns of TEXT_BLOCK widgets
  13. * spread over multiple pages.
  14. *
  15. * @package polyphony.resultprinter
  16. *
  17. * @copyright Copyright &copy; 2005, Middlebury College
  18. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  19. *
  20. * @version $Id: TableIteratorResultPrinter.class.php,v 1.17 2007/09/19 14:04:49 adamfranco Exp $
  21. */
  22.  
  23. class TableIteratorResultPrinter
  24. extends ResultPrinter
  25. {
  26. /**
  27. * Constructor
  28. *
  29. * @param object $iterator The iterator to print.
  30. * @param string $headRow The heading row.
  31. * @param integer $numResultsPerPage The number of iterator items to print on a page.
  32. * @param string $callbackFunction The name of the function that will be called to
  33. * to print each result.
  34. * @param optional mixed $callbackArgs Any additional arguements will be stored
  35. * and passed on to the callback function.
  36. * @access public
  37. * @date 8/5/04
  38. */
  39. function TableIteratorResultPrinter ($iterator, $headRow, $numResultsPerPage,
  40. $callbackFunction, $tableBorder = 0)
  41. {
  42. ArgumentValidator::validate($iterator, new HasMethodsValidatorRule("hasNext", "next"));
  43. ArgumentValidator::validate($headRow, new StringValidatorRule);
  44. ArgumentValidator::validate($numResultsPerPage, new IntegerValidatorRule);
  45. ArgumentValidator::validate($callbackFunction, new StringValidatorRule);
  46. ArgumentValidator::validate($tableBorder, new IntegerValidatorRule);
  47. $this->_iterator =$iterator;
  48. $this->_headRow =$headRow;
  49. preg_match_all("/<th>|<td>/", $headRow, $matches);
  50. $this->_numColumns = count($matches[0]);
  51. $this->_tableBorder = $tableBorder;
  52. $this->_pageSize =$numResultsPerPage;
  53. $this->_callbackFunction =$callbackFunction;
  54. $this->_callbackParams = array();
  55. $args = func_get_args();
  56. for ($i=4; $i<count($args); $i++) {
  57. $this->_callbackParams[] =$args[$i];
  58. }
  59. }
  60. /**
  61. * Returns a layout of the Results
  62. *
  63. * @param object Harmoni $harmoni The Harmoni object containing context data.
  64. * @param optional string $shouldPrintFunction The name of a function that will
  65. * return a boolean specifying whether or not to filter a given result.
  66. * If null, all results are printed.
  67. * @return string A table string.
  68. * @access public
  69. * @date 8/5/04
  70. */
  71. function getTable ($shouldPrintFunction = NULL) {
  72. $harmoni = Harmoni::instance();
  73. $defaultTextDomain = textdomain("polyphony");
  74. $startingNumber = $this->getStartingNumber();
  75. // print out all of the rows.
  76. $endingNumber = $startingNumber+$this->_pageSize-1;
  77. $numItems = 0;
  78. if ($this->_iterator->hasNext()) {
  79. ob_start();
  80. // trash the items before our starting number
  81. while ($this->_iterator->hasNext() && $numItems+1 < $startingNumber) {
  82. if (!$shouldPrintFunction) {
  83. $this->_iterator->skipNext();
  84. $numItems++;
  85. } else {
  86. $item =$this->_iterator->next();
  87. // Ignore this if it should be filtered.
  88. eval('$shouldPrint = ('.$shouldPrintFunction.'($item));');
  89. if ($shouldPrint)
  90. $numItems++;
  91. }
  92. }
  93. // print up to $this->_pageSize items
  94. $pageItems = 0;
  95. while ($this->_iterator->hasNext() && $numItems < $endingNumber) {
  96. $item =$this->_iterator->next();
  97. // Only Act if this item isn't to be filtered.
  98. eval('$shouldPrint = (!$shouldPrintFunction || '.$shouldPrintFunction.'($item));');
  99. if ($shouldPrint) {
  100. $numItems++;
  101. $pageItems++;
  102. $itemArray = array ($item);
  103. $params = array_merge($itemArray, $this->_callbackParams);
  104. // Add in our starting number to the end so that that it is accessible.
  105. $params[] = $numItems;
  106. print call_user_func_array($this->_callbackFunction, $params);
  107. }
  108. }
  109. // find the count of items
  110. if (!$shouldPrintFunction) {
  111. $numItems = $this->_iterator->count();
  112. } else {
  113. while ($this->_iterator->hasNext()) {
  114. $item =$this->_iterator->next();
  115. // Ignore this if it should be filtered.
  116. eval('$shouldPrint = ('.$shouldPrintFunction.'($item));');
  117. if ($shouldPrint)
  118. $numItems++;
  119. }
  120. }
  121. $rows = ob_get_clean();
  122. } else {
  123. $rows = "\n\t<tr>\n\t\t<td colspan='".$this->_numColumns."'>"._("No items are available.")."</td>\n\t</tr>";
  124. }
  125. /*********************************************************
  126. * Page Links
  127. * ------------
  128. * print out links to skip to more items if the number of Items is greater
  129. * than the number we display on the page
  130. *********************************************************/
  131. if ($linksHTML = $this->getPageLinks($startingNumber, $numItems)) {
  132. $linksRow = "\n\t<tr>\n\t\t<td colspan='".$this->_numColumns."' style='text-align: right'>\n\t\t\t".$linksHTML."\n\t\t</td>\n\t</tr>";
  133. } else {
  134. $linksRow = '';
  135. }
  136. ob_start();
  137. print "\n<table border='".$this->_tableBorder."'>";
  138. print $linksRow;
  139. print $this->_headRow;
  140. print $rows;
  141. print $linksRow;
  142. print "\n</table>";
  143. textdomain($defaultTextDomain);
  144. return ob_get_clean();
  145. }
  146. }
  147.  
  148. ?>

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