| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Allow locking of nodes. Locked nodes can only be edited (and unlocked)
|
| 7 |
* by users with the 'lock content' permission.
|
| 8 |
* Note: Users with the 'administer nodes' permission can always edit nodes.
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_perm().
|
| 13 |
*/
|
| 14 |
function node_lock_perm() {
|
| 15 |
return array('lock content');
|
| 16 |
}
|
| 17 |
|
| 18 |
/**
|
| 19 |
* Implementation of hook_menu().
|
| 20 |
*/
|
| 21 |
function node_lock_menu() {
|
| 22 |
$items = array();
|
| 23 |
$items['node/%node/lock'] = array(
|
| 24 |
'title' => 'Lock',
|
| 25 |
'page callback' => 'drupal_get_form',
|
| 26 |
'page arguments' => array('node_lock_lock_confirm', 1),
|
| 27 |
'access callback' => '_node_lock_can_lock',
|
| 28 |
'access arguments' => array(1),
|
| 29 |
'type' => MENU_LOCAL_TASK,
|
| 30 |
);
|
| 31 |
$items['node/%node/unlock'] = array(
|
| 32 |
'title' => 'Unlock',
|
| 33 |
'page callback' => 'drupal_get_form',
|
| 34 |
'page arguments' => array('node_lock_unlock_confirm', 1),
|
| 35 |
'access callback' => '_node_lock_can_unlock',
|
| 36 |
'access arguments' => array(1),
|
| 37 |
'type' => MENU_LOCAL_TASK,
|
| 38 |
);
|
| 39 |
return $items;
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* Implementation of hook_menu_alter().
|
| 44 |
*/
|
| 45 |
function node_lock_menu_alter(&$callbacks) {
|
| 46 |
$callbacks['node/%node/edit']['access callback'] = '_node_lock_access';
|
| 47 |
$callbacks['node/%node/delete']['access callback'] = '_node_lock_access';
|
| 48 |
if (module_exists('trash')) {
|
| 49 |
$callbacks['node/%node/trash']['access callback'] = '_node_lock_access';
|
| 50 |
$callbacks['node/%node/trash']['access arguments'] = array('delete', 1);
|
| 51 |
}
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* Access callback for lock tab.
|
| 56 |
*/
|
| 57 |
function _node_lock_can_lock($node) {
|
| 58 |
return user_access('lock content') && !$node->locked;
|
| 59 |
}
|
| 60 |
|
| 61 |
/**
|
| 62 |
* Access callback for unlock tab.
|
| 63 |
*/
|
| 64 |
function _node_lock_can_unlock($node) {
|
| 65 |
return user_access('lock content') && $node->locked;
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Access callback for node update and deletion.
|
| 70 |
*/
|
| 71 |
function _node_lock_access($op, $node) {
|
| 72 |
if ($node->locked && !(user_access('lock content') || user_access('administer nodes'))) {
|
| 73 |
return FALSE;
|
| 74 |
}
|
| 75 |
return node_access($op, $node);
|
| 76 |
}
|
| 77 |
|
| 78 |
/**
|
| 79 |
* Implementation of hook_form_alter()
|
| 80 |
*/
|
| 81 |
function node_lock_form_alter(&$form, $form_state, $form_id) {
|
| 82 |
if ($form['#id'] == 'node-form' && user_access('lock content')) {
|
| 83 |
// Node edit form
|
| 84 |
$form['access_control'] = array(
|
| 85 |
'#type' => 'fieldset',
|
| 86 |
'#title' => t('Access control'),
|
| 87 |
);
|
| 88 |
$form['access_control']['locked'] = array(
|
| 89 |
'#type' => 'checkbox',
|
| 90 |
'#title' => t('Lock update/delete'),
|
| 91 |
'#description' => t('Check if update and deletion of this page should be locked.'),
|
| 92 |
'#default_value' => isset($form['#node']->locked) ? $form['#node']->locked : FALSE,
|
| 93 |
);
|
| 94 |
}
|
| 95 |
}
|
| 96 |
|
| 97 |
/**
|
| 98 |
* Implementation of hook_nodeapi().
|
| 99 |
*
|
| 100 |
* - "delete", "insert", and "update":
|
| 101 |
* The module must track the access status of the node.
|
| 102 |
*/
|
| 103 |
function node_lock_nodeapi(&$node, $op, $arg = 0) {
|
| 104 |
switch ($op) {
|
| 105 |
case 'load':
|
| 106 |
$result = db_fetch_object(db_query('SELECT * FROM {node_lock} WHERE vid = %d', $node->vid));
|
| 107 |
$node->locked = $result->locked ? TRUE : FALSE;
|
| 108 |
break;
|
| 109 |
case 'delete':
|
| 110 |
db_query('DELETE FROM {node_lock} WHERE vid = %d', $node->vid);
|
| 111 |
break;
|
| 112 |
case 'insert':
|
| 113 |
if (user_access('lock content')) {
|
| 114 |
node_lock_set_lock_status($node);
|
| 115 |
}
|
| 116 |
break;
|
| 117 |
case 'update':
|
| 118 |
if (user_access('lock content')) {
|
| 119 |
node_lock_set_lock_status($node);
|
| 120 |
}
|
| 121 |
break;
|
| 122 |
}
|
| 123 |
}
|
| 124 |
|
| 125 |
/**
|
| 126 |
* Implementation of hook_diff().
|
| 127 |
*/
|
| 128 |
function node_lock_diff(&$old_node, &$new_node) {
|
| 129 |
$result = array();
|
| 130 |
$old_lock = $old_node->locked ? t('Locked') : t('Unlocked');
|
| 131 |
$new_lock = $new_node->locked ? t('Locked') : t('Unlocked');
|
| 132 |
$result[] = array(
|
| 133 |
'name' => t('Lock status'),
|
| 134 |
'old' => array($old_lock),
|
| 135 |
'new' => array($new_lock),
|
| 136 |
'format' => array(
|
| 137 |
'show_header' => FALSE,
|
| 138 |
)
|
| 139 |
);
|
| 140 |
return $result;
|
| 141 |
}
|
| 142 |
|
| 143 |
/**
|
| 144 |
* Lock node $node.
|
| 145 |
* This creates a new revision with the given log message.
|
| 146 |
*/
|
| 147 |
function node_lock_lock($node, $log_message = NULL) {
|
| 148 |
if (!$log_message) {
|
| 149 |
$log_message = t('Locked');
|
| 150 |
}
|
| 151 |
$node->locked = TRUE;
|
| 152 |
$node->revision = TRUE;
|
| 153 |
$node->log = $log_message;
|
| 154 |
node_save($node);
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Unlock node $node.
|
| 159 |
* This creates a new revision with the given log message.
|
| 160 |
*/
|
| 161 |
function node_lock_unlock($node, $log_message = NULL) {
|
| 162 |
if (!$log_message) {
|
| 163 |
$log_message = t('Unlocked');
|
| 164 |
}
|
| 165 |
$node->locked = FALSE;
|
| 166 |
$node->revision = TRUE;
|
| 167 |
$node->log = $log_message;
|
| 168 |
node_save($node);
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* Set locked status of a node.
|
| 173 |
* This sets the status of the latest revision of the node and does not create a new revision.
|
| 174 |
*
|
| 175 |
* @param $node
|
| 176 |
* The node object or node id of the node whose status will be set.
|
| 177 |
* @param $locked
|
| 178 |
* The locked flag. If this is omitted (or NULL) and the node is passed as an object, the 'locked' field is used.
|
| 179 |
*/
|
| 180 |
function node_lock_set_lock_status($node, $locked = NULL) {
|
| 181 |
// Sanitize arguments
|
| 182 |
if (is_object($node)) {
|
| 183 |
$vid = $node->nid;
|
| 184 |
if (is_null($locked) && isset($node->locked)) {
|
| 185 |
$locked = $node->locked;
|
| 186 |
}
|
| 187 |
}
|
| 188 |
else {
|
| 189 |
$vid = db_result(db_query('SELECT vid FROM {node} WHERE nid = %d', (int)$node));
|
| 190 |
}
|
| 191 |
// Convert $locked to booelan.
|
| 192 |
$locked = $locked ? TRUE : FALSE;
|
| 193 |
|
| 194 |
// Update datebase if necessary
|
| 195 |
if ($vid) {
|
| 196 |
if ($locked) {
|
| 197 |
$is_already_locked = db_result(db_query('SELECT COUNT(*) FROM {node_lock} WHERE vid = %d', $vid));
|
| 198 |
if (!$is_already_locked) {
|
| 199 |
db_query('INSERT INTO {node_lock} (vid, locked) VALUES (%d, %d)', $node->vid, TRUE);
|
| 200 |
}
|
| 201 |
}
|
| 202 |
else {
|
| 203 |
db_query('DELETE FROM {node_lock} WHERE vid = %d', $node->vid);
|
| 204 |
}
|
| 205 |
}
|
| 206 |
}
|
| 207 |
|
| 208 |
/**
|
| 209 |
* Menu callback to ask for locking confirmation.
|
| 210 |
*/
|
| 211 |
function node_lock_lock_confirm(&$form_state, $node) {
|
| 212 |
$form['nid'] = array(
|
| 213 |
'#type' => 'value',
|
| 214 |
'#value' => $node->nid,
|
| 215 |
);
|
| 216 |
$form['log'] = array(
|
| 217 |
'#type' => 'textfield',
|
| 218 |
'#title' => t('Log message'),
|
| 219 |
'#default_value' => t('Locked'),
|
| 220 |
'#weight' => -1,
|
| 221 |
);
|
| 222 |
return confirm_form($form,
|
| 223 |
t('Are you sure you want to lock %title?', array('%title' => $node->title)),
|
| 224 |
isset($_GET['destination']) ? $_GET['destination'] : 'node/'. $node->nid,
|
| 225 |
t(''),
|
| 226 |
t('Lock'),
|
| 227 |
t('Cancel')
|
| 228 |
);
|
| 229 |
}
|
| 230 |
|
| 231 |
/**
|
| 232 |
* Submit handler for locking confirmation form.
|
| 233 |
*/
|
| 234 |
function node_lock_lock_confirm_submit($form, &$form_state) {
|
| 235 |
if ($form_state['values']['confirm']) {
|
| 236 |
node_lock_lock(node_load($form_state['values']['nid']), $form_state['values']['log']);
|
| 237 |
}
|
| 238 |
$form_state['redirect'] = 'node/'.$form_state['values']['nid'];
|
| 239 |
}
|
| 240 |
|
| 241 |
/**
|
| 242 |
* Menu callback to ask for unlocking confirmation.
|
| 243 |
*/
|
| 244 |
function node_lock_unlock_confirm(&$form_state, $node) {
|
| 245 |
$form['nid'] = array(
|
| 246 |
'#type' => 'value',
|
| 247 |
'#value' => $node->nid,
|
| 248 |
);
|
| 249 |
$form['log'] = array(
|
| 250 |
'#type' => 'textfield',
|
| 251 |
'#title' => t('Log message'),
|
| 252 |
'#default_value' => t('Unlocked'),
|
| 253 |
'#weight' => -1,
|
| 254 |
);
|
| 255 |
return confirm_form($form,
|
| 256 |
t('Are you sure you want to unlock %title?', array('%title' => $node->title)),
|
| 257 |
isset($_GET['destination']) ? $_GET['destination'] : 'node/'. $node->nid,
|
| 258 |
t(''),
|
| 259 |
t('Unlock'),
|
| 260 |
t('Cancel')
|
| 261 |
);
|
| 262 |
}
|
| 263 |
|
| 264 |
/**
|
| 265 |
* Submit handler for unlocking confirmation form.
|
| 266 |
*/
|
| 267 |
function node_lock_unlock_confirm_submit($form, &$form_state) {
|
| 268 |
if ($form_state['values']['confirm']) {
|
| 269 |
node_lock_unlock(node_load($form_state['values']['nid']), $form_state['values']['log']);
|
| 270 |
}
|
| 271 |
$form_state['redirect'] = 'node/'.$form_state['values']['nid'];
|
| 272 |
}
|