Debug and enhance the feature to assign tasks on project assignation

This commit is contained in:
Laurent Destailleur 2021-02-09 17:52:33 +01:00
parent 1b129831a9
commit 21558dd0a6
4 changed files with 117 additions and 86 deletions

View File

@ -970,7 +970,7 @@ abstract class CommonObject
* @param int|string $type_contact Type of contact (code or id). Must be id or code found into table llx_c_type_contact. For example: SALESREPFOLL
* @param string $source external=Contact extern (llx_socpeople), internal=Contact intern (llx_user)
* @param int $notrigger Disable all triggers
* @return int <0 if KO, >0 if OK
* @return int <0 if KO, 0 if already added, >0 if OK
*/
public function add_contact($fk_socpeople, $type_contact, $source = 'external', $notrigger = 0)
{
@ -1075,7 +1075,9 @@ abstract class CommonObject
return -1;
}
}
} else return 0;
} else {
return 0;
}
}
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps

View File

@ -4308,7 +4308,7 @@ class Form
* @param string $question Question
* @param string $action Action
* @param array|string $formquestion An array with complementary inputs to add into forms: array(array('label'=> ,'type'=> , 'size'=>, 'morecss'=>, 'moreattr'=>))
* type can be 'hidden', 'text', 'password', 'checkbox', 'radio', 'date', 'morecss', ...
* type can be 'hidden', 'text', 'password', 'checkbox', 'radio', 'date', 'morecss', 'other' or 'onecolumn'...
* @param string $selectedchoice '' or 'no', or 'yes' or '1' or '0'
* @param int|string $useajax 0=No, 1=Yes, 2=Yes but submit page with &confirm=no if choice is No, 'xxx'=Yes and preoutput confirm box with div id=dialog-confirm-xxx
* @param int|string $height Force height of box (0 = auto)
@ -4422,6 +4422,8 @@ class Form
$moreonecolumn .= '<div class="margintoponly">';
$moreonecolumn .= $input['value'];
$moreonecolumn .= '</div>'."\n";
} else {
$more .= 'Error type '.$input['type'].' for the confirm box is not a supported type';
}
}
}
@ -4449,16 +4451,24 @@ class Form
}
$pageyes = $page.(preg_match('/\?/', $page) ? '&' : '?').'action='.$action.'&confirm=yes';
$pageno = ($useajax == 2 ? $page.(preg_match('/\?/', $page) ? '&' : '?').'confirm=no' : '');
// Add input fields into list of fields to read during submit (inputok and inputko)
if (is_array($formquestion))
{
foreach ($formquestion as $key => $input)
{
if (is_array($formquestion)) {
foreach ($formquestion as $key => $input) {
//print "xx ".$key." rr ".is_array($input)."<br>\n";
if (is_array($input) && isset($input['name'])) array_push($inputok, $input['name']);
// Add name of fields to propagate with the GET when submitting the form with button OK.
if (is_array($input) && isset($input['name'])) {
if (strpos($input['name'], ',') > 0) {
$inputok = array_merge($inputok, explode(',', $input['name']));
} else {
array_push($inputok, $input['name']);
}
}
// Add name of fields to propagate with the GET when submitting the form with button KO.
if (isset($input['inputko']) && $input['inputko'] == 1) array_push($inputko, $input['name']);
}
}
// Show JQuery confirm box.
$formconfirm .= '<div id="'.$dialogconfirm.'" title="'.dol_escape_htmltag($title).'" style="display: none;">';
if (is_array($formquestion) && !empty($formquestion['text'])) {

View File

@ -758,9 +758,10 @@ class FormCompany extends Form
* @param int $showempty 1=Add en empty line
* @param string $morecss Add more css to select component
* @param int $output 0=return HTML, 1= direct print
* @param int $forcehidetooltip Force hide tooltip for admin
* @return void
*/
public function selectTypeContact($object, $selected, $htmlname = 'type', $source = 'internal', $sortorder = 'position', $showempty = 0, $morecss = '', $output = 1)
public function selectTypeContact($object, $selected, $htmlname = 'type', $source = 'internal', $sortorder = 'position', $showempty = 0, $morecss = '', $output = 1, $forcehidetooltip = 0)
{
global $user, $langs;
$out = '';
@ -777,7 +778,7 @@ class FormCompany extends Form
$out .= '>'.$value.'</option>';
}
$out .= "</select>";
if ($user->admin) $out .= ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
if ($user->admin && empty($forcehidetooltip)) $out .= ' '.info_admin($langs->trans("YouCanChangeValuesForThisListFromDictionarySetup"), 1);
$out .= ajax_combobox($htmlname);

View File

@ -24,6 +24,7 @@
require '../main.inc.php';
require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php';
require_once DOL_DOCUMENT_ROOT.'/projet/class/task.class.php';
require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/lib/project.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
@ -53,10 +54,91 @@ $result = restrictedArea($user, 'projet', $id, 'projet&project');
$hookmanager->initHooks(array('projectcontactcard', 'globalcard'));
/*
* Actions
*/
// Test if we can add contact to the tasks at the same times, if not or not required, make a redirect
$formconfirmtoaddtasks = '';
if ($action == 'addcontact') {
$form = new Form($db);
$source=GETPOST("source", 'aZ09');
$taskstatic = new Task($db);
$task_array = $taskstatic->getTasksArray(0, 0, $object->id, 0, 0);
$nbTasks = count($task_array);
//If no task avaiblable, redirec to to add confirm
$type_to = (GETPOST('typecontact') ? 'typecontact='.GETPOST('typecontact') : 'type='.GETPOST('type'));
$personToAffect = (GETPOST('userid') ? GETPOST('userid', 'int') : GETPOST('contactid', 'int'));
$affect_to = (GETPOST('userid') ? 'userid='.$personToAffect : 'contactid='.$personToAffect);
$url_redirect='?id='.$object->id.'&'.$affect_to.'&'.$type_to.'&source='.$source;
if (empty($conf->global->PROJECT_HIDE_TASKS) || $nbTasks > 0) {
$text = $langs->trans('AddPersonToTask');
$textbody = $text.' (<a href="#" class="selectall">'.$langs->trans("SelectAll").'</a>)';
$formquestion = array('text' => $textbody);
$task_to_affect = array();
foreach ($task_array as $task) {
$task_already_affected=false;
$personsLinked = $task->liste_contact(-1, $source);
if (!is_array($personsLinked) && coun($personsLinked) < 0) {
setEventMessage($object->error, 'errors');
} else {
foreach ($personsLinked as $person) {
if ($person['id']==$personToAffect) {
$task_already_affected = true;
break;
}
}
if (!$task_already_affected) {
$task_to_affect[$task->id] = $task->id;
}
}
}
if (empty($task_to_affect)) {
$action = 'addcontact_confirm';
} else {
$formcompany = new FormCompany($db);
foreach ($task_array as $task) {
$key = $task->id;
$val = $task->ref . ' '.dol_trunc($task->label);
$formquestion[] = array(
'type' => 'other',
'name' => 'person_'.$key.',person_role_'.$key,
'label' => '<input type="checkbox" class="flat'.(in_array($key, $task_to_affect) ? ' taskcheckboxes"' : '" checked disabled').' id="person_'.$key.'" name="person_'.$key.'" value="1"> <label for="person_'.$key.'">'.$val.'<label>',
'value' => $formcompany->selectTypeContact($taskstatic, '', 'person_role_'.$key, $source, 'position', 0, 'minwidth100imp', 0, 1)
);
}
$formquestion[] = array('type'=> 'other', 'name'=>'tasksavailable', 'label'=>'', 'value' => '<input type="hidden" id="tasksavailable" name="tasksavailable" value="'.implode(',', array_keys($task_to_affect)).'">');
}
$formconfirmtoaddtasks = $form->formconfirm($_SERVER['PHP_SELF'] . $url_redirect, $text, '', 'addcontact_confirm', $formquestion, '', 1, 300, 590);
$formconfirmtoaddtasks .='
<script>
$(document).ready(function() {
var saveprop = false;
$(".selectall").click(function(){
console.log("We click on select all with "+saveprop);
if (!saveprop) {
$(".taskcheckboxes").prop("checked", true);
saveprop = true;
} else {
$(".taskcheckboxes").prop("checked", false);
saveprop = false;
}
});
});
</script>';
} else {
$action = 'addcontact_confirm';
}
}
// Add new contact
if ($action == 'addcontact_confirm' && $user->rights->projet->creer)
{
@ -67,10 +149,15 @@ if ($action == 'addcontact_confirm' && $user->rights->projet->creer)
{
$contactid = (GETPOST('userid') ? GETPOST('userid', 'int') : GETPOST('contactid', 'int'));
$typeid = (GETPOST('typecontact') ? GETPOST('typecontact') : GETPOST('type'));
$result = $object->add_contact($contactid, $typeid, GETPOST("source", 'aZ09'));
if ( $result < 0) {
if ($object->error == 'DB_ERROR_RECORD_ALREADY_EXISTS')
{
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 {
@ -84,11 +171,11 @@ if ($action == 'addcontact_confirm' && $user->rights->projet->creer)
$task_to_affect = explode(',', $affecttotask);
if (!empty($task_to_affect)) {
foreach ($task_to_affect as $task_id) {
if (GETPOSTISSET('person_'.$task_id, 'san_alpha') && GETPOST('person_'.$task_id, 'san_alpha')=='on') {
if (GETPOSTISSET('person_'.$task_id, 'san_alpha') && GETPOST('person_'.$task_id, 'san_alpha')) {
$tasksToAffect = new Task($db);
$result=$tasksToAffect->fetch($task_id);
if ($result < 0) {
setEventMessage($tasksToAffect->error, 'errors');
setEventMessages($tasksToAffect->error, null, 'errors');
} else {
$result = $tasksToAffect->add_contact($contactid, GETPOST('person_role_'.$task_id), GETPOST("source", 'aZ09'));
if ($result < 0) {
@ -140,6 +227,7 @@ if (($action == 'deleteline' || $action == 'deletecontact') && $user->rights->pr
}
/*
* View
*/
@ -150,7 +238,6 @@ $help_url = "EN:Module_Projects|FR:Module_Projets|ES:M&oacute;dulo_Proyectos";
llxHeader('', $title, $help_url);
$form = new Form($db);
$formcompany = new FormCompany($db);
$contactstatic = new Contact($db);
$userstatic = new User($db);
@ -173,76 +260,7 @@ if ($id > 0 || !empty($ref))
$head = project_prepare_head($object);
print dol_get_fiche_head($head, 'contact', $langs->trans("Project"), -1, ($object->public ? 'projectpub' : 'project'));
$formconfirm = '';
// Test if we can add contact to task at the same times
if ($action == 'addcontact') {
$source=GETPOST("source", 'aZ09');
$taskstatic = new Task($db);
$task_array = $taskstatic->getTasksArray(0, 0, $object->id, 0, 0);
$nbTasks = count($task_array);
//If no task avaiblable, redirec to to add confirm
$type_to = (GETPOST('typecontact') ? 'typecontact='.GETPOST('typecontact') : 'type='.GETPOST('type'));
$personToAffect = (GETPOST('userid') ? GETPOST('userid', 'int') : GETPOST('contactid', 'int'));
$affect_to = (GETPOST('userid') ? 'userid='.$personToAffect : 'contactid='.$personToAffect);
$url_redirect='?id='.$object->id.'&'.$affect_to.'&'.$type_to.'&source='.$source;
if (empty($conf->global->PROJECT_HIDE_TASKS) || $nbTasks > 0) {
$text = $langs->trans('AddPersonToTask');
$formquestion = array('text' => $text);
$task_to_affect = array();
foreach ($task_array as $task) {
$task_already_affected=false;
$personsLinked = $task->liste_contact(-1, $source);
if (!is_array($personsLinked) && coun($personsLinked) < 0) {
setEventMessage($object->error, 'errors');
} else {
foreach ($personsLinked as $person) {
if ($person['id']==$personToAffect) {
$task_already_affected = true;
break;
}
}
if (!$task_already_affected) {
$task_to_affect[$task->id] = $task->ref . ' '.dol_trunc($task->label);
}
}
}
if (empty($task_to_affect)) {
//If person is already add to all task (how it can happen ????, anyway), redirect to to add confirm
header("Location: ".$_SERVER['PHP_SELF'].$url_redirect);
exit;
} else {
foreach ($task_to_affect as $key=>$val) {
$formquestion[] = array('type' => 'checkbox', 'name' => 'person_'.$key, 'label' => $val, 'value' => true);
$formquestion[] = array(
'type' => 'other',
'name' => 'person_role_'. $key,
'label' => $langs->trans("ContactType"),
'value' => $formcompany->selectTypeContact($taskstatic, '', 'person_role_' . $key, $source, 'position', 0, 'minwidth100imp', 0));
}
$formquestion[] = array('type'=> 'other', 'name'=>'tasksavailable', 'label'=>'', 'value' => '<input type="hidden" id="tasksavailable" name="tasksavailable" value="'.implode(',', array_keys($task_to_affect)).'">');
}
$formconfirm = $form->formconfirm($_SERVER['PHP_SELF'] . $url_redirect, $text, '', 'addcontact_confirm', $formquestion, '', 1, 300, 590);
$formconfirm .='
<script>
$(document).ready(function(){
$("input:checkbox[name^=\'person_\']").change(function(){
let taskid=$(this).attr("name").replace("person_","");
$("#person_role_"+taskid).parents("div.tagtr").toggle();
});
});
</script>';
} else {
//If no task avaiblable, redirec to to add confirm
header("Location: ".$_SERVER['PHP_SELF'].$url_redirect.'&action=addcontact_confirm');
exit;
}
}
$formconfirm = $formconfirmtoaddtasks;
// Call Hook formConfirm
$parameters = array('formConfirm' => $formconfirm);