Fix outlet add new

This commit is contained in:
Kendrick Chan 2018-01-14 02:46:08 +08:00
parent 4fd997ce1c
commit a83e3dee5b
8 changed files with 578 additions and 264 deletions

View file

@ -11,17 +11,17 @@ outlet_rows:
outlet_create:
path: /outlets/create
controller: App\Controller\OutletController::create
controller: App\Controller\OutletController::addForm
methods: [GET]
outlet_create_submit:
path: /outlets/create
controller: App\Controller\OutletController::createSubmit
controller: App\Controller\OutletController::addSubmit
methods: [POST]
outlet_update:
path: /outlets/{id}
controller: App\Controller\OutletController::update
controller: App\Controller\OutletController::updateForm
methods: [GET]
outlet_update_submit:

View file

@ -11,6 +11,9 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use DateTime;
class OutletController extends BaseController
{
public function index()
@ -125,57 +128,49 @@ class OutletController extends BaseController
]);
}
public function create()
public function addForm()
{
$this->denyAccessUnlessGranted('outlet.add', null, 'No access.');
$params = $this->initParameters('outlet_list');
// get roles
$em = $this->getDoctrine()->getManager();
$params['roles'] = $em->getRepository(Role::class)->findAll();
$params['obj'] = new Outlet();
$params['mode'] = 'create';
// response
return $this->render('outlet/form.html.twig', $params);
}
public function createSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator)
public function addSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator)
{
$this->denyAccessUnlessGranted('outlet.add', null, 'No access.');
// create new row
error_log($req->request->get('time_open'));
// create new object
$em = $this->getDoctrine()->getManager();
$row = new Outlet();
$obj = new Outlet();
// coordinates
$point = new Point($req->request->get('coord_lng'), $req->request->get('coord_lat'));
// times
$format = 'h:i A';
$time_open = DateTime::createFromFormat($format, $req->request->get('time_open'));
$time_close = DateTime::createFromFormat($format, $req->request->get('time_close'));
error_log(print_r($time_open, true));
error_log(print_r(DateTime::getLastErrors(), true));
// set and save values
$row->setOutletname($req->request->get('outletname'))
->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();
// set roles
$roles = $req->request->get('roles');
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 outlet roles
if ($role->isSuperAdmin() && !$this->isGranted('outlet.role.sadmin'))
continue;
$row->addRole($role);
}
}
}
$obj->setName($req->request->get('name'))
->setAddress($req->request->get('address'))
->setContactNumbers($req->request->get('contact_nums'))
->setTimeOpen($time_open)
->setTimeClose($time_close)
->setCoordinates($point);
// validate
$errors = $validator->validate($row);
$errors = $validator->validate($obj);
// initialize error list
$error_array = [];
@ -185,24 +180,6 @@ class OutletController extends BaseController
$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
@ -212,7 +189,7 @@ class OutletController extends BaseController
], 422);
} else {
// validated! save the entity
$em->persist($row);
$em->persist($obj);
$em->flush();
// return successful response
@ -222,7 +199,7 @@ class OutletController extends BaseController
}
}
public function update($id)
public function updateForm($id)
{
$this->denyAccessUnlessGranted('outlet.update', null, 'No access.');
@ -236,10 +213,6 @@ class OutletController 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();
$params['row'] = $row;
$params['values'] = [];

View file

@ -6,6 +6,10 @@ use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use DateTime;
/**
* @ORM\Entity
* @ORM\Table(name="hub")
@ -20,6 +24,127 @@ class Hub
*/
protected $id;
// name of hub
/**
* @ORM\Column(type="string", length=80)
*/
protected $name;
// address
/**
* @ORM\Column(type="string", length=80)
*/
protected $address;
// address coordinates
/**
* @ORM\Column(type="point")
*/
protected $coordinates;
// contact numbers
// this is displayed in a textarea
/**
* @ORM\Column(type="string", length=200)
*/
protected $contact_numbers;
// opening time
/**
* @ORM\Column(type="time")
*/
protected $time_open;
// closing time
/**
* @ORM\Column(type="time")
*/
protected $time_close;
// riders assigned to this hub
/**
* @ORM\OneToMany(targetEntity="Rider", mappedBy="hub")
*/
protected $riders;
public function __construct()
{
$this->riders = new ArrayCollection();
}
public function getID()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setAddress($address)
{
$this->address = $address;
return $this;
}
public function getAddress()
{
return $this->address;
}
public function setCoordinates(Point $point)
{
$this->coordinates = $point;
return $this;
}
public function getCoordinates()
{
return $this->coordinates;
}
public function setContactNumbers($nums)
{
$this->contact_nums = $nums;
return $this;
}
public function getContactNumbers()
{
return $this->contact_nums;
}
public function setTimeOpen(DateTime $time_open)
{
$this->time_open = $time_open;
return $this;
}
public function getTimeOpen()
{
return $this->time_open;
}
public function setTimeClose(DateTime $time_close)
{
$this->time_close = $time_close;
return $this;
}
public function getTimeClose()
{
return $this->time_close;
}
public function getRiders()
{
return $this->riders;
}
}

View file

@ -20,6 +20,25 @@ class JobOrder
*/
protected $id;
// date job order was created
protected $date_create;
// date and time of schedule
// defaults to current date / time
protected $schedule;
// is it an advanced order (future date)
protected $flag_advance;
// user that created the job order
protected $created_by;
// service type
protected $service_type;
// customer that requested job order
protected $customer;
// customer vehicle that needs servicing
protected $cus_vehicle;
}

View file

@ -6,6 +6,9 @@ use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use DateTime;
/**
* @ORM\Entity
* @ORM\Table(name="outlet")
@ -43,7 +46,7 @@ class Outlet
/**
* @ORM\Column(type="string", length=200)
*/
protected $contact_numbers;
protected $contact_nums;
// opening time
/**
@ -57,4 +60,75 @@ class Outlet
*/
protected $time_close;
public function getID()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setAddress($address)
{
$this->address = $address;
return $this;
}
public function getAddress()
{
return $this->address;
}
public function setCoordinates(Point $point)
{
$this->coordinates = $point;
return $this;
}
public function getCoordinates()
{
return $this->coordinates;
}
public function setContactNumbers($nums)
{
$this->contact_nums = $nums;
return $this;
}
public function getContactNumbers()
{
return $this->contact_nums;
}
public function setTimeOpen(DateTime $time_open)
{
$this->time_open = $time_open;
return $this;
}
public function getTimeOpen()
{
return $this->time_open;
}
public function setTimeClose(DateTime $time_close)
{
$this->time_close = $time_close;
return $this;
}
public function getTimeClose()
{
return $this->time_close;
}
}

View file

@ -3,7 +3,6 @@
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
@ -20,6 +19,76 @@ class Rider
*/
protected $id;
// first name
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
protected $first_name;
// last name
/**
* @ORM\Column(type="string", length=50, nullable=true)
*/
protected $last_name;
// contact number
/**
* @ORM\Column(type="string", length=20, nullable=true)
*/
protected $contact_num;
/**
* @ORM\ManyToOne(targetEntity="Hub", inversedBy="riders")
* @ORM\JoinColumn(name="hub_id", referencedColumnName="id")
*/
protected $hub;
public function getID()
{
return $this->id;
}
public function setFirstName($name)
{
$this->first_name = $name;
return $this;
}
public function getFirstName()
{
return $this->first_name;
}
public function setLastName($name)
{
$this->last_name = $name;
return $this;
}
public function getLastName()
{
return $this->last_name;
}
public function setContactNumber($num)
{
$this->contact_num = $num;
return $this;
}
public function getContactNumber()
{
return $this->contact_num;
}
public function setHub(Hub $hub)
{
$this->hub = $hub;
return $this;
}
public function getHub()
{
return $this->hub;
}
}

View file

