Skip to content

Instantly share code, notes, and snippets.

@carlossg00
Created March 24, 2011 10:02
Show Gist options
  • Save carlossg00/884828 to your computer and use it in GitHub Desktop.
Save carlossg00/884828 to your computer and use it in GitHub Desktop.
SimpleChat Using JQuery / Symfony2
<?php
namespace CSG\SimpleChatBundle\Controller;
use Symfony\Component\Form\FormFactory;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpFoundation\Response;
use CSG\SimpleChatBundle\Entity\Message;
use CSG\SimpleChatBundle\Form\SimpleChatFormType;
class MainController extends ContainerAware
{
protected $request;
protected $router;
protected $templating;
protected $entity_manager;
protected $form_factory;
protected $http_kernel;
public function __construct(Request $request, UrlGeneratorInterface $router,
EngineInterface $templating, EntityManager $entity_manager, FormFactory $form_factory)
{
$this->templating = $templating;
$this->request = $request;
$this->router = $router;
$this->entity_manager = $entity_manager;
$this->form_factory = $form_factory;
}
public function indexAction()
{
$form = $this->form_factory->create('CSG\SimpleChatBundle\Form\SimpleChatFormType');
return $this->templating->renderResponse('CSGSimpleChatBundle:Main:index.html.twig',
array('form' => $form->getRenderer()));
}
public function createMessage()
{
$httpKernel = $this->container->get('http_kernel');
$msg = new Message();
$msg->setTimestamp(0);
$form = $this->form_factory->create('CSG\SimpleChatBundle\Form\SimpleChatFormType');
$form->setData($msg);
if ($this->request->getMethod() === 'POST') {
$form->bindRequest($this->request);
if ($form->isValid()) {
$this->entity_manager->persist($msg);
$this->entity_manager->flush();
// return $httpKernel->forward('simplechat.main.controller:updateMessage', array(
// 'timestamp' => 0));
}
}
$response = $httpKernel->forward('simplechat.main.controller:updateMessage', array(
'timestamp' => 0));
//$response->headers->set('Content-Type', 'application/json-rpc'); NO SENSE ANY WAY TO FORWARD XHR?
return $response;
//return new RedirectResponse($this->router->generate('simple_chat'));
}
/**
* Retrieve message from database and generate xml response
*/
public function updateMessage($timestamp = 0)
{
$messages = $this->entity_manager->createQuery('SELECT m FROM CSGSimpleChatBundle:Message m
WHERE m.timestamp >= :time ORDER BY m.id ASC')
->setParameter('time',$timestamp)
->setMaxResults(20)
->getResult();
if (!$messages) {
throw new NotFoundHttpException('The messages does not exist.');
}
$status_code = 1;
$xml = "<?xml version=\"1.0\"?>\n";
$xml .= "<response>\n";
$xml .= "\t<status>".$status_code."</status>\n";
$xml .= "\t<time>".$timestamp."</time>\n";
foreach ($messages as $msg)
{
$xml .= "\t<message>\n";
$xml .= "\t\t<author>".$msg->getUser()."</author>\n";
$xml .= "\t\t<text>".$msg->getMsg()."</text>\n";
$xml .= "\t</message>\n";
}
$xml .= "</response>\n";
if ($this->request->isXmlHttpRequest())
{
//return new Response("HELLO AJAX FrOM UPDATE");
return new Response($xml);
}
return new Response($xml);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment