annotate 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
rev   line source
pierre@1 1 <?php
pierre@1 2 // $Id: ad_weight_probability.module,v 1.1.4.5 2009/03/27 19:11:50 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')) {
pierre@1 51 // Fully load the node object to confirm that we are working with an
pierre@1 52 // advertisement.
pierre@1 53 $ad = node_load($node->nid);
pierre@1 54 if (isset($ad->adtype)) {
pierre@1 55 return _ad_weight_probability_node_save($node, $op);
pierre@1 56 }
pierre@1 57 }
pierre@1 58 case 'delete':
pierre@1 59 return _ad_weight_probability_node_delete($node);
pierre@1 60 }
pierre@1 61 }
pierre@1 62
pierre@1 63 /**
pierre@1 64 * Implementation of hook_perm().
pierre@1 65 */
pierre@1 66 function ad_weight_probability_perm() {
pierre@1 67 return array(t('configure ad probability'));
pierre@1 68 }
pierre@1 69
pierre@1 70 /**
pierre@1 71 * Implementation of hook_ad_build_cache().
pierre@1 72 */
pierre@1 73 function ad_weight_probability_ad_build_cache() {
pierre@1 74 $cache = array();
pierre@1 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'");
pierre@1 76 while ($ad = db_fetch_object($active)) {
pierre@1 77 $probability = $ad->probability ? $ad->probability : AD_PROBABILITY_DEFAULT;
pierre@1 78 $ads[$ad->aid] = $probability;
pierre@1 79 }
pierre@1 80 $cache['weight']['probability'] = $ads;
pierre@1 81 $cache['weight']['hook_weight'] = array(
pierre@1 82 'weight' => 10,
pierre@1 83 'file' => drupal_get_path('module', 'ad_weight_probability') .'/ad_weight_probability.inc',
pierre@1 84 'function' => 'ad_weight_probability_cache_filter',
pierre@1 85 );
pierre@1 86 return $cache;
pierre@1 87 }
pierre@1 88
pierre@1 89 /**
pierre@1 90 * Helper function, load the probability from the database.
pierre@1 91 */
pierre@1 92 function _ad_weight_probability_node_load($node) {
pierre@1 93 $probability = (int)db_result(db_query('SELECT probability FROM {ad_weight_probability} WHERE aid = %d', $node->nid));
pierre@1 94 $output['probability'] = $probability ? $probability : AD_PROBABILITY_DEFAULT;
pierre@1 95 return $output;
pierre@1 96 }
pierre@1 97
pierre@1 98 /**
pierre@1 99 * Helper function, save the probability to the database.
pierre@1 100 */
pierre@1 101 function _ad_weight_probability_node_save($node) {
pierre@1 102 if (is_object($node) && $node->nid) {
pierre@1 103 db_query('UPDATE {ad_weight_probability} SET probability = %d WHERE aid = %d', $node->probability, $node->nid);
pierre@1 104 if (!db_affected_rows()) {
pierre@1 105 db_query('INSERT INTO {ad_weight_probability} (aid, probability) VALUES(%d, %d)', $node->nid, $node->probability);
pierre@1 106 }
pierre@1 107 }
pierre@1 108 }
pierre@1 109
pierre@1 110 /**
pierre@1 111 * Helper function, delete the probability from the database.
pierre@1 112 */
pierre@1 113 function _ad_weight_probability_node_delete($node) {
pierre@1 114 db_query('DELETE FROM {ad_weight_probability} WHERE aid = %d', $node->nid);
pierre@1 115 }
pierre@1 116
pierre@1 117 /**
pierre@1 118 * Available weight probabilities.
pierre@1 119 */
pierre@1 120 function _ad_weight_probability_weights() {
pierre@1 121 return array(
pierre@1 122 25 => t('1/4'),
pierre@1 123 33 => t('1/3'),
pierre@1 124 50 => t('1/2'),
pierre@1 125 100 => t('1'),
pierre@1 126 200 => t('2'),
pierre@1 127 300 => t('3'),
pierre@1 128 400 => t('4'),
pierre@1 129 );
pierre@1 130 }
pierre@1 131