You are here

function ad_remote_form_submit in Advertisement 5.2

Same name and namespace in other branches
  1. 5 remote/ad_remote.module \ad_remote_form_submit()
  2. 6.3 remote/ad_remote.module \ad_remote_form_submit()
  3. 6 remote/ad_remote.module \ad_remote_form_submit()
  4. 6.2 remote/ad_remote.module \ad_remote_form_submit()
  5. 7 remote/ad_remote.module \ad_remote_form_submit()

Prepare quantity and group.

File

remote/ad_remote.module, line 75
Enhances the ad module to providing cut-and-paste source snippets allowing ads to be easily displayed on remote websites. This module is a proof of concept.

Code

function ad_remote_form_submit($form_id, $form_values) {
  global $user;
  if (isset($form_values['quantity'])) {

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

    // must display at least one advertisement
    $quantity = 1;
  }
  $group = NULL;
  if (is_array($form_values['group']) && !empty($form_values['group'])) {
    if ($form_values['group'][0] == 0) {
      unset($form_values['group'][0]);
    }
    $group = implode(',', $form_values['group']);

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

  // display instructions on how to use this snippet
  $output .= '<div class="instructions">';
  $output .= 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.');
  $output .= '</div>';

  // the hostid makes it possible to track multiple remote users displaying ads
  $hostid = ad_permission_create_hostid($user->uid);

  // build a snippet to display on the remote web page
  $output .= '<blockquote><div class="' . t('snippet') . '"><em>&lt;!--' . t('start') . '--&gt;<br />';

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

  // break up the inner script so the browser doesn't parse it
  $url = str_replace(array(
    '<script',
    '</script>',
    'u=ad_remote',
  ), array(
    '"<scr" + "ipt',
    '"+"</scr" + "ipt>"',
    'u="+adurl+"',
  ), $url);
  $wrapper .= "document.write({$url})\n</script>\n";
  $output .= htmlentities($wrapper);
  $output .= '<br />&lt;!--' . t('end') . '--&gt;</em></div></blockquote>';
  $output .= '<div>' . l(t('Generate a new snippet.'), 'ad_remote') . '</div>';
  print theme('page', $output);
}