View source
<?php
function rate_expiration_form_rate_widget_form_alter(&$form, &$form_state) {
if (isset($form['error'])) {
return;
}
if ($form['#widget_id']) {
$widgets = variable_get(RATE_VAR_WIDGETS, array());
$widget = $widgets[$form['#widget_id']];
}
else {
$widget = new stdClass();
}
$form['expiration'] = array(
'#type' => 'fieldset',
'#title' => t('Expiration settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$options = array(
-1 => t('Never'),
) + drupal_map_assoc(array(
300,
900,
1800,
3600,
10800,
21600,
32400,
43200,
86400,
172800,
345600,
604800,
1209600,
1814400,
2419200,
), 'format_interval');
$form['expiration']['expiration'] = array(
'#type' => 'select',
'#title' => t('Disable voting after this period'),
'#options' => $options,
'#default_value' => isset($widget->expiration) ? $widget->expiration : -1,
'#description' => t('Time that has to elapse after creating a node before voting is disabled.'),
);
$form['expiration']['expiration_allow_override'] = array(
'#type' => 'checkbox',
'#title' => t('Allow override'),
'#default_value' => isset($widget->expiration_allow_override) ? $widget->expiration_allow_override : FALSE,
'#description' => t('When enabled, the expiration date can be customized in the node edit screen.'),
);
}
function rate_expiration_rate_widget($op, &$widget, $values = NULL) {
switch ($op) {
case 'insert':
case 'update':
$widget->expiration = $values['expiration'];
$widget->expiration_allow_override = !empty($values['expiration_allow_override']);
break;
case 'delete':
$sql = 'DELETE FROM {rate_expiration} WHERE widget_name = \'%s\'';
db_query($sql, $widget->name);
break;
case 'view':
if ($values['content_type'] == 'node') {
if ($node = node_load($values['content_id'])) {
$configuration = _rate_expiration_get_node_values($node->nid);
if (isset($configuration[$widget->name])) {
$start = $configuration[$widget->name]['start'];
$end = $configuration[$widget->name]['end'];
}
else {
$start = 0;
$end = 0;
}
if ($widget->expiration > 0) {
!empty($end) or $end = $node->created + $widget->expiration;
}
if ($end > 0 && $end <= time() || $start > 0 && $start > time()) {
$widget->mode = RATE_CLOSED;
}
}
}
break;
}
}
function _rate_expiration_get_node_values($nid) {
static $cache = array();
if (isset($cache[$nid])) {
return $cache[$nid];
}
$output = array();
$sql = 'SELECT re.widget_name, re.start, re.end
FROM {rate_expiration} re
WHERE nid = %d';
$res = db_query($sql, $nid);
while ($rec = db_fetch_array($res)) {
$output[$rec['widget_name']] = $rec;
}
$cache[$nid] = $output;
return $output;
}
function rate_expiration_form_alter(&$form, &$form_state, $form_id) {
if (!preg_match('/_node_form$/', $form_id)) {
return;
}
$node = $form['#node'];
$widgets = rate_get_active_widgets('node', $node->type);
$widgets = array_filter($widgets, '_rate_expiration_filter_widgets');
if (!$widgets) {
return;
}
$defaults = empty($node->nid) ? array() : _rate_expiration_get_node_values($node->nid);
foreach ($widgets as $widget) {
if (isset($defaults[$widget->name])) {
$startdate = $defaults[$widget->name]['start'];
$enddate = $defaults[$widget->name]['end'];
}
else {
$startdate = empty($node->created) ? time() : $node->created;
$enddate = NULL;
$startdate -= 180;
}
$container = 'rate_expiration_' . $widget->name;
$form[$container] = array(
'#type' => 'fieldset',
'#title' => t('Expiration for %name widget', array(
'%name' => $widget->title,
)),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form[$container]["{$container}_begin"] = array(
'#type' => 'date_select',
'#title' => t('Enable voting on'),
'#description' => t('Leave empty to disable startdate.'),
'#default_value' => $startdate ? format_date($startdate, 'custom', 'Y-m-d H:i:s') : NULL,
'#date_increment' => 5,
);
$form[$container]["{$container}_end"] = array(
'#type' => 'date_select',
'#title' => t('Close voting on'),
'#description' => $widget->expiration > 0 ? t('Leave empty to allow for @interval after node creation', array(
'@interval' => format_interval($widget->expiration),
)) : t('Leave empty to allow forever.'),
'#default_value' => $enddate ? format_date($enddate, 'custom', 'Y-m-d H:i:s') : NULL,
'#date_increment' => 5,
);
}
}
function _rate_expiration_filter_widgets($item) {
return !empty($item->expiration_allow_override);
}
function rate_expiration_nodeapi(&$node, $op, $teaser, $page) {
global $user;
switch ($op) {
case 'insert':
case 'update':
$widgets = rate_get_active_widgets('node', $node->type);
$widgets = array_filter($widgets, '_rate_expiration_filter_widgets');
foreach ($widgets as $widget) {
$timezone = is_null($user->timezone) ? variable_get('date_default_timezone', 0) : $user->timezone;
$container = 'rate_expiration_' . $widget->name;
$begin = "{$container}_begin";
$end = "{$container}_end";
$begin = $node->{$begin} ? strtotime($node->{$begin} . 'Z') - $timezone : NULL;
$end = $node->{$end} ? strtotime($node->{$end} . 'Z') - $timezone : NULL;
if (empty($begin) && empty($end)) {
$sql = 'DELETE FROM {rate_expiration} WHERE nid = %d';
db_query($sql, $node->nid);
}
else {
$sql = 'UPDATE {rate_expiration}
SET start = %d, end = %d
WHERE nid = %d';
db_query($sql, $begin, $end, $node->nid);
if (!db_affected_rows()) {
$sql = 'INSERT INTO {rate_expiration}
(nid, widget_name, start, end)
VALUES
(%d, \'%s\', %d, %d)';
db_query($sql, $node->nid, $widget->name, $begin, $end);
}
}
}
break;
case 'delete':
$sql = 'DELETE FROM {rate_expiration} WHERE nid = %d';
db_query($sql, $node->nid);
break;
}
}