View source
<?php
function block_revisions_menu_alter(&$items) {
unset($items['admin/build/block/configure']);
}
function block_revisions_menu() {
global $base_root;
$items['admin/build/block/configure/%/%'] = array(
'title' => 'Configure block',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'block_admin_configure',
4,
5,
),
'access arguments' => array(
'administer blocks',
),
'file' => 'block.admin.inc',
'file path' => drupal_get_path('module', 'block'),
);
$items['admin/build/block/configure/%/%/edit'] = array(
'title' => 'Configure block',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/build/block/configure/%/%/revisions'] = array(
'title' => 'Revisions',
'page callback' => 'block_revisions_overview',
'page arguments' => array(
5,
),
'access callback' => '_block_revisions_access',
'access arguments' => array(
4,
5,
),
'type' => MENU_LOCAL_TASK,
'file' => 'block_revisions.admin.inc',
);
$items['admin/build/block/revisions/delete/%/%block_revision'] = array(
'title' => 'Delete earlier revision',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'block_revisions_delete_confirm',
6,
),
'load arguments' => array(
5,
),
'access callback' => '_block_revisions_access',
'access arguments' => array(
'block',
5,
),
'type' => MENU_CALLBACK,
'file' => 'block_revisions.admin.inc',
);
$items['admin/build/block/revisions/revert/%/%block_revision'] = array(
'title' => 'Delete earlier revision',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'block_revisions_revert_confirm',
6,
),
'load arguments' => array(
5,
),
'access callback' => '_block_revisions_access',
'access arguments' => array(
'block',
5,
),
'type' => MENU_CALLBACK,
'file' => 'block_revisions.admin.inc',
);
return $items;
}
function block_revision_load($vid, $delta) {
$result = db_query('SELECT bid, vid, body, format, br.timestamp, br.log, u.uid, u.name FROM {boxes_revisions} br LEFT JOIN {users} u ON br.uid = u.uid WHERE br.bid = %d AND br.vid = %d', $delta, $vid);
$revision = db_fetch_object($result);
return $revision;
}
function _block_revisions_access($module, $delta) {
$revision_count = db_result(db_query('SELECT count(vid) FROM {boxes_revisions} WHERE bid = %d', $delta));
return user_access('administer blocks') && $module === 'block' && $revision_count >= 1;
}
function block_revisions_form_alter(&$form, $form_state, $form_id) {
$module = arg(4);
if ($form_id === 'block_add_block_form' || $form_id === 'block_admin_configure' && isset($module) && $module === 'block') {
$form['block_settings'] += _block_revisions_form();
array_push($form['#submit'], '_block_revisions_form_submit');
}
}
function block_revisions_form_block_box_delete_alter(&$form, $form_state) {
array_unshift($form['#submit'], 'block_revisions_delete_revisions');
}
function _block_revisions_form() {
$form['revision_information'] = array(
'#type' => 'fieldset',
'#title' => t('Revision information'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => 20,
);
$form['revision_information']['revision'] = array(
'#access' => user_access('administer blocks'),
'#type' => 'checkbox',
'#title' => t('Create new revision'),
'#default_value' => variable_get('block_revisions_revision_default', FALSE),
);
$form['revision_information']['log'] = array(
'#type' => 'textarea',
'#title' => t('Log message'),
'#rows' => 2,
'#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
);
return $form;
}
function _block_revisions_form_submit($form, &$form_state) {
global $user;
$delta = $form_state['values']['delta'];
if (!isset($delta)) {
$delta = db_result(db_query("SELECT bid FROM {boxes} WHERE info = '%s'", $form_state['values']['info']));
}
if ($form_state['values']['revision']) {
block_revisions_create_revision($delta, $form_state['values']['body'], $form_state['values']['format'], $form_state['values']['log']);
}
db_query("UPDATE {boxes} SET uid = %d, timestamp = %d WHERE bid = %d", $user->uid, time(), $delta);
}
function block_revisions_create_revision($bid, $body, $format, $log = NULL) {
global $user;
$revision = new stdClass();
$revision->bid = $bid;
$revision->body = $body;
$revision->format = $format;
$revision->log = $log;
$revision_id = db_result(db_query('SELECT max(vid) FROM {boxes_revisions} WHERE bid = %d', $bid));
$revision->vid = isset($revision_id) ? $revision_id + 1 : 1;
$revision->timestamp = time();
$revision->uid = $user->uid;
return drupal_write_record('boxes_revisions', $revision);
}
function block_revisions_delete_revisions($form, &$form_state) {
$delta = $form_state['values']['bid'];
db_query("DELETE FROM {boxes_revisions} WHERE bid = %d", $delta);
$title = db_result(db_query("SELECT info FROM {boxes} WHERE bid = %d", $delta));
watchdog('content', "Deleted all remaining revisions for custom block '%title'.", array(
'%title' => $title,
));
drupal_set_message(t("All revisions for custom block '%title' have been deleted.", array(
'%title' => $title,
)));
}