pierre@1
|
1 <?php |
piotre@7
|
2 // $Id: ad_weight_probability.module,v 1.1.4.8 2009/07/11 16:39:22 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 } |
piotre@7
|
39 // TODO: This hack requires that the ad_channel module is enabled to work. |
piotre@7
|
40 // Ultimately ad.admin.inc should be enhanced to allow plug-in modules to |
piotre@7
|
41 // modify the overview page. |
piotre@7
|
42 else if ($form_id == 'ad_admin_ads' && function_exists('ad_channel_form_alter')) { |
piotre@7
|
43 if (variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL) != AD_CHANNEL_LIST_GROUP) { |
piotre@7
|
44 $weights = _ad_weight_probability_weights(); |
piotre@7
|
45 // ensure a filter has not been set that yeilds no results |
piotre@7
|
46 if (isset($form['title']) && is_array($form['title'])) { |
piotre@7
|
47 foreach ($form['title'] as $aid => $value) { |
piotre@7
|
48 $node->nid = $aid; |
piotre@7
|
49 $result = _ad_weight_probability_node_load($node); |
piotre@7
|
50 $form['probability'][$aid]['#value'] = $weights[$result['probability']]; |
piotre@7
|
51 } |
piotre@7
|
52 } |
piotre@7
|
53 } |
piotre@7
|
54 } |
pierre@1
|
55 } |
pierre@1
|
56 |
pierre@1
|
57 /** |
pierre@1
|
58 * Implementation of hook_nodeapi(). |
pierre@1
|
59 */ |
pierre@1
|
60 function ad_weight_probability_nodeapi($node, $op, $arg = 0) { |
pierre@1
|
61 switch ($op) { |
pierre@1
|
62 case 'load': |
pierre@1
|
63 return _ad_weight_probability_node_load($node); |
pierre@1
|
64 case 'insert': |
pierre@1
|
65 case 'update': |
pierre@1
|
66 if (user_access('configure ad probability')) { |
sly@4
|
67 if (is_object($node) && isset($node->adtype) && |
sly@4
|
68 isset($node->probability) && isset($node->nid)) { |
pierre@1
|
69 return _ad_weight_probability_node_save($node, $op); |
pierre@1
|
70 } |
pierre@1
|
71 } |
sly@4
|
72 break; |
pierre@1
|
73 case 'delete': |
pierre@1
|
74 return _ad_weight_probability_node_delete($node); |
pierre@1
|
75 } |
pierre@1
|
76 } |
pierre@1
|
77 |
pierre@1
|
78 /** |
pierre@1
|
79 * Implementation of hook_perm(). |
pierre@1
|
80 */ |
pierre@1
|
81 function ad_weight_probability_perm() { |
pierre@1
|
82 return array(t('configure ad probability')); |
pierre@1
|
83 } |
pierre@1
|
84 |
pierre@1
|
85 /** |
pierre@1
|
86 * Implementation of hook_ad_build_cache(). |
pierre@1
|
87 */ |
pierre@1
|
88 function ad_weight_probability_ad_build_cache() { |
pierre@1
|
89 $cache = array(); |
pierre@1
|
90 $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
|
91 while ($ad = db_fetch_object($active)) { |
pierre@1
|
92 $probability = $ad->probability ? $ad->probability : AD_PROBABILITY_DEFAULT; |
pierre@1
|
93 $ads[$ad->aid] = $probability; |
pierre@1
|
94 } |
pierre@1
|
95 $cache['weight']['probability'] = $ads; |
pierre@1
|
96 $cache['weight']['hook_weight'] = array( |
pierre@1
|
97 'weight' => 10, |
pierre@1
|
98 'file' => drupal_get_path('module', 'ad_weight_probability') .'/ad_weight_probability.inc', |
pierre@1
|
99 'function' => 'ad_weight_probability_cache_filter', |
pierre@1
|
100 ); |
pierre@1
|
101 return $cache; |
pierre@1
|
102 } |
pierre@1
|
103 |
pierre@1
|
104 /** |
pierre@1
|
105 * Helper function, load the probability from the database. |
pierre@1
|
106 */ |
pierre@1
|
107 function _ad_weight_probability_node_load($node) { |
pierre@1
|
108 $probability = (int)db_result(db_query('SELECT probability FROM {ad_weight_probability} WHERE aid = %d', $node->nid)); |
pierre@1
|
109 $output['probability'] = $probability ? $probability : AD_PROBABILITY_DEFAULT; |
pierre@1
|
110 return $output; |
pierre@1
|
111 } |
pierre@1
|
112 |
pierre@1
|
113 /** |
pierre@1
|
114 * Helper function, save the probability to the database. |
pierre@1
|
115 */ |
pierre@1
|
116 function _ad_weight_probability_node_save($node) { |
sly@4
|
117 if (isset($node->nid) && $node->nid) { |
pierre@1
|
118 db_query('UPDATE {ad_weight_probability} SET probability = %d WHERE aid = %d', $node->probability, $node->nid); |
pierre@1
|
119 if (!db_affected_rows()) { |
pierre@1
|
120 db_query('INSERT INTO {ad_weight_probability} (aid, probability) VALUES(%d, %d)', $node->nid, $node->probability); |
pierre@1
|
121 } |
pierre@1
|
122 } |
pierre@1
|
123 } |
pierre@1
|
124 |
pierre@1
|
125 /** |
pierre@1
|
126 * Helper function, delete the probability from the database. |
pierre@1
|
127 */ |
pierre@1
|
128 function _ad_weight_probability_node_delete($node) { |
pierre@1
|
129 db_query('DELETE FROM {ad_weight_probability} WHERE aid = %d', $node->nid); |
pierre@1
|
130 } |
pierre@1
|
131 |
pierre@1
|
132 /** |
pierre@1
|
133 * Available weight probabilities. |
pierre@1
|
134 */ |
pierre@1
|
135 function _ad_weight_probability_weights() { |
pierre@1
|
136 return array( |
pierre@1
|
137 25 => t('1/4'), |
pierre@1
|
138 33 => t('1/3'), |
pierre@1
|
139 50 => t('1/2'), |
pierre@1
|
140 100 => t('1'), |
pierre@1
|
141 200 => t('2'), |
pierre@1
|
142 300 => t('3'), |
pierre@1
|
143 400 => t('4'), |
pierre@1
|
144 ); |
pierre@1
|
145 } |
pierre@1
|
146 |