Skip to content

Instantly share code, notes, and snippets.

@ClementDuez
Created December 19, 2012 11:05
Show Gist options
  • Save ClementDuez/4335976 to your computer and use it in GitHub Desktop.
Save ClementDuez/4335976 to your computer and use it in GitHub Desktop.
Controller Ajax (voir updateAction)
<?php
namespace User\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel,
Zend\Session\Container,
Doctrine\ORM\EntityManager,
Zend\Form\Element;
use User\Entity\Article,
User\Entity\Commentaire,
User\Entity\Image,
User\Form\ArticleForm,
User\Form\ImageForm,
User\Form\CommentaireForm;
/**
* Description of ArticleController
*
* @author dudu
*/
class ArticleController extends AbstractActionController
{
protected $em;
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->em;
}
public function indexAction($article = null)
{
if ($article === null){
$article = $this->getEntityManager()->getRepository('User\Entity\Article')->findLastArticleOrNull();
}
$articles = $this->getEntityManager()->getRepository('User\Entity\Article')->findByIsPublished(true);
$formCommentaire = new CommentaireForm();
return new ViewModel(array(
'article' => $article,
'articles' => $articles,
'formCommentaire' => $formCommentaire
));
}
public function addCommentaireAction()
{
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
$em = $this->getEntityManager();
$article = $em->getRepository('User\Entity\Article')->find($id);
if ($article !== null) {
$form = new CommentaireForm();
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$commentaire = new Commentaire();
$commentaire->setPseudo($form->get('pseudo')->getValue())
->setContent($form->get('content')->getValue());
$article->addCommentaire($commentaire);
$em->persist($article);
$em->flush();
return $this->redirect()->toRoute('article', array('action' => 'show', 'id' => $id));
}
}
}
return $this->redirect()->toRoute('article', array('action' => 'show', 'id' => $id));
}
public function addAction()
{
$form = new ArticleForm();
$formImage = new ImageForm();
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$em = $this->getEntityManager();
$article = new Article();
$isPublished = $form->get('isPublished')->getValue() === null ? false : true;
$article->setTitle($form->get('title')->getValue())
->setContent($form->get('content')->getValue())
->setIsPublished($isPublished);
$em->persist($article);
$em->flush();
return $this->redirect()->toRoute('article', array('action' => 'index'));
}
}
return new ViewModel(array(
'form' => $form,
'formImage' => $formImage
));
}
public function addImageAction()
{
$form = new ImageForm();
$lol = $this->params()->fromFiles('upload');
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
$em = $this->getEntityManager();
$image = new Image();
$image->setNom($form->get('name')->getValue());
$image->setFile($this->params()->fromFiles('file'));
$em->persist($image);
$em->flush();
return $this->redirect()->toRoute('article', array('action' => 'index'));
}
}
}
public function updateAction()
{
$request = $this->getRequest();
if ($request->isPost()) {
$p = $p;
}
}
public function showAction()
{
$article = $this->getEntityManager()->getRepository('User\Entity\Article')->find((int)$this->getEvent()->getRouteMatch()->getParam('id'));
if ($article === null) {
return $this->redirect()->toRoute('article', array('action' => 'index'));
}
return $this->indexAction($article);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment