Skip to content

Instantly share code, notes, and snippets.

@johntron
Created November 4, 2011 22:46
Show Gist options
  • Save johntron/1340690 to your computer and use it in GitHub Desktop.
Save johntron/1340690 to your computer and use it in GitHub Desktop.
<?php
/**
* Splash Media
*
* LICENSE
*
* This file is intellectual property of Splash Media and may not
* be used without permission.
*
* @category Splash Media
* @copyright Copyright (c) 2011 Splash Media, LP. (http://www.splashmedia.com/)
*/
namespace SplashMedia\Bundle\OxygenBundle\Controller;
use FOS\FacebookBundle\Facebook\FacebookSessionPersistence;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use SplashMedia\Bundle\OxygenBundle\Event\UserLinkedAccountDeactivated;
use SplashMedia\Bundle\OxygenBundle\StoreEvents;
use Symfony\Bundle\FrameworkBundle\Controller\Controller,
Symfony\Component\HttpKernel\Exception,
SplashMedia\Bundle\OxygenBundle\Entity,
Doctrine\Common\Collections\ArrayCollection;
/**
* Account Controller
*
* @category Splash Media
* @copyright Copyright (c) 2011 Splash Media, LP. (http://www.splashmedia.com/)
*
* @Route("/account")
*/
class AccountController extends Controller
{
/**
* @Route("/", name="_account")
* @Template()
*/
public function indexAction()
{
/* @var $user Entity\User */
$user = $this->get('security.context')->getToken()->getUser();
/* @var $em \Doctrine\ORM\EntityManager */
$em = $this->get('doctrine.orm.entity_manager');
$repo = $em->getRepository('SplashMedia\Bundle\OxygenBundle\Entity\LinkedAccount');
$accounts = $repo->findBy(array('user' => $user->getId(), 'isActive' => true));
return array('user' => $user, 'accounts' => $accounts);
}
/**
* @Route("/facebook/page/{id}", name="account_facebook_selectpage")
* @Template()
* @var $id ID of LinkedAccount
*/
public function facebookPageAction($id)
{
$user = $this->get('security.context')->getToken()->getUser();
$em = $this->get('doctrine.orm.entity_manager');
/* @var $account SplashMedia\Bundle\OxygenBundle\Entity\LinkedAccount\FacebookAccount */
$account = $em->getRepository('SplashMedia\Bundle\OxygenBundle\Entity\LinkedAccount\FacebookAccount')->findOneBy(array('id' => $id, 'user' => $user->getId()));
if ($account === null) {
throw new Exception\NotFoundHttpException();
}
$facebook = $this->get('fos_facebook.api');
/* @var $facebook \Facebook */
$facebook->setAccessToken( $account->getAccessToken() );
$pagesArray = $facebook->api( '/me/accounts' );
$pagesArray = $pagesArray['data'];
if ( count( $pagesArray ) > 0 ) {
$pages = array();
$form = array();
foreach ($pagesArray as $pageAdmin) {
if ( $pageAdmin['access_token']) {
$page = $facebook->api('/' . $pageAdmin['id'], 'GET');
$pages[$pageAdmin['id']] = $page;
}
}
$pageObj = (object)array('page' => null);
$form = $this->get('form.factory')
->createBuilder('form', $pageObj)
->add('page', 'choice', array(
'choices' => $pages,
))
->getForm();
return array(
'account' => $account,
'pages' => $pages,
'form' => $form->createView()
);
}
return array(
'account' => $account
);
}
private function clearFacebookSession() {
$facebook = $this->get('fos_facebook.api');
$app_id = $facebook->getAppId();
unset($_SESSION['fb_'.$app_id.'_code']);
unset($_SESSION['fb_'.$app_id.'_access_token']);
unset($_SESSION['fb_'.$app_id.'_user_id']);
}
private function saveFacebookCredentials( FacebookSessionPersistence $facebook, Entity\User $user ) {
$em = $this->get('doctrine.orm.entity_manager');
$facebookAccount = $user->getLinkedFacebookAccount();
if ( !$facebookAccount ) {
$me = $facebook->api('/me');
$facebookAccount = new Entity\LinkedAccount\FacebookAccount();
$facebookAccount->setIsActive(false);
$facebookAccount->setUser($user);
$facebookAccount->setName(isset($me['name']) ? $me['name'] : $facebook->getUser() );
$facebookAccount->setSecret('');
$facebookAccount->setUID( $facebook->getUser() );
}
$facebookAccount->setAccessToken( $facebook->getAccessToken() );
$em->persist($facebookAccount);
$em->flush();
return $facebookAccount;
}
/**
* @Route("/link/{network}", name="account_link")
* @Template
*/
public function linkAction($network) {
if ( $this->getRequest()->get( 'id' ) ) {
$linkedAccount = $this->getDoctrine()->getRepository('SplashMediaOxygenBundle:LinkedAccount\FacebookAccount')->find($this->getRequest()->get( 'id' ) );
} else {
$linkedAccount = Entity\LinkedAccount::factoryGetAccountByNetwork($network);
}
$linkedAccount->setContainer( $this->container );
$user = $this->get('security.context')->getToken()->getUser();
if ( $linkedAccount->isCallback( $this->getRequest() ) ) {
$linkedAccount->handleCallback( $this->getRequest() );
// Have to load from the DB, because Doctrine thinks $consumer is a new entity
$em = $this->getDoctrine()->getEntityManager();
$linkedAccount->setUser( $user );
$em->persist( $linkedAccount );
$em->flush();
if ( 'facebook' == $network && !$linkedAccount->getFacebookPageId() ) {
return $this->redirect( $this->generateUrl( 'account_facebook_selectpage', array( 'id' => $linkedAccount->getId() ), true ) );
}
switch( $linkedAccount->getChannelName() ) {
case 'facebook':
$facebookProfileManager = $this->get('core.oxygen.facebook_profile.manager');
$profile = $facebookProfileManager->getOrCreateFacebookProfile(
$linkedAccount, $linkedAccount->getFacebookPageId() );
$profile->setLinkedAccount($linkedAccount);
$em->persist($profile);
break;
case 'twitter':
$twitterProfileManager = $this->get('core.oxygen.twitter_profile.manager');
$profile = $twitterProfileManager->getOrCreateTwitterProfile($linkedAccount);
$profile->setLinkedAccount($linkedAccount);
$em->persist($profile);
break;
case 'linkedin':
$linkedInProfileManager = $this->get('core.oxygen.linkedin_profile.manager');
$profile = $linkedInProfileManager->getOrCreateLinkedInProfile($linkedAccount);
$profile->setLinkedAccount($linkedAccount);
$em->persist($profile);
break;
}
$em->persist($linkedAccount);
if( $user->getLinkedAccounts()->count() == 1 ) {
// Show "now that you've linked an account" video
$this->setVideoFlash();
}
return $this->redirect( $this->generateUrl( '_account' ) );
} else {
$callbackUrl = $this->generateUrl('account_link', array( 'network' => $network ), true );
$redirectUrl = $linkedAccount->getAuthRedirectURL( $callbackUrl );
return $this->redirect( $redirectUrl );
}
}
private function setVideoFlash() {
$browserAsString = $this->get('request')->headers->get('user_agent');
if (strstr($browserAsString, " AppleWebKit/")
// && strstr($browserAsString, " Mobile/")
&& !strstr($browserAsString, " Chrome/")) { // chrome playback is atrocious.
$tryHTML5 = true;
} else {
$tryHTML5 = false;
}
$video = new Entity\CubeVideo();
$video->setDescription('Things to do after linking an account');
$video->setFilename('34_SCT_030.mp4');
$video->setThumbnail('34_SCT_030.png');
$video->setImage('34_SCT_030.png');
$video->setVideoWidth(896);
$video->setVideoHeight(504);
$videoHtml = $this->render('SplashMediaOxygenBundle:Account:postLinkingVideo.html.twig', array(
'video' => $video,
'tryHTML5' => $tryHTML5
))->getContent();
$this->get('session')->setFlash('video', $videoHtml);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment