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

Contents of /contributions/modules/letters/letters.module

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


Revision 1.23 - (show annotations) (download) (as text)
Mon Aug 10 14:49:43 2009 UTC (3 months, 2 weeks ago) by seanr
Branch: MAIN
CVS Tags: DRUPAL-6--1-4, HEAD
Changes since 1.22: +4 -4 lines
File MIME type: text/x-php
Form tweak
1 <?php
2 // $Id: letters.module,v 1.22 2009/06/08 15:36:42 seanr Exp $
3
4 include(drupal_get_path('module', 'letters') .'/letters_location.inc');
5
6 /**
7 * Implementation of hook_help().
8 */
9 function letters_help($path, $arg) {
10 switch ($path) {
11 case 'admin/modules#description':
12 return t('Allows users to search for and email local newspapers <em><strong>Note:</strong> Requires location.module.</em>');
13 }
14 }
15
16 /**
17 * Permissions
18 */
19 function letters_perm() {
20 return array('access letters', 'administer letters');
21 }
22
23 /**
24 * Implementation of hook_menu().
25 */
26 function letters_menu() {
27 $items = array();
28 $items['letter'] = array(
29 'title' => 'Write a letter to the editor',
30 'page callback' => 'drupal_get_form',
31 'page arguments' => array('letters_form'),
32 'access arguments' => array('access letters'),
33 'type' => MENU_NORMAL_ITEM
34 );
35 $items['admin/settings/letters'] = array(
36 'title' => 'Letters to the Editor',
37 'page callback' => 'letters_admin',
38 'access arguments' => array('administer letters'),
39 'type' => MENU_NORMAL_ITEM
40 );
41 $items['admin/settings/letters/list'] = array(
42 'title' => 'List',
43 'type' => MENU_DEFAULT_LOCAL_TASK,
44 'weight' => -10);
45 $items['admin/settings/letters/add'] = array(
46 'title' => 'Add editor',
47 'page callback' => 'drupal_get_form',
48 'page arguments' => array('letters_admin_edit_form'),
49 'access arguments' => array('administer letters'),
50 'type' => MENU_LOCAL_TASK
51 );
52 $items['admin/settings/letters/settings'] = array(
53 'title' => 'Settings',
54 'page callback' => 'drupal_get_form',
55 'page arguments' => array('letters_admin_settings_form'),
56 'access arguments' => array('administer letters'),
57 'type' => MENU_LOCAL_TASK
58 );
59 $items['admin/settings/letters/edit/%letters'] = array(
60 'title' => 'Edit editor',
61 'page callback' => 'drupal_get_form',
62 'page arguments' => array('letters_admin_edit_form', 4),
63 'access arguments' => array('administer letters'),
64 'type' => MENU_CALLBACK
65 );
66 $items['admin/settings/letters/delete/%letters'] = array(
67 'title' => 'Delete menu',
68 'page callback' => 'drupal_get_form',
69 'page arguments' => array('letters_admin_delete_form', 4),
70 'access arguments' => array('administer letters'),
71 'type' => MENU_CALLBACK
72 );
73 return $items;
74 }
75
76 /**
77 * Menu callback; loads an editor object
78 */
79 function letters_load($newsid) {
80 if (!is_numeric($newsid)) {
81 return FALSE;
82 }
83 if (!($paper = letters_loadpaper($newsid))) {
84 return FALSE;
85 }
86 return $paper;
87 }
88
89 /**
90 * Administration Page
91 */
92 function letters_admin() {
93 $query = "SELECT * FROM {letters_newspapers}";
94 $result = db_query($query);
95
96 $rows = array();
97 while ($paper = db_fetch_array($result)) {
98 $statewide = ($paper['statewide']) ? ' - statewide' : '';
99 $rows[] = array(
100 'name' => l($paper['title'], $paper['homepage']) . $statewide .'<br />'. l($paper['email'], $paper['email']),
101 'address' => check_plain($paper['address']) .'<br />'. check_plain($paper['city']) .', '. check_plain($paper['state']) .' '. check_plain($paper['zip']),
102 'phone' => 'p: '. check_plain($paper['phone']) .'<br />f: '. check_plain($paper['fax']),
103 'edit' => l(t('edit'), 'admin/settings/letters/edit/'. $paper['newsid']),
104 'delete' => l(t('delete'), 'admin/settings/letters/delete/'. $paper['newsid'])
105 );
106 }
107 if (empty($rows)) {
108 $rows[] = array(array('data' => t('No editors available.'), 'colspan' => '5', 'class' => 'message'));
109 }
110 $header = array(t('Name'), t('Address'), t('Phone Numbers'), array('data' => t('Operations'), 'colspan' => '2'));
111
112 return theme('table', $header, $rows, array('id' => 'tournament'));
113 }
114
115 function letters_admin_edit_form($form_state, $paper = array('newsid' => 0, 'title' => '', 'address' => '', 'city' => '', 'state' => '', 'zip' => '', 'phone' => '', 'fax' => '', 'email' => '', 'homepage' => '', 'statewide' => 0)) {
116 //dsm($paper);
117 $form['title'] = array(
118 '#type' => 'textfield',
119 '#title' => t('Title'),
120 '#default_value' => $paper['title'],
121 '#required' => TRUE,
122 );
123 $form['address'] = array(
124 '#type' => 'textfield',
125 '#title' => t('Address'),
126 '#default_value' => $paper['address'],
127 );
128 $form['city'] = array(
129 '#type' => 'textfield',
130 '#title' => t('City'),
131 '#default_value' => $paper['city'],
132 );
133 $form['state'] = array(
134 '#type' => 'textfield',
135 '#title' => t('State'),
136 '#default_value' => $paper['state'],
137 );
138 $form['zip'] = array(
139 '#type' => 'textfield',
140 '#title' => t('Zip code'),
141 '#default_value' => $paper['zip'],
142 '#required' => TRUE,
143 );
144 $form['phone'] = array(
145 '#type' => 'textfield',
146 '#title' => t('Phone'),
147 '#default_value' => $paper['phone'],
148 );
149 $form['fax'] = array(
150 '#type' => 'textfield',
151 '#title' => t('Fax'),
152 '#default_value' => $paper['fax'],
153 );
154 $form['email'] = array(
155 '#type' => 'textfield',
156 '#title' => t('Email'),
157 '#default_value' => $paper['email'],
158 );
159 $form['homepage'] = array(
160 '#type' => 'textfield',
161 '#title' => t('Homepage'),
162 '#default_value' => $paper['homepage'],
163 );
164 $form['statewide'] = array(
165 '#type' => 'checkbox',
166 '#title' => t('Statewide'),
167 '#default_value' => $paper['statewide'],
168 );
169 $form['#redirect'] = 'admin/settings/letters';
170 $form['newsid'] = array('#type' => 'value', '#value' => $paper['newsid']);
171 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
172
173 return $form;
174 }
175
176 function letters_admin_edit_form_submit($form, &$form_state) {
177 if ($form_state['values']['newsid']) {
178 db_query("UPDATE {letters_newspapers} SET title = '%s', address = '%s', city = '%s', state = '%s', zip = '%s', phone = '%s', fax = '%s', email = '%s', homepage = '%s', statewide = %d WHERE newsid = %d", $form_state['values']['title'], $form_state['values']['address'], $form_state['values']['city'], $form_state['values']['state'], $form_state['values']['zip'], $form_state['values']['phone'], $form_state['values']['fax'], $form_state['values']['email'], $form_state['values']['homepage'], $form_state['values']['statewide'], $form_state['values']['newsid']);
179 }
180 else {
181 db_query("INSERT INTO {letters_newspapers} (title, address, city, state, zip, phone, fax, email, homepage, statewide) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d)", $form_state['values']['title'], $form_state['values']['address'], $form_state['values']['city'], $form_state['values']['state'], $form_state['values']['zip'], $form_state['values']['phone'], $form_state['values']['fax'], $form_state['values']['email'], $form_state['values']['homepage'], $form_state['values']['statewide']);
182 }
183 }
184
185 function letters_admin_delete_form($form_state, $paper = NULL) {
186 $question = t('Are you sure you want to delete the editor %item?', array('%item' => $paper['title']));
187 $path = 'admin/settings/letters';
188 $description = t('This action cannot be undone.');
189
190 $form['newsid'] = array('#type' => 'hidden', '#value' => $paper['newsid']);
191 $form['name'] = array('#type' => 'hidden', '#value' => $paper['title']);
192
193 return confirm_form($form, $question, $path, $description, t('Delete'), t('Cancel'));
194 }
195
196 function letters_admin_delete_form_submit($form, &$form_state) {
197 db_query('DELETE FROM {letters_newspapers} WHERE newsid = %d', $form_state['values']['newsid']);
198
199 $t_args = array('%name' => $form_state['values']['name']);
200
201 drupal_set_message(t('The editor %name has been deleted.', $t_args));
202 watchdog('menu', 'Deleted editor %name.', $t_args, WATCHDOG_NOTICE);
203 $form_state['redirect'] = 'admin/settings/letters';
204 }
205
206 /**
207 * Administration Page
208 */
209 function letters_admin_settings_form() {
210 $form['letters_skip_search'] = array(
211 '#type' => 'checkbox',
212 '#title' => t('Skip zipcode proximity search'),
213 '#return_value' => 1,
214 '#default_value' => variable_get('letters_skip_search', FALSE),
215 '#description' => t('If checked, users submitting letters will go directly to the submittion form with all editors listed in the dropdown, bypassing the location search'),
216 );
217 return system_settings_form($form);
218 }
219
220 /**
221 * Letter submission form
222 */
223 function letters_form($form_state = NULL) {
224 //dsm($form_state);
225 if (empty($form_state['values']['step'])) {
226 if (module_exists('location') && (variable_get('letters_skip_search', FALSE) == FALSE)) {
227 $page = 1;
228 }
229 else {
230 $page = 2;
231 }
232 }
233 else {
234 $page = $form_state['values']['step'];
235 }
236
237 //dsm($page);
238 return call_user_func('letters_form_page_'. $page, $form_state);
239 }
240
241 function letters_form_page_1($form_state = NULL) {
242 global $user;
243
244 // CRMAPI hook - load contact data to prefill form
245 $contact = array();
246 if (module_exists('crmapi')) {
247 global $user;
248 if ($user->crmapi_contact) {
249 $contact = crmapi_contact_load('', $user->crmapi_contact);
250 }
251 }
252 // Create the fields for the first step of your form here
253 $form['distance'] = array(
254 '#type' => 'select',
255 '#title' => t('Distance'),
256 '#required' => TRUE,
257 '#options' => array('20' => '20', '50' => '50', '100' => '100', 'all' => 'show all'),
258 '#description' => t('Please choose an option.'),
259
260 );
261 $form['zip'] = array(
262 '#type' => 'textfield',
263 '#title' => t('zip'),
264 '#default_value' => !empty($contact['zip']) ? $contact['zip'] : NULL,
265 '#size' => 10,
266 '#required' => TRUE,
267 );
268 $form['step'] = array(
269 '#type' => 'hidden',
270 '#value' => 1,
271 );
272 $form['submit'] = array(
273 '#type' => 'submit',
274 '#value' => t('Submit'),
275 );
276
277 return $form;
278 }
279
280 function letters_form_page_2($form_state = NULL) {
281 global $user;
282
283 if ((!module_exists('location')) || (variable_get('letters_skip_search', FALSE) == TRUE) || ($form_state['values']['distance'] == 'all')) {
284 $ziparray = 'all';
285 }
286 else {
287 $zip = $form_state['values']['zip'];
288 $radius = $form_state['values']['distance'];
289 $location = array(
290 'country' => 'us',
291 'postal_code' => $zip,
292 );
293 $ziparray = letters_location_nearby_postalcodes_bylocation($location, $radius, 'mile');
294 }
295
296 $papers = letters_locate($ziparray);
297 $form['paper'] = array(
298 '#type' => 'select',
299 '#title' => t('Paper'),
300 '#required' => TRUE,
301 '#options' => $papers,
302 '#description' => t('Please choose an option.'),
303
304 );
305
306 $form['instructions'] = array(
307 '#type' => 'item',
308 '#title' => t('Instructions'),
309 '#value' => 'Enter Your Information Below. Keep in mind most newspapers require you to provide at least your address and a telephone number to publish your letter',
310 );
311
312 // CRMAPI hook - load contact data to prefill form
313 $contact = array();
314 if (module_exists('crmapi')) {
315 if ($user->crmapi_contact) {
316 $contact = crmapi_contact_load('', $user->crmapi_contact);
317 }
318 }
319
320 $form['first_name'] = array(
321 '#type' => 'textfield',
322 '#title' => t('First Name'),
323 '#default_value' => (!empty($contact['first_name'])) ? $contact['first_name'] : NULL,
324 '#required' => TRUE,
325 );
326 $form['last_name'] = array(
327 '#type' => 'textfield',
328 '#title' => t('Last Name'),
329 '#default_value' => (!empty($contact['last_name'])) ? $contact['last_name'] : NULL,
330 '#required' => TRUE,
331 );
332 $form['address'] = array(
333 '#type' => 'textfield',
334 '#title' => t('Address'),
335 '#default_value' => !empty($contact['address1']) ? $contact['address1'] : NULL,
336 '#required' => TRUE,
337 );
338 $form['city'] = array(
339 '#type' => 'textfield',
340 '#title' => t('City'),
341 '#default_value' => !empty($contact['city']) ? $contact['city'] : NULL,
342 '#required' => TRUE,
343 );
344 $form['state'] = array(
345 '#type' => 'textfield',
346 '#title' => t('State'),
347 '#default_value' => !empty($contact['state']) ? $contact['state'] : $form_state['values']['state'],
348 '#required' => TRUE,
349 );
350
351 if (!empty($contact['zip'])) {
352 $zip = $contact['zip'];
353 }
354 else if (!empty($form_state['values']['zip'])) {
355 $zip = $form_state['values']['zip'];
356 }
357 else {
358 $zip = NULL;
359 }
360
361 $form['zip'] = array(
362 '#type' => 'textfield',
363 '#title' => t('Zip'),
364 '#default_value' => $zip,
365 '#required' => TRUE,
366 );
367 $form['phone'] = array(
368 '#type' => 'textfield',
369 '#title' => t('Phone'),
370 '#default_value' => !empty($contact['phone_home']) ? $contact['phone_home'] : $form_state['values']['phone'],
371 '#required' => TRUE,
372 );
373 $form['email'] = array(
374 '#type' => 'textfield',
375 '#title' => t('Email'),
376 '#value' => !empty($user->mail) ? $user->mail : $form_state['values']['email'],
377 '#required' => TRUE,
378 '#disabled' => !empty($user->mail) ? TRUE : FALSE,
379 );
380 $form['instructions'] = array(
381 '#type' => 'item',
382 '#title' => t('4 Tips for Writing a Good Letter to the Editor'),
383 '#value' => ' <p>Letters to the Editor allow you to voice concern or praise about issues facing your community. It is very important that letters are concise and the message is clear and easy to understand.</p>
384 <ol>
385 <li>Keep it short (under 200 words) and to the point, otherwise the message will get lost.</li>
386 <li>If possible, refer to past articles or editorials in the paper. Quoting a source is a good way to back up your statement. Always make sure that someone who did not read the story you are referring to would understand what you are talking about.</li>
387 <li>Make sure your statements are accurate.</li>
388 <li>Check spelling and grammar.</li>
389 </ol><br>',
390 );
391 $form['topic'] = array(
392 '#type' => 'textfield',
393 '#title' => t('Subject'),
394 '#required' => TRUE,
395 );
396 $form['message'] = array(
397 '#type' => 'textarea',
398 '#title' => t('Your Message'),
399 '#cols' => '35',
400 '#rows' => '8',
401 '#wrap' => 'PHYSICAL',
402 );
403 $form['step'] = array(
404 '#type' => 'hidden',
405 '#value' => 2,
406 );
407 $form['submit'] = array(
408 '#type' => 'submit',
409 '#value' => t('Submit'),
410 );
411
412 return $form;
413 }
414
415 function letters_form_page_3($form_state = NULL) {
416 $form['zip'] = array(
417 '#type' => 'hidden',
418 '#value' => $form_state['values']['zip'],
419 );
420 $form['topic'] = array(
421 '#type' => 'hidden',
422 '#value' => $form_state['values']['topic'],
423 );
424 $form['paper'] = array(
425 '#type' => 'hidden',
426 '#value' => $form_state['values']['paper'],
427 );
428 $form['email'] = array(
429 '#type' => 'hidden',
430 '#value' => !empty($user->mail) ? $user->mail : $form_state['values']['email'],
431 );
432 $form['first_name'] = array(
433 '#type' => 'hidden',
434 '#value' => $form_state['values']['first_name'],
435 );
436 $form['last_name'] = array(
437 '#type' => 'hidden',
438 '#value' => $form_state['values']['last_name'],
439 );
440 $form['address'] = array(
441 '#type' => 'hidden',
442 '#value' => $form_state['values']['address'],
443 );
444 $form['city'] = array(
445 '#type' => 'hidden',
446 '#value' => $form_state['values']['city'],
447 );
448 $form['state'] = array(
449 '#type' => 'hidden',
450 '#value' => $form_state['values']['state'],
451 );
452 $form['zip'] = array(
453 '#type' => 'hidden',
454 '#value' => $form_state['values']['zip'],
455 );
456 $strmessage = "Editor, \n". $form_state['values']['message'] ."\n\n\n". $form_state['values']['first_name'] .' '. $form_state['values']['last_name'] ."\n". $form_state['values']['address'] ."\n". $form_state['values']['city'] .', '. $form_state['values']['state'] .' '. $form_state['values']['zip'] ."\n". $form_state['values']['phone'];
457 $form['preview'] = array(
458 '#type' => 'item',
459 '#title' => t('Preview Your Letter'),
460 '#value' => '',
461 );
462 $form['message'] = array(
463 '#type' => 'textarea',
464 '#title' => t('Is this how you want your letter to look? Make any changes below'),
465 '#cols' => '35',
466 '#rows' => '8',
467 '#wrap' => 'PHYSICAL',
468 '#value' => $strmessage,
469 );
470 $form['step'] = array(
471 '#type' => 'hidden',
472 '#value' => 3,
473 );
474 $form['submit'] = array(
475 '#type' => 'submit',
476 '#value' => t('Submit'),
477 );
478
479 return $form;
480 }
481
482 function letters_form_page_4($form_state = NULL) {
483 // we want to display a themed page containing the submission rather then just redirecting the user
484 $form['results'] = array(
485 '#type' => 'item',
486 '#value' => $form_state['data']['page_content'],
487 );
488
489 return $form;
490 }
491
492 function letters_form_validate($form, &$form_state) {
493 //dsm($form_state);
494 if ($form_state['values']['step'] == 1) {
495 $result = db_query('SELECT COUNT(*) FROM {zipcodes} WHERE zip = %d', substr($form_state['values']['zip'], 0, 5));
496 if (db_result($result) < 1) {
497 form_set_error('zip', t('Your Zip code is invalid.'));
498 }
499 }
500 if ($form_state['values']['step'] == 2 && (!valid_email_address($form_state['values']['email']))) {
501 form_set_error('email', t('Your Email address is invalid.'));
502 }
503 }
504
505 function letters_form_submit($form, &$form_state) {
506 $form_state['rebuild'] = TRUE;
507 if ($form_state['values']['step'] == 3) {
508 // Process the form here!
509 $subject = $form_state['values']['topic'];
510 $from = $form_state['values']['email'];
511 $paper = letters_loadpaper($form_state['values']['paper']);
512 $to = $paper['email'];
513 $body = $form_state['values']['message'];
514 $zip = $form_state['values']['zip'];
515 $preview = check_markup($body);
516
517 if ($paper['email'] != '') {
518 drupal_set_title('Your letter has been sent');
519 $form_state['data']['page_content'] = '<p><b>To:</b> '. $paper['title'] ."<br /><b>From:</b> $from<br /><b>Subject:</b> $subject</p><p><b>Message:</b></p> $preview";
520
521 $params = array('subject' => $subject, 'body' => check_markup($body), 'from' => $from);
522
523 // Produce Message
524 if (!drupal_mail('letters', 'letters_mail', 'seanr@ngpsoftware.com', language_default(), $params, $from)) {
525 drupal_set_message('Email to editor failed.');
526 }
527 }
528 else {
529 $form_state['data']['page_content'] = "
530 <SCRIPT language='JavaScript1.2'>
531 function printletter(preview) {
532 printpopup = window.open('','printpopup','status=1,scrollbars=1,width=350,height=350');
533 printpopup.document.write('<html><body>'+preview+'</body></html>');
534 printpopup.print();
535 }
536 </SCRIPT>
537 ";
538 drupal_set_title('Print and mail your letter');
539 $form_state['data']['page_content'] .= "<p>The newspaper you selected does not accept letter submissions via the web. Please <a href=\"javascript:printletter('". str_replace("'", "\'", $preview) .'\');">print your message</a> and mail it to the address listed below.</p><p><b>'. $paper['title'] .'</b><br />'. $paper['address'] .'<br />'. $paper['city'] .', '. $paper['state'] .' '. $paper['zip'] .'</p>';
540 }
541
542 // CRMAPI hook - saves data to default enabled CRM
543 if (module_exists('crmapi')) {
544 $contact['first_name'] = $form_state['values']['first_name'];
545 $contact['last_name'] = $form_state['values']['last_name'];
546 $contact['mail_name'] = $form_state['values']['first_name'] .' '. $form_state['values']['last_name'];
547 $contact['email'] = $form_state['values']['email'];
548 $contact['address1'] = $form_state['values']['address'];
549 $contact['city'] = $form_state['values']['city'];
550 $contact['state'] = $form_state['values']['state'];
551 $contact['zip'] = $form_state['values']['zip'];
552
553 $contact_id = crmapi_contact_save($contact);
554
555 $activity_params = array(
556 'contact_id' => $contact_id,
557 'activity_id' => 'OLLetter',
558 'activity_type' => 'L',
559 'level' => '',
560 'flag' => '',
561 'description' => substr($form_state['values']['topic'], 0, 100)
562 );
563 crmapi_activity_save($activity_params);
564 }
565 }
566 $form_state['values']['step']++;
567 }
568
569 /**
570 * Select Newspapers
571 */
572 function letters_locate($ziparray) {
573 $options = array();
574 if (count($ziparray) && $ziparray != 'all') {
575 $query_fragment = implode(array_keys($ziparray), ',');
576 $result = db_query("SELECT n.* FROM {zipcodes} z INNER JOIN {letters_newspapers} n ON z.zip = n.zip WHERE FIND_IN_SET(CONCAT(z.country, z.zip), '%s') OR n.statewide=1", $query_fragment);
577 }
578 else {
579 $result = db_query('SELECT * FROM {letters_newspapers}');
580 }
581
582 while ($paper = db_fetch_array($result)) {
583 if (user_access('administer letters') || ($paper['email'] != 'icampaignteam@ngpsoftware.com')) {
584 $options[$paper['newsid']] = $paper['title'].'- '.$paper['city'].', '.$paper['state'];
585 }
586 }
587
588 return $options;
589 }
590
591 function letters_loadpaper($newsid) {
592 $result = db_query('SELECT * FROM {letters_newspapers} WHERE newsid = %d', $newsid);
593
594 return db_fetch_array($result);
595 }
596
597 /**
598 * Implementation of hook_mail().
599 *
600 * Constructs the email notification message when the site is out of date.
601 *
602 * @param $key
603 * Unique key to indicate what message to build, always 'forward_page'.
604 * @param $message
605 * Reference to the message array being built.
606 * @param $params
607 * Array of parameters to indicate what text to include in the message body.
608 *
609 * @see drupal_mail();
610 * @see _update_cron_notify();
611 * @see _update_message_text();
612 */
613 function letters_mail($key, &$message, $params) {
614 $message['subject'] .= $params['subject'];
615 $message['body'][] = $params['body'];
616 $message['headers']['MIME-Version'] = '1.0';
617 $message['headers']['Content-Type'] = 'text/html; charset=utf-8';
618 }

  ViewVC Help
Powered by ViewVC 1.1.2