From 5edef2f02f01a60c74912260ffe334c49a4f80aa Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 5 Jun 2015 03:32:09 +0200 Subject: [PATCH] NEW A module can add its entries into cron module. --- dev/skeletons/modMyModule.class.php | 7 +- htdocs/admin/modules.php | 2 +- htdocs/core/lib/admin.lib.php | 3 +- htdocs/core/modules/DolibarrModules.class.php | 141 +++++++++++++++++- htdocs/core/modules/modGravatar.class.php | 7 + .../install/mysql/migration/3.7.0-3.8.0.sql | 4 +- htdocs/install/mysql/tables/llx_cronjob.sql | 5 +- 7 files changed, 160 insertions(+), 9 deletions(-) diff --git a/dev/skeletons/modMyModule.class.php b/dev/skeletons/modMyModule.class.php index 5bed0a26d33..7c207e9877b 100644 --- a/dev/skeletons/modMyModule.class.php +++ b/dev/skeletons/modMyModule.class.php @@ -167,6 +167,12 @@ class modMyModule extends DolibarrModules // Example: //$this->boxes=array(array(0=>array('file'=>'myboxa.php','note'=>'','enabledbydefaulton'=>'Home'),1=>array('file'=>'myboxb.php','note'=>''),2=>array('file'=>'myboxc.php','note'=>''));); + // Cronjobs + $this->cronjobs = array(); // List of cron jobs entries to add + // Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>3600, 'unitfrequency'=>3600), + // 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>3600, 'unitfrequency'=>3600) + // ); + // Permissions $this->rights = array(); // Permission array used by this module $r=0; @@ -180,7 +186,6 @@ class modMyModule extends DolibarrModules // $this->rights[$r][5] = 'level2'; // In php code, permission will be checked by test if ($user->rights->permkey->level1->level2) // $r++; - // Main menu entries $this->menu = array(); // List of menus to add $r=0; diff --git a/htdocs/admin/modules.php b/htdocs/admin/modules.php index aab09001bd8..86cfae1d606 100644 --- a/htdocs/admin/modules.php +++ b/htdocs/admin/modules.php @@ -5,7 +5,7 @@ * Copyright (C) 2004 Eric Seigne * Copyright (C) 2005-2012 Regis Houssin * Copyright (C) 2011 Juanjo Menent - * Copyright (C) 2015 Jean-François Ferry + * Copyright (C) 2015 Jean-François Ferry * * 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 diff --git a/htdocs/core/lib/admin.lib.php b/htdocs/core/lib/admin.lib.php index 3901e3231df..3a35c961905 100644 --- a/htdocs/core/lib/admin.lib.php +++ b/htdocs/core/lib/admin.lib.php @@ -833,6 +833,7 @@ function unActivateModule($value, $requiredby=1) { $objMod = new $modName($db); $result=$objMod->remove(); + if ($result <= 0) $ret=$objMod->error; } else { @@ -846,7 +847,7 @@ function unActivateModule($value, $requiredby=1) } // Desactivation des modules qui dependent de lui - if ($requiredby) + if (! $ret && $requiredby) { $countrb=count($objMod->requiredby); for ($i = 0; $i < $countrb; $i++) diff --git a/htdocs/core/modules/DolibarrModules.class.php b/htdocs/core/modules/DolibarrModules.class.php index 017d1eb8662..3a7da8b8941 100644 --- a/htdocs/core/modules/DolibarrModules.class.php +++ b/htdocs/core/modules/DolibarrModules.class.php @@ -60,6 +60,11 @@ abstract class DolibarrModules */ public $const = array(); + /** + * @var array Module cron jobs entries + */ + public $cronjobs = array(); + /** * @var array Module access rights */ @@ -209,18 +214,21 @@ abstract class DolibarrModules // Insert activation module constant if (! $err) $err+=$this->_active(); - // Insert new pages for tabs into llx_const + // Insert new pages for tabs (into llx_const) if (! $err) $err+=$this->insert_tabs(); // Insert activation of module's parts if (! $err) $err+=$this->insert_module_parts(); - // Insert constant defined by modules, into llx_const + // Insert constant defined by modules (into llx_const) if (! $err) $err+=$this->insert_const(); - // Insert boxes def into llx_boxes_def and boxes setup into llx_boxes + // Insert boxes def into llx_boxes_def and boxes setup (into llx_boxes) if (! $err && ! preg_match('/noboxes/',$options)) $err+=$this->insert_boxes($options); + // Insert cron job entries (entry in llx_cronjobs) + if (! $err) $err+=$this->insert_cronjobs(); + // Insert permission definitions of module into llx_rights_def. If user is admin, grant this permission to user. if (! $err) $err+=$this->insert_permissions(1); @@ -307,6 +315,9 @@ abstract class DolibarrModules // Remove list of module's available boxes (entry in llx_boxes) if (! $err && ! preg_match('/(newboxdefonly|noboxes)/',$options)) $err+=$this->delete_boxes(); // We don't have to delete if option ask to keep boxes safe or ask to add new box def only + // Remove list of module's cron job entries (entry in llx_cronjobs) + if (! $err) $err+=$this->delete_cronjobs(); + // Remove module's permissions from list of available permissions (entries in llx_rights_def) if (! $err) $err+=$this->delete_permissions(); @@ -823,6 +834,130 @@ abstract class DolibarrModules return $err; } + /** + * Adds cronjobs + * + * @return int Error count (0 if OK) + */ + function insert_cronjobs() + { + require_once DOL_DOCUMENT_ROOT . '/core/class/infobox.class.php'; + + global $conf; + + $err=0; + + if (is_array($this->cronjobs)) + { + foreach ($this->cronjobs as $key => $value) + { + $label = isset($this->cronjobs[$key]['label'])?$this->cronjobs[$key]['label']:''; + $jobtype = isset($this->cronjobs[$key]['jobtype'])?$this->cronjobs[$key]['jobtype']:''; + $class = isset($this->cronjobs[$key]['class'])?$this->cronjobs[$key]['class']:''; + $method = isset($this->cronjobs[$key]['method'])?$this->cronjobs[$key]['method']:''; + $command = isset($this->cronjobs[$key]['command'])?$this->cronjobs[$key]['command']:''; + $parameters = isset($this->cronjobs[$key]['parameters'])?$this->cronjobs[$key]['parameters']:''; + $comment = isset($this->cronjobs[$key]['comment'])?$this->cronjobs[$key]['comment']:''; + $frequency = isset($this->cronjobs[$key]['frequency'])?$this->cronjobs[$key]['frequency']:''; + $unitfrequency = isset($this->cronjobs[$key]['unitfrequency'])?$this->cronjobs[$key]['unitfrequency']:''; + + // Search if boxes def already present + $sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."cronjob"; + $sql.= " WHERE module_name = '".$this->db->escape($this->rights_class)."'"; + if ($class) $sql.= " AND classesname = '".$this->db->escape($class)."'"; + if ($method) $sql.= " AND methodename = '".$this->db->escape($method)."'"; + if ($command) $sql.= " AND command = '".$this->db->escape($command)."'"; + $sql.= " AND entity = ".$conf->entity; + + $now=dol_now(); + + dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG); + $result=$this->db->query($sql); + if ($result) + { + $obj = $this->db->fetch_object($result); + if ($obj->nb == 0) + { + $this->db->begin(); + + if (! $err) + { + $sql = "INSERT INTO ".MAIN_DB_PREFIX."cronjob (module_name, datec, label, jobtype, classesname, methodename, command, params, note, frequency, unitfrequency, entity)"; + $sql.= " VALUES ("; + $sql.= "'".$this->db->escape($this->rights_class)."', "; + $sql.= "'".$this->db->idate($now)."', "; + $sql.= "'".$this->db->escape($label)."', "; + $sql.= "'".$this->db->escape($jobtype)."', "; + $sql.= ($class?"'".$this->db->escape($class)."'":"null").","; + $sql.= ($method?"'".$this->db->escape($method)."'":"null").","; + $sql.= ($command?"'".$this->db->escape($command)."'":"null").","; + $sql.= ($parameters?"'".$this->db->escape($parameters)."'":"null").","; + $sql.= ($comment?"'".$this->db->escape($comment)."'":"null").","; + $sql.= "'".$this->db->escape($frequency)."', "; + $sql.= "'".$this->db->escape($unitfrequency)."', "; + $sql.= $conf->entity; + $sql.= ")"; +print $sql; + + dol_syslog(get_class($this)."::insert_cronjobs", LOG_DEBUG); + $resql=$this->db->query($sql); + if (! $resql) $err++; + + } + + if (! $err) + { + $this->db->commit(); + } + else + { + $this->error=$this->db->lasterror(); + $this->db->rollback(); + } + } + // else box already registered into database + } + else + { + $this->error=$this->db->lasterror(); + $err++; + } + } + } + + return $err; + } + + + /** + * Removes boxes + * + * @return int Error count (0 if OK) + */ + function delete_cronjobs() + { + global $conf; + + $err=0; + + if (is_array($this->cronjobs)) + { + $sql = "DELETE FROM ".MAIN_DB_PREFIX."cronjob"; + $sql.= " WHERE module_name = '".$this->db->escape($this->rights_class)."'"; + $sql.= " AND entity = ".$conf->entity; + + dol_syslog(get_class($this)."::delete_cronjobs", LOG_DEBUG); + $resql=$this->db->query($sql); + if (! $resql) + { + $this->error=$this->db->lasterror(); + $err++; + } + } + + return $err; + } + /** * Removes tabs * diff --git a/htdocs/core/modules/modGravatar.class.php b/htdocs/core/modules/modGravatar.class.php index 42eece9ce4f..aae38ae391b 100644 --- a/htdocs/core/modules/modGravatar.class.php +++ b/htdocs/core/modules/modGravatar.class.php @@ -99,6 +99,13 @@ class modGravatar extends DolibarrModules //$r++; + // Cronjobs + $this->cronjobs = array(); // List of cron jobs entries to add + // Example: $this->cronjobs=array(0=>array('label'=>'My label', 'jobtype'=>'method', 'class'=>'MyClass', 'method'=>'myMethod', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>3600, 'unitfrequency'=>3600), + // 1=>array('label'=>'My label', 'jobtype'=>'command', 'command'=>'', 'parameters'=>'', 'comment'=>'Comment', 'frequency'=>3600, 'unitfrequency'=>3600) + // ); + $r=0; + // Permissions $this->rights = array(); // Permission array used by this module $r=0; diff --git a/htdocs/install/mysql/migration/3.7.0-3.8.0.sql b/htdocs/install/mysql/migration/3.7.0-3.8.0.sql index 7a788a7d352..c8056508220 100755 --- a/htdocs/install/mysql/migration/3.7.0-3.8.0.sql +++ b/htdocs/install/mysql/migration/3.7.0-3.8.0.sql @@ -32,7 +32,9 @@ insert into llx_c_tva(rowid,fk_pays,taux,recuperableonly,note,active) values ( 2 ALTER TABLE llx_societe_rib ADD COLUMN rum varchar(32) after default_rib; ALTER TABLE llx_societe_rib ADD COLUMN frstrecur varchar(16) default 'FRST' after rum; - +ALTER TABLE llx_cronjob ADD COLUMN entity integer DEFAULT 0; +ALTER TABLE llx_cronjob MODIFY COLUMN params text NULL; + -- Loan create table llx_loan ( diff --git a/htdocs/install/mysql/tables/llx_cronjob.sql b/htdocs/install/mysql/tables/llx_cronjob.sql index a7ed4858024..5580b5ab830 100644 --- a/htdocs/install/mysql/tables/llx_cronjob.sql +++ b/htdocs/install/mysql/tables/llx_cronjob.sql @@ -29,7 +29,7 @@ CREATE TABLE llx_cronjob classesname varchar(255), objectname varchar(255), methodename varchar(255), - params text NOT NULL, + params text, md5params varchar(32), module_name varchar(255), priority integer DEFAULT 0, @@ -47,5 +47,6 @@ CREATE TABLE llx_cronjob fk_user_author integer DEFAULT NULL, fk_user_mod integer DEFAULT NULL, note text, - libname varchar(255) + libname varchar(255), + entity integer DEFAULT 0 )ENGINE=innodb;