View source
<?php
define('EDIT_LIMIT_NODE_COUNT_DEFAULT', 3);
define('EDIT_LIMIT_COMMENT_COUNT', 3);
define('EDIT_LIMIT_NODE_TIME_DEFAULT', 86400);
define('EDIT_LIMIT_COMMENT_TIME', 300);
define('EDIT_LIMIT_TIME_UNITS', t('@seconds,@minutes,@hours,@days', array(
"@seconds" => "seconds",
"@minutes" => "minutes",
"@hours" => "hours",
"@days" => "days",
)));
define('EDIT_LIMIT_TIME_UNIT_DEFAULT', t('seconds'));
function edit_limit_menu() {
$items['admin/config/content/edit_limit'] = array(
'title' => 'Edit Limit',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'edit_limit_settings',
),
'access arguments' => array(
'administer edit limit',
),
'file' => 'edit_limit.admin.inc',
'description' => 'Configure how often or how soon nodes and comments can be edited before they are locked.',
);
return $items;
}
function edit_limit_permission() {
return array(
'bypass edit limits' => array(
'title' => t('Bypass edit limits'),
'description' => t('Bypass the edit limits set in place for normal users.'),
),
'administer edit limit' => array(
'title' => t('Administer edit limits'),
'description' => t('Bypass the edit limits set in place for normal users.'),
),
);
}
function edit_limit_menu_alter(&$items) {
$items['node/%node/edit']['access callback'] = 'edit_limit_node_access';
$items['comment/%comment/edit']['access callback'] = 'edit_limit_comment_access';
}
function edit_limit_node_access($op, $node, $account = NULL) {
module_load_include('inc', 'node', 'node.pages');
if (node_access($op, $node, $account)) {
switch ($op) {
case 'update':
if (user_access('bypass edit limits')) {
return TRUE;
}
return _edit_limit_user_acccess($node);
}
}
return FALSE;
}
function _edit_limit_user_acccess($node) {
$return = TRUE;
if (empty($node->nid)) {
return $return;
}
$allowed_types = variable_get('edit_limit_node_types', array());
if (empty($allowed_types[$node->type])) {
return $return;
}
$edit_limit_node_time_enabled = variable_get('edit_limit_node_time_enabled', FALSE);
$time_limits['default'] = intval(variable_get('edit_limit_node_time_default', 10)) * 24 * 60 * 60;
$time_limits['node'][$node->type] = intval(variable_get('edit_limit_node_time_' . $node->type, $time_limits['default']));
$edit_limit_node_count_enabled = variable_get('edit_limit_node_count_enabled', FALSE);
$count_limits['default'] = intval(variable_get('edit_limit_node_count_default', EDIT_LIMIT_NODE_COUNT_DEFAULT));
$count_limits['node'][$node->type] = intval(variable_get('edit_limit_node_count_' . $node->type, $count_limits['default']));
if (!$edit_limit_node_time_enabled && !$edit_limit_node_count_enabled) {
return $return;
}
$count_limit_reached = FALSE;
if ($edit_limit_node_count_enabled && $return) {
$edit_count = edit_limit_node_count($node->nid);
$count_limit = $count_limits['node'][$node->type] ? $count_limits['node'][$node->type] : $count_limits['node']['default'];
if ($count_limit <= $edit_count) {
$return = FALSE;
$count_limit_reached = TRUE;
}
}
if ($edit_limit_node_time_enabled && $return) {
$time_unit = variable_get('edit_limit_node_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT);
$timeleft = intval($node->created) + intval(_unit_to_seconds(variable_get('edit_limit_node_time_default', EDIT_LIMIT_NODE_TIME_DEFAULT), $time_unit)) - REQUEST_TIME;
drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
if ($timeleft <= 0) {
$time_limit_reached = TRUE;
$return = FALSE;
}
elseif (!$count_limit_reached && arg(0) == 'node' && arg(2) == 'edit') {
static $type;
if (empty($type)) {
$type = 'status';
$message = '<span class="edit-limit-time-unit-' . $time_unit . '">' . t('You have %seconds %time_unit left to edit this content.', array(
'%seconds' => $timeleft,
'%time_unit' => t('seconds'),
)) . '</span>';
drupal_set_message(filter_xss($message, array(
'em',
'span',
)), $type, FALSE);
}
}
}
return $return;
}
function _unit_to_seconds($time = 0, $unit = 'seconds') {
switch ($unit) {
case 'minutes':
$time_in_seconds = $time * 60;
break;
case 'hours':
$time_in_seconds = $time * 60 * 60;
break;
case 'days':
$time_in_seconds = $time * 60 * 60 * 24;
break;
default:
$time_in_seconds = $time;
break;
}
return $time_in_seconds;
}
function edit_limit_comment_access($edit = NULL, $comment = NULL) {
if (user_access('post comments')) {
if (user_access('bypass edit limits')) {
return TRUE;
}
$return = comment_access($edit, $comment);
$node = node_load($comment->nid);
if (!in_array($node->type, variable_get('edit_limit_comments', array()))) {
return $return;
}
if (variable_get('edit_limit_comment_time_enabled', FALSE)) {
$time_unit = variable_get('edit_limit_comment_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT);
if ($comment->created + intval(_unit_to_seconds(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME), $time_unit)) < REQUEST_TIME) {
$return = FALSE;
}
else {
static $time_left;
if (empty($time_left)) {
drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
$time_left = $comment->created + intval(_unit_to_seconds(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME), $time_unit)) - REQUEST_TIME;
}
if (user_access('edit own comments')) {
$type = 'status';
$message = '<span class="edit-limit-time-unit-' . check_plain($time_unit) . '">' . t('You have %time_left %time_unit left to edit this comment.', array(
'%time_left' => $time_left,
'%time_unit' => 'seconds',
)) . '</span>';
drupal_set_message(filter_xss($message, array(
'em',
'span',
)), $type, FALSE);
}
}
}
return $return;
}
return FALSE;
}
function edit_limit_node_update($node) {
if (variable_get('edit_limit_node_count_enabled', FALSE)) {
$count_data = db_select('edit_limit_node_count', 'e')
->fields('e')
->condition('nid', $node->nid, '=')
->execute()
->fetchAssoc();
$edit_count = 0;
$update = array();
if (!empty($count_data)) {
$edit_count = $count_data['count'];
$update = array(
'nid',
);
}
$edit_count++;
$data = array(
'nid' => $node->nid,
'count' => $edit_count,
);
drupal_write_record('edit_limit_node_count', $data, $update);
}
}
function edit_limit_node_prepare($node) {
if (!empty($node->nid)) {
if (variable_get('edit_limit_node_count_enabled', FALSE)) {
$limit = variable_get('edit_limit_node_count_default', EDIT_LIMIT_NODE_COUNT_DEFAULT);
$count = edit_limit_node_count($node->nid);
$type = $count + 2 > $limit ? 'warning' : 'status';
$message = t("This node has been edit !count out of !limit times, after which it will be locked.", array(
'!count' => $count,
'!limit' => $limit,
));
drupal_set_message(filter_xss($message), $type);
}
}
}
function edit_limit_node_count($nid) {
$count = db_select('edit_limit_node_count', 'e')
->fields('e', array(
'count',
))
->condition('nid', $nid, '=')
->execute()
->fetchField();
$count = empty($count) ? 0 : $count;
return $count;
}
function edit_limit_node_view_alter(&$build) {
if (!user_access('edit own comments')) {
return FALSE;
}
if (!empty($build['links']['comment']['#links']['comment-add']) && !empty($build['comments']['comments'])) {
if (0) {
$results = db_query("SELECT * FROM {edit_limit_comment_time} WHERE cid = :cid", array(
':cid' => $comment->cid,
));
}
foreach ($build['comments']['comments'] as $key => $comment) {
if (!is_numeric($key)) {
continue;
}
if (!user_access('bypass edit limits') && variable_get('edit_limit_comment_time_enabled', FALSE) && !empty($build['comments']['comments'][$key]['links']['comment']['#links']['comment-edit'])) {
$time_unit = variable_get('edit_limit_comment_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT);
$time_limit = $comment['#comment']->created + intval(_unit_to_seconds(variable_get('edit_limit_comment_time', EDIT_LIMIT_COMMENT_TIME), $time_unit));
if ($time_limit < REQUEST_TIME) {
unset($build['comments']['comments'][$key]['links']['comment']['#links']['comment-edit']);
}
else {
drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
$build['comments']['comments'][$key]['links']['comment']['#links']['comment-edit']['title'] .= ' <span class="edit-limit-time-unit-' . $time_unit . '">' . t('(%time_limit %time_unit left)', array(
'%time_limit' => $time_limit - REQUEST_TIME,
'%time_unit' => 'seconds',
)) . '</span>';
}
}
}
}
}