'. t('The uascoop_contact module enables the use of a site-wide contact form. Site-wide form allows community members to contact the site administration from a central location. Users can specify a subject and message in the contact form, sign up for the newsletter and also request that a copy of the e-mail be sent to their own address.') .'

'; $output .= '

'. t('If the menu module is enabled, a menu item linking to the site-wide uascoop_contact page is added to the navigation block. It is disabled by default, but can be enabled via the menu management page. Links to the uascoop_contact page may also be added to the primary and secondary links using the same page.', array('%menu-module' => url('admin/menu'))) .'

'; $output .= t('uascoop_contact module links:') .''; return $output; case 'admin/modules#description': return t('Enables the use of both personal and site-wide uascoop_contact forms.'); case 'admin/uascoop_contact': $output = t('This page lets you setup your site-wide uascoop_contact form. To do so, add one or more categories. You can associate different recipients with each category to route e-mails to different people. For example, you can route website feedback to the webmaster and direct product information requests to the sales department. On the settings page, you can customize the information shown above the uascoop_contact form. This can be useful to provide additional uascoop_contact information such as your postal address and telephone number.', array('%settings' => url('admin/uascoop_contact/settings'), '%form' => url('uascoop_contact'))); if (!module_exist('menu')) { $menu_note = t('The menu item can be customized and configured only once the menu module has been enabled.', array('%modules-page' => url('admin/modules'))); } else { $menu_note = ''; } $output .= '

'. t('The uascoop_contact module also adds a menu item (disabled by default) to the navigation block.', array('%menu-settings' => url('admin/menu'))) .' '. $menu_note .'

'; return($output); } } /** * Implementation of hook_menu(). */ function uascoop_contact_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array('path' => 'admin/uascoop_contact', 'title' => t('uascoop_contact form'), 'callback' => 'uascoop_contact_admin_categories', 'access' => user_access('administer site configuration'), ); $items[] = array('path' => 'admin/uascoop_contact/category', 'title' => t('categories'), 'callback' => 'uascoop_contact_admin_categories', 'access' => user_access('administer site configuration'), 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items[] = array('path' => 'admin/uascoop_contact/category/list', 'title' => t('list'), 'callback' => 'uascoop_contact_admin_categories', 'access' => user_access('administer site configuration'), 'type' => MENU_DEFAULT_LOCAL_TASK, ); $items[] = array('path' => 'admin/uascoop_contact/category/add', 'title' => t('add category'), 'callback' => 'uascoop_contact_admin_edit', 'access' => user_access('administer site configuration'), 'type' => MENU_LOCAL_TASK, 'weight' => 1, ); $items[] = array('path' => 'admin/uascoop_contact/category/edit', 'title' => t('edit uascoop_contact category'), 'callback' => 'uascoop_contact_admin_edit', 'access' => user_access('administer site configuration'), 'type' => MENU_CALLBACK, ); $items[] = array('path' => 'admin/uascoop_contact/category/delete', 'title' => t('delete uascoop_contact'), 'callback' => 'uascoop_contact_admin_delete', 'access' => user_access('administer site configuration'), 'type' => MENU_CALLBACK, ); $items[] = array('path' => 'admin/uascoop_contact/settings', 'title' => t('settings'), 'callback' => 'uascoop_contact_admin_settings', 'access' => user_access('administer site configuration'), 'type' => MENU_LOCAL_TASK, 'weight' => 1, ); $items[] = array('path' => 'uascoop_contact', 'title' => variable_get('uascoop_contact_page_title', t('Contact Us')), 'callback' => 'uascoop_contact_mail_page', 'access' => user_access('access content'), 'type' => MENU_SUGGESTED_ITEM, ); } return $items; } /** * Categories/list tab. */ function uascoop_contact_admin_categories() { $result = db_query('SELECT cid, category, recipients, selected FROM {contact} ORDER BY weight, category'); $rows = array(); while ($category = db_fetch_object($result)) { $rows[] = array($category->category, $category->recipients, ($category->selected ? t('Yes') : t('No')), l(t('edit'), 'admin/uascoop_contact/category/edit/'. $category->cid), l(t('delete'), 'admin/uascoop_contact/category/delete/'. $category->cid)); } $header = array(t('Category'), t('Recipients'), t('Selected'), array('data' => t('Operations'), 'colspan' => 2)); return theme('table', $header, $rows); } /** * Category edit page. */ function uascoop_contact_admin_edit($cid = NULL) { if (arg(3) == "edit" && $cid > 0) { $edit = db_fetch_array(db_query("SELECT * FROM {contact} WHERE cid = %d", $cid)); } $form['category'] = array('#type' => 'textfield', '#title' => t('Category'), '#maxlength' => 255, '#default_value' => $edit['category'], '#description' => t("Example: 'website feedback' or 'product information'."), '#required' => TRUE, ); $form['recipients'] = array('#type' => 'textarea', '#title' => t('Recipients'), '#default_value' => $edit['recipients'], '#description' => t("Example: 'webmaster@yoursite.com' or 'sales@yoursite.com'. To specify multiple recipients, separate each e-mail address with a comma."), '#required' => TRUE, ); $form['reply'] = array('#type' => 'textarea', '#title' => t('Auto-reply'), '#default_value' => $edit['reply'], '#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'), ); $form['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#default_value' => $edit['weight'], '#description' => t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'), ); $form['selected'] = array('#type' => 'select', '#title' => t('Selected'), '#options' => array('0' => t('No'), '1' => t('Yes')), '#default_value' => $edit['selected'], '#description' => t('Set this to Yes if you would like this category to be selected by default.'), ); $form['cid'] = array('#type' => 'value', '#value' => $edit['cid'], ); $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), ); return drupal_get_form('uascoop_contact_admin_edit', $form); } /** * Validate the uascoop_contact category edit page form submission. */ function uascoop_contact_admin_edit_validate($form_id, $form_values) { if (empty($form_values['category'])) { form_set_error('category', t('You must enter a category.')); } if (empty($form_values['recipients'])) { form_set_error('recipients', t('You must enter one or more recipients.')); } else { $recipients = explode(',', $form_values['recipients']); foreach($recipients as $recipient) { if (!valid_email_address(trim($recipient))) { form_set_error('recipients', t('%recipient is an invalid e-mail address.', array('%recipient' => theme('placeholder', $recipient)))); } } } } /** * Process the uascoop_contact category edit page form submission. */ function uascoop_contact_admin_edit_submit($form_id, $form_values) { if ($form_values['selected']) { // Unselect all other uascoop_contact categories. db_query('UPDATE {contact} SET selected = 0'); } $recipients = explode(',', $form_values['recipients']); foreach($recipients as $key=>$recipient) { // E-mail address validation has already been done in _validate. $recipients[$key] = trim($recipient); } $form_values['recipients'] = implode(',', $recipients); if (arg(3) == 'add') { db_query("INSERT INTO {contact} (category, recipients, reply, weight, selected) VALUES ('%s', '%s', '%s', %d, %d)", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected']); drupal_set_message(t('Category %category has been added.', array('%category' => theme('placeholder', $form_values['category'])))); watchdog('mail', t('uascoop_contact form: category %category added.', array('%category' => theme('placeholder', $form_values['category']))), WATCHDOG_NOTICE, l(t('view'), 'admin/uascoop_contact')); } else { db_query("UPDATE {contact} SET category = '%s', recipients = '%s', reply = '%s', weight = %d, selected = %d WHERE cid = %d", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected'], $form_values['cid']); drupal_set_message(t('Category %category has been updated.', array('%category' => theme('placeholder', $form_values['category'])))); watchdog('mail', t('uascoop_contact form: category %category updated.', array('%category' => theme('placeholder', $form_values['category']))), WATCHDOG_NOTICE, l(t('view'), 'admin/uascoop_contact')); } return 'admin/uascoop_contact'; } /** * Category delete page. */ function uascoop_contact_admin_delete($cid = NULL) { if ($info = db_fetch_object(db_query("SELECT category FROM {contact} WHERE cid = %d", $cid))) { $form['category'] = array('#type' => 'value', '#value' => $info->category, ); return confirm_form('uascoop_contact_admin_delete', $form, t('Are you sure you want to delete %category?', array('%category' => theme('placeholder', $info->category))), 'admin/uascoop_contact', t('This action cannot be undone.'), t('Delete'), t('Cancel')); } else { drupal_set_message(t('Category not found.'), 'error'); drupal_goto('admin/uascoop_contact'); } } /** * Process category delete form submission. */ function uascoop_contact_admin_delete_submit($form_id, $form_values) { db_query("DELETE FROM {contact} WHERE cid = %d", arg(4)); drupal_set_message(t('Category %category has been deleted.', array('%category' => theme('placeholder', $form_values['category'])))); watchdog('mail', t('uascoop_contact form: category %category deleted.', array('%category' => theme('placeholder', $form_values['category']))), WATCHDOG_NOTICE); return 'admin/uascoop_contact'; } /** * Settings tab. Using a form rather than hook_settings(). */ function uascoop_contact_admin_settings() { $form['uascoop_contact_page_title'] = array('#type' => 'textfield', '#title' => t('Title for the contact page'), '#default_value' => variable_get('uascoop_contact_page_title', t('Contact Us')), '#description' => t('Title for the uascoop_contact page. This text will appear in the page title and above the contact form.', array('%form' => url('uascoop_contact'))), ); $form['uascoop_contact_form_information'] = array('#type' => 'textarea', '#title' => t('Additional information'), '#default_value' => variable_get('uascoop_contact_form_information', t('You can leave a message using the uascoop_contact form below.')), '#description' => t('Information to show on the uascoop_contact page. Can be anything from submission guidelines to your postal address or telephone number.', array('%form' => url('uascoop_contact'))), ); $form['uascoop_contact_copy_message'] = array('#type' => 'textarea', '#title' => t('User Copy Email Message'), '#default_value' => variable_get('uascoop_contact_copy_message', t('If you did not send this form, please let us know as soon as possible.')), '#description' => t('This message will appear in the copy of the contact request that is sent to the user. This can be used for letting a user know how they can contact in case they did not submit the form or did not intend to contact.'), ); $form['uascoop_contact_newsletter_text'] = array('#type' => 'textfield', '#title' => t('Newsletter invitation text'), '#default_value' => variable_get('uascoop_contact_newsletter_text', t('Would you like to receive our monthly newsletter?')), '#description' => t('Newsletter invitation text on the uascoop_contact page. This text will appear next to the newsletter checkbox.', array('%form' => url('uascoop_contact'))), ); $form['uascoop_contact_hourly_threshold'] = array('#type' => 'select', '#title' => t('Hourly threshold'), '#options' => drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50)), '#default_value' => variable_get('uascoop_contact_hourly_threshold', 3), '#description' => t('The maximum number of contact form submissions a user can perform per hour.'), ); $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'), ); $form['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults'), ); // Use system_settings_form for the callback. return drupal_get_form('uascoop_contact_admin_settings', $form, 'system_settings_form'); } /** * Site-wide uascoop_contact page */ function uascoop_contact_mail_page() { global $user; require_once( 'recaptcha-php-1.8/recaptchalib.php' ); if (!flood_is_allowed('uascoop_contact', variable_get('uascoop_contact_hourly_threshold', 3))) { $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('uascoop_contact_hourly_threshold', 3))); } else { /* autofill the email if authenticated user */ if ($user->uid) { $edit['mail'] = $user->mail; } $result = db_query('SELECT cid, category, selected FROM {contact} ORDER BY weight, category'); while ($category = db_fetch_object($result)) { $categories[$category->cid] = $category->category; if ($category->selected) { $default_category = $category->cid; } } if (count($categories) > 0) { $form['#token'] = $user->name . $user->mail; $form['uascoop_contact_information'] = array('#value' => filter_xss_admin(variable_get('uascoop_contact_form_information', t('You can leave us a message using the contact form below.')))); $form['first_name'] = array('#type' => 'textfield', '#title' => t('Your first name'), '#maxlength' => 255, '#required' => FALSE, ); $form['last_name'] = array('#type' => 'textfield', '#title' => t('Your last name'), '#maxlength' => 255, '#required' => FALSE, ); $form['mail'] = array('#type' => 'textfield', '#title' => t('Your e-mail address'), '#maxlength' => 255, '#default_value' => $edit['mail'], '#required' => TRUE, ); $form['newsletter'] = array('#type' => 'checkbox', '#title' => variable_get('uascoop_contact_newsletter_text', t('Would you like to receive our monthly newsletter?')), '#maxlength' => 255, '#required' => FALSE, ); $form['subject'] = array('#type' => 'textfield', '#title' => t('Subject'), '#maxlength' => 255, '#required' => TRUE, ); if (count($categories) > 1) { // If there is more than one category available and no default category has been selected, // prepend a default placeholder value. if (!isset($default_category)) { $categories = array(t('--')) + $categories; } $form['cid'] = array('#type' => 'select', '#title' => t('Category'), '#default_value' => $default_category, '#options' => $categories, '#required' => TRUE, ); } else { // If there is only one category, store its cid. $category_keys = array_keys($categories); $form['cid'] = array('#type' => 'value', '#value' => array_shift($category_keys), ); } $form['message'] = array('#type' => 'textarea', '#title' => t('Message'), '#required' => TRUE, ); $form['copy'] = array('#type' => 'checkbox', '#title' => t('Send me a copy.'), ); $form['recaptcha'] = array('#value' => '

'.recaptcha_get_html(RECAPTCHA_PUBLIC).'



'); $form['submit'] = array('#type' => 'submit', '#value' => t('Send'), ); $output = drupal_get_form('uascoop_contact_mail_page', $form); } else { $output = t('The uascoop_contact form has not been configured.'); } } return $output; } /** * Validate the site-wide uascoop_contact page form submission. */ function uascoop_contact_mail_page_validate($form_id, $form_values) { require_once( 'recaptcha-php-1.8/recaptchalib.php' ); if (!$form_values['cid']) { form_set_error('category', t('You must select a valid category.')); } if (!valid_email_address($form_values['mail'])) { form_set_error('mail', t('You must enter a valid e-mail address.')); } $resp = recaptcha_check_answer (RECAPTCHA_PRIVATE, $_SERVER["REMOTE_ADDR"], $_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]); if (!$resp->is_valid) { form_set_error('recaptcha', t("The reCAPTCHA wasn't entered correctly. Please, try it again.")); } } /** * Process the site-wide uascoop_contact page form submission. */ function uascoop_contact_mail_page_submit($form_id, $edit) { $contact_processed = uascoop_contact_process_civicrm_contact($edit); //generate an error message to log and send to site contacts //if there was a problem processing the contact in civicrm $problem_msg = ""; if (!$contact_processed){ $problem_msg = "There was a problem adding this contact and/or subscribing this contact to the newsletter."; } //process emails uascoop_contact_process_mail($edit, $problem_msg); //process logs $from = $edit['mail']; $name = $edit['first_name'].' '.$edit['last_name']; // Log the operation: flood_register_event('uascoop_contact'); watchdog('mail', t("%name-from sent an e-mail regarding %category. $problem_msg", array('%name-from' => theme('placeholder', $name ." <$from>"), '%category' => theme('placeholder', $uascoop_contact->category)))); // Update user: drupal_set_message(t('Your message has been sent.')); // Jump to home page rather than back to uascoop_contact page to avoid contradictory messages if flood control has been activated. return(''); } /** * This function gets called from the uascoop_contact_mail_page_submit function. * It's used for processing the data from the form in relation to the civicrm contacts. * It checks to see if the contact with an email submitted in the form already exists. * If it doesn't, we create a new civicrm contact with that email and first name and last name, * if they were specified on the form. */ function &uascoop_contact_process_civicrm_contact($edit){ //check if this contact is already in civicrm //if it's not, then create a new civicrm contact $params = array('email' => $edit['mail']); if (module_exist('civicrm')) { civicrm_initialize(TRUE); list( $contacts, $options ) = crm_contact_search( $params ); //if contact with this email not found //then create a new contact and //subscribe to newsletter if a user chose so $total_contacts_found = count($contacts); if ( $total_contacts_found == 0) { $params = array(); if ($edit['first_name']){ $params['first_name'] = $edit['first_name']; } if ($edit['last_name']){ $params['last_name'] = $edit['last_name']; } $params['email'] = $edit['mail']; $contact =& crm_create_contact($params, 'Individual'); if ($edit['newsletter']){ //subscribe to the newsletter newsletter_add_user_to_list($contact); newsletter_set_flag_for_contact($contact,true); } //everything was successful - return true return true; } //else if found one contact record //subscribe to newsletter if a user chose so else if ($total_contacts_found == 1){ if ($edit['newsletter']){ //subscribe contact to the newsletter $contact_ids = array_keys($contacts); $contact_id = $contact_ids[0]; newsletter_add_user_to_list($contact_id); newsletter_set_flag_for_contact($contact_id,true); } //everything was successful - return true return true; } } //something went wrong: either civicrm didn't load or // there were more than one contact found for the given email //thus return false return false; } /** * Process all of the mailings upon site-wide form submission */ function uascoop_contact_process_mail($edit, $processing_error){ $uas_body = $user_body = ""; // E-mail address of the sender: as the form field is a text field, // all instances of \r and \n have been automatically stripped from it. $from = $edit['mail']; $name = $edit['first_name'].' '.$edit['last_name']; // Compose the body: $uas_body_msg[] = t("This message was sent using the contact form at %form. Details of the request: \n", array('%form' => url($_GET['q'], NULL, NULL, TRUE))); $user_body_msg[] = t("You have send us a message using our contact form at %form. Below are details of your request. %user_message", array('%form' => url($_GET['q'], NULL, NULL, TRUE), '%user_message' => variable_get('uascoop_contact_copy_message', t('If you did not send this form, please let us know as soon as possible.')))); $uas_body_msg[] = $user_body_msg[] = t("Name: %name", array('%name' => $name)); $uas_body_msg[] = $user_body_msg[] = t("Message: %message", array('%message' => $edit['message'])); if ($edit['newsletter']){ $uas_body_msg[] = t("%name requested to be signed up for the newsletter.", array('%name' => $name)); $user_body_msg[] = t("You requested to be signed up for the newsletter."); } if (!$processing_error){ $uas_body_msg[] = "The contact data was successfully processed."; if ($edit['newsletter']){ $uas_body_msg[] = "The contact was successfully added to the appropriate newsletter group."; } } else{ $uas_body_msg[] = "$processing_error"; } // Tidy up the uas body: foreach ($uas_body_msg as $key => $value) { $uas_body_msg[$key] = wordwrap($value); } // Tidy up the user body: foreach ($user_body_msg as $key => $value) { $user_body_msg[$key] = wordwrap($value); } // Prepare the body for uas: $uas_body = implode("\n\n", $uas_body_msg); // Prepare the body for the user copy: $body_user = implode("\n\n", $user_body_msg); // Load the category information: $uascoop_contact = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $edit['cid'])); // Format the category: $subject = t('[%category] %subject', array('%category' => $uascoop_contact->category, '%subject' => $edit['subject'])); // Send the e-mail to the recipients: user_mail($uascoop_contact->recipients, $subject, $uas_body, "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from"); // Send a copy to the user. if ($edit['copy']) { user_mail($from, $subject, $body_user, "From: $uascoop_contact->recipients\nReply-to: $uascoop_contact->recipients\nX-Mailer: Drupal\nReturn-path: $uascoop_contact->recipients\nErrors-to: $uascoop_contact->recipients"); } // Send an auto-reply if necessary: if ($uascoop_contact->reply) { user_mail($from, $subject, wordwrap($uascoop_contact->reply), "From: $uascoop_contact->recipients\nReply-to: $uascoop_contact->recipients\nX-Mailer: Drupal\nReturn-path: $uascoop_contact->recipients\nErrors-to: $uascoop_contact->recipients"); } //if this user has been successfully signed up for the newsletter, //then send a confirmation email if ($edit['newsletter'] && !$processing_error){ newsletter_send_thank_you_email($from); } }