You are here

function organigrams_item_save in Organigrams 7

Save an organigrams item given an organigrams item object.

Parameters

object $organigrams_item: An organigrams item object.

Return value

int The SAVED_NEW or SAVED_UPDATED if successful, otherwise NULL.

2 calls to organigrams_item_save()
organigrams_form_organigrams_item_submit in ./organigrams_item.admin.inc
Form submit handler for 'organigrams_form_organigrams_item'.
organigrams_import_items in ./organigrams.module
Import organigram items in an existing organigram.

File

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

Code

function organigrams_item_save($organigrams_item) {

  // Prevent leading and trailing spaces in the organigrams item name.
  $organigrams_item->name = trim($organigrams_item->name);

  // If the organigrams machine name is not set, then load the organigrams and
  // assign the machine name.
  if (!isset($organigrams_item->organigrams_machine_name)) {

    // Load the organigrams using the organigrams ID.
    $organigrams = organigrams_load($organigrams_item->oid);

    // Assign the organigrams_machine_name property to the organigrams item.
    $organigrams_item->organigrams_machine_name = $organigrams->machine_name;
  }

  // Load the stored entity, if any.
  if (!empty($organigrams_item->iid) && !isset($organigrams_item->original)) {

    // Load the unchanged entity object.
    $organigrams_item->original = entity_load_unchanged('organigrams_item', $organigrams_item->iid);
  }

  // Allow modules to perform logic on presave.
  field_attach_presave('organigrams_item', $organigrams_item);
  module_invoke_all('organigrams_item_presave', $organigrams_item);
  module_invoke_all('entity_presave', $organigrams_item, 'organigrams_item');

  // Perform insert is no iid isset.
  if (empty($organigrams_item->iid)) {

    // Save operation (used for hooks).
    $op = 'insert';

    // Write organigrams item to DB.
    $status = drupal_write_record('organigrams_item_data', $organigrams_item);
    field_attach_insert('organigrams_item', $organigrams_item);
  }
  else {

    // Save operation (used for hooks).
    $op = 'update';

    // Write organigrams item to DB.
    $status = drupal_write_record('organigrams_item_data', $organigrams_item, 'iid');
    field_attach_update('organigrams_item', $organigrams_item);
  }

  // Reset the organigrams item cache.
  organigrams_items_static_reset();

  // Allow modules to react upon insert or update.
  module_invoke_all("organigrams_item_{$op}", $organigrams_item);
  module_invoke_all("entity_{$op}", $organigrams_item, 'organigrams_item');

  // Remove the original property.
  unset($organigrams_item->original);

  // Return the operation status.
  return $status;
}