/[drupal]/contributions/modules/multiforms/multiforms_ui.module
ViewVC logotype

Contents of /contributions/modules/multiforms/multiforms_ui.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.65 - (show annotations) (download) (as text)
Tue Jun 23 20:45:45 2009 UTC (5 months ago) by mcantelon
Branch: MAIN
CVS Tags: HEAD
Changes since 1.64: +7 -2 lines
File MIME type: text/x-php
Made closing message into a theme function.
1 <?php
2 // $Id: multiforms_ui.module,v 1.64 2008/12/17 18:11:29 mcantelon Exp $
3
4 /**
5 * Implementation of hook_help().
6 */
7 function multiforms_ui_help($section = '') {
8
9 $output = '';
10
11 switch ($section) {
12 case 'admin/modules#description':
13 $output = t('Provides multi-page form UI functionality.');
14 break;
15
16 case "admin/help":
17 $output = t('Provides multi-page form UI functionality.');
18 break;
19
20 case 'node/add#multiforms':
21 $output = t('A multiform allows you to create multi-page forms to collect data from users.');
22 break;
23 }
24
25 return $output;
26 }
27
28 /**
29 * Implementation of hook_perm().
30 */
31 function multiforms_ui_perm() {
32
33 return array('access multiform content');
34 }
35
36 /**
37 * Implementation of hook_menu().
38 */
39 function multiforms_ui_menu($may_cache) {
40
41 $items = array();
42
43 if (!$may_cache) {
44
45 if (arg(0) == 'node' && is_numeric(arg(1))) {
46
47 $node = node_load(arg(1));
48
49 if ($node->nid && $node->type == 'multiforms') {
50
51 $items[] = array('path' => 'node/'. arg(1) .'/multiform',
52 'callback' => 'multiforms_ui_dispatch',
53 'type' => MENU_CALLBACK_CUSTOM,
54 'access' => user_access('access multiform content')
55 );
56 }
57 }
58 }
59
60 return $items;
61 }
62
63 function multiforms_ui_access($op) {
64
65 switch ($op) {
66 case 'create':
67 return user_access('access multiform administration');
68 }
69 }
70
71 function multiforms_ui_view(&$node, $teaser, $page, $links) {
72
73 if (user_access('access multiform content') || $teaser) {
74
75 // If submission forms hasn't yet been submitted and viewing as page, unset submission ID
76 // (in case user was previously editing a submission, then aborted before completion)
77 if (!sizeof($_POST['edit']) && $page) {
78
79 unset($_SESSION['multiforms_submission_id']);
80 }
81
82 // determine ID of multiform data
83 $multiform_id = _multiforms_get_multiform_id_from_nid($node->nid);
84
85 // if in teaser mode and the node has a predefined teaser, then don't render form
86 if (!$teaser || !$node->teaser) {
87
88 $node->content['multiforms'] = array('#value' => multiforms_view_render($multiform_id, $teaser));
89 }
90
91 $node = node_prepare($node, $teaser);
92
93 $node->content['multiforms']['#value'] = str_replace(
94 '{multiform form html}',
95 drupal_get_form('multiforms_view', $multiform_id),
96 $node->content['multiforms']['#value']
97 );
98
99 return $node;
100 }
101 else {
102
103 drupal_access_denied();
104 }
105 }
106
107 function multiforms_view($multiform_id) {
108 return _multiforms_render_section_fields(0 - $multiform_id, 'multiforms_view', $multiform_id);
109 }
110
111 function multiforms_ui_form(&$node, &$param) {
112
113 $form['title'] = array(
114 '#type'=> 'textfield',
115 '#title' => t('Title'),
116 '#required' => TRUE,
117 '#default_value' => $node->title
118 );
119
120 $form['body_filter']['body'] = array('#type' => 'textarea',
121 '#title' => t('Body'),
122 '#default_value' => $node->body,
123 '#rows' => 20
124 );
125
126 $form['body_filter']['format'] = filter_form($node->format);
127
128 return $form;
129 }
130
131 function multiforms_ui_delete(&$node) {
132
133 if (function_exists('multiforms_admin_delete')) {
134
135 multiforms_admin_delete(_multiforms_get_multiform_id_from_nid($node->nid));
136 }
137 else {
138
139 drupal_set_message(t('Please contact your administrator! Multiforms cannot be deleted properly unless the main Multiforms module is enabled.'), 'error');
140 }
141 }
142
143 function multiforms_node_info() {
144
145 return array(
146 'multiforms' => array('name' => t('multiform'),
147 'module' => 'multiforms_ui',
148 'description' => t('A multiform is a form, or series of forms, for collecting data from users.')
149 )
150 );
151 }
152
153 function multiforms_ui_dispatch() {
154
155 drupal_set_title(_multiforms_ui_get_title());
156
157 $command_arg = arg(3);
158 $subcommand_arg = arg(4);
159
160 switch ($command_arg) {
161
162 case 'sections':
163 multiforms_section_list_page();
164 break;
165
166 case 'section':
167 multiforms_view_section_page($subcommand_arg);
168 break;
169
170 case 'complete':
171 multiforms_complete_page();
172 break;
173
174 case 'share':
175 multiforms_share_page($subcommand_arg);
176 break;
177
178 case 'thanks':
179 multiforms_thanks_page();
180 break;
181 }
182 }
183
184 function _multiforms_ui_get_title() {
185
186 $title = _multiforms_get_table_property('node', 'nid', arg(1), 'title');
187 drupal_set_title($node->title);
188
189 return $node->title;
190 }
191
192 function _multiforms_ui_check_close_status($multiform_id) {
193
194 $closed = FALSE;
195
196 if (_multiforms_get_property($multiform_id, 'multiform_closing_enabled')) {
197
198 $closing_date = str_replace('-', '/', _multiforms_get_property($multiform_id, 'multiform_closing_date'));
199 $current_date = date('n/j/Y');
200
201 if ($closing_date == $current_date) {
202
203 // if today is the closing date and no time is set, close...
204 // if a time is set, go by the time
205 $closing_time = _multiforms_get_property($multiform_id, 'multiform_closing_time');
206
207 if ($closing_time) {
208
209 if ($closing_time <= date('H:i:s')) {
210
211 $closed = TRUE;
212 }
213 }
214 else {
215
216 $closed = TRUE;
217 }
218 }
219 elseif (_multiforms_date_array_greater($current_date, $closing_date)) {
220
221 $closed = TRUE;
222 }
223 }
224
225 return $closed;
226 }
227
228 function multiforms_view_render($multiform_id, $teaser = FALSE) {
229
230 // Check to see if multiform is closed
231 $closed = _multiforms_ui_check_close_status($multiform_id);
232
233 // If closed, return closing text
234 if ($closed && $closing_text = t(_multiforms_get_property($multiform_id, 'multiform_closing_text'))) {
235
236 return $closing_text;
237 }
238
239 // Get intro text from node body
240 $nid = _multiforms_get_property($multiform_id, 'nid');
241 $node = node_load($nid);
242
243 if (!$teaser) {
244
245 if (!$closed) {
246
247 $output .= '{multiform form html}';
248
249 $output .= t(_multiforms_get_property($multiform_id, 'multiform_footer'));
250 }
251 else {
252
253 $output .= theme('multiforms_ui_closed_message');
254 }
255 }
256
257 return $output;
258 }
259
260 function theme_multiforms_ui_closed_message() {
261
262 return t('This contest is now closed.') .'<p></p>';
263 }
264
265 function multiforms_view_submit($form_id, $form_values) {
266
267 $is_duplicate = FALSE;
268
269 $field_values = $form_values['multiforms_view'];
270
271 // If a submission ID hasn't been set and the user isn't hitting the back button from
272 // the completion, create a new submission
273 if (!$_SESSION['multiforms_submission_id']) {
274
275 $submission_metadata = _multiforms_ui_view_submit_metadata($field_values);
276
277 // If primary section has fields, check for duplicate submissions
278 if ($submission_metadata['dynamic_field_count']) {
279
280 // Check to see if submission is a duplicate
281 $is_duplicate = _multiforms_ui_view_submit_duplicate_check(
282 $field_values['multiform_id'],
283 $submission_metadata['unique_key_fields'],
284 $submission_metadata['submission_unique_value']
285 );
286 }
287
288 // If no duplicates have been detected, create submission and set submission ID
289 if (!$is_duplicate) {
290
291 // Create submission record in database
292 _multiforms_ui_view_submit_create_submission(
293 $field_values['multiform_id'],
294 $submission_metadata['submission_unique_value']
295 );
296
297 // Note current submission ID in session
298 _multiforms_ui_view_submit_set_session();
299
300 // If no duplication, store dynamic submission fields
301 _multiforms_submit_section_data(0 - $field_values['multiform_id'], $field_values);
302 }
303 }
304
305 // If submission isn't a duplicate, trigger actions and list sections (or skip to completion)
306 if (!$is_duplicate) {
307
308 // Submit actions, if any
309 multiforms_view_submit_actions(0 - $field_values['multiform_id'], 'section', 'submit');
310
311 $query = "SELECT count(multiform_section_id) AS total_sections FROM {". MULTIFORM_SECTION_TABLE ."} WHERE multiform_section_multiform_id='%d'";
312
313 $result = db_query($query, $field_values['multiform_id']);
314 if ($result) {
315
316 if ($row = db_fetch_object($result)) {
317
318 return _multiforms_ui_primary_submission_response_path($row->total_sections, arg(1), $field_values['multiform_id']);
319
320 }
321 }
322 }
323 else {
324
325 drupal_set_message(t('You have already filled out this form.'), 'error');
326 }
327 }
328
329 function _multiforms_ui_primary_submission_response_path($total_sections, $nid, $multiform_id) {
330
331 if ($total_sections) {
332
333 $flow_type = _multiforms_get_property($multiform_id, 'multiform_flow_type');
334
335 if ($flow_type == MULTIFORMS_FLOW_TYPE_NORMAL) {
336
337 return 'node/'. $nid .'/multiform/sections';
338 }
339 elseif ($flow_type == MULTIFORMS_FLOW_TYPE_LINEAR) {
340
341 return 'node/'. $nid .'/multiform/section/'. _multiforms_ui_first_section_id($multiform_id);
342 }
343 }
344 else {
345
346 // multiform has no sections, so skip to completion
347 return 'node/'. $nid .'/multiform/complete';
348 }
349 }
350
351 function _multiforms_ui_first_section_id($multiform_id) {
352
353 $query = "SELECT multiform_section_id FROM {". MULTIFORM_SECTION_TABLE ."} \r
354 WHERE multiform_section_multiform_id='%d' \r
355 ORDER BY multiform_section_sortorder ASC \r
356 LIMIT 1";
357
358 $params = array($multiform_id);
359
360 $result = db_query($query, $params);
361
362 if ($result) {
363
364 if ($first_section = db_fetch_object($result)) {
365
366 return $first_section->multiform_section_id;
367 }
368 }
369 }
370
371 function _multiforms_ui_next_section_id($multiform_id, $current_section_id) {
372
373 return _multiforms_ui_section_id_in_sequence($multiform_id, $current_section_id, 'ASC');
374 }
375
376 function _multiforms_ui_previous_section_id($multiform_id, $current_section_id) {
377
378 return _multiforms_ui_section_id_in_sequence($multiform_id, $current_section_id, 'DESC');
379 }
380
381 function _multiforms_ui_section_id_in_sequence($multiform_id, $current_section_id, $sort_direction = 'ASC') {
382
383 $query = "SELECT multiform_section_id FROM {". MULTIFORM_SECTION_TABLE ."} \r
384 WHERE multiform_section_multiform_id='%d' \r
385 ORDER BY multiform_section_sortorder ". $sort_direction;
386
387 $params = array($multiform_id);
388
389 $result = db_query($query, $params);
390
391 if ($result) {
392
393 while ($section = db_fetch_object($result)) {
394
395 if ($last_section_id == $current_section_id) {
396
397 // Section ID in sequence found
398 return $section->multiform_section_id;
399 }
400
401 $last_section_id = $section->multiform_section_id;
402 }
403 }
404
405 // Section ID in sequence not found
406 return FALSE;
407 }
408
409 function _multiforms_ui_view_submit_metadata($field_values) {
410
411 // see if dynamic submission fields exist for this multiform
412 $dynamic_field_count = 0;
413 $submission_unique_value = '';
414 $unique_key_fields = 0;
415
416 $query = "SELECT * FROM {" . MULTIFORM_FIELD_TABLE . "} WHERE multiform_field_section_id='%d'";
417
418 $result = db_query($query, 0 - $field_values['multiform_id']);
419
420 if ($result) {
421
422 while ($row = db_fetch_object($result)) {
423
424 switch ($row->multiform_field_type) {
425
426 case 'unique_text':
427 $submission_unique_value .= trim($field_values[$row->multiform_field_id]) . '|';
428 $unique_key_fields++;
429 break;
430
431 case 'unique_phone':
432 $submission_unique_value .= _multiforms_filter_phone_number($field_values[$row->multiform_field_id]) . '|';
433 $unique_key_fields++;
434 break;
435
436 default:
437 break;
438 }
439
440 $dynamic_field_count++;
441 }
442 }
443
444 return array(
445 'dynamic_field_count' => $dynamic_field_count,
446 'submission_unique_value' => $submission_unique_value,
447 'unique_key_fields' => $unique_key_fields
448 );
449 }
450
451 function _multiforms_ui_view_submit_duplicate_check($multiform_id, $unique_key_fields, $submission_unique_value) {
452
453 if ($unique_key_fields) {
454
455 $query = "SELECT multiform_submission_id FROM {". MULTIFORM_SUBMISSION_TABLE ."} \r
456 WHERE multiform_submission_parent_multiform_id='%d' AND \r
457 multiform_submission_unique_key='%s'";
458
459 $result = db_query($query, $multiform_id, $submission_unique_value);
460
461 if ($result) {
462
463 if ($row = db_fetch_object($result)) {
464
465 return TRUE;
466 }
467 }
468 }
469
470 return FALSE;
471 }
472
473 function _multiforms_ui_view_submit_create_submission($multiform_id, $submission_unique_value) {
474
475 $query = "INSERT INTO {" . MULTIFORM_SUBMISSION_TABLE . "} \r
476 (multiform_submission_parent_multiform_id, \r
477 multiform_submission_ip, \r
478 multiform_submission_datetime, \r
479 multiform_submission_unique_key) \r
480 VALUES ('%d', '%s', now(), '%s')";
481
482 $result = db_query($query,
483 $multiform_id,
484 $_SERVER['REMOTE_ADDR'],
485 $submission_unique_value
486 );
487 }
488
489 function _multiforms_ui_view_submit_set_session() {
490
491 $query = "SELECT multiform_submission_id FROM {". MULTIFORM_SUBMISSION_TABLE ."} ORDER BY multiform_submission_id DESC LIMIT 1";
492
493 $result = db_query($query);
494
495 if ($result) {
496
497 if ($row = db_fetch_object($result)) {
498
499 if (!$_SESSION['multiforms_submission_id']) {
500
501 $_SESSION['multiforms_submission_id'] = $row->multiform_submission_id;
502 }
503 }
504 }
505 }
506
507 function multiforms_section_list_page() {
508
509 $multiform_id = _multiforms_get_multiform_id_from_nid(arg(1));
510
511 print theme('page', _multiforms_list_sections($multiform_id));
512 }
513
514 function _multiforms_list_sections($multiform_id) {
515
516 $output = '';
517 $rows = array();
518
519 $query = "SELECT * FROM {" . MULTIFORM_SECTION_TABLE . "} WHERE multiform_section_multiform_id='%d' ORDER BY multiform_section_sortorder ASC";
520
521 $result = db_query($query, $multiform_id);
522
523 if ($result) {
524
525 while ($row = db_fetch_object($result)) {
526
527 // Find number of fields in this section
528 $field_count = _multiforms_get_total_input_fields($multiform_id, $row->multiform_section_id);
529
530 // Find number of fields answered
531 $answered = _multiforms_list_section_fields_answered($multiform_id, $row->multiform_section_id);
532
533 $rows[] = array(
534 l($row->multiform_section_name, 'node/' . arg(1) . '/multiform/section/' . $row->multiform_section_id, array('class' => 'multiform_section_link')) . '&nbsp;',
535 $row->multiform_section_description .'&nbsp;',
536 $field_count . '&nbsp;',
537 $answered
538 );
539 }
540 }
541
542 return theme('multiforms_section_list', $multiform_id, $rows);
543 }
544
545 function _multiforms_list_section_fields_answered($multiform_id, $section_id) {
546
547 $answered = 0;
548
549 $submitted_values = _multiforms_return_existing_section_data($section_id);
550
551 reset($submitted_values);
552
553 while (list($key, $value) = each($submitted_values)) {
554
555 if (trim($value)) {
556
557 $answered++;
558 }
559 }
560
561 return $answered;
562 }
563
564 function theme_multiforms_section_list($multiform_id, $section_rows) {
565
566 $output = '';
567
568 if ($section_text = _multiforms_get_property($multiform_id, 'multiform_section_text')) {
569 $output .= t($section_text) .'<p></p>';
570 }
571
572 $headers = array(
573 'Section',
574 'Description',
575 'Questions',
576 'Answered'
577 );
578
579 $output .= theme_table($headers, $section_rows) .'<br />';
580
581 if (_multiforms_check_completion($multiform_id)) {
582
583 $output .= theme('multiforms_section_list_completion_form', $multiform_id);
584
585 }
586 else {
587
588 $output .= theme('multiforms_section_list_progress_display', $multiform_id);
589 }
590
591 $output .= _multiforms_get_property($multiform_id, 'multiform_section_footer');
592
593 return $output;
594 }
595
596 function theme_multiforms_section_list_completion_form($multiform_id) {
597
598 $output = '';
599
600 // show completion notification, if any
601 if ($end_notification = _multiforms_get_property($multiform_id, 'multiform_end_notification')) {
602
603 $output .= t($end_notification) .'<br></br>';
604 }
605
606 $output .= drupal_get_form('multiforms_complete_form', $multiform_id);
607
608 return $output;
609 }
610
611 function theme_multiforms_section_list_progress_display($multiform_id) {
612
613 $output = '';
614
615 $output .= t('Click on any of the above links to answer questions.') .'<br /><br />';
616 $output .= t('You have answered %answered of %possible possible questions.', array(
617 '%answered' => _multiforms_get_answered_fields($multiform_id),
618 '%possible' => _multiforms_get_total_input_fields($multiform_id)));
619 $output .= '<br /><br />';
620
621 return $output;
622 }
623
624 function multiforms_complete_form($multiform_id) {
625
626 $form = array();
627
628 $form['#action'] = url('node/'. arg(1) .'/multiform/complete/'. $multiform_id);
629
630 $multiform_name = _multiforms_get_property($multiform_id, 'multiform_name');
631 $form['#attributes'] = array(
632 'class' => 'multiform_finish_button multiform_finish_button_'. $multiform_name
633 );
634
635 $completion_submit_button_text = _multiforms_get_property($multiform_id, 'multiform_completion_submit_button_text');
636 $submit_value = ($completion_submit_button_text) ? $completion_submit_button_text : t('Finish');
637
638 $form['submit'] = array(
639 '#type' => 'submit',
640 '#value' => $submit_value
641 );
642
643 return $form;
644 }
645
646 function _multiforms_check_completion($multiform_id) {
647
648 $threshold = _multiforms_get_property($multiform_id, 'multiform_completion_threshold');
649
650 $answered = _multiforms_get_answered_fields($multiform_id);
651 $total = _multiforms_get_total_input_fields($multiform_id);
652
653 return (!$threshold || ($answered >= $threshold));
654 }
655
656 function _multiforms_get_answered_fields($multiform_id, $submission_id = 0) {
657
658 $total_answered = 0;
659
660 $query = "SELECT COUNT(f.multiform_field_id) AS field_count FROM {". MULTIFORM_TABLE ."} AS m \r
661 LEFT JOIN {". MULTIFORM_SECTION_TABLE ."} AS s ON m.multiform_id=s.multiform_section_multiform_id \r
662 LEFT JOIN {". MULTIFORM_FIELD_TABLE ."} AS f ON s.multiform_section_id=f.multiform_field_section_id \r
663 INNER JOIN {". MULTIFORM_FIELD_SUBMISSION_TABLE ."} AS fs ON f.multiform_field_id=fs.multiform_field_submission_parent_field_id \r
664 WHERE m.multiform_id='%d' AND fs.multiform_field_submission_parent_submission_id='%d' AND fs.multiform_field_submission_value != ''";
665
666 $submission_to_check = ($submission_id) ? $submission_id : $_SESSION['multiforms_submission_id'];
667
668 $result = db_query($query, $multiform_id, $submission_to_check);
669
670 if ($result) {
671
672 if ($row = db_fetch_object($result)) {
673
674 $total_answered = $row->field_count;
675 }
676 }
677
678 return $total_answered;
679 }
680
681 function _multiforms_add_draw_entry($multiform_id, $submission_id) {
682
683 $query = "INSERT INTO {". MULTIFORM_DRAW_ENTRY_TABLE ."} \r
684 (multiform_draw_entry_parent_multiform_id, \r
685 multiform_draw_entry_parent_submission_id) \r
686 VALUES ('%d', '%d')";
687
688 $result = db_query($query, $multiform_id, $submission_id);
689
690 if ($result) {
691
692 // Increment submission's draw entry counter
693 $query = "SELECT multiform_submission_draw_entry_counter FROM {". MULTIFORM_SUBMISSION_TABLE . "} \r
694 WHERE multiform_submission_id='%d'";
695
696 $result = db_query($query, $submission_id);
697
698 if ($result) {
699
700 if ($submission = db_fetch_object($result)) {
701
702 $counter = $submission->multiform_submission_draw_entry_counter + 1;
703
704 $query = "UPDATE {". MULTIFORM_SUBMISSION_TABLE . "} \r
705 SET multiform_submission_draw_entry_counter='%d' \r
706 WHERE multiform_submission_id='%d'";
707
708 $result = db_query($query, $counter, $submission_id);
709
710 if ($result) {
711 return TRUE;
712 }
713 }
714 }
715 }
716
717 return FALSE;
718 }
719
720 function multiforms_complete_page() {
721
722 $multiform_id = _multiforms_get_multiform_id_from_nid(arg(1));
723
724 $output = '';
725
726 if (_multiforms_check_completion($multiform_id)) {
727
728 $output .= t(_multiforms_end_text($multiform_id));
729
730 if (isset($_SESSION['multiforms_submission_id'])) {
731
732 // mark submission as closed
733 $query = "UPDATE {". MULTIFORM_SUBMISSION_TABLE ."} SET multiform_submission_status='complete', multiform_submission_total_answered='%d' \r
734 WHERE multiform_submission_id='%d'";
735
736 $result = db_query($query, _multiforms_get_answered_fields($multiform_id), $_SESSION['multiforms_submission_id']);
737
738 // if applicable, add to draw
739 if (_multiforms_get_property($multiform_id, 'multiform_draw_entry_on_submission')) {
740
741 _multiforms_add_draw_entry($multiform_id, $_SESSION['multiforms_submission_id']);
742 }
743 }
744
745 // if applicable, process actions
746 multiforms_view_submit_actions($multiform_id, 'multiform', 'complete');
747
748 // if applicable, show form for emailing friends about survey
749 if (_multiforms_get_property($multiform_id, 'multiform_email_allow')) {
750
751 $output .= drupal_get_form('multiforms_share', $multiform_id, $_SESSION['multiforms_submission_id']);
752
753 // take note that user has gone through completion...
754 // mark to prevent creation of new submission if back button is clicked
755 unset($_SESSION['multiforms_submission_id']);
756
757 print theme('page', $output);
758 }
759 else {
760
761 // take note that user has gone through completion...
762 // mark to prevent creation of new submission if back button is clicked
763 unset($_SESSION['multiforms_submission_id']);
764
765 multiforms_output_final_text($output);
766 }
767
768 }
769 else {
770
771 $threshold = _multiforms_get_property($multiform_id, 'multiform_completion_threshold');
772
773 $output .= t(
774 'Error: multiform not complete. %threshold questions need to be answered after the initial form is completed.',
775 array('%threshold' => $threshold)
776 );
777
778 print theme('page', $output);
779 }
780 }
781
782 function multiforms_output_final_text($text) {
783
784 $multiform_id = _multiforms_get_multiform_id_from_nid(arg(1));
785
786 $redirect_path = _multiforms_get_property($multiform_id, 'multiform_completion_redirect');
787
788 if ($redirect_path) {
789
790 drupal_set_message($text);
791 drupal_goto($redirect_path);
792 }
793 else {
794
795 print theme('page', $text);
796 }
797 }
798
799 function _multiforms_end_text($multiform_id) {
800
801 return ($end_text = _multiforms_get_property($multiform_id, 'multiform_end_text')) ?
802 t($end_text) : t(variable_get('multiform_default_completion_text', ''));
803 }
804
805 # session variable no longer lives, so optional submission ID is passed
806 function multiforms_share($multiform_id, $submission_id = '') {
807
808 $output = '';
809
810 $form_name = "multiforms_share";
811 $multiform_name = _multiforms_get_property($multiform_id, 'multiform_name');
812
813 $form = array(
814 '#action' => url('node/'. arg(1) .'/multiform/share/'. $multiform_id),
815 '#attributes' => array('class' => 'multiform multiform_email multiform_'. $multiform_name .' multiform_'. $multiform_name .'_email')
816 );
817
818 $form[$form_name] = array(
819 '#type' => 'fieldset',
820 '#title' => t($form_title),
821 '#tree' => TRUE
822 );
823
824 $form[$form_name]['submission_id'] = array(
825 '#type' => 'hidden',
826 '#default_value' => $submission_id
827 );
828
829 $form[$form_name]['from'] = array(
830 '#type' => 'fieldset',
831 '#title' => t('From Email Address'),
832 '#collapsible' => FALSE,
833 '#collapsed' => FALSE,
834 '#tree' => TRUE,
835 );
836
837 $form[$form_name]['from']['email'] = array(
838 '#type' => 'textfield',
839 '#title' => t('Your Email')
840 );
841
842 $form[$form_name]['to'] = array(
843 '#type' => 'fieldset',
844 '#title' => t('Friends to Email'),
845 '#collapsible' => FALSE,
846 '#collapsed' => FALSE,
847 '#tree' => TRUE,
848 );
849
850 for ($index = 0; $index < 5; $index++) {
851 $field_name = 'email'. ($index + 1);
852 $form[$form_name]['to'][$field_name] = array(
853 '#title' => t('Email Address') .' '. ($index + 1),
854 '#type' => 'textfield',
855 );
856 }
857
858 $form[$form_name]['submit'] = array(
859 '#type' => 'submit',
860 '#value' => t('Email')
861 );
862
863 $form[$form_name]['skip'] = array(
864 '#type' => 'markup',
865 '#value' => '<p>'. l(t('Skip'), 'node/'. arg(1) .'/multiform/thanks') .'</p>'
866 );
867
868 return $form;
869 }
870
871 function multiforms_share_submit($form_id, $form_data) {
872
873 $multiform_id = _multiforms_get_multiform_id_from_nid(arg(1));
874
875 $form_values = $form_data['multiforms_share'];
876
877 // get email subect and body
878 $query = "SELECT multiform_email_subject, multiform_email_body FROM {". MULTIFORM_TABLE ."} WHERE multiform_id='%d'";
879
880 $result = db_query($query, $multiform_id);
881
882 if ($result) {
883
884 if ($row = db_fetch_object($result)) {
885
886 // cycle through and send
887 for ($index = 0; $index < 5; $index++) {
888
889 $field_name = "email" . ($index + 1);
890
891 if ($form_values['to'][$field_name]) {
892
893 $from_address = ($form_values['from']['email']) ? $form_values['from']['email'] : variable_get('site_mail', '');
894
895 // send email
896 theme(
897 'multiforms_mail_referral',
898 $from_address,
899 $form_values['to'][$field_name],
900 $row->multiform_email_subject,
901 $row->multiform_email_body
902 );
903
904 // if multiform is being shared by someone who has submitted a form,
905 // add extra entries into the draw
906 if (
907 $form_values['submission_id'] &&
908 _multiforms_get_property($multiform_id, 'multiform_draw_entry_on_rererral')
909 ) {
910
911 _multiforms_add_draw_entry($multiform_id, $form_values['submission_id']);
912 }
913 }
914 }
915 }
916 }
917
918 return 'node/'. arg(1) .'/multiform/thanks';
919 }
920
921 function multiforms_thanks_page() {
922
923 $multiform_id = _multiforms_get_multiform_id_from_nid(arg(1));
924
925 multiforms_output_final_text(t(_multiforms_get_property($multiform_id, 'multiform_email_thanks')));
926 }
927
928 function theme_multiforms_mail_referral($from, $to, $subject, $body) {
929
930 // If mimemail is installed, use it for mailing
931 if (function_exists('mimemail')) {
932 mimemail($from, $to, $subject, $body);
933 }
934 else {
935 drupal_mail('multiforms_email_referral', $to, $subject, $body, $from);
936 }
937 }
938
939 function multiforms_share_page($multiform_id) {
940
941 print theme('page', drupal_get_form('multiforms_share', $multiform_id, $_SESSION['multiform_submission_id']));
942 }
943
944 function multiforms_view_section_page($section_id) {
945
946 $output = '';
947
948 $section_name = _multiforms_get_section_property($section_id, 'multiform_section_name');
949
950 if ($section_name) {
951 $output .= '<h1>'. t($section_name) .'</h1><p></p>';
952 }
953
954 $section_description = _multiforms_get_section_property($section_id, 'multiform_section_description');
955
956 if ($section_description) {
957 $output .= t($section_description) .'<p></p>';
958 }
959
960 $output .= drupal_get_form('multiforms_view_section', $section_id);
961
962 print theme('page', $output);
963 }
964
965 function multiforms_view_section($section_id) {
966
967 $multiform_id = _multiforms_get_multiform_id_from_nid (arg(1));
968
969 return _multiforms_render_section_fields($section_id, 'multiforms_view_section', $multiform_id);
970 }
971
972 function multiforms_view_section_submit($form_id, $form_values) {
973
974 $output = '';
975
976 $field_values = $form_values['multiforms_view_section'];
977
978 _multiforms_submit_section_data($field_values['section_id'], $field_values);
979
980 $multiform_id = _multiforms_get_multiform_id_from_nid(arg(1));
981
982 // Submit actions, if any
983 multiforms_view_submit_actions($field_values['section_id'], 'section', 'submit');
984
985 $next_section_id = _multiforms_ui_next_section_id($multiform_id, $field_values['section_id']);
986 $flow_type = _multiforms_get_property($multiform_id, 'multiform_flow_type');
987
988 if ($flow_type == MULTIFORMS_FLOW_TYPE_NORMAL) {
989
990 return 'node/'. arg(1) .'/multiform/sections';
991 }
992 elseif ($flow_type == MULTIFORMS_FLOW_TYPE_LINEAR) {
993
994 if ($next_section_id) {
995
996 return 'node/'. arg(1) .'/multiform/section/'. $next_section_id;
997 }
998 else {
999
1000 return 'node/'. arg(1) .'/multiform/complete';
1001 }
1002 }
1003 }
1004
1005 function multiforms_view_submit_actions($parent_id, $parent_type, $parent_trigger) {
1006
1007 $actions_performed = 0;
1008
1009 $query = "SELECT * FROM {" . MULTIFORM_ACTION_TABLE . "} \r
1010 WHERE multiform_action_parent_id='%d' AND multiform_action_parent_type='%s' AND multiform_action_parent_trigger='%s'";
1011
1012 $result = db_query($query, $parent_id, $parent_type, $parent_trigger);
1013
1014 if ($result) {
1015
1016 while ($action = db_fetch_object($result)) {
1017
1018 multiforms_perform_action($action);
1019 $actions_performed++;
1020 }
1021 }
1022
1023 return $actions_performed;
1024 }
1025
1026 function multiforms_perform_action($action) {
1027
1028 $action_function = $action->multiform_action_type;
1029 $params = unserialize($action->multiform_action_params);
1030 $node = node_load(arg(1));
1031
1032 return (function_exists($action_function)) ?
1033 $action_function('do', $params, $node) :
1034 '';
1035 }
1036
1037 function _multiforms_render_section_fields($section_id, $form_name = 'multiforms_view_section', $multiform_id = '') {
1038
1039 $field_values = _multiforms_return_existing_section_data($section_id);
1040
1041 $form = array();
1042
1043 $form[$form_name] = array(
1044 '#type' => 'fieldset',
1045 '#title' => t($form_title),
1046 '#tree' => TRUE
1047 );
1048
1049 if ($multiform_id) {
1050 $form[$form_name]['multiform_id'] = array(
1051 '#type' => 'hidden',
1052 '#value' => $multiform_id
1053 );
1054 }
1055
1056 $multiform_name =
1057 _multiforms_get_property(
1058 _multiforms_get_section_property($section_id, 'multiform_section_multiform_id'), 'multiform_name'
1059 );
1060 $form['#attributes'] = array(
1061 'class' => 'multiform multiform_section multiform_'. $multiform_name .' multiform_'. $multiform_name .'_section multiform_'. $multiform_name .'_section_'. $section_id
1062 );
1063
1064 $form[$form_name]['section_id'] = array(
1065 '#type' => 'hidden',
1066 '#value' => $section_id
1067 );
1068
1069 $type_handlers = module_invoke_all('multiforms_field_handlers');
1070
1071 $fields_output = 0;
1072 $show_submit = 0;
1073
1074 $query = "SELECT * FROM {". MULTIFORM_FIELD_TABLE ."} WHERE multiform_field_section_id='%d' ORDER BY multiform_field_sortorder";
1075
1076 $result = db_query($query, $section_id);
1077
1078 if ($result) {
1079
1080 while ($row = db_fetch_object($result)) {
1081
1082 $handler = $type_handlers[$row->multiform_field_type];
1083
1084 // Handle non-system field types
1085 if (function_exists($handler)) {
1086
1087 $form[$form_name][$row->multiform_field_id] = $handler('form', $field_values[$row->multiform_field_id], $row);
1088
1089 // This is a bit of a klude, to avoid exploding the stored value twice
1090 $form[$form_name][$row->multiform_field_id]['#default_value'] = $field_values[$row->multiform_field_id];
1091 }
1092 // Handle system field types
1093 else {
1094
1095 $form_type = _multiforms_ui_field_form_type($row->multiform_field_type);
1096 $form[$form_name][$row->multiform_field_id] = array(
1097 '#title' => t($row->multiform_field_name),
1098 '#type' => $form_type,
1099 '#default_value' => $field_values[$row->multiform_field_id],
1100 '#description' => t($row->multiform_field_description)
1101 );
1102 }
1103
1104 if ($row->multiform_field_validation_type == 'not_empty') {
1105
1106 $form[$form_name][$row->multiform_field_id]['#required'] = TRUE;
1107 }
1108
1109 $form[$form_name][$row->multiform_field_id]['#attributes'] = array(
1110 'class' => 'multiform multiform_field multiform_'. $multiform_name .' multiform_'. $multiform_name .'_field multiform_'. $multiform_name .'_field_'. $row->multiform_field_id
1111 );
1112
1113 if ($row->multiform_field_type != 'markup') {
1114 $show_submit = 1;
1115 }
1116
1117 $fields_output++;
1118 }
1119 }
1120
1121 // If no fields have been output and section isn't primary, allow no form to be defined
1122 if (!$fields_output && $section_id > 0) {
1123 return;
1124 }
1125
1126 // If section has any form fields or section is primary section, then show submit
1127 if ($show_submit || $section_id < 0) {
1128 $form[$form_name]['submit'] = theme('multiforms_section_form_submit_element', $multiform_id, $section_id);
1129 }
1130
1131 return $form;
1132 }
1133
1134 function theme_multiforms_section_form_submit_element($multiform_id, $section_id) {
1135
1136 if ($section_id > 0) {
1137 $submit_button_text = _multiforms_get_section_property($section_id, 'multiform_section_submit_button_text');
1138 }
1139 else {
1140 $submit_button_text = _multiforms_get_property($multiform_id, 'multiform_submit_button_text');
1141 }
1142
1143 $element_value = ($submit_button_text) ? t($submit_button_text) : t('Submit');
1144
1145 return array(
1146 '#type' => 'submit',
1147 '#value' => $element_value
1148 );
1149 }
1150
1151 function _multiforms_ui_field_form_type($field_type) {
1152
1153 switch ($field_type) {
1154 case 'unique_text':
1155 case 'unique_phone':
1156 return 'textfield';
1157 default:
1158 return $field_type;
1159 }
1160 }
1161
1162 function _multiforms_submit_section_data($section_id, $field_values) {
1163
1164 $existing_field_values = _multiforms_return_existing_section_data($section_id);
1165
1166 $type_handlers = module_invoke_all('multiforms_field_handlers');
1167
1168 $query = "SELECT * FROM {". MULTIFORM_FIELD_TABLE ."} WHERE multiform_field_section_id='%d'";
1169
1170 $result = db_query($query, $section_id);
1171
1172 if ($result) {
1173
1174 while ($row = db_fetch_object($result)) {
1175
1176 $handler = $type_handlers[$row->multiform_field_type];
1177
1178 if (function_exists($handler)) {
1179
1180 $field_value = $handler('store', $field_values[$row->multiform_field_id]);
1181 }
1182 else {
1183
1184 $field_value = $field_values[$row->multiform_field_id];
1185 }
1186
1187 if (!($field_value === FALSE)) {
1188 if (!sizeof($existing_field_values)) {
1189
1190 $query = "INSERT INTO {". MULTIFORM_FIELD_SUBMISSION_TABLE ."} \r
1191 (multiform_field_submission_parent_submission_id, \r
1192 multiform_field_submission_parent_field_id, \r
1193 multiform_field_submission_value) \r
1194 VALUES \r
1195 ('%d', '%d', '%s')";
1196
1197 $result2 = db_query(
1198 $query,
1199 $_SESSION['multiforms_submission_id'],
1200 $row->multiform_field_id,
1201 $field_value
1202 );
1203 }
1204 else {
1205
1206 $query = "UPDATE {". MULTIFORM_FIELD_SUBMISSION_TABLE ."} \r
1207 SET multiform_field_submission_value='%s' WHERE \r
1208 multiform_field_submission_parent_submission_id='%d' AND \r
1209 multiform_field_submission_parent_field_id='%d'";
1210
1211 $result2 = db_query(
1212 $query,
1213 $field_value,
1214 $_SESSION['multiforms_submission_id'],
1215 $row->multiform_field_id
1216 );
1217 }
1218 }
1219 }
1220 }
1221 }
1222
1223 function _multiforms_return_existing_section_data($section_id) {
1224
1225 $type_handlers = module_invoke_all('multiforms_field_handlers');
1226
1227 $field_values = array();
1228
1229 if ($_SESSION['multiforms_submission_id']) {
1230
1231 $query = "SELECT fs.*, f.multiform_field_type FROM {". MULTIFORM_FIELD_TABLE ."} AS f \r
1232 INNER JOIN {". MULTIFORM_FIELD_SUBMISSION_TABLE ."} AS fs \r
1233 ON f.multiform_field_id=fs.multiform_field_submission_parent_field_id \r
1234 WHERE f.multiform_field_section_id='%d' AND \r
1235 fs.multiform_field_submission_parent_submission_id='%d'";
1236
1237 $result = db_query($query, $section_id, $_SESSION['multiforms_submission_id']);
1238
1239 if ($result) {
1240
1241 while ($row = db_fetch_object($result)) {
1242
1243 $handler = $type_handlers[$row->multiform_field_type];
1244
1245 if (function_exists($handler)) {
1246
1247 $form_element = $handler('form', $row->multiform_field_submission_value, $row);
1248 $field_value = $form_element['#default_value'];
1249 }
1250 else {
1251
1252 $field_value = $row->multiform_field_submission_value;
1253 }
1254
1255 $field_values[$row->multiform_field_submission_parent_field_id] =
1256 $field_value;
1257 }
1258 }
1259 }
1260
1261 return $field_values;
1262 }
1263
1264 function _multiforms_date_array_greater($this_date, $that_date) {
1265
1266 return (strtotime($this_date) > strtotime($that_date));
1267 }
1268

  ViewVC Help
Powered by ViewVC 1.1.2