You are here

adsense_oldcode.module in Google AdSense integration 6

Displays Google AdSense ads on Drupal pages.

This is the core module of the AdSense package, with the Drupal hooks and other administrative functions.

File

old/oldcode/adsense_oldcode.module
View source
<?php

/**
 * @file
 * Displays Google AdSense ads on Drupal pages.
 *
 * This is the core module of the AdSense package, with the Drupal hooks
 * and other administrative functions.
 */
define('ADSENSE_MAX_GROUPS', 5);
define('ADSENSE_AD_TYPE_DEFAULT', 0);
define('ADSENSE_ALT_DEFAULT', 0);
define('ADSENSE_ALT_INFO_DEFAULT', '');
define('ADSENSE_COLOR_BG_DEFAULT', '#FFFFFF');
define('ADSENSE_COLOR_BORDER_DEFAULT', '#336699');
define('ADSENSE_COLOR_LINK_DEFAULT', '#0000FF');
define('ADSENSE_COLOR_TEXT_DEFAULT', '#000000');
define('ADSENSE_COLOR_URL_DEFAULT', '#008000');
define('ADSENSE_GROUP_TITLE_DEFAULT', '');
define('ADSENSE_OLDCODE_AD_BLOCK_DEFAULT', '');
define('ADSENSE_OLDCODE_NUMBER_BLOCKS_DEFAULT', 3);
define('ADSENSE_UI_FEATURES_DEFAULT', 'rc:0');

/**
 * Implementation of hook_menu().
 */
function adsense_oldcode_menu() {
  $items = array();
  $items['admin/settings/adsense/oldcode'] = array(
    'title' => 'Old Code Ads',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'adsense_oldcode_settings',
    ),
    'access arguments' => array(
      'administer adsense',
    ),
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
    'file' => 'adsense_oldcode.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_block().
 */
function adsense_oldcode_block($op = 'list', $delta = 0, $edit = array()) {
  $block = NULL;
  switch ($op) {
    case 'list':
      $max = variable_get('adsense_oldcode_number_blocks', ADSENSE_OLDCODE_NUMBER_BLOCKS_DEFAULT);
      for ($count = 0; $count < $max; $count++) {
        if ($ad = _adsense_oldcode_get_block_config($count)) {
          $title = $ad[0];
        }
        else {
          $title = t('AdSense old code: unconfigured !d', array(
            '!d' => $count + 1,
          ));
        }
        $block[$count]['info'] = $title;
        $block[$count]['cache'] = BLOCK_NO_CACHE;
      }
      break;
    case 'configure':
      $ad = _adsense_oldcode_get_block_config($delta);
      foreach (adsense_ad_formats() as $format => $data) {
        $ad_list[$format] = $format . ' : ' . $data['desc'];
      }
      for ($group = 1; $group <= ADSENSE_MAX_GROUPS; $group++) {
        $group_list[$group] = $group . ' : ' . variable_get('adsense_group_title_' . $group, ADSENSE_GROUP_TITLE_DEFAULT);
      }
      $channel_list[''] = t('None');
      for ($channel = 1; $channel <= ADSENSE_MAX_CHANNELS; $channel++) {
        $channel_list[$channel] = $channel . ' : ' . variable_get('adsense_ad_channel_' . $channel, ADSENSE_AD_CHANNEL_DEFAULT);
      }
      $form['info'] = array(
        '#type' => 'textfield',
        '#title' => t('Block description'),
        '#default_value' => $ad ? $ad[0] : '',
        '#maxlength' => 64,
        '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array(
          '@overview' => url('admin/build/block'),
        )),
        '#required' => TRUE,
        '#weight' => -19,
      );
      $form['ad_format'] = array(
        '#type' => 'select',
        '#title' => t('Ad format'),
        '#default_value' => $ad ? $ad[1] : '250x250',
        '#options' => $ad_list,
        '#description' => t('Select the ad dimensions you want for this block.'),
      );
      $form['ad_group'] = array(
        '#type' => 'select',
        '#title' => t('Group'),
        '#default_value' => $ad ? $ad[2] : 1,
        '#options' => $group_list,
      );
      $form['ad_channel'] = array(
        '#type' => 'select',
        '#title' => t('Channel'),
        '#default_value' => $ad ? $ad[3] : 1,
        '#options' => $channel_list,
      );
      $form['ad_align'] = array(
        '#type' => 'select',
        '#title' => t('Ad alignment'),
        '#default_value' => $ad ? $ad[4] : 'center',
        '#options' => array(
          '' => t('None'),
          'left' => t('Left'),
          'center' => t('Centered'),
          'right' => t('Right'),
        ),
        '#description' => t('Select the horizontal alignment of the ad within the block.'),
      );
      return $form;
    case 'save':
      $data = implode(':', array(
        urlencode($edit['info']),
        $edit['ad_format'],
        $edit['ad_group'],
        $edit['ad_channel'],
        $edit['ad_align'],
      ));
      variable_set('adsense_oldcode_ad_block_' . $delta, $data);
      return;
    case 'view':
      if (_adsense_page_match()) {
        $ad = _adsense_oldcode_get_block_config($delta);
        $block['content'] = $ad ? adsense_display(array(
          'title' => $ad[0],
          'format' => $ad[1],
          'group' => $ad[2],
          'channel' => $ad[3],
        )) : t('AdSense unconfigured block. <a href=!url>Click to configure.</a>', array(
          '!url' => url('admin/build/block/configure/adsense_oldcode/' . $delta),
        ));
        if (!empty($ad[4])) {
          $block['content'] = "<div style='text-align:{$ad[4]}'>{$block['content']}</div>";
        }
      }
      break;
  }
  return $block;
}

