YaWK  24.1
Yet another WebKit
fbGallery.php
Go to the documentation of this file.
1 <?php
3 {
4  /**
5  * @details<b>Use Facebook Graph API to get album photos from a Facebook Page. Requires an App ID and a valid access token.</b>
6  *
7  * <p>With this widget, you are able to embed photos from your facebook page onto your website.
8  * It helps you to keep your website up to date. Have you ever been bored of adding the same content twice?
9  * If you change your facebook photo album the data on your website will be updated automatically. </p>
10  *
11  * <p>You need an APP ID, as well as a valid access token for the facebook page you want to embed photos from.
12  * For reasons, you (respectively the app id / access token) needs administrative access rights to the facebook
13  * page you want to grab photos from. Create a new fb gallery widget in the backend, enter app id, access token
14  * and press save widget settings. The page reloads and your albums will get loaded into a select field.
15  *
16  * </p>
17  *
18  *
19  * @author Daniel Retzl <[email protected]>
20  * @copyright 2018 Daniel Retzl
21  * @version 1.0.0
22  * @brief Facebook Gallery Widget - grab photos from your Facebook albums.
23  */
24  class fbGallery
25  {
26  /** @param string your app ID (from developers.facebook.com) */
27  public $fbGalleryAppId = '';
28  /** @param string your page ID (http://facebook.com/{YOURPAGEID} */
29  public $fbGalleryAlbumId = '';
30  /** @param string your access token (secret word from developers.facebook.com) */
31  public $fbGalleryAccessToken = '';
32  /** @param string your graph request (the Album ID) */
33  public $fbGalleryGraphRequest = '/albums/';
34  /** @param string fields that should be selected from facebook graph */
35  public $fbGalleryFields = 'images,source,name,id';
36  /** @param string show ITEMS of this time range */
37  public $fbGalleryYearRange = '10';
38  /** @param string user defined start date */
39  public $fbGalleryStartDate = '';
40  /** @param string user defined end date */
41  public $fbGalleryEndDate = '';
42  /** @param string which items should be shown? future|past|all */
43  public $fbGalleryType = 'past';
44  /** @param string gallery heading */
45  public $fbGalleryHeading = '';
46  /** @param string gallery small subtext beside heading */
47  public $fbGallerySubtext = '';
48  /** @param string ITEMS since this date (used for calc) */
49  public $sinceDate = '';
50  /** @param int limit entries to (n) */
51  public $fbGalleryLimit = 0;
52  /** @param string sortation */
53  public $fbGallerySortation = 'desc';
54  /** @param int layout */
55  public $fbGalleryLayout = 6;
56  /** @param int show info under the gallery? 0|1 */
57  public $fbGalleryImageInfo = 1;
58  /** @param string fixed image height in pixels or auto (select field) */
59  public $fbGalleryFixedImageHeight = 'auto';
60  /** @param int shuffle 0|1 if true, images get shuffled on page load */
61  public $shuffle = 0;
62  /** @param string ITEMS until this date (used for calc) */
63  public $untilDate = '';
64  /** @param string true|false was the js SDK loaded? */
65  public $jsSDKLoaded = 'false';
66  /** @param object api result (as object) */
67  public $apiObject;
68  /** @param array temporary settings array */
69  public $settings;
70 
71 
72  /**
73  * @brief fbGallery constructor.
74  * Get widget settings into this->settings array and call checkRequirements
75  * @param $db
76  */
77  public function __construct($db)
78  {
79  // load this widget settings from db
80  $widget = new \YAWK\widget();
81  $this->settings = $widget->getWidgetSettingsArray($db);
82  foreach ($this->settings as $property => $value)
83  {
84  $this->$property = $value;
85  }
86  // check if required settings are set
87  $this->checkRequirements();
88  }
89 
90  /**
91  * @brief Check if all requirements are fulfilled to perform api call.
92  */
93  public function checkRequirements()
94  {
95  $this->checkAppId();
96  $this->checkAccessToken();
97  $this->checkAlbumId();
98  }
99 
100  /**
101  * @brief Check if App ID is set, not empty and numeric. Returns true if app ID is ok or abort with an error message.
102  * @return bool
103  */
104  public function checkAppId()
105  { // check if App ID is set and not empty
106  if (isset($this->fbGalleryAppId) && (!empty($this->fbGalleryAppId)))
107  { // is app ID a number for sure?
108  if (is_numeric($this->fbGalleryAppId))
109  { // app id seems legit
110  return true;
111  }
112  else
113  { // app id not numeric, abort with error message
114  die ("app ID is set, but not a numeric value! Please check your app ID - it should contain numbers only.");
115  }
116  }
117  else
118  { // app id not set or empty, abort with error message
119  die ("app ID is not set. Please add your app ID. You can obtain it from http://developers.facebook.com");
120  }
121  }
122 
123  /**
124  * @brief Check if access token is correctly set. Returns true or abort with an error message
125  * @return bool
126  */
127  public function checkAccessToken()
128  { // check if access token is set and not empty
129  if (isset($this->fbGalleryAccessToken) && (!empty($this->fbGalleryAccessToken)))
130  { // check if access token is a string
131  if (is_string($this->fbGalleryAccessToken))
132  { // access token seems legit
133  return true;
134  }
135  else
136  { // access token is not a string, abort with error message
137  die ("Access token is set, but not a string value! Please check your access token.");
138  }
139  }
140  else
141  { // access token is not set or empty, abort with error message
142  die ("Access token is not set. Please add your access token. You can obtain it from http://developers.facebook.com/apps");
143  }
144  }
145 
146  /**
147  * @brief Check if album id is correctly set. Returns true or abort with an error message
148  * @return bool
149  */
150  public function checkAlbumId()
151  { // check if Album ID is set and not empty
152  if (isset($this->fbGalleryAlbumId) && (!empty($this->fbGalleryAlbumId)))
153  { // check if album ID is a string
154  if (is_string($this->fbGalleryAlbumId))
155  { // seems legit
156  return true;
157  }
158  else
159  { // album id is not a string, abort with error msg
160  die ("Album ID is set, but not a string value! Please check your photo album ID.");
161  }
162  }
163  else
164  { // album id is not set or empty, abort with error msg
165  die ("Album ID is not set. Please add your photo album ID.");
166  }
167  }
168 
169  /**
170  * @brief Load Facebook JS Code.
171  */
172  public function loadJSSDK()
173  { // check if fb JS SDK was loaded before
174  if ($this->jsSDKLoaded == 'false')
175  { // check if app ID is set
176  if ($this->checkAppId() == true)
177  {
178  /*
179  // include facebook SDK JS
180  echo "<script>
181  window.fbAsyncInit = function() {
182  FB.init({
183  appId : '" . $this->fbGalleryAppId . "',
184  xfbml : true,
185  version : 'v3.3'
186  });
187  FB.AppITEMS.logPageView();
188  };
189 
190  (function(d, s, id){
191  var js, fjs = d.getElementsByTagName(s)[0];
192  if (d.getElementById(id)) {return;}
193  js = d.createElement(s); js.id = id;
194  js.src = \"https://connect.facebook.net/en_US/sdk.js\";
195  fjs.parentNode.insertBefore(js, fjs);
196  }(document, 'script', 'facebook-jssdk'));
197  </script>";
198  $this->jsSDKLoaded = 'true';
199  */
200  }
201  /*
202  else
203  { // check app ID failed, abort with error msg (app id not set correctly)
204  die ("unable to include facebook js SDK - checkAppId failed. Please check your app ID in the widget settings!");
205  }
206  */
207  }
208  }
209 
210  /**
211  * @brief Prepare object data, set json link, make API call and return apiObject
212  * @return object
213  */
214  public function makeApiCall()
215  {
216  // WHICH ITEMS TO DISPLAY?
217  // evaluation of event type select field
218  if ($this->fbGalleryType == "all")
219  {
220  // ALL ITEMS (FUTURE + PAST)
221  $this->sinceDate = date('Y-01-01', strtotime('-' . $this->fbGalleryYearRange . ' years'));
222  $this->untilDate = date('Y-01-01', strtotime('+' . $this->fbGalleryYearRange . ' years'));
223  }
224  elseif ($this->fbGalleryType == "future")
225  {
226  // UPCOMING ITEMS
227  $this->sinceDate = date('Y-m-d');
228  $this->untilDate = date('Y-12-31', strtotime('+' . $this->fbGalleryYearRange . ' years'));
229  }
230  elseif ($this->fbGalleryType == "past")
231  {
232  // PAST ITEMS
233  $this->sinceDate = date('Y-01-01', strtotime('-' . $this->fbGalleryYearRange . ' years'));
234  $this->untilDate = date('Y-m-d');
235  }
236  else
237  { // IF NOT SET - use default:
238  // UPCOMING ITEMS
239  $this->sinceDate = date('Y-m-d');
240  $this->untilDate = date('Y-12-31', strtotime('+' . $this->fbGalleryYearRange . ' years'));
241  }
242 
243  // IF START + END DATE IS SET
244  if (isset($this->fbGalleryStartDate) && (!empty($this->fbGalleryStartDate))
245  && (isset($this->fbGalleryEndDate) && (!empty($this->fbGalleryEndDate))))
246  {
247  $this->sinceDate = date($this->fbGalleryStartDate);
248  $this->untilDate = date($this->fbGalleryEndDate);
249  }
250 
251  // unix timestamp years
252  $since_unix_timestamp = strtotime($this->sinceDate);
253  $until_unix_timestamp = strtotime($this->untilDate);
254 
255  // check if fields are set
256  if (isset($this->fbGalleryFields) && (!empty($this->fbGalleryFields)))
257  { // set markup for api query string
258  $fieldsMarkup = "?fields={$this->fbGalleryFields}";
259  }
260  else
261  { // no fields wanted, leave markup empty
262  $fieldsMarkup = '';
263  }
264 
265  // prepare API call - get photos
266  $json_link = "https://graph.facebook.com/v3.3/{$this->fbGalleryAlbumId}/photos$fieldsMarkup&access_token={$this->fbGalleryAccessToken}";
267 
268  // get json string
269  $curl = curl_init();
270  curl_setopt($curl, CURLOPT_URL, $json_link);
271  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
272  $this->apiObject = json_decode(curl_exec($curl), true, 512, JSON_BIGINT_AS_STRING);
273  curl_close($curl);
274 
275  // $json = file_get_contents($json_link);
276  // convert json to object
277  return $this->apiObject;
278  }
279 
280  /**
281  * @brief Check if api object is set and not empty
282  * @return bool returns true or false
283  */
284  public function checkApiObjectData()
285  { // check if object data is set and not empty
286  if (isset($this->apiObject['data']) && (!empty($this->apiObject['data'])))
287  { // seems legit
288  return true;
289  }
290  else
291  { // object not set or empty
292  return false;
293  }
294  }
295 
296  /**
297  * @brief The heart of this widget: this method draws the gallery.
298  */
299  public function drawGallery()
300  {
301  // load facebook JS
302  $this->loadJSSDK();
303  // make facebook api call
304  $this->makeApiCall();
305 
306  /* ALBUM DETAILED VIEW */
307  if (isset($this->apiObject['data']) && (!empty($this->apiObject))) {
308 
309  // check if array needs to be sorted
310  if (is_string($this->fbGallerySortation) && ($this->fbGallerySortation == "desc"))
311  {
312  // sort array ascending / descending
313  $this->apiObject['data'] = array_reverse($this->apiObject['data']);
314  }
315 
316  // check if images needs to be shuffled
317  elseif ($this->fbGallerySortation == "shuffle")
318  { // shuffle images on page load
319  shuffle($this->apiObject['data']);
320  }
321 
322  // check if limit is set
323  if ($this->fbGalleryLimit > 0 && ($this->fbGalleryLimit != 25))
324  { // limit images to x entries
325  $this->apiObject['data'] = array_slice($this->apiObject['data'], 0, $this->fbGalleryLimit);
326  }
327 
328  // check gallery layout
329  if (!isset($this->fbGalleryLayout) || (empty($this->fbGalleryLayout)))
330  { // if no layout is set, use this as default value
331  $this->fbGalleryLayout = 6;
332  }
333 
334  // check heading
335  if (isset($this->fbGalleryHeading) && (!empty($this->fbGalleryHeading)))
336  {
337  // if heading is set, build markup
338  $this->fbGalleryHeading = "$this->fbGalleryHeading";
339  }
340  else
341  { // no heading was set, leave empty
342  $this->fbGalleryHeading = '';
343  }
344 
345  // check subtext
346  if (isset($this->fbGallerySubtext) && (!empty($this->fbGallerySubtext)))
347  {
348  // if subtext is set, build markup
349  $this->fbGallerySubtext = "<small>$this->fbGallerySubtext</small>";
350  }
351  else
352  { // no subtext was set, leave empty
353  $this->fbGallerySubtext = '';
354  }
355 
356  // draw heading + subtext
357  echo "<div class=\"row padding\"><div class=\"col-md-12\"><h1>$this->fbGalleryHeading&nbsp;$this->fbGallerySubtext</h1></div></div>";
358 
359 
360  $i = 0; // loop indicator
361  // walk through data array to help animation (first items fadeIn on load)
362  echo "<div class=\"row padding\">";
363  foreach ($this->apiObject['data'] as $property => $value)
364  {
365  // if loop runs for the first time
366  if (isset($i) && ($i <= 3))
367  { // first element get this css class
368  $animateMarkup = 'animated fadeIn';
369  }
370  else
371  { // all other items - set animate class on them
372  $animateMarkup = "animate";
373  }
374 
375  $i++; // raise loop indicator
376 
377  // check if image info is set
378  if (isset($this->fbGalleryImageInfo) && (!empty($this->fbGalleryImageInfo)))
379  {
380  // if image info is on
381  $this->fbGalleryImageInfo = $value['name'];
382  }
383  else
384  { // leave image info empty
385  $this->fbGalleryImageInfo = '';
386  }
387 
388  // random align images
389  $randomInt = rand(1,4);
390  if ($randomInt == 1)
391  {
392  $imgClass = "img-lefty";
393  }
394  elseif ($randomInt == 2)
395  {
396  $imgClass = "img-lefty-less";
397  }
398  elseif ($randomInt == 3)
399  {
400  $imgClass = "img-righty";
401  }
402  elseif ($randomInt == 4)
403  {
404  $imgClass = "img-righty-less";
405  }
406  /*
407  elseif ($randomInt == 5)
408  {
409  $imgClass = "img-thumbnail";
410  }
411  else
412  { // default value:
413  $imgClass = "img-thumbnail";
414  }
415  */
416 
417  // set filename to biggest image source
418  $fn = $value['images'][0]['source'];
419  // set layout div box, containing every single image
420  echo "<div class=\"col-md-$this->fbGalleryLayout text-center $animateMarkup\">
421  <a href=\"$fn\" data-lightbox=\"example-set\" data-title=\"$value[name]\">
422  <img src=\"$fn\" alt=\"$value[name]\" style=\"width:auto; height:$this->fbGalleryFixedImageHeight;\" class=\"img-thumbnail img-fluid ".$imgClass." hvr-grow\">
423  </a><br><br><small>$this->fbGalleryImageInfo</small><br><br><hr></div>";
424  } // end foreach
425  echo "</div>";
426  }
427  else
428  { // api object not set or empty - abort with error
429  die("Unable to load Images from Facebook API because the apiObject is not set or empty.");
430  }
431  }
432 
433  } // end class fbGallery
434 } // end namespace
die
Definition: block-user.php:27
Facebook Gallery Widget - grab photos from your Facebook albums.
Definition: fbGallery.php:24
checkRequirements()
Check if all requirements are fulfilled to perform api call.
Definition: fbGallery.php:92
__construct($db)
fbGallery constructor. Get widget settings into this->settings array and call checkRequirements
Definition: fbGallery.php:76
makeApiCall()
Prepare object data, set json link, make API call and return apiObject.
Definition: fbGallery.php:213
drawGallery()
The heart of this widget: this method draws the gallery.
Definition: fbGallery.php:298
checkAccessToken()
Check if access token is correctly set. Returns true or abort with an error message.
Definition: fbGallery.php:126
checkAppId()
Check if App ID is set, not empty and numeric. Returns true if app ID is ok or abort with an error me...
Definition: fbGallery.php:103
checkApiObjectData()
Check if api object is set and not empty.
Definition: fbGallery.php:283
loadJSSDK()
Load Facebook JS Code.
Definition: fbGallery.php:171
checkAlbumId()
Check if album id is correctly set. Returns true or abort with an error message.
Definition: fbGallery.php:149
Settings class: get and set YaWK system settings.
Definition: settings.php:9
print $tourdates date
$value
$i