CDataProvider.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. * CDataProvider is a base class that implements the {@link IDataProvider} interface.
  4. *
  5. * Derived classes mainly need to implement three methods: {@link fetchData},
  6. * {@link fetchKeys} and {@link calculateTotalItemCount}.
  7. *
  8. * @property string $id The unique ID that uniquely identifies the data provider among all data providers.
  9. * @property CPagination $pagination The pagination object. If this is false, it means the pagination is disabled.
  10. * @property CSort $sort The sorting object. If this is false, it means the sorting is disabled.
  11. * @property array $data The list of data items currently available in this data provider.
  12. * @property array $keys The list of key values corresponding to {@link data}. Each data item in {@link data}
  13. * is uniquely identified by the corresponding key value in this array.
  14. * @property integer $itemCount The number of data items in the current page.
  15. * @property integer $totalItemCount Total number of possible data items.
  16. *
  17. * @author Qiang Xue <qiang.xue@gmail.com>
  18. * @package system.web
  19. * @since 1.1
  20. */
  21. abstract class CDataProvider extends CComponent implements IDataProvider
  22. {
  23. private $_id;
  24. private $_data;
  25. private $_keys;
  26. private $_totalItemCount;
  27. private $_sort;
  28. private $_pagination;
  29. /**
  30. * Fetches the data from the persistent data storage.
  31. * @return array list of data items
  32. */
  33. abstract protected function fetchData();
  34. /**
  35. * Fetches the data item keys from the persistent data storage.
  36. * @return array list of data item keys.
  37. */
  38. abstract protected function fetchKeys();
  39. /**
  40. * Calculates the total number of data items.
  41. * @return integer the total number of data items.
  42. */
  43. abstract protected function calculateTotalItemCount();
  44. /**
  45. * Returns the ID that uniquely identifies the data provider.
  46. * @return string the unique ID that uniquely identifies the data provider among all data providers.
  47. */
  48. public function getId()
  49. {
  50. return $this->_id;
  51. }
  52. /**
  53. * Sets the provider ID.
  54. * @param string $value the unique ID that uniquely identifies the data provider among all data providers.
  55. */
  56. public function setId($value)
  57. {
  58. $this->_id=$value;
  59. }
  60. /**
  61. * Returns the pagination object.
  62. * @param string $className the pagination object class name. Parameter is available since version 1.1.13.
  63. * @return CPagination|false the pagination object. If this is false, it means the pagination is disabled.
  64. */
  65. public function getPagination($className='CPagination')
  66. {
  67. if($this->_pagination===null)
  68. {
  69. $this->_pagination=new $className;
  70. if(($id=$this->getId())!='')
  71. $this->_pagination->pageVar=$id.'_page';
  72. }
  73. return $this->_pagination;
  74. }
  75. /**
  76. * Sets the pagination for this data provider.
  77. * @param mixed $value the pagination to be used by this data provider. This could be a {@link CPagination} object
  78. * or an array used to configure the pagination object. If this is false, it means the pagination should be disabled.
  79. *
  80. * You can configure this property same way as a component:
  81. * <pre>
  82. * array(
  83. * 'class' => 'MyPagination',
  84. * 'pageSize' => 20,
  85. * ),
  86. * </pre>
  87. */
  88. public function setPagination($value)
  89. {
  90. if(is_array($value))
  91. {
  92. if(isset($value['class']))
  93. {
  94. $pagination=$this->getPagination($value['class']);
  95. unset($value['class']);
  96. }
  97. else
  98. $pagination=$this->getPagination();
  99. foreach($value as $k=>$v)
  100. $pagination->$k=$v;
  101. }
  102. else
  103. $this->_pagination=$value;
  104. }
  105. /**
  106. * Returns the sort object.
  107. * @param string $className the sorting object class name. Parameter is available since version 1.1.13.
  108. * @return CSort|false the sorting object. If this is false, it means the sorting is disabled.
  109. */
  110. public function getSort($className='CSort')
  111. {
  112. if($this->_sort===null)
  113. {
  114. $this->_sort=new $className;
  115. if(($id=$this->getId())!='')
  116. $this->_sort->sortVar=$id.'_sort';
  117. }
  118. return $this->_sort;
  119. }
  120. /**
  121. * Sets the sorting for this data provider.
  122. * @param mixed $value the sorting to be used by this data provider. This could be a {@link CSort} object
  123. * or an array used to configure the sorting object. If this is false, it means the sorting should be disabled.
  124. *
  125. * You can configure this property same way as a component:
  126. * <pre>
  127. * array(
  128. * 'class' => 'MySort',
  129. * 'attributes' => array('name', 'weight'),
  130. * ),
  131. * </pre>
  132. */
  133. public function setSort($value)
  134. {
  135. if(is_array($value))
  136. {
  137. if(isset($value['class']))
  138. {
  139. $sort=$this->getSort($value['class']);
  140. unset($value['class']);
  141. }
  142. else
  143. $sort=$this->getSort();
  144. foreach($value as $k=>$v)
  145. $sort->$k=$v;
  146. }
  147. else
  148. $this->_sort=$value;
  149. }
  150. /**
  151. * Returns the data items currently available.
  152. * @param boolean $refresh whether the data should be re-fetched from persistent storage.
  153. * @return array the list of data items currently available in this data provider.
  154. */
  155. public function getData($refresh=false)
  156. {
  157. if($this->_data===null || $refresh)
  158. $this->_data=$this->fetchData();
  159. return $this->_data;
  160. }
  161. /**
  162. * Sets the data items for this provider.
  163. * @param array $value put the data items into this provider.
  164. */
  165. public function setData($value)
  166. {
  167. $this->_data=$value;
  168. }
  169. /**
  170. * Returns the key values associated with the data items.
  171. * @param boolean $refresh whether the keys should be re-calculated.
  172. * @return array the list of key values corresponding to {@link data}. Each data item in {@link data}
  173. * is uniquely identified by the corresponding key value in this array.
  174. */
  175. public function getKeys($refresh=false)
  176. {
  177. if($this->_keys===null || $refresh)
  178. $this->_keys=$this->fetchKeys();
  179. return $this->_keys;
  180. }
  181. /**
  182. * Sets the data item keys for this provider.
  183. * @param array $value put the data item keys into this provider.
  184. */
  185. public function setKeys($value)
  186. {
  187. $this->_keys=$value;
  188. }
  189. /**
  190. * Returns the number of data items in the current page.
  191. * This is equivalent to <code>count($provider->getData())</code>.
  192. * When {@link pagination} is set false, this returns the same value as {@link totalItemCount}.
  193. * @param boolean $refresh whether the number of data items should be re-calculated.
  194. * @return integer the number of data items in the current page.
  195. */
  196. public function getItemCount($refresh=false)
  197. {
  198. return count($this->getData($refresh));
  199. }
  200. /**
  201. * Returns the total number of data items.
  202. * When {@link pagination} is set false, this returns the same value as {@link itemCount}.
  203. * @param boolean $refresh whether the total number of data items should be re-calculated.
  204. * @return integer total number of possible data items.
  205. */
  206. public function getTotalItemCount($refresh=false)
  207. {
  208. if($this->_totalItemCount===null || $refresh)
  209. $this->_totalItemCount=$this->calculateTotalItemCount();
  210. return $this->_totalItemCount;
  211. }
  212. /**
  213. * Sets the total number of data items.
  214. * This method is provided in case when the total number cannot be determined by {@link calculateTotalItemCount}.
  215. * @param integer $value the total number of data items.
  216. * @since 1.1.1
  217. */
  218. public function setTotalItemCount($value)
  219. {
  220. $this->_totalItemCount=$value;
  221. }
  222. }