Source for file PersistentOrderedSet.class.php

Documentation is available at PersistentOrderedSet.class.php

  1. <?php
  2.  
  3. require_once(dirname(__FILE__)."/OrderedSet.class.php");
  4.  
  5. /**
  6. * The Set provides an implementation of the set interface that stores the
  7. * sets of ids in a database. Note: Nothing should be implied in the order that
  8. * the ids are returned.
  9. * Sets provide for the easy storage of groups of ids.
  10. *
  11. * @package harmoni.sets
  12. *
  13. * @copyright Copyright &copy; 2005, Middlebury College
  14. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  15. *
  16. * @version $Id: PersistentOrderedSet.class.php,v 1.5 2007/09/13 16:04:21 adamfranco Exp $
  17. * @author Adam Franco
  18. */
  19. class PersistentOrderedSet
  20. extends OrderedSet {
  21. /**
  22. * @var int $_dbIndex The dbIndex where this set is stored.
  23. * @access private
  24. */
  25. var $_dbIndex;
  26. /**
  27. * Constructor.
  28. * @param object Id $setId The Id of this set.
  29. * @param integer $dbIndex The index of the database connection which has
  30. * tables in which to store the set.
  31. */
  32. function PersistentOrderedSet ( $setId, $dbIndex ) {
  33. parent::OrderedSet($setId);
  34. ArgumentValidator::validate($dbIndex, IntegerValidatorRule::getRule(), true);
  35. // Create our internal array
  36. $this->_dbIndex = $dbIndex;
  37. // populate our array with any previously stored items.
  38. $query = new SelectQuery;
  39. $query->addColumn("item_order", "item_order");
  40. $query->addColumn("item_id", "item_id");
  41. $query->addTable("sets");
  42. $query->addWhere("id = '".addslashes($this->_setId->getIdString())."'");
  43. $query->addOrderBy("item_order");
  44. $dbHandler = Services::getService("DatabaseManager");
  45. $result =$dbHandler->query($query, $this->_dbIndex);
  46. $i = 0;
  47. $oldItems = array();
  48. while ($result->hasMoreRows()) {
  49. // Add the items to our array
  50. $this->_items[$i] = $result->field("item_id");
  51. // Store an array of the order-key/value relationships to reference
  52. // when updating any inconsistancies in order numbering.
  53. $oldItems[$result->field("item_order")] = $result->field("item_id");
  54. $i++;
  55. $result->advanceRow();
  56. }
  57. $result->free();
  58.  
  59. // Make sure that we have our set is filled from 0 to count()
  60. reset($oldItems);
  61. $this->_updateOrders($oldItems);
  62. }
  63. /**
  64. * Add a new Id to the set.
  65. * @param object Id $id The Id to add.
  66. * @access public
  67. * @return void
  68. */
  69. function addItem ( $id ) {
  70. parent::addItem($id);
  71. $position = $this->getPosition($id);
  72. // Add the item to the database
  73. $query = new InsertQuery;
  74. $query->setTable("sets");
  75. $columns = array("id", "item_id", "item_order");
  76. $values = array("'".addslashes($this->_setId->getIdString())."'", "'".addslashes($id->getIdString())."'", "'".$position."'");
  77. $query->setColumns($columns);
  78. $query->setValues($values);
  79. $dbHandler = Services::getService("DatabaseManager");
  80. $dbHandler->query($query, $this->_dbIndex);
  81. }
  82. /**
  83. * Remove an Id from the set.
  84. * @param object Id $id The Id to remove.
  85. * @access public
  86. * @return void
  87. */
  88. function removeItem ( $id ) {
  89. // Store the old order
  90. $oldOrder = $this->_items;
  91. // remove the Item
  92. parent::removeItem($id);
  93. // update the database with the new order keys.
  94. $this->_updateOrders($oldOrder);
  95. // Remove the item from the database
  96. $query = new DeleteQuery;
  97. $query->setTable("sets");
  98. $query->addWhere("id='".addslashes($this->_setId->getIdString())."'");
  99. $query->addWhere("item_id='".addslashes($id->getIdString())."'");
  100. $dbHandler = Services::getService("DatabaseManager");
  101. $dbHandler->query($query, $this->_dbIndex);
  102. }
  103. /**
  104. * Remove all Items from the set.
  105. * @access public
  106. * @return void
  107. */
  108. function removeAllItems () {
  109. parent::removeAllItems();
  110. // Remove the item from the database
  111. $query = new DeleteQuery;
  112. $query->setTable("sets");
  113. $query->addWhere("id='".addslashes(
  114. $this->_setId->getIdString())."'");
  115. $dbHandler = Services::getService("DatabaseManager");
  116. $dbHandler->query($query, $this->_dbIndex);
  117. }
  118. /**
  119. * Move the specified id to the specified position in the set.
  120. * @param object Id $id The Id of the item to move.
  121. * @param integer $position The new position of the specified id.
  122. * @access public
  123. * @return void
  124. */
  125. function moveToPosition ( $id, $position ) {
  126. // Store the old order
  127. $oldOrder = $this->_items;
  128. // remove the Item
  129. parent::moveToPosition($id, $position);
  130. // update the database with the new order keys.
  131. $this->_updateOrders($oldOrder);
  132. }
  133. /**
  134. * Move the specified id toward the begining of the set.
  135. * @param object Id $id The Id of the item to move.
  136. * @access public
  137. * @return void
  138. */
  139. function moveUp ( $id ) {
  140. // Store the old order
  141. $oldOrder = $this->_items;
  142. // move the Item
  143. parent::moveUp($id);
  144. // update the database with the new order keys.
  145. $this->_updateOrders($oldOrder);
  146. }
  147. /**
  148. * Move the specified id toward the end of the set.
  149. * @param object Id $id The Id of the item to move.
  150. * @access public
  151. * @return void
  152. */
  153. function moveDown ( $id ) {
  154. // Store the old order
  155. $oldOrder = $this->_items;
  156. // move the Item
  157. parent::moveDown($id);
  158. // update the database with the new order keys.
  159. $this->_updateOrders($oldOrder);
  160. }
  161. /**
  162. * Update the orders of the ids in the database.
  163. * @param array $oldOrders The previous orders to compare so that only
  164. * updates will be done to changed orders.
  165. * @access private
  166. * @return void
  167. */
  168. function _updateOrders ( $oldOrders ) {
  169. $dbHandler = Services::getService("DatabaseManager");
  170.  
  171. foreach ($this->_items as $key => $val) {
  172. // If the old key-value pairs don't match the current ones,
  173. // update that row
  174. if ($oldOrders[$key] != $val) {
  175. $query = new UpdateQuery;
  176. $query->setTable("sets");
  177. $columns = array("item_order");
  178. $query->setColumns($columns);
  179. $values = array($key);
  180. $query->setValues($values);
  181. $query->addWhere("id = '".addslashes($this->_setId->getIdString())."'");
  182. $query->addWhere("item_id = '".addslashes($val)."'");
  183. $dbHandler->query($query);
  184. }
  185. }
  186. }
  187. }

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