commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / webform_civicrm / webform_civicrm.install
1 <?php
2
3 /**
4 * @file
5 * Webform CiviCRM module's install, uninstall and upgrade code.
6 */
7
8 /**
9 * Implements hook_requirements().
10 */
11 function webform_civicrm_requirements($phase) {
12 $requirements = array();
13
14 if ($phase == 'runtime') {
15 $t = get_t();
16 $status = _webform_civicrm_status();
17
18 if (!$status['webform_civicrm']) {
19
20 $requirements['webform_civicrm'] = array(
21 'title' => 'Webform CiviCRM Integration',
22 'value' => $t('Version error'),
23 'severity' => REQUIREMENT_ERROR,
24 'description' => $t('The versions of the Webform CiviCRM Integration, Webform, CiviCRM enabled are not compatible. ') .
25 l($t('See the Webform CiviCRM Integration project page for version compatibility'), 'https://drupal.org/project/webform_civicrm'),
26 );
27 }
28 else {
29
30 $requirements['webform_civicrm'] = array(
31 'title' => 'Webform CiviCRM Integration',
32 'severity' => REQUIREMENT_OK,
33 'value' => t('Required version of CiviCRM and Webform are enabled.'),
34 );
35 }
36 }
37
38 return $requirements;
39 }
40
41 /**
42 * Implements hook_schema().
43 */
44 function webform_civicrm_schema() {
45 $schema = array();
46 $schema['webform_civicrm_forms'] = array(
47 'description' => 'CiviCRM settings for individual Webform nodes.',
48 'fields' => array(
49 'nid' => array(
50 'type' => 'int',
51 'unsigned' => TRUE,
52 'not null' => TRUE,
53 'default' => 0,
54 'description' => 'Webform Node ID',
55 ),
56 'data' => array(
57 'type' => 'text',
58 'serialize' => TRUE,
59 'description' => 'Array of entity data for this webform',
60 ),
61 'prefix_known' => array(
62 'description' => 'Form prefix for known users.',
63 'type' => 'text',
64 'not null' => TRUE,
65 ),
66 'prefix_unknown' => array(
67 'description' => 'Form prefix for unknown users.',
68 'type' => 'text',
69 'not null' => TRUE,
70 ),
71 'message' => array(
72 'type' => 'varchar',
73 'length' => 255,
74 'not null' => TRUE,
75 'default' => '',
76 'description' => 'Message to show to known users',
77 ),
78 'confirm_subscription' => array(
79 'description' => 'Send confirmation for mailing list subscriptions.',
80 'type' => 'int',
81 'size' => 'tiny',
82 'not null' => TRUE,
83 'default' => 0,
84 ),
85 'block_unknown_users' => array(
86 'description' => 'Only allow known contacts to use form.',
87 'type' => 'int',
88 'size' => 'tiny',
89 'not null' => TRUE,
90 'default' => 0,
91 ),
92 'create_fieldsets' => array(
93 'description' => 'Add fieldsets around contacts.',
94 'type' => 'int',
95 'size' => 'tiny',
96 'not null' => TRUE,
97 'default' => 0,
98 ),
99 'new_contact_source' => array(
100 'type' => 'varchar',
101 'length' => 255,
102 'not null' => TRUE,
103 'default' => '',
104 'description' => 'Source label for newly created contacts',
105 ),
106 ),
107 'primary key' => array('nid'),
108 );
109 $schema['webform_civicrm_submissions'] = array(
110 'description' => 'Link between form submissions and CiviCRM data.',
111 'fields' => array(
112 'sid' => array(
113 'type' => 'int',
114 'unsigned' => TRUE,
115 'not null' => TRUE,
116 'default' => 0,
117 'description' => 'Webform Submission ID',
118 ),
119 'contact_id' => array(
120 'type' => 'varchar',
121 'length' => 2000,
122 'not null' => TRUE,
123 'default' => '',
124 'description' => 'CiviCRM contact ids from this submission',
125 ),
126 'civicrm_data' => array(
127 'type' => 'text',
128 'serialize' => TRUE,
129 'description' => 'Array of entity ids for this submission',
130 ),
131 ),
132 'primary key' => array('sid'),
133 );
134 return $schema;
135 }
136
137
138 /**
139 * Add column for message to webform_civicrm_forms table.
140 */
141 function webform_civicrm_update_6100() {
142 $message = array(
143 'type' => 'varchar',
144 'length' => 255,
145 'not null' => TRUE,
146 'default' => '',
147 'description' => 'Message to show to known users',
148 );
149 db_add_field('webform_civicrm_forms', 'message', $message);
150 }
151
152 /**
153 * Add missing activity targets.
154 */
155 function webform_civicrm_update_6101() {
156 civicrm_initialize();
157 $db = db_query('SELECT * FROM {webform_civicrm_submissions} WHERE contact_id <> :contact_id AND activity_id <> :activity_id', array(':contact_id' => 0, ':activity_id' => 0));
158 $sql = 'INSERT INTO civicrm_activity_target (activity_id, target_contact_id) VALUES (%1,%2)';
159 $n = 1;
160 $c = 0;
161 $params = array();
162 foreach ($db as $row) {
163 $params[$n] = array($row->activity_id, 'Integer');
164 $params[$n + 1] = array($row->contact_id, 'Integer');
165 if ($n > 1) {
166 $sql .= ',(%' . $n . ',%' . ($n + 1) . ')';
167 }
168 $n += 2;
169 ++$c;
170 }
171 if ($n > 1) {
172 CRM_Core_DAO::executeQuery($sql, $params);
173 }
174 return st('@num activity target contacts added.', array('@num' => $c));
175 }
176
177 /**
178 * Add column for confirm_subscription to webform_civicrm_forms table.
179 */
180 function webform_civicrm_update_6102() {
181 $confirm_subscription = array(
182 'description' => 'Send confirmation for mailing list subscriptions.',
183 'type' => 'int',
184 'size' => 'tiny',
185 'not null' => TRUE,
186 'default' => 0,
187 );
188 db_add_field('webform_civicrm_forms', 'confirm_subscription', $confirm_subscription);
189 }
190
191 /**
192 * Upgrade from 1.x to 2.x
193 * Add columns to webform_civicrm_forms table, and convert existing forms to new multi-entity schema.
194 */
195 function webform_civicrm_update_6200() {
196 $field = array(
197 'type' => 'text',
198 'serialize' => TRUE,
199 'description' => 'Array of entity data for this form',
200 );
201 db_add_field('webform_civicrm_forms', 'data', $field);
202 $field = array(
203 'description' => 'Add fieldsets around contacts.',
204 'type' => 'int',
205 'size' => 'tiny',
206 'not null' => TRUE,
207 'default' => 0,
208 );
209 db_add_field('webform_civicrm_forms', 'create_fieldsets', $field);
210
211 // Add value separator to CID
212 $field = array(
213 'type' => 'varchar',
214 'length' => 2000,
215 'not null' => TRUE,
216 'default' => '',
217 'description' => 'CiviCRM contact ids from this submission',
218 );
219 db_change_field('webform_civicrm_submissions', 'contact_id', 'contact_id', $field);
220
221 db_update('webform_civicrm_submissions')
222 ->expression('contact_id', "CONCAT('-', contact_id, '-')")
223 ->execute();
224
225 // Match field keys to new format
226 civicrm_initialize();
227 module_load_include('inc', 'webform_civicrm', 'includes/utils');
228 $fields = wf_crm_get_fields();
229 $match = $contacts = $update = array();
230 // Create matching table
231 foreach ($fields as $key => $field) {
232 list($table, $name) = explode('_', $key, 2);
233 $match['civicrm_' . $name] = 'civicrm_1_contact_1_' . $key;
234 }
235 // Include field types that no longer exist, to be dealt with by subsequent update hooks
236 $match['civicrm_state_province'] = 'civicrm_1_contact_1_address_state_province';
237
238 // Collect entity data
239 $db = db_query("SELECT form_key, nid FROM {webform_component} WHERE form_key LIKE 'civicrm_%%' AND type <> 'fieldset'");
240 foreach ($db as $row) {
241 if (array_key_exists($row->form_key, $match)) {
242 $update[$row->form_key] = $match[$row->form_key];
243 list($a, $b, $c, $d, $e, $f) = explode('_', $match[$row->form_key], 6);
244 if ($e != 'contact' && $e != 'other') {
245 $contacts[$row->nid]['number_of_' . $e] = 1;
246 if ($e == 'address' || $e == 'email' || $e == 'phone') {
247 $contacts[$row->nid][$e][1]['location_type_id'] = 1;
248 }
249 if ($e == 'phone') {
250 $contacts[$row->nid][$e][1]['phone_type_id'] = 1;
251 }
252 }
253 }
254 }
255
256 // Update field keys
257 foreach ($update as $old => $new) {
258 db_update('webform_component')
259 ->fields(array('form_key' => $new))
260 ->condition('form_key', $old)
261 ->execute();
262 }
263
264 // Populate entity data
265 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
266 foreach ($db as $row) {
267 $data = array(
268 'contact' => array(
269 1 => array(
270 'contact' => array(1 => array(
271 'contact_type' => 'individual',
272 'contact_sub_type' => NULL,
273 )),
274 'activity_target' => 1,
275 ),
276 ),
277 );
278 if (!empty($contacts[$row->nid])) {
279 $data['contact'][1] += $contacts[$row->nid];
280 }
281 if ($row->activity_type_id) {
282 $data['activity'][1]['add_link'] = TRUE;
283 $data['activity'][1]['activity'][1] = array(
284 'activity_type_id' => $row->activity_type_id,
285 'subject' => $row->activity_subject,
286 'status_id' => 2,
287 );
288 }
289 db_update('webform_civicrm_forms')
290 ->fields(array('data' => serialize($data)))
291 ->condition('nid', $row->nid)
292 ->execute();
293 }
294 // Remove activity fields
295 db_drop_field('webform_civicrm_forms', 'activity_type_id');
296 db_drop_field('webform_civicrm_forms', 'activity_subject');
297 return st('Upgrade to webform_civicrm 2 successful. Note: The field key (machine name) of civicrm-related webform fields has changed. If you were using any of these keys in webform hooks or tokens, please update it to use the new key.');
298 }
299
300 /**
301 * Add column for new_contact_source to webform_civicrm_forms table.
302 */
303 function webform_civicrm_update_6201() {
304 $field = array(
305 'type' => 'varchar',
306 'length' => 255,
307 'not null' => TRUE,
308 'default' => '',
309 'description' => 'Source label for newly created contacts',
310 );
311 db_add_field('webform_civicrm_forms', 'new_contact_source', $field);
312 }
313
314 /**
315 * Add column for contact_matching to webform_civicrm_forms table.
316 */
317 function webform_civicrm_update_6202() {
318 $field = array(
319 'description' => 'How to match and autofill for contacts',
320 'type' => 'int',
321 'size' => 'tiny',
322 'not null' => TRUE,
323 'default' => 1,
324 );
325 db_add_field('webform_civicrm_forms', 'contact_matching', $field);
326 }
327
328 /**
329 * Support new state/prov chain-select feature.
330 */
331 function webform_civicrm_update_6203() {
332 module_load_include('inc', 'webform', 'includes/webform.components');
333
334 // First get rid of redundant fields
335 $db = db_query("SELECT c1.* FROM {webform_component} c1, {webform_component} c2 WHERE c1.nid = c2.nid AND c1.form_key LIKE 'civicrm_%%_contact_%%_address_state_province' AND c2.form_key = CONCAT(c1.form_key, '_id')");
336 foreach ($db as $item) {
337 webform_component_delete($item, (array) $item);
338 }
339
340 // Update state_province fields
341 $submitted = array();
342 $db = db_query("SELECT * FROM {webform_component} WHERE form_key LIKE 'civicrm_%%_contact_%%_address_state_province%%'");
343 foreach ($db as $item) {
344 if (substr($item->form_key, -3) == '_id') {
345 $submitted[] = '(nid = ' . $item->nid . ' AND cid = ' . $item->cid . ')';
346 }
347 else {
348 $item->form_key .= '_id';
349 }
350 $item->type = 'textfield';
351 $item->extra = array(
352 'maxlength' => 5,
353 'width' => 4,
354 'private' => 0,
355 );
356 webform_component_update((array) $item);
357 }
358
359 // Update submission results - change numeric state/prov ids to abbreviations
360 if ($submitted) {
361 $where = implode(' OR ', $submitted);
362 civicrm_initialize();
363 module_load_include('inc', 'webform_civicrm', 'includes/utils');
364 $db = db_query('SELECT DISTINCT data FROM {webform_submitted_data} WHERE ' . $where);
365 foreach ($db as $row) {
366 if ($row->data && is_numeric($row->data)) {
367 db_query('UPDATE {webform_submitted_data} SET data = \'' . wf_crm_state_abbr($row->data) . '\' WHERE data = ' . $row->data . ' AND (' . $where . ')');
368 }
369 }
370 }
371
372 return st('Upgrade successful. Note: If you upgraded via drush you will now need to clear all caches with the command drush cc');
373 }
374
375 /**
376 * Group participant options into a single array.
377 */
378 function webform_civicrm_update_6204() {
379 module_load_include('inc', 'webform_civicrm', 'includes/utils');
380
381 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
382 foreach ($db as $form) {
383 $data = unserialize($form->data);
384 if (isset($data['event_type'])) {
385 $data['reg_options'] = array(
386 'event_type' => $data['event_type'],
387 'show_past_events' => wf_crm_aval($data, 'show_past_events', 0),
388 );
389 $form = (array) $form;
390 $form['data'] = $data;
391 drupal_write_record('webform_civicrm_forms', $form, 'nid');
392 }
393 }
394
395 return st('Upgrade successful. Note: If you upgraded via drush you will now need to clear all caches with the command drush cc');
396 }
397
398 /**
399 * Upgrade for CiviCRM 4.1 compatibility.
400 */
401 function webform_civicrm_update_6205() {
402 civicrm_initialize();
403 module_load_include('inc', 'webform_civicrm', 'includes/utils');
404 module_load_include('inc', 'webform', 'includes/webform.components');
405 $db = db_query("SELECT * FROM {webform_component} WHERE form_key LIKE 'civicrm_%%_other_tags' OR form_key LIKE 'civicrm_%%_other_group%%'");
406 $tag = $group = array();
407 foreach ($db as $row) {
408 if ($pieces = wf_crm_explode_key($row->form_key)) {
409 list( , $c, $ent, $n, $table, $name) = $pieces;
410 if ($name == 'groups') {
411 if (empty($group[$row->nid][$c])) {
412 $group[$row->nid][$c] = array('value' => '');
413 }
414 continue;
415 }
416 $type = $name == 'groups_hidden' ? 'group' : 'tag';
417 ${$type}[$row->nid][$c] = (array) $row;
418 }
419 }
420 db_query("UPDATE {webform_component} SET form_key = REPLACE(form_key, 'other_groups', 'other_group') WHERE form_key LIKE 'civicrm_%%_other_groups'");
421 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
422 foreach ($db as $form) {
423 $nid = $form->nid;
424 $data = unserialize($form->data);
425 if (!empty($data['activity'][1])) {
426 $data['activity'][1]['details'] = array(
427 'view_link' => !empty($data['activity'][1]['add_link']) ? 'view_link' : 0,
428 );
429 $data['activity'][1]['existing_activity_status'] = empty($data['activity'][1]['existing_activity_status']) ? array() : array($data['activity'][1]['existing_activity_status']);
430 }
431 foreach ($data['contact'] as &$con) {
432 if (empty($con['contact'][1]['contact_sub_type'])) {
433 $con['contact'][1]['contact_sub_type'] = array();
434 }
435 else {
436 $con['contact'][1]['contact_sub_type'] = (array) $con['contact'][1]['contact_sub_type'];
437 }
438 }
439 if (!empty($tag[$nid]) || !empty($group[$nid])) {
440 $node = node_load($nid);
441 $both = wf_crm_aval($tag, $nid, array()) + wf_crm_aval($group, $nid, array());
442 foreach ($both as $c => $val) {
443 $data['contact'][$c]['number_of_other'] = 1;
444 if (!empty($tag[$nid][$c])) {
445 $tag_ids = array();
446 $tag_names = drupal_explode_tags($tag[$nid][$c]['value']);
447 foreach ($tag_names as $t) {
448 $result = wf_civicrm_api('tag', 'get', array('name' => $t));
449 if ($tid = wf_crm_aval($result, 'id')) {
450 $tag_ids[$tid] = $tid;
451 }
452 }
453 $data['contact'][$c]['other'][1]['tag'] = $tag_ids;
454 webform_component_delete($node, $tag[$nid][$c]);
455 }
456 if (!empty($group[$nid][$c]['form_key'])) {
457 if (empty($group[$nid][$c]['value'])) {
458 $data['contact'][$c]['other'][1]['group'] = array();
459 }
460 else {
461 $data['contact'][$c]['other'][1]['group'] = drupal_map_assoc(explode(',', $group[$nid][$c]['value']));
462 }
463 webform_component_delete($node, $group[$nid][$c]);
464 }
465 }
466 }
467 $form->data = $data;
468 drupal_write_record('webform_civicrm_forms', $form, 'nid');
469 }
470
471 return st('Hidden tag and group fields have been removed, those options are now integrated into the CiviCRM tab of webforms.');
472 }
473
474 /**
475 * Note: There are differences in how contact references and relationships work in the 3.x branch.
476 * Read the upgrade instructions at http://drupal.org/node/1615380
477 */
478 function webform_civicrm_update_7300() {
479 module_load_include('inc', 'webform_civicrm', 'includes/wf_crm_admin_form');
480 module_load_include('inc', 'webform_civicrm', 'includes/contact_component');
481 module_load_include('inc', 'webform', 'includes/webform.components');
482 civicrm_initialize();
483 $fields = wf_crm_get_fields();
484 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
485 foreach ($db as $form) {
486 $form = (array) $form;
487 $form['data'] = unserialize($form['data']);
488 $data = &$form['data'];
489 $node = node_load($form['nid']);
490 $enabled = wf_crm_enabled_fields($node);
491 // Add existing contact field
492 $field = $fields['contact_existing'];
493 $field['nid'] = $form['nid'];
494 $field['form_key'] = 'civicrm_1_contact_1_contact_existing';
495 $field['weight'] = -255;
496 $field['extra']['default'] = $form['contact_matching'] ? 'user' : '';
497 $field['extra']['hide_fields'] = array();
498 wf_crm_admin_form::insertComponent($field, $enabled, $form);
499
500 // Activity target
501 if (!empty($data['activity'][1]['activity'])) {
502 foreach ($data['contact'] as $c => $contact) {
503 if (!empty($contact['activity_target'])) {
504 $data['activity'][1]['activity'][1]['target_contact_id'][$c] = $c;
505 }
506 }
507 }
508
509 // Update relationship type ids
510 foreach ($data['contact'] as $c => &$contact) {
511 if (!empty($contact['relationship'])) {
512 foreach ($contact['relationship'] as $n => $rel) {
513 if (!empty($rel['relationship_type_id'])) {
514 $r_types = wf_crm_field_options(array('form_key' => "civicrm_{$c}_contact_{$n}_relationship_relationship_type_id"), 'config_form', $data);
515 $type = $rel['relationship_type_id'];
516 list($r, $a_b) = explode('_', $type);
517 if (!isset($r_types[$type]) && isset($r_types[$r . '_r'])) {
518 $contact['relationship'][$n]['relationship_type_id'] = $r . '_r';
519 }
520 // Change current employer rel
521 if ($r == 'ce') {
522 // Get regular employer rel type
523 foreach (wf_crm_get_relationship_types() as $e_type => $r) {
524 if ($r['name_a_b'] == 'Employee of') {
525 break;
526 }
527 }
528 $contact['relationship'][$n]['relationship_type_id'] = $e_type . '_' . $a_b;
529 $i = $a_b == 'a' ? $c : $n;
530 $e = $a_b == 'a' ? $n : $c;
531 $data['contact'][$i]['contact'][1]['employer_id'] = $e;
532 if ($n == 1 && empty($enabled["civicrm_{$c}_contact_1_contact_existing"])) {
533 // Add existing contact field
534 $field = $fields['contact_existing'];
535 $field['name'] = $a_b == 'a' ? t('Employee') : t('Employer');
536 $field['nid'] = $form['nid'];
537 $field['weight'] = -255;
538 $field['form_key'] = "civicrm_{$c}_contact_1_contact_existing";
539 $field['extra']['widget'] = 'hidden';
540 $field['extra']['default'] = 'relationship';
541 $field['extra']['default_relationship'] = array($e_type . '_' . $a_b);
542 $field['extra']['hide_fields'] = array();
543 wf_crm_admin_form::insertComponent($field, $enabled, $form);
544 }
545 }
546 }
547 }
548 }
549 }
550
551 // Activity assignee & case manager
552 foreach (array('activity' => 'assignee_contact_id', 'case' => 'creator_id') as $table => $name) {
553 if (!empty($data[$table][1][$table][1][$name])) {
554 $val = &$data[$table][1][$table][1][$name];
555 if ($val[0] == '#') {
556 $val = substr($val, 1);
557 }
558 else {
559 // Create contact
560 $c = count($data['contact']) + 1;
561 $data['contact'][$c] = array(
562 'contact' => array(1 => array(
563 'contact_type' => 'individual',
564 'contact_sub_type' => array(),
565 )),
566 );
567 // Set existing contact id
568 $field = $fields['contact_existing'] + _webform_defaults_civicrm_contact();
569 $field['nid'] = $form['nid'];
570 $field['form_key'] = 'civicrm_' . $c . '_contact_1_contact_existing';
571 $field['weight'] = 255;
572 $field['name'] = $table == 'case' ? st('Case Manager') : st('Activity Assignee');
573 $field['extra']['widget'] = 'hidden';
574 $field['extra']['default'] = 'contact_id';
575 $field['extra']['default_contact_id'] = $val;
576 $val = $c;
577 webform_component_insert($field);
578 }
579 }
580 }
581 // Update some fields
582 foreach ($enabled as $key => $field_id) {
583 list( , $c, $ent, $n, $table, $name) = wf_crm_explode_key($key);
584 $field = $node->webform['components'][$field_id];
585 // Convert ContactRef fields to real contacts
586 if (wf_crm_aval($fields, "{$table}_$name:data_type") == 'ContactReference') {
587 // No big changes if selecting from webform contacts, just strip the # symbols
588 if (empty($field['extra']['civicrm_group'])) {
589 $field['value'] = str_replace('#', '', $field['value']);
590 // Hidden fields are no longer needed for referencing a webform contact
591 if ($field['type'] == 'hidden') {
592 $data[$ent][$c][$table][$n][$name] = $field['value'];
593 webform_component_delete($node, $field);
594 continue;
595 }
596 if (!empty($field['extra']['items'])) {
597 $old = wf_crm_str2array($field['extra']['items']);
598 $new = array();
599 foreach ($old as $k => $v) {
600 $new[substr($k, 1)] = $v;
601 }
602 $field['extra']['items'] = wf_crm_array2str($new);
603 }
604 }
605 // Big changes if selecting contact by group!
606 elseif ($field['type'] === 'select') {
607 $group = $field['extra']['civicrm_group'];
608 // Get a sample from the group for a hint of this contact's type
609 $result = wf_civicrm_api('contact', 'get', array('rowCount' => 1, 'group' => array($group => 1), 'return.contact_type' => 1));
610 $type = 'individual';
611 if (!empty($result['values'])) {
612 $result = array_pop($result['values']);
613 $type = strtolower($result['contact_type']);
614 }
615 // Create contact
616 $c = count($data['contact']) + 1;
617 $data['contact'][$c] = array(
618 'contact' => array(1 => array(
619 'contact_type' => $type,
620 'contact_sub_type' => array(),
621 )),
622 );
623 $data[$ent][$c][$table][$n][$name] = $c;
624 // Change this field type from select to civicrm_contact
625 $field['form_key'] = 'civicrm_' . $c . '_contact_1_contact_existing';
626 $field['type'] = 'civicrm_contact';
627 $field['extra'] = $fields['contact_existing']['extra'];
628 $field['extra']['widget'] = 'select';
629 $field['extra']['none_prompt'] = t('- None Found -');
630 $default = _webform_defaults_civicrm_contact();
631 $field['extra']['filters'] = $default['extra']['filters'];
632 $field['extra']['filters']['group'] = array($group);
633 }
634 webform_component_update($field);
635 }
636 // Update relationship type ids
637 elseif ($name == 'relationship_type_id') {
638 $r_types = wf_crm_field_options($field, '', $data);
639 if (!empty($field['extra']['items'])) {
640 $old = wf_crm_str2array($field['extra']['items']);
641 $new = array();
642 foreach ($old as $k => $v) {
643 if (!isset($r_types[$k])) {
644 list($r, $a_b) = explode('_', $k);
645 if (isset($r_types[$r . '_r'])) {
646 $k = $r . '_r';
647 }
648 else {
649 continue;
650 }
651 }
652 $new[$k] = $v;
653 }
654 $field['extra']['items'] = wf_crm_array2str($new);
655 }
656 // Default value
657 if (!empty($field['value'])) {
658 list($r, $a_b) = explode('_', $field['value']);
659 if (isset($r_types[$r . '_r'])) {
660 $field['value'] = $r . '_r';
661 }
662 }
663 webform_component_update($field);
664 }
665 }
666 drupal_write_record('webform_civicrm_forms', $form, 'nid');
667 }
668 db_drop_field('webform_civicrm_forms', 'contact_matching');
669
670 $msg = st('There are important changes in Webform CiviCRM 3 that require your attention. Read the upgrade instructions at http://drupal.org/node/1615380 and review your existing webforms to ensure they are working as expected. Enjoy the new contact matching features!');
671 drupal_set_message($msg, 'warning');
672 return $msg;
673 }
674
675 /**
676 * Update case features.
677 */
678 function webform_civicrm_update_7301() {
679 module_load_include('inc', 'webform_civicrm', 'includes/utils');
680
681 $db = db_query("SELECT * FROM {webform_civicrm_forms}");
682 foreach ($db as $form) {
683 $data = unserialize($form->data);
684 if (!empty($data['case'][1])) {
685 $data['case'][1]['existing_case_status'] = (array) wf_crm_aval($data['case'][1], 'case:1:status_id');
686 $data['case'][1]['case'][1]['client_id'] = 1;
687 $form->data = $data;
688 drupal_write_record('webform_civicrm_forms', $form, 'nid');
689 }
690 }
691
692 return st('Upgrade successful.');
693 }
694
695 /**
696 * This update will modify all "Existing Contact" fields to enable the "Enforce Permissions" setting.
697 * Earlier versions of this module did not make it sufficiently clear that this setting should almost always be enabled, and contained bugs that were sometimes worked-around by disabling the setting.
698 * If you have configured custom contact permissions that maintain security without needing "Enforce Permissions" to be enabled, please review your webforms and disable it as needed (with care).
699 */
700 function webform_civicrm_update_7302() {
701 db_update('webform_component')
702 ->condition('type', 'civicrm_contact')
703 ->expression('extra', 'REPLACE(extra, \'"check_permissions";i:0\', \'"check_permissions";i:1\')')
704 ->execute();
705
706 return st('All "Existing Contact" fields have been modified to enable the "Enforce Permissions" setting. In most cases you do not need to further modify this setting. You may wish to review your webforms if you are an advanced user and have configured custom contact permissions.');
707 }
708
709 /**
710 * Add column for contribution_id to webform_civicrm_submissions table.
711 */
712 function webform_civicrm_update_7400() {
713 $field = array(
714 'type' => 'int',
715 'unsigned' => TRUE,
716 'not null' => TRUE,
717 'default' => 0,
718 'description' => 'FK to {civicrm_contribution} id',
719 );
720 db_add_field('webform_civicrm_submissions', 'contribution_id', $field);
721 }
722
723 /**
724 * Move civicrm entity ids into a serialized array for each submission.
725 */
726 function webform_civicrm_update_7401(&$batch) {
727 // Initialize batch api and create new field
728 if (!isset($batch['progress'])) {
729 $field = array(
730 'type' => 'text',
731 'serialize' => TRUE,
732 'description' => 'Array of entity ids for this submission',
733 );
734 db_add_field('webform_civicrm_submissions', 'civicrm_data', $field);
735 $batch['progress'] = $batch['current_sid'] = 0;
736 $batch['max'] = db_query('SELECT COUNT(sid) FROM {webform_civicrm_submissions}')->fetchField();
737 }
738
739 // Process the next group of submissions.
740 $result = db_query("SELECT * FROM {webform_civicrm_submissions} WHERE sid > {$batch['current_sid']} ORDER BY sid LIMIT 500");
741 foreach ($result as $row) {
742 $data = array();
743 if ($row->activity_id) {
744 $data['activity'] = array(1 => array('id' => $row->activity_id));
745 }
746 if ($row->contribution_id) {
747 $data['contribution'] = array(1 => array('id' => $row->contribution_id));
748 }
749 db_update('webform_civicrm_submissions')
750 ->condition('sid', $row->sid)
751 ->fields(array('civicrm_data' => serialize($data)))
752 ->execute();
753
754 $batch['current_sid'] = $row->sid;
755 $batch['progress']++;
756 }
757
758 // Update batch progress bar
759 $batch['#finished'] = ($batch['progress'] >= $batch['max']) ? TRUE : ($batch['progress'] / $batch['max']);
760
761 // Drop old fields when done
762 if ($batch['#finished'] === TRUE) {
763 db_drop_field('webform_civicrm_submissions', 'activity_id');
764 db_drop_field('webform_civicrm_submissions', 'contribution_id');
765 }
766 }
767
768 /**
769 * Upgrade schema to handle multiple activities and cases
770 */
771 function webform_civicrm_update_7402() {
772 civicrm_initialize();
773 module_load_include('inc', 'webform', 'includes/webform.components');
774 module_load_include('inc', 'webform_civicrm', 'includes/utils');
775 $forms = db_query("SELECT * FROM {webform_civicrm_forms}");
776 foreach ($forms as $form) {
777 $data = $orig = unserialize($form->data);
778 if (isset($data['activity'][1]['activity'][1])) {
779 $data['activity']['number_of_activity'] = 1;
780 // Fix misnamed fields
781 foreach (array('campaign_id', 'survey_id') as $field) {
782 if (!empty($data['activity'][1]['activity'][1]['activity_' . $field])) {
783 $data['activity'][1]['activity'][1][$field] = $data['activity'][1]['activity'][1]['activity_' . $field];
784 unset($data['activity'][1]['activity'][1]['activity_' . $field]);
785 }
786 }
787 // Set "file on case" settings if activity and case are both set
788 // Previous behavior was to always file on case
789 if (!empty($data['case'][1]['case'][1]['case_type_id'])) {
790 $data['activity'][1]['case_type_id'] = $data['case'][1]['case'][1]['case_type_id'];
791 $data['activity'][1]['case_status_id'] = (array) wf_crm_aval($data['case'][1]['case'][1], 'status_id');
792 $data['activity'][1]['case_contact_id'] = wf_crm_aval($data['case'][1]['case'][1], 'client_id', 1);
793 }
794 // Create hidden field for activity subject to replace the removed "default subject" setting
795 $fid = 'civicrm_1_activity_1_activity_subject';
796 if (!(db_query("SELECT nid FROM {webform_component} WHERE form_key = '$fid' AND nid = {$form->nid}")->fetchField())) {
797 $component = array(
798 'form_key' => $fid,
799 'nid' => $form->nid,
800 'value' => $data['activity'][1]['activity'][1]['subject'],
801 'type' => 'hidden',
802 ) + wf_crm_get_field('activity_subject');
803 webform_component_defaults($component);
804 webform_component_insert($component);
805 }
806 }
807 if (isset($data['case'][1]['case'][1])) {
808 $data['case']['number_of_case'] = 1;
809 }
810 if ($data !== $orig) {
811 db_update('webform_civicrm_forms')
812 ->fields(array('data' => serialize($data)))
813 ->condition('nid', $form->nid)
814 ->execute();
815 }
816 }
817 }