View source
<?php
namespace Drupal\facebook_album\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\block\BlockInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\facebook_album\FacebookAlbumInterface;
use Zend\Diactoros\Response\JsonResponse;
class FacebookAlbumController extends ControllerBase {
protected $facebook_album;
public function __construct(FacebookAlbumInterface $facebook_album) {
$this->facebook_album = $facebook_album;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('facebook_album.controller'));
}
public function getAlbums(BlockInterface $block) {
return $this
->getAlbumsNext($block);
}
public function getAlbumsNext(BlockInterface $block, $after = NULL) {
$settings = $block
->get('settings');
$limit = $settings['album_limit'];
if ($limit < 1) {
$limit = NULL;
}
else {
$after = NULL;
}
$path = $settings['page_id'] . '/albums';
$parameters = [
'after' => $after,
'limit' => $limit,
'fields' => 'location,description,name,cover_photo.fields(images)',
];
$response = $this
->makeRequest($path, $parameters);
$filtered_content = $this
->filterAlbums($response['data'], $settings['albums'], $settings['album_visibility']);
$json_response = [];
$render = [
'#theme' => 'facebook_album_covers',
'#settings' => $settings,
'#photos' => $filtered_content,
];
$json_response['data']['content'] = \Drupal::service('renderer')
->render($render);
if (isset($response['paging']) && isset($response['paging']['next']) && $limit == NULL) {
$json_response['data']['after'] = $response['paging']['cursors']['after'];
}
else {
$json_response['data']['after'] = NULL;
}
return new JsonResponse($json_response);
}
public function getAlbum(BlockInterface $block, $album_id) {
return $this
->getAlbumNext($block, $album_id);
}
public function getAlbumNext(BlockInterface $block, $album_id, $after = NULL) {
$settings = $block
->get('settings');
$parameters = [
'after' => $after,
'fields' => 'url',
];
$response = $this
->makeRequest($album_id . '/photos', $parameters);
$json_response = [];
$render = [
'#theme' => 'facebook_album_photos',
'#settings' => $settings,
'#photos' => $response['data'],
];
$json_response['data']['content'] = \Drupal::service('renderer')
->render($render);
$json_response['data']['photo_ids'] = $response['data'];
if (isset($response['paging']) && isset($response['paging']['next'])) {
$json_response['data']['after'] = $response['paging']['cursors']['after'];
}
return new JsonResponse($json_response);
}
public function getPhoto($photo_id) {
$json_response = array(
'data' => NULL,
);
$parameters = [
'fields' => 'images,name',
];
$response = $this
->makeRequest($photo_id, $parameters);
if (!isset($response['error'])) {
$json_response['data']['url'] = $response['images'][0]['source'];
$json_response['data']['name'] = isset($response['name']) ? $response['name'] : '';
}
return new JsonResponse($json_response);
}
protected function filterAlbums($albums, $album_ids = [], $include = TRUE) {
if (isset($album_ids[0]) && ($album_ids[0] != '' || $album_ids[0] == 0)) {
$include = (bool) $include;
$albums = array_filter($albums, function ($album) use ($album_ids, $include) {
return $include === in_array($album['id'], $album_ids);
});
}
return $albums;
}
protected function makeRequest($path, $parameters = []) {
$cid = 'fba:' . str_replace("/", ":", $path);
foreach ($parameters as $key => $parameter) {
if ($key != 'fields' && !empty($parameter)) {
$cid .= ':' . $parameter;
}
}
if ($cache = \Drupal::cache()
->get($cid)) {
$response = $cache->data;
}
else {
$response = $this->facebook_album
->get($path, $parameters);
if (!isset($response['error'])) {
\Drupal::cache()
->set($cid, $response);
}
}
return $response;
}
}