diff 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
line wrap: on
line diff
--- a/channel/ad_channel.module	Thu Apr 02 15:28:21 2009 +0000
+++ b/channel/ad_channel.module	Wed Apr 15 07:58:32 2009 +0000
@@ -1,5 +1,5 @@
 <?php
-// $Id: ad_channel.module,v 1.1.4.15 2009/03/31 00:59:21 jeremy Exp $
+// $Id: ad_channel.module,v 1.1.4.20 2009/04/09 22:04:49 jeremy Exp $
 
 /**
  * @file
@@ -81,26 +81,43 @@
     'access arguments' => array('administer channels'),
     'type' => MENU_LOCAL_TASK,
     'weight' => 9);
-/*
-  else {
-    TODO:
-      $items[] = array(
-        'path' => "admin/content/ad/channel/container/$conid/delete",
-        'callback' => 'drupal_get_form',
-        'callback arguments' => array('ad_channel_admin_confirm_delete_container', $conid),
-        'type' => MENU_CALLBACK);
-      $items[] = array(
-        'path' => "admin/content/ad/channel/channel/$chid/delete",
-        'callback' => 'drupal_get_form',
-        'callback arguments' => array('ad_channel_admin_confirm_delete_channel', $chid),
-        'type' => MENU_CALLBACK);
-    }
-*/
+  $items['admin/content/ad/channel/container/%ad_channel_container/delete'] = array(
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ad_channel_admin_confirm_delete_container', 5),
+    'access arguments' => array('administer channels'),
+    'type' => MENU_CALLBACK);
+  $items['admin/content/ad/channel/channel/%ad_channel_channel/delete'] = array(
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('ad_channel_admin_confirm_delete_channel', 5),
+    'access arguments' => array('administer channels'),
+    'type' => MENU_CALLBACK);
 
   return $items;
 }
 
 /**
+ * Load a specified container.
+ */
+function ad_channel_container_load($conid = 0) {
+  static $containers = array();
+  if (!isset($containers[$conid])) {
+    $containers[$conid] = db_fetch_object(db_query('SELECT * FROM {ad_channel_container} WHERE conid = %d', $conid));
+  }
+  return $containers[$conid];
+}
+
+/**
+ * Load a specified channel.
+ */
+function ad_channel_channel_load($chid = 0) {
+  static $channels = array();
+  if (!isset($channels[$chid])) {
+    $channels[$chid] = db_fetch_object(db_query('SELECT * FROM {ad_channel} WHERE chid = %d', $chid));
+  }
+  return $channels[$chid];
+}
+
+/**
  * Implementation of hook_form_alter().
  * Generate a form for selecting channels to associate with an advertisement.
  */
@@ -152,14 +169,14 @@
     }
     $form['priority'] = array(
       '#type' => 'fieldset',
-      '#access' => user_access('configure ad premiere status'),
+      '#access' => user_access('configure ad premier status'),
       '#title' => t('Priority'),
       '#collapsible' => TRUE,
       '#collapsed' => FALSE,
     );
     $form['priority']['premiere'] = array(
       '#type' => 'checkbox',
-      '#access' => user_access('configure ad premiere status'),
+      '#access' => user_access('configure ad premier status'),
       '#title' => t('Premiere'),
       '#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.'),
       '#default_value' => isset($node->premiere) ? $node->premiere : FALSE,
@@ -461,7 +478,7 @@
     }
   }
   $channels = array();
-  $result = db_query('SELECT chid, display, urls FROM {ad_channel}');
+  $result = db_query('SELECT chid, display, urls, no_channel_weight FROM {ad_channel}');
   while ($channel = db_fetch_object($result)) {
     $channels[$channel->chid] = $channel;
   }
@@ -506,6 +523,14 @@
     '#default_value' => variable_get('ad_channel_admin_list', AD_CHANNEL_LIST_CHANNEL),
     '#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").'),
   );
+  $options = array(0 => t('No limit')) + drupal_map_assoc(array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
+  $form['ad_channel_ad_limit'] = array(
+    '#type' => 'select',
+    '#title' => t('Maximum number of channels that can be assigned to a single ad'),
+    '#options' => $options,
+    '#default_value' => variable_get('ad_channel_ad_limit', 0),
+    '#description' => t('Optionally limit the number of channels that a single advertisement can be assigned to.'),
+  );
 
   return system_settings_form($form);
 }
@@ -565,7 +590,7 @@
   foreach ($channels as $chid) {
     db_query('INSERT INTO {ad_channel_node} (chid, nid) VALUES(%d, %d)', $chid, $node->nid);
   }
