YaWK  24.1
Yet another WebKit
YAWK\WIDGETS\FACEBOOK\FEED\fbFeed Class Reference

Facebook Page Feed. More...

Public Member Functions

 __construct ($db)
 
 basicOutput ()
 
 checkAccessToken ()
 
 checkApiObjectData ()
 
 checkAppId ()
 
 checkPageId ()
 
 checkRequirements ()
 
 makeApiCall ()
 
 printApiObject ()
 

Public Attributes

 $apiObject
 
 $dateString
 
 $eventDate
 
 $fbEventsDatewordCss
 
 $fbFeedAccessToken = ''
 
 $fbFeedAppId = ''
 
 $fbFeedFields = 'picture,message,place,created_time,full_picture,coordinates'
 
 $fbFeedGraphRequest = '/me/feed/'
 
 $fbFeedLimit = '1'
 
 $fbFeedPageId = ''
 
 $jsonLink
 
 $message
 
 $picture
 
 $place
 

Detailed Description

Facebook Page Feed.

Use Facebook Graph API to get feed data from a Facebook Page. Require App ID and Access Token.

This is just an empty example widget for development and demo purpose!

This Widget gets the latest (limit) posts of your facebook page feed.

Author
Daniel Retzl danie.nosp@m.lret.nosp@m.zl@gm.nosp@m.ail..nosp@m.com
Version
1.0.0

Definition at line 14 of file fbFeed.php.

Constructor & Destructor Documentation

◆ __construct()

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::__construct (   $db)

Definition at line 45 of file fbFeed.php.

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();
if(isset($_POST['save'])) $settings
$value

References $db, $settings, $value, and YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\checkRequirements().

Member Function Documentation

◆ basicOutput()

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::basicOutput ( )

Definition at line 202 of file fbFeed.php.

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 
$now
die
Definition: block-user.php:27
$i

References YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\$fbFeedLimit, $i, $now, die, and YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\makeApiCall().

◆ checkAccessToken()

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::checkAccessToken ( )

Definition at line 84 of file fbFeed.php.

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  }

References die.

Referenced by YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\checkRequirements().

◆ checkApiObjectData()

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::checkApiObjectData ( )

Definition at line 172 of file fbFeed.php.

174  {
175  if (isset($this->apiObject['data']) && (!empty($this->apiObject['data'])))
176  {
177  return true;
178  }
179  else
180  {
181  return false;
182  }

Referenced by YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\printApiObject().

◆ checkAppId()

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::checkAppId ( )

Definition at line 65 of file fbFeed.php.

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  }

References die.

Referenced by YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\checkRequirements().

◆ checkPageId()

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::checkPageId ( )

Definition at line 102 of file fbFeed.php.

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  }

Referenced by YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\checkRequirements().

◆ checkRequirements()

◆ makeApiCall()

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::makeApiCall ( )

Definition at line 121 of file fbFeed.php.

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  }

References YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\$apiObject, YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\$fbFeedPageId, and die.

Referenced by YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\basicOutput(), and YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\printApiObject().

◆ printApiObject()

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::printApiObject ( )

Definition at line 184 of file fbFeed.php.

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  }
exit

References YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\checkApiObjectData(), exit, and YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\makeApiCall().

Member Data Documentation

◆ $apiObject

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$apiObject
Parameters
objectapi result (as object)

Definition at line 29 of file fbFeed.php.

Referenced by YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\makeApiCall().

◆ $dateString

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$dateString
Parameters
stringposting date

Definition at line 35 of file fbFeed.php.

◆ $eventDate

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$eventDate
Parameters
stringdate of the posting

Definition at line 33 of file fbFeed.php.

◆ $fbEventsDatewordCss

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$fbEventsDatewordCss
Parameters
stringdate (time ago) helper

Definition at line 37 of file fbFeed.php.

◆ $fbFeedAccessToken

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$fbFeedAccessToken = ''
Parameters
stringyour access token (secret word from developers.facebook.com)

Definition at line 21 of file fbFeed.php.

◆ $fbFeedAppId

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$fbFeedAppId = ''
Parameters
stringyour app ID (from developers.facebook.com)

Definition at line 17 of file fbFeed.php.

◆ $fbFeedFields

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$fbFeedFields = 'picture,message,place,created_time,full_picture,coordinates'
Parameters
stringfields that should be selected from facebook graph

Definition at line 25 of file fbFeed.php.

◆ $fbFeedGraphRequest

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$fbFeedGraphRequest = '/me/feed/'
Parameters
stringyour graph request

Definition at line 23 of file fbFeed.php.

◆ $fbFeedLimit

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$fbFeedLimit = '1'
Parameters
stringshow events of this time range

Definition at line 27 of file fbFeed.php.

Referenced by YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\basicOutput().

◆ $fbFeedPageId

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$fbFeedPageId = ''
Parameters
stringyour page ID (http://facebook.com/{YOURPAGEID}

Definition at line 19 of file fbFeed.php.

Referenced by YAWK\WIDGETS\FACEBOOK\FEED\fbFeed\makeApiCall().

◆ $jsonLink

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$jsonLink
Parameters
stringthe json link

Definition at line 31 of file fbFeed.php.

◆ $message

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$message
Parameters
stringposting message

Definition at line 39 of file fbFeed.php.

◆ $picture

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$picture
Parameters
stringphoto src

Definition at line 41 of file fbFeed.php.

◆ $place

YAWK\WIDGETS\FACEBOOK\FEED\fbFeed::$place
Parameters
stringplace

Definition at line 43 of file fbFeed.php.


The documentation for this class was generated from the following file: