You are here

public function CommercePricelistItemController::save in Commerce Pricelist 7

Implements EntityAPIControllerInterface.

Parameters

$transaction: Optionally a DatabaseTransaction object to use. Allows overrides to pass in their transaction object.

Overrides EntityAPIController::save

File

includes/commerce_pricelist_item.controller.inc, line 32
Contains CommercePricelistItemController

Class

CommercePricelistItemController
Handles CRUD for Pricelist item.

Code

public function save($entity, DatabaseTransaction $transaction = NULL) {

  // Make sure we are saving into an existing pricelist.
  $pricelist = entity_load('commerce_pricelist_list', array(
    $entity->pricelist_id,
  ));
  if (empty($pricelist)) {
    watchdog('commerce pricelist', t('Attepted to save a pricelist item into a non-existent pricelist.'));
    return FALSE;
  }

  // Set default values.
  if (empty($entity->created)) {
    $entity->created = time();
  }
  if (empty($entity->valid_to)) {
    $entity->valid_to = COMMERCE_PRICELIST_UNIX_TIME_APOCALYPSE;
  }

  // Check if an item already exists with these values.
  $existing_id = commerce_pricelist_item_get_existing_id($entity);
  if ($existing_id) {
    $entity->item_id = $existing_id;
    $entity->is_new = FALSE;
    drupal_set_message(t('Found and replaced a duplicate pricelist item in this list.'));
  }
  return parent::save($entity, $transaction);
}