Index: openads.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openads/openads.install,v
retrieving revision 1.1
diff -u -r1.1 openads.install
--- openads.install	9 Apr 2008 09:03:51 -0000	1.1
+++ openads.install	13 Jun 2009 15:23:41 -0000
@@ -2,6 +2,14 @@
 // $Id: openads.install,v 1.1 2008/04/09 09:03:51 jaydub Exp $
 
 /**
+ * Implementation of hook_install()
+ */
+function openads_install() {
+  // Create tables
+  drupal_install_schema('openads');
+}
+
+/**
  * Implementation of hook_uninstall().
  */
 function openads_uninstall() {
@@ -18,8 +26,39 @@
   variable_del('openads_xmlrpc_port');
   variable_del('openads_xmlrpc_timeout');
   variable_del('openads_xmlrpc_use_ssl');
+  variable_del('openads_source_vocabulary');
   db_query("DELETE FROM {variable} WHERE name LIKE 'openads_zones_code_%'");
   db_query("DELETE FROM {variable} WHERE name LIKE 'openads_zones_id_%'");
   db_query("DELETE FROM {variable} WHERE name LIKE 'openads_zones_name_%'");
   db_query("DELETE FROM {variable} WHERE name LIKE 'openads_zones_roles_%'");
+  
+  drupal_uninstall_schema('openads');
+}
+
+/**
+ * Implementation of hook_schema()
+ * 
+ */
+function openads_schema() {  
+  $schema['openads_term_source'] = array(
+    'description' => t('Associate a term and a "source" parameter for an OpenX channel'),
+    'fields' => array(
+      'tid' => array(
+        'description' => t('Term ID'),
+        'type' => 'int',
+        'unsigned' => TRUE,
+        'not null' => TRUE
+      ),
+      'source' => array(
+        'description' => t('"source" parameter to use for this term'),
+        'type' => 'varchar',
+        'length' => '255',
+        'not null' => TRUE,
+        'default' => '',
+      ),
+    ),
+    'primary key' => array('tid')
+  );
+  
+  return $schema;
 }
Index: openads.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openads/openads.js,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 openads.js
--- openads.js	14 Apr 2008 11:15:31 -0000	1.1.2.1
+++ openads.js	13 Jun 2009 10:23:58 -0000
@@ -4,13 +4,14 @@
 var m3_r=Math.floor(Math.random()*99999999999);
 if (!document.MAX_used) document.MAX_used = ',';
 
