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 newAction(Request $request)
 {
     $locationType = $this->get('campaignchain.core.form.type.location');
     $locationType->setBundleName('campaignchain/location-ezplatform');
     $locationType->setModuleIdentifier('campaignchain-ezplatform-user');
     $form = $this->createFormBuilder()->add('location_name', 'text', array('label' => 'Location name', 'attr' => array('placeholder' => 'For example, "Our corporate website"')))->add('url', 'url', array('label' => 'REST API base URL', 'attr' => array('placeholder' => 'For example, "http://www.example.com/api/ezp/v2/"')))->add('username', 'text', array('attr' => array('placeholder' => 'An eZ Platform user with access to the REST API')))->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();
             $locationName = $form->getData()['location_name'];
             $url = $form->getData()['url'];
             $username = $form->getData()['username'];
             $password = $form->getData()['password'];
             /*
              * TODO: Check whether URL exists
              * TODO: Check that credentials are correct
              * TODO: Check whether provided URL points to a valid eZ REST API
              */
             $client = $this->container->get('campaignchain.channel.ezplatform.rest.client');
             $connection = $client->connect($url, $username, $password);
             $locationService = $this->get('campaignchain.core.location');
             $locationModule = $locationService->getLocationModule('campaignchain/location-ezplatform', 'campaignchain-ezplatform-user');
             $location = new Location();
             $location->setLocationModule($locationModule);
             $location->setName($locationName);
             $location->setUrl($url);
             $wizard = $this->get('campaignchain.core.channel.wizard');
             $wizard->setName($location->getName());
             $wizard->addLocation($location->getUrl(), $location);
             $channel = $wizard->persist();
             $wizard->end();
             $user = new EZPlatformUser();
             $user->setLocation($channel->getLocations()[0]);
             $user->setUsername($username);
             $user->setPassword($password);
             $em->persist($user);
             $em->flush();
             $em->getConnection()->commit();
             $this->addFlash('success', 'The eZ Platform <a href="' . $url . '">' . $locationName . '</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 eZ Platform Instance', 'form' => $form->createView()));
 }
 public function handleCompanyPageCreation(Location $location)
 {
     $this->channelWizard->setName($location->getName());
     $pagesData = $this->channelWizard->get('pagesData');
     $pageData = $pagesData[$location->getIdentifier()];
     $companyLink = 'https://www.linkedin.com/company/' . $pageData['id'];
     $location->setUrl($companyLink);
     $page = new Page();
     $page->setLocation($location);
     $page->setIdentifier($pageData['id']);
     $page->setDisplayName($pageData['name']);
     $page->setDescription($pageData['description']);
     $page->setLink($companyLink);
     $page->setWebsiteLink($pageData['websiteUrl']);
     // Remember the user object in the Wizard.
     $this->channelWizard->set($page->getIdentifier(), $page);
     $flashBagMsg = $this->channelWizard->get('flashBagMsg');
     $flashBagMsg .= '<li>Page: <a href="' . $page->getLink() . '">' . $page->getDisplayName() . '</a></li>';
     $this->channelWizard->set('flashBagMsg', $flashBagMsg);
 }
 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()));
 }