4 # FILE: SPT--DoublyLinkedList.php
9 # SomeMethod($SomeParameter, $AnotherParameter)
10 # - short description of method
14 # Part of the Scout Portal Toolkit
15 # Copyright 2007 Internet Scout
16 # http://scout.wisc.edu
21 # ---- PUBLIC INTERFACE --------------------------------------------------
27 # grab our own database handle
36 # insert/move item to before specified item
40 $SourceItemId = is_object($SourceItemOrItemId)
41 ? $SourceItemOrItemId->Id() : $SourceItemOrItemId;
42 $TargetItemId = is_object($TargetItemOrItemId)
43 ? $TargetItemOrItemId->Id() : $TargetItemOrItemId;
45 # remove source item from current position if necessary
46 $this->
Remove($SourceItemId);
48 # update IDs to link in new item
51 if ($CurrentTargetItemPreviousId != -1)
53 $this->
SetNextItemId($CurrentTargetItemPreviousId, $SourceItemId);
56 $CurrentTargetItemPreviousId, $TargetItemId);
59 # insert/move item to after specified item
60 function InsertAfter($SourceItemOrItemId, $TargetItemOrItemId)
63 $SourceItemId = is_object($SourceItemOrItemId)
64 ? $SourceItemOrItemId->Id() : $SourceItemOrItemId;
65 $TargetItemId = is_object($TargetItemOrTargetItemId)
66 ? $TargetItemOrTargetItemId->Id() : $TargetItemOrTargetItemId;
68 # remove source item from current position if necessary
69 $this->
Remove($SourceItemId);
71 # update IDs to link in new item
74 if ($CurrentTargetItemNextId != -1)
79 $TargetItemId, $CurrentTargetItemNextId);
82 # add/move item to beginning of list
86 $ItemId = is_object($ItemOrItemId) ? $ItemOrItemId->Id() : $ItemOrItemId;
88 # remove new item from current position if necessary
91 # if there are items currently in list
92 $ItemIds = $this->
GetIds(FALSE);
95 # link last item to source item
96 $FirstItemId = array_shift($ItemIds);
102 # add item to list as only item
107 # add/move item to end of list
111 $ItemId = is_object($ItemOrItemId) ? $ItemOrItemId->Id() : $ItemOrItemId;
113 # remove item from current position if necessary
116 # if there are items currently in list
117 $ItemIds = $this->
GetIds(FALSE);
120 # link last item to source item
121 $LastItemId = array_pop($ItemIds);
127 # add item to list as only item
132 # retrieve list of item IDs in order
133 function GetIds($AddStrayItemsToOrder = TRUE)
135 # retrieve ordering IDs
136 $this->DB->Query(
"SELECT ".$this->ItemIdFieldName
137 .
", Previous".$this->ItemIdFieldName
138 .
", Next".$this->ItemIdFieldName
139 .
" FROM ".$this->ItemTableName
141 .
" ORDER BY ".$this->ItemIdFieldName.
" ASC");
142 $PreviousItemIds = array();
143 $NextItemIds = array();
144 while ($Record = $this->DB->FetchRow())
146 $ItemId = intval($Record[$this->ItemIdFieldName]);
147 $PreviousItemIds[$ItemId] =
148 intval($Record[
"Previous".$this->ItemIdFieldName]);
149 $NextItemIds[$ItemId] = intval($Record[
"Next".$this->ItemIdFieldName]);
152 # pull unordered items out of list
153 $StrayItemIds = array_keys($PreviousItemIds, -2);
154 foreach ($StrayItemIds as $StrayItemId)
156 unset($PreviousItemIds[$StrayItemId]);
157 unset($NextItemIds[$StrayItemId]);
161 $ItemId = array_search(-1, $PreviousItemIds);
163 # if first item was found
165 if ($ItemId !== FALSE)
167 # traverse linked list to build list of item IDs
170 $ItemIds[] = $ItemId;
171 unset($PreviousItemIds[$ItemId]);
172 if (isset($NextItemIds[$ItemId])) { $ItemId = $NextItemIds[$ItemId]; }
174 while (isset($NextItemIds[$ItemId]) && ($ItemId != -1)
175 && !in_array($ItemId, $ItemIds));
177 # add any items left over to stray items list
178 $StrayItemIds = array_unique($StrayItemIds + array_keys($PreviousItemIds));
181 # add any stray items to end of list (if so configured)
182 if ($AddStrayItemsToOrder)
184 foreach ($StrayItemIds as $StrayItemId)
186 $this->
Append($StrayItemId);
187 $ItemIds[] = $StrayItemId;
191 # return list of item IDs to caller
195 # remove item from existing order
200 if ($CurrentItemPreviousId >= 0)
203 $CurrentItemPreviousId, $CurrentItemNextId);
205 if ($CurrentItemNextId >= 0)
208 $CurrentItemNextId, $CurrentItemPreviousId);
212 # set SQL condition for ordering operations
213 # (use NULL to clear condition)
216 $this->Condition = $NewCondition;
220 # ---- PRIVATE INTERFACE -------------------------------------------------
227 # get/set ordering values
230 return $this->DB->Query(
"SELECT Previous".$this->ItemIdFieldName
231 .
" FROM ".$this->ItemTableName
232 .
" WHERE ".$this->ItemIdFieldName.
" = ".intval($ItemId)
234 "Previous".$this->ItemIdFieldName);
238 return $this->DB->Query(
"SELECT Next".$this->ItemIdFieldName
239 .
" FROM ".$this->ItemTableName
240 .
" WHERE ".$this->ItemIdFieldName.
" = ".intval($ItemId)
242 "Next".$this->ItemIdFieldName);
246 $this->DB->Query(
"UPDATE ".$this->ItemTableName
247 .
" SET Previous".$this->ItemIdFieldName.
" = ".intval($NewValue)
248 .
" WHERE ".$this->ItemIdFieldName.
" = ".intval($ItemId)
253 $this->DB->Query(
"UPDATE ".$this->ItemTableName
254 .
" SET Next".$this->ItemIdFieldName.
" = ".intval($NewValue)
255 .
" WHERE ".$this->ItemIdFieldName.
" = ".intval($ItemId)
260 $this->DB->Query(
"UPDATE ".$this->ItemTableName
261 .
" SET Previous".$this->ItemIdFieldName.
" = ".intval($NewPreviousId)
262 .
", Next".$this->ItemIdFieldName.
" = ".intval($NewNextId)
263 .
" WHERE ".$this->ItemIdFieldName.
" = ".intval($ItemId)
267 # return DB query condition (if any) with proper additional syntax
270 return $this->Condition ?
271 ($ThisIsOnlyCondition ?
" WHERE " :
" AND ").$this->Condition