diff --git a/htdocs/core/class/html.formticket.class.php b/htdocs/core/class/html.formticket.class.php index 40ab8d98c1aa1..1e88cdbf04fca 100644 --- a/htdocs/core/class/html.formticket.class.php +++ b/htdocs/core/class/html.formticket.class.php @@ -6,6 +6,7 @@ * Copyright (C) 2021 Alexandre Spangaro * Copyright (C) 2023 Charlene Benke * Copyright (C) 2024 MDW + * Copyright (C) 2024 Irvine FLEITH * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -150,6 +151,26 @@ public function __construct($db) //$this->withtopicreadonly=0; } + /** + * + * Check required fields + * + * @param array> $fields Array of fields to check + * @param int $errors Reference of errors variable + * @return void + */ + public static function checkRequiredFields(array $fields, int &$errors) + { + global $langs; + + foreach ($fields as $field => $type) { + if (!GETPOST($field, $type['check'])) { + $errors++; + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities($type['langs'])), null, 'errors'); + } + } + } + /** * Show the form to input ticket * @@ -158,15 +179,38 @@ public function __construct($db) * @param int $public 1=If we show the form for the public interface * @param Contact|null $with_contact [=NULL] Contact to link to this ticket if it exists * @param string $action [=''] Action in card + * @param ?Ticket $object [=NULL] Ticket object * @return void */ - public function showForm($withdolfichehead = 0, $mode = 'edit', $public = 0, Contact $with_contact = null, $action = '') + public function showForm($withdolfichehead = 0, $mode = 'edit', $public = 0, Contact $with_contact = null, $action = '', Ticket $object = null) { global $conf, $langs, $user, $hookmanager; // Load translation files required by the page $langs->loadLangs(array('other', 'mails', 'ticket')); + if ($mode == 'create') { + $ref = GETPOSTISSET("ref") ? GETPOST("ref", 'alpha') : ''; + $type_code = GETPOSTISSET('type_code') ? GETPOST('type_code', 'alpha') : ''; + $category_code = GETPOSTISSET('category_code') ? GETPOST('category_code', 'alpha') : ''; + $severity_code = GETPOSTISSET('severity_code') ? GETPOST('severity_code', 'alpha') : ''; + $subject = GETPOSTISSET('subject') ? GETPOST('subject', 'alpha') : ''; + $email = GETPOSTISSET('email') ? GETPOST('email', 'alpha') : ''; + $msg = GETPOSTISSET('message') ? GETPOST('message', 'restricthtml') : ''; + $projectid = GETPOSTISSET('projectid') ? GETPOSTINT('projectid', 'int') : ''; + $user_assign = GETPOSTISSET('fk_user_assign') ? GETPOSTINT('fk_user_assign') : $this->fk_user_create; + } else { + $ref = GETPOSTISSET("ref") ? GETPOST("ref", 'alpha') : $object->ref; + $type_code = GETPOSTISSET('type_code') ? GETPOST('type_code', 'alpha') : $object->type_code; + $category_code = GETPOSTISSET('category_code') ? GETPOST('category_code', 'alpha') : $object->category_code; + $severity_code = GETPOSTISSET('severity_code') ? GETPOST('severity_code', 'alpha') : $object->severity_code; + $subject = GETPOSTISSET('subject') ? GETPOST('subject', 'alpha') : $object->subject; + $email = GETPOSTISSET('email') ? GETPOST('email', 'alpha') : $object->email_from; + $msg = GETPOSTISSET('message') ? GETPOST('message', 'restricthtml') : $object->message; + $projectid = GETPOSTISSET('projectid') ? GETPOSTINT('projectid', 'int') : $object->fk_project; + $user_assign = GETPOSTISSET('fk_user_assign') ? GETPOSTINT('fk_user_assign') : $object->fk_user_assign; + } + $form = new Form($this->db); $formcompany = new FormCompany($this->db); $ticketstatic = new Ticket($this->db); @@ -190,6 +234,7 @@ public function showForm($withdolfichehead = 0, $mode = 'edit', $public = 0, Con print 'param["returnurl"] : $_SERVER['PHP_SELF']).'">'; print ''; print ''; + if (!empty($object->id)) print ''; print ''; foreach ($this->param as $key => $value) { print ''; @@ -201,13 +246,16 @@ public function showForm($withdolfichehead = 0, $mode = 'edit', $public = 0, Con if ($this->withref) { // Ref $defaultref = $ticketstat->getDefaultRef(); + + if ($mode == 'edit') { + $defaultref = $object->ref; + } print ''.$langs->trans("Ref").''; - print ''; + print ''; print ''; } // TITLE - $email = GETPOSTISSET('email') ? GETPOST('email', 'alphanohtml') : ''; if ($this->withemail) { print ''.$langs->trans("Email").''; print ''; @@ -215,7 +263,7 @@ public function showForm($withdolfichehead = 0, $mode = 'edit', $public = 0, Con if ($with_contact) { // contact search and result - $html_contact_search = ''; + $html_contact_search = ''; $html_contact_search .= ''; $html_contact_search .= ''; $html_contact_search .= '' . $langs->trans('Contact') . ''; @@ -231,19 +279,19 @@ public function showForm($withdolfichehead = 0, $mode = 'edit', $public = 0, Con $html_contact_lastname .= ''; print $html_contact_lastname; // contact firstname - $html_contact_firstname = ''; + $html_contact_firstname = ''; $html_contact_firstname .= '' . $langs->trans('Firstname') . ''; $html_contact_firstname .= ''; $html_contact_firstname .= ''; print $html_contact_firstname; // company name - $html_company_name = ''; + $html_company_name = ''; $html_company_name .= '' . $langs->trans('Company') . ''; $html_company_name .= ''; $html_company_name .= ''; print $html_company_name; // contact phone - $html_contact_phone = ''; + $html_contact_phone = ''; $html_contact_phone .= '' . $langs->trans('Phone') . ''; $html_contact_phone .= ''; $html_contact_phone .= ''; @@ -334,7 +382,7 @@ function(response) { // Type of Ticket print ''.$langs->trans("TicketTypeRequest").''; - $this->selectTypesTickets((GETPOST('type_code', 'alpha') ? GETPOST('type_code', 'alpha') : $this->type_code), 'type_code', '', 2, 1, 0, 0, 'minwidth200'); + $this->selectTypesTickets($type_code, 'type_code', '', 2, 1, 0, 0, 'minwidth200'); print ''; // Group => Category @@ -343,13 +391,12 @@ function(response) { if ($public) { $filter = 'public=1'; } - $selected = (GETPOST('category_code') ? GETPOST('category_code') : $this->category_code); - $this->selectGroupTickets($selected, 'category_code', $filter, 2, 1, 0, 0, 'minwidth200'); + $this->selectGroupTickets($category_code, 'category_code', $filter, 2, 1, 0, 0, 'minwidth200'); print ''; // Severity => Priority print ''.$langs->trans("TicketSeverity").''; - $this->selectSeveritiesTickets((GETPOST('severity_code') ? GETPOST('severity_code') : $this->severity_code), 'severity_code', '', 2, 1); + $this->selectSeveritiesTickets($severity_code, 'severity_code', '', 2, 1); print ''; if (isModEnabled('knowledgemanagement')) { @@ -423,8 +470,6 @@ function groupticketchange() { } else { if (isset($this->withreadid) && $this->withreadid > 0) { $subject = $langs->trans('SubjectAnswerToTicket').' '.$this->withreadid.' : '.$this->topic_title; - } else { - $subject = GETPOST('subject', 'alpha'); } print 'withemail) ? ' autofocus' : '').' />'; } @@ -432,7 +477,6 @@ function groupticketchange() { } // MESSAGE - $msg = GETPOSTISSET('message') ? GETPOST('message', 'restricthtml') : ''; print ''.$langs->trans("Message").''; // If public form, display more information @@ -611,17 +655,18 @@ function(response) { }); '; } - - // Contact and type - print ''.$langs->trans("Contact").''; - // If no socid, set to -1 to avoid full contacts list - $selectedCompany = ($this->withfromsocid > 0) ? $this->withfromsocid : -1; - print img_picto('', 'contact', 'class="paddingright"'); - // @phan-suppress-next-line PhanPluginSuspiciousParamOrder - print $form->selectcontacts($selectedCompany, $this->withfromcontactid, 'contactid', 3, '', '', 0, 'minwidth200'); - print ' '; - $formcompany->selectTypeContact($ticketstatic, '', 'type', 'external', '', 0, 'maginleftonly'); - print ''; + if ($mode == 'create') { + // Contact and type + print ''.$langs->trans("Contact").''; + // If no socid, set to -1 to avoid full contacts list + $selectedCompany = ($this->withfromsocid > 0) ? $this->withfromsocid : -1; + print img_picto('', 'contact', 'class="paddingright"'); + // @phan-suppress-next-line PhanPluginSuspiciousParamOrder + print $form->selectcontacts($selectedCompany, $this->withfromcontactid, 'contactid', 3, '', '', 0, 'minwidth200'); + print ' '; + $formcompany->selectTypeContact($ticketstatic, '', 'type', 'external', '', 0, 'maginleftonly'); + print ''; + } } else { print ''; print ''; @@ -629,7 +674,7 @@ function(response) { } // Notify thirdparty at creation - if (empty($this->ispublic)) { + if (empty($this->ispublic) && $action == 'create') { print ''.$langs->trans("TicketNotifyTiersAtCreation").''; print 'withnotifytiersatcreate ? ' checked="checked"' : '').'>'; print ''; @@ -640,7 +685,7 @@ function(response) { print $langs->trans("AssignedTo"); print ''; print img_picto('', 'user', 'class="pictofixedwidth"'); - print $form->select_dolusers(GETPOSTINT('fk_user_assign'), 'fk_user_assign', 1); + print $form->select_dolusers($user_assign, 'fk_user_assign', 1); print ''; print ''; } @@ -649,7 +694,7 @@ function(response) { if (isModEnabled('project') && !$this->ispublic) { $formproject = new FormProjets($this->db); print ''.$langs->trans("Project").''; - print img_picto('', 'project').$formproject->select_projects(-1, GETPOSTINT('projectid'), 'projectid', 0, 0, 1, 1, 0, 0, 0, '', 1, 0, 'maxwidth500'); + print img_picto('', 'project').$formproject->select_projects(-1, $projectid, 'projectid', 0, 0, 1, 1, 0, 0, 0, '', 1, 0, 'maxwidth500'); print ''; } } @@ -668,19 +713,26 @@ function(response) { $parameters = array(); $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $ticketstat, $action); // Note that $action and $object may have been modified by hook if (empty($reshook)) { - print $ticketstat->showOptionals($extrafields, 'create'); + if ($mode == 'create') { + print $object->showOptionals($extrafields, 'create'); + } else { + print $object->showOptionals($extrafields, 'edit'); + } } print ''; if ($withdolfichehead) { print dol_get_fiche_end(); - } else { - print ''; } + print ''; - print $form->buttonsSaveCancel(((isset($this->withreadid) && $this->withreadid > 0) ? "SendResponse" : "CreateTicket"), ($this->withcancel ? "Cancel" : "")); + if ($mode == 'create') { + print $form->buttonsSaveCancel(((isset($this->withreadid) && $this->withreadid > 0) ? "SendResponse" : "CreateTicket"), ($this->withcancel ? "Cancel" : "")); + } else { + print $form->buttonsSaveCancel(((isset($this->withreadid) && $this->withreadid > 0) ? "SendResponse" : "Save"), ($this->withcancel ? "Cancel" : "")); + } /* print ''; diff --git a/htdocs/ticket/card.php b/htdocs/ticket/card.php index 311b17beb4b96..4e0cf72d89261 100644 --- a/htdocs/ticket/card.php +++ b/htdocs/ticket/card.php @@ -7,6 +7,7 @@ * Copyright (C) 2022-2023 Charlene Benke * Copyright (C) 2023 Benjamin Falière * Copyright (C) 2024 MDW + * Copyright (C) 2024 Irvine FLEITH * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -164,151 +165,164 @@ $search_agenda_label = ''; } - $backurlforlist = DOL_URL_ROOT.'/ticket/list.php'; + $backurlforlist = DOL_URL_ROOT . '/ticket/list.php'; if (empty($backtopage) || ($cancel && empty($id))) { if (empty($backtopage) || ($cancel && strpos($backtopage, '__ID__'))) { if (empty($id) && (($action != 'add' && $action != 'create') || $cancel)) { $backtopage = $backurlforlist; } else { - $backtopage = DOL_URL_ROOT.'/ticket/card.php?id='.((!empty($id) && $id > 0) ? $id : '__ID__'); + $backtopage = DOL_URL_ROOT . '/ticket/card.php?id=' . ((!empty($id) && $id > 0) ? $id : '__ID__'); } } } if ($cancel) { if (!empty($backtopageforcancel)) { - header("Location: ".$backtopageforcancel); + header("Location: " . $backtopageforcancel); exit; } elseif (!empty($backtopage)) { - header("Location: ".$backtopage); + header("Location: " . $backtopage); exit; } $action = 'view'; } - // Action to add an action (not a message) - if (GETPOST('save', 'alpha') && $permissiontoadd) { + if (($action == 'add' || ($action == 'update' && $object->status < Ticket::STATUS_CLOSED)) && $permissiontoadd) { + $ifErrorAction = $action == 'add' ? 'create' : 'edit'; + if ($action == 'add') $object->track_id = null; $error = 0; - if (!GETPOST("type_code", 'alpha')) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TicketTypeRequest")), null, 'errors'); - $action = 'create'; - } elseif (!GETPOST("category_code", 'alpha')) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TicketCategory")), null, 'errors'); - $action = 'create'; - } elseif (!GETPOST("severity_code", 'alpha')) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("TicketSeverity")), null, 'errors'); - $action = 'create'; - } elseif (!GETPOST("subject", 'alphanohtml')) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Subject")), null, 'errors'); - $action = 'create'; - } elseif (!GETPOST("message", 'restricthtml')) { - $error++; - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentities("Message")), null, 'errors'); - $action = 'create'; + $fieldsToCheck = [ + 'ref' => ['check' => 'alpha', 'langs' => 'Ref'], + 'type_code' => ['check' => 'alpha', 'langs' => 'TicketTypeRequest'], + 'category_code' => ['check' => 'alpha', 'langs' => 'TicketCategory'], + 'severity_code' => ['check' => 'alpha', 'langs' => 'TicketSeverity'], + 'subject' => ['check' => 'alphanohtml', 'langs' => 'Subject'], + 'message' => ['check' => 'restricthtml', 'langs' => 'Message'] + ]; + + FormTicket::checkRequiredFields($fieldsToCheck, $error); + + if (!empty($error)) { + $action = $ifErrorAction; } + $ret = $extrafields->setOptionalsFromPost(null, $object); if ($ret < 0) { $error++; } + $getRef = GETPOST('ref', 'alpha'); - if (!$error) { - $db->begin(); + if (!empty($getRef)) { + $isExistingRef = $object->checkExistingRef($action, $getRef); + } else { + $isExistingRef = true; + } - $getRef = GETPOST("ref", 'alphanohtml'); - $test = new Ticket($db); - if ($test->fetch('', $getRef) > 0) { + $style = ''; + + if ($isExistingRef) { + if ($action == 'update') { + $error++; + $action = 'edit'; + $style = 'errors'; + } elseif ($action == 'add') { $object->ref = $object->getDefaultRef(); $object->track_id = null; - setEventMessage($langs->trans('TicketRefAlreadyUsed', $getRef, $object->ref)); - } else { - $object->ref = $getRef; + $style = 'warnings'; } + if (!empty($getRef)) { + setEventMessage($langs->trans('TicketRefAlreadyUsed', $getRef, $object->ref), $style); + } + } + if (!$error) { + $db->begin(); - $object->fk_soc = $object->socid = GETPOSTINT("socid") > 0 ? GETPOSTINT("socid") : 0; - $object->subject = GETPOST("subject", 'alphanohtml'); - $object->message = GETPOST("message", 'restricthtml'); - - $object->type_code = GETPOST("type_code", 'alpha'); - $object->type_label = $langs->trans($langs->getLabelFromKey($db, $object->type_code, 'c_ticket_type', 'code', 'label')); - $object->category_code = GETPOST("category_code", 'alpha'); - $object->category_label = $langs->trans($langs->getLabelFromKey($db, $object->category_code, 'c_ticket_category', 'code', 'label')); - $object->severity_code = GETPOST("severity_code", 'alpha'); - $object->severity_label = $langs->trans($langs->getLabelFromKey($db, $object->severity_code, 'c_ticket_severity', 'code', 'label')); - $object->fk_user_create = $user->id; - $object->email_from = $user->email; - $object->origin_email = null; - - $notifyTiers = GETPOST("notify_tiers_at_create", 'alpha'); - $object->notify_tiers_at_create = empty($notifyTiers) ? 0 : 1; + $object->type_code = GETPOST('type_code', 'alpha'); + $object->category_code = GETPOST('category_code', 'alpha'); + $object->severity_code = GETPOST('severity_code', 'alpha'); + $object->subject = GETPOST('subject', 'alpha'); + $object->message = GETPOST('message', 'restricthtml'); + $object->fk_soc = GETPOSTINT('socid'); + $fk_user_assign = GETPOSTINT('fk_user_assign'); + $object->fk_project = GETPOSTINT('projectid'); + $object->fk_contract = GETPOSTINT('fk_contract'); - $fk_user_assign = GETPOSTINT("fk_user_assign"); if ($fk_user_assign > 0) { - $object->fk_user_assign = $fk_user_assign; - $object->status = $object::STATUS_ASSIGNED; + $object->fk_user_assign = $fk_user_assign; + $object->status = $object::STATUS_ASSIGNED; } - $object->fk_project = $projectid; - $object->fk_contract = GETPOSTINT('fk_contract'); - - $object->context['contact_id'] = GETPOSTINT('contact_id'); + if ($action == 'add') { + $object->type_code = GETPOST("type_code", 'alpha'); + $object->type_label = $langs->trans($langs->getLabelFromKey($db, $object->type_code, 'c_ticket_type', 'code', 'label')); + $object->category_label = $langs->trans($langs->getLabelFromKey($db, $object->category_code, 'c_ticket_category', 'code', 'label')); + $object->severity_label = $langs->trans($langs->getLabelFromKey($db, $object->severity_code, 'c_ticket_severity', 'code', 'label')); + $object->fk_user_create = $user->id; + $object->email_from = $user->email; + $object->origin_email = null; + $notifyTiers = GETPOST("notify_tiers_at_create", 'alpha'); + $object->notify_tiers_at_create = empty($notifyTiers) ? 0 : 1; + $object->context['contact_id'] = GETPOSTINT('contact_id'); + $id = $object->create($user); + } else { + $id = $object->update($user); + } - $id = $object->create($user); if ($id <= 0) { $error++; setEventMessages($object->error, $object->errors, 'errors'); - $action = 'create'; + $action = $ifErrorAction; } if (!$error) { - // Add contact - $contactid = GETPOSTINT('contactid'); - $type_contact = GETPOST("type", 'alpha'); - // Category association $categories = GETPOST('categories', 'array'); $object->setCategories($categories); + } - if ($contactid > 0 && $type_contact) { - $typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type')); - $result = $object->add_contact($contactid, $typeid, 'external'); - } + if ($action == 'add') { + if (!$error) { + // Add contact + $contactid = GETPOSTINT('contactid'); + $type_contact = GETPOST("type", 'alpha'); - // Link ticket to project - if (GETPOST('origin', 'alpha') == 'projet') { - $projectid = GETPOSTINT('originid'); - } else { - $projectid = GETPOSTINT('projectid'); - } + if ($contactid > 0 && $type_contact) { + $typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type')); + $result = $object->add_contact($contactid, $typeid, 'external'); + } - if ($projectid > 0) { - $object->setProject($projectid); - } + // Link ticket to project + if (GETPOST('origin', 'alpha') == 'projet') { + $projectid = GETPOSTINT('originid'); + } else { + $projectid = GETPOSTINT('projectid'); + } - // Auto mark as read if created from backend - if (getDolGlobalString('TICKET_AUTO_READ_WHEN_CREATED_FROM_BACKEND') && $user->hasRight('ticket', 'write')) { - if (! $object->markAsRead($user) > 0) { - setEventMessages($object->error, $object->errors, 'errors'); + if ($projectid > 0) { + $object->setProject($projectid); + } + + // Auto mark as read if created from backend + if (getDolGlobalString('TICKET_AUTO_READ_WHEN_CREATED_FROM_BACKEND') && $user->hasRight('ticket', 'write')) { + if (!$object->markAsRead($user) > 0) { + setEventMessages($object->error, $object->errors, 'errors'); + } } - } - // Auto assign user - if (getDolGlobalString('TICKET_AUTO_ASSIGN_USER_CREATE')) { - $result = $object->assignUser($user, $user->id, 1); - $object->add_contact($user->id, "SUPPORTTEC", 'internal'); + // Auto assign user + if (getDolGlobalString('TICKET_AUTO_ASSIGN_USER_CREATE')) { + $result = $object->assignUser($user, $user->id, 1); + $object->add_contact($user->id, "SUPPORTTEC", 'internal'); + } } - } - if (!$error) { - // File transfer - $object->copyFilesForTicket(''); // trackid is forced to '' because files were uploaded when no id for ticket exists yet and trackid was '' + if (!$error) { + // File transfer + $object->copyFilesForTicket(''); // trackid is forced to '' because files were uploaded when no id for ticket exists yet and trackid was '' + } } - if (!$error) { $db->commit(); @@ -316,86 +330,19 @@ if (empty($id)) { $url = $backtopage; } else { - $url = 'card.php?track_id='.urlencode($object->track_id); + $url = 'card.php?track_id=' . urlencode($object->track_id); } } else { - $url = 'card.php?track_id='.urlencode($object->track_id); + $url = 'card.php?track_id=' . urlencode($object->track_id); } - header("Location: ".$url); + header("Location: " . $url); exit; } else { $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); } - } else { - setEventMessages($object->error, $object->errors, 'errors'); - $action = 'create'; - } - } - - if ($action == 'update' && $permissiontoadd && $object->status < Ticket::STATUS_CLOSED) { - $error = 0; - - $ret = $object->fetch(GETPOSTINT('id'), GETPOSTINT('ref'), GETPOST('track_id', 'alpha')); - if ($ret < 0) { - $error++; - array_push($object->errors, $langs->trans('ErrorTicketIsNotValid')); - } - - // check fields - if (!$error) { - if (!GETPOST('subject', 'alpha')) { - $error++; - array_push($object->errors, $langs->trans('ErrorFieldRequired', $langs->transnoentities('Subject'))); - } - $ret = $extrafields->setOptionalsFromPost(null, $object); - if ($ret < 0) { - $error++; - } - } - - if (!$error) { - $db->begin(); - - $object->subject = GETPOST('subject', 'alpha'); - $object->type_code = GETPOST('type_code', 'alpha'); - $object->category_code = GETPOST('category_code', 'alpha'); - $object->severity_code = GETPOST('severity_code', 'alpha'); - - $ret = $object->update($user); - if ($ret > 0) { - // Category association - $categories = GETPOST('categories', 'array'); - $object->setCategories($categories); - } else { - $error++; - } - - if ($error) { - $db->rollback(); - } else { - $db->commit(); - } - } - - if ($error) { - setEventMessages($object->error, $object->errors, 'errors'); - $action = 'edit'; - } else { - if (!empty($backtopage)) { - if (empty($id)) { - $url = $backtopage; - } else { - $url = 'card.php?track_id='.urlencode($object->track_id); - } - } else { - $url = 'card.php?track_id='.urlencode($object->track_id); - } - - header('Location: '.$url); - exit(); - } + } else $action = $ifErrorAction; } // Mark as Read @@ -405,7 +352,7 @@ if ($object->markAsRead($user) > 0) { setEventMessages($langs->trans('TicketMarkedAsRead'), null, 'mesgs'); - header("Location: card.php?track_id=".$object->track_id); + header("Location: card.php?track_id=" . $object->track_id); exit; } else { setEventMessages($object->error, $object->errors, 'errors'); @@ -461,7 +408,7 @@ $object->fetch_user($usertoassign); setEventMessages($langs->trans('TicketAssigned'), null, 'mesgs'); - header("Location: card.php?track_id=".$object->track_id); + header("Location: card.php?track_id=" . $object->track_id); exit; } else { array_push($object->errors, $object->error); @@ -478,10 +425,10 @@ if (!empty($backtopage)) { $url = $backtopage; } else { - $url = 'card.php?track_id='.urlencode($object->track_id); + $url = 'card.php?track_id=' . urlencode($object->track_id); } - header("Location: ".$url); + header("Location: " . $url); exit; } else { setEventMessages($object->error, $object->errors, 'errors'); @@ -495,8 +442,8 @@ if ($object->close($user, ($action == "confirm_abandon" ? 1 : 0))) { setEventMessages($langs->trans('TicketMarkedAsClosed'), null, 'mesgs'); - $url = 'card.php?track_id='.GETPOST('track_id', 'alpha'); - header("Location: ".$url); + $url = 'card.php?track_id=' . GETPOST('track_id', 'alpha'); + header("Location: " . $url); exit; } else { $action = ''; @@ -511,10 +458,10 @@ $object->close($user); - setEventMessages(''.$langs->trans('TicketMarkedAsClosed').'', null, 'mesgs'); + setEventMessages('' . $langs->trans('TicketMarkedAsClosed') . '', null, 'mesgs'); - $url = 'card.php?track_id='.GETPOST('track_id', 'alpha'); - header("Location: ".$url); + $url = 'card.php?track_id=' . GETPOST('track_id', 'alpha'); + header("Location: " . $url); exit; } else { setEventMessages($object->error, $object->errors, 'errors'); @@ -525,12 +472,12 @@ if ($action == 'confirm_delete_ticket' && GETPOST('confirm', 'alpha') == "yes" && $permissiontodelete) { if ($object->fetch(GETPOSTINT('id'), '', GETPOST('track_id', 'alpha')) >= 0) { if ($object->delete($user) > 0) { - setEventMessages(''.$langs->trans('TicketDeletedSuccess').'', null, 'mesgs'); - header("Location: ".DOL_URL_ROOT."/ticket/list.php"); + setEventMessages('' . $langs->trans('TicketDeletedSuccess') . '', null, 'mesgs'); + header("Location: " . DOL_URL_ROOT . "/ticket/list.php"); exit; } else { $langs->load("errors"); - $mesg = ''.$langs->trans($object->error).''; + $mesg = '' . $langs->trans($object->error) . ''; $action = ''; } } @@ -540,8 +487,8 @@ if ($action == 'set_thirdparty' && $user->hasRight('ticket', 'write')) { if ($object->fetch(GETPOSTINT('id'), '', GETPOST('track_id', 'alpha')) >= 0) { $result = $object->setCustomer(GETPOSTINT('editcustomer')); - $url = $_SERVER["PHP_SELF"].'?track_id='.GETPOST('track_id', 'alpha'); - header("Location: ".$url); + $url = $_SERVER["PHP_SELF"] . '?track_id=' . GETPOST('track_id', 'alpha'); + header("Location: " . $url); exit(); } } @@ -551,8 +498,8 @@ if ($object->fetch(GETPOSTINT('id'), '', GETPOST('track_id', 'alpha')) >= 0) { $result = $object->setProgression(GETPOST('progress', 'alpha')); - $url = 'card.php?track_id='.$object->track_id; - header("Location: ".$url); + $url = 'card.php?track_id=' . $object->track_id; + header("Location: " . $url); exit(); } } @@ -562,8 +509,8 @@ if ($object->fetch(GETPOSTINT('id'), '', GETPOST('track_id', 'alpha')) >= 0) { $result = $object->setCategories(GETPOST('categories', 'array')); - $url = 'card.php?track_id='.$object->track_id; - header("Location: ".$url); + $url = 'card.php?track_id=' . $object->track_id; + header("Location: " . $url); exit(); } } @@ -581,14 +528,14 @@ } if (!$error) { - if ($object->update($user) >= 0) { - header("Location: ".$_SERVER['PHP_SELF']."?track_id=".$object->track_id); - exit; - } else { + if (!$object->update($user) >= 0) { $error++; setEventMessages($object->error, $object->errors, 'errors'); } } + + header("Location: " . $_SERVER['PHP_SELF'] . "?track_id=" . $object->track_id); + exit; } } @@ -602,8 +549,8 @@ $res = $object->setStatut(Ticket::STATUS_NOT_READ); } if ($res) { - $url = 'card.php?track_id='.$object->track_id; - header("Location: ".$url); + $url = 'card.php?track_id=' . $object->track_id; + header("Location: " . $url); exit(); } else { $error++; @@ -615,16 +562,16 @@ // Categorisation dans projet if ($object->fetch(GETPOSTINT('id'), '', GETPOST('track_id', 'alpha')) >= 0) { $object->setProject($projectid); - $url = 'card.php?track_id='.$object->track_id; - header("Location: ".$url); + $url = 'card.php?track_id=' . $object->track_id; + header("Location: " . $url); exit(); } } elseif ($action == 'setcontract' && $permissiontoadd) { // Categorisation dans contrat if ($object->fetch(GETPOSTINT('id'), '', GETPOST('track_id', 'alpha')) >= 0) { $object->setContract(GETPOSTINT('contractid')); - $url = 'card.php?track_id='.$object->track_id; - header("Location: ".$url); + $url = 'card.php?track_id=' . $object->track_id; + header("Location: " . $url); exit(); } } elseif ($action == "set_message" && $user->hasRight('ticket', 'manage')) { @@ -655,8 +602,8 @@ //$old_status = $object->status; $res = $object->setStatut($new_status); if ($res) { - $url = 'card.php?track_id='.$object->track_id; - header("Location: ".$url); + $url = 'card.php?track_id=' . $object->track_id; + header("Location: " . $url); exit(); } else { $error++; @@ -709,17 +656,17 @@ $permissiondellink = $user->hasRight('ticket', 'write'); - include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once + include DOL_DOCUMENT_ROOT . '/core/actions_dellink.inc.php'; // Must be include, not include_once // Actions to build doc - include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php'; + include DOL_DOCUMENT_ROOT . '/core/actions_builddoc.inc.php'; // Actions to send emails $triggersendname = 'TICKET_SENTBYMAIL'; $paramname = 'id'; $autocopy = 'MAIN_MAIL_AUTOCOPY_TICKET_TO'; // used to know the automatic BCC to add - $trackid = 'tic'.$object->id; - include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php'; + $trackid = 'tic' . $object->id; + include DOL_DOCUMENT_ROOT . '/core/actions_sendmails.inc.php'; // Set $action to correct value for the case we used presend action to add a message if (GETPOSTISSET('actionbis') && $action == 'presend') { @@ -747,6 +694,10 @@ llxHeader('', $title, $help_url); if ($action == 'create' || $action == 'presend') { + if (empty($permissiontoadd)) { + accessforbidden('NotEnoughPermissions', 0, 1); + } + $formticket = new FormTicket($db); print load_fiche_titre($langs->trans('NewTicket'), '', 'ticket'); @@ -765,65 +716,46 @@ $formticket->withcancel = 1; - $formticket->showForm(1, 'create', 0, null, $action); - /*} elseif ($action == 'edit' && $user->hasRight('ticket', 'write') && $object->status < Ticket::STATUS_CLOSED) { + $formticket->showForm(1, 'create', 0, null, $action, $object); + + print dol_get_fiche_end(); +} elseif ($action == 'edit' && $object->status < Ticket::STATUS_CLOSED) { + if (empty($permissiontoadd)) { + accessforbidden('NotEnoughPermissions', 0, 1); + } + $formticket = new FormTicket($db); $head = ticket_prepare_head($object); - print ''; - print ''; - print ''; - print ''; - - print dol_get_fiche_head($head, 'card', $langs->trans('Ticket'), 0, 'ticket'); - - print ''; - print ''; - - // Type - print ''.$langs->trans("TicketTypeRequest").''; - $formticket->selectTypesTickets((GETPOSTISSET('type_code') ? GETPOST('type_code') : $object->type_code), 'type_code', '', '2'); - print ''; - - // Severity - print ''.$langs->trans("TicketSeverity").''; - $formticket->selectSeveritiesTickets((GETPOSTISSET('severity_code') ? GETPOST('severity_code') : $object->severity_code), 'severity_code', '', '2'); - print ''; - - // Group - print ''.$langs->trans("TicketCategory").''; - $formticket->selectGroupTickets((GETPOSTISSET('category_code') ? GETPOST('category_code') : $object->category_code), 'category_code', '', '2'); - print ''; - - // Subject - print ''.$langs->trans("Subject").''; - print ''; - print ''; - - // Other attributes - $parameters = array('colspan' => ' colspan="3"', 'colspanvalue' => '3'); - $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook - print $hookmanager->resPrint; - if (empty($reshook)) { - print $object->showOptionals($extrafields, 'edit'); - } + print dol_get_fiche_head($head, 'tabTicket', $langs->trans('Ticket'), -1, 'ticket'); - print ''; - print ''; + $formticket->trackid = $object->track_id; // TODO Use a unique key 'tic' to avoid conflict in upload file feature + $formticket->withfromsocid = $object->socid; + $formticket->withtitletopic = 1; + // $formticket->withnotifytiersatcreate = ($notifyTiers ? 1 : (getDolGlobalString('TICKET_CHECK_NOTIFY_THIRDPARTY_AT_CREATION') ? 1 : 0)); + $formticket->withnotifytiersatcreate = 0; + $formticket->withusercreate = 0; + $formticket->withref = 1; + $formticket->fk_user_create = $user->id; + $formticket->withfile = 0; + $formticket->action = 'update'; + $formticket->withextrafields = 1; + $formticket->param = array('origin' => GETPOST('origin'), 'originid' => GETPOST('originid')); - print dol_get_fiche_end(); + $formticket->withcancel = 1; - print $form->buttonsSaveCancel(); + $formticket->showForm(0, 'edit', 0, null, $action, $object); - print ''; */ + print dol_get_fiche_end(); } elseif (empty($action) || in_array($action, ['builddoc', 'view', 'addlink', 'dellink', 'presend', 'presend_addmessage', 'close', 'abandon', 'delete', 'editcustomer', 'progression', 'categories', 'reopen', 'edit_contrat', 'editsubject', 'edit_extras', 'update_extras', 'edit_extrafields', 'set_extrafields', 'classify', 'sel_contract', 'edit_message_init', 'set_status', 'dellink'])) { - if ($res > 0) { + if (!empty($res) && $res > 0) { // or for unauthorized internals users if (!$user->socid && (getDolGlobalString('TICKET_LIMIT_VIEW_ASSIGNED_ONLY') && $object->fk_user_assign != $user->id) && !$user->hasRight('ticket', 'manage')) { accessforbidden('', 0, 1); } + // Confirmation close if ($action == 'close') { $thirdparty_contacts = $object->getInfosTicketExternalContact(1); @@ -1506,14 +1438,17 @@ print dolGetButtonAction('', $langs->trans('ReOpen'), 'default', $_SERVER["PHP_SELF"].'?action=reopen&token='.newToken().'&track_id='.$object->track_id, ''); } + // Edit ticket + if ($permissiontoedit) { + print dolGetButtonAction('', $langs->trans('Modify'), 'default', $_SERVER["PHP_SELF"].'?id='.$object->id.'&action=edit&token='.newToken(), '', $permissiontoedit); + } + // Delete ticket if ($user->hasRight('ticket', 'delete') && !$user->socid) { print dolGetButtonAction('', $langs->trans('Delete'), 'delete', $_SERVER["PHP_SELF"].'?action=delete&token='.newToken().'&track_id='.$object->track_id, ''); } } print ''."\n"; - } else { - //print ''; } // Select mail models is same action as presend diff --git a/htdocs/ticket/class/actions_ticket.class.php b/htdocs/ticket/class/actions_ticket.class.php index a9c988b4386b4..c87ec4e7933b0 100644 --- a/htdocs/ticket/class/actions_ticket.class.php +++ b/htdocs/ticket/class/actions_ticket.class.php @@ -224,7 +224,7 @@ public function viewTicketOriginalMessage($user, $action, $object) } else { print ''; //print dol_escape_htmltag(dol_htmlwithnojs(dol_string_onlythesehtmltags(dol_htmlentitiesbr($object->message), 1, 1, 1, 0)), 1, 1, 'common', 0, 1); - print dolPrintHTML($object->message); + print nl2br($object->message); print ''; /*print ''; print $langs->trans("More").'...'; @@ -418,7 +418,7 @@ public function viewTicketTimelineMessages($show_private, $show_user, Ticket $ob foreach ($object->cache_msgs_ticket as $id => $arraymsgs) { if (!$arraymsgs['private'] - || ($arraymsgs['private'] == "1" && $show_private) + || ($arraymsgs['private'] == "1" && $show_private) ) { print ''; print ''; diff --git a/htdocs/ticket/class/ticket.class.php b/htdocs/ticket/class/ticket.class.php index 776c1941dfa60..5289a2424e477 100644 --- a/htdocs/ticket/class/ticket.class.php +++ b/htdocs/ticket/class/ticket.class.php @@ -485,6 +485,28 @@ private function verify() return $result; } + /** + * + * Check if ref exists or not + * + * @param string $action Action + * @param string $getRef Reference of object + * @return bool + */ + public function checkExistingRef(string $action, string $getRef): bool + { + $test = new self($this->db); + + if ($test->fetch('', $getRef) > 0) { + if (($action == 'add') || ($action == 'update' && $this->ref != $getRef)) { + return true; + } + } + + $this->ref = $getRef; + return false; + } + /** * Create object into database * @@ -2394,23 +2416,23 @@ public function listeContact($statusoflink = -1, $source = 'external', $list = 0 $transkey = "TypeContact_".$obj->element."_".$obj->source."_".$obj->code; $labelType = ($langs->trans($transkey) != $transkey ? $langs->trans($transkey) : $obj->type_contact_label); $tab[$i] = array( - 'source' => $obj->source, - 'socid' => $obj->socid, - 'id' => $obj->id, - 'nom' => $obj->lastname, // For backward compatibility - 'civility' => $obj->civility, - 'lastname' => $obj->lastname, - 'firstname' => $obj->firstname, - 'email' => $obj->email, - 'rowid' => $obj->rowid, - 'code' => $obj->code, - 'libelle' => $labelType, // deprecated, replaced with labeltype - 'labeltype' => $labelType, - 'status' => $obj->statuslink, - 'statuscontact' => $obj->statuscontact, - 'fk_c_type_contact' => $obj->fk_c_type_contact, - 'phone' => $obj->phone, - 'phone_mobile' => $obj->phone_mobile); + 'source' => $obj->source, + 'socid' => $obj->socid, + 'id' => $obj->id, + 'nom' => $obj->lastname, // For backward compatibility + 'civility' => $obj->civility, + 'lastname' => $obj->lastname, + 'firstname' => $obj->firstname, + 'email' => $obj->email, + 'rowid' => $obj->rowid, + 'code' => $obj->code, + 'libelle' => $labelType, // deprecated, replaced with labeltype + 'labeltype' => $labelType, + 'status' => $obj->statuslink, + 'statuscontact' => $obj->statuscontact, + 'fk_c_type_contact' => $obj->fk_c_type_contact, + 'phone' => $obj->phone, + 'phone_mobile' => $obj->phone_mobile); } else { $tab[$i] = $obj->id; }