3 # FILE: MetadataFieldOrder.php 5 # Part of the Collection Workflow Integration System (CWIS) 6 # Copyright 2013-2016 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");
178 foreach ($this->
GetItems() as $Item)
180 # add fields to the list 183 $Fields[$Item->Id()] = $Item;
186 # add fields of groups to the list 189 foreach ($Item->GetFields() as $Field)
191 $Fields[$Field->Id()] = $Field;
206 $GroupIds = array_filter($ItemIds, array($this,
"GroupFilterCallback"));
210 # transform group info to group objects 211 foreach ($GroupIds as $GroupId)
215 $Groups[$GroupId[
"ID"]] =
new $GroupId[
"Type"]($GroupId[
"ID"]);
217 catch (Exception $Exception)
219 # (moving to next item just to avoid empty catch statement) 237 # make sure the item is a field or group 240 throw new Exception(
"Item must be a field or group");
243 # make sure the item is in the order 246 throw new Exception(
"Item must exist in the ordering");
249 # make sure the filter is callable if set 250 if (!is_null($Filter) && !is_callable($Filter))
252 throw new Exception(
"Filter callback must be callable");
259 $PreviousId = $this->
GetItemId($Previous);
262 # determine if the item is at the top of the list 263 $ItemAtTop = is_null($Previous);
265 # determine if a field needs to be moved into a group 266 $FieldToGroup = $ItemType ==
"MetadataField";
267 $FieldToGroup = $FieldToGroup && $EnclosureType ==
"MetadataFieldOrder";
268 $FieldToGroup = $FieldToGroup && $PreviousType ==
"MetadataFieldGroup";
270 # determine if a field needs to be moved out of a group 271 $FieldToOrder = $ItemType ==
"MetadataField";
272 $FieldToOrder = $FieldToOrder && $EnclosureType ==
"MetadataFieldGroup";
273 $FieldToOrder = $FieldToOrder && $ItemAtTop;
275 # move a field into a group if necessary 281 # or move a field from a group to the order if necessary 282 else if ($FieldToOrder)
287 # otherwise just move the item up if not at the top of the list 288 else if (!$ItemAtTop)
304 # make sure the item is a field or group 307 throw new Exception(
"Item must be a field or group");
310 # make sure the item is in the order 313 throw new Exception(
"Item must exist in the ordering");
316 # make sure the filter is callable if set 317 if (!is_null($Filter) && !is_callable($Filter))
319 throw new Exception(
"Filter callback must be callable");
329 # determine if the item is at the bottom of the list 330 $ItemAtBottom = is_null($Next);
332 # determine if a field needs to be moved into a group 333 $FieldToGroup = $ItemType ==
"MetadataField";
334 $FieldToGroup = $FieldToGroup && $EnclosureType ==
"MetadataFieldOrder";
335 $FieldToGroup = $FieldToGroup && $NextType ==
"MetadataFieldGroup";
337 # determine if a field needs to be moved out of a group 338 $FieldToOrder = $ItemType ==
"MetadataField";
339 $FieldToOrder = $FieldToOrder && $EnclosureType ==
"MetadataFieldGroup";
340 $FieldToOrder = $FieldToOrder && $ItemAtBottom;
342 # move a field into a group if necessary 348 # or move a field from a group to the order if necessary 349 else if ($FieldToOrder)
354 # otherwise just move the item down if not at the bottom 355 else if (!$ItemAtBottom)
369 # make sure the item is either a field or group 372 throw new Exception(
"Item must be a either field or group");
375 # make sure the item is in the order 378 throw new Exception(
"Item must exist in the ordering");
386 $ItemEnclosureId = $this->
GetItemId($ItemEnclosure);
387 $ItemEnclosureType = $this->
GetItemType($ItemEnclosure);
389 $SameEnclosureId = $OrderId == $ItemEnclosureId;
390 $SameEnclosureType = $OrderType == $ItemEnclosureType;
392 # remove the item from its enclosure if necessary 393 if (!$SameEnclosureId || !$SameEnclosureType)
395 $ItemEnclosure->RemoveItem($ItemId, $ItemType);
398 # move the item to the top of the order 412 # make sure the items are in the order 415 throw new Exception(
"Item must exist in the ordering");
423 $FieldEnclosureId = $this->
GetItemId($FieldEnclosure);
424 $FieldEnclosureType = $this->
GetItemType($FieldEnclosure);
426 $SameEnclosureId = $GroupId == $FieldEnclosureId;
427 $SameEnclosureType = $GroupType == $FieldEnclosureType;
429 # remove the item from its enclosure if necessary 430 if (!$SameEnclosureId || !$SameEnclosureType)
432 $FieldEnclosure->RemoveItem($FieldId, $FieldType);
435 # move the item to the top of the group 449 # make sure the items are either a field or group 452 throw new Exception(
"Items must be a either field or group");
455 # make sure the items are in the order 458 throw new Exception(
"Items must exist in the ordering");
466 $TargetEnclosureId = $this->
GetItemId($TargetEnclosure);
467 $TargetEnclosureType = $this->
GetItemType($TargetEnclosure);
469 $ItemEnclosureId = $this->
GetItemId($ItemEnclosure);
470 $ItemEnclosureType = $this->
GetItemType($ItemEnclosure);
475 # make sure only fields are placed in groups 476 if ($TargetInGroup && !$ItemIsField)
478 throw new Exception(
"Only fields can go into field groups");
481 $SameEnclosureId = $TargetEnclosureId == $ItemEnclosureId;
482 $SameEnclosureType = $TargetEnclosureType == $ItemEnclosureType;
484 # move a field into a group if necessary 485 if (!$SameEnclosureId || !$SameEnclosureType)
487 $ItemEnclosure->RemoveItem($ItemId, $ItemType);
490 # move the item after the target 491 $TargetEnclosure->InsertItemAfter(
505 # the item would have to be a field or group to be in the order 514 # if the item is in the order, i.e., not in a group 520 # the item is in one of the groups, so search each one for it 523 if ($Group->ContainsItem($ItemId, $ItemType))
529 # the item was not found 546 array $FieldOrder=array())
548 $ExistingOrders = self::GetOrdersForSchema($Schema);
550 # remove existing orders with the same name 551 if (array_key_exists($Name, $ExistingOrders))
553 $ExistingOrders[$Name]->Delete();
558 $Folder = $FolderFactory->CreateMixedFolder(self::DEFAULT_FOLDER_NAME);
560 # get all the fields including disabled fields but excluding temp fields 561 $Fields = $Schema->
GetFields(NULL, NULL, TRUE);
563 # first, add each field from the given order 564 foreach ($FieldOrder as $FieldId)
566 # skip invalid field IDs 567 if (!array_key_exists($FieldId, $Fields))
572 # remove the field from the array of fields so that we'll know after 573 # looping which fields weren't added 574 unset($Fields[$FieldId]);
576 # add the metadata field to the folder 577 $Folder->AppendItem($FieldId,
"MetadataField");
580 # finally, add any remaining fields that weren't removed in the loop 582 foreach ($Fields as $FieldId => $Field)
584 $Folder->AppendItem($FieldId,
"MetadataField");
589 # associate the order with the schema in the database 591 INSERT INTO MetadataFieldOrders 592 SET SchemaId = '".addslashes($Schema->
Id()).
"', 593 OrderId = '".addslashes($Folder->Id()).
"', 594 OrderName = '".addslashes($Name).
"'");
596 # reconstruct the folder as a metadata schema order object and return 610 return self::GetOrderForSchemaId($Schema->
Id(), $Name);
623 $Orders = self::GetOrdersForSchemaId(
$SchemaId);
625 # return NULL if the order doesn't exist 626 if (!array_key_exists($Name, $Orders))
632 return $Orders[$Name];
643 return self::GetOrdersForSchemaId($Schema->
Id());
657 # query the database for the orders associated with the schema 659 SELECT * FROM MetadataFieldOrders 660 WHERE SchemaId = '".addslashes(
$SchemaId).
"'");
662 # loop through each found record 667 # construct an object using the ID and add it to the array 671 # remove invalid orders when encountered 672 catch (Exception $Exception)
675 DELETE FROM MetadataFieldOrders 676 WHERE OrderId = '".addslashes($Row[
"OrderId"]).
"'");
710 return is_object($Item) ? $Item->Id() : NULL;
720 return is_object($Item) ? get_class($Item) : NULL;
731 return $Item[
"Type"] ==
"MetadataFieldGroup";
746 # the item is in the order, i.e., not in a group 752 # the item is in one of the groups, so search each one for it 755 if ($Group->ContainsItem($ItemId, $ItemType))
761 # the item was not found 778 # the sibling is in the order, i.e., not in a group 784 # otherwise search for it in the groups 787 if ($Group->ContainsItem($Id, $Type))
802 catch (Exception $Exception)
804 # (moving to next item just to avoid empty catch statement) 826 $ItemIds = $Enclosure->GetItemIds();
828 # filter items if necessary 829 if (is_callable($Filter))
831 $ItemIds = array_filter($ItemIds, $Filter);
833 # maintain continuous indices 835 $ItemIds = array_values($ItemIds);
840 $Index = array_search(array(
"ID" => $Id,
"Type" => $Type), $ItemIds);
842 if (!is_null($Index) && array_key_exists($Index+$Offset, $ItemIds))
844 $SiblingInfo = $ItemIds[$Index+$Offset];
845 return new $SiblingInfo[
"Type"]($SiblingInfo[
"ID"]);
863 # determine which action to use based on the placement value 864 $Action = $Placement ==
"prepend" ?
"PrependItem" :
"AppendItem";
869 $OrderHasGroup = $this->
ContainsItem($GroupId,
"MetadataFieldGroup");
870 $OrderHasField = $this->
ContainsItem($FieldId,
"MetadataField");
872 # make sure the field and group are in the order before editing 873 if ($OrderHasGroup && $OrderHasField)
876 $Group->$Action($FieldId,
"MetadataField");
892 # determine which action to use based on the placement value 893 $Action = $Placement ==
"before" ?
"InsertItemBefore" :
"InsertItemAfter";
898 $OrderHasGroup = $this->
ContainsItem($GroupId,
"MetadataFieldGroup");
899 $GroupHasField = $Group->
ContainsItem($FieldId,
"MetadataField");
901 # make sure the field is in the group and the group is in the order 902 if ($OrderHasGroup && $GroupHasField)
904 $Group->
RemoveItem($FieldId,
"MetadataField");
908 "MetadataFieldGroup",
921 $PreviousItemId = $Group->
Id();
922 $PreviousItemType =
"MetadataFieldGroup";
924 foreach ($ItemIds as $ItemInfo)
926 $ItemId = $ItemInfo[
"ID"];
927 $ItemType = $ItemInfo[
"Type"];
935 $PreviousItemId = $ItemId;
936 $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.