forked from Islandora-Collaboration-Group/islandora_matomo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathislandora_matomo.install
168 lines (156 loc) · 4.9 KB
/
islandora_matomo.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
<?PHP
/**
* @file
* Install file for islandora_matomo.
*/
/**
* Implements hook_enable().
*/
function islandora_matomo_enable() {
// Check if this new send matomo report field is not already there.
if (!field_info_field('field_islandoramatomo_sendreport')) {
$field = [
'field_name' => 'field_islandoramatomo_sendreport',
'type' => 'list_boolean',
'cardinality' => 1,
'settings' => [
'allowed_values' => [
1 => 'Yes',
0 => 'No',
],
],
];
field_create_field($field);
// Create the instance on the bundle.
$instance = [
'field_name' => 'field_islandoramatomo_sendreport',
'entity_type' => 'user',
'label' => 'Send Islandora Matomo Report via email every month.',
'bundle' => 'user',
// If you don't set the "required" property then the field wont be
// required by default.
'required' => TRUE,
'settings' => [
// Field showing up on the registration form.
'user_register_form' => 1,
],
'widget' => [
'type' => 'options_buttons',
],
'weight' => '1',
];
field_create_instance($instance);
}
// Check if this new u1 to drupal value field is not already there.
if (!field_info_field('field_islandoramatomo_userkey')) {
$field2 = [
'field_name' => 'field_islandoramatomo_userkey',
'type' => 'text',
'cardinality' => 1,
'max_length' => 64,
];
field_create_field($field2);
// @TODO there could be security concern here.
// If a user can change its own userkey and access via that to
// another dashboard?
// Create the instance on the bundle.
$instance2 = [
'field_name' => 'field_islandoramatomo_userkey',
'entity_type' => 'user',
'label' => 'Value used to connect an Islandora Scholar Profile to this User account.',
'bundle' => 'user',
// If you don't set the "required" property then the field wont
// be required by default.
'required' => TRUE,
'settings' => [
// Field showing up on the registration form.
// If 0 it will only show on edit. Ask @noah.
'user_register_form' => 1,
],
'widget' => [
'type' => 'text_textfield',
],
'display' => [
'default' => [
'type' => 'text_default',
],
],
'weight' => '2',
];
field_create_instance($instance2);
}
if (!field_info_field('field_islandoramatomo_lastreport')) {
$field3 = [
'field_name' => 'field_islandoramatomo_lastreport',
'type' => 'number_integer',
'cardinality' => 1,
'translatable' => FALSE,
];
field_create_field($field3);
$instance3 = [
'field_name' => 'field_islandoramatomo_lastreport',
'entity_type' => 'user',
'no_ui' => TRUE,
'display' => [
'default' => [
'type' => 'hidden',
],
'teaser' => [
'type' => 'hidden',
],
],
'label' => 'Value used to track last sent report for this user.',
'bundle' => 'user',
'settings' => [
// Field showing up on the registration form.
// If 0 it will only show on edit. Ask @noah.
'user_register_form' => 0,
],
'weight' => '3',
];
field_create_instance($instance3);
}
}
/**
* Gives the field field_islandoramatomo_userkey on existing users a default.
*/
function islandora_matomo_update_7001(&$sandbox) {
$entity_type = 'user';
$bundle = 'user';
$field = 'field_islandoramatomo_userkey';
$field_info = field_info_field($field);
$field_keys = array($field_info['id']);
// Gives the field field_islandoramatomo_userkey on existing users a default.
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['current_uid'] = 0;
// We'll -1 to disregard the uid 0...
$sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {users}')->fetchField() - 1;
}
$users = db_select('users', 'u')
->fields('u', array('uid', 'name'))
->condition('uid', $sandbox['current_uid'], '>')
->range(0, 10)
->orderBy('uid', 'ASC')
->execute();
foreach ($users as $user) {
$userobject = user_load($user->uid);
$userobject->{$field}[LANGUAGE_NONE][0]['value'] = $userobject->name;
field_sql_storage_field_storage_write('user', $userobject, 'update', $field_keys);
// Clear field cache.
cache_clear_all("field:user:$user->uid", 'cache_field');
$sandbox['progress']++;
$sandbox['current_uid'] = $user->uid;
}
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : ($sandbox['progress'] / $sandbox['max']);
$t = get_t();
return $t("Islandora Matomo updates updates on users complete");
}
/**
* Implements hook_uninstall().
*/
function islandora_matomo_uninstall() {
field_delete_field('field_islandoramatomo_userkey');
field_delete_field('field_islandoramatomo_sendreport');
field_delete_field('field_islandoramatomo_lastreport');
}