pierre@1
|
1 <?php |
sly@8
|
2 // $Id: ad_channel.module,v 1.1.4.27 2009/07/29 23:00:39 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']; |
piotre@7
|
145 $default_value = isset($node->channel[$channel->chid]); |
pierre@1
|
146 } |
pierre@1
|
147 else { |
piotre@7
|
148 $default_value = 0; |
piotre@7
|
149 } |
piotre@7
|
150 if (!$default_value) { |
piotre@7
|
151 $channel = _ad_channel_get_channels($channel->chid); |
piotre@7
|
152 if (isset($channel->inventory) && $channel->inventory > 0) { |
piotre@7
|
153 $num_ads = _ad_channel_get_channel_active_ad_count($channel->chid); |
piotre@7
|
154 if($num_ads + 1 > $channel->inventory) { |
piotre@7
|
155 $disabled = TRUE; |
piotre@7
|
156 } |
piotre@7
|
157 } |
pierre@1
|
158 } |
pierre@1
|
159 $form['channel'][$container->conid]["channel-$channel->chid"] = array( |
pierre@1
|
160 '#type' => 'checkbox', |
pierre@1
|
161 '#title' => $channel->name, |
pierre@1
|
162 '#description' => $channel->description, |
piotre@7
|
163 '#default_value' => $default_value, |
piotre@7
|
164 '#disabled' => isset($disabled) ? $disabled : FALSE, |
pierre@1
|
165 ); |
pierre@1
|
166 } |
pierre@1
|
167 } |
pierre@1
|
168 } |
pierre@1
|
169 $node = node_load($form['nid']['#value']); |
pierre@1
|
170 if (isset($form['channel']) && is_array($form['channel']) && !empty($form['channel'])) { |
pierre@1
|
171 $form['channel'] += array( |
pierre@1
|
172 '#type' => 'fieldset', |
pierre@1
|
173 '#title' => t('Channels'), |
pierre@1
|
174 '#collapsible' => TRUE, |
pierre@1
|
175 '#collapsed' => FALSE, |
pierre@1
|
176 ); |
pierre@1
|
177 $form['channel']['#weight'] = -2; |
pierre@1
|
178 $form['channel']['#tree'] = TRUE; |
pierre@1
|
179 } |
pierre@1
|
180 $form['priority'] = array( |
pierre@1
|
181 '#type' => 'fieldset', |
sly@2
|
182 '#access' => user_access('configure ad premier status'), |
pierre@1
|
183 '#title' => t('Priority'), |
pierre@1
|
184 '#collapsible' => TRUE, |
pierre@1
|
185 '#collapsed' => FALSE, |
pierre@1
|
186 ); |
pierre@1
|
187 $form['priority']['premiere'] = array( |
pierre@1
|
188 '#type' => 'checkbox', |
sly@2
|
189 '#access' => user_access('configure ad premier status'), |
pierre@1
|
190 '#title' => t('Premiere'), |
pierre@1
|
191 '#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
|
192 '#default_value' => isset($node->premiere) ? $node->premiere : FALSE, |
pierre@1
|
193 ); |
pierre@1
|
194 $form['priority']['#weight'] = -1; |
piotre@7
|
195 |
piotre@7
|
196 $form['inventory'] = array( |
piotre@7
|
197 '#type' => 'fieldset', |
piotre@7
|
198 '#title' => t('Inventory'), |
piotre@7
|
199 '#collapsible' => TRUE, |
piotre@7
|
200 '#collapsed' => FALSE, |
piotre@7
|
201 ); |
piotre@7
|
202 |
piotre@7
|
203 $form['inventory']['remnant'] = array( |
piotre@7
|
204 '#type' => 'checkbox', |
piotre@7
|
205 '#title' => t('Remnant'), |
piotre@7
|
206 '#description' => t('An advertisement marked as a remnant is eligible for display in any channel where the number of assigned advertisements for the channel is less than the inventory level specified.'), |
piotre@7
|
207 '#default_value' => isset($node->remnant) ? $node->remnant : FALSE, |
piotre@7
|
208 ); |
pierre@1
|
209 } |
pierre@1
|
210 else if ($form_id == 'ad_filter_form') { |
pierre@1
|
211 $session = &$_SESSION['ad_overview_filter']; |
pierre@1
|
212 $session = is_array($session) ? $session : array(); |
pierre@1
|
213 |
pierre@1
|
214 $display_channel = TRUE; |
pierre@1
|
215 $display_premiere = TRUE; |
pierre@1
|
216 foreach ($session as $filter) { |
pierre@1
|
217 list($type, $value) = $filter; |
pierre@1
|
218 if ($type == 'channel') { |
pierre@1
|
219 $display_channel = FALSE; |
pierre@1
|
220 } |
pierre@1
|
221 else if ($type == 'premiere') { |
pierre@1
|
222 $display_premiere = FALSE; |
pierre@1
|
223 } |
pierre@1
|
224 } |
pierre@1
|
225 |
pierre@1
|
226 if ($display_channel) { |
pierre@1
|
227 $channels = _ad_channel_get_channels(); |
pierre@1
|
228 $options = array(); |
pierre@1
|
229 foreach ($channels as $channel) { |
pierre@1
|
230 $key = 'channel-'. $channel->chid; |
pierre@1
|
231 $options[$key] = $channel->name; |
pierre@1
|
232 } |
pierre@1
|
233 $form['filters']['status']['channel'] = array( |
pierre@1
|
234 '#type' => 'select', |
pierre@1
|
235 '#options' => $options |
pierre@1
|
236 ); |
pierre@1
|
237 $form['filters']['filter']['#options']['channel'] = 'channel'; |
pierre@1
|
238 } |
pierre@1
|
239 else { |
pierre@1
|
240 unset($form['filters']['status']['channel']); |
pierre@1
|
241 unset($form['filters']['filter']['#options']['channel']); |
pierre@1
|
242 } |
pierre@1
|
243 |
pierre@1
|
244 if ($display_premiere) { |
pierre@1
|
245 $options = array( |
pierre@1
|
246 '0' => t('false'), |
pierre@1
|
247 '1' => t('true')); |
pierre@1
|
248 $form['filters']['status']['premiere'] = array( |
pierre@1
|
249 '#type' => 'select', |
pierre@1
|
250 '#options' => $options |
pierre@1
|
251 ); |
pierre@1
|
252 $form['filters']['filter']['#options']['premiere'] = 'premiere'; |
pierre@1
|
253 } |
pierre@1
|
254 else { |
pierre@1
|
255 unset($form['filters']['status']['premiere']); |
pierre@1
|
256 unset($form['filters']['filter']['#options']['premiere']); |
pierre@1
|
257 } |
pierre@1
|
258 } |
pierre@1
|
259 else if ($form_id == 'ad_report_admin') { |
pierre@1
|
260 $channels = _ad_channel_get_channels(); |
pierre@1
|
261 if (is_array($channels) && !empty($channels)) { |
pierre@1
|
262 $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
|
263 $form['channels'] = array( |
pierre@1
|
264 '#type' => 'fieldset', |
pierre@1
|
265 '#title' => t('Channels'), |
pierre@1
|
266 ); |
pierre@1
|
267 $options = array(); |
piotre@7
|
268 $options['any'] = t('<any>'); |
piotre@7
|
269 $options['none'] = t('<none>'); |
pierre@1
|
270 foreach ($channels as $chan) { |
pierre@1
|
271 $options[$chan->chid] = $chan->name; |
pierre@1
|
272 } |
pierre@1
|
273 $form['channels']['channel'] = array( |
pierre@1
|
274 '#type' => 'select', |
pierre@1
|
275 '#title' => t('Ad channels'), |
pierre@1
|
276 '#options' => $options, |
pierre@1
|
277 '#multiple' => TRUE, |
pierre@1
|
278 '#required' => TRUE, |
pierre@1
|
279 '#default_value' => $channel, |
pierre@1
|
280 ); |
pierre@1
|
281 $form['#submit'] = array_merge(array('ad_channel_admin_report_submit'), $form['#submit']); |
pierre@1
|
282 } |
pierre@1
|
283 } |
pierre@1
|
284 else if ($form_id == 'ad_admin_ads' && is_array($form['group'])) { |
pierre@1
|
285 foreach ($form['group'] as $aid => $value) { |
pierre@1
|
286 $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $aid); |
pierre@1
|
287 $names = array(); |
pierre@1
|
288 while ($channel = db_fetch_object($result)) { |
pierre@1
|
289 $names[] = _ad_channel_get_name($channel->chid); |
pierre@1
|
290 } |
pierre@1
|
291 if (empty($names)) { |
pierre@1
|
292 $names[] = t('none'); |
pierre@1
|
293 } |
pierre@1
|
294 $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); |
pierre@1
|
295 switch ($list) { |
pierre@1
|
296 case AD_CHANNEL_LIST_CHANNEL: |
pierre@1
|
297 unset($form['group']); |
pierre@1
|
298 $form['channel'][$aid]['#value'] = implode(', ', $names); |
pierre@1
|
299 break; |
pierre@1
|
300 case AD_CHANNEL_LIST_BOTH: |
pierre@1
|
301 $form['channel'][$aid]['#value'] = implode(', ', $names); |
pierre@1
|
302 break; |
pierre@1
|
303 case AD_CHANNEL_LIST_GROUP: |
pierre@1
|
304 // do nothing |
pierre@1
|
305 break; |
pierre@1
|
306 } |
pierre@1
|
307 } |
pierre@1
|
308 } |
pierre@1
|
309 } |
pierre@1
|
310 |
pierre@1
|
311 /** |
pierre@1
|
312 * Register our own ad_admin_ads theme function. |
pierre@1
|
313 */ |
pierre@1
|
314 function ad_channel_theme_registry_alter(&$theme_registry) { |
pierre@1
|
315 $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); |
pierre@1
|
316 if ($list == AD_CHANNEL_LIST_CHANNEL || $list == AD_CHANNEL_LIST_BOTH) { |
pierre@1
|
317 if (!empty($theme_registry['ad_admin_ads'])) { |
pierre@1
|
318 $theme_registry['ad_admin_ads']['function'] = 'ad_channel_ad_admin_ads'; |
pierre@1
|
319 } |
pierre@1
|
320 } |
pierre@1
|
321 } |
pierre@1
|
322 |
pierre@1
|
323 /** |
pierre@1
|
324 * Implement custom theme function for displaying ad overview, replacing groups |
pierre@1
|
325 * with channels. |
pierre@1
|
326 */ |
pierre@1
|
327 function ad_channel_ad_admin_ads($form) { |
pierre@1
|
328 $list = variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL); |
pierre@1
|
329 |
piotre@7
|
330 // Conditionally build the header based on list type and |
piotre@7
|
331 // the availability of probability values from the |
piotre@7
|
332 // ad_weight_probability module |
piotre@7
|
333 $header = array(theme('table_select_header_cell'), t('Title')); |
piotre@7
|
334 |
pierre@1
|
335 // Overview table: |
piotre@7
|
336 if ($list != AD_CHANNEL_LIST_CHANNEL) { |
piotre@7
|
337 $header[] = t('Group'); |
pierre@1
|
338 } |
piotre@7
|
339 |
piotre@7
|
340 $header = array_merge($header, array(t('Channel'))); |
piotre@7
|
341 |
piotre@7
|
342 if (isset($form['probability']) && is_array($form['probability'])) { |
piotre@7
|
343 $header[] = t('Probability'); |
pierre@1
|
344 } |
pierre@1
|
345 |
piotre@7
|
346 $header[] = t('Type'); |
piotre@7
|
347 $header[] = t('Status'); |
piotre@7
|
348 $header[] = t('Operations'); |
piotre@7
|
349 |
pierre@1
|
350 $output = drupal_render($form['options']); |
pierre@1
|
351 if (isset($form['title']) && is_array($form['title'])) { |
pierre@1
|
352 foreach (element_children($form['title']) as $key) { |
pierre@1
|
353 $row = array(); |
pierre@1
|
354 $row[] = drupal_render($form['ads'][$key]); |
pierre@1
|
355 $row[] = drupal_render($form['title'][$key]); |
pierre@1
|
356 if ($list == AD_CHANNEL_LIST_BOTH) { |
pierre@1
|
357 $row[] = drupal_render($form['group'][$key]); |
pierre@1
|
358 } |
pierre@1
|
359 $row[] = drupal_render($form['channel'][$key]); |
piotre@7
|
360 if (isset($form['probability']) && is_array($form['probability'])) { |
piotre@7
|
361 $row[] = drupal_render($form['probability'][$key]); |
piotre@7
|
362 } |
pierre@1
|
363 $row[] = drupal_render($form['adtype'][$key]); |
pierre@1
|
364 $row[] = drupal_render($form['adstatus'][$key]); |
pierre@1
|
365 $row[] = drupal_render($form['operations'][$key]); |
pierre@1
|
366 $rows[] = $row; |
pierre@1
|
367 } |
pierre@1
|
368 |
pierre@1
|
369 } |
pierre@1
|
370 else { |
pierre@1
|
371 $rows[] = array(array('data' => t('No ads available.'), 'colspan' => '6')); |
pierre@1
|
372 } |
pierre@1
|
373 |
pierre@1
|
374 $output .= theme('table', $header, $rows); |
pierre@1
|
375 if ($form['pager']['#value']) { |
pierre@1
|
376 $output .= drupal_render($form['pager']); |
pierre@1
|
377 } |
pierre@1
|
378 |
pierre@1
|
379 $output .= drupal_render($form); |
pierre@1
|
380 |
pierre@1
|
381 return $output; |
pierre@1
|
382 } |
pierre@1
|
383 |
pierre@1
|
384 function _ad_channel_get_name($chid) { |
pierre@1
|
385 static $names = array(); |
pierre@1
|
386 if (!isset($names[$chid])) { |
pierre@1
|
387 $names[$chid] = db_result(db_query('SELECT name FROM {ad_channel} WHERE chid = %d', $chid)); |
pierre@1
|
388 } |
pierre@1
|
389 return $names[$chid]; |
pierre@1
|
390 } |
pierre@1
|
391 |
pierre@1
|
392 function ad_channel_admin_report_submit($form, $form_state) { |
pierre@1
|
393 if ($form_state['clicked_button']['#value'] == t('Reset report')) { |
pierre@1
|
394 unset($_SESSION['ad_report_channel']); |
pierre@1
|
395 } |
pierre@1
|
396 else if ($form_state['clicked_button']['#value'] == t('Generate report')) { |
pierre@1
|
397 if (isset($form_state['values']['channel']) && is_array($form_state['values']['channel'])) { |
pierre@1
|
398 $channels = array(); |
pierre@1
|
399 $any = FALSE; |
pierre@1
|
400 foreach ($form_state['values']['channel'] as $chid) { |
pierre@1
|
401 if (is_numeric($chid)) { |
pierre@1
|
402 $channels[] = $chid; |
pierre@1
|
403 } |
piotre@7
|
404 else if ($chid == 'none') { |
piotre@7
|
405 $channels[-1] = 'none'; |
piotre@7
|
406 } |
pierre@1
|
407 else { |
pierre@1
|
408 $any = TRUE; |
pierre@1
|
409 } |
pierre@1
|
410 } |
pierre@1
|
411 if (!$any && !empty($channels)) { |
pierre@1
|
412 $_SESSION['ad_report_channel'] = $channels; |
pierre@1
|
413 } |
pierre@1
|
414 else { |
pierre@1
|
415 if (isset($_SESSION['ad_report_channel'])) { |
pierre@1
|
416 unset($_SESSION['ad_report_channel']); |
pierre@1
|
417 } |
pierre@1
|
418 } |
pierre@1
|
419 } |
pierre@1
|
420 } |
pierre@1
|
421 } |
pierre@1
|
422 |
pierre@1
|
423 /** |
pierre@1
|
424 * Filter reports by selected channel. |
pierre@1
|
425 */ |
pierre@1
|
426 function ad_channel_adreport($join, $where, $args, $select) { |
pierre@1
|
427 if (isset($_SESSION['ad_report_channel']) && is_array($_SESSION['ad_report_channel']) && !empty($_SESSION['ad_report_channel'])) { |
piotre@7
|
428 // Check if we're filtering by <none> |
piotre@7
|
429 if (isset($_SESSION['ad_report_channel'][-1])) { |
piotre@7
|
430 $join = array('LEFT JOIN {ad_channel_node} acn ON acn.nid = a.aid'); |
piotre@7
|
431 $channels = $_SESSION['ad_report_channel']; |
piotre@7
|
432 unset($channels[-1]); |
piotre@7
|
433 // Check if we're filtering on <none> and one or more channels |
piotre@7
|
434 if (count($channels)) { |
piotre@7
|
435 $where = array('(ISNULL(acn.chid) OR acn.chid IN (%s))'); |
piotre@7
|
436 $args = array(implode(',', $channels)); |
piotre@7
|
437 } |
piotre@7
|
438 else { |
piotre@7
|
439 $where = array('ISNULL(acn.chid)'); |
piotre@7
|
440 } |
piotre@7
|
441 return array('join' => $join, 'where' => $where, 'args' => $args); |
piotre@7
|
442 } |
piotre@7
|
443 // Filtering by one or more channels |
piotre@7
|
444 else { |
piotre@7
|
445 $join = array('LEFT JOIN {ad_channel_node} acn ON acn.nid = a.aid'); |
piotre@7
|
446 $where = array('acn.chid IN (%s)'); |
piotre@7
|
447 $args = array(implode(',', $_SESSION['ad_report_channel'])); |
piotre@7
|
448 return array('join' => $join, 'where' => $where, 'args' => $args); |
piotre@7
|
449 } |
pierre@1
|
450 } |
pierre@1
|
451 } |
pierre@1
|
452 |
pierre@1
|
453 /** |
pierre@1
|
454 * Implement hook _adapi. |
pierre@1
|
455 */ |
pierre@1
|
456 function ad_channel_adapi($op, &$node) { |
pierre@1
|
457 switch ($op) { |
pierre@1
|
458 case 'admin_filters': |
pierre@1
|
459 $channels = _ad_channel_get_channels(); |
pierre@1
|
460 $options = array(); |
pierre@1
|
461 foreach ($channels as $channel) { |
pierre@1
|
462 $key = 'channel-'. $channel->chid; |
pierre@1
|
463 $options[$key] = $channel->name; |
pierre@1
|
464 } |
pierre@1
|
465 $filters['channel'] = array( |
pierre@1
|
466 'title' => t('channel'), |
pierre@1
|
467 'options' => $options, |
pierre@1
|
468 ); |
pierre@1
|
469 $options = array( |
pierre@1
|
470 '0' => t('false'), |
pierre@1
|
471 '1' => t('true')); |
pierre@1
|
472 $filters['premiere'] = array( |
pierre@1
|
473 'title' => t('premiere'), |
pierre@1
|
474 'options' => $options, |
pierre@1
|
475 ); |
piotre@7
|
476 $filters['remnant'] = array( |
piotre@7
|
477 'title' => t('remnant'), |
piotre@7
|
478 'options' => $options, |
piotre@7
|
479 ); |
pierre@1
|
480 return $filters; |
pierre@1
|
481 case 'admin_filter_query': |
pierre@1
|
482 if (is_array($node)) { |
pierre@1
|
483 list($key, $value) = $node; |
pierre@1
|
484 if ($key == 'channel') { |
pierre@1
|
485 list($key, $value) = explode('-', $value, 2); |
pierre@1
|
486 return array( |
pierre@1
|
487 'channel' => array( |
pierre@1
|
488 'where' => 'c.chid = %d', |
pierre@1
|
489 'join' => 'INNER JOIN {ad_channel_node} c ON n.nid = c.nid ', |
pierre@1
|
490 'value' => $value, |
pierre@1
|
491 )); |
pierre@1
|
492 } |
pierre@1
|
493 else if ($key == 'premiere') { |
pierre@1
|
494 return array( |
pierre@1
|
495 'premiere' => array( |
pierre@1
|
496 'where' => 'p.priority = %d', |
pierre@1
|
497 'join' => 'INNER JOIN {ad_priority} p ON n.nid = p.aid ', |
pierre@1
|
498 'value' => $value, |
pierre@1
|
499 )); |
pierre@1
|
500 } |
piotre@7
|
501 else if ($key == 'remnant') { |
piotre@7
|
502 return array( |
piotre@7
|
503 'remnant' => array( |
piotre@7
|
504 'where' => 'r.remnant = %d', |
piotre@7
|
505 'join' => 'INNER JOIN {ad_channel_remnant} r ON n.nid = r.aid ', |
piotre@7
|
506 'value' => $value, |
piotre@7
|
507 )); |
piotre@7
|
508 } |
pierre@1
|
509 } |
pierre@1
|
510 break; |
pierre@1
|
511 } |
pierre@1
|
512 } |
pierre@1
|
513 |
pierre@1
|
514 /** |
pierre@1
|
515 * Implementation of hook_nodeapi(). |
pierre@1
|
516 */ |
pierre@1
|
517 function ad_channel_nodeapi($node, $op, $arg = 0) { |
pierre@1
|
518 switch ($op) { |
pierre@1
|
519 case 'view': |
pierre@1
|
520 return _ad_channel_view_node($node); |
pierre@1
|
521 case 'load': |
pierre@1
|
522 return _ad_channel_load_node($node); |
pierre@1
|
523 case 'insert': |
pierre@1
|
524 case 'update': |
piotre@7
|
525 if (is_object($node) && isset($node->adtype) && isset($node->nid)) { |
pierre@1
|
526 return _ad_channel_save_node($node); |
pierre@1
|
527 } |
sly@4
|
528 break; |
pierre@1
|
529 case 'delete': |
pierre@1
|
530 return _ad_channel_delete_node($node); |
pierre@1
|
531 case 'validate': |
pierre@1
|
532 return _ad_channel_validate_nodes($node); |
pierre@1
|
533 } |
pierre@1
|
534 } |
pierre@1
|
535 |
pierre@1
|
536 /** |
pierre@1
|
537 * Implementation of hook_ad_build_cache(). |
pierre@1
|
538 */ |
pierre@1
|
539 function ad_channel_ad_build_cache() { |
pierre@1
|
540 $cache = array(); |
pierre@1
|
541 $ads = array(); |
pierre@1
|
542 $active = db_query("SELECT aid FROM {ads} WHERE adstatus = 'active'"); |
pierre@1
|
543 while ($ad = db_fetch_object($active)) { |
pierre@1
|
544 // cache channel<->node relation |
pierre@1
|
545 $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $ad->aid); |
pierre@1
|
546 while ($channel = db_fetch_object($result)) { |
pierre@1
|
547 $ads[$ad->aid][$channel->chid] = $channel->chid; |
pierre@1
|
548 //$ads[$channel->chid][$ad->aid] = $ad->aid; |
pierre@1
|
549 } |
pierre@1
|
550 } |
pierre@1
|
551 $channels = array(); |
piotre@7
|
552 $result = db_query('SELECT chid, display, urls, no_channel_percent, inventory FROM {ad_channel}'); |
pierre@1
|
553 while ($channel = db_fetch_object($result)) { |
pierre@1
|
554 $channels[$channel->chid] = $channel; |
pierre@1
|
555 } |
pierre@1
|
556 $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
|
557 $premiere = array(); |
pierre@1
|
558 while ($priority = db_fetch_object($result)) { |
pierre@1
|
559 $premiere[$priority->aid] = $priority->aid; |
pierre@1
|
560 } |
piotre@7
|
561 $result = db_query("SELECT r.aid, r.remnant FROM {ads} a LEFT JOIN {ad_channel_remnant} r ON a.aid = r.aid WHERE a.adstatus = 'active' AND r.remnant = 1"); |
piotre@7
|
562 $remnants = array(); |
piotre@7
|
563 while ($remnant = db_fetch_object($result)) { |
piotre@7
|
564 $remnants[$remnant->aid] = $remnant->aid; |
piotre@7
|
565 } |
pierre@1
|
566 $cache['channel']['ads'] = $ads; |
pierre@1
|
567 $cache['channel']['channels'] = $channels; |
pierre@1
|
568 $cache['channel']['display'] = variable_get('ad_channel_display', 0); |
pierre@1
|
569 $cache['premiere'] = $premiere; |
piotre@7
|
570 $cache['remnant'] = $remnants; |
pierre@1
|
571 |
pierre@1
|
572 $cache['channel']['hook_filter'] = array( |
pierre@1
|
573 'weight' => 0, |
pierre@1
|
574 'file' => drupal_get_path('module', 'ad_channel') .'/ad_channel.inc', |
pierre@1
|
575 'function' => 'ad_channel_cache_filter', |
pierre@1
|
576 ); |
pierre@1
|
577 |
pierre@1
|
578 return $cache; |
pierre@1
|
579 } |
pierre@1
|
580 |
pierre@1
|
581 /***/ |
pierre@1
|
582 |
pierre@1
|
583 /** |
pierre@1
|
584 * Settings form. |
pierre@1
|
585 */ |
pierre@1
|
586 function ad_channel_admin_settings() { |
pierre@1
|
587 $form = array(); |
pierre@1
|
588 |
pierre@1
|
589 $form['ad_channel_display'] = array( |
pierre@1
|
590 '#type' => 'radios', |
pierre@1
|
591 '#title' => t('Display advertisements not assigned to any channel'), |
pierre@1
|
592 '#options' => array(t('Only if no matching advertisements are found in the active channels'), t('Always'), t('Never')), |
pierre@1
|
593 '#default_value' => variable_get('ad_channel_display', 0), |
pierre@1
|
594 '#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
|
595 ); |
pierre@1
|
596 $form['ad_channel_admin_list'] = array( |
pierre@1
|
597 '#type' => 'radios', |
pierre@1
|
598 '#title' => t('Display channels on administrative ads overview listing'), |
pierre@1
|
599 '#options' => array(t('In addition to groups'), t('In place of groups'), t('Not at all')), |
pierre@1
|
600 '#default_value' => variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL), |
pierre@1
|
601 '#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
|
602 ); |
sly@2
|
603 $options = array(0 => t('No limit')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)); |
sly@2
|
604 $form['ad_channel_ad_limit'] = array( |
sly@2
|
605 '#type' => 'select', |
sly@2
|
606 '#title' => t('Maximum number of channels that can be assigned to a single ad'), |
sly@2
|
607 '#options' => $options, |
sly@2
|
608 '#default_value' => variable_get('ad_channel_ad_limit', 0), |
sly@2
|
609 '#description' => t('Optionally limit the number of channels that a single advertisement can be assigned to.'), |
sly@2
|
610 ); |
pierre@1
|
611 |
pierre@1
|
612 return system_settings_form($form); |
pierre@1
|
613 } |
pierre@1
|
614 |
pierre@1
|
615 /** |
pierre@1
|
616 * Add channel information when viewing node. |
pierre@1
|
617 */ |
pierre@1
|
618 function _ad_channel_view_node($node) { |
pierre@1
|
619 if (isset($node->adtype) && user_access('administer channels')) { |
pierre@1
|
620 if (isset($node->channel) && is_array($node->channel) && !empty($node->channel)) { |
pierre@1
|
621 $channels = array(); |
pierre@1
|
622 foreach ($node->channel as $chid) { |
pierre@1
|
623 $channel = _ad_channel_get_channels($chid); |
pierre@1
|
624 $channels[] = $channel->name; |
pierre@1
|
625 } |
pierre@1
|
626 $node->content['channel'] = array( |
pierre@1
|
627 '#value' => theme('box', t('Channels'), theme('item_list', $channels)), |
pierre@1
|
628 '#weight' => 1, |
pierre@1
|
629 ); |
pierre@1
|
630 } |
pierre@1
|
631 if (isset($node->premiere)) { |
pierre@1
|
632 if (isset($node->premiere) && $node->premiere == 1) { |
pierre@1
|
633 $output = t('This is a premiere advertisement.'); |
pierre@1
|
634 } |
pierre@1
|
635 else { |
pierre@1
|
636 $output = t('This is not a premiere advertisement.'); |
pierre@1
|
637 } |
pierre@1
|
638 $node->content['premiere'] = array( |
pierre@1
|
639 '#value' => theme('box', t('Premiere'), $output), |
pierre@1
|
640 '#weight' => 1, |
pierre@1
|
641 ); |
pierre@1
|
642 } |
piotre@7
|
643 if (isset($node->remnant)) { |
piotre@7
|
644 if (isset($node->remnant) && $node->remnant == 1) { |
piotre@7
|
645 $output = t('This is a remnant advertisement.'); |
piotre@7
|
646 $node->content['remnant'] = array( |
piotre@7
|
647 '#value' => theme('box', t('Remnant'), $output), |
piotre@7
|
648 '#weight' => 1, |
piotre@7
|
649 ); |
piotre@7
|
650 } |
piotre@7
|
651 } |
pierre@1
|
652 } |
pierre@1
|
653 } |
pierre@1
|
654 |
pierre@1
|
655 /** |
pierre@1
|
656 * Load channels associated with specified node. |
pierre@1
|
657 */ |
pierre@1
|
658 function _ad_channel_load_node($node) { |
pierre@1
|
659 $result = db_query('SELECT chid FROM {ad_channel_node} WHERE nid = %d', $node->nid); |
pierre@1
|
660 $output['channel'] = array(); |
pierre@1
|
661 while ($chid = db_fetch_object($result)) { |
pierre@1
|
662 $output['channel'][$chid->chid] = $chid->chid; |
pierre@1
|
663 } |
pierre@1
|
664 // currently 0 or 1, with one being a 'premiere' advertisement. |
pierre@1
|
665 $output['premiere'] = (int)db_result(db_query('SELECT priority FROM {ad_priority} WHERE aid = %d', $node->nid)); |
piotre@7
|
666 $output['remnant'] = (int)db_result(db_query('SELECT remnant FROM {ad_channel_remnant} WHERE aid = %d', $node->nid)); |
pierre@1
|
667 return $output; |
pierre@1
|
668 } |
pierre@1
|
669 |
pierre@1
|
670 /** |
pierre@1
|
671 * Save channels associated with added or updated node. |
pierre@1
|
672 */ |
pierre@1
|
673 function _ad_channel_save_node($node) { |
pierre@1
|
674 // delete old channel information, then add new |
pierre@1
|
675 db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid); |
pierre@1
|
676 $channels = _ad_channel_get_enabled($node); |
pierre@1
|
677 foreach ($channels as $chid) { |
pierre@1
|
678 db_query('INSERT INTO {ad_channel_node} (chid, nid) VALUES(%d, %d)', $chid, $node->nid); |
pierre@1
|
679 } |
sly@2
|
680 if (user_access('configure ad premier status')) { |
pierre@1
|
681 db_query('UPDATE {ad_priority} SET priority = %d WHERE aid = %d', isset($node->premiere) ? $node->premiere : 0, $node->nid); |
pierre@1
|
682 if (!db_affected_rows()) { |
pierre@1
|
683 db_query('INSERT INTO {ad_priority} (aid, priority) VALUES(%d, %d)', $node->nid, isset($node->premiere) ? $node->premiere : 0); |
pierre@1
|
684 } |
pierre@1
|
685 } |
piotre@7
|
686 db_query('UPDATE {ad_channel_remnant} SET remnant = %d WHERE aid = %d', isset($node->remnant) ? $node->remnant : 0, $node->nid); |
piotre@7
|
687 if (!db_affected_rows()) { |
piotre@7
|
688 db_query('INSERT INTO {ad_channel_remnant} (aid, remnant) VALUES(%d, %d)', $node->nid, isset($node->remnant) ? $node->remnant : 0); |
piotre@7
|
689 } |
pierre@1
|
690 } |
pierre@1
|
691 |
pierre@1
|
692 /** |
pierre@1
|
693 * Delete channel information associated with node. |
pierre@1
|
694 */ |
pierre@1
|
695 function _ad_channel_delete_node($node) { |
pierre@1
|
696 if ($node->nid) { |
pierre@1
|
697 db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid); |
pierre@1
|
698 db_query('DELETE FROM {ad_priority} WHERE aid = %d', $node->nid); |
pierre@1
|
699 } |
pierre@1
|
700 } |
pierre@1
|
701 |
pierre@1
|
702 /** |
pierre@1
|
703 * Be sure that the enabled channels actually can be enabled. |
pierre@1
|
704 */ |
pierre@1
|
705 function _ad_channel_validate_nodes($node) { |
pierre@1
|
706 $channels = _ad_channel_get_enabled($node); |
piotre@7
|
707 |
piotre@7
|
708 foreach ($channels as $chid) { |
piotre@7
|
709 $channel = _ad_channel_get_channels($chid); |
piotre@7
|
710 if(isset($channel->inventory) && $channel->inventory > 0) { |
piotre@7
|
711 $num_ads = _ad_channel_get_channel_active_ad_count($chid); |
piotre@7
|
712 if ($num_ads + 1 >= $channel->inventory) { |
piotre@7
|
713 form_set_error("channel[$channel->conid][channel-$chid]", t('You may not assign more than !inventory to %name.', array('!inventory' => format_plural($channel->inventory, '1 advertisement', '@count advertisements'), '%name' => $channel->name))); |
piotre@7
|
714 } |
piotre@7
|
715 } |
piotre@7
|
716 } |
piotre@7
|
717 |
sly@2
|
718 $limit = variable_get('ad_channel_ad_limit', 0); |
sly@2
|
719 if ($limit && sizeof($channels) > $limit) { |
sly@2
|
720 $quantity_error = TRUE; |
sly@2
|
721 } |
sly@2
|
722 else { |
sly@2
|
723 $quantity_error = FALSE; |
sly@2
|
724 } |
pierre@1
|
725 foreach ($channels as $chid) { |
pierre@1
|
726 $channel = _ad_channel_get_channels($chid); |
sly@2
|
727 if ($quantity_error) { |
sly@2
|
728 $quantity_error = FALSE; |
sly@2
|
729 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
|
730 } |
pierre@1
|
731 $taxonomy = is_array($node->taxonomy) ? $node->taxonomy : array(); |
pierre@1
|
732 $groups = unserialize($channel->groups); |
pierre@1
|
733 if (!empty($groups)) { |
pierre@1
|
734 $enabled = FALSE; |
pierre@1
|
735 foreach($groups as $group) { |
pierre@1
|
736 if ($group) { |
pierre@1
|
737 $enabled = TRUE; |
pierre@1
|
738 break; |
pierre@1
|
739 } |
pierre@1
|
740 } |
pierre@1
|
741 if ($enabled) { |
pierre@1
|
742 $ad_groups = taxonomy_get_tree(_ad_get_vid()); |
pierre@1
|
743 foreach ($ad_groups as $ad_group) { |
pierre@1
|
744 if (is_array($taxonomy[$ad_group->vid]) && |
pierre@1
|
745 isset($taxonomy[$ad_group->vid][$ad_group->tid]) && |
pierre@1
|
746 isset($groups[$ad_group->tid]) && !$groups[$ad_group->tid] && |
pierre@1
|
747 !isset($groups[''])) { |
pierre@1
|
748 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
|
749 } |
pierre@1
|
750 } |
pierre@1
|
751 } |
pierre@1
|
752 } |
pierre@1
|
753 } |
pierre@1
|
754 } |
pierre@1
|
755 |
pierre@1
|
756 /** |
pierre@1
|
757 * Retrive list of enabled channels from node object. |
pierre@1
|
758 */ |
pierre@1
|
759 function _ad_channel_get_enabled($node) { |
pierre@1
|
760 static $enabled = array(); |
pierre@1
|
761 if (!isset($enabled[$node->nid])) { |
pierre@1
|
762 $enabled[$node->nid] = array(); |
pierre@1
|
763 if (isset($node->channel) && is_array($node->channel) && !empty($node->channel)) { |
pierre@1
|
764 foreach ($node->channel as $conid => $channels) { |
pierre@1
|
765 foreach ($channels as $id => $enable) { |
pierre@1
|
766 if ($enable) { |
pierre@1
|
767 $chid = explode('-', $id); |
pierre@1
|
768 $enabled[$node->nid][] = $chid[1]; |
pierre@1
|
769 } |
pierre@1
|
770 } |
pierre@1
|
771 } |
pierre@1
|
772 } |
pierre@1
|
773 } |
pierre@1
|
774 return $enabled[$node->nid]; |
pierre@1
|
775 } |
pierre@1
|
776 |
pierre@1
|
777 /** |
pierre@1
|
778 * Display containers and channels. |
pierre@1
|
779 */ |
pierre@1
|
780 function ad_channel_admin_overview() { |
pierre@1
|
781 drupal_add_css(drupal_get_path('module', 'ad_channel') .'/ad_channel.css'); |
pierre@1
|
782 |
pierre@1
|
783 $containers = _ad_channel_get_containers(); |
pierre@1
|
784 $rows = array(); |
pierre@1
|
785 if (count($containers)) { |
piotre@7
|
786 $header = array(t('Name'), t('Inventory'), t('Options')); |
pierre@1
|
787 $output = '<div id="ad-channel">'; |
pierre@1
|
788 foreach ($containers as $conid => $container) { |
pierre@1
|
789 $channels = _ad_channel_get_container_channels($conid); |
pierre@1
|
790 if ($conid > 0 || count($channels)) { |
pierre@1
|
791 if ($conid > 0) { |
pierre@1
|
792 $description = '<div class="name">'. l($container->name, "admin/content/ad/channel/container/$conid/edit") . "</div>\n"; |
pierre@1
|
793 } |
pierre@1
|
794 else { |
pierre@1
|
795 $description = '<div class="name">'. $container->name . "</div>\n"; |
pierre@1
|
796 } |
pierre@1
|
797 if ($container->description) { |
pierre@1
|
798 $description .= '<div class="description">'. filter_xss_admin($container->description) ."</div>\n"; |
pierre@1
|
799 } |
piotre@7
|
800 $rows[] = array(array('data' => $description, 'class' => 'container', 'colspan' => 3)); |
pierre@1
|
801 } |
pierre@1
|
802 foreach ($channels as $chid => $channel) { |
pierre@1
|
803 $description = "<div style=\"margin-left: 30px;\">\n"; |
pierre@1
|
804 $description .= ' <div class="name">' . $channel->name . "</div>\n"; |
pierre@1
|
805 if ($channel->description) { |
pierre@1
|
806 $description .= ' <div class="description">'. filter_xss_admin($channel->description) ."</div>\n"; |
pierre@1
|
807 } |
pierre@1
|
808 $description .= "</div>\n"; |
piotre@7
|
809 $inventory = ' <div class="inventory">' . $channel->inventory . "</div>\n"; |
pierre@1
|
810 $rows[] = array( |
pierre@1
|
811 array('data' => $description, 'class' => 'channel'), |
piotre@7
|
812 array('data' => $inventory, 'class' => 'channel'), |
pierre@1
|
813 l(t('edit'), "admin/content/ad/channel/channel/$channel->chid/edit") .' '. l(t('delete'), "admin/content/ad/channel/channel/$channel->chid/delete"), |
pierre@1
|
814 ); |
pierre@1
|
815 } |
pierre@1
|
816 } |
pierre@1
|
817 $output .= theme('table', $header, $rows); |
pierre@1
|
818 $output .= '</div>'; |
pierre@1
|
819 } |
pierre@1
|
820 |
pierre@1
|
821 return $output; |
pierre@1
|
822 } |
pierre@1
|
823 |
pierre@1
|
824 /** |
pierre@1
|
825 * Load one or more containers, caching the results. |
pierre@1
|
826 */ |
pierre@1
|
827 function _ad_channel_get_containers($conid = 0) { |
pierre@1
|
828 static $cache; |
pierre@1
|
829 if (!isset($cache[$conid])) { |
pierre@1
|
830 if ($conid) { |
pierre@1
|
831 $cache[$conid] = db_fetch_object(db_query('SELECT * FROM {ad_channel_container} WHERE conid = %d', $conid)); |
pierre@1
|
832 } |
pierre@1
|
833 else { |
pierre@1
|
834 // Get all manually defined channels. |
pierre@1
|
835 $result = db_query('SELECT conid, name, description, weight FROM {ad_channel_container} ORDER BY weight ASC'); |
pierre@1
|
836 while ($container = db_fetch_object($result)) { |
pierre@1
|
837 $containers[$container->conid] = $container; |
pierre@1
|
838 } |
pierre@1
|
839 // Define default 'No container'. |
pierre@1
|
840 $none->conid = 0; |
pierre@1
|
841 $none->name = t('No container'); |
pierre@1
|
842 $none->weight = 0; |
pierre@1
|
843 $containers[0] = $none; |
pierre@1
|
844 $cache[$conid] = $containers; |
pierre@1
|
845 } |
pierre@1
|
846 } |
pierre@1
|
847 return $cache[$conid]; |
pierre@1
|
848 } |
pierre@1
|
849 |
pierre@1
|
850 /** |
pierre@1
|
851 * Load one or more channels, caching the results. |
pierre@1
|
852 */ |
pierre@1
|
853 function _ad_channel_get_container_channels($conid = 0) { |
pierre@1
|
854 static $cache; |
pierre@1
|
855 if (!isset($cache[$conid])) { |
pierre@1
|
856 $channels = array(); |
piotre@7
|
857 $result = db_query('SELECT chid, name, description, weight, inventory FROM {ad_channel} WHERE conid = %d ORDER BY weight ASC', $conid); |
pierre@1
|
858 while ($channel = db_fetch_object($result)) { |
pierre@1
|
859 $channels[$channel->chid] = $channel; |
pierre@1
|
860 } |
pierre@1
|
861 $cache[$conid] = $channels; |
pierre@1
|
862 } |
pierre@1
|
863 return $cache[$conid]; |
pierre@1
|
864 } |
pierre@1
|
865 |
pierre@1
|
866 /** |
pierre@1
|
867 * Load one or more channels. |
pierre@1
|
868 */ |
pierre@1
|
869 function _ad_channel_get_channels($chid = 0) { |
pierre@1
|
870 if ($chid) { |
pierre@1
|
871 return db_fetch_object(db_query('SELECT * FROM {ad_channel} WHERE chid = %d', $chid)); |
pierre@1
|
872 } |
pierre@1
|
873 else { |
pierre@1
|
874 $channels = array(); |
pierre@1
|
875 $result = db_query('SELECT chid, name, description FROM {ad_channel}'); |
pierre@1
|
876 while ($channel = db_fetch_object($result)) { |
pierre@1
|
877 $channels[$channel->chid] = $channel; |
pierre@1
|
878 } |
pierre@1
|
879 return $channels; |
pierre@1
|
880 } |
pierre@1
|
881 } |
pierre@1
|
882 |
pierre@1
|
883 /** |
pierre@1
|
884 * Administrative page for creating or editing containers. |
pierre@1
|
885 */ |
pierre@1
|
886 function ad_channel_admin_container($form_state, $conid = 0) { |
pierre@1
|
887 $form = array(); |
pierre@1
|
888 |
pierre@1
|
889 if ($conid) { |
pierre@1
|
890 $container = _ad_channel_get_containers($conid); |
pierre@1
|
891 if (empty($container)) { |
pierre@1
|
892 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
893 } |
pierre@1
|
894 $form['conid'] = array( |
pierre@1
|
895 '#type' => 'hidden', |
pierre@1
|
896 '#value' => $conid, |
pierre@1
|
897 ); |
pierre@1
|
898 } |
pierre@1
|
899 |
pierre@1
|
900 $form['name'] = array( |
pierre@1
|
901 '#type' => 'textfield', |
pierre@1
|
902 '#title' => t('Container name'), |
pierre@1
|
903 '#required' => TRUE, |
pierre@1
|
904 '#description' => t('Channel containers can be used to help organize channels, but they are not required.'), |
pierre@1
|
905 '#default_value' => $conid ? $container->name : '', |
pierre@1
|
906 ); |
pierre@1
|
907 $form['description'] = array( |
pierre@1
|
908 '#type' => 'textarea', |
pierre@1
|
909 '#title' => t('Description'), |
pierre@1
|
910 '#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
|
911 '#default_value' => $conid ? $container->description : '', |
pierre@1
|
912 ); |
pierre@1
|
913 $form['weight'] = array( |
pierre@1
|
914 '#type' => 'weight', |
pierre@1
|
915 '#title' => t('Weight'), |
pierre@1
|
916 '#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
|
917 '#default_value' => $conid ? $container->weight : 0, |
pierre@1
|
918 ); |
pierre@1
|
919 |
pierre@1
|
920 if ($conid) { |
pierre@1
|
921 $form['update'] = array( |
pierre@1
|
922 '#type' => 'submit', |
pierre@1
|
923 '#value' => t('Update'), |
pierre@1
|
924 ); |
pierre@1
|
925 $form['delete'] = array( |
pierre@1
|
926 '#type' => 'submit', |
pierre@1
|
927 '#value' => t('Delete'), |
pierre@1
|
928 ); |
pierre@1
|
929 } |
pierre@1
|
930 else { |
pierre@1
|
931 $form['create'] = array( |
pierre@1
|
932 '#type' => 'submit', |
pierre@1
|
933 '#value' => t('Create'), |
pierre@1
|
934 ); |
pierre@1
|
935 } |
pierre@1
|
936 $form['cancel'] = array( |
pierre@1
|
937 '#type' => 'markup', |
pierre@1
|
938 '#value' => l(t('Cancel'), 'admin/content/ad/channel'), |
pierre@1
|
939 ); |
pierre@1
|
940 |
pierre@1
|
941 return $form; |
pierre@1
|
942 } |
pierre@1
|
943 |
pierre@1
|
944 /** |
pierre@1
|
945 * Validate the container. |
pierre@1
|
946 */ |
pierre@1
|
947 function ad_channel_admin_container_validate($form, &$form_state) { |
pierre@1
|
948 $conid = 0; |
pierre@1
|
949 if ($form_state['values']['op'] == t('Create')) { |
pierre@1
|
950 $conid = db_result(db_query("SELECT conid FROM {ad_channel_container} WHERE name = '%s'", $form_state['values']['name'])); |
pierre@1
|
951 } |
pierre@1
|
952 else if ($form_state['values']['op'] == t('Update')) { |
pierre@1
|
953 $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
|
954 } |
pierre@1
|
955 if ($conid) { |
pierre@1
|
956 form_set_error('name', t('A container named %name already exists.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
957 } |
pierre@1
|
958 } |
pierre@1
|
959 |
pierre@1
|
960 /** |
pierre@1
|
961 * Save the container. |
pierre@1
|
962 */ |
pierre@1
|
963 function ad_channel_admin_container_submit($form, &$form_state) { |
pierre@1
|
964 switch ($form_state['values']['op']) { |
pierre@1
|
965 case t('Create'): |
pierre@1
|
966 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
|
967 drupal_set_message(t('The %name container has been created.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
968 break; |
pierre@1
|
969 case t('Update'): |
pierre@1
|
970 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
|
971 drupal_set_message(t('The %name container has been updated.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
972 break; |
pierre@1
|
973 case t('Delete'): |
pierre@1
|
974 drupal_goto('admin/content/ad/channel/container/'. $form_state['values']['conid'] .'/delete'); |
pierre@1
|
975 } |
pierre@1
|
976 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
977 } |
pierre@1
|
978 |
pierre@1
|
979 /** |
pierre@1
|
980 * Confirm whether or not to delete container, and the contained channels. |
pierre@1
|
981 */ |
sly@2
|
982 function ad_channel_admin_confirm_delete_container($form_state, $container) { |
pierre@1
|
983 $form = array(); |
pierre@1
|
984 |
pierre@1
|
985 $form['conid'] = array( |
pierre@1
|
986 '#type' => 'value', |
sly@2
|
987 '#value' => $container->conid, |
pierre@1
|
988 ); |
pierre@1
|
989 |
pierre@1
|
990 return confirm_form( |
pierre@1
|
991 $form, |
pierre@1
|
992 t('Are you sure you want to delete the %name container?', array('%name' => $container->name)), |
pierre@1
|
993 'admin/content/ad/channel', |
pierre@1
|
994 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
|
995 t('Delete'), |
pierre@1
|
996 t('Cancel')); |
pierre@1
|
997 } |
pierre@1
|
998 |
pierre@1
|
999 /** |
pierre@1
|
1000 * Delete a container. |
pierre@1
|
1001 */ |
pierre@1
|
1002 function ad_channel_admin_confirm_delete_container_submit($form, &$form_state) { |
pierre@1
|
1003 $container = _ad_channel_get_containers($form_state['values']['conid']); |
pierre@1
|
1004 if ($container->conid) { |
pierre@1
|
1005 db_query('UPDATE {ad_channel} SET conid = 0 WHERE conid = %d', $container->conid); |
pierre@1
|
1006 db_query('DELETE FROM {ad_channel_container} WHERE conid = %d', $container->conid); |
pierre@1
|
1007 drupal_set_message(t('The %name container has been deleted.', array('%name' => $container->name))); |
pierre@1
|
1008 } |
pierre@1
|
1009 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
1010 } |
pierre@1
|
1011 |
pierre@1
|
1012 /** |
pierre@1
|
1013 * Administrative page for creating or editing channels. |
pierre@1
|
1014 */ |
pierre@1
|
1015 function ad_channel_admin_channel($form_state, $chid = 0) { |
pierre@1
|
1016 $form = array(); |
pierre@1
|
1017 |
pierre@1
|
1018 if ($chid) { |
pierre@1
|
1019 $channel = _ad_channel_get_channels($chid); |
pierre@1
|
1020 if (empty($channel)) { |
pierre@1
|
1021 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
1022 } |
pierre@1
|
1023 $form['chid'] = array( |
pierre@1
|
1024 '#type' => 'hidden', |
pierre@1
|
1025 '#value' => $chid, |
pierre@1
|
1026 ); |
pierre@1
|
1027 } |
pierre@1
|
1028 |
pierre@1
|
1029 $form['name'] = array( |
pierre@1
|
1030 '#type' => 'textfield', |
pierre@1
|
1031 '#required' => TRUE, |
pierre@1
|
1032 '#title' => t('Channel name'), |
pierre@1
|
1033 '#description' => t('Enter a short, descriptive name for your channel.'), |
pierre@1
|
1034 '#default_value' => $chid ? $channel->name : '', |
pierre@1
|
1035 ); |
pierre@1
|
1036 $form['description'] = array( |
pierre@1
|
1037 '#type' => 'textarea', |
pierre@1
|
1038 '#title' => t('Description'), |
pierre@1
|
1039 '#description' => t('Enter a full description of your channel.'), |
pierre@1
|
1040 '#default_value' => $chid ? $channel->description : '', |
pierre@1
|
1041 ); |
pierre@1
|
1042 $result = db_query('SELECT conid, name FROM {ad_channel_container} ORDER BY weight ASC'); |
pierre@1
|
1043 $containers = array(t('<none>')); |
pierre@1
|
1044 while ($container = db_fetch_object($result)) { |
pierre@1
|
1045 $containers[$container->conid] = $container->name; |
pierre@1
|
1046 } |
pierre@1
|
1047 if (sizeof($containers) == 1) { |
pierre@1
|
1048 $containers = array(t('No containers have been created.')); |
pierre@1
|
1049 } |
pierre@1
|
1050 $form['conid'] = array( |
pierre@1
|
1051 '#type' => 'select', |
pierre@1
|
1052 '#title' => t('Container'), |
pierre@1
|
1053 '#options' => $containers, |
pierre@1
|
1054 '#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
|
1055 '#default_value' => $chid ? $channel->conid : 0, |
pierre@1
|
1056 ); |
pierre@1
|
1057 $form['weight'] = array( |
pierre@1
|
1058 '#type' => 'weight', |
pierre@1
|
1059 '#title' => t('Weight'), |
pierre@1
|
1060 '#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
|
1061 '#default_value' => $chid ? $channel->weight : 0, |
pierre@1
|
1062 ); |
pierre@1
|
1063 |
piotre@7
|
1064 // Inventory for remnant management |
piotre@7
|
1065 $form['channel_inventory'] = array( |
piotre@7
|
1066 '#type' => 'fieldset', |
piotre@7
|
1067 '#title' => t('Inventory'), |
piotre@7
|
1068 '#collapsible' => TRUE, |
piotre@7
|
1069 '#collapsed' => FALSE |
piotre@7
|
1070 ); |
piotre@7
|
1071 $form['channel_inventory']['inventory'] = array( |
piotre@7
|
1072 '#type' => 'textfield', |
piotre@7
|
1073 '#title' => t('Level'), |
piotre@7
|
1074 '#size' => 10, |
piotre@7
|
1075 '#maxlength' => 10, |
piotre@7
|
1076 '#required' => FALSE, |
piotre@7
|
1077 '#description' => t('Entering an inventory value will limit the number of advertisements that may be assigned to this channel. As well, if fewer advertisements than the inventory value are assigned to this channel, advertisements marked as remnants will be used to reach the assigned inventory level.'), |
piotre@7
|
1078 '#default_value' => $chid ? $channel->inventory : 0, |
piotre@7
|
1079 ); |
piotre@7
|
1080 |
pierre@1
|
1081 // URL rules |
pierre@1
|
1082 $form['URL_rules'] = array( |
pierre@1
|
1083 '#type' => 'fieldset', |
pierre@1
|
1084 '#title' => t('URL rules'), |
pierre@1
|
1085 '#collapsible' => TRUE, |
pierre@1
|
1086 '#collasped' => FALSE, |
pierre@1
|
1087 ); |
pierre@1
|
1088 $form['URL_rules']['display'] = array( |
pierre@1
|
1089 '#type' => 'radios', |
pierre@1
|
1090 '#title' => t('Display advertisements from this channel on specific URLs'), |
pierre@1
|
1091 '#options' => array(t('Display advertisements on every URL except the listed URLs.'), t('Display advertisements only on the listed URLs.')), |
pierre@1
|
1092 '#default_value' => $chid ? $channel->display : 0, |
pierre@1
|
1093 ); |
pierre@1
|
1094 $form['URL_rules']['urls'] = array( |
pierre@1
|
1095 '#type' => 'textarea', |
pierre@1
|
1096 '#title' => t('Paths'), |
sly@8
|
1097 '#description' => t("Enter one URL per line. If serving ads locally use Drupal relative paths. If serving ads remotely include the 'http://' or 'https:/'. Use '<front>' to specify the front page. The '*' character is a wildcard. Example URLs are <em>blog</em> for the blog page and <em>blog/*</em> for every personal blog on your local website. When specifying local paths you must enter the raw path (ie 'node/123') and not the url alias (ie 'my/custom/path'), if any."), |
pierre@1
|
1098 '#default_value' => $chid ? unserialize($channel->urls) : '', |
pierre@1
|
1099 ); |
pierre@1
|
1100 |
pierre@1
|
1101 // Group rules |
pierre@1
|
1102 $groups = taxonomy_get_tree(_ad_get_vid()); |
pierre@1
|
1103 $collapsed = is_array($groups) && !empty($groups) ? FALSE : TRUE; |
pierre@1
|
1104 $form['group_rules'] = array( |
pierre@1
|
1105 '#type' => 'fieldset', |
pierre@1
|
1106 '#title' => t('Ad group rules'), |
pierre@1
|
1107 '#collapsible' => TRUE, |
pierre@1
|
1108 '#collapsed' => $collapsed, |
pierre@1
|
1109 ); |
pierre@1
|
1110 if (!$collapsed) { |
pierre@1
|
1111 foreach ($groups as $group) { |
pierre@1
|
1112 $options[$group->tid] = $group->name; |
pierre@1
|
1113 } |
pierre@1
|
1114 $form['group_rules']['groups'] = array( |
pierre@1
|
1115 '#type' => 'checkboxes', |
pierre@1
|
1116 '#title' => t('Allow advertisements from specific ad groups'), |
pierre@1
|
1117 '#options' => $options, |
pierre@1
|
1118 '#prefix' => '<div>', |
pierre@1
|
1119 '#suffix' => '</div>', |
pierre@1
|
1120 '#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
|
1121 '#default_value' => $chid ? unserialize($channel->groups) : array(), |
pierre@1
|
1122 ); |
pierre@1
|
1123 } |
pierre@1
|
1124 else { |
pierre@1
|
1125 $form['group_rules']['none'] = array( |
pierre@1
|
1126 '#type' => 'markup', |
pierre@1
|
1127 '#value' => t('No ad groups have been created.'), |
pierre@1
|
1128 '#prefix' => '<div>', |
pierre@1
|
1129 '#suffix' => '</div>', |
pierre@1
|
1130 ); |
pierre@1
|
1131 } |
pierre@1
|
1132 |
sly@2
|
1133 $collapsed = (variable_get('ad_channel_display', 0) == 1) ? 0 : 1; |
sly@2
|
1134 $form['nonchannel_rules'] = array( |
sly@2
|
1135 '#type' => 'fieldset', |
sly@2
|
1136 '#title' => t('Not-in-channel ad rules'), |
sly@2
|
1137 '#collapsible' => TRUE, |
sly@2
|
1138 '#collapsed' => $collapsed, |
sly@2
|
1139 ); |
sly@2
|
1140 if ($collapsed) { |
sly@2
|
1141 $form['nonchannel_rules']['info'] = array( |
sly@2
|
1142 '#type' => 'markup', |
sly@2
|
1143 '#prefix' => '<div>', |
sly@2
|
1144 '#suffix' => '</div>', |
sly@2
|
1145 '#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
|
1146 ); |
sly@2
|
1147 } |
piotre@7
|
1148 $form['nonchannel_rules']['no_channel_percent'] = array( |
sly@2
|
1149 '#type' => 'select', |
piotre@7
|
1150 '#title' => t('Percentage'), |
piotre@7
|
1151 '#options' => _ad_channel_percentages(), |
piotre@7
|
1152 '#default_value' => $chid ? $channel->no_channel_percent : 10, |
piotre@7
|
1153 '#description' => t('Choose the maximum percentage of not-in-channel advertisements allowable for display. For instance, selecting 10% indicates that 1 out of 10 advertisements will be selected from those not assigned to this channel.'), |
sly@2
|
1154 ); |
pierre@1
|
1155 if ($chid) { |
pierre@1
|
1156 $form['update'] = array( |
pierre@1
|
1157 '#type' => 'submit', |
pierre@1
|
1158 '#value' => t('Update'), |
pierre@1
|
1159 ); |
pierre@1
|
1160 $form['delete'] = array( |
pierre@1
|
1161 '#type' => 'submit', |
pierre@1
|
1162 '#value' => t('Delete'), |
pierre@1
|
1163 ); |
pierre@1
|
1164 } |
pierre@1
|
1165 else { |
pierre@1
|
1166 $form['submit'] = array( |
pierre@1
|
1167 '#type' => 'submit', |
pierre@1
|
1168 '#value' => t('Create'), |
pierre@1
|
1169 ); |
pierre@1
|
1170 } |
pierre@1
|
1171 $form['cancel'] = array( |
pierre@1
|
1172 '#type' => 'markup', |
pierre@1
|
1173 '#value' => l(t('Cancel'), 'admin/content/ad/channel'), |
pierre@1
|
1174 ); |
pierre@1
|
1175 |
pierre@1
|
1176 return $form; |
pierre@1
|
1177 } |
pierre@1
|
1178 |
pierre@1
|
1179 /** |
pierre@1
|
1180 * Validate the channel. |
pierre@1
|
1181 */ |
pierre@1
|
1182 function ad_channel_admin_channel_validate($form, &$form_state) { |
pierre@1
|
1183 $chid = 0; |
pierre@1
|
1184 if ($form_state['values']['op'] == t('Create')) { |
pierre@1
|
1185 $chid = db_result(db_query("SELECT chid FROM {ad_channel} WHERE name = '%s'", $form_state['values']['name'])); |
pierre@1
|
1186 } |
pierre@1
|
1187 else if ($form_state['values']['op'] == t('Update')) { |
pierre@1
|
1188 $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
|
1189 } |
pierre@1
|
1190 if ($chid) { |
pierre@1
|
1191 form_set_error('name', t('A channel named %name already exists.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
1192 } |
pierre@1
|
1193 } |
pierre@1
|
1194 |
pierre@1
|
1195 /** |
pierre@1
|
1196 * Save the channel. |
pierre@1
|
1197 */ |
pierre@1
|
1198 function ad_channel_admin_channel_submit($form, &$form_state) { |
sly@2
|
1199 // remove extraneous white space from url list which can break matching |
sly@2
|
1200 $url_array = explode("\n", $form_state['values']['urls']); |
sly@2
|
1201 if (is_array($url_array)) { |
sly@2
|
1202 foreach ($url_array as $url) { |
sly@2
|
1203 $urls[] = trim($url); |
sly@2
|
1204 } |
sly@2
|
1205 $urls = implode("\n", $urls); |
sly@2
|
1206 } |
sly@2
|
1207 else { |
sly@2
|
1208 $urls = ''; |
sly@2
|
1209 } |
pierre@1
|
1210 switch ($form_state['values']['op']) { |
pierre@1
|
1211 case t('Create'): |
piotre@7
|
1212 db_query("INSERT INTO {ad_channel} (name, description, conid, weight, display, no_channel_percent, urls, groups, inventory) VALUES('%s', '%s', %d, %d, %d, '%s', '%s', %d, %d)", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['conid'], $form_state['values']['weight'], $form_state['values']['display'], $form_state['values']['no_channel_percent'], serialize($urls), serialize($form_state['values']['groups']), $form_state['values']['inventory']); |
pierre@1
|
1213 drupal_set_message(t('The %name channel has been created.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
1214 break; |
pierre@1
|
1215 case t('Update'): |
pierre@1
|
1216 $groups = array(); |
pierre@1
|
1217 // Don't store information about groups that no longer exist. |
pierre@1
|
1218 if (is_array($form_state['values']['groups'])) { |
pierre@1
|
1219 $ad_groups = taxonomy_get_tree(_ad_get_vid()); |
pierre@1
|
1220 foreach ($ad_groups as $ad_group) { |
pierre@1
|
1221 if (isset($form_state['values']['groups'][$ad_group->tid])) { |
pierre@1
|
1222 $groups[$ad_group->tid] = $form_state['values']['groups'][$ad_group->tid]; |
pierre@1
|
1223 } |
pierre@1
|
1224 } |
pierre@1
|
1225 } |
piotre@7
|
1226 db_query("UPDATE {ad_channel} SET name = '%s', description = '%s', conid = %d, weight = %d, display = %d, urls = '%s', groups = '%s', no_channel_percent = %d, inventory = %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_percent'], $form_state['values']['inventory'], $form_state['values']['chid']); |
pierre@1
|
1227 drupal_set_message(t('The %name channel has been updated.', array('%name' => $form_state['values']['name']))); |
pierre@1
|
1228 break; |
pierre@1
|
1229 case t('Delete'): |
pierre@1
|
1230 drupal_goto('admin/content/ad/channel/channel/'. $form_state['values']['chid'] .'/delete'); |
pierre@1
|
1231 } |
pierre@1
|
1232 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
1233 } |
pierre@1
|
1234 |
pierre@1
|
1235 /** |
pierre@1
|
1236 * Confirm whether or not to delete container, and the contained channels. |
pierre@1
|
1237 */ |
sly@2
|
1238 function ad_channel_admin_confirm_delete_channel($form_state, $channel) { |
pierre@1
|
1239 $form = array(); |
pierre@1
|
1240 |
pierre@1
|
1241 $form['chid'] = array( |
pierre@1
|
1242 '#type' => 'value', |
sly@2
|
1243 '#value' => $channel->chid, |
pierre@1
|
1244 ); |
pierre@1
|
1245 |
pierre@1
|
1246 return confirm_form( |
pierre@1
|
1247 $form, |
pierre@1
|
1248 t('Are you sure you want to delete the %name channel?', array('%name' => $channel->name)), |
pierre@1
|
1249 'admin/content/ad/channel', |
pierre@1
|
1250 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
|
1251 t('Delete'), |
pierre@1
|
1252 t('Cancel')); |
pierre@1
|
1253 } |
pierre@1
|
1254 |
pierre@1
|
1255 /** |
pierre@1
|
1256 * Delete a channel. |
pierre@1
|
1257 */ |
pierre@1
|
1258 function ad_channel_admin_confirm_delete_channel_submit($form, &$form_state) { |
pierre@1
|
1259 $channel = _ad_channel_get_channels($form_state['values']['chid']); |
pierre@1
|
1260 if ($channel->chid) { |
pierre@1
|
1261 db_query('DELETE FROM {ad_channel} WHERE chid = %d', $channel->chid); |
pierre@1
|
1262 drupal_set_message(t('The %name channel has been deleted.', array('%name' => $channel->name))); |
pierre@1
|
1263 } |
pierre@1
|
1264 drupal_goto('admin/content/ad/channel'); |
pierre@1
|
1265 } |
pierre@1
|
1266 |
sly@2
|
1267 /** |
sly@2
|
1268 * Available probabilities. |
sly@2
|
1269 */ |
sly@2
|
1270 function _ad_channel_probabilities() { |
sly@2
|
1271 return array( |
sly@2
|
1272 25 => t('1/4'), |
sly@2
|
1273 33 => t('1/3'), |
sly@2
|
1274 50 => t('1/2'), |
sly@2
|
1275 100 => t('1'), |
sly@2
|
1276 200 => t('2'), |
sly@2
|
1277 300 => t('3'), |
sly@2
|
1278 400 => t('4'), |
sly@2
|
1279 ); |
sly@2
|
1280 } |
piotre@7
|
1281 |
piotre@7
|
1282 /** |
piotre@7
|
1283 * Available percentages |
piotre@7
|
1284 */ |
piotre@7
|
1285 function _ad_channel_percentages() { |
piotre@7
|
1286 $options = array(); |
piotre@7
|
1287 for($i = 5; $i <= 95; $i += 5) { |
piotre@7
|
1288 $options[$i] = t($i.'%'); |
piotre@7
|
1289 } |
piotre@7
|
1290 return $options; |
piotre@7
|
1291 } |
piotre@7
|
1292 |
piotre@7
|
1293 function _ad_channel_get_channel_active_ad_count($chid) { |
piotre@7
|
1294 if ($chid) { |
piotre@7
|
1295 $count = db_result(db_query("SELECT COUNT(nid) AS count FROM {ad_channel_node} acn LEFT JOIN {ads} a ON acn.nid = a.aid WHERE acn.chid = %d AND a.adstatus = 'active'", $chid)); |
piotre@7
|
1296 } |
piotre@7
|
1297 return $count; |
piotre@7
|
1298 } |