You are here

commerce_pricelist.module in Commerce Pricelist 7

Same filename and directory in other branches
  1. 8.2 commerce_pricelist.module
  2. 8 commerce_pricelist.module

Implements the basic functionality required for price lists

File

commerce_pricelist.module
View source
<?php

/**
 * @file
 * Implements the basic functionality required for price lists
 */
define('COMMERCE_PRICELIST_UNIX_TIME_APOCALYPSE', 2147390000);

/**
 * Implements hook_menu().
 */
function commerce_pricelist_menu() {

  // This provides a place for Field API to hang its own
  // interface and has to be the same as what was defined
  // in entity_info() above.
  $items['admin/commerce/pricelist/commerce_pricelist_list'] = array(
    'title' => 'Price lists',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_pricelist_draggable_form',
    ),
    'access arguments' => array(
      'administer commerce_pricelist_list entities',
    ),
    'file' => 'includes/commerce_pricelist.admin.inc',
  );

  // Add example entities.
  $items['admin/commerce/pricelist/commerce_pricelist_list/add'] = array(
    'title' => 'Add a Price list',
    'page callback' => 'commerce_pricelist_list_add',
    'access arguments' => array(
      'create commerce_pricelist_list entities',
    ),
    'type' => MENU_LOCAL_ACTION,
    'file' => 'includes/commerce_pricelist.admin.inc',
  );

  // Add example entities.
  $items['admin/commerce/products/%commerce_product/add_price'] = array(
    'title' => 'Add a Price list item',
    'page callback' => 'commerce_pricelist_item_add',
    'page arguments' => array(
      3,
    ),
    'access arguments' => array(
      'edit any commerce_pricelist_list entity',
    ),
    'file' => 'includes/commerce_pricelist.admin.inc',
    'type' => MENU_CALLBACK,
  );

  // The page to view our entities - needs to follow what
  // is defined in basic_uri and will use load_basic to retrieve
  // the necessary entity info.
  $items['admin/commerce/pricelist/commerce_pricelist_list/%commerce_pricelist_list'] = array(
    'title callback' => 'commerce_pricelist_list_title',
    'title arguments' => array(
      4,
    ),
    'page callback' => 'commerce_pricelist_list_view',
    'page arguments' => array(
      4,
    ),
    'access arguments' => array(
      'view any commerce_pricelist_list entity',
    ),
    'file' => 'includes/commerce_pricelist.admin.inc',
  );

  // 'View' tab for an individual entity page.
  $items['admin/commerce/pricelist/commerce_pricelist_list/%commerce_pricelist_list/view'] = array(
    'title' => 'View',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
    'file' => 'includes/commerce_pricelist.admin.inc',
  );

  // 'Edit' tab for an individual entity page.
  $items['admin/commerce/pricelist/commerce_pricelist_list/%commerce_pricelist_list/edit'] = array(
    'title' => 'Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_pricelist_list_form',
      4,
    ),
    'access arguments' => array(
      'edit any commerce_pricelist_list entity',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/commerce_pricelist.admin.inc',
  );

  // 'Edit' tab for an individual entity page.
  $items['admin/commerce/pricelist/commerce_pricelist_item/%commerce_pricelist_item/edit'] = array(
    'title' => 'Edit',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_pricelist_item_form',
      4,
    ),
    'access arguments' => array(
      'edit any commerce_pricelist_list entity',
    ),
    'file' => 'includes/commerce_pricelist.admin.inc',
  );

  // 'Add' tab for an individual entity page.
  $items['admin/commerce/pricelist/commerce_pricelist_list/%commerce_pricelist_list/add'] = array(
    'title' => 'Add price',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_pricelist_item_form',
      NULL,
      4,
    ),
    'access arguments' => array(
      'edit any commerce_pricelist_list entity',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/commerce_pricelist.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_entity_info().
 *
 * This is the fundamental description of the entity.
 *
 * It provides a single entity with a single bundle and without revision
 * support.
 */
function commerce_pricelist_entity_info() {
  $info['commerce_pricelist_list'] = array(
    'label' => t('Commerce Price List list'),
    'entity class' => 'CommercePricelistList',
    'controller class' => 'CommercePricelistListController',
    'base table' => 'commerce_pricelist_list',
    'uri callback' => 'commerce_pricelist_list_uri',
    'access callback' => 'commerce_pricelist_entity_access',
    'metadata controller class' => FALSE,
    'fieldable' => FALSE,
    'uuid' => TRUE,
    'entity keys' => array(
      'id' => 'list_id',
      'uuid' => 'uuid',
      'label' => 'title',
    ),
    'bundle keys' => array(
      'bundle' => 'list_id',
    ),
    'bundles' => array(),
    'static cache' => TRUE,
    'view modes' => array(
      'full' => array(
        'label' => t('full'),
        'custom settings' => FALSE,
      ),
    ),
  );
  $info['commerce_pricelist_item'] = array(
    'label' => t('Commerce Price List item'),
    'entity class' => 'CommercePricelistItem',
    'controller class' => 'CommercePricelistItemController',
    'base table' => 'commerce_pricelist_item',
    'uri callback' => 'commerce_pricelist_item_uri',
    'access callback' => 'commerce_pricelist_entity_access',
    'metadata controller class' => FALSE,
    'fieldable' => FALSE,
    'uuid' => TRUE,
    'bundle keys' => array(
      'bundle' => 'item_id',
    ),
    'bundles' => array(),
    'entity keys' => array(
      'id' => 'item_id',
      'uuid' => 'uuid',
    ),
    'static cache' => TRUE,
    'view modes' => array(
      'full' => array(
        'label' => t('full'),
        'custom settings' => FALSE,
      ),
    ),
  );
  return $info;
}

/**
 * Implements hook_entity_property_info().
 *
 * @return array
 */
function commerce_pricelist_entity_property_info() {
  $info = array();

  // Add meta-data about the commerce_pricelist_list properties.
  $properties = array();
  $properties['list_id'] = array(
    'label' => t('List ID'),
    'description' => t('The internal numeric ID of the pricelist.'),
    'type' => 'integer',
    'schema field' => 'list_id',
  );
  $properties['title'] = array(
    'label' => t('Title'),
    'description' => t('The title of the list.'),
    'type' => 'text',
    'setter callback' => 'entity_property_verbatim_set',
    'required' => TRUE,
    'schema field' => 'title',
  );
  $properties['uid'] = array(
    'label' => t("Author"),
    'type' => 'user',
    'description' => t("The author of the pricelist."),
    'setter callback' => 'entity_property_verbatim_set',
    'required' => TRUE,
    'schema field' => 'uid',
  );
  $properties['weight'] = array(
    'label' => t('Weight'),
    'type' => 'integer',
    'setter callback' => 'entity_property_verbatim_set',
    'description' => t("The weight of the list."),
    'required' => TRUE,
    'schema field' => 'weight',
  );
  $properties['status'] = array(
    'label' => t('Status'),
    'description' => t('Whether or not the pricelist is active.'),
    'type' => 'boolean',
    'setter callback' => 'entity_property_verbatim_set',
    'schema field' => 'status',
  );
  $properties['created'] = array(
    'label' => t('Date created'),
    'description' => t('The date the pricelist was created.'),
    'type' => 'date',
    'setter callback' => 'entity_property_verbatim_set',
    'schema field' => 'created',
  );
  $properties['changed'] = array(
    'label' => t('Date updated'),
    'description' => t('The date the pricelist was last updated.'),
    'type' => 'date',
    'schema field' => 'changed',
  );

  // @todo: Info about 'data' property.
  $info['commerce_pricelist_list']['properties'] = $properties;

  // Add meta-data about the commerce_pricelist_item properties.
  $properties = array();
  $properties['item_id'] = array(
    'label' => t('List ID'),
    'description' => t('The internal numeric ID of the pricelist the item belongs to.'),
    'type' => 'integer',
    'schema field' => 'item_id',
  );
  $properties['sku'] = array(
    'label' => t('SKU'),
    'description' => t('The human readable product SKU.'),
    'type' => 'text',
    'setter callback' => 'entity_property_verbatim_set',
    'required' => TRUE,
    'schema field' => 'sku',
  );
  $properties['pricelist_id'] = array(
    'label' => t('List ID'),
    'description' => t('The internal numeric ID of the pricelist the item belongs to.'),
    'type' => 'commerce_pricelist_list',
    'setter callback' => 'entity_property_verbatim_set',
    'schema field' => 'pricelist_id',
  );
  $properties['valid_from'] = array(
    'label' => t('Valid from date'),
    'description' => t('The date item will be active from.'),
    'type' => 'date',
    'setter callback' => 'entity_property_verbatim_set',
    'schema field' => 'valid_from',
  );
  $properties['valid_to'] = array(
    'label' => t('Valid to date'),
    'description' => t('The date item will be active to.'),
    'type' => 'date',
    'setter callback' => 'entity_property_verbatim_set',
    'schema field' => 'valid_to',
  );
  $properties['quantity'] = array(
    'label' => t('Quantity'),
    'description' => t('Quantity associated with this pricelist item'),
    'type' => 'decimal',
    'setter callback' => 'entity_property_verbatim_set',
    'required' => TRUE,
    'schema field' => 'quantity',
  );
  $properties['price_amount'] = array(
    'label' => t('Amount'),
    'description' => t('The amount for this pricelist item.'),
    'type' => 'integer',
    'setter callback' => 'entity_property_verbatim_set',
    'schema field' => 'price_amount',
  );
  $properties['currency_code'] = array(
    'label' => t('Currency Code'),
    'description' => t('The currency code for this pricelist item.'),
    'type' => 'text',
    'setter callback' => 'entity_property_verbatim_set',
    'schema field' => 'currency_code',
  );
  $info['commerce_pricelist_item']['properties'] = $properties;
  return $info;
}
function commerce_pricelist_commerce_price_component_type_info() {
  return array(
    'list_price' => array(
      'title' => t('List price'),
      'display_title' => t('Your pricelist difference'),
      'weight' => -45,
    ),
  );
}

/**
 * Implements hook_permission().
 */
function commerce_pricelist_permission() {
  $permissions = array(
    'administer commerce_pricelist_list entities' => array(
      'title' => t('Administer commerce_pricelist_list entities'),
    ),
    'view any commerce_pricelist_list entity' => array(
      'title' => t('View any Price list entity'),
    ),
    'edit any commerce_pricelist_list entity' => array(
      'title' => t('Edit any Price list entity'),
    ),
    'create commerce_pricelist_list entities' => array(
      'title' => t('Create Price list Entities'),
    ),
  );
  return $permissions;
}

/**
 * Return an array of prices like {sku : {pricelist_id: {quantity : price }}}.
 *
 * @todo take currency into account...
 *
 * @param $sku
 * @param bool $time
 * @return array
 */
function commerce_pricelist_get_prices($sku, $time = FALSE) {
  $cache_id = 'commerce_pricelist_get_prices_' . $sku;
  $prices =& drupal_static($cache_id);
  if (!isset($prices)) {
    if ($cache = cache_get($cache_id)) {
      $prices = $cache->data;
    }
    else {
      if (!$time) {
        $time = time();
      }
      $prices = array();
      $pricelist_items = array();
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'commerce_pricelist_item')
        ->propertyCondition('sku', $sku, '=')
        ->propertyCondition('valid_from', $time, '<')
        ->propertyCondition('valid_to', $time, '>')
        ->propertyOrderBy('quantity', 'DESC');
      $result = $query
        ->execute();
      if (!empty($result['commerce_pricelist_item'])) {
        $pricelist_items = commerce_pricelist_item_load_multiple(array_keys($result['commerce_pricelist_item']));
      }

      // Create price array keyed by price list.
      foreach ($pricelist_items as $item) {
        $prices[$sku][$item->pricelist_id][$item->quantity] = array(
          'currency_code' => $item->currency_code,
          'amount' => $item->price_amount,
        );
      }
      cache_set($cache_id, $prices, 'cache', time() + 300);

      // 5min
    }
  }
  return $prices;
}

/**
 * Get the price for a quantity of a product for current user.
 *
 * @param $sku
 * @param int $quantity
 * @return int
 */
function commerce_pricelist_get_user_price($user, $sku, $quantity = 0, $time = FALSE) {
  $price = array();
  $user_pricelists = commerce_pricelist_get_user_pricelists($user);
  if (empty($user_pricelists)) {

    // @todo set error
    return $price;
  }
  $prices = commerce_pricelist_get_prices($sku, $time);
  if (empty($prices)) {

    // @todo set error
    return $price;
  }

  // Loop through all pricelists for current user and match against prices
  // for this product.
  foreach ($user_pricelists as $pricelist) {
    if (isset($prices[$sku][$pricelist->list_id])) {
      foreach ($prices[$sku][$pricelist->list_id] as $quantity_limit => $price_item) {

        // Check if quantity arg is higher than the quantity limit for this price.
        if ($quantity >= $quantity_limit) {

          // We found our price, break out.
          $price = $price_item;
          break 2;
        }
      }
    }
  }
  return $price;
}

/**
 * Get pricelists for a user.
 *
 * @return array
 */
function commerce_pricelist_get_user_pricelists($user) {
  $cache_id = 'commerce_pricelist_get_user_pricelists_' . $user->uid;
  $pricelists =& drupal_static($cache_id);
  if (!isset($pricelists)) {
    if ($cache = cache_get($cache_id)) {
      $pricelists = $cache->data;
    }
    else {
      $pricelists = array();
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', 'commerce_pricelist_list')
        ->propertyCondition('status', 1, '=')
        ->propertyOrderBy('weight');
      $result = $query
        ->execute();
      if (!empty($result['commerce_pricelist_list'])) {
        $pricelists = commerce_pricelist_list_load_multiple(array_keys($result['commerce_pricelist_list']));
      }

      // Let modules implementing hook_commerce_pricelists_alter() modify list.
      drupal_alter('commerce_pricelists', $pricelists, $user);
      foreach ($pricelists as $id => $pricelist) {
        if (isset($pricelist->score) && $pricelist->score < 0) {
          unset($pricelists[$id]);
        }
      }
      cache_set($cache_id, $pricelists, 'cache', time() + 3600);
    }
  }
  return $pricelists;
}

/**
 * Filter array of pricelists based on conditions.
 *
 * @param $pricelists
 * @param $conditions
 */
function commerce_pricelist_filter_pricelists(&$pricelists, $conditions) {
  foreach ($pricelists as $id => $pricelist) {
    $pass = FALSE;
    foreach ($conditions as $key => $values) {

      // Check if this pricelist has this condition set and has correct value.
      if (isset($pricelist->data['filter'][$key])) {

        // Loop over condition values and see if anyone matches this pricelist.
        foreach ($values as $value) {
          if (in_array($value, $pricelist->data['filter'][$key])) {
            $pass = TRUE;
          }
        }
      }
      else {

        // Pricelist is missing condition.
        $pass = FALSE;
        break;
      }
    }
    $pricelist->score = isset($pricelist->score) ? $pricelist->score : 0;
    $pricelist->score += $pass ? 1 : -1;
  }
}

/**
 * Set a filter value on an price list entity.
 *
 * @param $entity
 * @param $filter_key
 * @param $value
 */
function commerce_pricelist_list_filter_set(&$entity, $filter_key, $value) {
  if (!isset($entity->data['filter'])) {
    $entity->data['filter'] = array();
  }
  $entity->data['filter'][$filter_key] = $value;
}

/**
 * Get a filter value form a price list entity
 *
 * @param $entity
 * @param $filter
 */
function commerce_pricelist_list_filter_get($entity, $filter_key) {
  return isset($entity->data['filter'][$filter_key]) ? $entity->data['filter'][$filter_key] : array();
}

/**
 * Delete pricelist items when a pricelist is deleted.
 *
 * Implements hook_entity_delete().
 */
function commerce_pricelist_entity_delete($entity, $type) {
  if ($type == 'commerce_pricelist_list') {
    $query = new EntityFieldQuery();
    $query
      ->entityCondition('entity_type', 'commerce_pricelist_item')
      ->propertyCondition('pricelist_id', $entity->list_id, '=');
    $result = $query
      ->execute();
    if (!empty($result['commerce_pricelist_item'])) {
      commerce_pricelist_item_delete_multiple($result['commerce_pricelist_item']);
    }
  }
}

/**
 * Implements hook_theme().
 *
 * We need run our forms through custom theme functions in order to build the
 * table structure which is required by tabledrag.js.  Before we can use our
 * custom theme functions, we need to implement hook_theme in order to register
 * them with Drupal.
 *
 * We are defining our theme hooks with the same name as the form generation
 * function so that Drupal automatically calls our theming function when the
 * form is displayed.
 */
function commerce_pricelist_theme() {
  return array(
    // Theme function for the 'simple' example.
    'commerce_pricelist_draggable_form' => array(
      'render element' => 'form',
    ),
  );
}

/**
 * @param $op
 * @param $entity
 * @param $account
 * @param $entity_type
 * @return bool
 */
function commerce_pricelist_entity_access($op, $entity, $account, $entity_type) {
  global $user;
  $account = isset($account) ? $account : $user;

  // There are no separate permissions for pricelist items so handle as list.
  $entity_type = 'commerce_pricelist_list';
  if ($op == 'view') {
    return user_access('view any ' . $entity_type . ' entity', $account);
  }
  if ($op == 'create') {
    return user_access('create ' . $entity_type . ' entities', $account);
  }
  if ($op == 'edit') {
    return user_access('edit any ' . $entity_type . ' entity', $account);
  }
  if ($op == 'update') {
    return user_access('edit any ' . $entity_type . ' entity', $account);
  }
  if ($op == 'delete') {
    return user_access('edit any ' . $entity_type . ' entity', $account);
  }
  return FALSE;
}

/**
 *  Add price list fieldgroup to product form.
 *  Implements hook_form_BASE_FORM_ID_alter().
 */
function commerce_pricelist_form_commerce_product_ui_product_form_alter(&$form, &$form_state, $form_id) {
  if (user_access('view any commerce_pricelist_list entity') && ($sku = $form['sku']['#default_value'])) {
    module_load_include('inc', 'commerce_pricelist', 'includes/commerce_pricelist.admin');
    $pricelists = commerce_pricelist_list_load_multiple();
    $prices = array();
    foreach ($pricelists as $pricelist) {
      $table = commerce_pricelist_item_list_entities($pricelist->list_id, $sku);
      if (isset($table['entity_table'])) {
        $table['entity_table']['#prefix'] = check_plain($pricelist->title);
        $prices[$pricelist->list_id] = $table;
      }
    }
    $prices['actions'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'form-actions',
          'container-inline',
        ),
      ),
      '#weight' => 100,
    );
    $options = array(
      'attributes' => array(
        'class' => array(
          'button',
        ),
      ),
    );
    $prices['actions']['administer'] = array(
      '#type' => 'item',
      '#markup' => l(t('Administer price lists'), 'admin/commerce/pricelist/commerce_pricelist_list', $options),
      '#weight' => 50,
    );
    $product = $form['#entity'];
    $product_id = $product->product_id;
    if ($product_id) {
      $prices['actions']['add'] = array(
        '#type' => 'item',
        '#markup' => l(t('Add price'), 'admin/commerce/products/' . $product_id . '/add_price', $options + array(
          'query' => array(
            'destination' => current_path(),
          ),
        )),
        '#weight' => 49,
      );
    }
    $form['commerce_pricelists'] = array(
      '#type' => 'fieldset',
      '#title' => t('Price lists'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 100,
      'prices' => $prices,
    );
  }
}

/**
 * Format a date for display, return empty string for default date values.
 *
 * @param $value
 * @param bool $format
 * @return mixed|string
 */
function _commerce_pricelist_display_date($value, $format = FALSE) {
  if ($value < COMMERCE_PRICELIST_UNIX_TIME_APOCALYPSE && $value > 0 && is_numeric($value)) {
    return $format ? format_date($value, 'custom', $format) : format_date($value, 'short');
  }
  return '';
}

/**
 *
 * Pricelist Item
 *
 */

/**
 * Pricelist item class.
 */
class CommercePricelistItem extends Entity {

}

/**
 * Returns an initialized pricelist item object.
 *
 * @return
 *   A pricelist item object with all default fields initialized.
 */
function commerce_pricelist_item_new($pricelist_id = NULL, $sku = NULL) {
  return entity_create('commerce_pricelist_item', array(
    'pricelist_id' => $pricelist_id,
    'sku' => $sku,
  ));
}

/**
 * We save the entity by calling the controller.
 */
function commerce_pricelist_item_save(&$entity) {
  $result = entity_save('commerce_pricelist_item', $entity);
  cache_clear_all('commerce_pricelist', 'cache', TRUE);
  return $result;
}

/**
 * Use the controller to delete the entity.
 */
function commerce_pricelist_item_delete($id) {
  return commerce_pricelist_item_delete_multiple(array(
    $id,
  ));
}

/**
 * Use the controller to delete the entity.
 */
function commerce_pricelist_item_delete_multiple($ids) {
  $result = entity_delete_multiple('commerce_pricelist_item', $ids);
  cache_clear_all('commerce_pricelist', 'cache', TRUE);
  return $result;
}

/**
 * Get the id of an existing pricelist item with these values.
 * We do not want more than one item with these values.
 *
 * @param $entity
 * @return int
 */
function commerce_pricelist_item_get_existing_id($pricelist_item) {
  $item_id = db_query("SELECT item_id FROM {commerce_pricelist_item} " . "WHERE item_id != :item_id AND sku = :sku AND valid_to = :valid_to AND valid_from = :valid_from " . "AND quantity = :quantity AND pricelist_id = :pricelist_id", array(
    ':item_id' => $pricelist_item->item_id,
    ':sku' => $pricelist_item->sku,
    ':valid_from' => $pricelist_item->valid_from,
    ':valid_to' => $pricelist_item->valid_to,
    ':quantity' => $pricelist_item->quantity,
    ':pricelist_id' => $pricelist_item->pricelist_id,
  ))
    ->fetchField();
  return $item_id;
}

/**
 * Callback for a page title when this entity is displayed.
 */
function commerce_pricelist_item_title($entity) {
  return t('Pricelist item');
}

/**
 * Fetch a pricelist item.
 *
 * This function ends up being a shim between the menu system and
 * commerce_pricelist_item_load_multiple().
 *
 * This function gets its name from the menu system's wildcard
 * naming conventions. For example, /path/%wildcard would end
 * up calling wildcard_load(%wildcard value). In our case defining
 * the path: admin/commerce/pricelist/commerce_pricelist/basic/%commerce_pricelist_item in
 * hook_menu() tells Drupal to call commerce_pricelist_item_load().
 *
 * @param int $entity_id
 *   Integer specifying the basic entity id.
 * @param bool $reset
 *   A boolean indicating that the internal cache should be reset.
 *
 * @return object
 *   A fully-loaded $entity object or FALSE if it cannot be loaded.
 *
 * @see commerce_pricelist_item_load_multiple()
 * @see commerce_pricelist_menu()
 */
function commerce_pricelist_item_load($entity_id = NULL, $reset = FALSE) {
  $entity_ids = isset($entity_id) ? array(
    $entity_id,
  ) : array();
  $entity = commerce_pricelist_item_load_multiple($entity_ids, array(), $reset);
  return $entity ? reset($entity) : FALSE;
}

/**
 * Loads multiple basic entities.
 *
 * We only need to pass this request along to entity_load(), which
 * will in turn call the load() method of our entity controller class.
 */
function commerce_pricelist_item_load_multiple($entity_ids = FALSE, $conditions = array(), $reset = FALSE) {
  return entity_load('commerce_pricelist_item', $entity_ids, $conditions, $reset);
}

/**
 * Implements the uri callback.
 */
function commerce_pricelist_item_uri($entity) {
  return array(
    'path' => 'admin/commerce/pricelist/commerce_pricelist/item/' . $entity->item_id,
  );
}

/**
 *
 * Pricelist list
 *
 */

/**
 * Pricelist list class.
 */
class CommercePricelistList extends Entity {

}

/**
 * Returns an initialized pricelist list object.
 *
 * @return
 *   A pricelist list object with all default fields initialized.
 */
function commerce_pricelist_list_new() {
  return entity_create('commerce_pricelist_list', array());
}

/**
 * We save the entity by calling the controller.
 */
function commerce_pricelist_list_save(&$entity) {
  $result = entity_save('commerce_pricelist_list', $entity);
  cache_clear_all('commerce_pricelist', 'cache', TRUE);
  return $result;
}

/**
 * Use the controller to delete the entity.
 */
function commerce_pricelist_list_delete($id) {
  $result = entity_delete('commerce_pricelist_list', $id);
  cache_clear_all('commerce_pricelist', 'cache', TRUE);
  return $result;
}

