From 5588e1a3789507f370eed460eeedc532ab384351 Mon Sep 17 00:00:00 2001 From: Nicolas Domenech Date: Fri, 15 Sep 2023 09:56:49 +0200 Subject: [PATCH 1/2] #680 [SartuneForm] add: class for manage button and formconfirm --- class/saturneform.class.php | 204 ++++++++++++++++++++++++++++++++++++ 1 file changed, 204 insertions(+) create mode 100644 class/saturneform.class.php diff --git a/class/saturneform.class.php b/class/saturneform.class.php new file mode 100644 index 00000000..65c2dbd1 --- /dev/null +++ b/class/saturneform.class.php @@ -0,0 +1,204 @@ + + * + * 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 . + */ + +/** + * \file class/saturneform.class.php + * \ingroup saturne + * \brief Class file for manage SaturneForm + */ + +// Load Dolibarr libraries +require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; +require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php'; + +/** + * Class for SaturneForm + */ +class SaturneForm +{ + /** + * @var bool Browser layout is on phone + */ + public bool $OnPhone = false; + + /** + * Constructor + */ + public function __construct() + { + global $conf; + + if ($conf->browser->layout == 'phone') { + $this->OnPhone = true; + } + } + + /** + * Show modify button + * + * @param SaturneObject $object Current object + * @param array $moreParams More parameters + */ + public function showModifyButton(SaturneObject $object, array $moreParams = []) + { + global $langs; + + // Modify + $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Modify'); + if (($object->status == $object::STATUS_DRAFT || $moreParams['check'])) { + print '' . $displayButton . ''; + } else { + print '' . $displayButton . ''; + } + } + + /** + * Show validate button + * + * @param SaturneObject $object Current object + * @param array $moreParams More parameters + */ + public function showValidateButton(SaturneObject $object, array $moreParams = []) + { + global $langs; + + // Validate + $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Validate'); + if (($object->status == $object::STATUS_DRAFT || $moreParams['check'])) { + print '' . $displayButton . ''; + } elseif ($object->status < $object::STATUS_DRAFT) { + print '' . $displayButton . ''; + } + } + + /** + * Show reopen button + * + * @param SaturneObject $object Current object + */ + public function showReOpenButton(SaturneObject $object) + { + global $langs; + + // ReOpen + $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('ReOpenDoli'); + if ($object->status == $object::STATUS_VALIDATED) { + print '' . $displayButton . ''; + } elseif ($object->status > $object::STATUS_VALIDATED) { + print '' . $displayButton . ''; + } + } + + /** + * Show sign button + * + * @param SaturneObject $object Current object + * @param SaturneSignature $signatory Signatory object + * @throws Exception + */ + public function showSignButton(SaturneObject $object, SaturneSignature $signatory) + { + global $langs; + + // Sign + $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Sign'); + if ($object->status == $object::STATUS_VALIDATED && !$signatory->checkSignatoriesSignatures($object->id, $object->element)) { + print '' . $displayButton . ''; + } else { + print '' . $displayButton . ''; + } + } + + /** + * Show lock button + * + * @param SaturneObject $object Current object + * @param SaturneSignature $signatory Signatory object + * @throws Exception + */ + public function showlockButton(SaturneObject $object, SaturneSignature $signatory) + { + global $langs; + + // Lock + $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Lock'); + if ($object->status == $object::STATUS_VALIDATED && $signatory->checkSignatoriesSignatures($object->id, $object->element)) { + print '' . $displayButton . ''; + } else { + print '' . $displayButton . ''; + } + } + + /** + * Show send email button + * + * @param SaturneObject $object Current object + * @param string $uploadDir Upload dir path + */ + public function showSendEmailButton(SaturneObject $object, string $uploadDir) + { + global $langs; + + // Send email + $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('SendMail'); + if ($object->status == $object::STATUS_LOCKED) { + $fileParams = dol_most_recent_file($uploadDir . '/' . $object->element . 'document' . '/' . $object->ref); + $file = $fileParams['fullname']; + if (file_exists($file) && !strstr($fileParams['name'], 'specimen')) { + $forceBuildDoc = 0; + } else { + $forceBuildDoc = 1; + } + print dolGetButtonAction($displayButton, '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=presend&forcebuilddoc=' . $forceBuildDoc . '&mode=init#formmailbeforetitle'); + } else { + print '' . $displayButton . ''; + } + } + + /** + * Show archive button + * + * @param SaturneObject $object Current object + */ + public function showArchiveButton(SaturneObject $object) + { + global $langs; + + // Archive + $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Archive'); + if ($object->status == $object::STATUS_LOCKED) { + print '' . $displayButton . ''; + } else { + print '' . $displayButton . ''; + } + } + + /** + * Show delete button + * + * @param SaturneObject $object Current object + * @param int $permissionToDelete Delete object permission + */ + public function showDeleteButton(SaturneObject $object, int $permissionToDelete) + { + global $langs; + + // Delete (need delete permission, or if draft, just need create/modify permission) + $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Delete'); + print dolGetButtonAction($displayButton, '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete&token=' . newToken(), '', $permissionToDelete || ($object->status == $object::STATUS_DRAFT)); + } +} From e530cf465d30ec78430f587fd7938fcc59d60b0c Mon Sep 17 00:00:00 2001 From: Nicolas Domenech Date: Thu, 17 Oct 2024 17:39:42 +0200 Subject: [PATCH 2/2] #680 [SartuneForm] add: improve SaturneForm --- class/saturneform.class.php | 322 +++++++++++++++++++++++++++--------- langs/fr_FR/object.lang | 2 + 2 files changed, 242 insertions(+), 82 deletions(-) diff --git a/class/saturneform.class.php b/class/saturneform.class.php index 65c2dbd1..43722f79 100644 --- a/class/saturneform.class.php +++ b/class/saturneform.class.php @@ -1,5 +1,5 @@ +/* Copyright (C) 2021-2024 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 @@ -21,48 +21,26 @@ * \brief Class file for manage SaturneForm */ -// Load Dolibarr libraries -require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; -require_once DOL_DOCUMENT_ROOT . '/core/lib/functions.lib.php'; - /** * Class for SaturneForm */ -class SaturneForm +abstract class SaturneForm { - /** - * @var bool Browser layout is on phone - */ - public bool $OnPhone = false; - - /** - * Constructor - */ - public function __construct() - { - global $conf; - - if ($conf->browser->layout == 'phone') { - $this->OnPhone = true; - } - } - /** * Show modify button * * @param SaturneObject $object Current object * @param array $moreParams More parameters */ - public function showModifyButton(SaturneObject $object, array $moreParams = []) + public function showModifyButton(SaturneObject $object, array $moreParams = []): void { - global $langs; + global $conf, $langs; - // Modify - $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Modify'); + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('Modify'); if (($object->status == $object::STATUS_DRAFT || $moreParams['check'])) { - print '' . $displayButton . ''; - } else { - print '' . $displayButton . ''; + print '' . $displayButton . ''; + } elseif ($object->status < $object::STATUS_DRAFT) { + print '' . $displayButton . ''; } } @@ -72,90 +50,91 @@ public function showModifyButton(SaturneObject $object, array $moreParams = []) * @param SaturneObject $object Current object * @param array $moreParams More parameters */ - public function showValidateButton(SaturneObject $object, array $moreParams = []) + public static function showValidateButton(SaturneObject $object, array $moreParams = []): void { - global $langs; + global $conf, $langs; - // Validate - $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Validate'); + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('Validate'); if (($object->status == $object::STATUS_DRAFT || $moreParams['check'])) { print '' . $displayButton . ''; } elseif ($object->status < $object::STATUS_DRAFT) { - print '' . $displayButton . ''; + print '' . $displayButton . ''; } } /** * Show reopen button * - * @param SaturneObject $object Current object + * @param SaturneObject $object Current object + * @param array $moreParams More parameters */ - public function showReOpenButton(SaturneObject $object) + public static function showReOpenButton(SaturneObject $object, array $moreParams = []): void { - global $langs; + global $conf, $langs; - // ReOpen - $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('ReOpenDoli'); + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('ReOpenDoli'); if ($object->status == $object::STATUS_VALIDATED) { print '' . $displayButton . ''; - } elseif ($object->status > $object::STATUS_VALIDATED) { - print '' . $displayButton . ''; + } elseif ($object->status < $object::STATUS_VALIDATED) { + print '' . $displayButton . ''; } } /** * Show sign button * - * @param SaturneObject $object Current object - * @param SaturneSignature $signatory Signatory object + * @param SaturneObject $object Current object + * @param array $moreParams More parameters * @throws Exception */ - public function showSignButton(SaturneObject $object, SaturneSignature $signatory) + public static function showSignButton(SaturneObject $object, array $moreParams = []): void { - global $langs; + global $conf, $db, $document, $langs; - // Sign - $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Sign'); + $signatory = new SaturneSignature($db, $object->module, $object->element); + + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('Sign'); if ($object->status == $object::STATUS_VALIDATED && !$signatory->checkSignatoriesSignatures($object->id, $object->element)) { - print '' . $displayButton . ''; - } else { - print '' . $displayButton . ''; + print '' . $displayButton . ''; + } elseif ($object->status < $object::STATUS_VALIDATED) { + print '' . $displayButton . ''; } } /** * Show lock button * - * @param SaturneObject $object Current object - * @param SaturneSignature $signatory Signatory object + * @param SaturneObject $object Current object + * @param array $moreParams More parameters * @throws Exception */ - public function showlockButton(SaturneObject $object, SaturneSignature $signatory) + public static function showLockButton(SaturneObject $object, array $moreParams = []): void { - global $langs; + global $conf, $db, $langs; - // Lock - $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Lock'); + $signatory = new SaturneSignature($db, $object->module, $object->element); + + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('Lock'); if ($object->status == $object::STATUS_VALIDATED && $signatory->checkSignatoriesSignatures($object->id, $object->element)) { print '' . $displayButton . ''; - } else { - print '' . $displayButton . ''; + } elseif ($object->status < $object::STATUS_LOCKED) { + print '' . $displayButton . ''; } } /** * Show send email button * - * @param SaturneObject $object Current object - * @param string $uploadDir Upload dir path + * @param SaturneObject $object Current object + * @param array $moreParams More parameters */ - public function showSendEmailButton(SaturneObject $object, string $uploadDir) + public static function showSendEmailButton(SaturneObject $object, array $moreParams = []): void { - global $langs; + global $conf, $langs; - // Send email - $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('SendMail'); - if ($object->status == $object::STATUS_LOCKED) { + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('SendMail'); + if ($object->status >= $object::STATUS_VALIDATED) { + $uploadDir = $conf->{$object->module}->multidir_output[$conf->entity ?? 1]; $fileParams = dol_most_recent_file($uploadDir . '/' . $object->element . 'document' . '/' . $object->ref); $file = $fileParams['fullname']; if (file_exists($file) && !strstr($fileParams['name'], 'specimen')) { @@ -165,40 +144,219 @@ public function showSendEmailButton(SaturneObject $object, string $uploadDir) } print dolGetButtonAction($displayButton, '', 'default', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=presend&forcebuilddoc=' . $forceBuildDoc . '&mode=init#formmailbeforetitle'); } else { - print '' . $displayButton . ''; + print '' . $displayButton . ''; } } /** * Show archive button * - * @param SaturneObject $object Current object + * @param SaturneObject $object Current object + * @param array $moreParams More parameters */ - public function showArchiveButton(SaturneObject $object) + public static function showArchiveButton(SaturneObject $object, array $moreParams = []): void { - global $langs; + global $conf, $langs; - // Archive - $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Archive'); + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('Archive'); if ($object->status == $object::STATUS_LOCKED) { - print '' . $displayButton . ''; - } else { - print '' . $displayButton . ''; + print '' . $displayButton . ''; + } elseif ($object->status < $object::STATUS_ARCHIVED) { + print '' . $displayButton . ''; } } + /** + * Show clone button + * + * @param SaturneObject $object Current object + * @param array $moreParams More parameters + */ + public static function showCloneButton(SaturneObject $object, array $moreParams = []): void + { + global $conf, $langs; + + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('ToClone'); + print '' . $displayButton . ''; + } + /** * Show delete button * - * @param SaturneObject $object Current object - * @param int $permissionToDelete Delete object permission + * @param SaturneObject $object Current object + * @param array $moreParams More parameters */ - public function showDeleteButton(SaturneObject $object, int $permissionToDelete) + public static function showDeleteButton(SaturneObject $object, array $moreParams = []): void { - global $langs; + global $conf, $langs, $user; + + $displayButton = $conf->browser->layout != 'classic' ? '' : '' . ' ' . $langs->transnoentities('Delete'); + if ($object->status == $object::STATUS_DRAFT || $user->hasRight($object->module, $object->element, 'delete')) { + print '' . $displayButton . ''; + } + } + + /** + * Show buttons for actions + * + * @param object $object Current object + * @param string $action Action + * @param array $moreParams More parameters + */ + public static function showButtons(object $object, string $action = '', array $moreParams = []): void + { + global $hookmanager; + + $parameters = []; + $resHook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); + if ($resHook < 0) { + setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); + } + + if (empty($resHook)) { + $buttons = ['showModifyButton', 'showValidateButton', 'showReOpenButton', 'showSignButton', 'showLockButton', 'showSendEmailButton', 'showArchiveButton', 'showCloneButton', 'showDeleteButton']; + foreach ($buttons as $method) { + if (!isset($moreParams['override' . ucfirst($method)])) { + self::$method($object, $moreParams[$method] ?? []); + } + } + } + } + + /** + * Draft confirmation + * + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function draftConfirmation(array $moreParams = []): string + { + global $form, $langs, $object; + + return $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id, $langs->transnoentities('ReOpenObject', $langs->transnoentities('The' . ucfirst($object->element))), $langs->transnoentities('ConfirmReOpenObject', $langs->transnoentities('The' . ucfirst($object->element)), $langs->transnoentities('The' . ucfirst($object->element))), 'confirm_setdraft', '', 'yes', 'actionButtonInProgress'); + } + + /** + * Validate confirmation + * + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function validateConfirmation(array $moreParams = []): string + { + global $form, $langs, $object; + + return $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id, $langs->transnoentities('ValidateObject', $langs->transnoentities('The' . ucfirst($object->element))), $moreParams['question'] ?? $langs->transnoentities('ConfirmValidateObject', $langs->transnoentities('The' . ucfirst($object->element)), $langs->transnoentities('The' . ucfirst($object->element))), 'confirm_validate', '', 'yes', 'actionButtonValidate'); + } + + /** + * Lock confirmation + * + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function lockConfirmation(array $moreParams = []): string + { + global $form, $langs, $object; + + return $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id, $langs->transnoentities('LockObject', $langs->transnoentities('The' . ucfirst($object->element))), $langs->transnoentities('ConfirmLockObject', $langs->transnoentities('The' . ucfirst($object->element))), 'confirm_lock', '', 'yes', 'actionButtonLock'); + } + + /** + * Archive confirmation + * + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function archiveConfirmation(array $moreParams = []): string + { + global $form, $langs, $object; + + return $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id . '&forcebuilddoc=true', $langs->transnoentities('ArchiveObject', $langs->transnoentities('The' . ucfirst($object->element))), $langs->transnoentities('ConfirmArchiveObject', $langs->transnoentities('The' . ucfirst($object->element))), 'confirm_archive', '', 'yes', 'actionButtonArchive'); + } + + /** + * Clone confirmation + * + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function cloneConfirmation(array $moreParams = []): string + { + global $form, $langs, $object; + + return $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id, $langs->transnoentities('CloneObject', $langs->transnoentities('The' . ucfirst($object->element))), $langs->transnoentities('ConfirmCloneObject', $langs->transnoentities('The' . ucfirst($object->element))), 'confirm_clone', $moreParams['formQuestion'] ?? '', 'yes', 'actionButtonClone', 0, $moreParams['width'] ?? 500); + } + + /** + * Delete confirmation + * + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function deleteConfirmation(array $moreParams = []): string + { + global $form, $langs, $object; + + return $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id, $langs->transnoentities('DeleteObject', $langs->transnoentities('The' . ucfirst($object->element))), $langs->transnoentities('ConfirmDeleteObject', $langs->transnoentities('The' . ucfirst($object->element))), 'confirm_delete', '', 'yes', 'actionButtonDelete'); + } + + /** + * Delete line confirmation + * + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function deleteLineConfirmation(array $moreParams = []): string + { + global $form, $langs, $object, $objectLine; + + $lineID = GETPOST('lineid'); + $objectLine->fetch($lineID); + + return $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id . '&lineid=' . $lineID, $langs->transnoentities('DeleteLineObject', $langs->transnoentities('The' . ucfirst($objectLine->element)), $langs->transnoentities('The' . ucfirst($object->element))), $langs->transnoentities('ConfirmDeleteLineObject', $langs->transnoentities('The' . ucfirst($objectLine->element)), $objectLine->ref, $langs->transnoentities('The' . ucfirst($object->element))), 'confirm_delete_line', '', 'yes', 'actionButtonDeleteLine'); + } + + /** + * Remove file confirmation + * + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function removeFileConfirmation(array $moreParams = []): string + { + global $conf, $form, $langs, $object; + + return $form->formconfirm($_SERVER['PHP_SELF'] . '?id=' . $object->id . '&file=' . GETPOST('file') . '&entity=' . $conf->entity, $langs->transnoentities('RemoveFileObject'), $langs->transnoentities('ConfirmRemoveFileObject', GETPOST('file')), 'remove_file', '', 'yes', 'actionButtonRemoveFile'); + } + + /** + * Action confirmation + * + * @param string $action Action + * @param array $moreParams More parameters + * @return string Form confirm + */ + public static function actionConfirmation(string $action, array $moreParams = []): string + { + global $hookmanager, $object; + + $formConfirm = ''; + $confirmations = ['draftConfirmation', 'validateConfirmation', 'lockConfirmation', 'archiveConfirmation', 'cloneConfirmation', 'deleteConfirmation', 'removeFileConfirmation']; + foreach ($confirmations as $method) { + if (!isset($moreParams['override' . ucfirst($method)])) { + $formConfirm .= self::$method($moreParams[$method] ?? []); + } + } + + $parameters = ['formConfirm' => $formConfirm]; + $resHook = $hookmanager->executeHooks('formConfirm', $parameters, $object, $action); + if (empty($resHook)) { + $formConfirm .= $hookmanager->resPrint; + } elseif ($resHook > 0) { + $formConfirm = $hookmanager->resPrint; + } - // Delete (need delete permission, or if draft, just need create/modify permission) - $displayButton = $this->OnPhone ? '' : '' . ' ' . $langs->trans('Delete'); - print dolGetButtonAction($displayButton, '', 'delete', $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&action=delete&token=' . newToken(), '', $permissionToDelete || ($object->status == $object::STATUS_DRAFT)); + return $formConfirm; } } diff --git a/langs/fr_FR/object.lang b/langs/fr_FR/object.lang index 53e2fe36..0fd63fd1 100644 --- a/langs/fr_FR/object.lang +++ b/langs/fr_FR/object.lang @@ -84,6 +84,8 @@ CloneObject = Cloner %s NewLabelForClone = Nouveau libellé de %s ConfirmCloneObject = Êtes-vous sûr de vouloir cloner %s %s ? DeleteObject = Supprimer %s +DeleteLineObject = Supprimer %s sur %s +ConfirmDeleteLineObject = Êtes-vous sûr de vouloir supprimer %s %s sur %s ? RemoveFileObject = Supprimer le document ConfirmRemoveFileObject = Êtes-vous sûr de vouloir supprimer le document %s ?
Cette action est irréversible. Le document sera définitivement supprimé. ArchiveObject = Archiver %s