forked from vejlebib/ding_staff
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ding_staff.install
227 lines (198 loc) · 5.91 KB
/
ding_staff.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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* @file
* Handle module installation and updates.
*/
/**
* Implements of hook_install().
*/
function ding_staff_install() {
// Make sure that entity cache table for profile2 has been created
// if entity cache is enabled.
if (module_exists('entitycache') && !db_table_exists('cache_entity_profile2')) {
$schema = drupal_get_schema_unprocessed('system', 'cache');
$schema['description'] = 'Cache table used to store profile2 entity records.';
db_create_table('cache_entity_profile2', $schema);
}
}
/**
* Implements hook_enable().
*/
function ding_staff_enable() {
$path = array(
'source' => 'staff',
'language' => 'und',
);
// Create Danish alias for staff if needed.
if (!path_load($path)) {
$path['alias'] = 'personale';
path_save($path);
}
// Make sure that entity cache table for profile2 has been created
// if entity cache is enabled.
if (module_exists('entitycache') && !db_table_exists('cache_entity_profile2')) {
$schema = drupal_get_schema_unprocessed('system', 'cache');
$schema['description'] = 'Cache table used to store profile2 entity records.';
db_create_table('cache_entity_profile2', $schema);
}
}
/**
* Update the staff profile - prefix with module name.
*/
function ding_staff_update_7000() {
$ret = array();
// Update profile type name.
$ret[] = db_update('profile_type')
->fields(array('type' => 'ding_staff_profile'))
->condition('type', 'staff')
->execute();
// Update existing profile to the new type.
$ret[] = db_update('profile')
->fields(array('type' => 'ding_staff_profile'))
->condition('type', 'staff')
->execute();
return $ret;
}
/**
* Update bundle for staff field data and revisions.
*/
function ding_staff_update_7001() {
$ret = array();
$fields = array(
'field_ding_staff_department',
'field_ding_staff_description',
'field_ding_staff_email',
'field_ding_staff_forename',
'field_ding_staff_image',
'field_ding_staff_library',
'field_ding_staff_phone',
'field_ding_staff_position',
'field_ding_staff_surname',
);
foreach ($fields as $field_name) {
$ret[] = db_update('field_data_' . $field_name)
->fields(array('bundle' => 'ding_staff_profile'))
->execute();
$ret[] = db_update('field_revision_' . $field_name)
->fields(array('bundle' => 'ding_staff_profile'))
->execute();
}
return $ret;
}
/**
* Update staff bundle for field instances.
*/
function ding_staff_update_7002() {
$ret = array();
$ret[] = db_update('field_config_instance')
->fields(array('bundle' => 'ding_staff_profile'))
->condition('bundle', 'staff')
->execute();
return $ret;
}
/**
* Remove old library reference field.
*/
function ding_staff_update_7003() {
field_delete_field('field_ding_staff_library');
field_purge_batch(1000);
}
/**
* Move content into the OG library groups.
*/
function ding_staff_update_7004() {
// Only run on sites with legacy OG table from OG version prior to OG 7.x-2.x
if (!db_table_exists('og')) {
return;
}
$query = db_select('field_data_og_group_ref', 'group_ref');
$query->join('og', 'og', 'group_ref.og_group_ref_target_id = og.gid');
$query->fields('group_ref', array('entity_id', 'og_group_ref_target_id'))
->condition('bundle', 'ding_staff_profile');
$query->addField('og', 'etid');
$rows = $query->execute();
foreach ($rows as $row) {
db_insert('og_membership')
->fields(array(
'type' => 'og_membership_type_default',
'etid' => $row->entity_id,
'entity_type' => 'node',
'gid' => $row->etid,
'state' => 1,
'created' => time(),
'group_type' => 'node',
'field_name' => 'og_group_ref',
'language' => 'und',
))
->execute();
}
}
/**
* Create possibly missing Entity Cache table for profile2 entities.
*/
function ding_staff_update_7005() {
// Error on ding2 site with entitycache enabled matches issue described
// in https://drupal.org/node/1387268
// Issue is to be fixed so Entity API has support for Entity Cache,
// see https://drupal.org/node/2017685
// Only run for sites with entitycache enabled.
if (!module_exists('entitycache')) {
return;
}
// Create cache table if it doesn't exist.
if (!db_table_exists('cache_entity_profile2')) {
$schema = drupal_get_schema_unprocessed('system', 'cache');
$schema['description'] = 'Cache table used to store profile2 entity records.';
db_create_table('cache_entity_profile2', $schema);
}
}
/**
* Enable realname module to show profile name instead of user name for content.
*/
function ding_staff_update_7006() {
module_enable(array('realname'));
}
/**
* Update realname for all staff users.
*/
function ding_staff_update_7007(&$sandbox) {
// Page size.
$num = 100;
// Initialize sandbox.
if (!isset($sandbox['offset'])) {
$rid = user_role_load_by_name('staff');
if (!$rid) {
// No user role "staff", finish.
$sandbox['#finished'] = 1;
return;
}
if (is_object($rid)) {
$rid = $rid->rid;
}
else {
// Error loading user role.
throw new DrupalUpdateException('Error loading user role "staff".');
}
$query = db_select('users_roles', 'ur');
$query->condition('ur.rid', $rid);
$query->addField('ur', 'uid');
$count = $query->countQuery()->execute()->fetchField();
$sandbox['offset'] = 0;
$sandbox['rid'] = $rid;
$sandbox['count'] = $count;
$sandbox['#finished'] = 0;
}
// Load next $num elements.
$query = db_select('users_roles', 'ur');
$query->condition('ur.rid', $sandbox['rid']);
$query->addField('ur', 'uid');
$query->range($sandbox['offset'], $num);
$res = $query->execute();
while ($record = $res->fetchField()) {
$account = user_load($record);
realname_update($account);
}
// Update sandbox values.
$sandbox['offset'] += $res->rowCount();
$sandbox['#finished'] = $sandbox['offset'] / $sandbox['count'];
}