forked from backdrop-contrib/menu_per_role
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu_per_role.install
101 lines (89 loc) · 2.28 KB
/
menu_per_role.install
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
<?php
/**
* @file
* Install, update and uninstall functions for the menu_per_role module.
*/
/**
* Implements hook_schema().
*/
function menu_per_role_schema() {
$schema['menu_per_role'] = array(
'fields' => array(
'mlid' => array(
'description' => 'The menu identifier.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
'rids' => array(
'description' => 'The role identifiers separated by commas. Show to those roles.',
'type' => 'text',
'not null' => TRUE,
),
'hrids' => array(
'description' => 'The role identifiers separated by commas. Hide from those roles.',
'type' => 'text',
'not null' => TRUE,
),
),
'primary key' => array('mlid'),
'foreign keys' => array(
'mlid' => array(
'table' => 'menu',
'columns' => array('mlid' => 'mlid'),
),
),
);
return $schema;
}
/**
* Convert Drupal 7 migrated roles from IDs to Keys.
*/
function menu_per_role_update_1000() {
$records = db_select('menu_per_role', 'mpr')
->fields('mpr')
->execute()
->fetchAllAssoc('mlid');
foreach ($records as $key => $record) {
$updateRecord = FALSE;
$rids = $record->rids;
$ridParts = explode(',', $rids);
// Iterate through each rid that needs to be converted.
foreach ($ridParts as &$rid) {
switch ($rid) {
case 1:
$rid = BACKDROP_ANONYMOUS_ROLE;
$updateRecord = TRUE;
break;
case 2:
$rid = BACKDROP_AUTHENTICATED_ROLE;
$updateRecord = TRUE;
break;
}
}
$hrids = $record->hrids;
$hridParts = explode(',', $hrids);
// Iterate through each hrid that needs to be converted.
foreach ($hridParts as &$hrid) {
switch ($hrid) {
case 1:
$hrid = BACKDROP_ANONYMOUS_ROLE;
$updateRecord = TRUE;
break;
case 2:
$hrid = BACKDROP_AUTHENTICATED_ROLE;
$updateRecord = TRUE;
break;
}
}
if ($updateRecord) {
db_update('menu_per_role')
->fields([
'rids' => implode(',', $ridParts),
'hrids' => implode(',', $hridParts),
])
->condition('mlid', $key)
->execute();
}
}
}