[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/contact/ -> contact.pages.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * User page callbacks for the contact module.
   6   */
   7  
   8  
   9  /**
  10   * Site-wide contact page.
  11   */
  12  function contact_site_page() {
  13    global $user;
  14  
  15    if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
  16      $output = t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('contact_hourly_threshold', 3)));
  17    }
  18    else {
  19      $output = drupal_get_form('contact_mail_page');
  20    }
  21  
  22    return $output;
  23  }
  24  
  25  function contact_mail_page() {
  26    global $user;
  27  
  28    $form = $categories = array();
  29  
  30    $result = db_query('SELECT cid, category, selected FROM {contact} ORDER BY weight, category');
  31    while ($category = db_fetch_object($result)) {
  32      $categories[$category->cid] = $category->category;
  33      if ($category->selected) {
  34        $default_category = $category->cid;
  35      }
  36    }
  37  
  38    if (count($categories) > 0) {
  39      $form['#token'] = $user->uid ? $user->name . $user->mail : '';
  40      $form['contact_information'] = array('#value' => filter_xss_admin(variable_get('contact_form_information', t('You can leave a message using the contact form below.'))));
  41      $form['name'] = array('#type' => 'textfield',
  42        '#title' => t('Your name'),
  43        '#maxlength' => 255,
  44        '#default_value' => $user->uid ? $user->name : '',
  45        '#required' => TRUE,
  46      );
  47      $form['mail'] = array('#type' => 'textfield',
  48        '#title' => t('Your e-mail address'),
  49        '#maxlength' => 255,
  50        '#default_value' => $user->uid ? $user->mail : '',
  51        '#required' => TRUE,
  52      );
  53      $form['subject'] = array('#type' => 'textfield',
  54        '#title' => t('Subject'),
  55        '#maxlength' => 255,
  56        '#required' => TRUE,
  57      );
  58      if (count($categories) > 1) {
  59        // If there is more than one category available and no default category has been selected,
  60        // prepend a default placeholder value.
  61        if (!isset($default_category)) {
  62          $default_category = t('- Please choose -');
  63          $categories = array($default_category) + $categories;
  64        }
  65        $form['cid'] = array('#type' => 'select',
  66          '#title' => t('Category'),
  67          '#default_value' => $default_category,
  68          '#options' => $categories,
  69          '#required' => TRUE,
  70        );
  71      }
  72      else {
  73        // If there is only one category, store its cid.
  74        $category_keys = array_keys($categories);
  75        $form['cid'] = array('#type' => 'value',
  76          '#value' => array_shift($category_keys),
  77        );
  78      }
  79      $form['message'] = array('#type' => 'textarea',
  80        '#title' => t('Message'),
  81        '#required' => TRUE,
  82      );
  83      // We do not allow anonymous users to send themselves a copy
  84      // because it can be abused to spam people.
  85      if ($user->uid) {
  86        $form['copy'] = array('#type' => 'checkbox',
  87          '#title' => t('Send yourself a copy.'),
  88        );
  89      }
  90      else {
  91        $form['copy'] = array('#type' => 'value', '#value' => FALSE);
  92      }
  93      $form['submit'] = array('#type' => 'submit',
  94        '#value' => t('Send e-mail'),
  95      );
  96    }
  97    else {
  98      drupal_set_message(t('The contact form has not been configured. <a href="@add">Add one or more categories</a> to the form.', array('@add' => url('admin/build/contact/add'))), 'error');
  99    }
 100    return $form;
 101  }
 102  
 103  /**
 104   * Validate the site-wide contact page form submission.
 105   */
 106  function contact_mail_page_validate($form, &$form_state) {
 107    if (!$form_state['values']['cid']) {
 108      form_set_error('cid', t('You must select a valid category.'));
 109    }
 110    if (!valid_email_address($form_state['values']['mail'])) {
 111      form_set_error('mail', t('You must enter a valid e-mail address.'));
 112    }
 113  }
 114  
 115  /**
 116   * Process the site-wide contact page form submission.
 117   */
 118  function contact_mail_page_submit($form, &$form_state) {
 119    global $language;
 120  
 121    $values = $form_state['values'];
 122  
 123    // E-mail address of the sender: as the form field is a text field,
 124    // all instances of \r and \n have been automatically stripped from it.
 125    $from = $values['mail'];
 126  
 127    // Load category properties and save form values for email composition.
 128    $contact = contact_load($values['cid']);
 129    $values['contact'] = $contact;
 130  
 131    // Send the e-mail to the recipients using the site default language.
 132    drupal_mail('contact', 'page_mail', $contact['recipients'], language_default(), $values, $from);
 133  
 134    // If the user requests it, send a copy using the current language.
 135    if ($values['copy']) {
 136      drupal_mail('contact', 'page_copy', $from, $language, $values, $from);
 137    }
 138  
 139    // Send an auto-reply if necessary using the current language.
 140    if ($contact['reply']) {
 141      drupal_mail('contact', 'page_autoreply', $from, $language, $values, $contact['recipients']);
 142    }
 143  
 144    flood_register_event('contact');
 145    watchdog('mail', '%name-from sent an e-mail regarding %category.', array('%name-from' => $values['name'] ." [$from]", '%category' => $contact['category']));
 146    drupal_set_message(t('Your message has been sent.'));
 147  
 148    // Jump to home page rather than back to contact page to avoid
 149    // contradictory messages if flood control has been activated.
 150    $form_state['redirect'] = '';
 151  }
 152  
 153  /**
 154   * Personal contact page.
 155   */
 156  function contact_user_page($account) {
 157    global $user;
 158  
 159    if (!valid_email_address($user->mail)) {
 160      $output = t('You need to provide a valid e-mail address to contact other users. Please update your <a href="@url">user information</a> and try again.', array('@url' => url("user/$user->uid/edit")));
 161    }
 162    else if (!flood_is_allowed('contact', variable_get('contact_hourly_threshold', 3))) {
 163      $output = t('You cannot contact more than %number users per hour. Please try again later.', array('%number' => variable_get('contact_hourly_threshold', 3)));
 164    }
 165    else {
 166      drupal_set_title(check_plain($account->name));
 167      $output = drupal_get_form('contact_mail_user', $account);
 168    }
 169  
 170    return $output;
 171  }
 172  
 173  function contact_mail_user(&$form_state, $recipient) {
 174    global $user;
 175    $form['#token'] = $user->name . $user->mail;
 176    $form['recipient'] = array('#type' => 'value', '#value' => $recipient);
 177    $form['from'] = array('#type' => 'item',
 178      '#title' => t('From'),
 179      '#value' => theme('username', $user) .' &lt;'. check_plain($user->mail) .'&gt;',
 180    );
 181    $form['to'] = array('#type' => 'item',
 182      '#title' => t('To'),
 183      '#value' => theme('username', $recipient),
 184    );
 185    $form['subject'] = array('#type' => 'textfield',
 186      '#title' => t('Subject'),
 187      '#maxlength' => 50,
 188      '#required' => TRUE,
 189    );
 190    $form['message'] = array('#type' => 'textarea',
 191      '#title' => t('Message'),
 192      '#rows' => 15,
 193      '#required' => TRUE,
 194    );
 195    $form['copy'] = array('#type' => 'checkbox',
 196      '#title' => t('Send yourself a copy.'),
 197    );
 198    $form['submit'] = array('#type' => 'submit',
 199      '#value' => t('Send e-mail'),
 200    );
 201    return $form;
 202  }
 203  
 204  /**
 205   * Process the personal contact page form submission.
 206   */
 207  function contact_mail_user_submit($form, &$form_state) {
 208    global $user, $language;
 209  
 210    $account = $form_state['values']['recipient'];
 211  
 212    // Send from the current user to the requested user.
 213    $to = $account->mail;
 214    $from = $user->mail;
 215  
 216    // Save both users and all form values for email composition.
 217    $values = $form_state['values'];
 218    $values['account'] = $account;
 219    $values['user'] = $user;
 220  
 221    // Send the e-mail in the requested user language.
 222    drupal_mail('contact', 'user_mail', $to, user_preferred_language($account), $values, $from);
 223  
 224    // Send a copy if requested, using current page language.
 225    if ($form_state['values']['copy']) {
 226      drupal_mail('contact', 'user_copy', $from, $language, $values, $from);
 227    }
 228  
 229    flood_register_event('contact');
 230    watchdog('mail', '%name-from sent %name-to an e-mail.', array('%name-from' => $user->name, '%name-to' => $account->name));
 231    drupal_set_message(t('The message has been sent.'));
 232  
 233    // Back to the requested users profile page.
 234    $form_state['redirect'] = "user/$account->uid";
 235  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7