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

/aamenu/code/trunk/administrator/components/com_aamenu/libraries/jxtended/application/component/modellist.php

https://bitbucket.org/eddieajau/the-art-of-joomla-archive
PHP | 226 lines | 77 code | 29 blank | 120 comment | 5 complexity | 43cc76f2cad2adb14a7f61a2fc09b6e6 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: modellist.php 36 2009-05-19 01:06:55Z eddieajau $
  4. * @package JXtended.Libraries
  5. * @subpackage Application.Component
  6. * @copyright Copyright (C) 2008 - 2009 JXtended, LLC. All rights reserved.
  7. * @license GNU General Public License <http://www.gnu.org/copyleft/gpl.html>
  8. * @link http://jxtended.com
  9. */
  10. defined('JPATH_BASE') or die;
  11. jimport('joomla.application.component.model');
  12. jximport('jxtended.database.query');
  13. /**
  14. * Prototype list model.
  15. *
  16. * @package JXtended.Libraries
  17. * @subpackage Application
  18. * @version 1.0
  19. */
  20. class JXModelList extends JModel
  21. {
  22. /**
  23. * Flag to indicate model state initialization.
  24. *
  25. * @access private
  26. * @var boolean
  27. */
  28. var $__state_set = false;
  29. /**
  30. * An array of totals for the lists.
  31. *
  32. * @access protected
  33. * @var array
  34. */
  35. var $_totals = array();
  36. /**
  37. * Array of lists containing items.
  38. *
  39. * @access protected
  40. * @var array
  41. */
  42. var $_lists = array();
  43. /**
  44. * Model context string.
  45. *
  46. * @access protected
  47. * @var string
  48. */
  49. var $_context = 'group.type';
  50. /**
  51. * Overridden model constructor.
  52. *
  53. * @access public
  54. * @param array $config Configuration array
  55. * @return void
  56. * @since 1.0
  57. */
  58. function __construct($config = array())
  59. {
  60. // If ignore request flag is set, set the state set flag.
  61. if (!empty($config['ignore_request'])) {
  62. $this->__state_set = true;
  63. }
  64. parent::__construct($config);
  65. }
  66. /**
  67. * Overridden method to get model state variables.
  68. *
  69. * @access public
  70. * @param string $property Optional parameter name.
  71. * @return object The property where specified, the state object where omitted.
  72. * @since 1.0
  73. */
  74. function getState($property = null, $default = null)
  75. {
  76. if (!$this->__state_set)
  77. {
  78. // Private method to auto-populate the model state.
  79. $this->_populateState();
  80. // Set the model state set flat to true.
  81. $this->__state_set = true;
  82. }
  83. $value = parent::getState($property);
  84. return (is_null($value) ? $default : $value);
  85. }
  86. /**
  87. * Method to get a list of items.
  88. *
  89. * @access public
  90. * @return mixed An array of objects on success, false on failure.
  91. * @since 1.0
  92. */
  93. function &getItems()
  94. {
  95. // Get a unique key for the current list state.
  96. $key = $this->_getStoreId($this->_context);
  97. // Try to load the value from internal storage.
  98. if (!empty ($this->_lists[$key])) {
  99. return $this->_lists[$key];
  100. }
  101. // Load the list.
  102. $query = $this->_getListQuery();
  103. $rows = $this->_getList($query->toString(), $this->getState('list.start'), $this->getState('list.limit'));
  104. // Add the rows to the internal storage.
  105. $this->_lists[$key] = $rows;
  106. return $this->_lists[$key];
  107. }
  108. /**
  109. * Method to get a list pagination object.
  110. *
  111. * @access public
  112. * @return object A JPagination object.
  113. * @since 1.0
  114. */
  115. function &getPagination()
  116. {
  117. jimport('joomla.html.pagination');
  118. // Create the pagination object.
  119. $instance = new JPagination($this->getTotal(), (int)$this->getState('list.start'), (int)$this->getState('list.limit'));
  120. return $instance;
  121. }
  122. /**
  123. * Method to get the total number of published items.
  124. *
  125. * @access public
  126. * @return int The number of published items.
  127. * @since 1.0
  128. */
  129. function getTotal()
  130. {
  131. // Get a unique key for the current list state.
  132. $key = $this->_getStoreId($this->_context);
  133. // Try to load the value from internal storage.
  134. if (!empty ($this->_totals[$key])) {
  135. return $this->_totals[$key];
  136. }
  137. // Load the total.
  138. $query = $this->_getListQuery();
  139. $return = (int)$this->_getListCount($query->toString());
  140. // Check for a database error.
  141. if ($this->_db->getErrorNum()) {
  142. $this->setError($this->_db->getErrorMsg());
  143. return false;
  144. }
  145. // Push the value into internal storage.
  146. $this->_totals[$key] = $return;
  147. return $this->_totals[$key];
  148. }
  149. /**
  150. * Method to build an SQL query to load the list data.
  151. *
  152. * @access protected
  153. * @return string An SQL query
  154. * @since 1.0
  155. */
  156. function _getListQuery()
  157. {
  158. $query = new JXQuery();
  159. return $query;
  160. }
  161. /**
  162. * Method to get a store id based on model configuration state.
  163. *
  164. * This is necessary because the model is used by the component and
  165. * different modules that might need different sets of data or different
  166. * ordering requirements.
  167. *
  168. * @access protected
  169. * @param string $context A prefix for the store id.
  170. * @return string A store id.
  171. * @since 1.0
  172. */
  173. function _getStoreId($id = '')
  174. {
  175. // Compile the store id.
  176. $id .= ':'.$this->getState('list.start');
  177. $id .= ':'.$this->getState('list.limit');
  178. $id .= ':'.$this->getState('list.ordering');
  179. $id .= ':'.$this->getState('list.direction');
  180. return md5($id);
  181. }
  182. /**
  183. * Method to auto-populate the model state.
  184. *
  185. * This method should only be called once per instantiation and is designed
  186. * to be called on the first call to the getState() method unless the model
  187. * configuration flag to ignore the request is set.
  188. *
  189. * @access protected
  190. * @return void
  191. * @since 1.0
  192. */
  193. function _populateState()
  194. {
  195. $this->setState('list.start', 0);
  196. }
  197. }