2004-12-07 17:03:46 +00:00
<?php
/**
* @file
2006-01-14 10:33:22 +00:00
* Enables the use of personal and site-wide contact forms.
2004-12-07 17:03:46 +00:00
*/
2012-10-30 20:37:18 +00:00
use Drupal\contact\Plugin\Core\Entity\Category;
2012-10-18 08:55:55 +00:00
2004-12-07 17:03:46 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_help().
2004-12-07 17:03:46 +00:00
*/
2007-06-30 19:46:58 +00:00
function contact_help($path, $arg) {
switch ($path) {
2005-11-01 10:17:34 +00:00
case 'admin/help#contact':
2009-11-29 19:54:40 +00:00
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
2012-03-21 05:51:30 +00:00
$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>';
2009-11-29 19:54:40 +00:00
$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>';
2010-01-14 05:51:59 +00:00
$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>';
2009-11-29 19:54:40 +00:00
$output .= '<dt>' . t('Navigation') . '</dt>';
2012-10-25 15:53:18 +00:00
$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>';
2010-01-14 05:51:59 +00:00
$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>';
2009-11-29 19:54:40 +00:00
$output .= '</dl>';
2005-11-01 10:17:34 +00:00
return $output;
2009-07-20 18:51:36 +00:00
case 'admin/structure/contact':
2010-01-14 05:51:59 +00:00
$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>';
2012-10-25 15:53:18 +00:00
$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>';
2010-01-14 05:51:59 +00:00
$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>';
2007-04-13 08:56:59 +00:00
return $output;
2004-12-07 17:03:46 +00:00
}
}
2006-05-12 18:12:21 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_permission().
2006-05-12 18:12:21 +00:00
*/
2009-07-05 18:00:11 +00:00
function contact_permission() {
2008-02-20 13:46:43 +00:00
return array(
2009-10-09 00:54:33 +00:00
'administer contact forms' => array(
2009-12-01 13:14:43 +00:00
'title' => t('Administer contact forms and contact form settings'),
2008-10-09 15:15:55 +00:00
),
'access site-wide contact form' => array(
2009-12-01 13:14:43 +00:00
'title' => t('Use the site-wide contact form'),
2008-10-09 15:15:55 +00:00
),
2009-10-11 18:34:10 +00:00
'access user contact forms' => array(
2009-12-01 13:14:43 +00:00
'title' => t("Use users' personal contact forms"),
2009-10-11 18:34:10 +00:00
),
2008-02-20 13:46:43 +00:00
);
2006-05-12 18:12:21 +00:00
}
2008-02-20 13:46:43 +00:00
2004-12-07 17:03:46 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_menu().
2004-12-07 17:03:46 +00:00
*/
2007-01-24 14:48:36 +00:00
function contact_menu() {
2009-07-20 18:51:36 +00:00
$items['admin/structure/contact'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Contact form',
'description' => 'Create a system contact form and set up categories for the form to use.',
2009-10-09 02:34:07 +00:00
'page callback' => 'contact_category_list',
2009-10-09 00:54:33 +00:00
'access arguments' => array('administer contact forms'),
2009-08-24 00:14:23 +00:00
'file' => 'contact.admin.inc',
2007-01-24 14:48:36 +00:00
);
2009-07-20 18:51:36 +00:00
$items['admin/structure/contact/add'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Add category',
2012-10-18 08:55:55 +00:00
'page callback' => 'contact_category_add',
2009-10-09 00:54:33 +00:00
'access arguments' => array('administer contact forms'),
2009-08-23 01:05:12 +00:00
'type' => MENU_LOCAL_ACTION,
2007-01-24 14:48:36 +00:00
'weight' => 1,
2009-08-24 00:14:23 +00:00
'file' => 'contact.admin.inc',
2007-01-24 14:48:36 +00:00
);
2012-11-06 14:03:05 +00:00
$items['admin/structure/contact/manage/%contact_category'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Edit contact category',
2012-11-06 14:03:05 +00:00
'page callback' => 'contact_category_edit',
2012-10-18 08:55:55 +00:00
'page arguments' => array(4),
2009-10-09 00:54:33 +00:00
'access arguments' => array('administer contact forms'),
2012-11-06 14:03:05 +00:00
'file' => 'contact.admin.inc',
);
$items['admin/structure/contact/manage/%contact_category/edit'] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
2007-01-24 14:48:36 +00:00
);
2012-10-18 08:55:55 +00:00
$items['admin/structure/contact/manage/%contact_category/delete'] = array(
2012-11-06 14:03:05 +00:00
'title' => 'Delete',
2007-01-24 14:48:36 +00:00
'page callback' => 'drupal_get_form',
2009-10-09 02:34:07 +00:00
'page arguments' => array('contact_category_delete_form', 4),
2009-10-09 00:54:33 +00:00
'access arguments' => array('administer contact forms'),
2012-11-06 14:03:05 +00:00
'type' => MENU_LOCAL_TASK,
2009-08-24 00:14:23 +00:00
'file' => 'contact.admin.inc',
2007-01-24 14:48:36 +00:00
);
$items['contact'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Contact',
2009-10-11 01:06:27 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('contact_site_form'),
2007-01-24 14:48:36 +00:00
'access arguments' => array('access site-wide contact form'),
2012-11-20 11:36:51 +00:00
'menu_name' => 'footer',
2007-01-24 14:48:36 +00:00
'type' => MENU_SUGGESTED_ITEM,
2009-08-24 00:14:23 +00:00
'file' => 'contact.pages.inc',
2007-01-24 14:48:36 +00:00
);
2007-02-11 09:30:51 +00:00
$items['user/%user/contact'] = array(
2007-04-30 17:03:29 +00:00
'title' => 'Contact',
2009-10-11 01:06:27 +00:00
'page callback' => 'drupal_get_form',
'page arguments' => array('contact_personal_form', 1),
2007-01-24 14:48:36 +00:00
'type' => MENU_LOCAL_TASK,
2009-05-06 10:38:40 +00:00
'access callback' => '_contact_personal_tab_access',
2007-01-24 14:48:36 +00:00
'access arguments' => array(1),
'weight' => 2,
2009-08-24 00:14:23 +00:00
'file' => 'contact.pages.inc',
2007-01-24 14:48:36 +00:00
);
2004-12-07 17:03:46 +00:00
return $items;
}
2007-07-16 06:37:49 +00:00
/**
2011-12-05 14:23:20 +00:00
* Access callback: Checks access for a user's personal contact form.
*
2009-10-10 19:54:15 +00:00
* @param $account
2011-12-05 14:23:20 +00:00
* The user object of the user whose contact form is being requested.
*
* @see contact_menu()
2007-07-16 06:37:49 +00:00
*/
2010-12-20 19:59:44 +00:00
function _contact_personal_tab_access($account) {
2007-01-24 14:48:36 +00:00
global $user;
2009-10-10 19:54:15 +00:00
2009-10-11 18:34:10 +00:00
// Anonymous users cannot have contact forms.
if (!$account->uid) {
2009-10-10 19:54:15 +00:00
return FALSE;
}
// Users may not contact themselves.
if ($user->uid == $account->uid) {
return FALSE;
}
2012-06-29 13:55:56 +00:00
// User administrators should always have access to personal contact forms.
if (user_access('administer users')) {
return TRUE;
}
2012-11-27 22:26:22 +00:00
// If requested user has been blocked, do not allow users to contact them.
if (empty($account->status)) {
2009-10-10 19:54:15 +00:00
return FALSE;
}
2012-11-27 22:26:22 +00:00
// If the requested user has disabled their contact form, do not allow users
// to contact them.
$account_data = drupal_container()->get('user.data')->get('contact', $account->id(), 'enabled');
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.
elseif (!config('contact.settings')->get('user_default_enabled')) {
2010-02-17 04:39:49 +00:00
return FALSE;
}
2009-10-11 18:34:10 +00:00
return user_access('access user contact forms');
2007-01-24 14:48:36 +00:00
}
2012-10-18 08:55:55 +00:00
/**
* 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;
}
2007-07-16 06:37:49 +00:00
/**
2011-12-05 14:23:20 +00:00
* Loads a contact category.
2009-10-09 02:34:07 +00:00
*
2012-10-18 08:55:55 +00:00
* @param $id
* The ID of the contact category to load.
2011-12-05 14:23:20 +00:00
*
2012-10-30 20:37:18 +00:00
* @return Drupal\contact\Plugin\Core\Entity\Category|false
2012-10-18 08:55:55 +00:00
* A Category object or FALSE if the requested $id does not exist.
2007-07-16 06:37:49 +00:00
*/
2012-10-18 08:55:55 +00:00
function contact_category_load($id) {
return entity_load('contact_category', $id);
}
/**
* Entity URI callback.
*
2012-11-06 14:03:05 +00:00
* @param Drupal\contact\Plugin\Core\Entity\Category $category
2012-10-18 08:55:55 +00:00
* 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(),
);
2008-10-06 11:30:12 +00:00
}
2007-07-01 19:49:19 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_mail().
2007-07-01 19:49:19 +00:00
*/
function contact_mail($key, &$message, $params) {
2012-09-13 08:48:24 +00:00
$language = language_load($message['langcode']);
2009-10-09 02:34:07 +00:00
$variables = array(
2012-07-02 17:20:33 +00:00
'!site-name' => config('system.site')->get('name'),
2009-10-09 02:34:07 +00:00
'!subject' => $params['subject'],
2012-10-18 08:55:55 +00:00
'!category' => isset($params['category']) ? $params['category']->label() : '',
2012-04-29 15:16:27 +00:00
'!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
2012-01-16 02:18:26 +00:00
'!sender-name' => user_format_name($params['sender']),
2009-10-11 18:34:10 +00:00
'!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
2009-10-09 02:34:07 +00:00
);
2007-07-01 19:49:19 +00:00
switch ($key) {
case 'page_mail':
case 'page_copy':
2012-01-10 15:29:08 +00:00
$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));
2007-07-01 19:49:19 +00:00
$message['body'][] = $params['message'];
break;
2009-10-09 02:34:07 +00:00
2007-07-01 19:49:19 +00:00
case 'page_autoreply':
2012-01-10 15:29:08 +00:00
$message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->langcode));
2012-10-18 08:55:55 +00:00
$message['body'][] = $params['category']->reply;
2007-07-01 19:49:19 +00:00
break;
2009-10-09 02:34:07 +00:00
2007-07-01 19:49:19 +00:00
case 'user_mail':
case 'user_copy':
2009-10-09 02:34:07 +00:00
$variables += array(
2012-01-16 02:18:26 +00:00
'!recipient-name' => user_format_name($params['recipient']),
2009-10-11 18:34:10 +00:00
'!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
2009-10-09 02:34:07 +00:00
);
2012-01-10 15:29:08 +00:00
$message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->langcode));
$message['body'][] = t('Hello !recipient-name,', $variables, array('langcode' => $language->langcode));
2012-11-19 11:49:04 +00:00
$message['body'][] = t("!sender-name (!sender-url) has sent you a message via your contact form at !site-name.", $variables, array('langcode' => $language->langcode));
2012-01-10 15:29:08 +00:00
$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));
2007-07-01 19:49:19 +00:00
$message['body'][] = $params['message'];
break;
}
2005-10-07 06:48:33 +00:00
}
2009-09-26 00:13:19 +00:00
2009-10-09 02:34:07 +00:00
/**
2009-12-04 16:49:48 +00:00
* Implements hook_form_FORM_ID_alter().
2009-10-09 02:34:07 +00:00
*
* Add the enable personal contact form to an individual user's account page.
2011-12-05 14:23:20 +00:00
*
* @see user_profile_form()
2009-10-09 02:34:07 +00:00
*/
function contact_form_user_profile_form_alter(&$form, &$form_state) {
2011-11-03 07:34:18 +00:00
$form['contact'] = array(
2012-11-27 07:06:47 +00:00
'#type' => 'details',
2011-11-03 07:34:18 +00:00
'#title' => t('Contact settings'),
'#weight' => 5,
'#collapsible' => TRUE,
);
2012-08-16 11:30:43 +00:00
$account = $form_state['controller']->getEntity($form_state);
2012-11-27 22:26:22 +00:00
$account_data = drupal_container()->get('user.data')->get('contact', $account->id(), 'enabled');
2011-11-03 07:34:18 +00:00
$form['contact']['contact'] = array(
'#type' => 'checkbox',
'#title' => t('Personal contact form'),
2012-11-27 22:26:22 +00:00
'#default_value' => isset($account_data) ? $account_data : config('contact.settings')->get('user_default_enabled'),
2012-11-19 11:49:04 +00:00
'#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.'),
2011-11-03 07:34:18 +00:00
);
2009-10-09 02:34:07 +00:00
}
2010-03-20 14:55:06 +00:00
/**
2012-11-27 22:26:22 +00:00
* Implements hook_user_update().
2010-03-20 14:55:06 +00:00
*/
2012-11-27 22:26:22 +00:00
function contact_user_update($account) {
if (isset($account->contact)) {
drupal_container()->get('user.data')->set('contact', $account->id(), 'enabled', (int) $account->contact);
}
2010-03-20 14:55:06 +00:00
}
2009-09-26 00:13:19 +00:00
/**
2010-11-17 03:58:30 +00:00
* Implements hook_form_FORM_ID_alter().
2009-10-09 02:34:07 +00:00
*
* Add the default personal contact setting on the user settings page.
2011-12-05 14:23:20 +00:00
*
* @see user_admin_settings()
2009-09-26 00:13:19 +00:00
*/
2009-10-09 02:34:07 +00:00
function contact_form_user_admin_settings_alter(&$form, &$form_state) {
2009-09-26 00:13:19 +00:00
$form['contact'] = array(
2012-11-27 07:06:47 +00:00
'#type' => 'details',
2009-10-09 02:34:07 +00:00
'#title' => t('Contact settings'),
2009-09-26 00:13:19 +00:00
'#weight' => 0,
);
$form['contact']['contact_default_status'] = array(
'#type' => 'checkbox',
'#title' => t('Enable the personal contact form by default for new users.'),
2009-10-09 02:34:07 +00:00
'#description' => t('Changing this setting will not affect existing users.'),
2012-08-01 02:20:36 +00:00
'#default_value' => config('contact.settings')->get('user_default_enabled'),
2009-09-26 00:13:19 +00:00
);
2012-08-01 02:20:36 +00:00
// 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();
2009-09-26 00:13:19 +00:00
}