cms/drupal/modules/contact/contact.module
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Enables the use of personal and site-wide contact forms.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Implements hook_help().
       
    10  */
       
    11 function contact_help($path, $arg) {
       
    12   switch ($path) {
       
    13     case 'admin/help#contact':
       
    14       $output = '';
       
    15       $output .= '<h3>' . t('About') . '</h3>';
       
    16       $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>';
       
    17       $output .= '<h3>' . t('Uses') . '</h3>';
       
    18       $output .= '<dl>';
       
    19       $output .= '<dt>' . t('User contact forms') . '</dt>';
       
    20       $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>';
       
    21       $output .= '<dt>' . t('Site-wide contact forms') . '</dt>';
       
    22       $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>';
       
    23       $output .= '<dt>' . t('Navigation') . '</dt>';
       
    24       $output .= '<dd>' . t("When the site-wide contact form is enabled, a link in the main <em>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('@contact' => url('contact'), '@menu' => url('admin/structure/menu'))) . '</dd>';
       
    25       $output .= '<dt>' . t('Customization') . '</dt>';
       
    26       $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>';
       
    27       $output .= '</dl>';
       
    28       return $output;
       
    29     case 'admin/structure/contact':
       
    30       $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>';
       
    31       $output .= '<p>' . t('A <em>Contact</em> menu item (disabled by default) is added to the Navigation menu, which you can modify on the <a href="@menu-settings">Menus administration page</a>.', array('@menu-settings' => url('admin/structure/menu'))) . '</p>';
       
    32       $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>';
       
    33       return $output;
       
    34   }
       
    35 }
       
    36 
       
    37 /**
       
    38  * Implements hook_permission().
       
    39  */
       
    40 function contact_permission() {
       
    41   return array(
       
    42     'administer contact forms' => array(
       
    43       'title' => t('Administer contact forms and contact form settings'),
       
    44     ),
       
    45     'access site-wide contact form' => array(
       
    46       'title' => t('Use the site-wide contact form'),
       
    47     ),
       
    48     'access user contact forms' => array(
       
    49       'title' => t("Use users' personal contact forms"),
       
    50     ),
       
    51   );
       
    52 }
       
    53 
       
    54 /**
       
    55  * Implements hook_menu().
       
    56  */
       
    57 function contact_menu() {
       
    58   $items['admin/structure/contact'] = array(
       
    59     'title' => 'Contact form',
       
    60     'description' => 'Create a system contact form and set up categories for the form to use.',
       
    61     'page callback' => 'contact_category_list',
       
    62     'access arguments' => array('administer contact forms'),
       
    63     'file' => 'contact.admin.inc',
       
    64   );
       
    65   $items['admin/structure/contact/add'] = array(
       
    66     'title' => 'Add category',
       
    67     'page callback' => 'drupal_get_form',
       
    68     'page arguments' => array('contact_category_edit_form'),
       
    69     'access arguments' => array('administer contact forms'),
       
    70     'type' => MENU_LOCAL_ACTION,
       
    71     'weight' => 1,
       
    72     'file' => 'contact.admin.inc',
       
    73   );
       
    74   $items['admin/structure/contact/edit/%contact'] = array(
       
    75     'title' => 'Edit contact category',
       
    76     'page callback' => 'drupal_get_form',
       
    77     'page arguments' => array('contact_category_edit_form', 4),
       
    78     'access arguments' => array('administer contact forms'),
       
    79     'file' => 'contact.admin.inc',
       
    80   );
       
    81   $items['admin/structure/contact/delete/%contact'] = array(
       
    82     'title' => 'Delete contact',
       
    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     'page callback' => 'drupal_get_form',
       
    91     'page arguments' => array('contact_site_form'),
       
    92     'access arguments' => array('access site-wide contact form'),
       
    93     'type' => MENU_SUGGESTED_ITEM,
       
    94     'file' => 'contact.pages.inc',
       
    95   );
       
    96   $items['user/%user/contact'] = array(
       
    97     'title' => 'Contact',
       
    98     'page callback' => 'drupal_get_form',
       
    99     'page arguments' => array('contact_personal_form', 1),
       
   100     'type' => MENU_LOCAL_TASK,
       
   101     'access callback' => '_contact_personal_tab_access',
       
   102     'access arguments' => array(1),
       
   103     'weight' => 2,
       
   104     'file' => 'contact.pages.inc',
       
   105   );
       
   106   return $items;
       
   107 }
       
   108 
       
   109 /**
       
   110  * Menu access callback for a user's personal contact form.
       
   111  *
       
   112  * @param $account
       
   113  *   The user object of the user whose contact form is being requested.
       
   114  */
       
   115 function _contact_personal_tab_access($account) {
       
   116   global $user;
       
   117 
       
   118   // Anonymous users cannot have contact forms.
       
   119   if (!$account->uid) {
       
   120     return FALSE;
       
   121   }
       
   122 
       
   123   // User administrators should always have access to personal contact forms.
       
   124   if (user_access('administer users')) {
       
   125     return TRUE;
       
   126   }
       
   127 
       
   128   // Users may not contact themselves.
       
   129   if ($user->uid == $account->uid) {
       
   130     return FALSE;
       
   131   }
       
   132 
       
   133   // If the requested user has disabled their contact form, or this preference
       
   134   // has not yet been saved, do not allow users to contact them.
       
   135   if (empty($account->data['contact'])) {
       
   136     return FALSE;
       
   137   }
       
   138 
       
   139   // If requested user has been blocked, do not allow users to contact them.
       
   140   if (empty($account->status)) {
       
   141     return FALSE;
       
   142   }
       
   143 
       
   144   return user_access('access user contact forms');
       
   145 }
       
   146 
       
   147 /**
       
   148  * Loads a contact category.
       
   149  *
       
   150  * @param $cid
       
   151  *   The contact category ID.
       
   152  *
       
   153  * @return
       
   154  *   An array with the contact category's data.
       
   155  */
       
   156 function contact_load($cid) {
       
   157   return db_select('contact', 'c')
       
   158     ->addTag('translatable')
       
   159     ->fields('c')
       
   160     ->condition('cid', $cid)
       
   161     ->execute()
       
   162     ->fetchAssoc();
       
   163 }
       
   164 
       
   165 /**
       
   166  * Implements hook_mail().
       
   167  */
       
   168 function contact_mail($key, &$message, $params) {
       
   169   $language = $message['language'];
       
   170   $variables = array(
       
   171     '!site-name' => variable_get('site_name', 'Drupal'),
       
   172     '!subject' => $params['subject'],
       
   173     '!category' => isset($params['category']['category']) ? $params['category']['category'] : '',
       
   174     '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
       
   175     '!sender-name' => format_username($params['sender']),
       
   176     '!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
       
   177   );
       
   178 
       
   179   switch ($key) {
       
   180     case 'page_mail':
       
   181     case 'page_copy':
       
   182       $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
       
   183       $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, array('langcode' => $language->language));
       
   184       $message['body'][] = $params['message'];
       
   185       break;
       
   186 
       
   187     case 'page_autoreply':
       
   188       $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
       
   189       $message['body'][] = $params['category']['reply'];
       
   190       break;
       
   191 
       
   192     case 'user_mail':
       
   193     case 'user_copy':
       
   194       $variables += array(
       
   195         '!recipient-name' => format_username($params['recipient']),
       
   196         '!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
       
   197       );
       
   198       $message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->language));
       
   199       $message['body'][] = t('Hello !recipient-name,', $variables, array('langcode' => $language->language));
       
   200       $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->language));
       
   201       $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->language));
       
   202       $message['body'][] = t('Message:', array(), array('langcode' => $language->language));
       
   203       $message['body'][] = $params['message'];
       
   204       break;
       
   205   }
       
   206 }
       
   207 
       
   208 /**
       
   209  * Implements hook_form_FORM_ID_alter().
       
   210  *
       
   211  * Add the enable personal contact form to an individual user's account page.
       
   212  *
       
   213  * @see user_profile_form()
       
   214  */
       
   215 function contact_form_user_profile_form_alter(&$form, &$form_state) {
       
   216   if ($form['#user_category'] == 'account') {
       
   217     $account = $form['#user'];
       
   218     $form['contact'] = array(
       
   219       '#type' => 'fieldset',
       
   220       '#title' => t('Contact settings'),
       
   221       '#weight' => 5,
       
   222       '#collapsible' => TRUE,
       
   223     );
       
   224     $form['contact']['contact'] = array(
       
   225       '#type' => 'checkbox',
       
   226       '#title' => t('Personal contact form'),
       
   227       '#default_value' => !empty($account->data['contact']) ? $account->data['contact'] : FALSE,
       
   228       '#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"))),
       
   229     );
       
   230   }
       
   231 }
       
   232 
       
   233 /**
       
   234  * Implements hook_user_presave().
       
   235  */
       
   236 function contact_user_presave(&$edit, $account, $category) {
       
   237   if (isset($edit['contact'])) {
       
   238     // Set new value.
       
   239     $edit['data']['contact'] = $edit['contact'];
       
   240   }
       
   241   elseif (!isset($account->data['contact'])) {
       
   242     // Use default if none has been set.
       
   243     $edit['data']['contact'] = variable_get('contact_default_status', 1);
       
   244   }
       
   245 }
       
   246 
       
   247 /**
       
   248  * Implements hook_form_FORM_ID_alter().
       
   249  *
       
   250  * Add the default personal contact setting on the user settings page.
       
   251  *
       
   252  * @see user_admin_settings()
       
   253  */
       
   254 function contact_form_user_admin_settings_alter(&$form, &$form_state) {
       
   255   $form['contact'] = array(
       
   256     '#type' => 'fieldset',
       
   257     '#title' => t('Contact settings'),
       
   258     '#weight' => 0,
       
   259   );
       
   260   $form['contact']['contact_default_status'] = array(
       
   261     '#type' => 'checkbox',
       
   262     '#title' => t('Enable the personal contact form by default for new users.'),
       
   263     '#description' => t('Changing this setting will not affect existing users.'),
       
   264     '#default_value' => variable_get('contact_default_status', 1),
       
   265   );
       
   266 }