Skip to content

Instantly share code, notes, and snippets.

@jazlopez
Created February 21, 2016 06:35
Show Gist options
  • Save jazlopez/c916289388ba6c66cbc1 to your computer and use it in GitHub Desktop.
Save jazlopez/c916289388ba6c66cbc1 to your computer and use it in GitHub Desktop.
Abstract BackOffice Base Controller
<?php namespace BackOfficeBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
/**
* Class BackOfficeBaseController
* @author Jaziel Lopez <[email protected]>
* @package BackOfficeBundle\Controller
*/
abstract class BackOfficeBaseController extends Controller
{
/**
* Extended Class Controller Name
*
* @var
*/
public $entity;
/**
* Route for inserting new products
*
* @var string $routeInsert
*/
public $routeInsert;
/**
* Route for updating existing products
*
* @var string $routeUpdate
*/
public $routeUpdate;
/**
* Template Data
*
* @var Array $templateData
*/
public $templateData;
/**
* Template post action
*
* @var string $postTo
*/
public $postTo;
/**
* Response
*
* @var Response $response
*/
public $response;
/**
* @var
*/
public $request;
/**
* Get logged user
* @return \BackOfficeBundle\Entity\User|null|object
*/
public function getLoggedUser(){
return $this->em()->getRepository('BackOfficeBundle:User')->find($this->getUser()->getId());
}
/**
* Get Entitiy Manager
*
* @return ORM\EntityManager
*/
public function em()
{
return $this->getDoctrine()->getManager();
}
/**
* Collect return parameters
* Id in url determines the post back location
* Eval If there is a product object that needs to be displayed in the form
* If so, display it
* If not, get the request data and display it
* @param int $id
* @return array
*/
public function getTemplateData($id = null){
$this->postTo = (is_numeric($id)) ?
$this->routeUpdate : $this->routeInsert;
$this->templateData = (!empty($this->entity)) ? (array)$this->entity : $this->request->request->all();
/**
* Start transforming the object from Entity to array
*/
foreach((array)$this->templateData as $key => $value){
$name = explode('\\', $key);
$key = preg_replace( "/[^a-z0-9 ]/i", "_", $name[count($name)-1]);
$key = preg_replace('/\B([A-Z])/', '_$1', $key);
$key = strtolower($key);
$this->templateData[$key] = $value;
}
$this->templateData['post'] = (is_numeric($id)) ? $this->generateUrl($this->postTo, ['id' => $id]) :
$this->generateUrl($this->postTo);
return $this->templateData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment