Skip to content

Commit 84b3dca

Browse files
committed
Support collections
1 parent f7a25f3 commit 84b3dca

9 files changed

Lines changed: 162 additions & 8 deletions

blueprints.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ form:
3737
type: text
3838
size: medium
3939
label: Flickr API Key
40+
flickr_user_id:
41+
type: text
42+
size: medium
43+
label: Flickr User ID
4044

classes/Collection.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Grav\Plugin\Flickr;
4+
require_once(__DIR__.'/Photoset.php');
5+
use Grav\Plugin\Flickr\Photoset;
6+
7+
use Grav\Common\Grav;
8+
9+
class Collection
10+
{
11+
private $info;
12+
private $collections;
13+
private $sets;
14+
15+
public function __construct($tree, $api)
16+
{
17+
$this->info = $tree;
18+
$this->collections = [];
19+
$this->sets = [];
20+
dump($tree);
21+
if(is_array($tree['collection'])) {
22+
foreach($tree['collection'] as $collection) {
23+
$this->collections[] = new Collection($collection, $api);
24+
}
25+
}
26+
if(is_array($tree['set'])) {
27+
foreach($tree['set'] as $set) {
28+
$this->sets[] = $api->photoset($set['id'], []);
29+
}
30+
}
31+
dump($this->collections);
32+
dump($this->sets);
33+
}
34+
35+
36+
public function title() {
37+
return $this->info["title"];
38+
}
39+
public function description() {
40+
return $this->info["description"];
41+
}
42+
43+
public function collections() {
44+
return $this->collections;
45+
}
46+
47+
public function sets() {
48+
return $this->sets;
49+
}
50+
}

classes/FlickrAPI.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Grav\Plugin\Flickr;
44
require_once(__DIR__.'/Photoset.php');
5+
require_once(__DIR__.'/Collection.php');
56

67
use Grav\Common\Grav;
78
use Grav\Plugin\Flickr\Photoset;
9+
use Grav\Plugin\Flickr\Collection;
810
use Grav\Common\GPM\Response;
911
use Grav\Common\Cache;
1012

@@ -19,6 +21,7 @@ class FlickrAPI
1921
{
2022
protected $key;
2123
protected $secret;
24+
protected $user_id;
2225
protected $grav;
2326
protected $config;
2427
protected $cache;
@@ -32,26 +35,41 @@ public function __construct()
3235
$this->config = $this->grav['config'];
3336
$this->key = $this->config->get('plugins.flickr.flickr_api_key');
3437
$this->secret = $this->config->get('plugins.flickr.flickr_api_secret');
38+
$this->user_id = $this->config->get('plugins.flickr.flickr_user_id');
3539
$this->cache = new Cache($this->grav);
3640
}
3741

38-
/**
39-
* do some work
40-
*/
4142
public function photoset($id, $params)
4243
{
4344
$info = $this->request( ['method' => 'flickr.photosets.getInfo', 'photoset_id' => $id ])['photoset'];
4445
$get_photos_params = array_merge(
4546
[ "method" => "flickr.photosets.getPhotos", "photoset_id" => $id, 'user_id' => $info['owner'], 'extras' =>
46-
'license, date_upload, date_taken, owner_name, icon_server, original_format, last_update, geo, tags, machine_tags, o_dims, views, media, path_alias, url_sq, url_t, url_s, url_m, url_o' ],
47+
'license,date_upload,date_taken,owner_name,icon_server,original_format,last_update,geo,tags,machine_tags,views,media' ],
4748
$this->get_params($params, ['page', 'per_page', 'privacy_filter', 'media']));
4849

4950
$photos = $this->request( $get_photos_params )['photoset'];
5051
return new Photoset($info, $photos);
5152
}
5253

54+
public function photo($id)
55+
{
56+
$info = $this->request( ['method' => 'flickr.photos.getInfo', 'photo_id' => $id ])['photo'];
57+
return new Photo($info);
58+
}
59+
60+
public function collection($id)
61+
{
62+
$info = $this->request( ['method' => 'flickr.collections.getTree', 'collection_id' => $id ])['collections'];
63+
foreach($info['collection'] as $collection) {
64+
if( ! strpos($collection['id'], $id) ) {
65+
return new Collection($collection, $this);
66+
}
67+
}
68+
return null; // TODO
69+
}
70+
5371
private function request($params) {
54-
$url = 'https://api.flickr.com/services/rest/?' . http_build_query(array_merge($params, ['api_key' => $this->key, 'format' => 'php_serial']));
72+
$url = 'https://api.flickr.com/services/rest/?' . http_build_query(array_merge($params, ['api_key' => $this->key, 'format' => 'php_serial', 'user_id' => $this->user_id]));
5573
$obj = $this->cache->fetch($url);
5674
if($obj) {
5775
return $obj;

classes/Photo.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ public function id() {
1818
}
1919

2020
public function title() {
21-
return $this->info['title'];
21+
return $this->content($this->info['title']);
2222
}
2323

2424
public function datetaken() {
25-
return $this->info['datetaken'];
25+
return $this->content($this->info['datetaken']);
2626
}
2727

2828
public function url($format) {
@@ -34,5 +34,9 @@ public function url($format) {
3434
}
3535
return 'https://farm' . $this->info['farm'] . '.staticflickr.com/' . $this->info['server'] . '/' . $this->info['id'] . '_' . $this->info['secret'] . '.jpg';
3636
}
37+
38+
private function content($val) {
39+
return is_array($val) ? $val['_content'] : $val;
40+
}
3741
}
3842

flickr.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
enabled: true
22
flickr_api_key: ""
33
flickr_api_secret: ""
4+
flickr_user_id: ""
45

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Grav\Plugin\Shortcodes;
4+
require_once(__DIR__.'/../classes/FlickrAPI.php');
5+
use Grav\Common\Utils;
6+
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
7+
use Grav\Plugin\Flickr\FlickrAPI;
8+
use Grav\Plugin\Flickr\FlickrAPIException;
9+
use Grav\Plugin\Flickr\Photoset;
10+
11+
class FlickrCollectionShortcode extends Shortcode
12+
{
13+
public function init()
14+
{
15+
$this->shortcode->getHandlers()->add('flickr-collection', function(ShortcodeInterface $sc) {
16+
$content = $sc->getContent();
17+
$id = $sc->getParameter('id', '');
18+
$api = new FlickrAPI();
19+
$params = array_merge(['display-format-photo' => 's', 'display-format-photo-lightbox' => 'z'], $sc->getParameters());
20+
try {
21+
$collection = $api->collection($id );
22+
$output = $this->twig->processTemplate('partials/flickr-collection.html.twig', [
23+
'collection' => $collection,
24+
'params' => $params,
25+
]);
26+
27+
return $output;
28+
} catch(FlickrAPIException $e) {
29+
return $e->getMessage();
30+
}
31+
});
32+
}
33+
}
34+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Grav\Plugin\Shortcodes;
4+
require_once(__DIR__.'/../classes/FlickrAPI.php');
5+
use Grav\Common\Utils;
6+
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
7+
use Grav\Plugin\Flickr\FlickrAPI;
8+
use Grav\Plugin\Flickr\FlickrAPIException;
9+
use Grav\Plugin\Flickr\Photoset;
10+
11+
class FlickrPhotoShortcode extends Shortcode
12+
{
13+
public function init()
14+
{
15+
$this->shortcode->getHandlers()->add('flickr-photo', function(ShortcodeInterface $sc) {
16+
$content = $sc->getContent();
17+
$id = $sc->getParameter('id', '');
18+
$api = new FlickrAPI();
19+
$params = array_merge(['display-format-photo' => 's', 'display-format-photo-lightbox' => 'z'], $sc->getParameters());
20+
try {
21+
$photo = $api->photo($id );
22+
$output = $this->twig->processTemplate('partials/flickr-photo.html.twig', [
23+
'photo' => $photo,
24+
'params' => $params,
25+
]);
26+
27+
return $output;
28+
} catch(FlickrAPIException $e) {
29+
return $e->getMessage();
30+
}
31+
});
32+
}
33+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<div class="flickr flickr-collection">
2+
<h2>{{ collection.title() }}</h2>
3+
<h3>{{ collection.description() }}</h3>
4+
{% for set in collection.sets() %}
5+
{% include 'partials/flickr-photoset.html.twig' with {'photoset':set} %}
6+
{% endfor %}
7+
</div>
8+
{% for current in collection.collections() %}
9+
{% include 'partials/flickr-collection.html.twig' with {'collection':current} %}
10+
{% endfor %}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<div id="lightbox-{{ photo.id() }}" class="flickr-lightbox">
22
<img src="{{ photo.url(params['display-format-photo-lightbox']) }}">
33
<figcaption>{{ photo.title() }}</figcaption>
4-
<span><a href="{{ photo.url('o') }}" target='_blank'>High resolution version</a></span>
4+
<span><a href="{{ photo.url('o') }}" target='_blank'>Original version</a></span>
55
</div>

0 commit comments

Comments
 (0)