252 lines
7.4 KiB
PHP
252 lines
7.4 KiB
PHP
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use Doctrine\ORM\Query;
|
|
use Doctrine\ORM\QueryBuilder;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
|
|
|
|
use Catalyst\MenuBundle\Annotation\Menu;
|
|
|
|
use App\Entity\ServiceOffering;
|
|
|
|
class ServiceOfferingController extends Controller
|
|
{
|
|
/**
|
|
* @Menu(selected="service_offering_list")
|
|
* @IsGranted("service_offering.list")
|
|
*/
|
|
public function index()
|
|
{
|
|
return $this->render('service-offering/list.html.twig');
|
|
}
|
|
|
|
/**
|
|
* @IsGranted("service_offering.list")
|
|
*/
|
|
public function datatableRows(Request $req)
|
|
{
|
|
// get query builder
|
|
$qb = $this->getDoctrine()
|
|
->getRepository(ServiceOffering::class)
|
|
->createQueryBuilder('q');
|
|
|
|
// get datatable params
|
|
$datatable = $req->request->get('datatable');
|
|
|
|
// count total records
|
|
$tquery = $qb->select('COUNT(q)');
|
|
$this->setQueryFilters($datatable, $tquery);
|
|
$total = $tquery->getQuery()
|
|
->getSingleScalarResult();
|
|
|
|
// get current page number
|
|
$page = $datatable['pagination']['page'] ?? 1;
|
|
|
|
$perpage = $datatable['pagination']['perpage'];
|
|
$offset = ($page - 1) * $perpage;
|
|
|
|
// add metadata
|
|
$meta = [
|
|
'page' => $page,
|
|
'perpage' => $perpage,
|
|
'pages' => ceil($total / $perpage),
|
|
'total' => $total,
|
|
'sort' => 'asc',
|
|
'field' => 'id'
|
|
];
|
|
|
|
// build query
|
|
$query = $qb->select('q');
|
|
$this->setQueryFilters($datatable, $query);
|
|
|
|
// check if sorting is present, otherwise use default
|
|
if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) {
|
|
$order = $datatable['sort']['sort'] ?? 'asc';
|
|
$query->orderBy('q.' . $datatable['sort']['field'], $order);
|
|
} else {
|
|
$query->orderBy('q.id', 'asc');
|
|
}
|
|
|
|
// get rows for this page
|
|
$obj_rows = $query->setFirstResult($offset)
|
|
->setMaxResults($perpage)
|
|
->getQuery()
|
|
->getResult();
|
|
|
|
// process rows
|
|
$rows = [];
|
|
foreach ($obj_rows as $orow) {
|
|
// add row data
|
|
$row['id'] = $orow->getID();
|
|
$row['name'] = $orow->getName();
|
|
$row['fee'] = $orow->getFee();
|
|
|
|
// add row metadata
|
|
$row['meta'] = [
|
|
'update_url' => '',
|
|
'delete_url' => ''
|
|
];
|
|
|
|
// add crud urls
|
|
if ($this->isGranted('service_offering.update'))
|
|
$row['meta']['update_url'] = $this->generateUrl('service_offering_update_form', ['id' => $row['id']]);
|
|
if ($this->isGranted('service_offering.delete'))
|
|
$row['meta']['delete_url'] = $this->generateUrl('service_offering_delete', ['id' => $row['id']]);
|
|
|
|
$rows[] = $row;
|
|
}
|
|
|
|
// response
|
|
return $this->json([
|
|
'meta' => $meta,
|
|
'data' => $rows
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @Menu(selected="service_offering.list")
|
|
* @IsGranted("service_offering.add")
|
|
*/
|
|
public function addForm()
|
|
{
|
|
$service_offering = new ServiceOffering();
|
|
$params = [
|
|
'service_offering' => $service_offering,
|
|
'mode' => 'create',
|
|
];
|
|
|
|
// response
|
|
return $this->render('service-offering/form.html.twig', $params);
|
|
}
|
|
|
|
/**
|
|
* @IsGranted("service_offering.add")
|
|
*/
|
|
public function addSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator)
|
|
{
|
|
$service_offering = new ServiceOffering();
|
|
|
|
$this->setObject($service_offering, $req);
|
|
|
|
// validate
|
|
$errors = $validator->validate($service_offering);
|
|
|
|
// initialize error list
|
|
$error_array = [];
|
|
|
|
// add errors to list
|
|
foreach ($errors as $error) {
|
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
|
}
|
|
|
|
// check if any errors were found
|
|
if (!empty($error_array)) {
|
|
// return validation failure response
|
|
return $this->json([
|
|
'success' => false,
|
|
'errors' => $error_array
|
|
], 422);
|
|
}
|
|
|
|
// validated! save the entity
|
|
$em->persist($service_offering);
|
|
$em->flush();
|
|
|
|
// return successful response
|
|
return $this->json([
|
|
'success' => 'Changes have been saved!'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @Menu(selected="service_offering_list")
|
|
* @ParamConverter("service_offering", class="App\Entity\ServiceOffering")
|
|
* @IsGranted("service_offering.update")
|
|
*/
|
|
public function updateForm($id, EntityManagerInterface $em, ServiceOffering $service_offering)
|
|
{
|
|
$params = [];
|
|
$params['service_offering'] = $service_offering;
|
|
$params['mode'] = 'update';
|
|
|
|
// response
|
|
return $this->render('service-offering/form.html.twig', $params);
|
|
}
|
|
|
|
/**
|
|
* @ParamConverter("service_offering", class="App\Entity\ServiceOffering")
|
|
* @IsGranted("service_offering.update")
|
|
*/
|
|
public function updateSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator, ServiceOffering $service_offering)
|
|
{
|
|
$this->setObject($service_offering, $req);
|
|
|
|
// validate
|
|
$errors = $validator->validate($service_offering);
|
|
|
|
// initialize error list
|
|
$error_array = [];
|
|
|
|
// add errors to list
|
|
foreach ($errors as $error) {
|
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
|
}
|
|
|
|
// check if any errors were found
|
|
if (!empty($error_array)) {
|
|
// return validation failure response
|
|
return $this->json([
|
|
'success' => false,
|
|
'errors' => $error_array
|
|
], 422);
|
|
}
|
|
|
|
// validated! save the entity
|
|
$em->flush();
|
|
|
|
// return successful response
|
|
return $this->json([
|
|
'success' => 'Changes have been saved!'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @ParamConverter("service_offering", class="App\Entity\ServiceOffering")
|
|
* @IsGranted("service_offering.delete")
|
|
*/
|
|
public function deleteSubmit(EntityManagerInterface $em, ServiceOffering $service_offering)
|
|
{
|
|
// delete this object
|
|
$em->remove($service_offering);
|
|
$em->flush();
|
|
|
|
// response
|
|
$response = new Response();
|
|
$response->setStatusCode(Response::HTTP_OK);
|
|
$response->send();
|
|
}
|
|
|
|
protected function setObject(ServiceOffering $obj, Request $req)
|
|
{
|
|
// set and save values
|
|
$obj->setName($req->request->get('name'))
|
|
->setCode($req->request->get('code'))
|
|
->setFee($req->request->get('fee'));
|
|
}
|
|
|
|
protected function setQueryFilters($datatable, QueryBuilder $query)
|
|
{
|
|
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
|
$query->where('q.name LIKE :filter')
|
|
->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%');
|
|
}
|
|
}
|
|
}
|