From ba7b6a8d5146ba0d7f077701a8e8aae1b3acba2b Mon Sep 17 00:00:00 2001 From: kamel Date: Mon, 21 Jun 2021 16:38:36 +0200 Subject: [PATCH 1/5] FIX: Reload values of the specified entity when change entity in the execution of cron job --- htdocs/core/class/conf.class.php | 67 +++++++++++++++++++++++++++++ htdocs/cron/class/cronjob.class.php | 16 +++---- 2 files changed, 75 insertions(+), 8 deletions(-) diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index 8766d68c25b..ed919be568c 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -151,6 +151,19 @@ class Conf $this->productbatch = new stdClass(); } + /** + * Load setup values into conf object (read llx_const) for a specified entity + * Note that this->db->xxx, this->file->xxx and this->multicompany have been already loaded when setValues is called. + * + * @param DoliDB $db Database handler + * @param int $entity Entity to get + * @return int < 0 if KO, >= 0 if OK + */ + function setEntityValues($db, $entity) + { + $this->entity = $entity; + $this->setValues($db); + } /** * Load setup values into conf object (read llx_const) @@ -161,6 +174,60 @@ class Conf */ public function setValues($db) { + global $conf; + + // Unset all old modules values + if (!empty($this->modules)) { + foreach ($this->modules as $m) { + if (isset($this->$m)) unset($this->$m); + } + } + + // Properly declare multi-modules objects. + $this->global = new stdClass(); + $this->multicompany = new stdClass(); + + // First level object + // TODO Remove this part. + $this->expedition_bon = new stdClass(); + $this->delivery_note = new stdClass(); + $this->fournisseur = new stdClass(); + $this->product = new stdClass(); + $this->service = new stdClass(); + $this->contrat = new stdClass(); + $this->actions = new stdClass(); + $this->agenda = new stdClass(); + $this->commande = new stdClass(); + $this->propal = new stdClass(); + $this->facture = new stdClass(); + $this->contrat = new stdClass(); + $this->usergroup = new stdClass(); + $this->adherent = new stdClass(); + $this->bank = new stdClass(); + $this->notification = new stdClass(); + $this->mailing = new stdClass(); + $this->expensereport = new stdClass(); + $this->productbatch = new stdClass(); + $this->modules = array();; + $this->modules_parts = array( + 'css' => array(), + 'js' => array(), + 'tabs' => array(), + 'triggers' => array(), + 'login' => array(), + 'substitutions' => array(), + 'menus' => array(), + 'theme' => array(), + 'sms' => array(), + 'tpl' => array(), + 'barcode' => array(), + 'models' => array(), + 'societe' => array(), + 'hooks' => array(), + 'dir' => array(), + 'syslog' => array(), + ); + dol_syslog(get_class($this)."::setValues"); //Define all global constants into $this->global->key=value diff --git a/htdocs/cron/class/cronjob.class.php b/htdocs/cron/class/cronjob.class.php index 2ffce20a924..e2b7c2da47b 100644 --- a/htdocs/cron/class/cronjob.class.php +++ b/htdocs/cron/class/cronjob.class.php @@ -1081,7 +1081,7 @@ class Cronjob extends CommonObject dol_syslog("We try to run a job in entity ".$this->entity." when we are in entity ".$conf->entity, LOG_WARNING); } $savcurrententity = $conf->entity; - $conf->entity = $this->entity; + $conf->setEntityValues($this->db, $this->entity); dol_syslog(get_class($this)."::run_jobs entity for running job is ".$conf->entity); require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php'; @@ -1090,13 +1090,13 @@ class Cronjob extends CommonObject if ($result < 0) { $this->error = "User Error:".$user->error; dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); - $conf->entity = $savcurrententity; + $conf->setEntityValues($this->db, $savcurrententity); 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); - $conf->entity = $savcurrententity; + $conf->setEntityValues($this->db, $savcurrententity); return -1; } } @@ -1126,7 +1126,7 @@ class Cronjob extends CommonObject $result = $this->update($user); // This include begin/commit if ($result < 0) { dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); - $conf->entity = $savcurrententity; + $conf->setEntityValues($this->db, $savcurrententity); return -1; } @@ -1241,7 +1241,7 @@ class Cronjob extends CommonObject if ($ret === false) { $this->error = $langs->trans('CronCannotLoadLib').': '.$libpath; dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); - $conf->entity = $savcurrententity; + $conf->setEntityValues($this->db, $savcurrententity); return -1; } @@ -1250,7 +1250,7 @@ class Cronjob extends CommonObject $result = $langs->load($this->module_name.'@'.$this->module_name); // If this->module_name was an existing language file, this will make nothing if ($result < 0) { // If technical error dol_syslog(get_class($this)."::run_jobs Cannot load module langs".$langs->error, LOG_ERR); - $conf->entity = $savcurrententity; + $conf->setEntityValues($this->db, $savcurrententity); return -1; } @@ -1316,11 +1316,11 @@ class Cronjob extends CommonObject $result = $this->update($user); // This include begin/commit if ($result < 0) { dol_syslog(get_class($this)."::run_jobs ".$this->error, LOG_ERR); - $conf->entity = $savcurrententity; + $conf->setEntityValues($this->db, $savcurrententity); return -1; } - $conf->entity = $savcurrententity; + $conf->setEntityValues($this->db, $savcurrententity); return $error ?-1 : 1; } From dd61c497228aac8685dcd5774d7531a21c5fcf0f Mon Sep 17 00:00:00 2001 From: kamel Date: Mon, 21 Jun 2021 16:44:31 +0200 Subject: [PATCH 2/5] Correction --- htdocs/core/class/conf.class.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index ed919be568c..bc953faab2e 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -174,8 +174,6 @@ class Conf */ public function setValues($db) { - global $conf; - // Unset all old modules values if (!empty($this->modules)) { foreach ($this->modules as $m) { From 488d875854b29791e3a66dcddb372a506ffc0655 Mon Sep 17 00:00:00 2001 From: kamel Date: Mon, 21 Jun 2021 16:51:54 +0200 Subject: [PATCH 3/5] Correction --- htdocs/core/class/conf.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index bc953faab2e..4000dc4d6f1 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -159,7 +159,7 @@ class Conf * @param int $entity Entity to get * @return int < 0 if KO, >= 0 if OK */ - function setEntityValues($db, $entity) + public function setEntityValues($db, $entity) { $this->entity = $entity; $this->setValues($db); From 152aab4903afa548d25874c34d404a36bad9d0b9 Mon Sep 17 00:00:00 2001 From: kamel Date: Mon, 18 Oct 2021 10:10:31 +0200 Subject: [PATCH 4/5] Add test if actual entity is different to the entity provided --- htdocs/core/class/conf.class.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index 4000dc4d6f1..3f938fe778c 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -161,8 +161,10 @@ class Conf */ public function setEntityValues($db, $entity) { - $this->entity = $entity; - $this->setValues($db); + if ($this->entity != $entity) { + $this->entity = $entity; + $this->setValues($db); + } } /** From f328bb32f6ab545c1b98c8be47c9daa5dbdf04c0 Mon Sep 17 00:00:00 2001 From: kamel Date: Tue, 19 Oct 2021 17:34:03 +0200 Subject: [PATCH 5/5] Corrections --- htdocs/core/class/conf.class.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/htdocs/core/class/conf.class.php b/htdocs/core/class/conf.class.php index 3f938fe778c..7b35aad3075 100644 --- a/htdocs/core/class/conf.class.php +++ b/htdocs/core/class/conf.class.php @@ -163,8 +163,10 @@ class Conf { if ($this->entity != $entity) { $this->entity = $entity; - $this->setValues($db); + return $this->setValues($db); } + + return 0; } /** @@ -176,6 +178,8 @@ class Conf */ public function setValues($db) { + dol_syslog(get_class($this)."::setValues"); + // Unset all old modules values if (!empty($this->modules)) { foreach ($this->modules as $m) { @@ -228,8 +232,6 @@ class Conf 'syslog' => array(), ); - dol_syslog(get_class($this)."::setValues"); - //Define all global constants into $this->global->key=value $sql = "SELECT ".$db->decrypt('name')." as name,"; $sql .= " ".$db->decrypt('value')." as value, entity";