You are here

function organigrams_save in Organigrams 7

Save an organigram given an organigrams object.

Parameters

object $organigrams: An organigrams object.

Return value

int The SAVED_NEW or SAVED_UPDATED if successful, otherwise NULL.

3 calls to organigrams_save()
organigrams_form_import_submit in ./organigrams.admin.inc
Submit function for organigrams_form_import.
organigrams_form_organigrams_submit in ./organigrams.admin.inc
Form submit handler for 'organigrams_form_organigrams'.
organigrams_form_overview_organigrams_submit in ./organigrams.admin.inc
Form submit handler for 'organigrams_form_overview_organigrams'.

File

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

Code

function organigrams_save($organigrams) {

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

  // Load the stored entity, if any.
  if (!empty($organigrams->oid)) {

    // If the original property is not set.
    if (!isset($organigrams->original)) {

      // Load an unchanged organigrams object.
      $organigrams->original = entity_load_unchanged('organigrams', $organigrams->oid);
    }
    $organigrams->old_machine_name = $organigrams->original->machine_name;
  }

  // If the module property is missing.
  if (!isset($organigrams->module)) {

    // Then use the 'organigrams' as creator module.
    $organigrams->module = 'organigrams';
  }

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

  // Initialize the status variable.
  $status = NULL;

  // If the oid and name is not empty.
  if (!empty($organigrams->oid) && !empty($organigrams->name)) {

    // Write the organigrams to the DB.
    $status = drupal_write_record('organigrams_data', $organigrams, 'oid');

    // If the organigrams machine name changed.
    if ($organigrams->old_machine_name != $organigrams->machine_name) {

      // Rename the field bundle.
      field_attach_rename_bundle('organigrams_item', $organigrams->old_machine_name, $organigrams->machine_name);
    }

    // Allow modules to perform action on update.
    field_attach_update('organigrams', $organigrams);
    module_invoke_all('organigrams_update', $organigrams);
    module_invoke_all('entity_update', $organigrams, 'organigrams');
  }
  elseif (empty($organigrams->oid)) {

    // Write the organigrams to the DB.
    $status = drupal_write_record('organigrams_data', $organigrams);

    // Attach the field bundle.
    field_attach_create_bundle('organigrams_item', $organigrams->machine_name);

    // Allow modules to perform action on insert.
    field_attach_insert('organigrams', $organigrams);
    module_invoke_all('organigrams_insert', $organigrams);
    module_invoke_all('entity_insert', $organigrams, 'organigrams');
  }

  // Remove the original and old_machine_name property.
  unset($organigrams->original);
  unset($organigrams->old_machine_name);

  // Clear cache.
  cache_clear_all();

  // Request entity cache reset for the given oid.
  entity_get_controller('organigrams')
    ->resetCache(array(
    $organigrams->oid,
  ));

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