summaryrefslogtreecommitdiff
path: root/modules/contact/contact.module
blob: 0a9151e5fc8daf42475bb5a9063624dc866f7b46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
// $Id$

/**
 * @file
 * Enables the use of personal and site-wide contact forms.
 */

/**
 * Implementation of hook_help().
 */
function contact_help($path, $arg) {
  switch ($path) {
    case 'admin/help#contact':
      $output = '<p>'. t('The contact module enables the use of both personal and site-wide contact forms, thereby facilitating easy communication within the community. While personal contact forms allow users to contact each other by e-mail, site-wide forms allow community members to contact the site administration from a central location. Users can specify a subject and message in the contact form, and also request that a copy of the e-mail be sent to their own address.') .'</p>';
      $output .= '<p>'. t("Users can activate/deactivate their personal contact forms in their account settings. Upon activation, a contact tab will appear in their user profiles. Privileged users such as site administrators are able to contact users even if they have chosen not to enable this feature.") .'</p>';
      $output .= '<p>'. t("Note that the contact tab will not appear when a user views his or her own profile; only when viewing another user's profile, if that user's contact form is enabled.") .'</p>';
      $output .= '<p>'. t('If the menu module is enabled, a menu item linking to the site-wide contact page is added to the navigation block. It is disabled by default, but can be enabled via the <a href="@menu-module">menu management</a> page. Links to the contact page may also be added to the primary and secondary links using the same page.', array('@menu-module' => url('admin/build/menu'))) .'</p>';
      $output .= '<p>'. t('For more information, please read the configuration and customization handbook page for the <a href="@contact">contact module</a>.', array('@contact' => url('http://drupal.org/handbook/modules/contact/', array('absolute' => TRUE)))) .'</p>';
      return $output;
    case 'admin/build/contact':
      $output = '<p>'. t('This page lets you setup <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>';
      if (!module_exists('menu')) {
        $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')));
      }
      else {
        $menu_note = '';
      }
      $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>';
      return $output;
  }
}

/**
 * Implementation of hook_perm
 */
function contact_perm() {
  return array('access site-wide contact form', 'administer site-wide contact form');
}
/**
 * Implementation of hook_menu().
 */
function contact_menu() {
  $items['admin/build/contact'] = array(
    'title' => 'Contact form',
    'description' => 'Create a system contact form and set up categories for the form to use.',
    'page callback' => 'contact_admin_categories',
    'access arguments' => array('administer site-wide contact form'),
    'file' => 'contact.admin.inc',
  );
  $items['admin/build/contact/list'] = array(
    'title' => 'List',
    'page callback' => 'contact_admin_categories',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'file' => 'contact.admin.inc',
  );
  $items['admin/build/contact/add'] = array(
    'title' => 'Add category',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('contact_admin_edit', 3),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
    'file' => 'contact.admin.inc',
  );
  $items['admin/build/contact/edit/%contact'] = array(
    'title' => 'Edit contact category',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('contact_admin_edit', 3, 4),
    'type' => MENU_CALLBACK,
    'file' => 'contact.admin.inc',
  );
  $items['admin/build/contact/delete/%contact'] = array(
    'title' => 'Delete contact',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('contact_admin_delete', 4),
    'type' => MENU_CALLBACK,
    'file' => 'contact.admin.inc',
  );
  $items['admin/build/contact/settings'] = array(
    'title' => 'Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('contact_admin_settings'),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'contact.admin.inc',
  );
  $items['contact'] = array(
    'title' => 'Contact',
    'page callback' => 'contact_site_page',
    'access arguments' => array('access site-wide contact form'),
    'type' => MENU_SUGGESTED_ITEM,
    'file' => 'contact.pages.inc',
  );
  $items['user/%user/contact'] = array(
    'title' => 'Contact',
    'page callback' => 'contact_user_page',
    'page arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
    'access callback' => '_contact_user_tab_access',
    'access arguments' => array(1),
    'weight' => 2,
    'file' => 'contact.pages.inc',
  );
  return $items;
}

/**
 * Determine if a user can access to the contact tab.
 */
function _contact_user_tab_access($account) {
  global $user;
  if (!isset($account->contact)) {
    $account->contact = FALSE;
  }
  return
    $account &&
    (
      ($user->uid != $account->uid && $account->contact) ||
      user_access('administer users')
    );
}

/**
 * Load the data for a single contact category.
 */
function contact_load($cid) {
  $contact = db_fetch_array(db_query("SELECT * FROM {contact} WHERE cid = %d", $cid));
  return empty($contact) ? FALSE : $contact;
}

/**
 * Implementation of hook_user().
 *
 * Allows the user the option of enabling/disabling his personal contact form.
 */
function contact_user($type, &$edit, &$user, $category = NULL) {
  if ($type == 'form' && $category == 'account') {
    $form['contact'] = array('#type' => 'fieldset',
      '#title' => t('Contact settings'),
      '#weight' => 5,
      '#collapsible' => TRUE,
    );
    $form['contact']['contact'] = array('#type' => 'checkbox',
      '#title' => t('Personal contact form'),
      '#default_value' => !empty($edit['contact']) ? $edit['contact'] : FALSE,
      '#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"))),
    );
    return $form;
  }
  elseif ($type == 'validate') {
    return array('contact' => isset($edit['contact']) ? $edit['contact'] : FALSE);
  }
  elseif ($type == 'insert') {
    $edit['contact'] = variable_get('contact_default_status', 1);
  }
}

/**
 * Implementation of hook_mail().
 */
function contact_mail($key, &$message, $params) {
  $language = $message['language'];
  switch ($key) {
    case 'page_mail':
    case 'page_copy':
      $contact = $params['contact'];
      $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language);
      $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);
      $message['body'][] = $params['message'];
      break;
    case 'page_autoreply':
      $contact = $params['contact'];
      $message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), $language->language);
      $message['body'][] = $contact['reply'];
      break;
    case 'user_mail':
    case 'user_copy':
      $user = $params['user'];
      $account = $params['account'];
      $message['subject'] .= '['. variable_get('site_name', 'Drupal') .'] '. $params['subject'];
      $message['body'][] = "$account->name,";
      $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);
      $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);
      $message['body'][] = t('Message:', NULL, $language->language);
      $message['body'][] = $params['message'];
      break;
  }
}