diff --git a/htdocs/core/class/html.formcron.class.php b/htdocs/core/class/html.formcron.class.php new file mode 100644 index 00000000000..2acb83e3f05 --- /dev/null +++ b/htdocs/core/class/html.formcron.class.php @@ -0,0 +1,93 @@ + +* +* 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 2 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 cron/class/html.formcron.class.php +* \brief Fichier de la classe des fonctions predefinie de composants html cron +*/ + + +/** + * Class to manage building of HTML components +*/ +class FormCron extends Form +{ + var $db; + var $error; + + /** + * Constructor + * + * @param DoliDB $db Database handler + */ + function __construct($db) + { + $this->db = $db; + return 1; + } + + + /** + * Display On Off selector + * + * @param string $htmlname Html control name + * @param string $selected selected value + * @param string $readonly Select is read only or not + * @return string HTML select field + */ + function select_typejob($htmlname,$selected=0,$readonly=0) + { + global $langs; + + $langs->load('cron@cron'); + if (!empty($readonly)) { + if ($selected=='command') { + $out= $langs->trans('CronType_command'); + $out.=''; + } elseif ($selected=='method') { + $out= $langs->trans('CronType_method'); + $out.=''; + } + }else { + + $out=''; + } + + return $out; + } +} diff --git a/htdocs/core/lib/cron.lib.php b/htdocs/core/lib/cron.lib.php new file mode 100644 index 00000000000..abd85131e29 --- /dev/null +++ b/htdocs/core/lib/cron.lib.php @@ -0,0 +1,71 @@ + + * + * 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 cron/lib/cron.lib.php + * \brief Ensemble de fonctions de base pour le module jobs + * \ingroup jobs + */ + +/** + * Return array of tabs to used on pages for third parties cards. + * + * @param Object $object Object company shown + * @return array Array of tabs + */ + +function cronadmin_prepare_head() +{ + global $langs, $conf, $user; + $h = 0; + $head = array(); + + $head[$h][0] = dol_buildpath('/cron/admin/cron.php', 1); + $head[$h][1] = $langs->trans("CronSetup"); + $head[$h][2] = 'setup'; + $h++; + + complete_head_from_modules($conf, $langs, $object, $head, $h, 'cronadmin'); + + complete_head_from_modules($conf, $langs, $object, $head, $h, 'cronadmin', 'remove'); + + + return $head; +} + +function cron_prepare_head($object) +{ + global $langs, $conf, $user; + $h = 0; + $head = array(); + + $head[$h][0] = dol_buildpath('/cron/card.php', 1).'?id='.$object->id; + $head[$h][1] = $langs->trans("CronTask"); + $head[$h][2] = 'card'; + $h++; + + $head[$h][0] = dol_buildpath('/cron/info.php', 1).'?id='.$object->id; + $head[$h][1] = $langs->trans("CronInfoPage"); + $head[$h][2] = 'info'; + $h++; + + complete_head_from_modules($conf, $langs, $object, $head, $h, 'cron'); + + complete_head_from_modules($conf, $langs, $object, $head, $h, 'cron', 'remove'); + + return $head; +} diff --git a/htdocs/core/modules/modCron.class.php b/htdocs/core/modules/modCron.class.php index fbc14945f02..53d9afabf30 100644 --- a/htdocs/core/modules/modCron.class.php +++ b/htdocs/core/modules/modCron.class.php @@ -1,5 +1,6 @@ +/* Copyright (C) 2012 Nicolas Villa aka Boyquotes http://informetic.fr + * Copyright (C) 2013 Florian Henry * * 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,11 +17,11 @@ */ /** - * \defgroup webservices Module webservices - * \brief Module to enable the Dolibarr server of web services - * \file htdocs/core/modules/modCron.class.php - * \ingroup cron - * \brief File to describe cron module + * \defgroup cron Module cron + * \brief cron module descriptor. + * \file cron/core/modules/modCron.class.php + * \ingroup cron + * \brief Description and activation file for module Jobs */ include_once DOL_DOCUMENT_ROOT .'/core/modules/DolibarrModules.class.php'; @@ -65,11 +66,20 @@ class modCron extends DolibarrModules //------------- $this->depends = array(); $this->requiredby = array(); - $this->langfiles = array("cron"); + $this->langfiles = array("cron@cron"); // Constantes //----------- - $this->const = array(); + $this->const = array( + 0=>array( + 'MAIN_CRON_KEY', + 'chaine', + '', + 'CRON KEY', + 0, + 'main', + 0 + ),); // New pages on tabs // ----------------- @@ -79,25 +89,60 @@ class modCron extends DolibarrModules //------ $this->boxes = array(); - // Permissions - //------------ - $this->rights = array(); - $this->rights_class = 'cron'; - $r=0; + // Permissions + $this->rights = array(); // Permission array used by this module + $this->rights_class = 'cron'; + $r=0; + + $this->rights[$r][0] = 23001; + $this->rights[$r][1] = 'Read cron jobs'; + $this->rights[$r][3] = 1; + $this->rights[$r][4] = 'read'; + $r++; + + $this->rights[$r][0] = 23002; + $this->rights[$r][1] = 'Create cron Jobs'; + $this->rights[$r][3] = 0; + $this->rights[$r][4] = 'create'; + $r++; + + $this->rights[$r][0] = 23003; + $this->rights[$r][1] = 'Delete cron Jobs'; + $this->rights[$r][3] = 0; + $this->rights[$r][4] = 'delete'; + $r++; + + $this->rights[$r][0] = 23004; + $this->rights[$r][1] = 'Execute cron Jobs'; + $this->rights[$r][3] = 0; + $this->rights[$r][4] = 'execute'; + $r++; // Main menu entries $r=0; $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=home,fk_leftmenu=modulesadmintools', // Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode 'type'=>'left', // This is a Left menu entry - 'titre'=>'CronJobs', - 'url'=>'/cron/index.php', - 'langs'=>'cron@cron', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. - 'position'=>100, + 'titre'=>'CronListActive', + 'url'=>'/cron/list.php?status=1', + 'langs'=>'cron', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. + 'position'=>200, 'enabled'=>'$leftmenu==\'modulesadmintools\'', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected. - 'perms'=>'$user->admin', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules + 'perms'=>'$user->rights->cron->read', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules 'target'=>'', 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both $r++; + + $this->menu[$r]=array( 'fk_menu'=>'fk_mainmenu=home,fk_leftmenu=modulesadmintools', // Use 'fk_mainmenu=xxx' or 'fk_mainmenu=xxx,fk_leftmenu=yyy' where xxx is mainmenucode and yyy is a leftmenucode + 'type'=>'left', // This is a Left menu entry + 'titre'=>'CronListInactive', + 'url'=>'/cron/list.php?status=0', + 'langs'=>'cron', // Lang file to use (without .lang) by module. File must be in langs/code_CODE/ directory. + 'position'=>201, + 'enabled'=>'$leftmenu==\'modulesadmintools\'', // Define condition to show or hide menu entry. Use '$conf->mymodule->enabled' if entry must be visible if module is enabled. Use '$leftmenu==\'system\'' to show if leftmenu system is selected. + 'perms'=>'$user->rights->cron->read', // Use 'perms'=>'$user->rights->mymodule->level1->level2' if you want your menu with a permission rules + 'target'=>'', + 'user'=>2); // 0=Menu for internal users, 1=external users, 2=both + $r++; } @@ -114,8 +159,6 @@ class modCron extends DolibarrModules // Prevent pb of modules not correctly disabled //$this->remove($options); - $sql = array(); - return $this->_init($sql,$options); } diff --git a/htdocs/cron/admin/cron.php b/htdocs/cron/admin/cron.php index fd77bcc6851..894e9dc4457 100644 --- a/htdocs/cron/admin/cron.php +++ b/htdocs/cron/admin/cron.php @@ -1,34 +1,36 @@ * Copyright (C) 2005-2013 Laurent Destailleur - * Copyright (C) 2011 Juanjo Menent - * Copyright (C) 2012 Regis Houssin - * - * 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 . - */ +* Copyright (C) 2011 Juanjo Menent +* Copyright (C) 2012 Regis Houssin +* Copyright (C) 2013 Florian Henry +* +* 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 htdocs/cron/admin/cron.php - * \ingroup cron - * \brief Page to setup cron module - */ + * \file cron/admin/cron.php +* \ingroup cron +*/ -require '../../main.inc.php'; +// Dolibarr environment +$res = @include("../../main.inc.php"); // From htdocs directory require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/cron.lib.php'; -$langs->load("admin"); -$langs->load("cron"); +$langs->load("admin"); +$langs->load("cron"); if (! $user->admin) accessforbidden(); @@ -36,37 +38,40 @@ if (! $user->admin) $actionsave=GETPOST("save"); // Sauvegardes parametres -if ($actionsave) +if (!empty($actionsave)) { - $i=0; + $i=0; - $db->begin(); + $db->begin(); - $i+=dolibarr_set_const($db,'CRON_KEY',trim(GETPOST("CRON_KEY")),'chaine',0,'',$conf->entity); + $i+=dolibarr_set_const($db,'MAIN_CRON_KEY',trim(GETPOST("MAIN_CRON_KEY")),'chaine',0,'',0); - if ($i >= 1) - { - $db->commit(); - setEventMessage($langs->trans("SetupSaved")); - } - else - { - $db->rollback(); - setEventMessage($langs->trans("Error"), 'errors'); - } + if ($i >= 1) + { + $db->commit(); + setEventMessage($langs->trans("SetupSaved")); + } + else + { + $db->rollback(); + setEventMessage($langs->trans("Error"), 'errors'); + } } /* * View - */ +*/ llxHeader(); $linkback=''.$langs->trans("BackToModuleList").''; print_fiche_titre($langs->trans("CronSetup"),$linkback,'setup'); -print $langs->trans("CronDesc")."
\n"; +// Configuration header +$head = cronadmin_prepare_head(); +dol_fiche_head($head,'setup',$langs->trans("Module2300Name"),0,'cron'); + print "
\n"; print '
'; @@ -76,13 +81,12 @@ print ''; print ''; print ""; print ""; -//print ""; print ""; print ""; print ''; print ''; -print ''; @@ -102,12 +106,35 @@ print '

