Skip to content

Commit

Permalink
[PreventionPlan] fix: ajax combobox fort select user & fix edit action
Browse files Browse the repository at this point in the history
  • Loading branch information
evarisk-theo committed Jul 7, 2021
1 parent 24a71ef commit fea5498
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 46 deletions.
80 changes: 62 additions & 18 deletions class/digirisksignature.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DigiriskSignature extends CommonObject
/**
* @var string Name of table without prefix where object is stored. This is also the key used for extrafields management.
*/
public $table_element = 'digiriskdolibarr_digirisksignature';
public $table_element = 'digiriskdolibarr_preventionplan_signature';

/**
* @var int Does this object support multicompany module ?
Expand All @@ -64,14 +64,15 @@ class DigiriskSignature extends CommonObject
*/
public $picto = 'digirisksignature@digiriskdolibarr';

const STATUS_REGISTERED = 0;
const STATUS_SIGNATURE_REQUEST = 1;
const STATUS_PENDING_SIGNATURE = 2;
const STATUS_DENIED = 3;
const STATUS_SIGNED = 4;
const STATUS_UNSIGNED = 5;
const STATUS_ABSENT = 6;
const STATUS_JUSTIFIED_ABSENT = 7;
const STATUS_DELETED = 0;
const STATUS_REGISTERED = 1;
const STATUS_SIGNATURE_REQUEST = 2;
const STATUS_PENDING_SIGNATURE = 3;
const STATUS_DENIED = 4;
const STATUS_SIGNED = 5;
const STATUS_UNSIGNED = 6;
const STATUS_ABSENT = 7;
const STATUS_JUSTIFIED_ABSENT = 8;

/**
* @var array Array with all fields and their property. Do not use it as a static var. It may be modified by constructor.
Expand Down Expand Up @@ -242,6 +243,7 @@ public function fetchAll($sortorder = '', $sortfield = '', $limit = 0, $offset =
}

$resql = $this->db->query($sql);

if ($resql) {
$num = $this->db->num_rows($resql);
$i = 0;
Expand Down Expand Up @@ -398,19 +400,22 @@ public function LibStatut($status, $mode = 0)
}

/**
* Clone an object into another one
* Create signatory in database
*
* @param varchar $ref name of resource
* @param varchar $element_type type of resource
* @param int $element_id Id of resource
* @param int $fk_object ID of object linked
* @param varchar $element_type Type of resource
* @param int $element_id Id of resource
* @param varchar $role Role of resource
*/
function setSignatory($fk_object, $element_type, $element_ids, $role = "")
{
global $conf, $user;

$society = new Societe($this->db);
$this->deletePreviousSignatories($role, $fk_object);

foreach ( $element_ids as $element_id ) {

if ($element_type == 'user') {
$signatory_data = new User($this->db);

Expand Down Expand Up @@ -460,15 +465,54 @@ function setSignatory($fk_object, $element_type, $element_ids, $role = "")
}

/**
* Clone an object into another one
* Fetch signatory from database
*
* @param varchar $ref name of resource
* @param varchar $element_type type of resource
* @param int $element_id Id of resource
* @param varchar $role Role of resource
* @param int $fk_object ID of object linked
*/
function fetchSignatory($role = "", $fk_object)
{
return $this->fetchAll('', '', 0, 0, array('customsql' => 'fk_object = '.$fk_object .' AND '.'role = '.'"'.$role.'"'), 'AND');
$filter = array('fk_object' => $fk_object, 'status' => 1);
if (strlen($role)) {
$filter['role'] = $role;

return $this->fetchAll('', '', 0, 0, $filter, 'AND');
} else {

$signatories = $this->fetchAll('', '', 0, 0, $filter, 'AND');
if (!empty ($signatories) && $signatories > 0) {
foreach($signatories as $signatory) {
$signatoriesArray[$signatory->role][$signatory->id] = $signatory;
}

return $signatoriesArray;
} else {
return 0;
}
}
}

/**
* Set previous signatories status to 0
*
* @param varchar $role Role of resource
* @param int $fk_object ID of object linked
*/
function deletePreviousSignatories($role = "", $fk_object)
{
global $user;
$filter = array('customsql' => ' role="' . $role . '" AND fk_object=' . $fk_object . ' AND status=1');
$signatoriesToDelete = $this->fetchAll('', '', 0, 0, $filter, 'AND');

if ( ! empty($signatoriesToDelete) && $signatoriesToDelete > 0) {

foreach($signatoriesToDelete as $signatoryToDelete) {

$signatoryToDelete->status = 0;
$signatoryToDelete->update($user);

}
}
}
}

4 changes: 2 additions & 2 deletions langs/fr_FR/digiriskdolibarr.lang
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ DigiriskNumberOfModelFilesFound = Nombre de modèles par défaut
DigiriskConfig = Configuration du module
DigiriskConfigSociety = Configuration société
PermissionDenied = Permission refusée
AddUser = Ajouter un utilisateur
AddUser = Créer un utilisateur
StartDate = Date de début
EndDate = Date de fin
NoPhoneNumber = Pas de numéro de téléphone
Expand Down Expand Up @@ -287,7 +287,7 @@ PriorVisitText = Note de la visite
CSSCTIntervention = Intervention du CSSCT
CSSCTInterventionText = Intervention du CSSCT ?
MaitreOeuvre = Responsable de l'Entreprise Utilisatrice
ExtSociety = Entreprise extérieure
ExtSociety = Entreprise Extérieure
ExtSocietyResponsible = Responsable de l'Entreprise Extérieure
ExtSocietyIntervenants = Intervenants de l'Entreprise Extérieure
ActionsDescription = Description des actions
Expand Down
55 changes: 29 additions & 26 deletions preventionplan_card.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
if (!$res) die("Include of main fails");

require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';

require_once __DIR__ . '/class/digiriskdocuments.class.php';
require_once __DIR__ . '/class/digiriskelement.class.php';
Expand Down Expand Up @@ -156,11 +157,8 @@

if ($result > 0) {

// $digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_MAITRE_OEUVRE', 'user', array($maitre_oeuvre_id), $conf->entity, 'preventionplan', $object->id, 1);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_EXT_SOCIETY', 'societe', array($extsociety_id), $conf->entity, 'preventionplan', $object->id, 1);
// $digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_EXT_SOCIETY_RESPONSIBLE', 'socpeople', $extresponsible_id, $conf->entity, 'preventionplan', $object->id, 1);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_LABOUR_INSPECTOR_ASSIGNED', 'societe', array($labour_inspector_id), $conf->entity, 'preventionplan', $object->id, 1);
// $digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_EXT_SOCIETY_INTERVENANTS', 'socpeople', $extintervenant_ids, $conf->entity, 'preventionplan', $object->id, 1);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_EXT_SOCIETY', 'societe', array($extsociety_id), $conf->entity, 'preventionplan', $object->id, 1);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_LABOUR_INSPECTOR_ASSIGNED', 'societe', array($labour_inspector_id), $conf->entity, 'preventionplan', $object->id, 1);

$signatory->setSignatory($object->id,'user', array($maitre_oeuvre_id), 'PP_MAITRE_OEUVRE');
$signatory->setSignatory($object->id,'socpeople', array($extresponsible_id), 'PP_EXT_SOCIETY_RESPONSIBLE');
Expand Down Expand Up @@ -219,11 +217,12 @@

if ($result > 0) {

$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_MAITRE_OEUVRE', 'user', array($maitre_oeuvre_id), $conf->entity, 'preventionplan', $object->id, 0);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_EXT_SOCIETY', 'societe', array($extsociety_id), $conf->entity, 'preventionplan', $object->id, 0);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_EXT_SOCIETY_RESPONSIBLE', 'socpeople', $extresponsible_id, $conf->entity, 'preventionplan', $object->id, 0);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_LABOUR_DOCTOR_ASSIGNED', 'societe', array($labour_inspector_id), $conf->entity, 'preventionplan', $object->id, 0);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_EXT_SOCIETY_INTERVENANTS', 'socpeople', $extintervenant_ids, $conf->entity, 'preventionplan', $object->id, 0);
$digiriskresources->digirisk_dolibarr_set_resources($db, $user->id, 'PP_LABOUR_INSPECTOR_ASSIGNED', 'societe', array($labour_inspector_id), $conf->entity, 'preventionplan', $object->id, 0);

$signatory->setSignatory($object->id,'user', array($maitre_oeuvre_id), 'PP_MAITRE_OEUVRE');
$signatory->setSignatory($object->id,'socpeople', array($extresponsible_id), 'PP_EXT_SOCIETY_RESPONSIBLE');
$signatory->setSignatory($object->id,'socpeople', $extintervenant_ids, 'PP_EXT_SOCIETY_INTERVENANTS');

// Creation prevention plan OK
$urltogo = str_replace('__ID__', $result, $backtopage);
Expand Down Expand Up @@ -523,10 +522,11 @@
print '</td>';
print '<td>';
print '<input type="checkbox" id="prior_visit_bool" name="prior_visit_bool"'.(GETPOSTISSET('prior_visit_bool') ? (GETPOST('prior_visit_bool', 'alpha') != '' ? ' checked=""' : '') : ' checked=""').'"> ';
$htmltext = $langs->trans("PriorVisitText");
print $form->textwithpicto('', $htmltext);
print '</td></tr>';

print '<input class="flat" type="text" size="36" name="prior_visit_text" id="prior_visit_text" value="">';
print '<tr class="oddeven"><td><label for="prior_visit_text">'.$langs->trans("PriorVisitText").'</label></td><td>';
$doleditor = new DolEditor('prior_visit_text', '', '', 90, 'dolibarr_notes', '', false, true, $conf->global->FCKEDITOR_ENABLE_SOCIETE, ROWS_3, '90%');
$doleditor->Create();
print '</td></tr>';

//Labour inspector -- Inspecteur du travail
Expand Down Expand Up @@ -577,11 +577,11 @@

unset($object->fields['status']);
unset($object->fields['element_type']);
//unset($object->fields['fk_parent']);
unset($object->fields['last_main_doc']);
unset($object->fields['entity']);

$object_resources = $digiriskresources->fetchResourcesFromObject('', $object);
$object_signatories = $signatory->fetchSignatory('',$object->id);

print '<table class="border centpercent tableforfieldedit">'."\n";

Expand All @@ -604,16 +604,18 @@
print $form->selectDate(dol_time_plus_duree(dol_now(),1,'y'), 'date_fin', 1, 1, 0);
print '</td></tr>';

$maitre_oeuvre = is_array($object_signatories['PP_MAITRE_OEUVRE']) ? array_shift($object_signatories['PP_MAITRE_OEUVRE'])->element_id : '';

//Maitre d'oeuvre
$userlist = $form->select_dolusers(is_array($object_resources['PP_MAITRE_OEUVRE']) ? array_shift($object_resources['PP_MAITRE_OEUVRE'])->id : '', '', 0, null, 0, '', '', 0, 0, 0, 'AND u.statut = 1', 0, '', '', 0, 1);
$userlist = $form->select_dolusers($maitre_oeuvre, '', 0, null, 0, '', '', 0, 0, 0, 'AND u.statut = 1', 0, '', '', 0, 1);

print '<tr>';
print '<td style="width:10%">'.$form->editfieldkey('MaitreOeuvre', 'MaitreOeuvre_id', '', $object, 0).'</td>';
print '<td class="maxwidthonsmartphone">';

if (!GETPOSTISSET('backtopage')) print ' <a href="'.DOL_URL_ROOT.'/user/card.php?action=create&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create').'"><span class="fa fa-plus-circle valignmiddle paddingleft" title="'.$langs->trans("AddThirdParty").'"></span></a>';
print $form->selectarray('maitre_oeuvre', $userlist,$maitre_oeuvre, null, null, null, null, "40%", 0, 0, 0, '',1);

