/**
  * {@inheritDoc}
  */
 public function setTripadvisor($tripadvisor)
 {
     $this->__initializer__ && $this->__initializer__->__invoke($this, 'setTripadvisor', array($tripadvisor));
     return parent::setTripadvisor($tripadvisor);
 }
 public function addhostalAction(Request $request)
 {
     /****** Admin session checking**********/
     $response = $this->checkAdmin($request->getSession());
     if ($response) {
         return $response;
     }
     $em = $this->getDoctrine()->getManager();
     $hostal = new Hostal();
     $hostal_content = new HostalContent();
     $hostal_image = new HostalImage();
     //$hostal_rooms = new HostalRooms();
     $hostal_feature = new HostalFeature();
     $feature = new Feature();
     $cancel = new CancelDefault();
     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')));
         }
         // /********Checking duplicate hostal***********/
         // $check = $em->createQuery("SELECT h FROM MytripAdminBundle:Hostal h WHERE  h.url='".$page_link."' AND h.destination ='".$request->request->get('destination')."' AND  h.status NOT IN ('Trash') ");
         //       $check_hostal = $check->getArrayResult();
         // if(empty($check_hostal)){
         $destination_query = $em->createQuery("SELECT d,IDENTITY(d.country) AS country, IDENTITY(d.province) AS province FROM MytripAdminBundle:Destination d WHERE d.destinationId='" . $request->request->get('destination') . "' AND d.status NOT IN ('Trash')");
         $destination = $destination_query->getArrayResult();
         $country_query = $em->createQuery("SELECT c FROM MytripAdminBundle:Country c WHERE c.cid='" . $destination['0']['country'] . "'");
         $country = $country_query->getArrayResult();
         $state_query = $em->createQuery("SELECT s FROM MytripAdminBundle:States s WHERE s.sid='" . $destination['0']['province'] . "'");
         $state = $state_query->getArrayResult();
         $destination[0] = $destination[0][0];
         $address = $request->request->get('address') . "," . $country[0]['country'];
         $position = $this->getBoundLatitude_longitude($address);
         if (empty($position)) {
             $longitude = $destination['0']['longitude'];
             $latitude = $destination['0']['latitude'];
         } else {
             $longitude = $position['longitude'];
             $latitude = $position['latitude'];
         }
         $hostal->setName($request->request->get('name'));
         $hostal->setDestination($this->getDoctrine()->getRepository('MytripAdminBundle:Destination')->find($request->request->get('destination')));
         $hostal->setUrl($page_link);
         $hostal->setOwnerEmail($request->request->get('owneremail'));
         $hostal->setCccode($request->request->get('cccode'));
         $hostal->setPhone($request->request->get('phone'));
         $hostal->setCmcode($request->request->get('cmcode'));
         $hostal->setMobile($request->request->get('mobile'));
         $hostal->setVideo($request->request->get('svdesc'));
         $hostal->setTripadvisor($request->request->get('tripadvisor'));
         $hostal->setLongitude($longitude);
         $hostal->setLatitude($latitude);
         $hostal->setStatus('Active');
         $hostal->setCreatedDate(new \DateTime(date('Y-m-d H:i:s')));
         $hostal->setModifyDate(new \DateTime(date('Y-m-d H:i:s')));
         $em->persist($hostal);
         $em->flush();
         $em = $this->getDoctrine()->getManager();
         $lastid = $hostal->getHostalId();
         $hostal_content->setName($request->request->get('name'));
         $hostal_content->setHostal($this->getDoctrine()->getRepository('MytripAdminBundle:Hostal')->find($lastid));
         $hostal_content->setSmallDesc($request->request->get('smalldescription'));
         $hostal_content->setDescription($request->request->get('description'));
         $hostal_content->setLocationDesc($request->request->get('location_desc'));
         $hostal_content->setAddress($address);
         $hostal_content->setOwnerName($request->request->get('ownername'));
         $hostal_content->setCity($destination[0]['name']);
         $hostal_content->setProvince($state[0]['state']);
         $hostal_content->setCountry($country[0]['country']);
         $hostal_content->setMetaTitle($request->request->get('metatitle'));
         $hostal_content->setMetaDescription($request->request->get('metadescription'));
         $hostal_content->setMetaKeyword($request->request->get('metakeyword'));
         $hostal_content->setLan('en');
         $em->persist($hostal_content);
         $em->flush();
         // $em =  $this->getDoctrine()->getManager();
         // $hostal_rooms->setHostal($this->getDoctrine()->getRepository('MytripAdminBundle:Hostal')->find($lastid));
         // $hostal_rooms->setRooms($request->request->get('rooms'));
         // $hostal_rooms->setRoomtype($request->request->get('roomtype'));
         // $hostal_rooms->setGuests($request->request->get('guests'));
         // $hostal_rooms->setAdults($request->request->get('adults'));
         // $hostal_rooms->setChild($request->request->get('child'));
         // $hostal_rooms->setPrice($request->request->get('price'));
         // $em->persist($hostal_rooms);
         // $em->flush();
         $h_feature = $request->request->get('feature');
         foreach ($h_feature as $features) {
             $em = $this->getDoctrine()->getManager();
             $hostal_feature = new HostalFeature();
             $hostal_feature->setHostal($this->getDoctrine()->getRepository('MytripAdminBundle:Hostal')->find($lastid));
             $hostal_feature->setFeature($this->getDoctrine()->getRepository('MytripAdminBundle:Feature')->find($features));
             $hostal_feature->setCreatedDate(new \DateTime(date('Y-m-d H:i:s')));
             $em->persist($hostal_feature);
             $em->flush();
             unset($hostal_feature);
         }
         if ($request->files->get('image') != '') {
             $em = $this->getDoctrine()->getManager();
             $hostal_image->setHostal($this->getDoctrine()->getRepository('MytripAdminBundle:Hostal')->find($lastid));
             $ext = $request->files->get('image')->getClientOriginalExtension();
             $filename = $this->str_rand(8, "alphanum") . "." . $ext;
             //$request->files->get('image')->move("img/destination",$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) {
                 //$request->files->get('image')->move("img/hostal",$filename);
                 $hostal_image->setImage($filename);
                 $hostal_image->setCreatedDate(new \DateTime(date('Y-m-d H:i:s')));
                 $em->persist($hostal_image);
                 $em->flush();
             }
         }
         $canceldetails = $em->createQuery("SELECT d FROM MytripAdminBundle:CancelDefault d")->getArrayResult();
         foreach ($canceldetails as $canceldetails) {
             $em = $this->getDoctrine()->getManager();
             $hostal_cancel = new HostalCancelDetails();
             $hostal_cancel->setHostal($this->getDoctrine()->getRepository('MytripAdminBundle:Hostal')->find($lastid));
             $hostal_cancel->setDays($canceldetails['days']);
             $hostal_cancel->setPercentage($canceldetails['percentage']);
             $em->persist($hostal_cancel);
             $em->flush();
             unset($hostal_cancel);
         }
         $this->get('session')->getFlashBag()->add('error', '<div class="success msg">Hostal successfully added</div>');
         return $this->redirect($this->generateUrl('mytrip_admin_hostal'));
         // }
         // else{
         // 	$this->get('session')->getFlashBag()->add('error','<div class="error msg">Hostal already exists</div>');
         // }
     }
     $em = $this->getDoctrine()->getManager();
     $feature_query = $em->createQuery("SELECT f FROM MytripAdminBundle:Feature f");
     $feature = $feature_query->getArrayResult();
     $destination_query = $em->createQuery("SELECT f FROM MytripAdminBundle:Destination f WHERE f.status NOT IN ('Trash')");
     $destination = $destination_query->getArrayResult();
     return $this->render('MytripAdminBundle:Default:addhostal.html.php', array('feature' => $feature, 'destination' => $destination));
 }