You are here

commerce_stock_ui.module in Commerce Stock 7.2

Commerce stock UI.

File

commerce_stock_ui.module
View source
<?php

/**
 * @file
 * Commerce stock UI.
 */

/**
 * Implements hook_menu().
 */
function commerce_stock_ui_menu() {
  $items = array();

  // The stock events page.
  $items['admin/commerce/config/stock/validation'] = array(
    'title' => 'Stock validation',
    'description' => 'Manage and configure stock validation events.',
    'page callback' => 'commerce_stock_admin_event_rules',
    'access arguments' => array(
      'administer rules',
    ),
    'weight' => 5,
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/commerce_stock.admin.inc',
  );

  // Add the menu items for the various Rules forms.
  $controller = new RulesUIController();
  $items += $controller
    ->config_menu('admin/commerce/config/stock/validation');

  // Add rule validation events.
  $items['admin/commerce/config/stock/validation/add_cart_state'] = array(
    'title' => 'Add a Cart state rule',
    'description' => 'Disable or modify the add to cart form.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_stock_ui_add_cart_state_rule_form',
      'admin/commerce/config/stock/validation',
    ),
    'access arguments' => array(
      'administer rules',
    ),
    'file path' => drupal_get_path('module', 'rules_admin'),
    'file' => 'rules_admin.inc',
  );
  $items['admin/commerce/config/stock/validation/add_cart_action'] = array(
    'title' => 'Add a Cart action rule',
    'description' => 'Act on the add to cart action.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_stock_ui_add_cart_action_rule_form',
      'admin/commerce/config/stock/validation',
    ),
    'access arguments' => array(
      'administer rules',
    ),
    'file path' => drupal_get_path('module', 'rules_admin'),
    'file' => 'rules_admin.inc',
  );
  $items['admin/commerce/config/stock/validation/add_checkout'] = array(
    'title' => 'Add a Validate cart / checkout rule',
    'description' => 'Validate products in the the cart before checkout.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_stock_ui_add_checkout_rule_form',
      'admin/commerce/config/stock/validation',
    ),
    'access arguments' => array(
      'administer rules',
    ),
    'file path' => drupal_get_path('module', 'rules_admin'),
    'file' => 'rules_admin.inc',
  );

  // The stock control rules page.
  $items['admin/commerce/config/stock/control'] = array(
    'title' => 'Stock control',
    'description' => 'Manage and configure stock control rules.',
    'page callback' => 'commerce_stock_admin_stock_control_rules',
    'access arguments' => array(
      'administer rules',
    ),
    'weight' => 6,
    'type' => MENU_LOCAL_TASK,
    'file' => 'includes/commerce_stock.admin.inc',
  );

  // Add the menu items for the various Rules forms.
  $controller = new RulesUIController();
  $items += $controller
    ->config_menu('admin/commerce/config/stock/control');

  // Add stock control rule.
  $items['admin/commerce/config/stock/control/add'] = array(
    'title' => 'Create a stock control rule',
    'description' => 'Add a new stock control rule',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'commerce_stock_ui_stock_control_rule_form',
      'admin/commerce/config/stock/control',
    ),
    'access arguments' => array(
      'administer rules',
    ),
    'file path' => drupal_get_path('module', 'rules_admin'),
    'file' => 'rules_admin.inc',
  );
  return $items;
}

/**
 * Implements hook_menu_local_tasks_alter().
 */
function commerce_stock_ui_menu_local_tasks_alter(&$data, $router_item, $root_path) {

  // Add action links on 'admin/commerce/config/stock/validation'.
  if ($root_path == 'admin/commerce/config/stock/validation') {
    $item = menu_get_item('admin/commerce/config/stock/validation/add_cart_state');
    if ($item['access']) {
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
    $item = menu_get_item('admin/commerce/config/stock/validation/add_cart_action');
    if ($item['access']) {
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
    $item = menu_get_item('admin/commerce/config/stock/validation/add_checkout');
    if ($item['access']) {
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
  elseif ($root_path == 'admin/commerce/config/stock/control') {
    $item = menu_get_item('admin/commerce/config/stock/control/add');
    if ($item['access']) {
      $data['actions']['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $item,
      );
    }
  }
}

/**
 * Implements hook_forms().
 *
 * The stock rule forms that that will be modified with the correct event value.
 */
function commerce_stock_ui_forms($form_id, $args) {
  $forms = array();
  $forms['commerce_stock_ui_add_cart_state_rule_form'] = array(
    'callback' => 'rules_admin_add_reaction_rule',
  );
  $forms['commerce_stock_ui_add_cart_action_rule_form'] = array(
    'callback' => 'rules_admin_add_reaction_rule',
  );
  $forms['commerce_stock_ui_add_checkout_rule_form'] = array(
    'callback' => 'rules_admin_add_reaction_rule',
  );
  $forms['commerce_stock_ui_stock_control_rule_form'] = array(
    'callback' => 'rules_admin_add_reaction_rule',
  );
  return $forms;
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_stock_ui_form_commerce_stock_ui_add_cart_state_rule_form_alter(&$form, &$form_state) {
  unset($form['settings']['help']);
  $form['settings']['event']['#type'] = 'value';
  $form['settings']['event']['#value'] = 'commerce_stock_check_add_to_cart_form_state';
  $form['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/config/stock/validation');
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_stock_ui_form_commerce_stock_ui_add_cart_action_rule_form_alter(&$form, &$form_state) {
  unset($form['settings']['help']);
  $form['settings']['event']['#type'] = 'value';
  $form['settings']['event']['#value'] = 'commerce_stock_add_to_cart_check_product';
  $form['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/config/stock/validation');
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_stock_ui_form_commerce_stock_ui_add_checkout_rule_form_alter(&$form, &$form_state) {
  unset($form['settings']['help']);
  $form['settings']['event']['#type'] = 'value';
  $form['settings']['event']['#value'] = 'commerce_stock_check_product_checkout';
  $form['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/config/stock/validation');
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function commerce_stock_ui_form_commerce_stock_ui_stock_control_rule_form_alter(&$form, &$form_state) {
  $form['settings']['tags']['#value'] = 'stock_control';
  $form['submit']['#suffix'] = l(t('Cancel'), 'admin/commerce/config/stock/control');
}