3 # FILE: MetadataFieldOrder.php
5 # Part of the Collection Workflow Integration System (CWIS)
6 # Copyright 2013 Edward Almasy and Internet Scout Research Group
7 # http://scout.wisc.edu/cwis
30 # being loading by calling the superclass method
33 # create a class-wide database
36 # query for order associations from the database
38 SELECT * FROM MetadataFieldOrders
39 WHERE OrderId = '".addslashes($Id).
"'");
44 throw new Exception(
"Unknown metadata field order ID");
82 # remove the order from the orders associated with schemas
84 DELETE FROM MetadataFieldOrders
85 WHERE OrderId = '".addslashes($this->
Id()).
"'");
87 # remove the folder by calling the superclass method
98 # get all the fields including disabled fields but excluding temp fields
99 $Fields = $Schema->GetFields(NULL, NULL, TRUE);
101 foreach ($Fields as $Field)
103 # add the field if it isn't already in the order
106 $this->
AppendItem($Field->Id(),
"MetadataField");
120 foreach ($ItemIds as $Info)
124 $Items[] =
new $Info[
"Type"]($Info[
"ID"]);
127 # skip invalid fields
128 catch (InvalidArgumentException $Exception)
146 # create the new group
147 $Folder = $FolderFactory->CreateMixedFolder($Name);
150 # and add it to this ordering
151 $this->
AppendItem($Group->Id(),
"MetadataFieldGroup");
179 foreach ($this->
GetItems() as $Item)
181 # add fields to the list
184 $Fields[$Item->Id()] = $Item;
187 # add fields of groups to the list
190 foreach ($Item->GetFields() as $Field)
192 $Fields[$Field->Id()] = $Field;
207 $GroupIds = array_filter($ItemIds, array($this,
"GroupFilterCallback"));
211 # transform group info to group objects
212 foreach ($GroupIds as $GroupId)
216 $Groups[$GroupId[
"ID"]] =
new $GroupId[
"Type"]($GroupId[
"ID"]);
217 }
catch (Exception $Exception) {}
234 # make sure the item is a field or group
237 throw new Exception(
"Item must be a field or group");
240 # make sure the item is in the order
243 throw new Exception(
"Item must exist in the ordering");
246 # make sure the filter is callable if set
247 if (!is_null($Filter) && !is_callable($Filter))
249 throw new Exception(
"Filter callback must be callable");
256 $PreviousId = $this->
GetItemId($Previous);
259 # determine if the item is at the top of the list
260 $ItemAtTop = is_null($Previous);
262 # determine if a field needs to be moved into a group
263 $FieldToGroup = $ItemType ==
"MetadataField";
264 $FieldToGroup = $FieldToGroup && $EnclosureType ==
"MetadataFieldOrder";
265 $FieldToGroup = $FieldToGroup && $PreviousType ==
"MetadataFieldGroup";
267 # determine if a field needs to be moved out of a group
268 $FieldToOrder = $ItemType ==
"MetadataField";
269 $FieldToOrder = $FieldToOrder && $EnclosureType ==
"MetadataFieldGroup";
270 $FieldToOrder = $FieldToOrder && $ItemAtTop;
272 # move a field into a group if necessary
278 # or move a field from a group to the order if necessary
279 else if ($FieldToOrder)
284 # otherwise just move the item up if not at the top of the list
285 else if (!$ItemAtTop)
302 # make sure the item is a field or group
305 throw new Exception(
"Item must be a field or group");
308 # make sure the item is in the order
311 throw new Exception(
"Item must exist in the ordering");
314 # make sure the filter is callable if set
315 if (!is_null($Filter) && !is_callable($Filter))
317 throw new Exception(
"Filter callback must be callable");
327 # determine if the item is at the bottom of the list
328 $ItemAtBottom = is_null($Next);
330 # determine if a field needs to be moved into a group
331 $FieldToGroup = $ItemType ==
"MetadataField";
332 $FieldToGroup = $FieldToGroup && $EnclosureType ==
"MetadataFieldOrder";
333 $FieldToGroup = $FieldToGroup && $NextType ==
"MetadataFieldGroup";
335 # determine if a field needs to be moved out of a group
336 $FieldToOrder = $ItemType ==
"MetadataField";
337 $FieldToOrder = $FieldToOrder && $EnclosureType ==
"MetadataFieldGroup";
338 $FieldToOrder = $FieldToOrder && $ItemAtBottom;
340 # move a field into a group if necessary
346 # or move a field from a group to the order if necessary
347 else if ($FieldToOrder)
352 # otherwise just move the item down if not at the bottom
353 else if (!$ItemAtBottom)
367 # make sure the item is either a field or group
370 throw new Exception(
"Item must be a either field or group");
373 # make sure the item is in the order
376 throw new Exception(
"Item must exist in the ordering");
384 $ItemEnclosureId = $this->
GetItemId($ItemEnclosure);
385 $ItemEnclosureType = $this->
GetItemType($ItemEnclosure);
387 $SameEnclosureId = $OrderId == $ItemEnclosureId;
388 $SameEnclosureType = $OrderType == $ItemEnclosureType;
390 # remove the item from its enclosure if necessary
391 if (!$SameEnclosureId || !$SameEnclosureType)
393 $ItemEnclosure->RemoveItem($ItemId, $ItemType);
396 # move the item to the top of the order
410 # make sure the items are in the order
413 throw new Exception(
"Item must exist in the ordering");
421 $FieldEnclosureId = $this->
GetItemId($FieldEnclosure);
422 $FieldEnclosureType = $this->
GetItemType($FieldEnclosure);
424 $SameEnclosureId = $GroupId == $FieldEnclosureId;
425 $SameEnclosureType = $GroupType == $FieldEnclosureType;
427 # remove the item from its enclosure if necessary
428 if (!$SameEnclosureId || !$SameEnclosureType)
430 $FieldEnclosure->RemoveItem($FieldId, $FieldType);
433 # move the item to the top of the group
448 # make sure the items are either a field or group
451 throw new Exception(
"Items must be a either field or group");
454 # make sure the items are in the order
457 throw new Exception(
"Items must exist in the ordering");
465 $TargetEnclosureId = $this->
GetItemId($TargetEnclosure);
466 $TargetEnclosureType = $this->
GetItemType($TargetEnclosure);
468 $ItemEnclosureId = $this->
GetItemId($ItemEnclosure);
469 $ItemEnclosureType = $this->
GetItemType($ItemEnclosure);
474 # make sure only fields are placed in groups
475 if ($TargetInGroup && !$ItemIsField)
477 throw new Exception(
"Only fields can go into field groups");
480 $SameEnclosureId = $TargetEnclosureId == $ItemEnclosureId;
481 $SameEnclosureType = $TargetEnclosureType == $ItemEnclosureType;
483 # move a field into a group if necessary
484 if (!$SameEnclosureId || !$SameEnclosureType)
486 $ItemEnclosure->RemoveItem($ItemId, $ItemType);
489 # move the item after the target
490 $TargetEnclosure->InsertItemAfter(
504 # the item would have to be a field or group to be in the order
513 # if the item is in the order, i.e., not in a group
519 # the item is in one of the groups, so search each one for it
522 if ($Group->ContainsItem($ItemId, $ItemType))
528 # the item was not found
545 array $FieldOrder=array())
547 $ExistingOrders = self::GetOrdersForSchema($Schema);
549 # remove existing orders with the same name
550 if (array_key_exists($Name, $ExistingOrders))
552 $ExistingOrders[$Name]->Delete();
557 $Folder = $FolderFactory->CreateMixedFolder(self::DEFAULT_FOLDER_NAME);
559 # get all the fields including disabled fields but excluding temp fields
560 $Fields = $Schema->
GetFields(NULL, NULL, TRUE);
562 # first, add each field from the given order
563 foreach ($FieldOrder as $FieldId)
565 # skip invalid field IDs
566 if (!array_key_exists($FieldId, $Fields))
571 # remove the field from the array of fields so that we'll know after
572 # looping which fields weren't added
573 unset($Fields[$FieldId]);
575 # add the metadata field to the folder
576 $Folder->AppendItem($FieldId,
"MetadataField");
579 # finally, add any remaining fields that weren't removed in the loop
581 foreach ($Fields as $FieldId => $Field)
583 $Folder->AppendItem($FieldId,
"MetadataField");
588 # associate the order with the schema in the database
590 INSERT INTO MetadataFieldOrders
591 SET SchemaId = '".addslashes($Schema->
Id()).
"',
592 OrderId = '".addslashes($Folder->Id()).
"',
593 OrderName = '".addslashes($Name).
"'");
595 # reconstruct the folder as a metadata schema order object and return
609 return self::GetOrderForSchemaId($Schema->
Id(), $Name);
622 $Orders = self::GetOrdersForSchemaId(
$SchemaId);
624 # return NULL if the order doesn't exist
625 if (!array_key_exists($Name, $Orders))
631 return $Orders[$Name];
642 return self::GetOrdersForSchemaId($Schema->
Id());
656 # query the database for the orders associated with the schema
658 SELECT * FROM MetadataFieldOrders
659 WHERE SchemaId = '".addslashes(
$SchemaId).
"'");
661 # loop through each found record
666 # construct an object using the ID and add it to the array
670 # remove invalid orders when encountered
671 catch (Exception $Exception)
674 DELETE FROM MetadataFieldOrders
675 WHERE OrderId = '".addslashes($Row[
"OrderId"]).
"'");
709 return is_object($Item) ? $Item->Id() : NULL;
719 return is_object($Item) ? get_class($Item) : NULL;
730 return $Item[
"Type"] ==
"MetadataFieldGroup";
745 # the item is in the order, i.e., not in a group
751 # the item is in one of the groups, so search each one for it
754 if ($Group->ContainsItem($ItemId, $ItemType))
760 # the item was not found
777 # the sibling is in the order, i.e., not in a group
783 # otherwise search for it in the groups
786 if ($Group->ContainsItem($Id, $Type))
800 }
catch (Exception $Exception) {}
820 $ItemIds = $Enclosure->GetItemIds();
822 # filter items if necessary
823 if (is_callable($Filter))
825 $ItemIds = array_filter($ItemIds, $Filter);
827 # maintain continuous indices
829 $ItemIds = array_values($ItemIds);
834 $Index = array_search(array(
"ID" => $Id,
"Type" => $Type), $ItemIds);
836 if (!is_null($Index) && array_key_exists($Index+$Offset, $ItemIds))
838 $SiblingInfo = $ItemIds[$Index+$Offset];
839 return new $SiblingInfo[
"Type"]($SiblingInfo[
"ID"]);
857 # determine which action to use based on the placement value
858 $Action = $Placement ==
"prepend" ?
"PrependItem" :
"AppendItem";
863 $OrderHasGroup = $this->
ContainsItem($GroupId,
"MetadataFieldGroup");
864 $OrderHasField = $this->
ContainsItem($FieldId,
"MetadataField");
866 # make sure the field and group are in the order before editing
867 if ($OrderHasGroup && $OrderHasField)
870 $Group->$Action($FieldId,
"MetadataField");
887 # determine which action to use based on the placement value
888 $Action = $Placement ==
"before" ?
"InsertItemBefore" :
"InsertItemAfter";
893 $OrderHasGroup = $this->
ContainsItem($GroupId,
"MetadataFieldGroup");
894 $GroupHasField = $Group->
ContainsItem($FieldId,
"MetadataField");
896 # make sure the field is in the group and the group is in the order
897 if ($OrderHasGroup && $GroupHasField)
899 $Group->
RemoveItem($FieldId,
"MetadataField");
903 "MetadataFieldGroup",
917 $PreviousItemId = $Group->
Id();
918 $PreviousItemType =
"MetadataFieldGroup";
920 foreach ($ItemIds as $ItemInfo)
922 $ItemId = $ItemInfo[
"ID"];
923 $ItemType = $ItemInfo[
"Type"];
931 $PreviousItemId = $ItemId;
932 $PreviousItemType = $ItemType;
AppendItem($ItemOrItemId, $ItemType=NULL)
Add item to folder as the last item.
SQL database abstraction object with smart query caching.
FetchRow()
Get next database row retrieved by most recent query.
GetItemIds()
Retrieve array of IDs of items in folder, in the order that they appear in the folder.
Factory object for Folder class, used to retrieve and manage Folders and groups of Folders...
Folder object used to create and manage groups of items.
RemoveItem($ItemId, $ItemType=NULL)
Remove item from folder, if present.
InsertItemAfter($TargetItemOrItemId, $NewItemOrItemId, $TargetItemType=NULL, $NewItemType=NULL)
Insert item into folder after specified item.
NumRowsSelected()
Get number of rows returned by last SELECT or SHOW query.
Query($QueryString, $FieldName="")
Query database (with caching if enabled).
ContainsItem($ItemId, $ItemType=NULL)
Check whether specified item is contained in folder.
PrependItem($ItemOrItemId, $ItemType=NULL)
Add item to folder as the first item.