/**
  * {@inheritDoc}
  */
 public function setUrl($url)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setUrl', array($url));
     return parent::setUrl($url);
 }
 public function addstoryAction(Request $request)
 {
     /****** Admin session checking**********/
     $response = $this->checkAdmin($request->getSession());
     if ($response) {
         return $response;
     }
     $story = new Story();
     $story_content = new StoryContent();
     $story_image = new StoryImage();
     if ($request->getMethod() == "POST") {
         $page_link = strtolower(str_replace(' ', '_', preg_replace('/[^a-zA-Z0-9_ -]/s', ' ', $request->request->get('name'))));
         if ($page_link == '') {
             $page_link = strtolower(str_replace(array(' ', '\''), array('_', '-'), $request->request->get('name')));
         }
         $em = $this->getDoctrine()->getManager();
         /********Checking duplicate stories***********/
         $check = $em->createQuery("SELECT s FROM MytripAdminBundle:Story s WHERE  s.url='" . $page_link . "' AND s.hostal ='" . $request->request->get('hostal') . "'");
         $check_story = $check->getArrayResult();
         if (empty($check_story)) {
             $story->setName($request->request->get('name'));
             $story->setDestination($this->getDoctrine()->getRepository('MytripAdminBundle:Destination')->find($request->request->get('destination')));
             $story->setHostal($this->getDoctrine()->getRepository('MytripAdminBundle:Hostal')->find($request->request->get('hostal')));
             $story->setTopStory('No');
             $story->setUrl($page_link);
             $story->setStatus('Active');
             $story->setCreatedDate(new \DateTime(date('Y-m-d H:i:s')));
             $em->persist($story);
             $em->flush();
             $em = $this->getDoctrine()->getManager();
             $lastid = $story->getStoryId();
             $story_content->setName($request->request->get('name'));
             $story_content->setStory($this->getDoctrine()->getRepository('MytripAdminBundle:Story')->find($lastid));
             $story_content->setSubHead($request->request->get('subhead'));
             $story_content->setContent($request->request->get('content'));
             $story_content->setMetaTitle($request->request->get('metatitle'));
             $story_content->setMetaDescription($request->request->get('metadescription'));
             $story_content->setMetaKeyword($request->request->get('metakeyword'));
             $story_content->setLan('en');
             $em->persist($story_content);
             $em->flush();
             if ($request->files->get('image') != '') {
                 $em = $this->getDoctrine()->getManager();
                 $story_image->setStory($this->getDoctrine()->getRepository('MytripAdminBundle:Story')->find($lastid));
                 $ext = $request->files->get('image')->getClientOriginalExtension();
                 $filename = $this->str_rand(8, "alphanum") . "." . $ext;
                 //$request->files->get('image')->move("img/story",$filename);
                 $awsAccessKey = $this->container->get('mytrip_admin.helper.amazon')->getOption('awsAccessKey');
                 $awsSecretKey = $this->container->get('mytrip_admin.helper.amazon')->getOption('awsSecretKey');
                 $bucket = $this->container->get('mytrip_admin.helper.amazon')->getOption('bucket');
                 \S3::setAuth($awsAccessKey, $awsSecretKey);
                 $tmpfile = $request->files->get('image')->getPathName();
                 $putobject = \S3::putObjectFile($tmpfile, $bucket, $filename, \S3::ACL_PUBLIC_READ);
                 if ($putobject) {
                     $story_image->setImage($filename);
                     $story_image->setCreatedDate(new \DateTime(date('Y-m-d H:i:s')));
                     $em->persist($story_image);
                     $em->flush();
                 }
             }
             $this->get('session')->getFlashBag()->add('error', '<div class="success msg">Story successfully added</div>');
             return $this->redirect($this->generateUrl('mytrip_admin_story'));
         } else {
             $this->get('session')->getFlashBag()->add('error', '<div class="error msg">Story already exists</div>');
         }
     }
     $em = $this->getDoctrine()->getManager();
     $destination_query = $em->createQuery("SELECT d FROM MytripAdminBundle:Destination d");
     $destination = $destination_query->getArrayResult();
     return $this->render('MytripAdminBundle:Default:addstory.html.php', array('destination' => $destination));
 }