Add basic crud operations to roles
This commit is contained in:
parent
3e1d97e5ff
commit
aaeb6df92d
4 changed files with 195 additions and 46 deletions
|
|
@ -35,20 +35,22 @@ role_rows:
|
|||
|
||||
role_create:
|
||||
path: /roles/create
|
||||
controller: App\Controller\RowController::create
|
||||
controller: App\Controller\RoleController::create
|
||||
methods: [GET]
|
||||
|
||||
role_create_submit:
|
||||
path: /roles/create
|
||||
controller: App\Controller\RowController::createSubmit
|
||||
methods: [PUT]
|
||||
controller: App\Controller\RoleController::createSubmit
|
||||
methods: [POST]
|
||||
|
||||
role_update:
|
||||
path: /roles/{id}
|
||||
controller: App\Controller\RowController::update
|
||||
controller: App\Controller\RoleController::update
|
||||
methods: [GET]
|
||||
|
||||
role_update_submit:
|
||||
path: /roles/{id}
|
||||
controller: App\Controller\RowController::updateSubmit
|
||||
controller: App\Controller\RoleController::updateSubmit
|
||||
methods: [POST]
|
||||
|
||||
role_delete:
|
||||
|
|
@ -61,7 +63,3 @@ role_delete:
|
|||
test_acl:
|
||||
path: /test_acl
|
||||
controller: App\Controller\TestController::index
|
||||
|
||||
test_is_granted:
|
||||
path: /test_is_granted
|
||||
controller: App\Controller\TestController::testIsGranted
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ use App\Ramcar\BaseController;
|
|||
use App\Entity\Role;
|
||||
|
||||
use Doctrine\ORM\Query;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class RoleController extends BaseController
|
||||
{
|
||||
|
|
@ -21,6 +23,7 @@ class RoleController extends BaseController
|
|||
// get all rows
|
||||
$rows = $qb->getResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
// response
|
||||
return $this->render('role/list.html.twig', $params);
|
||||
}
|
||||
|
||||
|
|
@ -35,6 +38,13 @@ class RoleController extends BaseController
|
|||
// get all rows
|
||||
$rows = $qb->getResult(Query::HYDRATE_ARRAY);
|
||||
|
||||
// add crud urls
|
||||
foreach ($rows as $index => $row) {
|
||||
$rows[$index]['update_url'] = $this->generateUrl('role_update', ['id' => $row['id']]);
|
||||
$rows[$index]['delete_url'] = $this->generateUrl('role_delete', ['id' => $row['id']]);
|
||||
}
|
||||
|
||||
// response
|
||||
return $this->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
|
|
@ -42,12 +52,31 @@ class RoleController extends BaseController
|
|||
{
|
||||
$params = $this->initParameters('role_list');
|
||||
|
||||
// response
|
||||
return $this->render('role/form.html.twig', $params);
|
||||
}
|
||||
|
||||
public function createSubmit()
|
||||
public function createSubmit(Request $req)
|
||||
{
|
||||
// create new row
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$row = new Role();
|
||||
|
||||
// set and save values
|
||||
$row->setID($req->request->get('id'))
|
||||
->setName($req->request->get('name'));
|
||||
|
||||
$em->persist($row);
|
||||
$em->flush();
|
||||
|
||||
// set success
|
||||
$this->addFlash(
|
||||
'success',
|
||||
'Changes have been saved!'
|
||||
);
|
||||
|
||||
// response
|
||||
return $this->redirectToRoute('role_list');
|
||||
}
|
||||
|
||||
public function update($id)
|
||||
|
|
@ -55,21 +84,42 @@ class RoleController extends BaseController
|
|||
$params = $this->initParameters('role_list');
|
||||
|
||||
// get row data
|
||||
$repo = $this->getDoctrine()->getRepository(Role::class);
|
||||
$row = $repo->find($id);
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$row = $em->getRepository(Role::class)->find($id);
|
||||
|
||||
if (!empty($row)) {
|
||||
$params['row'] = $row;
|
||||
} else {
|
||||
if (empty($row))
|
||||
throw $this->createNotFoundException('The item does not exist');
|
||||
}
|
||||
|
||||
$params['row'] = $row;
|
||||
$params['values'] = [];
|
||||
|
||||
// response
|
||||
return $this->render('role/form.html.twig', $params);
|
||||
}
|
||||
|
||||
public function updateSubmit($id)
|
||||
public function updateSubmit(Request $req, $id)
|
||||
{
|
||||
// get row data
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$row = $em->getRepository(Role::class)->find($id);
|
||||
|
||||
if (empty($row))
|
||||
throw $this->createNotFoundException('The item does not exist');
|
||||
|
||||
// set and save values
|
||||
$row->setID($req->request->get('id'))
|
||||
->setName($req->request->get('name'));
|
||||
|
||||
$em->flush();
|
||||
|
||||
// set success
|
||||
$this->addFlash(
|
||||
'success',
|
||||
'Changes have been saved!'
|
||||
);
|
||||
|
||||
// response
|
||||
return $this->redirectToRoute('role_list');
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
|
|
@ -80,12 +130,16 @@ class RoleController extends BaseController
|
|||
$em = $this->getDoctrine()->getManager();
|
||||
$row = $em->getRepository(Role::class)->find($id);
|
||||
|
||||
if (!empty($row)) {
|
||||
// delete this row
|
||||
$em->remove($row);
|
||||
$em->flush();
|
||||
} else {
|
||||
if (empty($row))
|
||||
throw $this->createNotFoundException('The item does not exist');
|
||||
}
|
||||
|
||||
// delete this row
|
||||
$em->remove($row);
|
||||
$em->flush();
|
||||
|
||||
// response
|
||||
$response = new Response();
|
||||
$response->setStatusCode(Response::HTTP_OK);
|
||||
$response->send();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
79
templates/role/form.html.twig
Normal file
79
templates/role/form.html.twig
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
{% extends 'base.html.twig' %}
|
||||
|
||||
{% block body %}
|
||||
<!-- BEGIN: Subheader -->
|
||||
<div class="m-subheader">
|
||||
<div class="d-flex align-items-center">
|
||||
<div class="mr-auto">
|
||||
<h3 class="m-subheader__title">Roles</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- END: Subheader -->
|
||||
<div class="m-content">
|
||||
<!--Begin::Section-->
|
||||
<div class="row">
|
||||
<div class="col-xl-6 offset-xl-3">
|
||||
<div class="m-portlet m-portlet--mobile">
|
||||
<div class="m-portlet__head">
|
||||
<div class="m-portlet__head-caption">
|
||||
<div class="m-portlet__head-title">
|
||||
<span class="m-portlet__head-icon">
|
||||
<i class="la la-key"></i>
|
||||
</span>
|
||||
<span class="m-portlet__head-icon m--hide">
|
||||
<i class="la la-gear"></i>
|
||||
</span>
|
||||
<h3 class="m-portlet__head-text">
|
||||
{% if row is defined %}
|
||||
Edit Role
|
||||
<small>{{ row.getId() }}</small>
|
||||
{% else %}
|
||||
New Role
|
||||
{% endif %}
|
||||
</h3>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<form class="m-form" method="post" action="{{ row is defined ? url('role_update_submit', {'id': row.getId()}) : url('role_create_submit') }}">
|
||||
<div class="m-portlet__body">
|
||||
<div class="m-form__section m-form__section--first">
|
||||
<div class="form-group m-form__group row{{ error.id is defined ? ' has-danger' }}">
|
||||
<label class="col-lg-3 col-form-label">Role ID:</label>
|
||||
<div class="col-lg-6">
|
||||
<input type="text" name="id" class="form-control m-input{{ error.id is defined ? ' form-control-danger' }}" value="{{ values.id is defined ? values.id : (row is defined ? row.getId()) }}">
|
||||
{% if error.id is defined %}
|
||||
<div class="form-control-feedback">{{ error.id }}</div>
|
||||
{% endif %}
|
||||
<span class="m-form__help">Unique identifier for this role</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group m-form__group row{{ error.name is defined ? ' has-danger' }}">
|
||||
<label class="col-lg-3 col-form-label">Name:</label>
|
||||
<div class="col-lg-6">
|
||||
<input type="text" name="name" class="form-control m-input{{ error.id is defined ? ' form-control-danger' }}" value="{{ values.name is defined ? values.name : (row is defined ? row.getName()) }}">
|
||||
{% if error.name is defined %}
|
||||
<div class="form-control-feedback">{{ error.name }}</div>
|
||||
{% endif %}
|
||||
<span class="m-form__help">Display name for this role</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="m-portlet__foot m-portlet__foot--fit">
|
||||
<div class="m-form__actions m-form__actions">
|
||||
<div class="row">
|
||||
<div class="col-lg-3"></div>
|
||||
<div class="col-lg-6">
|
||||
<button type="submit" class="btn btn-success">Submit</button>
|
||||
<a href="{{ url('role_list') }}" class="btn btn-secondary">Cancel</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
|
|
@ -18,6 +18,12 @@
|
|||
<div class="col-xl-12">
|
||||
<div class="m-portlet m-portlet--mobile">
|
||||
<div class="m-portlet__body">
|
||||
{% for message in app.flashes('success') %}
|
||||
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||
<button type="button" class="close" data-dismiss="alert" aria-label="Close"></button>
|
||||
<strong>Success!</strong> {{ message }}
|
||||
</div>
|
||||
{% endfor %}
|
||||
<div class="m-form m-form--label-align-right m--margin-top-20 m--margin-bottom-30">
|
||||
<div class="row align-items-center">
|
||||
<div class="col-xl-8 order-2 order-xl-1">
|
||||
|
|
@ -33,7 +39,7 @@
|
|||
</div>
|
||||
</div>
|
||||
<div class="col-xl-4 order-1 order-xl-2 m--align-right">
|
||||
<a href="#" class="btn btn-focus m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">
|
||||
<a href="{{ url('role_create') }}" class="btn btn-focus m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">
|
||||
<span>
|
||||
<i class="la la-key"></i>
|
||||
<span>New Role</span>
|
||||
|
|
@ -56,25 +62,6 @@
|
|||
{% block scripts %}
|
||||
<script>
|
||||
$(function() {
|
||||
$(document).on('click', '.btn-delete', function() {
|
||||
var id = $(this).data('id');
|
||||
|
||||
swal({
|
||||
title: 'Confirmation',
|
||||
text: 'Are you sure you want to delete ' + id + '?',
|
||||
type: 'warning',
|
||||
showCancelButton: true
|
||||
}).then((result) => {
|
||||
if (result.value) {
|
||||
swal(
|
||||
'Deleted!',
|
||||
'Your imaginary file has been deleted.',
|
||||
'success'
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var options = {
|
||||
data: {
|
||||
type: 'remote',
|
||||
|
|
@ -101,10 +88,10 @@
|
|||
sortable: false,
|
||||
overflow: 'visible',
|
||||
template: function (row, index, datatable) {
|
||||
var actions = '<a href="#" 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.id + '" title="Edit"><i class="la la-edit"></i></a>';
|
||||
var actions = '<a href="' + row.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.id + '" title="Edit"><i class="la la-edit"></i></a>';
|
||||
|
||||
if (row.id != 'ROLE_SUPER_ADMIN') {
|
||||
actions += '<a href="#" 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.id + '" title="Delete"><i class="la la-trash"></i></a>';
|
||||
actions += '<a href="' + row.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.id + '" title="Delete"><i class="la la-trash"></i></a>';
|
||||
}
|
||||
|
||||
return actions;
|
||||
|
|
@ -123,7 +110,38 @@
|
|||
*/
|
||||
};
|
||||
|
||||
$("#data-rows").mDatatable(options);
|
||||
var table = $("#data-rows").mDatatable(options);
|
||||
|
||||
$(document).on('click', '.btn-delete', function(e) {
|
||||
var url = $(this).prop('href');
|
||||
var id = $(this).data('id');
|
||||
var btn = $(this);
|
||||
|
||||
e.preventDefault();
|
||||
|
||||
swal({
|
||||
title: 'Confirmation',
|
||||
text: 'Are you sure you want to delete ' + id + '?',
|
||||
type: 'warning',
|
||||
showCancelButton: true
|
||||
}).then((result) => {
|
||||
if (result.value) {
|
||||
$.ajax({
|
||||
method: "DELETE",
|
||||
url: url
|
||||
}).done(function(response) {
|
||||
table.row(btn.parents('tr')).remove();
|
||||
table.reload();
|
||||
}).fail(function() {
|
||||
swal({
|
||||
title: 'Whoops',
|
||||
text: 'An error occurred while deleting this item. Please contact support.',
|
||||
type: 'error'
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
Loading…
Reference in a new issue