Skip navigation
Help

contact.module

  1. drupal
    1. 6 drupal/modules/contact/contact.module
    2. 7 drupal/modules/contact/contact.module

Enables the use of personal and site-wide contact forms.

Functions & methods

NameDescription
contact_form_user_admin_settings_alterImplements hook_form_FORM_ID_alter().
contact_form_user_profile_form_alterImplements hook_form_FORM_ID_alter().
contact_helpImplements hook_help().
contact_loadLoad a contact category.
contact_mailImplements hook_mail().
contact_menuImplements hook_menu().
contact_permissionImplements hook_permission().
contact_user_presaveImplements hook_user_presave().
_contact_personal_tab_accessMenu access callback for a user's personal contact form.

File

drupal/modules/contact/contact.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * Enables the use of personal and site-wide contact forms.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function contact_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#contact':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Contact module allows visitors to contact site administrators and other users. Users specify a subject, write their message, and can have a copy of their message sent to their own e-mail address. For more information, see the online handbook entry for <a href="@contact">Contact module</a>.', array('@contact' => 'http://drupal.org/handbook/modules/contact/')) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('User contact forms') . '</dt>';
  18. $output .= '<dd>' . t('Site users can be contacted with a user contact form that keeps their e-mail address 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 leads to a personal contact form displayed on their user profile. Site administrators are still able to use the contact form, even if has been disabled. The <em>Contact</em> tab is not shown when you view your own profile.') . '</dd>';
  19. $output .= '<dt>' . t('Site-wide contact forms') . '</dt>';
  20. $output .= '<dd>' . t('The <a href="@contact">Contact page</a> provides a simple form for users with the <em>Use the site-wide contact form</em> permission to send comments, feedback, or other requests. You can create categories for directing the contact form messages to a set of defined recipients. Common categories for a business site, for example, might include "Website feedback" (messages are forwarded to website administrators) and "Product information" (messages are forwarded to members of the sales department). E-mail addresses defined within a category are not displayed publicly.', array('@contact' => url('contact'))) . '</p>';
  21. $output .= '<dt>' . t('Navigation') . '</dt>';
  22. $output .= '<dd>' . t("When the site-wide contact form is enabled, a link in the main <em>Navigation</em> menu is created, but the link is disabled by default. This menu link can be enabled on the <a href='@menu'>Menus administration page</a>.", array('@contact' => url('contact'), '@menu' => url('admin/structure/menu'))) . '</dd>';
  23. $output .= '<dt>' . t('Customization') . '</dt>';
  24. $output .= '<dd>' . t('If you would like additional text to appear on the site-wide or personal contact page, use a block. You can create and edit blocks on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</dd>';
  25. $output .= '</dl>';
  26. return $output;
  27. case 'admin/structure/contact':
  28. $output = '<p>' . t('Add one or more categories on this page to set up your site-wide <a href="@form">contact form</a>.', array('@form' => url('contact'))) . '</p>';
  29. $output .= '<p>' . t('A <em>Contact</em> menu item (disabled by default) is added to the Navigation menu, which you can modify on the <a href="@menu-settings">Menus administration page</a>.', array('@menu-settings' => url('admin/structure/menu'))) . '</p>';
  30. $output .= '<p>' . t('If you would like additional text to appear on the site-wide contact page, use a block. You can create and edit blocks on the <a href="@blocks">Blocks administration page</a>.', array('@blocks' => url('admin/structure/block'))) . '</p>';
  31. return $output;
  32. }
  33. }
  34. /**
  35. * Implements hook_permission().
  36. */
  37. function contact_permission() {
  38. return array(
  39. 'administer contact forms' => array(
  40. 'title' => t('Administer contact forms and contact form settings'),
  41. ),
  42. 'access site-wide contact form' => array(
  43. 'title' => t('Use the site-wide contact form'),
  44. ),
  45. 'access user contact forms' => array(
  46. 'title' => t("Use users' personal contact forms"),
  47. ),
  48. );
  49. }
  50. /**
  51. * Implements hook_menu().
  52. */
  53. function contact_menu() {
  54. $items['admin/structure/contact'] = array(
  55. 'title' => 'Contact form',
  56. 'description' => 'Create a system contact form and set up categories for the form to use.',
  57. 'page callback' => 'contact_category_list',
  58. 'access arguments' => array('administer contact forms'),
  59. 'file' => 'contact.admin.inc',
  60. );
  61. $items['admin/structure/contact/add'] = array(
  62. 'title' => 'Add category',
  63. 'page callback' => 'drupal_get_form',
  64. 'page arguments' => array('contact_category_edit_form'),
  65. 'access arguments' => array('administer contact forms'),
  66. 'type' => MENU_LOCAL_ACTION,
  67. 'weight' => 1,
  68. 'file' => 'contact.admin.inc',
  69. );
  70. $items['admin/structure/contact/edit/%contact'] = array(
  71. 'title' => 'Edit contact category',
  72. 'page callback' => 'drupal_get_form',
  73. 'page arguments' => array('contact_category_edit_form', 4),
  74. 'access arguments' => array('administer contact forms'),
  75. 'file' => 'contact.admin.inc',
  76. );
  77. $items['admin/structure/contact/delete/%contact'] = array(
  78. 'title' => 'Delete contact',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('contact_category_delete_form', 4),
  81. 'access arguments' => array('administer contact forms'),
  82. 'file' => 'contact.admin.inc',
  83. );
  84. $items['contact'] = array(
  85. 'title' => 'Contact',
  86. 'page callback' => 'drupal_get_form',
  87. 'page arguments' => array('contact_site_form'),
  88. 'access arguments' => array('access site-wide contact form'),
  89. 'type' => MENU_SUGGESTED_ITEM,
  90. 'file' => 'contact.pages.inc',
  91. );
  92. $items['user/%user/contact'] = array(
  93. 'title' => 'Contact',
  94. 'page callback' => 'drupal_get_form',
  95. 'page arguments' => array('contact_personal_form', 1),
  96. 'type' => MENU_LOCAL_TASK,
  97. 'access callback' => '_contact_personal_tab_access',
  98. 'access arguments' => array(1),
  99. 'weight' => 2,
  100. 'file' => 'contact.pages.inc',
  101. );
  102. return $items;
  103. }
  104. /**
  105. * Menu access callback for a user's personal contact form.
  106. *
  107. * @param $account
  108. * A user account object.
  109. * @return
  110. * TRUE if the current user has access to the requested user's contact form,
  111. * or FALSE otherwise.
  112. */
  113. function _contact_personal_tab_access($account) {
  114. global $user;
  115. // Anonymous users cannot have contact forms.
  116. if (!$account->uid) {
  117. return FALSE;
  118. }
  119. // User administrators should always have access to personal contact forms.
  120. if (user_access('administer users')) {
  121. return TRUE;
  122. }
  123. // Users may not contact themselves.
  124. if ($user->uid == $account->uid) {
  125. return FALSE;
  126. }
  127. // If the requested user has disabled their contact form, or this preference
  128. // has not yet been saved, do not allow users to contact them.
  129. if (empty($account->data['contact'])) {
  130. return FALSE;
  131. }
  132. // If requested user has been blocked, do not allow users to contact them.
  133. if (empty($account->status)) {
  134. return FALSE;
  135. }
  136. return user_access('access user contact forms');
  137. }
  138. /**
  139. * Load a contact category.
  140. *
  141. * @param $cid
  142. * The contact category ID.
  143. * @return
  144. * An array with the contact category's data.
  145. */
  146. function contact_load($cid) {
  147. return db_select('contact', 'c')
  148. ->addTag('translatable')
  149. ->fields('c')
  150. ->condition('cid', $cid)
  151. ->execute()
  152. ->fetchAssoc();
  153. }
  154. /**
  155. * Implements hook_mail().
  156. */
  157. function contact_mail($key, &$message, $params) {
  158. $language = $message['language'];
  159. $variables = array(
  160. '!site-name' => variable_get('site_name', 'Drupal'),
  161. '!subject' => $params['subject'],
  162. '!category' => isset($params['category']['category']) ? $params['category']['category'] : '',
  163. '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
  164. '!sender-name' => format_username($params['sender']),
  165. '!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
  166. );
  167. switch ($key) {
  168. case 'page_mail':
  169. case 'page_copy':
  170. $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
  171. $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, array('langcode' => $language->language));
  172. $message['body'][] = $params['message'];
  173. break;
  174. case 'page_autoreply':
  175. $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
  176. $message['body'][] = $params['category']['reply'];
  177. break;
  178. case 'user_mail':
  179. case 'user_copy':
  180. $variables += array(
  181. '!recipient-name' => format_username($params['recipient']),
  182. '!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
  183. );
  184. $message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->language));
  185. $message['body'][] = t('Hello !recipient-name,', $variables, array('langcode' => $language->language));
  186. $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));
  187. $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));
  188. $message['body'][] = t('Message:', array(), array('langcode' => $language->language));
  189. $message['body'][] = $params['message'];
  190. break;
  191. }
  192. }
  193. /**
  194. * Implements hook_form_FORM_ID_alter().
  195. *
  196. * Add the enable personal contact form to an individual user's account page.
  197. */
  198. function contact_form_user_profile_form_alter(&$form, &$form_state) {
  199. if ($form['#user_category'] == 'account') {
  200. $account = $form['#user'];
  201. $form['contact'] = array(
  202. '#type' => 'fieldset',
  203. '#title' => t('Contact settings'),
  204. '#weight' => 5,
  205. '#collapsible' => TRUE,
  206. );
  207. $form['contact']['contact'] = array(
  208. '#type' => 'checkbox',
  209. '#title' => t('Personal contact form'),
  210. '#default_value' => !empty($account->data['contact']) ? $account->data['contact'] : FALSE,
  211. '#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"))),
  212. );
  213. }
  214. }
  215. /**
  216. * Implements hook_user_presave().
  217. */
  218. function contact_user_presave(&$edit, $account, $category) {
  219. $edit['data']['contact'] = isset($edit['contact']) ? $edit['contact'] : variable_get('contact_default_status', 1);
  220. }
  221. /**
  222. * Implements hook_form_FORM_ID_alter().
  223. *
  224. * Add the default personal contact setting on the user settings page.
  225. */
  226. function contact_form_user_admin_settings_alter(&$form, &$form_state) {
  227. $form['contact'] = array(
  228. '#type' => 'fieldset',
  229. '#title' => t('Contact settings'),
  230. '#weight' => 0,
  231. );
  232. $form['contact']['contact_default_status'] = array(
  233. '#type' => 'checkbox',
  234. '#title' => t('Enable the personal contact form by default for new users.'),
  235. '#description' => t('Changing this setting will not affect existing users.'),
  236. '#default_value' => variable_get('contact_default_status', 1),
  237. );
  238. }