'; // Cron launch print ''.$langs->trans("URLToLaunchCronJobs").':
'; -$url=DOL_MAIN_URL_ROOT.'/public/cron/cron_run_jobs.php'.(empty($conf->global->CRON_KEY)?'':'?securitykey='.$conf->global->CRON_KEY); +$url=dol_buildpath('/public/cron/cron_run_jobs.php',1).(empty($conf->global->MAIN_CRON_KEY)?'':'?securitykey='.$conf->global->MAIN_CRON_KEY.'&').'userlogin='.$user->login; print img_picto('','object_globe.png').' '.$url."
\n"; print ' '.$langs->trans("OrToLaunchASpecificJob").'
'; -$url=DOL_MAIN_URL_ROOT.'/public/cron/cron_run_jobs.php?'.(empty($conf->global->CRON_KEY)?'':'securitykey='.$conf->global->CRON_KEY.'&').'id=cronjobid'; -print img_picto('','object_globe.png').' '.$url."
\n"; +$url=dol_buildpath('/public/cron/cron_run_jobs.php',1).(empty($conf->global->MAIN_CRON_KEY)?'':'?securitykey='.$conf->global->MAIN_CRON_KEY.'&').'userlogin='.$user->login.'&id=cronjobid'; +print img_picto('','object_globe.png').' '.$url."
\n"; print '
'; +print '
'; + + +$linuxlike=1; +if (preg_match('/^win/i',PHP_OS)) $linuxlike=0; +if (preg_match('/^mac/i',PHP_OS)) $linuxlike=0; + +if ($linuxlike) { + print $langs->trans("CronExplainHowToRunUnix"); +} else { + print $langs->trans("CronExplainHowToRunWin"); +} +print '
'; +print ''.$langs->trans("FileToLaunchCronJobs").':
'; +$file='/scripts/cron/cron_run_jobs.php'.' '.(empty($conf->global->MAIN_CRON_KEY)?'securitykey':''.$conf->global->MAIN_CRON_KEY.'').' '.$user->login.' cronjobid(optionnal)'; +if ($linuxlike) { + print 'user@host:'.DOL_DOCUMENT_ROOT.'$ php ..'.$file."
\n"; +} else { + print DOL_DOCUMENT_ROOT.'> php ..'.$file."
\n"; +} +print '
'; + + print '
'; @@ -116,20 +143,18 @@ if (! empty($conf->use_javascript_ajax)) { print "\n".''; } - llxFooter(); -$db->close(); -?> +$db->close(); \ No newline at end of file diff --git a/htdocs/cron/card.php b/htdocs/cron/card.php new file mode 100644 index 00000000000..bd6ffb0e5c9 --- /dev/null +++ b/htdocs/cron/card.php @@ -0,0 +1,596 @@ + + * +* 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 cron/card.php + * \ingroup cron + * \brief Cron Jobs Card + */ + +require '../main.inc.php'; + +require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php'; + +// librairie jobs +require_once DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php"; +require_once DOL_DOCUMENT_ROOT."/core/class/html.formcron.class.php"; +require_once DOL_DOCUMENT_ROOT.'/core/lib/cron.lib.php'; + + +$langs->load("admin"); +$langs->load("cron"); + +if (!$user->rights->cron->create) accessforbidden(); + +$id=GETPOST('id','int'); +$action=GETPOST('action','alpha'); +$confirm=GETPOST('confirm','alpha'); +$cancel=GETPOST('cancel'); + +$object = new Cronjob($db); +if (!empty($id)) { + $result=$object->fetch($id); + if ($result < 0) { + setEventMessage($object->error,'errors'); + } +} + +if(!empty($cancel)) { + if (!empty($id)) { + $action=''; + }else { + Header ( "Location: ".dol_buildpath('/cron/cron/list.php',1).'?status=1'); + } + +} + +// Delete jobs +if ($action == 'confirm_delete' && $confirm == "yes" && $user->rights->cron->delete){ + + + $result = $object->delete($user); + + if ($result < 0) { + setEventMessage($object->error,'errors'); + $action='edit'; + }else { + Header ( "Location: ".dol_buildpath('/cron/cron/list.php',1).'?status=1'); + } +} + +// Execute jobs +if ($action == 'confirm_execute' && $confirm == "yes" && $user->rights->cron->execute){ + + $result=$object->run_jobs($user->login); + + if ($result < 0) { + setEventMessage($object->error,'errors'); + $action=''; + }else { + $action=''; + } + +} + + +if ($action=='add') { + + $object->jobtype=GETPOST('jobtype','alpha'); + $object->label=GETPOST('label','alpha'); + $object->command=GETPOST('command','alpha'); + $object->priority=GETPOST('priority','int'); + $object->classesname=GETPOST('classesname','alpha'); + $object->objectname=GETPOST('objectname','alpha'); + $object->methodename=GETPOST('methodename','alpha'); + $object->params=GETPOST('params'); + $object->md5params=GETPOST('md5params'); + $object->module_name=GETPOST('module_name','alpha'); + $object->note=GETPOST('note'); + $object->datestart=dol_mktime(GETPOST('datestarthour','int'), GETPOST('datestartmin','int'), 0, GETPOST('datestartmonth','int'), GETPOST('datestartday','int'), GETPOST('datestartyear','int')); + $object->dateend=dol_mktime(GETPOST('dateendhour','int'), GETPOST('dateendmin','int'), 0, GETPOST('dateendmonth','int'), GETPOST('dateendday','int'), GETPOST('dateendyear','int')); + $object->unitfrequency=GETPOST('unitfrequency','int'); + $object->frequency=$object->unitfrequency * GETPOST('nbfrequency','int'); + + //Ajout de la tache cron + $result = $object->create($user); + + // test du Resultat de la requete + if ($result < 0) { + setEventMessage($object->error,'errors'); + $action='create'; + } + else { + setEventMessage($langs->trans('CronSaveSucess'),'mesgs'); + $action=''; + } +} + +// Save parameters +if ($action=='update') { + $object->id=$id; + $object->jobtype=GETPOST('jobtype','alpha'); + $object->label=GETPOST('label','alpha'); + $object->command=GETPOST('command','alpha'); + $object->classesname=GETPOST('classesname','alpha'); + $object->priority=GETPOST('priority','int'); + $object->objectname=GETPOST('objectname','alpha'); + $object->methodename=GETPOST('methodename','alpha'); + $object->params=GETPOST('params'); + $object->md5params=GETPOST('md5params'); + $object->module_name=GETPOST('module_name','alpha'); + $object->note=GETPOST('note'); + $object->datestart=dol_mktime(GETPOST('datestarthour','int'), GETPOST('datestartmin','int'), 0, GETPOST('datestartmonth','int'), GETPOST('datestartday','int'), GETPOST('datestartyear','int')); + $object->dateend=dol_mktime(GETPOST('dateendhour','int'), GETPOST('dateendmin','int'), 0, GETPOST('dateendmonth','int'), GETPOST('dateendday','int'), GETPOST('dateendyear','int')); + $object->unitfrequency=GETPOST('unitfrequency','int'); + $object->frequency=$object->unitfrequency * GETPOST('nbfrequency','int'); + + //Ajout de la tache cron + $result = $object->update($user); + + // test du Resultat de la requete + if ($result < 0) { + setEventMessage($object->error,'errors'); + $action='edit'; + } + else { + setEventMessage($langs->trans('CronSaveSucess'),'mesgs'); + $action=''; + } +} + +if ($action=='activate') { + $object->status=1; + + //Ajout de la tache cron + $result = $object->update($user); + + // test du Resultat de la requete + if ($result < 0) { + setEventMessage($object->error,'errors'); + $action='edit'; + } + else { + setEventMessage($langs->trans('CronSaveSucess'),'mesgs'); + $action=''; + } +} + +if ($action=='inactive') { + $object->status=0; + //Ajout de la tache cron + $result = $object->update($user); + + // test du Resultat de la requete + if ($result < 0) { + setEventMessage($object->error,'errors'); + $action='edit'; + } + else { + setEventMessage($langs->trans('CronSaveSucess'),'mesgs'); + $action=''; + } +} + + +/* + * View +*/ + +llxHeader('',$langs->trans("CronAdd")); + +if ($action=='edit' || empty($action) || $action=='delete' || $action=='execute') { + $head=cron_prepare_head($object); + dol_fiche_head($head, 'card', $langs->trans("CronTask"), 0, 'bill'); +} elseif ($action=='create') { + print_fiche_titre($langs->trans("CronTask"),'','setup'); +} + +if ($conf->use_javascript_ajax) +{ + print "\n".''."\n"; +} + +$form = new Form($db); +$formCron = new FormCron($db); + +if ($action == 'delete') +{ + $ret=$form->form_confirm($_SERVER['PHP_SELF']."?id=".$object->id,$langs->trans("CronDelete"),$langs->trans("CronConfirmDelete"),"confirm_delete",'','',1); + if ($ret == 'html') print '
'; + $action=''; +} + +if ($action == 'execute'){ + $ret=$form->form_confirm($_SERVER['PHP_SELF']."?id=".$object->id,$langs->trans("CronExecute"),$langs->trans("CronConfirmExecute"),"confirm_execute",'','',1); + if ($ret == 'html') print '
'; + $action=''; +} + + + +/* + * Create Template +*/ + +if (empty($object->status)) { + dol_htmloutput_mesg($langs->trans("CronTaskInactive"),'','warning',1); +} + +if (($action=="create") || ($action=="edit")) { + + print ''; + print ''."\n"; + if (!empty($object->id)) { + print ''."\n"; + print ''."\n"; + } else { + print ''."\n"; + } + + print '
".$langs->trans("Parameter")."".$langs->trans("Value")."".$langs->trans("Examples")." 
'.$langs->trans("KeyForCronAccess").''; +print ''; if (! empty($conf->use_javascript_ajax)) print ' '.img_picto($langs->trans('Generate'), 'refresh', 'id="generate_token" class="linkobject"'); print '
'; + + print '"; + print ""; + print ""; + print "\n"; + + print ""; + print ""; + print "\n"; + + print ""; + print ""; + print "\n"; + + print ""; + print ""; + print "\n"; + + print ""; + $priority=0; + if (!empty($object->priority)) { + $priority=$object->priority; + } + print ""; + print ""; + print "\n"; + + print ""; + print ""; + print ""; + print "\n"; + + + print '"; + print ""; + print "\n"; + + print '"; + print ""; + print "\n"; + + print '"; + print ""; + print "\n"; + + print '"; + print ""; + print "\n"; + + print '"; + print ""; + print "\n"; + + print '"; + print ""; + print "\n"; + + print '"; + print ""; + print "\n"; + + + print '"; + print ""; + print "\n"; + + print '
'; + print $langs->trans('CronLabel')."label."\" /> "; + print ""; + print "
"; + print $langs->trans('CronType').""; + print $formCron->select_typejob('jobtype',$object->jobtype); + print ""; + print "
"; + print $langs->trans('CronHourStart').""; + if(!empty($object->datestart)){ + $form->select_date($object->datestart,'datestart',1,1,'',"cronform"); + } + else{ + $form->select_date(dol_now(),'datestart',1,1,'',"cronform"); + } + print ""; + print "
"; + print $langs->trans('CronDtEnd').""; + if(!empty($object->dateend)){ + $form->select_date($object->dateend,'dateend',1,1,'',"cronform"); + } + else{ + $form->select_date('','dateend',1,1,1,"cronform"); + } + print ""; + print "
"; + print $langs->trans('CronPriority')." "; + print ""; + print "
"; + print $langs->trans('CronEvery')."unitfrequency=="60"){ + $input .= ' checked="checked" />'; + } + else{ + $input .= ' />'; + } + $input .= ""; + print $input; + + $input = "unitfrequency=="3600"){ + $input .= ' checked="checked" />'; + } + else{ + $input .= ' />'; + } + $input .= ""; + print $input; + + $input = "unitfrequency=="86400"){ + $input .= ' checked="checked" />'; + } + else{ + $input .= ' />'; + } + $input .= ""; + print $input; + + $input = "unitfrequency=="604800"){ + $input .= ' checked="checked" />'; + } + else{ + $input .= ' />'; + } + $input .= ""; + print $input; + print ""; + print "
'; + print $langs->trans('CronModule').""; + print "module_name."\" /> "; + print ""; + print $form->textwithpicto('',$langs->trans("CronModuleHelp"),1,'help'); + print "
'; + print $langs->trans('CronClassFile').""; + print "classesname."\" /> "; + print ""; + print $form->textwithpicto('',$langs->trans("CronClassFileHelp"),1,'help'); + print "
'; + print $langs->trans('CronObject').""; + print "objectname."\" /> "; + print ""; + print $form->textwithpicto('',$langs->trans("CronObjectHelp"),1,'help'); + print "
'; + print $langs->trans('CronMethod').""; + print "methodename."\" /> "; + print ""; + print $form->textwithpicto('',$langs->trans("CronMethodHelp"),1,'help'); + print "
'; + print $langs->trans('CronArgs').""; + print "params."\" /> "; + print ""; + print $form->textwithpicto('',$langs->trans("CronArgsHelp"),1,'help'); + print "
'; + print $langs->trans('CronCommand').""; + print "command."\" /> "; + print ""; + print $form->textwithpicto('',$langs->trans("CronCommandHelp"),1,'help'); + print "
'; + print $langs->trans('CronNote').""; + $doleditor = new DolEditor('note', $object->note, '', 160, 'dolibarr_notes', 'In', true, false, 0, 4, 90); + $doleditor->Create(); + print ""; + print "
'; + print "trans("Save")."\">"; + print "trans("Cancel")."\">"; + print ""; + print "
'; + + print "
\n"; + +}else { + + /* + * view Template + */ + + // box add_jobs_box + print ''; + + print '"; + print "\n"; + + print '"; + print ""; + + print ""; + + print ""; + + print ""; + + print ""; + print ""; + + print ""; + print ""; + + print ""; + print ""; + + + print '"; + + print '"; + + print '"; + + print '"; + + print '"; + + print '"; + + print '"; + + print '"; + + print '"; + + print '"; + + print '"; + + print '"; + + print '
'; + print $langs->trans('CronId')."".$form->showrefnav($object, 'id', $linkback, 1, 'rowid', 'id'); + print "
'; + print $langs->trans('CronLabel')."".$object->label; + print "
"; + print $langs->trans('CronType').""; + print $formCron->select_typejob('jobtype',$object->jobtype,1); + print "
"; + print $langs->trans('CronHourStart').""; + if(!empty($object->datestart)) {print dol_print_date($object->datestart,'dayhourtext');} else {print $langs->trans('CronNone');} + print "
"; + print $langs->trans('CronDtEnd').""; + if(!empty($object->dateend)) {print dol_print_date($object->dateend,'dayhourtext');} else {print $langs->trans('CronNone');} + print "
"; + print $langs->trans('CronPriority')."".$object->priority; + print "
"; + print $langs->trans('CronNbRun')."".$object->nbrun; + print "
"; + print $langs->trans('CronEvery').""; + if($object->unitfrequency == "60") print $langs->trans('CronEach')." ".($object->frequency/$object->unitfrequency)." ".$langs->trans('Minutes'); + if($object->unitfrequency == "3600") print $langs->trans('CronEach')." ".($object->frequency/$object->unitfrequency)." ".$langs->trans('Hours'); + if($object->unitfrequency == "86400") print $langs->trans('CronEach')." ".($object->frequency/$object->unitfrequency)." ".$langs->trans('Days'); + if($object->unitfrequency == "604800") print $langs->trans('CronEach')." ".($object->frequency/$object->unitfrequency)." ".$langs->trans('Weeks'); + print "
'; + print $langs->trans('CronModule').""; + print $object->module_name; + print "
'; + print $langs->trans('CronClassFile').""; + print $object->classesname; + print "
'; + print $langs->trans('CronObject').""; + print $object->objectname; + print "
'; + print $langs->trans('CronMethod').""; + print $object->methodename; + print "
'; + print $langs->trans('CronArgs').""; + print $object->params; + print "
'; + print $langs->trans('CronCommand').""; + print $object->command; + print "
'; + print $langs->trans('CronNote').""; + print $object->note; + print "
'; + print $langs->trans('CronDtLastLaunch').""; + if(!empty($object->datelastrun)) {print dol_print_date($object->datelastrun,'dayhourtext');} else {print $langs->trans('CronNone');} + print "
'; + print $langs->trans('CronDtNextLaunch').""; + if(!empty($object->datenextrun)) {print dol_print_date($object->datenextrun,'dayhourtext');} else {print $langs->trans('CronNone');} + print "
'; + print $langs->trans('CronDtLastResult').""; + if(!empty($object->datelastresult)) {print dol_print_date($object->datelastresult,'dayhourtext');} else {print $langs->trans('CronNone');} + print "
'; + print $langs->trans('CronLastResult').""; + print $object->lastresult; + print "
'; + print $langs->trans('CronLastOutput').""; + print nl2br($object->lastoutput); + print "
'; + + print "\n\n
\n"; + if (! $user->rights->cron->create) { + print ''.$langs->trans("Edit").''; + } else { + print ''.$langs->trans("Edit").''; + } + if (! $user->rights->cron->delete) { + print ''.$langs->trans("Delete").''; + } else { + print ''.$langs->trans("Delete").''; + } + if (! $user->rights->cron->create) { + print ''.$langs->trans("CronStatusActiveBtn").'/'.$langs->trans("CronStatusInactiveBtn").''; + } else { + if (empty($object->status)) { + print ''.$langs->trans("CronStatusActiveBtn").''; + } else { + print ''.$langs->trans("CronStatusInactiveBtn").''; + } + } + if ((! $user->rights->cron->execute) || (empty($object->status))) { + print ''.$langs->trans("CronExecute").''; + } else { + print ''.$langs->trans("CronExecute").''; + } + print '

