Merge branch '233-privacy-policy-fixes' into 'master'

Resolve "Privacy policy fixes"

Closes #233

See merge request jankstudio/resq!274
This commit is contained in:
Korina Cordero 2019-07-24 12:57:54 +00:00
commit 498772ea62
10 changed files with 904 additions and 1 deletions

View file

@ -252,6 +252,7 @@ access_keys:
label: Search
- id: warranty.search
label: Customer Battery Search
- id: ticket
label: Ticket Access
acls:
@ -329,3 +330,17 @@ access_keys:
label: View
- id: review.delete
label: Delete
- id: privacypolicy
label: Privacy Policy
acls:
- id: privacy_policy.menu
label: Menu
- id: privacy_policy.list
label: List
- id: privacy_policy.add
label: Add
- id: privacy_policy.update
label: Update
- id: privacy_policy.delete
label: Delete

View file

@ -143,6 +143,10 @@ main_menu:
acl: warranty.search
label: Customer Battery Search
parent: support
- id: privacy_policy_list
acl: privacy_policy.list
label: Privacy Policy
parent: support
- id: service
acl: service.menu

View file

@ -0,0 +1,33 @@
privacy_policy_list:
path: /privacy_policies
controller: App\Controller\PrivacyPolicyController::index
privacy_policy_rows:
path: /privacy_policies/rows
controller: App\Controller\PrivacyPolicyController::rows
methods: [POST]
privacy_policy_create:
path: /privacy_policies/create
controller: App\Controller\PrivacyPolicyController::addForm
methods: [GET]
privacy_policy_create_submit:
path: /privacy_policies/create
controller: App\Controller\PrivacyPolicyController:addSubmit
methods: [POST]
privacy_policy_update:
path: /privacy_policies/{id}
controller: App\Controller\PrivacyPolicyController::updateForm
methods: [GET]
privacy_policy_update_submit:
path : /privacy_policies/{id}
controller: App\Controller\PrivacyPolicyController:updateSubmit
methods: [POST]
privacy_policy_delete:
path: /privacy_policies/{id}
controller: App\Controller\PrivacyPolicyController:destroy
methods: [DELETE]

View file

@ -0,0 +1,81 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\Common\Persistence\ObjectManager;
use App\Entity\Customer;
use App\Entity\PrivacyPolicy;
use App\Entity\MobileSession;
class SetCustomerPrivacyPolicyCommand extends Command
{
private $em;
public function __construct(ObjectManager $om)
{
$this->em = $om;
parent::__construct();
}
protected function configure()
{
$this->setName('customer:setprivacypolicy')
->setDescription('Set customer private policy.')
->addArgument('third_party_policy_id', InputArgument::REQUIRED, 'third_party_policy_id')
->addArgument('mobile_policy_id', InputArgument::REQUIRED, 'mobile_policy_id')
->addArgument('promo_policy_id', InputArgument::REQUIRED, 'promo_policy_id' )
->setHelp('Set customer private policy. Order of ids: third party mobile promo');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$third_party_policy_id = $input->getArgument('third_party_policy_id');
$mobile_policy_id = $input->getArgument('mobile_policy_id');
$promo_policy_id = $input->getArgument('promo_policy_id');
// get third party policy
$third_party_policy = $this->em->getRepository(PrivacyPolicy::class)->findOneBy(['id' => $third_party_policy_id]);
// get customers on third party
$third_party_customers = $this->em->getRepository(Customer::class)->findBy(['priv_third_party' => true]);
foreach ($third_party_customers as $cust)
{
$cust->setPrivacyPolicyThirdParty($third_party_policy);
}
// get promo policy
$promo_policy = $this->em->getRepository(PrivacyPolicy::class)->findOneBy(['id' => $promo_policy_id]);
// get customers on promo
$promo_customers = $this->em->getRepository(Customer::class)->findBy(['priv_promo' => true]);
foreach ($promo_customers as $cust)
{
$cust->setPrivacyPolicyPromo($promo_policy);
}
$this->em->flush();
// get mobile policy
$mobile_policy = $this->em->getRepository(PrivacyPolicy::class)->findOneBy(['id' => $mobile_policy_id]);
// get mobile sessions
$mobile_sessions = $this->em->getRepository(MobileSession::class)->findAll();
foreach ($mobile_sessions as $session)
{
$cust = $session->getCustomer();
if (!(is_null($cust)))
{
$cust->setPrivacyPolicyMobile($mobile_policy);
}
}
$this->em->flush();
}
}

View file

@ -395,6 +395,17 @@ class APIController extends Controller
// update mobile phone of customer
$cust->setPhoneMobile(substr($this->session->getPhoneNumber(), 2));
// get privacy policy for mobile
$policy = $em->createQuery('SELECT policy FROM App\Entity\PrivacyPolicy policy WHERE policy.name LIKE :policy_type')
->setParameter('policy_type', "%" . "mobile" . "%")
->getOneOrNullResult();
// set policy id
if ($policy != null)
{
$cust->setPrivacyPolicyMobile($policy);
}
$em->flush();
return $res->getReturnResponse();
@ -1787,9 +1798,40 @@ class APIController extends Controller
// set privacy settings
$priv_promo = $req->request->get('priv_promo', false);
$cust->setPrivacyThirdParty($req->request->get('priv_third_party'))
$priv_third_party = $req->request->get('priv_third_party');
$cust->setPrivacyThirdParty($priv_third_party)
->setPrivacyPromo($priv_promo);
// check if privacy settings are true
// if true, set the private policy for the customer
if ($priv_promo)
{
// find the promo policy
$policy = $em->createQuery('SELECT policy FROM App\Entity\PrivacyPolicy policy WHERE policy.name LIKE :policy_type')
->setParameter('policy_type', "%" . "promo" . "%")
->getOneOrNullResult();
// set policy id
if ($policy != null)
{
$cust->setPrivacyPolicyPromo($policy);
}
}
if ($priv_third_party)
{
// find the third party policy
$policy = $em->createQuery('SELECT policy FROM App\Entity\PrivacyPolicy policy WHERE policy.name LIKE :policy_type')
->setParameter('policy_type', "%" . "third party" . "%")
->getOneOrNullResult();
// set policy id
if ($policy != null)
{
$cust->setPrivacyPolicyThirdParty($policy);
}
}
$em->flush();
return $res->getReturnResponse();

View file

@ -0,0 +1,272 @@
<?php
namespace App\Controller;
use App\Entity\PrivacyPolicy;
use Doctrine\ORM\Query;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Catalyst\MenuBundle\Annotation\Menu;
class PrivacyPolicyController extends Controller
{
/**
* @Menu(selected="privacy_policy_list")
*/
public function index()
{
$this->denyAccessUnlessGranted('privacy_policy.list', null, 'No access.');
return $this->render('privacy-policy/list.html.twig');
}
public function rows(Request $req)
{
$this->denyAccessUnlessGranted('privacy_policy.list', null, 'No access.');
// build query
$qb = $this->getDoctrine()
->getRepository(PrivacyPolicy::class)
->createQueryBuilder('q');
// get datatable params
$datatable = $req->request->get('datatable');
// count total records
$tquery = $qb->select('COUNT(q)');
// add fitlers to count query
$this->setQueryFilters($datatable, $tquery);
$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');
// add filters to query
$this->setQueryFilters($datatable, $query);
// check if sorting is present, otherwise use default
if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) {
$order = $datatable['sort']['sort'] ?? 'asc';
$query->orderBy('q.' . $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->getID();
$row['name'] = $orow->getName();
// add row metadata
$row['meta'] = [
'update_url' => '',
'delete_url' => ''
];
// add crud urls
if ($this->isGranted('privacy_policy.update'))
$row['meta']['update_url'] = $this->generateUrl('privacy_policy_update', ['id' => $row['id']]);
if ($this->isGranted('privacy_policy.delete'))
$row['meta']['delete_url'] = $this->generateUrl('privacy_policy_delete', ['id' => $row['id']]);
$rows[] = $row;
}
// response
return $this->json([
'meta' => $meta,
'data' => $rows
]);
}
/**
* @Menu(selected="privacy_policy_list")
*/
public function addForm()
{
$this->denyAccessUnlessGranted('privacy_policy.add', null, 'No access.');
$params = [];
$params['obj'] = new PrivacyPolicy();
$params['mode'] = 'create';
// response
return $this->render('privacy-policy/form.html.twig', $params);
}
public function addSubmit(Request $req, ValidatorInterface $validator)
{
$this->denyAccessUnlessGranted('privacy_policy.add', null, 'No access.');
// create new object
$em = $this->getDoctrine()->getManager();
$row = new PrivacyPolicy();
// set and save values
$row->setName($req->request->get('name'));
$row->setContent($req->request->get('content'));
// validate
$errors = $validator->validate($row);
// initialize error list
$error_array = [];
// 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($row);
$em->flush();
// return successful response
return $this->json([
'success' => 'Changes have been saved!'
]);
}
}
/**
* @Menu(selected="privacy_policy_list")
*/
public function updateForm($id)
{
$this->denyAccessUnlessGranted('privacy_policy.update', null, 'No access.');
$params = [];
$params['mode'] = 'update';
// get row data
$em = $this->getDoctrine()->getManager();
$row = $em->getRepository(PrivacyPolicy::class)->find($id);
// make sure this row exists
if (empty($row))
throw $this->createNotFoundException('The item does not exist');
$params['obj'] = $row;
// response
return $this->render('privacy-policy/form.html.twig', $params);
}
public function updateSubmit(Request $req, ValidatorInterface $validator, $id)
{
$this->denyAccessUnlessGranted('privacy_policy.update', null, 'No access.');
// get row data
$em = $this->getDoctrine()->getManager();
$row = $em->getRepository(PrivacyPolicy::class)->find($id);
// make sure this row exists
if (empty($row))
throw $this->createNotFoundException('The item does not exist');
// set and save values
$row->setName($req->request->get('name'));
// validate
$errors = $validator->validate($row);
// initialize error list
$error_array = [];
// 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!'
]);
}
}
/**
* @Menu(selected="privacy_policy_list")
*/
public function destroy($id)
{
$this->denyAccessUnlessGranted('privacy_policy.delete', null, 'No access.');
$params = [];
// get row data
$em = $this->getDoctrine()->getManager();
$row = $em->getRepository(PrivacyPolicy::class)->find($id);
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();
}
protected function setQueryFilters($datatable, &$query)
{
if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) {
$query->where('q.name LIKE :filter')
->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%');
}
}
}

View file

@ -147,6 +147,27 @@ class Customer
*/
protected $priv_promo;
/**
* @ORM\ManyToOne(targetEntity="PrivacyPolicy", inversedBy="cust_mobile_app")
* @ORM\JoinColumn(name="policy_mobile_app_id", referencedColumnName="id")
* @Assert\NotBlank()
*/
protected $privpol_mobile_app;
/**
* @ORM\ManyToOne(targetEntity="PrivacyPolicy", inversedBy="cust_third_party")
* @ORM\JoinColumn(name="policy_third_party_id", referencedColumnName="id")
* @Assert\NotBlank()
*/
protected $privpol_third_party;
/**
* @ORM\ManyToOne(targetEntity="PrivacyPolicy", inversedBy="cust_promo")
* @ORM\JoinColumn(name="policy_promo_id", referencedColumnName="id")
* @Assert\NotBlank()
*/
protected $privpol_promo;
public function __construct()
{
$this->numbers = new ArrayCollection();
@ -433,4 +454,39 @@ class Customer
{
return $this->priv_promo;
}
public function setPrivacyPolicyMobile($privpol_mobile_app)
{
$this->privpol_mobile_app = $privpol_mobile_app;
return $this;
}
public function getPrivacyPolicyMobile()
{
return $this->privpol_mobile_app;
}
public function setPrivacyPolicyThirdParty($privpol_third_party)
{
$this->privpol_third_party = $privpol_third_party;
return $this;
}
public function getPrivacyPolicyThirdParty()
{
return $this->privpol_third_party;
}
public function setPrivacyPolicyPromo($privpol_promo)
{
$this->privpol_promo = $privpol_promo;
return $this;
}
public function getPrivacyPolicyPromo()
{
return $this->privpol_promo;
}
}

View file

@ -0,0 +1,116 @@
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity
* @ORM\Table(name="privacy_policy")
*/
class PrivacyPolicy
{
// unique id
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
// name
/**
* @ORM\Column(type="string", length=80)
*/
protected $name;
// content
/**
* @ORM\Column(type="text")
*/
protected $content;
// link to mobile app customers
/**
* @ORM\OneToMany(targetEntity="Customer", mappedBy="privpol_mobile_app")
*/
protected $cust_mobile_app;
// link to third party customers
/**
* @ORM\OneToMany(targetEntity="Customer", mappedBy="privpol_third_party")
*/
protected $cust_third_party;
/**
* @ORM\OneToMany(targetEntity="Customer", mappedBy="privpol_promo")
*/
protected $cust_promo;
public function getID()
{
return $this->id;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
}
public function setContent($content)
{
$this->content = $content;
return $this;
}
public function getContent()
{
return $this->content;
}
public function setCustMobileApp($cust_mobile_app)
{
$this->cust_mobile_app = $cust_mobile_app;
return $this;
}
public function getCustMobileApp()
{
return $this->cust_mobile_app;
}
public function setCustThirdParty($cust_third_party)
{
$this->cust_third_party = $cust_third_party;
return $this;
}
public function getCustThirdParty()
{
return $this->cust_third_party;
}
public function setCustPromo($cust_promo)
{
$this->cust_promo = $cust_promo;
return $this;
}
public function getCustPromo()
{
return $this->cust_promo;
}
}

View file

@ -0,0 +1,142 @@
{% 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">Privacy Policies</h3>
</div>
</div>
</div>
<!-- END: Subheader -->
<div class="m-content">
<!--Begin::Section -->
<div class="row">
<div class="col-xl-8">
<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>
<h3 class="m-portlet__head-text">
{% if mode == 'update' %}
Edit Policy
<small>{{ obj.getID() }}</small>
{% else %}
New Policy
{% 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('privacy_policy_update_submit', {'id': obj.getID()}) : url('privacy_policy_create_submit') }}">
<div class="m-portlet__body">
<div class="form-group m-form__group row no-border">
<div class="col-lg-12">
<label 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-12">
<label data-field="content">
Content
</label>
<textarea name="content" class="form-control m-input" data-name="content" rows="4">{{ obj.getContent() }}</textarea>
<div class="form-control-feedback hide" data-field="content"></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('privacy_policy_list') }}" class="btn btn-secondary">Back</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('privacy_policy_list') }}";
}
});
}).fail(function(response) {
if (response.status == 422 || response.status == 403) {
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

@ -0,0 +1,142 @@
{% 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">
Privacy Policies
</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('privacy_policy_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 Policy</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("privacy_policy_rows") }}',
method: 'POST'
}
},
saveState: {
cookie: false,
webstorage: false
},
pageSize: 10,
serverPaging: true,
serverFiltering: true,
serverSorting: true
},
columns: [
{
field: 'id',
title: 'ID'
},
{
field: 'name',
title: 'Name'
},
{
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.name + '" 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 <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();
});
}
});
});
});
</script>
{% endblock %}