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