Initial commit for ticketing system

This commit is contained in:
Ramon Gutierrez 2018-01-31 05:59:48 +08:00
parent bf0bd708e8
commit 72112ebe6c
10 changed files with 1020 additions and 15 deletions

View file

@ -187,3 +187,22 @@ access_keys:
label: Processing
- id: jo_assign.list
label: Assigning
- id: support
label: Customer Support Access
acls:
- id: support.menu
label: Menu
- id: ticket
label: Ticket Access
acls:
- id: ticket.menu
label: Menu
- id: ticket.list
label: List
- id: ticket.add
label: Add
- id: ticket.update
label: Update
- id: ticket.delete
label: Delete

View file

@ -94,3 +94,11 @@ main_menu:
label: Assigning
parent: joborder
- id: support
acl: support.menu
label: Customer Support
icon: flaticon-support
- id: ticket_list
acl: ticket.list
label: Tickets
parent: support

40
config/routes/ticket.yaml Normal file
View file

@ -0,0 +1,40 @@
# ticket
ticket_list:
path: /tickets
controller: App\Controller\TicketController::index
ticket_rows:
path: /tickets/rows
controller: App\Controller\TicketController::rows
methods: [POST]
ticket_create:
path: /tickets/create/{customer_id}
controller: App\Controller\TicketController::addForm
methods: [GET]
defaults:
customer_id: false
ticket_create_submit:
path: /tickets/create/{customer_id}
controller: App\Controller\TicketController::addSubmit
methods: [POST]
defaults:
customer_id: false
ticket_update:
path: /tickets/{id}
controller: App\Controller\TicketController::updateForm
methods: [GET]
ticket_update_submit:
path: /tickets/{id}
controller: App\Controller\TicketController::updateSubmit
methods: [POST]
ticket_delete:
path: /tickets/{id}
controller: App\Controller\TicketController::destroy
methods: [DELETE]

View file

@ -0,0 +1,405 @@
<?php
namespace App\Controller;
use App\Ramcar\BaseController;
use App\Ramcar\TicketType;
use App\Ramcar\TicketStatus;
use App\Entity\Ticket;
use App\Entity\Customer;
use Doctrine\ORM\Query;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use DateTime;
class TicketController extends BaseController
{
public function index()
{
$this->denyAccessUnlessGranted('ticket.list', null, 'No access.');
$params = $this->initParameters('ticket_list');
return $this->render('ticket/list.html.twig', $params);
}
public function rows(Request $req)
{
$this->denyAccessUnlessGranted('ticket.list', null, 'No access.');
// get query builder
$qb = $this->getDoctrine()
->getRepository(Ticket::class)
->createQueryBuilder('q');
// get datatable params
$datatable = $req->request->get('datatable');
// count total records
$tquery = $qb->select('COUNT(q)')
->leftJoin('q.created_by', 'u');
// add filters to count query
$this->setQueryFilters($datatable, $tquery, $qb);
$total = $tquery->getQuery()
->getSingleScalarResult();
// get current page number
$page = $datatable['pagination']['page'] ?? 1;
$perpage = $datatable['pagination']['perpage'];
$offset = ($page - 1) * $perpage;
// add metadata
$meta = [
'page' => $page,
'perpage' => $perpage,
'pages' => ceil($total / $perpage),
'total' => $total,
'sort' => 'asc',
'field' => 'id'
];
// build query
$query = $qb->select('q')
->addSelect($qb->expr()->concat('u.first_name', $qb->expr()->literal(' '), 'u.last_name') . ' as user_full_name');
// add filters to query
$this->setQueryFilters($datatable, $query, $qb);
// check if sorting is present, otherwise use default
if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) {
$prefix = '';
if (!in_array($datatable['sort']['field'], ['user_full_name']))
$prefix = 'q.';
$order = $datatable['sort']['sort'] ?? 'asc';
$query->orderBy($prefix . $datatable['sort']['field'], $order);
} else {
$query->orderBy('q.id', 'asc');
}
// get rows for this page
$obj_rows = $query->setFirstResult($offset)
->setMaxResults($perpage)
->getQuery()
->getResult();
// process rows
$rows = [];
foreach ($obj_rows as $orow) {
// add row data
$row['id'] = $orow[0]->getID();
$row['date_create'] = $orow[0]->getDateCreate()->format("d M Y g:i A");
$row['first_name'] = $orow[0]->getFirstName();
$row['last_name'] = $orow[0]->getLastName();
$row['contact_num'] = $orow[0]->getContactNumber();
$row['status'] = $orow[0]->getStatusText();
$row['ticket_type'] = $orow[0]->getTicketTypeText();
$row['user_full_name'] = $orow['user_full_name'];
// add row metadata
$row['meta'] = [
'update_url' => '',
'delete_url' => ''
];
// add crud urls
if ($this->isGranted('ticket.update'))
$row['meta']['update_url'] = $this->generateUrl('ticket_update', ['id' => $row['id']]);
if ($this->isGranted('ticket.delete'))
$row['meta']['delete_url'] = $this->generateUrl('ticket_delete', ['id' => $row['id']]);
$rows[] = $row;
}
// response
return $this->json([
'meta' => $meta,
'data' => $rows
]);
}
public function addForm($customer_id)
{
$this->denyAccessUnlessGranted('ticket.add', null, 'No access.');
$params = $this->initParameters('ticket_list');
$params['obj'] = new Ticket();
$params['mode'] = 'create';
$params['customer'] = false;
// get customer data
if ($customer_id) {
$em = $this->getDoctrine()->getManager();
$customer = $em->getRepository(Customer::class)->find($customer_id);
// make sure this row exists
if (empty($customer))
throw $this->createNotFoundException('This customer does not exist');
// add to view
$params['customer'] = $customer;
}
// get parent associations
$params['ticket_types'] = TicketType::getCollection();
$params['statuses'] = TicketStatus::getCollection();
$params['other_ticket_type'] = TicketType::OTHER;
// response
return $this->render('ticket/form.html.twig', $params);
}
public function addSubmit(Request $req, ValidatorInterface $validator, $customer_id)
{
$this->denyAccessUnlessGranted('ticket.add', null, 'No access.');
$em = $this->getDoctrine()->getManager();
// get customer data
if ($customer_id) {
$customer = $em->getRepository(Customer::class)->find($customer_id);
// make sure this row exists
if (empty($customer))
throw $this->createNotFoundException('This customer does not exist');
// set autopopulated fields
$first_name = $customer->getFirstName();
$last_name = $customer->getLastName();
$contact_num = false;
$mobile_numbers = $customer->getMobileNumbers();
if (!empty($mobile_numbers)) {
$contact_num = $mobile_numbers[0]->getID();
}
} else {
// get values directly from form
$first_name = $req->request->get('first_name');
$last_name = $req->request->get('last_name');
$contact_num = $req->request->get('contact_num');
}
// create new row
$obj = new Ticket();
// get ticket type
$ticket_type = $req->request->get('ticket_type');
$other_ticket_type = false;
if ($ticket_type == TicketType::OTHER) {
$other_ticket_type = $req->request->get('other_ticket_type');
}
// set and save values
$obj->setFirstName($first_name)
->setLastName($last_name)
->setContactNumber($contact_num)
->setStatus($req->request->get('status'))
->setTicketType($ticket_type)
->setOtherTicketType($other_ticket_type)
->setDetails($req->request->get('details'))
->setDateCreate(new DateTime())
->setCreatedBy($this->getUser());
// if assigned to customer, set association
if ($customer_id) {
$obj->setCustomer($customer);
}
// initialize error list
$error_array = [];
// validate
$errors = $validator->validate($obj);
// custom validation for other ticket type
if ($ticket_type == TicketType::OTHER && empty($other_ticket_type)) {
$error_array['other_ticket_type'] = 'Ticket type not specified.';
}
// 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);
} else {
// validated! save the entity
$em->persist($obj);
$em->flush();
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
}
public function updateForm($id)
{
$this->denyAccessUnlessGranted('ticket.update', null, 'No access.');
$params = $this->initParameters('ticket_list');
$params['mode'] = 'update';
// get row data
$em = $this->getDoctrine()->getManager();
$obj = $em->getRepository(Ticket::class)->find($id);
// make sure this row exists
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
$em = $this->getDoctrine()->getManager();
// get parent associations
$params['customer'] = $obj->getCustomer();
$params['ticket_types'] = TicketType::getCollection();
$params['statuses'] = TicketStatus::getCollection();
$params['other_ticket_type'] = TicketType::OTHER;
$params['obj'] = $obj;
// response
return $this->render('ticket/form.html.twig', $params);
}
public function updateSubmit(Request $req, ValidatorInterface $validator, $id)
{
$this->denyAccessUnlessGranted('ticket.update', null, 'No access.');
// get row data
$em = $this->getDoctrine()->getManager();
$obj = $em->getRepository(Ticket::class)->find($id);
// make sure this row exists
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
// get customer data
if (!empty($obj->getCustomer())) {
$customer = $obj->getCustomer();
// make sure this row exists
if (empty($customer))
throw $this->createNotFoundException('This customer does not exist');
// set autopopulated fields
$first_name = $customer->getFirstName();
$last_name = $customer->getLastName();
$contact_num = false;
$mobile_numbers = $customer->getMobileNumbers();
if (!empty($mobile_numbers)) {
$contact_num = $mobile_numbers[0]->getID();
}
} else {
// get values directly from form
$first_name = $req->request->get('first_name');
$last_name = $req->request->get('last_name');
$contact_num = $req->request->get('contact_num');
}
// get ticket type
$ticket_type = $req->request->get('ticket_type');
$other_ticket_type = false;
if ($ticket_type == TicketType::OTHER) {
$other_ticket_type = $req->request->get('other_ticket_type');
}
// set and save values
$obj->setFirstName($first_name)
->setLastName($last_name)
->setContactNumber($contact_num)
->setStatus($req->request->get('status'))
->setTicketType($ticket_type)
->setOtherTicketType($other_ticket_type)
->setDetails($req->request->get('details'));
// initialize error list
$error_array = [];
// validate
$errors = $validator->validate($obj);
// custom validation for other ticket type
if ($ticket_type == TicketType::OTHER && empty($other_ticket_type)) {
$error_array['other_ticket_type'] = 'Ticket type not specified.';
}
// 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);
} else {
// validated! save the entity
$em->flush();
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
}
public function destroy($id)
{
$this->denyAccessUnlessGranted('ticket.delete', null, 'No access.');
$params = $this->initParameters('ticket_list');
// get row data
$em = $this->getDoctrine()->getManager();
$obj = $em->getRepository(Ticket::class)->find($id);
if (empty($obj))
throw $this->createNotFoundException('The item does not exist');
// delete this row
$em->remove($obj);
$em->flush();
// response
$response = new Response();
$response->setStatusCode(Response::HTTP_OK);
$response->send();
}
// check if datatable filter is present and append to query
protected function setQueryFilters($datatable, &$query, $qb) {
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
$query->where($qb->expr()->concat('u.first_name', $qb->expr()->literal(' '), 'u.last_name') . ' LIKE :filter')
->orWhere('q.status LIKE :filter')
->orWhere('q.ticket_type LIKE :filter')
->orWhere('q.other_ticket_type LIKE :filter')
->orWhere('q.first_name LIKE :filter')
->orWhere('q.last_name LIKE :filter')
->orWhere('q.contact_num LIKE :filter')
->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%');
}
}
}

