Source for file IteratorResultPrinter.class.php

Documentation is available at IteratorResultPrinter.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: IteratorResultPrinter.class.php,v 1.30 2007/09/19 14:04:49 adamfranco Exp $
  9. */
  10. require_once(dirname(__FILE__)."/ResultPrinter.abstract.php");
  11. require_once(HARMONI."GUIManager/StyleProperties/MarginTopSP.class.php");
  12. /**
  13. * Print out an Iterator of items in rows and columns of TEXT_BLOCK widgets
  14. * spread over multiple pages.
  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: IteratorResultPrinter.class.php,v 1.30 2007/09/19 14:04:49 adamfranco Exp $
  22. */
  23.  
  24. class IteratorResultPrinter
  25. extends ResultPrinter
  26. {
  27. /**
  28. * Constructor
  29. *
  30. * @param object $iterator The iterator to print.
  31. * @param integer $numColumns The number of result columns to print on each page.
  32. * @param integer $numResultsPerPage The number of iterator items to print on a page.
  33. * @param string $callbackFunction The name of the function that will be called to
  34. * to print each result.
  35. * @param optional mixed $callbackArgs Any additional arguements will be stored
  36. * and passed on to the callback function.
  37. * @access public
  38. * @date 8/5/04
  39. */
  40. function IteratorResultPrinter ($iterator, $numColumns,
  41. $numResultsPerPage, $callbackFunction) {
  42. ArgumentValidator::validate($iterator, new HasMethodsValidatorRule("hasNext", "next"));
  43. ArgumentValidator::validate($numColumns, new IntegerValidatorRule);
  44. ArgumentValidator::validate($numResultsPerPage, new IntegerValidatorRule);
  45. ArgumentValidator::validate($callbackFunction, new StringValidatorRule);
  46. $this->_iterator =$iterator;
  47. $this->_numColumns = $numColumns;
  48. $this->_pageSize = $numResultsPerPage;
  49. $this->_callbackFunction = $callbackFunction;
  50. $this->_callbackParams = array();
  51. $args = func_get_args();
  52. for ($i=4; $i<count($args); $i++) {
  53. $this->_callbackParams[] =$args[$i];
  54. }
  55. $this->_resultLayout = new TableLayout($this->_numColumns);
  56. }
  57. /**
  58. * Set the direction of component rendering from the default of Left-Right/Top-Bottom.
  59. * Allowed values:
  60. * Left-Right/Top-Bottom
  61. * Top-Bottom/Left-Right
  62. * Right-Left/Top-Bottom
  63. * Top-Bottom/Right-Left
  64. *
  65. * The other possible directions, listed below, are not implemented due to
  66. * lack of utility:
  67. * Left-Right/Bottom-Top
  68. * Bottom-Top/Left-Right
  69. * Right-Left/Bottom-Top
  70. * Bottom-Top/Right-Left
  71. *
  72. * @param string $direction
  73. * @return void
  74. * @access public
  75. * @since 8/18/06
  76. */
  77. function setRenderDirection ($direction) {
  78. $this->_resultLayout->setRenderDirection($direction);
  79. }
  80. /**
  81. * Returns a layout of the Results
  82. *
  83. * @param optional string $shouldPrintFunction The name of a function that will
  84. * return a boolean specifying whether or not to filter a given result.
  85. * If null, all results are printed.
  86. * @return object Layout A layout containing the results/page links
  87. * @access public
  88. * @date 8/5/04
  89. */
  90. function getLayout ($shouldPrintFunction = NULL) {
  91. $defaultTextDomain = textdomain("polyphony");
  92. $startingNumber = $this->getStartingNumber();
  93. $yLayout = new YLayout();
  94. $container = new Container($yLayout,OTHER,1);
  95. $endingNumber = $startingNumber+$this->_pageSize-1;
  96. $numItems = 0;
  97. $resultContainer = new Container($this->_resultLayout, OTHER, 1);
  98. if ($this->_iterator->hasNext()) {
  99. // trash the items before our starting number
  100. while ($this->_iterator->hasNext() && $numItems+1 < $startingNumber) {
  101. $item =$this->_iterator->next();
  102. // Ignore this if it should be filtered.
  103. if (!$shouldPrintFunction || $shouldPrintFunction($item))
  104. $numItems++;
  105. }
  106. // print up to $this->_pageSize items
  107. $pageItems = 0;
  108. while ($this->_iterator->hasNext() && $numItems < $endingNumber) {
  109. $item =$this->_iterator->next();
  110. // Only Act if this item isn't to be filtered.
  111. eval('$shouldPrint = (!$shouldPrintFunction || '.$shouldPrintFunction.'($item));');
  112. if ($shouldPrint) {
  113. $numItems++;
  114. $pageItems++;
  115. $itemArray = array ($item);
  116. $params = array_merge($itemArray, $this->_callbackParams);
  117. // Add in our starting number to the end so that that it is accessible.
  118. $params[] = $numItems;
  119. $itemLayout = call_user_func_array(
  120. $this->_callbackFunction, $params);
  121. $resultContainer->add($itemLayout,
  122. floor(100/$this->_numColumns)."%",
  123. "100%",
  124. CENTER, TOP);
  125. // If $itemLayout is not unset, since it is an object,
  126. // it may references to it made in add() will be changed.
  127. unset($itemLayout);
  128. }
  129. }
  130. //if we have a partially empty last row, add more empty layouts
  131. // to better-align the columns
  132. // while ($pageItems % $this->_numColumns != 0) {
  133. // $currentRow->addComponent(new Content(" &nbsp; "));
  134. // $pageItems++;
  135. // }
  136. // find the count of items
  137. while ($this->_iterator->hasNext()) {
  138. $item =$this->_iterator->next();
  139. // Ignore this if it should be filtered.
  140. eval('$shouldPrint = (!$shouldPrintFunction || '.$shouldPrintFunction.'($item));');
  141. if ($shouldPrint)
  142. $numItems++;
  143. }
  144. } else {
  145. $text = new Block("<ul><li>"._("No items are available.")."</li></ul>", STANDARD_BLOCK);
  146. $resultContainer->add($text, null, null, CENTER, CENTER);
  147. }
  148. /*********************************************************
  149. * Page Links
  150. * ------------
  151. * print out links to skip to more items if the number of Items is greater
  152. * than the number we display on the page
  153. *********************************************************/
  154. if ($linksHTML = $this->getPageLinks($startingNumber, $numItems)) {
  155. // Add the links to the page
  156. $pageLinkBlock = new Block($linksHTML, BACKGROUND_BLOCK);
  157. $container->add($pageLinkBlock, "100%", null, CENTER, CENTER);
  158. $styleCollection = new StyleCollection("*.result_page_links", "result_page_links", "Result Page Links", "Links to other pages of results.");
  159. $styleCollection->addSP(new MarginTopSP("10px"));
  160. $pageLinkBlock->addStyle($styleCollection);
  161. }
  162. $container->add($resultContainer, "100%", null, LEFT, CENTER);
  163. if ($numItems > $this->_pageSize) {
  164. $container->add($pageLinkBlock, null, null, CENTER, CENTER);
  165. }
  166. textdomain($defaultTextDomain);
  167. return $container;
  168. }
  169. }
  170.  
  171. ?>

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