contact.module 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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/documentation/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. * The user object of the user whose contact form is being requested.
  109. */
  110. function _contact_personal_tab_access($account) {
  111. global $user;
  112. // Anonymous users cannot have contact forms.
  113. if (!$account->uid) {
  114. return FALSE;
  115. }
  116. // User administrators should always have access to personal contact forms.
  117. if (user_access('administer users')) {
  118. return TRUE;
  119. }
  120. // Users may not contact themselves.
  121. if ($user->uid == $account->uid) {
  122. return FALSE;
  123. }
  124. // If the requested user has disabled their contact form, or this preference
  125. // has not yet been saved, do not allow users to contact them.
  126. if (empty($account->data['contact'])) {
  127. return FALSE;
  128. }
  129. // If requested user has been blocked, do not allow users to contact them.
  130. if (empty($account->status)) {
  131. return FALSE;
  132. }
  133. return user_access('access user contact forms');
  134. }
  135. /**
  136. * Loads a contact category.
  137. *
  138. * @param $cid
  139. * The contact category ID.
  140. *
  141. * @return
  142. * An array with the contact category's data.
  143. */
  144. function contact_load($cid) {
  145. return db_select('contact', 'c')
  146. ->addTag('translatable')
  147. ->fields('c')
  148. ->condition('cid', $cid)
  149. ->execute()
  150. ->fetchAssoc();
  151. }
  152. /**
  153. * Implements hook_mail().
  154. */
  155. function contact_mail($key, &$message, $params) {
  156. $language = $message['language'];
  157. $variables = array(
  158. '!site-name' => variable_get('site_name', 'Drupal'),
  159. '!subject' => $params['subject'],
  160. '!category' => isset($params['category']['category']) ? $params['category']['category'] : '',
  161. '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
  162. '!sender-name' => format_username($params['sender']),
  163. '!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
  164. );
  165. switch ($key) {
  166. case 'page_mail':
  167. case 'page_copy':
  168. $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
  169. $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, array('langcode' => $language->language));
  170. $message['body'][] = $params['message'];
  171. break;
  172. case 'page_autoreply':
  173. $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
  174. $message['body'][] = $params['category']['reply'];
  175. break;
  176. case 'user_mail':
  177. case 'user_copy':
  178. $variables += array(
  179. '!recipient-name' => format_username($params['recipient']),
  180. '!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
  181. );
  182. $message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->language));
  183. $message['body'][] = t('Hello !recipient-name,', $variables, array('langcode' => $language->language));
  184. $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));
  185. $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));
  186. $message['body'][] = t('Message:', array(), array('langcode' => $language->language));
  187. $message['body'][] = $params['message'];
  188. break;
  189. }
  190. }
  191. /**
  192. * Implements hook_form_FORM_ID_alter().
  193. *
  194. * Add the enable personal contact form to an individual user's account page.
  195. *
  196. * @see user_profile_form()
  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. * @see user_admin_settings()
  227. */
  228. function contact_form_user_admin_settings_alter(&$form, &$form_state) {
  229. $form['contact'] = array(
  230. '#type' => 'fieldset',
  231. '#title' => t('Contact settings'),
  232. '#weight' => 0,
  233. );
  234. $form['contact']['contact_default_status'] = array(
  235. '#type' => 'checkbox',
  236. '#title' => t('Enable the personal contact form by default for new users.'),
  237. '#description' => t('Changing this setting will not affect existing users.'),
  238. '#default_value' => variable_get('contact_default_status', 1),
  239. );
  240. }