contact.module 14.1 KB
Newer Older
1 2 3 4
<?php

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

8
use Drupal\contact\Entity\Category;
9
use Drupal\user\UserInterface;
10

11
/**
12
 * Implements hook_help().
13
 */
14 15
function contact_help($path, $arg) {
  switch ($path) {
16
    case 'admin/help#contact':
17 18
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
19
      $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>';
20 21 22 23 24
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('User contact forms') . '</dt>';
      $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>';
      $output .= '<dt>' . t('Site-wide contact forms') . '</dt>';
25
      $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>';
26
      $output .= '<dt>' . t('Navigation') . '</dt>';
27
      $output .= '<dd>' . t('When the site-wide contact form is enabled, a link in the <em>Footer</em> menu is created, which you can modify on the <a href="@menu">Menus administration page</a>.', array('@menu' => url('admin/structure/menu'))) . '</dd>';
28 29
      $output .= '<dt>' . t('Customization') . '</dt>';
      $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>';
30
      $output .= '</dl>';
31
      return $output;
32
    case 'admin/structure/contact':
33
      $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>';
34
      $output .= '<p>' . t('A <em>Contact</em> menu item is added to the <em>Footer</em> menu, which you can modify on the <a href="@menu-settings">Menus administration page</a>.', array('@menu-settings' => url('admin/structure/menu'))) . '</p>';
35
      $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>';
36
      return $output;
37 38 39
  }
}

40
/**
41
 * Implements hook_permission().
42
 */
43
function contact_permission() {
44
  return array(
45
    'administer contact forms' => array(
46
      'title' => t('Administer contact forms and contact form settings'),
47 48
    ),
    'access site-wide contact form' => array(
49
      'title' => t('Use the site-wide contact form'),
50
    ),
51
    'access user contact forms' => array(
52
      'title' => t("Use users' personal contact forms"),
53
    ),
54
  );
55
}
56

57
/**
58
 * Implements hook_menu().
59
 */
60
function contact_menu() {
61
  $items['admin/structure/contact'] = array(
62
    'title' => 'Contact form categories',
63
    'description' => 'Create a system contact form and set up categories for the form to use.',
64
    'route_name' => 'contact.category_list',
65
  );
66
  $items['admin/structure/contact/add'] = array(
67
    'title' => 'Add category',
68
    'route_name' => 'contact.category_add',
69
    'type' => MENU_LOCAL_ACTION,
70 71
    'weight' => 1,
  );
72
  $items['admin/structure/contact/manage/%contact_category'] = array(
73
    'title' => 'Edit contact category',
74
    'route_name' => 'contact.category_edit',
75 76 77 78
  );
  $items['admin/structure/contact/manage/%contact_category/edit'] = array(
    'title' => 'Edit',
    'type' => MENU_DEFAULT_LOCAL_TASK,
79
  );
80

81
  $items['contact'] = array(
82
    'title' => 'Contact',
83
    'page callback' => 'contact_site_page',
84
    'access arguments' => array('access site-wide contact form'),
85
    'menu_name' => 'footer',
86
    'type' => MENU_SUGGESTED_ITEM,
87
    'file' => 'contact.pages.inc',
88
  );
89 90 91 92 93 94 95 96 97 98
  $items['contact/%contact_category'] = array(
    'title' => 'Contact category form',
    'title callback' => 'entity_page_label',
    'title arguments' => array(1),
    'page callback' => 'contact_site_page',
    'page arguments' => array(1),
    'access arguments' => array('access site-wide contact form'),
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
    'file' => 'contact.pages.inc',
  );
99
  $items['user/%user/contact'] = array(
100
    'title' => 'Contact',
101 102
    'page callback' => 'contact_personal_page',
    'page arguments' => array(1),
103
    'type' => MENU_LOCAL_TASK,
104
    'access callback' => '_contact_personal_tab_access',
105 106
    'access arguments' => array(1),
    'weight' => 2,
107
    'file' => 'contact.pages.inc',
108
  );
109 110 111
  return $items;
}

112
/**
113 114
 * Access callback: Checks access for a user's personal contact form.
 *
115
 * @param $account
116 117 118
 *   The user object of the user whose contact form is being requested.
 *
 * @see contact_menu()
119
 */
