comparison remote/ad_remote.module @ 0:d8a3998dac8e ad

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