You are here

function ad_remote_form in Advertisement 6

Same name and namespace in other branches
  1. 5.2 remote/ad_remote.module \ad_remote_form()
  2. 5 remote/ad_remote.module \ad_remote_form()
  3. 6.3 remote/ad_remote.module \ad_remote_form()
  4. 6.2 remote/ad_remote.module \ad_remote_form()
  5. 7 remote/ad_remote.module \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.

1 string reference to 'ad_remote_form'
ad_remote_menu in remote/ad_remote.module
Implementation of hook_menu().

File

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

Code

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;
}