Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules /

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba

SS3 (3.02) Flickr Photoset && Custom Pagination (page/1, page/2, …)


Go to End
Reply


848 Views

Avatar
nantoga

Community Member, 6 Posts

23 November 2012 at 9:12am

Edited: 25/11/2012 2:16am

The flickr module from SS2.x is still working in SS3 but it creates some error output in SS3 (dev-mode enabled) due to some depreciated function calls. Therefore I created my own Pagetype to display Photosets.

As I received some feedback from this forum to create the page I will share the outcome in order to give back a little bit. It is a good starting point for beginners such as me to use the RestfulService together with flickr. You need to adapt the script to your own needs.

At the same time I would like to ask more experienced Silverstripe-Users to give feedback how this page could be further improved as I think a lot of users might be interested in flickr integration in Silverstripe 3. Especially it was very difficult to figure out how to create subpages page/1 page/2 … page/x. Finally it turned out to be simple but I couldn't find proper documentation on this topic at all. Thankfully there were some postings in the Forum.

****************
FlickrFotoSet.php

<?php

/* ------------------------------------------------------------------
* ------------------------------------------------------------------
* MODEL V20121124
* Compatible: Silverstripe 3.0+
* ------------------------------------------------------------------
*/

class FlickrFotoSet extends Page {

public static $db = array(
'flickr_api_key' => 'Varchar', // API-Key
'flickr_photoset_id' => 'Varchar', // Photoset-ID
'flickr_fotos_per_page' => 'Int', // Images per Page
'flickr_bildgroesse' => 'Varchar', // Flickr Imagesize: Flicker stores the images in different sizes
'PaginationPrefix' => 'Varchar', // Prefix Symbol for Pagination [1][2] (1)(2) etc.
'PaginationSuffix' => 'Varchar' // Suffix Symbol for Pagination
);

public static $has_one = array(
);

/* ------------------------------------------------------------------ */
public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Main', new TextField('flickr_api_key'), 'Content');
$fields->addFieldToTab('Root.Main', new TextField('flickr_photoset_id'), 'Content');
$fields->addFieldToTab('Root.Main', new NumericField('flickr_fotos_per_page'), 'Content');

$dropdownFlickrBildgroesse = new DropdownField(
$name ='flickr_bildgroesse',
$title ='flickr_bildgroesse',
$source = array('small' => 'small size (flickr image suffix: _n)','medium' => 'medium size (flickr image suffix: _z)','large' => 'large size (flickr image suffix: _c)'),
$form = null
);
$dropdownFlickrBildgroesse->setEmptyString('Select flickr image extension...');

$fields->addFieldToTab('Root.Main', $dropdownFlickrBildgroesse,'Content');

$tfPrefix = new TextField(
$name = 'PaginationPrefix',
$title = 'Prefix for Page Numbers (Pagination)'
);
$tfPrefix->setMaxLength(1);
$fields->addFieldToTab('Root.Main', $tfPrefix, 'Content');

$tfSuffix = new TextField(
$name = 'PaginationSuffix',
$title = 'Suffix for Page Numbers (Pagination)'
);
$tfSuffix->setMaxLength(1);
$fields->addFieldToTab('Root.Main', $tfSuffix, 'Content');

$fields->removeFieldFromTab("Root.Main","Content"); // Remove Content Field
return $fields;
} // end getCMSFields

} // end class

/* ------------------------------------------------------------------
* ------------------------------------------------------------------
* CONTROLLER V20121124
* Compatible: Silverstripe 3.0+
* ------------------------------------------------------------------
*/

