You are here

function merci_permissions_add_form in MERCI (Manage Equipment Reservations, Checkout and Inventory) 6

Same name and namespace in other branches
  1. 6.2 modules/merci_permissions/merci_permissions.module \merci_permissions_add_form()
2 calls to merci_permissions_add_form()
merci_permissions_form in modules/merci_permissions/merci_permissions.module
merci_permissions_form_alter in modules/merci_permissions/merci_permissions.module

File

modules/merci_permissions/merci_permissions.module, line 107
Provides functions for resetting permissions on the permission page.

Code

function merci_permissions_add_form(&$form) {
  $role_names = user_roles();
  $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid ORDER BY name');

  // Compile role array:
  // Add a comma at the end so when searching for a permission, we can
  // always search for "$perm," to make sure we do not confuse
  // permissions that are substrings of each other.
  while ($role = db_fetch_object($result)) {
    $role_permissions[$role->rid] = $role->perm . ',';
  }
  $merci_types = merci_permissions_load_merci_type_settings();
  $options = array();
  $admin = array();
  $form['permission'][] = array(
    '#value' => t("General Permissions"),
  );
  $form['permission']['admin all reservations'] = array(
    '#value' => t('Admin all reservations'),
  );
  $options['admin all reservations'] = '';
  $merci_perms = merci_perm();
  foreach ($merci_perms as $perm) {
    $options[$perm] = '';
    $form['permission'][$perm] = array(
      '#value' => t($perm),
    );
    foreach ($role_names as $rid => $name) {

      // Builds arrays for checked boxes for each role
      if (strpos($role_permissions[$rid], $perm . ',') !== FALSE) {
        $status[$rid][] = $perm;
      }
    }
  }
  $form['permission'][] = array(
    '#value' => "Bucket and Resource Permissions",
  );
  foreach ($merci_types as $type => $name) {
    $perm = "reserve {$type}";
    $options[$perm] = '';
    $form['permission'][$perm] = array(
      '#value' => t($name),
    );
    foreach ($role_names as $rid => $name) {

      // Builds arrays for checked boxes for each role
      if (strpos($role_permissions[$rid], 'delete own ' . $type . ' content,') !== FALSE && strpos($role_permissions[$rid], 'edit own ' . $type . ' content,') !== FALSE) {
        $status[$rid][] = $perm;
      }
      if (strpos($role_permissions[$rid], 'delete any ' . $type . ' content,') !== FALSE && strpos($role_permissions[$rid], 'edit any ' . $type . ' content,') !== FALSE) {
        if (!strstr($admin[$rid], 'false')) {
          $admin[$rid] = 'true';
        }
      }
      else {
        $admin[$rid] = 'false';
      }
    }
  }
  foreach ($role_names as $rid => $name) {
    if (strstr($admin[$rid], 'true')) {
      $status[$rid][] = 'admin all reservations';
    }
  }

  // Have to build checkboxes here after checkbox arrays are built
  foreach ($role_names as $rid => $name) {
    $form['checkboxes'][$rid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => isset($status[$rid]) ? $status[$rid] : array(),
    );
    $form['role_names'][$rid] = array(
      '#value' => $name,
      '#tree' => TRUE,
    );
  }
  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Permissions'),
  );
  $form['#submit'][] = 'merci_permissions_settings_form_submit';
}