/**
 * Fetch a basic object.
 *
 * This function ends up being a shim between the menu system and
 * commerce_pricelist_list_load_multiple().
 *
 * This function gets its name from the menu system's wildcard
 * naming conventions. For example, /path/%wildcard would end
 * up calling wildcard_load(%wildcard value). In our case defining
 * the path: admin/commerce/pricelist/commerce_pricelist/basic/%commerce_pricelist_list in
 * hook_menu() tells Drupal to call commerce_pricelist_list_load().
 *
 * @param int $entity_id
 *   Integer specifying the basic entity id.
 * @param bool $reset
 *   A boolean indicating that the internal cache should be reset.
 *
 * @return object
 *   A fully-loaded $entity object or FALSE if it cannot be loaded.
 *
 * @see commerce_pricelist_list_load_multiple()
 * @see commerce_pricelist_menu()
 */
function commerce_pricelist_list_load($list_id = NULL, $reset = FALSE) {
  $list_id = isset($list_id) ? array(
    $list_id,
  ) : array();
  $entity = commerce_pricelist_list_load_multiple($list_id, array(), $reset);
  return $entity ? reset($entity) : FALSE;
}

/**
 * Loads multiple basic entities.
 *
 * We only need to pass this request along to entity_load(), which
 * will in turn call the load() method of our entity controller class.
 */
function commerce_pricelist_list_load_multiple($list_id = FALSE, $conditions = array(), $reset = FALSE) {
  return entity_load('commerce_pricelist_list', $list_id, $conditions, $reset);
}

/**
 * Implements the uri callback.
 */
function commerce_pricelist_list_uri($entity) {
  return array(
    'path' => 'admin/commerce/pricelist/commerce_pricelist_list/' . $entity->list_id,
  );
}

/**
 * Callback for a page title when this entity is displayed.
 */
function commerce_pricelist_list_title($entity) {
  return t('@item_title', array(
    '@item_title' => $entity->title,
  ));
}

Functions

Namesort descending Description
commerce_pricelist_commerce_price_component_type_info
commerce_pricelist_entity_access
commerce_pricelist_entity_delete Delete pricelist items when a pricelist is deleted.
commerce_pricelist_entity_info Implements hook_entity_info().
commerce_pricelist_entity_property_info Implements hook_entity_property_info().
commerce_pricelist_filter_pricelists Filter array of pricelists based on conditions.
commerce_pricelist_form_commerce_product_ui_product_form_alter Add price list fieldgroup to product form. Implements hook_form_BASE_FORM_ID_alter().
commerce_pricelist_get_prices Return an array of prices like {sku : {pricelist_id: {quantity : price }}}.
commerce_pricelist_get_user_price Get the price for a quantity of a product for current user.
commerce_pricelist_get_user_pricelists Get pricelists for a user.
commerce_pricelist_item_delete Use the controller to delete the entity.
commerce_pricelist_item_delete_multiple Use the controller to delete the entity.
commerce_pricelist_item_get_existing_id Get the id of an existing pricelist item with these values. We do not want more than one item with these values.
commerce_pricelist_item_load Fetch a pricelist item.
commerce_pricelist_item_load_multiple Loads multiple basic entities.
commerce_pricelist_item_new Returns an initialized pricelist item object.
commerce_pricelist_item_save We save the entity by calling the controller.
commerce_pricelist_item_title Callback for a page title when this entity is displayed.
commerce_pricelist_item_uri Implements the uri callback.
commerce_pricelist_list_delete Use the controller to delete the entity.
commerce_pricelist_list_filter_get Get a filter value form a price list entity
commerce_pricelist_list_filter_set Set a filter value on an price list entity.
commerce_pricelist_list_load Fetch a basic object.
commerce_pricelist_list_load_multiple Loads multiple basic entities.
commerce_pricelist_list_new Returns an initialized pricelist list object.
commerce_pricelist_list_save We save the entity by calling the controller.
commerce_pricelist_list_title Callback for a page title when this entity is displayed.
commerce_pricelist_list_uri Implements the uri callback.
commerce_pricelist_menu Implements hook_menu().
commerce_pricelist_permission Implements hook_permission().
commerce_pricelist_theme Implements hook_theme().
_commerce_pricelist_display_date Format a date for display, return empty string for default date values.

Constants

Namesort descending Description
COMMERCE_PRICELIST_UNIX_TIME_APOCALYPSE @file Implements the basic functionality required for price lists

Classes

Namesort descending Description
CommercePricelistItem Pricelist item class.
CommercePricelistList Pricelist list class.