View file

@ -2,10 +2,14 @@
namespace App\Entity;
use App\Ramcar\TicketType;
use App\Ramcar\TicketStatus;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use DateTime;
/**
* @ORM\Entity
* @ORM\Table(name="ticket")
@ -29,36 +33,41 @@ class Ticket
// status of the ticket
/**
* @ORM\Column(type="string", length=15)
* @Assert\NotBlank()
*/
protected $status;
// ticket type
/**
* @ORM\Column(type="string", length=15)
* @Assert\NotBlank()
*/
protected $type;
protected $ticket_type;
// user defined ticket type
/**
* @ORM\Column(type="string", length=80, nullable=true)
*/
protected $other_type;
protected $other_ticket_type;
// first name of ticket owner
/**
* @ORM\Column(type="string", length=80, nullable=true)
* @ORM\Column(type="string", length=80)
* @Assert\NotBlank()
*/
protected $first_name;
// last name of ticket owner
/**
* @ORM\Column(type="string", length=80, nullable=true)
* @ORM\Column(type="string", length=80)
* @Assert\NotBlank()
*/
protected $last_name;
// contact number of ticket owner
/**
* @ORM\Column(type="string", length=20, nullable=true)
* @ORM\Column(type="string", length=20)
* @Assert\NotBlank()
*/
protected $contact_num;
@ -79,7 +88,6 @@ class Ticket
/**
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="tickets")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id", nullable=true)
* Assert\NotBlank()
*/
protected $customer;
@ -115,26 +123,42 @@ class Ticket
return $this->status;
}
public function setType($type)
public function getStatusText()
{
$this->type = $type;
$statuses = TicketStatus::getCollection();
return $statuses[$this->status] ?? "";
}
public function setTicketType($ticket_type)
{
$this->ticket_type = $ticket_type;
return $this;
}
public function getType()
public function getTicketType()
{
return $this->type;
return $this->ticket_type;
}
public function setOtherType($other_type)
public function setOtherTicketType($other_ticket_type)
{
$this->other_type = $other_type;
$this->other_ticket_type = $other_ticket_type;
return $this;
}
public function getOtherType()
public function getOtherTicketType()
{
return $this->other_type;
return $this->other_ticket_type;
}
public function getTicketTypeText()
{
if ($this->ticket_type == TicketType::OTHER) {
return $this->other_ticket_type;
} else {
$types = TicketType::getCollection();
return $types[$this->ticket_type] ?? "";
}
}
public function setFirstName($first_name)

