Finish adding rest of CRUD functions
This commit is contained in:
parent
c90bf25b10
commit
0adbfbd861
5 changed files with 133 additions and 15 deletions
|
|
@ -308,6 +308,6 @@ catalyst_menu:
|
|||
|
||||
- id: competitor
|
||||
acl: competitor.menu
|
||||
label: 'Competitors'
|
||||
label: Competitors
|
||||
icon: fa fa-battery-0 #hehe
|
||||
order: 17
|
||||
order: 1
|
||||
|
|
@ -15,4 +15,19 @@ competitor_create:
|
|||
competitor_create_submit:
|
||||
path: /competitors/create
|
||||
controller: App\Controller\CompetitorController::addSubmit
|
||||
methods: [POST]
|
||||
methods: [POST]
|
||||
|
||||
competitor_update:
|
||||
path: /competitors/update/{id}
|
||||
controller: App\Controller\CompetitorController::updateForm
|
||||
methods: [GET]
|
||||
|
||||
competitor_update_submit:
|
||||
path: /competitors/update/{id}
|
||||
controller: App\Controller\CompetitorController::updateSubmit
|
||||
methods: [POST]
|
||||
|
||||
competitor_delete:
|
||||
path: /competitors/{id}
|
||||
controller: App\Controller\CompetitorController::destroy
|
||||
methods: [DELETE]
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ namespace App\Controller;
|
|||
|
||||
use App\Entity\Competitor;
|
||||
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
|
|
@ -19,7 +20,7 @@ class CompetitorController extends Controller
|
|||
{
|
||||
|
||||
/**
|
||||
* @Menu(selected="competitor_list")
|
||||
* @Menu(selected="competitor")
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
|
|
@ -95,7 +96,12 @@ class CompetitorController extends Controller
|
|||
$row['branch_name'] = $orow->getBranchName();
|
||||
$row['brand'] = $orow->getBrand();
|
||||
$row['address'] = $orow->getAddress();
|
||||
$row['is_near_partner'] = $orow->getIsNearPartner();
|
||||
if ($orow->getIsNearPartner()){
|
||||
$row['is_near_partner'] = "Yes";
|
||||
}
|
||||
else{
|
||||
$row['is_near_partner'] = "No";
|
||||
}
|
||||
|
||||
// add row metadata
|
||||
$row['meta'] = [
|
||||
|
|
@ -105,9 +111,9 @@ class CompetitorController extends Controller
|
|||
|
||||
// add crud urls
|
||||
if ($this->isGranted('competitor.update'))
|
||||
$row['meta']['update_url'] = $this->generateUrl('home', ['id' => $row['id']]);
|
||||
$row['meta']['update_url'] = $this->generateUrl('competitor_update', ['id' => $row['id']]);
|
||||
if ($this->isGranted('competitor.delete'))
|
||||
$row['meta']['delete_url'] = $this->generateUrl('home', ['id' => $row['id']]);
|
||||
$row['meta']['delete_url'] = $this->generateUrl('competitor_delete', ['id' => $row['id']]);
|
||||
|
||||
$rows[] = $row;
|
||||
}
|
||||
|
|
@ -119,6 +125,9 @@ class CompetitorController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Menu(selected="competitor")
|
||||
*/
|
||||
public function addForm()
|
||||
{
|
||||
$this->denyAccessUnlessGranted('competitor.add', null, 'No access.');
|
||||
|
|
@ -173,6 +182,95 @@ class CompetitorController extends Controller
|
|||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Menu(selected="competitor")
|
||||
*/
|
||||
public function updateForm($id)
|
||||
|
||||
{
|
||||
// $id = 14;
|
||||
$this->denyAccessUnlessGranted('competitor.update', null, 'No access.');
|
||||
|
||||
$params['mode'] = 'update';
|
||||
|
||||
// get row data
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$row = $em->getRepository(Competitor::class)->find($id);
|
||||
|
||||
// make sure this row exists
|
||||
if (empty($row))
|
||||
throw $this->createNotFoundException('The item does not exist');
|
||||
|
||||
$params['obj'] = $row;
|
||||
|
||||
// response
|
||||
return $this->render('competitor/form.html.twig', $params);
|
||||
}
|
||||
|
||||
public function updateSubmit(Request $req, ValidatorInterface $validator, $id)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('competitor.update', null, 'No access.');
|
||||
|
||||
// get object data
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$obj = $em->getRepository(Competitor::class)->find($id);
|
||||
|
||||
// make sure this object exists
|
||||
if (empty($obj))
|
||||
throw $this->createNotFoundException('The item does not exist');
|
||||
|
||||
$this->setObject($obj, $em, $req);
|
||||
|
||||
// validate
|
||||
$errors = $validator->validate($obj);
|
||||
|
||||
// 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!'
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$this->denyAccessUnlessGranted('competitor.delete', null, 'No access.');
|
||||
|
||||
// get object data
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$obj = $em->getRepository(Competitor::class)->find($id);
|
||||
|
||||
if (empty($obj))
|
||||
throw $this->createNotFoundException('The item does not exist');
|
||||
|
||||
// delete this object
|
||||
$em->remove($obj);
|
||||
$em->flush();
|
||||
|
||||
// response
|
||||
$response = new Response();
|
||||
$response->setStatusCode(Response::HTTP_OK);
|
||||
$response->send();
|
||||
}
|
||||
|
||||
protected function setQueryFilters($datatable, QueryBuilder $query)
|
||||
{
|
||||
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
<h3 class="m-portlet__head-text">
|
||||
{% if mode == 'update' %}
|
||||
Edit Competitor
|
||||
<small>{{ obj.getName() }}</small>
|
||||
<small>{{ obj.getBranchName() }}</small>
|
||||
{% else %}
|
||||
New Competitor
|
||||
{% endif %}
|
||||
|
|
@ -66,7 +66,7 @@
|
|||
<label for="name" data-field="name">
|
||||
Address / Location
|
||||
</label>
|
||||
<input type="text" name="address" class="form-control m-input" value="{{ obj.getBranchName()|default('') }}">
|
||||
<input type="text" name="address" class="form-control m-input" value="{{ obj.getAddress()|default('') }}">
|
||||
<div class="form-control-feedback hide" data-field="name"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -94,7 +94,8 @@
|
|||
},
|
||||
{
|
||||
field: 'is_near_partner',
|
||||
title: 'Near a Partner?'
|
||||
title: 'Near a Partner?',
|
||||
sortable: false
|
||||
},
|
||||
{
|
||||
field: 'Actions',
|
||||
|
|
@ -106,11 +107,13 @@
|
|||
var actions = '';
|
||||
|
||||
if (row.meta.update_url != '') {
|
||||
actions += '<a href="' + row.meta.update_url + '" class="m-portlet__nav-link btn m-btn m-btn--hover-accent m-btn--icon m-btn--icon-only m-btn--pill btn-edit" data-id="' + row.name + '" title="Edit"><i class="la la-edit"></i></a>';
|
||||
console.log(row.branch_name,"hi");
|
||||
console.log(row);
|
||||
actions += '<a href="' + row.meta.update_url + '" class="m-portlet__nav-link btn m-btn m-btn--hover-accent m-btn--icon m-btn--icon-only m-btn--pill btn-edit" data-id="' + row.branch_name + '" title="Edit"><i class="la la-edit"></i></a>';
|
||||
}
|
||||
|
||||
if (row.meta.delete_url != '') {
|
||||
actions += '<a href="' + row.meta.delete_url + '" class="m-portlet__nav-link btn m-btn m-btn--hover-danger m-btn--icon m-btn--icon-only m-btn--pill btn-delete" data-id="' + row.name + '" title="Delete"><i class="la la-trash"></i></a>';
|
||||
actions += '<a href="' + row.meta.delete_url + '" class="m-portlet__nav-link btn m-btn m-btn--hover-danger m-btn--icon m-btn--icon-only m-btn--pill btn-delete" data-id="' + row.branch_name + '" title="Delete"><i class="la la-trash"></i></a>';
|
||||
}
|
||||
|
||||
return actions;
|
||||
|
|
@ -127,11 +130,13 @@
|
|||
|
||||
var table = $("#data-rows").mDatatable(options);
|
||||
|
||||
console.log(table);
|
||||
|
||||
$(document).on('click', '.btn-delete', function(e) {
|
||||
var url = $(this).prop('href');
|
||||
var id = $(this).data('id');
|
||||
var btn = $(this);
|
||||
var id = $(this).data('id');
|
||||
var btn = $(this);
|
||||
|
||||
console.log($(this).data(),"whee");
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue