/application/modules/Core/Model/Item/Collection.php

https://github.com/grandison/budo16 · PHP · 309 lines · 227 code · 43 blank · 39 comment · 23 complexity · 19fe874f5898ff2a412d41ccd6ec97c2 MD5 · raw file

  1. <?php
  2. /**
  3. * SocialEngine
  4. *
  5. * @category Application_Core
  6. * @package Core
  7. * @copyright Copyright 2006-2010 Webligo Developments
  8. * @license http://www.socialengine.net/license/
  9. * @version $Id: Collection.php 7244 2010-09-01 01:49:53Z john $
  10. * @author Sami
  11. */
  12. /**
  13. * @category Application_Core
  14. * @package Core
  15. * @copyright Copyright 2006-2010 Webligo Developments
  16. * @license http://www.socialengine.net/license/
  17. */
  18. class Core_Model_Item_Collection extends Core_Model_Item_Abstract implements Countable
  19. {
  20. protected $_collectible_type;
  21. protected function getCollectionColumnName()
  22. {
  23. if (!empty($this->_collection_column_name)) {
  24. return $this->_collection_column_name;
  25. }
  26. return "collection_id";
  27. }
  28. public function getCollectibles($reverse = false)
  29. {
  30. $table = Engine_Api::_()->getItemTable($this->_collectible_type);
  31. $primary = current($table->info("primary"));
  32. $select = $table->select()
  33. ->where($this->getCollectionColumnName() . ' = ?', $this->getIdentity());
  34. if( $reverse )
  35. {
  36. $select->order($primary . ' DESC');
  37. }
  38. return $select->getTable()->fetchAll($select);
  39. }
  40. public function getCollectiblesSelect()
  41. {
  42. $table = Engine_Api::_()->getItemTable($this->_collectible_type);
  43. $orderCol = ( in_array('order', $table->info('cols')) ? 'order' : current($table->info('primary')) );
  44. $select = $table->select()
  45. ->where($this->getCollectionColumnName() . ' = ?', $this->getIdentity())
  46. ->order($orderCol.' ASC');
  47. return $select;
  48. }
  49. public function getCollectiblesPaginator()
  50. {
  51. return Zend_Paginator::factory($this->getCollectiblesSelect());
  52. }
  53. public function getCollectibleIndex($collectible, $reverse = false)
  54. {
  55. if( is_numeric($collectible) )
  56. {
  57. $collectible = Engine_Api::_()->getItem($this->_collectible_type, $collectible);
  58. }
  59. if( !($collectible instanceof Core_Model_Item_Collectible))
  60. {
  61. throw new Core_Model_Item_Exception('Improper argument passed to getNextCollectible');
  62. }
  63. if( isset($collectible->collection_index) )
  64. {
  65. return $collectible->collection_index;
  66. }
  67. //if( !isset($this->store()->collectible_index) || !isset($this->store()->collectible_index[$collectible->getIdentity()]) )
  68. //{
  69. $table = $collectible->getTable();
  70. $col = current($table->info("primary"));
  71. $select = $table->select()
  72. ->from($table->info('name'), $col)
  73. ->where($this->getCollectionColumnName() . ' = ?', $this->getIdentity())
  74. ;
  75. // Order supported
  76. if( isset($collectible->order) )
  77. {
  78. $select->order('order ASC');
  79. }
  80. // Identity
  81. else
  82. {
  83. $select->order($col.' ASC');
  84. }
  85. $i = 0;
  86. $index = 0;
  87. //$this->store()->collectible_index = array();
  88. foreach( $table->fetchAll($select) as $row )
  89. {
  90. if( $row->$col == $collectible->getIdentity() )
  91. {
  92. $index = $i;
  93. }
  94. $i++;
  95. //$this->store()->collectible_index[$row->$col] = $i++;
  96. }
  97. //}
  98. //$index = $this->store()->collectible_index[$collectible->getIdentity()];
  99. return ( $reverse ? $this->count() - $index : $index );
  100. }
  101. public function getCollectibleByIndex($index = 0, $reverse = false)
  102. {
  103. $table = Engine_Api::_()->getItemTable($this->_collectible_type);
  104. $hasOrder = ( in_array('order', $table->info('cols')));
  105. // Check index bounds
  106. $count = $this->count();
  107. if( $index >= $count )
  108. {
  109. $index -= $count;
  110. }
  111. else if( $index < 0 )
  112. {
  113. $index += $count;
  114. }
  115. $select = $table->select()
  116. ->where($this->getCollectionColumnName() . ' = ?', $this->getIdentity())
  117. ->limit(1, (int) $index)
  118. ;
  119. if( $hasOrder )
  120. {
  121. $select->order('order '.($reverse ? 'DESC' : 'ASC'));
  122. }
  123. else
  124. {
  125. $col = current($table->info("primary"));
  126. $select->order($col.' '.($reverse ? 'DESC' : 'ASC'));
  127. }
  128. $rowset = $table->fetchAll($select);
  129. if( null === $rowset )
  130. {
  131. // @todo throw?
  132. return null;
  133. }
  134. return $rowset->current();
  135. }
  136. public function getFirstCollectible()
  137. {
  138. return $this->getCollectibleByIndex(0);
  139. }
  140. public function getLastCollectible()
  141. {
  142. return $this->getCollectibleByIndex(0, true);
  143. }
  144. public function getPrevCollectible($collectible)
  145. {
  146. return $this->getCollectibleByIndex($this->getCollectibleIndex($collectible)-1);
  147. }
  148. public function getNextCollectible($collectible)
  149. {
  150. return $this->getCollectibleByIndex($this->getCollectibleIndex($collectible)+1);
  151. }
  152. public function count()
  153. {
  154. // @todo this doesn't work if collection_id is init to 0
  155. /*
  156. if( isset($this->collectible_count) )
  157. {
  158. return $this->collectible_count;
  159. }
  160. */
  161. if( isset($this->store()->collectible_count) )
  162. {
  163. return $this->store()->collectible_count;
  164. }
  165. $table = Engine_Api::_()->getItemTable($this->_collectible_type);
  166. $select = new Zend_Db_Select($table->getAdapter());
  167. $select->from($table->info('name'), new Zend_Db_Expr('COUNT(*) as count'))
  168. ->where($this->getCollectionColumnName() . " = ?", $this->getIdentity());
  169. $data = $table->getAdapter()->fetchRow($select);
  170. return $this->store()->collectible_count = (int) $data['count'];;
  171. }
  172. public function getCount()
  173. {
  174. trigger_error('Deprecated', E_USER_WARNING);
  175. return $this->count();
  176. }
  177. protected function _delete()
  178. {
  179. foreach( $this->getCollectibles() as $collectible )
  180. {
  181. $collectible->delete();
  182. }
  183. return parent::_delete();
  184. }
  185. // Order stuff
  186. public function getHighestOrder()
  187. {
  188. $table = Engine_Api::_()->getItemTable($this->_collectible_type);
  189. if( !in_array('order', $table->info('cols')) )
  190. {
  191. throw new Core_Model_Item_Exception('Unable to use order as order column doesn\'t exist');
  192. }
  193. $select = new Zend_Db_Select($table->getAdapter());
  194. $select
  195. ->from($table->info('name'), new Zend_Db_Expr('MAX(`order`) as max_order'))
  196. ->where($this->getCollectionColumnName() . ' = ?', $this->getIdentity())
  197. ;
  198. $data = $select->query()->fetch();
  199. $next = (int) @$data['max_order'];
  200. return $next;
  201. }
  202. public function setOrders($ids = null, $consecutive = true)
  203. {
  204. $table = Engine_Api::_()->getItemTable($this->_collectible_type);
  205. if( !in_array('order', $table->info('cols')) )
  206. {
  207. throw new Core_Model_Item_Exception('Unable to use order as order column doesn\'t exist');
  208. }
  209. $ids = (array) $ids;
  210. if( empty($ids) ) $consecutive = false;
  211. $col = current($table->info('primary'));
  212. $select = $table->select()
  213. ->where($this->getCollectionColumnName() . ' = ?', $this->getIdentity())
  214. ->order('order ASC')
  215. ;
  216. // Only re-ordering a chunk
  217. if( $consecutive && !empty($ids) )
  218. {
  219. $select->where($col.' IN(?)', $ids);
  220. }
  221. $collectibles = $table->fetchAll($select);
  222. // Non-consecutive unspecified
  223. if( empty($ids) )
  224. {
  225. $i = 0;
  226. foreach( $collectibles as $collectible )
  227. {
  228. $collectible->order = $i++;
  229. $collectible->save();
  230. }
  231. }
  232. // specified non-consecutive
  233. else if( $consecutive )
  234. {
  235. $i = count($ids);
  236. foreach( $table->fetchAll($select) as $collectible )
  237. {
  238. $order = array_search($collectible->getIdentity(), $ids);
  239. if( !$order )
  240. {
  241. $order = $i++;
  242. }
  243. $collectible->order = $order;
  244. $collectible->save();
  245. }
  246. }
  247. // specified consecutive
  248. else
  249. {
  250. // Build index
  251. $orderIndex = array();
  252. foreach( $collectibles as $collectible )
  253. {
  254. $orderIndex[] = $collectible->order;
  255. }
  256. sort($orderIndex);
  257. foreach( $collectibles as $collectible )
  258. {
  259. $index = array_search($collectible->getIdentity(), $ids);
  260. $collectible->order = $orderIndex[$index];
  261. $collectible->save();
  262. }
  263. }
  264. return $this;
  265. }
  266. }