-function az_adjs(z, n) {
-  var az="<scr"+"ipt type='text/javascript' ";
-  az+="src='"+m3_u+"?zoneid="+z+"&amp;withtext="+az_wt+"&amp;cb="+m3_r;
-  if (!document.MAX_used != ',') az+="&amp;exclude="+document.MAX_used;
-  if (document.referrer) az+="&amp;referer="+escape(document.referer);
-  if (document.content) az+="&context="+escape(document.context);
-  if (document.mmm_fo) az+="&amp;mmm_fo=1";
-  az+="'></scr"+"ipt>";
+function az_adjs(z, n, source) {
+  var az = "<scr" + "ipt type='text/javascript' ";
+  az += "src='" + m3_u + "?zoneid=" + z + "&amp;withtext=" + az_wt + "&amp;cb=" + m3_r;
+  if (source) az += "&amp;source=" + source;
+  if (!document.MAX_used != ',') az += "&amp;exclude=" + document.MAX_used;
+  if (document.referrer) az += "&amp;referer=" + escape(document.referer);
+  if (document.content) az += "&context=" + escape(document.context);
+  if (document.mmm_fo) az += "&amp;mmm_fo=1";
+  az += "'></scr" + "ipt>";
   document.write(az);
 }
Index: openads.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/openads/openads.module,v
retrieving revision 1.7.2.4.2.2
diff -u -r1.7.2.4.2.2 openads.module
--- openads.module	19 May 2008 06:22:53 -0000	1.7.2.4.2.2
+++ openads.module	13 Jun 2009 15:25:29 -0000
@@ -12,9 +12,10 @@
  *
  * @author Gerd Riesselmann
  * @author Jeff Warrington (jaydub) is new maintainer March 2008
+ * @author Dimitri De Franciscis
  *
  */
- 
+
 /**
  * Implementation of hook_help().
  */
@@ -69,294 +70,13 @@
     'access callback' => 'user_access',
     'access arguments' => array('administer site configuration'),
     'type' => MENU_NORMAL_ITEM, // optional
+    'file' => 'openads.admin.inc'
   );
 
   return $items;
 }
 
 /**
- * Implemenation of hook_settings().
- */
-function openads_admin_settings() {
-  $form = array();
-
-  $form['adserver'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Adserver settings'),
-    '#description' => t('Select which delivery method to use - Javascript, Remote, Local or XML-RPC. <strong>Note</strong> Javascript will be used if caching is enabled and the current user is not logged in.'),
-    '#collapsible' => TRUE,
-  );
-
-  if (variable_get('openads_delivery_method', 'js') == 'xmlrpc') {
-    $PEAR_missing = $openads_xmlrpc_missing = FALSE;
-    if (!@include('XML/RPC.php')) {
-      $PEAR_missing = TRUE;
-    }
-    if (!@include('openads-xmlrpc.inc.php')) {
-      $openads_xmlrpc_missing = TRUE;
-    }
-  }
-
-  $form['adserver']['openads_delivery_method'] = array(
-    '#type' => 'radios',
-    '#title' => t('Delivery Method'),
-    '#description' => t('Select the delivery method to use. <br/><strong>Note:</strong> To use the Local method OpenX and your banners must be stored on the same machine with the same top level domain. <br/><strong>Note:</strong> To use XML-RPC method you must have the PEAR XML-RPC package installed and the OpenX openads-xmlrpc.inc.php file must be in your PHP include path. '. ($PEAR_missing ? '<br/><span style="color:#f00;">PEAR XML-RPC appears to not be installed!</span> You must install the <a href="@link">PEAR XML-RPC</a> package.' : '') . ($openads_xmlrpc_missing ? '<br/><span style="color:#f00;">opeands-xmlrpc.inc.php file appears to not be installed!</span> You must put the file openads-xmlrpc.inc.php from the OpenX installation files into your PHP path.' : ''), array('@link' => 'http://pear.php.net/package/XML_RPC')),
-    '#options' => array('js' => 'Javascript', 'remote' => 'Remote', 'local' => 'Local', 'xmlrpc' => 'XML-RPC'),
-    '#default_value' => variable_get('openads_delivery_method', 'js'),
-  );
-
-  $form['adserver']['openads_use_text_below_image'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Enable OpenX "Text below Image"'),
-    '#description' => t('OpenX is able to show a user defined text below each banner. Click this checkbox to enable this feature. Only availble on Javascript, Local and XML-RPC delivery methods.'),
-    '#default_value' => variable_get('openads_use_text_below_image', 0),
-  );
-  $form['adserver']['server_access_paths'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('OpenX Server Access Paths'),
-    '#description' => t('This group of settings defines access paths to specific elements of your OpenX server. These settings are located on your OpenX server at <strong>Settings -> Main Settings -> Delivery Settings</strong>'),
-  );
-  $form['adserver']['server_access_paths']['openads_delivery_url'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_delivery_url', ''),
-    '#title' => t('The OpenX delivery url'),
-    '#description' => t('The OpenX server address, for example "ads.example.org/delivery". "http://" is automatically prefixed.'),
-    '#required' => TRUE,
-  );
-  $form['adserver']['server_access_paths']['openads_delivery_url_https'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_delivery_url_https', ''),
-    '#title' => t('The OpenX https delivery url'),
-    '#description' => t('The OpenX server address, for example "ads.example.org/delivery". "https://" is automatically prefixed.')
-  );
-
-  $form['adserver']['javascript_options'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Javascript options'),
-    '#description' => t('The following options affect the Javascript invocation method.'),
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['adserver']['javascript_options']['openads_js_delivery_filename'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_js_delivery_filename', 'ajs.php'),
-    '#title' => t('The OpenX JavaScript delivery filename'),
-    '#description' => t('The OpenX JavaScript delivery filename for example "ajs.php"'),
-    '#required' => TRUE,
-  );
-
-  $form['adserver']['remote_options'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Remote options'),
-    '#description' => t('The following options affect the Remote invocation method.'),
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['adserver']['remote_options']['openads_remote_delivery_click_filename'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_remote_delivery_click_filename', 'ck.php'),
-    '#title' => t('The OpenX Remote delivery click filename'),
-    '#description' => t('The OpenX Remote delivery click filename for example "ck.php"')
-  );
-  $form['adserver']['remote_options']['openads_remote_delivery_view_filename'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_remote_delivery_view_filename', 'avw.php'),
-    '#title' => t('The OpenX Remote delivery view filename'),
-    '#description' => t('The OpenX Remote delivery view filename for example "avw.php"')
-  );
-
-  $form['adserver']['local_options'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Local options'),
-    '#description' => t('The following options affect the Local invocation method.'),
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['adserver']['local_options']['openads_local_delivery_base_path'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_local_delivery_base_path', ''),
-    '#title' => t('The OpenX Local delivery base path'),
-    '#description' => t('The base path of the OpenX installation for example "/opt/www/openx.example.com"')
-  );
-  $form['adserver']['local_options']['openads_local_delivery_filename'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_local_delivery_filename', 'alocal.php'),
-    '#title' => t('The OpenX Local delivery filename'),
-    '#description' => t('The OpenX Local delivery filename for example "alocal.php"')
-  );
-
-  $form['adserver']['xmlrpc_options'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('XML-RPC options'),
-    '#description' => t('The following options affect the XML-RPC invocation method.'),
-    '#collapsible' => TRUE,
-    '#collapsed' => TRUE,
-  );
-  $form['adserver']['xmlrpc_options']['openads_xmlrpc_delivery_filename'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_xmlrpc_delivery_filename', 'axmlrpc.php'),
-    '#title' => t('The OpenX XML-RPC delivery filename'),
-    '#description' => t('The OpenX XML-RPC delivery filename for example "axmlrpc.php"')
-  );
-  $form['adserver']['xmlrpc_options']['openads_xmlrpc_port'] = array(
-    '#type' => 'textfield',
-    '#title' => t('XML-RPC Port'),
-    '#description' => t('If your XML-RPC server is on a non-standard port, specify it here.'),
-    '#default_value' => variable_get('openads_xmlrpc_port', 80),
-    '#size' => 4,
-  );
-  $form['adserver']['xmlrpc_options']['openads_xmlrpc_use_ssl'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Use SSL'),
-    '#description' => t('If you wish to use SSL to connect to your OpenX XML-RPC server, check this option'),
-    '#default_value' => variable_get('openads_xmlrpc_use_ssl', ''),
-  );
-  $form['adserver']['xmlrpc_options']['openads_xmlrpc_timeout'] = array(
-    '#type' => 'textfield',
-    '#title' => t('Timeout'),
-    '#description' => t('Set the timeout in seconds to connect to the OpenX XML-RPC server'),
-    '#default_value' => variable_get('openads_xmlrpc_timeout', 2),
-    '#size' => 4,
-  );
-
-  $form['publisher'] = array(
-    '#type' => 'fieldset',
-    '#title' => t('Publisher'),
-    '#description' => t('The following settings can be copied from the publisher page of your OpenX adserver.'),
-    '#collapsible' => TRUE,
-  );
-  $form['publisher']['openads_id'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_id', ''),
-    '#title' => t('Publisher ID'),
-    '#description' => t("This is the publisher ID of the publisher you wish to serve ads from."),
-    '#required' => TRUE,
-    '#size' => 6,
-  );
-    $form['publisher']['openads_num_zones'] = array(
-    '#type' => 'textfield',
-    '#default_value' => variable_get('openads_num_zones', '5'),
-    '#title' => t('The number of zones defined'),
-    '#description' => t('This is the number of zones you can enter below'),
-    '#size' => 6,
-    );
-  
-  $form['openads_zones'] = array(
-    '#theme' => 'openads_admin_settings_form',
-    '#type' => 'fieldset',
-    '#title' => t('Zones'),
-    '#description' => t('Configure your zones here. '.
-            '<ul>'.
-            '<li>Both zone id and code are generated by your adserver, so copy it from there.</li>'.
-            '<li>The name can be freely chosen, and you are able to invoke ads using this name later one.</li>'.
-            '<li>For each user role, you can either show (checked) or hide (unchecked) ad of the according zone</li>'.
-            '</ul>'.
-            'Once you have convigured your zones, you can invoke them within your templates using either the '.
-            'index number (0-4, for example, this is the first column) or by the names you gave your zones. '.
-            'The according code is <strong>&lt;?php print openads_invoke(0);&gt;</strong> or <strong>&lt;?php print openads_invoke("my zone name");&gt;</strong>'),
-    '#collapsible' => TRUE,
-  );
-  $zones = openads_get_zones();
-  $roles = user_roles();
-  $i = 0;
-  foreach ($zones as $zone) {
-    $form['openads_zones'][$i]['openads_zones_id_'. $i] = array(
-      '#type' => 'textfield',
-      '#default_value' => $zone['id'],
-      '#size' => 6,
-    );
-    $form['openads_zones'][$i]['openads_zones_name_'. $i] = array(
-      '#type' => 'textfield',
-      '#default_value' => $zone['name'],
-      '#size' => 20,
-    );
-    $form['openads_zones'][$i]['openads_zones_code_'. $i] = array(
-      '#type' => 'textfield',
-      '#default_value' => $zone['code'],
-      '#size' => 10,
-    );
-    foreach ($roles as $key => $display) {
-      $element_key = 'openads_zones_roles_'. $key .'_'. $i;
-      $form['openads_zones'][$i][$element_key] = array(
-        '#type' => 'checkbox',
-        '#default_value' => $zone['roles'][$key],
-      );
-    }
-    $i++;
-  }
-
-  return system_settings_form($form);
-}
-
-/**
- * admin_settings form validate
- */
-function openads_admin_settings_validate($form, &$form_state) {
-  switch ($form_state['values']['openads_delivery_method']) {
-    case 'remote':
-      if (!$form_state['values']['openads_remote_delivery_click_filename']) {
-        form_set_error('openads_remote_delivery_click_filename', t('For Remote delivery you must specifiy the OpenX remote delivery "click" filename.'));
-      }
-      if (!$form_state['values']['openads_remote_delivery_view_filename']) {
-        form_set_error('openads_remote_delivery_view_filename', t('For Remote delivery you must specifiy the OpenX remote delivery "view" filename.'));
-      }
-      break;
-    case 'local':
-      if (!$form_state['values']['openads_local_delivery_filename']) {
-        form_set_error('openads_local_delivery_filename', t('For Local delivery you must specifiy the OpenX local delivery filename.'));
-      }
-      if (!file_exists($form_state['values']['openads_local_delivery_base_path'] .'/www/delivery/'. $form_state['values']['openads_local_delivery_filename'])) {
-        form_set_error('openads_local_delivery_base_path', t('For Local delivery you must specifiy a valid path to the OpenX installation.'));
-      }
-      break;
-    case 'xmlrpc':
-      if (!$form_state['values']['openads_xmlrpc_delivery_filename']) {
-        form_set_error('openads_xmlrpc_delivery_filename', t('For XML-RPC delivery you must specifiy the OpenX XML-RPC delivery filename.'));
-      }
-      if (!is_numeric($form_state['values']['openads_xmlrpc_port'])) {
-        form_set_error('openads_xmlrpc_port', t('For XML-RPC delivery you must specifiy a valid number for the OpenX XML-RPC port number.'));
-      }
-      if (!is_numeric($form_state['values']['openads_xmlrpc_timeout'])) {
-        form_set_error('openads_xmlrpc_timeout', t('For XML-RPC delivery you must specifiy a valid number for the OpenX XML-RPC connection timeout.'));
-      }
-      break;
-  }
-}
-
-/**
- * Theme function for the admin settings form
- */
-function theme_openads_admin_settings_form($form) {
-  $roles = user_roles();
-  foreach ($form as $name => $element) {
-    $id_col = "openads_zones_id_$name";
-    $code_col = "openads_zones_code_$name";
-    $name_col = "openads_zones_name_$name";
-    if (isset($element[$id_col]) && is_array($element[$id_col])) {
-      $arr = array(
-        check_plain($name),
-        drupal_render($element[$id_col]),
-        drupal_render($element[$name_col]),
-        drupal_render($element[$code_col]),
-      );
-      foreach ($roles as $key => $rolename) {
-        $element_key = 'openads_zones_roles_'. $key .'_'. $name;
-        $arr[] = drupal_render($element[$element_key]);
-      }
-      $rows[] = $arr;
-    }
-  }
-
-  $header = array(t('Number'), t('Zone ID'), t('Name'), t('Code'));
-  foreach ($roles  as $key => $rolename) {
-    $header[] = $rolename;
-  }
-  $output .= theme('table', $header, $rows);
-
-  return $output;
-}
-
-/**
  * Implementation of hook_block().
  */
 function openads_block($op = 'list', $delta = 0, $edit = array()) {
@@ -503,7 +223,6 @@
       return '';
     }
   }
-  
   return theme('openads_invoke', $id, $code);
 }
 
@@ -513,12 +232,30 @@
 function theme_openads_invoke($zoneid, $code) {
   global $user;
   $delivery_method = variable_get('openads_delivery_method', 'js');
-
+  
   if (_openads_is_cache_page() || $delivery_method == 'js') {
-    $out = "
-    <script type=\"text/javascript\"><!--
-      az_adjs($zoneid, '$code');
-    --></script>";
+    $node = menu_get_object();
+    
+    $out = "";
+    $out .= "\n<script type=\"text/javascript\"><!--";
+    $out .= "\naz_adjs($zoneid, '$code'";
+    
+    if (isset($node)) {
+      $source = NULL;
+      $vid = variable_get('openads_source_vocabulary', NULL);
+      if ($vid) {
+        foreach($node->taxonomy as $term) {
+          if ($term->vid == $vid) {
+            $source = $term->name;
+            break;
+          }
+        }
+      }
+      if ($source != NULL) {
+        $out .= ", '" . check_plain($source) . "'";
+      }
+    }
+    $out .= ");\n--></script>";
     return $out;
   }
 
@@ -624,3 +361,99 @@
   }
   return FALSE;
 }
