View source
<?php
define("NODE_LIMIT_INTERVAL_SECONDS", "1");
define("NODE_LIMIT_INTERVAL_MINUTES", "60");
define("NODE_LIMIT_INTERVAL_HOURS", "3600");
define("NODE_LIMIT_INTERVAL_DAYS", "86400");
define("NODE_LIMIT_INTERVAL_WEEKS", "604800");
define("NODE_LIMIT_INTERVAL_MONTHS", "2419200");
define("NODE_LIMIT_INTERVAL_YEARS", "31536000");
function node_limit_interval_node_limit_applies_in_context($lid, $node, $user) {
$limit = node_limit_interval_node_limit_load($lid);
$applies = NODE_LIMIT_LIMIT_DOES_APPLY;
if (empty($limit)) {
$applies = NODE_LIMIT_LIMIT_NEUTRAL;
}
return array(
'node_limit_interval' => $applies,
);
}
function node_limit_interval_node_limit_sql($lid, SelectQuery $select) {
$limit = node_limit_interval_node_limit_load($lid);
if (empty($limit)) {
return;
}
if ($limit['node_limit_interval'] != NODE_LIMIT_INTERVAL_MONTHS) {
$interval = floatval($limit['node_limit_interval']['interval']) * intval($limit['node_limit_interval']['unit']);
}
else {
$then = mktime(date('H'), date('i'), date('s'), date('n') - floatval($limit['node_limit_interval']['interval']));
$now = mktime();
$interval = $now - $then;
}
$select
->where('created + :interval >= :reference', array(
':interval' => $interval,
':reference' => \Drupal::time()
->getRequestTime(),
));
}
function node_limit_interval_node_limit_render_element(&$element) {
$title = $element['interval']['#title'];
unset($element['interval']['#title']);
$interval = \Drupal::service('renderer')
->render($element['interval']);
$unit = \Drupal::service('renderer')
->render($element['unit']);
$html = str_replace('div', 'span', $title . $interval . $unit);
return $html;
}
function node_limit_interval_node_limit_element($lid = 0) {
$limit = node_limit_interval_node_limit_load($lid);
$interval_options = _node_limit_interval_interval_options();
$interval = !empty($limit['node_limit_interval']['interval']) ? $limit['node_limit_interval']['interval'] : '';
$unit = !empty($limit['node_limit_interval']['unit']) ? $limit['node_limit_interval']['unit'] : '';
return array(
'node_limit_interval' => array(
'#title' => t('Time Interval'),
'#custom_render' => TRUE,
'interval' => array(
'#type' => 'textfield',
'#title' => t('Within the past'),
'#size' => 10,
'#default_value' => $interval,
),
'unit' => array(
'#type' => 'select',
'#options' => $interval_options,
'#default_value' => $unit,
),
),
);
}
function _node_limit_interval_interval_options() {
return array(
NODE_LIMIT_INTERVAL_SECONDS => t('Seconds'),
NODE_LIMIT_INTERVAL_MINUTES => t('Minutes'),
NODE_LIMIT_INTERVAL_HOURS => t('Hours'),
NODE_LIMIT_INTERVAL_DAYS => t('Days'),
NODE_LIMIT_INTERVAL_WEEKS => t('Weeks'),
NODE_LIMIT_INTERVAL_MONTHS => t('Months'),
NODE_LIMIT_INTERVAL_YEARS => t('Years'),
);
}
function node_limit_interval_node_limit_element_validate($element) {
if (!is_numeric($element['interval']) || floatval($element['interval']) <= 0) {
return array(
'error' => t('Time intervals must be an integer greater than 0'),
'element' => 'interval',
);
}
return TRUE;
}
function node_limit_interval_node_limit_save($lid, $applies, $element) {
if ($applies) {
$interval = intval($element['interval']);
$unit = intval($element['unit']);
\Drupal::database()
->insert('node_limit_interval')
->fields(array(
'lid' => $lid,
'ninterval' => $interval,
'unit' => $unit,
))
->execute();
}
}
function node_limit_interval_node_limit_delete($lids) {
\Drupal::database()
->delete('node_limit_interval')
->condition('lid', $lids, 'IN')
->execute();
}
function node_limit_interval_node_limit_load($lid) {
$select = \Drupal::database()
->select('node_limit_interval', 'nti')
->fields('nti')
->condition('lid', $lid);
$info = $select
->execute()
->fetchAssoc();
if (empty($info['lid'])) {
return array();
}
return array(
'node_limit_interval' => array(
'interval' => $info['ninterval'],
'unit' => $info['unit'],
),
);
}