contact.module

You are here

  1. 7 modules/contact/contact.module
  2. 4.6 modules/contact.module
  3. 4.7 modules/contact.module
  4. 5 modules/contact/contact.module
  5. 6 modules/contact/contact.module
  6. 8 core/modules/contact/contact.module

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

Functions

Namesort descending Description
contact_help Implementation of hook_help().
contact_load Load the data for a single contact category.
contact_mail Implementation of hook_mail().
contact_menu Implementation of hook_menu().
contact_perm Implementation of hook_perm
contact_user Implementation of hook_user().
_contact_user_tab_access Menu access callback for a user's personal contact form.

File

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. * Implementation of hook_help().
  8. */
  9. function contact_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#contact':
  12. $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>';
  13. $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>';
  14. $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>';
  15. $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>';
  16. $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/build/contact/settings'), '@contact' => url('contact'))) .'</p>';
  17. $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>';
  18. return $output;
  19. case 'admin/build/contact':
  20. $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/build/contact/settings'), '@form' => url('contact'))) .'</p>';
  21. if (!module_exists('menu')) {
  22. $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/settings/modules')));
  23. }
  24. else {
  25. $menu_note = '';
  26. }
  27. $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/build/menu'))) .' '. $menu_note .'</p>';
  28. return $output;
  29. }
  30. }
  31. /**
  32. * Implementation of hook_perm
  33. */
  34. function contact_perm() {
  35. return array('access site-wide contact form', 'administer site-wide contact form');
  36. }
  37. /**
  38. * Implementation of hook_menu().
  39. */
  40. function contact_menu() {
  41. $items['admin/build/contact'] = array(
  42. 'title' => 'Contact form',
  43. 'description' => 'Create a system contact form and set up categories for the form to use.',
  44. 'page callback' => 'contact_admin_categories',
  45. 'access arguments' => array('administer site-wide contact form'),
  46. 'file' => 'contact.admin.inc',
  47. );
  48. $items['admin/build/contact/list'] = array(
  49. 'title' => 'List',
  50. 'page callback' => 'contact_admin_categories',
  51. 'type' => MENU_DEFAULT_LOCAL_TASK,
  52. 'file' => 'contact.admin.inc',
  53. );
  54. $items['admin/build/contact/add'] = array(
  55. 'title' => 'Add category',
  56. 'page callback' => 'drupal_get_form',
  57. 'page arguments' => array('contact_admin_edit', 3),
  58. 'access arguments' => array('administer site-wide contact form'),
  59. 'type' => MENU_LOCAL_TASK,
  60. 'weight' => 1,
  61. 'file' => 'contact.admin.inc',
  62. );
  63. $items['admin/build/contact/edit/%contact'] = array(
  64. 'title' => 'Edit contact category',
  65. 'page callback' => 'drupal_get_form',
  66. 'page arguments' => array('contact_admin_edit', 3, 4),
  67. 'access arguments' => array('administer site-wide contact form'),
  68. 'type' => MENU_CALLBACK,
  69. 'file' => 'contact.admin.inc',
  70. );
  71. $items['admin/build/contact/delete/%contact'] = array(
  72. 'title' => 'Delete contact',
  73. 'page callback' => 'drupal_get_form',
  74. 'page arguments' => array('contact_admin_delete', 4),
  75. 'access arguments' => array('administer site-wide contact form'),
  76. 'type' => MENU_CALLBACK,
  77. 'file' => 'contact.admin.inc',
  78. );
  79. $items['admin/build/contact/settings'] = array(
  80. 'title' => 'Settings',
  81. 'page callback' => 'drupal_get_form',
  82. 'page arguments' => array('contact_admin_settings'),
  83. 'access arguments' => array('administer site-wide contact form'),
  84. 'type' => MENU_LOCAL_TASK,
  85. 'weight' => 2,
  86. 'file' => 'contact.admin.inc',
  87. );
  88. $items['contact'] = array(
  89. 'title' => 'Contact',
  90. 'page callback' => 'contact_site_page',
  91. 'access arguments' => array('access site-wide contact form'),
  92. 'type' => MENU_SUGGESTED_ITEM,
  93. 'file' => 'contact.pages.inc',
  94. );
  95. $items['user/%user/contact'] = array(
  96. 'title' => 'Contact',
  97. 'page callback' => 'contact_user_page',
  98. 'page arguments' => array(1),
  99. 'type' => MENU_LOCAL_TASK,
  100. 'access callback' => '_contact_user_tab_access',
  101. 'access arguments' => array(1),
  102. 'weight' => 2,
  103. 'file' => 'contact.pages.inc',
  104. );
  105. return $items;
  106. }
  107. /**
  108. * Menu access callback for a user's personal contact form.
  109. *
  110. * @param $account
  111. * A user account object.
  112. * @return
  113. * TRUE if the current user has access to the requested user's contact form,
  114. * or FALSE otherwise.
  115. */
  116. function _contact_user_tab_access($account) {
  117. global $user;
  118. // Anonymous users cannot use or have contact forms.
  119. if (!$user->uid || !$account->uid) {
  120. return FALSE;
  121. }
  122. // User administrators should always have access to personal contact forms.
  123. if (user_access('administer users')) {
  124. return TRUE;
  125. }
  126. // Users may not contact themselves.
  127. if ($user->uid == $account->uid) {
  128. return FALSE;
  129. }
  130. // If the requested user has disabled their contact form, or this preference
  131. // has not yet been saved, do not allow users to contact them.
  132. if (empty($account->contact)) {
  133. return FALSE;
  134. }
  135. return TRUE;
  136. }
  137. /**
  138. * Load the data for a single contact category.
  139. */
  140. function contact_load($cid) {
  141. $contact = db_fetch_array(db_query("SELECT * FROM {contact} WHERE cid = %d", $cid));
  142. return empty($contact) ? FALSE : $contact;
  143. }
  144. /**
  145. * Implementation of hook_user().
  146. *
  147. * Allows the user the option of enabling/disabling his personal contact form.
  148. */
  149. function contact_user($type, &$edit, &$user, $category = NULL) {
  150. if ($type == 'form' && $category == 'account') {
  151. $form['contact'] = array('#type' => 'fieldset',
  152. '#title' => t('Contact settings'),
  153. '#weight' => 5,
  154. '#collapsible' => TRUE,
  155. );
  156. $form['contact']['contact'] = array('#type' => 'checkbox',
  157. '#title' => t('Personal contact form'),
  158. '#default_value' => !empty($edit['contact']) ? $edit['contact'] : FALSE,
  159. '#description' => t('Allow other users to contact you by e-mail via <a href="@url">your personal contact form</a>. Note that while your e-mail address is not made public to other members of the community, privileged users such as site administrators are able to contact you even if you choose not to enable this feature.', array('@url' => url("user/$user->uid/contact"))),
  160. );
  161. return $form;
  162. }
  163. elseif ($type == 'validate') {
  164. return array('contact' => isset($edit['contact']) ? $edit['contact'] : FALSE);
  165. }
  166. elseif ($type == 'insert') {
  167. $edit['contact'] = variable_get('contact_default_status', 1);
  168. }
  169. }
  170. /**
  171. * Implementation of hook_mail().
  172. */
  173. function contact_mail($key, &$message, $params) {
  174. $language = $message['language'];
  175. switch ($key) {
  176. case 'page_mail':
  177. case 'page_copy':
  178. $contact = $params['contact'];
  179. $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language);
  180. $message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), $language->language);
  181. $message['body'][] = $params['message'];
  182. break;
  183. case 'page_autoreply':
  184. $contact = $params['contact'];
  185. $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language);
  186. $message['body'][] = $contact['reply'];
  187. break;
  188. case 'user_mail':
  189. case 'user_copy':
  190. $user = $params['user'];
  191. $account = $params['account'];
  192. $message['subject'] .= '['. variable_get('site_name', 'Drupal') .'] '. $params['subject'];
  193. $message['body'][] = "$account->name,";
  194. $message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)), '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language);
  195. $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE, 'language' => $language))), $language->language);
  196. $message['body'][] = t('Message:', NULL, $language->language);
  197. $message['body'][] = $params['message'];
  198. break;
  199. }
  200. }