Mercurial > defr > drupal > ad
comparison ad.admin.inc @ 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.admin.inc,v 1.1.2.9 2009/02/17 19:22:45 jeremy Exp $ | |
3 | |
4 /** | |
5 * @file | |
6 * Advertisement admin pages and functions. | |
7 * | |
8 * Copyright (c) 2005-2009. | |
9 * Jeremy Andrews <jeremy@tag1consulting.com>. | |
10 */ | |
11 | |
12 /** | |
13 * Build default ad administration page. | |
14 */ | |
15 function ad_admin_list() { | |
16 _ad_check_installation(); | |
17 | |
18 $output = drupal_get_form('ad_filter_form'); | |
19 | |
20 if (isset($_POST['operation']) && ($_POST['operation'] == 'delete') && isset($_POST['ads'])) { | |
21 return drupal_get_form('ad_multiple_delete_confirm'); | |
22 } | |
23 $output .= drupal_get_form('ad_admin_ads'); | |
24 | |
25 return $output; | |
26 } | |
27 | |
28 /** | |
29 * Provide a filterable list of advertisements. | |
30 */ | |
31 function ad_admin_ads() { | |
32 $filter = ad_build_filter_query(); | |
33 $result = pager_query('SELECT a.*, n.* FROM {ads} a INNER JOIN {node} n ON a.aid = n.nid '. $filter['join'] .' '. $filter['where'] .' ORDER BY n.changed DESC', 50, 0, NULL, $filter['args']); | |
34 | |
35 $form['options'] = array('#type' => 'fieldset', | |
36 '#title' => t('Update options'), | |
37 '#prefix' => '<div class="container-inline">', | |
38 '#suffix' => '</div>', | |
39 ); | |
40 $options = array(); | |
41 foreach (module_invoke_all('ad_operations') as $operation => $array) { | |
42 $options[$operation] = $array['label']; | |
43 } | |
44 $form['options']['operation'] = array('#type' => 'select', '#options' => $options, '#default_value' => 'approve'); | |
45 $form['options']['submit'] = array('#type' => 'submit', '#value' => t('Update')); | |
46 | |
47 $destination = drupal_get_destination(); | |
48 $ads = array(); | |
49 while ($ad = db_fetch_object($result)) { | |
50 $ads[$ad->aid] = ''; | |
51 $form['title'][$ad->aid] = array('#value' => l($ad->title, 'node/'. $ad->aid)); | |
52 $form['group'][$ad->aid] = array('#value' => _ad_get_group($ad->aid)); | |
53 $form['adtype'][$ad->aid] = array('#value' => t(check_plain($ad->adtype))); | |
54 $form['adstatus'][$ad->aid] = array('#value' => t(check_plain($ad->adstatus))); | |
55 $form['operations'][$ad->aid] = array('#value' => l(t('edit'), 'node/'. $ad->aid .'/edit', array('query' => $destination))); | |
56 } | |
57 $form['ads'] = array('#type' => 'checkboxes', '#options' => $ads); | |
58 $form['pager'] = array('#value' => theme('pager', NULL, 50, 0)); | |
59 return $form; | |
60 } | |
61 | |
62 /** | |
63 * Implementation of hook_ad_operations(). | |
64 */ | |
65 function ad_ad_operations() { | |
66 $operations = array( | |
67 'approved' => array( | |
68 'label' => t('Mark as approved'), | |
69 'callback' => 'ad_operations_callback', | |
70 'callback arguments' => array('approved'), | |
71 ), | |
72 'active' => array( | |
73 'label' => t('Mark as active'), | |
74 'callback' => 'ad_operations_callback', | |
75 'callback arguments' => array('active'), | |
76 ), | |
77 'expired' => array( | |
78 'label' => t('Mark as expired'), | |
79 'callback' => 'ad_operations_callback', | |
80 'callback arguments' => array('expired'), | |
81 ), | |
82 'pending' => array( | |
83 'label' => t('Mark as pending'), | |
84 'callback' => 'ad_operations_callback', | |
85 'callback arguments' => array('pending'), | |
86 ), | |
87 'offline' => array( | |
88 'label' => t('Mark as offline'), | |
89 'callback' => 'ad_operations_callback', | |
90 'callback arguments' => array('offline'), | |
91 ), | |
92 'unpublished' => array( | |
93 'label' => t('Mark as unpublished'), | |
94 'callback' => 'ad_operations_callback', | |
95 'callback arguments' => array('unpublished'), | |
96 ), | |
97 'denied' => array( | |
98 'label' => t('Mark as denied'), | |
99 'callback' => 'ad_operations_callback', | |
100 'callback arguments' => array('denied'), | |
101 ), | |
102 'delete' => array( | |
103 'label' => t('Delete'), | |
104 ), | |
105 ); | |
106 return $operations; | |
107 } | |
108 | |
109 /** | |
110 * Callback function for admin mass approving ads. | |
111 * TODO: Update activated and expired when appropriate. | |
112 * TODO: Publish/unpublish nodes when appropriate. | |
113 */ | |
114 function ad_operations_callback($ads, $action) { | |
115 $placeholders = implode(',', array_fill(0, count($ads), '%d')); | |
116 db_query("UPDATE {ads} SET adstatus = '". $action ."' WHERE aid IN(". $placeholders .')', $ads); | |
117 foreach ($ads as $aid) { | |
118 $node = node_load($aid); | |
119 ad_statistics_increment($aid, 'update'); | |
120 ad_statistics_increment($aid, $action); | |
121 // Allow ad type module to act on nodeapi events. The adapi hook provides | |
122 // access to additional variables not available in the nodeapi hook. | |
123 if (isset($node->adtype)) { | |
124 // Don't use module_invoke, as in pre-PHP5 the changes to $node won't be | |
125 // passed back. | |
126 $function = "ad_$node->adtype" .'_adapi'; | |
127 if (function_exists($function)) { | |
128 $function('update', $node); | |
129 } | |
130 } | |
131 // Allow ad cache module to act on nodeapi events. | |
132 $cache = variable_get('ad_cache', 'none'); | |
133 if ($cache != 'none') { | |
134 $function = "ad_cache_$cache" .'_adcacheapi'; | |
135 if (function_exists($function)) { | |
136 $function($action, $node); | |
137 } | |
138 } | |
139 } | |
140 } | |
141 | |
142 /** | |
143 * Display a form to confirm whether to really delete the selected ads. | |
144 */ | |
145 function ad_multiple_delete_confirm($form_state) { | |
146 $form['ads'] = array('#prefix' => '<ul>', '#suffix' => '</ul>', '#tree' => TRUE); | |
147 // array_filter returns only elements with TRUE values | |
148 foreach (array_filter($form_state['post']['ads']) as $aid => $value) { | |
149 $title = db_result(db_query('SELECT title FROM {node} WHERE nid = %d', $aid)); | |
150 $form['ads'][$aid] = array('#type' => 'hidden', '#value' => $aid, '#prefix' => '<li>', '#suffix' => check_plain($title) ."</li>\n"); | |
151 } | |
152 $form['operation'] = array('#type' => 'hidden', '#value' => 'delete'); | |
153 | |
154 return confirm_form($form, | |
155 t('Are you sure you want to delete these ads?'), | |
156 'admin/content/ad', t('This action cannot be undone.'), | |
157 t('Delete all'), t('Cancel')); | |
158 } | |
159 | |
160 /** | |
161 * Perform the actual ad deletions. | |
162 */ | |
163 function ad_multiple_delete_confirm_submit($form, &$form_state) { | |
164 if ($form_state['values']['confirm']) { | |
165 foreach ($form_state['values']['ads'] as $aid => $value) { | |
166 node_delete($aid); | |
167 } | |
168 drupal_set_message(t('The ads have been deleted.')); | |
169 } | |
170 $form_state['redirect'] = 'admin/content/ad'; | |
171 } | |
172 | |
173 /** | |
174 * Theme ad administration overview. | |
175 */ | |
176 function theme_ad_admin_ads($form) { | |
177 // Overview table: | |
178 $header = array(theme('table_select_header_cell'), t('Title'), t('Group'), t('Type'), t('Status'), t('Operations')); | |
179 | |
180 $output = drupal_render($form['options']); | |
181 if (isset($form['title']) && is_array($form['title'])) { | |
182 foreach (element_children($form['title']) as $key) { | |
183 $row = array(); | |
184 $row[] = drupal_render($form['ads'][$key]); | |
185 $row[] = drupal_render($form['title'][$key]); | |
186 $row[] = drupal_render($form['group'][$key]); | |
187 $row[] = drupal_render($form['adtype'][$key]); | |
188 $row[] = drupal_render($form['adstatus'][$key]); | |
189 $row[] = drupal_render($form['operations'][$key]); | |
190 $rows[] = $row; | |
191 } | |
192 | |
193 } | |
194 else { | |
195 $rows[] = array(array('data' => t('No ads available.'), 'colspan' => '6')); | |
196 } | |
197 | |
198 $output .= theme('table', $header, $rows); | |
199 if ($form['pager']['#value']) { | |
200 $output .= drupal_render($form['pager']); | |
201 } | |
202 | |
203 $output .= drupal_render($form); | |
204 | |
205 return $output; | |
206 } | |
207 | |
208 /** | |
209 * Must select an ad if performing an operation. | |
210 */ | |
211 function ad_admin_ads_validate($form, &$form_state) { | |
212 $ads = array_filter($form_state['values']['ads']); | |
213 if (count($ads) == 0) { | |
214 form_set_error('', t('No ads selected.')); | |
215 } | |
216 } | |
217 | |
218 /** | |
219 * Submit the ad administration update form. | |
220 */ | |
221 function ad_admin_ads_submit($form, &$form_state) { | |
222 $operations = module_invoke_all('ad_operations'); | |
223 $operation = $operations[$form_state['values']['operation']]; | |
224 // Filter out unchecked nodes | |
225 $ads = array_filter($form_state['values']['ads']); | |
226 if ($function = $operation['callback']) { | |
227 // Add in callback arguments if present. | |
228 if (isset($operation['callback arguments'])) { | |
229 $args = array_merge(array($ads), $operation['callback arguments']); | |
230 } | |
231 else { | |
232 $args = array($ads); | |
233 } | |
234 call_user_func_array($function, $args); | |
235 | |
236 cache_clear_all(); | |
237 drupal_set_message(t('The update has been performed.')); | |
238 } | |
239 } | |
240 | |
241 /** | |
242 * Build query for ad administration filters based on session. | |
243 */ | |
244 function ad_build_filter_query() { | |
245 $filters = ad_filters(); | |
246 | |
247 // Build query | |
248 $where = $args = array(); | |
249 $join = ''; | |
250 foreach ($_SESSION['ad_overview_filter'] as $index => $filter) { | |
251 list($key, $value) = $filter; | |
252 switch ($key) { | |
253 case 'status': | |
254 list($value, $key) = explode('-', $value, 2); | |
255 $op = $key == 1 ? '=' : '!='; | |
256 $where[] = "a.adstatus $op '%s'"; | |
257 break; | |
258 case 'group': | |
259 $table = "tn$index"; | |
260 $where[] = "$table.tid = %d"; | |
261 $join .= "INNER JOIN {term_node} $table ON n.nid = $table.nid "; | |
262 break; | |
263 case 'type': | |
264 $where[] = "a.adtype = '%s'"; | |
265 } | |
266 $args[] = $value; | |
267 } | |
268 $where = count($where) ? 'WHERE '. implode(' AND ', $where) : ''; | |
269 | |
270 return array('where' => $where, 'join' => $join, 'args' => $args); | |
271 } | |
272 | |
273 /** | |
274 * List ad administration filters that can be applied. | |
275 */ | |
276 function ad_filters() { | |
277 $session = &$_SESSION['ad_overview_filter']; | |
278 $session = is_array($session) ? $session : array(); | |
279 // Regular filters | |
280 $options = array(); | |
281 $options = array( | |
282 'pending-1' => t('pending'), | |
283 'approved-1' => t('approved'), | |
284 'active-1' => t('active'), | |
285 'offline-1' => t('offline'), | |
286 'unpublished-1' => t('unpublished'), | |
287 'expired-1' => t('expired'), | |
288 'denied-1' => t('denied'), | |
289 'pending-0' => t('not pending'), | |
290 'approved-0' => t('not approved'), | |
291 'active-0' => t('not active'), | |
292 'offline-0' => t('not offline'), | |
293 'unpublished-0' => t('not unpublished'), | |
294 'expired-0' => t('not expired'), | |
295 'denied-0' => t('not denied') | |
296 ); | |
297 | |
298 $filters['status'] = array( | |
299 'title' => t('status'), | |
300 'options' => $options | |
301 ); | |
302 $adtypes = ad_get_types(); | |
303 $filters['type'] = array( | |
304 'title' => t('type'), | |
305 'options' => $adtypes, | |
306 ); | |
307 // The taxonomy filter | |
308 if ($taxonomy = module_invoke('taxonomy', 'get_tree', _ad_get_vid())) { | |
309 $options = array(); | |
310 // TODO: Add support for the default group. | |
311 //$options[0] = t('default'); | |
312 foreach ($taxonomy as $term) { | |
313 $options[$term->tid] = check_plain($term->name); | |
314 } | |
315 $filters['group'] = array('title' => t('group'), 'options' => $options); | |
316 } | |
317 | |
318 return $filters; | |
319 } | |
320 | |
321 /** | |
322 * Theme ad administration filter selector. | |
323 */ | |
324 function theme_ad_filters($form) { | |
325 $output = '<ul class="clear-block">'; | |
326 if (isset($form['current']) && sizeof($form['current'])) { | |
327 foreach (element_children($form['current']) as $key) { | |
328 $output .= '<li>'. drupal_render($form['current'][$key]) .'</li>'; | |
329 } | |
330 } | |
331 | |
332 $output .= '<li><dl class="multiselect">'. (isset($form['current']) && sizeof($form['current']) ? '<dt><em>'. t('and') .'</em> '. t('where') .'</dt>' : '') .'<dd class="a">'; | |
333 foreach (element_children($form['filter']) as $key) { | |
334 $output .= drupal_render($form['filter'][$key]); | |
335 } | |
336 $output .= '</dd>'; | |
337 | |
338 $output .= '<dt>'. t('is') .'</dt><dd class="b">'; | |
339 | |
340 if (isset($form['status'])) { | |
341 foreach (element_children($form['status']) as $key) { | |
342 $output .= drupal_render($form['status'][$key]); | |
343 } | |
344 } | |
345 $output .= '</dd>'; | |
346 | |
347 $output .= '</dl>'; | |
348 $output .= '<div class="container-inline" id="ad-admin-buttons">'. drupal_render($form['buttons']) .'</div>'; | |
349 $output .= '</li></ul>'; | |
350 | |
351 return $output; | |
352 } | |
353 | |
354 /** | |
355 * Return form for advertisement administration filters. | |
356 */ | |
357 function ad_filter_form($form_state) { | |
358 $session = &$_SESSION['ad_overview_filter']; | |
359 $session = is_array($session) ? $session : array(); | |
360 $filters = ad_filters(); | |
361 | |
362 $i = 0; | |
363 $form['filters'] = array('#type' => 'fieldset', | |
364 '#title' => t('Show only ads where'), | |
365 '#theme' => 'ad_filters', | |
366 ); | |
367 foreach ($session as $filter) { | |
368 list($type, $value) = $filter; | |
369 if ($type == 'category') { | |
370 // Load term name from DB rather than search and parse options array. | |
371 $value = module_invoke('taxonomy', 'get_term', $value); | |
372 $value = $value->name; | |
373 } | |
374 else if ($type == 'status') { | |
375 $value = $filters['status']['options'][$value]; | |
376 } | |
377 else { | |
378 $value = $filters[$type]['options'][$value]; | |
379 } | |
380 $string = ($i++ ? '<em>and</em> where <strong>%a</strong> is <strong>%b</strong>' : '<strong>%a</strong> is <strong>%b</strong>'); | |
381 $form['filters']['current'][] = array('#value' => t($string, array('%a' => $filters[$type]['title'] , '%b' => $value))); | |
382 if ($type == 'type') { | |
383 // Remove the type option if it is already being filtered on. | |
384 unset($filters['type']); | |
385 } | |
386 else if ($type == 'group') { | |
387 unset($filters['group']); | |
388 } | |
389 if ($type == 'status') { | |
390 foreach ($session as $option) { | |
391 if ($option[0] == 'status') { | |
392 list($value, $key) = explode('-', $option[1], 2); | |
393 if ($key) { | |
394 // One postive key means we can't have any more. | |
395 // Remove the status option if we're already filtering on a positive | |
396 // key (ie, 'active', as an ad can't be 'active' and 'pending') | |
397 unset($filters['status']); | |
398 } | |
399 else { | |
400 // When a key is selected, remove it and its inverse as there's | |
401 // no logic in selecting the same key multiple times, and selecting | |
402 // two opposite keys will always return 0 results. | |
403 $inverse = $key == 1 ? 0 : 1; | |
404 unset($filters['status']['options'][$option[1]]); | |
405 unset($filters['status']['options'][$value .'-'. $inverse]); | |
406 } | |
407 } | |
408 } | |
409 } | |
410 } | |
411 | |
412 $names = array(); | |
413 foreach ($filters as $key => $filter) { | |
414 $names[$key] = $filter['title']; | |
415 $form['filters']['status'][$key] = array('#type' => 'select', '#options' => $filter['options']); | |
416 } | |
417 | |
418 $form['filters']['filter'] = array('#type' => 'radios', '#options' => $names, '#default_value' => 'status'); | |
419 $form['filters']['buttons']['submit'] = array('#type' => 'submit', '#value' => (count($session) ? t('Refine') : t('Filter'))); | |
420 if (count($session)) { | |
421 $form['filters']['buttons']['undo'] = array('#type' => 'submit', '#value' => t('Undo')); | |
422 $form['filters']['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset')); | |
423 } | |
424 | |
425 return $form; | |
426 } | |
427 | |
428 /** | |
429 * Theme ad administration filter form. | |
430 */ | |
431 function theme_ad_filter_form($form) { | |
432 $output = '<div id="ad-admin-filter">'; | |
433 $output .= drupal_render($form['filters']); | |
434 $output .= '</div>'; | |
435 $output .= drupal_render($form); | |
436 return $output; | |
437 } | |
438 | |
439 /** | |
440 * Process result from ad administration filter form. | |
441 */ | |
442 function ad_filter_form_submit($form, &$form_state) { | |
443 $filters = ad_filters(); | |
444 /* TODO The 'op' element in the form values is deprecated. | |
445 Each button can have #validate and #submit functions associated with it. | |
446 Thus, there should be one button that submits the form and which invokes | |
447 the normal form_id_validate and form_id_submit handlers. Any additional | |
448 buttons which need to invoke different validate or submit functionality | |
449 should have button-specific functions. */ | |
450 switch ($form_state['values']['op']) { | |
451 case t('Filter'): | |
452 case t('Refine'): | |
453 if (isset($form_state['values']['filter'])) { | |
454 $filter = $form_state['values']['filter']; | |
455 | |
456 // Flatten the options array to accommodate hierarchical/nested options. | |
457 $flat_options = form_options_flatten($filters[$filter]['options']); | |
458 | |
459 if (isset($form_state['values'][$filter]) && isset($flat_options[$form_state['values'][$filter]])) { | |
460 $_SESSION['ad_overview_filter'][] = array($filter, $form_state['values'][$filter]); | |
461 } | |
462 } | |
463 break; | |
464 case t('Undo'): | |
465 array_pop($_SESSION['ad_overview_filter']); | |
466 break; | |
467 case t('Reset'): | |
468 $_SESSION['ad_overview_filter'] = array(); | |
469 break; | |
470 } | |
471 } | |
472 | |
473 | |
474 /** | |
475 * | |
476 */ | |
477 function ad_admin_statistics($form_state) { | |
478 $groups = ad_groups_list(TRUE); | |
479 foreach ($groups as $tid => $group) { | |
480 if ($tid) { | |
481 $ads = db_result(db_query("SELECT count(aid) FROM {ads} a JOIN {term_node} t ON a.aid = t.nid WHERE t.tid = %d AND adstatus = 'active'", $tid)); | |
482 $filter = "= ". $tid; | |
483 } | |
484 else { | |
485 $ads = db_result(db_query("SELECT count(aid) FROM {ads} a LEFT JOIN {term_node} t ON a.aid = t.nid WHERE t.tid IS NULL AND adstatus = 'active'")); | |
486 $filter = "IS NULL"; | |
487 } | |
488 if (!$ads) { | |
489 continue; | |
490 } | |
491 | |
492 $form[$group->name] = array( | |
493 '#type' => 'fieldset', | |
494 '#title' => check_plain($group->name), | |
495 '#collapsible' => TRUE, | |
496 ); | |
497 | |
498 // Get overall global statistics. | |
499 $statistics['global']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND n.tid %s", $filter)); | |
500 $statistics['global']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND n.tid %s", $filter)); | |
501 | |
502 // Get overall statistics for this year and last year. | |
503 $this_year = date('Y000000'); | |
504 $last_year = date('Y') - 1 .'000000'; | |
505 $statistics['last_year']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND date <= %d AND n.tid %s", $last_year, $this_year, $filter)); | |
506 $statistics['last_year']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND date <= %d AND n.tid %s", $last_year, $this_year, $filter)); | |
507 $statistics['this_year']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND n.tid %s", $this_year, $filter)); | |
508 $statistics['this_year']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND n.tid %s", $this_year, $filter)); | |
509 | |
510 // Get statistics for this month and last month. | |
511 $this_month = date('Ym0000'); | |
512 $last_month = date('m') - 1; | |
513 if ($last_month == 0) { | |
514 $last_month = date('Y') - 1 .'120000'; | |
515 } | |
516 else { | |
517 $last_month = date('Y') . ($last_month < 10 ? '0' : '') . $last_month .'0000'; | |
518 } | |
519 $statistics['last_month']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND date <= %d AND n.tid %s", $last_month, $this_month, $filter)); | |
520 $statistics['last_month']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND date <= %d AND n.tid %s", $last_month, $this_month, $filter)); | |
521 $statistics['this_month']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND n.tid %s", $this_month, $filter)); | |
522 $statistics['this_month']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND n.tid %s", $this_month, $filter)); | |
523 | |
524 // Get statistics for this week. | |
525 $this_week_start = date('Ymd00', time() - 60*60*24*6); | |
526 $statistics['this_week']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND n.tid %s", $this_week_start, $filter)); | |
527 $statistics['this_week']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND n.tid %s", $this_week_start, $filter)); | |
528 | |
529 // Get statistics for yesterday and today. | |
530 $yesterday_start = date('Ymd00', time() - 60*60*24); | |
531 $yesterday_end = date('Ymd24', time() - 60*60*24); | |
532 $today_start = date('Ymd00', time()); | |
533 $statistics['yesterday']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND date <= %d AND n.tid %s", $yesterday_start, $yesterday_end, $filter)); | |
534 $statistics['yesterday']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND date <= %d AND n.tid %s", $yesterday_start, $yesterday_end, $filter)); | |
535 $statistics['today']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date >= %d AND n.tid %s", $today_start, $filter)); | |
536 $statistics['today']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date >= %d AND n.tid %s", $today_start, $filter)); | |
537 | |
538 // Get statistics for this hour and the last hour. | |
539 $last_hour = date('YmdH', time() - 60*60); | |
540 $this_hour = date('YmdH', time()); | |
541 $statistics['last_hour']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date = %d AND n.tid %s", $last_hour, $filter)); | |
542 $statistics['last_hour']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date = %d AND n.tid %s", $last_hour, $filter)); | |
543 $statistics['this_hour']['views'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'view' AND date = %d AND n.tid %s", $this_hour, $filter)); | |
544 $statistics['this_hour']['clicks'] = (int)db_result(db_query("SELECT SUM(s.count) FROM {ad_statistics} s LEFT JOIN {ads} a ON s.aid = a.aid LEFT JOIN {term_node} n ON a.aid = n.nid WHERE action = 'click' AND date = %d AND n.tid %s", $this_hour, $filter)); | |
545 | |
546 // TODO: Create this view and remove the && FALSE to enable this code. | |
547 if (module_exists('views') && FALSE) { | |
548 $form[$group->name]['statistics'] = array( | |
549 '#type' => 'markup', | |
550 '#value' => '<p>'. format_plural($ads, 'There is <a href="!url">1 active ad</a> in this group.', 'There are <a href="!url">%count</a> active ads in this group.', array('!url' => url('ad/'. $group->tid .'/group'))) .'</p>'. theme('ad_statistics_display', $statistics), | |
551 ); | |
552 } | |
553 else { | |
554 $form[$group->name]['statistics'] = array( | |
555 '#type' => 'markup', | |
556 '#value' => '<p>'. format_plural($ads, 'There is 1 active ad in this group.', 'There are @count active ads in this group.') .'</p>'. theme('ad_statistics_display', $statistics), | |
557 ); | |
558 } | |
559 } | |
560 | |
561 if (!isset($form) || count($form) == 0) { | |
562 $form['header'] = array( | |
563 '#type' => 'markup', | |
564 '#value' => '<p>'. t('There are no active ads.') .'</p>', | |
565 ); | |
566 } | |
567 | |
568 return $form; | |
569 } | |
570 | |
571 /** | |
572 * Display a form for the ad module settings. | |
573 */ | |
574 function ad_admin_configure_settings($form_state) { | |
575 _ad_check_installation(); | |
576 | |
577 $adserve = variable_get('adserve', ''); | |
578 $adserveinc = variable_get('adserveinc', ''); | |
579 $form['configuration'] = array( | |
580 '#type' => 'fieldset', | |
581 '#title' => t('Status'), | |
582 ); | |
583 $form['configuration']['adserve'] = array( | |
584 '#type' => 'markup', | |
585 '#value' => t('Using detected adserve scripts: %adserve, %adserveinc', array('%adserve' => ($adserve ? $adserve : t('not found')), '%adserveinc' => ($adserveinc ? $adserveinc : t('not found')))), | |
586 ); | |
587 | |
588 $form['general'] = array( | |
589 '#type' => 'fieldset', | |
590 '#title' => t('General'), | |
591 '#collapsible' => TRUE, | |
592 '#collapsed' => FALSE, | |
593 ); | |
594 | |
595 // TODO: This needs a per-group over-ride, in case some groups are IFrames, | |
596 // while others are JavaScript, etc. | |
597 $form['general']['ad_link_target'] = array( | |
598 '#type' => 'radios', | |
599 '#title' => t('Click-through target'), | |
600 '#options' => array( | |
601 '_self' => t('same browser window and frame'), | |
602 '_blank' => t('new browser window'), | |
603 '_parent' => t('parent frame'), | |
604 '_top' => t('same browser window, removing all frames'), | |
605 ), | |
606 '#default_value' => variable_get('ad_link_target', '_self'), | |
607 '#description' => t('Select an option above to configure what happens when an ad is clicked. These options set the <em>a target</em>, and are <em>_self</em>, <em>_blank</em>, <em>_parent</em> and <em>_top</em> respectively.'), | |
608 ); | |
609 | |
610 $form['general']['ad_link_nofollow'] = array( | |
611 '#type' => 'checkbox', | |
612 '#title' => t('nofollow'), | |
613 '#default_value' => variable_get('ad_link_nofollow', 0), | |
614 '#description' => t('If enabled, %tag will be added to advertisement links generated by this module.', array('%tag' => t('rel="nofollow"'))), | |
615 ); | |
616 | |
617 // Provide hook for ad_display_TYPE modules to set display TYPE. | |
618 $display_options = array_merge(array('javascript' => t('JavaScript'), 'jquery' => t('jQuery'), 'iframe' => t('IFrame'), 'raw' => t('Raw')), module_invoke_all('displayapi', 'display_method'), array()); | |
619 | |
620 // Provide hook for ad_display_TYPE modules to define inline description. | |
621 $description = t('This setting configures the default method for displaying advertisements on your website. It is possible to override this setting when making direct calls to ad(), as described in the documentation. Using the JavaScript, jQuery, and IFrame display methods allows you to display random ads and track impressions even on cached pages. When using the Raw display method together with Drupal\'s page cache, impressions will be properly tracked but advertisements will only change when the page cache is updated.'); | |
622 $return = module_invoke_all('displayapi', 'display_description', array()); | |
623 foreach ($return as $describe) { | |
624 $description .= ' '. $describe; | |
625 } | |
626 | |
627 $form['general']['ad_display'] = array( | |
628 '#type' => 'radios', | |
629 '#title' => t('Display type'), | |
630 '#default_value' => variable_get('ad_display', 'javascript'), | |
631 '#options' => $display_options, | |
632 '#description' => $description, | |
633 ); | |
634 | |
635 $form['general']['ad_validate_url'] = array( | |
636 '#type' => 'checkbox', | |
637 '#title' => t('Validate URLs'), | |
638 '#default_value' => variable_get('ad_validate_url', 1), | |
639 '#description' => t('If enabled, any destination URLs entered in ads will be required to be complete URLs (including http:// or https:// at the beginning). If you wish to include internal urls, you will need to disable this option.'), | |
640 ); | |
641 | |
642 $form['iframe'] = array( | |
643 '#type' => 'fieldset', | |
644 '#title' => t('IFrame'), | |
645 '#collapsible' => TRUE, | |
646 '#collapsed' => variable_get('ad_display', 'javascript') == 'iframe' ? FALSE : TRUE | |
647 ); | |
648 $form['iframe']['ad_iframe_frameborder'] = array( | |
649 '#type' => 'checkbox', | |
650 '#title' => t('Frameborder'), | |
651 '#default_value' => variable_get('ad_iframe_frameborder', 0), | |
652 '#description' => t('If enabled, IFrames used for displaying ads will have a frameborder.'), | |
653 ); | |
654 $form['iframe']['ad_iframe_scroll'] = array( | |
655 '#type' => 'radios', | |
656 '#title' => t('Scrolling'), | |
657 '#default_value' => variable_get('ad_iframe_scroll', 'auto'), | |
658 '#options' => array('auto' => 'auto', 'on' => 'on', 'off' => 'off'), | |
659 '#description' => t('Define whether or not scroll bars should be enabled for the ad IFrame.'), | |
660 ); | |
661 $form['iframe']['ad_iframe_width'] = array( | |
662 '#type' => 'textfield', | |
663 '#title' => t('Width'), | |
664 '#default_value' => variable_get('ad_iframe_width', ''), | |
665 '#maxlength' => 8, | |
666 '#size' => 5, | |
667 '#required' => FALSE, | |
668 '#description' => t('The default width for advertisement IFrames'), | |
669 ); | |
670 $form['iframe']['ad_iframe_height'] = array( | |
671 '#type' => 'textfield', | |
672 '#title' => t('Height'), | |
673 '#default_value' => variable_get('ad_iframe_height', ''), | |
674 '#maxlength' => 8, | |
675 '#size' => 5, | |
676 '#required' => FALSE, | |
677 '#description' => t('The default height for advertisement IFrames'), | |
678 ); | |
679 | |
680 $form['cache'] = array( | |
681 '#type' => 'fieldset', | |
682 '#title' => t('Cache'), | |
683 '#collapsible' => TRUE, | |
684 '#collapsed' => variable_get('ad_cache', 'none') == 'none' ? TRUE : FALSE, | |
685 ); | |
686 | |
687 // Provide hook for ad_cache_TYPE modules to set cache TYPE. | |
688 $cache_options = array_merge(array('none' => t('None')), module_invoke_all('adcacheapi', 'method', array())); | |
689 | |
690 // Provide hook for ad_cache_TYPE modules to define inline description. | |
691 $description = t('A cache can be used to efficiently track how many times advertisements are displayed and clicked.'); | |
692 $return = module_invoke_all('adcacheapi', 'description', array()); | |
693 foreach ($return as $describe) { | |
694 $description .= ' '. $describe; | |
695 } | |
696 | |
697 $form['cache']['ad_cache'] = array( | |
698 '#type' => 'radios', | |
699 '#title' => t('Type'), | |
700 '#default_value' => variable_get('ad_cache', 'none'), | |
701 '#options' => $cache_options, | |
702 '#description' => $description, | |
703 ); | |
704 | |
705 // Provide hook for ad_cache_TYPE modules to add inline settings. | |
706 $form['cache'] = array_merge($form['cache'], module_invoke_all('adcacheapi', 'settings')); | |
707 | |
708 $form['save'] = array( | |
709 '#type' => 'submit', | |
710 '#value' => t('Save'), | |
711 ); | |
712 | |
713 return $form; | |
714 } | |
715 | |
716 /** | |
717 * Validate form settings, calling attention to any illogical configurations. | |
718 */ | |
719 function ad_admin_configure_settings_validate($form, &$form_state) { | |
720 if ($form_state['values']['ad_link_target'] == '_self' && | |
721 $form_state['values']['ad_display'] == 'iframe') { | |
722 // We don't consider this an error, as this could be exactly what the | |
723 // administrator is trying to do. But as for most people it is likely | |
724 // to be a misconfiguration, display a helpful warning... | |
725 drupal_set_message(t('You have configured your advertisements to be displayed in iframes, and you have configured your click-through target as "same browser window and frame". This is an unusual configuration, as when you click your advertisements only the IFrame will be redirected. Be sure that this is actually what you are trying to do.')); | |
726 } | |
727 } | |
728 | |
729 /** | |
730 * Save updated values from settings form. | |
731 */ | |
732 function ad_admin_configure_settings_submit($form, &$form_state) { | |
733 variable_set('ad_link_target', $form_state['values']['ad_link_target']); | |
734 variable_set('ad_link_nofollow', $form_state['values']['ad_link_nofollow']); | |
735 variable_set('ad_cache', $form_state['values']['ad_cache']); | |
736 variable_set('ad_display', $form_state['values']['ad_display']); | |
737 variable_set('ad_validate_url', $form_state['values']['ad_validate_url']); | |
738 variable_set('ad_iframe_frameborder', $form_state['values']['ad_iframe_frameborder']); | |
739 variable_set('ad_iframe_scroll', $form_state['values']['ad_iframe_scroll']); | |
740 variable_set('ad_iframe_width', $form_state['values']['ad_iframe_width']); | |
741 variable_set('ad_iframe_height', $form_state['values']['ad_iframe_height']); | |
742 if (($cache = variable_get('ad_cache', 'none')) != 'none') { | |
743 // Allow external cache types to store their settings | |
744 module_invoke('ad_cache_'. $cache, 'adcacheapi', 'settings_submit', $form_state['values']); | |
745 } | |
746 /* | |
747 // TODO: Write an external display module and implement this. | |
748 $display = variable_get('ad_display', 'javascript'); | |
749 if ($display != 'javascript' && $display != 'raw') { | |
750 // Allow external display types to store their settings | |
751 module_invoke('ad_cache_'. $cache, 'adcacheapi', 'settings_submit', $form_state['values']); | |
752 }*/ | |
753 } | |
754 | |
755 /** | |
756 * Empty page for ad_type modules that don't define a global settings page. | |
757 * This way admins can still set default permissions for this ad type. | |
758 */ | |
759 function ad_no_global_settings($form_state) { | |
760 $form = array(); | |
761 | |
762 $form['save'] = array( | |
763 '#type' => 'submit', | |
764 '#value' => t('Save'), | |
765 ); | |
766 | |
767 return $form; | |
768 } | |
769 | |
770 function ad_admin_groups_list() { | |
771 _ad_check_installation(); | |
772 | |
773 $header = array( | |
774 array('data' => t('Name'), 'field' => 'name'), | |
775 array('data' => t('Description'), 'field' => 'description'), | |
776 array('data' => t('Options')), | |
777 ); | |
778 | |
779 $groups = taxonomy_get_tree(_ad_get_vid()); | |
780 | |
781 if ($groups != array()) { | |
782 foreach ($groups as $group) { | |
783 $row = array(); | |
784 $row[] = check_plain($group->name); | |
785 $row[] = check_plain($group->description); | |
786 $row[] = l(t('edit'), "admin/content/ad/groups/$group->tid/edit"); | |
787 $rows[] = $row; | |
788 } | |
789 } | |
790 else { | |
791 $rows[] = array(array('data' => t('No groups have been created.'), 'colspan' => 3)); | |
792 } | |
793 | |
794 $output = theme('table', $header, $rows); | |
795 $output .= theme('pager', NULL, 15, 0); | |
796 | |
797 return $output; | |
798 } | |
799 | |
800 /** | |
801 * Returns a form for adding an ad group. | |
802 */ | |
803 function ad_admin_group_form($form_state, $group = NULL) { | |
804 $form['name'] = array( | |
805 '#type' => 'textfield', | |
806 '#title' => t('Group name'), | |
807 '#default_value' => isset($group->name) ? check_plain($group->name) : '', | |
808 '#maxlength' => 64, | |
809 '#required' => TRUE, | |
810 '#description' => t('Specify a name for the ad group.') | |
811 ); | |
812 | |
813 $form['description'] = array( | |
814 '#type' => 'textarea', | |
815 '#title' => t('Description'), | |
816 '#default_value' => isset($group->description) ? check_plain($group->description) : '', | |
817 '#required' => TRUE, | |
818 '#description' => t('Describe this ad group.') | |
819 ); | |
820 | |
821 $form['weight'] = array( | |
822 '#type' => 'weight', | |
823 '#title' => t('Weight'), | |
824 '#default_value' => isset($group->weight) ? $group->weight : 0, | |
825 '#description' => t('When listing ad groups, those with lighter (smaller) weights get listed before ad groups with heavier (larger) weights. Ad groups with equal weights are sorted alphabetically.') | |
826 ); | |
827 | |
828 $form['vid'] = array( | |
829 '#type' => 'hidden', | |
830 '#value' => _ad_get_vid(), | |
831 ); | |
832 | |
833 | |
834 if (isset($group->tid)) { | |
835 $form['submit'] = array( | |
836 '#type' => 'submit', | |
837 '#value' => t('Save'), | |
838 ); | |
839 $form['delete'] = array( | |
840 '#type' => 'submit', | |
841 '#value' => t('Delete'), | |
842 ); | |
843 $form['tid'] = array( | |
844 '#type' => 'value', | |
845 '#value' => $group->tid | |
846 ); | |
847 } | |
848 else { | |
849 $form['submit'] = array( | |
850 '#type' => 'submit', | |
851 '#value' => t('Create group'), | |
852 ); | |
853 } | |
854 | |
855 return $form; | |
856 } | |
857 | |
858 /** | |
859 * Save a newly created ad group. | |
860 */ | |
861 function ad_admin_group_form_validate($form, &$form_state) { | |
862 if ($form_state['values']['op'] == t('Delete')) { | |
863 drupal_goto('admin/content/ad/groups/'. $form_state['values']['tid'] .'/delete'); | |
864 } | |
865 } | |
866 | |
867 | |
868 /** | |
869 * Save a newly created ad group. | |
870 */ | |
871 function ad_admin_group_form_submit($form, &$form_state) { | |
872 $status = taxonomy_save_term($form_state['values']); | |
873 switch ($status) { | |
874 case SAVED_NEW: | |
875 $groups = variable_get('ad_groups', array()); | |
876 $groups[] = $form_state['values']['tid']; | |
877 variable_set('ad_groups', $groups); | |
878 drupal_set_message(t('Created new ad group %term.', array('%term' => $form_state['values']['name']))); | |
879 break; | |
880 case SAVED_UPDATED: | |
881 drupal_set_message(t('The ad group %term has been updated.', array('%term' => $form_state['values']['name']))); | |
882 } | |
883 $form_state['redirect'] = 'admin/content/ad/groups'; | |
884 } | |
885 | |
886 /** | |
887 * Returns a confirmation page when deleting an ad group and all of its ads. | |
888 */ | |
889 function ad_confirm_group_delete($form_state, $group = NULL) { | |
890 $form['tid'] = array( | |
891 '#type' => 'value', | |
892 '#value' => $group->tid, | |
893 ); | |
894 $form['name'] = array( | |
895 '#type' => 'value', | |
896 '#value' => check_plain($group->name), | |
897 ); | |
898 | |
899 return confirm_form( | |
900 $form, | |
901 t('Are you sure you want to delete the ad group %name?', array('%name' => $group->name)), | |
902 'admin/content/ad/groups', | |
903 t('Ads that were within this group will not be deleted. This action cannot be undone.'), | |
904 t('Delete'), | |
905 t('Cancel')); | |
906 } | |
907 | |
908 /** | |
909 * Delete ad group. | |
910 */ | |
911 function ad_confirm_group_delete_submit($form, &$form_state) { | |
912 taxonomy_del_term($form_state['values']['tid']); | |
913 drupal_set_message(t('The ad group %term has been deleted.', array('%term' => $form_state['values']['name']))); | |
914 watchdog('ad', 'mailarchive: deleted %term ad group.', array('%term' => $form_state['values']['name'])); | |
915 | |
916 $form_state['redirect'] = 'admin/content/ad/groups'; | |
917 } | |
918 |