You are here

ad_remote.module in Advertisement 6

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 your web page to display ads hosted on this web site.  Include the entire snippet, and do not modify it in any way.'),
    );
    $hostid = ad_host_id_create($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;
    }
    $output = '<!--' . t('start') . '-->' . ad($group, $quantity, array(
      'raw' => 1,
      'hostid' => $hostid,
    )) . '<!--' . 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().