You are here

mobile_tools_roles.module in Mobile Tools 6.3

Contains the functionality to add mobile user roles

File

mobile_tools_roles.module
View source
<?php

/**
 * @file
 * Contains the functionality to add mobile user roles 
 *
 */

/**
 * Being called in the hook_init() implementation
 * This function is in charge of changing the user role 
 */
function mobile_tools_roles_init() {
  if (variable_get('mobile_tools_enable_roles', 0)) {
    global $user;
    $roles = $user->roles;
    foreach ($roles as $rid => $role_name) {
      $mobile_role = mobile_tools_roles_get_mobile_role($rid);
      if (!empty($mobile_role) && mobile_tools_site_type() == 'mobile') {
        unset($user->roles[$rid]);
        $user->roles[$mobile_role['mrid']] = $mobile_role['name'];
      }
    }
  }
}

/**
 * Implementation of hook_menu
 */
function mobile_tools_roles_menu() {
  $items['admin/settings/mobile-tools/roles'] = array(
    'title' => 'Mobile roles',
    'page arguments' => array(
      'mobile_tools_roles_configuration_form',
    ),
    'page callback' => 'drupal_get_form',
    'access arguments' => array(
      'configure Mobile Tools',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'mobile_tools_roles.inc',
    'weight' => 0,
    'file' => 'mobile_tools_roles.admin.inc',
  );
  return $items;
}

/**
* Function helping in getting information for each role:
* @param $identifier
*  array('id' => id) or array('name' => name)
*   
* @return 
*   0 in case there is not mobile role
*   rid of the mobile role
*/
function mobile_tools_roles_get_mobile_role($rid) {
  $query = "SELECT m.mrid, r.name FROM {mobile_tools_roles_relations} m JOIN {role} r ON m.mrid = r.rid WHERE m.rid = %d";
  $mobile_role = db_fetch_array(db_query($query, $rid));
  if (empty($mobile_role)) {
    return NULL;
  }
  else {
    return $mobile_role;
  }
}

/**
 * Get an overview of all the mobile roles
 *
 */
function mobile_tools_roles_overview() {
  $output = '';
  $query = "SELECT * FROM {mobile_tools_roles_relations}";
  $result = db_query($query);
  $rows = array();
  while ($item = db_fetch_object($result)) {
    $query = "SELECT * FROM {role} WHERE rid = %d";
    $result1 = db_query($query, $item->rid);
    $result2 = db_query($query, $item->mrid);
    $rows[] = array(
      db_fetch_object($result1)->name,
      db_fetch_object($result2)->name,
    );
  }
  $headers = array(
    "original role",
    "mobile role",
  );
  if (count($rows) == 0) {
    $output .= '<div class="message">No Mobile roles were assigned</div>';
  }
  else {
    $output .= theme('table', $headers, $rows);
    $output .= '<br>' . t('Configure the !permissions', array(
      '!permissions' => l('permissions', 'admin/user/permissions'),
    )) . '<br />';
  }
  return $output;
}

/**
 * Helper function to return the mobile user roles
 */
function mobile_tools_roles_mobile_user_roles() {
  $query = "SELECT m.mrid, r.name FROM {mobile_tools_roles_relations} m LEFT JOIN {role} r ON m.mrid = r.rid";
  $result = db_query($query);
  while ($mobile_role = db_fetch_object($result)) {
    $mobile_roles[] = $mobile_role;
  }
  return $mobile_roles;
}

/**
 * Function helping in saving and deleting the mobile roles
 * @param $op
 *  the operation that has to be performed: 'delete' will delete the mobile role, 'add' will add the mobile role.  
 * @param $rid
 *  Role id of the related desktop role
 * @param $mrid
 *  Role id of the mobile role (only when deleting) 
 */
function mobile_tools_roles_edit_mobile_role($op, $rid, $mrid = '') {
  switch ($op) {
    case 'delete':
      db_query("DELETE FROM {role} WHERE rid = %d", $mrid);
      db_query("DELETE FROM {mobile_tools_roles_relations} WHERE mrid = %d", $mrid);
      db_query("DELETE FROM {permission} WHERE rid = %d", $mrid);
      break;
    case 'add':
      $query = "SELECT name FROM {role} WHERE rid = %d";
      $name = db_result(db_query($query, $rid));
      $result = db_query("INSERT INTO {role} (name) VALUES ('%s')", $name . ' (Mobile)');
      $mrid = db_last_insert_id('role', 'rid');
      db_query("INSERT INTO {mobile_tools_roles_relations} (rid, mrid) VALUES (%d, %d)", $rid, $mrid);
      $perm = db_fetch_object(db_query("SELECT * FROM {permission} WHERE rid = %d", $rid));
      if (!empty($perm)) {
        db_query("INSERT INTO {permission} (rid, perm, tid) VALUES (%d, '%s', %d)", $mrid, $perm->perm, $perm->tid);
      }
      break;
  }
}

/**
 * Alteration to global setting form
 */
function mobile_tools_roles_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'user_profile_form':

      // We make sure that the mobile roles are not being displayed
      $mobile_roles = mobile_tools_roles_mobile_user_roles();
      if (!empty($mobile_roles)) {
        foreach ($mobile_roles as $mobile_role) {
          unset($form['account']['roles']['#options'][$mobile_role->mrid]);
        }
      }
  }
}

Functions

Namesort descending Description
mobile_tools_roles_edit_mobile_role Function helping in saving and deleting the mobile roles
mobile_tools_roles_form_alter Alteration to global setting form
mobile_tools_roles_get_mobile_role Function helping in getting information for each role:
mobile_tools_roles_init Being called in the hook_init() implementation This function is in charge of changing the user role
mobile_tools_roles_menu Implementation of hook_menu
mobile_tools_roles_mobile_user_roles Helper function to return the mobile user roles
mobile_tools_roles_overview Get an overview of all the mobile roles