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