* Copyright (C) 2016 Christophe Battarel * Copyright (C) 2019 Frédéric France * * 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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/core/class/html.formticket.class.php * \ingroup ticket * \brief Fichier de la classe permettant la generation du formulaire html d'envoi de mail unitaire */ require_once DOL_DOCUMENT_ROOT."/core/class/html.form.class.php"; require_once DOL_DOCUMENT_ROOT."/core/class/html.formmail.class.php"; require_once DOL_DOCUMENT_ROOT."/core/class/html.formprojet.class.php"; if (!class_exists('FormCompany')) { include DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; } /** * Classe permettant la generation du formulaire d'un nouveau ticket. * * @package Ticket * \remarks Utilisation: $formticket = new FormTicket($db) * \remarks $formticket->proprietes=1 ou chaine ou tableau de valeurs * \remarks $formticket->show_form() affiche le formulaire */ class FormTicket { /** * @var DoliDB Database handler. */ public $db; public $track_id; /** * @var int ID */ public $fk_user_create; public $message; public $topic_title; public $action; public $withtopic; public $withemail; /** * * @var int $withsubstit Show substitution array */ public $withsubstit; public $withfile; public $withfilereadonly; public $ispublic; // To show information or not into public form public $withtitletopic; public $withcompany; // affiche liste déroulante company public $withfromsocid; public $withfromcontactid; public $withnotifytiersatcreate; public $withusercreate; // Show name of creating user in form public $withcreatereadonly; public $withref; // Show ref field public $withcancel; /** * * @var array $substit Substitutions */ public $substit = array(); public $param = array(); /** * @var string Error code (or message) */ public $error; /** * Constructor * * @param DoliDB $db Database handler */ public function __construct($db) { $this->db = $db; $this->action = 'add'; $this->withcompany = 1; $this->withfromsocid = 0; $this->withfromcontactid = 0; //$this->withthreadid=0; //$this->withtitletopic=''; $this->withnotifytiersatcreate = 0; $this->withusercreate = 1; $this->withcreatereadonly = 1; $this->withemail = 0; $this->withref = 0; $this->withextrafields = 0; // Show extrafields or not //$this->withtopicreadonly=0; } /** * Show the form to input ticket * * @param int $withdolfichehead With dol_fiche_head * @return void */ public function showForm($withdolfichehead = 0) { global $conf, $langs, $user, $hookmanager; // Load translation files required by the page $langs->loadLangs(array('other', 'mails', 'ticket')); $form = new Form($this->db); $formcompany = new FormCompany($this->db); $ticketstatic = new Ticket($this->db); $soc = new Societe($this->db); if (!empty($this->withfromsocid) && $this->withfromsocid > 0) { $soc->fetch($this->withfromsocid); } $ticketstat = new Ticket($this->db); $extrafields = new ExtraFields($this->db); $extrafields->fetch_name_optionals_label($ticketstat->table_element); print "\n\n"; if ($withdolfichehead) dol_fiche_head(null, 'card', '', 0, ''); print '
'; print ''; print ''; foreach ($this->param as $key => $value) { print ''; } print ''; print ''; if ($this->withref) { // Ref $defaultref = $ticketstat->getDefaultRef(); print ''; } // TITLE if ($this->withemail) { print ''; } // Si origin du ticket if (isset($this->param['origin']) && $this->param['originid'] > 0) { // Parse element/subelement (ex: project_task) $element = $subelement = $this->param['origin']; if (preg_match('/^([^_]+)_([^_]+)/i', $this->param['origin'], $regs)) { $element = $regs[1]; $subelement = $regs[2]; } dol_include_once('/'.$element.'/class/'.$subelement.'.class.php'); $classname = ucfirst($subelement); $objectsrc = new $classname($this->db); $objectsrc->fetch(GETPOST('originid', 'int')); if (empty($objectsrc->lines) && method_exists($objectsrc, 'fetch_lines')) { $objectsrc->fetch_lines(); } $objectsrc->fetch_thirdparty(); $newclassname = $classname; print ''; } // Type print ''; // Severity print ''; // Group print ''; // Subject if ($this->withtitletopic) { print ''; } else { if ($this->withthreadid > 0) { $subject = $langs->trans('SubjectAnswerToTicket').' '.$this->withthreadid.' : '.$this->topic_title.''; } print ''; print ''; } } // MESSAGE $msg = GETPOSTISSET('message') ? GETPOST('message', 'none') : ''; print ''; // FK_USER_CREATE if ($this->withusercreate > 0 && $this->fk_user_create) { print '\n"; } // Customer or supplier if ($this->withcompany) { // altairis: force company and contact id for external user if (empty($user->socid)) { // Company print ''; if (!empty($conf->use_javascript_ajax) && !empty($conf->global->COMPANY_USE_SEARCH_TO_SELECT)) { $htmlname = 'socid'; print ''; } // Contact and type print ''; } else { print ''; print ''; print ''; } // Notify thirdparty at creation if (empty($this->ispublic)) { print ''; } } if (!empty($conf->projet->enabled) && !$this->ispublic) { $formproject = new FormProjets($this->db); print ''; } // Attached files if (!empty($this->withfile)) { // Define list of attached files $listofpaths = array(); $listofnames = array(); $listofmimes = array(); if (!empty($_SESSION["listofpaths"])) { $listofpaths = explode(';', $_SESSION["listofpaths"]); } if (!empty($_SESSION["listofnames"])) { $listofnames = explode(';', $_SESSION["listofnames"]); } if (!empty($_SESSION["listofmimes"])) { $listofmimes = explode(';', $_SESSION["listofmimes"]); } $out = ''; $out .= ''; $out .= '\n"; print $out; } // Other attributes $parameters = array(); $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $ticketstat, $this->action); // Note that $action and $object may have been modified by hook if (empty($reshook)) { print $ticketstat->showOptionals($extrafields, 'edit'); } print '
'.$langs->trans("Ref").'
'; print ''; print '
'.$langs->trans($newclassname).''.$objectsrc->getNomUrl(1).'
'; $this->selectTypesTickets((GETPOST('type_code', 'alpha') ? GETPOST('type_code', 'alpha') : $this->type_code), 'type_code', '', '2', 0, 0, 0, 'minwidth150'); print '
'; $this->selectSeveritiesTickets((GETPOST('severity_code') ? GETPOST('severity_code') : $this->severity_code), 'severity_code', '', '2'); print '
'; $this->selectGroupTickets((GETPOST('category_code') ? GETPOST('category_code') : $this->category_code), 'category_code', '', '2'); print '
'; // Réponse à un ticket : affichage du titre du thread en readonly if ($this->withtopicreadonly) { print $langs->trans('SubjectAnswerToTicket').' '.$this->topic_title; print '
'; // If public form, display more information $toolbarname = 'dolibarr_notes'; if ($this->ispublic) { $toolbarname = 'dolibarr_details'; print '
'.($conf->global->TICKET_PUBLIC_TEXT_HELP_MESSAGE ? $conf->global->TICKET_PUBLIC_TEXT_HELP_MESSAGE : $langs->trans('TicketPublicPleaseBeAccuratelyDescribe')).'
'; } include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; $uselocalbrowser = true; $doleditor = new DolEditor('message', $msg, '100%', 230, $toolbarname, 'In', true, $uselocalbrowser, $conf->global->FCKEDITOR_ENABLE_TICKET, ROWS_8, '90%'); $doleditor->Create(); print '
'.$langs->trans("CreatedBy").''; $langs->load("users"); $fuser = new User($this->db); if ($this->withcreatereadonly) { if ($res = $fuser->fetch($this->fk_user_create)) { print $fuser->getNomUrl(1); } } print '   '; print "
'.$langs->trans("ThirdParty").''; $events = array(); $events[] = array('method' => 'getContacts', 'url' => dol_buildpath('/core/ajax/contacts.php', 1), 'htmlname' => 'contactid', 'params' => array('add-customer-contact' => 'disabled')); print $form->select_company($this->withfromsocid, 'socid', '', 1, 1, '', $events, 0, 'minwidth200'); print '
'.$langs->trans("Contact").''; // If no socid, set to -1 to avoid full contacts list $selectedCompany = ($this->withfromsocid > 0) ? $this->withfromsocid : -1; $nbofcontacts = $form->select_contacts($selectedCompany, $this->withfromcontactid, 'contactid', 3, '', '', 0, 'minwidth200'); print ' '; $formcompany->selectTypeContact($ticketstatic, '', 'type', 'external', '', 0, 'maginleftonly'); print '
'; print 'withnotifytiersatcreate ? ' checked="checked"' : '').'>'; print '
'; print $formproject->select_projects(-1, GETPOST('projectid', 'int'), 'projectid', 0, 0, 1, 1); print '
'.$langs->trans("MailFile").''; // TODO Trick to have param removedfile containing nb of image to delete. But this does not works without javascript $out .= ''."\n"; $out .= ''."\n"; if (count($listofpaths)) { foreach ($listofpaths as $key => $val) { $out .= '
'; $out .= img_mime($listofnames[$key]).' '.$listofnames[$key]; if (!$this->withfilereadonly) { $out .= ' '; } $out .= '
'; } } else { $out .= $langs->trans("NoAttachedFiles").'
'; } if ($this->withfile == 2) { // Can add other files $out .= ''; $out .= ' '; $out .= ''; } $out .= "
'; if ($withdolfichehead) dol_fiche_end(); print '
'; print ''; if ($this->withcancel) { print "      "; print "trans("Cancel")."\">"; } print '
'; print "
\n"; print "\n"; } /** * Return html list of tickets type * * @param string $selected Id du type pre-selectionne * @param string $htmlname Nom de la zone select * @param string $filtertype To filter on field type in llx_c_ticket_type (array('code'=>xx,'label'=>zz)) * @param int $format 0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code * @param int $empty 1=peut etre vide, 0 sinon * @param int $noadmininfo 0=Add admin info, 1=Disable admin info * @param int $maxlength Max length of label * @param string $morecss More CSS * @return void */ public function selectTypesTickets($selected = '', $htmlname = 'tickettype', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0, $morecss = '') { global $langs, $user; $ticketstat = new Ticket($this->db); dol_syslog(get_class($this)."::select_types_tickets ".$selected.", ".$htmlname.", ".$filtertype.", ".$format, LOG_DEBUG); $filterarray = array(); if ($filtertype != '' && $filtertype != '-1') { $filterarray = explode(',', $filtertype); } $ticketstat->loadCacheTypesTickets(); print ''; if ($user->admin && !$noadmininfo) { print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1); } print ajax_combobox('select'.$htmlname); } /** * Return html list of ticket anaytic codes * * @param string $selected Id categorie pre-selectionnée * @param string $htmlname Nom de la zone select * @param string $filtertype To filter on field type in llx_c_ticket_category (array('code'=>xx,'label'=>zz)) * @param int $format 0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code * @param int $empty 1=peut etre vide, 0 sinon * @param int $noadmininfo 0=Add admin info, 1=Disable admin info * @param int $maxlength Max length of label * @param string $morecss More CSS * @return void */ public function selectGroupTickets($selected = '', $htmlname = 'ticketcategory', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0, $morecss = '') { global $langs, $user; $ticketstat = new Ticket($this->db); dol_syslog(get_class($this)."::selectCategoryTickets ".$selected.", ".$htmlname.", ".$filtertype.", ".$format, LOG_DEBUG); $filterarray = array(); if ($filtertype != '' && $filtertype != '-1') { $filterarray = explode(',', $filtertype); } $ticketstat->loadCacheCategoriesTickets(); print ''; if ($user->admin && !$noadmininfo) { print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1); } print ajax_combobox('select'.$htmlname); } /** * Return html list of ticket severitys * * @param string $selected Id severity pre-selectionnée * @param string $htmlname Nom de la zone select * @param string $filtertype To filter on field type in llx_c_ticket_severity (array('code'=>xx,'label'=>zz)) * @param int $format 0=id+libelle, 1=code+code, 2=code+libelle, 3=id+code * @param int $empty 1=peut etre vide, 0 sinon * @param int $noadmininfo 0=Add admin info, 1=Disable admin info * @param int $maxlength Max length of label * @param string $morecss More CSS * @return void */ public function selectSeveritiesTickets($selected = '', $htmlname = 'ticketseverity', $filtertype = '', $format = 0, $empty = 0, $noadmininfo = 0, $maxlength = 0, $morecss = '') { global $langs, $user; $ticketstat = new Ticket($this->db); dol_syslog(get_class($this)."::selectSeveritiesTickets ".$selected.", ".$htmlname.", ".$filtertype.", ".$format, LOG_DEBUG); $filterarray = array(); if ($filtertype != '' && $filtertype != '-1') { $filterarray = explode(',', $filtertype); } $ticketstat->loadCacheSeveritiesTickets(); print ''; if ($user->admin && !$noadmininfo) { print info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1); } print ajax_combobox('select'.$htmlname); } // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps /** * Clear list of attached files in send mail form (also stored in session) * * @return void */ public function clear_attached_files() { // phpcs:enable global $conf, $user; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; // Set tmp user directory $vardir = $conf->user->dir_output."/".$user->id; $upload_dir = $vardir.'/temp/'; // TODO Add $keytoavoidconflict in upload_dir path if (is_dir($upload_dir)) dol_delete_dir_recursive($upload_dir); $keytoavoidconflict = empty($this->trackid) ? '' : '-'.$this->trackid; // this->trackid must be defined unset($_SESSION["listofpaths".$keytoavoidconflict]); unset($_SESSION["listofnames".$keytoavoidconflict]); unset($_SESSION["listofmimes".$keytoavoidconflict]); } /** * Show the form to add message on ticket * * @param string $width Width of form * @return void */ public function showMessageForm($width = '40%') { global $conf, $langs, $user, $hookmanager, $form, $mysoc; $formmail = new FormMail($this->db); $addfileaction = 'addfile'; if (!is_object($form)) $form = new Form($this->db); // Load translation files required by the page $langs->loadLangs(array('other', 'mails')); // Clear temp files. Must be done at beginning, before call of triggers if (GETPOST('mode', 'alpha') == 'init' || (GETPOST('modelmailselected', 'alpha') && GETPOST('modelmailselected', 'alpha') != '-1')) { $this->clear_attached_files(); } // Define output language $outputlangs = $langs; $newlang = ''; if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $this->param['langsmodels']; if (!empty($newlang)) { $outputlangs = new Translate("", $conf); $outputlangs->setDefaultLang($newlang); $outputlangs->load('other'); } // Get message template for $this->param["models"] into c_email_templates $arraydefaultmessage = -1; if ($this->param['models'] != 'none') { $model_id = 0; if (array_key_exists('models_id', $this->param)) { $model_id = $this->param["models_id"]; } $arraydefaultmessage = $formmail->getEMailTemplate($this->db, $this->param["models"], $user, $outputlangs, $model_id); // If $model_id is empty, preselect the first one } // Define list of attached files $listofpaths = array(); $listofnames = array(); $listofmimes = array(); $keytoavoidconflict = empty($this->trackid) ? '' : '-'.$this->trackid; // this->trackid must be defined if (GETPOST('mode', 'alpha') == 'init' || (GETPOST('modelmailselected', 'alpha') && GETPOST('modelmailselected', 'alpha') != '-1')) { if (!empty($arraydefaultmessage->joinfiles) && is_array($this->param['fileinit'])) { foreach ($this->param['fileinit'] as $file) { $this->add_attached_files($file, basename($file), dol_mimetype($file)); } } } if (!empty($_SESSION["listofpaths".$keytoavoidconflict])) $listofpaths = explode(';', $_SESSION["listofpaths".$keytoavoidconflict]); if (!empty($_SESSION["listofnames".$keytoavoidconflict])) $listofnames = explode(';', $_SESSION["listofnames".$keytoavoidconflict]); if (!empty($_SESSION["listofmimes".$keytoavoidconflict])) $listofmimes = explode(';', $_SESSION["listofmimes".$keytoavoidconflict]); // Define output language $outputlangs = $langs; $newlang = ''; if ($conf->global->MAIN_MULTILANGS && empty($newlang)) { $newlang = $this->param['langsmodels']; } if (!empty($newlang)) { $outputlangs = new Translate("", $conf); $outputlangs->setDefaultLang($newlang); $outputlangs->load('other'); } print "\n\n"; $send_email = GETPOST('send_email', 'int') ? GETPOST('send_email', 'int') : 0; // Example 1 : Adding jquery code print ''; print '
'; print ''; print ''; print ''; foreach ($this->param as $key => $value) { print ''; } // Get message template $model_id = 0; if (array_key_exists('models_id', $this->param)) { $model_id = $this->param["models_id"]; $arraydefaultmessage = $formmail->getEMailTemplate($this->db, $this->param["models"], $user, $outputlangs, $model_id); } $result = $formmail->fetchAllEMailTemplate($this->param["models"], $user, $outputlangs); if ($result < 0) { setEventMessages($this->error, $this->errors, 'errors'); } $modelmail_array = array(); foreach ($formmail->lines_model as $line) { $modelmail_array[$line->id] = $line->label; } print ''; // External users can't send message email if ($user->rights->ticket->write && !$user->socid) { print ''; // Zone to select its email template if (count($modelmail_array) > 0) { print ''; } // Private message (not visible by customer/external user) if (!$user->socid) { print ''; } print ''; $label_title = empty($conf->global->MAIN_APPLICATION_TITLE) ? $mysoc->name : $conf->global->MAIN_APPLICATION_TITLE; print ''; // Destinataires print ''; } // Intro // External users can't send message email if ($user->rights->ticket->write && !$user->socid) { $mail_intro = GETPOST('mail_intro') ? GETPOST('mail_intro') : $conf->global->TICKET_MESSAGE_MAIL_INTRO; print ''; } // MESSAGE $defaultmessage = ""; if (is_array($arraydefaultmessage) && count($arraydefaultmessage) > 0 && $arraydefaultmessage->content) { $defaultmessage = $arraydefaultmessage->content; } $defaultmessage = str_replace('\n', "\n", $defaultmessage); // Deal with format differences between message and signature (text / HTML) if (dol_textishtml($defaultmessage) && !dol_textishtml($this->substit['__SIGNATURE__'])) { $this->substit['__SIGNATURE__'] = dol_nl2br($this->substit['__SIGNATURE__']); } elseif (!dol_textishtml($defaultmessage) && dol_textishtml($this->substit['__SIGNATURE__'])) { $defaultmessage = dol_nl2br($defaultmessage); } if (isset($_POST["message"]) && !$_POST['modelselected']) { $defaultmessage = GETPOST('message'); } else { $defaultmessage = make_substitutions($defaultmessage, $this->substit); // Clean first \n and br (to avoid empty line when CONTACTCIVNAME is empty) $defaultmessage = preg_replace("/^(
)+/", "", $defaultmessage); $defaultmessage = preg_replace("/^\n+/", "", $defaultmessage); } print ''; // Signature // External users can't send message email if ($user->rights->ticket->write && !$user->socid) { $mail_signature = GETPOST('mail_signature') ? GETPOST('mail_signature') : $conf->global->TICKET_MESSAGE_MAIL_SIGNATURE; print ''; } // Attached files if (!empty($this->withfile)) { $out = ''; $out .= ''; $out .= '\n"; print $out; } print '
'; $checkbox_selected = (GETPOST('send_email') == "1" ? ' checked' : ''); print ' '; print ''; print '
'; $checkbox_selected = (GETPOST('private_message', 'alpha') == "1" ? ' checked' : ''); print ' '; print ''; print ''; print $form->textwithpicto('', $langs->trans("TicketMessagePrivateHelp"), 1, 'help'); print '
'; //$toolbarname = 'dolibarr_details'; $toolbarname = 'dolibarr_notes'; include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; $doleditor = new DolEditor('message', $defaultmessage, '100%', 200, $toolbarname, '', false, true, $conf->global->FCKEDITOR_ENABLE_SOCIETE, ROWS_5, 70); $doleditor->Create(); print ''; if ($user->rights->ticket->write && !$user->socid) { print $form->textwithpicto('', $langs->trans("TicketMessageHelp"), 1, 'help'); } print '
'.$langs->trans("MailFile").''; // TODO Trick to have param removedfile containing nb of image to delete. But this does not works without javascript $out .= ''."\n"; $out .= ''."\n"; if (count($listofpaths)) { foreach ($listofpaths as $key => $val) { $out .= '
'; $out .= img_mime($listofnames[$key]).' '.$listofnames[$key]; if (!$this->withfilereadonly) { $out .= ' '; } $out .= '
'; } } else { $out .= $langs->trans("NoAttachedFiles").'
'; } if ($this->withfile == 2) { // Can add other files $out .= ''; $out .= ' '; $out .= ''; } $out .= "
'; print '

'; print ''; if ($this->withcancel) { print "     "; print "trans("Cancel")."\">"; } print "
\n"; print "
\n"; print "\n"; } }