annotate remote/ad_remote.module @ 1:948362c2a207 ad

update advertisement
author pierre
date Thu, 02 Apr 2009 15:28:21 +0000
parents d8a3998dac8e
children 32c1a7d9e1fa
rev   line source
pierre@0 1 <?php
pierre@1 2 // $Id: ad_remote.module,v 1.1.4.5.2.5.2.5 2009/03/05 00:35:56 jeremy Exp $
pierre@0 3
pierre@0 4 /**
pierre@0 5 * @file
pierre@0 6 * Enhances the ad module to providing cut-and-paste source snippets allowing
pierre@0 7 * ads to be easily displayed on remote websites.
pierre@0 8 *
pierre@0 9 * Copyright (c) 2005-2009.
pierre@0 10 * Jeremy Andrews <jeremy@tag1consulting.com>.
pierre@0 11 */
pierre@0 12
pierre@0 13 /**
pierre@0 14 * Implementation of hook_perm().
pierre@0 15 */
pierre@0 16 function ad_remote_perm() {
pierre@0 17 return array('host remote advertisements');
pierre@0 18 }
pierre@0 19
pierre@0 20 /**
pierre@0 21 * Implementation of hook_menu().
pierre@0 22 */
pierre@0 23 function ad_remote_menu() {
pierre@0 24 $items = array();
pierre@0 25
pierre@0 26 $items['admin/content/ad/ad_remote'] = array(
pierre@0 27 'title' => 'Remote ads',
pierre@0 28 'page callback' => 'drupal_get_form',
pierre@0 29 'page arguments' => array('ad_remote_form'),
pierre@0 30 'access arguments' => array('host remote advertisements'),
pierre@0 31 'type' => MENU_LOCAL_TASK,
pierre@0 32 'weight' => 1,
pierre@0 33 );
pierre@0 34 return $items;
pierre@0 35 }
pierre@0 36
pierre@0 37 /**
pierre@1 38 * A simple page providing source snippets for displaying ads on remote
pierre@0 39 * websites. When form is being submitted, it rebuilds with needed code snippet.
pierre@0 40 */
pierre@0 41 function ad_remote_form($form_state) {
pierre@0 42 global $user;
pierre@0 43
pierre@0 44 $form['settings'] = array(
pierre@0 45 '#type' => 'fieldset',
pierre@0 46 '#title' => t('Settings'),
pierre@0 47 '#description' => t('Use the following options to build a source snippet for displaying ads on your website.'),
pierre@0 48 '#collapsible' => TRUE,
pierre@0 49 '#collapsed' => isset($form_state['values']['group']),
pierre@0 50 '#weight' => -1,
pierre@0 51 );
pierre@0 52
pierre@0 53 $form['settings']['group'] = taxonomy_form(_ad_get_vid(), 0, t('Select one or more groups to display ads from.'));
pierre@0 54 $form['settings']['group']['#default_value'] = isset($form_state['values']['group']) ? $form_state['values']['group'] : '';
pierre@0 55
pierre@0 56 if (isset($form_state['values']['quantity'])) {
pierre@1 57 // sanity check, be sure quantity is an integer
pierre@0 58 $quantity = (int)$form_state['values']['quantity'];
pierre@0 59 }
pierre@0 60 if (!isset($quantity)) {
pierre@1 61 // must display at least one advertisement
pierre@0 62 $quantity = 1;
pierre@0 63 }
pierre@0 64
pierre@0 65 $form['settings']['quantity'] = array(
pierre@0 66 '#type' => 'select',
pierre@0 67 '#title' => t('Quantity'),
pierre@0 68 '#options' => drupal_map_assoc(array(1,2,3,4,5,6,7,8,9,10,15,20,25,50)),
pierre@0 69 '#default_value' => $quantity,
pierre@0 70 '#description' => t('Select the maximum number of unique ads that should be displayed together.'),
pierre@0 71 );
pierre@1 72
pierre@0 73 if (isset($form_state['values']['group'])) {
pierre@0 74 $form['code'] = array(
pierre@0 75 '#type' => 'fieldset',
pierre@0 76 '#title' => t('Code snippet'),
pierre@1 77 '#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.'),
pierre@0 78 );
pierre@0 79
pierre@1 80 // the hostid allows the tracking of multiple remote sites displaying ads
pierre@1 81 $hostid = ad_owners_create_hostid($user->uid);
pierre@0 82 $group = NULL;
pierre@0 83 if (is_array($form_state['values']['group']) && !empty($form_state['values']['group'])) {
pierre@0 84 if (isset($form_state['values']['group'][0]) && $form_state['values']['group'][0] == 0) {
pierre@0 85 unset($form_state['values']['group'][0]);
pierre@0 86 }
pierre@0 87 $group = implode(',', $form_state['values']['group']);
pierre@1 88 // sanity check, be sure group is only numbers and commas
pierre@0 89 $group = preg_replace('/[^0-9,]/', '', $group);
pierre@0 90 }
pierre@0 91 if (!$group) {
pierre@0 92 $group = 0;
pierre@0 93 }
pierre@0 94
pierre@1 95 // build a snippet to display on the remote web page
pierre@1 96 $output = '<!--'. t('start') .'-->';
pierre@1 97 // build a wrapper script which collects the url the ad is displayed on
pierre@1 98 $output .= "\n<script language='JavaScript' type='text/javascript'>\n var adurl = window.location.href;\n";
pierre@1 99 $url = ad($group, $quantity, array('hostid' => $hostid, 'div' => FALSE));
pierre@1 100 // break up the inner script so the browser doesn't parse it
pierre@1 101 $url = str_replace(array('<script', '</script>', 'u=admin/content/ad/ad_remote'), array('"<scr" + "ipt', '"+"</scr" + "ipt>"', 'u="+adurl+"'), $url);
pierre@1 102 $output .= "document.write($url)\n</script>\n";
pierre@1 103 $output .= '<!--'. t('end') .'-->';
pierre@1 104
pierre@0 105 $form['code']['snippet'] = array(
pierre@0 106 '#type' => 'textarea',
pierre@0 107 '#value' => $output,
pierre@0 108 '#attributes' => array(
pierre@0 109 'onclick' => 'this.select();',
pierre@0 110 'onfocus' => 'this.select();',
pierre@0 111 ),
pierre@0 112 );
pierre@0 113 }
pierre@0 114
pierre@0 115 $form['submit'] = array(
pierre@0 116 '#type' => 'submit',
pierre@0 117 '#value' => t('Generate code snippet'),
pierre@0 118 );
pierre@0 119
pierre@0 120 return $form;
pierre@0 121 }
pierre@0 122
pierre@0 123 /**
pierre@0 124 * Form validator.
pierre@0 125 */
pierre@0 126 function ad_remote_form_validate($form, &$form_state) {
pierre@0 127 if (empty($form_state['values']['group'])) {
pierre@0 128 form_set_error('group', t('At least one group should be selected'));
pierre@0 129 }
pierre@0 130 }
pierre@0 131 /**
pierre@0 132 * Tell the form to rebuild.
pierre@0 133 */
pierre@0 134 function ad_remote_form_submit($form, &$form_state) {
pierre@0 135 $form_state['rebuild'] = TRUE;
pierre@0 136 }
pierre@0 137