You are here

public function CommerceCouponEntityController::save in Commerce Coupon 7

Same name and namespace in other branches
  1. 7.2 includes/commerce_coupon.controller.inc \CommerceCouponEntityController::save()

Saves a coupon.

Parameters

$commerce_coupon: The full coupon object to save.

$transaction: An optional transaction object.

Return value

SAVED_NEW or SAVED_UPDATED depending on the operation performed.

Overrides DrupalCommerceEntityController::save

File

classes/commerce_coupon.inc, line 41
Coupon controller class.

Class

CommerceCouponEntityController
@file Coupon controller class.

Code

public function save($entity, DatabaseTransaction $transaction = NULL) {
  $transaction = isset($transaction) ? $transaction : db_transaction();

  // Hardcode the changed time.
  $entity->changed = REQUEST_TIME;
  if (empty($entity->{$this->idKey}) || !empty($entity->is_new)) {

    // Set the creation timestamp if not set, for new entities.
    if (empty($entity->created)) {
      $entity->created = REQUEST_TIME;
    }
  }

  // Generate a code if no is set:
  $code = field_get_items('commerce_coupon', $entity, 'commerce_coupon_code');
  if (!isset($code) || empty($code)) {
    $entity->commerce_coupon_code[LANGUAGE_NONE][0]['value'] = commerce_coupon_generate_coupon_code();
  }
  return parent::save($entity);
}