You are here

function ad_remote_form in Advertisement 6.3

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