/src/Aggregator/DealsBundle/Controller/FeedController.php
PHP | 439 lines | 281 code | 57 blank | 101 comment | 28 complexity | 490802cd421003f0a47872da0d2988d7 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause, LGPL-2.0, MIT, Apache-2.0, LGPL-2.1, LGPL-3.0
- <?php
- namespace Aggregator\DealsBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Aggregator\DealsBundle\Entity\Feed;
- use Aggregator\DealsBundle\Entity\link;
- use Aggregator\DealsBundle\Entity\Category;
- use Aggregator\DealsBundle\Form\FeedType;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\Security\Core\Exception\AccessDeniedException;
- use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
- use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
- use Symfony\Component\Security\Acl\Permission\MaskBuilder;
- /**
- * Feed controller.
- *
- */
- class FeedController extends Controller {
- /**
- * Lists all Feed entities.
- *
- */
- public function __construct($container = null) {
- $this->container = $container;
- // ... deal with any more arguments etc here
- }
- public function indexAction() {
- $em = $this->getDoctrine()->getEntityManager();
- $entities = $em->getRepository('AggregatorDealsBundle:Feed')->findAll();
- return $this->render('AggregatorDealsBundle:Feed:index.html.twig', array(
- 'entities' => $entities
- ));
- }
- /**
- * Finds and displays a Feed entity.
- *
- */
- public function showAction($id) {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('AggregatorDealsBundle:Feed')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find Feed entity.');
- }
- $deleteForm = $this->createDeleteForm($id);
- return $this->render('AggregatorDealsBundle:Feed:show.html.twig', array(
- 'entity' => $entity,
- 'delete_form' => $deleteForm->createView(),
- ));
- }
- /**
- * Displays a form to create a new Feed entity.
- *
- */
- public function newAction() {
- $entity = new Feed();
- $form = $this->createForm(new FeedType(), $entity);
- return $this->render('AggregatorDealsBundle:Feed:new.html.twig', array(
- 'entity' => $entity,
- 'form' => $form->createView()
- ));
- }
- /**
- * Creates a new Feed entity.
- *
- */
- public function createAction() {
- $entity = new Feed();
- $request = $this->getRequest();
- $form = $this->createForm(new FeedType(), $entity);
- $form->bindRequest($request);
- if ($form->isValid()) {
- $em = $this->getDoctrine()->getEntityManager();
- $em->persist($entity);
- $em->flush();
- return $this->redirect($this->generateUrl('feed_show', array('id' => $entity->getId())));
- }
- return $this->render('AggregatorDealsBundle:Feed:new.html.twig', array(
- 'entity' => $entity,
- 'form' => $form->createView()
- ));
- }
- /**
- * Displays a form to edit an existing Feed entity.
- *
- */
- public function editAction($id) {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('AggregatorDealsBundle:Feed')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find Feed entity.');
- }
- $editForm = $this->createForm(new FeedType(), $entity);
- $deleteForm = $this->createDeleteForm($id);
- return $this->render('AggregatorDealsBundle:Feed:edit.html.twig', array(
- 'entity' => $entity,
- 'edit_form' => $editForm->createView(),
- 'delete_form' => $deleteForm->createView(),
- ));
- }
- /**
- * Edits an existing Feed entity.
- *
- */
- public function updateAction($id) {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('AggregatorDealsBundle:Feed')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find Feed entity.');
- }
- $editForm = $this->createForm(new FeedType(), $entity);
- $deleteForm = $this->createDeleteForm($id);
- $request = $this->getRequest();
- $editForm->bindRequest($request);
- if ($editForm->isValid()) {
- $em->persist($entity);
- $em->flush();
- return $this->redirect($this->generateUrl('feed_edit', array('id' => $id)));
- }
- return $this->render('AggregatorDealsBundle:Feed:edit.html.twig', array(
- 'entity' => $entity,
- 'edit_form' => $editForm->createView(),
- 'delete_form' => $deleteForm->createView(),
- ));
- }
- /**
- * Deletes a Feed entity.
- *
- */
- public function deleteAction($id) {
- $form = $this->createDeleteForm($id);
- $request = $this->getRequest();
- $form->bindRequest($request);
- if ($form->isValid()) {
- $em = $this->getDoctrine()->getEntityManager();
- $entity = $em->getRepository('AggregatorDealsBundle:Feed')->find($id);
- if (!$entity) {
- throw $this->createNotFoundException('Unable to find Feed entity.');
- }
- $link = $em->getRepository('AggregatorDealsBundle:link')->findBy(array('feed' => $entity->getId()));
- foreach ($link as $ln) {
- $em->remove($ln);
- }
- $em->remove($entity);
- $em->flush();
- }
- return $this->redirect($this->generateUrl('feed'));
- }
- private function createDeleteForm($id) {
- return $this->createFormBuilder(array('id' => $id))
- ->add('id', 'hidden')
- ->getForm()
- ;
- }
- public function testAction() {
- $em = $this->getDoctrine()->getEntityManager();
- $em->createQuery("delete from AggregatorDealsBundle:link")->execute();
- $entity = $em->getRepository('AggregatorDealsBundle:Feed')->findAll();
- $ch = curl_init();
- foreach ($entity as $feed) {
- if ($feed->getLink() != null || $feed->getLink() != '') {
- // Set request url
- curl_setopt($ch, CURLOPT_URL, $feed->getLink());
- // TRUE to include the header in the output.
- curl_setopt($ch, CURLOPT_HEADER, false);
- // A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request.
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //curl_setopt($ch, CURLOPT_GET, $feed);
- $data = curl_exec($ch);
- //print_r($data);
- //die();
- $xml = new SimpleXMLElement($data);
- //print_r($xml);
- //die();
- foreach ($xml->channel->item as $items) {
- $link = new link();
- //$link->setImage(urldecode($xml->channel->image->url));
- $link->setLink(urldecode($items->link));
- $link->setTitle(addslashes(trim(urldecode($items->title))));
- $link->setDescription(trim(strip_tags(urldecode($items->description))));
- $image = $this->scrapeImage(urldecode($items->description));
- if (isset($image[1][2]) && $image[1][2] != null) {
- $link->setImage($image[1][2]);
- } else {
- $link->setImage($image[1][1]);
- }
- $deal = $items->children('http://www.dealfind.com/ns/1.0/deal#');
- $category = $this->getDoctrine()->getRepository('AggregatorDealsBundle:Category')->findBy(array('name' => $deal->category));
- //print_r($category);
- if (count($category) == 0) {
- $category = new Category();
- $category->setName($deal->category);
- $em->persist($category);
- } else {
- $link->setCategory($category[0]);
- }
- $link->setPrice($deal->price);
- $link->setAvailable($deal->voucherlimit);
- $link->setStartdate($deal->start);
- $link->setEnddate($deal->end);
- $link->setRegularprice($deal->regularPrice);
- $link->setFeed($feed);
- $em = $this->getDoctrine()->getEntityManager();
- $em->persist($link);
- $em->flush();
- }
- }
- //print_r($allItems);
- }
- /*
- $objectIdentity = new ObjectIdentity('class', 'Aggregator\\DealsBundle\\Entity\\link');
- //print('<xmp>');
- //print_r($objectIdentity);
- $aclProvider = $this->get('security.acl.provider');
- //print_r($aclProvider);
- $aclProvider->deleteAcl($objectIdentity);
- $acl = $aclProvider->createAcl($objectIdentity);
- //print_r($acl);
- // retrieving the security identity of the currently logged-in user
- //print('<xmp>');
- $securityContext = $this->get('security.context');
- //print_r($securityContext);
- $user = $securityContext->getToken()->getUser();
- //print_r($user);
- $securityIdentity = UserSecurityIdentity::fromAccount($user);
- //print_r($securityIdentity);
- //print('hi');
- //print('</xmp>');
- //error_log($securityIdentity);
- // grant owner access
- $builder = new MaskBuilder();
- $builder
- ->add('view')
- ->add('edit');
- // print_r($builder->get());
- $mask = $builder->get(); // int(29)
- $acl->insertObjectAce($securityIdentity, $mask);
- $aclProvider->updateAcl($acl);
- */
- $this->yipitAction();
- return new Response(
- 'Done'
- );
- }
- public function parseGrouponAction() {
- $url = "http://api.groupon.com/v2/deals?client_id=a663679ed3c8b01097634333fa743cf3a721f4fe";
- $ch = curl_init();
- // Set request url
- curl_setopt($ch, CURLOPT_URL, $url);
- // TRUE to include the header in the output.
- curl_setopt($ch, CURLOPT_HEADER, false);
- // A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request.
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //curl_setopt($ch, CURLOPT_GET, $feed);
- $data = curl_exec($ch);
- $data = json_decode($data);
- //print_r($data);
- foreach ($data->deals as $dat) {
- foreach ($dat as $test) {
- foreach ($test as $t) {
- echo($t->title);
- echo($t->price->formattedAmount);
- echo($t->value->formattedAmount);
- print_r($t->tags);
- print_r($t->locationNote);
- echo("<br>");
- }
- print_r($test->tags);
- print_r($test->locationNote);
- }
- print_r($dat->tags);
- print_r($dat->locationNote);
- }
- return $this->render('AggregatorDealsBundle:Feed:me.html.twig', array('rss' => $data));
- }
- public function yipitAction() {
- $em = $this->getDoctrine()->getEntityManager();
- $em->createQuery("delete from AggregatorDealsBundle:link")->execute();
- $city = $this->getDoctrine()->getRepository('AggregatorDealsBundle:City')->findAll();
- $allCities = array();
- foreach($city as $cit)
- {
- $allCities[]=$cit->getName();
- }
- //die();
- $places = urlencode(strtolower(implode(',',$allCities)));
- $url = "http://api.yipit.com/v1/deals/?key=shh5kNSS9GXkVYVM&division=".$places."&limit=5000";
- $ch = curl_init();
- // Set request url
- curl_setopt($ch, CURLOPT_URL, $url);
- // TRUE to include the header in the output.
- curl_setopt($ch, CURLOPT_HEADER, false);
- // A custom request method to use instead of "GET" or "HEAD" when doing a HTTP request.
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //curl_setopt($ch, CURLOPT_GET, $feed);
- $data = curl_exec($ch);
- $data = json_decode($data);
- //print('<xmp>');
- //print_r($data);
- //print('</xmp>');
- //die();
- foreach ($data->response->deals as $deal) {
- $link = new link();
- $link->setImage($deal->images->image_small);
- $link->setLink($deal->yipit_url);
- if(gettype($deal->title)==='string')
- {
- $link->setTitle($deal->title);
- }
- else
- {
- $link->setTitle('na');
- }
- $link->setDescription('');
- $category = $this->getDoctrine()->getRepository('AggregatorDealsBundle:Category')->findBy(array('name' => $deal->tags[0]->name));
- //print_r($category);
- if (count($category) == 0) {
- $category = new Category();
- $category->setName($deal->tags[0]->name);
- $em->persist($category);
- } else {
- $link->setCategory($category[0]);
- }
- $feed = $this->getDoctrine()->getRepository('AggregatorDealsBundle:Feed')->findBy(array('name' => $deal->source->name));
- //print_r($category);
- if (count($feed) == 0) {
- $feed = new Feed();
- $feed->setName($deal->source->name);
- $em->persist($feed);
- } else {
- $link->setFeed($feed[0]);
- }
- $city = $this->getDoctrine()->getRepository('AggregatorDealsBundle:City')->findBy(array('name' => $deal->division->name));
- if (count($city) == 0) {
- $city = new City();
- $city->setName($deal->division->name);
- $em->persist($city);
- } else {
- $link->setCity($city[0]);
- }
- $link->setPrice($deal->price->raw);
- $link->setAvailable(1);
- $link->setStartdate($deal->date_added);
- $link->setEnddate($deal->end_date);
- $link->setRegularprice($deal->value->raw);
- $link->setLatitude($deal->business->locations[0]->lat);
- $link->setLongitude($deal->business->locations[0]->lon);
- $em->persist($link);
- $em->flush();
- // echo($deal->title.' '.$deal->source->name);
- // echo("<img src='".$deal->images->image_small."'>");
- // echo("<b>".$deal->price->formatted."</b>");
- // echo("<b>".$deal->value->formatted."</b>");
- // echo("<b>".$deal->yipit_url."</b>");
- // echo("<b>".$deal->tags[0]->name."</b>");
- // echo("<br>");
- }
- return $this->render('AggregatorDealsBundle:Feed:yipit.html.twig', array('rss' => $data));
- }
- public function doctrineAction() {
- $kernel = $this->get('kernel');
- $application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
- $application->setAutoExit(false);
- //Create de Schema
- $options = array('command' => 'doctrine:schema:update', "--force" => true);
- $application->run(new \Symfony\Component\Console\Input\ArrayInput($options));
- }
- public function clearAction() {
- $kernel = $this->get('kernel');
- $application = new \Symfony\Bundle\FrameworkBundle\Console\Application($kernel);
- $application->setAutoExit(false);
- //Create de Schema
- $options = array('command' => 'cache:clear');
- $output = array();
- return new Response($application->run(new \Symfony\Component\Console\Input\ArrayInput($options), new \Symfony\Component\Console\Output\ConsoleOutput($output)));
- }
- private function scrapeImage($text) {
- $pattern = '/src=[\'"]?([^\'" >]+)[\'" >]/';
- preg_match_all($pattern, $text, $link);
- return $link;
- }
- }