diff --git a/class/actions_digiriskdolibarr.class.php b/class/actions_digiriskdolibarr.class.php index 5ea1a6b9d..36b17738d 100644 --- a/class/actions_digiriskdolibarr.class.php +++ b/class/actions_digiriskdolibarr.class.php @@ -815,16 +815,20 @@ public function printFieldListFrom($parameters, $object) global $conf, $user, $langs; $sql = ''; - /* print_r($parameters); print_r($object); echo "action: " . $action; */ - if (preg_match('/ticketlist|thirdpartyticket|projectticket/', $parameters['context'])) { // do something only for the context 'somecontext1' or 'somecontext2' + if (preg_match('/ticketlist|thirdpartyticket|projectticket/', $parameters['context'])) { $searchCategoryTicketList = GETPOST('search_category_ticket_list'); if (!empty($searchCategoryTicketList)) { - $sql = ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_ticket as ct ON t.rowid = ct.fk_ticket"; // We'll need this table joined to the select in order to filter by categ + $sql = ' LEFT JOIN '.MAIN_DB_PREFIX."categorie_ticket as ct ON t.rowid = ct.fk_ticket"; } } + if (preg_match('/preventionplanlist/', $parameters['context'])) { + $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'digiriskdolibarr_digiriskresources as rs' + . ' ON rs.ref = "ExtSociety" AND rs.object_type = "preventionplan" AND rs.object_id = t.rowid'; + } + $this->resprints = $sql; - return 0; // or return 1 to replace standard code + return 0; } @@ -1314,6 +1318,98 @@ public function saturneAddAttendantRow($parameters) return 0; } + /** + * Overloading the printFieldListSearch function : replacing the parent's function with the one below + * + * @param array $parameters Hook metadata (context, etc...) + * @return int 0 < on error, 0 on success, 1 to replace standard code + */ + public function printFieldListSearch(array $parameters): int + { + if (preg_match('/preventionplanlist/', $parameters['context'])) { + if ($parameters['key'] == 'ExtSociety') { + $val = (int) ($parameters['val'] ?? 0); + if ($val > 0) { + echo ' AND rs.element_id = ' . $val; + } + return 1; // Replace default SQL generation + } + } + + return 0; + } + + /** + * Overloading the saturnePrintFieldListLoopObject function : replacing the parent's function with the one below + * + * @param array $parameters Hook metadata (context, etc...) + * @param object $object Current object + * @return int 0 < on error, 0 on success, 1 to replace standard code + */ + public function saturnePrintFieldListLoopObject(array $parameters, object $object): int + { + if (preg_match('/preventionplanlist/', $parameters['context'])) { + $key = $parameters['key']; + + if (!in_array($key, ['MasterWorker', 'ExtSociety', 'ExtSocietyResponsible', 'ExtSocietyAttendant'])) { + return 0; + } + + require_once DOL_DOCUMENT_ROOT . '/user/class/user.class.php'; + require_once DOL_DOCUMENT_ROOT . '/contact/class/contact.class.php'; + require_once __DIR__ . '/../../saturne/class/saturnesignature.class.php'; + require_once __DIR__ . '/../class/digiriskresources.class.php'; + + global $moduleNameLowerCase; + + $signatory = new SaturneSignature($this->db, $moduleNameLowerCase, $object->element); + $digiriskresources = new DigiriskResources($this->db); + $out = []; + + if ($key == 'MasterWorker') { + $element = $signatory->fetchSignatory('MasterWorker', $object->id, 'preventionplan'); + if (is_array($element) && !empty($element)) { + $element = array_shift($element); + $usertmp = new User($this->db); + $usertmp->fetch($element->element_id); + $out[$key] = $usertmp->getNomUrl(1); + } else { + $out[$key] = ''; + } + } elseif ($key == 'ExtSociety') { + $extSociety = $digiriskresources->fetchResourcesFromObject('ExtSociety', $object); + $out[$key] = ($extSociety > 0) ? $extSociety->getNomUrl(1) : ''; + } elseif ($key == 'ExtSocietyResponsible') { + $element = $signatory->fetchSignatory('ExtSocietyResponsible', $object->id, 'preventionplan'); + if (is_array($element) && !empty($element)) { + $element = array_shift($element); + $contact = new Contact($this->db); + $contact->fetch($element->element_id); + $out[$key] = $contact->getNomUrl(1); + } else { + $out[$key] = ''; + } + } elseif ($key == 'ExtSocietyAttendant') { + $attendants = $signatory->fetchSignatory('ExtSocietyAttendant', $object->id, 'preventionplan'); + $html = ''; + if (is_array($attendants) && !empty($attendants)) { + $contact = new Contact($this->db); + foreach ($attendants as $attendant) { + if ($attendant->element_id > 0) { + $contact->fetch($attendant->element_id); + $html .= $contact->getNomUrl(1) . '
'; + } + } + } + $out[$key] = $html; + } + + $this->results = $out; + } + + return 0; + } + public function printFieldListValue($parameters, $object, $action) { global $db; diff --git a/core/modules/modDigiriskDolibarr.class.php b/core/modules/modDigiriskDolibarr.class.php index 4c5b1e055..1d58c874c 100644 --- a/core/modules/modDigiriskDolibarr.class.php +++ b/core/modules/modDigiriskDolibarr.class.php @@ -441,7 +441,8 @@ public function __construct($db) 'category', 'categoryindex', 'main', - 'saturnegetobjectsmetadata' + 'saturnegetobjectsmetadata', + 'preventionplanlist' ], 'tabs' => [ 'mycompany_admin' diff --git a/view/preventionplan/preventionplan_list.php b/view/preventionplan/preventionplan_list.php index 706c75736..637c395ea 100644 --- a/view/preventionplan/preventionplan_list.php +++ b/view/preventionplan/preventionplan_list.php @@ -1,5 +1,5 @@ +/* Copyright (C) 2021-2025 EVARISK * * 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 @@ -16,572 +16,209 @@ */ /** - * \file view/preventionplan/preventionplan_list.php - * \ingroup digiriskdolibarr - * \brief List page for prevention plan + * \file view/preventionplan/preventionplan_list.php + * \ingroup digiriskdolibarr + * \brief List page for prevention plan */ -// Load DigiriskDolibarr environment. +// Load DigiriskDolibarr environment if (file_exists('../../digiriskdolibarr.main.inc.php')) { - require_once __DIR__ . '/../../digiriskdolibarr.main.inc.php'; + require_once __DIR__ . '/../../digiriskdolibarr.main.inc.php'; } elseif (file_exists('../../../digiriskdolibarr.main.inc.php')) { - require_once __DIR__ . '/../../../digiriskdolibarr.main.inc.php'; + require_once __DIR__ . '/../../../digiriskdolibarr.main.inc.php'; } else { - die('Include of digiriskdolibarr main fails'); + die('Include of digiriskdolibarr main fails'); } -// Load Dolibarr libraries. -require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php'; -require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php'; -require_once DOL_DOCUMENT_ROOT . '/core/lib/images.lib.php'; - -// load Saturne libraries. -require_once __DIR__ . '/../../../saturne/class/saturnesignature.class.php'; - -// load DigiriskDolibarr libraries. +// Load DigiriskDolibarr libraries require_once __DIR__ . '/../../class/preventionplan.class.php'; -require_once __DIR__ . '/../../class/digiriskresources.class.php'; -// Global variables definitions. +// Global variables definitions global $conf, $db, $hookmanager, $langs, $user; -// Load translation files required by the page. +// Load translation files required by the page saturne_load_langs(['projects', 'companies', 'commercial']); -// Get parameters. -$id = GETPOST('id', 'int'); -$ref = GETPOST('ref', 'alpha'); -$action = GETPOST('action', 'aZ09') ? GETPOST('action', 'aZ09') : 'view'; -$confirm = GETPOST('confirm', 'alpha'); -$cancel = GETPOST('cancel', 'alpha'); -$backtopage = GETPOST('backtopage', 'alpha'); // Go back to a dedicated page - -// Get list parameters. -$massaction = GETPOST('massaction', 'alpha'); // The bulk action (combo box choice into lists) -$show_files = GETPOST('show_files', 'int'); // Show files area generated by bulk actions ? -$toselect = GETPOST('toselect', 'array'); // Array of ids of elements selected into a list -$contextpage = GETPOST('contextpage', 'aZ') ? GETPOST('contextpage', 'aZ') : 'timesheetlist'; // To manage different context of search -$optioncss = GETPOST('optioncss', 'aZ'); // Option for the css output (always '' except when 'print') -$mode = GETPOST('mode', 'aZ'); - -// Get pagination parameters. -$limit = GETPOST('limit', 'int') ? GETPOST('limit', 'int') : $conf->liste_limit; -$sortfield = GETPOST("sortfield", "alpha"); -$sortorder = GETPOST("sortorder", 'alpha'); -$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int'); -$page = is_numeric($page) ? $page : 0; -$page = $page == -1 ? 0 : $page; - -$offset = $limit * $page; -$pageprev = $page - 1; -$pagenext = $page + 1; - -// Initialize technical objects. -$object = new PreventionPlan($db); -$digiriskresources = new DigiriskResources($db); -$extrafields = new ExtraFields($db); -$societe = new Societe($db); -$contact = new Contact($db); -$usertmp = new User($db); -$signatory = new SaturneSignature($db, $moduleNameLowerCase, $object->element); - -// Initialize view objects. -$form = new Form($db); -$formother = new FormOther($db); - -$hookmanager->initHooks(['preventionplanlist']); // Note that conf->hooks_modules contains array. - -// Fetch optionals attributes and labels. -$extrafields->fetch_name_optionals_label($object->table_element); -$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_'); - -// Default sort order (if not yet defined by previous GETPOST). -if (!$sortfield) { - reset($object->fields); // Reset is required to avoid key() to return null. - $sortfield = 't.' . key($object->fields); // Set here default search field. By default, 1st field in definition. -} -if (!$sortorder) { - $sortorder = 'ASC'; -} - -// Initialize array of search criterias. -$search_all = GETPOST('search_all') ? GETPOST('search_all') : GETPOST('sall'); -$search = []; -foreach ($object->fields as $key => $val) { - if (GETPOST('search_' . $key, 'alpha') !== '') { - $search[$key] = GETPOST('search_' . $key, 'alpha'); - } - if (preg_match('/^(date|timestamp|datetime)/', $val['type'])) { - $search[$key . '_date_start'] = dol_mktime(0, 0, 0, GETPOST('search_' . $key . '_date_startmonth', 'int'), GETPOST('search_' . $key . '_date_startday', 'int'), GETPOST('search_' . $key . '_date_startyear', 'int')); - $search[$key . '_date_end'] = dol_mktime(23, 59, 59, GETPOST('search_' . $key . '_date_endmonth', 'int'), GETPOST('search_' . $key . '_date_endday', 'int'), GETPOST('search_' . $key . '_date_endyear', 'int')); - } -} - -// List of fields to search into when doing a "search in all". -$fieldstosearchall = []; -foreach ($object->fields as $key => $val) { - if (!empty($val['searchall'])) { - $fieldstosearchall['t.' . $key] = $val['label']; - } -} - -// Definition of fields for list -$arrayfields = array(); - -// Definition of array of fields for columns. -$arrayfields = []; -foreach ($object->fields as $key => $val) { - // If $val['visible']==0, then we never show the field - if (!empty($val['visible'])) { - $visible = (int)dol_eval($val['visible'], 1); - $arrayfields['t.' . $key] = [ - 'label' => $val['label'], - 'checked' => (($visible < 0) ? 0 : 1), - 'enabled' => ($visible != 3 && dol_eval($val['enabled'], 1)), - 'position' => $val['position'], - 'help' => $val['help'] ?? '' - ]; - } -} - -// Extra fields -include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_list_array_fields.tpl.php'; - -// Load PreventionPlan object -include DOL_DOCUMENT_ROOT . '/core/actions_fetchobject.inc.php'; // Must be include, not include_once. - -$object->fields = dol_sort_array($object->fields, 'position'); -$arrayfields = dol_sort_array($arrayfields, 'position'); - -// Security check (enable the most restrictive one) - Protection if external user -$permissiontoread = $user->rights->digiriskdolibarr->preventionplan->read; -$permissiontoadd = $user->rights->digiriskdolibarr->preventionplan->write; -$permissiontodelete = $user->rights->digiriskdolibarr->preventionplan->delete; -saturne_check_access($permissiontoread); - -/* - * Actions - */ - -$parameters = []; -$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks. -if ($reshook < 0) { - setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); -} +// Get parameters +$action = GETPOSTISSET('action') ? GETPOST('action', 'aZ09') : 'view'; +$massaction = GETPOST('massaction', 'alpha'); -if (empty($reshook)) { - if (GETPOST('cancel', 'alpha')) { - $action = 'list'; - $massaction = ''; - } - if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { - $massaction = ''; - } - - // Selection of new fields. - include DOL_DOCUMENT_ROOT . '/core/actions_changeselectedfields.inc.php'; - - // Purge search criteria. - if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers. - foreach ($object->fields as $key => $val) { - $search[$key] = ''; - if (preg_match('/^(date|timestamp|datetime)/', $val['type'])) { - $search[$key.'_date_start'] = ''; - $search[$key.'_date_end'] = ''; - } - } - $toselect = []; - $search_array_options = []; - $search_category_array = []; - } - if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha') - || GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha') || GETPOST('button_search', 'alpha')) { - $massaction = ''; // Protection to avoid mass action if we force a new search during a mass action confirmation. - } - - $error = 0; - if ( ! $error && ($massaction == 'delete' || ($action == 'delete' && $confirm == 'yes')) && $permissiontodelete) { - if ( ! empty($toselect)) { - foreach ($toselect as $toselectedid) { - $objecttodelete = $object; - $objecttodelete->fetch($toselectedid); - - $objecttodelete->status = 0; - $result = $objecttodelete->delete($user); - - if ($result < 0) { - // Delete preventionplan KO - if ( ! empty($risk->errors)) setEventMessages(null, $risk->errors, 'errors'); - else setEventMessages($risk->error, null, 'errors'); - } - } - - // Delete preventionplan OK - $urltogo = str_replace('__ID__', $result, $backtopage); - $urltogo = preg_replace('/--IDFORBACKTOPAGE--/', $id, $urltogo); // New method to autoselect project after a New on another form object creation - header("Location: " . $_SERVER["PHP_SELF"]); - exit; - } - } +// Get list parameters +$toselect = []; +[$confirm, $contextpage, $optioncss, $mode] = ['', '', '', '']; +$listParameters = saturne_load_list_parameters(basename(dirname(__FILE__))); +foreach ($listParameters as $listParameterKey => $listParameter) { + $$listParameterKey = $listParameter; } -/* - * View - */ - - -$title = $langs->trans("PreventionPlanList"); -$helpUrl = 'FR:Module_Digirisk#DigiRisk_-_Plan_de_pr.C3.A9vention'; - -saturne_header(0, '', $title, $helpUrl); - -// Add $param from extra fields -include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_list_search_param.tpl.php'; - -// List of mass actions available -$arrayofmassactions = array(); -if ($permissiontodelete) $arrayofmassactions['predelete'] = '' . $langs->trans("Delete"); -if (in_array($massaction, array('presend', 'predelete'))) $arrayofmassactions = array(); - -$massactionbutton = $form->selectMassAction('', $arrayofmassactions); - -$newcardbutton = ''; -if ($permissiontoadd) { - $newcardbutton .= dolGetButtonTitle($langs->trans('NewPreventionPlan'), '', 'fa fa-plus-circle', DOL_URL_ROOT . '/custom/digiriskdolibarr/view/preventionplan/preventionplan_card.php?action=create'); +// Get pagination parameters +[$limit, $page, $offset] = [0, 0, 0]; +[$sortfield, $sortorder] = ['', '']; +$paginationParameters = saturne_load_pagination_parameters(); +foreach ($paginationParameters as $paginationParameterKey => $paginationParameter) { + $$paginationParameterKey = $paginationParameter; } -print '
'; -if ($optioncss != '') print ''; -print ''; -print ''; -print ''; -print ''; -print ''; -print ''; -print ''; - -include DOL_DOCUMENT_ROOT . '/core/tpl/massactions_pre.tpl.php'; - -// Build and execute select -// -------------------------------------------------------------------- - -$sql = 'SELECT '; -foreach ($object->fields as $key => $val) { - $sql .= 't.' . $key . ', '; -} -// Add fields from extrafields -if ( ! empty($extrafields->attributes[$object->table_element]['label'])) { - foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) $sql .= ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? "ef." . $key . ' as options_' . $key . ', ' : ''); -} -// Add fields from hooks -$parameters = array(); -$reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object); // Note that $action and $objectdocument may have been modified by hook -$sql .= preg_replace('/^,/', '', $hookmanager->resPrint); -$sql = preg_replace('/,\s*$/', '', $sql); -$sql .= " FROM " . MAIN_DB_PREFIX . $object->table_element . " as t"; - -if (isset($extrafields->attributes[$object->table_element]['label']) && - is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . $object->table_element . "_extrafields as ef on (t.rowid = ef.fk_object)"; -$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'digiriskdolibarr_digiriskresources as rs ON rs.ref = "ExtSociety" AND rs.object_type = "preventionplan" AND rs.object_id = t.rowid'; - -if ($object->ismultientitymanaged == 1) $sql .= " WHERE t.entity IN (" . getEntity($object->element) . ")"; -else $sql .= " WHERE 1 = 1"; -$sql .= ' AND t.status != -1'; - - -foreach ($search as $key => $val) { - if ($key == 'status' && $search[$key] == -1) - continue; - $mode_search = ((isset($object->fields[$key]) && ($object->isInt($object->fields[$key]) || $object->isFloat($object->fields[$key]))) ? 1 : 0); - if (strpos($object->fields[$key]['type'] ?? '', 'integer:') === 0) { - if ($search[$key] == '-1') - $search[$key] = ''; - $mode_search = 2; - } - if ($search[$key] != '') - $sql .= natural_search($key, $search[$key], (($key == 'status') ? 2 : $mode_search)); -} -if ($search_all) $sql .= natural_search(array_keys($fieldstosearchall), $search_all); +// Initialize technical objects +$object = new PreventionPlan($db); +$extrafields = new ExtraFields($db); -if (GETPOSTISSET('search_extsociety') && GETPOSTINT('search_extsociety') != -1) { - $sql .= ' AND rs.element_id = ' . GETPOSTINT('search_extsociety'); -} +// Initialize view objects +$form = new Form($db); -// Add where from extra fields -include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_list_search_sql.tpl.php'; -// Add where from hooks -$parameters = []; -$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $objectdocument may have been modified by hook -$sql .= $hookmanager->resPrint; - -$sql .= $db->order($sortfield, $sortorder); - -// Count total nb of records -$nbtotalofrecords = ''; -if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) { - $resql = $db->query($sql); - $nbtotalofrecords = $db->num_rows($resql); - if (($page * $limit) > $nbtotalofrecords) { // if total of record found is smaller than page * limit, goto and load page 0 - $page = 0; - $offset = 0; - } -} - // if total of record found is smaller than limit, no need to do paging and to restart another select with limits set. -if (is_numeric($nbtotalofrecords) && ($limit > $nbtotalofrecords || empty($limit))) { - $num = $nbtotalofrecords; -} else { - if ($limit) $sql .= $db->plimit($limit + 1, $offset); +$hookmanager->initHooks([$contextpage]); // Note that conf->hooks_modules contains array - $resql = $db->query($sql); - if ( ! $resql) { - dol_print_error($db); - exit; - } +// Fetch optionals attributes and labels +$extrafields->fetch_name_optionals_label($object->table_element); +$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_'); - $num = $db->num_rows($resql); +// Default sort order (if not yet defined by previous GETPOST) +if (!$sortfield) { + $sortfield = 't.ref'; } - - // Direct jump if only one record found -if ($num == 1 && ! empty($conf->global->MAIN_SEARCH_DIRECT_OPEN_IF_ONLY_ONE) && $search_all && ! $page) { - $obj = $db->fetch_object($resql); - $id = $obj->rowid; - header("Location: " . dol_buildpath('/digiriskdolibarr/view/preventionplan/preventionplan_card.php', 1) . '?id=' . $id); - exit; +if (!$sortorder) { + $sortorder = 'ASC'; } -if ($search_all) { - foreach ($fieldstosearchall as $key => $val) $fieldstosearchall[$key] = $langs->trans($val); - print '
' . $langs->trans("FilterOnInto", $search_all) . join(', ', $fieldstosearchall) . '
'; -} +// Enable multi-select for status field and use short labels (matching LibStatut short) +$object->fields['status']['searchmulti'] = 1; +$object->fields['status']['arrayofkeyval'] = [ + PreventionPlan::STATUS_DRAFT => 'Draft', + PreventionPlan::STATUS_VALIDATED => 'Enabled', + PreventionPlan::STATUS_LOCKED => 'Locked', + PreventionPlan::STATUS_ARCHIVED => 'Archived', +]; -$moreforfilter = ''; +// Fields excluded from SQL SELECT (virtual/computed columns) +$excludeFields = ['MasterWorker', 'ExtSociety', 'ExtSocietyResponsible', 'ExtSocietyAttendant']; -$varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage; +// Add custom display-only fields +$object->fields['MasterWorker'] = ['label' => 'MasterWorker', 'enabled' => 1, 'position' => 190, 'visible' => 1, 'disablesort' => 1, 'csslist' => 'tdoverflowmax125', 'type' => 'varchar(255)', 'disablesearch' => 1]; +$object->fields['ExtSociety'] = ['label' => 'ExtSociety', 'enabled' => 1, 'position' => 200, 'visible' => 1, 'disablesort' => 1, 'csslist' => 'tdoverflowmax125', 'type' => 'integer', 'arrayofkeyval' => []]; +$object->fields['ExtSocietyResponsible'] = ['label' => 'ExtSocietyResponsible', 'enabled' => 1, 'position' => 210, 'visible' => 1, 'disablesort' => 1, 'csslist' => 'tdoverflowmax125', 'type' => 'varchar(255)', 'disablesearch' => 1]; +$object->fields['ExtSocietyAttendant'] = ['label' => 'ExtSocietyAttendant', 'enabled' => 1, 'position' => 220, 'visible' => 1, 'disablesort' => 1, 'csslist' => 'tdoverflowmax125', 'type' => 'varchar(255)', 'disablesearch' => 1]; -$arrayfields['MasterWorker'] = ['label' => 'MasterWorker', 'checked' => 1]; -$arrayfields['ExtSociety'] = ['label' => 'ExtSociety', 'checked' => 1, 'arrayofkeyval' => [], 'sortfield' => 'rs.object_id']; -$tmpsql = 'SELECT rowid, nom FROM ' . MAIN_DB_PREFIX . 'societe WHERE entity = ' . $conf->entity; +// Populate companies list for ExtSociety filter dropdown +$tmpsql = 'SELECT rowid, nom FROM ' . MAIN_DB_PREFIX . 'societe WHERE entity = ' . $conf->entity . ' ORDER BY nom'; $tmpresql = $db->query($tmpsql); while ($tmpobj = $db->fetch_object($tmpresql)) { - $arrayfields['ExtSociety']['arrayofkeyval'][$tmpobj->rowid] = $tmpobj->nom; + $object->fields['ExtSociety']['arrayofkeyval'][$tmpobj->rowid] = $tmpobj->nom; } $db->free($tmpresql); -$arrayfields['ExtSocietyResponsible'] = ['label' => 'ExtSocietyResponsible', 'checked' => 1]; -$arrayfields['ExtSocietyAttendant'] = ['label' => 'ExtSocietyAttendant', 'checked' => 1]; - -print_barre_liste($form->textwithpicto($title, $texthelp ?? ''), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, $object->picto, 0, $newcardbutton, '', $limit, 0, 0, 1); - -$selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields -if ($massactionbutton) $selectedfields .= $form->showCheckAddButtons('checkforselect', 1); - -print '
'; -print '' . "\n"; -print ''; - -$object->fields['Custom']['MasterWorker'] = $arrayfields['MasterWorker'] ; -$object->fields['Custom']['ExtSociety'] = $arrayfields['ExtSociety']; -$object->fields['Custom']['ExtSocietyResponsible'] = $arrayfields['ExtSocietyResponsible']; -$object->fields['Custom']['ExtSocietyAttendant'] = $arrayfields['ExtSocietyAttendant'] ; +// Initialize array of search criterias +$searchAll = trim(GETPOST('search_all')); +$search = []; foreach ($object->fields as $key => $val) { - $cssforfield = (empty($val['css']) ? '' : $val['css']); - if ($key == 'status') $cssforfield .= ($cssforfield ? ' ' : '') . 'center'; - if ( ! empty($arrayfields['t.' . $key]['checked'])) { - print ''; - } - if ($key == 'Custom') { - foreach ($val as $resource) { - if ($resource['checked']) { - print ''; - } - } - } + if (!empty($val['searchmulti'])) { + $search[$key] = GETPOST('search_' . $key, 'array'); + } elseif (GETPOST('search_' . $key, 'alpha') !== '') { + $search[$key] = GETPOST('search_' . $key, 'alpha'); + } + if (isset($val['type']) && in_array($val['type'], ['date', 'datetime', 'timestamp'])) { + $search[$key . '_dtstart'] = dol_mktime(0, 0, 0, GETPOSTINT('search_' . $key . '_dtstartmonth'), GETPOSTINT('search_' . $key . '_dtstartday'), GETPOSTINT('search_' . $key . '_dtstartyear')); + $search[$key . '_dtend'] = dol_mktime(23, 59, 59, GETPOSTINT('search_' . $key . '_dtendmonth'), GETPOSTINT('search_' . $key . '_dtendday'), GETPOSTINT('search_' . $key . '_dtendyear')); + } } -// Extra fields -include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_list_search_input.tpl.php'; - -// Fields from hook -$parameters = array('arrayfields' => $arrayfields); -$reshook = $hookmanager->executeHooks('printFieldListOption', $parameters, $object); // Note that $action and $objectdocument may have been modified by hook -print $hookmanager->resPrint; - -// Action column -print ''; -print '' . "\n"; - -// Fields title label -// -------------------------------------------------------------------- -print ''; +// List of fields to search into when doing a "search in all" +$fieldsToSearchAll = []; +foreach ($object->fields as $key => $val) { + if (!empty($val['searchall'])) { + $fieldsToSearchAll['t.' . $key] = $val['label']; + } +} +// Definition of array of fields for columns +$arrayfields = []; foreach ($object->fields as $key => $val) { - $cssforfield = (empty($val['css']) ? '' : $val['css']); - if ($key == 'status') $cssforfield .= ($cssforfield ? ' ' : '') . 'center'; - if ( ! empty($arrayfields['t.' . $key]['checked'])) { - if (preg_match('/MasterWorker/', $arrayfields['t.' . $key]['label']) || preg_match('/ExtSociety/', $arrayfields['t.' . $key]['label']) || preg_match('/NbIntervenants/', $arrayfields['t.' . $key]['label']) || preg_match('/NbInterventions/', $arrayfields['t.' . $key]['label']) || preg_match('/Location/', $arrayfields['t.' . $key]['label'])) { - $disablesort = 1; - } else { - $disablesort = 0; - } - print getTitleFieldOfList($arrayfields['t.' . $key]['label'], 0, $_SERVER['PHP_SELF'], 't.' . $key, '', $param, ($cssforfield ? 'class="' . $cssforfield . '"' : ''), $sortfield, $sortorder, ($cssforfield ? $cssforfield . ' ' : ''), $disablesort) . "\n"; - } - if ($key == 'Custom') { - foreach ($val as $resource) { - if (!empty($resource['checked'])) { - if (!empty($resource['sortfield'])) { - $disablesort = 0; - } else { - $disablesort = 1; - } - print getTitleFieldOfList($resource['label'], 0, $_SERVER['PHP_SELF'], $resource['sortfield'], '', $param, ($cssforfield ? 'class="' . $cssforfield . '"' : ''), $sortfield, $sortorder, ($cssforfield ? $cssforfield . ' ' : ''), $disablesort) . "\n"; - } - } - } + if (!empty($val['visible'])) { + $visible = (int) dol_eval($val['visible']); + $arrayfields['t.' . $key] = [ + 'label' => $val['label'], + 'checked' => (($visible < 0) ? 0 : 1), + 'enabled' => ($visible != 3 && dol_eval($val['enabled'])), + 'position' => $val['position'], + 'help' => $val['help'] ?? '', + ]; + } } // Extra fields -include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_list_search_title.tpl.php'; - -// Hook fields -$parameters = array('arrayfields' => $arrayfields, 'param' => $param, 'sortfield' => $sortfield, 'sortorder' => $sortorder); -$reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters, $object); // Note that $action and $objectdocument may have been modified by hook -print $hookmanager->resPrint; +require_once DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_list_array_fields.tpl.php'; -// Action column -print getTitleFieldOfList($selectedfields, 0, $_SERVER["PHP_SELF"], '', '', '', '', $sortfield, $sortorder, 'center maxwidthsearch ') . "\n"; -print '' . "\n"; +$object->fields = dol_sort_array($object->fields, 'position'); +$arrayfields = dol_sort_array($arrayfields, 'position'); -$arrayofselected = is_array($toselect) ? $toselect : array(); +// Permissions +$permissiontoread = $user->hasRight($object->module, 'preventionplan', 'read'); +$permissiontoadd = $user->hasRight($object->module, 'preventionplan', 'write'); +$permissiontodelete = $user->hasRight($object->module, 'preventionplan', 'delete'); -// Loop on record -// -------------------------------------------------------------------- +// Security check +saturne_check_access($permissiontoread, $object); -// contenu -$i = 0; -$totalarray = ['nbfield' => 0]; +/* + * Actions + */ -while ($i < ($limit ? min($num, $limit) : $num)) { - $obj = $db->fetch_object($resql); +$parameters = ['arrayfields' => &$arrayfields]; +$resHook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks +if ($resHook < 0) { + setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); +} - if (empty($obj)) break; // Should not happen +if (empty($resHook)) { + // Selection of new fields + require_once DOL_DOCUMENT_ROOT . '/core/actions_changeselectedfields.inc.php'; - // Store properties in $objectdocument - $object->setVarsFromFetchObj($obj); + // Purge search criteria + if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers + foreach ($object->fields as $key => $val) { + $search[$key] = ''; + if (isset($val['type']) && in_array($val['type'], ['date', 'datetime', 'timestamp'])) { + $search[$key . '_dtstart'] = ''; + $search[$key . '_dtend'] = ''; + } + } + $searchAll = ''; + $toselect = []; + $search_array_options = []; + } + if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha') + || GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha') || GETPOST('button_search', 'alpha')) { + $massaction = ''; // Protection to avoid mass action if we force a new search during a mass action confirmation + } - if (isset($object->json)) { - $json = json_decode($object->json, false, 512, JSON_UNESCAPED_UNICODE)->PreventionPlan; - } else { - $json = []; + if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { + $massaction = ''; } - // Show here line of result - print ''; - foreach ($object->fields as $key => $val) { - $cssforfield = (empty($val['css']) ? '' : $val['css']); - if ($key == 'status') $cssforfield .= ($cssforfield ? ' ' : '') . 'center'; - elseif ($key == 'ref') $cssforfield .= ($cssforfield ? ' ' : '') . 'nowrap'; - elseif ($key == 'category') $cssforfield .= ($cssforfield ? ' ' : '') . 'preventionplandocument-category'; - elseif ($key == 'description') $cssforfield .= ($cssforfield ? ' ' : '') . 'preventionplandocument-description'; - if ( ! empty($arrayfields['t.' . $key]['checked'])) { - print ''; - if ($key == 'status') print $object->getLibStatut(5); - elseif ($key == 'ref') { - print ' ' . $object->getNomUrl(); - } elseif ($key == 'date_start') { - print dol_print_date($object->date_start, 'dayhour', 'tzserver'); // We suppose dates without time are always gmt (storage of course + output) - } elseif ($key == 'date_end') { - print dol_print_date($object->date_end, 'dayhour', 'tzserver'); // We suppose dates without time are always gmt (storage of course + output) - } else print $object->showOutputField($val, $key, $object->$key, ''); - print ''; - if ( ! $i) $totalarray['nbfield']++; - if ( ! empty($val['isameasure'])) { - if ( ! $i) $totalarray['pos'][$totalarray['nbfield']] = 't.' . $key; - $totalarray['val']['t.' . $key] += $object->$key; - } - } - if ($key == 'Custom') { - foreach ($val as $name => $resource) { - if ($resource['checked']) { - print ''; - } - } - } - } - // Action column - print ''; - if ( ! $i) $totalarray['nbfield']++; - print '' . "\n"; - $i++; -} -// If no record found -if ($num == 0) { - $colspan = 1; - foreach ($arrayfields as $key => $val) { if ( ! empty($val['checked'])) $colspan++; } - print ''; + // Mass actions + $objectclass = 'PreventionPlan'; + $objectlabel = 'PreventionPlan'; + $uploaddir = $conf->digiriskdolibarr->dir_output; + require_once DOL_DOCUMENT_ROOT . '/core/actions_massactions.inc.php'; + + // Mass actions archive + require_once __DIR__ . '/../../../saturne/core/tpl/actions/list_massactions.tpl.php'; } -$db->free($resql); -$parameters = array('arrayfields' => $arrayfields, 'sql' => $sql); -$reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters, $risk); // Note that $action and $risk may have been modified by hook -print $hookmanager->resPrint; +/* + * View + */ + +$title = $langs->trans('PreventionPlanList'); +$helpUrl = 'FR:Module_Digirisk#DigiRisk_-_Plan_de_pr.C3.A9vention'; + +saturne_header(0, '', $title, $helpUrl, '', 0, 0, [], [], '', 'mod-' . $object->module . '-' . $object->element . ' page-list bodyforlist'); -print "
'; - - if (isset($val['arrayofkeyval']) && is_array($val['arrayofkeyval'])) print $form->selectarray('search_' . $key, $val['arrayofkeyval'], $search[$key] ?? '', $val['notnull'], 0, 0, '', 1, 0, 0, '', 'maxwidth75'); - elseif (strpos($val['type'], 'integer:') === 0) { - print $object->showInputField($val, $key, $search[$key] ?? '', '', '', 'search_', 'maxwidth150', 1); - } elseif ( ! preg_match('/^(date|timestamp)/', $val['type'])) print ''; - print ''; - if (isset($resource['arrayofkeyval']) && is_array($resource['arrayofkeyval'])) { - print $form->selectarray('search_' . dol_strtolower($resource['label']), $resource['arrayofkeyval'], GETPOSTINT('search_' . dol_strtolower($resource['label'])), 1, 0, 0, '', 1, 0, 0, '', 'maxwidth150'); - } - print ''; -$searchpicto = $form->showFilterButtons(); -print $searchpicto; -print '
'; - if ($resource['label'] == 'MasterWorker') { - $element = $signatory->fetchSignatory('MasterWorker', $object->id, 'preventionplan'); - if (is_array($element) && !empty($element)) { - $element = array_shift($element); - $usertmp->fetch($element->element_id); - print $usertmp->getNomUrl(1); - } - } elseif ($resource['label'] == 'ExtSociety') { - $extSociety = $digiriskresources->fetchResourcesFromObject('ExtSociety', $object); - if ($extSociety > 0) { - print $extSociety->getNomUrl(1); - } - } - if ($resource['label'] == 'ExtSocietyResponsible') { - $element = $signatory->fetchSignatory('ExtSocietyResponsible', $object->id, 'preventionplan'); - if (is_array($element) && !empty($element)) { - $element = array_shift($element); - $contact->fetch($element->element_id); - print $contact->getNomUrl(1); - } - } - if ($resource['label'] == 'ExtSocietyAttendant') { - $extSociety_intervenants = $signatory->fetchSignatory('ExtSocietyAttendant', $object->id, 'preventionplan'); - if (is_array($extSociety_intervenants) && ! empty($extSociety_intervenants) && $extSociety_intervenants > 0) { - foreach ($extSociety_intervenants as $element) { - if ($element > 0) { - $contact->fetch($element->element_id); - print $contact->getNomUrl(1); - print '
'; - } - } - } - } - print '
'; - if ($massactionbutton || $massaction) { // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined - $selected = 0; - if (in_array($object->id, $arrayofselected)) $selected = 1; - print ''; - } - - print '
' . $langs->trans("NoRecordFound") . '
\n"; -print '
'; -print "
\n"; +require_once __DIR__ . '/../../../saturne/core/tpl/list/objectfields_list_build_sql_select.tpl.php'; +require_once __DIR__ . '/../../../saturne/core/tpl/list/objectfields_list_header.tpl.php'; +require_once __DIR__ . '/../../../saturne/core/tpl/list/objectfields_list_search_input.tpl.php'; +require_once __DIR__ . '/../../../saturne/core/tpl/list/objectfields_list_search_title.tpl.php'; +require_once __DIR__ . '/../../../saturne/core/tpl/list/objectfields_list_loop_object.tpl.php'; +require_once __DIR__ . '/../../../saturne/core/tpl/list/objectfields_list_footer.tpl.php'; // End of page llxFooter();