? textlinkads.module-multiple-inventory-support.patch
? tla_multiple_pages.patch
Index: textlinkads.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/textlinkads/textlinkads.install,v
retrieving revision 1.2
diff -u -p -r1.2 textlinkads.install
--- textlinkads.install	8 Feb 2007 20:01:10 -0000	1.2
+++ textlinkads.install	8 Aug 2007 16:35:38 -0000
@@ -15,6 +15,7 @@ function textlinkads_install() {
         rsstext varchar(256) NOT NULL,
         rssbeforetext varchar(256) NOT NULL,
         rssaftertext varchar(256) NOT NULL,
+        inventory_key varchar(256 NOT NULL,
         PRIMARY KEY  (tlid)
       ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
       break;
@@ -29,6 +30,7 @@ function textlinkads_install() {
         rsstext varchar(256) NOT NULL,
         rssbeforetext varchar(256) NOT NULL,
         rssaftertext varchar(256) NOT NULL,
+        inventory_key varchar(256 NOT NULL,
         PRIMARY KEY (tlid)
       );");
       break;
@@ -43,3 +45,19 @@ function textlinkads_update_1() {
   variable_del('textlinkads_ad_data');
   return array();
 }
+
+/*
+ * Import links from existing variable.
+ */
+function textlinkads_update_2() {
+  $ret = array();
+
+  if ($GLOBALS['db_type'] == 'mysql') {
+    $ret[] = update_sql("ALTER TABLE {textlinkads} ADD inventory_key varchar(256) NOT NULL");
+  }
+  elseif ($GLOBALS['db_type'] == 'pgsql') {
+    db_add_column($ret, 'textlinkads', 'inventory_key', 'varchar(256)', array('default' => "''", 'not null' => TRUE));
+  }
+
+  return $ret;
+}
Index: textlinkads.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/textlinkads/textlinkads.module,v
retrieving revision 1.5
diff -u -p -r1.5 textlinkads.module
--- textlinkads.module	10 Feb 2007 04:32:46 -0000	1.5
+++ textlinkads.module	8 Aug 2007 16:35:38 -0000
@@ -1,5 +1,16 @@
 <?php
 
+/**  
+ *  Support multiple inventory keys per site
+ *  1.  Multiple blocks? (with a key per block) - let drupal block visibility system manage visibility?
+ *      
+ *  2. Database changes - store inventory key?
+ *  3. Cron changes
+ *  4. Settings changes
+ *     Inventory key(s), path mach pattern? 
+ *     Module will provide block per key
+ */
+
 define('TEXTLINKADS_VERSION', '$Id: textlinkads.module,v 1.5 2007/02/10 04:32:46 kbahey Exp $');
 define('TEXTLINKADS_MODULE_PATH', drupal_get_path('module', 'textlinkads'));
 
@@ -28,7 +39,7 @@ function textlinkads_help($section) {
       return $output;
 
     case 'admin/settings/textlinkads':
-      return '<p>'. t('You are using version %version of the Text-Link-Ads module.', array('%version' => TEXTLINKADS_VERSION)). '</p><p>'. t('For complete instructions on configuring this module, refer to the %help_pages.', array('%help_pages' => l(t('Help pages'), 'admin/help/textlinkads'))). '</p>';
+      return '<p>'. t('You are using version %version of the Text-Link-Ads module.', array('%version' => TEXTLINKADS_VERSION)). '</p><p>'. t('For complete instructions on configuring this module, refer to the !help_pages.', array('!help_pages' => l(t('Help pages'), 'admin/help/textlinkads'))). '</p>';
   }
 }
 
@@ -74,24 +85,29 @@ function textlinkads_menu($may_cache) {
   return $items;
 }
 
+function textlinkads_get_keys() {
+  $keys = explode(',', variable_get('textlinkads_website_xml_key', ''));
+  return $keys;
+}
+
 function textlinkads_block($op = 'list', $delta = 0, $edit = array()) {
+  $keys = textlinkads_get_keys();
+  $nblocks = count($keys);
   switch ($op) {
     case 'list':
-      $blocks[0]['info'] = t('text link ads advertisments');
+      for ($i = 0; $i < $nblocks; $i++) {
+        $blocks[$i]['info'] = t('TLA Block for key= !key', array('!key'=>$keys[$i]));
+      }
       return $blocks;
     case 'view':
-      switch ($delta) {
-        case 0:
-          $block['subject'] = t(variable_get('textlinkads_ad_block_title', 'Advertisments'));
-          $block['content'] = textlinkads_content();
-          break;
-      }
+      $block['subject'] = t(variable_get('textlinkads_ad_block_title', t('Advertisements')));
+      $block['content'] = textlinkads_content($keys[$delta]);
       return $block;
   }
 }
 
-function textlinkads_content() {
-  $output = theme('textlinkads_ads', textlinkads_get_links('text'));
+function textlinkads_content($key) {
+  $output = theme('textlinkads_ads', textlinkads_get_links('text', $key), $key);
   return $output;
 }
 
@@ -102,9 +118,9 @@ function textlinkads_admin_settings() {
 
   $form['textlinkads_website_xml_key'] = array(
     '#type' => 'textfield',
-    '#title' => t('Website XML Key'),
+    '#title' => t('Website XML Keys'),
     '#default_value' => variable_get('textlinkads_website_xml_key', ''),
-    '#description' => t('You get your XML Site Key from the <a href="http://www.text-link-ads.com/my_account.php?view=my_sites">Get Ad Code page</a> on Text-Link-Ads.com. Each of your sites has its own code that looks something like this: <code>A4WWSAAOULU1XGHWU78G</code>.'),
+    '#description' => t('A comma-separated list of keys (no spaces after commas).  You get your XML Site Key from the <a href="http://www.text-link-ads.com/my_account.php?view=my_sites">Get Ad Code page</a> on Text-Link-Ads.com. Each of your site or page within a site has its own code depending on your configuration.  It should look something like this: <code>A4WWSAAOULU1XGHWU78G,A4WWSAAOULU1XGHWU78G</code>.'),
   );
   $form['textlinkads_ad_block_title'] = array(
     '#type' => 'textfield',
@@ -195,7 +211,11 @@ function textlinkads_nodeapi(&$node, $op
 }
 
 function textlinkads_stats_page() {
-  if (!$key = variable_get('textlinkads_website_xml_key', '')) {
+  // @todo: fix this to work with multi-keys
+  $keys = textlinkads_get_keys();
+  $key = $keys[0]; // first key is RSS key - sucks, needs to be smarter
+
+  if (!$key) {
     drupal_set_message(t('You need to enter your Customer ID on the <a href="%admin">administration page</a> before you can view your statistics here.', array('%admin' => url('admin/settings/textlinkads'))), 'error');
     drupal_goto('admin/settings/textlinkads');
   }
@@ -266,7 +286,7 @@ function theme_textlinkads_rss_ad($link)
 }
 
 
-function theme_textlinkads_ads($links) {
+function theme_textlinkads_ads($links, $key) {
   $output = '';
 
   $font_size = variable_get('textlinkads_font', 12);
@@ -346,19 +366,33 @@ function theme_textlinkads_themer_colors
  */
 function textlinkads_check_update() {
   $CONNECTION_TIMEOUT = 10;
-  $url = 'http://www.text-link-ads.com/xml.php?inventory_key=' . check_plain(variable_get(textlinkads_website_xml_key, ''))
+  // enumerate inventory keylist - pass key to textlinkads_update_links function
+  $keys = textlinkads_get_keys();
+  $nkeys = count($keys);
+
+  $prevcount = db_result(db_query("SELECT COUNT(*) FROM {textlinkads}"));
+  db_query("DELETE FROM {textlinkads}");
+  $count = 0;
+  for ($i=0; $i<$nkeys; $i++) {
+    $key = $keys[$i];
+    $url = 'http://www.text-link-ads.com/xml.php?inventory_key=' . check_plain($key)
     . '&referer=' . urlencode($_SERVER['REQUEST_URI'])
     . '&user_agent=' . urlencode($_SERVER['HTTP_USER_AGENT'])
     . '&drupal_module_version=' . urlencode(TEXTLINKADS_VERSION);
-  textlinkads_update_links($url, $CONNECTION_TIMEOUT);
+    $count += textlinkads_update_links($url, $CONNECTION_TIMEOUT, $key);
+  }
+  // report changes as appropriate
+  if ($count != $prevcount) {
+    watchdog('TextLinkAds', "update_links: Inventory change (was: $prevcount now: $count)" );
+  }
+  variable_set('textlinkads_last_update', time());
 }
 
-function textlinkads_update_links($url, $time_out) {
+function textlinkads_update_links($url, $time_out, $inventory_key) {
   $xml = textlinkads_file_get_contents($url, $time_out);
   $xml = substr($xml, strpos($xml, '<?'));
-
-  db_query("DELETE FROM {textlinkads}");
   $parser = new xml2array();
+
   if ($data = $parser->parseXMLintoarray($xml)) {
     // there are no links or just one link
     if (is_array($data['LINKS']) && $data['LINKS']['LINK']['URL']) {
@@ -378,12 +412,10 @@ function textlinkads_update_links($url, 
       }
     }
     foreach ($links as $key => $values) {
-      db_query("INSERT INTO {textlinkads} (url, text, beforetext, aftertext, rsstext, rssbeforetext, rssaftertext) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')", $values['URL'], $values['TEXT'], $values['BEFORETEXT'], $values['AFTERTEXT'], $values['RSSTEXT'], $values['RSSBEFORETEXT'], $values['RSSAFTERTEXT']);
+      db_query("INSERT INTO {textlinkads} (url, text, beforetext, aftertext, rsstext, rssbeforetext, rssaftertext, inventory_key) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')", $values['URL'], $values['TEXT'], $values['BEFORETEXT'], $values['AFTERTEXT'], $values['RSSTEXT'], $values['RSSBEFORETEXT'], $values['RSSAFTERTEXT'], $inventory_key);
     }
   }
-  $count=count($links);
-  watchdog('TextLinkAds', "update_links: $count ads found" );
-  variable_set('textlinkads_last_update', time());
+  return count($links);
 }
 
 /**
@@ -394,7 +426,9 @@ function textlinkads_next_rss_ad() {
   static $links = NULL;
 
   if (!is_array($links)) {
-    $links = textlinkads_get_links('rss');
+    $keys = textlinkads_get_keys();
+    $key = $keys[0]; // rss is first key
+    $links = textlinkads_get_links('rss', $key);
     shuffle($links);
   }
 
@@ -406,12 +440,16 @@ function textlinkads_next_rss_ad() {
  *
  * @param string $type
  *   'text' or 'rss'
+ * @param $key The inventory key if not null
  * @return Array
  */
-function textlinkads_get_links($type = 'text') {
+function textlinkads_get_links($type = 'text', $key=NULL) {
   $links = array();
   $limit = ($type == 'text') ? variable_get('textlinkads_total', 4) : variable_get('feed_default_items', 10);
   $where = ($type == 'text') ? 'text IS NOT NULL && beforetext IS NOT NULL && aftertext IS NOT NULL' : 'rsstext IS NOT NULL';
+  if ($key) {
+    $where .= " AND inventory_key='$key'";
+  }
   $result = db_query("SELECT * FROM {textlinkads}  WHERE $where LIMIT %d", $limit);
   while ($row = db_fetch_array($result)) {
     $links[] = $row;