'; +} + +$db->close(); +llxFooter(); \ No newline at end of file diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index b2b5fcddd1a..b24055c6f3d 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -1,6 +1,6 @@ - * Copyright (C) ---Put here your own copyright and developer email--- + * Copyright (C) 2013 Florian Henry * * 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 @@ -17,42 +17,58 @@ */ /** - * \file dev/skeletons/cronjob.class.php + * \file cron/class/cronjob.class.php * \ingroup cron - * \brief CRUD class file (Create/Read/Update/Delete) for cronjob table - * Initialy built by build_class_from_table on 2013-03-17 18:50 */ // Put here all includes required by your class file require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php"); -//require_once(DOL_DOCUMENT_ROOT."/societe/class/societe.class.php"); -//require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php"); /** - * Put here description of your class + * Crob Job class */ class Cronjob extends CommonObject { var $db; //!< To store db handler var $error; //!< To return error code (or message) var $errors=array(); //!< To return several error codes (or messages) - var $element='cronjob'; //!< Id that identify managed objects + var $element='cronjob'; //!< Id that identify managed objects var $table_element='cronjob'; //!< Name of table without prefix where object is stored var $id; - + + var $ref; //Use for prevnext_ref + var $jobtype; var $tms=''; var $datec=''; + var $label; var $command; + var $classesname; + var $objectname; + var $methodename; var $params; + var $md5params; + var $module_name; + var $priority; var $datelastrun=''; - var $lastresult=''; + var $datenextrun=''; + var $dateend=''; + var $datestart=''; + var $datelastresult=''; + var $lastresult; var $lastoutput; - var $fk_user; + var $unitfrequency; + var $frequency; + var $status; + var $fk_user_author; + var $fk_user_mod; var $note; + var $nbrun; + + var $lines; - + /** @@ -80,51 +96,128 @@ class Cronjob extends CommonObject $error=0; // Clean parameters - + + if (isset($this->label)) $this->label=trim($this->label); + if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype); if (isset($this->command)) $this->command=trim($this->command); + if (isset($this->classesname)) $this->classesname=trim($this->classesname); + if (isset($this->objectname)) $this->objectname=trim($this->objectname); + if (isset($this->methodename)) $this->methodename=trim($this->methodename); if (isset($this->params)) $this->params=trim($this->params); + if (isset($this->md5params)) $this->md5params=trim($this->md5params); + if (isset($this->module_name)) $this->module_name=trim($this->module_name); + if (isset($this->priority)) $this->priority=trim($this->priority); if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput); - if (isset($this->fk_user)) $this->fk_user=trim($this->fk_user); + if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult); + if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency); + if (isset($this->frequency)) $this->frequency=trim($this->frequency); + if (isset($this->status)) $this->status=trim($this->status); if (isset($this->note)) $this->note=trim($this->note); - - + if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun); // Check parameters - // Put here code to add control on parameters values + // Put here code to add a control on parameters values + if (dol_strlen($this->datestart)==0) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart')); + $error++; + } + if (empty($this->label)) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLabel')); + $error++; + } + if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) { + $this->errors[]=$langs->trans('CronErrEndDateStartDt'); + $error++; + } + if (empty($this->unitfrequency)) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronFrequency')); + $error++; + } + if (($this->jobtype=='command') && (empty($this->command))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronCommand')); + $error++; + } + if (($this->jobtype=='method') && (empty($this->classesname))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass')); + $error++; + } + if (($this->jobtype=='method') && (empty($this->methodename))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod')); + $error++; + } + if (($this->jobtype=='method') && (empty($this->objectname))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronObject')); + $error++; + } // Insert request $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob("; - + $sql.= "datec,"; + $sql.= "jobtype,"; + $sql.= "label,"; $sql.= "command,"; + $sql.= "classesname,"; + $sql.= "objectname,"; + $sql.= "methodename,"; $sql.= "params,"; + $sql.= "md5params,"; + $sql.= "module_name,"; + $sql.= "priority,"; $sql.= "datelastrun,"; + $sql.= "datenextrun,"; + $sql.= "dateend,"; + $sql.= "datestart,"; $sql.= "lastresult,"; + $sql.= "datelastresult,"; $sql.= "lastoutput,"; - $sql.= "fk_user,"; - $sql.= "note"; - + $sql.= "unitfrequency,"; + $sql.= "frequency,"; + $sql.= "status,"; + $sql.= "fk_user_author,"; + $sql.= "fk_user_mod,"; + $sql.= "note,"; + $sql.= "nbrun"; + $sql.= ") VALUES ("; - - $sql.= " ".(! isset($this->datec) || dol_strlen($this->datec)==0?'NULL':$this->db->idate($this->datec)).","; + + $sql.= " ".$this->db->idate(dol_now()).","; + $sql.= " ".(! isset($this->jobtype)?'NULL':"'".$this->db->escape($this->jobtype)."'").","; + $sql.= " ".(! isset($this->label)?'NULL':"'".$this->db->escape($this->label)."'").","; $sql.= " ".(! isset($this->command)?'NULL':"'".$this->db->escape($this->command)."'").","; + $sql.= " ".(! isset($this->classesname)?'NULL':"'".$this->db->escape($this->classesname)."'").","; + $sql.= " ".(! isset($this->objectname)?'NULL':"'".$this->db->escape($this->objectname)."'").","; + $sql.= " ".(! isset($this->methodename)?'NULL':"'".$this->db->escape($this->methodename)."'").","; $sql.= " ".(! isset($this->params)?'NULL':"'".$this->db->escape($this->params)."'").","; + $sql.= " ".(! isset($this->md5params)?'NULL':"'".$this->db->escape($this->md5params)."'").","; + $sql.= " ".(! isset($this->module_name)?'NULL':"'".$this->db->escape($this->module_name)."'").","; + $sql.= " ".(! isset($this->priority)?'NULL':"'".$this->priority."'").","; $sql.= " ".(! isset($this->datelastrun) || dol_strlen($this->datelastrun)==0?'NULL':$this->db->idate($this->datelastrun)).","; - $sql.= " ".(! isset($this->lastresult) || dol_strlen($this->lastresult)==0?'NULL':$this->db->idate($this->lastresult)).","; + $sql.= " ".(! isset($this->datenextrun) || dol_strlen($this->datenextrun)==0?'NULL':$this->db->idate($this->datenextrun)).","; + $sql.= " ".(! isset($this->dateend) || dol_strlen($this->dateend)==0?'NULL':$this->db->idate($this->dateend)).","; + $sql.= " ".(! isset($this->datestart) || dol_strlen($this->datestart)==0?'NULL':$this->db->idate($this->datestart)).","; + $sql.= " ".(! isset($this->lastresult)?'NULL':"'".$this->db->escape($this->lastresult)."'").","; + $sql.= " ".(! isset($this->datelastresult) || dol_strlen($this->datelastresult)==0?'NULL':$this->db->idate($this->datelastresult)).","; $sql.= " ".(! isset($this->lastoutput)?'NULL':"'".$this->db->escape($this->lastoutput)."'").","; - $sql.= " ".(! isset($this->fk_user)?'NULL':"'".$this->fk_user."'").","; - $sql.= " ".(! isset($this->note)?'NULL':"'".$this->db->escape($this->note)."'").""; - + $sql.= " ".(! isset($this->unitfrequency)?'NULL':"'".$this->unitfrequency."'").","; + $sql.= " ".(! isset($this->frequency)?'NULL':"'".$this->frequency."'").","; + $sql.= " ".(! isset($this->status)?'0':"'".$this->status."'").","; + $sql.= " ".$user->id.","; + $sql.= " ".$user->id.","; + $sql.= " ".(! isset($this->note)?'NULL':"'".$this->db->escape($this->note)."'").","; + $sql.= " ".(! isset($this->nbrun)?'0':"'".$this->db->escape($this->nbrun)."'").""; + $sql.= ")"; + $this->db->begin(); dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG); $resql=$this->db->query($sql); if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } - + if (! $error) { $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."cronjob"); @@ -173,18 +266,35 @@ class Cronjob extends CommonObject global $langs; $sql = "SELECT"; $sql.= " t.rowid,"; - + $sql.= " t.tms,"; $sql.= " t.datec,"; + $sql.= " t.jobtype,"; + $sql.= " t.label,"; $sql.= " t.command,"; + $sql.= " t.classesname,"; + $sql.= " t.objectname,"; + $sql.= " t.methodename,"; $sql.= " t.params,"; + $sql.= " t.md5params,"; + $sql.= " t.module_name,"; + $sql.= " t.priority,"; $sql.= " t.datelastrun,"; + $sql.= " t.datenextrun,"; + $sql.= " t.dateend,"; + $sql.= " t.datestart,"; $sql.= " t.lastresult,"; + $sql.= " t.datelastresult,"; $sql.= " t.lastoutput,"; - $sql.= " t.fk_user,"; - $sql.= " t.note"; - + $sql.= " t.unitfrequency,"; + $sql.= " t.frequency,"; + $sql.= " t.status,"; + $sql.= " t.fk_user_author,"; + $sql.= " t.fk_user_mod,"; + $sql.= " t.note,"; + $sql.= " t.nbrun"; + $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t"; $sql.= " WHERE t.rowid = ".$id; @@ -197,18 +307,36 @@ class Cronjob extends CommonObject $obj = $this->db->fetch_object($resql); $this->id = $obj->rowid; - + $this->ref = $obj->rowid; + $this->tms = $this->db->jdate($obj->tms); $this->datec = $this->db->jdate($obj->datec); + $this->label = $obj->label; + $this->jobtype = $obj->jobtype; $this->command = $obj->command; + $this->classesname = $obj->classesname; + $this->objectname = $obj->objectname; + $this->methodename = $obj->methodename; $this->params = $obj->params; + $this->md5params = $obj->md5params; + $this->module_name = $obj->module_name; + $this->priority = $obj->priority; $this->datelastrun = $this->db->jdate($obj->datelastrun); - $this->lastresult = $this->db->jdate($obj->lastresult); + $this->datenextrun = $this->db->jdate($obj->datenextrun); + $this->dateend = $this->db->jdate($obj->dateend); + $this->datestart = $this->db->jdate($obj->datestart); + $this->lastresult = $obj->lastresult; $this->lastoutput = $obj->lastoutput; - $this->fk_user = $obj->fk_user; + $this->datelastresult = $this->db->jdate($obj->datelastresult); + $this->unitfrequency = $obj->unitfrequency; + $this->frequency = $obj->frequency; + $this->status = $obj->status; + $this->fk_user_author = $obj->fk_user_author; + $this->fk_user_mod = $obj->fk_user_mod; $this->note = $obj->note; + $this->nbrun = $obj->nbrun; - + } $this->db->free($resql); @@ -221,6 +349,141 @@ class Cronjob extends CommonObject return -1; } } + + /** + * Load object in memory from the database + * + * @param string $sortorder sort order + * @param string $sortfield sort field + * @param int $limit limit page + * @param int $offset page + * @param int $status display active or not + * @param array $filter filter output + * @return int <0 if KO, >0 if OK + */ + function fetch_all($sortorder='DESC', $sortfield='t.rowid', $limit=0, $offset=0, $status=1, $filter='') + { + global $langs; + $sql = "SELECT"; + $sql.= " t.rowid,"; + $sql.= " t.tms,"; + $sql.= " t.datec,"; + $sql.= " t.jobtype,"; + $sql.= " t.label,"; + $sql.= " t.command,"; + $sql.= " t.classesname,"; + $sql.= " t.objectname,"; + $sql.= " t.methodename,"; + $sql.= " t.params,"; + $sql.= " t.md5params,"; + $sql.= " t.module_name,"; + $sql.= " t.priority,"; + $sql.= " t.datelastrun,"; + $sql.= " t.datenextrun,"; + $sql.= " t.dateend,"; + $sql.= " t.datestart,"; + $sql.= " t.lastresult,"; + $sql.= " t.datelastresult,"; + $sql.= " t.lastoutput,"; + $sql.= " t.unitfrequency,"; + $sql.= " t.frequency,"; + $sql.= " t.status,"; + $sql.= " t.fk_user_author,"; + $sql.= " t.fk_user_mod,"; + $sql.= " t.note,"; + $sql.= " t.nbrun"; + + + $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t"; + $status = (empty($status))?'0':'1'; + $sql.= " WHERE t.status=".$status; + //Manage filter + if (is_array($filter) && count($filter)>0) { + foreach($filter as $key => $value) { + $sql.= ' AND '.$key.' LIKE \'%'.$value.'%\''; + } + } + + + $sql.= " ORDER BY $sortfield $sortorder "; + if (!empty($limit) && !empty($offset)) { + $sql.= $this->db->plimit( $limit + 1 ,$offset); + } + + $sqlwhere = array(); + + if (!empty($module_name)) { + $sqlwhere[]='(t.module_name='.$module_name.')'; + } + if (count($sqlwhere)>0) { + $sql.= " WHERE ".implode(' AND ',$sqlwhere); + } + + dol_syslog(get_class($this)."::fetch_all sql=".$sql, LOG_DEBUG); + $resql=$this->db->query($sql); + if ($resql) + { + $num=$this->db->num_rows($resql); + $i=0; + + if ($num) + { + $this->lines=array(); + + while ($i < $num) + { + + $line = new Cronjobline(); + + $obj = $this->db->fetch_object($resql); + + $line->id = $obj->rowid; + $line->ref = $obj->rowid; + + $line->tms = $this->db->jdate($obj->tms); + $line->datec = $this->db->jdate($obj->datec); + $line->label = $obj->label; + $line->jobtype = $obj->jobtype; + $line->command = $obj->command; + $line->classesname = $obj->classesname; + $line->objectname = $obj->objectname; + $line->methodename = $obj->methodename; + $line->params = $obj->params; + $line->md5params = $obj->md5params; + $line->module_name = $obj->module_name; + $line->priority = $obj->priority; + $line->datelastrun = $this->db->jdate($obj->datelastrun); + $line->datenextrun = $this->db->jdate($obj->datenextrun); + $line->dateend = $this->db->jdate($obj->dateend); + $line->datestart = $this->db->jdate($obj->datestart); + $line->lastresult = $obj->lastresult; + $line->datelastresult = $this->db->jdate($obj->datelastresult); + $line->lastoutput = $obj->lastoutput; + $line->unitfrequency = $obj->unitfrequency; + $line->frequency = $obj->frequency; + $line->status = $obj->status; + $line->fk_user_author = $obj->fk_user_author; + $line->fk_user_mod = $obj->fk_user_mod; + $line->note = $obj->note; + $line->nbrun = $obj->nbrun; + + $this->lines[]=$line; + + $i++; + + } + } + $this->db->free($resql); + + return 1; + } + else + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR); + return -1; + } + } /** @@ -233,35 +496,98 @@ class Cronjob extends CommonObject function update($user=0, $notrigger=0) { global $conf, $langs; + + $langs->load('cron'); + $error=0; // Clean parameters - + + if (isset($this->label)) $this->label=trim($this->label); + if (isset($this->jobtype)) $this->jobtype=trim($this->jobtype); if (isset($this->command)) $this->command=trim($this->command); + if (isset($this->classesname)) $this->classesname=trim($this->classesname); + if (isset($this->objectname)) $this->objectname=trim($this->objectname); + if (isset($this->methodename)) $this->methodename=trim($this->methodename); if (isset($this->params)) $this->params=trim($this->params); + if (isset($this->md5params)) $this->md5params=trim($this->md5params); + if (isset($this->module_name)) $this->module_name=trim($this->module_name); + if (isset($this->priority)) $this->priority=trim($this->priority); if (isset($this->lastoutput)) $this->lastoutput=trim($this->lastoutput); - if (isset($this->fk_user)) $this->fk_user=trim($this->fk_user); + if (isset($this->lastresult)) $this->lastresult=trim($this->lastresult); + if (isset($this->unitfrequency)) $this->unitfrequency=trim($this->unitfrequency); + if (isset($this->frequency)) $this->frequency=trim($this->frequency); + if (isset($this->status)) $this->status=trim($this->status); if (isset($this->note)) $this->note=trim($this->note); - - + if (isset($this->nbrun)) $this->nbrun=trim($this->nbrun); // Check parameters // Put here code to add a control on parameters values + if (empty($this->status)) { + $this->dateend=dol_now(); + } + if (dol_strlen($this->datestart)==0) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronDtStart')); + $error++; + } + if ((dol_strlen($this->datestart)!=0) && (dol_strlen($this->dateend)!=0) && ($this->dateend<$this->datestart)) { + $this->errors[]=$langs->trans('CronErrEndDateStartDt'); + $error++; + } + if (empty($this->label)) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLabel')); + $error++; + } + if (empty($this->unitfrequency)) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronFrequency')); + $error++; + } + if (($this->jobtype=='command') && (empty($this->command))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronCommand')); + $error++; + } + if (($this->jobtype=='method') && (empty($this->classesname))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass')); + $error++; + } + if (($this->jobtype=='method') && (empty($this->methodename))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod')); + $error++; + } + if (($this->jobtype=='method') && (empty($this->objectname))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronObject')); + $error++; + } + // Update request $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET"; - - $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').","; - $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').","; + + $sql.= " label=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").","; + $sql.= " jobtype=".(isset($this->jobtype)?"'".$this->db->escape($this->jobtype)."'":"null").","; $sql.= " command=".(isset($this->command)?"'".$this->db->escape($this->command)."'":"null").","; + $sql.= " classesname=".(isset($this->classesname)?"'".$this->db->escape($this->classesname)."'":"null").","; + $sql.= " objectname=".(isset($this->objectname)?"'".$this->db->escape($this->objectname)."'":"null").","; + $sql.= " methodename=".(isset($this->methodename)?"'".$this->db->escape($this->methodename)."'":"null").","; $sql.= " params=".(isset($this->params)?"'".$this->db->escape($this->params)."'":"null").","; + $sql.= " md5params=".(isset($this->md5params)?"'".$this->db->escape($this->md5params)."'":"null").","; + $sql.= " module_name=".(isset($this->module_name)?"'".$this->db->escape($this->module_name)."'":"null").","; + $sql.= " priority=".(isset($this->priority)?$this->priority:"null").","; $sql.= " datelastrun=".(dol_strlen($this->datelastrun)!=0 ? "'".$this->db->idate($this->datelastrun)."'" : 'null').","; - $sql.= " lastresult=".(dol_strlen($this->lastresult)!=0 ? "'".$this->db->idate($this->lastresult)."'" : 'null').","; + $sql.= " datenextrun=".(dol_strlen($this->datenextrun)!=0 ? "'".$this->db->idate($this->datenextrun)."'" : 'null').","; + $sql.= " dateend=".(dol_strlen($this->dateend)!=0 ? "'".$this->db->idate($this->dateend)."'" : 'null').","; + $sql.= " datestart=".(dol_strlen($this->datestart)!=0 ? "'".$this->db->idate($this->datestart)."'" : 'null').","; + $sql.= " datelastresult=".(dol_strlen($this->datelastresult)!=0 ? "'".$this->db->idate($this->datelastresult)."'" : 'null').","; + $sql.= " lastresult=".(isset($this->lastresult)?"'".$this->db->escape($this->lastresult)."'":"null").","; $sql.= " lastoutput=".(isset($this->lastoutput)?"'".$this->db->escape($this->lastoutput)."'":"null").","; - $sql.= " fk_user=".(isset($this->fk_user)?$this->fk_user:"null").","; - $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").""; - + $sql.= " unitfrequency=".(isset($this->unitfrequency)?$this->unitfrequency:"null").","; + $sql.= " frequency=".(isset($this->frequency)?$this->frequency:"null").","; + $sql.= " status=".(isset($this->status)?$this->status:"null").","; + $sql.= " fk_user_mod=".$user->id.","; + $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").","; + $sql.= " nbrun=".(isset($this->nbrun)?$this->nbrun:"null"); + $sql.= " WHERE rowid=".$this->id; $this->db->begin(); @@ -269,7 +595,7 @@ class Cronjob extends CommonObject dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG); $resql = $this->db->query($sql); if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); } - + if (! $error) { if (! $notrigger) @@ -377,7 +703,7 @@ class Cronjob extends CommonObject $error=0; - $object=new Cronjobs($this->db); + $object=new Cronjob($this->db); $this->db->begin(); @@ -428,19 +754,277 @@ class Cronjob extends CommonObject function initAsSpecimen() { $this->id=0; - + $this->ref=0; + $this->tms=''; $this->datec=''; + $this->label=''; + $this->jobtype=''; $this->command=''; + $this->classesname=''; + $this->objectname=''; + $this->methodename=''; $this->params=''; + $this->md5params=''; + $this->module_name=''; + $this->priority=''; $this->datelastrun=''; - $this->lastresult=''; + $this->datenextrun=''; + $this->dateend=''; + $this->datestart=''; + $this->datelastresult=''; $this->lastoutput=''; - $this->fk_user=''; - $this->note=''; - - + $this->lastresult=''; + $this->unitfrequency=''; + $this->frequency=''; + $this->status=''; + $this->fk_user_author=''; + $this->fk_user_mod=''; + $this->note=''; + $this->nbrun=''; } + + /** + * Load object information + * + * @return void + */ + function info() + { + global $langs; + + $sql = "SELECT"; + $sql.= " f.rowid, f.datec, f.tms, f.fk_user_mod, f.fk_user_author"; + $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as f"; + $sql.= " WHERE f.rowid = ".$this->id; + + dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG); + $resql=$this->db->query($sql); + if ($resql) + { + if ($this->db->num_rows($resql)) + { + $obj = $this->db->fetch_object($resql); + $this->id = $obj->rowid; + $this->date_creation = $this->db->jdate($obj->datec); + $this->date_modification = $this->db->jdate($obj->tms); + $this->user_modification = $obj->fk_user_mod; + $this->user_creation = $obj->fk_user_author; + } + $this->db->free($resql); + + return 1; + } + else + { + $this->error="Error ".$this->db->lasterror(); + dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR); + return -1; + } + } + + /** + * Run a job + * + * @param string $userlogin User login + * @return int <0 if KO, >0 if OK + */ + function run_jobs($userlogin) + { + global $langs, $conf; + + $langs->load('cron'); + + if (empty($userlogin)) { + $this->error="User login is mandatory"; + dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); + return -1; + } + + require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; + $user=new User($this->db); + $result=$user->fetch('',$userlogin); + if ($result<0) { + $this->error="User Error:".$user->error; + dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); + return -1; + }else { + if (empty($user->id)) { + $this->error=" User user login:".$userlogin." do not exists"; + dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); + return -1; + } + } + + dol_syslog(get_class($this)."::run_jobs userlogin:$userlogin", LOG_DEBUG); + + $error=0; + $now=dol_now(); + + $this->db->begin(); + + if ($this->jobtype=='method') { + // load classes + $ret=dol_include_once("/".$this->module_name."/class/".$this->classesname,$this->objectname); + if ($ret===false) { + $this->error=$langs->trans('CronCannotLoadClass',$file,$this->objectname); + dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); + return -1; + } + + // Load langs + $result=$langs->load($this->module_name.'@'.$this->module_name); + if ($result<0) { + dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR); + return -1; + } + + dol_syslog(get_class($this)."::run_jobs ".$this->objectname."->".$this->methodename."(".$this->params.");", LOG_DEBUG); + + // Create Object for the call module + $object = new $this->objectname($this->db); + + $params_arr = array(); + $params_arr=explode(", ",$this->params); + if (!is_array($params_arr)) { + $result = call_user_func(array($object, $this->methodename), $this->params); + }else { + $result = call_user_func_array(array($object, $this->methodename), $params_arr); + } + + if ($result===false) { + dol_syslog(get_class($this)."::run_jobs ".$object->error, LOG_ERR); + return -1; + }else { + $this->lastoutput=var_export($result,true); + $this->lastresult=var_export($result,true); + } + + } elseif ($this->jobtype=='command') { + dol_syslog(get_class($this)."::run_jobs system:".$this->command, LOG_DEBUG); + $output_arr=array(); + + exec($this->command, $output_arr,$retval); + + dol_syslog(get_class($this)."::run_jobs output_arr:".var_export($output_arr,true), LOG_DEBUG); + + $this->lastoutput=''; + if (is_array($output_arr) && count($output_arr)>0) { + foreach($output_arr as $val) { + $this->lastoutput.=$val."\n"; + } + } + $this->lastresult=$retval; + } + + $this->datelastresult=$now; + $this->datelastrun=$now; + $this->nbrun=$this->nbrun+1; + $result = $this->update($user); + if ($result<0) { + dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); + $this->db->rollback(); + return -1; + }else { + $this->db->commit(); + return 1; + } + + + } + + /** + * Reprogram a job + * + * @param string $userlogin User login + * @return int <0 if KO, >0 if OK + * + */ + function reprogram_jobs($userlogin) + { + global $langs, $conf; + + dol_syslog(get_class($this)."::reprogram_jobs userlogin:$userlogin", LOG_DEBUG); + + require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; + $user=new User($this->db); + $result=$user->fetch('',$userlogin); + if ($result<0) { + $this->error="User Error:".$user->error; + dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR); + return -1; + }else { + if (empty($user->id)) { + $this->error=" User user login:".$userlogin." do not exists"; + dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR); + return -1; + } + } + + dol_syslog(get_class($this)."::reprogram_jobs ", LOG_DEBUG); + + if (empty($this->datenextrun)) { + $this->datenextrun=dol_now()+$this->frequency; + } else { + if ($this->datenextrundatenextrun=dol_now()+$this->frequency; + } else { + $this->datenextrun=$this->datenextrun+$this->frequency; + } + } + $result = $this->update($user); + if ($result<0) { + dol_syslog(get_class($this)."::reprogram_jobs ".$this->error, LOG_ERR); + return -1; + } + + return 1; + + } } -?> + + +/** + * Crob Job line class + */ +class Cronjobline{ + + var $id; + var $ref; + + var $tms=''; + var $datec=''; + var $label; + var $jobtype; + var $command; + var $classesname; + var $objectname; + var $methodename; + var $params; + var $md5params; + var $module_name; + var $priority; + var $datelastrun=''; + var $datenextrun=''; + var $dateend=''; + var $datestart=''; + var $lastresult=''; + var $lastoutput; + var $unitfrequency; + var $frequency; + var $status; + var $fk_user_author; + var $fk_user_mod; + var $note; + var $nbrun; + + /** + * Constructor + * + */ + function __construct() + { + return 1; + } +} \ No newline at end of file diff --git a/htdocs/cron/functions_cron.lib.php b/htdocs/cron/functions_cron.lib.php deleted file mode 100644 index 31cc8ebefad..00000000000 --- a/htdocs/cron/functions_cron.lib.php +++ /dev/null @@ -1,27 +0,0 @@ - - * Copyright (C) 2007-2009 Laurent Destailleur - * - * 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 cron/functions_cron.lib.php - * \ingroup core - * \brief Functions for miscellaneous cron tasks - */ - - - -?> diff --git a/htdocs/cron/index.php b/htdocs/cron/index.php deleted file mode 100644 index 3d7ef46ad77..00000000000 --- a/htdocs/cron/index.php +++ /dev/null @@ -1,219 +0,0 @@ - - * - * 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 dev/Cronjobss/Cronjobs_page.php - * \ingroup mymodule othermodule1 othermodule2 - * \brief This file is an example of a php page - * Initialy built by build_class_from_table on 2013-03-17 18:50 - */ - -//if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); -//if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); -//if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); -//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); -//if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); -//if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); -//if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show -//if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php -//if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); -//if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session) - -// Change this following line to use the correct relative path (../, ../../, etc) -$res=0; -if (! $res && file_exists("../main.inc.php")) $res=@include '../main.inc.php'; -if (! $res && file_exists("../../main.inc.php")) $res=@include '../../main.inc.php'; -if (! $res && file_exists("../../../main.inc.php")) $res=@include '../../../main.inc.php'; -if (! $res && file_exists("../../../dolibarr/htdocs/main.inc.php")) $res=@include '../../../dolibarr/htdocs/main.inc.php'; // Used on dev env only -if (! $res && file_exists("../../../../dolibarr/htdocs/main.inc.php")) $res=@include '../../../../dolibarr/htdocs/main.inc.php'; // Used on dev env only -if (! $res && file_exists("../../../../../dolibarr/htdocs/main.inc.php")) $res=@include '../../../../../dolibarr/htdocs/main.inc.php'; // Used on dev env only -if (! $res) die("Include of main fails"); -// Change this following line to use the correct relative path from htdocs -dol_include_once('/module/class/cronjob.class.php'); - -// Load traductions files requiredby by page -$langs->load("companies"); -$langs->load("other"); -$langs->load("cron"); - -// Get parameters -$id = GETPOST('id','int'); -$action = GETPOST('action','alpha'); -$myparam = GETPOST('myparam','alpha'); -$action='list'; - -// Protection if external user -if ($user->societe_id > 0) accessforbidden(); -if (! $user->admin) accessforbidden(); - - -/******************************************************************* -* ACTIONS -* -* Put here all code to do according to value of "action" parameter -********************************************************************/ - -if ($action == 'add') -{ - $object=new Cronjobs($db); - $object->prop1=$_POST["field1"]; - $object->prop2=$_POST["field2"]; - $result=$object->create($user); - if ($result > 0) - { - // Creation OK - } - { - // Creation KO - $mesg=$object->error; - } -} - - - - - -/*************************************************** -* VIEW -* -* Put here all code to build page -****************************************************/ - -llxHeader('','MyPageName',''); - -$form=new Form($db); - - -//print ''; -//print '
'; -print '
'; - - - - - -//print '
'; -print '
'; - - - -// Example 1 : Adding jquery code -print ''; - - -// Example 2 : Adding links to objects -// The class must extends CommonObject class to have this method available -//$somethingshown=$object->showLinkedObjectBlock(); - - -// Example 3 : List of data -if ($action == 'list') -{ - $sql = "SELECT"; - $sql.= " t.rowid,"; - - $sql.= " t.tms,"; - $sql.= " t.datec,"; - $sql.= " t.command,"; - $sql.= " t.params,"; - $sql.= " t.datelastrun,"; - $sql.= " t.lastresult,"; - $sql.= " t.lastoutput,"; - $sql.= " t.fk_user,"; - $sql.= " t.note"; - - - $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t"; - //$sql.= " WHERE field3 = 'xxx'"; - //$sql.= " ORDER BY field1 ASC"; - - - print_fiche_titre($langs->trans("ListOfCronJobs"),'','').'
'; - - - print ''."\n"; - print ''; - print_liste_field_titre($langs->trans('Id'),$_SERVER['PHP_SELF'],'t.rowid','',$param,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('Command'),$_SERVER['PHP_SELF'],'t.command','',$param,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('DateCreation'),$_SERVER['PHP_SELF'],'t.datec','align="center"',$param,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('LastOutput'),$_SERVER['PHP_SELF'],'','',$param,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('DateLastRun'),$_SERVER['PHP_SELF'],'t.datelastrun','align="center"',$param,'',$sortfield,$sortorder); - print_liste_field_titre($langs->trans('LastResult'),$_SERVER['PHP_SELF'],'t.lastresult','align="right"',$param,'',$sortfield,$sortorder); - print ''; - - dol_syslog($script_file." sql=".$sql, LOG_DEBUG); - $resql=$db->query($sql); - if ($resql) - { - $num = $db->num_rows($resql); - $i = 0; - if ($num) - { - while ($i < $num) - { - $obj = $db->fetch_object($resql); - if ($obj) - { - // You can use here results - print ''; - } - $i++; - } - } - } - else - { - $error++; - dol_print_error($db); - } - - print '
'; - print $obj->rowid; - print ''; - print $obj->command; - print ''; - print $db->jdate($obj->datec); - print ''; - print ''; - print ''; - print $db->jdate($obj->datelastrun); - print ''; - print $obj->lastresult; - print '
'."\n"; -} - - -//print '
'; -print '
'; - - -// End of page -llxFooter(); -$db->close(); -?> diff --git a/htdocs/cron/info.php b/htdocs/cron/info.php new file mode 100644 index 00000000000..55c294d54ed --- /dev/null +++ b/htdocs/cron/info.php @@ -0,0 +1,59 @@ + +* +* 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, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ + +/** + * \file /cron/cron/info.php + * \brief Page fiche d'une operation + */ + +require '../main.inc.php'; + +require_once DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php"; +require_once DOL_DOCUMENT_ROOT.'/core/lib/cron.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/functions2.lib.php'; + + +// Security check +if (!$user->rights->cron->read) accessforbidden(); + +$id=GETPOST('id','int'); + +$mesg = ''; + +/* + * View +*/ + +llxHeader('',$langs->trans("CronInfo")); + +$object = new Cronjob($db); +$object->fetch($id); +$object->info($id); + +$head = cron_prepare_head($object); + +dol_fiche_head($head, 'info', $langs->trans("CronTask"), 0, 'bill'); + +print '
'; +dol_print_object_info($object); +print '
'; +print ''; + + +$db->close(); +llxFooter(); \ No newline at end of file diff --git a/htdocs/cron/list.php b/htdocs/cron/list.php new file mode 100644 index 00000000000..5e972f0e55b --- /dev/null +++ b/htdocs/cron/list.php @@ -0,0 +1,295 @@ + + * + * 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 cron/cron/list.php + * \ingroup cron + * \brief Lists Jobs + */ + + +require '../main.inc.php'; +require_once(DOL_DOCUMENT_ROOT."/core/lib/admin.lib.php"); + +// librairie jobs +require_once DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php"; +require_once DOL_DOCUMENT_ROOT.'/core/lib/cron.lib.php'; + +$langs->load("admin"); +$langs->load("cron"); + +if (!$user->rights->cron->read) accessforbidden(); + +/* + * Actions + */ +$action=GETPOST('action','alpha'); +$confirm=GETPOST('confirm','alpha'); +$id=GETPOST('id','int'); + +$sortorder=GETPOST('sortorder','alpha'); +$sortfield=GETPOST('sortfield','alpha'); +$page=GETPOST('page','int'); +$status=GETPOST('status','int'); + +//Search criteria +$search_label=GETPOST("search_label",'alpha'); + +if (empty($sortorder)) $sortorder="DESC"; +if (empty($sortfield)) $sortfield="t.datenextrun"; +if (empty($arch)) $arch = 0; + +if ($page == -1) { + $page = 0 ; +} + +$limit = $conf->global->MAIN_SIZE_LISTE_LIMIT; +$offset = $limit * $page ; +$pageprev = $page - 1; +$pagenext = $page + 1; + +// Do we click on purge search criteria ? +if (GETPOST("button_removefilter_x")) +{ + $search_label=''; +} + +$filter=array(); +if (!empty($search_label)) { + $filter['t.label']=$search_label; +} + +// Delete jobs +if ($action == 'confirm_delete' && $confirm == "yes" && $user->rights->cron->delete){ + + //Delete de la tache cron + $object = new Cronjob($db); + $object->id=$id; + $result = $object->delete($user); + + if ($result < 0) { + setEventMessage($object->error,'errors'); + } +} + +// Execute jobs +if ($action == 'confirm_execute' && $confirm == "yes" && $user->rights->cron->execute){ + + //Execute jobs + $object = new Cronjob($db); + $job = $object->fetch($id); + + $result = $object->run_jobs($user->login); + if ($result < 0) { + setEventMessage($object->error,'errors'); + } + +} + + +/* + * View + */ +if (!empty($status)) { + $pagetitle=$langs->trans("CronListActive"); +}else { + $pagetitle=$langs->trans("CronListInactive"); +} + +llxHeader('',$pagetitle); + + +// Form object for popup +$form = new Form($db); + +if ($action == 'delete') +{ + $ret=$form->form_confirm($_SERVER['PHP_SELF']."?id=".$id.'&status='.$status,$langs->trans("CronDelete"),$langs->trans("CronConfirmDelete"),"confirm_delete",'','',1); + if ($ret == 'html') print '
'; +} + +if ($action == 'execute'){ + $ret=$form->form_confirm($_SERVER['PHP_SELF']."?id=".$id.'&status='.$status,$langs->trans("CronExecute"),$langs->trans("CronConfirmExecute"),"confirm_execute",'','',1); + if ($ret == 'html') print '
'; +} + + +print_fiche_titre($pagetitle,'','setup'); + +print $langs->trans('CronInfo'); + +// liste des jobs creer +$object = new Cronjob($db); +$result=$object->fetch_all($sortorder, $sortfield, $limit, $offset, $status, $filter); +if ($result < 0) { + setEventMessage($object->error,'errors'); +} + + +print "

"; +print $langs->trans('CronWaitingJobs'); +print "

"; + +if (count($object->lines)>0) { + + print ''; + print ''; + $arg_url='&page='.$page.'&status='.$status.'&search_label='.$search_label; + print_liste_field_titre($langs->trans("CronLabel"),$_SERVEUR['PHP_SELF'],"t.label","",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronTask"),'','',"",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronDtStart"),$_SERVEUR['PHP_SELF'],"t.datestart","",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronDtEnd"),$_SERVEUR['PHP_SELF'],"t.dateend","",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronDtLastLaunch"),$_SERVEUR['PHP_SELF'],"t.datelastrun","",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronDtNextLaunch"),$_SERVEUR['PHP_SELF'],"t.datenextrun","",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronFrequency"),'',"","",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronNbRun"),$_SERVEUR['PHP_SELF'],"t.nbrun","",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronLastResult"),$_SERVEUR['PHP_SELF'],"t.lastresult","",$arg_url,'',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("CronLastOutput"),$_SERVEUR['PHP_SELF'],"t.lastoutput","",$arg_url,'',$sortfield,$sortorder); + print ''; + + print ''; + + print ''."\n"; + print ''; + print ''; + + + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + + + + // Boucler sur chaque job + $style='impair'; + foreach($object->lines as $line){ + // title profil + if ($style=='pair') {$style='impair';} + else {$style='pair';} + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + + print ''; + } + print '
'; + print ''; + print '         '; + print '  '; + print ''; + print '
'; + if(!empty($line->label)) { + print ''.$line->label.''; + } + else { + print $langs->trans('CronNone'); + } + print ''; + if ($line->jobtype=='method') { + print $langs->trans('CronModule').':'.$line->module_name.'
'; + print $langs->trans('CronClass').':'. $line->classesname.'
'; + print $langs->trans('CronObject').':'. $line->objectname.'
'; + print $langs->trans('CronMethod').':'. $line->methodename; + if(!empty($line->params)) { + print '
'.$langs->trans('CronArgs').':'. $line->params; + } + + }elseif ($line->jobtype=='command') { + print $langs->trans('CronCommand').':'. dol_trunc($line->command); + if(!empty($line->params)) { + print '
'.$langs->trans('CronArgs').':'. $line->params; + } + } + print '
'; + if(!empty($line->datestart)) {print dol_print_date($line->datestart,'dayhourtext');} else {print $langs->trans('CronNone');} + print ''; + if(!empty($line->dateend)) {print dol_print_date($line->dateend,'dayhourtext');} else {print $langs->trans('CronNone');} + print ''; + if(!empty($line->datelastrun)) {print dol_print_date($line->datelastrun,'dayhourtext');} else {print $langs->trans('CronNone');} + print ''; + if(!empty($line->datenextrun)) {print dol_print_date($line->datenextrun,'dayhourtext');} else {print $langs->trans('CronNone');} + print ''; + if($line->unitfrequency == "60") print $langs->trans('CronEach')." ".($line->frequency/$line->unitfrequency)." ".$langs->trans('Minutes'); + if($line->unitfrequency == "3600") print $langs->trans('CronEach')." ".($line->frequency/$line->unitfrequency)." ".$langs->trans('Hours'); + if($line->unitfrequency == "86400") print $langs->trans('CronEach')." ".($line->frequency/$line->unitfrequency)." ".$langs->trans('Days'); + if($line->unitfrequency == "604800") print $langs->trans('CronEach')." ".($line->frequency/$line->unitfrequency)." ".$langs->trans('Weeks'); + print ''; + if(!empty($line->nbrun)) {print $line->nbrun;} else {print '0';} + print ''; + if(!empty($line->lastresult)) {print dol_trunc($line->lastresult);} else {print $langs->trans('CronNone');} + print ''; + if(!empty($line->lastoutput)) {print dol_trunc(nl2br($line->lastoutput),100);} else {print $langs->trans('CronNone');} + print ''; + if ($user->rights->cron->delete) { + print "id."&status=".$status."&action=delete\" title=\"".$langs->trans('CronDelete')."\">\"".$langs-trans('CronDelete')."\" />"; + } else { + print "trans('NotEnoughPermissions')."\">\"".$langs-trans('NotEnoughPermissions')."\" />"; + } + if ($user->rights->cron->execute) { + print "id."&status=".$status."&action=execute\" title=\"".$langs->trans('CronExecute')."\">\"".$langs-trans('CronExecute')."\" />"; + } else { + print "trans('NotEnoughPermissions')."\">\"".$langs-trans('NotEnoughPermissions')."\" />"; + } + print '
'; +} else { + print $langs->trans('CronNoJobs'); +} + +print "\n\n
\n"; +if (! $user->rights->cron->create) { + print ''.$langs->trans("New").''; +} else { + print ''.$langs->trans("New").''; +} +print '

