1) Create a custom front-end controller to intercept the "Create" button action on the top right corner of the list view eg: client/custom/src/controllers/{{your-entity}}.js, in my case "service-ticket"
Code:
Espo.define('service-request-management:controllers/service-ticket', 'controllers/record', function (Dep) { return Dep.extend({ // intercept the default "create" action create: function (options) { options = options || {}; // if the user is a portal user, filter for excessive tickets: // more than one non-priority ticket submitted within the past 3 months var okToSubmit = true; var userObject = this.getUser(); var self = this; self.createOptions = options; if(userObject.attributes.isPortalUser) { var contactId = userObject.attributes.contactId; // use plain ajax to invoke an entry point action which returns the latest excessive ticket date if any // the response text format will be: "MM/DD/YYYY" or an empty string url = '?entryPoint=serviceTicketHandler&contactId='+contactId+'&action=portalPreCreateTicketFilter'; var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState === XMLHttpRequest.DONE) { // XMLHttpRequest.DONE == 4 if (xmlhttp.status === 200) { var serverResponse = xmlhttp.responseText; var preCreateTicketData = JSON.parse(serverResponse); if(preCreateTicketData.latestExcessiveTicket) { var options = {}; options.lastExcessive = preCreateTicketData.latestExcessiveTicket; options.nextTicket = preCreateTicketData.nextAllowedTicket; options.ticketReset = preCreateTicketData.ticketReset; self.viewFactory.create('service-request-management:views/service-ticket/modals/service-ticket-filter',options,function(view){ // display the excessive ticket warning modal view.render(); }); } else { // continue with the create action self.actionProceedToCreate(self.createOptions); } } else if (xmlhttp.status === 400) { alert('There was an error 400'); } else { alert('something else other than 200 was returned'); } } }; xmlhttp.open("POST",url , true); xmlhttp.send(); } else { // if the user is not a portal user, continue with the create action self.actionProceedToCreate(self.createOptions); } }, // custom function to proceed with the create action after checking and warning for excessive tickets actionProceedToCreate: function (options) { var self = this; // close the open warning modal if necessary var $ticketWarning = $('a[data-name="cancelExcessiveTicketWarning"]'); if($ticketWarning.length !== 0) { $ticketWarning.click(); } this.getModel().then(function (model) { if (options.relate) { model.setRelate(options.relate); } var o = { scope: this.name, model: model, returnUrl: options.returnUrl, returnDispatchParams: options.returnDispatchParams, params: options }; if (options.attributes) { model.set(options.attributes); } // determine if the user is a Tenant (portal user) and update the model accordingly var userObject = self.getUser(); if(userObject.attributes.isPortalUser) { model.attributes.isTenant = true; } else { model.attributes.isTenant = false; } this.prepareModelCreate(model, options); this.main(this.getViewName('edit'), o); }.bind(this)); } }); });
Code:
&lt;?php namespace Espo\Modules\ServiceRequestManagement\EntryPoints; use \Espo\Core\Exceptions\NotFound; use \Espo\Core\Exceptions\Forbidden; use \Espo\Core\Exceptions\BadRequest; class ServiceTicketHandler extends \Espo\Core\EntryPoints\Base { public static $authRequired = true; public $excessDays = 122; // default action public function run() { // determine the threshold date for excessive ticket submission $dt = new \DateTime(); $dt->modify('-' . $this->excessDays . ' days'); $excessiveTicketThreshold = $dt->format('Y-m-d H:i:s'); // find the most recent ticket within the excessive threshold $sql = 'SELECT `created_at` FROM `service_ticket` WHERE `created_at` > "'.$excessiveTicketThreshold.'" AND `account_id` = "'.$accountId.'" AND `is_priority` <> "1" AND `deleted` <> "1" ORDER By `created_at` DESC LIMIT 1'; $pdo = $this->getEntityManager()->getPDO(); $ps = $pdo->query($sql); if ($ps) { $dataArr = $ps->fetchAll(); } if($dataArr[0]['created_at']) { $latestDt = new \DateTime($dataArr[0]['created_at']); $latestExcessiveTicketDate = $latestDt->format('m/d/Y'); $nextAllowedTicketDate = $latestDt->modify('+' . $this->excessDays . ' days')->format('m/d/Y'); $resetDt = new \DateTime(); $ticketResetDate = $resetDt->modify('+' . $this->excessDays . ' days')->format('m/d/Y'); } else { $latestExcessiveTicketDate = ''; $nextAllowedTicketDate = ''; $ticketResetDate = ''; } $excessiveTicketData = array('latestExcessiveTicket' => $latestExcessiveTicketDate, 'nextAllowedTicket' => $nextAllowedTicketDate,'ticketReset' => $ticketResetDate); return json_encode($excessiveTicketData); } }
3) Change the metadata settings of your entity's clientDefs to specify the custom front-end controller:
Code:
"controller": "service-request-management:controllers/service-ticket",
Leave a comment: