/[drupal]/contributions/modules/formcorral/formcorral.module
ViewVC logotype

Contents of /contributions/modules/formcorral/formcorral.module

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


Revision 1.1 - (show annotations) (download) (as text)
Sun Jun 3 01:40:31 2007 UTC (2 years, 5 months ago) by calebg
Branch: MAIN
CVS Tags: HEAD
File MIME type: text/x-php
initial commit of formcorral - created by Aaron Stewart and Jonathan Lambert of WorkHabit.com
1 <?php
2 /* $Id */
3
4 /* WorkHabit FormCorral Module
5 * created by : Aaron Stewart (aaron@firebright.com)
6 * For more information on this and other open source initiatives,
7 * please visit http://www.workhabit.org/
8 */
9
10 function formcorral_menu($may_cache) {
11 $items = array();
12 // if ($may_cache) {
13 $items[] = array("path" => "admin/settings/formcorral",
14 "title" => t("FormCorral Lists"),
15 "access" => user_access("administer formcorral"),
16 "callback" => "formcorral_admin_page",
17 "type" => MENU_NORMAL_ITEM);
18 $items[] = array("path" => "admin/settings/formcorral/view",
19 "title" => t("view lists"),
20 "access" => user_access("administer formcorral"),
21 "callback" => "formcorral_admin_page",
22 "type" => MENU_DEFAULT_LOCAL_TASK);
23 $items[] = array("path" => "admin/settings/formcorral/add",
24 "title" => t("add list"),
25 "access" => user_access("administer formcorral"),
26 "callback" => "drupal_get_form",
27 "callback arguments" => "formcorral_edit_list_form",
28 "type" => MENU_LOCAL_TASK);
29
30 /** menu items related to validation **/
31 $items[] = array("path" => "admin/settings/formcorral/validation",
32 "title" => t("validation rules"),
33 "access" => user_access("administer formcorral"),
34 "callback" => "formcorral_validation_page",
35 "type" => MENU_LOCAL_TASK);
36 $items[] = array("path" => "admin/settings/formcorral/validation/add",
37 "title" => t("validation"),
38 "access" => user_access("administer formcorral"),
39 "callback" => "drupal_get_form",
40 "callback arguments" => "formcorral_validator_edit_form",
41 "type" => MENU_CALLBACK);
42 $items[] = array("path" => "admin/settings/formcorral/validation/edit",
43 "title" => t("validation"),
44 "access" => user_access("administer formcorral"),
45 "callback" => "drupal_get_form",
46 "callback arguments" => "formcorral_validator_edit_form",
47 "type" => MENU_CALLBACK);
48
49 $items[] = array("path" => "admin/settings/formcorral/edit",
50 "title" => t("edit list"),
51 "access" => user_access("administer formcorral"),
52 "callback" => "drupal_get_form",
53 "callback arguments" => "formcorral_edit_list_form",
54 "type" => MENU_CALLBACK);
55 $items[] = array("path" => "admin/settings/formcorral/recipients",
56 "title" => t("edit recipients"),
57 "access" => user_access("administer formcorral"),
58 "callback" => "drupal_get_form",
59 "callback arguments" => "formcorral_edit_recipients_form",
60 "type" => MENU_CALLBACK);
61 /** menu items related to fields **/
62 $items[] = array("path" => "admin/settings/formcorral/fields",
63 "title" => t("edit fields"),
64 "access" => user_access("administer formcorral"),
65 "callback" => "drupal_get_form",
66 "callback arguments" => "formcorral_edit_fields_form",
67 "type" => MENU_CALLBACK);
68 $items[] = array("path" => "admin/settings/formcorral/fields/delete",
69 "access" => user_access("administer formcorral"),
70 "callback" => "formcorral_delete_field",
71 "type" => MENU_CALLBACK);
72 $items[] = array("path" => "admin/settings/formcorral/fields/edit",
73 "access" => user_access("administer formcorral"),
74 "callback" => "drupal_get_form",
75 "callback arguments" => "formcorral_edit_field_form",
76 "type" => MENU_CALLBACK);
77
78 /** menu items related to submissions */
79 $items[] = array("path" => "admin/settings/formcorral/submissions",
80 "title" => "View Submissions",
81 "access" => user_access("administer formcorral"),
82 "callback" => "formcorral_admin_submissions_page",
83 "type" => MENU_CALLBACK);
84 $items[] = array("path" => "admin/settings/formcorral/submissions/view",
85 "title" => "View Submission",
86 "access" => user_access("administer formcorral"),
87 "callback" => "formcorral_admin_submissions_view_page",
88 "type" => MENU_CALLBACK);
89 //}
90 return $items;
91 }
92
93 function formcorral_admin_submissions_view_page($lid, $sid) {
94 $submission = formcorral_get_submission($sid);
95 $list = formcorral_get_list($lid);
96 $fields_map = array();
97 foreach($list['fields'] as $field) {
98 $fields_map[$field['fieldName']] = $field['friendlyName'];
99 }
100 $rows = array();
101
102 $header = array("Field Name", "Value");
103 $date_format = "d-M-Y H:i:s";
104 $rows[] = array("Submission Date", date($date_format, $submission['timestamp']));
105 foreach($submission as $field => $value) {
106 if ($field != "timestamp") {
107 $rows[] = array($fields_map[$field], $value);
108 }
109 }
110 print(theme("page", theme_table($header, $rows)));
111 }
112
113 function formcorral_admin_submissions_page($lid) {
114 $list = formcorral_get_list($lid);
115 $fields = $list['fields'];
116 $display_fields = array();
117 $header = array();
118 $header[] = array("data" => "Date Submitted",
119 "field" => "timestamp",
120 "sort" => "asc");
121 for ($i = 0; $i < 2; $i++) {
122 $field = array_shift($fields);
123 $display_fields[] = $field;
124 $header[] = array("data" => $field['friendlyName'],
125 "field" => $field['fieldName'],
126 "sort" => "asc");
127 }
128 if ($_REQUEST['order']) {
129 $order = array('order' => $ordering['sql'], 'sort' => $_REQUEST['sort']);
130 } else {
131 $order = array('order' => 'timestamp', 'sort' => 'desc');
132 }
133 $submissions = formcorral_get_submissions($lid, $order);
134 uasort($submissions, "_formcorral_sort_submission");
135
136 $rows = array();
137 foreach($submissions as $sid => $submission) {
138 $row = array();
139 $row[] = array("data" => date("Y-M-d h:i:s", $submission['timestamp']));
140 foreach($display_fields as $field) {
141 $row[] = $submission[$field['fieldName']];
142 }
143 $actions = l("view", "admin/settings/formcorral/submissions/view/$lid/$sid");
144 $row[] = $actions;
145 $rows[] = $row;
146 }
147 $ordering = tablesort_get_order($header);
148 print(theme("page", theme_table($header, $rows)));
149 }
150
151 function formcorral_delete_field($lid, $fid) {
152 $query = "DELETE FROM {formcorral_fields} where lid=%d and fid=%d";
153 db_query($query, $lid, $fid);
154 drupal_set_message("Deleted field.");
155 drupal_goto("admin/settings/formcorral/fields/{$lid}");
156 }
157
158 function formcorral_save_field($lid, $field) {
159 if ($field['fid']) {
160 $query = "UPDATE {formcorral_fields} set fieldName='%s', friendlyName='%s', vid=%d where lid=%d and fid=%d";
161 db_query($query,
162 db_escape_string($field['fieldName']),
163 db_escape_string($field['friendlyName']),
164 db_escape_string($field['vid']),
165 db_escape_string($lid),
166 db_escape_string($field['fid']));
167 } else {
168 $query = "INSERT INTO {formcorral_fields} (lid, fieldName, friendlyName, vid) values(%d, '%s', '%s', %d)";
169 db_query($query,
170 db_escape_string($lid),
171 db_escape_string($field['fieldName']),
172 db_escape_string($field['friendlyName']),
173 db_escape_string($field['vid']));
174 }
175 }
176
177 function formcorral_perm() {
178 return array("administer formcorral");
179 }
180
181 function formcorral_edit_field_form($lid, $fid) {
182 $list = formcorral_get_list($lid);
183 if (!$list) {
184 drupal_set_error("The requested list doesn't exist!");
185 drupal_goto("admin/settings/formcorral");
186 }
187
188 $field = formcorral_get_field($lid, $fid);
189
190 $form['edit_field'] = array("#type" => "fieldset",
191 "#title" => t("Edit Field"));
192
193 $form['edit_field']['lid'] = array("#type" => "hidden",
194 "#value" => $lid);
195 $form['edit_field']['fid'] = array("#type" => "hidden",
196 "#value" => $fid);
197
198 $form['edit_field']['fieldName'] = array("#type" => "textfield",
199 "#title" => t("Field Name"),
200 "#required" => TRUE,
201 "#default_value" => $field['fieldName'],
202 "#description" => t("Enter the machine readable name of this form field.")
203 );
204
205 $form['edit_field']['friendlyName'] = array("#type" => "textfield",
206 "#title" => t("Friendly Name"),
207 "#default_value" => $field['friendlyName'],
208 "#required" => FALSE,
209 "#description" => t($description)
210 );
211
212 $validators = formcorral_get_validators();
213 $validator_options = array();
214 $validator_options[0] = "none";
215 foreach($validators as $vid => $validator) {
216 $validator_options[$vid] = $validator['name'];
217 }
218
219 $form['edit_field']['vid'] = array("#type" => "select",
220 "#title" => "Validator",
221 "#options" => $validator_options,
222 "#default_value" => $field['vid']);
223
224 $description = "The friendly name of this field. This will be used in titles and " .
225 "error messages visible by the user. If omitted, the Field Name will be used instead.";
226
227 $form['edit-field-submit'] = array("#type" => "submit",
228 "#value" => "Save Field"
229 );
230 return $form;
231
232 }
233
234 function formcorral_edit_field_form_submit($form_id, $form_values) {
235 $lid = $form_values['lid'];
236 formcorral_save_field($lid, $form_values);
237 drupal_set_message("Form field saved.");
238 return "admin/settings/formcorral/fields/{$lid}";
239 }
240
241 function formcorral_edit_fields_form($lid = "") {
242 $list = formcorral_get_list($lid);
243 if (!$list) {
244 drupal_set_error("The requested list doesn't exist!");
245 drupal_goto("admin/settings/formcorral");
246 }
247 $rows = array();
248 if (count($list['fields']) == 0) {
249 $rows[] = array("no fields defined..", "", "");
250 } else {
251 $validators = formcorral_get_validators();
252 foreach($list['fields'] as $fid => $field) {
253 $actions = array(l('edit', "admin/settings/formcorral/fields/edit/{$lid}/{$fid}", array("style" => "padding-right:15px;")),
254 l("delete", "admin/settings/formcorral/fields/delete/{$lid}/{$fid}"));
255 $actions = implode("", $actions);
256 $rows[] = array($field['fieldName'], $field['friendlyName'], $field['vid'] ? $validators[$field['vid']]['name'] : 'none', $actions);
257 }
258 }
259 $table = theme_table(array("Field Name", "Friendly Name", "Validator", "Actions"), $rows);
260
261 $form = array();
262 $form['all_fields'] = array("#type" => "fieldset",
263 "#title" => t("All fields for list {$list['name']}"));
264
265 $form['all_fields']['table'] = array("#type" => "markup",
266 "#value" => $table);
267
268 $form['add_field'] = array("#type" => "fieldset",
269 "#title" => t("Add New Field"));
270
271 $form['add_field']['lid'] = array("#type" => "hidden",
272 "#value" => $lid);
273
274 $form['add_field']['fieldName'] = array("#type" => "textfield",
275 "#title" => t("Field Name"),
276 "#required" => TRUE,
277 "#description" => t("Enter the machine readable name of this form field.")
278 );
279
280 $form['add_field']['friendlyName'] = array("#type" => "textfield",
281 "#title" => t("Friendly Name"),
282 "#required" => FALSE,
283 "#description" => t($description)
284 );
285
286 $validators = formcorral_get_validators();
287 $validator_options = array();
288 $validator_options[0] = "none";
289 foreach($validators as $vid => $validator) {
290 $validator_options[$vid] = $validator['name'];
291 }
292 $form['add_field']['validator'] = array("#type" => "select",
293 "#title" => "Validator",
294 "#options" => $validator_options);
295
296 $description = "The friendly name of this field. This will be used in titles and " .
297 "error messages visible by the user. If omitted, the Field Name will be used instead.";
298
299 $form['add-field-submit'] = array("#type" => "submit",
300 "#value" => "Add Field"
301 );
302 return $form;
303 }
304
305 function formcorral_edit_fields_form_submit($form_id, $form_values) {
306 $lid = $form_values['lid'];
307 formcorral_save_field($lid, $form_values);
308
309 }
310
311 function formcorral_edit_recipients_form($lid = "") {
312 $list = formcorral_get_list($lid);
313 if (!$list) {
314 drupal_set_error("The requested list doesn't exist!");
315 drupal_goto("admin/settings/formcorral");
316 }
317
318 $form = array();
319 $form['lid'] = array("#type" => "hidden",
320 "#value" => $lid);
321 $recipients = $list['recipients'];
322 $all_recipients = array();
323
324 foreach ($recipients as $recipient_list) {
325 if (count($recipient_list) > 0) {
326 $all_recipients = array_merge($all_recipients, $recipient_list);
327 }
328 }
329 $form['recipients'] = array("#type" => "textarea",
330 "#rows" => 4,
331 "#cols" => 30,
332 "#title" => t("Email Recipients"),
333 "#default_value" => implode(", ", $all_recipients),
334 "#description" => "Enter a list of email addresses (separated by commas) that should get notified when a new submission to this list is posted.");
335 $form['submit'] = array("#type" => "submit",
336 "#value" => "Save Recipients");
337 return $form;
338 }
339
340 function formcorral_edit_recipients_form_validate($form_id, $form_values) {
341 $new_recipients = explode(",", $form_values['recipients']);
342 $lid = $form_values['lid'];
343 $list = formcorral_get_list($lid);
344 if (!isset($list['recipients'])) {
345 $list['recipients']= array();
346 }
347 foreach($new_recipients as $new_recipient) {
348 $new_recipient = trim($new_recipient);
349 if (!valid_email_address($new_recipient)) {
350 form_set_message("recipients", "The email address '$new_recipient' is invalid.");
351 }
352 }
353 }
354
355 function formcorral_edit_recipients_form_submit($form_id, $form_values) {
356 $new_recipients = explode(",", $form_values['recipients']);
357 $lid = $form_values['lid'];
358 $list = formcorral_get_list($lid);
359 // flush the existing addresses
360 //
361 $list['recipients'] = array();
362 $new_recipients = explode(",", $form_values['recipients']);
363 foreach ($new_recipients as $recipient) {
364 $list['recipients'][] = trim($recipient);
365 }
366 formcorral_save_list($list);
367 }
368
369 function formcorral_edit_list_form($lid = "") {
370 $form = array();
371 $list = formcorral_get_list($lid);
372 $form['#base'] = "formcorral_edit_list_form";
373 $form['list_info'] = array("#type" => "fieldset",
374 "#title" => t("List Information"));
375 $form['list_info']['name'] = array("#type" => "textfield",
376 "#size" => 50,
377 "#required" => TRUE,
378 "#default_value" => $list['name'],
379 "#title" => t("List Name"));
380 $form['submit'] = array("#type" => "submit",
381 "#value" => "Save");
382
383 return $form;
384 }
385
386 function formcorral_edit_list_form_validate($form_id, $form_values) {
387 // nop
388 }
389
390 function formcorral_edit_list_form_submit($form_id, $form_values) {
391 $list = array("name" => $form_values['name']);
392 formcorral_save_list($list);
393 drupal_set_message("List '{$form_values['name']}' saved.");
394 drupal_goto("admin/settings/formcorral");
395 }
396
397 function formcorral_validation_page() {
398 $validators = formcorral_get_validators();
399 $rows = array();
400 if (count($validators) > 0) {
401 foreach ($validators as $vid => $validator) {
402 $actions = array(l("edit", "admin/settings/formcorral/validation/edit/{$vid}"));
403 $actions = implode("", $actions);
404 $rows[] = array($validator['name'], $actions);
405 }
406 } else {
407 $rows[] = array("no validators defined. " . l("Add one here", "admin/settings/formcorral/validation/add") . ".", "");
408 }
409 $output .= theme_table(array("Validator Name", "Actions"), $rows);
410 $output .= l("Add new validation rule..", "admin/settings/formcorral/validation/add");
411
412 print(theme("page", $output));
413 }
414
415 function formcorral_admin_page() {
416 $output = "";
417 $lists = formcorral_get_lists();
418 $rows = array();
419 if (count($lists) == 0) {
420 $rows[] = array("No lists defined..");
421 } else {
422 foreach ($lists as $lid => $list) {
423 $actions = array(l("edit", "admin/settings/formcorral/edit/$lid", array("style" => "padding-right: 15px;")),
424 l("recipients", "admin/settings/formcorral/recipients/$lid", array("style" => "padding-right: 15px;")),
425 l("fields", "admin/settings/formcorral/fields/$lid", array("style" => "padding-right: 15px;")),
426 l("submissions", "admin/settings/formcorral/submissions/$lid")
427 );
428
429 $rows[] = array($list['name'],
430 implode("", $actions)
431 );
432 }
433 }
434 $output = theme_table(array("List Name", "Actions"), $rows);
435
436 return $output;
437 }
438
439 function formcorral_help($section) {
440 if ($section == "admin/modules#description") {
441 return "Helper module for managing Laszlo Mailing List data";
442 }
443 }
444 function formcorral_save_submission($form) {
445 $queries = array();
446 // get the submitted listID from the form
447 //
448 $lid = $form['lid'];
449 if (!$lid) {
450 watchdog("error", "unspecified LIST ID");
451 return FALSE;
452 }
453 // walk through all form values and construct a query for each
454 //
455 $fields = formcorral_get_fields($lid);
456 $sid = db_next_id("formcorral_submission_sid");
457 $timestamp = mktime();
458 $submission_query = "INSERT INTO {formcorral_submission} (sid, lid, timestamp) values({$sid}, {$lid}, {$timestamp})";
459
460 $query = "INSERT INTO {formcorral_submission_values} (sid, fid, value) values";
461 $bStart = TRUE;
462 foreach ($fields as $fid => $field) {
463 if ($bStart) {
464 $bStart = false;
465 } else {
466 $query.=",";
467 }
468 $value = $form[$field];
469 $formatted_value = $value;
470 if (is_array($value)) {
471 $formatted_value = implode(", ", $value);
472 }
473 $query.="('$sid', '{$fid}', '" . db_escape_string($formatted_value) . "')";
474 }
475 db_query($submission_query);
476 db_query($query);
477 formcorral_send_email($sid, $form);
478
479 }
480
481 function formcorral_send_email($sid, $form) {
482 $query = "SELECT l.lid, name, sendToEmail from {formcorral} l, {formcorral_submission} ls, {formcorral_recipients} lr where ls.lid=lr.lid and l.lid=ls.lid and ls.sid=%s";
483 $result = db_query(sprintf($query, $sid));
484 $emails = array();
485 while ($row = db_fetch_array($result)) {
486 $emails[] = $row['sendToEmail'];
487 $listName = $row['name'];
488 $lid = $row['lid'];
489 }
490 $mail = implode(", ", $emails);
491 $header = "From: info@workhabit.com";
492
493 $message = formcorral_format_message($lid, $sid, $form);
494
495 $subjecttemplate = dirname(__FILE__) . "/mailtemplate/mailtemplate-subject-" . db_escape_string($lid) . ".inc";
496 if (file_exists($subjecttemplate)) {
497 ob_start();
498 require_once($subjecttemplate);
499 $subject = ob_get_contents();
500 ob_end_clean();
501 } else {
502 $subject = "Submission to form: " . $listName;
503 }
504 drupal_mail("formcorral-submission-$sid", $mail, $subject, $message, $header);
505 }
506
507 function formcorral_format_message($lid, $sid, $form) {
508 $mailtemplate = dirname(__FILE__) . "/mailtemplate/mailtemplate-" . db_escape_string($lid) . ".inc";
509 if (file_exists($mailtemplate)) {
510 ob_start();
511 require_once($mailtemplate);
512 $message = ob_get_contents();
513 ob_end_clean();
514 return $message;
515 }
516 $message = "New form submission!\r\n";
517 foreach ($form as $key => $value) {
518 $formatted_value = $value;
519 if (is_array($value)) {
520 $formatted_value = implode(", ", $value);
521 }
522 $message.="{$key}\t{$formatted_value}\r\n";
523 }
524 return $message;
525 }
526
527 function formcorral_get_submissions($lid = 0, $order = array()) {
528 if ($lid == 0) {
529 watchdog("error", "Invalid or no listID specified");
530 }
531 $query = "SELECT s.sid as sid, timestamp, fieldName, sv.value as datavalue FROM {formcorral_fields} f, {formcorral_submission} s, {formcorral_submission_values} sv WHERE s.sid=sv.sid AND f.lid=s.lid AND sv.fid=f.fid AND s.lid=%s order by timestamp";
532 $request = db_query(sprintf($query, db_escape_string($lid)));
533 $submissions = array();
534 $bFirst = TRUE;
535 while ($submission = db_fetch_array($request)) {
536 $sid = $submission['sid'];
537 if (!isset($submissions[$sid])) {
538 $submissions[$sid] = array();
539 $submissions[$sid]['timestamp'] = $submission['timestamp'];
540 $submissions[$sid]['order'] = $order['order'];
541 $submissions[$sid]['sort'] = $order['sort'];
542 }
543 $value = $submission['datavalue'];
544 if ($value != null) {
545 $submissions["{$sid}"][$submission['fieldName']] = $value;
546 }
547 }
548 return $submissions;
549 }
550
551 function formcorral_get_submission($sid) {
552 $query = "SELECT timestamp, fieldName, value FROM {formcorral_fields} f, {formcorral_submission} s, {formcorral_submission_values} sv WHERE s.sid=sv.sid AND f.lid=s.lid and f.fid=sv.fid AND s.sid=%s order by sv.fid";
553 $request = db_query(sprintf($query, db_escape_string($sid)));
554 $bFirst = TRUE;
555 $submission = array();
556 while ($row = db_fetch_array($request)) {
557 if ($bFirst) {
558 $bFirst = false;
559 $submission['timestamp'] = $row['timestamp'];
560 }
561 $submission[$row['fieldName']] = $row['value'];
562 }
563 return $submission;
564 }
565
566
567 function formcorral_get_fields($lid = 0) {
568 static $lists;
569 // return the cached list if it exists
570 //
571 if ($lists[$lid]) {
572 return $lists[$lid];
573 }
574 // fetch the field list from the DB
575 $query = "SELECT * from {formcorral_fields} where lid=%s";
576 $query = sprintf($query, mysql_escape_string($lid));
577 $result = db_query($query);
578 $fields = array();
579 while ($row = db_fetch_array($result)) {
580 $fields["{$row['fid']}"] = $row['fieldName'];
581 }
582 $lists[$lid] = $fields;
583 return $fields;
584 }
585
586 function formcorral_get_field($lid, $fid) {
587 $query = "SELECT * from {formcorral_fields} where lid=%d and fid=%d";
588 $resultset = db_query($query,
589 db_escape_string($lid),
590 db_escape_string($fid));
591 $field = db_fetch_array($resultset);
592 return $field;
593 }
594
595 /** saves a new list and returns the list id for it
596 */
597 function formcorral_save_list($list) {
598 $lid = NULL;
599 // create a new list if none already exists
600 //
601 $is_new_list = FALSE;
602 if ($list['lid']) {
603 $lid = $list['lid'];
604 } else {
605 $is_new_list = TRUE;
606 $lid = db_next_id("formcorral_lid");
607 }
608 if (!isset($list['function_callback'])) {
609 $list['function_callback'] = "";
610 }
611
612 // save the primary list record along with its callback (if specified) and name
613 //
614 if ($is_new_list) {
615 $query_list = "INSERT INTO {formcorral} (lid, name, function_callback) values(%s, '%s', '%s')";
616 db_query($query_list, $lid, $list['name'], $list['function_callback']);
617 } else {
618 $query_list = "UPDATE {formcorral} set name='%s', function_callback='%s' where lid=%d";
619 db_query($query_list, $list['name'], $list['function_callback'], $lid);
620 }
621
622 // save recipients
623 //
624 if (isset($list['recipients']) && count($list['recipients']) > 0) {
625 $query_recipients = "INSERT INTO {formcorral_recipients} (lid, sendToEmail) values('%s', '%s')";
626 $recipients = implode(",", $list['recipients']);
627 db_query($query_recipients, db_escape_string($list['lid']), db_escape_string($recipients));
628 }
629
630 if (isset($list['new_fields']) && count($list['new_fields']) > 0) {
631 // save field names
632 $query_fields = "INSERT INTO {formcorral_fields} (lid, fieldName, friendlyName) values";
633 $bFirst = TRUE;
634 foreach ($list['new_fields'] as $fid => $field) {
635 if ($bFirst) {
636 $bFirst = FALSE;
637 } else {
638 $query_fields .= ",";
639 }
640 $query_fields .= "({$lid}, '{$field['fieldName']}', '{$field['friendlyName']}')";
641 }
642 db_query($query_fields);
643 }
644
645 return $lid;
646 }
647
648 function formcorral_get_list($vMixed) {
649 if (!is_array($vMixed)) {
650 $lid = $vMixed;
651 $query = "SELECT lid, name, function_callback from {formcorral} l WHERE l.lid=%d";
652 $resultset = db_query($query, $lid);
653 } else if (isset($vMixed['name'])) {
654 $name = $vMixed['name'];
655 $query = "SELECT lid, name, function_callback from {formcorral} l where l.name='%s'";
656 $resultset = db_query($query, $name);
657 }
658
659
660 $list = db_fetch_array($resultset);
661 $lid = $list['lid'];
662
663 if ($list) {
664 // fetch recipients
665 //
666 $query = "SELECT rid, sendToEmail FROM formcorral_recipients r where r.lid=%d";
667 $resultset = db_query($query, $lid);
668 $list['recipients'] = array();
669 while ($row = db_fetch_array($resultset)) {
670 $list['recipients'][$row['rid']] = array();
671 $list['recipients'][$row['rid']]['sendToEmail'] = $row['sendToEmail'];
672 }
673
674 // fetch fields
675 //
676 $query = "SELECT fid, fieldName, friendlyName, vid FROM formcorral_fields f where f.lid=%d";
677 $resultset = db_query($query, $lid);
678 $list['fields'] = array();
679 while ($row = db_fetch_array($resultset)) {
680 $list['fields'][$row['fid']] = $row;
681 }
682 }
683 return $list;
684 }
685
686 function formcorral_get_lists() {
687 $query = "SELECT lid, name, function_callback from {formcorral}";
688 $resultset = db_query($query);
689 $lists = array();
690 while ($row = db_fetch_array($resultset)) {
691 $lists[$row['lid']] = array();
692 $lists[$row['lid']]['lid'] = $row['lid'];
693 $lists[$row['lid']]['name'] = $row['name'];
694 }
695 return $lists;
696 }
697
698 function _formcorral_sort_submission($a, $b) {
699 if ($a == $b) { return 0; }
700 $ordering = ($a['sort'] == 'desc') ? -1 : 1;
701 return strcmp(strtolower($a[$a['order']]), strtolower($b[$b['order']])) * $ordering;
702 }
703
704 function formcorral_get_validators() {
705 $query = "SELECT vid, name, type, rule, error_message from {formcorral_validators} order by vid";
706 $resultset = db_query($query);
707 $validators = array();
708 while ($row = db_fetch_array($resultset)) {
709 $validators[$row['vid']] = $row;
710 }
711 return $validators;
712 }
713
714
715 function formcorral_validator_edit_form($vid = "") {
716 if ($vid) {
717 $validator = formcorral_get_validator($vid);
718 $name = $validator['name'];
719 $type = $validator['type'];
720 $rule = $validator['rule'];
721 $error_message = $validator['error_message'];
722 }
723
724 $form = array();
725 $markup_description = "Validation rules are re-usable rules for validating individual form ".
726 "fields. WorkHabit Submission lists come with common validators for ".
727 "email, phone, and postal codes, among others. You may define your ".
728 "own rules below.";
729 $form['markup_1'] = array("#type" => "markup",
730 "#value" => $markup_description);
731 $form['name'] = array("#type" => "textfield",
732 "#title" => t("Name"),
733 "#default_value" => $name,
734 "#required" => TRUE,
735 "#description" => "The name of this validation rule as it should appear in the Submission List field builder.");
736
737 $type_options = array();
738 $type_options[''] = "Select...";
739 $type_options['regexp'] = "Regular Expression";
740 $type_options['callback'] = "PHP Function";
741
742 $form['type'] = array("#type" => "select",
743 "#title" => t("Type"),
744 "#default_value" => $type,
745 "#options" => $type_options,
746 "#description" => t("Select the type of rule, e.g. Regular Expression. See below for more info."));
747
748 $rule_description = t("For Regular Expressions, a valid regular expression that the value of a ".
749 "field should match.<br/> For PHP Function, the name of a callback to call ".
750 "that returns TRUE if valid, FALSE if not.");
751
752 $form['rule'] = array("#type" => "textarea",
753 "#title" => t("Validation rule"),
754 "#default_value" => $rule,
755 "#description" => $rule_description
756 );
757
758 $message_description = "This message will be shown when a field's validation fails. Use ".
759 "%fieldname as a placeholder for the actual field name.";
760 $form['error_message'] = array("#type" => "textarea",
761 "#title" => t("Error message"),
762 "#default_value" => $error_message,
763 "#description" => $message_description);
764
765 $form['submit'] = array("#type" => "submit",
766 "#value" => "Save Validation Rule");
767 return $form;
768 }
769
770 function formcorral_validator_edit_form_validate($form_id, $form_values) {
771
772 }
773
774 function formcorral_validator_edit_form_submit($form_id, $form_values) {
775 formcorral_save_validator($form_values);
776 drupal_set_message("Validator saved.");
777 return "admin/settings/formcorral/validation";
778 }
779
780
781 function formcorral_save_validator($validator) {
782 if (!$validator['vid']) {
783 $validator['vid'] = db_next_id("formcorral_validator_vid");
784 $query = "INSERT INTO {formcorral_validators} (vid, name, type, rule, error_message) values(%d, '%s', '%s', '%s', '%s')";
785
786 db_query($query,
787 db_escape_string($validator['vid']),
788 db_escape_string($validator['name']),
789 db_escape_string($validator['type']),
790 db_escape_string($validator['rule']),
791 db_escape_string($validator['error_message']));
792 } else {
793 $query = "UPDATE {formcorral_validator} set name='%s', type='%s', value='%s', message='%s' where vid=%d";
794 db_query($query,
795 db_escape_string($validator['name']),
796 db_escape_string($validator['type']),
797 db_escape_string($validator['rule']),
798 db_escape_string($validator['error_message']));
799 }
800 }
801
802 function formcorral_validate_form($form_id, $form_values) {
803 $list = formcorral_get_list($form_values['lid']);
804 $validators = formcorral_get_validators();
805 foreach ($list['fields'] as $field) {
806 if ($field['vid']) {
807 $validator = $validators[$field['vid']];
808 formcorral_validate_field($validator, $field, $form_values[$field['fieldName']]);
809 }
810 }
811 }
812
813 function formcorral_validate_field($validator, $field, $value) {
814 if ($validator['type'] == "regexp") {
815 if (!preg_match("/" . $validator['rule'] . "/", $value) && $value != "") {
816 form_set_error($field['fieldName'], str_replace("%fieldname", $field['friendlyName'], $validator['error_message']));
817 }
818 // otherwise it passed
819 } else if ($validator['type'] == "callback") {
820 $result = call_user_func($validator['rule'], $value);
821 if (!$result && $value != "") {
822 form_set_error($field['fieldName'], str_replace("%fieldname", $field['friendlyName'], $validator['error_message']));
823 }
824 }
825 }
826
827 ?>

  ViewVC Help
Powered by ViewVC 1.1.2