You are here

ad_remote.module in Advertisement 6.2

Enhances the ad module to providing cut-and-paste source snippets allowing ads to be easily displayed on remote websites.

Copyright (c) 2005-2009. 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.
*
* Copyright (c) 2005-2009.
*   Jeremy Andrews <jeremy@tag1consulting.com>.
*/

/**
 * Implementation of hook_perm().
 */
function ad_remote_perm() {
  return array(
    'host remote advertisements',
  );
}

/**
 * Implementation of hook_menu().
 */
function ad_remote_menu() {
  $items = array();
  $items['admin/content/ad/ad_remote'] = array(
    'title' => 'Remote ads',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ad_remote_form',
    ),
    'access arguments' => array(
      'host remote advertisements',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 1,
  );
  return $items;
}

/**
 * A simple page providing source snippets for displaying ads on remote
 * websites. When form is being submitted, it rebuilds with needed code snippet.
 */
function ad_remote_form($form_state) {
  global $user;
  $form['settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Settings'),
    '#description' => t('Use the following options to build a source snippet for displaying ads on your website.'),
    '#collapsible' => TRUE,
    '#collapsed' => isset($form_state['values']['group']),
    '#weight' => -1,
  );
  $form['settings']['group'] = taxonomy_form(_ad_get_vid(), 0, t('Select one or more groups to display ads from.'));
  $form['settings']['group']['#default_value'] = isset($form_state['values']['group']) ? $form_state['values']['group'] : '';
  if (isset($form_state['values']['quantity'])) {

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

    // must display at least one advertisement
    $quantity = 1;
  }
  $form['settings']['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' => $quantity,
    '#description' => t('Select the maximum number of unique ads that should be displayed together.'),
  );
  if (isset($form_state['values']['group'])) {
    $form['code'] = array(
      '#type' => 'fieldset',
      '#title' => t('Code snippet'),
      '#description' => 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.'),
    );

    // the hostid allows the tracking of multiple remote sites displaying ads
    $hostid = ad_owners_create_hostid($user->uid);
    $group = NULL;
    if (is_array($form_state['values']['group']) && !empty($form_state['values']['group'])) {
      if (isset($form_state['values']['group'][0]) && $form_state['values']['group'][0] == 0) {
        unset($form_state['values']['group'][0]);
      }
      $group = implode(',', $form_state['values']['group']);

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

    // build a snippet to display on the remote web page
    $output = '<!--' . t('start') . '-->';

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

    // break up the inner script so the browser doesn't parse it
    $url = str_replace(array(
      '<script',
      '</script>',
      'u=admin/content/ad/ad_remote',
    ), array(
      '"<scr" + "ipt',
      '"+"</scr" + "ipt>"',
      'u="+adurl+"',
    ), $url);
    $output .= "document.write({$url})\n</script>\n";
    $output .= '<!--' . t('end') . '-->';
    $form['code']['snippet'] = array(
      '#type' => 'textarea',
      '#value' => $output,
      '#attributes' => array(
        'onclick' => 'this.select();',
        'onfocus' => 'this.select();',
      ),
    );
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Generate code snippet'),
  );
  return $form;
}

/**
 * Form validator.
 */
function ad_remote_form_validate($form, &$form_state) {
  if (empty($form_state['values']['group'])) {
    form_set_error('group', t('At least one group should be selected'));
  }
}

/**
 * Tell the form to rebuild.
 */
function ad_remote_form_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
}

Functions

Namesort descending Description
ad_remote_form A simple page providing source snippets for displaying ads on remote websites. When form is being submitted, it rebuilds with needed code snippet.
ad_remote_form_submit Tell the form to rebuild.
ad_remote_form_validate Form validator.
ad_remote_menu Implementation of hook_menu().
ad_remote_perm Implementation of hook_perm().