contact.module 13.8 KB
Newer Older
Dries's avatar
 
Dries committed
1 2 3 4
<?php

/**
 * @file
5
 * Enables the use of personal and site-wide contact forms.
Dries's avatar
 
Dries committed
6 7
 */

8 9
use Drupal\contact\Category;

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

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

Dries's avatar
 
Dries committed
56
/**
57
 * Implements hook_menu().
Dries's avatar
 
Dries committed
58
 */
59
function contact_menu() {
60
  $items['admin/structure/contact'] = array(
61 62
    'title' => 'Contact form',
    'description' => 'Create a system contact form and set up categories for the form to use.',
63
    'page callback' => 'contact_category_list',
64
    'access arguments' => array('administer contact forms'),
65
    'file' => 'contact.admin.inc',
66
  );
67
  $items['admin/structure/contact/add'] = array(
68
    'title' => 'Add category',
69
    'page callback' => 'contact_category_add',
70
    'access arguments' => array('administer contact forms'),
71
    'type' => MENU_LOCAL_ACTION,
72
    'weight' => 1,
73
    'file' => 'contact.admin.inc',
74
  );
75
  $items['admin/structure/contact/manage/%contact_category/edit'] = array(
76
    'title' => 'Edit contact category',
77 78
    'page callback' => 'entity_get_form',
    'page arguments' => array(4),
79
    'access arguments' => array('administer contact forms'),
80
  );
81 82
  $items['admin/structure/contact/manage/%contact_category/delete'] = array(
    'title' => 'Delete contact category',
83
    'page callback' => 'drupal_get_form',
84
    'page arguments' => array('contact_category_delete_form', 4),
85
    'access arguments' => array('administer contact forms'),
86
    'file' => 'contact.admin.inc',
87 88
  );
  $items['contact'] = array(
89
    'title' => 'Contact',
90 91
    'page callback' => 'drupal_get_form',
    'page arguments' => array('contact_site_form'),
92
    'access arguments' => array('access site-wide contact form'),
93
    'menu_name' => 'main',
94
    'type' => MENU_SUGGESTED_ITEM,
95
    'file' => 'contact.pages.inc',
96
  );
97
  $items['user/%user/contact'] = array(
98
    'title' => 'Contact',
99 100
    'page callback' => 'drupal_get_form',
    'page arguments' => array('contact_personal_form', 1),
101
    'type' => MENU_LOCAL_TASK,
102
    'access callback' => '_contact_personal_tab_access',
103 104
    'access arguments' => array(1),
    'weight' => 2,
105
    'file' => 'contact.pages.inc',
106
  );
Dries's avatar
 
Dries committed
107 108 109
  return $items;
}

110
/**
111 112
 * Access callback: Checks access for a user's personal contact form.
 *
113
 * @param $account
114 115
 *   The user object of the user whose contact form is being requested.
 *
116 117 118 119
 * @return
 *   A Boolean TRUE if the user has permission to access a personal contact
 *   form.
 *
120
 * @see contact_menu()
121
 */
122
function _contact_personal_tab_access($account) {
123
  global $user;
124

125 126
  // Anonymous users cannot have contact forms.
  if (!$account->uid) {
127 128 129 130 131 132 133 134
    return FALSE;
  }

  // Users may not contact themselves.
  if ($user->uid == $account->uid) {
    return FALSE;
  }

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

140 141
  // If the requested user has disabled their contact form, or this preference
  // has not yet been saved, do not allow users to contact them.
142
  if (empty($account->data['contact'])) {
143 144 145
    return FALSE;
  }

146 147 148 149 150
  // If requested user has been blocked, do not allow users to contact them.
  if (empty($account->status)) {
    return FALSE;
  }

151
  return user_access('access user contact forms');
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/**
 * Implements MODULE_config_import_create().
 */
function contact_config_import_create($name, $new_config, $old_config) {
  if (strpos($name, 'contact.category.') !== 0) {
    return FALSE;
  }

  $category = entity_create('contact_category', $new_config->get());
  $category->save();
  return TRUE;
}

/**
 * Implements MODULE_config_import_change().
 */
function contact_config_import_change($name, $new_config, $old_config) {
  if (strpos($name, 'contact.category.') !== 0) {
    return FALSE;
  }

  list(, , $id) = explode('.', $name);
  $category = entity_load('contact_category', $id);

  $category->original = clone $category;
  foreach ($old_config->get() as $property => $value) {
    $category->original->$property = $value;
  }

  foreach ($new_config->get() as $property => $value) {
    $category->$property = $value;
  }

  $category->save();
  return TRUE;
}

/**
 * Implements MODULE_config_import_delete().
 */
function contact_config_import_delete($name, $new_config, $old_config) {
  if (strpos($name, 'contact.category.') !== 0) {
    return FALSE;
  }

  list(, , $id) = explode('.', $name);
  entity_delete_multiple('contact_category', array($id));
  return TRUE;
}

/**
 * Implements hook_entity_info().
 */
function contact_entity_info() {
  $types['contact_category'] = array(
    'label' => 'Category',
    'entity class' => 'Drupal\contact\Category',
    'controller class' => 'Drupal\Core\Config\Entity\ConfigStorageController',
    'list controller class' => 'Drupal\contact\CategoryListController',
    'form controller class' => array(
      'default' => 'Drupal\contact\CategoryFormController',
    ),
    'uri callback' => 'contact_category_uri',
    'config prefix' => 'contact.category',
    'entity keys' => array(
      'id' => 'id',
      'label' => 'label',
      'uuid' => 'uuid',
    ),
  );
  return $types;
}

227
/**
228
 * Loads a contact category.
229
 *
230 231
 * @param $id
 *   The ID of the contact category to load.
232
 *
233 234
 * @return Drupal\contact\Category|false
 *   A Category object or FALSE if the requested $id does not exist.
235
 */
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
function contact_category_load($id) {
  return entity_load('contact_category', $id);
}

/**
 * Entity URI callback.
 *
 * @param Drupal\category\Category $category
 *   A contact category entity.
 *
 * @return array
 *   An array with 'path' as the key and the path to the category as the value.
 */
function contact_category_uri(Category $category) {
  return array(
    'path' => 'admin/structure/contact/manage/' . $category->id(),
  );
253 254
}

255
/**
256
 * Implements hook_mail().
257 258
 */
function contact_mail($key, &$message, $params) {
259
  $language = language_load($message['langcode']);
260
  $variables = array(
261
    '!site-name' => config('system.site')->get('name'),
262
    '!subject' => $params['subject'],
263
    '!category' => isset($params['category']) ? $params['category']->label() : '',
264
    '!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
265
    '!sender-name' => user_format_name($params['sender']),
266
    '!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
267 268
  );

269 270 271
  switch ($key) {
    case 'page_mail':
    case 'page_copy':
272 273
      $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->langcode));
      $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, array('langcode' => $language->langcode));
274 275
      $message['body'][] = $params['message'];
      break;
276

277
    case 'page_autoreply':
278
      $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->langcode));
279
      $message['body'][] = $params['category']->reply;
280
      break;
281

282 283
    case 'user_mail':
    case 'user_copy':
284
      $variables += array(
285
        '!recipient-name' => user_format_name($params['recipient']),
286
        '!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
287
      );
288 289 290 291 292
      $message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->langcode));
      $message['body'][] = t('Hello !recipient-name,', $variables, array('langcode' => $language->langcode));
      $message['body'][] = t("!sender-name (!sender-url) has sent you a message via your contact form (!form-url) at !site-name.", $variables, array('langcode' => $language->langcode));
      $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !recipient-edit-url.", $variables, array('langcode' => $language->langcode));
      $message['body'][] = t('Message:', array(), array('langcode' => $language->langcode));
293 294 295
      $message['body'][] = $params['message'];
      break;
  }
296
}
297

298
/**
299
 * Implements hook_form_FORM_ID_alter().
300 301
 *
 * Add the enable personal contact form to an individual user's account page.
302 303
 *
 * @see user_profile_form()
304 305
 */
function contact_form_user_profile_form_alter(&$form, &$form_state) {
306 307 308 309 310 311
  $form['contact'] = array(
    '#type' => 'fieldset',
    '#title' => t('Contact settings'),
    '#weight' => 5,
    '#collapsible' => TRUE,
  );
312
  $account = $form_state['controller']->getEntity($form_state);
313 314 315 316 317 318
  $form['contact']['contact'] = array(
    '#type' => 'checkbox',
    '#title' => t('Personal contact form'),
    '#default_value' => !empty($account->data['contact']) ? $account->data['contact'] : FALSE,
    '#description' => t('Allow other users to contact you via a <a href="@url">personal contact form</a> 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.', array('@url' => url("user/$account->uid/contact"))),
  );
319 320
}

321 322 323
/**
 * Implements hook_user_presave().
 */
324
function contact_user_presave($account) {
325
  $account->data['contact'] = isset($account->contact) ? $account->contact : config('contact.settings')->get('user_default_enabled');
326 327
}

328
/**
329
 * Implements hook_form_FORM_ID_alter().
330 331
 *
 * Add the default personal contact setting on the user settings page.
332 333
 *
 * @see user_admin_settings()
334
 */
335
function contact_form_user_admin_settings_alter(&$form, &$form_state) {
336 337
  $form['contact'] = array(
    '#type' => 'fieldset',
338
    '#title' => t('Contact settings'),
339 340 341 342 343
    '#weight' => 0,
  );
  $form['contact']['contact_default_status'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable the personal contact form by default for new users.'),
344
    '#description' => t('Changing this setting will not affect existing users.'),
345
    '#default_value' => config('contact.settings')->get('user_default_enabled'),
346
  );
347 348 349 350 351 352 353 354 355 356 357 358 359
  // 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) {
  config('contact.settings')
    ->set('user_default_enabled', $form_state['values']['contact_default_status'])
    ->save();
360
}