public function AddCheckAction(Request $request)
 {
     // Check's the added Villa
     try {
         $AddVilf = new AddVilF();
         $form = $this->createForm(new AddVilFType(), $AddVilf);
         $form->handleRequest($request);
         if ($form->isValid()) {
             $sc = $this->get('security.context');
             if ($sc->isGranted('ROLE_USER') && !$sc->isGranted('ROLE_OWNER') || $sc->isGranted('ROLE_ADMIN')) {
                 $user = $this->getUser();
                 $rr = $this->getDoctrine()->getManager();
                 $query = $rr->createQuery("SELECT u FROM WebVLWebVillasBundle:Users u WHERE u.id = :id")->setParameter(':id', $user->getId());
                 $eduser = $query->getSingleResult();
                 $res = false;
                 $adm = false;
                 $i = 0;
                 $rl = $eduser->getRoles();
                 while (!$res && $i < count($rl)) {
                     if ($rl[$i]->getName() == "User") {
                         $res = true;
                         if ($rl[$i]->getName() == "Admin") {
                             $adm = true;
                         }
                     } else {
                         $i++;
                     }
                 }
                 if ($res) {
                     $villas = new \WebVL\WebVillasBundle\Entity\Villas();
                     $villas->setAddress($AddVilf->getAddress());
                     if ($AddVilf->getDetails() != "") {
                         $villas->setDetails($AddVilf->getDetails());
                     }
                     $villas->setGeox($AddVilf->getGeoX());
                     $villas->setGeoy($AddVilf->getGeoY());
                     $villas->setPrice($AddVilf->getPrice());
                     $villas->setRestrooms($AddVilf->getRestrooms());
                     $villas->setRooms($AddVilf->getRooms());
                     $villas->setSize($AddVilf->getSize());
                     $villas->setUsers($user);
                     if ($AddVilf->getView() != "") {
                         $villas->setView($AddVilf->getView());
                     }
                     $state = new \WebVL\WebVillasBundle\Entity\States();
                     if ($request->request->get('_State') != null && $request->request->get('_State') != '' && preg_match("/^[0-9]+\$/", $request->request->get('_State'))) {
                         $sta = $request->request->get('_State');
                         $st = $this->getDoctrine()->getManager();
                         $query = $st->createQuery("SELECT r FROM WebVLWebVillasBundle:States r WHERE r.id = :id")->setParameter(':id', (int) $sta);
                         $state = $query->getSingleResult();
                         $villas->setStates($state);
                     } else {
                         //state not valid
                         $sta = $request->request->get('_State');
                         $er = $this->getDoctrine()->getManager();
                         $query = $er->createQuery('SELECT r.id, r.state FROM WebVLWebVillasBundle:States r')->getResult();
                         return $this->render('WebVLWebVillasBundle:Villas:AddVilla.html.twig', array('form' => $form->createView(), 'statere' => 'Please choose a state! ' . $sta, 'Query' => $query));
                     }
                     $em = $this->getDoctrine()->getManager();
                     $em->persist($villas);
                     $em->flush();
                     $st = $this->getDoctrine()->getManager();
                     $query = $st->createQuery("SELECT count(r) FROM WebVLWebVillasBundle:AvailEquipment r ");
                     $id = $query->getSingleResult();
                     $Avail = new \WebVL\WebVillasBundle\Entity\AvailEquipment($villas, $id[1] + 1);
                     $Avail->setParking($AddVilf->getParking());
                     if (!preg_match("/^True|TRUE|true|1\$/", $AddVilf->getGym())) {
                         $Avail->setGym(true);
                     } else {
                         $Avail->setGym(false);
                     }
                     if (!preg_match("/^True|TRUE|true|1\$/", $AddVilf->getPool())) {
                         $Avail->setPool(true);
                     } else {
                         $Avail->setPool(false);
                     }
                     if (!preg_match("/^True|TRUE|true|1\$/", $AddVilf->getSauna())) {
                         $Avail->setSauna(true);
                     } else {
                         $Avail->setSauna(false);
                     }
                     $em->persist($Avail);
                     $em->flush();
                     if ($adm == false) {
                         $eduser->removeRole($rl[$i]);
                         $roles = $rr->createQuery("SELECT r FROM WebVLWebVillasBundle:Roles r WHERE r.name = 'Villas Owner'")->getSingleResult();
                         // $rr  = Doctrine Manager (aka Database manager)
                         // updates the user role to Owner!!
                         $eduser->addRole($roles);
                         $this->get('security.context')->getToken()->setUser($eduser);
                         // Creates a token for $_SESSION update!!
                         $token = new \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken($eduser, null, 'main', $eduser->getRoles());
                         $this->container->get('security.context')->setToken($token);
                         $rr->flush();
                         // $this->get('security.context')->getToken()->setAuthenticated(false);
                     }
                     return $this->redirect($this->generateUrl('_Villas', array('Success' => 'you created the Villa in this address: ' . $AddVilf->getAddress())));
                 } else {
                     return $this->redirect($this->generateUrl('_Villas', array('Errors' => 'You dont have the right kind of permissions!')));
                 }
             } else {
                 return $this->redirect($this->generateUrl('_Villas', array('Errors' => 'You dont have the right kind of permissions!')));
             }
         } else {
             //form not valid
             $em = $this->getDoctrine()->getManager();
             $query = $em->createQuery('SELECT r.id, r.state FROM WebVLWebVillasBundle:States r')->getResult();
             return $this->render('WebVLWebVillasBundle:Villas:AddVilla.html.twig', array('form' => $form->createView(), 'Query' => $query));
         }
     } catch (\PDOException $e) {
         return $this->render('WebVLWebVillasBundle:Default:Errors.html.twig', array('Errors' => array("Name" => "Database Error", "Message" => "We have some problem with our database!")));
     }
 }
 public function CreateTestAction()
 {
     //Creates The Admin User and some fake users as well Villa's
     try {
         $em = $this->getDoctrine()->getManager();
         //<editor-fold defaultstate="collapsed" desc="Admin User">
         $AdminUser = $em->createQuery("SELECT r FROM WebVLWebVillasBundle:Users r WHERE r.username = '******'")->getSingleResult();
         if (count($AdminUser) == 0) {
             $admin = \WebVL\WebVillasBundle\Entity\Users();
             $factory = $this->get('security.encoder_factory');
             $admin->setUsername("Administrator");
             $admin->setActivated(true);
             $admin->setEmail("*****@*****.**");
             $ActiveCode = rand(1000000000, 9999999999.0);
             //  var_dump( $ActiveCode);
             $admin->setActivationCode($ActiveCode);
             $encoder = $factory->getEncoder($admin);
             $password = $encoder->encodePassword('Adm1n1str@t0r', $admin->getSalt());
             $admin->setPassword($password);
             $admin->setJoined(new \DateTime());
             $AdminRole = $em->createQuery("SELECT r FROM WebVLWebVillasBundle:Roles r WHERE r.name = 'Admin'")->getSingleResult();
             $admin->addRole($AdminRole);
             $em->persist($admin);
             $em->flush();
         }
         //</editor-fold>
         $roleUser = $em->createQuery("SELECT r FROM WebVLWebVillasBundle:Roles r WHERE r.name = 'User'")->getSingleResult();
         if (true == false) {
             //<editor-fold defaultstate="collapsed" desc="Users">
             for ($i = 0; $i < 40; $i++) {
                 $user = new \WebVL\WebVillasBundle\Entity\Users();
                 $factory = $this->get('security.encoder_factory');
                 $user->setUsername("User{$i}");
                 $user->setActivated(true);
                 $user->setEmail("User{$i}@plus.gr");
                 $ActiveCode = rand(1000000000, 9999999999.0);
                 //  var_dump( $ActiveCode);
                 $user->setActivationCode($ActiveCode);
                 $encoder = $factory->getEncoder($user);
                 $password = $encoder->encodePassword('0123456789AA', $user->getSalt());
                 $user->setPassword($password);
                 $user->setJoined(new \DateTime());
                 $user->addRole($roleUser);
                 $em->persist($user);
             }
             $em->flush();
             //</editor-fold>
         }
         //<editor-fold defaultstate="collapsed" desc="Villas">
         $rr = $this->getDoctrine()->getManager();
         $roleOwner = $rr->createQuery("SELECT r FROM WebVLWebVillasBundle:Roles r WHERE r.name = 'Villas Owner'")->getSingleResult();
         for ($i = 0; $i < 40; $i++) {
             $User = $rr->createQuery("SELECT u FROM WebVLWebVillasBundle:Users u WHERE u.username = '******'")->getSingleResult();
             $User->removeRole($roleUser);
             $User->addRole($roleOwner);
             $villas = new \WebVL\WebVillasBundle\Entity\Villas();
             $villas->setAddress("{$i} Paradise");
             $villas->setDetails("Somethings {$i}");
             $villas->setView("Views WhatEver {$i}");
             $villas->setGeox(37.94130964422101 - $i);
             $villas->setGeoy(23.90991121530533 - $i);
             $villas->setPrice(20000 + $i * 2000);
             $villas->setRestrooms(rand(1, 10));
             $villas->setRooms(rand(1, 15));
             $villas->setSize(rand(1, 20));
             $villas->setUsers($User);
             $state = new \WebVL\WebVillasBundle\Entity\States();
             $st = $this->getDoctrine()->getManager();
             $query = $st->createQuery("SELECT r FROM WebVLWebVillasBundle:States r WHERE r.id = :id")->setParameter(':id', (int) rand(1, 15));
             $state = $query->getSingleResult();
             $villas->setStates($state);
             $em = $this->getDoctrine()->getManager();
             $em->persist($villas);
             $em->flush();
             $st = $this->getDoctrine()->getManager();
             $query = $st->createQuery("SELECT count(r) FROM WebVLWebVillasBundle:AvailEquipment r ");
             $id = $query->getSingleResult();
             $Avail = new \WebVL\WebVillasBundle\Entity\AvailEquipment($villas, $id[1] + 1);
             $Avail->setParking(rand(0, 5));
             if ($i == 2 || $i == 5 || $i == 10 || $i == 18) {
                 $Avail->setGym(true);
             } else {
                 $Avail->setGym(false);
             }
             if ($i == 2 || $i == 4 || $i == 10 || $i == 14) {
                 $Avail->setPool(true);
             } else {
                 $Avail->setPool(false);
             }
             if ($i == 2 || $i == 3 || $i == 14 || $i == 18) {
                 $Avail->setSauna(true);
             } else {
                 $Avail->setSauna(false);
             }
             $em->persist($Avail);
             $em->flush();
         }
         //</editor-fold>
         return new \Symfony\Component\HttpFoundation\Response('Test Vars Created!');
     } catch (\PDOException $e) {
         return $this->render('WebVLWebVillasBundle:Default:Errors.html.twig', array('Errors' => array("Name" => "Database Error", "Message" => "We have some problem with our database!")));
     }
 }