/**
  * Sort information objects by lft value so that parent objects are inserted
  * before their children.
  *
  * @return QubitMigrate105 this object
  */
 protected function sortQubitInformationObjects()
 {
     QubitMigrate::sortByLft($this->data['QubitInformationObject']);
     return $this;
 }
 /**
  * Sort information objects by lft value so that parent objects are inserted
  * before their children.
  *
  * @return QubitMigrate104 this object
  */
 protected function sortQubitInformationObjects()
 {
     $newList = array();
     $highLft = 0;
     foreach ($this->data['QubitInformationObject'] as $key => $row) {
         // If this left value is higher than any previous value, then just add
         // current row to the end of $newList
         if ($row['lft'] > $highLft) {
             $newList[$key] = $row;
             $highLft = $row['lft'];
         } else {
             $i = 0;
             foreach ($newList as $newKey => $newRow) {
                 if ($newRow['lft'] > $row['lft']) {
                     QubitMigrate::array_insert($newList, $i, array($key => $row));
                     break;
                 }
                 $i++;
             }
         }
     }
     $this->data['QubitInformationObject'] = $newList;
 }
 /**
  * Sort term objects with pre-defined IDs to start of array to prevent
  * pre-emptive assignment by auto-increment
  *
  * @return QubitMigrate110 this object
  */
 protected function sortQubitTerms()
 {
     $qubitTermConstantIds = array('ROOT_ID', 'CREATION_ID', 'CUSTODY_ID', 'PUBLICATION_ID', 'CONTRIBUTION_ID', 'COLLECTION_ID', 'ACCUMULATION_ID', 'TITLE_NOTE_ID', 'PUBLICATION_NOTE_ID', 'SOURCE_NOTE_ID', 'SCOPE_NOTE_ID', 'DISPLAY_NOTE_ID', 'ARCHIVIST_NOTE_ID', 'GENERAL_NOTE_ID', 'OTHER_DESCRIPTIVE_DATA_ID', 'MAINTENANCE_NOTE_ID', 'ARCHIVAL_MATERIAL_ID', 'PUBLISHED_MATERIAL_ID', 'ARTEFACT_MATERIAL_ID', 'CORPORATE_BODY_ID', 'PERSON_ID', 'FAMILY_ID', 'FAMILY_NAME_FIRST_NAME_ID', 'AUDIO_ID', 'IMAGE_ID', 'TEXT_ID', 'VIDEO_ID', 'OTHER_ID', 'MASTER_ID', 'REFERENCE_ID', 'THUMBNAIL_ID', 'COMPOUND_ID', 'LOCATION_ID', 'CONTAINER_ID', 'ARTEFACT_ID', 'HAS_PHYSICAL_OBJECT_ID', 'PARALLEL_FORM_OF_NAME_ID', 'OTHER_FORM_OF_NAME_ID', 'HIERARCHICAL_RELATION_ID', 'TEMPORAL_RELATION_ID', 'FAMILY_RELATION_ID', 'ASSOCIATIVE_RELATION_ID', 'RELATION_NOTE_DESCRIPTION_ID', 'RELATION_NOTE_DATE_ID', 'ALTERNATIVE_LABEL_ID', 'TERM_RELATION_ASSOCIATIVE_ID', 'STATUS_TYPE_PUBLICATION_ID', 'PUBLICATION_STATUS_DRAFT_ID', 'PUBLICATION_STATUS_PUBLISHED_ID', 'NAME_ACCESS_POINT_ID', 'ISDF_HIERARCHICAL_RELATION_ID', 'ISDF_TEMPORAL_RELATION_ID', 'ISDF_ASSOCIATIVE_RELATION_ID', 'STANDARDIZED_FORM_OF_NAME_ID', 'EXTERNAL_URI_ID', 'ACCESSION_ID', 'RIGHT_ID', 'DONOR_ID', 'RIGHT_BASIS_COPYRIGHT_ID', 'RIGHT_BASIS_LICENSE_ID', 'RIGHT_BASIS_STATUTE_ID', 'RIGHT_BASIS_POLICY_ID');
     // Restack array with Constant values at top
     $qubitTermArray = $this->data['QubitTerm'];
     foreach ($qubitTermConstantIds as $key => $constantName) {
         foreach ($qubitTermArray as $key => $term) {
             if (isset($term['id']) && $term['id'] == '<?php echo QubitTerm::' . $constantName . '."\\n" ?>') {
                 $newTermArray[$key] = $term;
                 unset($qubitTermArray[$key]);
                 break;
             }
         }
     }
     // Sort remainder of array by lft values
     QubitMigrate::sortByLft($qubitTermArray);
     // Append remaining (variable id) terms to the end of the new array
     foreach ($qubitTermArray as $key => $term) {
         $newTermArray[$key] = $term;
     }
     $this->data['QubitTerm'] = $newTermArray;
     return $this;
 }
 /**
  * Insert a non-hierarchical $newData into an existing dataset ($originalData),
  * which contains nested set columns (but is also non-hierarchical in
  * structure), before the row specified by $pivotKey.  Update lft and rgt
  * values appropriately.
  *
  * @param array $originalData The existing YAML dataset array
  * @param string $pivotKey key of row that should follow the inserted data
  * @param array $newData data to insert in $originalData
  */
 protected static function insertBeforeNestedSet(array &$originalData, $pivotKey, array $newData)
 {
     // If pivotKey doesn't exist, then just return a simple array merge
     if (!isset($originalData[$pivotKey])) {
         return array_merge($originalData, $newData);
     }
     $pivotIndex = null;
     $pivotLft = null;
     $width = count($newData) * 2;
     // Get index ($i) of pivot row and it's left value (if any)
     $i = 0;
     foreach ($originalData as $key => $row) {
         if ($pivotKey == $key) {
             $pivotIndex = $i;
             if (isset($originalData[$key]['lft'])) {
                 $pivotLft = $originalData[$key]['lft'];
             }
             break;
         }
         $i++;
     }
     // If a left value was found, then set merged values for lft & rgt columns
     if (null !== $pivotIndex) {
         // Loop through $newData and assign lft & rgt values
         $j = 0;
         foreach ($newData as &$row) {
             $row['lft'] = $pivotLft + $j * 2;
             $row['rgt'] = $pivotLft + $j * 2 + 1;
             $j++;
         }
         // Bump existing lft & rgt values
         foreach ($originalData as &$row) {
             if (isset($row['lft']) && $pivotLft <= $row['lft']) {
                 $row['lft'] += $width;
             }
             if (isset($row['rgt']) && $pivotLft < $row['rgt']) {
                 $row['rgt'] += $width;
             }
         }
     }
     // Merge $newData into $originalData
     QubitMigrate::array_insert($originalData, $i, $newData);
 }