? flickr_136317.patch
Index: flickr.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flickr/flickr.inc,v
retrieving revision 1.18
diff -u -p -r1.18 flickr.inc
--- flickr.inc	13 Apr 2007 22:44:51 -0000	1.18
+++ flickr.inc	16 Apr 2007 21:36:11 -0000
@@ -288,20 +288,24 @@ function flickr_photoset_get_list($nsid)
  *   valid nsid or false if none can be found
  */
 function flickr_user_find_by_identifier($identifier) {
-  if (preg_match('/^\d+@N\d+$/', $identifier)) {
+  if (flickr_is_nsid($identifier)) {
     //identifier is an NSID
     return $identifier;
   }
-  if (valid_email_address($identifier) && !flickr_error(($response = flickr_user_find_by_email($identifier)))) {
+  if (valid_email_address($identifier) && !flickr_response_has_error(($response = flickr_user_find_by_email($identifier)))) {
     return $response['user']['nsid'];
   }
-  if (!flickr_error($response = flickr_user_find_by_username($identifier))) {
+  if (!flickr_response_has_error($response = flickr_user_find_by_username($identifier))) {
     return $response['user']['nsid'];
   }
 
   return FALSE;
 }
 
+function flickr_is_nsid($id) {
+  return preg_match('/^\d+@N\d+$/', $id);
+}
+
 /**
  * Lookup an nsid for a username.
  *
@@ -369,6 +373,30 @@ function flickr_tags_get_list_user($nsid
   );
 }
 
+
+function flickr_photos_search($nsid, $page = 1, $other_args = array()){
+  $args = array (
+    'user_id' => $nsid,
+    'page' => $page,
+  );
+
+  //set per_page to flickr module default if it is not specified in $other_args
+  if (!isset($other_args['per_page'])) {
+    $args['per_page'] = variable_get('flickr_photos_per_page', 20);
+  }
+
+  return flickr_request('flickr.photos.search', array_merge($args, $other_args));
+}
+
+function flickr_tag_request_args($tags = array(), $mode = 'all') {
+  $args = array();
+  if (!empty($tags)) {
+    $args['tags'] = implode(',', $tags);
+    $args['tag_mode'] = $mode == 'all' ? $mode : 'any';
+  }
+  return $args;
+}
+
 /**
  * Check if the response from the Flickr api call was an error
  *
@@ -378,18 +406,13 @@ function flickr_tags_get_list_user($nsid
  * @return
  *    true if the response is an error message
  */
-function flickr_error($response) {
-  if (array_key_exists('stat', $response) && $response['stat'] == 'ok') {
-    return FALSE;
-  }
-  else {
-    return TRUE;
-  }
+function flickr_response_has_error($response) {
+  return !(array_key_exists('stat', $response) && $response['stat'] == 'ok');
 }
 
 /**
  * Display an error message and write an error to watchdog.
- * Only call this method if flickr_error returns TRUE.
+ * Only call this method if flickr_response_has_error returns TRUE.
  *
  * @param $response
  *   response to display
Index: flickr.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flickr/flickr.module,v
retrieving revision 1.32
diff -u -p -r1.32 flickr.module
--- flickr.module	15 Apr 2007 05:43:39 -0000	1.32
+++ flickr.module	16 Apr 2007 21:36:12 -0000
@@ -139,7 +139,7 @@ function flickr_admin_settings_validate(
     form_set_error('flickr_api_secret', t('This does not appear to be a Flickr API secret.'));
   }
   if ($uid) {
-    if (preg_match('/^\d+@N\d+$/', $uid)) {
+    if (flickr_is_nsid($uid)) {
       // it's already a uid
     }
     else {
@@ -158,7 +158,7 @@ function flickr_admin_settings_submit($f
   $form['flickr_default_userid'] = trim($form['flickr_default_userid']);
 
   // ... replace the usernames with a user id ...
-  if (preg_match('/^\d+@N\d+$/', $form['flickr_default_userid']) == 0) {
+  if (!flickr_is_nsid($form['flickr_default_userid'])) {
     $username = $form['flickr_default_userid'];
     $response = flickr_user_find_by_username($username);
     if (isset($response['stat']) && $response['stat'] == 'ok') {
@@ -250,6 +250,10 @@ function flickr_user($op, &$edit, &$acco
   }
 }
 
+function flickr_get_nsid($uid) {
+  return db_result(db_query('SELECT nsid FROM {flickr_users} WHERE uid=%d', $uid));
+}
+
 function flickr_photos($uid = NULL) {
   drupal_add_css(drupal_get_path('module', 'flickr') .'/flickr.css');
 
@@ -276,9 +280,13 @@ function flickr_photos($uid = NULL) {
     }
   }
 
-  $photo_arr = flickr_photos_get($nsid, array(), $pager_page_array[$element]+1);
+  $photo_arr = flickr_photos_search($nsid, $pager_page_array[$element]+1);
 
-  if (empty($photo_arr['photos'])) {
+  if (flickr_response_has_error($photo_arr)) {
+    flickr_set_error($photo_arr);
+    return FALSE;
+  }
+  else if (empty($photo_arr['photos'])) {
     drupal_set_message(t('No accessible photos found for Flickr %userid', array('%userid'=> $nsid)));
     return FALSE;
   }
@@ -291,37 +299,6 @@ function flickr_photos($uid = NULL) {
 }
 
 
-function flickr_photos_get($nsid, $tags = array(), $page = 1, $other_args = array()){
-  $args = array (
-    'user_id' => $nsid,
-    'page' => $page,
-  );
-
-  //process tags
-  if (!empty($tags)) {
-    $args['tags'] = implode(',', $tags);
-    //change tag_mode default from any to all
-    if (!isset($other_args['tag_mode'])) {
-      $args['tag_mode'] = 'all';
-    }
-  }
-
-  //set per_page to flickr module default if it is not specified in $other_args
-  if (!isset($other_args['per_page'])) {
-    $args['per_page'] = variable_get('flickr_photos_per_page', 20);
-  }
-
-  $args = array_merge($args, $other_args);
-  $search_response = flickr_request('flickr.photos.search', $args);
-
-  if (flickr_error($search_response)) {
-    flickr_set_error($search_response);
-    return FALSE;
-  }
-
-  return $search_response;
-}
-
 function theme_flickr_photos($uid, $photo_arr){
   $output =
     theme('pager', NULL, variable_get('flickr_photos_per_page', 20));
@@ -335,3 +312,11 @@ function theme_flickr_photos($uid, $phot
   return $output;
 }
 
+function theme_flickr_photoset($ps, $owner, $size, $attribs=NULL) {
+  $img = flickr_img($ps, $size, $attribs);
+  $photo_url = flickr_photoset_page_url($owner, $ps['id']);
+  return '<span class="flickr-photoset">'
+    . l($img, $photo_url, NULL, NULL, NULL, TRUE, TRUE)
+    . "</span>\n";
+}
+
Index: block/flickr_block.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flickr/block/flickr_block.module,v
retrieving revision 1.7
diff -u -p -r1.7 flickr_block.module
--- block/flickr_block.module	13 Apr 2007 16:27:03 -0000	1.7
+++ block/flickr_block.module	16 Apr 2007 21:36:13 -0000
@@ -115,20 +115,25 @@ function flickr_block($op = 'list', $del
       $settings['user_id'] = variable_get('flickr_default_userid', '');
     }
 
+    // Get per user nsid if necessary
+    if ($delta < 3) {
+      $user_nsid = flickr_get_nsid(arg(1));
+    }
+
     switch ($delta) {
     case 0:
       $block['subject'] = t('Flickr recent photos by user');
-      $block['content'] = _flickr_block_recent(_flickr_block_get_userid(arg(1)), $settings['show_n'], $settings['size']);
+      $block['content'] = _flickr_block_recent($user_nsid, $settings['show_n'], $settings['size']);
       break;
 
     case 1:
       $block['subject'] = t('Flickr recent photosets');
-      $block['content'] = _flickr_block_photosets(_flickr_block_get_userid(arg(1)), $settings['show_n'], $settings['size']);
+      $block['content'] = _flickr_block_photosets($user_nsid, $settings['show_n'], $settings['size']);
       break;
 
     case 2:
       $block['subject'] = t('Flickr random photos');
-      $block['content'] = _flickr_block_random(_flickr_block_get_userid(arg(1)), $settings['show_n'], $settings['size']);
+      $block['content'] = _flickr_block_random($user_nsid, $settings['show_n'], $settings['size']);
       break;
 
     case 3:
@@ -151,44 +156,27 @@ function flickr_block($op = 'list', $del
   }
 }
 
-function _flickr_block_get_userid($id) {
-  $result = db_fetch_object(db_query('SELECT data FROM {users} WHERE uid = %d', $id));
-  $data = unserialize($result->data);
-  return $data['nsid'];
-}
-
-function _flickr_block_recent($user_id, $show_n, $size) {
-  $result = flickr_request(
-    'flickr.photos.search',
-    array(
-      'user_id' => $user_id,
-      'per_page' => $show_n,
-    )
-  );
+function _flickr_block_recent($nsid, $show_n, $size) {
+  $result = flickr_photos_search($nsid, 1, array('per_page' => $show_n));
   $output = '';
   foreach((array)$result['photos']['photo'] as $photo) {
-    $output .= theme('flickr_photo', $photo, $size);
+    $output .= theme('flickr_block_photo', $photo, $size);
   }
   return $output;
 }
 
-function _flickr_block_photosets($user_id, $show_n, $size) {
+function _flickr_block_photosets($nsid, $show_n, $size) {
+  $result = flickr_photoset_get_list($nsid);
   $output = '';
-  $result = flickr_request('flickr.photosets.getList', array('user_id' => $user_id));
   $to = min($show_n, count($result['photosets']['photoset']));
   for ($i = 0; $i < $to; $i++) {
-    $output .= theme('flickr_photoset', $result['photosets']['photoset'][$i], $user_id, $size);
+    $output .= theme('flickr_block_photoset', $result['photosets']['photoset'][$i], $nsid, $size);
   }
   return $output;
 }
 
-function _flickr_block_random($user_id, $show_n, $size) {
-  $request = array(
-    'user_id' => $user_id,
-    'per_page' => 500,
-    'page' => 1,
-  );
-  $result = flickr_request('flickr.photos.search', $request);
+function _flickr_block_random($nsid, $show_n, $size) {
+  $result = flickr_photos_search($nsid, 1, array('per_page' => 500));
   $page_count = $result['photos']['pages'];
 
   // we shouldn't try to return more than the total number of photos
@@ -197,12 +185,19 @@ function _flickr_block_random($user_id, 
   for ($i = 0; $i < $to; $i++) {
     sleep(0.125);
     // request a random page
-    $request['page'] = rand(1, $page_count);
-    $result = flickr_request('flickr.photos.search', $request);
+    $result = flickr_photos_search($nsid, rand(1, $page_count), array('per_page' => 500));
     // then select a random photo
     $index = rand(0, count($result['photos']['photo']));
-    $output .= theme('flickr_photo', $result['photos']['photo'][$index], $size);
+    $output .= theme('flickr_block_photo', $result['photos']['photo'][$index], $size);
   }
 
   return $output;
 }
+
+function theme_flickr_block_photo($p, $size = NULL) {
+  return theme_flickr_photo($p, $size);
+}
+
+function theme_flickr_block_photoset($ps, $owner, $size) {
+  return theme_flickr_photoset($ps, $owner, $size);
+}
\ No newline at end of file
Index: filter/flickr_filter.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/flickr/filter/flickr_filter.module,v
retrieving revision 1.7
diff -u -p -r1.7 flickr_filter.module
--- filter/flickr_filter.module	14 Apr 2007 02:03:30 -0000	1.7
+++ filter/flickr_filter.module	16 Apr 2007 21:36:14 -0000
@@ -83,11 +83,11 @@ function flickr_filter_callback_photo($m
 
   if (isset($config['id'])) {
     $result = flickr_photo_get_info($config['id']);
-    if (!flickr_error($result)) {
-      return theme('flickr_filter_photo', $result['photo'], $config['size'], $attribs);
+    if (flickr_response_has_error($result)) {
+      drupal_set_message(t('The Flickr filter had a problem: @message', array('@message' => $result['message'])), 'error');
     }
     else {
-      drupal_set_message(t('The Flickr filter had a problem. Flickr says: %message', array('%id' => $config['id'], '%message' => $result['message'])), 'error');
+      return theme('flickr_filter_photo', $result['photo'], $config['size'], $attribs);
     }
   }
   return '';
@@ -101,28 +101,20 @@ function flickr_filter_callback_photoset
 
   if (isset($config['id'])) {
     $result = flickr_photoset_get_info($config['id']);
-    if (!flickr_error($result)) {
-      return theme('flickr_filter_photoset', $result['photoset'], $result['photoset']['owner'], $config['size'], $attribs);
+    if (flickr_response_has_error($result)) {
+      drupal_set_message(t('The Flickr filter had a problem: @message', array('@message' => $result['message'])), 'error');
     }
     else {
-      drupal_set_message(t('The Flickr filter had a problem. Flickr says: %message', array('%id' => $config['id'], '%message' => $result['message'])), 'error');
+      return theme('flickr_filter_photoset', $result['photoset'], $result['photoset']['owner'], $config['size'], $attribs);
     }
   }
   return '';
 }
 
 function theme_flickr_filter_photo($p, $size, $attribs) {
-  $img = flickr_img($p, $size, $attribs);
-  $photo_url = flickr_photo_page_url($p['owner'], $p['id']);
-  return '<span class="flickr-photo">'
-    . l($img, $photo_url, NULL, NULL, NULL, TRUE, TRUE)
-    . "</span>\n";
+  return theme_flickr_photo($p, $size, NULL, $attribs);
 }
 
 function theme_flickr_filter_photoset($ps, $owner, $size, $attribs) {
-  $img = flickr_img($ps, $size, $attribs);
-  $photo_url = flickr_photoset_page_url($owner, $ps['id']);
-  return '<span class="flickr-photoset">'
-    . l($img, $photo_url, NULL, NULL, NULL, TRUE, TRUE)
-    . "</span>\n";
+  return theme_flickr_photoset($ps, $owner, $size, $attribs);
 }
