| 1 |
<?php
|
| 2 |
// $Id: contact.module,v 1.134 2009/10/11 18:34:10 dries Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Enables the use of personal and site-wide contact forms.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implement hook_help().
|
| 11 |
*/
|
| 12 |
function contact_help($path, $arg) {
|
| 13 |
switch ($path) {
|
| 14 |
case 'admin/help#contact':
|
| 15 |
$output = '<p>' . t('The contact module facilitates communication via e-mail, by allowing your site\'s visitors to contact one another (personal contact forms), and by providing a simple way to direct messages to a set of administrator-defined recipients (the <a href="@contact">contact page</a>). With either form, users specify a subject, write their message, and (optionally) have a copy of their message sent to their own e-mail address.', array('@contact' => url('contact'))) . '</p>';
|
| 16 |
$output .= '<p>' . t("Personal contact forms allow users to be contacted via e-mail, while keeping recipient e-mail addresses private. Users may enable or disable their personal contact forms by editing their <em>My account</em> page. If enabled, a <em>Contact</em> tab leading to their personal contact form is available on their user profile. Site administrators have access to all personal contact forms (even if they have been disabled). The <em>Contact</em> tab is only visible when viewing another user's profile (users do not see their own <em>Contact</em> tab).") . '</p>';
|
| 17 |
$output .= '<p>' . t('The <a href="@contact">contact page</a> provides a simple form for visitors to leave comments, feedback, or other requests. Messages are routed by selecting a category from a list of administrator-defined options; each category has its own set of e-mail recipients. Common categories for a business site include, for example, "Website feedback" (messages are forwarded to web site administrators) and "Product information" (messages are forwarded to members of the sales department). The actual e-mail addresses defined within a category are not displayed. Only users in roles with the <em>access site-wide contact form</em> permission may access the <a href="@contact">contact page</a>.', array('@contact' => url('contact'))) . '</p>';
|
| 18 |
$output .= '<p>' . t('A link to your site\'s <a href="@contact">contact page</a> from the main <em>Navigation</em> menu is created, but is disabled by default. Create a similar link on another menu by adding a menu item pointing to the path "contact"', array('@contact' => url('contact'))) . '</p>';
|
| 19 |
$output .= '<p>' . t('Customize the <a href="@contact">contact page</a> with additional information (like physical location, mailing address, and telephone number) using the <a href="@contact-settings">contact form settings page</a>. The <a href="@contact-settings">settings page</a> also provides configuration options for the maximum number of contact form submissions a user may perform per hour, and the default status of users\' personal contact forms.', array('@contact-settings' => url('admin/structure/contact/settings'), '@contact' => url('contact'))) . '</p>';
|
| 20 |
$output .= '<p>' . t('For more information, see the online handbook entry for <a href="@contact">Contact module</a>.', array('@contact' => url('http://drupal.org/handbook/modules/contact/', array('absolute' => TRUE)))) . '</p>';
|
| 21 |
return $output;
|
| 22 |
case 'admin/structure/contact':
|
| 23 |
$output = '<p>' . t('This page lets you set up <a href="@form">your site-wide contact form</a>. 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 <a href="@settings">settings page</a>, you can customize the information shown above the contact form. This can be useful to provide additional contact information such as your postal address and telephone number.', array('@settings' => url('admin/structure/contact/settings'), '@form' => url('contact'))) . '</p>';
|
| 24 |
if (!module_exists('menu')) {
|
| 25 |
$menu_note = t('The menu item can be customized and configured only once the menu module has been <a href="@modules-page">enabled</a>.', array('@modules-page' => url('admin/config/modules')));
|
| 26 |
}
|
| 27 |
else {
|
| 28 |
$menu_note = '';
|
| 29 |
}
|
| 30 |
$output .= '<p>' . t('The contact module also adds a <a href="@menu-settings">menu item</a> (disabled by default) to the navigation block.', array('@menu-settings' => url('admin/structure/menu'))) . ' ' . $menu_note . '</p>';
|
| 31 |
return $output;
|
| 32 |
}
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Implement hook_permission().
|
| 37 |
*/
|
| 38 |
function contact_permission() {
|
| 39 |
return array(
|
| 40 |
'administer contact forms' => array(
|
| 41 |
'title' => t('Administer contact forms'),
|
| 42 |
'description' => t('Manage contact forms and configure contact form administrative settings.'),
|
| 43 |
),
|
| 44 |
'access site-wide contact form' => array(
|
| 45 |
'title' => t('Access site-wide contact form'),
|
| 46 |
'description' => t('Send e-mails to administrator-defined recipients using the site-wide contact form.'),
|
| 47 |
),
|
| 48 |
'access user contact forms' => array(
|
| 49 |
'title' => t('Access user contact forms'),
|
| 50 |
'description' => t('Send e-mails to users using their contact forms.'),
|
| 51 |
),
|
| 52 |
);
|
| 53 |
}
|
| 54 |
|
| 55 |
/**
|
| 56 |
* Implement hook_menu().
|
| 57 |
*/
|
| 58 |
function contact_menu() {
|
| 59 |
$items['admin/structure/contact'] = array(
|
| 60 |
'title' => 'Contact form',
|
| 61 |
'description' => 'Create a system contact form and set up categories for the form to use.',
|
| 62 |
'page callback' => 'contact_category_list',
|
| 63 |
'access arguments' => array('administer contact forms'),
|
| 64 |
'file' => 'contact.admin.inc',
|
| 65 |
);
|
| 66 |
$items['admin/structure/contact/add'] = array(
|
| 67 |
'title' => 'Add category',
|
| 68 |
'page callback' => 'drupal_get_form',
|
| 69 |
'page arguments' => array('contact_category_edit_form'),
|
| 70 |
'access arguments' => array('administer contact forms'),
|
| 71 |
'type' => MENU_LOCAL_ACTION,
|
| 72 |
'weight' => 1,
|
| 73 |
'file' => 'contact.admin.inc',
|
| 74 |
);
|
| 75 |
$items['admin/structure/contact/edit/%contact'] = array(
|
| 76 |
'title' => 'Edit contact category',
|
| 77 |
'page callback' => 'drupal_get_form',
|
| 78 |
'page arguments' => array('contact_category_edit_form', 4),
|
| 79 |
'access arguments' => array('administer contact forms'),
|
| 80 |
'type' => MENU_CALLBACK,
|
| 81 |
'file' => 'contact.admin.inc',
|
| 82 |
);
|
| 83 |
$items['admin/structure/contact/delete/%contact'] = array(
|
| 84 |
'title' => 'Delete contact',
|
| 85 |
'page callback' => 'drupal_get_form',
|
| 86 |
'page arguments' => array('contact_category_delete_form', 4),
|
| 87 |
'access arguments' => array('administer contact forms'),
|
| 88 |
'type' => MENU_CALLBACK,
|
| 89 |
'file' => 'contact.admin.inc',
|
| 90 |
);
|
| 91 |
$items['contact'] = array(
|
| 92 |
'title' => 'Contact',
|
| 93 |
'page callback' => 'drupal_get_form',
|
| 94 |
'page arguments' => array('contact_site_form'),
|
| 95 |
'access arguments' => array('access site-wide contact form'),
|
| 96 |
'type' => MENU_SUGGESTED_ITEM,
|
| 97 |
'file' => 'contact.pages.inc',
|
| 98 |
);
|
| 99 |
$items['user/%user/contact'] = array(
|
| 100 |
'title' => 'Contact',
|
| 101 |
'page callback' => 'drupal_get_form',
|
| 102 |
'page arguments' => array('contact_personal_form', 1),
|
| 103 |
'type' => MENU_LOCAL_TASK,
|
| 104 |
'access callback' => '_contact_personal_tab_access',
|
| 105 |
'access arguments' => array(1),
|
| 106 |
'weight' => 2,
|
| 107 |
'file' => 'contact.pages.inc',
|
| 108 |
);
|
| 109 |
return $items;
|
| 110 |
}
|
| 111 |
|
| 112 |
/**
|
| 113 |
* Menu access callback for a user's personal contact form.
|
| 114 |
*
|
| 115 |
* @param $account
|
| 116 |
* A user account object.
|
| 117 |
* @return
|
| 118 |
* TRUE if the current user has access to the requested user's contact form,
|
| 119 |
* or FALSE otherwise.
|
| 120 |
*/
|
| 121 |
function _contact_personal_tab_access(stdClass $account) {
|
| 122 |
global $user;
|
| 123 |
|
| 124 |
// Anonymous users cannot have contact forms.
|
| 125 |
if (!$account->uid) {
|
| 126 |
return FALSE;
|
| 127 |
}
|
| 128 |
|
| 129 |
// User administrators should always have access to personal contact forms.
|
| 130 |
if (user_access('administer users')) {
|
| 131 |
return TRUE;
|
| 132 |
}
|
| 133 |
|
| 134 |
// Users may not contact themselves.
|
| 135 |
if ($user->uid == $account->uid) {
|
| 136 |
return FALSE;
|
| 137 |
}
|
| 138 |
|
| 139 |
// If the requested user has disabled their contact form, or this preference
|
| 140 |
// has not yet been saved, do not allow users to contact them.
|
| 141 |
if (empty($account->contact)) {
|
| 142 |
return FALSE;
|
| 143 |
}
|
| 144 |
|
| 145 |
return user_access('access user contact forms');
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* Load a contact category.
|
| 150 |
*
|
| 151 |
* @param $cid
|
| 152 |
* The contact category ID.
|
| 153 |
* @return
|
| 154 |
* An array with the contact category's data.
|
| 155 |
*/
|
| 156 |
function contact_load($cid) {
|
| 157 |
return db_query("SELECT * FROM {contact} WHERE cid = :cid", array(':cid' => $cid))->fetchAssoc();
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Implement hook_user_insert().
|
| 162 |
*/
|
| 163 |
function contact_user_insert(&$edit, $account, $category) {
|
| 164 |
$edit['contact'] = variable_get('contact_default_status', 1);
|
| 165 |
}
|
| 166 |
|
| 167 |
/**
|
| 168 |
* Implement hook_mail().
|
| 169 |
*/
|
| 170 |
function contact_mail($key, &$message, $params) {
|
| 171 |
$language = $message['language'];
|
| 172 |
$variables = array(
|
| 173 |
'!site-name' => variable_get('site_name', 'Drupal'),
|
| 174 |
'!subject' => $params['subject'],
|
| 175 |
'!category' => isset($params['category']['category']) ? $params['category']['category'] : '',
|
| 176 |
'!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
|
| 177 |
'!sender-name' => format_username($params['sender']),
|
| 178 |
'!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
|
| 179 |
);
|
| 180 |
|
| 181 |
switch ($key) {
|
| 182 |
case 'page_mail':
|
| 183 |
case 'page_copy':
|
| 184 |
$message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
|
| 185 |
$message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, array('langcode' => $language->language));
|
| 186 |
$message['body'][] = $params['message'];
|
| 187 |
break;
|
| 188 |
|
| 189 |
case 'page_autoreply':
|
| 190 |
$message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
|
| 191 |
$message['body'][] = $params['category']['reply'];
|
| 192 |
break;
|
| 193 |
|
| 194 |
case 'user_mail':
|
| 195 |
case 'user_copy':
|
| 196 |
$variables += array(
|
| 197 |
'!recipient-name' => format_username($params['recipient']),
|
| 198 |
'!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
|
| 199 |
);
|
| 200 |
$message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->language));
|
| 201 |
$message['body'][] = t('!recipient-name,', $variables, array('langcode' => $language->language));
|
| 202 |
$message['body'][] = t("!sender-name (!sender-url) has sent you a message via your contact form (!form-url) at !site-name.", $variables, array('langcode' => $language->language));
|
| 203 |
$message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !recipient-edit-url.", $variables, array('langcode' => $language->language));
|
| 204 |
$message['body'][] = t('Message:', array(), array('langcode' => $language->language));
|
| 205 |
$message['body'][] = $params['message'];
|
| 206 |
break;
|
| 207 |
}
|
| 208 |
}
|
| 209 |
|
| 210 |
/**
|
| 211 |
* Implement hook_form_FORM_ID_alter().
|
| 212 |
*
|
| 213 |
* Add the enable personal contact form to an individual user's account page.
|
| 214 |
*/
|
| 215 |
function contact_form_user_profile_form_alter(&$form, &$form_state) {
|
| 216 |
if ($form['#user_category'] == 'account') {
|
| 217 |
$account = $form['#user'];
|
| 218 |
$form['contact'] = array(
|
| 219 |
'#type' => 'fieldset',
|
| 220 |
'#title' => t('Contact settings'),
|
| 221 |
'#weight' => 5,
|
| 222 |
'#collapsible' => TRUE,
|
| 223 |
);
|
| 224 |
$form['contact']['contact'] = array(
|
| 225 |
'#type' => 'checkbox',
|
| 226 |
'#title' => t('Personal contact form'),
|
| 227 |
'#default_value' => !empty($account->contact) ? $account->contact : FALSE,
|
| 228 |
'#description' => t('Allow other users to contact you via a <a href="@url">personal contact form</a> which keeps your e-mail address hidden. Note that some privileged users such as site administrators are still able to contact you even if you choose to disable this feature.', array('@url' => url("user/$account->uid/contact"))),
|
| 229 |
);
|
| 230 |
}
|
| 231 |
}
|
| 232 |
|
| 233 |
/**
|
| 234 |
* Implement of hook_form_FORM_ID_alter().
|
| 235 |
*
|
| 236 |
* Add the default personal contact setting on the user settings page.
|
| 237 |
*/
|
| 238 |
function contact_form_user_admin_settings_alter(&$form, &$form_state) {
|
| 239 |
$form['contact'] = array(
|
| 240 |
'#type' => 'fieldset',
|
| 241 |
'#title' => t('Contact settings'),
|
| 242 |
'#weight' => 0,
|
| 243 |
);
|
| 244 |
$form['contact']['contact_default_status'] = array(
|
| 245 |
'#type' => 'checkbox',
|
| 246 |
'#title' => t('Enable the personal contact form by default for new users.'),
|
| 247 |
'#description' => t('Changing this setting will not affect existing users.'),
|
| 248 |
'#default_value' => 1,
|
| 249 |
);
|
| 250 |
}
|