Add crud saving for rider
This commit is contained in:
parent
ed24ace9b6
commit
0bd009d74f
3 changed files with 45 additions and 93 deletions
|
|
@ -37,7 +37,7 @@ class RiderController extends BaseController
|
||||||
|
|
||||||
// count total records
|
// count total records
|
||||||
$tquery = $qb->select('COUNT(q)')
|
$tquery = $qb->select('COUNT(q)')
|
||||||
->join('q.hub', 'hub');
|
->leftJoin('q.hub', 'hub');
|
||||||
|
|
||||||
// add filters to count query
|
// add filters to count query
|
||||||
$this->setQueryFilters($datatable, $tquery);
|
$this->setQueryFilters($datatable, $tquery);
|
||||||
|
|
@ -87,10 +87,10 @@ class RiderController extends BaseController
|
||||||
$rows = [];
|
$rows = [];
|
||||||
foreach ($obj_rows as $orow) {
|
foreach ($obj_rows as $orow) {
|
||||||
// add row data
|
// add row data
|
||||||
$row['id'] = $orow->getID();
|
$row['id'] = $orow[0]->getID();
|
||||||
$row['first_name'] = $orow->getFirstName();
|
$row['first_name'] = $orow[0]->getFirstName();
|
||||||
$row['last_name'] = $orow->getLastName();
|
$row['last_name'] = $orow[0]->getLastName();
|
||||||
$row['contact_num'] = $orow->getContactNumber();
|
$row['contact_num'] = $orow[0]->getContactNumber();
|
||||||
$row['hub'] = $orow['hub_name'];
|
$row['hub'] = $orow['hub_name'];
|
||||||
|
|
||||||
// add row metadata
|
// add row metadata
|
||||||
|
|
@ -99,13 +99,6 @@ class RiderController extends BaseController
|
||||||
'delete_url' => ''
|
'delete_url' => ''
|
||||||
];
|
];
|
||||||
|
|
||||||
// check if they have access to super admin users
|
|
||||||
if (!$this->isGranted('rider.role.sadmin') && $orow->isSuperAdmin())
|
|
||||||
{
|
|
||||||
$rows[] = $row;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// add crud urls
|
// add crud urls
|
||||||
if ($this->isGranted('rider.update'))
|
if ($this->isGranted('rider.update'))
|
||||||
$row['meta']['update_url'] = $this->generateUrl('rider_update', ['id' => $row['id']]);
|
$row['meta']['update_url'] = $this->generateUrl('rider_update', ['id' => $row['id']]);
|
||||||
|
|
@ -148,61 +141,34 @@ class RiderController extends BaseController
|
||||||
$row = new Rider();
|
$row = new Rider();
|
||||||
|
|
||||||
// set and save values
|
// set and save values
|
||||||
$row->setRidername($req->request->get('username'))
|
$row->setFirstName($req->request->get('first_name'))
|
||||||
->setFirstName($req->request->get('first_name'))
|
|
||||||
->setLastName($req->request->get('last_name'))
|
->setLastName($req->request->get('last_name'))
|
||||||
->setEmail($req->request->get('email'))
|
->setContactNumber($req->request->get('contact_no'));
|
||||||
->setContactNumber($req->request->get('contact_no'))
|
|
||||||
->setEnabled($req->request->get('enabled') ? true : false)
|
|
||||||
->clearRoles();
|
|
||||||
|
|
||||||
// set roles
|
// initialize error list
|
||||||
$roles = $req->request->get('roles');
|
$error_array = [];
|
||||||
|
|
||||||
if (!empty($roles)) {
|
// custom validation for associations
|
||||||
foreach ($roles as $role_id) {
|
$hub_id = $req->request->get('hub');
|
||||||
// check if role exists
|
|
||||||
$role = $em->getRepository(Role::class)->find($role_id);
|
|
||||||
if (!empty($role))
|
|
||||||
{
|
|
||||||
// check access to super user roles
|
|
||||||
if ($role->isSuperAdmin() && !$this->isGranted('rider.role.sadmin'))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
$row->addRole($role);
|
if ($hub_id) {
|
||||||
}
|
$hub = $em->getRepository(Hub::class)
|
||||||
}
|
->find($hub_id);
|
||||||
|
|
||||||
|
if (empty($hub))
|
||||||
|
$error_array['hub'] = 'Invalid hub selected.';
|
||||||
|
else
|
||||||
|
$row->setHub($hub);
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate
|
// validate
|
||||||
$errors = $validator->validate($row);
|
$errors = $validator->validate($row);
|
||||||
|
|
||||||
// initialize error list
|
|
||||||
$error_array = [];
|
|
||||||
|
|
||||||
// add errors to list
|
// add errors to list
|
||||||
foreach ($errors as $error) {
|
foreach ($errors as $error) {
|
||||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
// get password inputs
|
|
||||||
$password = $req->request->get('password');
|
|
||||||
$confirm_password = $req->request->get('confirm_password');
|
|
||||||
|
|
||||||
// custom validation for password fields
|
|
||||||
if (!$password) {
|
|
||||||
$error_array['password'] = 'This value should not be blank.';
|
|
||||||
} else if ($password != $confirm_password) {
|
|
||||||
$error_array['confirm_password'] = 'Passwords do not match.';
|
|
||||||
} else {
|
|
||||||
// encode password
|
|
||||||
$enc = $ef->getEncoder($row);
|
|
||||||
$encoded_password = $enc->encodePassword($req->request->get('password'), $row->getSalt());
|
|
||||||
|
|
||||||
// set password
|
|
||||||
$row->setPassword($encoded_password);
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if any errors were found
|
// check if any errors were found
|
||||||
if (!empty($error_array)) {
|
if (!empty($error_array)) {
|
||||||
// return validation failure response
|
// return validation failure response
|
||||||
|
|
@ -237,12 +203,12 @@ class RiderController extends BaseController
|
||||||
if (empty($row))
|
if (empty($row))
|
||||||
throw $this->createNotFoundException('The item does not exist');
|
throw $this->createNotFoundException('The item does not exist');
|
||||||
|
|
||||||
// get roles
|
|
||||||
$em = $this->getDoctrine()->getManager();
|
$em = $this->getDoctrine()->getManager();
|
||||||
$params['roles'] = $em->getRepository(Role::class)->findAll();
|
|
||||||
|
// get parent associations
|
||||||
|
$params['hubs'] = $em->getRepository(Hub::class)->findAll();
|
||||||
|
|
||||||
$params['obj'] = $row;
|
$params['obj'] = $row;
|
||||||
$params['values'] = [];
|
|
||||||
|
|
||||||
// response
|
// response
|
||||||
return $this->render('rider/form.html.twig', $params);
|
return $this->render('rider/form.html.twig', $params);
|
||||||
|
|
@ -261,56 +227,36 @@ class RiderController extends BaseController
|
||||||
throw $this->createNotFoundException('The item does not exist');
|
throw $this->createNotFoundException('The item does not exist');
|
||||||
|
|
||||||
// set and save values
|
// set and save values
|
||||||
$row->setRidername($req->request->get('username'))
|
$row->setFirstName($req->request->get('first_name'))
|
||||||
->setFirstName($req->request->get('first_name'))
|
|
||||||
->setLastName($req->request->get('last_name'))
|
->setLastName($req->request->get('last_name'))
|
||||||
->setEmail($req->request->get('email'))
|
->setContactNumber($req->request->get('contact_no'));
|
||||||
->setContactNumber($req->request->get('contact_no'))
|
|
||||||
->setEnabled($req->request->get('enabled') ? true : false)
|
|
||||||
->clearRoles();
|
|
||||||
|
|
||||||
// set roles
|
// initialize error list
|
||||||
$roles = $req->request->get('roles');
|
$error_array = [];
|
||||||
|
|
||||||
if (!empty($roles)) {
|
// custom validation for associations
|
||||||
foreach ($roles as $role_id) {
|
$hub_id = $req->request->get('hub');
|
||||||
// check if role exists
|
|
||||||
$role = $em->getRepository(Role::class)->find($role_id);
|
|
||||||
|
|
||||||
if (!empty($role))
|
if ($hub_id) {
|
||||||
$row->addRole($role);
|
$hub = $em->getRepository(Hub::class)
|
||||||
}
|
->find($hub_id);
|
||||||
|
|
||||||
|
if (empty($hub))
|
||||||
|
$error_array['hub'] = 'Invalid hub selected.';
|
||||||
|
else
|
||||||
|
$row->setHub($hub);
|
||||||
|
} else {
|
||||||
|
$row->clearHub();
|
||||||
}
|
}
|
||||||
|
|
||||||
// validate
|
// validate
|
||||||
$errors = $validator->validate($row);
|
$errors = $validator->validate($row);
|
||||||
|
|
||||||
// initialize error list
|
|
||||||
$error_array = [];
|
|
||||||
|
|
||||||
// add errors to list
|
// add errors to list
|
||||||
foreach ($errors as $error) {
|
foreach ($errors as $error) {
|
||||||
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
$error_array[$error->getPropertyPath()] = $error->getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
// get password inputs
|
|
||||||
$password = $req->request->get('password');
|
|
||||||
$confirm_password = $req->request->get('confirm_password');
|
|
||||||
|
|
||||||
// custom validation for password fields
|
|
||||||
if ($password || $confirm_password) {
|
|
||||||
if ($password != $confirm_password) {
|
|
||||||
$error_array['confirm_password'] = 'Passwords do not match.';
|
|
||||||
} else {
|
|
||||||
// encode password
|
|
||||||
$enc = $ef->getEncoder($row);
|
|
||||||
$encoded_password = $enc->encodePassword($req->request->get('password'), $row->getSalt());
|
|
||||||
|
|
||||||
// set password
|
|
||||||
$row->setPassword($encoded_password);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if any errors were found
|
// check if any errors were found
|
||||||
if (!empty($error_array)) {
|
if (!empty($error_array)) {
|
||||||
// return validation failure response
|
// return validation failure response
|
||||||
|
|
|
||||||
|
|
@ -103,4 +103,10 @@ class Rider
|
||||||
{
|
{
|
||||||
return $this->hub;
|
return $this->hub;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function clearHub()
|
||||||
|
{
|
||||||
|
$this->hub = null;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,7 +24,7 @@
|
||||||
<h3 class="m-portlet__head-text">
|
<h3 class="m-portlet__head-text">
|
||||||
{% if mode == 'update' %}
|
{% if mode == 'update' %}
|
||||||
Edit Rider
|
Edit Rider
|
||||||
<small>{{ obj.getRidername() }}</small>
|
<small>{{ obj.getFirstName() ~ ' ' ~ obj.getLastName() }}</small>
|
||||||
{% else %}
|
{% else %}
|
||||||
New Rider
|
New Rider
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue