You are here

function commerce_file_license_form_submit in Commerce File 7

Submit callback for commerce_file_license_form().

File

includes/commerce_file_license.forms.inc, line 322
Forms for creating / editing, deleting, issuing licenses

Code

function commerce_file_license_form_submit($form, &$form_state) {
  global $user;
  $entity = $form_state[COMMERCE_FILE_LICENSE_ENTITY_NAME];
  $values =& $form_state['values'];

  // If the user is editing, load a fresh copy to merge changes to.
  if (!empty($entity->license_id)) {
    $entity = $form_state[COMMERCE_FILE_LICENSE_ENTITY_NAME] = commerce_file_license_load($entity->license_id);
  }

  // Set the license's owner uid based on the supplied name
  // - name exists since it is REQUIRED and checked for existence
  $account = user_load_by_name($values['name']);
  $entity->uid = $account->uid;

  // Set created timestamp
  $entity->created = !empty($values['date']) ? strtotime($values['date']) : REQUEST_TIME;

  // Notify field widgets.
  field_attach_submit(COMMERCE_FILE_LICENSE_ENTITY_NAME, $entity, $form, $form_state);

  // Update the status if specified.
  if ($values['status'] != $values['status_original']) {

    // We skip saving in the update since we do it below once for the entire form submission.
    commerce_file_license_status_update($entity, $values['status'], TRUE);
  }

  // Save the license.
  if ($entity
    ->save()) {
    drupal_set_message(t('License saved.'));
  }
  else {
    drupal_set_message(t('License NOT saved. An error has occurred during saving the license.'), 'error');
  }
}