Permalink
Browse files

Use kdyby/facebook

  • Loading branch information...
fprochazka committed Jun 16, 2014
1 parent 743a911 commit 5dd7bed8fb30eb22284ee2e0fc92a726db0913fd
View
@@ -33,9 +33,20 @@ nette:
options: {lazy: yes}
extensions:
facebook: Kdyby\Facebook\DI\FacebookExtension
facebook:
# getting it from parameters is actually not neccesary
# you can put the variables directly in this section instead of the parameters
# in your config.local.neon
appId: 123
appSecret: "The cake is a lie"
services:
- App\Model\UserManager
- App\RouterFactory
router: @App\RouterFactory::createRouter
- App\Model\FacebookWallposts
- App\Model\FacebookSessionManager(%facebook.appId%, %facebook.appSecret%)

This file was deleted.

Oops, something went wrong.
@@ -6,9 +6,15 @@
namespace App\Model;
use Kdyby\Facebook\Facebook;
use Kdyby\Facebook\FacebookApiException;
use Nette\Database\Context;
use Nette\Object;
use Facebook\FacebookRequest;
use Nette\Utils\ArrayHash;
use Nette\Utils\DateTime;
use Tracy\Debugger;
class FacebookWallposts extends Object
{
@@ -19,18 +25,16 @@ class FacebookWallposts extends Object
protected $database;
/**
* @var \App\Model\FacebookSessionManager
* @var Facebook
*/
protected $facebookSessionManager;
protected $facebook;
/**
* @param Context $database
* @param FacebookSessionManager $facebook
*/
function __construct(Context $database, FacebookSessionManager $facebook)
public function __construct(Context $database, Facebook $facebook)
{
$this->database = $database;
$this->facebookSessionManager = $facebook;
$this->facebook = $facebook;
}
public function getLastPosts($count = 5)
@@ -44,68 +48,51 @@ public function getLastPosts($count = 5)
public function importPostFromFacebook()
{
$session = $this->facebookSessionManager->getAppSession();
try {
$request = new FacebookRequest($session, 'GET', '/nettefw/feed');
$response = $request->execute();
$posts = $response->getGraphObject()->asArray();
$data = $posts['data'];
} catch (\Exception $ex) {
throw $ex;
$this->terminate();
// when you're loading a lot of posts, it's better to use ->iterate() instead of several ->get()'s
$posts = $this->facebook->iterate('/nettefw/feed');
} catch (FacebookApiException $ex) {
Debugger::log($ex->getMessage(), 'facebook');
return array();
}
// save data to database
if (is_array($data) && !empty($data)) {
foreach ($data as $rowPost) {
$post = array(
'id' => $rowPost->id,
'type' => $rowPost->type,
'created_time' => $rowPost->created_time,
'updated_time' => $rowPost->updated_time,
);
if (isset($rowPost->name)) {
$post['name'] = $rowPost->name;
}
if (isset($rowPost->link)) {
$post['link'] = $rowPost->link;
}
if (isset($rowPost->status_type)) {
$post['status_type'] = $rowPost->status_type;
}
if (isset($rowPost->message)) {
$post['message'] = $rowPost->message;
}
if (isset($rowPost->caption)) {
$post['caption'] = $rowPost->caption;
}
if (isset($rowPost->picture)) {
$post['picture'] = $rowPost->picture;
}
$imported = array();
// post 'status' use story, we need message
if ($rowPost->type == 'status') {
if (isset($rowPost->story)) {
$post['message'] = $rowPost->story;
}
}
// save data to database
foreach ($posts as $rowPost) {
$post = array(
'id' => $rowPost->id,
'type' => $rowPost->type,
'created_time' => DateTime::from($rowPost->created_time)->format('Y-m-d H:i:s'),
'updated_time' => DateTime::from($rowPost->updated_time)->format('Y-m-d H:i:s'),
'name' => isset($rowPost->name) ? $rowPost->name : NULL,
'link' => isset($rowPost->link) ? $rowPost->link : NULL,
'status_type' => isset($rowPost->status_type) ? $rowPost->status_type : NULL,
'message' => isset($rowPost->message) ? $rowPost->message : NULL,
'caption' => isset($rowPost->caption) ? $rowPost->caption : NULL,
'picture' => isset($rowPost->picture) ? $rowPost->picture : NULL,
);
// post 'status' use story, we need message
if ($rowPost->type == 'status' && isset($rowPost->story)) {
$post['message'] = $rowPost->story;
}
try {
$this->database->table('facebook_wallposts')->insert($post);
} catch (\PDOException $e) {
if ($e->getCode() == '23000') {
$this->database->table('facebook_wallposts')->where('id', $rowPost->id)->update($post);
} else {
throw $e;
}
try {
$this->database->table('facebook_wallposts')->insert($post);
} catch (\PDOException $e) {
if ($e->getCode() == '23000') {
$this->database->table('facebook_wallposts')->where('id', $rowPost->id)->update($post);
} else {
throw $e;
}
}
$imported[$post['id']] = ArrayHash::from($rowPost);
}
return $data;
return $imported;
}
}
}
@@ -22,4 +22,25 @@ public function renderDefault()
$this->template->wallPosts = $this->wallposts->getLastPosts();
}
protected function createTemplate()
{
/** @var Nette\Bridges\ApplicationLatte\Template $template */
$template = parent::createTemplate();
$template->addFilter('fbPostLink', function ($fbPost) {
if (!empty($fbPost->link)) {
return $fbPost->link;
}
if ($m = Nette\Utils\Strings::match($fbPost->id, '~^(?P<pageId>[^_]+)_(?P<postId>[^_]+)\\z~')) {
return 'https://www.facebook.com/nettefw/posts/' . urlencode($m['postId']);
}
return NULL;
});
return $template;
}
}
@@ -7,17 +7,10 @@
<h3 n:if="$post->name">{$post->name}</h3>
<img n:if="$post->picture" src="{$post->picture}" />
{if $post->message}
<p>
{$post->message|truncate:250:'...'}
<a n:if="$post->link" href="{$post->link}">více</a>
</p>
{else}
{if $post->link}
<a href="{$post->link}">více</a>
{else}
<a href="http://www.facebook.com/nettefw">více</a>
{/if}
{/if}
<p>
{if $post->message}{$post->message|truncate:250:'...'}{/if}
<a href="{$post|fbPostLink}">více</a>
</p>
</div>
</div>
</div>
View
@@ -23,7 +23,7 @@
"php": ">= 5.3.7",
"nette/nette": "~2.2.0",
"dg/adminer-custom": "~1.0",
"facebook/php-sdk-v4" : "4.0.*"
"kdyby/facebook" : "dev-master"
},
"require-dev": {
"nette/tester": "~1.0"
Oops, something went wrong.

0 comments on commit 5dd7bed

Please sign in to comment.