You are here

commerce_pricelist_role.module in Commerce Pricelist 7

Summary

Description

File

commerce_pricelist_role/commerce_pricelist_role.module
View source
<?php

/**
* @file
* Summary
*
* Description
*/
function commerce_pricelist_role_commerce_pricelists_alter(&$pricelists, $user) {
  commerce_pricelist_filter_pricelists($pricelists, array(
    'role' => array_keys($user->roles),
  ));
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function commerce_pricelist_role_form_commerce_pricelist_list_form_alter(&$form, $form_state) {
  $entity = $form['entity']['#value'];
  $form['#validate'][] = 'commerce_pricelist_role_form_validate';
  $options = user_roles();
  $default = commerce_pricelist_list_filter_get($entity, 'role');
  $form['commerce_pricelist_role'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Which roles should the price list apply to'),
    '#options' => $options,
    '#default_value' => $default,
  );
}

/**
 * @param $form
 * @param $form_state
 */
function commerce_pricelist_role_form_validate($form, &$form_state) {
  if ($form_state['triggering_element']['#id'] !== 'edit-delete') {
    $entity =& $form_state['values']['entity'];
    commerce_pricelist_list_filter_set($entity, 'role', $form_state['values']['commerce_pricelist_role']);
  }
}

/**
 * Add info to pricelist overview
 *
 * @param $info
 * @param $entity
 */
function commerce_pricelist_role_commerce_pricelists_list_info_alter(&$info, $entity) {
  $roles = 0;
  $filter = commerce_pricelist_list_filter_get($entity, 'role');
  if ($filter) {
    $roles = count($filter);
    $values = array_count_values($filter);
    $roles -= isset($values['0']) ? $values['0'] : 0;
  }
  $info['role'] = $roles . ' ' . t('roles') . '<br>';
}

/**
 * Implements hook_user_update().
 *
 * @param $edit
 * @param $account
 * @param $category
 */
function commerce_pricelist_role_user_update(&$edit, $account, $category) {

  // Clear pricelist cache for this user
  $cache_id = 'commerce_pricelist_get_user_pricelists_' . $account->uid;
  if (cache_get($cache_id)) {
    cache_clear_all($cache_id, 'cache');
  }
}