contact.pages.inc

User page callbacks for the contact module.

Archivo

drupal-6.x/modules/contact/contact.pages.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * User page callbacks for the contact module.
  5. */
  6. /**
  7. * Site-wide contact page.
  8. */
  9. function contact_site_page() {
  10. global $user;
  11. if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
  12. $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
  13. }
  14. else {
  15. $output = drupal_get_form('contact_mail_page');
  16. }
  17. return $output;
  18. }
  19. function contact_mail_page() {
  20. global $user;
  21. $form = $categories = array();
  22. $result = db_query('SELECT cid, category, selected FROM {contact} ORDER BY weight, category');
  23. while ($category = db_fetch_object($result)) {
  24. $categories[$category->cid] = $category->category;
  25. if ($category->selected) {
  26. $default_category = $category->cid;
  27. }
  28. }
  29. if (count($categories) > 0) {
  30. $form['#token'] = $user->uid ? $user->name . $user->mail : '';
  31. $form['contact_information'] = array('#value' => filter_xss_admin(variable_get('contact_form_information', t('You can leave a message using the contact form below.'))));
  32. $form['name'] = array('#type' => 'textfield',
  33. '#title' => t('Your name'),
  34. '#maxlength' => 255,
  35. '#default_value' => $user->uid ? $user->name : '',
  36. '#required' => TRUE,
  37. );
  38. $form['mail'] = array('#type' => 'textfield',
  39. '#title' => t('Your e-mail address'),
  40. '#maxlength' => 255,
  41. '#default_value' => $user->uid ? $user->mail : '',
  42. '#required' => TRUE,
  43. );
  44. $form['subject'] = array('#type' => 'textfield',
  45. '#title' => t('Subject'),
  46. '#maxlength' => 255,
  47. '#required' => TRUE,
  48. );
  49. if (count($categories) > 1) {
  50. // If there is more than one category available and no default category has been selected,
  51. // prepend a default placeholder value.
  52. if (!isset($default_category)) {
  53. $default_category = t('- Please choose -');
  54. $categories = array($default_category) + $categories;
  55. }
  56. $form['cid'] = array('#type' => 'select',
  57. '#title' => t('Category'),
  58. '#default_value' => $default_category,
  59. '#options' => $categories,
  60. '#required' => TRUE,
  61. );
  62. }
  63. else {
  64. // If there is only one category, store its cid.
  65. $category_keys = array_keys($categories);
  66. $form['cid'] = array('#type' => 'value',
  67. '#value' => array_shift($category_keys),
  68. );
  69. }
  70. $form['message'] = array('#type' => 'textarea',
  71. '#title' => t('Message'),
  72. '#required' => TRUE,
  73. );
  74. // We do not allow anonymous users to send themselves a copy
  75. // because it can be abused to spam people.
  76. if ($user->uid) {
  77. $form['copy'] = array('#type' => 'checkbox',
  78. '#title' => t('Send yourself a copy.'),
  79. );
  80. }
  81. else {
  82. $form['copy'] = array('#type' => 'value', '#value' => FALSE);
  83. }
  84. $form['submit'] = array('#type' => 'submit',
  85. '#value' => t('Send e-mail'),
  86. );
  87. }
  88. else {
  89. drupal_set_message(t('The contact form has not been configured. <a href="@add">Add one or more categories</a> to the form.', array('@add' => url('admin/build/contact/add'))), 'error');
  90. }
  91. return $form;
  92. }
  93. /**
  94. * Validate the site-wide contact page form submission.
  95. */
  96. function contact_mail_page_validate($form, &$form_state) {
  97. if (!$form_state['values']['cid']) {
  98. form_set_error('cid', t('You must select a valid category.'));
  99. }
  100. if (!valid_email_address($form_state['values']['mail'])) {
  101. form_set_error('mail', t('You must enter a valid e-mail address.'));
  102. }
  103. }
  104. /**
  105. * Process the site-wide contact page form submission.
  106. */
  107. function contact_mail_page_submit($form, &$form_state) {
  108. global $language;
  109. $values = $form_state['values'];
  110. // E-mail address of the sender: as the form field is a text field,
  111. // all instances of \r and \n have been automatically stripped from it.
  112. $from = $values['mail'];
  113. // Load category properties and save form values for email composition.
  114. $contact = contact_load($values['cid']);
  115. $values['contact'] = $contact;
  116. // Send the e-mail to the recipients using the site default language.
  117. drupal_mail('contact', 'page_mail', $contact['recipients'], language_default(), $values, $from);
  118. // If the user requests it, send a copy using the current language.
  119. if ($values['copy']) {
  120. drupal_mail('contact', 'page_copy', $from, $language, $values, $from);
  121. }
  122. // Send an auto-reply if necessary using the current language.
  123. if ($contact['reply']) {
  124. drupal_mail('contact', 'page_autoreply', $from, $language, $values, $contact['recipients']);
  125. }
  126. flood_register_event('contact');
  127. watchdog('mail', '%name-from sent an e-mail regarding %category.', array('%name-from' => $values['name'] ." [$from]", '%category' => $contact['category']));
  128. drupal_set_message(t('Your message has been sent.'));
  129. // Jump to home page rather than back to contact page to avoid
  130. // contradictory messages if flood control has been activated.
  131. $form_state['redirect'] = '';
  132. }
  133. /**
  134. * Personal contact page.
  135. */
  136. function contact_user_page($account) {
  137. global $user;
  138. if (!valid_email_address($user->mail)) {
  139. $output = t('You need to provide a valid e-mail address to contact other users. Please update your <a href="@url">user information</a> and try again.', array('@url' => url("user/$user->uid/edit")));
  140. }
  141. else if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
  142. $output = t('You cannot contact more than %number users per hour. Please try again later.', array('%number' => variable_get('contact_hourly_threshold', 3)));
  143. }
  144. else {
  145. drupal_set_title(check_plain($account->name));
  146. $output = drupal_get_form('contact_mail_user', $account);
  147. }
  148. return $output;
  149. }
  150. function contact_mail_user(&$form_state, $recipient) {
  151. global $user;
  152. $form['#token'] = $user->name . $user->mail;
  153. $form['recipient'] = array('#type' => 'value', '#value' => $recipient);
  154. $form['from'] = array('#type' => 'item',
  155. '#title' => t('From'),
  156. '#value' => theme('username', $user) .' &lt;'. check_plain($user->mail) .'&gt;',
  157. );
  158. $form['to'] = array('#type' => 'item',
  159. '#title' => t('To'),
  160. '#value' => theme('username', $recipient),
  161. );
  162. $form['subject'] = array('#type' => 'textfield',
  163. '#title' => t('Subject'),
  164. '#maxlength' => 50,
  165. '#required' => TRUE,
  166. );
  167. $form['message'] = array('#type' => 'textarea',
  168. '#title' => t('Message'),
  169. '#rows' => 15,
  170. '#required' => TRUE,
  171. );
  172. $form['copy'] = array('#type' => 'checkbox',
  173. '#title' => t('Send yourself a copy.'),
  174. );
  175. $form['submit'] = array('#type' => 'submit',
  176. '#value' => t('Send e-mail'),
  177. );
  178. return $form;
  179. }
  180. /**
  181. * Process the personal contact page form submission.
  182. */
  183. function contact_mail_user_submit($form, &$form_state) {
  184. global $user, $language;
  185. $account = $form_state['values']['recipient'];
  186. // Send from the current user to the requested user.
  187. $to = $account->mail;
  188. $from = $user->mail;
  189. // Save both users and all form values for email composition.
  190. $values = $form_state['values'];
  191. $values['account'] = $account;
  192. $values['user'] = $user;
  193. // Send the e-mail in the requested user language.
  194. drupal_mail('contact', 'user_mail', $to, user_preferred_language($account), $values, $from);
  195. // Send a copy if requested, using current page language.
  196. if ($form_state['values']['copy']) {
  197. drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
  198. }
  199. flood_register_event('contact');
  200. watchdog('mail', '%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name));
  201. drupal_set_message(t('The message has been sent.'));
  202. // Back to the requested users profile page.
  203. $form_state['redirect'] = "user/$account->uid";
  204. }

Functions

Nombreorden descendente Descripción
contact_mail_page
contact_mail_page_submit Process the site-wide contact page form submission.
contact_mail_page_validate Validate the site-wide contact page form submission.
contact_mail_user
contact_mail_user_submit Process the personal contact page form submission.
contact_site_page Site-wide contact page.
contact_user_page Personal contact page.