You are here

ad_remote.module in Advertisement 5.2

Enhances the ad module to providing cut-and-paste source snippets allowing ads to be easily displayed on remote websites. This module is a proof of concept.

Copyright (c) 2007-2008. Jeremy Andrews <jeremy@tag1consulting.com>.

File

remote/ad_remote.module
View source
<?php

/**
* @file
* Enhances the ad module to providing cut-and-paste source snippets allowing 
* ads to be easily displayed on remote websites.  This module is a proof of 
* concept.
*
* Copyright (c) 2007-2008.
*   Jeremy Andrews <jeremy@tag1consulting.com>.
*/

/**
 * Drupal _perm hook.
 */
function ad_remote_perm() {
  return array(
    'host remote advertisements',
  );
}

/**
 * Drupal _menu hook.
 */
function ad_remote_menu($may_cache) {
  global $user;
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => "ad_remote",
      'title' => 'Remote ads',
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'ad_remote_form',
      ),
      'access' => user_access('host remote advertisements', $user),
    );
  }
  return $items;
}

/**
 * A simple page providing source snippets for displaying ads on remote 
 * websites.  
 */
function ad_remote_form() {
  global $user;
  $form = array();
  $form['overview'] = array(
    '#type' => 'markup',
    '#value' => t('Use the following options to build a source snippet for displaying ads on your website.'),
    '#weight' => -16,
  );
  $form['group'] = taxonomy_form(_ad_get_vid(), 0, t('Select one or more groups to display ads from.'));
  $form['quantity'] = array(
    '#type' => 'select',
    '#title' => t('Quantity'),
    '#options' => drupal_map_assoc(array(
      1,
      2,
      3,
      4,
      5,
      6,
      7,
      8,
      9,
      10,
      15,
      20,
      25,
      50,
    )),
    '#default_value' => isset($_POST['quantity']) ? $_POST['quantity'] : 1,
    '#description' => t('Select the maximum number of unique ads that should be displayed together.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Generate code snippet'),
  );
  return $form;
}

/**
 * Prepare quantity and group.
 */
function ad_remote_form_submit($form_id, $form_values) {
  global $user;
  if (isset($form_values['quantity'])) {

    // sanity check, be sure quantity is an integer
    $quantity = (int) $form_values['quantity'];
  }
  if (!$quantity) {

    // must display at least one advertisement
    $quantity = 1;
  }
  $group = NULL;
  if (is_array($form_values['group']) && !empty($form_values['group'])) {
    if ($form_values['group'][0] == 0) {
      unset($form_values['group'][0]);
    }
    $group = implode(',', $form_values['group']);

    // sanity check, be sure group is only numbers and commas
    $group = preg_replace('/[^0-9,]/', '', $group);
  }
  if (!$group) {
    $group = 0;
  }
  $output = '<div class="ad_remote">';

  // display instructions on how to use this snippet
  $output .= '<div class="instructions">';
  $output .= t('Insert the following source snippet into the source code of your remote web page.  The remote website will then display advertisements from this website.  It is necessary to include the entire snippet, and to not modify it in any way.');
  $output .= '</div>';

  // the hostid makes it possible to track multiple remote users displaying ads
  $hostid = ad_permission_create_hostid($user->uid);

  // build a snippet to display on the remote web page
  $output .= '<blockquote><div class="' . t('snippet') . '"><em>&lt;!--' . t('start') . '--&gt;<br />';

  // build a wrapper script, which collects the url the ad is displayed on
  $wrapper = "<script language='JavaScript' type='text/javascript'>\n var adurl = window.location.href;\n";
  $url = ad($group, $quantity, array(
    'hostid' => $hostid,
    'div' => FALSE,
  ));

  // break up the inner script so the browser doesn't parse it
  $url = str_replace(array(
    '<script',
    '</script>',
    'u=ad_remote',
  ), array(
    '"<scr" + "ipt',
    '"+"</scr" + "ipt>"',
    'u="+adurl+"',
  ), $url);
  $wrapper .= "document.write({$url})\n</script>\n";
  $output .= htmlentities($wrapper);
  $output .= '<br />&lt;!--' . t('end') . '--&gt;</em></div></blockquote>';
  $output .= '<div>' . l(t('Generate a new snippet.'), 'ad_remote') . '</div>';
  print theme('page', $output);
}

Functions

Namesort descending Description
ad_remote_form A simple page providing source snippets for displaying ads on remote websites.
ad_remote_form_submit Prepare quantity and group.
ad_remote_menu Drupal _menu hook.
ad_remote_perm Drupal _perm hook.