/**
 * Configuration of the provided block.
 *
 * @param int $delta
 *   Block number.
 *
 * @return array
 *   array with the block configuration or FALSE if no such block was found
 */
function _adsense_oldcode_get_block_config($delta = 0) {
  if ($data = variable_get('adsense_oldcode_ad_block_' . $delta, ADSENSE_OLDCODE_AD_BLOCK_DEFAULT)) {
    $ad = explode(':', $data);
    $ad[0] = urldecode($ad[0]);
    return $ad;
  }
  return FALSE;
}

/**
 * Generates the AdSense ad.
 *
 * @param string $format
 *   Format of the ad.
 * @param int $group
 *   (optional) Group ID of the configured color attributes group.
 * @param int $channel
 *   (optional) Channel ID of the configured Ad Channel.
 *
 * @return string
 *   JavaScript that embeds the Google AdSense ad
 */
function _adsense_oldcode_get_ad($format, $client, $group = 1, $channel = 1) {
  $ad = adsense_ad_formats($format);
  if ($ad === NULL) {
    $output = "";
  }
  elseif (variable_get('adsense_test_mode', ADSENSE_TEST_MODE_DEFAULT)) {
    $output = theme('adsense_placeholder', "client = {$client}<br />format = {$format}<br />width = {$ad['width']}<br />height = {$ad['height']}", $ad['width'], $ad['height']);
  }
  else {
    if ($group < 1 || $group > ADSENSE_MAX_GROUPS) {

      // Default to 1 if an invalid group is supplied.
      $group = 1;
    }
    switch (variable_get('adsense_ad_type_' . $group, ADSENSE_AD_TYPE_DEFAULT)) {
      case 2:
        $type = 'text_image';
        break;
      case 1:
        $type = 'image';
        break;
      default:
        $type = 'text';
        break;
    }
    $channel = variable_get('adsense_ad_channel_' . $channel, ADSENSE_AD_CHANNEL_DEFAULT);
    $border = substr(variable_get('adsense_color_border_' . $group, ADSENSE_COLOR_BORDER_DEFAULT), 1);
    $bg = substr(variable_get('adsense_color_bg_' . $group, ADSENSE_COLOR_BG_DEFAULT), 1);
    $link = substr(variable_get('adsense_color_link_' . $group, ADSENSE_COLOR_LINK_DEFAULT), 1);
    $url = substr(variable_get('adsense_color_url_' . $group, ADSENSE_COLOR_URL_DEFAULT), 1);
    $text = substr(variable_get('adsense_color_text_' . $group, ADSENSE_COLOR_TEXT_DEFAULT), 1);
    $corner = variable_get('adsense_ui_features_' . $group, ADSENSE_UI_FEATURES_DEFAULT);
    $alt = variable_get('adsense_alt_' . $group, ADSENSE_ALT_DEFAULT);
    $alt_info = variable_get('adsense_alt_info_' . $group, ADSENSE_ALT_INFO_DEFAULT);
    switch ($alt) {
      case 1:
        $part1 = "google_alternate_ad_url = \"{$alt_info}\";";
        break;
      case 2:
        $part1 = "google_alternate_color = \"{$alt_info}\";";
        break;
      case 0:

      // disabled.
      default:
        $part1 = "";
    }
    $part2 = "";
    if ($ad['type'] == ADSENSE_TYPE_AD) {
      $part2 .= "google_ad_type = \"{$type}\";\n";
    }
    if (!empty($channel)) {
      $part2 .= "google_ad_channel = \"{$channel}\";";
    }
    $secret = '';
    if ($lang = variable_get('adsense_secret_language', ADSENSE_SECRET_LANGUAGE_DEFAULT)) {
      $secret = "google_language = '{$lang}';\n";
    }
    $output = <<<OLDCODE_TXT
<script type="text/javascript"><!--
google_ad_client = "{<span class="php-variable">$client</span>}";
{<span class="php-variable">$part1</span>}
google_ad_width = {<span class="php-variable">$ad</span>[<span class="php-string">'width'</span>]};
google_ad_height = {<span class="php-variable">$ad</span>[<span class="php-string">'height'</span>]};
google_ad_format = "{<span class="php-variable">$ad</span>[<span class="php-string">'code'</span>]}";
{<span class="php-variable">$part2</span>}
google_color_border = "{<span class="php-variable">$border</span>}";
google_color_bg = "{<span class="php-variable">$bg</span>}";
google_color_link = "{<span class="php-variable">$link</span>}";
google_color_text = "{<span class="php-variable">$text</span>}";
google_color_url = "{<span class="php-variable">$url</span>}";
google_ui_features = "{<span class="php-variable">$corner</span>}";
{<span class="php-variable">$secret</span>}
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
OLDCODE_TXT;
  }
  return $output;
}