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
|
- id: competitor
|
||||||
acl: competitor.menu
|
acl: competitor.menu
|
||||||
label: 'Competitors'
|
label: Competitors
|
||||||
icon: fa fa-battery-0 #hehe
|
icon: fa fa-battery-0 #hehe
|
||||||
order: 17
|
order: 1
|
||||||
|
|
@ -16,3 +16,18 @@ competitor_create_submit:
|
||||||
path: /competitors/create
|
path: /competitors/create
|
||||||
controller: App\Controller\CompetitorController::addSubmit
|
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 App\Entity\Competitor;
|
||||||
|
|
||||||
|
|
||||||
use Doctrine\ORM\Query;
|
use Doctrine\ORM\Query;
|
||||||
use Doctrine\ORM\QueryBuilder;
|
use Doctrine\ORM\QueryBuilder;
|
||||||
use Doctrine\ORM\EntityManager;
|
use Doctrine\ORM\EntityManager;
|
||||||
|
|
@ -19,7 +20,7 @@ class CompetitorController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Menu(selected="competitor_list")
|
* @Menu(selected="competitor")
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
|
|
@ -95,7 +96,12 @@ class CompetitorController extends Controller
|
||||||
$row['branch_name'] = $orow->getBranchName();
|
$row['branch_name'] = $orow->getBranchName();
|
||||||
$row['brand'] = $orow->getBrand();
|
$row['brand'] = $orow->getBrand();
|
||||||
$row['address'] = $orow->getAddress();
|
$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
|
// add row metadata
|
||||||
$row['meta'] = [
|
$row['meta'] = [
|
||||||
|
|
@ -105,9 +111,9 @@ class CompetitorController extends Controller
|
||||||
|
|
||||||
// add crud urls
|
// add crud urls
|
||||||
if ($this->isGranted('competitor.update'))
|
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'))
|
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;
|
$rows[] = $row;
|
||||||
}
|
}
|
||||||
|
|
@ -119,6 +125,9 @@ class CompetitorController extends Controller
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Menu(selected="competitor")
|
||||||
|
*/
|
||||||
public function addForm()
|
public function addForm()
|
||||||
{
|
{
|
||||||
$this->denyAccessUnlessGranted('competitor.add', null, 'No access.');
|
$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)
|
protected function setQueryFilters($datatable, QueryBuilder $query)
|
||||||
{
|
{
|
||||||
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
<h3 class="m-portlet__head-text">
|
<h3 class="m-portlet__head-text">
|
||||||
{% if mode == 'update' %}
|
{% if mode == 'update' %}
|
||||||
Edit Competitor
|
Edit Competitor
|
||||||
<small>{{ obj.getName() }}</small>
|
<small>{{ obj.getBranchName() }}</small>
|
||||||
{% else %}
|
{% else %}
|
||||||
New Competitor
|
New Competitor
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
@ -66,7 +66,7 @@
|
||||||
<label for="name" data-field="name">
|
<label for="name" data-field="name">
|
||||||
Address / Location
|
Address / Location
|
||||||
</label>
|
</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 class="form-control-feedback hide" data-field="name"></div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -94,7 +94,8 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'is_near_partner',
|
field: 'is_near_partner',
|
||||||
title: 'Near a Partner?'
|
title: 'Near a Partner?',
|
||||||
|
sortable: false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
field: 'Actions',
|
field: 'Actions',
|
||||||
|
|
@ -106,11 +107,13 @@
|
||||||
var actions = '';
|
var actions = '';
|
||||||
|
|
||||||
if (row.meta.update_url != '') {
|
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 != '') {
|
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;
|
return actions;
|
||||||
|
|
@ -127,11 +130,13 @@
|
||||||
|
|
||||||
var table = $("#data-rows").mDatatable(options);
|
var table = $("#data-rows").mDatatable(options);
|
||||||
|
|
||||||
console.log(table);
|
|
||||||
$(document).on('click', '.btn-delete', function(e) {
|
$(document).on('click', '.btn-delete', function(e) {
|
||||||
var url = $(this).prop('href');
|
var url = $(this).prop('href');
|
||||||
var id = $(this).data('id');
|
var id = $(this).data('id');
|
||||||
var btn = $(this);
|
var btn = $(this);
|
||||||
|
|
||||||
|
console.log($(this).data(),"whee");
|
||||||
|
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue