You are here

dfp.adtest.inc in Doubleclick for Publishers (DFP) 7

Same filename and directory in other branches
  1. 7.2 dfp.adtest.inc

Creates a test page for DFP ads.

File

dfp.adtest.inc
View source
<?php

/**
 * @file
 * Creates a test page for DFP ads.
 */

/**
 * Page callback for the DFP test page. It displays all tags on the site.
 */
function dfp_adtest_page() {
  $output = array();
  $form = drupal_get_form('dfp_adtest_form');
  $output[] = array(
    '#markup' => render($form),
    '#attached' => array(
      'css' => array(
        'dfp-admin' => drupal_get_path('module', 'dfp') . '/dfp.admin.css',
      ),
    ),
  );
  $tags = dfp_tag_load_all();
  foreach ($tags as $tag) {
    $tag_slot = array(
      'tag slot name' => array(
        '#markup' => '<h3>' . check_plain($tag->slot) . '</h3>',
      ),
    );
    $tag_tag = dfp_tag($tag->machinename);
    $tag_devel = array();
    if (module_exists('devel')) {
      $tag_devel['object'] = array(
        '#markup' => kpr($tag, TRUE),
      );
    }
    $output[] = array(
      $tag_slot,
      $tag_tag,
      $tag_devel,
    );
  }
  return $output;
}

/**
 * Build a form for display at the top of the adtest page allowing users to set
 * the adunit pattern for all dfp tags on that page.
 */
function dfp_adtest_form($form, &$form_state) {
  form_load_include($form_state, 'inc', 'dfp', 'dfp.adtest');
  $form['#method'] = 'get';
  $form['adunit_override'] = array(
    '#type' => 'textfield',
    '#title' => t('Ad Unit Override'),
    '#default_value' => isset($_GET['adunit_override']) ? $_GET['adunit_override'] : '',
    '#description' => t('Override the Ad Unit value for all the ad tags below. Use the tokens below to define how the ad unit should display. Example: [dfp_tag:network_id]/test/[dfp_tag:slot]'),
  );
  $form['tokens'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array(
      'dfp_tag',
      'node',
      'term',
      'user',
    ),
    '#global_types' => TRUE,
    '#click_insert' => TRUE,
    '#dialog' => TRUE,
    '#weight' => 100,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Override'),
  );
  return $form;
}

/**
 * Alter tags for various adtest scenaroios.
 */
function dfp_adtest_alter_tag(&$tag) {

  // Check for a submit on adtest form.
  if (isset($_GET['adunit_override'])) {

    // For security reasons, ensure that adunit_override contains only letters,
    // numbers, hyphens, dashes, periods, hard brackets and colons. Typically
    // only a site builder or an ad ops person will be using the adtest page so
    // if there is a problem with the adunit override value we can safely
    // display a somewhat technical message.
    if (!preg_match('@[^a-zA-Z0-9\\-_\\.\\[\\]\\:]+@', $_GET['adunit_override'])) {
      $tag->adunit = $_GET['adunit_override'];
    }
    else {
      drupal_set_message(t('The adunit_override query string can only contain letters, numbers, hyphens, dashes, periods, hard brackets and colons. Please doublecheck the override string you were provided.'), 'error');
    }
  }

  // Check for an "adtest" query string variable.
  if (isset($_GET['adtest'])) {
    $tag->adunit = variable_get('dfp_adtest_adunit', '');
  }
}

Functions

Namesort descending Description
dfp_adtest_alter_tag Alter tags for various adtest scenaroios.
dfp_adtest_form Build a form for display at the top of the adtest page allowing users to set the adunit pattern for all dfp tags on that page.
dfp_adtest_page Page callback for the DFP test page. It displays all tags on the site.