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

Line #Times calledCode
1
<?php
2
// $Id: contact.module,v 1.111 2008/10/09 15:15:51 webchick 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
 */
12149
function contact_help($path, $arg) {
13
  switch ($path) {
14104
    case 'admin/help#contact':
1534
      $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>';
1634
      $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>';
1734
      $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>';
1834
      $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>';
1934
      $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>';
2034
      $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>';
2134
      return $output;
22104
    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
  }
3397
}
34
35
/**
36
 * Implementation of hook_perm().
37
 */
38149
function contact_perm() {
39
  return array(
40
    'administer site-wide contact form' => array(
4114
      'title' => t('Administer site-wide contact form'),
4214
      'description' => t('Configure site-wide contact form administration
settings.'),
4314
    ),
44
    'access site-wide contact form' => array(
4514
      'title' => t('Access site-wide contact form'),
4614
      'description' => t('Send feedback to administrators via e-mail using
the site-wide contact form.'),
4714
    ),
4814
  );
490
}
50
51
/**
52
 * Implementation of hook_menu().
53
 */
54149
function contact_menu() {
552
  $items['admin/build/contact'] = array(
562
    'title' => 'Contact form',
572
    'description' => 'Create a system contact form and set up categories
for the form to use.',
582
    'page callback' => 'contact_admin_categories',
592
    'access arguments' => array('administer site-wide contact form'),
60
  );
612
  $items['admin/build/contact/list'] = array(
622
    'title' => 'List',
632
    'page callback' => 'contact_admin_categories',
642
    'type' => MENU_DEFAULT_LOCAL_TASK,
65
  );
662
  $items['admin/build/contact/add'] = array(
672
    'title' => 'Add category',
682
    'page callback' => 'drupal_get_form',
692
    'page arguments' => array('contact_admin_edit', 3),
702
    'access arguments' => array('administer site-wide contact form'),
712
    'type' => MENU_LOCAL_TASK,
722
    'weight' => 1,
73
  );
742
  $items['admin/build/contact/edit/%contact'] = array(
752
    'title' => 'Edit contact category',
762
    'page callback' => 'drupal_get_form',
772
    'page arguments' => array('contact_admin_edit', 3, 4),
782
    'access arguments' => array('administer site-wide contact form'),
792
    'type' => MENU_CALLBACK,
80
  );
812
  $items['admin/build/contact/delete/%contact'] = array(
822
    'title' => 'Delete contact',
832
    'page callback' => 'drupal_get_form',
842
    'page arguments' => array('contact_admin_delete', 4),
852
    'access arguments' => array('administer site-wide contact form'),
862
    'type' => MENU_CALLBACK,
87
  );
882
  $items['admin/build/contact/settings'] = array(
892
    'title' => 'Settings',
902
    'page callback' => 'drupal_get_form',
912
    'page arguments' => array('contact_admin_settings'),
922
    'access arguments' => array('administer site-wide contact form'),
932
    'type' => MENU_LOCAL_TASK,
942
    'weight' => 2,
95
  );
962
  $items['contact'] = array(
972
    'title' => 'Contact',
982
    'page callback' => 'contact_site_page',
992
    'access arguments' => array('access site-wide contact form'),
1002
    'type' => MENU_SUGGESTED_ITEM,
101
  );
1022
  $items['user/%user/contact'] = array(
1032
    'title' => 'Contact',
1042
    'page callback' => 'contact_user_page',
1052
    'page arguments' => array(1),
1062
    'type' => MENU_LOCAL_TASK,
1072
    'access callback' => '_contact_user_tab_access',
1082
    'access arguments' => array(1),
1092
    'weight' => 2,
110
  );
1112
  return $items;
1120
}
113
114
/**
115
 * Determine if a user can access to the contact tab.
116
 */
117149
function _contact_user_tab_access($account) {
11819
  global $user;
11919
  if (!isset($account->contact)) {
1200
    $account->contact = FALSE;
1210
  }
122
  return
12319
    $account && $user->uid &&
124
    (
12519
      ($user->uid != $account->uid && $account->contact) ||
12610
      user_access('administer users')
12719
    );
1280
}
129
130
/**
131
 * Load the data for a single contact category.
132
 */
133149
function contact_load($cid) {
13411
  $contact = db_fetch_array(db_query("SELECT * FROM {contact} WHERE cid =
%d", $cid));
13511
  return empty($contact) ? FALSE : $contact;
1360
}
137
138
/**
139
 * Implementation of hook_user_form().
140
 */
141149
function contact_user_form(&$edit, &$user, $category = NULL) {
1420
  if ($category == 'account') {
1430
    $form['contact'] = array('#type' => 'fieldset',
1440
      '#title' => t('Contact settings'),
1450
      '#weight' => 5,
1460
      '#collapsible' => TRUE,
147
    );
1480
    $form['contact']['contact'] = array('#type' => 'checkbox',
1490
      '#title' => t('Personal contact form'),
1500
      '#default_value' => !empty($edit['contact']) ? $edit['contact'] :
FALSE,
1510
      '#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"))),
152
    );
1530
    return $form;
1540
  }
1550
}
156
157
/**
158
 * Implementation of hook_user_insert().
159
 */
160149
function contact_user_insert(&$edit, &$user, $category = NULL) {
1612
  $edit['contact'] = variable_get('contact_default_status', 1);
1622
}
163
164
/**
165
 * Implementation of hook_user_validate().
166
 */
167149
function contact_user_validate(&$edit, &$user, $category = NULL) {
1680
  return array('contact' => isset($edit['contact']) ? $edit['contact'] :
FALSE);
1690
}
170
171
/**
172
 * Implementation of hook_mail().
173
 */
174149
function contact_mail($key, &$message, $params) {
1757
  $language = $message['language'];
176
  switch ($key) {
1777
    case 'page_mail':
1787
    case 'page_copy':
1793
      $contact = $params['contact'];
1803
      $message['subject'] .= t('[!category] !subject', array('!category' =>
$contact['category'], '!subject' => $params['subject']),
$language->language);
1813
      $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);
1823
      $message['body'][] = $params['message'];
1833
      break;
1847
    case 'page_autoreply':
1853
      $contact = $params['contact'];
1863
      $message['subject'] .= t('[!category] !subject', array('!category' =>
$contact['category'], '!subject' => $params['subject']),
$language->language);
1873
      $message['body'][] = $contact['reply'];
1883
      break;
1894
    case 'user_mail':
1904
    case 'user_copy':
1914
      $user = $params['user'];
1924
      $account = $params['account'];
1934
      $message['subject'] .= '[' . variable_get('site_name', 'Drupal') . ']
' . $params['subject'];
1944
      $message['body'][] = "$account->name,";
1954
      $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);
1964
      $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);
1974
      $message['body'][] = t('Message:', NULL, $language->language);
1984
      $message['body'][] = $params['message'];
1994
      break;
2000
  }
2017
}
202149