Close #27774 new contact in project (#27947)

* Close  #27774 new contact in project

* add test to not show

* fix as advised

---------

Co-authored-by: Hystepik <lmarcouiller@nltechno.com>
This commit is contained in:
Lucas Marcouiller
2024-02-03 02:06:14 +01:00
committed by GitHub
parent 2dfc8df648
commit c01b7e793c
3 changed files with 100 additions and 33 deletions

View File

@@ -113,7 +113,28 @@ if ($permission) {
<div class="tagtd"><?php echo $conf->global->MAIN_INFO_SOCIETE_NOM; ?></div>
<!-- <div class="nowrap tagtd"><?php echo img_object('', 'user').' '.$langs->trans("Users"); ?></div> -->
<div class="tagtd maxwidthonsmartphone"><?php echo img_object('', 'user', 'class="pictofixedwidth"').$form->select_dolusers($user->id, 'userid', 0, (!empty($userAlreadySelected) ? $userAlreadySelected : null), 0, null, null, 0, 56, 0, '', 0, '', 'minwidth100imp widthcentpercentminusxx maxwidth400'); ?></div>
<div class="tagtd maxwidthonsmartphone">
<?php echo img_object('', 'user', 'class="pictofixedwidth"').$form->select_dolusers($user->id, 'userid', 1, (!empty($userAlreadySelected) ? $userAlreadySelected : null), 0, null, null, 0, 56, 0, '', 0, '', 'minwidth100imp widthcentpercentminusxx maxwidth400 userselectcontact');
if (empty($hideaddcontactforgroups) && $module == 'project') {
print '<span> '.$langs->trans("or").' </span>';
echo img_object('', 'group', 'class="pictofixedwidth"').$form->select_dolgroups(0, 'groupid', 1, '', 0, '', array(), '0', false, 'minwidth100imp widthcentpercentminusxx maxwidth400 groupselectcontact');
}
?>
<script>
jQuery(document).ready(function(){
$(".userselectcontact").on("change", function(){
if ($(this).val() != -1) {
$(".groupselectcontact").val(-1).change();
}
});
$(".groupselectcontact").on("change", function(){
if ($(this).val() != -1) {
$(".userselectcontact").val(-1).change();
}
});
});
</script>
</div>
<div class="tagtd maxwidthonsmartphone">
<?php
$tmpobject = $object;
@@ -124,7 +145,6 @@ if ($permission) {
<div class="tagtd">&nbsp;</div>
<div class="tagtd center"><input type="submit" class="button small" value="<?php echo $langs->trans("Add"); ?>"></div>
</form>
<?php
}

View File

@@ -408,3 +408,5 @@ TotalAmountEmpty=Total Amount Empty
FailedToFoundTheConversionRateForInvoice=Failed to found the conversion rate for invoice
ThisIdNotDefined=$this->id not defined
OperNotDefined=Oper not defined
ErrorThisContactXIsAlreadyDefinedAsThisType=%s is already defined as contact for this type.
ErrorThisGroupIsAlreadyDefinedAsThisType=The contacts with this group are already defined as contact for this type.

View File

@@ -165,50 +165,83 @@ if (empty($reshook)) {
$contactid = (GETPOST('userid') ? GETPOST('userid', 'int') : GETPOST('contactid', 'int'));
$typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type'));
$groupid = GETPOSTINT('groupid');
$contactarray = array();
$errorgroup = 0;
$errorgrouparray = array();
if (! ($contactid > 0)) {
if ($groupid > 0) {
require_once DOL_DOCUMENT_ROOT.'/user/class/usergroup.class.php';
$usergroup = new UserGroup($db);
$result = $usergroup->fetch($groupid);
if ($result > 0) {
$tmpcontactarray = $usergroup->listUsersForGroup();
if ($contactarray <= 0) {
$error++;
} else {
foreach ($tmpcontactarray as $tmpuser) {
$contactarray[] = $tmpuser->id;
}
}
} else {
$error++;
}
} elseif (! ($contactid > 0)) {
$error++;
$langs->load("errors");
setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Contact")), null, 'errors');
} else {
$contactarray[] = $contactid;
}
$result = 0;
$result = $object->fetch($id);
if (!$error && $result > 0 && $id > 0) {
$result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
foreach ($contactarray as $key => $contactid) {
$result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
if ($result == 0) {
$langs->load("errors");
setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
} elseif ($result < 0) {
if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
$langs->load("errors");
setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
} else {
setEventMessages($object->error, $object->errors, 'errors');
if ($result == 0) {
if ($groupid > 0) {
$errorgroup++;
$errorgrouparray[] = $contactid;
} else {
$langs->load("errors");
setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
}
} elseif ($result < 0) {
if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
if ($groupid > 0) {
$errorgroup++;
$errorgrouparray[] = $contactid;
} else {
$langs->load("errors");
setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
}
} else {
setEventMessages($object->error, $object->errors, 'errors');
}
}
}
$affecttotask=GETPOST('tasksavailable', 'intcomma');
if (!empty($affecttotask)) {
require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
$task_to_affect = explode(',', $affecttotask);
if (!empty($task_to_affect)) {
foreach ($task_to_affect as $task_id) {
if (GETPOSTISSET('person_'.$task_id) && GETPOST('person_'.$task_id, 'san_alpha')) {
$tasksToAffect = new Task($db);
$result=$tasksToAffect->fetch($task_id);
if ($result < 0) {
setEventMessages($tasksToAffect->error, null, 'errors');
} else {
$result = $tasksToAffect->add_contact($contactid, GETPOST('person_role_'.$task_id), GETPOST("source", 'aZ09'));
$affecttotask=GETPOST('tasksavailable', 'intcomma');
if (!empty($affecttotask)) {
require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
$task_to_affect = explode(',', $affecttotask);
if (!empty($task_to_affect)) {
foreach ($task_to_affect as $task_id) {
if (GETPOSTISSET('person_'.$task_id) && GETPOST('person_'.$task_id, 'san_alpha')) {
$tasksToAffect = new Task($db);
$result=$tasksToAffect->fetch($task_id);
if ($result < 0) {
if ($tasksToAffect->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
$langs->load("errors");
setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
} else {
setEventMessages($tasksToAffect->error, $tasksToAffect->errors, 'errors');
setEventMessages($tasksToAffect->error, null, 'errors');
} else {
$result = $tasksToAffect->add_contact($contactid, GETPOST('person_role_'.$task_id), GETPOST("source", 'aZ09'));
if ($result < 0) {
if ($tasksToAffect->error == 'DB_ERROR_RECORD_ALREADY_EXISTS') {
$langs->load("errors");
setEventMessages($langs->trans("ErrorThisContactIsAlreadyDefinedAsThisType"), null, 'errors');
} else {
setEventMessages($tasksToAffect->error, $tasksToAffect->errors, 'errors');
}
}
}
}
@@ -217,6 +250,18 @@ if (empty($reshook)) {
}
}
}
if ($errorgroup > 0) {
$langs->load("errors");
if ($errorgroup == count($contactarray)) {
setEventMessages($langs->trans("ErrorThisGroupIsAlreadyDefinedAsThisType"), null, 'errors');
} else {
$tmpuser = new User($db);
foreach ($errorgrouparray as $key => $value) {
$tmpuser->fetch($value);
setEventMessages($langs->trans("ErrorThisContactXIsAlreadyDefinedAsThisType", dolGetFirstLastname($tmpuser->firstname, $tmpuser->lastname)), null, 'errors');
}
}
}
if ($result >= 0) {
header("Location: ".$_SERVER['PHP_SELF']."?id=".$object->id);