public function execute(Location $location)
 {
     // Set HTML title as Location name.
     $location->setName('Form on ' . ParserUtil::getHTMLTitle($location->getUrl()));
     // Set the Website page module as the Location module.
     $locationModule = $this->locationService->getLocationModule('campaignchain/location-website', 'campaignchain-website-form');
     $location->setLocationModule($locationModule);
     // Set the image.
     $location->setImage($this->assetsHelper->getUrl('bundles/campaignchainlocationwebsite/images/icons/256x256/form.png', null));
     return $location;
 }
 public function newAction(Request $request)
 {
     $locationType = $this->get('campaignchain.core.form.type.location');
     $locationType->setBundleName('campaignchain/location-website');
     $locationType->setModuleIdentifier('campaignchain-website');
     $form = $this->createFormBuilder()->add('URL', 'url', array('label' => 'Website URL', 'constraints' => array(new Url(array('checkDNS' => true)))))->getForm();
     $form->handleRequest($request);
     if ($form->isValid()) {
         $locationURL = $form->getData()['URL'];
         $locationName = ParserUtil::getHTMLTitle($locationURL, $locationURL);
         $locationService = $this->get('campaignchain.core.location');
         $locationModule = $locationService->getLocationModule('campaignchain/location-website', 'campaignchain-website');
         $location = new Location();
         $location->setLocationModule($locationModule);
         $location->setName($locationName);
         $location->setUrl($locationURL);
         // Get the Website's favicon as Channel image if possible.
         $favicon = ParserUtil::getFavicon($locationURL);
         if ($favicon) {
             $locationImage = $favicon;
         } else {
             //                $locationImage = $this->container->get('templating.helper.assets')
             //                    ->getUrl(
             //                        'bundles/campaignchainlocationwebsite/images/icons/256x256/website.png',
             //                        null
             //                    );
             $locationImage = null;
         }
         $location->setImage($locationImage);
         $wizard = $this->get('campaignchain.core.channel.wizard');
         $wizard->setName($location->getName());
         $repository = $this->getDoctrine()->getRepository('CampaignChainCoreBundle:Location');
         if (!$repository->findBy(array('url' => $location->getUrl()))) {
             $wizard->addLocation($location->getUrl(), $location);
             try {
                 $channel = $wizard->persist();
                 $wizard->end();
                 $this->get('session')->getFlashBag()->add('success', "The Website '" . $location->getUrl() . "' has been connected.");
                 return $this->redirect($this->generateUrl('campaignchain_core_location'));
             } catch (\Exception $e) {
                 $this->addFlash('warning', "An error occured during the creation of the website location");
                 $this->get('logger')->addError($e->getMessage());
             }
         } else {
             $this->addFlash('warning', "The website  '" . $location->getUrl() . "' already exists.");
         }
     }
     //return $this->redirect($this->generateUrl(
     //  'campaignchain_channel_website_page_new',
     //array('id' => $channel->getLocations()[0]->getId())));
     return $this->render('CampaignChainCoreBundle:Base:new.html.twig', array('page_title' => 'Connect Website', 'form' => $form->createView()));
 }
 public function loginAction(Request $request)
 {
     $oauth = $this->get('campaignchain.security.authentication.client.oauth.authentication');
     $status = $oauth->authenticate(self::RESOURCE_OWNER, $this->applicationInfo);
     $profile = $oauth->getProfile();
     if ($status) {
         try {
             $em = $this->getDoctrine()->getManager();
             $em->getConnection()->beginTransaction();
             $wizard = $this->get('campaignchain.core.channel.wizard');
             $wizard->setName($profile->displayName);
             // Get the location module.
             $locationService = $this->get('campaignchain.core.location');
             $locationModule = $locationService->getLocationModule(self::LOCATION_BUNDLE, self::LOCATION_MODULE);
             $location = new Location();
             $location->setIdentifier($profile->identifier);
             $location->setName($profile->displayName);
             $location->setImage($profile->photoURL);
             $location->setLocationModule($locationModule);
             $wizard->addLocation($location->getIdentifier(), $location);
             $channel = $wizard->persist();
             $wizard->end();
             $oauth->setLocation($channel->getLocations()[0]);
             $user = new MailChimpUser();
             $user->setLocation($channel->getLocations()[0]);
             $user->setIdentifier($profile->identifier);
             $user->setUsername($profile->username);
             $user->setDisplayName($profile->displayName);
             $user->setFirstName($profile->firstName);
             $user->setLastName($profile->lastName);
             $user->setEmail($profile->email);
             $user->setProfileImageUrl($profile->photoURL);
             $user->setAddress($profile->address);
             $user->setCity($profile->city);
             $user->setCountry($profile->country);
             $user->setZip($profile->zip);
             $user->setPhone($profile->phone);
             $user->setWebsiteUrl($profile->webSiteURL);
             $em->persist($user);
             $em->flush();
             $em->getConnection()->commit();
             $this->get('session')->getFlashBag()->add('success', 'The MailChimp location <a href="#">' . $profile->displayName . '</a> was connected successfully.');
         } catch (\Exception $e) {
             $em->getConnection()->rollback();
             throw $e;
         }
     } else {
         $this->get('session')->getFlashBag()->add('warning', 'A location has already been connected for this MailChimp account.');
     }
     return $this->render('CampaignChainChannelMailChimpBundle:Create:login.html.twig', array('redirect' => $this->generateUrl('campaignchain_core_location')));
 }
 public function loginAction()
 {
     $oauth = $this->get('campaignchain.security.authentication.client.oauth.authentication');
     $status = $oauth->authenticate(self::RESOURCE_OWNER, $this->applicationInfo);
     $profile = $oauth->getProfile();
     if ($status) {
         try {
             $em = $this->getDoctrine()->getManager();
             $em->getConnection()->beginTransaction();
             $wizard = $this->get('campaignchain.core.channel.wizard');
             $wizard->setName($profile->displayName);
             // Get the location module.
             $locationService = $this->get('campaignchain.core.location');
             $locationModule = $locationService->getLocationModule('campaignchain/location-twitter', 'campaignchain-twitter-user');
             $location = new Location();
             $location->setIdentifier($profile->identifier);
             $location->setName($profile->displayName);
             $location->setLocationModule($locationModule);
             $location->setImage($profile->photoURL);
             $location->setUrl($profile->profileURL);
             $wizard->addLocation($location->getIdentifier(), $location);
             $channel = $wizard->persist();
             $wizard->end();
             $oauth->setLocation($channel->getLocations()[0]);
             $twitterUser = new TwitterUser();
             $twitterUser->setLocation($channel->getLocations()[0]);
             $twitterUser->setIdentifier($profile->identifier);
             $twitterUser->setDisplayName($profile->firstName);
             $twitterUser->setUsername($profile->displayName);
             $twitterUser->setProfileImageUrl($profile->photoURL);
             $twitterUser->setProfileUrl($profile->profileURL);
             $em->persist($twitterUser);
             // schedule job to get metrics from now on
             if ($channel->getLocations()[0]->getLocationModule()->getIdentifier() === 'campaignchain-twitter-user') {
                 $this->get('campaignchain.job.report.location.twitter')->schedule($channel->getLocations()[0]);
             }
             $em->flush();
             $em->getConnection()->commit();
             $this->addFlash('success', 'The Twitter location <a href="#">' . $profile->displayName . '</a> was connected successfully.');
         } catch (\Exception $e) {
             $em->getConnection()->rollback();
             throw $e;
         }
     } else {
         // A channel already exists that has been connected with this Facebook account
         $this->addFlash('warning', 'A location has already been connected for this Twitter account.');
     }
     return $this->render('CampaignChainChannelTwitterBundle:Create:login.html.twig', ['redirect' => $this->generateUrl('campaignchain_core_location')]);
 }
 /**
  * @return Location[]
  */
 public function getParsedLocationsFromLinkedIn()
 {
     $channel = $this->channelWizard->getChannel();
     $profile = $this->channelWizard->get('profile');
     $locations = [];
     $locationName = $profile->displayName;
     if (!empty($profile->username)) {
         $locationName .= ' (' . $profile->username . ')';
     }
     // Get the location module for the user stream.
     $locationModuleUser = $this->locationService->getLocationModule('campaignchain/location-linkedin', 'campaignchain-linkedin-user');
     // Create the location instance for the user stream.
     $locationUser = new Location();
     $locationUser->setIdentifier($profile->identifier);
     $locationUser->setName($locationName);
     $locationUser->setLocationModule($locationModuleUser);
     if (!$profile->photoURL || strlen($profile->photoURL) == 0) {
         $locationUser->setImage($this->assetsHelper->getUrl('/bundles/campaignchainchannellinkedin/ghost_person.png'));
     } else {
         $locationUser->setImage($profile->photoURL);
     }
     $locationUser->setChannel($channel);
     $locationModuleUser->addLocation($locationUser);
     $locations[$profile->identifier] = $locationUser;
     $tokens = $this->channelWizard->get('tokens');
     /** @var Token $userToken */
     $userToken = array_values($tokens)[0];
     $connection = $this->client->getConnectionByToken($userToken);
     $companies = $connection->getCompanies();
     //there is only a user page
     if (empty($companies)) {
         return $locations;
     }
     // Get the location module for the page stream.
     $locationModulePage = $this->locationService->getLocationModule('campaignchain/location-linkedin', 'campaignchain-linkedin-page');
     $wizardPages = [];
     foreach ($companies as $company) {
         $newToken = new Token();
         $newToken->setAccessToken($userToken->getAccessToken());
         $newToken->setApplication($userToken->getApplication());
         $newToken->setTokenSecret($userToken->getTokenSecret());
         $tokens[$company['id']] = $newToken;
         $this->channelWizard->set('tokens', $tokens);
         $companyData = $connection->getCompanyProfile($company['id']);
         $locationPage = new Location();
         $locationPage->setChannel($channel);
         $locationPage->setName($companyData['name']);
         $locationPage->setIdentifier($companyData['id']);
         if (isset($companyData['squareLogoUrl'])) {
             $locationPage->setImage($companyData['squareLogoUrl']);
         } else {
             $locationPage->setImage($this->assetsHelper->getUrl('/bundles/campaignchainchannellinkedin/ghost_person.png'));
         }
         $locationPage->setLocationModule($locationModulePage);
         $locationModulePage->addLocation($locationPage);
         $locations[$companyData['id']] = $locationPage;
         $wizardPages[$companyData['id']] = $companyData;
     }
     $this->channelWizard->set('pagesData', $wizardPages);
     return $locations;
 }
 public function newAction(Request $request)
 {
     $locationType = $this->get('campaignchain.core.form.type.location');
     $locationType->setBundleName('campaignchain/location-slideshare');
     $locationType->setModuleIdentifier('campaignchain-slideshare');
     $form = $this->createFormBuilder()->add('username', 'text')->add('password', 'repeated', array('required' => false, 'type' => 'password', 'first_name' => 'password', 'second_name' => 'password_again', 'invalid_message' => 'The password fields must match.'))->getForm();
     $form->handleRequest($request);
     try {
         if ($form->isValid()) {
             $em = $this->getDoctrine()->getManager();
             $em->getConnection()->beginTransaction();
             $locationUsername = $form->getData()['username'];
             $locationPassword = $form->getData()['password'];
             /*
             $oauthApp = $this->get('campaignchain.security.authentication.client.oauth.application');
             $application = $oauthApp->getApplication(self::RESOURCE_OWNER);
             $ts = time();
             $client = new \Guzzle\Http\Client;
             $request = $client->createRequest('GET', 'https://www.slideshare.net/api/2/get_user_tags');
             $query = $request->getQuery();
             $query->set('api_key', $application->getKey());
             $query->set('ts',  $ts);
             $query->set('hash', sha1($application->getSecret().$ts));
             $query->set('username', $locationUsername);
             $query->set('password', $locationPassword);
             $response = $request->send();
             
             $xml = $response->xml();
             */
             $oauthApp = $this->get('campaignchain.security.authentication.client.oauth.application');
             $application = $oauthApp->getApplication(self::RESOURCE_OWNER);
             $client = $this->container->get('campaignchain.channel.slideshare.rest.client');
             $connection = $client->connect($application->getKey(), $application->getSecret(), $locationUsername, $locationPassword);
             $xml = $connection->getUserTags();
             if (isset($xml->Message) && strtolower($xml->Message) == 'failed user authentication') {
                 throw new \Exception('The credentials provided are invalid');
             }
             $locationURL = 'http://www.slideshare.net/' . $locationUsername;
             $locationService = $this->get('campaignchain.core.location');
             $locationModule = $locationService->getLocationModule('campaignchain/location-slideshare', 'campaignchain-slideshare-user');
             $location = new Location();
             $location->setLocationModule($locationModule);
             $location->setName($locationUsername);
             $location->setUrl($locationURL);
             /*
              * If user uploaded an image, use that as the Location image,
              * otherwise, take the SlideShare default profile image.
              */
             $slideShareUserImage = 'http://cdn.slidesharecdn.com/profile-photo-' . $locationUsername . '-96x96.jpg';
             try {
                 getimagesize($slideShareUserImage);
             } catch (\Exception $e) {
                 $slideShareUserImage = 'http://public.slidesharecdn.com/b/images/user-96x96.png';
             }
             $location->setImage($slideShareUserImage);
             $wizard = $this->get('campaignchain.core.channel.wizard');
             $wizard->setName($location->getName());
             $wizard->addLocation($location->getUrl(), $location);
             $channel = $wizard->persist();
             $wizard->end();
             $slideshareUser = new SlideShareUser();
             $slideshareUser->setLocation($channel->getLocations()[0]);
             $slideshareUser->setIdentifier($locationUsername);
             $slideshareUser->setPassword($locationPassword);
             $slideshareUser->setDisplayName($locationUsername);
             $em->persist($slideshareUser);
             $em->flush();
             $em->getConnection()->commit();
             $this->get('session')->getFlashBag()->add('success', 'The Slideshare location <a href="#">' . $locationUsername . '</a> was connected successfully.');
             return $this->redirect($this->generateUrl('campaignchain_core_location'));
         }
     } catch (\Exception $e) {
         $em->getConnection()->rollback();
         throw $e;
     }
     return $this->render('CampaignChainCoreBundle:Base:new.html.twig', array('page_title' => 'Connect SlideShare Account', 'form' => $form->createView()));
 }
 public function addLocationAction(Request $request)
 {
     $wizard = $this->get('campaignchain.core.channel.wizard');
     $channel = $wizard->getChannel();
     $profile = $wizard->get('profile');
     $locations = [];
     $locationName = $profile->displayName;
     $username = $profile->username;
     if (!empty($username)) {
         $locationName .= ' (' . $username . ')';
     }
     // Get the location module for the user stream.
     $locationService = $this->get('campaignchain.core.location');
     $locationModuleUser = $locationService->getLocationModule('campaignchain/location-facebook', 'campaignchain-facebook-user');
     // Create the location instance for the user stream.
     $locationUser = new Location();
     $locationUser->setChannel($channel);
     $locationUser->setName($locationName);
     $locationUser->setIdentifier($profile->identifier);
     $locationUser->setImage($profile->photoURL);
     $locationUser->setLocationModule($locationModuleUser);
     $locationModuleUser->addLocation($locationUser);
     $locations[$profile->identifier] = $locationUser;
     // Connect to Facebook to retrieve pages related to the user.
     $tokens = $wizard->get('tokens');
     $client = $this->container->get('campaignchain.channel.facebook.rest.client');
     $connection = $client->connect($tokens[$profile->identifier]->getAccessToken());
     if ($connection) {
         // TODO: Check whether user has manage_pages permission with /me/permissions
         // check if the user owns Facebook pages
         $response = $connection->api('/me/accounts');
         $pagesData = $response['data'];
         if (is_array($pagesData) && count($pagesData)) {
             // TODO: Should we check whether the Facebook page has actually been published (through is_published), because if not, then posting to it won't make sense? Same with can_post and perms from /me/accounts?
             // Get the location module for the page stream.
             $locationModulePage = $locationService->getLocationModule('campaignchain/location-facebook', 'campaignchain-facebook-page');
             // User owns pages, so let's build the form and ask him whether to create channels for each of them
             // with the respective channel name
             foreach ($pagesData as $pageData) {
                 // Save the token in the Wizard.
                 $tokens = $wizard->get('tokens');
                 $newToken = new Token();
                 $newToken->setAccessToken($pageData['access_token']);
                 $application = $tokens[$wizard->get('facebook_user_id')]->getApplication();
                 $newToken->setApplication($application);
                 $tokens[$pageData['id']] = $newToken;
                 $wizard->set('tokens', $tokens);
                 // Get the page picture
                 $pageConnection = $client->connect($pageData['access_token']);
                 $pagePicture = $pageConnection->api('/' . $pageData['id'] . '/picture', 'GET', ['redirect' => false, 'type' => 'large']);
                 $pageData['picture_url'] = $pagePicture['data']['url'];
                 // Create the location instance for the page stream.
                 $locationPage = new Location();
                 $locationPage->setChannel($channel);
                 $locationPage->setName($pageData['name']);
                 $locationPage->setIdentifier($pageData['id']);
                 $locationPage->setImage($pageData['picture_url']);
                 $locationPage->setLocationModule($locationModulePage);
                 $locationModulePage->addLocation($locationPage);
                 $locations[$pageData['id']] = $locationPage;
                 $wizardPages[$pageData['id']] = $pageData;
             }
             $wizard->set('pagesData', $wizardPages);
         }
     }
     $data = [];
     $form = $this->createFormBuilder($data);
     foreach ($locations as $identifier => $location) {
         // Has the page already been added as a location?
         $repository = $this->getDoctrine()->getRepository('CampaignChainCoreBundle:Location');
         $pageExists = $repository->findOneBy(['identifier' => $identifier, 'locationModule' => $location->getLocationModule()]);
         // Compose the checkbox form field.
         $form->add($identifier, CheckboxType::class, ['label' => '<img class="campaignchain-location-image-input-prepend" src="' . $location->getImage() . '"> ' . $location->getName(), 'required' => false, 'data' => true, 'mapped' => false, 'disabled' => $pageExists, 'attr' => ['align_with_widget' => true]]);
         // If a location has already been added before, remove it from this process.
         // TODO: Also assign existing locations to the new FB user.
         if ($pageExists) {
             unset($locations[$identifier]);
         }
     }
     $form = $form->getForm();
     $form->handleRequest($request);
     if ($form->isValid()) {
         // Find out which locations should be added, i.e. which respective checkbox is active.
         foreach ($locations as $identifier => $location) {
             if (!$form->get($identifier)->getData()) {
                 unset($locations[$identifier]);
                 $wizard->removeLocation($identifier);
             }
         }
         // If there's at least one location to be added, then have the user configure it.
         if (is_array($locations) && count($locations)) {
             $wizard->setLocations($locations);
             return $this->redirectToRoute('campaignchain_channel_facebook_location_configure', ['step' => 0]);
         } else {
             $this->addFlash('warning', 'No new location has been added.');
             return $this->redirectToRoute('campaignchain_core_location');
         }
     }
     return $this->render('CampaignChainCoreBundle:Base:new.html.twig', ['page_title' => 'Add Facebook Locations', 'form' => $form->createView()]);
 }