-  if (user_access('configure ad premiere status')) {
+  if (user_access('configure ad premier status')) {
     db_query('UPDATE {ad_priority} SET priority = %d WHERE aid = %d', isset($node->premiere) ? $node->premiere : 0, $node->nid);
     if (!db_affected_rows()) {
       db_query('INSERT INTO {ad_priority} (aid, priority) VALUES(%d, %d)', $node->nid, isset($node->premiere) ? $node->premiere : 0);
@@ -588,8 +613,19 @@
  */
 function _ad_channel_validate_nodes($node) {
   $channels = _ad_channel_get_enabled($node);
+  $limit = variable_get('ad_channel_ad_limit', 0);
+  if ($limit && sizeof($channels) > $limit) {
+    $quantity_error = TRUE;
+  }
+  else {
+    $quantity_error = FALSE;
+  }
   foreach ($channels as $chid) {
     $channel = _ad_channel_get_channels($chid);
+    if ($quantity_error) {
+      $quantity_error = FALSE;
+      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'))));
+    }
     $taxonomy = is_array($node->taxonomy) ? $node->taxonomy : array();
     $groups = unserialize($channel->groups);
     if (!empty($groups)) {
@@ -839,14 +875,12 @@
 /**
  * Confirm whether or not to delete container, and the contained channels.
  */
-function ad_channel_admin_confirm_delete_container($conid) {
+function ad_channel_admin_confirm_delete_container($form_state, $container) {
   $form = array();
 
-  $container = _ad_channel_get_containers($conid);
-
   $form['conid'] = array(
     '#type' => 'value',
-    '#value' => $conid,
+    '#value' => $container->conid,
   );
 
   return confirm_form(
@@ -975,6 +1009,29 @@
     );
   }
 
+  $collapsed = (variable_get('ad_channel_display', 0) == 1) ? 0 : 1;
+  $form['nonchannel_rules'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Not-in-channel ad rules'),
+    '#collapsible' => TRUE,
+    '#collapsed' => $collapsed,
+  );
+  if ($collapsed) {
+    $form['nonchannel_rules']['info'] = array(
+      '#type' => 'markup',
+      '#prefix' => '<div>',
+      '#suffix' => '</div>',
+      '#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')),
+    );
+  }
+  $form['nonchannel_rules']['no_channel_weight'] = array(
+    '#type' => 'select',
+    '#title' => t('Probability'),
+    '#options' => _ad_channel_probabilities(),
+    '#default_value' => $channel->no_channel_weight ? $channel->no_channel_weight : 100,
+    '#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.'),
+  );
+
   if ($chid) {
     $form['update'] = array(
       '#type' => 'submit',
@@ -1019,9 +1076,20 @@
  * Save the channel.
  */
 function ad_channel_admin_channel_submit($form, &$form_state) {
+  // remove extraneous white space from url list which can break matching
+  $url_array = explode("\n", $form_state['values']['urls']);
+  if (is_array($url_array)) {
+    foreach ($url_array as $url) {
+      $urls[] = trim($url);
+    }
+    $urls = implode("\n", $urls);
+  }
+  else {
+    $urls = '';
+  }
   switch ($form_state['values']['op']) {
     case t('Create'):
-      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']));
+      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']);
       drupal_set_message(t('The %name channel has been created.', array('%name' => $form_state['values']['name'])));
       break;
     case t('Update'):
@@ -1035,7 +1103,7 @@
           }
         }
       }
-      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']);
+      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']);
       drupal_set_message(t('The %name channel has been updated.', array('%name' => $form_state['values']['name'])));
       break;
     case t('Delete'):
@@ -1047,14 +1115,12 @@
 /**
  * Confirm whether or not to delete container, and the contained channels.
  */
-function ad_channel_admin_confirm_delete_channel($chid) {
+function ad_channel_admin_confirm_delete_channel($form_state, $channel) {
   $form = array();
 
-  $channel = _ad_channel_get_channels($chid);
-
   $form['chid'] = array(
     '#type' => 'value',
-    '#value' => $chid,
+    '#value' => $channel->chid,
   );
 
   return confirm_form(
@@ -1078,3 +1144,17 @@
   drupal_goto('admin/content/ad/channel');
 }
 
+/**
+ * Available probabilities.
+ */
+function _ad_channel_probabilities() {
+  return array(
+    25 => t('1/4'),
+    33 => t('1/3'),
+    50 => t('1/2'),
+    100 => t('1'),
+    200 => t('2'),
+    300 => t('3'),
+    400 => t('4'),
+  );
+}