three Flickr blocks!

bobthecow - September 15, 2006 - 11:26
Project:Flickr Module
Version:4.7.x-1.x-dev
Component:Code
Category:task
Priority:normal
Assigned:bobthecow
Status:active
Description

alright kids. i love this module... but it didn't do everything i wanted it to. so i made it do blocks too. three different kinds!

i don't really feel like making a patch right now, but i might later if i get a chance. for now, copy and paste the code block into an empty section of flickr.module

this stuff can all be themed.

i would suggest something like this:

<?php
.flickr, .flickr_recent {
   
padding: 4px;
   
margin: 6px;
   
border-top:1px solid #222;
   
border-bottom:1px solid #222;
}

.
flickr_title {
   
font-weight: bold;
}

.
flickr_thumbs {
   
/* get 'em to line up all pretty */
   
display: block;
   
float: left;
   
height: 100px;
   
width: 100px;
   
margin: 4px;
   
text-align: center;
}
?>

screenshots from my website:
most recent flickr photosets
most recent photoset thumbnails
most recent photoset titles
(hosted on ImageShack.us)

WARNING: big chunk of code ahead (it's also attached)...

<?php
/**
* Implementation of hook_block() for flickr module.
* by bobthecow < justinhileman [shift-2] hotmail [dot] com >
*
* three 'recent flickr photoset' blocks:
*   'flickr recent sets' Displays the most recent  photosets, titles, and descriptions in a block.
*   'flickr recent titles' displays the most recent photoset titles in a block.
*   'flickr recent thumbs' displays the most recent photoset thumbnails in a block.
*
* all three are css styleable
* recent sets class is '{$flickr_class}_recent'
* recent sets class is '{$flickr_class}_titles'
* recent sets class is '{$flickr_class}_thumbs'
*
* i would suggest something like:
*
* .flickr, .flickr_recent {
*     padding: 4px;
*     margin: 6px;
*     border-top:1px solid #222;
*     border-bottom:1px solid #222;
* }
* .flickr_title {
*     font-weight: bold;
* }
* .flickr_thumbs {
*     display: block;
*     float: left;
*     height: 100px;
*     width: 100px;
*     margin: 4px;
*     text-align: center;
* }
*
*
* the number of photosets to display is configurable (per block) in the block config
*/
function flickr_block($op='list', $delta=0, $edit = array()) {
  switch(
$delta) {
    case
0: // full flickr photosets
     
$block_type = 'flickr_recent';
     
$count_var = 'flickr_recent_count';
      break;
    case
1: // flickr photoset titles
     
$block_type = 'flickr_titles';
     
$count_var = 'flickr_titles_count';
      break;
    case
2: // flickr photosets thumbs
     
$block_type = 'flickr_thumbs';
     
$count_var = 'flickr_thumbs_count';
      break;
  }

  if (
$op == "list") {
   
$block[0]["info"] = t('Flickr recent sets');
   
$block[1]["info"] = t('Flickr recent titles');
   
$block[2]["info"] = t('Flickr recent thumbs');
    return
$block;
  }
  else if (
$op == 'configure') {
   
$form = array();
   
$form[$count_var] = array(
     
'#type' => 'textfield',
     
'#title' => t('Max photosets'),
     
'#size' => 4,
     
'#description' => t('This is the maximum number of photosets to display.'),
     
'#default_value' =>
       
variable_get($count_var'5')
    );
    return
$form;
  }
  else if (
$op == 'save') {
   
variable_set($count_var, $edit[$count_var]);
  }
  else if (
$op == 'view') {
    global
$user;
    if (
user_access('access content')) {
     
$flickr_api_key = variable_get('flickr_apik','bd4441fc9cef3745a18955ffdf91c732');
     
$flickr_uid     = variable_get('flickr_uid','35034355182@N01');
     
$count = variable_get($count_var, 5);

     
$url = "http://www.flickr.com/services/rest/?method=flickr.photosets.getList&api_key=$flickr_api_key&user_id=$flickr_uid";
     
$resp = flickr_get_cached_file_contents($url, 'r', 600, variable_get('flickr_cache','/tmp'));
     
$settree = flickr_make_xmltree($resp);

      if (!empty(
$top_level_tree['rsp']['err']) || empty($settree))
        {
     
$output = "<h2>An error occurred</h2> Somehow we were unable to parse the response. Please contact the site owner for investigation.";
        }
      else {
       
$i = 0;
        if (empty(
$settree['rsp']['photosets'])) {
     
$output = "no photosets found.<br />";
        }
        else {
     
$output = '';
     
$i = 0;
         
// if it's titles, take care of the <ul> tags
         
if ($delta == 1) {
           
$output .='<ul>';
          }
          foreach (
$settree['rsp']['photosets'] as $album_id => $album_details) {
            if (
$i++ < $count) {
             
$output .= theme($block_type, $album_details, $album_id, "_t");
            }
          }
         
// if it's titles, take care of the <ul> tags
         
if ($delta == 1) {
           
$output .='</ul>';
          }
      else if (
$delta == 2) {
           
$output .= "<br clear='both' />";
          }
        }
      }

     
$block['content'] = $output;
     
$block['content'] .= '<div class="more-link">'. l(t('more'), 'flickr', array('title' => t('See the rest of my photosets.'))) .'</div>';
     
$block['subject'] = t('Recent Flickr photosets');
      return
$block;
    }
  }
}

/**
* generate recent flickr photosets
*
* a better way to do this would be to use templates or something...
*/
function theme_flickr_recent($album, $album_id, $tn_size="_t") {
 
// hand:
  // $album == array containing album data
  // $album_id == the Flickr ID
  // $tn_size == set to _t, _s, _m, "", or _o

 
$flickr_class = variable_get('flickr_class','flickr');

 
$thumbnail = 'http://static.flickr.com/'.$album['server'].'/'.$album['primary']."_".$album['secret'].$tn_size.".jpg";
 
$link_to_photoset_on_flickr = variable_get('flickr_proot','http://www.flickr.com/photos/').variable_get('flickr_uname','jluster')."/sets/". $album_id;
 
$album_title = $album['title'];
 
$album_description = $album['description'];
 
$css_class = variable_get('flickr_class', 'flickr');

 
// draw the thumbnail to the right.
 
$output .= "<div class='" . $flickr_class . "_recent'><div style='float: right;'><img src='".$thumbnail."' alt='".$album_title."' class='$class'></div>";

 
$output .= "<h2>".$album_title."</h2>";
 
$output .= "<p>". format_plural($album['photos'], '1 photo', '%count photos') ." in this album<br />(".l(t('view locally'), "flickr/setimages/$album_id")." - <a href='$link_to_photoset_on_flickr'>view on Flickr</a>).</p>";
 
$output .= "<p>$album_description</p><br clear='both' /></div>";

  return
$output;
}

/**
* generate recent flickr photoset titles
*
* a better way to do this would be to use templates or something...
*/
function theme_flickr_titles($album, $album_id) {
 
// hand:
  // $album == array containing album data
  // $album_id == the Flickr ID

 
$flickr_class = variable_get('flickr_class','flickr');

 
$link_to_photoset_on_flickr = variable_get('flickr_proot','http://www.flickr.com/photos/').variable_get('flickr_uname','jluster')."/sets/". $album_id;
 
$album_title = $album['title'];

 
// output the title in an unordered list...
 
$output = "<div class='" . $flickr_class . "_titles'><li><span class='" . $flickr_class . "_title'>".$album_title . " (" . $album['photos'] . ")</span>&nbsp;&nbsp; [ ".l(t('local'), "flickr/setimages/$album_id")." | <a href='$link_to_photoset_on_flickr'>Flickr</a> ]</li>";

  return
$output;
}

/**
* generate recent flickr photoset thumbs
*
* a better way to do this would be to use templates or something...
*/
function theme_flickr_thumbs($album, $album_id, $tn_size="_t") {
 
// hand:
  // $album == array containing album data
  // $album_id == the Flickr ID
  // $tn_size == set to _t, _s, _m, "", or _o

 
$flickr_class = variable_get('flickr_class','flickr');

 
$thumbnail = 'http://static.flickr.com/'.$album['server'].'/'.$album['primary']."_".$album['secret'].$tn_size.".jpg";
 
$link_to_photoset_on_flickr = variable_get('flickr_proot','http://www.flickr.com/photos/').variable_get('flickr_uname','jluster')."/sets/". $album_id;

 
// draw the thumbnail, add a caption
 
$output = "<div class='" . $flickr_class . "_thumbs'><img src='".$thumbnail."' alt='".$album_title."' class='$class'><br />";
 
$output .= $album['photos'] . " [ ".l(t('local'), "flickr/setimages/$album_id")." | <a href='$link_to_photoset_on_flickr'>Flickr</a> ]</div>";

  return
$output;
}
?>

AttachmentSize
flickr.module.blocks7.1 KB
 
 

Drupal is a registered trademark of Dries Buytaert.