| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id:$
|
| 4 |
|
| 5 |
function flickr_block($section) {
|
| 6 |
switch ($section) {
|
| 7 |
case 'admin/modules#description':
|
| 8 |
$output = t('Blocks displaying Flickr images');
|
| 9 |
break;
|
| 10 |
}
|
| 11 |
return $output;
|
| 12 |
}
|
| 13 |
|
| 14 |
|
| 15 |
function flickr_block_block($op = "list", $delta = 0) {
|
| 16 |
static $context;
|
| 17 |
|
| 18 |
switch ($op)
|
| 19 |
{
|
| 20 |
case "list":
|
| 21 |
$blocks[0]["info"] = t('Related Flickr images');
|
| 22 |
$blocks[1]["info"] = t('My Flickr images');
|
| 23 |
|
| 24 |
return $blocks;
|
| 25 |
|
| 26 |
case "view":
|
| 27 |
switch ($delta) {
|
| 28 |
case 0:
|
| 29 |
$block["subject"] = t('Related Flickr images');
|
| 30 |
$block["content"] = _flickr_block_related($tid);
|
| 31 |
break;
|
| 32 |
|
| 33 |
case 1:
|
| 34 |
$block["subject"] = t('My latest Flickr images');
|
| 35 |
$block["content"] = _flickr_block_my();
|
| 36 |
break;
|
| 37 |
}
|
| 38 |
return $block;
|
| 39 |
|
| 40 |
}
|
| 41 |
}
|
| 42 |
|
| 43 |
function _flickr_block_related($tid) {
|
| 44 |
|
| 45 |
if (arg(1) == 'term' || arg(1) == 'tid') {
|
| 46 |
$tid = arg(2);
|
| 47 |
}
|
| 48 |
|
| 49 |
if ($tid) {
|
| 50 |
|
| 51 |
$term = taxonomy_get_term($tid);
|
| 52 |
$tags = array_merge(array($term->name), taxonomy_get_synonyms($tid));
|
| 53 |
|
| 54 |
$result = _flickr_request('flickr.photos.search', array('tags' => implode(',', $tags), 'per_page' => '4'));
|
| 55 |
|
| 56 |
if (count($result['photos'][0]['children'])) {
|
| 57 |
foreach ($result['photos'][0]['children']['photo'] as $photo) {
|
| 58 |
$items[] = '<a href="' ._flickr_page_url($photo['attrs']['owner'], $photo['attrs']['id']). '" title="'. $photo['attrs']['title'].'" target="new"><img src="' ._flickr_img_url($photo['attrs']['server'], $photo['attrs']['id'], $photo['attrs']['secret'], 's'). '" /></a>';
|
| 59 |
}
|
| 60 |
}
|
| 61 |
|
| 62 |
}
|
| 63 |
return (count($items) ? theme('item_list', $items) : NULL);
|
| 64 |
}
|
| 65 |
|
| 66 |
|
| 67 |
function _flickr_block_my() {
|
| 68 |
|
| 69 |
$result = _flickr_request('flickr.photos.search', array('user_id' => variable_get('flickr_uid','44124434048@N01'), 'per_page' => '4'));
|
| 70 |
|
| 71 |
if (count($result['photos'][0]['children'])) {
|
| 72 |
foreach ($result['photos'][0]['children']['photo'] as $photo) {
|
| 73 |
$items[] = '<a href="' ._flickr_page_url($photo['attrs']['owner'], $photo['attrs']['id']). '" title="'. $photo['attrs']['title'].'" target="new"><img src="' ._flickr_img_url($photo['attrs']['server'], $photo['attrs']['id'], $photo['attrs']['secret'], 's'). '" /></a>';
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
return (count($items) ? theme('item_list', $items) : NULL);
|
| 78 |
|
| 79 |
return $output;
|
| 80 |
}
|
| 81 |
|
| 82 |
|
| 83 |
function _flickr_request($method, $arguments) {
|
| 84 |
|
| 85 |
if (!$arguments['api_key']) {
|
| 86 |
$arguments['api_key'] = variable_get('flickr_apik','bd4441fc9cef3745a18955ffdf91c732');
|
| 87 |
}
|
| 88 |
$result = xmlrpc('http://www.flickr.com/services/xmlrpc/', $method, $arguments);
|
| 89 |
$result = _xml2array($result);
|
| 90 |
|
| 91 |
return $result;
|
| 92 |
}
|
| 93 |
|
| 94 |
function _flickr_img_url($server, $id, $secret, $size = NULL, $format = NULL) {
|
| 95 |
return 'http://photos' .$server. '.flickr.com/' .$id. '_' .$secret. ($size ? '_' .$size : '') . '.' .($size == 'o' ? $format : 'jpg');
|
| 96 |
}
|
| 97 |
|
| 98 |
function _flickr_page_url($owner, $id = NULL) {
|
| 99 |
return 'http://flickr.com/photos/' .$owner. ($id ? '/' .$id : '');
|
| 100 |
}
|
| 101 |
|
| 102 |
/**
|
| 103 |
** xml to array
|
| 104 |
** from http://www.php.net/manual/tw/function.xml-parse-into-struct.php#27015
|
| 105 |
**/
|
| 106 |
|
| 107 |
function _xml2array($string) {
|
| 108 |
$parser = xml_parser_create();
|
| 109 |
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
|
| 110 |
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
|
| 111 |
xml_parse_into_struct($parser, $string, $vals, $index);
|
| 112 |
xml_parser_free($parser);
|
| 113 |
|
| 114 |
$stack = array( array() );
|
| 115 |
$stacktop = 0;
|
| 116 |
$parent = array();
|
| 117 |
foreach($vals as $val)
|
| 118 |
{
|
| 119 |
$type = $val['type'];
|
| 120 |
if($type=='open' || $type=='complete')
|
| 121 |
{
|
| 122 |
// open tag
|
| 123 |
$stack[$stacktop++] = $tagi;
|
| 124 |
$tagi = array('tag' => $val['tag']);
|
| 125 |
|
| 126 |
if(isset($val['attributes'])) $tagi['attrs'] = $val['attributes'];
|
| 127 |
if(isset($val['value']))
|
| 128 |
$tagi['values'][] = $val['value'];
|
| 129 |
}
|
| 130 |
if($type=='complete' || $type=='close')
|
| 131 |
{
|
| 132 |
// finish tag
|
| 133 |
$tags[] = $oldtagi = $tagi;
|
| 134 |
$tagi = $stack[--$stacktop];
|
| 135 |
$oldtag = $oldtagi['tag'];
|
| 136 |
unset($oldtagi['tag']);
|
| 137 |
$tagi['children'][$oldtag][] = $oldtagi;
|
| 138 |
$parent = $tagi;
|
| 139 |
}
|
| 140 |
if($type=='cdata')
|
| 141 |
{
|
| 142 |
$tagi['values'][] = $val['value'];
|
| 143 |
}
|
| 144 |
}
|
| 145 |
return $parent['children'];
|
| 146 |
}
|
| 147 |
|
| 148 |
?>
|