class FlickrFotoSet_Controller extends Page_Controller {

private $CurrentPage = 1;
private $Seitenanzahl = 0;
private $Bilderanzahl = 0;
private $HTMLPaginierung = '';

private $Photos;
private $Pagination;

/* In order to create subpage urls such as page/1, page/2, page/3, … page/X509_PURPOSE_ANY
* you need to allow an url action in this case 'page'
* The action will trigger a function by the same name 'page'
*/
public static $allowed_actions = array (
'page' => 'page'
);
function page($link) {
if(is_numeric($this->urlParams['ID'])) {$pagenr = $this->urlParams['ID'];}
$this->CurrentPage = intval($pagenr);
return $this->renderWith(array('FlickrFotoSet', 'Page'));
}

/* ------------------------------------------------------------------
* Private Functions
*/

/* ------------------------------------------------------------------ */

/* ------------------------------------------------------------------ */
private function getArrayFromResult($results) {
$result = new ArrayList(); // Ergebnis wird in einem Array gespeichert

foreach($results as $photo){
// Handelt es sich bei dem Foto um ein Querformat?
$Querformat = ($photo->height_m >= $photo->width_m) ? false : true;

// Bildendungen bei Flickr abhängig von der Bildgröße
switch($this->flickr_bildgroesse) {
case ('small'):
$Bildsuffix = "_n";
break;
case ('medium'):
$Bildsuffix = "_z";
break;
case ('large'):
$Bildsuffix = "_c";
break;
default:
$Bildsuffix = "_c";
} // end switch

// Pfade zusammenbauen
$Dateipfad = 'http://farm' .$photo->farm .".staticflickr.com/" . $photo->server . "/" . $photo->id . "_" . $photo->secret . $Bildsuffix . '.jpg';
$LinkFlickrPhotostream = 'http://www.flickr.com/photos/' . $photo->pathalias . '/' . $photo->id . '/in/photostream';
$Bildunterschrift = htmlentities($photo->title);

// Folgende Variablen können im Template ausgelesen werden:
$result->push(new ArrayData(array(
"Querformat" => $Querformat,
"Dateipfad" => $Dateipfad,
"LinkFlickrPhotostream" => $LinkFlickrPhotostream,
"Bildunterschrift" => $Bildunterschrift,
"Latitude" => $photo->latidude,
"Longitude" => $photo->longitude,
"Bildbreite" => $photo->width_m,
"Bildhoehe" => $photo->height_m,
"Tag"=> $photo->tags,
"error"=>0
)));

} // end foreach
$this->Photos = $result;
return $result;
} // end getArrayFromResult

/* ------------------------------------------------------------------ */
private function setPaginationValues($paginierung) {
$result = new ArrayList(); // Ergebnis wird in einem Array gespeichert
$HTMLPaginierung = '';
foreach($paginierung as $page) {
$Seitenanzahl = intval($page->pages); // Total amount of pages
$Bilderanzahl = intval($page->total); // Total amount of images
$CurrentPage = intval($page->page); // Current page
} // end foreach
if ($Seitenanzahl != 1) {
for($i=1; $i <= $Seitenanzahl ; $i++){
if ($i == $CurrentPage) {
$HTMLPaginierung .= '<span class="currentPage">' . $this->PaginationPrefix . $i . $this->PaginationSuffix . '</span>';
} else {
$current_url = Controller::curr()->Link();
$HTMLPaginierung .= '<span class="Page">' . $this->PaginationPrefix . '<a href="' .$current_url . 'page/' . $i . '">' . $i . '</a>' . $this->PaginationSuffix . '</span>';
}
}
}
$HTMLPaginierung .= ' (' . $Bilderanzahl . ' Bilder)';

$this->Seitenanzahl = $Seitenanzahl;
$this->Bilderanzahl = $Bilderanzahl;
$this->CurrentPage = $CurrentPage;
$this->HTMLPaginierung = $HTMLPaginierung;

return 0;
}

/* ------------------------------------------------------------------ */
private function getFlickr_Fotoset($page) {

// Restful-Service-Pfad festlegen:
$flickr = new RestfulService('http://www.flickr.com/services/rest/');

// Parameter für die Abfrage
$params = array(
'method' => 'flickr.photosets.getPhotos',
'photoset_id' => $this->flickr_photoset_id,
'per_page' => $this->flickr_fotos_per_page,
'page' => $page,
'api_key' => $this->flickr_api_key,
'media' => 'photos',
'extras' => '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'
);
$flickr->setQueryString($params);

// Request senden
$response = $flickr->request();
// Check for Error (200 = OK)
$status = $response->getstatusCode();
if ($status == 200) {
// Daten zu den einzelnen Bildern auslesen:
$fotos = $this->getArrayFromResult($flickr->getAttributes($response->getBody(), 'photoset', 'photo'));
// Seitenzahlen auslesen:
$paginierung = $this->setPaginationValues($flickr->getAttributes($response->getBody(), 'photoset'));
}
else {
Debug::show('Flickr Service Error. API-Key wrong, Service not reached...');
$error_message = new ArrayList();
$error_message->push(new ArrayData(array(
"error" => 1
)));
return $error_message;
}
return $fotos;
} // end getFlickr_Fotoset

/* ------------------------------------------------------------------ */
private function getFlickr_Pagination($page) {
$result = new ArrayList(); // Ergebnis wird in einem Array gespeichert
$result->push(new ArrayData(array(
"Seitenanzahl" => $this->Seitenanzahl,
"Bilderanzahl" => $this->Bilderanzahl,
"CurrentPage" => $this->CurrentPage,
"Paginierung" => $this->HTMLPaginierung
)));
$this->Pagination = $result;
return $result;
}

/* ------------------------------------------------------------------
* Public functions
*/

/* ------------------------------------------------------------------ */
public function init() {
parent::init();

} // end init

/* ------------------------------------------------------------------ */
public function getPhotos() {
$this->getFlickr_Fotoset($this->CurrentPage);
return $this->Photos;
} // end FlickrFotoset

public function getPagination() {
$this->getFlickr_Pagination($this->CurrentPage);
return $this->Pagination;
} // end FlickrPaginierung

} // end class

?>

****************
FlickrFotoSet.ss

<div class="content-container">
<div class="content">
<div class="FlickrFotoset">
<% loop getPhotos %>
<% if $error = 0 %>
<article>
<p class="FlickrFoto"><a href="{$LinkFlickrPhotostream}"><img <% if Querformat %>class="FlickrFotoQuerformat" <% else %>class="FlickrFotoHochformat" <% end_if %>src="{$Dateipfad}" alt="{$Bildunterschrift}"></a></p>
<p class="FlickrBildunterschrift"><a href="{$LinkFlickrPhotostream}">{$Bildunterschrift}</a> <%--<%if $Tag %>#<% end_if %>{$Tag}--%></p>
</article>
<% else %>
<p class="FlickrBildunterschrift"><% _t('FLICKR_ERROR','www.flickr.com: API Service not available. Please try again later.') %></p>
<% end_if %>
<% end_loop %>
<p id="Paginierung">
<% loop getPagination %>
{$Paginierung}
<% end_loop %>
</p>
</div> <!-- end FlickrPhotoset -->
<p>$Fusszeile</p>
</div> <!-- end content -->
</div> <!-- end content-container -->