YaWK  24.1
Yet another WebKit
fbFeed.php
Go to the documentation of this file.
1 <?php
3 {
4  /**
5  * @details <b>Use Facebook Graph API to get feed data from a Facebook Page. Require App ID and Access Token.</b>
6  * <p>This is just an empty example widget for development and demo purpose!</p>
7  *
8  * <p>This Widget gets the latest (limit) posts of your facebook page feed.</p>
9  *
10  * @author Daniel Retzl <[email protected]>
11  * @copyright 2019 Daniel Retzl
12  * @version 1.0.0
13  * @brief Facebook Page Feed
14  */
15 class fbFeed
16 {
17  /** @param string your app ID (from developers.facebook.com) */
18  public $fbFeedAppId = '';
19  /** @param string your page ID (http://facebook.com/{YOURPAGEID} */
20  public $fbFeedPageId = '';
21  /** @param string your access token (secret word from developers.facebook.com) */
22  public $fbFeedAccessToken = '';
23  /** @param string your graph request */
24  public $fbFeedGraphRequest = '/me/feed/';
25  /** @param string fields that should be selected from facebook graph */
26  public $fbFeedFields = 'picture,message,place,created_time,full_picture,coordinates';
27  /** @param string show events of this time range */
28  public $fbFeedLimit = '1';
29  /** @param object api result (as object) */
30  public $apiObject;
31  /** @param string the json link */
32  public $jsonLink;
33  /** @param string date of the posting */
34  public $eventDate;
35  /** @param string posting date */
36  public $dateString;
37  /** @param string date (time ago) helper */
38  public $fbEventsDatewordCss;
39  /** @param string posting message */
40  public $message;
41  /** @param string photo src */
42  public $picture;
43  /** @param string place */
44  public $place;
45 
46  public function __construct($db)
47  {
48  // load this widget settings from db
49  $widget = new \YAWK\widget();
50  $settings = $widget->getWidgetSettingsArray($db);
51  foreach ($settings as $property => $value)
52  {
53  $this->$property = $value;
54  }
55  // check if required settings are set
56  $this->checkRequirements();
57  }
58 
59  public function checkRequirements()
60  {
61  $this->checkAppId();
62  $this->checkAccessToken();
63  $this->checkPageId();
64  }
65 
66  public function checkAppId()
67  {
68  if (isset($this->fbFeedAppId) && (!empty($this->fbFeedAppId)))
69  {
70  if (is_numeric($this->fbFeedAppId))
71  {
72  return true;
73  }
74  else
75  {
76  die ("app ID is set, but not a numeric value! Please check your app ID - it should contain numbers only.");
77  }
78  }
79  else
80  {
81  die ("app ID is not set. Please add your app ID. You can obtain it from http://developers.facebook.com");
82  }
83  }
84 
85  public function checkAccessToken()
86  {
87  if (isset($this->fbFeedAccessToken) && (!empty($this->fbFeedAccessToken)))
88  {
89  if (is_string($this->fbFeedAccessToken))
90  {
91  return true;
92  }
93  else
94  {
95  die ("Access token is set, but not a string value! Please check your access token.");
96  }
97  }
98  else
99  {
100  die ("Access token is not set. Please add your access token. You can obtain it from http://developers.facebook.com");
101  }
102  }
103  public function checkPageId()
104  {
105  if (isset($this->fbFeedPageId) && (!empty($this->fbFeedPageId)))
106  {
107  if (is_string($this->fbFeedPageId))
108  {
109  return true;
110  }
111  else
112  {
113  die ("Page ID is set, but not a string value! Please check your page ID.");
114  }
115  }
116  else
117  {
118  die ("Page ID is not set. Please add your page ID. The Page ID is: https://www.facebook.com/{YOURPAGEID}");
119  }
120  }
121 
122  public function makeApiCall()
123  {
124  // check if pageID is set
125  if (isset($this->fbFeedPageId) && (!empty($this->fbFeedPageId)))
126  {
127  // set markup for pageID string
128  $this->fbFeedAppId = $this->fbFeedPageId;
129  }
130  else
131  { // leave empty if no page id is given (to build custom graph string)
132  $this->fbFeedPageId = '';
133  }
134 
135  // check if fields are set
136  if (isset($this->fbFeedFields) && (!empty($this->fbFeedFields)))
137  { // set markup for api query string
138  if (empty($this->fbFeedPageId))
139  {
140  $this->fbFeedFields = "?fields={$this->fbFeedFields}";
141  }
142  }
143  else
144  { // no fields wanted, leave markup empty
145  $this->fbFeedFields = '';
146  }
147 
148  // prepare API call
149  // $json_link = "https://graph.facebook.com/v3.3/me/events/?fields={$this->fields}&access_token={$this->fbEventsAccessToken}&since={$since_unix_timestamp}&until={$until_unix_timestamp}";
150  $this->jsonLink = "https://graph.facebook.com/v3.3/{$this->fbFeedGraphRequest}?fields={$this->fbFeedFields}&access_token={$this->fbFeedAccessToken}&limit={$this->fbFeedLimit}";
151  // get json string
152  $curl = curl_init();
153  curl_setopt($curl, CURLOPT_URL, $this->jsonLink);
154  curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
155  // decode json and create object
156  $this->apiObject = json_decode(curl_exec($curl), true, 512, JSON_BIGINT_AS_STRING);
157  curl_close($curl);
158 
159 
160  // if object is set, but array got not data...
161  if (isset($this->apiObject) && (is_array($this->apiObject['data']))) { // no upcoming data found
162  return $this->apiObject;
163  }
164  else
165  {
166  echo "<pre>";
167  print_r($this->apiObject);
168  echo "</pre><hr>";
169  die('There was an error during the curl process. Please check your facebook connection data.');
170  }
171  }
172 
173  public function checkApiObjectData()
174  {
175  if (isset($this->apiObject['data']) && (!empty($this->apiObject['data'])))
176  {
177  return true;
178  }
179  else
180  {
181  return false;
182  }
183  }
184 
185  public function printApiObject()
186  {
187  $this->makeApiCall();
188  if ($this->checkApiObjectData() === true)
189  {
190  echo "<pre>";
191  print_r($this);
192  echo "</pre>";
193  }
194  else
195  {
196  echo "<pre>";
197  echo "Could not retrieve any data from Facebook. Please check your PageID, API request, field and date settings";
198  echo "</pre>";
199  exit;
200  }
201  }
202 
203  public function basicOutput()
204  {
205  // get data
206  $this->makeApiCall();
207 
208  // set limit
210  // remove 1, to loop correctly trough array
211  // because $this->apiObject['data'] starts with zero
212  $i--;
213 
214  // newest items first
215  $this->apiObject['data'] = array_reverse($this->apiObject['data']);
216 
217  // starting row
218  echo '<div class="row" style="margin:10px;">';
219 
220  if ($i === 0)
221  {
222  die('Oops, something went wrong here: NEWS data could not be retrieved.');
223  }
224 
225  $loopCount = 0;
226  // loop, while there is array data
227  while ($i >= 0)
228  {
229  // create date obj
230  $this->eventDate = new \DateTime($this->apiObject['data'][$i]['created_time']);
231  // format as mysql date
232  $this->prettyDate = $this->eventDate->format('d.m.Y H:i');
233  // current time as obj
234  $now = new \DateTime();
235 
236  // difference (object) between now and event date
237  $until = $now->diff($this->eventDate, true);
238 
239  // the event date as string in format:
240  $eventDateSimple = $this->eventDate->format('Y-m-d');
241  // current date as string in format:
242  $currentDateSimple = $now->format('Y-m-d');
243 
244  // some german stuff
245  if ($this->eventDate > $now)
246  { // future prepend text
247  $prepend = "in";
248  }
249  else
250  { // past prepend text
251  $prepend = "vor";
252  }
253 
254  // calculate how many days, weeks, months or years the event is away
255  // MONTHS: time between more than one month and under one year
256  if ($until->days >= 31 && ($until->days < 365))
257  { // less than two months
258  if ($until->m < 2)
259  { // singular
260  $duration = "Monat";
261  }
262  else
263  { // plural
264  $duration = "Monaten";
265  }
266  // set wordy date string
267  $this->dateString = "$prepend $until->m $duration";
268  }
269  // not within a year
270  else if ($until->days >= 365)
271  { // less than two years
272  if ($until->y < 2)
273  { // singular
274  $duration = "Jahr";
275  }
276  else
277  { // plural
278  $duration = "Jahren";
279  }
280  // set wordy date string
281  $this->dateString = "$prepend $until->y $duration";
282  }
283 
284  // split month into smaller bits
285  else if ($until->days === 14)
286  {
287  if ($this->eventDate > $now)
288  {
289  // in exactly two weeks
290  $this->dateString = "in zwei Wochen";
291  }
292  else
293  {
294  // two weeks ago
295  $this->dateString = "vor zwei Wochen";
296  }
297  }
298  else if ($until->days >= 8 && ($until->days <= 13))
299  { if ($this->eventDate > $now)
300  {
301  // nearly two weeks
302  $this->dateString = "in knapp zwei Wochen";
303  }
304  else
305  {
306  // nearly two weeks ago
307  $this->dateString = "vor knapp zwei Wochen";
308  }
309  }
310  else if ($until->days === 7)
311  { if ($this->eventDate > $now)
312  {
313  // in exactly one week
314  $this->dateString = "in einer Woche";
315  }
316  else
317  {
318  // exactly one week ago
319  $this->dateString = "vor einer Woche";
320  }
321  }
322  else if ($until->days === 2)
323  {
324  if ($this->eventDate > $now)
325  {
326  // in two days
327  $this->dateString = "&uuml;bermorgen";
328  }
329  else
330  {
331  // two days ago
332  $this->dateString = "vorgestern";
333  }
334  }
335  else if ($until->days === 1)
336  {
337  if ($this->eventDate > $now)
338  {
339  // tomorrow
340  $this->dateString = "<span class=\"".$this->fbEventsDatewordCss."\">morgen</span>";
341  }
342  else
343  {
344  // yesterday
345  $this->dateString = "<span class=\"".$this->fbEventsDatewordCss."\">gestern</span>";
346  }
347  }
348  // 0 days remaining, eventDate and currentDate are the same -
349  else if ($eventDateSimple == $currentDateSimple)
350  { // it must be today
351  $this->dateString = "<span class=\"".$this->fbEventsDatewordCss."\">HEUTE !</span>";
352  }
353  else
354  { if ($this->eventDate > $now)
355  {
356  // any other amount of days
357  $this->dateString = "$prepend $until->d Tagen";
358  }
359  else
360  {
361  // must be yesterday
362  if ($until->d === 0 && ($until->m === 0))
363  {
364  $this->dateString = "gestern";
365  }
366  else
367  { // x days ago
368  // less than two months
369  if ($until->m < 2)
370  { // singular
371  $duration = "Monat";
372  }
373  else
374  { // plural
375  $duration = "Monaten";
376  }
377  if ($until->m >= 1)
378  {
379  $this->dateString = "$prepend $until->m $duration";
380  }
381  else
382  {
383  $this->dateString = "$prepend $until->d Tagen";
384  }
385  }
386  }
387  }
388 
389  if (!empty($this->apiObject['data'][$i]['message']))
390  {
391  $this->message = $this->apiObject['data'][$i]['message'];
392  }
393  else
394  {
395  $this->message = '';
396  }
397 
398  if (!empty($this->apiObject['data'][$i]['full_picture']))
399  {
400  $this->picture = '<img src="'.$this->apiObject['data'][$i]['full_picture'].'" class="card-img-top protected">';
401  }
402  else
403  {
404  $this->picture = '';
405  }
406 
407  if (isset($this->apiObject['data'][$i]['place']['name']))
408  {
409  $this->place = "@ ".$this->apiObject['data'][$i]['place']['name'];
410  }
411  else
412  {
413  $this->place = '';
414  }
415 
416  /*
417  if ($loopCount < 3)
418  {
419  $animation = "animated fadeIn";
420  }
421  else
422  {
423  $animation = "animate";
424  }
425  */
426 
427  echo '<div class="col-lg-4 animate" data-fx="fadeIn">';
428  echo '<div class="card">
429  <div class="card-header"><h5 class="card-title">'.$this->apiObject['data'][$i]['from']['name'].'
430  <small class="text-muted">'.$this->place.'</small></h5>
431  <h5 class="card-subtitle mb-2 text-muted"><small>'.$this->dateString.'</small></h5>
432  </div>
433  '.$this->picture.'
434  <div class="card-body">
435  <p class="card-text">'.$this->message.'</p>
436  </div>
437  </div>';
438 
439 
440  echo '<br><br></div>';
441  $i--;
442  $loopCount++;
443  }
444  echo'</div>';
445 
446  }
447 
448  } // end class events
449 } // end namespace
$now
die
Definition: block-user.php:27
Facebook Page Feed.
Definition: fbFeed.php:15
exit
if(isset($_POST['save'])) $settings
$value
$i