+
+/**
+ * Implementation of hook_form_alter().
+ * 
+ */
+function openads_form_alter(&$form, &$form_state, $form_id) {
+  if ($form_id == 'taxonomy_form_term') {
+    $vid = variable_get('openads_source_vocabulary', NULL);
+    
+    if ($vid != NULL && $vid == $form['vid']['#value']) {
+      $form['openads'] = array(
+        '#type' => 'fieldset',
+        '#title' => t('OpenX (Openads) adserver configuration'),
+        '#collapsible' => TRUE,
+        '#weight' => 1,
+      );
+      
+      $current = NULL;
+      if (isset($form['tid'])) {
+        $current = db_result(db_query("SELECT ts.source FROM {openads_term_source} ts WHERE tid = %d", $form['tid']['#value']));
+      }
+      
+      $form['openads']['source'] = array(
+        '#type' => 'textfield',
+        '#title' => t('"source" parameter'),
+        '#description' => t('This value will be used to add a "source" parameter on
+   each OpenX banner invocation for nodes associated to this term'),
+        '#default_value' => $current,
+        '#maxlength' => 255,
+      ); 
+      
+      $form['submit']['#weight'] = 2;
+      $form['delete']['#weight'] = 3;
+    }
+  }
+}
+
+/**
+ * Implementation of hook_taxonomy().
+ * 
+ */
+function openads_taxonomy($op, $type, $array = NULL) {
+  $vid = variable_get('openads_source_vocabulary', NULL);
+  if (!isset($vid)) {
+    return;
+  }
+  
+  if ($array['vid'] == $vid) {
+    
+    if ($type == 'vocabulary' && $op == 'delete') {
+      variable_del('openads_source_vocabulary');
+      
+    } else if ($type == 'term') {
+      $source = $array['source'];
+      $tid = $array['tid'];
+      
+      if ($op == 'insert' && isset($source) && $source != ""){
+        _openads_taxonomy_insert_term($tid, $source);
+      }
+      else if ($op == 'update') {
+        if (!isset($source) || $source == "") {
+          // default to delete
+          _openads_taxonomy_delete_term($tid);
+        }
+        else {
+          _openads_taxonomy_insertOrUpdate_term($tid, $source);
+        }
+      }
+      else if ($op == 'delete') {
+        _openads_taxonomy_delete_term($tid);
+      }
+    }
+  }
+}
+
+function _openads_taxonomy_insert_term($tid, $source) {
+  watchdog('openads', 'Set source=%source for term %tid', array('%source' => $source, '%tid' => $tid));
+  db_query("INSERT INTO {openads_term_source} (tid, source) VALUES (%d, '%s')", $tid, $source);
+}
+
+function _openads_taxonomy_insertOrUpdate_term($tid, $source) {
+  $existing = db_result(db_query("SELECT count(ts.tid) from {openads_term_source} ts WHERE ts.tid = %d", $tid));
+  if (isset($existing) && $existing > 0) {
+    db_query("UPDATE {openads_term_source} ts SET ts.source = '%s' WHERE ts.tid = %d", $source, $tid);
+    watchdog('openads', 'Update source=%source for term %tid', array('%source' => $source, '%tid' => $tid));
+  }
+  else {
+    // default to insert
+    _openads_taxonomy_insert_term($tid, $source);
+  }
+}
+
+function _openads_taxonomy_delete_term($tid) {
+  watchdog('openads', 'Remove source=%source for term %tid', array('%source' => $source, '%tid' => $tid));
+  db_query('DELETE FROM {openads_term_source} WHERE tid = %d', $tid);
+}
\ No newline at end of file
--- openads.admin.inc
+++ openads.admin.inc
@@ -0,0 +1,330 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Integrates Drupal with OpenX Ad server
+ *
+ * Administrative functions.
+ *
+ * Created on 12.01.2007
+ *
+ * @author Dimitri De Franciscis
+ *
+ */
+
+
+/**
+ * Implemenation of hook_settings().
+ */
+function openads_admin_settings() {
+  $form = array();
+
+  $form['adserver'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Adserver settings'),
+    '#description' => t('Select which delivery method to use - Javascript, Remote, Local or XML-RPC. <strong>Note</strong> Javascript will be used if caching is enabled and the current user is not logged in.'),
+    '#collapsible' => TRUE,
+  );
+
+  if (variable_get('openads_delivery_method', 'js') == 'xmlrpc') {
+    $PEAR_missing = $openads_xmlrpc_missing = FALSE;
+    if (!@include('XML/RPC.php')) {
+      $PEAR_missing = TRUE;
+    }
+    if (!@include('openads-xmlrpc.inc.php')) {
+      $openads_xmlrpc_missing = TRUE;
+    }
+  }
+
+  $form['adserver']['openads_delivery_method'] = array(
+    '#type' => 'radios',
+    '#title' => t('Delivery Method'),
+    '#description' => t('Select the delivery method to use. <br/><strong>Note:</strong> To use the Local method OpenX and your banners must be stored on the same machine with the same top level domain. <br/><strong>Note:</strong> To use XML-RPC method you must have the PEAR XML-RPC package installed and the OpenX openads-xmlrpc.inc.php file must be in your PHP include path. '. ($PEAR_missing ? '<br/><span style="color:#f00;">PEAR XML-RPC appears to not be installed!</span> You must install the <a href="@link">PEAR XML-RPC</a> package.' : '') . ($openads_xmlrpc_missing ? '<br/><span style="color:#f00;">opeands-xmlrpc.inc.php file appears to not be installed!</span> You must put the file openads-xmlrpc.inc.php from the OpenX installation files into your PHP path.' : ''), array('@link' => 'http://pear.php.net/package/XML_RPC')),
+    '#options' => array('js' => 'Javascript', 'remote' => 'Remote', 'local' => 'Local', 'xmlrpc' => 'XML-RPC'),
+    '#default_value' => variable_get('openads_delivery_method', 'js'),
+  );
+
+  $form['adserver']['openads_use_text_below_image'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Enable OpenX "Text below Image"'),
+    '#description' => t('OpenX is able to show a user defined text below each banner. Click this checkbox to enable this feature. Only availble on Javascript, Local and XML-RPC delivery methods.'),
+    '#default_value' => variable_get('openads_use_text_below_image', 0),
+  );
+  $form['adserver']['server_access_paths'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('OpenX Server Access Paths'),
+    '#description' => t('This group of settings defines access paths to specific elements of your OpenX server. These settings are located on your OpenX server at <strong>Settings -> Main Settings -> Delivery Settings</strong>'),
+  );
+  $form['adserver']['server_access_paths']['openads_delivery_url'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_delivery_url', ''),
+    '#title' => t('The OpenX delivery url'),
+    '#description' => t('The OpenX server address, for example "ads.example.org/delivery". "http://" is automatically prefixed.'),
+    '#required' => TRUE,
+  );
+  $form['adserver']['server_access_paths']['openads_delivery_url_https'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_delivery_url_https', ''),
+    '#title' => t('The OpenX https delivery url'),
+    '#description' => t('The OpenX server address, for example "ads.example.org/delivery". "https://" is automatically prefixed.')
+  );
+
+  $form['adserver']['javascript_options'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Javascript options'),
+    '#description' => t('The following options affect the Javascript invocation method.'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['adserver']['javascript_options']['openads_js_delivery_filename'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_js_delivery_filename', 'ajs.php'),
+    '#title' => t('The OpenX JavaScript delivery filename'),
+    '#description' => t('The OpenX JavaScript delivery filename for example "ajs.php"'),
+    '#required' => TRUE,
+  );
+
+  $form['adserver']['remote_options'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Remote options'),
+    '#description' => t('The following options affect the Remote invocation method.'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['adserver']['remote_options']['openads_remote_delivery_click_filename'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_remote_delivery_click_filename', 'ck.php'),
+    '#title' => t('The OpenX Remote delivery click filename'),
+    '#description' => t('The OpenX Remote delivery click filename for example "ck.php"')
+  );
+  $form['adserver']['remote_options']['openads_remote_delivery_view_filename'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_remote_delivery_view_filename', 'avw.php'),
+    '#title' => t('The OpenX Remote delivery view filename'),
+    '#description' => t('The OpenX Remote delivery view filename for example "avw.php"')
+  );
+
+  $form['adserver']['local_options'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Local options'),
+    '#description' => t('The following options affect the Local invocation method.'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['adserver']['local_options']['openads_local_delivery_base_path'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_local_delivery_base_path', ''),
+    '#title' => t('The OpenX Local delivery base path'),
+    '#description' => t('The base path of the OpenX installation for example "/opt/www/openx.example.com"')
+  );
+  $form['adserver']['local_options']['openads_local_delivery_filename'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_local_delivery_filename', 'alocal.php'),
+    '#title' => t('The OpenX Local delivery filename'),
+    '#description' => t('The OpenX Local delivery filename for example "alocal.php"')
+  );
+
+  $form['adserver']['xmlrpc_options'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('XML-RPC options'),
+    '#description' => t('The following options affect the XML-RPC invocation method.'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+  $form['adserver']['xmlrpc_options']['openads_xmlrpc_delivery_filename'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_xmlrpc_delivery_filename', 'axmlrpc.php'),
+    '#title' => t('The OpenX XML-RPC delivery filename'),
+    '#description' => t('The OpenX XML-RPC delivery filename for example "axmlrpc.php"')
+  );
+  $form['adserver']['xmlrpc_options']['openads_xmlrpc_port'] = array(
+    '#type' => 'textfield',
+    '#title' => t('XML-RPC Port'),
+    '#description' => t('If your XML-RPC server is on a non-standard port, specify it here.'),
+    '#default_value' => variable_get('openads_xmlrpc_port', 80),
+    '#size' => 4,
+  );
+  $form['adserver']['xmlrpc_options']['openads_xmlrpc_use_ssl'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Use SSL'),
+    '#description' => t('If you wish to use SSL to connect to your OpenX XML-RPC server, check this option'),
+    '#default_value' => variable_get('openads_xmlrpc_use_ssl', ''),
+  );
+  $form['adserver']['xmlrpc_options']['openads_xmlrpc_timeout'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Timeout'),
+    '#description' => t('Set the timeout in seconds to connect to the OpenX XML-RPC server'),
+    '#default_value' => variable_get('openads_xmlrpc_timeout', 2),
+    '#size' => 4,
+  );
+
+  $form['publisher'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Publisher'),
+    '#description' => t('The following settings can be copied from the publisher page of your OpenX adserver.'),
+    '#collapsible' => TRUE,
+  );
+  $form['publisher']['openads_id'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_id', ''),
+    '#title' => t('Publisher ID'),
+    '#description' => t("This is the publisher ID of the publisher you wish to serve ads from."),
+    '#required' => TRUE,
+    '#size' => 6,
+  );
+    $form['publisher']['openads_num_zones'] = array(
+    '#type' => 'textfield',
+    '#default_value' => variable_get('openads_num_zones', '5'),
+    '#title' => t('The number of zones defined'),
+    '#description' => t('This is the number of zones you can enter below'),
+    '#size' => 6,
+    );
+  
+  $form['openads_zones'] = array(
+    '#theme' => 'openads_admin_settings_form',
+    '#type' => 'fieldset',
+    '#title' => t('Zones'),
+    '#description' => t('Configure your zones here. '.
+            '<ul>'.
+            '<li>Both zone id and code are generated by your adserver, so copy it from there.</li>'.
+            '<li>The name can be freely chosen, and you are able to invoke ads using this name later one.</li>'.
+            '<li>For each user role, you can either show (checked) or hide (unchecked) ad of the according zone</li>'.
+            '</ul>'.
+            'Once you have convigured your zones, you can invoke them within your templates using either the '.
+            'index number (0-4, for example, this is the first column) or by the names you gave your zones. '.
+            'The according code is <strong>&lt;?php print openads_invoke(0);&gt;</strong> or <strong>&lt;?php print openads_invoke("my zone name");&gt;</strong>'),
+    '#collapsible' => TRUE,
+  );
+  $zones = openads_get_zones();
+  $roles = user_roles();
+  $i = 0;
+  foreach ($zones as $zone) {
+    $form['openads_zones'][$i]['openads_zones_id_'. $i] = array(
+      '#type' => 'textfield',
+      '#default_value' => $zone['id'],
+      '#size' => 6,
+    );
+    $form['openads_zones'][$i]['openads_zones_name_'. $i] = array(
+      '#type' => 'textfield',
+      '#default_value' => $zone['name'],
+      '#size' => 20,
+    );
+    $form['openads_zones'][$i]['openads_zones_code_'. $i] = array(
+      '#type' => 'textfield',
+      '#default_value' => $zone['code'],
+      '#size' => 10,
+    );
+    foreach ($roles as $key => $display) {
+      $element_key = 'openads_zones_roles_'. $key .'_'. $i;
+      $form['openads_zones'][$i][$element_key] = array(
+        '#type' => 'checkbox',
+        '#default_value' => $zone['roles'][$key],
+      );
+    }
+    $i++;
+  }
+  
+  // Channel targeting with "source" parameter
+  $form['openads_channel'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Channel targeting'),
+    '#collapsible' => TRUE,
+  );
+  
+  // show only vocabularies WITHOUT multiple choices
+  $vocabularies = db_query("SELECT v.vid, v.name from {vocabulary} v where v.multiple = 0 and tags = 0");
+  $options = array();
+  while($data = db_fetch_object($vocabularies)) {
+    $options[$data->vid] = $data->name;
+  }
+  
+  if (count($options) == 0) {
+    $form['openads_channel']['notice'] = array(
+      '#prefix' => '<div>',
+      '#value' => t('Channel targeting: no suitable vocabularies found'),
+      '#suffix' => '</div>',
+    );
+  }
+  else {
+    $form['openads_channel']['openads_source_vocabulary'] = array(
+      '#type' => 'radios',
+      '#title' => t('Channel targeting'),
+      '#options' => $options,
+      '#default_value' => variable_get('openads_source_vocabulary', NULL),
+      '#description' => t('Terms of the specified vocabulary will have an additional field "source",
+   which is the "source" parameter used by OpenX. Only one source parameter is allowed in OpenX, so
+   only single-choice vocabularies can be used'),
+    );
+  }
+
+  return system_settings_form($form);
+}
+
+/**
+ * admin_settings form validate
+ */
+function openads_admin_settings_validate($form, &$form_state) {
+  switch ($form_state['values']['openads_delivery_method']) {
+    case 'remote':
+      if (!$form_state['values']['openads_remote_delivery_click_filename']) {
+        form_set_error('openads_remote_delivery_click_filename', t('For Remote delivery you must specifiy the OpenX remote delivery "click" filename.'));
+      }
+      if (!$form_state['values']['openads_remote_delivery_view_filename']) {
+        form_set_error('openads_remote_delivery_view_filename', t('For Remote delivery you must specifiy the OpenX remote delivery "view" filename.'));
+      }
+      break;
+    case 'local':
+      if (!$form_state['values']['openads_local_delivery_filename']) {
+        form_set_error('openads_local_delivery_filename', t('For Local delivery you must specifiy the OpenX local delivery filename.'));
+      }
+      if (!file_exists($form_state['values']['openads_local_delivery_base_path'] .'/www/delivery/'. $form_state['values']['openads_local_delivery_filename'])) {
+        form_set_error('openads_local_delivery_base_path', t('For Local delivery you must specifiy a valid path to the OpenX installation.'));
+      }
+      break;
+    case 'xmlrpc':
+      if (!$form_state['values']['openads_xmlrpc_delivery_filename']) {
+        form_set_error('openads_xmlrpc_delivery_filename', t('For XML-RPC delivery you must specifiy the OpenX XML-RPC delivery filename.'));
+      }
+      if (!is_numeric($form_state['values']['openads_xmlrpc_port'])) {
+        form_set_error('openads_xmlrpc_port', t('For XML-RPC delivery you must specifiy a valid number for the OpenX XML-RPC port number.'));
+      }
+      if (!is_numeric($form_state['values']['openads_xmlrpc_timeout'])) {
+        form_set_error('openads_xmlrpc_timeout', t('For XML-RPC delivery you must specifiy a valid number for the OpenX XML-RPC connection timeout.'));
+      }
+      break;
+  }
+}
+
+/**
+ * Theme function for the admin settings form
+ */
+function theme_openads_admin_settings_form($form) {
+  $roles = user_roles();
+  foreach ($form as $name => $element) {
+    $id_col = "openads_zones_id_$name";
+    $code_col = "openads_zones_code_$name";
+    $name_col = "openads_zones_name_$name";
+    if (isset($element[$id_col]) && is_array($element[$id_col])) {
+      $arr = array(
+        check_plain($name),
+        drupal_render($element[$id_col]),
+        drupal_render($element[$name_col]),
+        drupal_render($element[$code_col]),
+      );
+      foreach ($roles as $key => $rolename) {
+        $element_key = 'openads_zones_roles_'. $key .'_'. $name;
+        $arr[] = drupal_render($element[$element_key]);
+      }
+      $rows[] = $arr;
+    }
+  }
+
+  $header = array(t('Number'), t('Zone ID'), t('Name'), t('Code'));
+  foreach ($roles  as $key => $rolename) {
+    $header[] = $rolename;
+  }
+  $output .= theme('table', $header, $rows);
+
+  return $output;
+}


