You are here

commerce_option.module in Commerce Product Option 7

Same filename and directory in other branches
  1. 7.2 commerce_option.module

File

commerce_option.module
View source
<?php

/**
 * Implements hook_entity_info()
 *
 * This function provides informations about the option entities.
 */
function commerce_option_entity_info() {
  $return = array();
  $return['commerce_option'] = array(
    'label' => t('Commerce Option'),
    'entity class' => 'CommerceOption',
    'controller class' => 'EntityAPIController',
    'base table' => 'commerce_option',
    'fieldable' => TRUE,
    'entity keys' => array(
      'id' => 'option_id',
      'bundle' => 'set_id',
      'label' => 'set_id',
    ),
    'access callback' => 'commerce_option_set_access',
    'module' => 'commerce_option',
    'bundle keys' => array(
      'bundle' => 'set_id',
    ),
    'bundles' => array(),
    'view modes' => array(
      'attribute_view' => array(
        'label' => t('Attribute View'),
        'custom settings' => FALSE,
      ),
    ),
  );
  $return['commerce_option_set'] = array(
    'label' => t('Commerce Option Set'),
    'entity class' => 'CommerceOptionSet',
    'controller class' => 'EntityAPIControllerExportable',
    'base table' => 'commerce_option_set',
    'fieldable' => FALSE,
    // TODO: Make the option set exportable
    'exportable' => FALSE,
    'entity keys' => array(
      'id' => 'set_id',
      'label' => 'name',
      'name' => 'set_id',
    ),
    'bundle of' => 'commerce_option',
    'access callback' => 'commerce_option_set_access',
    'module' => 'commerce_option',
    'bundles' => array(),
    'admin ui' => array(
      'path' => 'admin/commerce/products/option-sets',
      'file' => 'includes/commerce_option_set.forms.inc',
    ),
  );
  return $return;
}

/**
 * Implements hook_entity_info_alter().
 *
 * Use this hook to specify commerce set bundles to avoid a recursion, as loading
 * the commerce coupon types needs the entity info too.
 */
function commerce_option_entity_info_alter(&$entity_info) {
  foreach (commerce_option_get_sets() as $type => $info) {
    $entity_info['commerce_option']['bundles'][$type] = array(
      'label' => $info->name,
      'admin' => array(
        'path' => 'admin/commerce/products/option-sets/manage/%commerce_option_set',
        'real path' => 'admin/commerce/products/option-sets/manage/' . $type,
        'bundle argument' => 5,
        'access arguments' => array(
          'administer option sets',
        ),
      ),
    );
  }
}

/**
 * Implemenation of hook_menu_alter
 */
function commerce_option_menu_alter(&$items) {
  $items['admin/commerce/products/option-sets']['type'] = MENU_LOCAL_TASK;
  $items['admin/commerce/products/option-sets']['title'] = t('Option sets');
}

/**
 * Returns an initialized product object.
 *
 * @param $set_id
 *   The machine-readable set id of the option.
 *
 * @return
 *   A product object with all default fields initialized.
 */
function commerce_option_new($set_id = '') {
  return entity_get_controller('commerce_product')
    ->create($set_id);
}

/**
 * Creation callback for the Entity module.
 */
function _commerce_option_create($values = array()) {

  // Create a new product of the specified type.
  $option = commerce_product_new($values['set_id']);
  unset($values['set_id']);
  $wrapper = entity_metadata_wrapper('commerce_option', $option);
  foreach ($values as $name => $value) {
    $wrapper->{$name}
      ->set($value);
  }
  return $wrapper
    ->value();
}

/**
 * Check if the given operation is allowed.
 */
function commerce_option_set_access($op, $type = NULL, $account = NULL) {
  return user_access('administer option sets', $account);
}

/**
 * Load multiple test entities based on certain conditions.
 *
 * @param $pids
 *   An array of entity IDs.
 * @param $conditions
 *   An array of conditions to match against the {entity} table.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   An array of test entity objects, indexed by pid.
 */
function commerce_option_load_multiple($pids = array(), $conditions = array(), $reset = FALSE) {
  return entity_load('commerce_option', $pids, $conditions, $reset);
}

/**
 * Fetch a option object.
 *
 * @param $option_id
 *   Integer specifying the option id.
 * @param $reset
 *   A boolean indicating that the internal cache should be reset.
 * @return
 *   A fully-loaded $option object or FALSE if it cannot be loaded.
 *
 * @see commerce_option_load_multiple()
 */
function commerce_option_load($option_id, $reset = FALSE) {
  $options = commerce_option_load_multiple(array(
    $option_id,
  ), array(), $reset);
  return reset($options);
}

/**
 * Load all options associated with the line item.
 *
 * @param $line_item_id
 *  Line item id.
 * @return Array of options
 */
function commerce_option_load_by_line_item($line_item_id) {
  $options = db_select('commerce_option', 'o')
    ->fields('o', array(
    'option_id',
  ))
    ->condition('line_item_id', $line_item_id, '=')
    ->execute();
  $option_ids = array();
  foreach ($options as $option) {
    $option_ids[] = $option->option_id;
  }
  return commerce_option_load_multiple($option_ids);
}

/**
 * Save a given option.
 */
function commerce_option_save($option) {
  return entity_get_controller('commerce_option')
    ->save($option);
}

/**
 * Implementation of hook_permission
 */
function commerce_option_permission() {
  $permissions = array(
    'administer option sets' => array(
      'title' => t('Administer Option Sets'),
      'description' => t('Allows users to manage option sets.'),
    ),
  );
  return $permissions;
}

/**
 * Implementation of hook_entity_delete
 *
 * Delete all options, when the associated line item is deleted.
 */
function commerce_option_entity_delete($entity, $type) {
  if ($type == 'commerce_line_item') {
    db_delete('commerce_option')
      ->condition('line_item_id', $entity->line_item_id)
      ->execute();
  }
}
function commerce_option_get_sets($set_id = NULL) {
  $sets = entity_load('commerce_option_set', isset($set_id) ? array(
    $set_id,
  ) : FALSE, array(), TRUE);
  return isset($set_id) ? reset($sets) : $sets;
}

/**
 * Implements hook_commerce_option_set_info()
 */
function commerce_option_commerce_option_set_info() {
  return db_query('SELECT * FROM {commerce_option_set}')
    ->fetchAllAssoc('set_id', PDO::FETCH_ASSOC);
}

/**
 * Creates a new option set array. 
 */
function commerce_option_set_new($values = array()) {
  return new CommerceOptionSet($values);
}

/**
 * Implements hook_views_api().
 */
function commerce_option_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'commerce_option') . '/includes/views',
  );
}

/**
 * Saves a option set.
 *
 * This function will either insert a new option set if $option_set['is_new']
 * is set or attempt to update an existing option set if it is not. It does
 * not currently support changing the machine-readable name of the option set,
 * nor is this possible through the form supplied by the Option module.
 *
 * @param $option_set
 *   The option set array containing the basic properties as initialized in
 *     commerce_option_set_new().
 *
 * @return
 *   The return value of the call to drupal_write_record() to save the option
 *     set, either FALSE on failure or SAVED_NEW or SAVED_UPDATED indicating
 *     the type of query performed to save the option set.
 */
function commerce_option_set_save(CommerceOptionSet $set) {

  // Store the entity, must be done before they are configured
  $set
    ->save();

  // Ensure the creation of the rules (components)
  entity_defaults_rebuild();
  menu_rebuild();
  return $set;
}
function commerce_option_set_delete($set_id) {
  $option_set = commerce_option_set_load($set_id);
  db_delete('commerce_option_set')
    ->condition('set_id', $set_id)
    ->execute();

  // Rebuild the menu to get rid of this option set's edit menu item.
  commerce_option_set_reset();
  menu_rebuild();

  // Notify the field API that this bundle has been destroyed.
  field_attach_delete_bundle('commerce_option', $set_id);

  // Notify other modules that this option set has been deleted.
  module_invoke_all('commerce_option_set_delete', $option_set);
}

/**
 * Loads a option set
 *
 * @param $set_id
 *   The machine-readable name of the option set; accepts normal machine names
 *     and URL prepared machine names with underscores replaced by hyphens.
 */
function commerce_option_set_load($set_id) {
  return commerce_option_get_sets($set_id);
}

/**
 * Checks to see if a given option set exists
 *
 * @param $set_id
 *   The machine-readable name of the set.
 *
 * @return
 *   TRUE or FALSE indicating whether or not the option set exists.
 */
function commerce_option_set_exists($set_id) {

  // Look for a match of the set.
  if ($match_id = db_query('SELECT set_id FROM {commerce_option_set} WHERE set_id = :set', array(
    ':set' => $set_id,
  ))
    ->fetchField()) {
    return FALSE;
  }
  return TRUE;
}

/**
 * Form callback wrapper: create or edit a option set.
 *
 * @param $set_id
 *   The machine-name of the option set being created or edited by this form
 *     or a full option set array.
 *
 * @see commerce_option_set_form()
 */
function commerce_option_set_form_wrapper($set_id) {
  if (is_array($set_id)) {
    $option_set = $set_id;
  }
  else {
    $option_set = commerce_option_set_load($set_id);
  }

  // Add the breadcrumb for the form's location.
  commerce_option_breadcrumb(TRUE);

  // Return a message if the option set is not governed by the commerce option module.
  if (!empty($option_set['set_id']) && $option_set['module'] != 'commerce_option') {
    return t('This option set cannot be edited, because it is not defined by the Option module.');
  }

  // Include the forms file from the Product module.
  module_load_include('inc', 'commerce_option', 'includes/commerce_option_set.forms');
  return drupal_get_form('commerce_option_set_form', $option_set);
}

/**
 * Sets the breadcrumb for administrative option pages.
 *
 * @param $option_sets
 *   TRUE or FALSE indicating whether or not the breadcrumb should include the
 *     option sets administrative page.
 */
function commerce_option_breadcrumb($option_sets = FALSE) {
  $breadcrumb = array(
    l(t('Home'), '<front>'),
    l(t('Administration'), 'admin'),
    l(t('Store'), 'admin/commerce'),
    l(t('Configuration'), 'admin/commerce/config'),
  );
  if ($option_sets) {
    $breadcrumb[] = l(t('Option Sets'), 'admin/commerce/config/option-sets');
  }
  drupal_set_breadcrumb($breadcrumb);
}

/**
 * Form callback wrapper: confirmation form for deleting a option set.
 *
 * @param $set_id
 *   The machine-name of the option set being created or edited by this form
 *     or a full option set array.
 *
 * @see commerce_option_set_delete_form()
 */
function commerce_option_set_delete_form_wrapper($set_id) {
  if (is_array($set_id)) {
    $option_set = $set_id;
  }
  else {
    $option_set = commerce_option_set_load($set_id);
  }

  // Add the breadcrumb for the form's location.
  commerce_option_breadcrumb(TRUE);

  // Return a message if the option set is not governed by Product UI.
  if ($option_set['module'] != 'commerce_option') {
    return t('This option set cannot be deleted, because it is not defined by the Option module.');
  }

  // Don't allow deletion of option set that have options defined.
  if (($count = db_query("SELECT option_id FROM {commerce_option} WHERE set = :set", array(
    ':set' => $option_set['set_id'],
  ))
    ->rowCount()) > 0) {
    drupal_set_title(t('Cannot delete the %name option set', array(
      '%name' => $option_set['name'],
    )), PASS_THROUGH);
    return format_plural($count, 'There is 1 option of this option set. It cannot be deleted.', 'There are @count options of this option set. It cannot be deleted.');
  }
  module_load_include('inc', 'commerce_option', 'includes/commerce_option_set.forms');
  return drupal_get_form('commerce_option_set_delete_form', $option_set);
}

/**
 * Implements hook_attribute_field()
 */
function commerce_option_attribute_field(&$element, &$line_item) {
  $element['options'] = array();
  $options = commerce_option_load_by_line_item($line_item->line_item_id);
  foreach ($options as $option) {
    field_attach_prepare_view('commerce_option', array(
      $option->option_id => $option,
    ), 'attribute_view');
    $option_view = field_attach_view('commerce_option', $option, 'attribute_view');
    $element['options'][] = array(
      '#markup' => drupal_render($option_view),
    );
  }

  /*if (count($sub_items) > 0) {

      $element['#attached']['css'][] = drupal_get_path('module', 'commerce_product_bundle') . '/theme/commerce_product_bundle_cart.css';

      foreach ($sub_items as $item) {


        $item_wrapper = entity_metadata_wrapper('commerce_line_item', $item);


        $element['bundles'][] = array(
          '#markup' => theme('commerce_product_bundle_attribute', array('sub_line_item' => $item, 'product_attribute_view' => drupal_render($product_attribute_view))),
        );
      }
    }*/
}

Functions

Namesort descending Description
commerce_option_attribute_field Implements hook_attribute_field()
commerce_option_breadcrumb Sets the breadcrumb for administrative option pages.
commerce_option_commerce_option_set_info Implements hook_commerce_option_set_info()
commerce_option_entity_delete Implementation of hook_entity_delete
commerce_option_entity_info Implements hook_entity_info()
commerce_option_entity_info_alter Implements hook_entity_info_alter().
commerce_option_get_sets
commerce_option_load Fetch a option object.
commerce_option_load_by_line_item Load all options associated with the line item.
commerce_option_load_multiple Load multiple test entities based on certain conditions.
commerce_option_menu_alter Implemenation of hook_menu_alter
commerce_option_new Returns an initialized product object.
commerce_option_permission Implementation of hook_permission
commerce_option_save Save a given option.
commerce_option_set_access Check if the given operation is allowed.
commerce_option_set_delete
commerce_option_set_delete_form_wrapper Form callback wrapper: confirmation form for deleting a option set.
commerce_option_set_exists Checks to see if a given option set exists
commerce_option_set_form_wrapper Form callback wrapper: create or edit a option set.
commerce_option_set_load Loads a option set
commerce_option_set_new Creates a new option set array.
commerce_option_set_save Saves a option set.
commerce_option_views_api Implements hook_views_api().
_commerce_option_create Creation callback for the Entity module.