You are here

public function CommerceCustomerProfileEntityController::save in Commerce Core 7

Saves a customer profile.

When saving a profile without an ID, this function will create a new profile at that time. Subsequent profiles that should be saved as new revisions should set $profile->revision to TRUE and include a log string in $profile->log.

Parameters

$profile: The full customer profile object to save.

$transaction: An optional transaction object.

Return value

SAVED_NEW or SAVED_UPDATED depending on the operation performed.

Overrides DrupalCommerceEntityController::save

File

modules/customer/includes/commerce_customer_profile.controller.inc, line 53
The controller for the customer profile entity containing the CRUD operations.

Class

CommerceCustomerProfileEntityController
The controller class for customer profiles contains methods for the profile CRUD operations. The load method is inherited from the default controller.

Code

public function save($profile, DatabaseTransaction $transaction = NULL) {
  if (!isset($transaction)) {
    $transaction = db_transaction();
    $started_transaction = TRUE;
  }
  try {
    global $user;

    // Determine if we will be inserting a new profile.
    $profile->is_new = empty($profile->profile_id);

    // Set the timestamp fields.
    if ($profile->is_new && empty($profile->created)) {
      $profile->created = REQUEST_TIME;
    }
    else {

      // Otherwise if the profile is not new but comes from an entity_create()
      // or similar function call that initializes the created timestamp and
      // uid value to empty strings, unset them to prevent destroying existing
      // data in those properties on update.
      if ($profile->created === '') {
        unset($profile->created);
      }
      if ($profile->uid === '') {
        unset($profile->uid);
      }
    }
    $profile->changed = REQUEST_TIME;
    $profile->revision_uid = $user->uid;
    $profile->revision_timestamp = REQUEST_TIME;
    if ($profile->is_new || !empty($profile->revision)) {

      // When inserting either a new profile or revision, $profile->log must
      // be set because {commerce_customer_profile_revision}.log is a text
      // column and therefore cannot have a default value. However, it might
      // not be set at this point, so we ensure that it is at least an empty
      // string in that case.
      if (!isset($profile->log)) {
        $profile->log = '';
      }
    }
    elseif (empty($profile->log)) {

      // If we are updating an existing profile without adding a new revision,
      // we need to make sure $profile->log is unset whenever it is empty. As
      // long as $profile->log is unset, drupal_write_record() will not attempt
      // to update the existing database column when re-saving the revision.
      unset($profile->log);
    }
    return parent::save($profile, $transaction);
  } catch (Exception $e) {
    if (!empty($started_transaction)) {
      $transaction
        ->rollback();
      watchdog_exception($this->entityType, $e);
    }
    throw $e;
  }
}