/**
  * {@inheritDoc}
  */
 public function setName($name)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setName', array($name));
     return parent::setName($name);
 }
 public function editstoryAction(Request $request)
 {
     /****** Admin session checking**********/
     $response = $this->checkAdmin($request->getSession());
     if ($response) {
         return $response;
     }
     $em = $this->getDoctrine()->getManager();
     $lan = $this->container->get('request')->get('lan');
     $id = $this->container->get('request')->get('id');
     $checkquery = $em->createQuery("SELECT p FROM MytripAdminBundle:Language  p WHERE p.lanCode='" . $lan . "'");
     $checklanguage = $checkquery->getArrayResult();
     if (empty($checklanguage)) {
         return $this->redirect($this->generateUrl('mytrip_admin_editstory', array('id' => $id, 'lan' => 'en')));
     }
     if ($request->getMethod() == "POST") {
         if ($lan == "en") {
             $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 hostal***********/
             $check = $em->createQuery("SELECT d FROM MytripAdminBundle:Story d WHERE  d.url='" . $page_link . "' AND d.storyId NOT IN ({$id})  AND d.hostal NOT IN (" . $request->request->get('hostal') . ")");
             $check_hostal = $check->getArrayResult();
             if (empty($check_hostal)) {
                 /***Story english language update***/
                 $repository = $em->getRepository('MytripAdminBundle:Story');
                 $story = $repository->findOneByStoryId($id);
                 $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->setUrl($page_link);
                 $em->flush();
             } else {
                 $this->get('session')->getFlashBag()->add('error', '<div class="success msg">Story already exists</div>');
                 return $this->redirect($this->generateUrl('mytrip_admin_editstory', array('id' => $id, 'lan' => $lan)));
             }
         }
         /******Story content update with corresponding lanugage******/
         $em = $this->getDoctrine()->getManager();
         $check = $em->createQuery("SELECT p FROM MytripAdminBundle:StoryContent p WHERE p.story=" . $id . " AND p.lan='" . $lan . "'");
         $check_content = $check->getArrayResult();
         if (!empty($check_content)) {
             $repository_content = $em->getRepository('MytripAdminBundle:StoryContent');
             $story_content = $repository_content->findOneByStoryContentId($check_content['0']['storyContentId']);
             $story_content->setName($request->request->get('name'));
             $story_content->setStory($this->getDoctrine()->getRepository('MytripAdminBundle:Story')->find($id));
             $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($lan);
         } else {
             $story_content = new StoryContent();
             $story_content->setName($request->request->get('name'));
             $story_content->setStory($this->getDoctrine()->getRepository('MytripAdminBundle:Story')->find($id));
             $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($lan);
             $em->persist($story_content);
         }
         $em->flush();
         /**********Story Image update********/
         if ($request->files->get('image') != '') {
             $story_image = new StoryImage();
             $em = $this->getDoctrine()->getManager();
             $checkimage_query = $em->createQuery("SELECT p FROM MytripAdminBundle:StoryImage p WHERE p.story=" . $id);
             $checkimage = $checkimage_query->getArrayResult();
             $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);
             if (!empty($checkimage)) {
                 $deleteobject = \S3::deleteObject($bucket, $checkimage[0]['image']);
                 /*$file_path="img/story/".$checkimage[0]['image'];
                 		if($checkimage[0]['image']!='' && file_exists($file_path)){
                 			unlink($file_path);
                 		}*/
                 $em->createQuery("DELETE FROM MytripAdminBundle:StoryImage b WHERE b.story ={$id}")->execute();
             }
             $story_image->setStory($this->getDoctrine()->getRepository('MytripAdminBundle:Story')->find($id));
             $ext = $request->files->get('image')->getClientOriginalExtension();
             $filename = $this->str_rand(8, "alphanum") . "." . $ext;
             //$request->files->get('image')->move("img/story",$filename);
             $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 updated</div>');
         return $this->redirect($this->generateUrl('mytrip_admin_editstory', array('id' => $id, 'lan' => $lan)));
     }
     /******Fetch language********/
     $query = $em->createQuery("SELECT p FROM MytripAdminBundle:Language  p ");
     $language = $query->getArrayResult();
     /*******Fetch story details*****/
     $story_query = $em->createQuery("SELECT p,IDENTITY(p.destination) AS destination,IDENTITY(p.hostal) AS hostal FROM MytripAdminBundle:Story p WHERE p.storyId=" . $id);
     $story = $story_query->getArrayResult();
     if (empty($story)) {
         return $this->redirect($this->generateUrl('mytrip_admin_story'));
     }
     /*******Fetch Story content details*****/
     $story_content_query = $em->createQuery("SELECT d FROM MytripAdminBundle:StoryContent d WHERE d.story=" . $id . " AND d.lan='" . $lan . "'");
     $story_content = $story_content_query->getArrayResult();
     if (empty($story_content)) {
         $story_content_query = $em->createQuery("SELECT d FROM MytripAdminBundle:StoryContent d WHERE d.story=" . $id . " AND d.lan='en'");
         $story_content = $story_content_query->getArrayResult();
     }
     $story_image_query = $em->createQuery("SELECT d FROM MytripAdminBundle:StoryImage d WHERE d.story=" . $id);
     $story_image = $story_image_query->getArrayResult();
     $destination_query = $em->createQuery("SELECT f FROM MytripAdminBundle:Destination f");
     $destination = $destination_query->getArrayResult();
     $hostal_query = $em->createQuery("SELECT f FROM MytripAdminBundle:Hostal f WHERE f.destination=" . $story[0]['destination']);
     $hostal = $hostal_query->getArrayResult();
     return $this->render('MytripAdminBundle:Default:editstory.html.php', array('language' => $language, 'story' => $story, 'story_content' => $story_content, 'story_image' => $story_image, 'destination' => $destination, 'hostal' => $hostal));
 }