Source for file RecordSet.class.php

Documentation is available at RecordSet.class.php

  1. <?php
  2.  
  3. /**
  4. * The RecordSet class holds a list of IDs and their Records in a specific record set.
  5. *
  6. * @package harmoni.datamanager
  7. *
  8. * @copyright Copyright &copy; 2005, Middlebury College
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  10. *
  11. * @version $Id: RecordSet.class.php,v 1.12 2007/09/04 20:25:32 adamfranco Exp $
  12. */
  13. class RecordSet {
  14. var $_records;
  15. var $_dirty;
  16. function RecordSet() {
  17. $this->_records = array();
  18. $this->_dirty = false;
  19. }
  20. /**
  21. * Adds the records in the passed array to our set.
  22. * @param ref array $records An array of {@link Record} objects.
  23. * @access public
  24. * @return void
  25. */
  26. function addRecords($records)
  27. {
  28. $this->_dirty = true;
  29. for ($i = 0; $i < count($records); $i++) {
  30. $this->_records[] =$records[$i];
  31. }
  32. }
  33.  
  34. /**
  35. * Returns the number of records in this set.
  36. * @access public
  37. * @return int
  38. */
  39. function numRecords()
  40. {
  41. return count($this->_records);
  42. }
  43. /**
  44. * Returns an array of records.
  45. * @access public
  46. * @return ref array
  47. */
  48. function getRecords()
  49. {
  50. return $this->_records;
  51. }
  52. /**
  53. * Returns if this set contains the passed {@link Record}
  54. * @param ref object $record
  55. * @access public
  56. * @return boolean
  57. */
  58. function contains($record)
  59. {
  60. $id = $record->getID();
  61. if (!$id) return false;
  62. for ($i = 0; $i < count($this->_records); $i++) {
  63. if ($this->_records[$i]->getID() == $id) {
  64. return true;
  65. }
  66. }
  67. return false;
  68. }
  69. /**
  70. * Removes the given record from the set.
  71. * @param ref object $record A {@link Record} object.
  72. * @access public
  73. * @return void
  74. */
  75. function removeRecord($record)
  76. {
  77. $id = $record->getID();
  78. if (!$id) {
  79. throwError(new Error("Could not remove record from set because the record does not yet have an ID.","RecordSet",false));
  80. return;
  81. }
  82. $newArr = array();
  83. for ($i = 0; $i < count($this->_records); $i++) {
  84. if ($this->_records[$i]->getID() != $id) $newArr[] =$this->_records[$i];
  85. }
  86. unset($this->_records);
  87. $this->_records =$newArr;
  88. $this->_dirty = true;
  89. }
  90. /**
  91. * Returns an array of Record IDs that we know about.
  92. * @return array
  93. */
  94. function getRecordIDs() {
  95. $ids = array();
  96. for ($i = 0; $i < count($this->_records); $i++) {
  97. $id = $this->_records[$i]->getID();
  98. if ($id) $ids[] = $id;
  99. }
  100. return array_unique($ids);
  101. }
  102. /**
  103. * Returns an array of {@link Records} of the passed {@link Schema} {@link HarmoniType Type}.
  104. * @param string $id A type/ID string linked with a {@link Schema}.
  105. * @access public
  106. * @return ref array
  107. */
  108. function getRecordsByType($id)
  109. {
  110. $records =$this->getRecords();
  111. $return = array();
  112. for ($i = 0; $i < count($records); $i++) {
  113. if ($id == $records[$i]->getSchemaID()) {
  114. $return[] =$records[$i];
  115. }
  116. }
  117. return $return;
  118. }
  119. /**
  120. * Returns an array of records based on a label assigned previously. NOTE: this is a proposed method and has not been implemented.
  121. * @param string $label The string label of the {@link Records} to return.
  122. * @access public
  123. * @return ref array
  124. */
  125. function getRecordsByLabel($label)
  126. {
  127. // @todo decide if this is useful or if getRecordsByType is sufficient.
  128. }
  129. /**
  130. * Adds the given {@link Record} to our list.
  131. * @param ref object $record
  132. * @return void
  133. */
  134. function add($record) {
  135. $this->_dirty = true;
  136. $this->_records[] =$record;
  137. }
  138. /**
  139. * This is a synonym for {@link RecordSet::add()}.
  140. * @param ref object $record
  141. * @access public
  142. * @return void
  143. */
  144. function addRecord($record)
  145. {
  146. $this->add($record);
  147. }
  148. /**
  149. * Returns an array of merged tag-dates (as {@link DateAndTime} objects) which can be used to setup our Records as they were on a specific date.
  150. * @access public
  151. * @return array
  152. */
  153. function getMergedTagDates()
  154. {
  155. // first get all the tags for each of our Records, then ask them for their dates. avoid duplicates
  156. $tagManager = Services::getService("RecordTagManager");
  157. $dates = array();
  158. $dateStrings = array();
  159. foreach ($this->getRecordIDs() as $id) {
  160. $tags =$tagManager->fetchTagDescriptors($id);
  161. foreach (array_keys($tags) as $key) {
  162. $date =$tags[$key]->getDate();
  163. $str = $date->asString();
  164. if (!in_array($str, $dateStrings)) {
  165. $dateStrings[] = $str;
  166. $dates[] =$date;
  167. }
  168. }
  169. }
  170. return $dates;
  171. }
  172. /**
  173. * Returns all of the {@link Record}s within this Set to the state they were in on the given date.
  174. * @param ref object $date A {@link DateAndTime} object.
  175. * @access public
  176. * @return boolean
  177. */
  178. function revertToDate($date)
  179. {
  180. // this function goes through each Record, gets all the tags, and finds the one that has the closest tag
  181. // date to $date, as long as the tag date is *before* $date.
  182. // then, we activate that tag on the Record and commit it to the database
  183. $tagManager = Services::getService("RecordTagManager");
  184. for ($i = 0; $i < count($this->_records); $i++) {
  185. $id = $this->_records[$i]->getID();
  186. if (!$id) continue;
  187. $tags =$tagManager->fetchTags($id);
  188. $closest = null;
  189. $closestDate = null;
  190. foreach (array_keys($tags) as $key) {
  191. $tagDate =$tags[$key]->getDate();
  192. if ($tagDate->isLessThanOrEqualTo($date)
  193. && ($closestDate == null || $tagDate->isGreaterThan($closestDate)))
  194. {
  195. // print "-> for record $id, new best is " . $tagDate->asString() . " with sep = $sep<br/>";
  196. $closestDate =$tagDate;
  197. $closest =$tags[$key];
  198. }
  199. }
  200. if ($closest) {
  201. // we're going to activate the tag, then commit the Record
  202. $d =$closest->getDate();
  203. $this->_records[$i]->activateTag($closest);
  204. $this->_records[$i]->commit();
  205. }
  206. }
  207. return true;
  208. }
  209. }
  210.  
  211. ?>

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