diff --git a/classes/Modules/Ticket/Task/TicketImportHelper.php b/classes/Modules/Ticket/Task/TicketImportHelper.php index cd641bda..0e4011da 100644 --- a/classes/Modules/Ticket/Task/TicketImportHelper.php +++ b/classes/Modules/Ticket/Task/TicketImportHelper.php @@ -216,7 +216,7 @@ class TicketImportHelper } } - /* Function from Gateway */ + /* Functions from Gateway */ /** * @param string $ticketNumber * @@ -233,6 +233,81 @@ class TicketImportHelper return (int) $count; } + /** + * @param string $recipientMail + * @param string $senderMail + * @param string $senderName + * @param string $subject + * + * @return array + */ + public function getTicketRules( + string $recipientMail, + string $senderMail, + string $senderName, + string $subject + ): array { + +/* + $sql = "SELECT + tr.spam AS `is_spam`, + tr.dsgvo AS `is_gdpr_relevant`, + tr.prio AS `priority`, + tr.persoenlich AS `is_private`, + tr.warteschlange AS `queue_id` + FROM `ticket_regeln` AS `tr` + WHERE + tr.aktiv = 1 + AND (tr.empfaenger_email LIKE :source_email OR empfaenger_email = '') + AND ( + tr.sender_email LIKE :sender_email + OR (tr.sender_email LIKE '@%' AND :sender_email LIKE CONCAT('%', tr.sender_email)) + OR tr.sender_email = '' + ) + AND (tr.name LIKE :sender_name OR tr.name = '') + AND (tr.betreff LIKE :subject OR tr.betreff = '')"; + $values = [ + 'source_email' => $recipientMail, + 'sender_email' => $senderMail, + 'sender_name' => $senderName, + 'subject' => $subject, + ]; + + return $this->db->fetchAll($sql, $values); + +*/ + + // Legacy DB implementation: + + $sql = "SELECT + tr.id, + tr.spam AS `is_spam`, + tr.dsgvo AS `is_gdpr_relevant`, + tr.prio AS `priority`, + tr.persoenlich AS `is_private`, + tr.warteschlange AS `queue_id` + FROM `ticket_regeln` AS `tr` + WHERE + tr.aktiv = 1 + AND ('".$recipientMail."' LIKE tr.empfaenger_email OR tr.empfaenger_email = '') + AND ('".$senderMail."' LIKE tr.sender_email OR tr.sender_email = '') + AND ('".$senderMail."' LIKE tr.name OR tr.name = '') + AND ('".$subject."' LIKE tr.betreff OR tr.betreff = '')"; + + $this->logger->debug('ticket rule',['sql' => $sql]); + + $result = $this->db->SelectArr($sql); + + if ($result != null) { + $this->logger->debug('ticket rules',['count',count($result)]); + return ($result); + } else { + $this->logger->debug('no ticket rules applicable',['']); + return(array()); + } + } + + /* END Functions from Gateway */ public function createTicket( int $projectId, @@ -322,21 +397,81 @@ class TicketImportHelper $messageId = $this->db->GetInsertID(); $this->logger->debug('inserted',['id' => $messageId, 'schluessel' => $ticketNumber]); - $this->updateTicketMessagesCount($ticketNumber); $this->resetTicketStatus($ticketNumber); // $this->db->commit(); - - return (int) $messageId; + $success = 1; + $this->markTicketMessagesCompleted($ticketNumber); } catch (Throwable $e) { // $this->db->rollBack(); - + $success = 0; $this->logger->error('Failed to insert ticket message into db', ['exception' => $e]); - } - $this->markTicketMessagesCompleted($ticketNumber); + + + $this->applyTicketRules($messageId); + + return($success); } + public function applyTicketRules(int $ticketMessageId): void + { +/* $ticketData = $this->gateway->tryGetTicketDataByByMessage($ticketMessageId); */ + + $ticketData = $this->db->SelectArr("SELECT t.id, tn.mail as sender_email, tn.verfasser as sender_name, tn.betreff as subject, t.quelle as source_email FROM ticket t INNER JOIN ticket_nachricht tn ON tn.ticket = t.schluessel WHERE tn.id = '".$ticketMessageId."'")[0]; + + if ($ticketData === null) { + throw new InvalidArgumentException('cannot find ticket by message id'); + } + + $ticketId = $ticketData['id']; + $senderMail = $ticketData['sender_email']; + $senderName = $ticketData['sender_name']; + $subject = $ticketData['subject']; + $source = $ticketData['source_email']; + + $this->logger->debug('check ticket rules',['tn_id' => $ticketMessageId, 'sender_email' => $senderMail, 'subject' => $subject]); + + //TODO: richtig loggen: $this->app->erp->LogFile("Empfaengermail: $quelle Sendermail: $mailadresse Kunde: $kunde Betreff: $betreff"); + $ruleArray = $this->getTicketRules($source, $senderMail, $senderName, $subject); + if (empty($ruleArray)) { + return; + } + + foreach ($ruleArray as $rule) { + + $this->logger->debug('ticket rule applies',['rule_id' => $rule['id']]); + +/* + $update = $this->db->update(); + $update->table('ticket'); + if ($rule['is_spam'] === 1) { + $update->set('inbearbeitung', 0); + $update->set('zugewiesen', 1); + $this->db->perform( + 'UPDATE `ticket_nachricht` SET `status` = :status WHERE `ticket` = :ticket_id', + ['status' => TicketGateway::STATUS_SPAM, 'ticket_id' => $ticketId] + ); + } + $update->set('dsgvo', $rule['is_gdpr_relevant']); + $update->set('privat', $rule['is_private'] ); + $update->set('prio', $rule['priority'] ); + $update->where('id = :ticket_id'); + $sql = $update->getStatement(); + $this->db->perform($sql, ['ticket_id' => $ticketId]); +*/ + + if ($rule['is_spam'] === 1) { + $sql = "UPDATE `ticket_nachricht` SET `status` = \'spam\' WHERE `id` = '".$ticketMessageId."'"; + $this->db->Update($sql); + } + + $sql = "UPDATE `ticket` SET `dsgvo` = '".$rule['is_gdpr_relevant']."', `privat` = '".$rule['is_private']."', `prio` = '".$rule['priority']."', `warteschlange` = '".$rule['queue_id']."' WHERE `id` = '".$ticketId."'"; + $this->db->Update($sql); + } + } + + /* End TicketService */ /** @@ -355,6 +490,9 @@ class TicketImportHelper continue; } try { + + $this->logger->debug('Start import', ['message' => $message->getSubject()]); + $this->importMessage($message); $insertedMailsCount++; if ($this->mailAccount->isDeleteAfterImportEnabled()) { diff --git a/www/lib/class.erpapi.php b/www/lib/class.erpapi.php index ed77f5ee..ac1d4177 100644 --- a/www/lib/class.erpapi.php +++ b/www/lib/class.erpapi.php @@ -7084,6 +7084,7 @@ title: 'Abschicken', $navarray['menu']['admin'][++$menu]['first'] = array('Verwaltung','rechnung','list'); + $navarray['menu']['admin'][$menu]['sec'][] = array('Ticketregeln','ticketregeln','list'); $navarray['menu']['admin'][$menu]['sec'][] = array('Zeitkonten','zeiterfassung','list'); if(!$this->RechteVorhanden('mitarbeiterzeiterfassung','dashboard')){ $navarray['menu']['admin'][$menu]['sec'][] = array('Antrag einreichen','mitarbeiterzeiterfassung','timemanagementrequest'); diff --git a/www/pages/content/ticketregeln_edit.tpl b/www/pages/content/ticketregeln_edit.tpl new file mode 100644 index 00000000..0dbc4b4e --- /dev/null +++ b/www/pages/content/ticketregeln_edit.tpl @@ -0,0 +1,91 @@ +
+ + +
+ [MESSAGE] +
+ [FORMHANDLEREVENT] +
+
+
+
+
+ {|Ticketregeln|}Ticketregeln fü die Verarbeitung bei Ticketeingang. Platzhalter werden mit % angegeben. + + + + + + + + + + + + +
{|E-Mail Empfänger|}:
{|E-Mail Verfasser|}:
{|Verfasser Name|}:
{|Betreff|}:
{|Papierkorb|}:
{|Persönlich|}:
{|Prio|}:
{|DSGVO|}:
{|Verantwortliche Warteschlange|}:
{|Aktiv|}:
+
+
+
+
+
+ + +
+
+ +
+ diff --git a/www/pages/content/ticketregeln_list.tpl b/www/pages/content/ticketregeln_list.tpl new file mode 100644 index 00000000..1742e34f --- /dev/null +++ b/www/pages/content/ticketregeln_list.tpl @@ -0,0 +1,5 @@ +
+ [MESSAGE] + [TAB1] + [TAB1NEXT] +
diff --git a/www/pages/ticketregeln.php b/www/pages/ticketregeln.php new file mode 100644 index 00000000..efcf536e --- /dev/null +++ b/www/pages/ticketregeln.php @@ -0,0 +1,208 @@ +app = $app; + if ($intern) + return; + + $this->app->ActionHandlerInit($this); + $this->app->ActionHandler("list", "ticketregeln_list"); + $this->app->ActionHandler("create", "ticketregeln_edit"); // This automatically adds a "New" button + $this->app->ActionHandler("edit", "ticketregeln_edit"); + $this->app->ActionHandler("delete", "ticketregeln_delete"); + $this->app->DefaultActionHandler("list"); + $this->app->ActionHandlerListen($app); + } + + public function Install() { + /* Fill out manually later */ + } + + static function TableSearch(&$app, $name, $erlaubtevars) { + switch ($name) { + case "ticketregeln_list": + $allowed['ticketregeln_list'] = array('list'); + $heading = array('E-Mail Empfänger', 'E-Mail Verfasser', 'Verfasser Name', 'Betreff', 'Papierkorb', 'Persönlich', 'Prio', 'DSGVO', 'Verantw.', 'Aktiv', 'Menü'); + $width = array('10%'); // Fill out manually later + + $findcols = array('t.empfaenger_email', 't.sender_email', 't.name', 't.betreff', 't.spam', 't.persoenlich', 't.prio', 't.dsgvo', 't.warteschlange', 't.aktiv'); + $searchsql = array('t.empfaenger_email', 't.sender_email', 't.name', 't.betreff', 't.spam', 't.persoenlich', 't.prio', 't.dsgvo', 't.warteschlange', 't.aktiv'); + + $defaultorder = 1; + $defaultorderdesc = 0; + + $menu = "
" . "Conf->WFconf['defaulttheme']}/images/edit.svg\" border=\"0\"> " . "Conf->WFconf['defaulttheme']}/images/delete.svg\" border=\"0\">" . "
"; + + $sql = "SELECT t.id, t.empfaenger_email, t.sender_email, t.name, t.betreff, t.spam, t.persoenlich, t.prio, t.dsgvo, w.warteschlange, t.aktiv, t.id FROM ticket_regeln t LEFT JOIN warteschlangen w ON t.warteschlange = w.label"; + + $where = "1"; + $count = "SELECT count(DISTINCT id) FROM ticket_regeln WHERE $where"; +// $groupby = ""; + + break; + } + + $erg = false; + + foreach ($erlaubtevars as $k => $v) { + if (isset($$v)) { + $erg[$v] = $$v; + } + } + return $erg; + } + + function ticketregeln_list() { + $this->app->erp->MenuEintrag("index.php?module=ticketregeln&action=list", "Übersicht"); + $this->app->erp->MenuEintrag("index.php?module=ticketregeln&action=create", "Neu anlegen"); + + $this->app->erp->MenuEintrag("index.php", "Zurück"); + + $this->app->YUI->TableSearch('TAB1', 'ticketregeln_list', "show", "", "", basename(__FILE__), __CLASS__); + $this->app->Tpl->Parse('PAGE', "ticketregeln_list.tpl"); + } + + public function ticketregeln_delete() { + $id = (int) $this->app->Secure->GetGET('id'); + + $this->app->DB->Delete("DELETE FROM `ticket_regeln` WHERE `id` = '{$id}'"); + $this->app->Tpl->Set('MESSAGE', "
Der Eintrag wurde gelöscht.
"); + + $this->ticketregeln_list(); + } + + /* + * Edit ticketregeln item + * If id is empty, create a new one + */ + + function ticketregeln_edit() { + $id = $this->app->Secure->GetGET('id'); + + $this->app->Tpl->Set('ID', $id); + + $this->app->erp->MenuEintrag("index.php?module=ticketregeln&action=edit&id=$id", "Details"); + $this->app->erp->MenuEintrag("index.php?module=ticketregeln&action=list", "Zurück zur Übersicht"); + $id = $this->app->Secure->GetGET('id'); + $input = $this->GetInput(); + $submit = $this->app->Secure->GetPOST('submit'); + + if (empty($id)) { + // New item + $id = 'NULL'; + } + + if ($submit != '') + { + + // Write to database + + // Add checks here + $input['warteschlange'] = explode(" ",$input['warteschlange'])[0]; // Just the label + + $columns = "id, "; + $values = "$id, "; + $update = ""; + + $fix = ""; + + foreach ($input as $key => $value) { + $columns = $columns.$fix.$key; + $values = $values.$fix."'".$value."'"; + $update = $update.$fix.$key." = '$value'"; + + $fix = ", "; + } + +// echo($columns."
"); +// echo($values."
"); +// echo($update."
"); + + $sql = "INSERT INTO ticket_regeln (".$columns.") VALUES (".$values.") ON DUPLICATE KEY UPDATE ".$update; + +// echo($sql); + + $this->app->DB->Update($sql); + + if ($id == 'NULL') { + $msg = $this->app->erp->base64_url_encode("
Das Element wurde erfolgreich angelegt.
"); + header("Location: index.php?module=ticketregeln&action=list&msg=$msg"); + } else { + $this->app->Tpl->Set('MESSAGE', "
Die Einstellungen wurden erfolgreich übernommen.
"); + } + } + + + // Load values again from database + $result = $this->app->DB->SelectArr("SELECT t.id, t.empfaenger_email, t.sender_email, t.name, t.betreff, t.spam, t.persoenlich, t.prio, t.dsgvo, t.warteschlange, t.aktiv, t.id FROM ticket_regeln t"." WHERE id=$id"); + + foreach ($result[0] as $key => $value) { + $this->app->Tpl->Set(strtoupper($key), $value); + } + + /* + * Add displayed items later + * + + $this->app->Tpl->Add('KURZUEBERSCHRIFT2', $email); + $this->app->Tpl->Add('EMAIL', $email); + $this->app->Tpl->Add('ANGEZEIGTERNAME', $angezeigtername); + */ + + $this->app->YUI->AutoComplete("warteschlange","warteschlangename"); + + +// $this->SetInput($input); + $this->app->Tpl->Parse('PAGE', "ticketregeln_edit.tpl"); + } + + /** + * Get all paramters from html form and save into $input + */ + public function GetInput(): array { + $input = array(); + //$input['EMAIL'] = $this->app->Secure->GetPOST('email'); + + $input['empfaenger_email'] = $this->app->Secure->GetPOST('empfaenger_email'); + $input['sender_email'] = $this->app->Secure->GetPOST('sender_email'); + $input['name'] = $this->app->Secure->GetPOST('name'); + $input['betreff'] = $this->app->Secure->GetPOST('betreff'); + $input['spam'] = $this->app->Secure->GetPOST('spam'); + $input['persoenlich'] = $this->app->Secure->GetPOST('persoenlich'); + $input['prio'] = $this->app->Secure->GetPOST('prio'); + $input['dsgvo'] = $this->app->Secure->GetPOST('dsgvo'); + $input['warteschlange'] = $this->app->Secure->GetPOST('warteschlange'); + $input['aktiv'] = $this->app->Secure->GetPOST('aktiv'); + + + return $input; + } + + /* + * Set all fields in the page corresponding to $input + */ + function SetInput($input) { + // $this->app->Tpl->Set('EMAIL', $input['email']); + + $this->app->Tpl->Set('EMPFAENGER_EMAIL', $input['empfaenger_email']); + $this->app->Tpl->Set('SENDER_EMAIL', $input['sender_email']); + $this->app->Tpl->Set('NAME', $input['name']); + $this->app->Tpl->Set('BETREFF', $input['betreff']); + $this->app->Tpl->Set('SPAM', $input['spam']); + $this->app->Tpl->Set('PERSOENLICH', $input['persoenlich']); + $this->app->Tpl->Set('PRIO', $input['prio']); + $this->app->Tpl->Set('DSGVO', $input['dsgvo']); + $this->app->Tpl->Set('WARTESCHLANGE', $input['warteschlange']); + $this->app->Tpl->Set('AKTIV', $input['aktiv']); + + } + +}