120
function _contact_personal_tab_access(UserInterface $account) {
121
  global $user;
122

123
  // Anonymous users cannot have contact forms.
124
  if ($account->isAnonymous()) {
125 126 127 128
    return FALSE;
  }

  // Users may not contact themselves.
129
  if ($user->id() == $account->id()) {
130 131 132
    return FALSE;
  }

133 134 135 136 137
  // User administrators should always have access to personal contact forms.
  if (user_access('administer users')) {
    return TRUE;
  }

138
  // If requested user has been blocked, do not allow users to contact them.
139
  if ($account->isBlocked()) {
140 141 142
    return FALSE;
  }

143 144
  // If the requested user has disabled their contact form, do not allow users
  // to contact them.
145
  $account_data = \Drupal::service('user.data')->get('contact', $account->id(), 'enabled');
146 147 148 149 150
  if (isset($account_data) && empty($account_data)) {
    return FALSE;
  }
  // If the requested user did not save a preference yet, deny access if the
  // configured default is disabled.
151
  elseif (!\Drupal::config('contact.settings')->get('user_default_enabled')) {
152 153 154
    return FALSE;
  }

155
  return user_access('access user contact forms');
156 157
}

158
/**
159
 * Implements hook_entity_bundle_info().
160
 */
161 162
function contact_entity_bundle_info() {
  $bundles = array();
163
  foreach (config_get_storage_names_with_prefix('contact.category.') as $config_name) {
164
    $config = \Drupal::config($config_name);
165
    $bundles['contact_message'][$config->get('id')]['label'] = $config->get('label');
166
  }
167
  return $bundles;
168 169 170 171 172 173 174
}

/**
 * Implements hook_field_extra_fields().
 */
function contact_field_extra_fields() {
  $fields = array();
175
  foreach (array_keys(entity_get_bundles('contact_message')) as $bundle) {
176 177 178 179 180 181 182 183 184 185
    $fields['contact_message'][$bundle]['form']['name'] = array(
      'label' => t('Sender name'),
      'description' => t('Text'),
      'weight' => -50,
    );
    $fields['contact_message'][$bundle]['form']['mail'] = array(
      'label' => t('Sender e-mail'),
      'description' => t('E-mail'),
      'weight' => -40,
    );
186 187 188 189 190 191 192
    if ($bundle == 'personal') {
      $fields['contact_message'][$bundle]['form']['recipient'] = array(
        'label' => t('Recipient user name'),
        'description' => t('User'),
        'weight' => -30,
      );
    }
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    $fields['contact_message'][$bundle]['form']['subject'] = array(
      'label' => t('Subject'),
      'description' => t('Text'),
      'weight' => -10,
    );
    $fields['contact_message'][$bundle]['form']['message'] = array(
      'label' => t('Message'),
      'description' => t('Long text'),
      'weight' => 0,
    );
    $fields['contact_message'][$bundle]['form']['copy'] = array(
      'label' => t('Send copy to sender'),
      'description' => t('Option'),
      'weight' => 50,
    );

    $fields['contact_message'][$bundle]['display']['message'] = array(
      'label' => t('Message'),
      'description' => t('The main contact message'),
      'weight' => 0,
    );
  }
215 216 217 218 219 220

  $fields['user']['user']['form']['contact'] = array(
    'label' => t('Contact settings'),
    'description' => t('Contact module form element.'),
    'weight' => 5,
  );
221 222 223
  return $fields;
}

224
/**
225
 * Loads a contact category.
226
 *
227 228
 * @param $id
 *   The ID of the contact category to load.
229
 *
230
 * @return \Drupal\contact\Entity\Category|null
231
 *   A Category object or NULL if the requested $id does not exist.
232
 */
233 234 235 236
function contact_category_load($id) {
  return entity_load('contact_category', $id);
}

237
/**
238
 * Implements hook_mail().
239 240
 */
function contact_mail($key, &$message, $params) {
241 242
  $contact_message = $params['contact_message'];
  $sender = $params['sender'];
243
  $language = language_load($message['langcode']);
244

245
  $variables = array(
246
    '!site-name' => \Drupal::config('system.site')->get('name'),
247 248
    '!subject' => $contact_message->getSubject(),
    '!category' => !empty($params['contact_category']) ? $params['contact_category']->label() : NULL,
249
    '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
250
    '!sender-name' => user_format_name($sender),
251
  );
252
  if ($sender->isAuthenticated()) {
253 254 255 256
    $sender_uri = $sender->uri();
    $variables['!sender-url'] = url($sender_uri['path'], array('absolute' => TRUE, 'language' => $language) + $sender_uri['options']);
  }
  else {
257
    $variables['!sender-url'] = $params['sender']->getEmail();
258 259
  }

260
  $options = array('langcode' => $language->id);
261

262 263 264
  switch ($key) {
    case 'page_mail':
    case 'page_copy':
265 266
      $message['subject'] .= t('[!category] !subject', $variables, $options);
      $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, $options);
267
      $build = entity_view($contact_message, 'mail', $language->id);
268
      $message['body'][] = drupal_render($build);
269
      break;
270

271
    case 'page_autoreply':
272 273
      $message['subject'] .= t('[!category] !subject', $variables, $options);
      $message['body'][] = $params['contact_category']->reply;
274
      break;
275

276 277
    case 'user_mail':
    case 'user_copy':
278
      $variables += array(
279
        '!recipient-name' => user_format_name($params['recipient']),
280
        '!recipient-edit-url' => url('user/' . $params['recipient']->id() . '/edit', array('absolute' => TRUE, 'language' => $language)),
281
      );
282 283 284 285
      $message['subject'] .= t('[!site-name] !subject', $variables, $options);
      $message['body'][] = t('Hello !recipient-name,', $variables, $options);
      $message['body'][] = t("!sender-name (!sender-url) has sent you a message via your contact form at !site-name.", $variables, $options);
      $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !recipient-edit-url.", $variables, $options);
286
      $build = entity_view($contact_message, 'mail', $language->id);
287
      $message['body'][] = drupal_render($build);
288 289
      break;
  }
290
}
291

292
/**
293
 * Implements hook_form_FORM_ID_alter().
294 295
 *
 * Add the enable personal contact form to an individual user's account page.
296
 *
297
 * @see \Drupal\user\ProfileFormController::form()
298
 */
299
function contact_form_user_form_alter(&$form, &$form_state) {
300
  $form['contact'] = array(
301
    '#type' => 'details',
302 303 304
    '#title' => t('Contact settings'),
    '#weight' => 5,
  );
305
  $account = $form_state['controller']->getEntity();
306
  $account_data = !user_is_anonymous() ? \Drupal::service('user.data')->get('contact', $account->id(), 'enabled') : NULL;
307 308 309
  $form['contact']['contact'] = array(
    '#type' => 'checkbox',
    '#title' => t('Personal contact form'),
310
    '#default_value' => isset($account_data) ? $account_data : \Drupal::config('contact.settings')->get('user_default_enabled'),
311
    '#description' => t('Allow other users to contact you via a personal contact form 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.'),
312
  );
313
  $form['actions']['submit']['#submit'][] = 'contact_user_profile_form_submit';
314 315
}

316
/**
317
 * Submit callback for the user profile form to save the contact page setting.
318
 */
319 320 321
function contact_user_profile_form_submit($form, &$form_state) {
  $account = $form_state['controller']->getEntity();
  if ($account->id() && isset($form_state['values']['contact'])) {
322
    \Drupal::service('user.data')->set('contact', $account->id(), 'enabled', (int) $form_state['values']['contact']);
323
  }
324 325
}

326
/**
327
 * Implements hook_form_FORM_ID_alter().
328 329
 *
 * Add the default personal contact setting on the user settings page.
330 331
 *
 * @see user_admin_settings()
332
 */
333
function contact_form_user_admin_settings_alter(&$form, &$form_state) {
334
  $form['contact'] = array(
335
    '#type' => 'details',
336
    '#title' => t('Contact settings'),
337 338 339 340 341
    '#weight' => 0,
  );
  $form['contact']['contact_default_status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable the personal contact form by default for new users.'),
342
    '#description' => t('Changing this setting will not affect existing users.'),
343
    '#default_value' => \Drupal::config('contact.settings')->get('user_default_enabled'),
344
  );
345 346 347 348 349 350 351 352 353 354
  // Add submit handler to save contact configuration.
  $form['#submit'][] = 'contact_form_user_admin_settings_submit';
}

/**
 * Form submission handler for user_admin_settings().
 *
 * @see contact_form_user_admin_settings_alter()
 */
function contact_form_user_admin_settings_submit($form, &$form_state) {
355
  \Drupal::config('contact.settings')
356 357
    ->set('user_default_enabled', $form_state['values']['contact_default_status'])
    ->save();
358
}