View file

@ -0,0 +1,31 @@
<?php
namespace App\Ramcar;
class TicketStatus
{
const FILED = 'filed';
const ACKNOWLEDGED = 'acknowledged';
const CLOSED = 'closed';
const COLLECTION = [
'filed' => 'Filed',
'acknowledged' => 'Acknowledged',
'closed' => 'Closed',
];
static public function getCollection()
{
return self::COLLECTION;
}
static public function validate($type_text)
{
if (isset(self::COLLECTION[$type_text]))
return true;
return false;
}
}

35
src/Ramcar/TicketType.php Normal file
View file

@ -0,0 +1,35 @@
<?php
namespace App\Ramcar;
class TicketType
{
const COMPLAINT = 'complaint';
const QUESTION = 'question';
const ORDERS = 'orders';
const BILLING = 'billing';
const OTHER = 'other';
const COLLECTION = [
'complaint' => 'Complaint',
'question' => 'Question',
'orders' => 'Orders',
'billing' => 'Billing',
'other' => 'Other',
];
static public function getCollection()
{
return self::COLLECTION;
}
static public function validate($type_text)
{
if (isset(self::COLLECTION[$type_text]))
return true;
return false;
}
}

View file

@ -44,6 +44,11 @@
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#vehicles">Vehicles</a>
</li>
{% if mode == 'update' %}
<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#tickets">Tickets</a>
</li>
{% endif %}
</ul>
<div class="tab-content">
<div class="tab-pane active" id="customer-info" role="tabpanel">
@ -110,12 +115,26 @@
</div>
</div>
</div>
{% if mode == 'update' %}
<div class="tab-pane" id="tickets" role="tabpanel">
<div class="form-group m-form__group row form-group-inner row">
<div class="col-lg-12">
<div class="m_datatable" id="data-tickets"></div>
</div>
</div>
</div>
{% endif %}
</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">
<div class="col-lg-6 text-left">
{% if mode == 'update' %}
<a href="{{ url('ticket_create', {'customer_id': obj.getID}) }}" class="btn btn-info"><i class="fa fa-ticket fa-fw"></i>Create Ticket</a>
{% endif %}
</div>
<div class="col-lg-6">
<button type="submit" class="btn btn-success">Submit</button>
<a href="{{ url('customer_list') }}" class="btn btn-secondary">Cancel</a>
</div>
@ -473,6 +492,7 @@
var numberIds = [];
var mfgVehicles = [];
var vehicleRows = [];
var ticketRows = [];
{% for number in obj.getMobileNumbers() %}
nrow = {
@ -513,6 +533,18 @@
vehicleRows.push(vrow);
{% endfor %}
{% for ticket in obj.getTickets %}
trow = {
id: "{{ ticket.getID }}",
date_create: "{{ ticket.getDateCreate|date('d M Y - h:i A') }}",
ticket_type: "{{ ticket.getTicketTypeText }}",
status: "{{ ticket.getStatusText }}",
edit_url: "{{ url('ticket_update', {'id': ticket.getID}) }}"
};
ticketRows.push(trow);
{% endfor %}
// add a mobile number to the table
$("#btn-add-mobile-number").click(function() {
var id = $("#mobile-number").val();
@ -875,6 +907,7 @@
{
field: 'date_registered',
title: 'Date Registered',
width: 200,
template: function (row, index, datatable) {
return row.date_registered + '<div class="form-control-feedback hide" data-field="date_registered"></div>';
}
@ -882,6 +915,7 @@
{
field: 'date_confirmed',
title: 'Date Confirmed',
width: 200,
template: function (row, index, datatable) {
return row.date_confirmed + '<div class="form-control-feedback hide" data-field="date_confirmed"></div>';
}
@ -1047,6 +1081,54 @@
};
var vehicleTable = $("#data-vehicles").mDatatable(vehicleOptions);
// tickets data table
var ticketOptions = {
data: {
type: 'local',
source: ticketRows,
saveState: {
cookie: false,
webstorage: false
}
},
layout: {
scroll: true
},
columns: [
{
field: 'id',
title: 'ID',
width: 30
},
{
field: 'date_create',
title: 'Date Created',
width: 200
},
{
field: 'ticket_type',
title: 'Ticket Type'
},
{
field: 'status',
title: 'Status'
},
{
field: 'Actions',
width: 70,
title: 'Actions',
sortable: false,
overflow: 'visible',
template: function (row, index, datatable) {
return '<a href="' + row.edit_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-ticket" title="Edit"><i class="la la-edit"></i></a>';
},
}
],
pagination: false
};
var numberTable = $("#data-tickets").mDatatable(ticketOptions);
});
</script>
{% endblock %}

View file

@ -0,0 +1,188 @@
{% 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">Tickets</h3>
</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="fa fa-ticket"></i>
</span>
<h3 class="m-portlet__head-text">
{% if mode == 'update' %}
Edit Ticket
<small>{{ obj.getID }}</small>
{% else %}
New Ticket
{% if customer %}
<small>for {{ customer.getFirstName ~ " " ~ customer.getLastName }}</small>
{% endif %}
{% 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('ticket_update_submit', {'id': obj.getId}) : url('ticket_create_submit', {'customer_id': customer ? customer.getID : false}) }}">
<div class="m-portlet__body">
<div class="form-group m-form__group row no-border">
<div class="col-lg-4">
<label data-field="first_name">First Name</label>
<input type="text" name="first_name" class="form-control m-input" value="{{ customer ? customer.getFirstName : obj.getFirstName }}"{{ customer ? ' disabled' }}>
<div class="form-control-feedback hide" data-field="first_name"></div>
</div>
<div class="col-lg-4">
<label data-field="last_name">Last Name</label>
<input type="text" name="last_name" class="form-control m-input" value="{{ customer ? customer.getLastName : obj.getLastName }}"{{ customer ? ' disabled' }}>
<div class="form-control-feedback hide" data-field="last_name"></div>
</div>
<div class="col-lg-4">
<label data-field="contact_num">Contact Number</label>
<input type="text" name="contact_num" class="form-control m-input" value="{{ customer ? customer.getMobileNumbers[0].getID : obj.getContactNumber }}"{{ customer ? ' disabled' }}>
<div class="form-control-feedback hide" data-field="contact_num"></div>
</div>
</div>
<div class="form-group m-form__group row no-border">
<div class="col-lg-4">
<label data-field="status">Status</label>
<select class="form-control m-input" id="status" name="status">
{% for key, status in statuses %}
<option value="{{ key }}"{{ key == obj.getStatus ? ' selected' }}>{{ status }}</option>
{% endfor %}
</select>
<div class="form-control-feedback hide" data-field="status"></div>
</div>
<div class="col-lg-4">
<label data-field="status">Ticket Type</label>
<select class="form-control m-input" id="ticket-type" name="ticket_type">
<option value=""></option>
{% for key, ticket_type in ticket_types %}
<option value="{{ key }}"{{ key == obj.getTicketType ? ' selected' }}>{{ ticket_type }}</option>
{% endfor %}
</select>
<div class="form-control-feedback hide" data-field="ticket_type"></div>
</div>
<div class="col-lg-4">
<label data-field="other_ticket_type">Other Ticket Type <small>(please specify)</small></label>
<input type="text" name="other_ticket_type" id="other-ticket-type" class="form-control m-input" value="{{ obj.getOtherTicketType }}" disabled>
<div class="form-control-feedback hide" data-field="other_ticket_type"></div>
</div>
</div>
<div class="form-group m-form__group row no-border">
<div class="col-lg-12">
<label for="details" data-field="details">
Details
</label>
<textarea class="form-control m-input" id="details" rows="4" name="details">{{ obj.getDetails }}</textarea>
<div class="form-control-feedback hide" data-field="details"></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('ticket_list') }}" class="btn btn-secondary">Cancel</a>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<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('ticket_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');
}
// toggle other ticket type field
$("#ticket-type").change(function() {
var field = $("#other-ticket-type");
if ($(this).val() == '{{ other_ticket_type }}') {
field.prop('disabled', false);
} else {
field.prop('disabled', true).val("");
}
}).change();
});
</script>
{% endblock %}

