View source
<?php
function views_autorefresh_views_api() {
return array(
'api' => 2.0,
);
}
function views_autorefresh_theme() {
return array(
'views_autorefresh' => array(
'arguments' => array(
'interval' => array(
'default' => NULL,
),
'ping' => array(
'default' => NULL,
),
'incremental' => array(
'default' => NULL,
),
'view' => array(
'default' => NULL,
),
),
),
);
}
function theme_views_autorefresh($interval, $ping = NULL, $incremental = NULL, $view = NULL) {
if (empty($view)) {
$view = views_get_current_view();
}
if (!empty($incremental)) {
$view->autorefresh['incremental'] = $incremental;
}
if (!empty($ping)) {
$view->autorefresh['ping'] = $ping;
}
drupal_add_js(drupal_get_path('module', 'views_autorefresh') . '/views_autorefresh.js');
drupal_add_js(array(
'views_autorefresh' => array(
$view->name => array(
'interval' => $interval,
'ping' => $ping,
'incremental' => $incremental,
'timestamp' => __views_autorefresh_get_timestamp($view),
),
),
), 'setting');
$query_string = drupal_query_string_encode($_REQUEST, array_merge(array(
'q',
'pass',
), array_keys($_COOKIE)));
$query[] = $query_string;
$link = l('', $_GET['q'], array(
'query' => count($query) ? implode('&', $query) : NULL,
));
module_invoke_all('views_autorefresh_plugins', $view);
return '<div class="auto-refresh">' . $link . '</div>';
}
function views_autorefresh_ajax_data_alter(&$object, $type, $view) {
if (!empty($view->autorefresh['incremental']) || !empty($view->autorefresh['ping'])) {
$object->view_name = $view->name;
$object->timestamp = __views_autorefresh_get_timestamp($view);
if (!empty($view->autorefresh['incremental']) && $view->autorefresh['incremental']['view_base_path'] == $_REQUEST['view_base_path']) {
unset($object->__callbacks[array_search('Drupal.Views.Ajax.ajaxViewResponse', $object->__callbacks)]);
array_unshift($object->__callbacks, 'Drupal.views_autorefresh.ajaxViewResponseUpdate');
}
else {
$object->__callbacks[] = 'Drupal.views_autorefresh.ajaxViewResponseRefresh';
}
}
}
function __views_autorefresh_get_timestamp($view) {
if (empty($view->autorefresh['ping']) || empty($view->autorefresh['ping']['use_base_field'])) {
return time();
}
$base_field = $view->base_field;
$max = 0;
foreach ($view->result as $row) {
if ($row->{$base_field} > $max) {
$max = $row->{$base_field};
}
}
return $max;
}