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