-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmobile_tools_roles.module
144 lines (136 loc) · 4.52 KB
/
mobile_tools_roles.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?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();
foreach($mobile_roles as $mobile_role) {
unset($form['account']['roles']['#options'][$mobile_role->mrid]);
}
}
}