Mercurial > defr > drupal > ad
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/weight/probability/ad_weight_probability.module Thu Apr 02 15:28:21 2009 +0000 @@ -0,0 +1,131 @@ +<?php +// $Id: ad_weight_probability.module,v 1.1.4.5 2009/03/27 19:11:50 jeremy Exp $ + +/** + * @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); + if (isset($ad->adtype)) { + return _ad_weight_probability_node_save($node, $op); + } + } + 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'), + ); +} +