Skip to content

Instantly share code, notes, and snippets.

@mockiemockiz
Created November 29, 2014 14:16
Show Gist options
  • Save mockiemockiz/ef73d78ff63d42d619b0 to your computer and use it in GitHub Desktop.
Save mockiemockiz/ef73d78ff63d42d619b0 to your computer and use it in GitHub Desktop.
<?php
namespace Mockizart\Bundle\BlogBundle\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Mockizart\Bundle\BlogBundle\Entity\MockblogPost;
/**
* MockblogPost controller.
*
*/
class MockblogPostController extends Controller
{
private $formTypeName = 'blog_post_type';
private $entityService = 'post_entity';
private $entityName = 'post_entity_name';
private $routingList = [];
public function __construct()
{
$this->routingList['show'] = 'post_show';
$this->routingList['create'] = 'post_create';
$this->routingList['edit'] = 'post_edit';
$this->routingList['update'] = 'post_update' ;
$this->routingList['delete'] = 'post_delete';
}
/**
* Lists all MockblogPost entities.
*
*/
public function indexAction()
{
$form = $this->createForm($this->formTypeName);
$entity = $this->get($this->entityService);
$entity->setDefaultEntity($entity->initialize());
$entity->setRequest($this->getRequest());
$entity->arrayKeyDataRequest = $this->formTypeName;
$entity->setSearchColumns(['title' => ['LIKE','%%%s%%',1]]);
$query = $entity->search();
$pagination = $this->get('knp_paginator')->paginate(
$query,
$this->get('request')->query->get('page', 1)/*page number*/,
20/*limit per page*/
);
return $this->render(
$this->container->getParameter($this->entityName) . ':index.html.twig',
array(
'pagination' => $pagination,
'form' => $form->createView()
)
);
}
/**
* Creates a new MockblogPost entity.
*
*/
public function createAction(Request $request)
{
$entity = $this->get($this->entityService);
$entity->arrayKeyDataRequest = $this->formTypeName;
$postEntity = $entity->initialize();
$form = $this->createCreateForm($postEntity);
$entity->setDefaultEntity($postEntity);
$entity->setForm($form);
$entity->setRequest($request);
$entity->setUserId($this->getUser()->getId());
$newId = $entity->create();
if ($newId) {
return $this->redirect($this->generateUrl($this->routingList['show'], array('id' => $newId)));
}
return $this->render($this->container->getParameter($this->entityName) . ':new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Creates a form to create a MockblogPost entity.
*
* @param MockblogPost $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(MockblogPost $entity)
{
$form = $this->createForm($this->formTypeName, $entity, array(
'action' => $this->generateUrl($this->routingList['create']),
'method' => 'POST',
'attr' => ['class' => 'form-box']
));
$form->add('tags','text',['required' => false, 'attr' => ['data-url' => $this->generateUrl('tag_ajax', [], true)]]);
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new MockblogPost entity.
*
*/
public function newAction()
{
$entity = $this->get('post_entity')->initialize();
$form = $this->createCreateForm($entity);
return $this->render($this->container->getParameter($this->entityName) . ':new.html.twig', array(
'entity' => $entity,
'form' => $form->createView(),
));
}
/**
* Finds and displays a MockblogPost entity.
*
*/
public function showAction($id)
{
$entity = $this->get($this->entityService)->find($id);
$deleteForm = $this->createDeleteForm($id);
return $this->render($this->container->getParameter($this->entityName) . ':show.html.twig', array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing MockblogPost entity.
*
*/
public function editAction($id)
{
$entity = $this->get($this->entityService)->find($id);
$tags = [];
foreach($entity->getTagPostAssociations() as $b){
$tags[] = ['id' => $b->getTag()->getId(),'name' => $b->getTag()->getName()];
}
$editForm = $this->createEditForm($entity);
$editForm->get('tags_default')->setData(json_encode($tags));
$deleteForm = $this->createDeleteForm($id);
return $this->render($this->container->getParameter($this->entityName) . ':edit.html.twig', array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Creates a form to edit a MockblogPost entity.
*
* @param MockblogPost $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(MockblogPost $entity)
{
$form = $this->createForm($this->formTypeName, $entity, array(
'action' => $this->generateUrl($this->routingList['update'], array('id' => $entity->getId())),
'method' => 'PUT',
'attr' => ['class' => 'form-box']
));
$form->add('tags','text',['required' => false, 'attr' => ['data-url' => $this->generateUrl('tag_ajax', [], true)]]);
$form->add('tags_default', 'hidden', ['required' => false]);
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing MockblogPost entity.
*
*/
public function updateAction(Request $request, $id)
{
$deleteForm = $this->createDeleteForm($id);
$entity = $this->get($this->entityService);
$userEntity = $entity->find($id);
$entity->setDefaultEntity($userEntity);
$editForm = $this->createEditForm($userEntity);
$entity->setRequest($request);
$entity->setForm($editForm);
$entity->arrayKeyDataRequest = $this->formTypeName;
if ($entity->save()) {
return $this->redirect($this->generateUrl($this->routingList['edit'], array('id' => $id)));
}
return $this->render($this->container->getParameter($this->entityName) . ':edit.html.twig', array(
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a MockblogPost entity.
*
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$id = (int) $id;
$entity = $this->get($this->entityService);
$entity->setDefaultEntity($entity->find($id));
$entity->setForm($form);
$entity->setRequest($request);
$entity->delete();
return $this->redirect($this->generateUrl('post'));
}
/**
* Creates a form to delete a MockblogPost entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl($this->routingList['delete'], array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => ' ','attr' => ['class' => 'btn btn-danger glyphicon glyphicon-remove']))
->getForm()
;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment