comparison channel/ad_channel.module @ 2:e5584a19768b ad

maj module ad
author sly
date Wed, 15 Apr 2009 07:58:32 +0000
parents 948362c2a207
children 0d1c70d51fbe
comparison
equal deleted inserted replaced
1:948362c2a207 2:e5584a19768b
1 <?php 1 <?php
2 // $Id: ad_channel.module,v 1.1.4.15 2009/03/31 00:59:21 jeremy Exp $ 2 // $Id: ad_channel.module,v 1.1.4.20 2009/04/09 22:04:49 jeremy Exp $
3 3
4 /** 4 /**
5 * @file 5 * @file
6 * Ad Channel module. 6 * Ad Channel module.
7 * 7 *
79 'page callback' => 'drupal_get_form', 79 'page callback' => 'drupal_get_form',
80 'page arguments' => array('ad_channel_admin_settings'), 80 'page arguments' => array('ad_channel_admin_settings'),
81 'access arguments' => array('administer channels'), 81 'access arguments' => array('administer channels'),
82 'type' => MENU_LOCAL_TASK, 82 'type' => MENU_LOCAL_TASK,
83 'weight' => 9); 83 'weight' => 9);
84 /* 84 $items['admin/content/ad/channel/container/%ad_channel_container/delete'] = array(
85 else { 85 'page callback' => 'drupal_get_form',
86 TODO: 86 'page arguments' => array('ad_channel_admin_confirm_delete_container', 5),
87 $items[] = array( 87 'access arguments' => array('administer channels'),
88 'path' => "admin/content/ad/channel/container/$conid/delete", 88 'type' => MENU_CALLBACK);
89 'callback' => 'drupal_get_form', 89 $items['admin/content/ad/channel/channel/%ad_channel_channel/delete'] = array(
90 'callback arguments' => array('ad_channel_admin_confirm_delete_container', $conid), 90 'page callback' => 'drupal_get_form',
91 'type' => MENU_CALLBACK); 91 'page arguments' => array('ad_channel_admin_confirm_delete_channel', 5),
92 $items[] = array( 92 'access arguments' => array('administer channels'),
93 'path' => "admin/content/ad/channel/channel/$chid/delete", 93 'type' => MENU_CALLBACK);
94 'callback' => 'drupal_get_form',
95 'callback arguments' => array('ad_channel_admin_confirm_delete_channel', $chid),
96 'type' => MENU_CALLBACK);
97 }
98 */
99 94
100 return $items; 95 return $items;
96 }
97
98 /**
99 * Load a specified container.
100 */
101 function ad_channel_container_load($conid = 0) {
102 static $containers = array();
103 if (!isset($containers[$conid])) {
104 $containers[$conid] = db_fetch_object(db_query('SELECT * FROM {ad_channel_container} WHERE conid = %d', $conid));
105 }
106 return $containers[$conid];
107 }
108
109 /**
110 * Load a specified channel.
111 */
112 function ad_channel_channel_load($chid = 0) {
113 static $channels = array();
114 if (!isset($channels[$chid])) {
115 $channels[$chid] = db_fetch_object(db_query('SELECT * FROM {ad_channel} WHERE chid = %d', $chid));
116 }
117 return $channels[$chid];
101 } 118 }
102 119
103 /** 120 /**
104 * Implementation of hook_form_alter(). 121 * Implementation of hook_form_alter().
105 * Generate a form for selecting channels to associate with an advertisement. 122 * Generate a form for selecting channels to associate with an advertisement.
150 $form['channel']['#weight'] = -2; 167 $form['channel']['#weight'] = -2;
151 $form['channel']['#tree'] = TRUE; 168 $form['channel']['#tree'] = TRUE;
152 } 169 }
153 $form['priority'] = array( 170 $form['priority'] = array(
154 '#type' => 'fieldset', 171 '#type' => 'fieldset',
155 '#access' => user_access('configure ad premiere status'), 172 '#access' => user_access('configure ad premier status'),
156 '#title' => t('Priority'), 173 '#title' => t('Priority'),
157 '#collapsible' => TRUE, 174 '#collapsible' => TRUE,
158 '#collapsed' => FALSE, 175 '#collapsed' => FALSE,
159 ); 176 );
160 $form['priority']['premiere'] = array( 177 $form['priority']['premiere'] = array(
161 '#type' => 'checkbox', 178 '#type' => 'checkbox',
162 '#access' => user_access('configure ad premiere status'), 179 '#access' => user_access('configure ad premier status'),
163 '#title' => t('Premiere'), 180 '#title' => t('Premiere'),
164 '#description' => t('An active premiere advertisement will override all other non-premiere advertisements in the same channel. As long as one or more premiere advertisements are active in a channel, non-premiere advertisements will not be displayed in that channel.'), 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.'),
165 '#default_value' => isset($node->premiere) ? $node->premiere : FALSE, 182 '#default_value' => isset($node->premiere) ? $node->premiere : FALSE,
166 ); 183 );
167 $form['priority']['#weight'] = -1; 184 $form['priority']['#weight'] = -1;
459 $ads[$ad->aid][$channel->chid] = $channel->chid; 476 $ads[$ad->aid][$channel->chid] = $channel->chid;
460 //$ads[$channel->chid][$ad->aid] = $ad->aid; 477 //$ads[$channel->chid][$ad->aid] = $ad->aid;
461 } 478 }
462 } 479 }
463 $channels = array(); 480 $channels = array();
464 $result = db_query('SELECT chid, display, urls FROM {ad_channel}'); 481 $result = db_query('SELECT chid, display, urls, no_channel_weight FROM {ad_channel}');
465 while ($channel = db_fetch_object($result)) { 482 while ($channel = db_fetch_object($result)) {
466 $channels[$channel->chid] = $channel; 483 $channels[$channel->chid] = $channel;
467 } 484 }
468 $result = db_query("SELECT p.aid, p.priority FROM {ads} a LEFT JOIN {ad_priority} p ON a.aid = p.aid WHERE a.adstatus = 'active' AND p.priority = 1"); 485 $result = db_query("SELECT p.aid, p.priority FROM {ads} a LEFT JOIN {ad_priority} p ON a.aid = p.aid WHERE a.adstatus = 'active' AND p.priority = 1");
469 $premiere = array(); 486 $premiere = array();
503 '#type' => 'radios', 520 '#type' => 'radios',
504 '#title' => t('Display channels on administrative ads overview listing'), 521 '#title' => t('Display channels on administrative ads overview listing'),
505 '#options' => array(t('In addition to groups'), t('In place of groups'), t('Not at all')), 522 '#options' => array(t('In addition to groups'), t('In place of groups'), t('Not at all')),
506 '#default_value' => variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL), 523 '#default_value' => variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL),
507 '#description' => t('By default, channels will be listed along with groups on the administrative ads list. You can optionally disable either the groups column (by selecting "in place of groups"), or the channel column (by selecting "not at all").'), 524 '#description' => t('By default, channels will be listed along with groups on the administrative ads list. You can optionally disable either the groups column (by selecting "in place of groups"), or the channel column (by selecting "not at all").'),
525 );
526 $options = array(0 => t('No limit')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
527 $form['ad_channel_ad_limit'] = array(
528 '#type' => 'select',
529 '#title' => t('Maximum number of channels that can be assigned to a single ad'),
530 '#options' => $options,
531 '#default_value' => variable_get('ad_channel_ad_limit', 0),
532 '#description' => t('Optionally limit the number of channels that a single advertisement can be assigned to.'),
508 ); 533 );
509 534
510 return system_settings_form($form); 535 return system_settings_form($form);
511 } 536 }
512 537
563 db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid); 588 db_query('DELETE FROM {ad_channel_node} WHERE nid = %d', $node->nid);
564 $channels = _ad_channel_get_enabled($node); 589 $channels = _ad_channel_get_enabled($node);
565 foreach ($channels as $chid) { 590 foreach ($channels as $chid) {
566 db_query('INSERT INTO {ad_channel_node} (chid, nid) VALUES(%d, %d)', $chid, $node->nid); 591 db_query('INSERT INTO {ad_channel_node} (chid, nid) VALUES(%d, %d)', $chid, $node->nid);
567 } 592 }
568 if (user_access('configure ad premiere status')) { 593 if (user_access('configure ad premier status')) {
569 db_query('UPDATE {ad_priority} SET priority = %d WHERE aid = %d', isset($node->premiere) ? $node->premiere : 0, $node->nid); 594 db_query('UPDATE {ad_priority} SET priority = %d WHERE aid = %d', isset($node->premiere) ? $node->premiere : 0, $node->nid);
570 if (!db_affected_rows()) { 595 if (!db_affected_rows()) {
571 db_query('INSERT INTO {ad_priority} (aid, priority) VALUES(%d, %d)', $node->nid, isset($node->premiere) ? $node->premiere : 0); 596 db_query('INSERT INTO {ad_priority} (aid, priority) VALUES(%d, %d)', $node->nid, isset($node->premiere) ? $node->premiere : 0);
572 } 597 }
573 } 598 }
586 /** 611 /**
587 * Be sure that the enabled channels actually can be enabled. 612 * Be sure that the enabled channels actually can be enabled.
588 */ 613 */
589 function _ad_channel_validate_nodes($node) { 614 function _ad_channel_validate_nodes($node) {
590 $channels = _ad_channel_get_enabled($node); 615 $channels = _ad_channel_get_enabled($node);
616 $limit = variable_get('ad_channel_ad_limit', 0);
617 if ($limit && sizeof($channels) > $limit) {
618 $quantity_error = TRUE;
619 }
620 else {
621 $quantity_error = FALSE;
622 }
591 foreach ($channels as $chid) { 623 foreach ($channels as $chid) {
592 $channel = _ad_channel_get_channels($chid); 624 $channel = _ad_channel_get_channels($chid);
625 if ($quantity_error) {
626 $quantity_error = FALSE;
627 form_set_error("channel[$channel->conid][channel-$chid]", t('You can not assign this advertisement to more than !limit.', array('!limit' => format_plural(sizeof($limit), '1 channel', '@count channels'))));
628 }
593 $taxonomy = is_array($node->taxonomy) ? $node->taxonomy : array(); 629 $taxonomy = is_array($node->taxonomy) ? $node->taxonomy : array();
594 $groups = unserialize($channel->groups); 630 $groups = unserialize($channel->groups);
595 if (!empty($groups)) { 631 if (!empty($groups)) {
596 $enabled = FALSE; 632 $enabled = FALSE;
597 foreach($groups as $group) { 633 foreach($groups as $group) {
837 } 873 }
838 874
839 /** 875 /**
840 * Confirm whether or not to delete container, and the contained channels. 876 * Confirm whether or not to delete container, and the contained channels.
841 */ 877 */
842 function ad_channel_admin_confirm_delete_container($conid) { 878 function ad_channel_admin_confirm_delete_container($form_state, $container) {
843 $form = array(); 879 $form = array();
844
845 $container = _ad_channel_get_containers($conid);
846 880
847 $form['conid'] = array( 881 $form['conid'] = array(
848 '#type' => 'value', 882 '#type' => 'value',
849 '#value' => $conid, 883 '#value' => $container->conid,
850 ); 884 );
851 885
852 return confirm_form( 886 return confirm_form(
853 $form, 887 $form,
854 t('Are you sure you want to delete the %name container?', array('%name' => $container->name)), 888 t('Are you sure you want to delete the %name container?', array('%name' => $container->name)),
973 '#prefix' => '<div>', 1007 '#prefix' => '<div>',
974 '#suffix' => '</div>', 1008 '#suffix' => '</div>',
975 ); 1009 );
976 } 1010 }
977 1011
1012 $collapsed = (variable_get('ad_channel_display', 0) == 1) ? 0 : 1;
1013 $form['nonchannel_rules'] = array(
1014 '#type' => 'fieldset',
1015 '#title' => t('Not-in-channel ad rules'),
1016 '#collapsible' => TRUE,
1017 '#collapsed' => $collapsed,
1018 );
1019 if ($collapsed) {
1020 $form['nonchannel_rules']['info'] = array(
1021 '#type' => 'markup',
1022 '#prefix' => '<div>',
1023 '#suffix' => '</div>',
1024 '#value' => t('You will need to set "!variable" to %value for these %type ad rules to be applicable. Currently these settings are doing nothing.', array('!variable' => l(t('display advertisements not assigned to any channel'), 'admin/content/ad/channel/settings'), '%value' => t('always'), '%type' => 'not-in-channel')),
1025 );
1026 }
1027 $form['nonchannel_rules']['no_channel_weight'] = array(
1028 '#type' => 'select',
1029 '#title' => t('Probability'),
1030 '#options' => _ad_channel_probabilities(),
1031 '#default_value' => $channel->no_channel_weight ? $channel->no_channel_weight : 100,
1032 '#description' => t('The greater the probability, the more frequently advertisements that aren\'t in any channel will be displayed along with ads in this channel. For example, if set to 2, advertisements without any channel will be displayed twice as often as advertisements assigned to this channel. If set to 1/2, advertisements without any channel will be displayed half as often as advertisements assigned to this channel. If set to 1, advertisements without any channel will be displayed as often as advertisements assigned to this channel.'),
1033 );
1034
978 if ($chid) { 1035 if ($chid) {
979 $form['update'] = array( 1036 $form['update'] = array(
980 '#type' => 'submit', 1037 '#type' => 'submit',
981 '#value' => t('Update'), 1038 '#value' => t('Update'),
982 ); 1039 );
1017 1074
1018 /** 1075 /**
1019 * Save the channel. 1076 * Save the channel.
1020 */ 1077 */
1021 function ad_channel_admin_channel_submit($form, &$form_state) { 1078 function ad_channel_admin_channel_submit($form, &$form_state) {
1079 // remove extraneous white space from url list which can break matching
1080 $url_array = explode("\n", $form_state['values']['urls']);
1081 if (is_array($url_array)) {
1082 foreach ($url_array as $url) {
1083 $urls[] = trim($url);
1084 }
1085 $urls = implode("\n", $urls);
1086 }
1087 else {
1088 $urls = '';
1089 }
1022 switch ($form_state['values']['op']) { 1090 switch ($form_state['values']['op']) {
1023 case t('Create'): 1091 case t('Create'):
1024 db_query("INSERT INTO {ad_channel} (name, description, conid, weight, display, urls, groups) VALUES('%s', '%s', %d, %d, %d, '%s', '%s')", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['conid'], $form_state['values']['weight'], $form_state['values']['display'], serialize($form_state['values']['urls']), serialize($form_state['values']['groups'])); 1092 db_query("INSERT INTO {ad_channel} (name, description, conid, weight, display, urls, groups, no_channel_weight) VALUES('%s', '%s', %d, %d, %d, '%s', '%s', %d)", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['conid'], $form_state['values']['weight'], $form_state['values']['display'], serialize($urls), serialize($form_state['values']['groups']), $form_state['values']['no_channel_weight']);
1025 drupal_set_message(t('The %name channel has been created.', array('%name' => $form_state['values']['name']))); 1093 drupal_set_message(t('The %name channel has been created.', array('%name' => $form_state['values']['name'])));
1026 break; 1094 break;
1027 case t('Update'): 1095 case t('Update'):
1028 $groups = array(); 1096 $groups = array();
1029 // Don't store information about groups that no longer exist. 1097 // Don't store information about groups that no longer exist.
1033 if (isset($form_state['values']['groups'][$ad_group->tid])) { 1101 if (isset($form_state['values']['groups'][$ad_group->tid])) {
1034 $groups[$ad_group->tid] = $form_state['values']['groups'][$ad_group->tid]; 1102 $groups[$ad_group->tid] = $form_state['values']['groups'][$ad_group->tid];
1035 } 1103 }
1036 } 1104 }
1037 } 1105 }
1038 db_query("UPDATE {ad_channel} SET name = '%s', description = '%s', conid = %d, weight = %d, display = %d, urls = '%s', groups = '%s' WHERE chid = %d", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['conid'], $form_state['values']['weight'], $form_state['values']['display'], serialize($form_state['values']['urls']), serialize($groups), $form_state['values']['chid']); 1106 db_query("UPDATE {ad_channel} SET name = '%s', description = '%s', conid = %d, weight = %d, display = %d, urls = '%s', groups = '%s', no_channel_weight = %d WHERE chid = %d", $form_state['values']['name'], $form_state['values']['description'], $form_state['values']['conid'], $form_state['values']['weight'], $form_state['values']['display'], serialize($urls), serialize($groups), $form_state['values']['no_channel_weight'], $form_state['values']['chid']);
1039 drupal_set_message(t('The %name channel has been updated.', array('%name' => $form_state['values']['name']))); 1107 drupal_set_message(t('The %name channel has been updated.', array('%name' => $form_state['values']['name'])));
1040 break; 1108 break;
1041 case t('Delete'): 1109 case t('Delete'):
1042 drupal_goto('admin/content/ad/channel/channel/'. $form_state['values']['chid'] .'/delete'); 1110 drupal_goto('admin/content/ad/channel/channel/'. $form_state['values']['chid'] .'/delete');
1043 } 1111 }
1045 } 1113 }
1046 1114
1047 /** 1115 /**
1048 * Confirm whether or not to delete container, and the contained channels. 1116 * Confirm whether or not to delete container, and the contained channels.
1049 */ 1117 */
1050 function ad_channel_admin_confirm_delete_channel($chid) { 1118 function ad_channel_admin_confirm_delete_channel($form_state, $channel) {
1051 $form = array(); 1119 $form = array();
1052
1053 $channel = _ad_channel_get_channels($chid);
1054 1120
1055 $form['chid'] = array( 1121 $form['chid'] = array(
1056 '#type' => 'value', 1122 '#type' => 'value',
1057 '#value' => $chid, 1123 '#value' => $channel->chid,
1058 ); 1124 );
1059 1125
1060 return confirm_form( 1126 return confirm_form(
1061 $form, 1127 $form,
1062 t('Are you sure you want to delete the %name channel?', array('%name' => $channel->name)), 1128 t('Are you sure you want to delete the %name channel?', array('%name' => $channel->name)),
1076 drupal_set_message(t('The %name channel has been deleted.', array('%name' => $channel->name))); 1142 drupal_set_message(t('The %name channel has been deleted.', array('%name' => $channel->name)));
1077 } 1143 }
1078 drupal_goto('admin/content/ad/channel'); 1144 drupal_goto('admin/content/ad/channel');
1079 } 1145 }
1080 1146
1147 /**
1148 * Available probabilities.
1149 */
1150 function _ad_channel_probabilities() {
1151 return array(
1152 25 => t('1/4'),
1153 33 => t('1/3'),
1154 50 => t('1/2'),
1155 100 => t('1'),
1156 200 => t('2'),
1157 300 => t('3'),
1158 400 => t('4'),
1159 );
1160 }