You are here

function organigrams_import_items in Organigrams 7

Import organigram items in an existing organigram.

Parameters

int $oid: An organigram ID.

string $import: A JSON string containing organigram items to import.

Return value

bool|string TRUE if the import succeeded, otherwise a string containing an error message.

Throws

\Exception

2 calls to organigrams_import_items()
organigrams_form_import_items_submit in ./organigrams.admin.inc
Submit function for organigrams_form_import_items.
organigrams_form_import_submit in ./organigrams.admin.inc
Submit function for organigrams_form_import.

File

./organigrams.module, line 1916
Defines the organigrams functions and entity types.

Code

function organigrams_import_items($oid, $import) {

  // Load the organigram to import the items into.
  $organigram = organigrams_load($oid);

  // Check if the organigram exists.
  if (empty($organigram)) {
    watchdog('organigrams', 'Failed importing organigram items because no organigram was found.');
    return t('Failed importing organigram items because no organigram was found.');
  }

  // Check if we have any import data.
  if (empty($import)) {
    watchdog('organigrams', 'Failed importing organigram items because no import was found.');
    return t('Failed importing organigram items because no import was found.');
  }

  // Store the old data for rollback purposes.
  $old_data = organigrams_get_tree($oid, 0, NULL, TRUE);

  // Remove all old data from the organigram.
  foreach ($old_data as $old_item) {
    organigrams_item_delete($old_item->iid);
  }

  // This array stores items which need to be saved again when a parent has
  // changed.
  $needs_resave = array();

  // Iterate through the new data.
  foreach ($import as $new_item) {

    // Store the original item ID.
    $orig_iid = $new_item->iid;

    // If this item was marked for resave, remove it from the array because it
    // gets saved now.
    if (isset($needs_resave[$orig_iid])) {
      unset($needs_resave[$orig_iid]);
    }

    // Unset the item ID. It will receive a new ID when saving to prevent
    // duplicate IDs in the database.
    unset($new_item->iid);

    // Set the organigram ID and machine name to the organigram we are importing
    // into.
    $new_item->oid = $oid;
    $new_item->organigrams_machine_name = $organigram->machine_name;

    // Save the new item.
    $result = organigrams_item_save($new_item);

    // If saving failed somehow, abort and call this function again with the
    // old data to restore the organigram.
    if (!$result) {
      watchdog('organigrams', 'An error occurred importing item %name. Old values are restored.', array(
        '%name' => $new_item->name,
      ));
      organigrams_import_items($oid, $old_data);
      return t('An error occurred importing item %name. Old values are restored.', array(
        '%name' => $new_item->name,
      ));
    }

    // On saving, an item receives a new ID. Iterate once more through the
    // import data to update the parent IDs referencing to this newly saved
    // item.
    foreach ($import as $key => $value) {
      if ($value->parent == $orig_iid) {
        $import[$key]->parent = $new_item->iid;

        // Add this item to the resave array to make sure its new data gets
        // saved even if this item already has been imported.
        $needs_resave[$value->iid] = $import[$key];
      }
    }
  }

  // Resave the items in the resave array.
  if (!empty($needs_resave)) {
    foreach ($needs_resave as $item) {
      organigrams_item_save($item);
    }
  }
  watchdog('organigrams', 'Successfully imported items in organigram %name', array(
    '%name' => $organigram->name,
  ));
  return TRUE;
}