You are here

adsense_cse.module in Google AdSense integration 6

Same filename and directory in other branches
  1. 5.3 cse/adsense_cse.module
  2. 7 cse/adsense_cse.module

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

cse/adsense_cse.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_CSE_RESULTS_PATH', 'adsense/cse');
define('ADSENSE_CSE_AD_BLOCK_DEFAULT', '');
define('ADSENSE_CSE_AD_LOCATION_DEFAULT', 'adsense_cse_loc_top_right');
define('ADSENSE_CSE_COLOR_BOX_BACKGROUND_DEFAULT', 'FFFFFF');
define('ADSENSE_CSE_COUNTRY_DEFAULT', 'www.google.com');
define('ADSENSE_CSE_ENCODING_DEFAULT', 'UTF-8');
define('ADSENSE_CSE_FRAME_WIDTH_DEFAULT', 800);
define('ADSENSE_CSE_LOGO_DEFAULT', 'adsense_cse_branding_right');
define('ADSENSE_CSE_NUMBER_BLOCKS_DEFAULT', 2);
define('ADSENSE_CSE_TEXTBOX_LENGTH_DEFAULT', 31);
define('ADSENSE_CSE_LANGUAGE_DEFAULT', 'en');

/**
 * Implementation of hook_menu().
 */
function adsense_cse_menu() {
  $items = array();
  $results_path = variable_get('clean_url', 0) ? ADSENSE_CSE_RESULTS_PATH : '.';
  $items['admin/settings/adsense/cse'] = array(
    'title' => 'Custom Search',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'adsense_cse_settings',
    ),
    'access arguments' => array(
      'administer adsense',
    ),
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
    'file' => 'adsense_cse.admin.inc',
  );
  $items[$results_path] = array(
    'title' => 'Search Results',
    'page callback' => '_adsense_cse_results',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
    'file' => 'adsense_cse.results.inc',
  );
  return $items;
}

/**
 * Implementation of hook_block().
 */
