jammer_messages.module in Jammer 7
Same filename and directory in other branches
Filter status messages set by drupal_set_message().
File
jammer_messages.moduleView source
<?php
/**
* @file
* Filter status messages set by drupal_set_message().
*/
/**
* Implements hook_boot().
*/
function jammer_messages_boot() {
if (!empty($_SESSION['messages'])) {
$results = db_query("SELECT message_string FROM {jammer_messages}");
foreach ($results as $message_filter) {
$message_filter = $message_filter->message_string;
foreach ($_SESSION['messages'] as $type => $messages) {
$grep_results = preg_grep('/' . $message_filter . '/', $_SESSION['messages'][$type]);
foreach ($grep_results as $key => $match) {
unset($_SESSION['messages'][$type][$key]);
}
if (empty($_SESSION['messages'][$type])) {
unset($_SESSION['messages'][$type]);
}
}
}
}
}
/**
* Implements hook_menu().
*
* @return An array of menu items.
*/
function jammer_messages_menu() {
$items['admin/config/user-interface/jammer_messages'] = array(
'title' => 'Message Jammer',
'description' => 'Filter status messages.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'_jammer_messages_settings',
),
'access arguments' => array(
'administer site configuration',
),
);
$items['admin/config/user-interface/jammer_messages/delete/%jammer_message'] = array(
'title' => 'Message jammer delete',
'description' => 'Delete filter on status messages.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'jammer_messages_settings_confirm_delete',
5,
),
'access arguments' => array(
'administer site configuration',
),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Load jammed message to verify they exist.
*
* @return object
*/
function jammer_message_load($mid) {
$result = db_select('jammer_messages', 'j')
->fields('j', array(
'mid',
'message_string',
))
->condition('mid', $mid)
->range(0, 1)
->execute();
return $result ? $result
->fetchObject() : FALSE;
}
/**
* Central configuration form for jammer_messages.
*
* @return array
*/
function _jammer_messages_settings() {
$form['jammer_messages_string'] = array(
'#type' => 'textfield',
'#title' => t('Message to jam'),
'#required' => TRUE,
'#description' => t('The message format is processed in regular expression form. Enter either an exact string to suppress, ' . 'or one which matches !PCRE formatting.', array(
'!PCRE' => l(t('PCRE'), 'http://us3.php.net/manual/en/book.pcre.php', array(
'external' => TRUE,
)),
)),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Jam message',
);
$current_settings = db_query("SELECT mid, message_string FROM {jammer_messages}");
$headers = array(
t('Message Pattern ID'),
t('Message Pattern'),
t('Delete'),
);
$data = array();
foreach ($current_settings as $setting) {
$data[] = array(
$setting->mid,
$setting->message_string,
l(t('Delete'), 'admin/config/user-interface/jammer_messages/delete/' . $setting->mid),
);
}
if (!empty($data)) {
$form['configured'] = array(
'#type' => 'fieldset',
'#title' => 'Jammed messages',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['configured']['table'] = array(
'#type' => 'item',
'#markup' => theme('table', array(
'header' => $headers,
'rows' => $data,
)),
);
}
return $form;
}
/**
* Magic form submission handler.
*
* @param array $form
* @param array &$form_state
*/
function _jammer_messages_settings_submit($form, &$form_state) {
$form_values = $form_state['values'];
if ($form_values['jammer_messages_string'] == '') {
drupal_set_message(t('Empty strings are not an accepted match string.'));
return;
}
$mid = db_insert('jammer_messages')
->fields(array(
'message_string' => $form_values['jammer_messages_string'],
))
->execute();
}
/**
* Returns a confirmation page for deleting a jammed message setting.
*
* @return array
*/
function jammer_messages_settings_confirm_delete($form, &$form_state, $message) {
$confirm_form['message'] = array(
'#type' => 'value',
'#value' => $message,
);
return confirm_form($confirm_form, t('Are you sure you want to un-jam this message?'), 'admin/config/user-interface/jammer_messages', t('Deleting this setting will cause previously hidden messages containing %message to display again. To review the settings on this message, please visit <a href="@content">Message Jammer administration</a> first. This action cannot be undone.', array(
'@content' => url('admin/config/user-interface/jammer_messages'),
'%message' => $message->message_string,
)), t('Delete'), t('Cancel'));
}
/**
* Deletes settings that would jam messages.
*/
function jammer_messages_settings_confirm_delete_submit($form, &$form_state) {
db_delete('jammer_messages')
->condition('mid', $form_state['values']['message']->mid)
->execute();
drupal_set_message(t('The filter for %message was deleted.', array(
'%message' => $form_state['values']['message']->message_string,
)));
watchdog('jammer_messages', '%message filter deleted.', array(
'%message' => $form_state['values']['message']->message_string,
), WATCHDOG_INFO);
$form_state['redirect'] = 'admin/config/user-interface/jammer_messages';
}
Functions
Name | Description |
---|---|
jammer_messages_boot | Implements hook_boot(). |
jammer_messages_menu | Implements hook_menu(). |
jammer_messages_settings_confirm_delete | Returns a confirmation page for deleting a jammed message setting. |
jammer_messages_settings_confirm_delete_submit | Deletes settings that would jam messages. |
jammer_message_load | Load jammed message to verify they exist. |
_jammer_messages_settings | Central configuration form for jammer_messages. |
_jammer_messages_settings_submit | Magic form submission handler. |