Add crud saving for rider

This commit is contained in:
Ramon Gutierrez 2018-01-16 06:21:45 +08:00
parent ed24ace9b6
commit 0bd009d74f
3 changed files with 45 additions and 93 deletions

View file

@ -37,7 +37,7 @@ class RiderController extends BaseController
// count total records
$tquery = $qb->select('COUNT(q)')
->join('q.hub', 'hub');
->leftJoin('q.hub', 'hub');
// add filters to count query
$this->setQueryFilters($datatable, $tquery);
@ -87,10 +87,10 @@ class RiderController extends BaseController
$rows = [];
foreach ($obj_rows as $orow) {
// add row data
$row['id'] = $orow->getID();
$row['first_name'] = $orow->getFirstName();
$row['last_name'] = $orow->getLastName();
$row['contact_num'] = $orow->getContactNumber();
$row['id'] = $orow[0]->getID();
$row['first_name'] = $orow[0]->getFirstName();
$row['last_name'] = $orow[0]->getLastName();
$row['contact_num'] = $orow[0]->getContactNumber();
$row['hub'] = $orow['hub_name'];
// add row metadata
@ -99,13 +99,6 @@ class RiderController extends BaseController
'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
if ($this->isGranted('rider.update'))
$row['meta']['update_url'] = $this->generateUrl('rider_update', ['id' => $row['id']]);
@ -148,61 +141,34 @@ class RiderController extends BaseController
$row = new Rider();
// set and save values
$row->setRidername($req->request->get('username'))
->setFirstName($req->request->get('first_name'))
$row->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name'))
->setEmail($req->request->get('email'))
->setContactNumber($req->request->get('contact_no'))
->setEnabled($req->request->get('enabled') ? true : false)
->clearRoles();
->setContactNumber($req->request->get('contact_no'));
// set roles
$roles = $req->request->get('roles');
// initialize error list
$error_array = [];
if (!empty($roles)) {
foreach ($roles as $role_id) {
// 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;
// custom validation for associations
$hub_id = $req->request->get('hub');
$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
$errors = $validator->validate($row);
// initialize error list
$error_array = [];
// add errors to list
foreach ($errors as $error) {
$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
if (!empty($error_array)) {
// return validation failure response
@ -237,12 +203,12 @@ class RiderController extends BaseController
if (empty($row))
throw $this->createNotFoundException('The item does not exist');
// get roles
$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['values'] = [];
// response
return $this->render('rider/form.html.twig', $params);
@ -261,56 +227,36 @@ class RiderController extends BaseController
throw $this->createNotFoundException('The item does not exist');
// set and save values
$row->setRidername($req->request->get('username'))
->setFirstName($req->request->get('first_name'))
$row->setFirstName($req->request->get('first_name'))
->setLastName($req->request->get('last_name'))
->setEmail($req->request->get('email'))
->setContactNumber($req->request->get('contact_no'))
->setEnabled($req->request->get('enabled') ? true : false)
->clearRoles();
->setContactNumber($req->request->get('contact_no'));
// set roles
$roles = $req->request->get('roles');
// initialize error list
$error_array = [];
if (!empty($roles)) {
foreach ($roles as $role_id) {
// check if role exists
$role = $em->getRepository(Role::class)->find($role_id);
// custom validation for associations
$hub_id = $req->request->get('hub');
if (!empty($role))
$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);
} else {
$row->clearHub();
}
// validate
$errors = $validator->validate($row);
// initialize error list
$error_array = [];
// add errors to list
foreach ($errors as $error) {
$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
if (!empty($error_array)) {
// return validation failure response

View file

@ -103,4 +103,10 @@ class Rider
{
return $this->hub;
}
public function clearHub()
{
$this->hub = null;
return $this;
}
}

View file

@ -24,7 +24,7 @@
<h3 class="m-portlet__head-text">
{% if mode == 'update' %}
Edit Rider
<small>{{ obj.getRidername() }}</small>
<small>{{ obj.getFirstName() ~ ' ' ~ obj.getLastName() }}</small>
{% else %}
New Rider
{% endif %}