From b14de0bff7d0bd2fe4281dfcf53fab09204dd01a Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Tue, 4 May 2021 10:56:21 +0200 Subject: [PATCH] NEW Can clone a cron job. --- htdocs/cron/card.php | 55 ++++++++++++++++++++++++----- htdocs/cron/class/cronjob.class.php | 11 +++--- htdocs/langs/en_US/cron.lang | 2 +- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/htdocs/cron/card.php b/htdocs/cron/card.php index 0a042e56e03..1683fadf16a 100644 --- a/htdocs/cron/card.php +++ b/htdocs/cron/card.php @@ -36,10 +36,6 @@ require_once DOL_DOCUMENT_ROOT.'/core/lib/cron.lib.php'; // Load translation files required by the page $langs->loadLangs(array('admin', 'cron', 'members')); -if (!$user->rights->cron->create) { - accessforbidden(); -} - $id = GETPOST('id', 'int'); $action = GETPOST('action', 'aZ09'); $confirm = GETPOST('confirm', 'alpha'); @@ -49,6 +45,12 @@ $backtopageforcancel = GETPOST('backtopageforcancel', 'alpha'); $securitykey = GETPOST('securitykey', 'alpha'); +$permissiontoadd = $user->rights->cron->create; + +if (!$user->rights->cron->create) { + accessforbidden(); +} + /* * Actions @@ -219,6 +221,30 @@ if ($action == 'inactive') { } } +// Action clone object +if ($action == 'confirm_clone' && $confirm == 'yes' && !empty($permissiontoadd)) { + if (1 == 0 && !GETPOST('clone_content') && !GETPOST('clone_receivers')) { + setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors'); + } else { + $objectutil = dol_clone($object, 1); // To avoid to denaturate loaded object when setting some properties for clone or if createFromClone modifies the object. We use native clone to keep this->db valid. + + $result = $objectutil->createFromClone($user, (($object->id > 0) ? $object->id : $id)); + if (is_object($result) || $result > 0) { + $newid = 0; + if (is_object($result)) { + $newid = $result->id; + } else { + $newid = $result; + } + header("Location: ".$_SERVER['PHP_SELF'].'?id='.$newid); // Open record of new object + exit; + } else { + setEventMessages($objectutil->error, $objectutil->errors, 'errors'); + $action = ''; + } + } +} + /* @@ -230,9 +256,9 @@ $formCron = new FormCron($db); llxHeader('', $langs->trans("CronTask")); -if ($action == 'edit' || empty($action) || $action == 'delete' || $action == 'execute') { - $head = cron_prepare_head($object); -} elseif ($action == 'create') { +$head = cron_prepare_head($object); + +if ($action == 'create') { print load_fiche_titre($langs->trans("CronTask"), '', 'title_setup'); } @@ -259,17 +285,26 @@ if ($conf->use_javascript_ajax) { } if ($action == 'delete') { - print $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id, $langs->trans("CronDelete"), $langs->trans("CronConfirmDelete"), "confirm_delete", '', '', 1); + $formconfirm = $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id, $langs->trans("CronDelete"), $langs->trans("CronConfirmDelete"), "confirm_delete", '', '', 1); $action = ''; } if ($action == 'execute') { - print $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id.'&securitykey='.$securitykey, $langs->trans("CronExecute"), $langs->trans("CronConfirmExecute"), "confirm_execute", '', '', 1); + $formconfirm = $form->formconfirm($_SERVER['PHP_SELF']."?id=".$object->id.'&securitykey='.$securitykey, $langs->trans("CronExecute"), $langs->trans("CronConfirmExecute"), "confirm_execute", '', '', 1); $action = ''; } +// Clone confirmation +if ($action == 'clone') { + // Create an array for form + $formquestion = array(); + $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneAsk', $object->ref), 'confirm_clone', $formquestion, 'yes', 1); +} + +// Print form confirm +print $formconfirm; /* @@ -727,6 +762,8 @@ if (($action == "create") || ($action == "edit")) { if (!$user->rights->cron->create) { print ''.$langs->trans("CronStatusActiveBtn").'/'.$langs->trans("CronStatusInactiveBtn").''; } else { + print ''.$langs->trans("Clone").''; + if (empty($object->status)) { print ''.$langs->trans("CronStatusActiveBtn").''; } else { diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index f54f7c40c4c..2ffce20a924 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -859,6 +859,8 @@ class Cronjob extends CommonObject */ public function createFromClone(User $user, $fromid) { + global $langs; + $error = 0; $object = new Cronjob($this->db); @@ -868,10 +870,10 @@ class Cronjob extends CommonObject // Load source object $object->fetch($fromid); $object->id = 0; - $object->statut = 0; // Clear fields - // ... + $object->status = self::STATUS_DISABLED; + $object->label = $langs->trans("CopyOf").' '.$object->label; // Create clone $object->context['createfromclone'] = 'createfromclone'; @@ -883,11 +885,6 @@ class Cronjob extends CommonObject $error++; } - //if (! $error) - //{ - - //} - unset($object->context['createfromclone']); // End diff --git a/htdocs/langs/en_US/cron.lang b/htdocs/langs/en_US/cron.lang index 2ebdda1e685..4fd2220dea6 100644 --- a/htdocs/langs/en_US/cron.lang +++ b/htdocs/langs/en_US/cron.lang @@ -60,7 +60,7 @@ CronErrEndDateStartDt=End date cannot be before start date StatusAtInstall=Status at module installation CronStatusActiveBtn=Schedule CronStatusInactiveBtn=Disable -CronTaskInactive=This job is disabled +CronTaskInactive=This job is disabled (not scheduled) CronId=Id CronClassFile=Filename with class CronModuleHelp=Name of Dolibarr module directory (also work with external Dolibarr module).
For example to call the fetch method of Dolibarr Product object /htdocs/product/class/product.class.php, the value for module is
product