<?
/*
*  clustr - arrange flickr pool photos by time_taken
*
*  Paul Downey (paul.downey@whatfettle.com)
*
*  uses http://www.phpflickr.com
*  released under GNU General Public License (http://www.gnu.org/copyleft/gpl.html)
*  source: http://clustr.whatfettle.com/index.phps
*/

/*
*  The quite natty phpFlickr.php API from:
*  http://www.phpflickr.com/
*/
require_once("phpFlickr.php");

/*
*  a secret php file with:
*
*    $flickr_api_key="my flickr api key";
*/
require_once("api_keys.php");

global
$flickr_api_key;

$flickr = new phpFlickr($flickr_api_key);

/*
*  URI form parameters
*/
$group_id = trim($_GET['group']);
$size = trim($_GET['size']);
$per_page = ((int)$_GET['per_page']);

if (
$size == "") {
   
$size = "square";
}

if (
$per_page <= 0) {
   
$per_page = 500;
}

/*
*  Group info
*/
$info = $flickr->groups_getInfo($group_id);
$title = $info['name'];
$description = $info['description'];
$group_url = "http://www.flickr.com/groups/" . $group_id . "/";

/*
*  Pool photos
*/
$photos = $flickr->groups_pools_getPhotos($group_id, '', 'date_taken,owner_name', $per_page);

foreach (
$photos['photo'] as $photo)
{
    
$owner = str_replace(" ", "", $photo['ownername']);
    
$url = "http://www.flickr.com/photos/" . $photo['owner'] . "/" . $photo['id'];
    
$date = $photo['datetaken'];
    
$img = $flickr->buildPhotoURL($photo, $size);

    
$skew[$owner] = (int)$_GET[$owner];

    
#0123456789012345678
    #YYYY-MM-DD HH-MM-SS

    
$time = mktime(
            (int)
substr($date, 11, 2),
            (int)
substr($date, 14, 2),
            (int)
substr($date, 17, 2),
            (int)
substr($date, 5, 2),
            (int)
substr($date, 8, 2),
            (int)
substr($date, 0, 4))

     + (
$skew[$owner]*3600);

    
$hour = strftime("%Y-%m-%d %H:00", $time);

    
$hours[$hour] = $hour;
    
asort($hours);

    
$owners[$owner] = $owner;
    
asort($owners);

    
$list[$owner][$hour][$img] = $img;

    
$p[$img] = $url;
}

print <<<EOF
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link rel="STYLESHEET" type="text/css" href="style.css">
  <title>$title</title>
</head>
<body>
  <center>
    <table width="75%"><tr><td align="center">
        <h2><a href="$group_url">$title</a></h2>
    </td></tr><tr><td align="center">
        <p>$description</p>
    </td></tr></table>

    <hr>

<table class='outer'>
<tr>
<td></td>
EOF;

foreach (
$hours as $hour)
{
    
$time = substr($hour, 0, 10) . "<br/>" . substr($hour, 11, 5);
    print
"  <th align='center'>" . $time . "</th>\n";
}

print
" </tr>\n";

foreach (
$owners as $owner)
{
    
$skewing = "";
    if (
$skew[$owner] != "")
    {
        
$sign = $skew[$owner] > 0 ? '+' : '';
        
$skewing = "<br/>(UTC" . $sign . $skew[$owner] . ")";
    }

    print
"\n\n <tr>\n";
    print
"  <th valign='middle' align='right'>" . $owners[$owner] . $skewing . "</th>\n";

    foreach (
$hours as $hour)
    {
        print
"  <td align='center'><table class='inner'><tr>";

        foreach (
$list[$owner][$hour] as $img)
        {
            print
'     <td><a href="' . $p[$img] . '">'
                
. '<img border="0" alt="' . $hour . "-" . $owner . '" src="'
                
. $img
                
. '"></a></td>' . "\n";
        }

        print
"  </tr></table></td>\n";
    }

    print
"  </tr>\n";
}

print <<<EOF
      </table>
    </tr>
</table>
<hr>
</center>
</body>
</html>
EOF;
?>