| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
function nodeupdate_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
|
| 5 |
$types = variable_get('nodeupdate_types', NULL);
|
| 6 |
if (isset($types) && !$types[$node->type]) return;
|
| 7 |
switch ($op) {
|
| 8 |
case 'view':
|
| 9 |
if ($a3 == TRUE) break; // teaser?
|
| 10 |
_nodeupdate_inject($node->nid);
|
| 11 |
break;
|
| 12 |
case 'insert':
|
| 13 |
case 'update':
|
| 14 |
cache_set('nodeupdate:'. $node->nid, array('changed' => $node->changed), 'cache');
|
| 15 |
break;
|
| 16 |
case 'delete':
|
| 17 |
cache_clear_all('nodeupdate:'. $node->nid, 'cache');
|
| 18 |
break;
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
function nodeupdate_form_alter(&$form, $form_state, $form_id) {
|
| 23 |
if (!isset($form['type']) || !isset($form['nid']['#value'])) return;
|
| 24 |
$types = variable_get('nodeupdate_types', NULL);
|
| 25 |
if (isset($types) && !$types[$form['type']['#value']]) return;
|
| 26 |
_nodeupdate_inject(intval($form['nid']['#value']));
|
| 27 |
}
|
| 28 |
|
| 29 |
function nodeupdate_menu() {
|
| 30 |
$items['nodeupdate'] = array(
|
| 31 |
'title' => 'Node update',
|
| 32 |
'description' => 'Check if the node has been updated since a given time.',
|
| 33 |
'page callback' => '_nodeupdate_check',
|
| 34 |
'type' => MENU_CALLBACK,
|
| 35 |
'access arguments' => array('access content'),
|
| 36 |
);
|
| 37 |
$items['admin/settings/nodeupdate'] = array(
|
| 38 |
'title' => 'Node update settings',
|
| 39 |
'description' => 'Administer settings for the node update notification.',
|
| 40 |
'page callback' => 'drupal_get_form',
|
| 41 |
'page arguments' => array('_nodeupdate_settings'),
|
| 42 |
'access arguments' => array('administer site settings'),
|
| 43 |
);
|
| 44 |
return $items;
|
| 45 |
}
|
| 46 |
|
| 47 |
function _nodeupdate_inject($nid) {
|
| 48 |
drupal_add_js(drupal_get_path('module', 'nodeupdate').'/nodeupdate.js');
|
| 49 |
drupal_add_js(array('nodeupdate' => array(
|
| 50 |
'nid' => $nid,
|
| 51 |
'time' => time(),
|
| 52 |
'messageUpdated' => t('This node has been updated. Would you like to reload it?'),
|
| 53 |
'messageDeleted' => t('This node has been deleted. We will navigate away from it.'),
|
| 54 |
'interval' => variable_get('nodeupdate_interval', 5000),
|
| 55 |
)),
|
| 56 |
'setting'
|
| 57 |
);
|
| 58 |
}
|
| 59 |
|
| 60 |
function _nodeupdate_last_changed($nid) {
|
| 61 |
if (($cache = cache_get('nodeupdate:'. $nid, 'cache')) && !empty($cache->data)) {
|
| 62 |
$changed = $cache->data['changed'];
|
| 63 |
}
|
| 64 |
else {
|
| 65 |
$changed = node_last_changed($nid);
|
| 66 |
if ($changed) { // NULL if deleted
|
| 67 |
cache_set('nodeupdate:'. $nid, array('changed' => $changed), 'cache');
|
| 68 |
}
|
| 69 |
}
|
| 70 |
return $changed;
|
| 71 |
}
|
| 72 |
|
| 73 |
function _nodeupdate_check($nid, $time) {
|
| 74 |
$changed = _nodeupdate_last_changed($nid);
|
| 75 |
drupal_json(array('changed' => (!$changed ? -1 : ($changed > $time ? $changed : 0))));
|
| 76 |
}
|
| 77 |
|
| 78 |
function _nodeupdate_settings() {
|
| 79 |
$form['nodeupdate_interval'] = array(
|
| 80 |
'#type' => 'textfield',
|
| 81 |
'#title' => t('Refresh interval'),
|
| 82 |
'#default_value' => variable_get('nodeupdate_interval', 5000),
|
| 83 |
'#field_suffix' => t('milliseconds'),
|
| 84 |
'#description' => t('The time interval before the module checks again if the node has been updated.'),
|
| 85 |
);
|
| 86 |
$form['nodeupdate_types'] = array(
|
| 87 |
'#title' => t('Content types'),
|
| 88 |
'#type' => 'checkboxes',
|
| 89 |
'#options' => node_get_types('names'),
|
| 90 |
'#default_value' => variable_get('nodeupdate_types', NULL),
|
| 91 |
'#description' => t('Content types that the module will intercept to check for node updates.'),
|
| 92 |
);
|
| 93 |
return system_settings_form($form);
|
| 94 |
}
|
| 95 |
|