annotate remote/ad_remote.module @ 0:d8a3998dac8e ad

ajout module ad
author pierre
date Fri, 20 Feb 2009 14:04:09 +0000
parents
children 948362c2a207
rev   line source
pierre@0 1 <?php
pierre@0 2 // $Id: ad_remote.module,v 1.1.4.5.2.5 2009/02/16 23:12:29 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@0 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@0 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@0 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@0 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@0 77 '#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.'),
pierre@0 78 );
pierre@0 79
pierre@0 80 $hostid = ad_host_id_create($user->uid);
pierre@0 81 $group = NULL;
pierre@0 82 if (is_array($form_state['values']['group']) && !empty($form_state['values']['group'])) {
pierre@0 83 if (isset($form_state['values']['group'][0]) && $form_state['values']['group'][0] == 0) {
pierre@0 84 unset($form_state['values']['group'][0]);
pierre@0 85 }
pierre@0 86 $group = implode(',', $form_state['values']['group']);
pierre@0 87 // Sanity check, be sure group is only numbers and commas.
pierre@0 88 $group = preg_replace('/[^0-9,]/', '', $group);
pierre@0 89 }
pierre@0 90 if (!$group) {
pierre@0 91 $group = 0;
pierre@0 92 }
pierre@0 93
pierre@0 94 $output = '<!--'. t('start') .'-->'. ad($group, $quantity, array('raw' => 1, 'hostid' => $hostid)) .'<!--'. t('end') .'-->';
pierre@0 95 $form['code']['snippet'] = array(
pierre@0 96 '#type' => 'textarea',
pierre@0 97 '#value' => $output,
pierre@0 98 '#attributes' => array(
pierre@0 99 'onclick' => 'this.select();',
pierre@0 100 'onfocus' => 'this.select();',
pierre@0 101 ),
pierre@0 102 );
pierre@0 103 }
pierre@0 104
pierre@0 105 $form['submit'] = array(
pierre@0 106 '#type' => 'submit',
pierre@0 107 '#value' => t('Generate code snippet'),
pierre@0 108 );
pierre@0 109
pierre@0 110 return $form;
pierre@0 111 }
pierre@0 112
pierre@0 113 /**
pierre@0 114 * Form validator.
pierre@0 115 */
pierre@0 116 function ad_remote_form_validate($form, &$form_state) {
pierre@0 117 if (empty($form_state['values']['group'])) {
pierre@0 118 form_set_error('group', t('At least one group should be selected'));
pierre@0 119 }
pierre@0 120 }
pierre@0 121 /**
pierre@0 122 * Tell the form to rebuild.
pierre@0 123 */
pierre@0 124 function ad_remote_form_submit($form, &$form_state) {
pierre@0 125 $form_state['rebuild'] = TRUE;
pierre@0 126 }
pierre@0 127