Mercurial > defr > drupal > ad
comparison text/ad_text.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_text.module,v 1.2.2.7.2.24.2.13 2009/02/17 18:56:26 jeremy Exp $ | |
3 | |
4 /** | |
5 * @file | |
6 * Enhances the ad module to support static text ads. | |
7 * | |
8 * Copyright (c) 2005-2009. | |
9 * Jeremy Andrews <jeremy@tag1consulting.com>. | |
10 */ | |
11 | |
12 /** | |
13 * Function used to display the selected ad. | |
14 */ | |
15 function ad_text_display_ad($ad) { | |
16 return theme('ad_text_ad', $ad); | |
17 } | |
18 | |
19 /** | |
20 * Return a themed ad of type ad_text. | |
21 * | |
22 * @param @ad | |
23 * The ad object. | |
24 * @return | |
25 * A string containing the ad markup. | |
26 */ | |
27 function theme_ad_text_ad($ad) { | |
28 if (isset($ad->aid)) { | |
29 $output = '<div class="text-advertisement" id="ad-'. $ad->aid .'">'; | |
30 if (isset($ad->url) && !empty($ad->url)) { | |
31 $output .= theme('ad_text_text', ad_text_display_prepare($ad->adheader, $ad->format), ad_text_display_prepare($ad->adbody, $ad->format), $ad->redirect .'/@HOSTID___'); | |
32 } | |
33 else { | |
34 $output .= theme('ad_text_text', ad_text_display_prepare($ad->adheader, $ad->format), ad_text_display_prepare($ad->adbody, $ad->format)); | |
35 } | |
36 $output .= '</div>'; | |
37 return $output; | |
38 } | |
39 } | |
40 | |
41 /** | |
42 * Return a themed text ad. | |
43 * | |
44 * @param $header | |
45 * The header of the text ad. | |
46 * @param $body | |
47 * The body of the text ad. | |
48 * @param $link | |
49 * Optional link URL for header. | |
50 * @return | |
51 * A string containing the text ad. | |
52 */ | |
53 function theme_ad_text_text($header, $body, $link = NULL) { | |
54 $output = '<div class="ad-header">'; | |
55 if (isset($link) && !empty($link)) { | |
56 $output .= l($header, $link, array('attributes' => ad_link_attributes(), 'html' => TRUE)); | |
57 } | |
58 else { | |
59 $output .= $header; | |
60 } | |
61 $output .= '</div>'; | |
62 $output .= '<div class="ad-body">'. $body .'</div>'; | |
63 return $output; | |
64 } | |
65 | |
66 /** | |
67 * Strip illegal characters, apply input filters, then encode the rest UTF-8. | |
68 */ | |
69 function ad_text_display_prepare($string, $format) { | |
70 $string = preg_replace("/[\b\f\n\r\t]/", ' ', $string); | |
71 $string = check_markup($string, $format, FALSE); | |
72 return $string; | |
73 } | |
74 | |
75 /** | |
76 * Implementation of hook_theme(). | |
77 */ | |
78 function ad_text_theme() { | |
79 return array( | |
80 'ad_text_ad' => array( | |
81 'file' => 'ad_text.module', | |
82 'arguments' => array( | |
83 'ad' => NULL, | |
84 ), | |
85 ), | |
86 'ad_text_text' => array( | |
87 'file' => 'ad_text.module', | |
88 'arguments' => array( | |
89 'header' => NULL, | |
90 'body' => NULL, | |
91 'link' => NULL, | |
92 ), | |
93 ), | |
94 ); | |
95 } | |
96 | |
97 /** | |
98 * Implementation of hook_help(). | |
99 */ | |
100 function ad_text_help($path, $arg) { | |
101 $output = ''; | |
102 switch ($path) { | |
103 case 'node/add/ad#text': | |
104 $output = t('A text advertisement.'); | |
105 break; | |
106 } | |
107 return $output; | |
108 } | |
109 | |
110 /** | |
111 * Implementation of hook_access(). | |
112 */ | |
113 function ad_text_access($op, $node, $account) { | |
114 return ad_access($op, $node, $account); | |
115 } | |
116 | |
117 /** | |
118 * Text ad settings form. | |
119 */ | |
120 function ad_text_global_settings($form_state) { | |
121 $form = array(); | |
122 | |
123 $form['header_min'] = array( | |
124 '#type' => 'textfield', | |
125 '#title' => t('Header minimum length'), | |
126 '#size' => 3, | |
127 '#maxlength' => 3, | |
128 '#default_value' => variable_get('header_min', 0), | |
129 '#description' => t('Optionally specify the minimum number of characters allowed in the header of a text ad. Set to <em>0</em> to specify no minimum length.'), | |
130 ); | |
131 $form['header_max'] = array( | |
132 '#type' => 'textfield', | |
133 '#title' => t('Header maximum length'), | |
134 '#size' => 3, | |
135 '#maxlength' => 3, | |
136 '#default_value' => variable_get('header_max', 0), | |
137 '#description' => t('Optionally specify the maximum number of characters allowed in the header of a text ad. Set to <em>0</em> to specify no maximum length.'), | |
138 ); | |
139 $form['body_min'] = array( | |
140 '#type' => 'textfield', | |
141 '#title' => t('Body minimum length'), | |
142 '#size' => 10, | |
143 '#maxlength' => 10, | |
144 '#default_value' => variable_get('body_min', 0), | |
145 '#description' => t('Optionally specify the minimum number of characters allowed in the body of a text ad. Set to <em>0</em> to specify no minimum length.'), | |
146 ); | |
147 $form['body_max'] = array( | |
148 '#type' => 'textfield', | |
149 '#title' => t('Body maximum length'), | |
150 '#size' => 10, | |
151 '#maxlength' => 10, | |
152 '#default_value' => variable_get('body_max', 0), | |
153 '#description' => t('Optionally specify the maximum number of characters allowed in the body of a text ad. Set to <em>0</em> to specify no maximum length.'), | |
154 ); | |
155 | |
156 | |
157 $form['save'] = array( | |
158 '#type' => 'submit', | |
159 '#value' => t('Save'), | |
160 ); | |
161 | |
162 return $form; | |
163 } | |
164 | |
165 /** | |
166 * Settings form validation handler. | |
167 */ | |
168 function ad_text_global_settings_validate($form, &$form_state) { | |
169 if ((int)$form_state['values']['header_min'] > (int)$form_state['values']['header_max']) { | |
170 form_set_error('header_min', t('The header minimum length can not be greater than the header maximum length.')); | |
171 } | |
172 if ((int)$form_state['values']['body_min'] > (int)$form_state['values']['body_max']) { | |
173 form_set_error('body_min', t('The body minimum length can not be greater than the body maximum length.')); | |
174 } | |
175 } | |
176 | |
177 /** | |
178 * Settings form submit handler. | |
179 */ | |
180 function ad_text_global_settings_submit($form, &$form_state) { | |
181 variable_set('header_min', $form_state['values']['header_min']); | |
182 variable_set('header_max', $form_state['values']['header_max']); | |
183 variable_set('body_min', $form_state['values']['body_min']); | |
184 variable_set('body_max', $form_state['values']['body_max']); | |
185 } | |
186 | |
187 /** | |
188 * Implementation of hook_adapi(). | |
189 */ | |
190 function ad_text_adapi($op, &$node) { | |
191 switch ($op) { | |
192 case 'load': | |
193 $return = db_fetch_array(db_query('SELECT aid, url, adheader, adbody FROM {ad_text} WHERE aid = %d', $node['aid'])); | |
194 $return['ad'] = ad_text_display_prepare($return['adheader'], $node['format']) .'<br />'. ad_text_display_prepare($return['adbody'], $node['format']); | |
195 return $return; | |
196 | |
197 case 'insert': | |
198 db_query("INSERT INTO {ad_text} (aid, url, adheader, adbody) VALUES(%d, '%s', '%s', '%s')", $node->nid, $node->url, $node->adheader, $node->adbody); | |
199 break; | |
200 | |
201 case 'update': | |
202 if (ad_adaccess($node, 'manage ad text')) { | |
203 db_query("UPDATE {ad_text} SET url = '%s', adheader = '%s', adbody = '%s' WHERE aid = %d", $node->url, $node->adheader, $node->adbody, $node->nid); | |
204 } | |
205 break; | |
206 | |
207 case 'delete': | |
208 db_query('DELETE FROM {ad_text} WHERE aid = %d', $node->nid); | |
209 break; | |
210 | |
211 case 'form': | |
212 return ad_text_node_form($node); | |
213 | |
214 case 'view': | |
215 return ad_text_node_view($node); | |
216 | |
217 case 'redirect': | |
218 return db_result(db_query('SELECT url FROM {ad_text} WHERE aid = %d', $node->nid)); | |
219 | |
220 case 'validate': | |
221 $todo = array(); | |
222 return ad_text_node_validate($node, $todo); | |
223 | |
224 case 'type': | |
225 return array( | |
226 'text' => array( | |
227 'name' => t('Text ad'), | |
228 'module' => 'ad_text', | |
229 'description' => t('A text advertisement.'), | |
230 'help' => t('A text advertisement.'), | |
231 ), | |
232 ); | |
233 | |
234 case 'permissions': | |
235 if (!isset($node->adtype) || $node->adtype == 'text') { | |
236 return array('manage ad text'); | |
237 } | |
238 } | |
239 } | |
240 | |
241 /** | |
242 * Adapi helper function for displaying a node form. | |
243 */ | |
244 function ad_text_node_form(&$node) { | |
245 $form = array(); | |
246 | |
247 $form['ad_text'] = array( | |
248 '#type' => 'fieldset', | |
249 '#title' => t('Text'), | |
250 '#collapsible' => TRUE, | |
251 ); | |
252 | |
253 if (ad_adaccess($node, 'manage ad text') || arg(1) == 'add' && user_access('create advertisements')) { | |
254 $access = TRUE; | |
255 } | |
256 else { | |
257 $access = FALSE; | |
258 $form['ad_text']['notice'] = array( | |
259 '#type' => 'markup', | |
260 '#value' => '<p>'. t('You do not have permission to edit this advertisement.') .'</p>', | |
261 ); | |
262 } | |
263 | |
264 $form['ad_text']['text'] = array( | |
265 '#type' => 'markup', | |
266 '#value' => ad_text_display_ad($node), | |
267 ); | |
268 | |
269 $form['ad_text']['url'] = array( | |
270 '#type' => 'textfield', | |
271 '#title' => t('Destination URL'), | |
272 '#maxlength' => 255, | |
273 '#default_value' => isset($node->url) ? $node->url : '', | |
274 '#description' => t('Enter the complete URL where you want people to be redirected when they click on this advertisement. The URL must be valid and begin with http:// or https://, for example %url, unless you !disable. If you do not enter a URL, the advertisement will not be clickable.', array('%url' => t('http://www.sample.org/'), '!disable' => l(t('disable URL validation'), 'admin/content/ad/configure', array('fragment' => 'edit-ad-validate-url-wrapper')))), | |
275 '#disabled' => !$access, | |
276 ); | |
277 | |
278 $form['ad_text']['adheader'] = array( | |
279 '#type' => 'textfield', | |
280 '#title' => t('Ad header'), | |
281 '#required' => $access, | |
282 '#default_value' => isset($node->adheader) ? $node->adheader : '', | |
283 '#description' => t('This is the first line of the ad which will be linked to the URL entered above.'), | |
284 '#disabled' => !$access, | |
285 ); | |
286 $form['ad_text']['adbody'] = array( | |
287 '#type' => 'textarea', | |
288 '#title' => t('Ad body'), | |
289 '#required' => $access, | |
290 '#default_value' => isset($node->adbody) ? $node->adbody : '', | |
291 '#description' => t('This is the rest of the ad.'), | |
292 '#disabled' => !$access, | |
293 ); | |
294 | |
295 return $form; | |
296 } | |
297 | |
298 /** | |
299 * Adapi helper function for displaying ad itself. | |
300 */ | |
301 function ad_text_node_view(&$node) { | |
302 $node->content['ad'] = array( | |
303 '#value' => theme('box', '', preg_replace('&@HOSTID___&', '0', (ad_text_display_ad($node)))), | |
304 '#weight' => -1, | |
305 ); | |
306 if (!empty($node->url)) { | |
307 $link = t('Links to !url.', array('!url' => $node->url)); | |
308 $link = check_plain($link, $node->format, FALSE); | |
309 $node->content['ad-link'] = array( | |
310 '#value' => "<div class=\"links-to\">$link</div>", | |
311 '#weight' => 1, | |
312 ); | |
313 } | |
314 } | |
315 | |
316 /** | |
317 * Text ads node validator. | |
318 */ | |
319 function ad_text_node_validate($node, &$form_state) { | |
320 // Enforce minimum and maximum lengths. | |
321 $header_len = isset($node->adheader) ? strlen($node->adheader) : 0; | |
322 $header_min = variable_get('header_min', 0); | |
323 $header_max = variable_get('header_max', 0); | |
324 if ($header_min && ($header_len < $header_min)) { | |
325 form_set_error('adheader', t('Your text ad header is only %cur characters but must be at least %min characters.', array('%cur' => $header_len, '%min' => $header_min))); | |
326 } | |
327 else if ($header_max && ($header_len > $header_max)) { | |
328 form_set_error('adheader', t('Your text ad header is %cur characters but can not be more than %max characters.', array('%cur' => $header_len, '%max' => $header_max))); | |
329 } | |
330 $body_len = strlen($node->adbody); | |
331 $body_min = variable_get('body_min', 0); | |
332 $body_max = variable_get('body_max', 0); | |
333 if ($body_min && ($body_len < $body_min)) { | |
334 form_set_error('adbody', t('Your text ad body is only %cur characters but must be at least %min characters.', array('%cur' => $body_len, '%min' => $body_min))); | |
335 } | |
336 else if ($body_max && ($body_len > $body_max)) { | |
337 form_set_error('adbody', t('Your text ad body is %cur characters but can not be more than %max characters.', array('%cur' => $body_len, '%max' => $body_max))); | |
338 } | |
339 | |
340 if ($node->url && variable_get('ad_validate_url', 1) && (!valid_url($node->url, TRUE))) { | |
341 form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL')))); | |
342 } | |
343 } |