Source for file ArrayResultPrinter.class.php

Documentation is available at ArrayResultPrinter.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: ArrayResultPrinter.class.php,v 1.27 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 of TEXT_BLOCK widgets
  15. * spread over multiple pages.
  16. *
  17. * @package polyphony.resultprinter
  18. *
  19. * @copyright Copyright &copy; 2005, Middlebury College
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  21. *
  22. * @version $Id: ArrayResultPrinter.class.php,v 1.27 2007/09/19 14:04:49 adamfranco Exp $
  23. */
  24.  
  25. class ArrayResultPrinter
  26. extends ResultPrinter
  27. {
  28. /**
  29. * Constructor
  30. *
  31. * @param object $iterator The iterator to print.
  32. * @param integer $numColumns The number of result columns to print on each page.
  33. * @param integer $numResultsPerPage The number of iterator items to print on a page.
  34. * @param mixed $callbackFunction The name of the function that will be called to
  35. * to print each result. If null, the first parameter is assumed to be an
  36. * array of gui components that can be rendered without further processing.
  37. * @param optional mixed $callbackArgs Any additional arguements will be stored
  38. * and passed on to the callback function.
  39. * @access public
  40. * @date 8/5/04
  41. */
  42. function ArrayResultPrinter ($array, $numColumns,
  43. $numResultsPerPage, $callbackFunction = NULL) {
  44. ArgumentValidator::validate($array, ArrayValidatorRule::getRule());
  45. ArgumentValidator::validate($numColumns, IntegerValidatorRule::getRule());
  46. ArgumentValidator::validate($numResultsPerPage, IntegerValidatorRule::getRule());
  47. // ArgumentValidator::validate($callbackFunction, StringValidatorRule::getRule());
  48. if (is_null($callbackFunction))
  49. ArgumentValidator::validate($array, ArrayValidatorRuleWithRule::getRule(
  50. ExtendsValidatorRule::getRule("ComponentInterface")));
  51. $this->_array = $array;
  52. $this->_numColumns = $numColumns;
  53. $this->_pageSize = $numResultsPerPage;
  54. $this->_callbackFunction = $callbackFunction;
  55. $this->_callbackParams = array();
  56. $args = func_get_args();
  57. for ($i=4; $i<count($args); $i++) {
  58. $this->_callbackParams[] =$args[$i];
  59. }
  60. $this->_resultLayout = new TableLayout($this->_numColumns);
  61. $this->_resultLayout->printEmptyCells = false;
  62. $this->_linksStyleCollection = new StyleCollection(
  63. "*.result_page_links",
  64. "result_page_links",
  65. "Result Page Links",
  66. "Links to other pages of results.");
  67. // $this->_linksStyleCollection->addSP(new MarginTopSP("10px"));
  68. }
  69. /**
  70. * Set the direction of component rendering from the default of Left-Right/Top-Bottom.
  71. * Allowed values:
  72. * Left-Right/Top-Bottom
  73. * Top-Bottom/Left-Right
  74. * Right-Left/Top-Bottom
  75. * Top-Bottom/Right-Left
  76. *
  77. * The other possible directions, listed below, are not implemented due to
  78. * lack of utility:
  79. * Left-Right/Bottom-Top
  80. * Bottom-Top/Left-Right
  81. * Right-Left/Bottom-Top
  82. * Bottom-Top/Right-Left
  83. *
  84. * @param string $direction
  85. * @return void
  86. * @access public
  87. * @since 8/18/06
  88. */
  89. function setRenderDirection ($direction) {
  90. $this->_resultLayout->setRenderDirection($direction);
  91. }
  92. /**
  93. * Set the tdStyles
  94. *
  95. * @param string $tdStyles
  96. * @return void
  97. * @access public
  98. * @since 9/18/06
  99. */
  100. function setTdStyles ($tdStyles) {
  101. $this->_resultLayout->setTdStyles($tdStyles);
  102. }
  103. /**
  104. * Add style properties to the links
  105. *
  106. * @param object StyleProperty $styleProperty
  107. * @return void
  108. * @access public
  109. * @since 9/19/06
  110. */
  111. function addLinksStyleProperty ($styleProperty) {
  112. $this->_linksStyleCollection->addSP($styleProperty);
  113. }
  114. /**
  115. * Returns a layout of the Results
  116. *
  117. * @param optional string $shouldPrintFunction The name of a function that will
  118. * return a boolean specifying whether or not to filter a given result.
  119. * If null, all results are printed.
  120. * @return object Layout A layout containing the results/page links
  121. * @access public
  122. * @date 8/5/04
  123. */
  124. function getLayout ($shouldPrintFunction = NULL) {
  125. $defaultTextDomain = textdomain("polyphony");
  126. $startingNumber = $this->getStartingNumber();
  127. $yLayout = new YLayout();
  128. $container = new Container($yLayout,OTHER,1);
  129. $endingNumber = $startingNumber+$this->_pageSize-1;
  130. $numItems = 0;
  131. $resultContainer = new Container($this->_resultLayout, OTHER, 1);
  132. $shouldPrintEval = $shouldPrintFunction?"\$shouldPrint = ".$shouldPrintFunction."(\$item);":"\$shouldPrint = true;";
  133. if (count($this->_array)) {
  134. reset($this->_array);
  135. // trash the items before our starting number
  136. while ($numItems+1 < $startingNumber && $numItems < count($this->_array)) {
  137. $item = current($this->_array);
  138. next($this->_array);
  139. // Ignore this if it should be filtered.
  140. eval($shouldPrintEval);
  141. if ($shouldPrint)
  142. $numItems++;
  143. }
  144. // print up to $this->_pageSize items
  145. $pageItems = 0;
  146. while ($numItems < $endingNumber && $numItems < count($this->_array) && current($this->_array)) {
  147. $item = current($this->_array);
  148. next($this->_array);
  149. // Only Act if this item isn't to be filtered.
  150. eval($shouldPrintEval);
  151. if ($shouldPrint) {
  152. $numItems++;
  153. $pageItems++;
  154. $itemArray = array ($item);
  155. $params = array_merge($itemArray, $this->_callbackParams);
  156. // Add in our starting number to the end so that that it is accessible.
  157. $params[] = $numItems;
  158. // If the callback function is null, assume that we have an
  159. // array of GUI components that can be used directly
  160. if (is_null($this->_callbackFunction)) {
  161. $itemComponent = $item;
  162. } else {
  163. // The following call returns an object, but notices are given
  164. // if an '&' is used.
  165. $itemComponent = call_user_func_array(
  166. $this->_callbackFunction, $params);
  167. }
  168. $resultContainer->add($itemComponent, null, null, CENTER, TOP);
  169. // If $itemComponent is not unset, since it is an object,
  170. // it may references to it made in add() will be changed.
  171. unset($itemComponent);
  172. }
  173. }
  174. //if we have a partially empty last row, add more empty layouts
  175. // to better-align the columns
  176. // while ($pageItems % $this->_numColumns != 0) {
  177. // $currentRow->addComponent(new Content(" &nbsp; "));
  178. // $pageItems++;
  179. // }
  180. // find the count of items
  181. while (true) {
  182. $item = current($this->_array);
  183. if (!$item) break;
  184. next($this->_array);
  185. // Ignore this if it should be filtered.
  186. if (!is_object($item))
  187. var_dump($item);
  188. eval($shouldPrintEval);
  189. if ($shouldPrint)
  190. $numItems++;
  191. }
  192. } else {
  193. $text = new Block("<ul><li>"._("No items are available.")."</li></ul>", STANDARD_BLOCK);
  194. $resultContainer->add($text, null, null, CENTER, CENTER);
  195. }
  196. /*********************************************************
  197. * Page Links
  198. * ------------
  199. * print out links to skip to more items if the number of Items is greater
  200. * than the number we display on the page
  201. *********************************************************/
  202. if ($linksHTML = $this->getPageLinks($startingNumber, $numItems)) {
  203. // Add the links to the page
  204. $pageLinkBlock = new Block($linksHTML, BACKGROUND_BLOCK);
  205. $container->add($pageLinkBlock, "100%", null, CENTER, CENTER);
  206. $pageLinkBlock->addStyle($this->_linksStyleCollection);
  207. }
  208. $container->add($resultContainer, "100%", null, LEFT, CENTER);
  209. if ($numItems > $this->_pageSize) {
  210. $container->add($pageLinkBlock, null, null, CENTER, CENTER);
  211. }
  212. textdomain($defaultTextDomain);
  213. return $container;
  214. }
  215. }
  216.  
  217. ?>

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