pierre@1
|
1 <?php |
sly@4
|
2 // $Id: ad_weight_probability.module,v 1.1.4.6 2009/04/22 15:14:46 jeremy Exp $ |
pierre@1
|
3 |
pierre@1
|
4 /** |
pierre@1
|
5 * @file |
pierre@1
|
6 * A plug in for the ad.module, allowing an admin to set the probability that |
pierre@1
|
7 * a given advertisement will be displayed. |
pierre@1
|
8 * |
pierre@1
|
9 * Copyright (c) 2008-2009. |
pierre@1
|
10 * Jeremy Andrews <jeremy@tag1consulting.com>. |
pierre@1
|
11 */ |
pierre@1
|
12 |
pierre@1
|
13 define('AD_PROBABILITY_DEFAULT', 100); |
pierre@1
|
14 |
pierre@1
|
15 /** |
pierre@1
|
16 * Implementation of hook_form_alter(). |
pierre@1
|
17 * Generate a form for assigning a weight to an advertisement. |
pierre@1
|
18 */ |
pierre@1
|
19 function ad_weight_probability_form_alter(&$form, &$form_state, $form_id) { |
pierre@1
|
20 if (isset($form['type']) && $form_id == 'ad_node_form') { |
pierre@1
|
21 $node = $form['#node']; |
pierre@1
|
22 $form['weighting'] = array( |
pierre@1
|
23 '#type' => 'fieldset', |
pierre@1
|
24 '#access' => user_access('configure ad probability'), |
pierre@1
|
25 '#title' => t('Weight'), |
pierre@1
|
26 '#collapsible' => TRUE, |
pierre@1
|
27 '#collapsed' => FALSE, |
pierre@1
|
28 ); |
pierre@1
|
29 $form['weighting']['probability'] = array( |
pierre@1
|
30 '#type' => 'select', |
pierre@1
|
31 '#access' => user_access('configure ad probability'), |
pierre@1
|
32 '#title' => t('Probability'), |
pierre@1
|
33 '#options' => _ad_weight_probability_weights(), |
pierre@1
|
34 '#default_value' => isset($node->probability) ? $node->probability : 100, |
pierre@1
|
35 '#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.'), |
pierre@1
|
36 ); |
pierre@1
|
37 $form['weighting']['#weight'] = -1; |
pierre@1
|
38 } |
pierre@1
|
39 } |
pierre@1
|
40 |
pierre@1
|
41 /** |
pierre@1
|
42 * Implementation of hook_nodeapi(). |
pierre@1
|
43 */ |
pierre@1
|
44 function ad_weight_probability_nodeapi($node, $op, $arg = 0) { |
pierre@1
|
45 switch ($op) { |
pierre@1
|
46 case 'load': |
pierre@1
|
47 return _ad_weight_probability_node_load($node); |
pierre@1
|
48 case 'insert': |
pierre@1
|
49 case 'update': |
pierre@1
|
50 if (user_access('configure ad probability')) { |
sly@4
|
51 if (is_object($node) && isset($node->adtype) && |
sly@4
|
52 isset($node->probability) && isset($node->nid)) { |
pierre@1
|
53 return _ad_weight_probability_node_save($node, $op); |
pierre@1
|
54 } |
pierre@1
|
55 } |
sly@4
|
56 break; |
pierre@1
|
57 case 'delete': |
pierre@1
|
58 return _ad_weight_probability_node_delete($node); |
pierre@1
|
59 } |
pierre@1
|
60 } |
pierre@1
|
61 |
pierre@1
|
62 /** |
pierre@1
|
63 * Implementation of hook_perm(). |
pierre@1
|
64 */ |
pierre@1
|
65 function ad_weight_probability_perm() { |
pierre@1
|
66 return array(t('configure ad probability')); |
pierre@1
|
67 } |
pierre@1
|
68 |
pierre@1
|
69 /** |
pierre@1
|
70 * Implementation of hook_ad_build_cache(). |
pierre@1
|
71 */ |
pierre@1
|
72 function ad_weight_probability_ad_build_cache() { |
pierre@1
|
73 $cache = array(); |
pierre@1
|
74 $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'"); |
pierre@1
|
75 while ($ad = db_fetch_object($active)) { |
pierre@1
|
76 $probability = $ad->probability ? $ad->probability : AD_PROBABILITY_DEFAULT; |
pierre@1
|
77 $ads[$ad->aid] = $probability; |
pierre@1
|
78 } |
pierre@1
|
79 $cache['weight']['probability'] = $ads; |
pierre@1
|
80 $cache['weight']['hook_weight'] = array( |
pierre@1
|
81 'weight' => 10, |
pierre@1
|
82 'file' => drupal_get_path('module', 'ad_weight_probability') .'/ad_weight_probability.inc', |
pierre@1
|
83 'function' => 'ad_weight_probability_cache_filter', |
pierre@1
|
84 ); |
pierre@1
|
85 return $cache; |
pierre@1
|
86 } |
pierre@1
|
87 |
pierre@1
|
88 /** |
pierre@1
|
89 * Helper function, load the probability from the database. |
pierre@1
|
90 */ |
pierre@1
|
91 function _ad_weight_probability_node_load($node) { |
pierre@1
|
92 $probability = (int)db_result(db_query('SELECT probability FROM {ad_weight_probability} WHERE aid = %d', $node->nid)); |
pierre@1
|
93 $output['probability'] = $probability ? $probability : AD_PROBABILITY_DEFAULT; |
pierre@1
|
94 return $output; |
pierre@1
|
95 } |
pierre@1
|
96 |
pierre@1
|
97 /** |
pierre@1
|
98 * Helper function, save the probability to the database. |
pierre@1
|
99 */ |
pierre@1
|
100 function _ad_weight_probability_node_save($node) { |
sly@4
|
101 if (isset($node->nid) && $node->nid) { |
pierre@1
|
102 db_query('UPDATE {ad_weight_probability} SET probability = %d WHERE aid = %d', $node->probability, $node->nid); |
pierre@1
|
103 if (!db_affected_rows()) { |
pierre@1
|
104 db_query('INSERT INTO {ad_weight_probability} (aid, probability) VALUES(%d, %d)', $node->nid, $node->probability); |
pierre@1
|
105 } |
pierre@1
|
106 } |
pierre@1
|
107 } |
pierre@1
|
108 |
pierre@1
|
109 /** |
pierre@1
|
110 * Helper function, delete the probability from the database. |
pierre@1
|
111 */ |
pierre@1
|
112 function _ad_weight_probability_node_delete($node) { |
pierre@1
|
113 db_query('DELETE FROM {ad_weight_probability} WHERE aid = %d', $node->nid); |
pierre@1
|
114 } |
pierre@1
|
115 |
pierre@1
|
116 /** |
pierre@1
|
117 * Available weight probabilities. |
pierre@1
|
118 */ |
pierre@1
|
119 function _ad_weight_probability_weights() { |
pierre@1
|
120 return array( |
pierre@1
|
121 25 => t('1/4'), |
pierre@1
|
122 33 => t('1/3'), |
pierre@1
|
123 50 => t('1/2'), |
pierre@1
|
124 100 => t('1'), |
pierre@1
|
125 200 => t('2'), |
pierre@1
|
126 300 => t('3'), |
pierre@1
|
127 400 => t('4'), |
pierre@1
|
128 ); |
pierre@1
|
129 } |
pierre@1
|
130 |