Code coverage for /20080809/modules/contact/contact.module

Line #Times calledCode
1
<?php
2
// $Id: contact.module,v 1.108 2008/07/24 16:25:17 dries Exp $
3
4
/**
5
 * @file
6
 * Enables the use of personal and site-wide contact forms.
7
 */
8
9
/**
10
 * Implementation of hook_help().
11
 */
12112
function contact_help($path, $arg) {
13
  switch ($path) {
1482
    case 'admin/help#contact':
1530
      $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>';
1630
      $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>';
1730
      $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>';
1830
      $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>';
1930
      $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>';
2030
      $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>';
2130
      return $output;
2282
    case 'admin/build/contact':
237
      $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>';
247
      if (!module_exists('menu')) {
250
        $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')));
260
      }
27
      else {
287
        $menu_note = '';
29
      }
307
      $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>';
317
      return $output;
320
  }
3375
}
34
35
/**
36
 * Implementation of hook_perm().
37
 */
38112
function contact_perm() {
39
  return array(
408
    'access site-wide contact form' => t('Send feedback to administrators
via e-mail using the site-wide contact form.'),
418
    'administer site-wide contact form' => t('Configure site-wide contact
form administration settings.'),
428
  );
430
}
44
45
/**
46
 * Implementation of hook_menu().
47
 */
48112
function contact_menu() {
492
  $items['admin/build/contact'] = array(
502
    'title' => 'Contact form',
512
    'description' => 'Create a system contact form and set up categories
for the form to use.',
522
    'page callback' => 'contact_admin_categories',
532
    'access arguments' => array('administer site-wide contact form'),
54
  );
552
  $items['admin/build/contact/list'] = array(
562
    'title' => 'List',
572
    'page callback' => 'contact_admin_categories',
582
    'type' => MENU_DEFAULT_LOCAL_TASK,
59
  );
602
  $items['admin/build/contact/add'] = array(
612
    'title' => 'Add category',
622
    'page callback' => 'drupal_get_form',
632
    'page arguments' => array('contact_admin_edit', 3),
642
    'access arguments' => array('administer site-wide contact form'),
652
    'type' => MENU_LOCAL_TASK,
662
    'weight' => 1,
67
  );
682
  $items['admin/build/contact/edit/%contact'] = array(
692
    'title' => 'Edit contact category',
702
    'page callback' => 'drupal_get_form',
712
    'page arguments' => array('contact_admin_edit', 3, 4),
722
    'access arguments' => array('administer site-wide contact form'),
732
    'type' => MENU_CALLBACK,
74
  );
752
  $items['admin/build/contact/delete/%contact'] = array(
762
    'title' => 'Delete contact',
772
    'page callback' => 'drupal_get_form',
782
    'page arguments' => array('contact_admin_delete', 4),
792
    'access arguments' => array('administer site-wide contact form'),
802
    'type' => MENU_CALLBACK,
81
  );
822
  $items['admin/build/contact/settings'] = array(
832
    'title' => 'Settings',
842
    'page callback' => 'drupal_get_form',
852
    'page arguments' => array('contact_admin_settings'),
862
    'access arguments' => array('administer site-wide contact form'),
872
    'type' => MENU_LOCAL_TASK,
882
    'weight' => 2,
89
  );
902
  $items['contact'] = array(
912
    'title' => 'Contact',
922
    'page callback' => 'contact_site_page',
932
    'access arguments' => array('access site-wide contact form'),
942
    'type' => MENU_SUGGESTED_ITEM,
95
  );
962
  $items['user/%user/contact'] = array(
972
    'title' => 'Contact',
982
    'page callback' => 'contact_user_page',
992
    'page arguments' => array(1),
1002
    'type' => MENU_LOCAL_TASK,
1012
    'access callback' => '_contact_user_tab_access',
1022
    'access arguments' => array(1),
1032
    'weight' => 2,
104
  );
1052
  return $items;
1060
}
107
108
/**
109
 * Determine if a user can access to the contact tab.
110
 */
111112
function _contact_user_tab_access($account) {
11210
  global $user;
11310
  if (!isset($account->contact)) {
1140
    $account->contact = FALSE;
1150
  }
116
  return
11710
    $account && $user->uid &&
118
    (
11910
      ($user->uid != $account->uid && $account->contact) ||
1208
      user_access('administer users')
12110
    );
1220
}
123
124
/**
125
 * Load the data for a single contact category.
126
 */
127112
function contact_load($cid) {
12811
  $contact = db_fetch_array(db_query("SELECT * FROM {contact} WHERE cid =
%d", $cid));
12911
  return empty($contact) ? FALSE : $contact;
1300
}
131
132
/**
133
 * Implementation of hook_user().
134
 *
135
 * Allows the user the option of enabling/disabling his personal contact
form.
136
 */
137112
function contact_user($type, &$edit, &$user, $category = NULL) {
13897
  if ($type == 'form' && $category == 'account') {
1390
    $form['contact'] = array('#type' => 'fieldset',
1400
      '#title' => t('Contact settings'),
1410
      '#weight' => 5,
1420
      '#collapsible' => TRUE,
143
    );
1440
    $form['contact']['contact'] = array('#type' => 'checkbox',
1450
      '#title' => t('Personal contact form'),
1460
      '#default_value' => !empty($edit['contact']) ? $edit['contact'] :
FALSE,
1470
      '#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"))),
148
    );
1490
    return $form;
1500
  }
15197
  elseif ($type == 'validate') {
1520
    return array('contact' => isset($edit['contact']) ? $edit['contact'] :
FALSE);
1530
  }
15497
  elseif ($type == 'insert') {
1552
    $edit['contact'] = variable_get('contact_default_status', 1);
1562
  }
15797
}
158
159
/**
160
 * Implementation of hook_mail().
161
 */
162112
function contact_mail($key, &$message, $params) {
1634
  $language = $message['language'];
164
  switch ($key) {
1654
    case 'page_mail':
1664
    case 'page_copy':
1673
      $contact = $params['contact'];
1683
      $message['subject'] .= t('[!category] !subject', array('!category' =>
$contact['category'], '!subject' => $params['subject']),
$language->language);
1693
      $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);
1703
      $message['body'][] = $params['message'];
1713
      break;
1724
    case 'page_autoreply':
1733
      $contact = $params['contact'];
1743
      $message['subject'] .= t('[!category] !subject', array('!category' =>
$contact['category'], '!subject' => $params['subject']),
$language->language);
1753
      $message['body'][] = $contact['reply'];
1763
      break;
1771
    case 'user_mail':
1781
    case 'user_copy':
1791
      $user = $params['user'];
1801
      $account = $params['account'];
1811
      $message['subject'] .= '[' . variable_get('site_name', 'Drupal') . ']
' . $params['subject'];
1821
      $message['body'][] = "$account->name,";
1831
      $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);
1841
      $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);
1851
      $message['body'][] = t('Message:', NULL, $language->language);
1861
      $message['body'][] = $params['message'];
1871
      break;
1880
  }
1894
}
190112