function ad_nodeapi in Advertisement 6.3
Same name and namespace in other branches
- 5.2 ad.module \ad_nodeapi()
- 5 ad.module \ad_nodeapi()
- 6 ad.module \ad_nodeapi()
- 6.2 ad.module \ad_nodeapi()
- 7 ad.module \ad_nodeapi()
Implementation of hook_nodeapi().
File
- ./
ad.module, line 829
Code
function ad_nodeapi(&$node, $op, $teaser, $page) {
global $user;
switch ($op) {
case 'load':
$ad = db_fetch_array(db_query('SELECT * FROM {ads} WHERE aid = %d', $node->nid));
$merge = array_merge((array) $node, (array) $ad);
$adtype = module_invoke('ad_' . $ad['adtype'], 'adapi', 'load', $merge);
if (is_array($adtype)) {
return array_merge($ad, $adtype);
}
else {
return $ad;
}
break;
case 'insert':
if (isset($node->adtype)) {
if ($node->status != 1 && $node->adstatus == 'active') {
$node->adstatus = 'expired';
}
$activated = $node->adstatus == 'active' ? time() : 0;
$node->autoactivate = isset($node->autoactivate) ? trim($node->autoactivate) : 0;
$node->autoactivate = is_numeric($node->autoactivate) ? $node->autoactivate : strtotime($node->autoactivate);
$node->autoexpire = isset($node->autoexpire) ? trim($node->autoexpire) : 0;
$node->autoexpire = is_numeric($node->autoexpire) ? $node->autoexpire : strtotime($node->autoexpire);
if (!isset($node->maxviews)) {
$node->maxviews = 0;
}
if (!isset($node->maxclicks)) {
$node->maxclicks = 0;
}
db_query("INSERT INTO {ads} (aid, uid, adstatus, adtype, redirect, autoactivate, autoexpire, activated, maxviews, maxclicks) VALUES(%d, %d, '%s', '%s', '%s', %d, %d, %d, %d, %d)", $node->nid, $node->uid, $node->adstatus, $node->adtype, url('ad/redirect/' . $node->nid, array(
'absolute' => TRUE,
)), $node->autoactivate, $node->autoexpire, $activated, $node->maxviews, $node->maxclicks);
ad_statistics_increment($node->nid, 'create');
}
break;
case 'update':
if (isset($node->adtype)) {
$ad = db_fetch_object(db_query('SELECT * FROM {ads} WHERE aid = %d', $node->nid));
$node->autoactivate = isset($node->autoactivate) ? trim($node->autoactivate) : 0;
$node->autoactivate = is_numeric($node->autoactivate) ? $node->autoactivate : strtotime($node->autoactivate);
$node->autoexpire = isset($node->autoexpire) ? trim($node->autoexpire) : 0;
$node->autoexpire = is_numeric($node->autoexpire) ? $node->autoexpire : strtotime($node->autoexpire);
// Ad must be in approved state to be able to autoactivate it.
if ($node->adstatus != 'approved' && $node->autoactivate) {
if ($node->adstatus == 'active') {
// This ad is already active, no need to autoactivate it.
drupal_set_message(t('This ad is already active, ignoring autoactivate date.'));
$node->autoactivate = 0;
}
else {
drupal_set_message(t('This ad will not be automatically activated at the scheduled time because it is not in the <em>approved</em> state.'), 'error');
}
}
// If this node has been upublished, the ad should no longer be active.
if ($node->status != 1 && $node->adstatus == 'active') {
$node->adstatus = 'expired';
}
// Check if ad is being manually activated.
if ($ad->adstatus != 'active' && $node->adstatus == 'active') {
$activated = time();
}
else {
if ($ad->adstatus != 'expired' && $node->adstatus == 'expired') {
// Ad has been manually expired.
$activated = $ad->activated;
$expired = time();
}
else {
$activated = $ad->activated;
$expired = $ad->expired;
}
}
// Ad status has changed, record the event.
if ($ad->adstatus != $node->adstatus) {
ad_statistics_increment($node->nid, $node->adstatus);
}
// Update ads table with new information.
db_query("UPDATE {ads} SET uid = %d, adstatus = '%s', adtype = '%s', redirect = '%s', autoactivate = %d, autoexpire = %d, activated = %d, maxviews = %d, maxclicks = %d, expired = %d WHERE aid = %d", $node->uid, $node->adstatus, $node->adtype, url('ad/redirect/' . $node->nid, array(
'absolute' => TRUE,
)), $node->autoactivate, $node->autoexpire, isset($activated) ? $activated : 0, isset($node->maxviews) ? (int) $node->maxviews : 0, isset($node->maxclicks) ? (int) $node->maxclicks : 0, isset($expired) ? $expired : 0, $node->nid);
ad_statistics_increment($node->nid, 'update');
}
break;
case 'delete':
// All that's left of the ad is a single timestamp as to when it was
// deleted.
ad_statistics_increment($node->nid, 'delete');
db_query("DELETE FROM {ads} WHERE aid = %d", $node->nid);
db_query("DELETE FROM {ad_statistics} WHERE aid = %d", $node->nid);
break;
case 'view':
if (isset($node->adtype)) {
if (variable_get('ad_meta_noindex', 0)) {
ad_noindex_meta();
}
$node = node_prepare($node, $teaser);
$node->content['body'] = array(
'#value' => $teaser ? $node->teaser : theme('node_ad', $node, $page),
'#weight' => 1,
);
}
break;
case 'validate':
$autoactivate = isset($node->autoactivate) ? trim($node->autoactivate) : '';
if (!empty($autoactivate)) {
$timestamp = is_numeric($autoactivate) ? $autoactivate : strtotime($autoactivate);
if ($timestamp <= 0) {
form_set_error('autoactivate', 'Please select a valid time to automatically active this ad.');
}
}
$autoexpire = isset($node->autoexpire) ? trim($node->autoexpire) : '';
if (!empty($autoexpire)) {
$timestamp = is_numeric($autoexpire) ? $autoexpire : strtotime($autoexpire);
if ($timestamp <= 0) {
form_set_error('autoexpire', 'Please select a valid time to automatically expire this ad.');
}
}
break;
}
// Allow ad type module to act on nodeapi events. The adapi hook provides
// access to additional variables not available in the nodeapi hook.
if (isset($node->adtype)) {
$module = "ad_{$node->adtype}";
module_invoke("ad_{$node->adtype}", 'adapi', $op, $node);
}
// Allow ad cache module to act on nodeapi events.
$cache = variable_get('ad_cache', 'none');
if ($cache != 'none') {
$function = "ad_cache_{$cache}" . '_adcacheapi';
if (function_exists($function)) {
$function($op, $node);
}
}
// Rebuild the cache after all hooks are invoked.
switch ($op) {
case 'insert':
case 'update':
case 'delete':
if (variable_get('ad_cache_file_rebuild_realtime', 0) && isset($node->adtype)) {
ad_rebuild_cache();
}
}
}