You are here

merci_rro.module in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6

Hooks and utility functions for MERCI Role Rule Override

File

modules/merci_rro/merci_rro.module
View source
<?php

/**
 * @file
 * Hooks and utility functions for MERCI Role Rule Override
 */

/**
 * Implementation of hook_menu().
 */
function merci_rro_menu() {
  $items = array();
  foreach (array_keys(node_get_types()) as $type_name) {
    $type_url_str = str_replace('_', '-', $type_name);
    $items['admin/content/node-type/' . $type_url_str . '/merci_rro'] = array(
      'title' => 'MERCI Role Rule Overrides',
      'page callback' => 'merci_rro_admin_page',
      'page arguments' => array(
        $type_name,
      ),
      'access callback' => 'merci_rro_node_type_menu_access',
      'access arguments' => array(
        $type_name,
      ),
      'type' => MENU_LOCAL_TASK,
      'weight' => 10,
      'file' => 'merci_rro.admin.inc',
    );
  }

  // foreach
  return $items;
}

// merci_rro_menu
function merci_rro_node_type_menu_access($type_name) {
  $merci_settings = merci_load_content_type_settings($type_name);
  if ($merci_settings->type_setting != 'disabled' && user_access('administer MERCI')) {
    return TRUE;
  }

  // if
  return FALSE;
}

// merci_rro_node_type_menu_access

/**
 * Returns list of overrides associated with a given content type
 */
function merci_rro_content_type_overrides($content_type, $cache = TRUE) {
  global $user;
  static $overrides = array();
  if (!isset($overrides[$content_type]) || !$cache) {
    $overrides[$content_type] = array();
    $result = db_query("SELECT * FROM {merci_rro} WHERE type = '%s'", $content_type);
    while ($override = db_fetch_object($result)) {
      if ($user->roles[$override->rid] != '') {
        $overrides[$content_type][] = $override;
      }

      // if
    }

    // while
  }

  // if
  return $overrides[$content_type];
}

// merci_rro_content_type_overrides

/**
 * Implementation of hook_theme().
 */
function merci_rro_theme($existing, $type, $theme, $path) {
  return array(
    'merci_rro_override' => array(
      'arguments' => array(
        'override' => NULL,
      ),
    ),
  );
}

// merci_rro_theme

/**
 * Theme an override object
 */
function theme_merci_rro_override($override) {
  $role = merci_rro_role_name_from_rid($override->rid);
  $html = '<div class="merci-rro-override">
    <h3 class="fn">' . check_plain($role) . '</h3>
    <ul>
      <li><b>' . t('Maximum hours per reservation:') . '</b> <span class="value">' . $override->max_hours_per_reservation . '</span></li>
      <li><b>' . t('Allow overnight reservation:') . '</b> <span class="value">' . ($override->allow_overnight == 1 ? 'Yes' : 'No') . '</span></li>
      <li><b>' . t('Allow weekend reservation:') . '</b> <span class="value">' . ($override->allow_weekends == 1 ? 'Yes' : 'No') . '</span></li>
      <li><b>' . t('Rate per hour:') . '</b> <span class="value">' . $override->rate_per_hour . '</span></li>
      <li><b>' . t('Late fee per hour:') . '</b> <span class="value">' . $override->late_fee_per_hour . '</span></li>
      <li><b>' . t('Fee free hours:') . '</b> <span class="value">' . $override->fee_free_hours . '</span></li>
      <li><b>' . t('Minimum hours for cancelation without No Show:') . '</b> <span class="value">' . $override->min_cancel_hours . '</span></li>
      <li><b>' . t('Monday Hours:') . '</b> <span class="value">' . $override->hours_mon . '</span></li>
      <li><b>' . t('Tuesday Hours:') . '</b> <span class="value">' . $override->hours_tue . '</span></li>
      <li><b>' . t('Wednesday Hours:') . '</b> <span class="value">' . $override->hours_wed . '</span></li>
      <li><b>' . t('Thursday Hours:') . '</b> <span class="value">' . $override->hours_thu . '</span></li>
      <li><b>' . t('Friday Hours:') . '</b> <span class="value">' . $override->hours_fri . '</span></li>
      <li><b>' . t('Saturday Hours:') . '</b> <span class="value">' . $override->hours_sat . '</span></li>
      <li><b>' . t('Sunday Hours:') . '</b> <span class="value">' . $override->hours_sun . '</span></li>
    </ul>';
  if (user_access('administer MERCI')) {
    $html .= '<p><a href="' . base_path() . 'admin/content/node-type/' . $override->type . '/merci_rro?delete=' . $override->rid . '">Remove</a></p>';
  }

  // if
  $html .= '</div>';
  return $html;
}

// theme_merci_rro_override
function merci_rro_role_name_from_rid($rid, $cache = TRUE) {
  static $roles;
  if (!isset($roles) || !$cache) {
    $roles = user_roles();
  }

  // if
  return $roles[$rid];
}

// merci_rro_role_name_from_rid

/**
 * Implementation of hook_merci_rules_alter().
 */
function merci_rro_merci_rules_alter(&$rules) {
  $overrides = merci_rro_content_type_overrides($rules->type);
  $rule_types = array(
    'max_hours_per_reservation',
    'allow_overnight',
    'allow_weekends',
    'rate_per_hour',
    'late_fee_per_hour',
    'fee_free_hours',
    'min_cancel_hours',
    'hours_mon',
    'hours_tue',
    'hours_wed',
    'hours_thu',
    'hours_fri',
    'hours_sat',
    'hours_sun',
  );

  // Get best value for every rule type in every override
  foreach ($overrides as $override) {
    foreach ($rule_types as $rule_type) {
      $rules->{$rule_type} = merci_rro_determine_best_value($rule_type, $rules->{$rule_type}, $override->{$rule_type});
    }

    // foreach
  }

  // foreach
}

// merci_rro_merci_rules_alter
function merci_rro_determine_best_value($rule_type, $a, $b) {
  switch ($rule_type) {

    // Max value is best
    case 'allow_overnight':
    case 'allow_weekends':
    case 'fee_free_hours':
      return $a > $b ? $a : $b;

    // Minimum value is best
    case 'min_cancel_hours':
    case 'rate_per_hour':
    case 'late_fee_per_hour':
      return $a < $b ? $a : $b;

    // Longest hours is best
    case 'hours_mon':
    case 'hours_tue':
    case 'hours_wed':
    case 'hours_thu':
    case 'hours_fri':
    case 'hours_sat':
    case 'hours_sun':
      $a_parts = merci_hours_str_to_array($a);
      if (!$a_parts) {
        return $b;
      }
      $b_parts = merci_hours_str_to_array($b);
      if ($a_parts['open'] > $b_parts['open']) {
        return $b;
      }
      if ($a_parts['close'] < $b_parts['close']) {
        return $b;
      }
      return $a;

    // Other
    case 'max_hours_per_reservation':
      if ($a == 0) {
        return $a;
      }
      if ($b == 0) {
        return $b;
      }
      return $b > $a ? $b : $a;
    default:
      return $a;
  }

  // switch
}

// merci_rro_determine_best_value

Functions

Namesort descending Description
merci_rro_content_type_overrides Returns list of overrides associated with a given content type
merci_rro_determine_best_value
merci_rro_menu Implementation of hook_menu().
merci_rro_merci_rules_alter Implementation of hook_merci_rules_alter().
merci_rro_node_type_menu_access
merci_rro_role_name_from_rid
merci_rro_theme Implementation of hook_theme().
theme_merci_rro_override Theme an override object