You are here

google_admanager.module in DFP Small Business (Google Ad Manager) 5

File

google_admanager.module
View source
<?php

/**
 * Google Admanager
 * https://www.google.com/admanager/
 */

/**
 * Implementing hook_block
 */
function google_admanager_block($op = 'list', $delta = 0, $edit = array()) {
  $id = variable_get('google_admanager_account', '');
  if (!empty($id)) {
    $ad_slots = variable_get('google_admanager_ad_slots', '');
    $ad_slots = explode("\n", str_replace(array(
      "\r",
      "\t",
      "\0",
      "\v",
      " ",
    ), '', $ad_slots));
    if ($op == 'list') {
      foreach ($ad_slots as $ad_slot) {
        $blocks[] = array(
          'info' => 'Ad Slot: ' . $ad_slot,
        );
      }
      return $blocks;
    }
    else {
      if ($op == 'view') {
        if ($ad_slot = $ad_slots[$delta]) {
          $block = array(
            'subject' => '',
            'content' => theme('google_admanager_block', $id, $ad_slot),
          );
        }
        return $block;
      }
    }
  }
}

/**
 * Implementing hook_perm
 */
function google_admanager_perm() {
  return array(
    'administer google admanager',
  );
}

/**
 * Implementing hook_menu
 */
function google_admanager_menu($may_cache) {
  $items = array();
  if (!$maycache) {
    $items[] = array(
      'path' => 'admin/settings/google_admanager',
      'title' => t('Google Admanager'),
      'description' => t('Configure the settings used to generate the Google Admanager Slot Ad code.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'google_admanager_admin_settings_form',
      'access' => user_access('administer google admanager'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Implementation of hook_admin_settings() for configuring the module
 */
function google_admanager_admin_settings_form() {
  $form['google_admanager_account'] = array(
    '#type' => 'textfield',
    '#title' => t('Google Admanager account number'),
    '#default_value' => variable_get('google_admanager_account', 'ca-pub-'),
    '#size' => 30,
    '#maxlength' => 40,
    '#required' => TRUE,
    '#description' => t('You can obtain a user account under the <strong>Admin</strong> tab on the <a href="@url">Google Admanager</a> website.', array(
      '@url' => 'https://www.google.com/admanager/',
    )),
  );
  $form['google_admanager_ad_slots'] = array(
    '#type' => 'textarea',
    '#title' => t('Ad slots'),
    '#default_value' => variable_get('google_admanager_ad_slots', ''),
    '#description' => t('Enter one Ad Slot name per line.'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_admin_settings_form_validate()
 */
function google_admanager_admin_settings_form_validate($form_id, $form_values) {
  if (!preg_match('/^ca-pub-\\d+$/', $form_values['google_admanager_account'])) {
    form_set_error('google_admanager_account', t('A valid Google Admanager account number is case sensitive and formatted like ca-pub-xxxxxxxxxxxxxxx.'));
  }
}

/**
 * Theme function the Ad Slot code
 * @param $id google admanager account id
 * @param $id google admanager slot name
 * @return google admanager slot script 
 */
function theme_google_admanager_block($id, $ad_slot) {
  google_admanager_add_js('GA_googleAddSlot("' . $id . '", "' . $ad_slot . '");');
  return '<script type="text/javascript">GA_googleFillSlot("' . $ad_slot . '");</script>';
}

/**
 * Stored the ad slots and when called with no slot, return the whole ad manager javascript
 * This function should be called from pianofiles_preprocess_page(&$vars) as followed
 *  if (module_exists('google_admanager')) {
 *    $vars['scripts'] .= google_admanager_add_js();
 *  }
 * @param $ad_slot string with the addSlot script
 * @return google admanager code 
 */
function google_admanager_add_js($ad_slot = NULL) {
  static $google_admanager_slots;
  if (isset($ad_slot)) {
    $google_admanager_slots .= $ad_slot;
  }
  else {
    if (isset($google_admanager_slots)) {
      $id = variable_get('google_admanager_account', '');

      // javascript which is supposed to come in the header
      $js = '<script type="text/javascript" src="http://partner.googleadservices.com/gampad/google_service.js"></script>' . "\n";
      $js .= '<script type="text/javascript">GS_googleAddAdSenseService("' . $id . '"); GS_googleEnableAllServices();</script>' . "\n";
      $js .= '<script type="text/javascript">' . $google_admanager_slots . '</script>' . "\n";
      $js .= '<script type="text/javascript">GA_googleFetchAds();</script>' . "\n";
      return $js;
    }
  }
}

/**
 * Implementation of hook_filter().
 *
 * Filter option to enable Google Admanager filter [google_ad:ad_slot]
 * @see google_admanager_nodeapi()
 */
function google_admanager_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(
        0 => t('Google Admanager filter'),
      );
    case 'description':
      return t('Substitutes [google_ad:ad_slot] tags with the Google Admanager script.');
    case 'prepare':
      return $text;
    case 'process':
      return _google_admanager_substitute_tags($text);
  }
}

/**
 * Implementation of hook_filter_tips().
 */
function google_admanager_filter_tips($delta, $format, $long = FALSE) {
  return t('You may use [google_ad:ad_slot] to display Google Admanager ads within your content.');
}

/**
 * Replace all Admanager tags with their corresponding files or images.
 *
 * @param object $text
 *   The text to process.
 *
 * @return string
 *   The processed content of the given text.
 */
function _google_admanager_substitute_tags($text) {
  if (preg_match_all("/\\[(google_ad):([^=\\]]+)=?([^\\]]*)?\\]/i", $text, $match)) {
    $id = variable_get('google_admanager_account', '');
    $s = $r = array();
    foreach ($match[2] as $key => $ad_slot) {
      $s[] = $match[0][$key];
      $r[] = theme('google_admanager_block', $id, $ad_slot);
    }

    // Perform the replacements and return processed field.
    return str_replace($s, $r, $text);
  }
  return $text;
}

Functions

Namesort descending Description
google_admanager_add_js Stored the ad slots and when called with no slot, return the whole ad manager javascript This function should be called from pianofiles_preprocess_page(&$vars) as followed if (module_exists('google_admanager'))…
google_admanager_admin_settings_form Implementation of hook_admin_settings() for configuring the module
google_admanager_admin_settings_form_validate Implementation of hook_admin_settings_form_validate()
google_admanager_block Implementing hook_block
google_admanager_filter Implementation of hook_filter().
google_admanager_filter_tips Implementation of hook_filter_tips().
google_admanager_menu Implementing hook_menu
google_admanager_perm Implementing hook_perm
theme_google_admanager_block Theme function the Ad Slot code
_google_admanager_substitute_tags Replace all Admanager tags with their corresponding files or images.