? 293329_tla_multiple_blocks.patch
Index: textlinkads.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/textlinkads/textlinkads.install,v
retrieving revision 1.3
diff -u -p -r1.3 textlinkads.install
--- textlinkads.install	5 Jun 2008 23:36:00 -0000	1.3
+++ textlinkads.install	5 Dec 2008 19:35:16 -0000
@@ -59,6 +59,13 @@ function textlinkads_schema() {
         'not null' => TRUE,
         'default' => '',
       ),
+      'inventory_key' => array(
+        'description' => t('XML Key for ad. Provides support for multiple sets of links on one site.'),
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+        'default' => '',
+      ),
     ),
     'primary key' => array('tlid'),
   );
@@ -82,3 +89,15 @@ function textlinkads_update_1() {
   return array();
 }
  */
+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.6
diff -u -p -r1.6 textlinkads.module
--- textlinkads.module	5 Jun 2008 23:36:00 -0000	1.6
+++ textlinkads.module	5 Dec 2008 19:35:16 -0000
@@ -2,7 +2,7 @@
 // $Id: textlinkads.module,v 1.6 2008/06/05 23:36:00 mikejoconnor Exp $
 
 define('TEXTLINKADS_VERSION', '$Id: textlinkads.module,v 1.6 2008/06/05 23:36:00 mikejoconnor Exp $');
-//define('TEXTLINKADS_MODULE_PATH', drupal_get_path('module', 'textlinkads'));
+define('TEXTLINKADS_MODULE_PATH', drupal_get_path('module', 'textlinkads'));
 
 function textlinkads_help($path, $arg) {
   switch ($path) {
@@ -68,18 +68,23 @@ function textlinkads_menu() {
   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', 'Advertisments'));
+      $block['content'] = textlinkads_content($keys[$delta]);
       return $block;
   }
 }
@@ -90,7 +95,7 @@ function textlinkads_theme() {
       'arguments' => array('link'),
     ),
     'textlinkads_ads' => array(
-      'arguments' => array('link'),
+      'arguments' => array('link', 'key'),
     ),
     'textlinkads_stats' => array(
       'arguments' => array('stats_data'),
@@ -101,8 +106,8 @@ function textlinkads_theme() {
   );
 }
 
-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;
 }
 
@@ -114,9 +119,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',
@@ -208,7 +213,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');
   }
@@ -279,7 +288,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);
@@ -359,18 +368,34 @@ 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, ''))
-    .'&referer='. urlencode(request_uri())
-    .'&user_agent='. urlencode($_SERVER['HTTP_USER_AGENT'])
-    .'&drupal_module_version='. urlencode(TEXTLINKADS_VERSION);
-  textlinkads_update_links($url, $CONNECTION_TIMEOUT);
+  // Enumerate inventory keylist and pass key to the update links function
+  $keys = textlinkads_get_keys();
+  $nkeys = count($keys);
+
+  $prevcount = db_result(db_query("SELECT COUNT(*) FROM {textlinkads}"));
+
+  // Get a clean slate of links before we loop and try to build the new set.
+  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(request_uri())
+      .'&user_agent='. urlencode($_SERVER['HTTP_USER_AGENT'])
+      .'&drupal_module_version='. urlencode(TEXTLINKADS_VERSION);
+    $count += textlinkads_update_links($url, $CONNECTION_TIMEOUT, $key);
+  }
+  // Report changes if 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
@@ -391,12 +416,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);
 }
 
 /**
@@ -407,7 +430,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);
   }
 
@@ -419,16 +444,20 @@ function textlinkads_next_rss_ad() {
  *
  * @param string $type
  *   'text' or 'rss'
+ * @param string $key
  * @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);
+  $args[] = ($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';
-  $result = db_query("SELECT * FROM {textlinkads}  WHERE $where LIMIT %d", $limit);
-  while ($row = db_fetch_array($result)) {
+  if ($key) { 
+    $where .= " AND inventory_key='%s'";
+    $args = array($key, $args[0]);
+  } 
 
+  $result = db_query("SELECT * FROM {textlinkads}  WHERE $where LIMIT %d", $args);
+  while ($row = db_fetch_array($result)) {
     $links[] = $row;
   }
 
