You are here

merci_restrictions.module in MERCI (Manage Equipment Reservations, Checkout and Inventory) 7.3

File

merci_restrictions/merci_restrictions.module
View source
<?php

define('MERCI_RESTRICTIONS', 'merci_restrictions');
define('MERCI_OVERRIDE_RESTRICTIONS', 'merci_override_restrictions');

//define('MERCI_ALLOWED_ROLES', 'field_allowed_roles');
define('MERCI_CHECKOUT_MAX_LENGTH', 'field_max_length_of_checkout');
define('MERCI_CHECKOUT_OFFSITE', 'field_checkout_offsite');
define('MERCI_DEFAULT_MAX_LENGTH', '');
define('MERCI_DEFAULT_ALLOW_OVERNIGHT', TRUE);

//module_load_include('inc', 'merci_restrictions', 'merci_restrictions.element');
define('MERCI_RESTRICTIONS_TYPE_PATH', MERCI_ADMIN_CONFIG_PATH . '/merci-restrictions');
define('MERCI_RESTRICTIONS_PATH', MERCI_RESTRICTIONS_TYPE_PATH);
function merci_restrictions_field_widget_form_alter(&$element, &$form_state, $context) {

  // Add a css class to widget form elements for all fields of type mytype.
  if ($context['field']['field_name'] == MERCI_OVERRIDE_RESTRICTIONS) {
    $line_item = $element['target_id']['#entity'];
    if (property_exists($line_item, MERCI_OVERRIDE_RESTRICTIONS) and !empty($line_item->{MERCI_OVERRIDE_RESTRICTIONS}['und'])) {
      return;
    }

    // Be sure not to overwrite existing attributes.
    $element['#attributes']['class'][] = 'myclass';
    $element['override_password'] = array(
      '#type' => 'password',
      '#title' => t('Password'),
    );
    $element['#type'] = 'fieldset';
    $element['#title'] = 'Override restrictions';
    $element['#element_validate'][] = 'merci_restrictions_override_validate';
  }
}
function merci_restrictions_override_validate($element, &$form_state, $form) {
  if (!empty($element['target_id']['#value'])) {
    $line_item = $element['target_id']['#entity'];
    if ($line_item->is_new) {
      if (preg_match("/.+\\((\\d+)\\)/", $element['target_id']['#value'], $matches)) {
        $value = $matches[1];
        $account = user_load($value);
        $name = $account->name;
      }
      else {
        $name = $element['target_id']['#value'];
      }
      $password = $element['override_password']['#value'];
      if (!user_authenticate($name, $password)) {
        form_error($element, t('Password for override operator incorrect.'));
      }
    }
  }
}

/**
 * Implements hook_permission().
 */
function merci_restrictions_permission() {
  $perms = array();
  $perms['administer all merci restrictions'] = array(
    'title' => t('Administer all merci restrictions'),
    'description' => t('Perform all administration tasks for merci restrictions.'),
  );
  return $perms;
}

/**
 * Redirects a line item type URL to its fields management page.
 */
function merci_restrictions_redirect($type = NULL) {
  drupal_goto(MERCI_RESTRICTIONS_TYPE_PATH . '/fields');
}

/**
 * Implements hook_menu().
 */
function merci_restrictions_menu() {
  $items = array();
  $items[MERCI_RESTRICTIONS_TYPE_PATH] = array(
    'title' => 'Merci Restrictions',
    'description' => 'Manage fields for merci restrictions',
    'page callback' => 'merci_restrictions_redirect',
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer MERCI',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  $items = array();
  $items['admin/merci/config/merci-restrictions/settings'] = array(
    'title' => 'Sitewide merci restrictions',
    'description' => "Site's default merci restrictions.",
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'merci_restrictions_sitewide_settings',
    ),
    'access arguments' => array(
      'administer all merci restrictions',
    ),
    'access callback' => 'user_access',
    'file' => 'merci_restrictions.admin.inc',
    'type' => MENU_LOCAL_TASK,
    'weight' => 5,
  );

  /*
    $items['admin/config/merci/merci-restrictions/settings'] = array(
   'title' => 'Sitewide merci restrictions',
   'description' => "Site's default merci restrictions.",
   'page callback' => 'drupal_get_form',
   'page arguments' => array('merci_restrictions_sitewide_settings'),
   'access arguments' => array('administer MERCI'),
   'access callback' => 'user_access',
   'file' => 'merci_restrictions.admin.inc',
   'type' => MENU_LOCAL_TASK,
   'weight' => 5,
    );
  */
  return $items;
}
function merci_restrictions_page() {
  return "Default page.";
}

/**
 * Implements hook_entity_info().
 */
function merci_restrictions_entity_info() {
  $info = array();
  $info['merci_restrictions'] = array(
    'label' => t('Merci restrictions'),
    'plural label' => t('Merci restrictions'),
    'module' => 'merci_restrictions',
    'base table' => 'merci_restrictions',
    'fieldable' => TRUE,
    'entity keys' => array(
      'id' => 'id',
      'bundle' => 'type',
      'label' => 'title',
    ),
    'bundle keys' => array(
      'bundle' => 'type',
    ),
    'bundles' => array(
      'merci_restrictions' => array(
        'label' => t('Merci restrictions'),
        // Provide a default administration path for Field UI, but not if 'admin'
        // has been explicitly set to NULL.
        'admin' => array(
          'path' => MERCI_RESTRICTIONS_TYPE_PATH,
        ),
      ),
    ),
    'admin ui' => array(
      'path' => 'merci_restrictions',
      'file' => 'merci_restrictions.pages.inc',
      'controller class' => 'MerciRestrictionsUIController',
    ),
    //'entity class' => 'MerciHours',
    'entity class' => 'MerciRestrictions',
    'controller class' => 'EntityAPIController',
    'exportable' => TRUE,
    'access callback' => 'merci_restrictions_access',
    'inline entity form' => array(
      'controller' => 'MerciRestrictionInlineEntityFormController',
    ),
  );
  return $info;
}

/**
 * * List of task Types.
 * */
function merci_restrictions_add_page($entity_type) {
  $merci_restrictions_types = merci_restrictions_types();

  // Bypass the node/add listing if only one content type is available.
  if (count($merci_restrictions_types) == 1) {

    // Get entity info for our bundles.
    $info = entity_get_info($entity_type);
    $type = reset($merci_restrictions_types);
    drupal_goto($info['admin ui']['path'] . '/add/' . $type->type);
  }
  return entity_ui_bundle_add_page($entity_type);
}
function merci_restrictions_get_action_title($op, $entity_type, $bundle_name = NULL) {
  $info = entity_get_info($entity_type);
  $merci_restrictions_types = merci_restrictions_types();

  // Bypass the node/add listing if only one content type is available.
  if (count($merci_restrictions_types) == 1) {

    // Get entity info for our bundles.
    $info = entity_get_info($entity_type);
    $type = reset($merci_restrictions_types);
    $bundle_name = isset($bundle_name) ? $bundle_name : $type->type;
    switch ($op) {
      case 'add':
        if (isset($bundle_name) && $bundle_name != $entity_type) {
          return t('Add @bundle_name', array(
            '@bundle_name' => drupal_strtolower($info['bundles'][$bundle_name]['label']),
          ));
        }
        else {
          return t('Add @entity_type', array(
            '@entity_type' => drupal_strtolower($info['label']),
          ));
        }
      case 'import':
        return t('Import @entity_type', array(
          '@entity_type' => drupal_strtolower($info['label']),
        ));
    }
  }
  return entity_ui_get_action_title($op, $entity_type, $bundle_name);
}

/**
 * Implements access callback.
 */
function merci_restrictions_access($op, $entity = NULL, $account = NULL) {

  /*
    switch ($op) {
   case 'view':
   //case 'update':
   //case 'create':
   //case 'edit':
   case 'delete':
     $default = variable_get('merci_restrictions_default', NULL);
     if ($entity->name == $default) {
       drupal_set_message(t("This is the default merci restrictions and can't be deleted. Please change the default merci restrictions at <a href='!url'>admin/config/merci/merci-restrictions/settings</a> if you want to delete this one.", array('!url' => url('admin/config/merci/merci-restrictions/settings'))), 'error');
         return false;
     }
     break;
    }
  */
  if ($op == 'view') {
    return TRUE;
  }
  return user_access('administer all merci restrictions');
}

/**
 * Returns all merci restrictions indexed by name, or just the one requested.
 *
 * Used by machine name existence check
 * also useful for menu path wildcard loader.
 */
function merci_restrictions_load($name = NULL) {
  $wc = entity_load_multiple_by_name('merci_restrictions', isset($name) ? array(
    $name,
  ) : FALSE);
  return isset($name) ? reset($wc) : $wc;
}

/**
 * Returns a merci restrictions selection form element.
 *
 * @param boolean include_default wether to add "use site default" as an option.
 * @param integer default_value Id of the default merci restrictions.
 *
 * @return form element.
 */
function merci_restrictions_form_element($include_default = FALSE, $default_value = NULL) {
  $options = array();
  if ($include_default && variable_get('merci_restrictions_default', FALSE)) {
    $options[0] = t('-- use site default --');
  }
  $options += db_select('merci_restrictions', 'wc')
    ->fields('wc', array(
    'name',
    'label',
  ))
    ->execute()
    ->fetchAllKeyed(0, 1);
  $element = array(
    '#type' => 'select',
    '#title' => t('Choose merci restrictions'),
    '#default_value' => $default_value,
    '#options' => $options,
  );
  return $element;
}

/**
 * Implements hook_merci_fields_info().
 */
function merci_restrictions_merci_fields_info() {
  $fields = array();
  module_load_include('inc', 'merci_restrictions', 'merci_restrictions_fields');
  $field_bases = merci_restrictions_fields();
  $instances = merci_restrictions_instances();
  foreach ($field_bases as $field_name => $field) {
    $fields[$field_name] = array(
      'field' => $field,
      'instance' => $instances[$field_name],
    );
  }
  return $fields;
}