Browse Source

NEW create third-party with contact if not found on public ticket

lvessiller 3 years ago
parent
commit
7c17f6b1cf

+ 86 - 19
htdocs/admin/ticket_public.php

@@ -42,21 +42,24 @@ $label = GETPOST('label', 'alpha');
 $scandir = GETPOST('scandir', 'alpha');
 $type = 'ticket';
 
-$error = 0;
 
 /*
  * Actions
  */
+$error = 0;
+$errors = array();
 
 if ($action == 'setTICKET_ENABLE_PUBLIC_INTERFACE') {
 	if (GETPOST('value')) {
-		dolibarr_set_const($db, 'TICKET_ENABLE_PUBLIC_INTERFACE', 1, 'chaine', 0, '', $conf->entity);
+		$res = dolibarr_set_const($db, 'TICKET_ENABLE_PUBLIC_INTERFACE', 1, 'chaine', 0, '', $conf->entity);
 	} else {
-		dolibarr_set_const($db, 'TICKET_ENABLE_PUBLIC_INTERFACE', 0, 'chaine', 0, '', $conf->entity);
+		$res = dolibarr_set_const($db, 'TICKET_ENABLE_PUBLIC_INTERFACE', 0, 'chaine', 0, '', $conf->entity);
 	}
-}
-
-if ($action == 'setvar') {
+	if (!($res > 0)) {
+		$error++;
+		$errors[] = $db->lasterror();
+	}
+} elseif ($action == 'setvar') {
 	include_once DOL_DOCUMENT_ROOT."/core/lib/files.lib.php";
 
 	if (GETPOSTISSET('TICKET_ENABLE_PUBLIC_INTERFACE')) {	// only for no js case
@@ -64,14 +67,7 @@ if ($action == 'setvar') {
 		$res = dolibarr_set_const($db, 'TICKET_ENABLE_PUBLIC_INTERFACE', $param_enable_public_interface, 'chaine', 0, '', $conf->entity);
 		if (!($res > 0)) {
 			$error++;
-		}
-	}
-
-	if (GETPOSTISSET('TICKET_EMAIL_MUST_EXISTS')) {	// only for no js case
-		$param_must_exists = GETPOST('TICKET_EMAIL_MUST_EXISTS', 'alpha');
-		$res = dolibarr_set_const($db, 'TICKET_EMAIL_MUST_EXISTS', $param_must_exists, 'chaine', 0, '', $conf->entity);
-		if (!($res > 0)) {
-			$error++;
+			$errors[] = $db->lasterror();
 		}
 	}
 
@@ -80,6 +76,7 @@ if ($action == 'setvar') {
 		$res = dolibarr_set_const($db, 'TICKET_DISABLE_CUSTOMER_MAILS', $param_disable_email, 'chaine', 0, '', $conf->entity);
 		if (!($res > 0)) {
 			$error++;
+			$errors[] = $db->lasterror();
 		}
 	}
 
@@ -88,6 +85,7 @@ if ($action == 'setvar') {
 		$res = dolibarr_set_const($db, 'TICKET_SHOW_COMPANY_LOGO', $param_show_module_logo, 'chaine', 0, '', $conf->entity);
 		if (!($res > 0)) {
 			$error++;
+			$errors[] = $db->lasterror();
 		}
 	}
 
@@ -99,6 +97,7 @@ if ($action == 'setvar') {
 	}
 	if (!($res > 0)) {
 		$error++;
+		$errors[] = $db->lasterror();
 	}
 
 	$text_home = GETPOST('TICKET_PUBLIC_TEXT_HOME', 'restricthtml');
@@ -109,6 +108,7 @@ if ($action == 'setvar') {
 	}
 	if (!($res > 0)) {
 		$error++;
+		$errors[] = $db->lasterror();
 	}
 
 	$text_help = GETPOST('TICKET_PUBLIC_TEXT_HELP_MESSAGE', 'restricthtml');
@@ -119,6 +119,7 @@ if ($action == 'setvar') {
 	}
 	if (!($res > 0)) {
 		$error++;
+		$errors[] = $db->lasterror();
 	}
 
 	$mail_new_ticket = GETPOST('TICKET_MESSAGE_MAIL_NEW', 'restricthtml');
@@ -129,6 +130,7 @@ if ($action == 'setvar') {
 	}
 	if (!($res > 0)) {
 		$error++;
+		$errors[] = $db->lasterror();
 	}
 
 	$url_interface = GETPOST('TICKET_URL_PUBLIC_INTERFACE', 'alpha');
@@ -139,12 +141,14 @@ if ($action == 'setvar') {
 	}
 	if (!($res > 0)) {
 		$error++;
+		$errors[] = $db->lasterror();
 	}
 
 	$param_public_notification_new_message_default_email = GETPOST('TICKET_PUBLIC_NOTIFICATION_NEW_MESSAGE_DEFAULT_EMAIL', 'alpha');
 	$res = dolibarr_set_const($db, 'TICKET_PUBLIC_NOTIFICATION_NEW_MESSAGE_DEFAULT_EMAIL', $param_public_notification_new_message_default_email, 'chaine', 0, '', $conf->entity);
 	if (!($res > 0)) {
 		$error++;
+		$errors[] = $db->lasterror();
 	}
 
 	if ($conf->global->MAIN_FEATURES_LEVEL >= 2) {
@@ -152,10 +156,60 @@ if ($action == 'setvar') {
 		$res = dolibarr_set_const($db, 'TICKET_NOTIFICATION_ALSO_MAIN_ADDRESS', $param_notification_also_main_addressemail, 'chaine', 0, '', $conf->entity);
 		if (!($res > 0)) {
 			$error++;
+			$errors[] = $db->lasterror();
+		}
+	}
+} elseif (preg_match('/set_(.*)/',$action,$reg)) {
+	$code = $reg[1];
+	$value = GETPOSTISSET($code) ? GETPOST($code, 'int') : 1;
+	$res = dolibarr_set_const($db, $code, $value, 'chaine', 0, '', $conf->entity);
+	if (!($res > 0)) {
+		$error++;
+		$errors[] = $db->lasterror();
+	}
+
+	if (!$error) {
+		if ($code == 'TICKET_EMAIL_MUST_EXISTS') {
+			$res = dolibarr_del_const($db, 'TICKET_CREATE_THIRD_PARTY_WITH_CONTACT_IF_NOT_EXIST', $conf->entity);
+			if (!($res > 0)) {
+				$error++;
+				$errors[] = $db->lasterror();
+			}
+		} elseif ($code == 'TICKET_CREATE_THIRD_PARTY_WITH_CONTACT_IF_NOT_EXIST') {
+			$res = dolibarr_del_const($db, 'TICKET_EMAIL_MUST_EXISTS', $conf->entity);
+			if (!($res > 0)) {
+				$error++;
+				$errors[] = $db->lasterror();
+			}
+
+			// enable captcha by default
+			$res = dolibarr_set_const($db, 'MAIN_SECURITY_ENABLECAPTCHA', 1, 'chaine', 0, '', $conf->entity);
+			if (!($res > 0)) {
+				$error++;
+				$errors[] = $db->lasterror();
+			}
 		}
 	}
+} elseif (preg_match('/del_(.*)/',$action,$reg)) {
+	$code = $reg[1];
+	$res = dolibarr_del_const($db, $code, $conf->entity);
+	if (!($res > 0)) {
+		$error++;
+		$errors[] = $db->lasterror();
+	}
 }
 
+if ($action != '') {
+	if (!$error) {
+		$db->commit();
+		setEventMessage($langs->trans('SetupSaved'));
+		header("Location: " . $_SERVER['PHP_SELF']);
+		exit;
+	} else {
+		$db->rollback();
+		setEventMessages('', $errors, 'errors');
+	}
+}
 
 
 /*
@@ -180,7 +234,7 @@ $head = ticketAdminPrepareHead();
 
 print dol_get_fiche_head($head, 'public', $langs->trans("Module56000Name"), -1, "ticket");
 
-print '<span class="opacitymedium">'.$langs->trans("TicketPublicAccess").'</span> : <a class="wordbreak" href="'.DOL_URL_ROOT.'/public/ticket/index.php" target="_blank" rel="noopener noreferrer">'.dol_buildpath('/public/ticket/index.php', 2).'</a>';
+print '<span class="opacitymedium">'.$langs->trans("TicketPublicAccess").'</span> : <a class="wordbreak" href="'.DOL_URL_ROOT.'/public/ticket/index.php?entity='.$conf->entity.'" target="_blank" rel="noopener noreferrer">'.dol_buildpath('/public/ticket/index.php?entity='.$conf->entity, 2).'</a>';
 
 print dol_get_fiche_end();
 
@@ -220,11 +274,10 @@ if (!empty($conf->global->TICKET_ENABLE_PUBLIC_INTERFACE)) {
 	// Check if email exists
 	print '<tr class="oddeven"><td>'.$langs->trans("TicketsEmailMustExist").'</td>';
 	print '<td class="left">';
-	if ($conf->use_javascript_ajax) {
-		print ajax_constantonoff('TICKET_EMAIL_MUST_EXISTS');
+	if (empty(getDolGlobalInt('TICKET_EMAIL_MUST_EXISTS'))) {
+		print '<a href="' . $_SERVER['PHP_SELF'] . '?action=set_TICKET_EMAIL_MUST_EXISTS">' . img_picto($langs->trans('Disabled'), 'switch_off') . '</a>';
 	} else {
-		$arrval = array('0' => $langs->trans("No"), '1' => $langs->trans("Yes"));
-		print $form->selectarray("TICKET_EMAIL_MUST_EXISTS", $arrval, $conf->global->TICKET_EMAIL_MUST_EXISTS);
+		print '<a href="' . $_SERVER['PHP_SELF'] . '?action=del_TICKET_EMAIL_MUST_EXISTS">' . img_picto($langs->trans('Enabled'), 'switch_on') . '</a>';
 	}
 	print '</td>';
 	print '<td class="center width75">';
@@ -232,6 +285,20 @@ if (!empty($conf->global->TICKET_ENABLE_PUBLIC_INTERFACE)) {
 	print '</td>';
 	print '</tr>';
 
+	// Create third-party with contact if email not linked to a contact
+	print '<tr class="oddeven"><td>'.$langs->trans("TicketCreateThirdPartyWithContactIfNotExist").'</td>';
+	print '<td class="left">';
+	if (empty(getDolGlobalInt('TICKET_CREATE_THIRD_PARTY_WITH_CONTACT_IF_NOT_EXIST'))) {
+		print '<a href="' . $_SERVER['PHP_SELF'] . '?action=set_TICKET_CREATE_THIRD_PARTY_WITH_CONTACT_IF_NOT_EXIST">' . img_picto($langs->trans('Disabled'), 'switch_off') . '</a>';
+	} else {
+		print '<a href="' . $_SERVER['PHP_SELF'] . '?action=del_TICKET_CREATE_THIRD_PARTY_WITH_CONTACT_IF_NOT_EXIST">' . img_picto($langs->trans('Enabled'), 'switch_on') . '</a>';
+	}
+	print '</td>';
+	print '<td class="center width75">';
+	print $form->textwithpicto('', $langs->trans("TicketCreateThirdPartyWithContactIfNotExistHelp"), 1, 'help');
+	print '</td>';
+	print '</tr>';
+
 	/*if ($conf->global->MAIN_FEATURES_LEVEL >= 2)
 	{
 		// Show logo for module

+ 101 - 6
htdocs/core/class/html.formticket.class.php

@@ -127,12 +127,13 @@ class FormTicket
 	/**
 	 * Show the form to input ticket
 	 *
-	 * @param  	int	 		$withdolfichehead		With dol_get_fiche_head() and dol_get_fiche_end()
-	 * @param	string		$mode					Mode ('create' or 'edit')
-	 * @param	int			$public					1=If we show the form for the public interface
+	 * @param  	int	 			$withdolfichehead		With dol_get_fiche_head() and dol_get_fiche_end()
+	 * @param	string			$mode					Mode ('create' or 'edit')
+	 * @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 exists
 	 * @return 	void
 	 */
-	public function showForm($withdolfichehead = 0, $mode = 'edit', $public = 0)
+	public function showForm($withdolfichehead = 0, $mode = 'edit', $public = 0, Contact $with_contact = null)
 	{
 		global $conf, $langs, $user, $hookmanager;
 
@@ -178,10 +179,104 @@ class FormTicket
 		}
 
 		// TITLE
+		$email = GETPOSTISSET('email') ? GETPOST('email', 'alphanohtml') : '';
 		if ($this->withemail) {
 			print '<tr><td class="titlefield"><label for="email"><span class="fieldrequired">'.$langs->trans("Email").'</span></label></td><td>';
-			print '<input  class="text minwidth200" id="email" name="email" value="'.(GETPOST('email', 'alpha') ? GETPOST('email', 'alpha') : $subject).'" autofocus>';
+			print '<input class="text minwidth200" id="email" name="email" value="'.$email.'" autofocus>';
 			print '</td></tr>';
+
+			if ($with_contact) {
+				// contact search and result
+				$html_contact_search  = '';
+				$html_contact_search .= '<tr id="contact_search_line">';
+				$html_contact_search .= '<td class="titlefield">';
+				$html_contact_search .= '<label for="contact"><span class="fieldrequired">' . $langs->trans('Contact') . '</span></label>';
+				$html_contact_search .= '<input type="hidden" id="contact_id" name="contact_id" value="" />';
+				$html_contact_search .= '</td>';
+				$html_contact_search .= '<td id="contact_search_result"></td>';
+				$html_contact_search .= '</tr>';
+				print $html_contact_search;
+				// contact lastname
+				$html_contact_lastname = '';
+				$html_contact_lastname .= '<tr id="contact_lastname_line" class="contact_field"><td class="titlefield"><label for="contact_lastname"><span class="fieldrequired">' . $langs->trans('Lastname') . '</span></label></td><td>';
+				$html_contact_lastname .= '<input type="text" id="contact_lastname" name="contact_lastname" value="' . dol_escape_htmltag(GETPOSTISSET('contact_lastname') ? GETPOST('contact_lastname', 'alphanohtml') : '') . '" />';
+				$html_contact_lastname .= '</td></tr>';
+				print $html_contact_lastname;
+				// contact firstname
+				$html_contact_firstname  = '';
+				$html_contact_firstname .= '<tr id="contact_firstname_line" class="contact_field"><td class="titlefield"><label for="contact_firstname"><span class="fieldrequired">' . $langs->trans('Firstname') . '</span></label></td><td>';
+				$html_contact_firstname .= '<input type="text" id="contact_firstname" name="contact_firstname" value="' . dol_escape_htmltag(GETPOSTISSET('contact_firstname') ? GETPOST('contact_firstname', 'alphanohtml') : '') . '" />';
+				$html_contact_firstname .= '</td></tr>';
+				print $html_contact_firstname;
+				// company name
+				$html_company_name  = '';
+				$html_company_name .= '<tr id="contact_company_name_line" class="contact_field"><td><label for="company_name"><span>' . $langs->trans('Company') . '</span></label></td><td>';
+				$html_company_name .= '<input type="text" id="company_name" name="company_name" value="' . dol_escape_htmltag(GETPOSTISSET('company_name') ? GETPOST('company_name', 'alphanohtml') : '') . '" />';
+				$html_company_name .= '</td></tr>';
+				print $html_company_name;
+				// contact phone
+				$html_contact_phone  = '';
+				$html_contact_phone .= '<tr id="contact_phone_line" class="contact_field"><td><label for="contact_phone"><span>' . $langs->trans('Phone') . '</span></label></td><td>';
+				$html_contact_phone .= '<input type="text" id="contact_phone" name="contact_phone" value="' . dol_escape_htmltag(GETPOSTISSET('contact_phone') ? GETPOST('contact_phone', 'alphanohtml') : '') . '" />';
+				$html_contact_phone .= '</td></tr>';
+				print $html_contact_phone;
+
+				// search contact form email
+				$langs->load('errors');
+				print '<script type="text/javascript">
+                    jQuery(document).ready(function() {
+                        var contact = jQuery.parseJSON("'.dol_escape_js(json_encode($with_contact), 2).'");
+                        jQuery("#contact_search_line").hide();
+                        if (contact) {
+                        	if (contact.id > 0) {
+                        		jQuery("#contact_search_line").show();
+                        		jQuery("#contact_id").val(contact.id);
+								jQuery("#contact_search_result").html(contact.firstname+" "+contact.lastname);
+								jQuery(".contact_field").hide();
+                        	} else {
+                        		jQuery(".contact_field").show();
+                        	}
+                        }
+
+                    	jQuery("#email").change(function() {
+                            jQuery("#contact_search_line").show();
+                            jQuery("#contact_search_result").html("'.dol_escape_js($langs->trans('Select2SearchInProgress')).'");
+                            jQuery("#contact_id").val("");
+                            jQuery("#contact_lastname").val("");
+                            jQuery("#contact_firstname").val("");
+                            jQuery("#company_name").val("");
+                            jQuery("#contact_phone").val("");
+
+                            jQuery.getJSON(
+                                "'.dol_escape_js(dol_buildpath('/public/ticket/ajax/ajax.php', 1)).'",
+								{
+									action: "getContacts",
+									email: jQuery("#email").val()
+								},
+								function(response) {
+									if (response.error) {
+                                        jQuery("#contact_search_result").html("<span class=\"error\">"+response.error+"</span>");
+									} else {
+                                        var contact_list = response.contacts;
+										if (contact_list.length == 1) {
+                                            var contact = contact_list[0];
+											jQuery("#contact_id").val(contact.id);
+											jQuery("#contact_search_result").html(contact.firstname+" "+contact.lastname);
+                                            jQuery(".contact_field").hide();
+										} else if (contact_list.length <= 0) {
+                                            jQuery("#contact_search_line").hide();
+                                            jQuery(".contact_field").show();
+										}
+									}
+								}
+                            ).fail(function(jqxhr, textStatus, error) {
+    							var error_msg = "'.dol_escape_js($langs->trans('ErrorAjaxRequestFailed')).'"+" ["+textStatus+"] : "+error;
+                                jQuery("#contact_search_result").html("<span class=\"error\">"+error_msg+"</span>");
+                            });
+                        });
+                    });
+                    </script>';
+			}
 		}
 
 		// If ticket created from another object
@@ -325,7 +420,7 @@ class FormTicket
 			print '</span>';
 			print '<span class="nowrap inline-block">';
 			print '<img class="inline-block valignmiddle" src="'.DOL_URL_ROOT.'/core/antispamimage.php" border="0" width="80" height="32" id="img_securitycode" />';
-			print '<a class="inline-block valignmiddle" href="'.$php_self.'" tabindex="4" data-role="button">'.img_picto($langs->trans("Refresh"), 'refresh', 'id="captcha_refresh_img"').'</a>';
+			print '<a class="inline-block valignmiddle" href="" tabindex="4" data-role="button">'.img_picto($langs->trans("Refresh"), 'refresh', 'id="captcha_refresh_img"').'</a>';
 			print '</span>';
 			print '</td></tr>';
 		}

+ 1 - 1
htdocs/core/lib/ticket.lib.php

@@ -233,7 +233,7 @@ function llxHeaderTicket($title, $head = "", $disablejs = 0, $disablehead = 0, $
 		print '<div class="backgreypublicpayment">';
 		print '<div class="logopublicpayment">';
 		if ($urllogo) {
-			print '<a href="'.($conf->global->TICKET_URL_PUBLIC_INTERFACE ? $conf->global->TICKET_URL_PUBLIC_INTERFACE : dol_buildpath('/public/ticket/index.php', 1)).'">';
+			print '<a href="'.($conf->global->TICKET_URL_PUBLIC_INTERFACE ? $conf->global->TICKET_URL_PUBLIC_INTERFACE : dol_buildpath('/public/ticket/index.php?entity='.$conf->entity, 1)).'">';
 			print '<img id="dolpaymentlogo" src="'.$urllogo.'"';
 			print '>';
 			print '</a>';

+ 1 - 0
htdocs/langs/en_US/errors.lang

@@ -286,6 +286,7 @@ ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency=You try to pay in
 ErrorInvoiceLoadThirdParty=Can't load third-party object for invoice "%s"
 ErrorInvoiceLoadThirdPartyKey=Third-party key "%s" no set for invoice "%s"
 ErrorDeleteLineNotAllowedByObjectStatus=Delete line is not allowed by actual object status
+ErrorAjaxRequestFailed=Request failed
 
 # Warnings
 WarningParamUploadMaxFileSizeHigherThanPostMaxSize=Your PHP parameter upload_max_filesize (%s) is higher than PHP parameter post_max_size (%s). This is not a consistent setup.

+ 2 - 0
htdocs/langs/en_US/ticket.lang

@@ -99,6 +99,8 @@ TicketNewEmailBodyHelp=The text specified here will be inserted into the email c
 TicketParamPublicInterface=Public interface setup
 TicketsEmailMustExist=Require an existing email address to create a ticket
 TicketsEmailMustExistHelp=In the public interface, the email address should already be filled in the database to create a new ticket.
+TicketCreateThirdPartyWithContactIfNotExist=Create a third party with contact if it does not exist with the given email
+TicketCreateThirdPartyWithContactIfNotExistHelp=Create a third party with contact if it does not exist with the given email
 PublicInterface=Public interface
 TicketUrlPublicInterfaceLabelAdmin=Alternative URL for public interface
 TicketUrlPublicInterfaceHelpAdmin=It is possible to define an alias to the web server and thus make available the public interface with another URL (the server must act as a proxy on this new URL)

+ 1 - 0
htdocs/langs/fr_FR/errors.lang

@@ -277,6 +277,7 @@ ErrorIsNotADraft=%s n'est pas au statut brouillon
 ErrorExecIdFailed=Impossible d'exécuter la commande "id"
 ErrorBadCharIntoLoginName=Caractère non autorisé dans le nom de connexion
 ErrorRequestTooLarge=Error, request too large
+ErrorAjaxRequestFailed=La requête a échoué
 
 # Warnings
 WarningParamUploadMaxFileSizeHigherThanPostMaxSize=Votre paramètre PHP upload_max_filesize (%s) est supérieur au paramètre PHP post_max_size (%s). Ceci n'est pas une configuration cohérente.

+ 2 - 0
htdocs/langs/fr_FR/ticket.lang

@@ -99,6 +99,8 @@ TicketNewEmailBodyHelp=Le texte spécifié ici sera inséré dans l'e-mail confi
 TicketParamPublicInterface=Configuration de l'interface publique\n
 TicketsEmailMustExist=Une adresse e-mail existante est requise pour créer un ticket
 TicketsEmailMustExistHelp=Pour accéder à l'interface publique et créer un nouveau ticket, votre compte doit déjà être existant.
+TicketCreateThirdPartyWithContactIfNotExist=Créer un tiers avec contact s'il n'existe pas avec l'email saisi
+TicketCreateThirdPartyWithContactIfNotExistHelp=Créer un tiers avec contact s'il n'existe pas avec l'email saisi
 PublicInterface=Interface publique
 TicketUrlPublicInterfaceLabelAdmin=URL alternative pour l'interface publique
 TicketUrlPublicInterfaceHelpAdmin=Il est possible de définir un alias vers le serveur et de rendre ainsi l'interface publique accessible avec une autre URL (le serveur doit agir comme un proxy sur cette nouvelle URL)

+ 87 - 0
htdocs/public/ticket/ajax/ajax.php

@@ -0,0 +1,87 @@
+<?php
+/**
+ * Copyright (C) 2020 Laurent Destailleur <eldy@users.sourceforge.net>
+ *
+ * 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 3 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 <https://www.gnu.org/licenses/>.
+ */
+
+/**
+ *	\file       htdocs/public/ticket/ajax/ajax.php
+ *	\brief      Ajax component for Ticket.
+ */
+
+if (!defined('NOTOKENRENEWAL')) {
+	define('NOTOKENRENEWAL', '1'); // Disables token renewal
+}
+if (!defined('NOREQUIREHTML')) {
+	define('NOREQUIREHTML', '1');
+}
+if (!defined('NOREQUIREAJAX')) {
+	define('NOREQUIREAJAX', '1');
+}
+if (!defined('NOREQUIRESOC')) {
+	define('NOREQUIRESOC', '1');
+}
+if (!defined('NOCSRFCHECK')) {
+	define('NOCSRFCHECK', '1');
+}
+// Do not check anti CSRF attack test
+if (!defined('NOREQUIREMENU')) {
+	define('NOREQUIREMENU', '1');
+}
+// If there is no need to load and show top and left menu
+if (!defined("NOLOGIN")) {
+	define("NOLOGIN", '1');
+}
+if (!defined('NOIPCHECK')) {
+	define('NOIPCHECK', '1'); // Do not check IP defined into conf $dolibarr_main_restrict_ip
+}
+if (!defined('NOBROWSERNOTIF')) {
+	define('NOBROWSERNOTIF', '1');
+}
+
+include_once '../../../main.inc.php'; // Load $user and permissions
+
+$action = GETPOST('action', 'aZ09');
+$id = GETPOST('id', 'int');
+$email = GETPOST('email', 'alphanohtml');
+
+
+/*
+ * View
+ */
+
+top_httphead();
+
+if ($action == 'getContacts') {
+	$return = array(
+		'contacts' => array(),
+		'error' => '',
+	);
+
+	if (!empty($email)) {
+		require_once DOL_DOCUMENT_ROOT.'/ticket/class/ticket.class.php';
+
+		$ticket = new Ticket($db);
+		$contacts = $ticket->searchContactByEmail($email);
+		if (is_array($contacts)) {
+			$return['contacts'] = $contacts;
+		} else {
+			$return['error'] = $ticket->errorsToString();
+		}
+	}
+
+	echo json_encode($return);
+	exit();
+}

+ 84 - 7
htdocs/public/ticket/create_ticket.php

@@ -63,6 +63,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/core/lib/payments.lib.php';
 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
+require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
 
 // Load translation files required by the page
 $langs->loadLangs(array('companies', 'other', 'mails', 'ticket'));
@@ -78,6 +79,11 @@ $hookmanager->initHooks(array('publicnewticketcard', 'globalcard'));
 
 $object = new Ticket($db);
 $extrafields = new ExtraFields($db);
+$contacts = array();
+$with_contact = null;
+if (!empty($conf->global->TICKET_CREATE_THIRD_PARTY_WITH_CONTACT_IF_NOT_EXIST)) {
+	$with_contact = new Contact($db);
+}
 
 $extrafields->fetch_name_optionals_label($object->table_element);
 
@@ -153,18 +159,50 @@ if (empty($reshook) && $action == 'create_ticket' && GETPOST('save', 'alpha')) {
 		}
 	}
 
+	$contact_lastname = '';
+	$contact_firstname = '';
+	$company_name = '';
+	$contact_phone = '';
+	if ($with_contact) {
+		// set linked contact to add in form
+		if (is_array($contacts) && count($contacts) == 1) {
+			$with_contact = current($contacts);
+		}
+
+		// check mandatory fields on contact
+		$contact_lastname = trim(GETPOST('contact_lastname', 'alphanohtml'));
+		$contact_firstname = trim(GETPOST('contact_firstname', 'alphanohtml'));
+		$company_name = trim(GETPOST('company_name', 'alphanohtml'));
+		$contact_phone = trim(GETPOST('contact_phone', 'alphanohtml'));
+		if (!($with_contact->id > 0)) {
+			// check lastname
+			if (empty($contact_lastname)) {
+				$error++;
+				array_push($object->errors, $langs->trans('ErrorFieldRequired', $langs->transnoentities('Lastname')));
+				$action = '';
+			}
+			// check firstname
+			if (empty($contact_firstname)) {
+				$error++;
+				array_push($object->errors, $langs->trans('ErrorFieldRequired', $langs->transnoentities('Firstname')));
+				$action = '';
+			}
+		}
+	}
+
 	if (!GETPOST("subject", "restricthtml")) {
 		$error++;
 		array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Subject")));
 		$action = '';
-	} elseif (!GETPOST("message", "restricthtml")) {
+	}
+	if (!GETPOST("message", "restricthtml")) {
 		$error++;
-		array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("message")));
+		array_push($object->errors, $langs->trans("ErrorFieldRequired", $langs->transnoentities("Message")));
 		$action = '';
 	}
 
 	// Check email address
-	if (!isValidEmail($origin_email)) {
+	if (!empty($origin_email) && !isValidEmail($origin_email)) {
 		$error++;
 		array_push($object->errors, $langs->trans("ErrorBadEmailAddress", $langs->transnoentities("email")));
 		$action = '';
@@ -193,6 +231,48 @@ if (empty($reshook) && $action == 'create_ticket' && GETPOST('save', 'alpha')) {
 		$object->type_code = GETPOST("type_code", 'aZ09');
 		$object->category_code = GETPOST("category_code", 'aZ09');
 		$object->severity_code = GETPOST("severity_code", 'aZ09');
+
+		if (!is_object($user)) {
+			$user = new User($db);
+		}
+
+		// create third-party with contact
+		$usertoassign = 0;
+		if ($with_contact && !($with_contact->id > 0)) {
+			$company = new Societe($db);
+			if (!empty($company_name)) {
+				$company->name = $company_name;
+			} else {
+				$company->particulier = 1;
+				$company->name = dolGetFirstLastname($contact_firstname, $contact_lastname);
+			}
+			$result = $company->create($user);
+			if ($result < 0) {
+				$error++;
+				$errors = ($company->error ? array($company->error) : $company->errors);
+				array_push($object->errors, $errors);
+				$action = 'create_ticket';
+			}
+
+			// create contact and link to this new company
+			if (!$error) {
+				$with_contact->email = $origin_email;
+				$with_contact->lastname = $contact_lastname;
+				$with_contact->firstname = $contact_firstname;
+				$with_contact->socid = $company->id;
+				$with_contact->phone_pro = $contact_phone;
+				$result = $with_contact->create($user);
+				if ($result < 0) {
+					$error++;
+					$errors = ($with_contact->error ? array($with_contact->error) : $with_contact->errors);
+					array_push($object->errors, $errors);
+					$action = 'create_ticket';
+				} else {
+					$contacts = array($with_contact);
+				}
+			}
+		}
+
 		if (is_array($searched_companies)) {
 			$object->fk_soc = $searched_companies[0]->id;
 		}
@@ -206,9 +286,6 @@ if (empty($reshook) && $action == 'create_ticket' && GETPOST('save', 'alpha')) {
 
 		// Generate new ref
 		$object->ref = $object->getDefaultRef();
-		if (!is_object($user)) {
-			$user = new User($db);
-		}
 
 		$object->context['disableticketemail'] = 1; // Disable emails sent by ticket trigger when creation is done from this page, emails are already sent later
 
@@ -402,7 +479,7 @@ if ($action != "infos_success") {
 		print '</div>';
 	} else {
 		print '<div class="info marginleftonly marginrightonly">'.$langs->trans('TicketPublicInfoCreateTicket').'</div>';
-		$formticket->showForm(0, 'edit', 1);
+		$formticket->showForm(0, 'edit', 1, $with_contact);
 	}
 }