PageRenderTime 38ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/webapp/_lib/model/class.Dataset.php

https://github.com/unruthless/ThinkUp
PHP | 138 lines | 54 code | 5 blank | 79 comment | 9 complexity | 124323650a875a2957b6dcfd5ad4b78a MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.1
  1. <?php
  2. /**
  3. *
  4. * ThinkUp/webapp/_lib/model/class.Dataset.php
  5. *
  6. * Copyright (c) 2009-2011 Gina Trapani
  7. *
  8. * LICENSE:
  9. *
  10. * This file is part of ThinkUp (http://thinkupapp.com).
  11. *
  12. * ThinkUp is free software: you can redistribute it and/or modify it under the terms of the GNU General Public
  13. * License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any
  14. * later version.
  15. *
  16. * ThinkUp is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
  18. * details.
  19. *
  20. * You should have received a copy of the GNU General Public License along with ThinkUp. If not, see
  21. * <http://www.gnu.org/licenses/>.
  22. *
  23. *
  24. * Dataset
  25. * Parameters needed to retrieve a set of data to display in ThinkUp.
  26. * @license http://www.gnu.org/licenses/gpl.html
  27. * @copyright 2009-2011 Gina Trapani
  28. * @author Gina Trapani <ginatrapani[at]gmail[dot]com>
  29. *
  30. */
  31. class Dataset {
  32. /**
  33. * @var str
  34. */
  35. var $name;
  36. /**
  37. * @var str
  38. */
  39. var $dao_name;
  40. /**
  41. *
  42. * @var str
  43. */
  44. var $dao_method_name;
  45. /**
  46. *
  47. * @var array
  48. */
  49. var $method_params;
  50. /**
  51. *
  52. * @var str
  53. */
  54. var $iterator_method_name;
  55. /**
  56. *
  57. * @var array
  58. */
  59. var $iterator_method_params;
  60. /**
  61. *
  62. * @var array String of allowed DAO names
  63. */
  64. var $FETCHING_DAOS = array('FollowDAO', 'PostDAO', 'LinkDAO', 'FollowerCountDAO', 'FavoritePostDAO',
  65. 'PluginOptionDAO');
  66. /**
  67. * Constructor
  68. * @param str $name
  69. * @param str $dao_name
  70. * @param str $dao_method_name
  71. * @param array $method_params
  72. * @return Dataset
  73. */
  74. public function __construct($name, $dao_name, $dao_method_name, $method_params=array(),
  75. $iterator_method_name = null, $iterator_method_params = array()) {
  76. $this->name = $name;
  77. if (in_array($dao_name, $this->FETCHING_DAOS)) {
  78. $this->dao_name = $dao_name;
  79. $this->dao_method_name = $dao_method_name;
  80. $this->method_params = $method_params;
  81. if( isset($iterator_method_name) ) {
  82. $this->iterator_method_name = $iterator_method_name;
  83. $this->iterator_method_params = $iterator_method_params;
  84. }
  85. } else {
  86. throw new Exception($dao_name . ' is not one of the allowed DAOs');
  87. }
  88. }
  89. /**
  90. * Retrieve dataset
  91. * Run the specified DAO method and return results
  92. * @param int $page_number Page number of the list
  93. * @return array DAO method results
  94. */
  95. public function retrieveDataset($page_number=1) {
  96. $dao = DAOFactory::getDAO($this->dao_name);
  97. if (method_exists($dao, $this->dao_method_name)) {
  98. $page_pos = array_search('#page_number#', $this->method_params);
  99. if ($page_pos !== false) {
  100. $this->method_params[$page_pos] = $page_number;
  101. }
  102. return call_user_func_array(array($dao, $this->dao_method_name), $this->method_params);
  103. } else {
  104. throw new Exception($this->dao_name . ' does not have a ' . $this->dao_method_name . ' method.');
  105. }
  106. }
  107. /**
  108. * Is this tab searchable
  109. * Returns true if there is an Iterator method defined for this tab
  110. * @return boolean
  111. */
  112. public function isSearchable() {
  113. return isset($this->iterator_method_name);
  114. }
  115. /**
  116. * Retrieve Iterator
  117. * Run the specified DAO Iterator method and return results
  118. * @return PostIterator
  119. */
  120. public function retrieveIterator() {
  121. $dao = DAOFactory::getDAO($this->dao_name);
  122. $iterator = null;
  123. if(! is_null($this->iterator_method_name) ) {
  124. if (method_exists($dao, $this->iterator_method_name)) {
  125. $iterator = call_user_func_array(array($dao, $this->iterator_method_name),
  126. $this->iterator_method_params);
  127. } else {
  128. throw new Exception($this->dao_name . ' does not have a ' . $this->dao_method_name . ' method.');
  129. }
  130. }
  131. return $iterator;
  132. }
  133. }