'; + +llxFooter(); +$db->close(); \ No newline at end of file diff --git a/htdocs/install/mysql/migration/3.3.0-3.4.0.sql b/htdocs/install/mysql/migration/3.3.0-3.4.0.sql index 9684651832c..0394764a5a7 100755 --- a/htdocs/install/mysql/migration/3.3.0-3.4.0.sql +++ b/htdocs/install/mysql/migration/3.3.0-3.4.0.sql @@ -159,3 +159,36 @@ ALTER TABLE llx_c_action_trigger ADD INDEX idx_action_trigger_rang (rang); ALTER TABLE llx_facture_fourn_det ADD COLUMN fk_code_ventilation integer DEFAULT 0 NOT NULL; ALTER TABLE llx_facturedet DROP COLUMN fk_export_compta; + +CREATE TABLE llx_cronjob +( + rowid integer AUTO_INCREMENT PRIMARY KEY, + tms timestamp, + datec datetime, + jobtype varchar(10) NOT NULL, + label text NOT NULL, + command varchar(255), + classesname varchar(255), + objectname varchar(255), + methodename varchar(255), + params text NOT NULL, + md5params varchar(32), + module_name varchar(255), + priority integer DEFAULT 0, + datelastrun datetime, + datenextrun datetime, + datestart datetime, + dateend datetime, + datelastresult datetime, + lastresult text, + lastoutput text, + unitfrequency integer NOT NULL DEFAULT 0, + frequency integer NOT NULL DEFAULT 0, + nbrun integer, + status integer NOT NULL DEFAULT 1, + fk_user_author integer DEFAULT NULL, + fk_user_mod integer DEFAULT NULL, + note text +)ENGINE=innodb; + + diff --git a/htdocs/install/mysql/tables/llx_cronjob.sql b/htdocs/install/mysql/tables/llx_cronjob.sql index 5a8e33ef38f..a5bb3c10f59 100644 --- a/htdocs/install/mysql/tables/llx_cronjob.sql +++ b/htdocs/install/mysql/tables/llx_cronjob.sql @@ -1,32 +1,52 @@ --- =================================================================== --- Copyright (C) 2013 Laurent Destailleur --- --- 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 . --- --- =================================================================== - -create table llx_cronjob -( - rowid integer AUTO_INCREMENT PRIMARY KEY, - tms timestamp, - datec datetime, - command varchar(256), - params text, - frequency varchar(24), - datelastrun datetime, - lastresult date, - lastoutput text, - fk_user integer DEFAULT NULL, - note text -)ENGINE=innodb; +-- =================================================================== +-- Copyright (C) 2013 Laurent Destailleur +-- Copyright (C) 2013 Florian Henry +-- +-- 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 . +-- +-- =================================================================== + + +CREATE TABLE llx_cronjob +( + rowid integer AUTO_INCREMENT PRIMARY KEY, + tms timestamp, + datec datetime, + jobtype varchar(10) NOT NULL, + label text NOT NULL, + command varchar(255), + classesname varchar(255), + objectname varchar(255), + methodename varchar(255), + params text NOT NULL, + md5params varchar(32), + module_name varchar(255), + priority integer DEFAULT 0, + datelastrun datetime, + datenextrun datetime, + datestart datetime, + dateend datetime, + datelastresult datetime, + lastresult text, + lastoutput text, + unitfrequency integer NOT NULL DEFAULT 0, + frequency integer NOT NULL DEFAULT 0, + nbrun integer, + status integer NOT NULL DEFAULT 1, + fk_user_author integer DEFAULT NULL, + fk_user_mod integer DEFAULT NULL, + note text +)ENGINE=innodb; + + diff --git a/htdocs/install/pgsql/functions/functions.sql b/htdocs/install/pgsql/functions/functions.sql index 0c7f39025f9..b1f9282b6bf 100644 --- a/htdocs/install/pgsql/functions/functions.sql +++ b/htdocs/install/pgsql/functions/functions.sql @@ -116,3 +116,4 @@ CREATE TRIGGER update_customer_modtime BEFORE UPDATE ON llx_tva FOR EACH ROW EXE CREATE TRIGGER update_customer_modtime BEFORE UPDATE ON llx_user FOR EACH ROW EXECUTE PROCEDURE update_modified_column_tms(); CREATE TRIGGER update_customer_modtime BEFORE UPDATE ON llx_user_extrafields FOR EACH ROW EXECUTE PROCEDURE update_modified_column_tms(); CREATE TRIGGER update_customer_modtime BEFORE UPDATE ON llx_usergroup FOR EACH ROW EXECUTE PROCEDURE update_modified_column_tms(); +CREATE TRIGGER update_customer_modtime BEFORE UPDATE ON llx_cronjob FOR EACH ROW EXECUTE PROCEDURE update_modified_column_tms(); diff --git a/htdocs/langs/en_US/admin.lang b/htdocs/langs/en_US/admin.lang index 671a00ddf16..a9aae951692 100644 --- a/htdocs/langs/en_US/admin.lang +++ b/htdocs/langs/en_US/admin.lang @@ -672,6 +672,10 @@ Permission1237=Export supplier orders and their details Permission1251=Run mass imports of external data into database (data load) Permission1321=Export customer invoices, attributes and payments Permission1421=Export customer orders and attributes +Permission23001 = Read Scheduled task +Permission23002 = Create/update Scheduled task +Permission23003 = Delete Scheduled task +Permission23004 = Execute Scheduled task Permission2401=Read actions (events or tasks) linked to his account Permission2402=Create/modify actions (events or tasks) linked to his account Permission2403=Delete actions (events or tasks) linked to his account diff --git a/htdocs/langs/en_US/cron.lang b/htdocs/langs/en_US/cron.lang index 4f504d34e59..c64c74f1f58 100644 --- a/htdocs/langs/en_US/cron.lang +++ b/htdocs/langs/en_US/cron.lang @@ -1,11 +1,87 @@ # Dolibarr language file - en_US - cron CHARSET=UTF-8 -CronSetup=Cron scheduler setup -CronDesc=This page can be used to setup options of the scheduler manager + +# +# Admin +# +CronSetup= Configuration Scheduled task management URLToLaunchCronJobs=URL to launch cron jobs +OrToLaunchASpecificJob=Or to launch a specific job KeyForCronAccess=Security key for URL to launch cron jobs -DateLastRun=Last run -LastOutput=Last run output -LastResult=Last result code -ListOfCronJobs=List of scheduled jobs -Command=Command \ No newline at end of file +FileToLaunchCronJobs=Command to launch cron jobs +CronExplainHowToRunUnix=On Unix environement you should use crontab to run Command line each minutes +CronExplainHowToRunWin=On Microsoft(tm) Windows environement you can use Scheduled task tools to run Command line each minutes + +# +# Page list +# +CronDateLastRun=Last run +CronLastOutput=Last run output +CronLastResult=Last result code +CronListOfCronJobs=List of scheduled jobs +CronCommand=Command +CronList= Job list +CronDelete= Delete cron jobs +CronConfirmDelete= Are you sure to delete this cron jobs ? +CronExecute=Launch task +CronConfirmExecute= Are you sure to execute this job now +CronInfo= Jobs allow to execute task that have been planned +CronWaitingJobs=Wainting jobs +CronTask=Task +CronNone= None +CronDtStart=Start date +CronDtEnd=End fin +CronDtNextLaunch=Next execution +CronDtLastLaunch=Last execution +CronFrequency=Frequancy +CronClass=Classe +CronMethod=Method +CronModule=Module +CronAction=Action +CronStatus=Status +CronStatusActive=Active +CronStatusInactive=Inactive +CronNoJobs=No jobs registered +CronPriority=Priority +CronLabel=Description +CronNbRun=Nb. launch + +# +#Page card +# +CronAdd= Add jobs +CronHourStart= Start Hour and date of task +CronEvery= And execute task each +CronObject= Instance/Object to create +CronArgs=Parameters +CronSaveSucess=Save succefully +CronNote=Comment +CronFieldMandatory=Fields %s is mandatory +CronErrEndDateStartDt=End date cannot be before start date +CronStatusActiveBtn=Active +CronStatusInactiveBtn=Inactive +CronTaskInactive=This task is inactive +CronDtLastResult=Last result date +CronId=Id +CronClassFile=Classes (file name) +CronModuleHelp=Name of Dolibarr module directory (also work with external Dolibarr module).
For exemple to fetch method of Dolibarr Product object /htdocs/product/class/product.class.php, the value of module is product +CronClassFileHelp=The file name to load.
For exemple to fetch method of Dolibarr Product object /htdocs/product/class/product.class.php, the value of class file name is product.class.php +CronObjectHelp=The object name to load.
For exemple to fetch method of Dolibarr Product object /htdocs/product/class/product.class.php, the value of class file name is Product +CronMethodHelp=The object method to launch.
For exemple to fetch method of Dolibarr Product object /htdocs/product/class/product.class.php, the value of method is is fecth +CronArgsHelp=The method arguments.
For exemple to fetch method of Dolibarr Product object /htdocs/product/class/product.class.php, the value of paramters can be 0, ProductRef +CronCommandHelp=The system command line to execute. + +# +# Info +# +CronInfoPage=Information + + +# +# Common +# +CronType=Task type +CronType_method=Call method of a Dolibarr Class +CronType_command=Shell command +CronMenu=Cron +CronCannotLoadClass=Cannot load class %s or object %s \ No newline at end of file diff --git a/htdocs/langs/fr_FR/admin.lang b/htdocs/langs/fr_FR/admin.lang index 095e1a63dc6..a20821cd61b 100644 --- a/htdocs/langs/fr_FR/admin.lang +++ b/htdocs/langs/fr_FR/admin.lang @@ -678,6 +678,10 @@ Permission1411= Lire les mouvements comptables Permission1412= Créer/modifier/annuler les mouvements comptables Permission1415= Lire CA, bilans, résultats, journaux, grands livres Permission1421= Exporter les commandes clients et attributs +Permission23001 = Voir les taches planifiée +Permission23002 = Créer/Modifier les taches planifiée +Permission23003 = Supprimer les taches planifiée +Permission23004 = Executer les taches planifiée Permission2401= Lire les actions (événements ou tâches) liées à son compte Permission2402= Créer/modifier les actions (événements ou tâches) liées à son compte Permission2403= Supprimer les actions (événements ou tâches) liées à son compte diff --git a/htdocs/langs/fr_FR/cron.lang b/htdocs/langs/fr_FR/cron.lang index 1a9bf136e02..d0aaccfecf7 100644 --- a/htdocs/langs/fr_FR/cron.lang +++ b/htdocs/langs/fr_FR/cron.lang @@ -1,11 +1,93 @@ # Dolibarr language file - fr_FR - cron CHARSET=UTF-8 -CronSetup=Configuration du séquenceur de taches -CronDesc=Cette page permet de configurer certaines options du séquenceur de taches + +# +# Admin +# +CronSetup = Page de configuration du module - Gestion de tâches planifiées URLToLaunchCronJobs=URL pour lancer les taches automatiques +OrToLaunchASpecificJob=Ou pour lancer une tache spécifique KeyForCronAccess=Clé de sécurité pour l'URL de lancement des taches automatiques -DateLastRun=Dernier lancement -LastOutput=Dernière sortie -LastResult=Dernière code retour -ListOfCronJobs=Liste des taches programmées -Command=Commande \ No newline at end of file +FileToLaunchCronJobs=Commande pour lancer les taches automatiques +CronExplainHowToRunUnix=Sur un environement Unix vous pouvez paramétré CronTab pour executer cette commande toute les minutes +CronExplainHowToRunWin=Sur un environement Microsoft(tm) Windows vous pouvez utilisr le planificateur de tache pour lancer cette commande toute les minutes + +# +# Menu +# +CronListActive= Liste des tâches planifiées active +CronListInactive= Liste des tâches planifiées inactive + + +# +# Page list +# +CronDateLastRun=Dernier lancement +CronLastOutput=Dernière sortie +CronLastResult=Dernier code retour +CronCommand=Commande +CronList= Liste des tâches planifiées +CronDelete= Supprimer la tâche planifiée +CronConfirmDelete= Êtes-vous sûr de vouloir supprimer cette tâche planifiée ? +CronExecute=Exécuter cette tâche +CronConfirmExecute= Êtes-vous sûr de vouloir exécuter cette tâche maintenant? +CronInfo= Les jobs permettent d'exécuter des tâches à intervales réguliers +CronWaitingJobs= Vos jobs en attente: +CronTask= Tâche +CronNone=Aucun(e) +CronDtStart=Date début +CronDtEnd=Date fin +CronDtNextLaunch=Prochaine éxécution +CronDtLastLaunch=Dernière éxécution +CronFrequency=Fréquence +CronClass=Classes +CronMethod=Méthode +CronModule=Module +CronAction=Action +CronStatus=Status +CronStatusActive=Active +CronStatusInactive=Inactive +CronEach=Tou(te)s +CronNoJobs= Aucun jobs actuellement +CronPriority=Priorité +CronLabel=Description +CronNbRun=Nb. exec. +CronDtLastResult=Date du derniétre resulat de la dernière éxécution + +# +#Page card +# +CronAdd= Ajoutez une tâche +CronHourStart= Jour et Heure de début de la tâche +CronEvery= Puis execution toutes les +CronObject= Instance/Objet à créer +CronArgs= Argument +CronSaveSucess=Enregistrement effectué +CronNote=Note +CronFieldMandatory=Le champ %s est obligatoire +CronErrEndDateStartDt=La date de fin ne peux être avant la date de début +CronStatusActiveBtn=Activer +CronStatusInactiveBtn=Désactiver +CronTaskInactive=Cette tâche est désactivée +CronId=Id +CronClassFile=Classes (fichier) +CronModuleHelp=Nom du repertoire du module dolibarr (fonctionne automatiquement avec les modules externe Dolibarr).
Par exemple pour appeler la mèthode fetch de l'object Product de Dolibarr /htdocs/product/class/product.class.php, la valeur de module est product +CronClassFileHelp=Le fichier qui contient l'objet .
Par exemple pour appeler la mèthode fetch de l'object Product de Dolibarr /htdocs/product/class/product.class.php, la valeur de classe est product.class.php +CronObjectHelp=Le nom de l'object a crée.
Par exemple pour appeler la mèthode fetch de l'object Product de Dolibarr /htdocs/product/class/product.class.php, la valeur de objet est Product +CronMethodHelp=La mèthode a lancer.
Par exemple pour appeler la mèthode fetch de l'object Product de Dolibarr /htdocs/product/class/product.class.php, la valeur de mèthode est fecth +CronArgsHelp=Les arguments de la mèthode.
For exemple to fetch method of Dolibarr Product object /htdocs/product/class/product.class.php, la valeur de paramétre pourrait être 0, RefProduit +CronCommandHelp=La commande système a éxecuter + +# +# Info +# +CronInfoPage=Suivie + +# +# Common +# +CronType=Type d'action a executer +CronType_method=Méthode d'une classe d'un module Dolibarr +CronType_command=Commande Shell +CronMenu=Cron +CronCannotLoadClass=impossible de charger la classe %s ou l'object %s \ No newline at end of file diff --git a/htdocs/public/cron/cron_run_jobs.php b/htdocs/public/cron/cron_run_jobs.php index 75e15a414a0..838c298212f 100644 --- a/htdocs/public/cron/cron_run_jobs.php +++ b/htdocs/public/cron/cron_run_jobs.php @@ -1,5 +1,6 @@ +/* Copyright (C) 2012 Nicolas Villa aka Boyquotes http://informetic.fr + * Copyright (C) 2013 Florian Henry Export agenda cal'; -} -/** - * Footer function - * - * @return void - */ -function llxFooterVierge() { - print ''; -} - - - -// Load traductions files requiredby by page -$langs->load("companies"); -$langs->load("other"); -$langs->load("cron@cron"); - -// Get parameters -$id = GETPOST('id','int'); -$action = GETPOST('action','alpha'); - -// Protection if external user -if ($user->societe_id > 0) -{ - //accessforbidden(); +// librarie core +// Dolibarr environment +$res = @include("../../main.inc.php"); // From htdocs directory +if (! $res) { + $res = @include("../../../main.inc.php"); // From "custom" directory } +if (! $res) die("Include of master.inc.php fails"); -// Security check -if (empty($conf->cron->enabled)) accessforbidden('',1,1,1); +// librarie jobs +dol_include_once("/cron/class/cronjob.class.php"); -// Check also security key -if (empty($_GET["securitykey"]) || $conf->global->CRON_KEY != $_GET["securitykey"]) -{ - $user->getrights(); - - llxHeaderVierge(); - print '
Bad value for key.
'; - llxFooterVierge(); - exit; + +global $langs, $conf; + +// Check the key, avoid that a stranger starts cron +$key = $_GET['securitykey']; +if (empty($key)) { + echo 'securitykey is require'; + exit; } - - -/******************************************************************* -* ACTIONS -* -* Put here all code to do according to value of "action" parameter -********************************************************************/ - -if ($action == 'add') +if($key != $conf->global->MAIN_CRON_KEY) { - $object=new Cronjobs($db); - $object->prop1=$_POST["field1"]; - $object->prop2=$_POST["field2"]; - $result=$object->create($user); - if ($result > 0) - { - // Creation OK - } - { - // Creation KO - $mesg=$object->error; + echo 'securitykey is wrong'; + exit; +} +// Check the key, avoid that a stranger starts cron +$userlogin = $_GET['userlogin']; +if (empty($userlogin)) { + echo 'userlogin is require'; + exit; +} +require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; +$user=new User($db); +$result=$user->fetch('',$userlogin); +if ($result<0) { + echo "User Error:".$user->error; + dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR); + exit; +}else { + if (empty($user->id)) { + echo " User user login:".$userlogin." do not exists"; + dol_syslog(" User user login:".$userlogin." do not exists", LOG_ERR); + exit; } } +$id = $_GET['id']; +// Language Management +$langs->load("admin"); +$langs->load("cron@cron"); +// create a jobs object +$object = new Cronjob($db); +$filter=array(); +if (empty($id)) { + $filter=array(); + $filter['t.rowid']=$id; +} +$result = $object->fetch_all('DESC','t.rowid', 0, 0, 1, $filter); +if ($result<0) { + echo "Error:".$cronjob->error; + dol_syslog("cron_run_jobs.php:: fetch Error".$cronjob->error, LOG_ERR); + exit; +} -/*************************************************** -* VIEW -* -* Put here all code to build page -****************************************************/ +// current date +$now=dol_now(); -llxHeader('',$langs->trans('CronList'),''); +if(is_array($object->lines) && (count($object->lines)>0)){ + // Loop over job + foreach($object->lines as $line){ -$form=new Form($db); - - -// Put here content of your page - -// Example 1 : Adding jquery code -print ''; - - -$cronjob=new CronJob($db); -$result=$cronjob->fetch($id); - -if ($result > 0) -{ - - + echo "OK"; +} else { + echo "No Jobs to run"; } -else -{ - $langs->load("errors"); - print $langs->trans("ErrorRecordNotFound"); -} - - -// End of page -llxFooter(); - -$db->close(); -?> diff --git a/htdocs/theme/amarok/img/cron.png b/htdocs/theme/amarok/img/cron.png new file mode 100644 index 00000000000..ddfe62e7b24 Binary files /dev/null and b/htdocs/theme/amarok/img/cron.png differ diff --git a/htdocs/theme/amarok/img/object_cron.png b/htdocs/theme/amarok/img/object_cron.png new file mode 100644 index 00000000000..b929050598d Binary files /dev/null and b/htdocs/theme/amarok/img/object_cron.png differ diff --git a/htdocs/theme/auguria/img/cron.png b/htdocs/theme/auguria/img/cron.png new file mode 100644 index 00000000000..ddfe62e7b24 Binary files /dev/null and b/htdocs/theme/auguria/img/cron.png differ diff --git a/htdocs/theme/auguria/img/object_cron.png b/htdocs/theme/auguria/img/object_cron.png new file mode 100644 index 00000000000..b929050598d Binary files /dev/null and b/htdocs/theme/auguria/img/object_cron.png differ diff --git a/htdocs/theme/bureau2crea/img/cron.png b/htdocs/theme/bureau2crea/img/cron.png new file mode 100644 index 00000000000..ddfe62e7b24 Binary files /dev/null and b/htdocs/theme/bureau2crea/img/cron.png differ diff --git a/htdocs/theme/bureau2crea/img/object_cron.png b/htdocs/theme/bureau2crea/img/object_cron.png new file mode 100644 index 00000000000..b929050598d Binary files /dev/null and b/htdocs/theme/bureau2crea/img/object_cron.png differ diff --git a/htdocs/theme/cameleo/img/cron.png b/htdocs/theme/cameleo/img/cron.png new file mode 100644 index 00000000000..ddfe62e7b24 Binary files /dev/null and b/htdocs/theme/cameleo/img/cron.png differ diff --git a/htdocs/theme/cameleo/img/object_cron.png b/htdocs/theme/cameleo/img/object_cron.png new file mode 100644 index 00000000000..b929050598d Binary files /dev/null and b/htdocs/theme/cameleo/img/object_cron.png differ diff --git a/htdocs/theme/eldy/img/menus/cron.png b/htdocs/theme/eldy/img/menus/cron.png new file mode 100644 index 00000000000..ddfe62e7b24 Binary files /dev/null and b/htdocs/theme/eldy/img/menus/cron.png differ diff --git a/htdocs/theme/eldy/img/menus/object_cron.png b/htdocs/theme/eldy/img/menus/object_cron.png new file mode 100644 index 00000000000..b929050598d Binary files /dev/null and b/htdocs/theme/eldy/img/menus/object_cron.png differ diff --git a/scripts/cron/cron_run_jobs.php b/scripts/cron/cron_run_jobs.php new file mode 100644 index 00000000000..0c22107bde6 --- /dev/null +++ b/scripts/cron/cron_run_jobs.php @@ -0,0 +1,146 @@ +#!/usr/bin/php +. + */ + +/** + * \file cron/script/cron/cron_run_jobs.php + * \ingroup cron + * \brief Execute pendings jobs + */ +if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); // Disables token renewal +if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); +if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); +if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); +if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +if (! defined('NOLOGIN')) define('NOLOGIN','1'); +//if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); + +$sapi_type = php_sapi_name(); +$script_file = basename(__FILE__); +$path=dirname(__FILE__).'/'; + +// Test if batch mode +if (substr($sapi_type, 0, 3) == 'cgi') { + echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; + exit; +} + +if (! isset($argv[1]) || ! $argv[1]) { + print "Usage: ".$script_file." securitykey userlogin cronjobid(optional)\n"; + exit; +} +$key=$argv[1]; + +if (! isset($argv[2]) || ! $argv[2]) { + print "Usage: ".$script_file." securitykey userlogin cronjobid(optional)\n"; + exit; +} else { + $userlogin=$argv[2]; +} + + +$res=@include("../../master.inc.php"); // For root directory +if (! $res) $res=@include("../../../master.inc.php"); // For "custom" directory +if (! $res) die("Include of master.inc.php fails"); + + +// librarie jobs +require_once (DOL_DOCUMENT_ROOT_ALT."/cron/class/cronjob.class.php"); + + +//Check security key +if($key != $conf->global->MAIN_CRON_KEY) +{ + echo 'securitykey is wrong'; + exit; +} + +//Check user login +require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; +$user=new User($db); +$result=$user->fetch('',$userlogin); +if ($result<0) { + echo "User Error:".$user->error; + dol_syslog("cron_run_jobs.php:: User Error:".$user->error, LOG_ERR); + exit; +}else { + if (empty($user->id)) { + echo " User user login:".$userlogin." do not exists"; + dol_syslog(" User user login:".$userlogin." do not exists", LOG_ERR); + exit; + } +} + +if (isset($argv[3]) || $argv[3]) { + $id = $argv[3]; +} + +// librarie jobs +require_once (DOL_DOCUMENT_ROOT_ALT."/cron/class/cronjob.class.php"); + +// create a jobs object +$object = new Cronjob($db); + +$filter=array(); +if (empty($id)) { + $filter=array(); + $filter['t.rowid']=$id; +} + +$result = $object->fetch_all('DESC','t.rowid', 0, 0, 1, $filter); +if ($result<0) { + echo "Error:".$cronjob->error; + dol_syslog("cron_run_jobs.php:: fetch Error".$cronjob->error, LOG_ERR); + exit; +} + +// current date +$now=dol_now(); + +if(is_array($object->lines) && (count($object->lines)>0)){ + // Loop over job + foreach($object->lines as $line){ + + //If date_next_jobs is less of current dat, execute the program, and store the execution time of the next execution in database + if (($line->datenextrun < $now) && $line->dateend < $now){ + $cronjob=new Cronjob($db); + $result=$cronjob->fetch($line->id); + if ($result<0) { + echo "Error:".$cronjob->error; + dol_syslog("cron_run_jobs.php:: fetch Error".$cronjob->error, LOG_ERR); + exit; + } + // execute methode + $result=$cronjob->run_jobs($userlogin); + if ($result<0) { + echo "Error:".$cronjob->error; + dol_syslog("cron_run_jobs.php:: run_jobs Error".$cronjob->error, LOG_ERR); + exit; + } + + // we re-program the next execution and stores the last execution time for this job + $result=$cronjob->reprogram_jobs($userlogin); + if ($result<0) { + echo "Error:".$cronjob->error; + dol_syslog("cron_run_jobs.php:: reprogram_jobs Error".$cronjob->error, LOG_ERR); + exit; + } + + } + } +} \ No newline at end of file