Files
bodyrep-sandpit/src/BodyRep/Controller/MemberController.php
2012-09-24 10:32:34 +10:00

215 lines
6.7 KiB
PHP

<?php
namespace BodyRep\Controller;
use BodyRep\Form\Profile;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
# Annotations & templates
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use JMS\SecurityExtraBundle\Annotation\Secure;
class MemberController extends Controller
{
/**
* @Route("/login", name="_login")
* @Template()
*/
public function loginAction()
{
if ($this->get('request')->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $this->get('request')->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $this->get('request')->getSession()->get(SecurityContext::AUTHENTICATION_ERROR);
}
return array(
'last_username' => $this->get('request')->getSession()->get(SecurityContext::LAST_USERNAME),
'error' => $error,
);
}
/**
* @Route("/login_check", name="_security_check")
*/
public function securityCheckAction()
{
// The security layer will intercept this request
}
/**
* @Route("/logout", name="_logout")
*/
public function logoutAction()
{
// The security layer will intercept this request
}
/**
* @Route("/", name="_member")
* @Template()
*/
public function indexAction()
{
$username = $this->getUser()->getUsername();
$db = $this->getDoctrine()->getManager();
$query = $db->createQuery('
SELECT m
FROM BodyRep:Member m
WHERE m.username = :username')
->setParameter('username', $username)
->setMaxResults(1);
if (sizeof($query->getResult()) != 1)
throw $this->createNotFoundException("User '".$username."' not found");
$member = $query->getSingleResult();
return array('name' => $member->getFullName());
}
/**
* @Route("/profile/", name="_member_profile")
* @Template()
*/
public function profileAction()
{
$username = $this->getUser()->getUsername();
$db = $this->getDoctrine()->getManager();
$query = $db->createQuery('
SELECT p
FROM BodyRep:Profile p
WHERE p.username = :username')
->setParameter('username', $username)
->setMaxResults(1);
if (sizeof($query->getResult()) != 1)
throw $this->createNotFoundException("User '".$username."' not found");
$profile = $query->getSingleResult();
$db = $this->getDoctrine()->getManager();
$query = $db->createQuery('
SELECT m
FROM BodyRep:Member m
WHERE m.username = :username')
->setParameter('username', $username)
->setMaxResults(1);
if (sizeof($query->getResult()) != 1)
throw $this->createNotFoundException("User '".$username."' not found");
$member = $query->getSingleResult();
return (array('sFullName' => $profile->getFullName(), 'name' => $member->getFullName()));
}
/**
* @Route("/profile/edit", name="_member_profile_edit")
* @Template()
*/
public function editProfileAction()
{
$username = $this->getUser()->getUsername();
$db = $this->getDoctrine()->getManager();
$query = $db->createQuery('
SELECT m
FROM BodyRep:Member m
WHERE m.username = :username')
->setParameter('username', $username)
->setMaxResults(1);
if (sizeof($query->getResult()) != 1)
throw $this->createNotFoundException("User '".$username."' not found");
$member = $query->getSingleResult();
$form = $this->get('form.factory')->create(new Profile(), array('fullname' => $member->getFullName()));
$error = '';
return array('form' => $form->createView(), 'error' => '');
}
/**
* @Route("/profile/save", name="_member_profile_save")
*/
public function saveAction()
{
$username = $this->getUser()->getUsername();
$db = $this->getDoctrine()->getManager();
$query = $db->createQuery('
SELECT m
FROM BodyRep:Member m
WHERE m.username = :username')
->setParameter('username', $username)
->setMaxResults(1);
if (sizeof($query->getResult()) != 1)
throw $this->createNotFoundException("User '".$username."' not found");
$member = $query->getSingleResult();
$json = array('result' => 0);
$form = $this->get('form.factory')->create(new Profile());
$request = $this->get('request');
$form->bind($request);
if ($form->isValid())
{
$json['result'] = 1;
$d = $form->getClientData();
$member->setFullName($d['fullname']);
$db->persist($member);
$db->flush();
}
$resp = new Response (json_encode($json));
$resp->headers->set('Content-Type', 'text/plain');
return $resp;
}
/**
* @Route("/search/{param}", name="_member_search", defaults={"param" = 0})
* @Template()
*/
public function searchAction($param='')
{
/*
* Integreted suggester response
*
*/
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery("SELECT m FROM BodyRep:Member m WHERE m.fullname ILIKE '%$param%'");
$res = $query->getResult();
$resc = sizeof($res);
$sugg = array();
/*if ($res > 0)
{
foreach ($res as $member)
{
$text = preg_replace('/<br[^\>]*>/i', "\n", $member->getFullname());
$item['text'] = strip_tags($text);
$item['html'] = $text;
$item['data'] = array('username' => htmlspecialchars($member->getUsername()));
$sugg[] = $item;
}
}
if (!empty($param))
{
$json = array('result' => 1, 'suggestions' => $sugg);
$resp = new Response (json_encode($json));
$resp->headers->set('Content-Type', 'text/plain');
return $resp;
}
else*/
return array('search' => $res);
}
}