print $form->selectarray('maitre_oeuvre', $userlist, 0, null, null, null, null, "40%");
if (!GETPOSTISSET('backtopage')) print ' <a href="'.DOL_URL_ROOT.'/user/card.php?action=create&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create').'" target="_blank"><span class="fa fa-plus-circle valignmiddle paddingleft" title="'.$langs->trans("AddUser").'"></span></a>';

print '</td></tr>';

Expand All @@ -623,7 +625,6 @@
print '</td>';

print '<td>';
if (!GETPOSTISSET('backtopage')) print ' <a href="'.DOL_URL_ROOT.'/societe/card.php?action=create&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create').'"><span class="fa fa-plus-circle valignmiddle paddingleft" title="'.$langs->trans("AddThirdParty").'"></span></a>';

$events = array();
$events[1] = array('method' => 'getContacts', 'url' => dol_buildpath('/core/ajax/contacts.php?showempty=1', 1), 'htmlname' => 'ext_society_responsible', 'params' => array('add-customer-contact' => 'disabled'));
Expand All @@ -635,12 +636,13 @@
$ext_society_id = is_array($object_resources['PP_EXT_SOCIETY']) ? array_shift($object_resources['PP_EXT_SOCIETY'])->id : '';

print $form->select_company($ext_society_id, 'ext_society', '', 'SelectThirdParty', 1, 0, $events, 0, 'minwidth300');
} print '<br>';
}
if (!GETPOSTISSET('backtopage')) print ' <a href="'.DOL_URL_ROOT.'/societe/card.php?action=create&backtopage='.urlencode($_SERVER["PHP_SELF"].'?action=create').'" target="_blank"><span class="fa fa-plus-circle valignmiddle paddingleft" title="'.$langs->trans("AddThirdParty").'"></span></a>';

print '</td></tr>';

//External responsible -- Responsable de la société extérieure
$ext_society_responsible_id = is_array($object_resources['PP_EXT_SOCIETY_RESPONSIBLE']) ? array_shift($object_resources['PP_EXT_SOCIETY_RESPONSIBLE'])->id : '';
$ext_society_responsible_id = is_array($object_signatories['PP_EXT_SOCIETY_RESPONSIBLE']) ? array_shift($object_signatories['PP_EXT_SOCIETY_RESPONSIBLE'])->id : '';
print '<tr class="oddeven"><td>'.$langs->trans("ExtSocietyResponsible").'</td><td>';
print $form->selectcontacts(GETPOST('ext_society', 'int'), $ext_society_responsible_id, 'ext_society_responsible[]', 1, '', '', 0, 'quatrevingtpercent', false, 0, array(), false, '', 'ext_society_responsible');

Expand All @@ -649,8 +651,8 @@
//Intervenants extérieurs
$resources_ids = array();

if (!empty ($object_resources['PP_EXT_SOCIETY_INTERVENANTS']) && $object_resources['PP_EXT_SOCIETY_INTERVENANTS'] > 0) {
foreach ($object_resources['PP_EXT_SOCIETY_INTERVENANTS'] as $resource) {
if (!empty ($object_signatories['PP_EXT_SOCIETY_INTERVENANTS']) && $object_signatories['PP_EXT_SOCIETY_INTERVENANTS'] > 0) {
foreach ($object_signatories['PP_EXT_SOCIETY_INTERVENANTS'] as $resource) {
$resources_ids[] = $resource->id;
}
}
Expand All @@ -664,22 +666,23 @@
print $langs->trans("CSSCTIntervention");
print '</td>';
print '<td>';
print '<input type="checkbox" id="cssct_intervention" name="cssct_intervention"'.($object->cssct_intervention? ' checked=""' : '').'"> ';
print '<input type="checkbox" id="cssct_intervention" name="cssct_intervention"'.($object->cssct_intervention ? ' checked=""' : '').'"> ';
$htmltext = $langs->trans("CSSCTInterventionText");
print $form->textwithpicto('', $htmltext);
print '<br>';
print '</td></tr>';

//Prior Visit -- Visite préalable
print '<tr><td class="tdtop">';
print '<tr class="oddeven"><td class="tdtop">';
print $langs->trans("PriorVisit");
print '</td>';
print '<td>';
print '<input type="checkbox" id="prior_visit_bool" name="prior_visit_bool"'.($object->prior_visit_bool? ' checked=""' : '').'"> ';
$htmltext = $langs->trans("PriorVisitText");
print $form->textwithpicto('', $htmltext);
print '</td></tr>';

print '<input class="flat" type="text" size="36" name="prior_visit_text" id="prior_visit_text" value="'.$object->prior_visit_text.'">';
print '<tr class="oddeven"><td><label for="prior_visit_text">'.$langs->trans("PriorVisitText").'</label></td><td>';
$doleditor = new DolEditor('prior_visit_text', $object->prior_visit_text, '', 90, 'dolibarr_notes', '', false, true, $conf->global->FCKEDITOR_ENABLE_SOCIETE, ROWS_3, '90%');
$doleditor->Create();
print '</td></tr>';

//Labour inspector -- Inspecteur du travail
Expand Down

0 comments on commit fea5498

Please sign in to comment.