Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / eu_cookie_compliance / eu_cookie_compliance.install
1 <?php
2
3 /**
4  * @file
5  * Update scripts for the EU Cookie Compliance module.
6  */
7
8 use Drupal\user\Entity\Role;
9 use Drupal\Core\Database\Database;
10 use Drupal\filter\Entity\FilterFormat;
11
12 /**
13  * Implements hook_schema().
14  */
15 function eu_cookie_compliance_schema() {
16   $schema['eu_cookie_compliance_basic_consent'] = [
17     'description' => 'Basic consent storage for EU Cookie Compliance / GDPR.',
18     'fields' => [
19       'cid' => [
20         'type' => 'serial',
21         'not null' => TRUE,
22         'description' => 'Primary Key: Unique consent storage ID.',
23       ],
24       'uid' => [
25         'description' => '{users}.uid for user.',
26         'type' => 'int',
27         'unsigned' => TRUE,
28         'not null' => TRUE,
29         'default' => 0,
30       ],
31       'timestamp' => [
32         'description' => 'Time of consent.',
33         'type' => 'int',
34         'unsigned' => FALSE,
35         'not null' => TRUE,
36         'default' => 0,
37       ],
38       'ip_address' => [
39         'description' => 'The IP address.',
40         'type' => 'varchar',
41         // Maximum length of an ipv6 IP address.
42         'length' => 45,
43         'not null' => TRUE,
44         'default' => '',
45       ],
46       'consent_type' => [
47         'description' => 'The type of consent, such as "banner" for the banner and form_id for forms.',
48         'type' => 'varchar',
49         'length' => 255,
50         'not null' => TRUE,
51         'default' => '',
52       ],
53       'revision_id' => [
54         'description' => 'Revision of the privacy policy at the time of consent.',
55         'type' => 'int',
56         'unsigned' => TRUE,
57         'not null' => TRUE,
58         'default' => 0,
59       ],
60     ],
61     'primary key' => ['cid'],
62     'indexes' => [
63       'uid' => ['uid'],
64     ],
65     'foreign keys' => [
66       'uid' => ['users' => 'uid'],
67     ],
68   ];
69
70   return $schema;
71 }
72
73 /**
74  * Implements hook_install().
75  */
76 function eu_cookie_compliance_install() {
77   module_load_include('module', 'eu_cookie_compliance', 'eu_cookie_compliance');
78
79   $roles = Role::loadMultiple();
80   $permission = 'display eu cookie compliance popup';
81   foreach ($roles as $rid => $role) {
82     user_role_grant_permissions($rid, [$permission]);
83   }
84
85   $cookie_policy = _eu_cookie_compliance_find_privacy_policy();
86   if ($cookie_policy != FALSE) {
87     \Drupal::configFactory()
88       ->getEditable('eu_cookie_compliance.settings')
89       ->set('popup_link', $cookie_policy)
90       ->save();
91   }
92
93   eu_cookie_compliance_module_set_weight();
94 }
95
96 /**
97  * Implements hook_requirements().
98  */
99 function eu_cookie_compliance_requirements($phase) {
100   $requirements = [];
101
102   if ($phase == 'runtime') {
103     $popup_link = Drupal::config('eu_cookie_compliance.settings')->get('popup_link');
104     $show_policy = Drupal::config('eu_cookie_compliance.settings')->get('show_disagree_button');
105
106     if ($popup_link == '<front>' && $show_policy) {
107       $requirements['eu_cookie_compliance'] = [
108         'title' => t('EU Cookie Compliance'),
109         'severity' => REQUIREMENT_WARNING,
110         'description' => t('Your privacy policy link is pointing at the front page. This is the default value after installation, and unless your privacy policy is actually posted at the front page, you will need to create a separate page for the privacy policy and link to that page.'),
111         'value' => t('Privacy policy link not provided'),
112       ];
113     }
114   }
115
116   return $requirements;
117 }
118
119 /**
120  * Force default value for "cookie_lifetime" item.
121  */
122 function eu_cookie_compliance_update_8101() {
123   \Drupal::configFactory()
124     ->getEditable('eu_cookie_compliance.settings')
125     ->set('cookie_lifetime', 100)->save();
126 }
127
128 /**
129  * Reverse the setting for "Consent by clicking" (solving a module beta bug).
130  */
131 function eu_cookie_compliance_update_8102() {
132   $consent_by_clicking_value = \Drupal::configFactory()
133     ->get('eu_cookie_compliance.settings')
134     ->get('popup_clicking_confirmation');
135   \Drupal::configFactory()
136     ->getEditable('eu_cookie_compliance.settings')
137     ->set('popup_clicking_confirmation', !$consent_by_clicking_value)->save();
138 }
139
140 /**
141  * Fix bug with mobile banner message from beta 9.
142  */
143 function eu_cookie_compliance_update_8103() {
144   $mobile_popup_info = \Drupal::configFactory()
145     ->get('eu_cookie_compliance.settings')
146     ->get('mobile_popup_info');
147   if (!is_array($mobile_popup_info) && $mobile_popup_info == '') {
148     \Drupal::configFactory()
149       ->getEditable('eu_cookie_compliance.settings')
150       ->set('mobile_popup_info', [
151         'value' => '',
152         'format' => filter_default_format(),
153       ])->save();
154   }
155 }
156
157 /**
158  * Change seconds to milliseconds for animation duration.
159  */
160 function eu_cookie_compliance_update_8104() {
161   $popup_delay = \Drupal::configFactory()
162     ->get('eu_cookie_compliance.settings')
163     ->get('popup_delay');
164   if ($popup_delay < 10) {
165     \Drupal::configFactory()
166       ->getEditable('eu_cookie_compliance.settings')
167       ->set('popup_delay', $popup_delay * 1000)->save();
168   }
169 }
170
171 /**
172  * Create new config value for show or hide the cookie policy button.
173  */
174 function eu_cookie_compliance_update_8105() {
175   \Drupal::configFactory()
176     ->getEditable('eu_cookie_compliance.settings')
177     ->set('show_disagree_button', TRUE)
178     ->save();
179 }
180
181 /**
182  * Create new config value to handle consent options.
183  */
184 function eu_cookie_compliance_update_8106() {
185   \Drupal::configFactory()
186     ->getEditable('eu_cookie_compliance.settings')
187     ->set('method', 'default')
188     ->set('disagree_button_label', 'No, thanks')
189     ->set('disabled_javascripts', '')
190     ->set('whitelisted_cookies', '')
191     ->save();
192 }
193
194 /**
195  * Add table to handle basic consent.
196  */
197 function eu_cookie_compliance_update_8107() {
198   $schema['eu_cookie_compliance_basic_consent'] = [
199     'description' => 'Basic consent storage for EU Cookie Compliance / GDPR.',
200     'fields' => [
201       'cid' => [
202         'type' => 'serial',
203         'not null' => TRUE,
204         'description' => 'Primary Key: Unique consent storage ID.',
205       ],
206       'uid' => [
207         'description' => '{users}.uid for user.',
208         'type' => 'int',
209         'unsigned' => TRUE,
210         'not null' => TRUE,
211         'default' => 0,
212       ],
213       'timestamp' => [
214         'description' => 'Time of consent.',
215         'type' => 'int',
216         'unsigned' => FALSE,
217         'not null' => TRUE,
218         'default' => 0,
219       ],
220       'ip_address' => [
221         'description' => 'The IP address.',
222         'type' => 'varchar',
223         // Maximum length of an ipv6 IP address.
224         'length' => 45,
225         'not null' => TRUE,
226         'default' => '',
227       ],
228       'consent_type' => [
229         'description' => 'The type of consent, such as "banner" for the banner and form_id for forms.',
230         'type' => 'varchar',
231         'length' => 255,
232         'not null' => TRUE,
233         'default' => '',
234       ],
235       'revision_id' => [
236         'description' => 'Revision of the privacy policy at the time of consent.',
237         'type' => 'int',
238         'unsigned' => TRUE,
239         'not null' => TRUE,
240         'default' => 0,
241       ],
242     ],
243     'primary key' => ['cid'],
244     'indexes' => [
245       'uid' => ['uid'],
246     ],
247     'foreign keys' => [
248       'uid' => ['users' => 'uid'],
249     ],
250   ];
251
252   Database::getConnection()->schema()->createTable('eu_cookie_compliance_basic_consent', $schema['eu_cookie_compliance_basic_consent']);
253   \Drupal::configFactory()
254     ->getEditable('eu_cookie_compliance.settings')
255     ->set('consent_storage_method', 'do_not_store')
256     ->save();
257 }
258
259 /**
260  * Change module weight to load after other modules, ensure all JSs are handled.
261  */
262 function eu_cookie_compliance_update_8108() {
263   module_load_include('module', 'eu_cookie_compliance', 'eu_cookie_compliance');
264   eu_cookie_compliance_module_set_weight();
265 }
266
267 /**
268  * Add config variables for the withdraw banner.
269  */
270 function eu_cookie_compliance_update_8109() {
271   $default_filter_format = filter_default_format();
272   $full_html_format = FilterFormat::load('full_html');
273   if ($default_filter_format == 'restricted_html' && !empty($full_html_format) && $full_html_format->get('status')) {
274     $default_filter_format = 'full_html';
275   }
276
277   \Drupal::configFactory()
278     ->getEditable('eu_cookie_compliance.settings')
279     ->set('withdraw_message', [
280       'value' => '<h2>We use cookies on this site to enhance your user experience</h2><p>You have given your consent for us to set cookies.</p>',
281       'format' => $default_filter_format,
282     ])
283     ->set('withdraw_action_button_label', 'Withdraw consent')
284     ->set('withdraw_tab_button_label', 'Privacy settings')
285     ->set('withdraw_enabled', TRUE)
286     ->save();
287 }
288
289 /**
290  * Disable withdraw tab and banner in the consent method "Consent by default".
291  */
292 function eu_cookie_compliance_update_8110() {
293   $withdraw_enabled = \Drupal::configFactory()
294     ->get('eu_cookie_compliance.settings')
295     ->get('withdraw_enabled');
296   $method = \Drupal::configFactory()
297     ->get('eu_cookie_compliance.settings')
298     ->get('method');
299
300   if ($method == 'default' && $withdraw_enabled == 1) {
301     \Drupal::configFactory()
302       ->getEditable('eu_cookie_compliance.settings')
303       ->set('withdraw_enabled', FALSE)
304       ->save();
305   }
306 }