function adsense_cse_block($op = 'list', $delta = 0, $edit = array()) {
  $block = NULL;
  switch ($op) {
    case 'list':
      $max = variable_get('adsense_cse_number_blocks', ADSENSE_CSE_NUMBER_BLOCKS_DEFAULT);
      for ($count = 0; $count < $max; $count++) {
        if ($ad = _adsense_cse_get_block_config($count)) {
          $title = $ad[0];
        }
        else {
          $title = t('AdSense CSE: unconfigured !d', array(
            '!d' => $count + 1,
          ));
        }
        $block[$count]['info'] = $title;
        $block[$count]['cache'] = BLOCK_NO_CACHE;
      }
      break;
    case 'configure':
      $ad = _adsense_cse_get_block_config($delta);
      $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_slot'] = array(
        '#type' => 'textfield',
        '#title' => t('Ad Slot ID'),
        '#default_value' => $ad ? $ad[1] : '',
        '#description' => t('This is the provided by the AdSense site in the Search Box Code "cx" field. This is usually provided in the form partner-<em>Publisher ID</em>:<em>Slot Id</em>. If the code provided is, for example, partner-pub-0123456789:<strong>abcdefghij</strong>, then insert only <strong>abcdefghij</strong> here.'),
        '#required' => TRUE,
      );
      return $form;
    case 'save':
      $data = implode(':', array(
        urlencode($edit['info']),
        $edit['ad_slot'],
      ));
      variable_set('adsense_cse_ad_block_' . $delta, $data);
      return;
    case 'view':
      if (_adsense_page_match()) {
        $ad = _adsense_cse_get_block_config($delta);
        $block['content'] = $ad ? adsense_display(array(
          'title' => $ad[0],
          'format' => 'Search Box',
          'slot' => $ad[1],
        )) : t('AdSense unconfigured block. <a href=!url>Click to configure.</a>', array(
          '!url' => url('admin/build/block/configure/adsense_cse/' . $delta),
        ));
      }
      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_cse_get_block_config($delta = 0) {
  if ($data = variable_get('adsense_cse_ad_block_' . $delta, ADSENSE_CSE_AD_BLOCK_DEFAULT)) {
    $ad = explode(':', $data);
    $ad[0] = urldecode($ad[0]);
    return $ad;
  }
  return FALSE;
}

/**
 * Generates the CSE search box.
 *
 * @param string $client
 *   Publisher ID.
 * @param int $slot
 *   Slot Id for the AdSense for Search.
 *
 * @return string
 *   HTML with the search input form
 */
function _adsense_cse_get_searchbox($client, $slot = NULL) {
  global $base_url;
  $branding = variable_get('adsense_cse_logo', ADSENSE_CSE_LOGO_DEFAULT);
  $box_background_color = variable_get('adsense_cse_color_box_background', ADSENSE_CSE_COLOR_BOX_BACKGROUND_DEFAULT);
  $ad_location = variable_get('adsense_cse_ad_location', ADSENSE_CSE_AD_LOCATION_DEFAULT);
  $encoding = variable_get('adsense_cse_encoding', ADSENSE_CSE_ENCODING_DEFAULT);
  $textbox_length = variable_get('adsense_cse_textbox_length', ADSENSE_CSE_TEXTBOX_LENGTH_DEFAULT);
  $language = variable_get('adsense_cse_language', ADSENSE_CSE_LANGUAGE_DEFAULT);
  $search = t('Search');
  $custom_search = t('Custom Search');
  if (variable_get('clean_url', 0)) {
    $results_path = url(ADSENSE_CSE_RESULTS_PATH, array(
      'absolute' => TRUE,
    ));
    $hidden_q_field = '';
  }
  else {
    $results_path = $base_url;
    $hidden_q_field = '<input type="hidden" name="q" value="." />';
  }
  if ($box_background_color == '000000') {
    $box_text_color = 'FFFFFF';
  }
  else {
    $box_text_color = '000000';
  }
  switch ($ad_location) {
    default:
    case 'adsense_cse_loc_top_right':
      $forid = 10;
      break;
    case 'adsense_cse_loc_top_bottom':
      $forid = 11;
      break;
    case 'adsense_cse_loc_right':
      $forid = 9;
      break;
  }
  if ($branding == 'adsense_cse_branding_watermark') {

    // Since we use as_q, we must use a modified copy of Google's Javascript.
    $script = $base_url . '/' . drupal_get_path('module', 'adsense_cse') . '/adsense_cse.js';

    // When using a watermark, code is not reusable due to indentation.
    $output = <<<CSE_TXT1
<script type="text/javascript"><!--
drupal_adsense_cse_lang = '{<span class="php-variable">$language</span>}';
//-->
</script>
<form action="{<span class="php-variable">$results_path</span>}" id="cse-search-box">
  <div>{<span class="php-variable">$hidden_q_field</span>}
    <input type="hidden" name="cx" value="partner-{<span class="php-variable">$client</span>}:{<span class="php-variable">$slot</span>}" />
    <input type="hidden" name="cof" value="FORID:{<span class="php-variable">$forid</span>}" />
    <input type="hidden" name="ie" value="{<span class="php-variable">$encoding</span>}" />
    <input type="text" name="as_q" size="{<span class="php-variable">$textbox_length</span>}" />
    <input type="submit" name="sa" value="{<span class="php-variable">$search</span>}" />
  </div>
</form>
<script type="text/javascript" src="{<span class="php-variable">$script</span>}"></script>
CSE_TXT1;

    /*<script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&amp;lang=en"></script>*/
  }
  else {
    if ($branding == 'adsense_cse_branding_right') {
      $class = 'cse-branding-right';
    }
    else {
      $class = 'cse-branding-bottom';
    }
    $output = <<<CSE_TXT2
<style type="text/css">
@import url(http://www.google.com/cse/api/branding.css);
</style>
<div class="{<span class="php-variable">$class</span>}" style="background-color:#{<span class="php-variable">$box_background_color</span>};color:#{<span class="php-variable">$box_text_color</span>}">
  <div class="cse-branding-form">
    <form action="{<span class="php-variable">$results_path</span>}" id="cse-search-box">
      <div>{<span class="php-variable">$hidden_q_field</span>}
        <input type="hidden" name="cx" value="partner-{<span class="php-variable">$client</span>}:{<span class="php-variable">$slot</span>}" />
        <input type="hidden" name="cof" value="FORID:{<span class="php-variable">$forid</span>}" />
        <input type="hidden" name="ie" value="{<span class="php-variable">$encoding</span>}" />
        <input type="text" name="as_q" size="{<span class="php-variable">$textbox_length</span>}" />
        <input type="submit" name="sa" value="{<span class="php-variable">$search</span>}" />
      </div>
    </form>
  </div>
  <div class="cse-branding-logo">
    <img src="http://www.google.com/images/poweredby_transparent/poweredby_{<span class="php-variable">$box_background_color</span>}.gif" alt="Google" />
  </div>
  <div class="cse-branding-text">
    {<span class="php-variable">$custom_search</span>}
  </div>
</div>
CSE_TXT2;
  }
  return $output;
}