View file

@ -0,0 +1,173 @@
{% 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">
Tickets
</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__body">
<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">
<div class="form-group m-form__group row align-items-center">
<div class="col-md-4">
<div class="m-input-icon m-input-icon--left">
<input type="text" class="form-control m-input m-input--solid" placeholder="Search..." id="data-rows-search">
<span class="m-input-icon__icon m-input-icon__icon--left">
<span><i class="la la-search"></i></span>
</span>
</div>
</div>
</div>
</div>
<div class="col-xl-4 order-1 order-xl-2 m--align-right">
<a href="{{ url('ticket_create') }}" class="btn btn-focus m-btn m-btn--custom m-btn--icon m-btn--air m-btn--pill">
<span>
<i class="fa fa-ticket"></i>
<span>New Ticket</span>
</span>
</a>
<div class="m-separator m-separator--dashed d-xl-none"></div>
</div>
</div>
</div>
<!--begin: Datatable -->
<div id="data-rows"></div>
<!--end: Datatable -->
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
<script>
$(function() {
var options = {
data: {
type: 'remote',
source: {
read: {
url: '{{ url("ticket_rows") }}',
method: 'POST',
}
},
saveState: {
cookie: false,
webstorage: false
},
pageSize: 10,
serverPaging: true,
serverFiltering: true,
serverSorting: true
},
columns: [
{
field: 'id',
title: 'ID',
width: 30
},
{
field: 'date_create',
title: 'Date Created'
},
{
field: 'first_name',
title: 'First Name'
},
{
field: 'last_name',
title: 'Last Name'
},
{
field: 'contact_num',
title: 'Contact No.'
},
{
field: 'ticket_type',
title: 'Type'
},
{
field: 'status',
title: 'Status'
},
{
field: 'user_full_name',
title: 'Created By'
},
{
field: 'Actions',
width: 110,
title: 'Actions',
sortable: false,
overflow: 'visible',
template: function (row, index, datatable) {
var actions = '';
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.id + '" title="Edit"><i class="la la-edit"></i></a>';
}
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.id + '" title="Delete"><i class="la la-trash"></i></a>';
}
return actions;
},
}
],
search: {
onEnter: false,
input: $('#data-rows-search'),
delay: 400
}
};
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',
html: 'Are you sure you want to delete ticket #<strong>' + id + '</strong>?',
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 %}