pierre@1
|
1 <?php |
sly@2
|
2 // $Id: ad_channel.module,v 1.1.4.20 2009/04/09 22:04:49 jeremy Exp $ |
pierre@1
|
3 |
pierre@1
|
4 /** |
pierre@1
|
5 * @file |
pierre@1
|
6 * Ad Channel module. |
pierre@1
|
7 * |
pierre@1
|
8 * Copyright (c) 2008-2009. |
pierre@1
|
9 * Jeremy Andrews <jeremy@tag1consulting.com>. |
pierre@1
|
10 */ |
pierre@1
|
11 |
pierre@1
|
12 define('AD_CHANNEL_LIST_BOTH', 0); |
pierre@1
|
13 define('AD_CHANNEL_LIST_CHANNEL', 1); |
pierre@1
|
14 define('AD_CHANNEL_LIST_GROUP', 2); |
pierre@1
|
15 |
pierre@1
|
16 /** |
pierre@1
|
17 * Implementation of hook_help(). |
pierre@1
|
18 */ |
pierre@1
|
19 function ad_channel_help($path) { |
pierre@1
|
20 $output = ''; |
pierre@1
|
21 switch ($path) { |
pierre@1
|
22 case 'admin/help#ad_channel': |
pierre@1
|
23 $output = '<p>'. t('This module provides the ability to create advertisement channels, for which rules can be defined.') .'</p>'; |
pierre@1
|
24 break; |
pierre@1
|
25 case 'admin/content/ad/channel': |
pierre@1
|
26 case 'admin/content/ad/channel/list': |
pierre@1
|
27 return '<p>'. t('This is a list of existing containers and channels that you can edit. Containers hold channels, and channels hold advertisements.') .'</p>'; |
pierre@1
|
28 case 'admin/content/ad/channel/container': |
pierre@1
|
29 return '<p>'. t('Containers help you organize your advertisement channels. A container can hold one or more related advertisement channels.') .'</p>'; |
pierre@1
|
30 case 'admin/content/ad/channel/channel': |
pierre@1
|
31 return '<p>'. t('Advertisements can be assigned to one or more advertisement channels. Rules can then be applied to these channels.') .'</p>'; |
pierre@1
|
32 } |
pierre@1
|
33 return $output; |
pierre@1
|
34 } |
pierre@1
|
35 |
pierre@1
|
36 /** |
pierre@1
|
37 * Drupal _perm hook. Establishes permissions used by this module. |
pierre@1
|
38 * |
pierre@1
|
39 * @return An array of permissions used by this module. |
pierre@1
|
40 */ |
pierre@1
|
41 function ad_channel_perm() { |
pierre@1
|
42 return (array('administer channels', 'configure ad premier status')); |
pierre@1
|
43 } |
pierre@1
|
44 |
pierre@1
|
45 /** |
pierre@1
|
46 * Implementation of hook_menu. |
pierre@1
|
47 */ |
pierre@1
|
48 function ad_channel_menu() { |
pierre@1
|
49 $items = array(); |
pierre@1
|
50 |
pierre@1
|
51 $items['admin/content/ad/channel'] = array( |
pierre@1
|
52 'title' => t('Channels'), |
pierre@1
|
53 'page callback' => 'ad_channel_admin_overview', |
pierre@1
|
54 'access arguments' => array('administer channels'), |
pierre@1
|
55 'type' => MENU_LOCAL_TASK, |
pierre@1
|
56 'weight' => 6); |
pierre@1
|
57 $items['admin/content/ad/channel/list'] = array( |
pierre@1
|
58 'title' => t('List'), |
pierre@1
|
59 'page callback' => 'ad_channel_admin_overview', |
pierre@1
|
60 'access arguments' => array('administer channels'), |
pierre@1
|
61 'type' => MENU_DEFAULT_LOCAL_TASK, |
pierre@1
|
62 'weight' => 0); |
pierre@1
|
63 $items['admin/content/ad/channel/container'] = array( |
pierre@1
|
64 'title' => t('Create container'), |
pierre@1
|
65 'page callback' => 'drupal_get_form', |
pierre@1
|
66 'page arguments' => array('ad_channel_admin_container'), |
pierre@1
|
67 'access arguments' => array('administer channels'), |
pierre@1
|
68 'type' => MENU_LOCAL_TASK, |
pierre@1
|
69 'weight' => 2); |
pierre@1
|
70 $items['admin/content/ad/channel/channel'] = array( |
pierre@1
|
71 'title' => t('Create channel'), |
pierre@1
|
72 'page callback' => 'drupal_get_form', |
pierre@1
|
73 'page arguments' => array('ad_channel_admin_channel'), |
pierre@1
|
74 'access arguments' => array('administer channels'), |
pierre@1
|
75 'type' => MENU_LOCAL_TASK, |
pierre@1
|
76 'weight' => 4); |
pierre@1
|
77 $items['admin/content/ad/channel/settings'] = array( |
pierre@1
|
78 'title' => t('Settings'), |
pierre@1
|
79 'page callback' => 'drupal_get_form', |
pierre@1
|
80 'page arguments' => array('ad_channel_admin_settings'), |
pierre@1
|
81 'access arguments' => array('administer channels'), |
pierre@1
|
82 'type' => MENU_LOCAL_TASK, |
pierre@1
|
83 'weight' => 9); |
sly@2
|
84 $items['admin/content/ad/channel/container/%ad_channel_container/delete'] = array( |
sly@2
|
85 'page callback' => 'drupal_get_form', |
sly@2
|
86 'page arguments' => array('ad_channel_admin_confirm_delete_container', 5), |
sly@2
|
87 'access arguments' => array('administer channels'), |
sly@2
|
88 'type' => MENU_CALLBACK); |
sly@2
|
89 $items['admin/content/ad/channel/channel/%ad_channel_channel/delete'] = array( |
sly@2
|
90 'page callback' => 'drupal_get_form', |
sly@2
|
91 'page arguments' => array('ad_channel_admin_confirm_delete_channel', 5), |
sly@2
|
92 'access arguments' => array('administer channels'), |
sly@2
|
93 'type' => MENU_CALLBACK); |
pierre@1
|
94 |
pierre@1
|
95 return $items; |
pierre@1
|
96 } |
pierre@1
|
97 |
pierre@1
|
98 /** |
sly@2
|
99 * Load a specified container. |
sly@2
|
100 */ |
sly@2
|
101 function ad_channel_container_load($conid = 0) { |
sly@2
|
102 static $containers = array(); |
sly@2
|
103 if (!isset($containers[$conid])) { |
sly@2
|
104 $containers[$conid] = db_fetch_object(db_query('SELECT * FROM {ad_channel_container} WHERE conid = %d', $conid)); |
sly@2
|
105 } |
sly@2
|
106 return $containers[$conid]; |
sly@2
|
107 } |
sly@2
|
108 |
sly@2
|
109 /** |
sly@2
|
110 * Load a specified channel. |
sly@2
|
111 */ |
sly@2
|
112 function ad_channel_channel_load($chid = 0) { |
sly@2
|
113 static $channels = array(); |
sly@2
|
114 if (!isset($channels[$chid])) { |
sly@2
|
115 $channels[$chid] = db_fetch_object(db_query('SELECT * FROM {ad_channel} WHERE chid = %d', $chid)); |
sly@2
|
116 } |
sly@2
|
117 return $channels[$chid]; |
sly@2
|
118 } |
sly@2
|
119 |
sly@2
|
120 /** |
pierre@1
|
121 * Implementation of hook_form_alter(). |
pierre@1
|
122 * Generate a form for selecting channels to associate with an advertisement. |
pierre@1
|
123 */ |
pierre@1
|
124 function ad_channel_form_alter(&$form, &$form_state, $form_id) { |
pierre@1
|
125 if (isset($form['type']) && $form_id == 'ad_node_form') { |
pierre@1
|
126 $fieldset = FALSE; |
pierre@1
|
127 $containers = _ad_channel_get_containers(); |
pierre@1
|
128 foreach ($containers as $container) { |
pierre@1
|
129 $channels = _ad_channel_get_container_channels($container->conid); |
pierre@1
|
130 if (!empty($channels)) { |
pierre@1
|
131 if ($container->conid) { |
pierre@1
|
132 $fieldset = TRUE; |
pierre@1
|
133 } |
pierre@1
|
134 if ($fieldset) { |
pierre@1
|
135 $form['channel'][$container->conid] = array( |
pierre@1
|
136 '#type' => 'fieldset', |
pierre@1
|
137 '#title' => $container->name, |
pierre@1
|
138 '#collapsible' => FALSE, |
pierre@1
|
139 '#collapsed' => FALSE, |
pierre@1
|
140 ); |
pierre@1
|
141 } |
pierre@1
|
142 foreach ($channels as $channel) { |
pierre@1
|
143 if (is_object($form['#node'])) { |
pierre@1
|
144 $node = $form['#node']; |
pierre@1
|
145 $default = isset($node->channel[$channel->chid]); |
pierre@1
|
146 } |
pierre@1
|
147 else { |
pierre@1
|
148 $default = 0; |
pierre@1
|
149 } |
pierre@1
|
150 $form['channel'][$container->conid]["channel-$channel->chid"] = array( |
pierre@1
|
151 '#type' => 'checkbox', |
pierre@1
|
152 '#title' => $channel->name, |
pierre@1
|
153 '#description' => $channel->description, |
pierre@1
|
154 '#default_value' => $default, |
pierre@1
|
155 ); |
pierre@1
|
156 } |
pierre@1
|
157 } |
pierre@1
|
158 } |
pierre@1
|
159 $node = node_load($form['nid']['#value']); |
pierre@1
|
160 if (isset($form['channel']) && is_array($form['channel']) && !empty($form['channel'])) { |
pierre@1
|
161 $form['channel'] += array( |
pierre@1
|
162 '#type' => 'fieldset', |
pierre@1
|
163 '#title' => t('Channels'), |
pierre@1
|
164 '#collapsible' => TRUE, |
pierre@1
|
165 '#collapsed' => FALSE, |
pierre@1
|
166 ); |
pierre@1
|
167 $form['channel']['#weight'] = -2; |
pierre@1
|
168 $form['channel']['#tree'] = TRUE; |
pierre@1
|
169 } |
pierre@1
|
170 $form['priority'] = array( |
pierre@1
|
171 '#type' => 'fieldset', |
sly@2
|
172 '#access' => user_access('configure ad premier status'), |
pierre@1
|
173 '#title' => t('Priority'), |
pierre@1
|
174 '#collapsible' => TRUE, |
pierre@1
|
175 '#collapsed' => FALSE, |
pierre@1
|
176 ); |
pierre@1
|
177 $form['priority']['premiere'] = array( |
pierre@1
|
178 '#type' => 'checkbox', |
sly@2
|
179 '#access' => user_access('configure ad premier status'), |
pierre@1
|
180 '#title' => t('Premiere'), |
pierre@1
|
181 '#description' => t('An active premiere advertisement will override all other non-premiere advertisements in the same channel. As long as one or more premiere advertisements are active in a channel, non-premiere advertisements will not be displayed in that channel.'), |
pierre@1
|
182 '#default_value' => isset($node->premiere) ? $node->premiere : FALSE, |
pierre@1
|
183 ); |
pierre@1
|
184 $form['priority']['#weight'] = -1; |
pierre@1
|
185 } |
pierre@1
|
186 else if ($form_id == 'ad_filter_form') { |
pierre@1
|
187 $session = &$_SESSION['ad_overview_filter']; |
pierre@1
|
188 $session = is_array($session) ? $session : array(); |
pierre@1
|
189 |
pierre@1
|
190 $display_channel = TRUE; |
pierre@1
|
191 $display_premiere = TRUE; |
pierre@1
|
192 foreach ($session as $filter) { |
pierre@1
|
193 list($type, $value) = $filter; |
pierre@1
|
194 if ($type == 'channel') { |
pierre@1
|
195 $display_channel = FALSE; |
pierre@1
|
196 } |
pierre@1
|
197 else if ($type == 'premiere') { |
pierre@1
|
198 $display_premiere = FALSE; |
pierre@1
|
199 } |
pierre@1
|
200 } |
pierre@1
|
201 |
pierre@1
|
202 if ($display_channel) { |
pierre@1
|
203 $channels = _ad_channel_get_channels(); |
pierre@1
|
204 $options = array(); |
pierre@1
|
205 foreach ($channels as $channel) { |
pierre@1
|
206 $key = 'channel-'. $channel->chid; |
pierre@1
|
207 $options[$key] = $channel->name; |
pierre@1
|
208 } |
pierre@1
|
209 $form['filters']['status']['channel'] = array( |
pierre@1
|
210 '#type' => 'select', |
pierre@1
|
211 '#options' => $options |
pierre@1
|
212 ); |
pierre@1
|
213 $form['filters']['filter']['#options']['channel'] = 'channel'; |
pierre@1
|
214 } |
pierre@1
|
215 else { |
pierre@1
|
216 unset($form['filters']['status']['channel']); |
pierre@1
|
217 unset($form['filters']['filter']['#options']['channel']); |
pierre@1
|
218 } |
pierre@1
|
219 |
pierre@1
|
220 if ($display_premiere) { |
pierre@1
|
221 $options = array( |
pierre@1
|
222 '0' => t('false'), |
pierre@1
|
223 '1' => t('true')); |
pierre@1
|
224 $form['filters']['status']['premiere'] = array( |
pierre@1
|
225 '#type' => 'select', |
pierre@1
|
226 '#options' => $options |
pierre@1
|
227 ); |
pierre@1
|
228 $form['filters']['filter']['#options']['premiere'] = 'premiere'; |
pierre@1
|
229 } |
pierre@1
|
230 else { |
pierre@1
|
231 unset($form['filters']['status']['premiere']); |
pierre@1
|
232 unset($form['filters']['filter']['#options']['premiere']); |
pierre@1
|
233 } |
pierre@1
|
234 } |
pierre@1
|
235 else if ($form_id == 'ad_report_admin') { |
pierre@1
|
236 $channels = _ad_channel_get_channels(); |
pierre@1
|
237 if (is_array($channels) && !empty($channels)) { |
pierre@1
|
238 $channel = isset($_SESSION['ad_report_channel']) && is_array($_SESSION['ad_report_channel']) && !empty($_SESSION['ad_report_channel']) ? $_SESSION['ad_report_channel'] : array('any'); |
pierre@1
|
239 $form['channels'] = array( |
pierre@1
|
240 '#type' => 'fieldset', |
pierre@1
|
241 '#title' => t('Channels'), |
pierre@1
|
242 ); |
pierre@1
|
243 $options = array(); |
pierre@1
|
244 $options['any'] = t('- Any -'); |
pierre@1
|
245 foreach ($channels as $chan) { |
pierre@1
|
246 $options[$chan->chid] = $chan->name; |
pierre@1
|
247 } |
pierre@1
|
248 $form['channels']['channel'] = array( |
pierre@1
|
249 '#type' => 'select', |
pierre@1
|
250 '#title' => t('Ad channels'), |
pierre@1
|
251 '#options' => $options, |
pierre@1
|
252 '#multiple' => TRUE, |
pierre@1
|
253 '#required' => TRUE, |
pierre@1
|
254 '#default_value' => $channel, |
pierre@1
|
255 ); |
pierre@1
|
256 $form['#submit'] = array_merge(array('ad_channel_admin_report_submit'), $form['#submit']); |
pierre@1
|
257 } |
pierre@1
|
258 } |
pierre@1
|
259 else if ($form_id == 'ad_admin_ads' && is_array($form['group'])) { |
pierre@1
|
260 foreach ($form['group'] as $aid => $value) { |
pierre@1
|
261 $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $aid); |
pierre@1
|
262 $names = array(); |
pierre@1
|
263 while ($channel = db_fetch_object($result)) { |
pierre@1
|
264 $names[] = _ad_channel_get_name($channel->chid); |
pierre@1
|
265 } |
pierre@1
|
266 if (empty($names)) { |
pierre@1
|
267 $names[] = t('none'); |
pierre@1
|
268 } |
pierre@1
|
269 $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); |
pierre@1
|
270 switch ($list) { |
pierre@1
|
271 case AD_CHANNEL_LIST_CHANNEL: |
pierre@1
|
272 unset($form['group']); |
pierre@1
|
273 $form['channel'][$aid]['#value'] = implode(', ', $names); |
pierre@1
|
274 break; |
pierre@1
|
275 case AD_CHANNEL_LIST_BOTH: |
pierre@1
|
276 $form['channel'][$aid]['#value'] = implode(', ', $names); |
pierre@1
|
277 break; |
pierre@1
|
278 case AD_CHANNEL_LIST_GROUP: |
pierre@1
|
279 // do nothing |
pierre@1
|
280 break; |
pierre@1
|
281 } |
pierre@1
|
282 } |
pierre@1
|
283 } |
pierre@1
|
284 } |
pierre@1
|
285 |
pierre@1
|
286 /** |
pierre@1
|
287 * Register our own ad_admin_ads theme function. |
pierre@1
|
288 */ |
pierre@1
|
289 function ad_channel_theme_registry_alter(&$theme_registry) { |
pierre@1
|
290 $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); |
pierre@1
|
291 if ($list == AD_CHANNEL_LIST_CHANNEL || $list == AD_CHANNEL_LIST_BOTH) { |
pierre@1
|
292 if (!empty($theme_registry['ad_admin_ads'])) { |
pierre@1
|
293 $theme_registry['ad_admin_ads']['function'] = 'ad_channel_ad_admin_ads'; |
pierre@1
|
294 } |
pierre@1
|
295 } |
pierre@1
|
296 } |
pierre@1
|
297 |
pierre@1
|
298 /** |
pierre@1
|
299 * Implement custom theme function for displaying ad overview, replacing groups |
pierre@1
|
300 * with channels. |
pierre@1
|
301 */ |
pierre@1
|
302 function ad_channel_ad_admin_ads($form) { |
pierre@1
|
303 $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); |
pierre@1
|
304 |
pierre@1
|
305 // Overview table: |
pierre@1
|
306 if ($list == AD_CHANNEL_LIST_CHANNEL) { |
pierre@1
|
307 $header = array(theme('table_select_header_cell'), t('Title'), t('Channel'), t('Type'), t('Status'), t('Operations')); |
pierre@1
|
308 } |
pierre@1
|
309 else { |
pierre@1
|
310 $header = array(theme('table_select_header_cell'), t('Title'), t('Group'), t('Channel'), t('Type'), t('Status'), t('Operations')); |
pierre@1
|
311 } |
pierre@1
|
312 |
pierre@1
|
313 $output = drupal_render($form['options']); |
pierre@1
|
314 if (isset($form['title']) && is_array($form['title'])) { |
pierre@1
|
315 foreach (element_children($form['title']) as $key) { |
pierre@1
|
316 $row = array(); |
pierre@1
|
317 $row[] = drupal_render($form['ads'][$key]); |
pierre@1
|
318 $row[] = drupal_render($form['title'][$key]); |
pierre@1
|
319 if ($list == AD_CHANNEL_LIST_BOTH) { |
pierre@1
|
320 $row[] = drupal_render($form['group'][$key]); |
pierre@1
|
321 } |
pierre@1
|
322 $row[] = drupal_render($form['channel'][$key]); |
pierre@1
|
323 $row[] = drupal_render($form['adtype'][$key]); |
pierre@1
|
324 $row[] = drupal_render($form['adstatus'][$key]); |
pierre@1
|
325 $row[] = drupal_render($form['operations'][$key]); |
pierre@1
|
326 $rows[] = $row; |
pierre@1
|
327 } |
pierre@1
|
328 |
pierre@1
|
329 } |
pierre@1
|
330 else { |
pierre@1
|
331 $rows[] = array(array('data' => t('No ads available.'), 'colspan' => '6')); |
pierre@1
|
332 } |
pierre@1
|
333 |
pierre@1
|
334 $output .= theme('table', $header, $rows); |
pierre@1
|
335 if ($form['pager']['#value']) { |
pierre@1
|
336 $output .= drupal_render($form['pager']); |
pierre@1
|
337 } |
pierre@1
|
338 |
pierre@1
|
339 $output .= drupal_render($form); |
pierre@1
|
340 |
pierre@1
|
341 return $output; |
pierre@1
|
342 } |
pierre@1
|
343 |
pierre@1
|
344 function _ad_channel_get_name($chid) { |
pierre@1
|
345 static $names = array(); |
pierre@1
|
346 if (!isset($names[$chid])) { |
pierre@1
|
347 $names[$chid] = db_result(db_query('SELECT name FROM {ad_channel} WHERE chid = %d', $chid)); |
pierre@1
|
348 } |
pierre@1
|
349 return $names[$chid]; |
pierre@1
|
350 } |
pierre@1
|
351 |
pierre@1
|
352 function ad_channel_admin_report_submit($form, $form_state) { |
pierre@1
|
353 if ($form_state['clicked_button']['#value'] == t('Reset report')) { |
pierre@1
|
354 unset($_SESSION['ad_report_channel']); |
pierre@1
|
355 } |
pierre@1
|
356 else if ($form_state['clicked_button']['#value'] == t('Generate report')) { |
pierre@1
|
357 if (isset($form_state['values']['channel']) && is_array($form_state['values']['channel'])) { |
pierre@1
|
358 $channels = array(); |
pierre@1
|
359 $any = FALSE; |
pierre@1
|
360 foreach ($form_state['values']['channel'] as $chid) { |
pierre@1
|
361 if (is_numeric($chid)) { |
pierre@1
|
362 $channels[] = $chid; |
pierre@1
|
363 } |
pierre@1
|
364 else { |
pierre@1
|
365 $any = TRUE; |
pierre@1
|
366 } |
pierre@1
|
367 } |
pierre@1
|
368 if (!$any && !empty($channels)) { |
pierre@1
|
369 $_SESSION['ad_report_channel'] = $channels; |
pierre@1
|
370 } |
pierre@1
|
371 else { |
pierre@1
|
372 if (isset($_SESSION['ad_report_channel'])) { |
pierre@1
|
373 unset($_SESSION['ad_report_channel']); |
pierre@1
|
374 } |
pierre@1
|
375 } |
pierre@1
|
376 } |
pierre@1
|
377 } |
pierre@1
|
378 } |
pierre@1
|
379 |
pierre@1
|
380 /** |
pierre@1
|
381 * Filter reports by selected channel. |
pierre@1
|
382 */ |
pierre@1
|
383 function ad_channel_adreport($join, $where, $args, $select) { |
pierre@1
|
384 if (isset($_SESSION['ad_report_channel']) && is_array($_SESSION['ad_report_channel']) && !empty($_SESSION['ad_report_channel'])) { |
pierre@1
|
385 $join = array('LEFT JOIN {ad_channel_node} acn ON acn.nid = a.aid'); |
pierre@1
|
386 $where = array('acn.chid IN (%s)'); |
pierre@1
|
387 $args = array(implode(',', $_SESSION['ad_report_channel'])); |
pierre@1
|
388 return array('join' => $join, 'where' => $where, 'args' => $args); |
pierre@1
|
389 } |
pierre@1
|
390 } |
pierre@1
|
391 |
pierre@1
|
392 /** |
pierre@1
|
393 * Implement hook _adapi. |
pierre@1
|
394 */ |
pierre@1
|
395 function ad_channel_adapi($op, &$node) { |
pierre@1
|
396 switch ($op) { |
pierre@1
|
397 case 'admin_filters': |
pierre@1
|
398 $channels = _ad_channel_get_channels(); |
pierre@1
|
399 $options = array(); |
pierre@1
|
400 foreach ($channels as $channel) { |
pierre@1
|
401 $key = 'channel-'. $channel->chid; |
pierre@1
|
402 $options[$key] = $channel->name; |
pierre@1
|
403 } |
pierre@1
|
404 $filters['channel'] = array( |
pierre@1
|
405 'title' => t('channel'), |
pierre@1
|
406 'options' => $options, |
pierre@1
|
407 ); |
pierre@1
|
408 $options = array( |
pierre@1
|
409 '0' => t('false'), |
pierre@1
|
410 '1' => t('true')); |
pierre@1
|
411 $filters['premiere'] = array( |
pierre@1
|
412 'title' => t('premiere'), |
pierre@1
|
413 'options' => $options, |
pierre@1
|
414 ); |
pierre@1
|
415 return $filters; |
pierre@1
|
416 case 'admin_filter_query': |
pierre@1
|
417 if (is_array($node)) { |
pierre@1
|
418 list($key, $value) = $node; |
pierre@1
|
419 if ($key == 'channel') { |
pierre@1
|
420 list($key, $value) = explode('-', $value, 2); |
pierre@1
|
421 return array( |
pierre@1
|
422 'channel' => array( |
pierre@1
|
423 'where' => 'c.chid = %d', |
pierre@1
|
424 'join' => 'INNER JOIN {ad_channel_node} c ON n.nid = c.nid ', |
pierre@1
|
425 'value' => $value, |
pierre@1
|
426 )); |
pierre@1
|
427 } |
pierre@1
|
428 else if ($key == 'premiere') { |
pierre@1
|
429 return array( |
pierre@1
|
430 'premiere' => array( |
pierre@1
|
431 'where' => 'p.priority = %d', |
pierre@1
|
432 'join' => 'INNER JOIN {ad_priority} p ON n.nid = p.aid ', |
pierre@1
|
433 'value' => $value, |
pierre@1
|
434 )); |
pierre@1
|
435 } |
pierre@1
|
436 } |
pierre@1
|
437 break; |
pierre@1
|
438 } |
pierre@1
|
439 } |
pierre@1
|
440 |
pierre@1
|
441 /** |
pierre@1
|
442 * Implementation of hook_nodeapi(). |
pierre@1
|
443 */ |
pierre@1
|
444 function ad_channel_nodeapi($node, $op, $arg = 0) { |
pierre@1
|
445 switch ($op) { |
pierre@1
|
446 case 'view': |
pierre@1
|
447 return _ad_channel_view_node($node); |
pierre@1
|
448 case 'load': |
pierre@1
|
449 return _ad_channel_load_node($node); |
pierre@1
|
450 case 'insert': |
pierre@1
|
451 case 'update': |
pierre@1
|
452 // Fully load the node object to confirm that we are working with an |
pierre@1
|
453 // advertisement. |
pierre@1
|
454 $ad = node_load($node->nid); |
pierre@1
|
455 if (isset($ad->adtype)) { |
pierre@1
|
456 return _ad_channel_save_node($node); |
pierre@1
|
457 } |
pierre@1
|
458 case 'delete': |
pierre@1
|
459 return _ad_channel_delete_node($node); |
pierre@1
|
460 case 'validate': |
pierre@1
|
461 return _ad_channel_validate_nodes($node); |
pierre@1
|
462 } |
pierre@1
|
463 } |
pierre@1
|
464 |
pierre@1
|
465 /** |
pierre@1
|
466 * Implementation of hook_ad_build_cache(). |
pierre@1
|
467 */ |
pierre@1
|
468 function ad_channel_ad_build_cache() { |
pierre@1
|
469 $cache = array(); |
pierre@1
|
470 $ads = array(); |
pierre@1
|
471 $active = db_query("SELECT aid FROM {ads} WHERE adstatus = 'active'"); |
pierre@1
|
472 while ($ad = db_fetch_object($active)) { |
pierre@1
|
473 // cache channel<->node relation |
pierre@1
|
474 $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $ad->aid); |
pierre@1
|
475 while ($channel = db_fetch_object($result)) { |
pierre@1
|
476 $ads[$ad->aid][$channel->chid] = $channel->chid; |
pierre@1
|
477 //$ads[$channel->chid][$ad->aid] = $ad->aid; |
pierre@1
|
478 } |
pierre@1
|
479 } |
pierre@1
|
480 $channels = array(); |
sly@2
|
481 $result = db_query('SELECT chid, display, urls, no_channel_weight FROM {ad_channel}'); |
pierre@1
|
482 while ($channel = db_fetch_object($result)) { |
pierre@1
|
483 $channels[$channel->chid] = $channel; |
pierre@1
|
484 } |
pierre@1
|
485 $result = db_query("SELECT p.aid, p.priority FROM {ads} a LEFT JOIN {ad_priority} p ON a.aid = p.aid WHERE a.adstatus = 'active' AND p.priority = 1"); |
pierre@1
|
486 $premiere = array(); |
pierre@1
|
487 while ($priority = db_fetch_object($result)) { |
pierre@1
|
488 $premiere[$priority->aid] = $priority->aid; |
pierre@1
|
489 } |
pierre@1
|
490 $cache['channel']['ads'] = $ads; |
pierre@1
|
491 $cache['channel']['channels'] = $channels; |
pierre@1
|
492 $cache['channel']['display'] = variable_get('ad_channel_display', 0); |
pierre@1
|
493 $cache['premiere'] = $premiere; |
pierre@1
|
494 |
pierre@1
|
495 $cache['channel']['hook_filter'] = array( |
pierre@1
|
496 'weight' => 0, |
pierre@1
|
497 'file' => drupal_get_path('module', 'ad_channel') .'/ad_channel.inc', |
pierre@1
|
498 'function' => 'ad_channel_cache_filter', |
pierre@1
|
499 ); |
pierre@1
|
500 |
pierre@1
|
501 return $cache; |
pierre@1
|
502 } |
pierre@1
|
503 |
pierre@1
|
504 /***/ |
pierre@1
|
505 |
pierre@1
|
506 /** |
pierre@1
|
507 * Settings form. |
pierre@1
|
508 */ |
pierre@1
|
509 function ad_channel_admin_settings() { |
pierre@1
|
510 $form = array(); |
pierre@1
|
511 |
pierre@1
|
512 $form['ad_channel_display'] = array( |
pierre@1
|
513 '#type' => 'radios', |
pierre@1
|
514 '#title' => t('Display advertisements not assigned to any channel'), |
pierre@1
|
515 '#options' => array(t('Only if no matching advertisements are found in the active channels'), t('Always'), t('Never')), |
pierre@1
|
516 '#default_value' => variable_get('ad_channel_display', 0), |
pierre@1
|
517 '#description' => t('By default, advertisements will first be selected out of the active channels, and if none are found they will be selected out of advertisements not assigned to any channel. If you select "Always", advertisements will be selected out of the active channels and from advertisements not assigned to any channels. If you select "Never", advertisements will only be selected out of the active channels, and advertisements not assigned to any channel will not ever be displayed.'), |
pierre@1
|
518 ); |
pierre@1
|
519 $form['ad_channel_admin_list'] = array( |
pierre@1
|
520 '#type' => 'radios', |
pierre@1
|
521 '#title' => t('Display channels on administrative ads overview listing'), |
pierre@1
|
522 '#options' => array(t('In addition to groups'), t('In place of groups'), t('Not at all')), |
pierre@1
|
523 '#default_value' => variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL), |
pierre@1
|
524 '#description' => t('By default, channels will be listed along with groups on the administrative ads list. You can optionally disable either the groups column (by selecting "in place of groups"), or the channel column (by selecting "not at all").'), |
pierre@1
|
525 ); |
sly@2
|
526 $options = array(0 => t('No limit')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
sly@2
|
527 $form['ad_channel_ad_limit'] = array( |
sly@2
|
528 '#type' => 'select', |
sly@2
|
529 '#title' => t('Maximum number of channels that can be assigned to a single ad'), |
sly@2
|
530 '#options' => $options, |
sly@2
|
531 '#default_value' => variable_get('ad_channel_ad_limit', 0), |
sly@2
|
532 '#description' => t('Optionally limit the number of channels that a single advertisement can be assigned to.'), |
sly@2
|
533 ); |
pierre@1
|
534 |
pierre@1
|
535 return system_settings_form($form); |
pierre@1
|
536 } |
pierre@1
|
537 |
pierre@1
|
538 /** |
pierre@1
|
539 * Add channel information when viewing node. |
pierre@1
|
540 */ |
pierre@1
|
541 function _ad_channel_view_node($node) { |
pierre@1
|
542 if (isset($node->adtype) && user_access('administer channels')) { |
pierre@1
|
543 if (isset($node->channel) && is_array($node->channel) && !empty($node->channel)) { |
pierre@1
|
544 $channels = array(); |
pierre@1
|
545 foreach ($node->channel as $chid) { |
pierre@1
|
546 $channel = _ad_channel_get_channels($chid); |
pierre@1
|
547 $channels[] = $channel->name; |
pierre@1
|
548 } |
pierre@1
|
549 $node->content['channel'] = array( |
pierre@1
|
550 '#value' => theme('box', t('Channels'), theme('item_list', $channels)), |
pierre@1
|
551 '#weight' => 1, |
pierre@1
|
552 ); |
pierre@1
|
553 } |
pierre@1
|
554 if (isset($node->premiere)) { |
pierre@1
|
555 if (isset($node->premiere) && $node->premiere == 1) { |
pierre@1
|
556 $output = t('This is a premiere advertisement.'); |
pierre@1
|
557 } |
pierre@1
|
558 else { |
pierre@1
|
559 $output = t('This is not a premiere advertisement.'); |
pierre@1
|
560 } |
pierre@1
|
561 $node->content['premiere'] = array( |
pierre@1
|
562 '#value' => theme('box', t('Premiere'), $output), |
pierre@1
|
563 '#weight' => 1, |
pierre@1
|
564 ); |
pierre@1
|
565 } |
pierre@1
|
566 } |
pierre@1
|
567 } |
pierre@1
|
568 |
pierre@1
|
569 /** |
pierre@1
|
570 * Load channels associated with specified node. |
pierre@1
|
571 */ |
pierre@1
|
572 function _ad_channel_load_node($node) { |
pierre@1
|
573 $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $node->nid); |
pierre@1
|
574 $output['channel'] = array(); |
pierre@1
|
575 while ($chid = db_fetch_object($result)) { |
pierre@1
|
576 $output['channel'][$chid->chid] = $chid->chid; |
pierre@1
|
577 } |
pierre@1
|
578 // currently 0 or 1, with one being a 'premiere' advertisement. |
pierre@1
|
579 $output['premiere'] = (int)db_result(db_query('SELECT priority FROM {ad_priority} WHERE aid = %d', $node->nid)); |
pierre@1
|
580 return $output; |
pierre@1
|
581 } |
pierre@1
|
582 |
pierre@1
|
583 /** |
pierre@1
|
584 * Save channels associated with added or updated node. |
pierre@1
|
585 */ |
pierre@1
|
586 function _ad_channel_save_node($node) { |
pierre@1
|
587 // delete old channel information, then add new |
pierre@1
|
588 db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid); |
pierre@1
|
589 $channels = _ad_channel_get_enabled($node); |
pierre@1
|
590 foreach ($channels as $chid) { |
pierre@1
|
591 db_query('INSERT INTO {ad_channel_node} (chid, nid) VALUES(%d, %d)', $chid, $node->nid); |
pierre@1
|
592 } |
sly@2
|
593 if (user_access('configure ad premier status')) { |
pierre@1
|
594 db_query('UPDATE {ad_priority} SET priority = %d WHERE aid = %d', isset($node->premiere) ? $node->premiere : 0, $node->nid); |
pierre@1
|
595 if (!db_affected_rows()) { |
pierre@1
|
596 db_query('INSERT INTO {ad_priority} (aid, priority) VALUES(%d, %d)', $node->nid, isset($node->premiere) ? $node->premiere : 0); |
pierre@1
|
597 } |
pierre@1
|
598 } |
pierre@1
|
599 } |
pierre@1
|
600 |
pierre@1
|
601 /** |
pierre@1
|
602 * Delete channel information associated with node. |
pierre@1
|
603 */ |
pierre@1
|
604 function _ad_channel_delete_node($node) { |
pierre@1
|
605 if ($node->nid) { |
pierre@1
|
606 db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid); |
pierre@1
|
607 db_query('DELETE FROM {ad_priority} WHERE aid = %d', $node->nid); |
pierre@1
|
608 } |
pierre@1
|
609 } |
pierre@1
|
610 |
pierre@1
|
611 /** |
pierre@1
|
612 * Be sure that the enabled channels actually can be enabled. |
pierre@1
|
613 */ |
pierre@1
|
614 function _ad_channel_validate_nodes($node) { |
pierre@1
|
615 $channels = _ad_channel_get_enabled($node); |
sly@2
|
616 $limit = variable_get('ad_channel_ad_limit', 0); |
sly@2
|
617 if ($limit && sizeof($channels) > $limit) { |
sly@2
|
618 $quantity_error = TRUE; |
sly@2
|
619 } |
sly@2
|
620 else { |
sly@2
|
621 $quantity_error = FALSE; |
sly@2
|
622 } |
pierre@1
|
623 foreach ($channels as $chid) { |
pierre@1
|
624 $channel = _ad_channel_get_channels($chid); |
sly@2
|
625 if ($quantity_error) { |
sly@2
|
626 $quantity_error = FALSE; |
sly@2
|
627 form_set_error("channel[$channel->conid][channel-$chid]", t('You can not assign this advertisement to more than !limit.', array('!limit' => format_plural(sizeof($limit), '1 channel', '@count channels')))); |
sly@2
|
628 } |
pierre@1
|
629 $taxonomy = is_array($node->taxonomy) ? $node->taxonomy : array(); |
pierre@1
|
630 $groups = unserialize($channel->groups); |
pierre@1
|
631 if (!empty($groups)) { |
pierre@1
|
632 $enabled = FALSE; |
pierre@1
|
633 foreach($groups as $group) { |
pierre@1
|
634 if ($group) { |
pierre@1
|
635 $enabled = TRUE; |
pierre@1
|
636 break; |
pierre@1
|
637 } |
pierre@1
|
638 } |
pierre@1
|
639 if ($enabled) { |
pierre@1
|
640 $ad_groups = taxonomy_get_tree(_ad_get_vid()); |
pierre@1
|
641 foreach ($ad_groups as $ad_group) { |
pierre@1
|
642 if (is_array($taxonomy[$ad_group->vid]) && |
pierre@1
|
643 isset($taxonomy[$ad_group->vid][$ad_group->tid]) && |
pierre@1
|
644 isset($groups[$ad_group->tid]) && !$groups[$ad_group->tid] && |
pierre@1
|
645 !isset($groups[''])) { |
pierre@1
|
646 form_set_error("channel[$channel->conid][channel-$chid]", t('The %channel channel does not allow advertisements from the %group group.', array('%channel' => $channel->name, '%group' => $ad_group->name))); |
pierre@1
|
647 } |
pierre@1
|
648 } |
pierre@1
|
649 } |
pierre@1
|
650 } |
pierre@1
|
651 } |
pierre@1
|
652 } |
pierre@1
|
653 |
pierre@1
|
654 /** |
pierre@1
|
655 * Retrive list of enabled channels from node object. |
pierre@1
|
656 */ |
pierre@1
|
657 function _ad_channel_get_enabled($node) { |
pierre@1
|
658 static $enabled = array(); |
pierre@1
|
659 if (!isset($enabled[$node->nid])) { |
pierre@1
|
660 $enabled[$node->nid] = array(); |
pierre@1
|
661 if (isset($node->channel) && is_array($node->channel) && !empty($node->channel)) { |
pierre@1
|
662 foreach ($node->channel as $conid => $channels) { |
pierre@1
|
663 foreach ($channels as $id => $enable) { |
pierre@1
|
664 if ($enable) { |
pierre@1
|
665 $chid = explode('-', $id); |
pierre@1
|
666 $enabled[$node->nid][] = $chid[1]; |
pierre@1
|
667 } |
pierre@1
|
668 } |
pierre@1
|
669 } |
pierre@1
|
670 } |
pierre@1
|
671 } |
pierre@1
|
672 return $enabled[$node->nid]; |
pierre@1
|
673 } |
pierre@1
|
674 |
pierre@1
|
675 /** |
pierre@1
|
676 * Display containers and channels. |
pierre@1
|
677 */ |
pierre@1
|
678 function ad_channel_admin_overview() { |
pierre@1
|
679 drupal_add_css(drupal_get_path('module', 'ad_channel') .'/ad_channel.css'); |
pierre@1
|
680 |
pierre@1
|
681 $containers = _ad_channel_get_containers(); |
pierre@1
|
682 $rows = array(); |
pierre@1
|
683 if (count($containers)) { |
pierre@1
|
684 $header = array(t('Name'), t('Options')); |
pierre@1
|
685 $output = '<div id="ad-channel">'; |
pierre@1
|
686 foreach ($containers as $conid => $container) { |
pierre@1
|
687 $channels = _ad_channel_get_container_channels($conid); |
pierre@1
|
688 if ($conid > 0 || count($channels)) { |
pierre@1
|
689 if ($conid > 0) { |
pierre@1
|
690 $description = '<div class="name">'. l($container->name, "admin/content/ad/channel/container/$conid/edit") . "</div>\n"; |
pierre@1
|
691 } |
pierre@1
|
692 else { |
pierre@1
|
693 $description = '<div class="name">'. $container->name . "</div>\n"; |
pierre@1
|
694 } |
pierre@1
|
695 if ($container->description) { |
pierre@1
|
696 $description .= '<div class="description">'. filter_xss_admin($container->description) ."</div>\n"; |
pierre@1
|
697 } |
pierre@1
|
698 $rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => 2)); |
pierre@1
|
699 } |
pierre@1
|
700 foreach ($channels as $chid => $channel) { |
pierre@1
|
701 $description = "<div style=\"margin-left: 30px;\">\n"; |
pierre@1
|
702 $description .= ' <div class="name">' . $channel->name . "</div>\n"; |
pierre@1
|
703 if ($channel->description) { |
pierre@1
|
704 $description .= ' <div class="description">'. filter_xss_admin($channel->description) ."</div>\n"; |
pierre@1
|
705 } |
pierre@1
|
706 $description .= "</div>\n"; |
pierre@1
|
707 $rows[] = array( |
pierre@1
|
708 array('data' => $description, 'class' => 'channel'), |
pierre@1
|
709 l(t('edit'), "admin/content/ad/channel/channel/$channel->chid/edit") .' '. l(t('delete'), "admin/content/ad/channel/channel/$channel->chid/delete"), |
pierre@1
|
710 ); |
pierre@1
|
711 } |
pierre@1
|
712 } |
pierre@1
|
713 $output .= theme('table', $header, $rows); |
pierre@1
|
714 $output .= '</div>'; |
pierre@1
|
715 } |
pierre@1
|
716 |
pierre@1
|
717 return $output; |
pierre@1
|
718 } |
pierre@1
|
719 |
pierre@1
|
720 /** |
pierre@1
|
721 * Load one or more containers, caching the results. |
pierre@1
|
722 */ |
pierre@1
|
723 function _ad_channel_get_containers($conid = 0) { |
pierre@1
|
724 static $cache; |
pierre@1
|
725 if (!isset($cache[$conid])) { |
pierre@1
|
726 if ($conid) { |
pierre@1
|
727 $cache[$conid] = db_fetch_object(db_query('SELECT * FROM {ad_channel_container} WHERE conid = %d', $conid)); |
pierre@1
|
728 } |
pierre@1
|
729 else { |
pierre@1
|
730 // Get all manually defined channels. |
pierre@1
|
731 $result = db_query('SELECT conid, name, description, weight FROM {ad_channel_container} ORDER BY weight ASC'); |
pierre@1
|
732 while ($container = db_fetch_object($result)) { |
pierre@1
|
733 $containers[$container->conid] = $container; |
pierre@1
|
734 } |
pierre@1
|
735 // Define default 'No container'. |
pierre@1
|
736 $none->conid = 0; |
pierre@1
|
737 $none->name = t('No container'); |
pierre@1
|
738 $none->weight = 0; |
pierre@1
|
739 $containers[0] = $none; |
pierre@1
|
740 $cache[$conid] = $containers; |
pierre@1
|
741 } |
pierre@1
|
742 } |
pierre@1
|
743 return $cache[$conid]; |
pierre@1
|
744 } |
pierre@1
|
745 |
pierre@1
|
746 /** |
pierre@1
|
747 * Load one or more channels, caching the results. |
pierre@1
|
748 */ |
pierre@1
|
749 function _ad_channel_get_container_channels($conid = 0) { |
pierre@1
|
750 static $cache; |
pierre@1
|
751 if (!isset($cache[$conid])) { |
pierre@1
|
752 $channels = array(); |
pierre@1
|
753 $result = db_query('SELECT chid, name, description, weight FROM {ad_channel} WHERE conid = %d ORDER BY weight ASC', $conid); |
pierre@1
|
754 while ($channel = db_fetch_object($result)) { |
pierre@1
|
755 $channels[$channel->chid] = $channel; |
pierre@1
|
756 } |
pierre@1
|
757 $cache[$conid] = $channels; |
pierre@1
|
758 } |
pierre@1
|
759 return $cache[$conid]; |
pierre@1
|
760 } |
pierre@1
|
761 |
pierre@1
|
762 /** |
pierre@1
|
763 * Load one or more channels. |
pierre@1
|
764 */ |
pierre@1
|
765 function _ad_channel_get_channels($chid = 0) { |
pierre@1
|
766 if ($chid) { |
pierre@1
|
767 return db_fetch_object(db_query('SELECT * FROM {ad_channel} WHERE chid = %d', $chid)); |
pierre@1
|
768 } |
pierre@1
|
769 else { |
pierre@1
|
770 $channels = array(); |
pierre@1
|
771 $result = db_query('SELECT chid, name, description FROM {ad_channel}'); |
pierre@1
|
772 while ($channel = db_fetch_object($result)) { |
pierre@1
|
773 $channels[$channel->chid] = $channel; |
pierre@1
|
774 } |
pierre@1
|
775 return $channels; |
pierre@1
|
776 } |
pierre@1
|
777 } |
pierre@1
|
778 |
pierre@1
|
779 /** |
pierre@1
|
780 * Administrative page for creating or editing containers. |
pierre@1
|
781 */ |
pierre@1
|
782 function ad_channel_admin_container($form_state, $conid = 0) { |
pierre@1
|
783 $form = array(); |
pierre@1
|
784 |
pierre@1
|
785 if ($conid) { |
pierre@1
|
786 $container = _ad_channel_get_containers($conid); |
pierre@1
|
787 if (empty($container)) { |
pierre@1
|
788 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
789 } |
pierre@1
|
790 $form['conid'] = array( |
pierre@1
|
791 '#type' => 'hidden', |
pierre@1
|
792 '#value' => $conid, |
pierre@1
|
793 ); |
pierre@1
|
794 } |
pierre@1
|
795 |
pierre@1
|
796 $form['name'] = array( |
pierre@1
|
797 '#type' => 'textfield', |
pierre@1
|
798 '#title' => t('Container name'), |
pierre@1
|
799 '#required' => TRUE, |
pierre@1
|
800 '#description' => t('Channel containers can be used to help organize channels, but they are not required.'), |
pierre@1
|
801 '#default_value' => $conid ? $container->name : '', |
pierre@1
|
802 ); |
pierre@1
|
803 $form['description'] = array( |
pierre@1
|
804 '#type' => 'textarea', |
pierre@1
|
805 '#title' => t('Description'), |
pierre@1
|
806 '#description' => t('The channel container description can be used to help you define the purpose of your different channels. The descriptions are only visible to advertisement administrators.'), |
pierre@1
|
807 '#default_value' => $conid ? $container->description : '', |
pierre@1
|
808 ); |
pierre@1
|
809 $form['weight'] = array( |
pierre@1
|
810 '#type' => 'weight', |
pierre@1
|
811 '#title' => t('Weight'), |
pierre@1
|
812 '#description' => t('When listing containers, those with the lighter (smaller) weights get listed before containers with heavier (larger) weights. Containers with equal weights are sorted alphabetically.'), |
pierre@1
|
813 '#default_value' => $conid ? $container->weight : 0, |
pierre@1
|
814 ); |
pierre@1
|
815 |
pierre@1
|
816 if ($conid) { |
pierre@1
|
817 $form['update'] = array( |
pierre@1
|
818 '#type' => 'submit', |
pierre@1
|
819 '#value' => t('Update'), |
pierre@1
|
820 ); |
pierre@1
|
821 $form['delete'] = array( |
pierre@1
|
822 '#type' => 'submit', |
pierre@1
|
823 '#value' => t('Delete'), |
pierre@1
|
824 ); |
pierre@1
|
825 } |
pierre@1
|
826 else { |
pierre@1
|
827 $form['create'] = array( |
pierre@1
|
828 '#type' => 'submit', |
pierre@1
|
829 '#value' => t('Create'), |
pierre@1
|
830 ); |
pierre@1
|
831 } |
pierre@1
|
832 $form['cancel'] = array( |
pierre@1
|
833 '#type' => 'markup', |
pierre@1
|
834 '#value' => l(t('Cancel'), 'admin/content/ad/channel'), |
pierre@1
|
835 ); |
pierre@1
|
836 |
pierre@1
|
837 return $form; |
pierre@1
|
838 } |
pierre@1
|
839 |
pierre@1
|
840 /** |
pierre@1
|
841 * Validate the container. |
pierre@1
|
842 */ |
pierre@1
|
843 function ad_channel_admin_container_validate($form, &$form_state) { |
pierre@1
|
844 $conid = 0; |
pierre@1
|
845 if ($form_state['values']['op'] == t('Create')) { |
pierre@1
|
846 $conid = db_result(db_query("SELECT conid FROM {ad_channel_container} WHERE name = '%s'", $form_state['values']['name'])); |
pierre@1
|
847 } |
pierre@1
|
848 else if ($form_state['values']['op'] == t('Update')) { |
pierre@1
|
849 $conid = db_result(db_query("SELECT conid FROM {ad_channel_container} WHERE name = '%s' AND conid != %d", $form_state['values']['name'], $form_state['values']['conid'])); |
pierre@1
|
850 } |
pierre@1
|
851 if ($conid) { |
pierre@1
|
852 form_set_error('name', t('A container named %name already exists.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
853 } |
pierre@1
|
854 } |
pierre@1
|
855 |
pierre@1
|
856 /** |
pierre@1
|
857 * Save the container. |
pierre@1
|
858 */ |
pierre@1
|
859 function ad_channel_admin_container_submit($form, &$form_state) { |
pierre@1
|
860 switch ($form_state['values']['op']) { |
pierre@1
|
861 case t('Create'): |
pierre@1
|
862 db_query("INSERT INTO {ad_channel_container} (name, description, weight) VALUES('%s', '%s', %d)", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['weight']); |
pierre@1
|
863 drupal_set_message(t('The %name container has been created.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
864 break; |
pierre@1
|
865 case t('Update'): |
pierre@1
|
866 db_query("UPDATE {ad_channel_container} SET name = '%s', description = '%s', weight = '%s' WHERE conid = %d", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['weight'], $form_state['values']['conid']); |
pierre@1
|
867 drupal_set_message(t('The %name container has been updated.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
868 break; |
pierre@1
|
869 case t('Delete'): |
pierre@1
|
870 drupal_goto('admin/content/ad/channel/container/'. $form_state['values']['conid'] .'/delete'); |
pierre@1
|
871 } |
pierre@1
|
872 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
873 } |
pierre@1
|
874 |
pierre@1
|
875 /** |
pierre@1
|
876 * Confirm whether or not to delete container, and the contained channels. |
pierre@1
|
877 */ |
sly@2
|
878 function ad_channel_admin_confirm_delete_container($form_state, $container) { |
pierre@1
|
879 $form = array(); |
pierre@1
|
880 |
pierre@1
|
881 $form['conid'] = array( |
pierre@1
|
882 '#type' => 'value', |
sly@2
|
883 '#value' => $container->conid, |
pierre@1
|
884 ); |
pierre@1
|
885 |
pierre@1
|
886 return confirm_form( |
pierre@1
|
887 $form, |
pierre@1
|
888 t('Are you sure you want to delete the %name container?', array('%name' => $container->name)), |
pierre@1
|
889 'admin/content/ad/channel', |
pierre@1
|
890 t('Any channels currently assigned to the %name container will not be deleted, they will be reassigned. <p>This action can not be undone.', array('%name' => $container->name)), |
pierre@1
|
891 t('Delete'), |
pierre@1
|
892 t('Cancel')); |
pierre@1
|
893 } |
pierre@1
|
894 |
pierre@1
|
895 /** |
pierre@1
|
896 * Delete a container. |
pierre@1
|
897 */ |
pierre@1
|
898 function ad_channel_admin_confirm_delete_container_submit($form, &$form_state) { |
pierre@1
|
899 $container = _ad_channel_get_containers($form_state['values']['conid']); |
pierre@1
|
900 if ($container->conid) { |
pierre@1
|
901 db_query('UPDATE {ad_channel} SET conid = 0 WHERE conid = %d', $container->conid); |
pierre@1
|
902 db_query('DELETE FROM {ad_channel_container} WHERE conid = %d', $container->conid); |
pierre@1
|
903 drupal_set_message(t('The %name container has been deleted.', array('%name' => $container->name))); |
pierre@1
|
904 } |
pierre@1
|
905 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
906 } |
pierre@1
|
907 |
pierre@1
|
908 /** |
pierre@1
|
909 * Administrative page for creating or editing channels. |
pierre@1
|
910 */ |
pierre@1
|
911 function ad_channel_admin_channel($form_state, $chid = 0) { |
pierre@1
|
912 $form = array(); |
pierre@1
|
913 |
pierre@1
|
914 if ($chid) { |
pierre@1
|
915 $channel = _ad_channel_get_channels($chid); |
pierre@1
|
916 if (empty($channel)) { |
pierre@1
|
917 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
918 } |
pierre@1
|
919 $form['chid'] = array( |
pierre@1
|
920 '#type' => 'hidden', |
pierre@1
|
921 '#value' => $chid, |
pierre@1
|
922 ); |
pierre@1
|
923 } |
pierre@1
|
924 |
pierre@1
|
925 $form['name'] = array( |
pierre@1
|
926 '#type' => 'textfield', |
pierre@1
|
927 '#required' => TRUE, |
pierre@1
|
928 '#title' => t('Channel name'), |
pierre@1
|
929 '#description' => t('Enter a short, descriptive name for your channel.'), |
pierre@1
|
930 '#default_value' => $chid ? $channel->name : '', |
pierre@1
|
931 ); |
pierre@1
|
932 $form['description'] = array( |
pierre@1
|
933 '#type' => 'textarea', |
pierre@1
|
934 '#title' => t('Description'), |
pierre@1
|
935 '#description' => t('Enter a full description of your channel.'), |
pierre@1
|
936 '#default_value' => $chid ? $channel->description : '', |
pierre@1
|
937 ); |
pierre@1
|
938 $result = db_query('SELECT conid, name FROM {ad_channel_container} ORDER BY weight ASC'); |
pierre@1
|
939 $containers = array(t('<none>')); |
pierre@1
|
940 while ($container = db_fetch_object($result)) { |
pierre@1
|
941 $containers[$container->conid] = $container->name; |
pierre@1
|
942 } |
pierre@1
|
943 if (sizeof($containers) == 1) { |
pierre@1
|
944 $containers = array(t('No containers have been created.')); |
pierre@1
|
945 } |
pierre@1
|
946 $form['conid'] = array( |
pierre@1
|
947 '#type' => 'select', |
pierre@1
|
948 '#title' => t('Container'), |
pierre@1
|
949 '#options' => $containers, |
pierre@1
|
950 '#description' => t('Optionally assign your channel to a container. Containers can be used to help organize your channels, but they are not required.'), |
pierre@1
|
951 '#default_value' => $chid ? $channel->conid : 0, |
pierre@1
|
952 ); |
pierre@1
|
953 $form['weight'] = array( |
pierre@1
|
954 '#type' => 'weight', |
pierre@1
|
955 '#title' => t('Weight'), |
pierre@1
|
956 '#description' => t('When listing channels, those with the lighter (smaller) weights get listed before channels with heavier (larger) weights. Channels with equal weights are sorted alphabetically.'), |
pierre@1
|
957 '#default_value' => $chid ? $channel->weight : 0, |
pierre@1
|
958 ); |
pierre@1
|
959 |
pierre@1
|
960 // URL rules |
pierre@1
|
961 $form['URL_rules'] = array( |
pierre@1
|
962 '#type' => 'fieldset', |
pierre@1
|
963 '#title' => t('URL rules'), |
pierre@1
|
964 '#collapsible' => TRUE, |
pierre@1
|
965 '#collasped' => FALSE, |
pierre@1
|
966 ); |
pierre@1
|
967 $form['URL_rules']['display'] = array( |
pierre@1
|
968 '#type' => 'radios', |
pierre@1
|
969 '#title' => t('Display advertisements from this channel on specific URLs'), |
pierre@1
|
970 '#options' => array(t('Display advertisements on every URL except the listed URLs.'), t('Display advertisements only on the listed URLs.')), |
pierre@1
|
971 '#default_value' => $chid ? $channel->display : 0, |
pierre@1
|
972 ); |
pierre@1
|
973 $form['URL_rules']['urls'] = array( |
pierre@1
|
974 '#type' => 'textarea', |
pierre@1
|
975 '#title' => t('Paths'), |
pierre@1
|
976 '#description' => t("Enter one URL per line, including the 'http://' or 'https:/'. The '*' character is a wildcard. Example URLs are <em>http://www.example.com/blog</em> for the blog page and <em>http://www.example.com/blog/*</em> for every personal blog."), |
pierre@1
|
977 '#default_value' => $chid ? unserialize($channel->urls) : '', |
pierre@1
|
978 ); |
pierre@1
|
979 |
pierre@1
|
980 // Group rules |
pierre@1
|
981 $groups = taxonomy_get_tree(_ad_get_vid()); |
pierre@1
|
982 $collapsed = is_array($groups) && !empty($groups) ? FALSE : TRUE; |
pierre@1
|
983 $form['group_rules'] = array( |
pierre@1
|
984 '#type' => 'fieldset', |
pierre@1
|
985 '#title' => t('Ad group rules'), |
pierre@1
|
986 '#collapsible' => TRUE, |
pierre@1
|
987 '#collapsed' => $collapsed, |
pierre@1
|
988 ); |
pierre@1
|
989 if (!$collapsed) { |
pierre@1
|
990 foreach ($groups as $group) { |
pierre@1
|
991 $options[$group->tid] = $group->name; |
pierre@1
|
992 } |
pierre@1
|
993 $form['group_rules']['groups'] = array( |
pierre@1
|
994 '#type' => 'checkboxes', |
pierre@1
|
995 '#title' => t('Allow advertisements from specific ad groups'), |
pierre@1
|
996 '#options' => $options, |
pierre@1
|
997 '#prefix' => '<div>', |
pierre@1
|
998 '#suffix' => '</div>', |
pierre@1
|
999 '#description' => t('By selecting one or more groups, only advertisements from the selected group(s) will be allowed to be added to this channel. If no groups are selected, any advertisement can be added to this channel regardless of its group.'), |
pierre@1
|
1000 '#default_value' => $chid ? unserialize($channel->groups) : array(), |
pierre@1
|
1001 ); |
pierre@1
|
1002 } |
pierre@1
|
1003 else { |
pierre@1
|
1004 $form['group_rules']['none'] = array( |
pierre@1
|
1005 '#type' => 'markup', |
pierre@1
|
1006 '#value' => t('No ad groups have been created.'), |
pierre@1
|
1007 '#prefix' => '<div>', |
pierre@1
|
1008 '#suffix' => '</div>', |
pierre@1
|
1009 ); |
pierre@1
|
1010 } |
pierre@1
|
1011 |
sly@2
|
1012 $collapsed = (variable_get('ad_channel_display', 0) == 1) ? 0 : 1; |
sly@2
|
1013 $form['nonchannel_rules'] = array( |
sly@2
|
1014 '#type' => 'fieldset', |
sly@2
|
1015 '#title' => t('Not-in-channel ad rules'), |
sly@2
|
1016 '#collapsible' => TRUE, |
sly@2
|
1017 '#collapsed' => $collapsed, |
sly@2
|
1018 ); |
sly@2
|
1019 if ($collapsed) { |
sly@2
|
1020 $form['nonchannel_rules']['info'] = array( |
sly@2
|
1021 '#type' => 'markup', |
sly@2
|
1022 '#prefix' => '<div>', |
sly@2
|
1023 '#suffix' => '</div>', |
sly@2
|
1024 '#value' => t('You will need to set "!variable" to %value for these %type ad rules to be applicable. Currently these settings are doing nothing.', array('!variable' => l(t('display advertisements not assigned to any channel'), 'admin/content/ad/channel/settings'), '%value' => t('always'), '%type' => 'not-in-channel')), |
sly@2
|
1025 ); |
sly@2
|
1026 } |
sly@2
|
1027 $form['nonchannel_rules']['no_channel_weight'] = array( |
sly@2
|
1028 '#type' => 'select', |
sly@2
|
1029 '#title' => t('Probability'), |
sly@2
|
1030 '#options' => _ad_channel_probabilities(), |
sly@2
|
1031 '#default_value' => $channel->no_channel_weight ? $channel->no_channel_weight : 100, |
sly@2
|
1032 '#description' => t('The greater the probability, the more frequently advertisements that aren\'t in any channel will be displayed along with ads in this channel. For example, if set to 2, advertisements without any channel will be displayed twice as often as advertisements assigned to this channel. If set to 1/2, advertisements without any channel will be displayed half as often as advertisements assigned to this channel. If set to 1, advertisements without any channel will be displayed as often as advertisements assigned to this channel.'), |
sly@2
|
1033 ); |
sly@2
|
1034 |
pierre@1
|
1035 if ($chid) { |
pierre@1
|
1036 $form['update'] = array( |
pierre@1
|
1037 '#type' => 'submit', |
pierre@1
|
1038 '#value' => t('Update'), |
pierre@1
|
1039 ); |
pierre@1
|
1040 $form['delete'] = array( |
pierre@1
|
1041 '#type' => 'submit', |
pierre@1
|
1042 '#value' => t('Delete'), |
pierre@1
|
1043 ); |
pierre@1
|
1044 } |
pierre@1
|
1045 else { |
pierre@1
|
1046 $form['submit'] = array( |
pierre@1
|
1047 '#type' => 'submit', |
pierre@1
|
1048 '#value' => t('Create'), |
pierre@1
|
1049 ); |
pierre@1
|
1050 } |
pierre@1
|
1051 $form['cancel'] = array( |
pierre@1
|
1052 '#type' => 'markup', |
pierre@1
|
1053 '#value' => l(t('Cancel'), 'admin/content/ad/channel'), |
pierre@1
|
1054 ); |
pierre@1
|
1055 |
pierre@1
|
1056 return $form; |
pierre@1
|
1057 } |
pierre@1
|
1058 |
pierre@1
|
1059 /** |
pierre@1
|
1060 * Validate the channel. |
pierre@1
|
1061 */ |
pierre@1
|
1062 function ad_channel_admin_channel_validate($form, &$form_state) { |
pierre@1
|
1063 $chid = 0; |
pierre@1
|
1064 if ($form_state['values']['op'] == t('Create')) { |
pierre@1
|
1065 $chid = db_result(db_query("SELECT chid FROM {ad_channel} WHERE name = '%s'", $form_state['values']['name'])); |
pierre@1
|
1066 } |
pierre@1
|
1067 else if ($form_state['values']['op'] == t('Update')) { |
pierre@1
|
1068 $chid = db_result(db_query("SELECT chid FROM {ad_channel} WHERE name = '%s' AND chid != %d", $form_state['values']['name'], $form_state['values']['chid'])); |
pierre@1
|
1069 } |
pierre@1
|
1070 if ($chid) { |
pierre@1
|
1071 form_set_error('name', t('A channel named %name already exists.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
1072 } |
pierre@1
|
1073 } |
pierre@1
|
1074 |
pierre@1
|
1075 /** |
pierre@1
|
1076 * Save the channel. |
pierre@1
|
1077 */ |
pierre@1
|
1078 function ad_channel_admin_channel_submit($form, &$form_state) { |
sly@2
|
1079 // remove extraneous white space from url list which can break matching |
sly@2
|
1080 $url_array = explode("\n", $form_state['values']['urls']); |
sly@2
|
1081 if (is_array($url_array)) { |
sly@2
|
1082 foreach ($url_array as $url) { |
sly@2
|
1083 $urls[] = trim($url); |
sly@2
|
1084 } |
sly@2
|
1085 $urls = implode("\n", $urls); |
sly@2
|
1086 } |
sly@2
|
1087 else { |
sly@2
|
1088 $urls = ''; |
sly@2
|
1089 } |
pierre@1
|
1090 switch ($form_state['values']['op']) { |
pierre@1
|
1091 case t('Create'): |
sly@2
|
1092 db_query("INSERT INTO {ad_channel} (name, description, conid, weight, display, urls, groups, no_channel_weight) VALUES('%s', '%s', %d, %d, %d, '%s', '%s', %d)", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['conid'], $form_state['values']['weight'], $form_state['values']['display'], serialize($urls), serialize($form_state['values']['groups']), $form_state['values']['no_channel_weight']); |
pierre@1
|
1093 drupal_set_message(t('The %name channel has been created.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
1094 break; |
pierre@1
|
1095 case t('Update'): |
pierre@1
|
1096 $groups = array(); |
pierre@1
|
1097 // Don't store information about groups that no longer exist. |
pierre@1
|
1098 if (is_array($form_state['values']['groups'])) { |
pierre@1
|
1099 $ad_groups = taxonomy_get_tree(_ad_get_vid()); |
pierre@1
|
1100 foreach ($ad_groups as $ad_group) { |
pierre@1
|
1101 if (isset($form_state['values']['groups'][$ad_group->tid])) { |
pierre@1
|
1102 $groups[$ad_group->tid] = $form_state['values']['groups'][$ad_group->tid]; |
pierre@1
|
1103 } |
pierre@1
|
1104 } |
pierre@1
|
1105 } |
sly@2
|
1106 db_query("UPDATE {ad_channel} SET name = '%s', description = '%s', conid = %d, weight = %d, display = %d, urls = '%s', groups = '%s', no_channel_weight = %d WHERE chid = %d", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['conid'], $form_state['values']['weight'], $form_state['values']['display'], serialize($urls), serialize($groups), $form_state['values']['no_channel_weight'], $form_state['values']['chid']); |
pierre@1
|
1107 drupal_set_message(t('The %name channel has been updated.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
1108 break; |
pierre@1
|
1109 case t('Delete'): |
pierre@1
|
1110 drupal_goto('admin/content/ad/channel/channel/'. $form_state['values']['chid'] .'/delete'); |
pierre@1
|
1111 } |
pierre@1
|
1112 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
1113 } |
pierre@1
|
1114 |
pierre@1
|
1115 /** |
pierre@1
|
1116 * Confirm whether or not to delete container, and the contained channels. |
pierre@1
|
1117 */ |
sly@2
|
1118 function ad_channel_admin_confirm_delete_channel($form_state, $channel) { |
pierre@1
|
1119 $form = array(); |
pierre@1
|
1120 |
pierre@1
|
1121 $form['chid'] = array( |
pierre@1
|
1122 '#type' => 'value', |
sly@2
|
1123 '#value' => $channel->chid, |
pierre@1
|
1124 ); |
pierre@1
|
1125 |
pierre@1
|
1126 return confirm_form( |
pierre@1
|
1127 $form, |
pierre@1
|
1128 t('Are you sure you want to delete the %name channel?', array('%name' => $channel->name)), |
pierre@1
|
1129 'admin/content/ad/channel', |
pierre@1
|
1130 t('Any advertisements currently assigned to the %name channel will not be deleted, they will be reassigned. <p>This action can not be undone.', array('%name' => $channel->name)), |
pierre@1
|
1131 t('Delete'), |
pierre@1
|
1132 t('Cancel')); |
pierre@1
|
1133 } |
pierre@1
|
1134 |
pierre@1
|
1135 /** |
pierre@1
|
1136 * Delete a channel. |
pierre@1
|
1137 */ |
pierre@1
|
1138 function ad_channel_admin_confirm_delete_channel_submit($form, &$form_state) { |
pierre@1
|
1139 $channel = _ad_channel_get_channels($form_state['values']['chid']); |
pierre@1
|
1140 if ($channel->chid) { |
pierre@1
|
1141 db_query('DELETE FROM {ad_channel} WHERE chid = %d', $channel->chid); |
pierre@1
|
1142 drupal_set_message(t('The %name channel has been deleted.', array('%name' => $channel->name))); |
pierre@1
|
1143 } |
pierre@1
|
1144 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
1145 } |
pierre@1
|
1146 |
sly@2
|
1147 /** |
sly@2
|
1148 * Available probabilities. |
sly@2
|
1149 */ |
sly@2
|
1150 function _ad_channel_probabilities() { |
sly@2
|
1151 return array( |
sly@2
|
1152 25 => t('1/4'), |
sly@2
|
1153 33 => t('1/3'), |
sly@2
|
1154 50 => t('1/2'), |
sly@2
|
1155 100 => t('1'), |
sly@2
|
1156 200 => t('2'), |
sly@2
|
1157 300 => t('3'), |
sly@2
|
1158 400 => t('4'), |
sly@2
|
1159 ); |
sly@2
|
1160 } |