Commit e2e70b1e authored by Dries's avatar Dries

- Patch #599186 by Dave Reid: code clean-ups

parent 02d4ddb7
......@@ -9,28 +9,34 @@
/**
* Categories/list tab.
*/
function contact_admin_categories() {
function contact_category_list() {
$header = array(
t('Category'),
t('Recipients'),
t('Selected'),
array('data' => t('Operations'), 'colspan' => 2),
);
$rows = array();
$header = array(t('Category'), t('Recipients'), t('Selected'), array('data' => t('Operations'), 'colspan' => 2));
// Get all the contact categories from the database.
$result = db_query('SELECT cid, category, recipients, selected FROM {contact} ORDER BY weight, category');
$categories = db_query('SELECT cid, category, recipients, selected FROM {contact} ORDER BY weight, category')->fetchAll();
// Loop through the categories and add them to the table.
foreach ($result as $record) {
foreach ($categories as $category) {
$rows[] = array(
$record->category,
$record->recipients,
($record->selected ? t('Yes') : t('No')),
l(t('edit'), 'admin/structure/contact/edit/' . $record->cid),
l(t('delete'), 'admin/structure/contact/delete/' . $record->cid),
$category->category,
$category->recipients,
($category->selected ? t('Yes') : t('No')),
l(t('Edit'), 'admin/structure/contact/edit/' . $category->cid),
l(t('Delete'), 'admin/structure/contact/delete/' . $category->cid),
);
}
// If no categories were found, let the user know.
if (empty($rows)) {
$rows[] = array(array('data' => t('No categories available. <a href="@link">Add category</a>.', array('@link' => url('admin/structure/contact/add'))), 'colspan' => 5));
if (!$rows) {
$rows[] = array(array(
'data' => t('No categories available.'),
'colspan' => 5,
));
}
return theme('table', array('header' => $header, 'rows' => $rows));
......@@ -39,52 +45,60 @@ function contact_admin_categories() {
/**
* Category edit page.
*/
function contact_admin_edit($form, $form_state = array(), $op, $contact = NULL) {
if (empty($contact) || $op == 'add') {
$contact = array(
'category' => '',
'recipients' => '',
'reply' => '',
'weight' => 0,
'selected' => 0,
'cid' => NULL,
);
}
$form['contact_op'] = array('#type' => 'value', '#value' => $op);
$form['category'] = array('#type' => 'textfield',
function contact_category_edit_form($form, &$form_state, array $category = array()) {
// If this is a new category, add the default values.
$category += array(
'category' => '',
'recipients' => '',
'reply' => '',
'weight' => 0,
'selected' => 0,
'cid' => NULL,
);
$form['category'] = array(
'#type' => 'textfield',
'#title' => t('Category'),
'#maxlength' => 255,
'#default_value' => $contact['category'],
'#default_value' => $category['category'],
'#description' => t("Example: 'website feedback' or 'product information'."),
'#required' => TRUE,
);
$form['recipients'] = array('#type' => 'textarea',
$form['recipients'] = array(
'#type' => 'textarea',
'#title' => t('Recipients'),
'#default_value' => $contact['recipients'],
'#default_value' => $category['recipients'],
'#description' => t("Example: 'webmaster@example.com' or 'sales@example.com,support@example.com' . To specify multiple recipients, separate each e-mail address with a comma."),
'#required' => TRUE,
);
$form['reply'] = array('#type' => 'textarea',
$form['reply'] = array(
'#type' => 'textarea',
'#title' => t('Auto-reply'),
'#default_value' => $contact['reply'],
'#default_value' => $category['reply'],
'#description' => t('Optional auto-reply. Leave empty if you do not want to send the user an auto-reply message.'),
);
$form['weight'] = array('#type' => 'weight',
$form['weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => $contact['weight'],
'#default_value' => $category['weight'],
'#description' => t('When listing categories, those with lighter (smaller) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'),
);
$form['selected'] = array('#type' => 'select',
$form['selected'] = array(
'#type' => 'select',
'#title' => t('Selected'),
'#options' => array('0' => t('No'), '1' => t('Yes')),
'#default_value' => $contact['selected'],
'#options' => array(
0 => t('No'),
1 => t('Yes'),
),
'#default_value' => $category['selected'],
'#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'),
);
$form['cid'] = array('#type' => 'value',
'#value' => $contact['cid'],
$form['cid'] = array(
'#type' => 'value',
'#value' => $category['cid'],
);
$form['submit'] = array('#type' => 'submit',
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
);
......@@ -94,71 +108,76 @@ function contact_admin_edit($form, $form_state = array(), $op, $contact = NULL)
/**
* Validate the contact category edit page form submission.
*/
function contact_admin_edit_validate($form, &$form_state) {
function contact_category_edit_form_validate($form, &$form_state) {
// Validate and each e-mail recipient.
$recipients = explode(',', $form_state['values']['recipients']);
foreach ($recipients as $recipient) {
if (!valid_email_address(trim($recipient))) {
foreach ($recipients as &$recipient) {
$recipient = trim($recipient);
if (!valid_email_address($recipient)) {
form_set_error('recipients', t('%recipient is an invalid e-mail address.', array('%recipient' => $recipient)));
}
}
$form_state['values']['recipients'] = implode(',', $recipients);
}
/**
* Process the contact category edit page form submission.
*/
function contact_admin_edit_submit($form, &$form_state) {
function contact_category_edit_form_submit($form, &$form_state) {
if ($form_state['values']['selected']) {
// Unselect all other contact categories.
db_update('contact')
->fields(array('selected' => '0'))
->execute();
}
$recipients = explode(',', $form_state['values']['recipients']);
foreach ($recipients as $key => $recipient) {
// E-mail address validation has already been done in _validate.
$recipients[$key] = trim($recipient);
}
$form_state['values']['recipients'] = implode(',', $recipients);
if (empty($form_state['values']['cid']) || $form_state['values']['contact_op'] == 'add') {
drupal_write_record('contact', $form_state['values']);
drupal_set_message(t('Category %category has been added.', array('%category' => $form_state['values']['category'])));
watchdog('mail', 'Contact form: category %category added.', array('%category' => $form_state['values']['category']), WATCHDOG_NOTICE, l(t('view'), 'admin/structure/contact'));
if (empty($form_state['values']['cid'])) {
drupal_write_record('contact', $form_state['values']);
}
else {
drupal_write_record('contact', $form_state['values'], 'cid');
drupal_set_message(t('Category %category has been updated.', array('%category' => $form_state['values']['category'])));
watchdog('mail', 'Contact form: category %category updated.', array('%category' => $form_state['values']['category']), WATCHDOG_NOTICE, l(t('view'), 'admin/structure/contact'));
drupal_write_record('contact', $form_state['values'], array('cid'));
}
drupal_set_message(t('Category %category has been saved.', array('%category' => $form_state['values']['category'])));
watchdog('contact', 'Category %category has been saved.', array('%category' => $form_state['values']['category']), WATCHDOG_NOTICE, l(t('Edit'), 'admin/structure/contact/edit/' . $form_state['values']['cid']));
$form_state['redirect'] = 'admin/structure/contact';
return;
}
/**
* Category delete page.
* Form builder for deleting a contact category.
*
* @see contact_category_delete_form_submit()
*/
function contact_admin_delete($form, &$form_state, $contact) {
function contact_category_delete_form($form, &$form_state, array $contact) {
$form['contact'] = array(
'#type' => 'value',
'#value' => $contact,
);
return confirm_form($form, t('Are you sure you want to delete %category?', array('%category' => $contact['category'])), 'admin/structure/contact', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
return confirm_form(
$form,
t('Are you sure you want to delete %category?', array('%category' => $contact['category'])),
'admin/structure/contact',
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Process category delete form submission.
* Submit handler for the confirm delete category form.
*
* @see contact_category_delete_form()
*/
function contact_admin_delete_submit($form, &$form_state) {
$contact = $form_state['values']['contact'];
function contact_category_delete_form_submit($form, &$form_state) {
$contact = $form['contact']['#value'];
db_delete('contact')
->condition('cid', $contact['cid'])
->execute();
drupal_set_message(t('Category %category has been deleted.', array('%category' => $contact['category'])));
watchdog('mail', 'Contact form: category %category deleted.', array('%category' => $contact['category']), WATCHDOG_NOTICE);
watchdog('contact', 'Category %category has been deleted.', array('%category' => $contact['category']), WATCHDOG_NOTICE);
$form_state['redirect'] = 'admin/structure/contact';
return;
}
......@@ -6,15 +6,6 @@
* Install, update and uninstall functions for the contact module.
*/
/**
* Implement hook_uninstall().
*/
function contact_uninstall() {
variable_del('contact_default_status');
variable_del('contact_threshold_limit');
variable_del('contact_threshold_window');
}
/**
* Implement hook_schema().
*/
......@@ -74,6 +65,15 @@ function contact_schema() {
return $schema;
}
/**
* Implement hook_uninstall().
*/
function contact_uninstall() {
variable_del('contact_default_status');
variable_del('contact_threshold_limit');
variable_del('contact_threshold_window');
}
/**
* @defgroup updates-6.x-to-7.x Contact updates from 6.x to 7.x
* @{
......
......@@ -55,14 +55,14 @@ function contact_menu() {
$items['admin/structure/contact'] = array(
'title' => 'Contact form',
'description' => 'Create a system contact form and set up categories for the form to use.',
'page callback' => 'contact_admin_categories',
'page callback' => 'contact_category_list',
'access arguments' => array('administer contact forms'),
'file' => 'contact.admin.inc',
);
$items['admin/structure/contact/add'] = array(
'title' => 'Add category',
'page callback' => 'drupal_get_form',
'page arguments' => array('contact_admin_edit', 3),
'page arguments' => array('contact_category_edit_form'),
'access arguments' => array('administer contact forms'),
'type' => MENU_LOCAL_ACTION,
'weight' => 1,
......@@ -71,7 +71,7 @@ function contact_menu() {
$items['admin/structure/contact/edit/%contact'] = array(
'title' => 'Edit contact category',
'page callback' => 'drupal_get_form',
'page arguments' => array('contact_admin_edit', 3, 4),
'page arguments' => array('contact_category_edit_form', 4),
'access arguments' => array('administer contact forms'),
'type' => MENU_CALLBACK,
'file' => 'contact.admin.inc',
......@@ -79,7 +79,7 @@ function contact_menu() {
$items['admin/structure/contact/delete/%contact'] = array(
'title' => 'Delete contact',
'page callback' => 'drupal_get_form',
'page arguments' => array('contact_admin_delete', 4),
'page arguments' => array('contact_category_delete_form', 4),
'access arguments' => array('administer contact forms'),
'type' => MENU_CALLBACK,
'file' => 'contact.admin.inc',
......@@ -107,7 +107,7 @@ function contact_menu() {
/**
* Determine permission to a user's personal contact form.
*/
function _contact_personal_tab_access($account) {
function _contact_personal_tab_access(stdClass $account) {
global $user;
if (!isset($account->contact)) {
$account->contact = FALSE;
......@@ -121,31 +121,15 @@ function _contact_personal_tab_access($account) {
}
/**
* Load the data for a single contact category.
* Load a contact category.
*
* @param $cid
* The contact category ID.
* @return
* An array with the contact category's data.
*/
function contact_load($cid) {
$contact = db_query("SELECT cid, category, recipients, reply, weight, selected FROM {contact} WHERE cid = :cid", array(':cid' => $cid))
->fetchAssoc();
return empty($contact) ? FALSE : $contact;
}
/**
* Implement hook_form_FORM_ID_alter().
*/
function contact_form_user_profile_form_alter(&$form, &$form_state) {
if ($form['#user_category'] == 'account') {
$account = $form['#user'];
$form['contact'] = array('#type' => 'fieldset',
'#title' => t('Contact settings'),
'#weight' => 5,
'#collapsible' => TRUE,
);
$form['contact']['contact'] = array('#type' => 'checkbox',
'#title' => t('Personal contact form'),
'#default_value' => !empty($account->contact) ? $account->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"))),
);
}
return db_query("SELECT * FROM {contact} WHERE cid = :cid", array(':cid' => $cid))->fetchAssoc();
}
/**
......@@ -160,45 +144,85 @@ function contact_user_insert(&$edit, $account, $category) {
*/
function contact_mail($key, &$message, $params) {
$language = $message['language'];
$variables = array(
'!site-name' => variable_get('site_name', 'Drupal'),
'!subject' => $params['subject'],
'!category' => isset($params['contact']['category']) ? $params['contact']['category'] : '',
'!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)),
);
switch ($key) {
case 'page_mail':
case 'page_copy':
$contact = $params['contact'];
$message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), array('langcode' => $language->language));
$message['body'][] = t("!name sent a message using the contact form at !form.", array('!name' => $params['name'], '!form' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language))), array('langcode' => $language->language));
$variables += array(
'!sender-name' => $params['name'],
);
$message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
$message['body'][] = t("!sender-name sent a message using the contact form at !form-url.", $variables, array('langcode' => $language->language));
$message['body'][] = $params['message'];
break;
case 'page_autoreply':
$contact = $params['contact'];
$message['subject'] .= t('[!category] !subject', array('!category' => $contact['category'], '!subject' => $params['subject']), array('langcode' => $language->language));
$message['body'][] = $contact['reply'];
$message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
$message['body'][] = $params['contact']['reply'];
break;
case 'user_mail':
case 'user_copy':
$user = $params['user'];
$account = $params['account'];
$message['subject'] .= '[' . variable_get('site_name', 'Drupal') . '] ' . $params['subject'];
$message['body'][] = "$account->name,";
$message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)), '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), array('langcode' => $language->language));
$message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE, 'language' => $language))), array('langcode' => $language->language));
$variables += array(
'!user-name' => $params['account']->name,
'!user-edit-url' => url('user/' . $params['account']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
'!sender-name' => $params['user']->name,
'!sender-url' => url('user/' . $params['user']->uid, array('absolute' => TRUE, 'language' => $language)),
);
$message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->language));
$message['body'][] = t('!user-name,', $variables, array('langcode' => $language->language));
$message['body'][] = t("!sender-name (!sender-name-url) has sent you a message via your contact form (!form-url) at !site.", $variables, array('langcode' => $language->language));
$message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !user-edit-url.", $variables, array('langcode' => $language->language));
$message['body'][] = t('Message:', array(), array('langcode' => $language->language));
$message['body'][] = $params['message'];
break;
}
}
/**
* Implement hook_form_FORM_ID_alter().
*
* Add the enable personal contact form to an individual user's account page.
*/
function contact_form_user_profile_form_alter(&$form, &$form_state) {
if ($form['#user_category'] == 'account') {
$account = $form['#user'];
$form['contact'] = array(
'#type' => 'fieldset',
'#title' => t('Contact settings'),
'#weight' => 5,
'#collapsible' => TRUE,
);
$form['contact']['contact'] = array(
'#type' => 'checkbox',
'#title' => t('Personal contact form'),
'#default_value' => !empty($account->contact) ? $account->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"))),
);
}
}
/**
* Implement of hook_form_FORM_ID_alter().
*
* Add the default personal contact setting on the user settings page.
*/
function contact_form_user_admin_settings_alter(&$form, $form_state) {
function contact_form_user_admin_settings_alter(&$form, &$form_state) {
$form['contact'] = array(
'#type' => 'fieldset',
'#title' => t('Contact forms'),
'#title' => t('Contact settings'),
'#weight' => 0,
);
$form['contact']['contact_default_status'] = array(
'#type' => 'checkbox',
'#title' => t('Enable the personal contact form by default for new users.'),
'#description' => t('Changing this setting will not affect existing users.'),
'#default_value' => 1,
);
}
......@@ -11,10 +11,13 @@
* Site-wide contact page.
*/
function contact_site_page() {
if (!flood_is_allowed('contact', variable_get('contact_threshold_limit', 5), variable_get('contact_threshold_window', 3600)) && !user_access('administer contact forms')) {
$output = t("You cannot send more than %number messages in @interval. Please try again later.", array('%number' => variable_get('contact_threshold_limit', 3), '@interval' => format_interval(variable_get('contact_threshold_window', 3600))));
$limit = variable_get('contact_threshold_limit', 5);
$window = variable_get('contact_threshold_window', 3600);
if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) {
$output = t("You cannot send more than %limit messages in @interval. Please try again later.", array('%limit' => $limit, '@interval' => format_interval($window)));
}
elseif (!db_query("SELECT COUNT(cid) FROM {contact}")->fetchField()) {
elseif (!db_query("SELECT 1 FROM {contact}")->fetchField()) {
if (user_access('administer contact forms')) {
$output = t('The contact form has not been configured. <a href="@add">Add one or more categories</a> to the form.', array('@add' => url('admin/structure/contact/add')));
}
......@@ -32,14 +35,14 @@ function contact_site_page() {
/**
* Form builder; the site-wide contact form.
*/
function contact_site_form() {
function contact_site_form($form, &$form_state) {
global $user;
$categories = db_query("SELECT cid, category FROM {contact} ORDER BY weight, category")->fetchAllKeyed();
$default_category = (int) db_query("SELECT cid FROM {contact} WHERE selected = 1")->fetchField();
$default_category = db_query("SELECT cid FROM {contact} WHERE selected = 1")->fetchField();
// If there is more than one category available and no default category has been selected,
// prepend a default placeholder value.
// If there is more than one category available and no default category has
// been selected, prepend a default placeholder value.
if (!$default_category) {
if (count($categories) > 1) {
$categories = array(0 => t('- Please choose -')) + $categories;
......@@ -151,18 +154,21 @@ function contact_site_form_submit($form, &$form_state) {
/**
* Personal contact page.
*/
function contact_personal_page($account) {
function contact_personal_page(stdClass $recipient) {
global $user;
$limit = variable_get('contact_threshold_limit', 5);
$window = variable_get('contact_threshold_window', 3600);
if (!valid_email_address($user->mail)) {
$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", array('query' => drupal_get_destination()))));
}
elseif (!flood_is_allowed('contact', variable_get('contact_threshold_limit', 5), variable_get('contact_threshold_window', 3600)) && !user_access('administer contact forms')) {
$output = t("You cannot send more than %number messages in @interval. Please try again later.", array('%number' => variable_get('contact_threshold_limit', 3), '@interval' => format_interval(variable_get('contact_threshold_window', 3600))));
elseif (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms') && !user_access('administer users')) {
$output = t("You cannot send more than %limit messages in @interval. Please try again later.", array('%limit' => $limit, '@interval' => format_interval($window)));
}
else {
drupal_set_title($account->name);
$output = drupal_get_form('contact_personal_form', $account);
drupal_set_title(t('Contact @username', array('@username' => $recipient->name)), PASS_THROUGH);
$output = drupal_get_form('contact_personal_form', $recipient);
}
return $output;
......@@ -171,7 +177,7 @@ function contact_personal_page($account) {
/**
* Form builder; the personal contact form.
*/
function contact_personal_form($form, &$form_state, $recipient) {
function contact_personal_form($form, &$form_state, stdClass $recipient) {
global $user;
$form['#token'] = $user->name . $user->mail;
$form['recipient'] = array('#type' => 'value', '#value' => $recipient);
......
......@@ -64,7 +64,7 @@ class ContactSitewideTestCase extends DrupalWebTestCase {
// Create first valid category.
$recipients = array('simpletest@example.com', 'simpletest2@example.com', 'simpletest3@example.com');
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0])), '', TRUE);
$this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
$this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.'));
// Make sure the newly created category is included in the list of categories.
$this->assertNoUniqueText($category, t('New category included in categories list.'));
......@@ -77,7 +77,7 @@ class ContactSitewideTestCase extends DrupalWebTestCase {
$this->assertEqual($category_array['recipients'], $recipients_str);
$this->assertEqual($category_array['reply'], $reply);
$this->assertFalse($category_array['selected']);
$this->assertRaw(t('Category %category has been updated.', array('%category' => $category)), t('Category successfully updated.'));
$this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.'));
// Ensure that the contact form is shown without a category selection input.
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access site-wide contact form'));
......@@ -89,10 +89,10 @@ class ContactSitewideTestCase extends DrupalWebTestCase {
// Add more categories.
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1])), '', FALSE);
$this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
$this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.'));
$this->addCategory($category = $this->randomName(16), implode(',', array($recipients[0], $recipients[1], $recipients[2])), '', FALSE);
$this->assertRaw(t('Category %category has been added.', array('%category' => $category)), t('Category successfully added.'));
$this->assertRaw(t('Category %category has been saved.', array('%category' => $category)), t('Category successfully saved.'));
// Clear flood table in preparation for flood test and allow other checks to complete.
db_delete('flood')->execute();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment