From aa4f2ba8fb845d140b58b5da0ac9fb7381e8bac2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Salvador?= Date: Wed, 19 Jun 2013 18:50:52 +0200 Subject: [PATCH] made it possible to call a function from a library Conflicts: htdocs/install/mysql/tables/llx_cronjob.sql scripts/cron/cron_run_jobs.php --- htdocs/cron/class/cronjob.class.php | 73 +++++++++++++++++++-- htdocs/install/mysql/tables/llx_cronjob.sql | 6 +- scripts/cron/cron_run_jobs.php | 3 +- 3 files changed, 71 insertions(+), 11 deletions(-) diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index 454eb457517..1dd6b32a8c2 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -65,7 +65,7 @@ class Cronjob extends CommonObject var $fk_user_mod; var $note; var $nbrun; - + var $libname; var $lines; @@ -114,6 +114,7 @@ class Cronjob extends CommonObject 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); + if (isset($this->libname)) $this->libname = trim($this->libname); // Check parameters // Put here code to add a control on parameters values @@ -141,7 +142,7 @@ class Cronjob extends CommonObject $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass')); $error++; } - if (($this->jobtype=='method') && (empty($this->methodename))) { + if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) { $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod')); $error++; } @@ -149,6 +150,11 @@ class Cronjob extends CommonObject $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronObject')); $error++; } + + if (($this->jobtype=='function') && (empty($this->libname))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLib')); + $error++; + } // Insert request $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob("; @@ -178,6 +184,7 @@ class Cronjob extends CommonObject $sql.= "fk_user_mod,"; $sql.= "note,"; $sql.= "nbrun"; + $sql .= ",libname"; $sql.= ") VALUES ("; @@ -206,7 +213,8 @@ class Cronjob extends CommonObject $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.= " ".(! isset($this->nbrun)?'0':"'".$this->db->escape($this->nbrun)."'").","; + $sql.= " ".(! isset($this->libname)?'NULL':"'".$this->db->escape($this->libname)."'").""; $sql.= ")"; @@ -293,6 +301,7 @@ class Cronjob extends CommonObject $sql.= " t.fk_user_mod,"; $sql.= " t.note,"; $sql.= " t.nbrun"; + $sql .= ", t.libname"; $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t"; @@ -335,7 +344,7 @@ class Cronjob extends CommonObject $this->fk_user_mod = $obj->fk_user_mod; $this->note = $obj->note; $this->nbrun = $obj->nbrun; - + $this->libname = $obj->libname; } $this->db->free($resql); @@ -392,6 +401,7 @@ class Cronjob extends CommonObject $sql.= " t.fk_user_mod,"; $sql.= " t.note,"; $sql.= " t.nbrun"; + $sql .= ", t.libname"; $sql.= " FROM ".MAIN_DB_PREFIX."cronjob as t"; $sql.= " WHERE 1 = 1"; @@ -464,7 +474,7 @@ class Cronjob extends CommonObject $line->fk_user_mod = $obj->fk_user_mod; $line->note = $obj->note; $line->nbrun = $obj->nbrun; - + $line->libname = $obj->libname; $this->lines[]=$line; $i++; @@ -518,6 +528,7 @@ class Cronjob extends CommonObject 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); + if (isset($this->libname)) $this->libname = trim($this->libname); // Check parameters // Put here code to add a control on parameters values @@ -548,7 +559,7 @@ class Cronjob extends CommonObject $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronClass')); $error++; } - if (($this->jobtype=='method') && (empty($this->methodename))) { + if (($this->jobtype=='method' || $this->jobtype == 'function') && (empty($this->methodename))) { $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronMethod')); $error++; } @@ -557,6 +568,11 @@ class Cronjob extends CommonObject $error++; } + if (($this->jobtype=='function') && (empty($this->libname))) { + $this->errors[]=$langs->trans('CronFieldMandatory',$langs->trans('CronLib')); + $error++; + } + // Update request $sql = "UPDATE ".MAIN_DB_PREFIX."cronjob SET"; @@ -584,6 +600,7 @@ class Cronjob extends CommonObject $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.= ", libname=".(isset($this->libname)?"'".$this->db->escape($this->libname)."'":"null"); $sql.= " WHERE rowid=".$this->id; @@ -780,6 +797,7 @@ class Cronjob extends CommonObject $this->fk_user_mod=''; $this->note=''; $this->nbrun=''; + $this->libname = ''; } /** @@ -941,6 +959,48 @@ class Cronjob extends CommonObject } + if($this->jobtype == 'function') + { + //load lib + $libpath = '/' . strtolower($this->module_name) . '/lib/' . $this->libname; + $ret = dol_include_once($libpath); + if ($ret === false) + { + $this->error = $langs->trans('CronCannotLoadLib') . ': ' . $libpath; + 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->libname . "::" . $this->methodename."(" . $this->params . ");", LOG_DEBUG); + $params_arr = array(); + $params_arr = explode(", ", $this->params); + if (!is_array($params_arr)) + { + $result = call_user_func($this->methodename, $this->params); + } + else + { + $result = call_user_func_array($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); + } + } + // Run a command line if ($this->jobtype=='command') { @@ -1090,6 +1150,7 @@ class Cronjobline var $fk_user_mod; var $note; var $nbrun; + var $libname; /** * Constructor diff --git a/htdocs/install/mysql/tables/llx_cronjob.sql b/htdocs/install/mysql/tables/llx_cronjob.sql index 6c83994a936..01c8d5eef5c 100644 --- a/htdocs/install/mysql/tables/llx_cronjob.sql +++ b/htdocs/install/mysql/tables/llx_cronjob.sql @@ -46,6 +46,6 @@ CREATE TABLE llx_cronjob status integer NOT NULL DEFAULT 1, fk_user_author integer DEFAULT NULL, fk_user_mod integer DEFAULT NULL, - note text - -)ENGINE=innodb; \ No newline at end of file + note text, + libname varchar(255) +)ENGINE=innodb; diff --git a/scripts/cron/cron_run_jobs.php b/scripts/cron/cron_run_jobs.php index 3cdf7493663..79783b53d31 100755 --- a/scripts/cron/cron_run_jobs.php +++ b/scripts/cron/cron_run_jobs.php @@ -58,7 +58,6 @@ if (! isset($argv[2]) || ! $argv[2]) { require_once ($path."../../htdocs/master.inc.php"); require_once (DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php"); require_once (DOL_DOCUMENT_ROOT.'/user/class/user.class.php'); -require_once (DOL_DOCUMENT_ROOT."/cron/class/cronjob.class.php"); // Global variables $version=DOL_VERSION; @@ -161,4 +160,4 @@ if(is_array($object->lines) && (count($object->lines)>0)) $db->close(); exit(0); -?> \ No newline at end of file +?>