Newer
Older
<?php
// $Id$
/**
* @file
* A plug in for the ad.module, allowing an admin to set the probability that
* a given advertisement will be displayed.
*
* Copyright (c) 2008-2009.
* Jeremy Andrews <jeremy@tag1consulting.com>.
*/
define('AD_PROBABILITY_DEFAULT', 100);
/**
* Implementation of hook_form_alter().
* Generate a form for assigning a weight to an advertisement.
*/
function ad_weight_probability_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type']) && $form_id == 'ad_node_form') {
$node = $form['#node'];
$form['weighting'] = array(
'#type' => 'fieldset',
'#access' => user_access('configure ad probability'),
'#title' => t('Weight'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['weighting']['probability'] = array(
'#type' => 'select',
'#access' => user_access('configure ad probability'),
'#title' => t('Probability'),
'#options' => _ad_weight_probability_weights(),
'#default_value' => isset($node->probability) ? $node->probability : 100,
'#description' => t('The greater the probability, the more frequently this advertisement will be displayed. An advertisement with a probablity of 2 will be displayed twice as frequently as an advertisement with a probability of 1.'),
);
$form['weighting']['#weight'] = -1;
}
}
/**
* Implementation of hook_nodeapi().
*/
function ad_weight_probability_nodeapi($node, $op, $arg = 0) {
switch ($op) {
case 'load':
return _ad_weight_probability_node_load($node);
case 'insert':
case 'update':
if (user_access('configure ad probability')) {
// Fully load the node object to confirm that we are working with an
// advertisement.
$ad = node_load($node->nid);
return _ad_weight_probability_node_save($node, $op);
}
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
}
case 'delete':
return _ad_weight_probability_node_delete($node);
}
}
/**
* Implementation of hook_perm().
*/
function ad_weight_probability_perm() {
return array(t('configure ad probability'));
}
/**
* Implementation of hook_ad_build_cache().
*/
function ad_weight_probability_ad_build_cache() {
$cache = array();
$active = db_query("SELECT a.aid, p.probability FROM {ads} a LEFT JOIN {ad_weight_probability} p ON a.aid = p.aid WHERE adstatus = 'active'");
while ($ad = db_fetch_object($active)) {
$probability = $ad->probability ? $ad->probability : AD_PROBABILITY_DEFAULT;
$ads[$ad->aid] = $probability;
}
$cache['weight']['probability'] = $ads;
$cache['weight']['hook_weight'] = array(
'weight' => 10,
'file' => drupal_get_path('module', 'ad_weight_probability') .'/ad_weight_probability.inc',
'function' => 'ad_weight_probability_cache_filter',
);
return $cache;
}
/**
* Helper function, load the probability from the database.
*/
function _ad_weight_probability_node_load($node) {
$probability = (int)db_result(db_query('SELECT probability FROM {ad_weight_probability} WHERE aid = %d', $node->nid));
$output['probability'] = $probability ? $probability : AD_PROBABILITY_DEFAULT;
return $output;
}
/**
* Helper function, save the probability to the database.
*/
function _ad_weight_probability_node_save($node) {
if (is_object($node) && $node->nid) {
db_query('UPDATE {ad_weight_probability} SET probability = %d WHERE aid = %d', $node->probability, $node->nid);
if (!db_affected_rows()) {
db_query('INSERT INTO {ad_weight_probability} (aid, probability) VALUES(%d, %d)', $node->nid, $node->probability);
}
}
}
/**
* Helper function, delete the probability from the database.
*/
function _ad_weight_probability_node_delete($node) {
db_query('DELETE FROM {ad_weight_probability} WHERE aid = %d', $node->nid);
}
/**
* Available weight probabilities.
*/
function _ad_weight_probability_weights() {
return array(
25 => t('1/4'),
33 => t('1/3'),
50 => t('1/2'),
100 => t('1'),
200 => t('2'),
300 => t('3'),
400 => t('4'),
);
}