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 31 parent::__construct($Id);
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"]);
218 catch (Exception $Exception)
220 # (moving to next item just to avoid empty catch statement) 239 # make sure the item is a field or group 242 throw new Exception(
"Item must be a field or group");
245 # make sure the item is in the order 248 throw new Exception(
"Item must exist in the ordering");
251 # make sure the filter is callable if set 252 if (!is_null($Filter) && !is_callable($Filter))
254 throw new Exception(
"Filter callback must be callable");
261 $PreviousId = $this->
GetItemId($Previous);
264 # determine if the item is at the top of the list 265 $ItemAtTop = is_null($Previous);
267 # determine if a field needs to be moved into a group 268 $FieldToGroup = $ItemType ==
"MetadataField";
269 $FieldToGroup = $FieldToGroup && $EnclosureType ==
"MetadataFieldOrder";
270 $FieldToGroup = $FieldToGroup && $PreviousType ==
"MetadataFieldGroup";
272 # determine if a field needs to be moved out of a group 273 $FieldToOrder = $ItemType ==
"MetadataField";
274 $FieldToOrder = $FieldToOrder && $EnclosureType ==
"MetadataFieldGroup";
275 $FieldToOrder = $FieldToOrder && $ItemAtTop;
277 # move a field into a group if necessary 283 # or move a field from a group to the order if necessary 284 else if ($FieldToOrder)
289 # otherwise just move the item up if not at the top of the list 290 else if (!$ItemAtTop)
307 # make sure the item is a field or group 310 throw new Exception(
"Item must be a field or group");
313 # make sure the item is in the order 316 throw new Exception(
"Item must exist in the ordering");
319 # make sure the filter is callable if set 320 if (!is_null($Filter) && !is_callable($Filter))
322 throw new Exception(
"Filter callback must be callable");
332 # determine if the item is at the bottom of the list 333 $ItemAtBottom = is_null($Next);
335 # determine if a field needs to be moved into a group 336 $FieldToGroup = $ItemType ==
"MetadataField";
337 $FieldToGroup = $FieldToGroup && $EnclosureType ==
"MetadataFieldOrder";
338 $FieldToGroup = $FieldToGroup && $NextType ==
"MetadataFieldGroup";
340 # determine if a field needs to be moved out of a group 341 $FieldToOrder = $ItemType ==
"MetadataField";
342 $FieldToOrder = $FieldToOrder && $EnclosureType ==
"MetadataFieldGroup";
343 $FieldToOrder = $FieldToOrder && $ItemAtBottom;
345 # move a field into a group if necessary 351 # or move a field from a group to the order if necessary 352 else if ($FieldToOrder)
357 # otherwise just move the item down if not at the bottom 358 else if (!$ItemAtBottom)
372 # make sure the item is either a field or group 375 throw new Exception(
"Item must be a either field or group");
378 # make sure the item is in the order 381 throw new Exception(
"Item must exist in the ordering");
389 $ItemEnclosureId = $this->
GetItemId($ItemEnclosure);
390 $ItemEnclosureType = $this->
GetItemType($ItemEnclosure);
392 $SameEnclosureId = $OrderId == $ItemEnclosureId;
393 $SameEnclosureType = $OrderType == $ItemEnclosureType;
395 # remove the item from its enclosure if necessary 396 if (!$SameEnclosureId || !$SameEnclosureType)
398 $ItemEnclosure->RemoveItem($ItemId, $ItemType);
401 # move the item to the top of the order 415 # make sure the items are in the order 418 throw new Exception(
"Item must exist in the ordering");
426 $FieldEnclosureId = $this->
GetItemId($FieldEnclosure);
427 $FieldEnclosureType = $this->
GetItemType($FieldEnclosure);
429 $SameEnclosureId = $GroupId == $FieldEnclosureId;
430 $SameEnclosureType = $GroupType == $FieldEnclosureType;
432 # remove the item from its enclosure if necessary 433 if (!$SameEnclosureId || !$SameEnclosureType)
435 $FieldEnclosure->RemoveItem($FieldId, $FieldType);
438 # move the item to the top of the group 453 # make sure the items are either a field or group 456 throw new Exception(
"Items must be a either field or group");
459 # make sure the items are in the order 462 throw new Exception(
"Items must exist in the ordering");
470 $TargetEnclosureId = $this->
GetItemId($TargetEnclosure);
471 $TargetEnclosureType = $this->
GetItemType($TargetEnclosure);
473 $ItemEnclosureId = $this->
GetItemId($ItemEnclosure);
474 $ItemEnclosureType = $this->
GetItemType($ItemEnclosure);
479 # make sure only fields are placed in groups 480 if ($TargetInGroup && !$ItemIsField)
482 throw new Exception(
"Only fields can go into field groups");
485 $SameEnclosureId = $TargetEnclosureId == $ItemEnclosureId;
486 $SameEnclosureType = $TargetEnclosureType == $ItemEnclosureType;
488 # move a field into a group if necessary 489 if (!$SameEnclosureId || !$SameEnclosureType)
491 $ItemEnclosure->RemoveItem($ItemId, $ItemType);
494 # move the item after the target 495 $TargetEnclosure->InsertItemAfter(
509 # the item would have to be a field or group to be in the order 518 # if the item is in the order, i.e., not in a group 524 # the item is in one of the groups, so search each one for it 527 if ($Group->ContainsItem($ItemId, $ItemType))
533 # the item was not found 550 array $FieldOrder=array())
552 $ExistingOrders = self::GetOrdersForSchema($Schema);
554 # remove existing orders with the same name 555 if (array_key_exists($Name, $ExistingOrders))
557 $ExistingOrders[$Name]->Delete();
562 $Folder = $FolderFactory->CreateMixedFolder(self::DEFAULT_FOLDER_NAME);
564 # get all the fields including disabled fields but excluding temp fields 565 $Fields = $Schema->
GetFields(NULL, NULL, TRUE);
567 # first, add each field from the given order 568 foreach ($FieldOrder as $FieldId)
570 # skip invalid field IDs 571 if (!array_key_exists($FieldId, $Fields))
576 # remove the field from the array of fields so that we'll know after 577 # looping which fields weren't added 578 unset($Fields[$FieldId]);
580 # add the metadata field to the folder 581 $Folder->AppendItem($FieldId,
"MetadataField");
584 # finally, add any remaining fields that weren't removed in the loop 586 foreach ($Fields as $FieldId => $Field)
588 $Folder->AppendItem($FieldId,
"MetadataField");
593 # associate the order with the schema in the database 595 INSERT INTO MetadataFieldOrders 596 SET SchemaId = '".addslashes($Schema->
Id()).
"', 597 OrderId = '".addslashes($Folder->Id()).
"', 598 OrderName = '".addslashes($Name).
"'");
600 # reconstruct the folder as a metadata schema order object and return 614 return self::GetOrderForSchemaId($Schema->
Id(), $Name);
627 $Orders = self::GetOrdersForSchemaId(
$SchemaId);
629 # return NULL if the order doesn't exist 630 if (!array_key_exists($Name, $Orders))
636 return $Orders[$Name];
647 return self::GetOrdersForSchemaId($Schema->
Id());
661 # query the database for the orders associated with the schema 663 SELECT * FROM MetadataFieldOrders 664 WHERE SchemaId = '".addslashes(
$SchemaId).
"'");
666 # loop through each found record 671 # construct an object using the ID and add it to the array 675 # remove invalid orders when encountered 676 catch (Exception $Exception)
679 DELETE FROM MetadataFieldOrders 680 WHERE OrderId = '".addslashes($Row[
"OrderId"]).
"'");
714 return is_object($Item) ? $Item->Id() : NULL;
724 return is_object($Item) ? get_class($Item) : NULL;
735 return $Item[
"Type"] ==
"MetadataFieldGroup";
750 # the item is in the order, i.e., not in a group 756 # the item is in one of the groups, so search each one for it 759 if ($Group->ContainsItem($ItemId, $ItemType))
765 # the item was not found 782 # the sibling is in the order, i.e., not in a group 788 # otherwise search for it in the groups 791 if ($Group->ContainsItem($Id, $Type))
806 catch (Exception $Exception)
808 # (moving to next item just to avoid empty catch statement) 830 $ItemIds = $Enclosure->GetItemIds();
832 # filter items if necessary 833 if (is_callable($Filter))
835 $ItemIds = array_filter($ItemIds, $Filter);
837 # maintain continuous indices 839 $ItemIds = array_values($ItemIds);
844 $Index = array_search(array(
"ID" => $Id,
"Type" => $Type), $ItemIds);
846 if (!is_null($Index) && array_key_exists($Index+$Offset, $ItemIds))
848 $SiblingInfo = $ItemIds[$Index+$Offset];
849 return new $SiblingInfo[
"Type"]($SiblingInfo[
"ID"]);
867 # determine which action to use based on the placement value 868 $Action = $Placement ==
"prepend" ?
"PrependItem" :
"AppendItem";
873 $OrderHasGroup = $this->
ContainsItem($GroupId,
"MetadataFieldGroup");
874 $OrderHasField = $this->
ContainsItem($FieldId,
"MetadataField");
876 # make sure the field and group are in the order before editing 877 if ($OrderHasGroup && $OrderHasField)
880 $Group->$Action($FieldId,
"MetadataField");
897 # determine which action to use based on the placement value 898 $Action = $Placement ==
"before" ?
"InsertItemBefore" :
"InsertItemAfter";
903 $OrderHasGroup = $this->
ContainsItem($GroupId,
"MetadataFieldGroup");
904 $GroupHasField = $Group->
ContainsItem($FieldId,
"MetadataField");
906 # make sure the field is in the group and the group is in the order 907 if ($OrderHasGroup && $GroupHasField)
909 $Group->
RemoveItem($FieldId,
"MetadataField");
913 "MetadataFieldGroup",
927 $PreviousItemId = $Group->
Id();
928 $PreviousItemType =
"MetadataFieldGroup";
930 foreach ($ItemIds as $ItemInfo)
932 $ItemId = $ItemInfo[
"ID"];
933 $ItemType = $ItemInfo[
"Type"];
941 $PreviousItemId = $ItemId;
942 $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.
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.
GetItemIds($Offset=NULL, $Length=NULL)
Retrieve array of IDs of items in folder, in the order that they appear in the folder.