@ -1,219 +1,273 @@
{% 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">Users</h3>
<!-- BEGIN: Subheader -->
<div class="m-subheader">
<div class="d-flex align-items-center">
<div class="mr-auto">
<h3 class="m-subheader__title">Outlet</h3>
</div>
</div>
</div>
<!-- END: Subheader -->
<div class="m-content">
<!--Begin::Section-->
<div class="row">
<div class="col-xl-12">
<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="fa fa-building"></i>
</span>
<h3 class="m-portlet__head-text">
{% if mode == 'update' %}
Edit Outlet
<small>{{ obj.getName() }}</small>
{% else %}
New Outlet
{% endif %}
</h3>
</div>
</div>
</div>
<form id="row-form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ mode == 'update' ? url('outlet_update_submit', {'id': obj.getId()}) : url('outlet_create_submit') }}">
<div class="m-portlet__body">
<div class="form-group m-form__group row no-border">
<div class="col-lg-8">
<label for="name" data-field="name">
Name
</label>
<input type="text" name="name" class="form-control m-input" value="{{ obj.getName() }}">
<div class="form-control-feedback hide" data-field="name"></div>
</div>
</div>
<div class="form-group m-form__group row no-border">
<div class="col-lg-8">
<label for="address" data-field="address">
Address
</label>
<textarea class="form-control m-input" id="address" rows="3" name="address">{{ obj.getAddress }}</textarea>
<div class="form-control-feedback hide" data-field="address"></div>
</div>
</div>
<div class="form-group m-form__group row">
<div class="col-lg-8">
<label for="contact_nums" data-field="contact_nums">
Contact Numbers
</label>
<textarea class="form-control m-input" id="contact_nums" rows="3" name="contact_nums">{{ obj.getAddress }}</textarea>
<div class="form-control-feedback hide" data-field="contact_nums"></div>
</div>
</div>
<div class="form-group m-form__group row">
<div class="col-lg-4">
<label for="time_open">
Time Open
</label>
<div class="input-group timepicker">
<input id="timepicker_open" type="text" name="time_open" class="form-control m-input" readonly placeholder="Select time" type="text"/>
<span class="input-group-addon">
<i class="la la-clock-o"></i>
</span>
</div>
<div class="form-control-feedback hide" data-field="time_open"></div>
</div>
<div class="col-lg-4">
<label for="time_close">
Time Close
</label>
<div class="input-group timepicker">
<input id="timepicker_close" type="text" name="time_close" class="form-control m-input" readonly placeholder="Select time" type="text"/>
<span class="input-group-addon">
<i class="la la-clock-o"></i>
</span>
</div>
<div class="form-control-feedback hide" data-field="time_close"></div>
</div>
</div>
<div class="form-group m-form__group row">
<div class="col-lg-12">
<label>
Map Coordinates
</label>
<input type="hidden" id="map_lat" name="coord_lat" value="">
<input type="hidden" id="map_lng" name="coord_lng" value="">
<div class="input-group">
<input type="text" class="form-control" id="m_gmap_address" placeholder="Search">
<span class="input-group-btn">
<button class="btn btn-primary" id="m_gmap_btn">
<i class="fa fa-search"></i>
</button>
</span>
</div>
<div id="m_gmap" style="height:600px;"></div>
</div>
</div>
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-success">Submit</button>
<a href="{{ url('outlet_list') }}" class="btn btn-secondary">Cancel</a>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<!-- END: Subheader -->
<div class="m-content">
<!--Begin::Section-->
<div class="row">
<div class="col-xl-8 offset-xl-2">
<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-user"></i>
</span>
<h3 class="m-portlet__head-text">
{% if row is defined %}
Edit User
<small>{{ row.getUsername() }}</small>
{% else %}
New User
{% endif %}
</h3>
</div>
</div>
</div>
<form id="row-form" class="m-form m-form--fit m-form--label-align-right m-form--group-seperator-dashed" method="post" action="{{ row is defined ? url('user_update_submit', {'id': row.getId()}) : url('user_create_submit') }}">
<div class="m-portlet__body">
<div class="form-group m-form__group row no-border">
<label class="col-lg-2 col-form-label" data-field="username">
Username:
</label>
<div class="col-lg-4">
<input type="text" name="username" class="form-control m-input" value="{{ values.username is defined ? values.username : (row is defined ? row.getUsername()) }}">
<div class="form-control-feedback hide" data-field="username"></div>
<span class="m-form__help">Unique alias for this user</span>
</div>
</div>
<div class="form-group m-form__group row">
<label class="col-lg-2 col-form-label" data-field="password">
Password:
</label>
<div class="col-lg-4">
<input type="password" name="password" class="form-control m-input">
<div class="form-control-feedback hide" data-field="password"></div>
{% if row is defined %}
<span class="m-form__help">Leave both fields blank for unchanged</span>
{% endif %}
</div>
<label class="col-lg-2 col-form-label" data-field="confirm_password">
Confirm Password:
</label>
<div class="col-lg-4">
<input type="password" name="confirm_password" class="form-control m-input">
<div class="form-control-feedback hide" data-field="confirm_password"></div>
</div>
</div>
<div class="form-group m-form__group row no-border">
<label class="col-lg-2 col-form-label" data-field="first_name">
First Name:
</label>
<div class="col-lg-4">
<input type="text" name="first_name" class="form-control m-input" value="{{ values.first_name is defined ? values.first_name : (row is defined ? row.getFirstName()) }}">
<div class="form-control-feedback hide" data-field="first_name"></div>
</div>
<label class="col-lg-2 col-form-label" data-field="last_name">
Last Name:
</label>
<div class="col-lg-4">
<input type="text" name="last_name" class="form-control m-input" value="{{ values.last_name is defined ? values.last_name : (row is defined ? row.getLastName()) }}">
<div class="form-control-feedback hide" data-field="last_name"></div>
</div>
</div>
<div class="form-group m-form__group row">
<label class="col-lg-2 col-form-label" data-field="email">
E-mail Address:
</label>
<div class="col-lg-4">
<input type="email" name="email" class="form-control m-input" value="{{ values.email is defined ? values.email : (row is defined ? row.getEmail()) }}">
<div class="form-control-feedback hide" data-field="email"></div>
</div>
<label class="col-lg-2 col-form-label" data-field="contact_no">
Contact Number:
</label>
<div class="col-lg-4">
<input type="text" name="contact_no" class="form-control m-input" value="{{ values.contact_no is defined ? values.contact_no : (row is defined ? row.getContactNumber()) }}">
<div class="form-control-feedback hide" data-field="contact_no"></div>
</div>
</div>
<div class="form-group m-form__group row">
<label class="col-lg-2 col-form-label" data-field="roles">
Roles:
</label>
<div class="col-lg-10">
<div class="m-checkbox-list">
{% for role in roles %}
{% if role.isSuperAdmin and not is_granted('user.role.sadmin') %}
{% else %}
<label class="m-checkbox">
<input type="checkbox" name="roles[]" value="{{ role.getID() }}"{{ (values.roles is defined and role.getID() in value.roles) or (row is defined and values.roles is not defined and role.getID() in row.getRoles()) ? ' checked' : '' }}>
{{ role.getName() }}
<span></span>
</label>
{% endif %}
{% endfor %}
</div>
<div class="form-control-feedback hide" data-field="roles"></div>
<span class="m-form__help">Check all roles that apply</span>
</div>
</div>
<div class="form-group m-form__group row">
<label class="col-lg-2 col-form-label" data-field="enabled">
Enabled:
</label>
<div class="col-lg-10">
<span class="m-switch m-switch--icon">
<label>
<input type="checkbox" name="enabled" value="1"{{ (values.enabled is defined and values.enabled) or (row is defined and values.enabled is not defined and row.isEnabled()) or (values.enabled is not defined and row is not defined) ? ' checked' }}>
<span></span>
</label>
</span>
<div class="form-control-feedback hide" data-field="enabled"></div>
</div>
</div>
</div>
<div class="m-portlet__foot m-portlet__foot--fit">
<div class="m-form__actions m-form__actions--solid m-form__actions--right">
<div class="row">
<div class="col-lg-12">
<button type="submit" class="btn btn-success">Submit</button>
<a href="{{ url('user_list') }}" class="btn btn-secondary">Cancel</a>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script>
$(function() {
$("#row-form").submit(function(e) {
var form = $(this);
<script src="//maps.google.com/maps/api/js?key={{ gmaps_api_key }}" type="text/javascript"></script>
<script src="/assets/vendors/custom/gmaps/gmaps.js" type="text/javascript"></script>
e.preventDefault();
<script>
$.ajax({
method: "POST",
url: form.prop('action'),
data: form.serialize()
}).done(function(response) {
// remove all error classes
removeErrors();
swal({
title: 'Done!',
text: 'Your changes have been saved!',
type: 'success',
onClose: function() {
window.location.href = "{{ url('user_list') }}";
}
});
}).fail(function(response) {
var errors = response.responseJSON.errors;
var firstfield = false;
// BEGIN google maps stuff
function selectPoint(map, latlng) {
var lat = latlng.lat();
var lng = latlng.lng();
// remove all error classes first
removeErrors();
// show it in map
map.removeMarkers();
map.setCenter(lat, lng);
map.addMarker({
lat: lat,
lng: lng
});
// display errors contextually
$.each(errors, function(field, msg) {
var formfield = $("[name='" + field + "']");
var label = $("label[data-field='" + field + "']");
var msgbox = $(".form-control-feedback[data-field='" + field + "']");
// set value in hidden input
$('#map_lat').val(lat);
$('#map_lng').val(lng);
}
// add error classes to bad fields
formfield.addClass('form-control-danger');
label.addClass('has-danger');
msgbox.html(msg).addClass('has-danger').removeClass('hide');
var map = new GMaps({
div: '#m_gmap',
lat: 14.6091,
lng: 121.0223,
click: function(e) {
// handle click in map
selectPoint(map, e.latLng);
e.stop();
}
});
var handleAction = function() {
var text = $.trim($('#m_gmap_address').val());
GMaps.geocode({
address: text,
callback: function(results, status) {
map.removeMarkers();
if (status == 'OK') {
selectPoint(map, results[0].geometry.location);
}
},
region: 'ph'
});
}
// check if this field comes first in DOM
var domfield = formfield.get(0);
$('#m_gmap_btn').click(function(e) {
e.preventDefault();
handleAction();
});
if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) {
firstfield = domfield;
}
});
$("#m_gmap_address").keypress(function(e) {
var keycode = (e.keyCode ? e.keyCode : e.which);
if (keycode == '13') {
e.preventDefault();
handleAction();
}
});
// focus on first bad field
firstfield.focus();
// END google maps stuff
// scroll to above that field to make it visible
$('html, body').animate({
scrollTop: $(firstfield).offset().top - 200
}, 100);
});
});
// timepickers
$(document).ready(function() {
$('#timepicker_open, #timepicker_close').timepicker();
/*
minuteStep: 15,
showSeconds: false,
showMeridian: false,
snapToStep: true
});
*/
});
// remove all error classes
function removeErrors() {
$(".form-control-danger").removeClass('form-control-danger');
$("[data-field]").removeClass('has-danger');
$(".form-control-feedback[data-field]").addClass('hide');
}
});
</script>
$(function() {
$("#row-form").submit(function(e) {
var form = $(this);
e.preventDefault();
$.ajax({
method: "POST",
url: form.prop('action'),
data: form.serialize()
}).done(function(response) {
// remove all error classes
removeErrors();
swal({
title: 'Done!',
text: 'Your changes have been saved!',
type: 'success',
onClose: function() {
window.location.href = "{{ url('outlet_list') }}";
}
});
}).fail(function(response) {
var errors = response.responseJSON.errors;
var firstfield = false;
// remove all error classes first
removeErrors();
// display errors contextually
$.each(errors, function(field, msg) {
var formfield = $("[name='" + field + "']");
var label = $("label[data-field='" + field + "']");
var msgbox = $(".form-control-feedback[data-field='" + field + "']");
// add error classes to bad fields
formfield.addClass('form-control-danger');
label.addClass('has-danger');
msgbox.html(msg).addClass('has-danger').removeClass('hide');
// check if this field comes first in DOM
var domfield = formfield.get(0);
if (!firstfield || (firstfield && firstfield.compareDocumentPosition(domfield) === 2)) {
firstfield = domfield;
}
});
// focus on first bad field
firstfield.focus();
// scroll to above that field to make it visible
$('html, body').animate({
scrollTop: $(firstfield).offset().top - 200
}, 100);
});
});
// remove all error classes
function removeErrors() {
$(".form-control-danger").removeClass('form-control-danger');
$("[data-field]").removeClass('has-danger');
$(".form-control-feedback[data-field]").addClass('hide');
}
});
</script>
{% endblock %}

View file

@ -42,7 +42,7 @@
</span>
</div>
</form>
<div id="m_gmap" style="height:800px;"></div>
<div id="m_gmap" style="height:600px;"></div>
</div>
</div>
</div>
@ -51,7 +51,7 @@
{% endblock %}
{% block scripts %}
<script src="//maps.google.com/maps/api/js?key=AIzaSyBbXDzw9kDx1hPvR3M8xdQihuioEUm_h7k" type="text/javascript"></script>
<script src="//maps.google.com/maps/api/js?key={{ gmaps_api_key }}" type="text/javascript"></script>
<script src="/assets/vendors/custom/gmaps/gmaps.js" type="text/javascript"></script>
<script>