2004-10-20 00:24:10 +02:00
< ? php
2007-04-10 14:08:44 +02:00
/* Copyright ( C ) 2003 - 2007 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2004 Sebastien Di Cintio < sdicintio @ ressource - toi . org >
* Copyright ( C ) 2004 Benoit Mortier < benoit . mortier @ opensides . be >
* Copyright ( C ) 2004 Eric Seigne < eric . seigne @ ryxeo . com >
2013-06-03 15:18:36 +02:00
* Copyright ( C ) 2005 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
2012-12-30 15:11:07 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2003-11-15 18:41:07 +01:00
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2003-11-15 18:41:07 +01:00
* ( 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
2011-08-01 01:24:38 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2003-11-15 18:41:07 +01:00
*/
2005-04-09 17:52:07 +02:00
/**
2011-10-24 14:11:49 +02:00
* \file htdocs / core / modules / DolibarrModules . class . php
2013-06-03 15:18:36 +02:00
* \brief File of parent class of module descriptor class files
2008-10-06 09:39:52 +02:00
*/
2004-08-15 14:40:18 +02:00
2005-04-09 17:52:07 +02:00
/**
2013-06-03 15:18:36 +02:00
* Parent class of module descriptor class files
2008-10-06 09:39:52 +02:00
*/
2011-08-28 16:58:29 +02:00
abstract class DolibarrModules
2003-11-15 18:41:07 +01:00
{
2011-06-09 02:05:08 +02:00
//! Database handler
var $db ;
//! Relative path to module style sheet
2012-02-27 09:09:34 +01:00
var $style_sheet = '' ; // deprecated
2011-06-09 02:05:08 +02:00
//! Path to create when module activated
var $dirs = array ();
//! Tableau des boites
var $boxes ;
//! Tableau des constantes
var $const ;
//! Tableau des droits
var $rights ;
//! Tableau des menus
var $menu = array ();
2012-02-27 09:09:34 +01:00
//! Module parts array
var $module_parts = array ();
2011-06-09 02:05:08 +02:00
//! Tableau des documents ???
var $docs ;
2011-09-29 10:08:26 +02:00
var $dbversion = " - " ;
2011-06-09 02:05:08 +02:00
2013-10-26 16:18:28 +02:00
2011-06-09 02:05:08 +02:00
/**
2013-10-26 16:18:28 +02:00
* Method to enable a module . Insert into database all constants , boxes of module
2011-08-31 12:41:06 +02:00
*
2013-10-26 16:18:28 +02:00
* @ param array $array_sql Array of SQL requests to execute when enabling module
* @ param string $options String with options when disabling module ( 'newboxdefonly|noboxes' )
* @ return int 1 if OK , 0 if KO
2011-06-09 02:05:08 +02:00
*/
function _init ( $array_sql , $options = '' )
{
2012-08-22 17:33:09 +02:00
global $conf , $langs ;
2011-06-09 02:05:08 +02:00
$err = 0 ;
$this -> db -> begin ();
// Insert activation module constant
if ( ! $err ) $err += $this -> _active ();
// Insert new pages for tabs into llx_const
if ( ! $err ) $err += $this -> insert_tabs ();
2012-02-24 11:06:05 +01:00
2012-02-23 15:42:03 +01:00
// Insert activation of module's parts
if ( ! $err ) $err += $this -> insert_module_parts ();
2011-06-09 02:05:08 +02:00
// Insert constant defined by modules, into llx_const
if ( ! $err ) $err += $this -> insert_const ();
2013-10-26 16:18:28 +02:00
// Insert boxes def into llx_boxes_def and boxes setup into llx_boxes
if ( ! $err && ! preg_match ( '/noboxes/' , $options )) $err += $this -> insert_boxes ( $options );
2011-06-09 02:05:08 +02:00
// 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 );
2011-12-30 23:07:27 +01:00
// Insert specific menus entries into database
2011-06-09 02:05:08 +02:00
if ( ! $err ) $err += $this -> insert_menus ();
// Create module's directories
if ( ! $err ) $err += $this -> create_dirs ();
2012-02-05 19:37:52 +01:00
// Execute addons requests
$num = count ( $array_sql );
for ( $i = 0 ; $i < $num ; $i ++ )
2011-06-09 02:05:08 +02:00
{
2012-02-05 19:37:52 +01:00
if ( ! $err )
2011-06-09 02:05:08 +02:00
{
2012-02-05 19:37:52 +01:00
$val = $array_sql [ $i ];
$sql = '' ;
$ignoreerror = 0 ;
if ( is_array ( $val ))
{
$sql = $val [ 'sql' ];
$ignoreerror = $val [ 'ignoreerror' ];
}
else
2011-06-09 02:05:08 +02:00
{
2012-02-05 19:37:52 +01:00
$sql = $val ;
}
2012-08-22 17:33:09 +02:00
// Add current entity id
$sql = str_replace ( '__ENTITY__' , $conf -> entity , $sql );
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::_init ignoreerror= " . $ignoreerror . " " , LOG_DEBUG );
2014-03-16 17:37:54 +01:00
$result = $this -> db -> query ( $sql , $ignoreerror );
2012-02-05 19:37:52 +01:00
if ( ! $result )
{
if ( ! $ignoreerror )
2011-06-09 02:05:08 +02:00
{
2012-02-05 19:37:52 +01:00
$this -> error = $this -> db -> lasterror ();
$err ++ ;
2011-06-09 02:05:08 +02:00
}
else
{
2012-02-05 19:37:52 +01:00
dol_syslog ( get_class ( $this ) . " ::_init Warning " . $this -> db -> lasterror (), LOG_WARNING );
2011-06-09 02:05:08 +02:00
}
}
}
}
// Return code
if ( ! $err )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return 0 ;
}
}
/**
* Fonction de desactivation . Supprime de la base les constantes et boites du module
2011-08-31 12:41:06 +02:00
*
* @ param array $array_sql Array of SQL requests to execute when disable module
2013-10-26 16:18:28 +02:00
* @ param string $options String with options when disabling module ( 'newboxdefonly|noboxes' )
2011-08-31 12:41:06 +02:00
* @ return int 1 if OK , 0 if KO
2011-06-09 02:05:08 +02:00
*/
function _remove ( $array_sql , $options = '' )
{
global $langs ;
$err = 0 ;
$this -> db -> begin ();
// Remove activation module line (constant MAIN_MODULE_MYMODULE in llx_const)
if ( ! $err ) $err += $this -> _unactive ();
// Remove activation of module's new tabs (MAIN_MODULE_MYMODULE_TABS_XXX in llx_const)
if ( ! $err ) $err += $this -> delete_tabs ();
2012-02-24 11:06:05 +01:00
2012-02-23 15:42:03 +01:00
// Remove activation of module's parts (MAIN_MODULE_MYMODULE_XXX in llx_const)
if ( ! $err ) $err += $this -> delete_module_parts ();
2009-01-20 00:32:18 +01:00
2011-01-30 17:05:57 +01:00
// Remove constants defined by modules
if ( ! $err ) $err += $this -> delete_const ();
2011-06-09 02:05:08 +02:00
// Remove list of module's available boxes (entry in llx_boxes)
2013-10-26 16:18:28 +02:00
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
2011-06-09 02:05:08 +02:00
// Remove module's permissions from list of available permissions (entries in llx_rights_def)
if ( ! $err ) $err += $this -> delete_permissions ();
// Remove module's menus (entries in llx_menu)
if ( ! $err ) $err += $this -> delete_menus ();
// Remove module's directories
if ( ! $err ) $err += $this -> delete_dirs ();
// Run complementary sql requests
2011-09-17 21:49:50 +02:00
$num = count ( $array_sql );
for ( $i = 0 ; $i < $num ; $i ++ )
2011-06-09 02:05:08 +02:00
{
if ( ! $err )
{
2014-06-12 13:49:05 +02:00
dol_syslog ( get_class ( $this ) . " ::_remove " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$result = $this -> db -> query ( $array_sql [ $i ]);
if ( ! $result )
{
$this -> error = $this -> db -> error ();
$err ++ ;
}
}
}
// Return code
if ( ! $err )
{
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return 0 ;
}
}
/**
2011-06-28 22:31:23 +02:00
* Retourne le nom traduit du module si la traduction existe dans admin . lang ,
* sinon le nom defini par defaut dans le module .
2012-03-13 09:38:00 +01:00
*
2011-06-28 22:31:23 +02:00
* @ return string Nom du module traduit
2011-06-09 02:05:08 +02:00
*/
function getName ()
{
global $langs ;
$langs -> load ( " admin " );
if ( $langs -> trans ( " Module " . $this -> numero . " Name " ) != ( " Module " . $this -> numero . " Name " ))
{
// Si traduction du nom du module existe
return $langs -> trans ( " Module " . $this -> numero . " Name " );
}
else
{
// If translation of module with its numero does not exists, we take its name
return $this -> name ;
}
}
/**
2011-06-28 22:31:23 +02:00
* Retourne la description traduite du module si la traduction existe dans admin . lang ,
2011-08-31 13:07:16 +02:00
* sinon la description definie par defaut dans le module
*
2011-06-28 22:31:23 +02:00
* @ return string Nom du module traduit
2011-06-09 02:05:08 +02:00
*/
function getDesc ()
{
global $langs ;
$langs -> load ( " admin " );
if ( $langs -> trans ( " Module " . $this -> numero . " Desc " ) != ( " Module " . $this -> numero . " Desc " ))
{
// Si traduction de la description du module existe
return $langs -> trans ( " Module " . $this -> numero . " Desc " );
}
else
{
// Si traduction de la description du module n'existe pas, on prend definition en dur dans module
return $this -> description ;
}
}
/**
2013-10-28 01:37:04 +01:00
* Return module version .
2011-06-28 22:31:23 +02:00
* Pour les modules a l 'etat ' experimental ', retourne la traduction de ' experimental '
* Pour les modules 'dolibarr' , retourne la version de Dolibarr
* Pour les autres modules , retourne la version du module
2011-08-31 13:07:16 +02:00
*
2011-06-28 22:31:23 +02:00
* @ return string Version du module
2011-06-09 02:05:08 +02:00
*/
function getVersion ()
{
global $langs ;
$langs -> load ( " admin " );
if ( $this -> version == 'experimental' ) return $langs -> trans ( " VersionExperimental " );
elseif ( $this -> version == 'development' ) return $langs -> trans ( " VersionDevelopment " );
elseif ( $this -> version == 'dolibarr' ) return DOL_VERSION ;
elseif ( $this -> version ) return $this -> version ;
else return $langs -> trans ( " VersionUnknown " );
}
2013-10-28 01:37:04 +01:00
/**
* Return if a module is a core or external module
*
* @ return string 'core' , 'external' or 'unknown'
*/
function isCoreOrExternalModule ()
{
if ( $this -> version == 'dolibarr' ) return 'core' ;
if ( ! empty ( $this -> version ) && ! in_array ( $this -> version , array ( 'experimental' , 'development' ))) return 'external' ;
if ( ! empty ( $this -> editor_name ) || ! empty ( $this -> editor_web )) return 'external' ;
return 'unknown' ;
}
2011-06-09 02:05:08 +02:00
/**
* Return list of lang files related to module
2011-08-31 13:07:16 +02:00
*
2011-06-09 02:05:08 +02:00
* @ return array Array of lang files
*/
function getLangFilesArray ()
{
return $this -> langfiles ;
}
/**
* Return translated label of a export dataset
2011-08-31 13:07:16 +02:00
*
2012-03-13 09:38:00 +01:00
* @ param int $r Index of dataset
* @ return string Label of databaset
2011-06-09 02:05:08 +02:00
*/
function getExportDatasetLabel ( $r )
{
global $langs ;
$langstring = " ExportDataset_ " . $this -> export_code [ $r ];
if ( $langs -> trans ( $langstring ) == $langstring )
{
// Traduction non trouvee
return $langs -> trans ( $this -> export_label [ $r ]);
}
else
{
// Traduction trouvee
return $langs -> trans ( $langstring );
}
}
/**
* Return translated label of an import dataset
2011-08-31 13:07:16 +02:00
*
2012-03-13 09:38:00 +01:00
* @ param int $r Index of dataset
* @ return string Label of databaset
2011-06-09 02:05:08 +02:00
*/
function getImportDatasetLabel ( $r )
{
global $langs ;
$langstring = " ImportDataset_ " . $this -> import_code [ $r ];
//print "x".$langstring;
if ( $langs -> trans ( $langstring ) == $langstring )
{
// Traduction non trouvee
return $langs -> trans ( $this -> import_label [ $r ]);
}
else
{
// Traduction trouvee
return $langs -> trans ( $langstring );
}
}
/**
* Insert constant to activate module
2011-08-31 13:07:16 +02:00
*
2011-06-09 02:05:08 +02:00
* @ return int Nb of errors ( 0 if OK )
*/
function _active ()
{
global $conf ;
$err = 0 ;
// Common module
$entity = (( ! empty ( $this -> always_enabled ) || ! empty ( $this -> core_enabled )) ? 0 : $conf -> entity );
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " const " ;
2012-02-19 09:59:13 +01:00
$sql .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " = ' " . $this -> const_name . " ' " ;
$sql .= " AND entity IN (0, " . $entity . " ) " ;
2011-06-09 02:05:08 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::_active " , LOG_DEBUG );
2012-02-19 03:21:10 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) $err ++ ;
2011-06-09 02:05:08 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " const (name,value,visible,entity) VALUES " ;
2012-02-19 09:59:13 +01:00
$sql .= " ( " . $this -> db -> encrypt ( $this -> const_name , 1 );
2011-06-09 02:05:08 +02:00
$sql .= " , " . $this -> db -> encrypt ( '1' , 1 );
$sql .= " ,0, " . $entity . " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::_active " , LOG_DEBUG );
2012-02-19 03:21:10 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) $err ++ ;
2011-06-09 02:05:08 +02:00
return $err ;
}
/**
* Remove activation line
2011-08-31 13:07:16 +02:00
*
2011-06-09 02:05:08 +02:00
* @ return int Nb of errors ( 0 if OK )
**/
function _unactive ()
{
global $conf ;
$err = 0 ;
// Common module
$entity = (( ! empty ( $this -> always_enabled ) || ! empty ( $this -> core_enabled )) ? 0 : $conf -> entity );
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " const " ;
$sql .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " = ' " . $this -> const_name . " ' " ;
2012-02-19 09:59:13 +01:00
$sql .= " AND entity IN (0, " . $entity . " ) " ;
2011-06-09 02:05:08 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::_unactive " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$this -> db -> query ( $sql );
return $err ;
}
/**
2012-03-13 09:38:00 +01:00
* Create tables and keys required by module .
* Files module . sql and module . key . sql with create table and create keys
* commands must be stored in directory reldir = '/module/sql/'
* This function is called by this -> init
2011-08-31 12:41:06 +02:00
*
2012-03-13 09:38:00 +01:00
* @ param string $reldir Relative directory where to scan files
* @ return int <= 0 if KO , > 0 if OK
2011-06-09 02:05:08 +02:00
*/
function _load_tables ( $reldir )
{
global $db , $conf ;
$error = 0 ;
2012-11-16 13:25:49 +01:00
$dirfound = 0 ;
if ( empty ( $reldir )) return 1 ;
2011-06-09 02:05:08 +02:00
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/admin.lib.php' ;
2013-10-26 16:18:28 +02:00
2011-06-09 02:05:08 +02:00
$ok = 1 ;
foreach ( $conf -> file -> dol_document_root as $dirroot )
{
if ( $ok )
{
$dir = $dirroot . $reldir ;
$ok = 0 ;
$handle =@ opendir ( $dir ); // Dir may not exists
if ( is_resource ( $handle ))
{
2012-11-16 13:25:49 +01:00
$dirfound ++ ;
2013-03-30 19:11:52 +01:00
2012-11-16 13:25:49 +01:00
// Run llx_mytable.sql files
while (( $file = readdir ( $handle )) !== false )
2011-06-09 02:05:08 +02:00
{
if ( preg_match ( '/\.sql$/i' , $file ) && ! preg_match ( '/\.key\.sql$/i' , $file ) && substr ( $file , 0 , 4 ) == 'llx_' && substr ( $file , 0 , 4 ) != 'data' )
{
$result = run_sql ( $dir . $file , 1 , '' , 1 );
if ( $result <= 0 ) $error ++ ;
}
}
2013-03-30 19:11:52 +01:00
2012-11-16 13:25:49 +01:00
rewinddir ( $handle );
2011-06-09 02:05:08 +02:00
2012-11-16 13:25:49 +01:00
// Run llx_mytable.key.sql files (Must be done after llx_mytable.sql)
while (( $file = readdir ( $handle )) !== false )
2011-06-09 02:05:08 +02:00
{
if ( preg_match ( '/\.key\.sql$/i' , $file ) && substr ( $file , 0 , 4 ) == 'llx_' && substr ( $file , 0 , 4 ) != 'data' )
{
$result = run_sql ( $dir . $file , 1 , '' , 1 );
if ( $result <= 0 ) $error ++ ;
}
}
2012-11-16 13:25:49 +01:00
rewinddir ( $handle );
2013-03-30 19:11:52 +01:00
2012-11-16 13:25:49 +01:00
// Run data_xxx.sql files (Must be done after llx_mytable.key.sql)
while (( $file = readdir ( $handle )) !== false )
2011-06-09 02:05:08 +02:00
{
if ( preg_match ( '/\.sql$/i' , $file ) && ! preg_match ( '/\.key\.sql$/i' , $file ) && substr ( $file , 0 , 4 ) == 'data' )
{
$result = run_sql ( $dir . $file , 1 , '' , 1 );
if ( $result <= 0 ) $error ++ ;
}
}
2013-03-30 19:11:52 +01:00
2012-11-16 13:25:49 +01:00
rewinddir ( $handle );
2013-03-30 19:11:52 +01:00
2012-11-16 13:25:49 +01:00
// Run update_xxx.sql files
while (( $file = readdir ( $handle )) !== false )
2011-06-09 02:05:08 +02:00
{
if ( preg_match ( '/\.sql$/i' , $file ) && ! preg_match ( '/\.key\.sql$/i' , $file ) && substr ( $file , 0 , 6 ) == 'update' )
{
$result = run_sql ( $dir . $file , 1 , '' , 1 );
if ( $result <= 0 ) $error ++ ;
}
}
2013-03-30 19:11:52 +01:00
2011-06-09 02:05:08 +02:00
closedir ( $handle );
}
if ( $error == 0 )
{
$ok = 1 ;
}
}
}
2012-11-16 13:25:49 +01:00
if ( ! $dirfound ) dol_syslog ( " A module ask to load sql files into " . $reldir . " but this directory was not found. " , LOG_WARNING );
2011-06-09 02:05:08 +02:00
return $ok ;
}
/**
2011-06-28 22:31:23 +02:00
* Insert boxes into llx_boxes_def
2011-08-31 13:07:16 +02:00
*
2013-10-26 16:18:28 +02:00
* @ param string $option String with options when disabling module ( 'newboxdefonly' = insert only boxes definition )
* @ return int Nb of errors ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
2013-10-26 16:18:28 +02:00
function insert_boxes ( $option = '' )
2011-06-09 02:05:08 +02:00
{
2013-04-23 16:18:26 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/infobox.class.php' ;
global $conf ;
2011-06-09 02:05:08 +02:00
$err = 0 ;
if ( is_array ( $this -> boxes ))
{
2014-08-01 00:53:29 +02:00
$pos_name = InfoBox :: getListOfPagesForBoxes ();
foreach ( $this -> boxes as $key => $value )
{
2013-04-23 16:18:26 +02:00
$file = isset ( $this -> boxes [ $key ][ 'file' ]) ? $this -> boxes [ $key ][ 'file' ] : '' ;
$note = isset ( $this -> boxes [ $key ][ 'note' ]) ? $this -> boxes [ $key ][ 'note' ] : '' ;
$enabledbydefaulton = isset ( $this -> boxes [ $key ][ 'enabledbydefaulton' ]) ? $this -> boxes [ $key ][ 'enabledbydefaulton' ] : 'Home' ;
if ( empty ( $file )) $file = isset ( $this -> boxes [ $key ][ 1 ]) ? $this -> boxes [ $key ][ 1 ] : '' ; // For backward compatibility
if ( empty ( $note )) $note = isset ( $this -> boxes [ $key ][ 2 ]) ? $this -> boxes [ $key ][ 2 ] : '' ; // For backward compatibility
2011-06-09 02:05:08 +02:00
2013-10-26 16:18:28 +02:00
// Search if boxes def already present
2012-03-17 14:29:48 +01:00
$sql = " SELECT count(*) as nb FROM " . MAIN_DB_PREFIX . " boxes_def " ;
$sql .= " WHERE file = ' " . $this -> db -> escape ( $file ) . " ' " ;
2011-06-09 02:05:08 +02:00
$sql .= " AND entity = " . $conf -> entity ;
if ( $note ) $sql .= " AND note =' " . $this -> db -> escape ( $note ) . " ' " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_boxes " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
2012-03-17 14:29:48 +01:00
$obj = $this -> db -> fetch_object ( $result );
if ( $obj -> nb == 0 )
2011-06-09 02:05:08 +02:00
{
2012-03-17 14:29:48 +01:00
$this -> db -> begin ();
if ( ! $err )
{
2013-10-26 16:18:28 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " boxes_def (file, entity, note) " ;
$sql .= " VALUES (' " . $this -> db -> escape ( $file ) . " ', " ;
$sql .= $conf -> entity . " , " ;
2012-03-17 14:29:48 +01:00
$sql .= $note ? " ' " . $this -> db -> escape ( $note ) . " ' " : " null " ;
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_boxes " , LOG_DEBUG );
2012-03-17 14:29:48 +01:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) $err ++ ;
}
2013-11-20 09:40:56 +01:00
if ( ! $err && ! preg_match ( '/newboxdefonly/' , $option ))
2012-03-17 14:29:48 +01:00
{
$lastid = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " boxes_def " , " rowid " );
2013-04-23 16:18:26 +02:00
foreach ( $pos_name as $key2 => $val2 )
{
//print 'key2='.$key2.'-val2='.$val2."<br>\n";
if ( $enabledbydefaulton && $val2 != $enabledbydefaulton ) continue ; // Not enabled by default onto this page.
2012-03-17 14:29:48 +01:00
2013-04-23 16:18:26 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " boxes (box_id,position,box_order,fk_user,entity) " ;
$sql .= " VALUES ( " . $lastid . " , " . $key2 . " , '0', 0, " . $conf -> entity . " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_boxes onto page " . $key2 . " = " . $val2 . " " , LOG_DEBUG );
2013-04-23 16:18:26 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) $err ++ ;
}
2012-03-17 14:29:48 +01:00
}
if ( ! $err )
{
$this -> db -> commit ();
}
else
2013-10-26 16:18:28 +02:00
{
2012-03-17 14:29:48 +01:00
$this -> error = $this -> db -> lasterror ();
$this -> db -> rollback ();
2011-06-09 02:05:08 +02:00
}
}
2013-04-23 16:18:26 +02:00
// else box already registered into database
2011-06-09 02:05:08 +02:00
}
else
2013-04-23 16:18:26 +02:00
{
2011-06-09 02:05:08 +02:00
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
}
}
return $err ;
}
/**
2011-06-28 22:31:23 +02:00
* Delete boxes
2011-08-31 13:07:16 +02:00
*
2011-06-28 22:31:23 +02:00
* @ return int Nb of errors ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
function delete_boxes ()
{
global $conf ;
$err = 0 ;
if ( is_array ( $this -> boxes ))
{
foreach ( $this -> boxes as $key => $value )
{
//$titre = $this->boxes[$key][0];
2013-05-31 13:21:01 +02:00
$file = $this -> boxes [ $key ][ 'file' ];
2011-06-09 02:05:08 +02:00
//$note = $this->boxes[$key][2];
2013-06-05 16:24:32 +02:00
if ( empty ( $file )) $file = isset ( $this -> boxes [ $key ][ 1 ]) ? $this -> boxes [ $key ][ 1 ] : '' ; // For backward compatibility
2013-10-26 16:18:28 +02:00
2011-06-09 02:05:08 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " boxes " ;
$sql .= " USING " . MAIN_DB_PREFIX . " boxes, " . MAIN_DB_PREFIX . " boxes_def " ;
$sql .= " WHERE " . MAIN_DB_PREFIX . " boxes.box_id = " . MAIN_DB_PREFIX . " boxes_def.rowid " ;
$sql .= " AND " . MAIN_DB_PREFIX . " boxes_def.file = ' " . $this -> db -> escape ( $file ) . " ' " ;
2012-09-07 17:16:47 +02:00
$sql .= " AND " . MAIN_DB_PREFIX . " boxes.entity = " . $conf -> entity ;
2011-06-09 02:05:08 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_boxes " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " boxes_def " ;
$sql .= " WHERE file = ' " . $this -> db -> escape ( $file ) . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_boxes " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
}
}
return $err ;
}
/**
2011-06-28 22:31:23 +02:00
* Remove links to new module page present in llx_const
2011-08-31 12:41:06 +02:00
*
2011-06-28 22:31:23 +02:00
* @ return int Nb of errors ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
function delete_tabs ()
{
global $conf ;
$err = 0 ;
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " const " ;
$sql .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " like ' " . $this -> const_name . " _TABS_%' " ;
$sql .= " AND entity = " . $conf -> entity ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_tabs " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
if ( ! $this -> db -> query ( $sql ))
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
return $err ;
}
2010-09-30 21:25:58 +02:00
/**
2011-06-28 22:31:23 +02:00
* Add links of new pages from modules in llx_const
2011-08-31 12:41:06 +02:00
*
2011-06-28 22:31:23 +02:00
* @ return int Number of errors ( 0 if ok )
2010-09-30 21:25:58 +02:00
*/
2011-06-09 02:05:08 +02:00
function insert_tabs ()
2010-09-30 21:25:58 +02:00
{
2011-06-09 02:05:08 +02:00
global $conf ;
2010-09-30 21:25:58 +02:00
2011-06-09 02:05:08 +02:00
$err = 0 ;
if ( ! empty ( $this -> tabs ))
2010-09-30 21:25:58 +02:00
{
2011-06-09 02:05:08 +02:00
$i = 0 ;
foreach ( $this -> tabs as $key => $value )
{
if ( $value )
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " const ( " ;
$sql .= " name " ;
$sql .= " , type " ;
$sql .= " , value " ;
$sql .= " , note " ;
$sql .= " , visible " ;
$sql .= " , entity " ;
$sql .= " ) " ;
$sql .= " VALUES ( " ;
$sql .= $this -> db -> encrypt ( $this -> const_name . " _TABS_ " . $i , 1 );
$sql .= " , 'chaine' " ;
$sql .= " , " . $this -> db -> encrypt ( $value , 1 );
$sql .= " , null " ;
$sql .= " , '0' " ;
$sql .= " , " . $conf -> entity ;
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_tabs " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$resql = $this -> db -> query ( $sql );
/* Allow duplicate key
if ( ! $resql )
{
$err ++ ;
}
*/
}
$i ++ ;
}
2010-09-30 21:25:58 +02:00
}
2011-06-09 02:05:08 +02:00
return $err ;
2010-09-30 21:25:58 +02:00
}
/**
2011-06-09 02:05:08 +02:00
* Insert constants defined into $this -> const array into table llx_const
2011-08-31 12:41:06 +02:00
*
2011-06-09 02:05:08 +02:00
* @ return int Number of errors ( 0 if OK )
*/
function insert_const ()
{
global $conf ;
2008-10-06 09:39:52 +02:00
2011-06-09 02:05:08 +02:00
$err = 0 ;
foreach ( $this -> const as $key => $value )
{
$name = $this -> const [ $key ][ 0 ];
$type = $this -> const [ $key ][ 1 ];
$val = $this -> const [ $key ][ 2 ];
$note = isset ( $this -> const [ $key ][ 3 ]) ? $this -> const [ $key ][ 3 ] : '' ;
$visible = isset ( $this -> const [ $key ][ 4 ]) ? $this -> const [ $key ][ 4 ] : 0 ;
$entity = ( ! empty ( $this -> const [ $key ][ 5 ]) && $this -> const [ $key ][ 5 ] != 'current' ) ? 0 : $conf -> entity ;
// Clean
if ( empty ( $visible )) $visible = '0' ;
if ( empty ( $val )) $val = '' ;
$sql = " SELECT count(*) " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " const " ;
$sql .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " = ' " . $name . " ' " ;
$sql .= " AND entity = " . $entity ;
$result = $this -> db -> query ( $sql );
if ( $result )
{
$row = $this -> db -> fetch_row ( $result );
if ( $row [ 0 ] == 0 ) // If not found
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " const (name,type,value,note,visible,entity) " ;
$sql .= " VALUES ( " ;
$sql .= $this -> db -> encrypt ( $name , 1 );
$sql .= " ,' " . $type . " ' " ;
$sql .= " , " . ( $val ? $this -> db -> encrypt ( $val , 1 ) : " '' " );
$sql .= " , " . ( $note ? " ' " . $this -> db -> escape ( $note ) . " ' " : " null " );
$sql .= " ,' " . $visible . " ' " ;
$sql .= " , " . $entity ;
$sql .= " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_const " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
if ( ! $this -> db -> query ( $sql ) )
{
$err ++ ;
}
}
else
{
2011-07-17 20:13:44 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_const constant ' " . $name . " ' already exists " , LOG_WARNING );
2011-06-09 02:05:08 +02:00
}
}
else
{
$err ++ ;
}
}
return $err ;
}
2005-04-09 17:52:07 +02:00
2011-01-30 17:05:57 +01:00
/**
* Remove constants with tags deleteonunactive
2011-08-31 12:41:06 +02:00
*
2011-01-30 17:05:57 +01:00
* @ return int < 0 if KO , 0 if OK
*/
function delete_const ()
{
global $conf ;
$err = 0 ;
foreach ( $this -> const as $key => $value )
{
$name = $this -> const [ $key ][ 0 ];
$deleteonunactive = ( ! empty ( $this -> const [ $key ][ 6 ])) ? 1 : 0 ;
if ( $deleteonunactive )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " const " ;
$sql .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " = ' " . $name . " ' " ;
$sql .= " AND entity in (0, " . $conf -> entity . " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_const " , LOG_DEBUG );
2011-01-30 17:05:57 +01:00
if ( ! $this -> db -> query ( $sql ))
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
}
}
return $err ;
}
2011-06-09 02:05:08 +02:00
/**
2011-06-28 22:31:23 +02:00
* Insert permissions definitions related to the module into llx_rights_def
2011-08-31 12:41:06 +02:00
*
2012-03-13 09:38:00 +01:00
* @ param int $reinitadminperms If 1 , we also grant them to all admin users
2012-07-06 20:25:00 +02:00
* @ param int $force_entity Force current entity
2012-03-13 09:38:00 +01:00
* @ return int Number of error ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
2012-07-06 20:25:00 +02:00
function insert_permissions ( $reinitadminperms = 0 , $force_entity = null )
2011-06-09 02:05:08 +02:00
{
global $conf , $user ;
$err = 0 ;
2012-07-06 20:25:00 +02:00
$entity = ( ! empty ( $force_entity ) ? $force_entity : $conf -> entity );
2011-06-09 02:05:08 +02:00
2011-07-17 20:13:44 +02:00
// Test if module is activated
2011-06-09 02:05:08 +02:00
$sql_del = " SELECT " . $this -> db -> decrypt ( 'value' ) . " as value " ;
$sql_del .= " FROM " . MAIN_DB_PREFIX . " const " ;
$sql_del .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " = ' " . $this -> const_name . " ' " ;
2012-07-06 20:25:00 +02:00
$sql_del .= " AND entity IN (0, " . $entity . " ) " ;
2011-06-09 02:05:08 +02:00
2014-06-12 13:49:05 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_permissions " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$resql = $this -> db -> query ( $sql_del );
2014-07-27 23:52:42 +02:00
2011-06-09 02:05:08 +02:00
if ( $resql )
{
$obj = $this -> db -> fetch_object ( $resql );
2012-07-10 13:20:53 +02:00
if ( ! empty ( $obj -> value ) && ! empty ( $this -> rights ))
2011-06-09 02:05:08 +02:00
{
// Si module actif
foreach ( $this -> rights as $key => $value )
{
$r_id = $this -> rights [ $key ][ 0 ];
$r_desc = $this -> rights [ $key ][ 1 ];
2011-07-13 23:54:07 +02:00
$r_type = isset ( $this -> rights [ $key ][ 2 ]) ? $this -> rights [ $key ][ 2 ] : '' ;
2011-06-09 02:05:08 +02:00
$r_def = $this -> rights [ $key ][ 3 ];
$r_perms = $this -> rights [ $key ][ 4 ];
$r_subperms = isset ( $this -> rights [ $key ][ 5 ]) ? $this -> rights [ $key ][ 5 ] : '' ;
$r_modul = $this -> rights_class ;
if ( empty ( $r_type )) $r_type = 'w' ;
2014-07-27 23:52:42 +02:00
// Search if perm already present
$sql = " SELECT count(*) as nb FROM " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " WHERE id = " . $r_id . " AND entity = " . $entity ;
$resqlselect = $this -> db -> query ( $sql );
2011-06-09 02:05:08 +02:00
2014-07-27 23:52:42 +02:00
$obj = $this -> db -> fetch_object ( $resqlselect );
if ( $obj -> nb == 0 )
2011-06-09 02:05:08 +02:00
{
2014-07-27 23:52:42 +02:00
if ( dol_strlen ( $r_perms ) )
{
if ( dol_strlen ( $r_subperms ) )
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " (id, entity, libelle, module, type, bydefault, perms, subperms) " ;
$sql .= " VALUES " ;
$sql .= " ( " . $r_id . " , " . $entity . " ,' " . $this -> db -> escape ( $r_desc ) . " ',' " . $r_modul . " ',' " . $r_type . " ', " . $r_def . " ,' " . $r_perms . " ',' " . $r_subperms . " ') " ;
}
else
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " (id, entity, libelle, module, type, bydefault, perms) " ;
$sql .= " VALUES " ;
$sql .= " ( " . $r_id . " , " . $entity . " ,' " . $this -> db -> escape ( $r_desc ) . " ',' " . $r_modul . " ',' " . $r_type . " ', " . $r_def . " ,' " . $r_perms . " ') " ;
}
}
else
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " (id, entity, libelle, module, type, bydefault) " ;
$sql .= " VALUES " ;
$sql .= " ( " . $r_id . " , " . $entity . " ,' " . $this -> db -> escape ( $r_desc ) . " ',' " . $r_modul . " ',' " . $r_type . " ', " . $r_def . " ) " ;
}
$resqlinsert = $this -> db -> query ( $sql , 1 );
if ( ! $resqlinsert )
{
if ( $this -> db -> errno () != " DB_ERROR_RECORD_ALREADY_EXISTS " )
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
break ;
}
else dol_syslog ( get_class ( $this ) . " ::insert_permissions record already exists " , LOG_INFO );
}
$this -> db -> free ( $resqlinsert );
2011-06-09 02:05:08 +02:00
}
2014-07-27 23:52:42 +02:00
$this -> db -> free ( $resqlselect );
2011-06-09 02:05:08 +02:00
2011-07-17 20:13:44 +02:00
// If we want to init permissions on admin users
2011-07-14 00:15:19 +02:00
if ( $reinitadminperms )
2011-06-09 02:05:08 +02:00
{
2012-09-27 11:54:51 +02:00
if ( ! class_exists ( 'User' )) {
require DOL_DOCUMENT_ROOT . '/user/class/user.class.php' ;
}
2012-07-06 20:25:00 +02:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " user WHERE admin = 1 " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_permissions Search all admin users " , LOG_DEBUG );
2011-07-17 20:13:44 +02:00
$resqlseladmin = $this -> db -> query ( $sql , 1 );
if ( $resqlseladmin )
2011-07-14 00:15:19 +02:00
{
2011-07-17 20:13:44 +02:00
$num = $this -> db -> num_rows ( $resqlseladmin );
$i = 0 ;
while ( $i < $num )
{
$obj2 = $this -> db -> fetch_object ( $resqlseladmin );
dol_syslog ( get_class ( $this ) . " ::insert_permissions Add permission to user id= " . $obj2 -> rowid );
$tmpuser = new User ( $this -> db );
$tmpuser -> fetch ( $obj2 -> rowid );
2013-05-03 20:56:58 +02:00
if ( ! empty ( $tmpuser -> id )) {
$tmpuser -> addrights ( $r_id );
}
2011-07-17 20:13:44 +02:00
$i ++ ;
}
if ( ! empty ( $user -> admin )) // Reload permission for current user if defined
{
// We reload permissions
$user -> clearrights ();
$user -> getrights ();
}
2011-07-14 00:15:19 +02:00
}
2011-07-17 20:13:44 +02:00
else dol_print_error ( $this -> db );
2011-06-09 02:05:08 +02:00
}
}
}
2011-07-17 20:13:44 +02:00
$this -> db -> free ( $resql );
2011-06-09 02:05:08 +02:00
}
else
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
return $err ;
}
/**
2011-06-28 22:31:23 +02:00
* Delete permissions
2011-08-31 12:41:06 +02:00
*
2011-06-28 22:31:23 +02:00
* @ return int Nb of errors ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
function delete_permissions ()
{
global $conf ;
$err = 0 ;
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " rights_def " ;
$sql .= " WHERE module = ' " . $this -> rights_class . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_permissions " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
if ( ! $this -> db -> query ( $sql ))
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
return $err ;
}
/**
* Insert menus entries found into $this -> menu into llx_menu *
2011-08-31 13:07:16 +02:00
*
2011-06-09 02:05:08 +02:00
* @ return int Nb of errors ( 0 if OK )
*/
function insert_menus ()
{
2011-12-12 00:07:28 +01:00
global $user ;
2011-12-30 23:07:27 +01:00
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/menubase.class.php' ;
2011-06-09 02:05:08 +02:00
$err = 0 ;
$this -> db -> begin ();
//var_dump($this->menu); exit;
foreach ( $this -> menu as $key => $value )
{
$menu = new Menubase ( $this -> db );
$menu -> menu_handler = 'all' ;
$menu -> module = $this -> rights_class ;
if ( ! $this -> menu [ $key ][ 'fk_menu' ])
{
$menu -> fk_menu = 0 ;
//print 'aaa'.$this->menu[$key]['fk_menu'];
}
else
{
//print 'xxx'.$this->menu[$key]['fk_menu'];exit;
$foundparent = 0 ;
$fk_parent = $this -> menu [ $key ][ 'fk_menu' ];
2013-03-30 19:11:52 +01:00
if ( preg_match ( '/^r=/' , $fk_parent )) // old deprecated method
2011-06-09 02:05:08 +02:00
{
$fk_parent = str_replace ( 'r=' , '' , $fk_parent );
if ( isset ( $this -> menu [ $fk_parent ][ 'rowid' ]))
{
$menu -> fk_menu = $this -> menu [ $fk_parent ][ 'rowid' ];
2011-04-06 13:11:32 +02:00
$foundparent = 1 ;
2011-06-09 02:05:08 +02:00
}
}
2013-03-30 19:11:52 +01:00
elseif ( preg_match ( '/^fk_mainmenu=([a-zA-Z0-9_]+),fk_leftmenu=([a-zA-Z0-9_]+)$/' , $fk_parent , $reg ))
2011-06-09 02:05:08 +02:00
{
$menu -> fk_menu =- 1 ;
$menu -> fk_mainmenu = $reg [ 1 ];
$menu -> fk_leftmenu = $reg [ 2 ];
2011-04-06 13:11:32 +02:00
$foundparent = 1 ;
2011-06-09 02:05:08 +02:00
}
2013-03-30 19:11:52 +01:00
elseif ( preg_match ( '/^fk_mainmenu=([a-zA-Z0-9_]+)$/' , $fk_parent , $reg ))
2011-12-31 01:33:12 +01:00
{
$menu -> fk_menu =- 1 ;
$menu -> fk_mainmenu = $reg [ 1 ];
$menu -> fk_leftmenu = '' ;
$foundparent = 1 ;
}
2011-06-09 02:05:08 +02:00
if ( ! $foundparent )
{
$this -> error = " ErrorBadDefinitionOfMenuArrayInModuleDescriptor (bad value for key fk_menu) " ;
2011-07-17 20:13:44 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_menus " . $this -> error . " " . $this -> menu [ $key ][ 'fk_menu' ], LOG_ERR );
2011-06-09 02:05:08 +02:00
$err ++ ;
}
}
$menu -> type = $this -> menu [ $key ][ 'type' ];
2012-01-16 12:09:44 +01:00
$menu -> mainmenu = isset ( $this -> menu [ $key ][ 'mainmenu' ]) ? $this -> menu [ $key ][ 'mainmenu' ] : ( isset ( $menu -> fk_mainmenu ) ? $menu -> fk_mainmenu : '' );
2011-12-30 23:07:27 +01:00
$menu -> leftmenu = isset ( $this -> menu [ $key ][ 'leftmenu' ]) ? $this -> menu [ $key ][ 'leftmenu' ] : '' ;
2011-06-09 02:05:08 +02:00
$menu -> titre = $this -> menu [ $key ][ 'titre' ];
$menu -> url = $this -> menu [ $key ][ 'url' ];
$menu -> langs = $this -> menu [ $key ][ 'langs' ];
$menu -> position = $this -> menu [ $key ][ 'position' ];
$menu -> perms = $this -> menu [ $key ][ 'perms' ];
$menu -> target = $this -> menu [ $key ][ 'target' ];
$menu -> user = $this -> menu [ $key ][ 'user' ];
$menu -> enabled = isset ( $this -> menu [ $key ][ 'enabled' ]) ? $this -> menu [ $key ][ 'enabled' ] : 0 ;
if ( ! $err )
{
2011-12-12 00:07:28 +01:00
$result = $menu -> create ( $user );
2011-06-09 02:05:08 +02:00
if ( $result > 0 )
{
$this -> menu [ $key ][ 'rowid' ] = $result ;
}
else
{
$this -> error = $menu -> error ;
2011-12-30 23:07:27 +01:00
dol_syslog ( get_class ( $this ) . '::insert_menus result=' . $result . " " . $this -> error , LOG_ERR );
2011-06-09 02:05:08 +02:00
$err ++ ;
break ;
}
}
}
if ( ! $err )
{
$this -> db -> commit ();
}
else
{
2011-07-17 20:13:44 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_menus " . $this -> error , LOG_ERR );
2011-06-09 02:05:08 +02:00
$this -> db -> rollback ();
}
return $err ;
}
/**
2011-06-28 22:31:23 +02:00
* Remove menus entries
2011-08-31 12:41:06 +02:00
*
2011-06-28 22:31:23 +02:00
* @ return int Nb of errors ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
function delete_menus ()
{
global $conf ;
$err = 0 ;
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " menu " ;
$sql .= " WHERE module = ' " . $this -> db -> escape ( $this -> rights_class ) . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_menus " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
return $err ;
}
/**
2011-06-28 22:31:23 +02:00
* Create directories required by module
2011-08-31 12:41:06 +02:00
*
2011-06-28 22:31:23 +02:00
* @ return int Nb of errors ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
function create_dirs ()
{
global $langs , $conf ;
$err = 0 ;
2012-09-03 10:17:58 +02:00
if ( isset ( $this -> dirs ) && is_array ( $this -> dirs ))
2011-06-09 02:05:08 +02:00
{
foreach ( $this -> dirs as $key => $value )
{
$addtodatabase = 0 ;
if ( ! is_array ( $value )) $dir = $value ; // Default simple mode
else {
$constname = $this -> const_name . " _DIR_ " ;
$dir = $this -> dirs [ $key ][ 1 ];
$addtodatabase = empty ( $this -> dirs [ $key ][ 2 ]) ? '' : $this -> dirs [ $key ][ 2 ]; // Create constante in llx_const
$subname = empty ( $this -> dirs [ $key ][ 3 ]) ? '' : strtoupper ( $this -> dirs [ $key ][ 3 ]); // Add submodule name (ex: $conf->module->submodule->dir_output)
$forcename = empty ( $this -> dirs [ $key ][ 4 ]) ? '' : strtoupper ( $this -> dirs [ $key ][ 4 ]); // Change the module name if different
2012-09-02 22:48:52 +02:00
if ( ! empty ( $forcename )) $constname = 'MAIN_MODULE_' . $forcename . " _DIR_ " ;
if ( ! empty ( $subname )) $constname = $constname . $subname . " _ " ;
2011-06-09 02:05:08 +02:00
2012-09-02 22:48:52 +02:00
$name = $constname . strtoupper ( $this -> dirs [ $key ][ 0 ]);
2011-06-09 02:05:08 +02:00
}
// Define directory full path ($dir must start with "/")
if ( empty ( $conf -> global -> MAIN_MODULE_MULTICOMPANY ) || $conf -> entity == 1 ) $fulldir = DOL_DATA_ROOT . $dir ;
else $fulldir = DOL_DATA_ROOT . " / " . $conf -> entity . $dir ;
// Create dir if it does not exists
2012-09-02 22:48:52 +02:00
if ( ! empty ( $fulldir ) && ! file_exists ( $fulldir ))
2011-06-09 02:05:08 +02:00
{
2012-09-02 22:48:52 +02:00
if ( dol_mkdir ( $fulldir , DOL_DATA_ROOT ) < 0 )
2011-06-09 02:05:08 +02:00
{
$this -> error = $langs -> trans ( " ErrorCanNotCreateDir " , $fulldir );
2011-07-17 20:13:44 +02:00
dol_syslog ( get_class ( $this ) . " ::_init " . $this -> error , LOG_ERR );
2011-06-09 02:05:08 +02:00
$err ++ ;
}
}
// Define the constant in database if requested (not the default mode)
2012-09-02 22:48:52 +02:00
if ( ! empty ( $addtodatabase ))
2011-06-09 02:05:08 +02:00
{
2012-09-02 22:48:52 +02:00
$result = $this -> insert_dirs ( $name , $dir );
2011-06-09 02:05:08 +02:00
if ( $result ) $err ++ ;
}
}
}
return $err ;
}
/**
2011-06-28 22:31:23 +02:00
* Insert directories in llx_const
2011-08-31 12:41:06 +02:00
*
2012-03-13 09:38:00 +01:00
* @ param string $name Name
* @ param string $dir Directory
* @ return int Nb of errors ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
function insert_dirs ( $name , $dir )
{
global $conf ;
$err = 0 ;
$sql = " SELECT count(*) " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " const " ;
$sql .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " = ' " . $name . " ' " ;
$sql .= " AND entity = " . $conf -> entity ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_dirs " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$result = $this -> db -> query ( $sql );
if ( $result )
{
$row = $this -> db -> fetch_row ( $result );
if ( $row [ 0 ] == 0 )
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " const (name,type,value,note,visible,entity) " ;
$sql .= " VALUES ( " . $this -> db -> encrypt ( $name , 1 ) . " ,'chaine', " . $this -> db -> encrypt ( $dir , 1 ) . " ,'Directory for module " . $this -> name . " ','0', " . $conf -> entity . " ) " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_dirs " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
$resql = $this -> db -> query ( $sql );
}
}
else
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
return $err ;
}
/**
2011-06-28 22:31:23 +02:00
* Remove directory entries
2011-08-31 12:41:06 +02:00
*
2011-06-28 22:31:23 +02:00
* @ return int Nb of errors ( 0 if OK )
2011-06-09 02:05:08 +02:00
*/
function delete_dirs ()
{
global $conf ;
$err = 0 ;
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " const " ;
2012-09-03 21:00:46 +02:00
$sql .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " LIKE ' " . $this -> const_name . " _DIR_%' " ;
2011-06-09 02:05:08 +02:00
$sql .= " AND entity = " . $conf -> entity ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_dirs " , LOG_DEBUG );
2011-06-09 02:05:08 +02:00
if ( ! $this -> db -> query ( $sql ))
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
return $err ;
}
2012-02-23 15:42:03 +01:00
/**
* Insert activation of generic parts from modules in llx_const
2012-08-08 20:43:23 +02:00
* Input entry use $this -> module_parts = array (
2013-01-08 13:45:52 +01:00
* 'triggers' => 0 , // Set this to 1 if module has its own trigger directory (/mymodule/core/triggers)
* 'login' => 0 , // Set this to 1 if module has its own login method directory (/mymodule/core/login)
* 'substitutions' => 0 , // Set this to 1 if module has its own substitution function file (/mymodule/core/substitutions)
* 'menus' => 0 , // Set this to 1 if module has its own menus handler directory (/mymodule/core/menus)
* 'theme' => 0 , // Set this to 1 if module has its own theme directory (/mymodule/theme)
* 'tpl' => 0 , // Set this to 1 if module overwrite template dir (/mymodule/core/tpl)
* 'barcode' => 0 , // Set this to 1 if module has its own barcode directory (/mymodule/core/modules/barcode)
* 'models' => 0 , // Set this to 1 if module has its own models directory (/mymodule/core/modules/xxx)
2012-08-08 20:43:23 +02:00
* 'css' => '/mymodule/css/mymodule.css.php' , // Set this to relative path of css file if module has its own css file
* 'js' => '/mymodule/js/mymodule.js' , // Set this to relative path of js file if module must load a js on all pages
* 'hooks' => array ( 'hookcontext1' , 'hookcontext2' ) // Set here all hooks context managed by module
* 'workflow' => array ( 'WORKFLOW_MODULE1_YOURACTIONTYPE_MODULE2' => array ( 'enabled' => '! empty($conf->module1->enabled) && ! empty($conf->module2->enabled)' , 'picto' => 'yourpicto@mymodule' ) // Set here all workflow context managed by module
* )
2012-02-24 11:06:05 +01:00
*
2012-02-23 15:42:03 +01:00
* @ return int Nb of errors ( 0 if OK )
*/
function insert_module_parts ()
{
global $conf ;
2012-02-24 11:06:05 +01:00
2012-03-21 13:50:59 +01:00
$error = 0 ;
2012-02-24 08:52:30 +01:00
$entity = $conf -> entity ;
2012-02-24 11:06:05 +01:00
2012-02-27 09:09:34 +01:00
if ( is_array ( $this -> module_parts ) && ! empty ( $this -> module_parts ))
2012-02-23 15:42:03 +01:00
{
foreach ( $this -> module_parts as $key => $value )
{
2013-05-01 16:00:06 +02:00
if ( is_array ( $value ) && count ( $value ) == 0 ) continue ; // Discard empty arrays
2012-02-24 08:52:30 +01:00
$newvalue = $value ;
2012-02-24 11:06:05 +01:00
2012-02-23 19:51:56 +01:00
// Serialize array parameters
2012-02-24 08:52:30 +01:00
if ( is_array ( $value ))
{
// Can defined other parameters
if ( is_array ( $value [ 'data' ]) && ! empty ( $value [ 'data' ]))
{
2012-03-18 19:59:38 +01:00
$newvalue = json_encode ( $value [ 'data' ]);
2012-02-24 08:52:30 +01:00
if ( isset ( $value [ 'entity' ])) $entity = $value [ 'entity' ];
}
else
{
2012-03-18 19:59:38 +01:00
$newvalue = json_encode ( $value );
2012-02-24 08:52:30 +01:00
}
}
2012-02-24 11:06:05 +01:00
2012-02-23 15:42:03 +01:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " const ( " ;
$sql .= " name " ;
$sql .= " , type " ;
$sql .= " , value " ;
$sql .= " , note " ;
$sql .= " , visible " ;
$sql .= " , entity " ;
$sql .= " ) " ;
$sql .= " VALUES ( " ;
2012-02-24 08:52:30 +01:00
$sql .= $this -> db -> encrypt ( $this -> const_name . " _ " . strtoupper ( $key ), 1 );
2012-02-23 15:42:03 +01:00
$sql .= " , 'chaine' " ;
2012-02-24 08:52:30 +01:00
$sql .= " , " . $this -> db -> encrypt ( $newvalue , 1 );
2012-02-23 15:42:03 +01:00
$sql .= " , null " ;
$sql .= " , '0' " ;
2012-02-24 08:52:30 +01:00
$sql .= " , " . $entity ;
2012-02-23 15:42:03 +01:00
$sql .= " ) " ;
2012-02-24 11:06:05 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::insert_const_ " . $key . " " , LOG_DEBUG );
2012-03-21 13:50:59 +01:00
$resql = $this -> db -> query ( $sql , 1 );
2012-02-23 15:42:03 +01:00
if ( ! $resql )
{
2012-03-21 13:50:59 +01:00
if ( $this -> db -> lasterrno () != 'DB_ERROR_RECORD_ALREADY_EXISTS' )
{
$error ++ ;
$this -> error = $this -> db -> lasterror ();
}
else
{
dol_syslog ( get_class ( $this ) . " ::insert_const_ " . $key . " Record already exists. " , LOG_WARNING );
}
2012-02-23 15:42:03 +01:00
}
}
}
2012-03-21 13:50:59 +01:00
return $error ;
2012-02-23 15:42:03 +01:00
}
2012-02-24 11:06:05 +01:00
2012-02-23 15:42:03 +01:00
/**
2012-02-24 11:06:05 +01:00
* Remove activation of generic parts of modules from llx_const
*
2012-02-23 15:42:03 +01:00
* @ return int Nb of errors ( 0 if OK )
*/
function delete_module_parts ()
{
global $conf ;
2012-02-24 11:06:05 +01:00
2012-02-23 15:42:03 +01:00
$err = 0 ;
2012-03-03 20:19:21 +01:00
$entity = $conf -> entity ;
2012-02-24 11:06:05 +01:00
2012-02-27 09:09:34 +01:00
if ( is_array ( $this -> module_parts ) && ! empty ( $this -> module_parts ))
2012-02-23 15:42:03 +01:00
{
foreach ( $this -> module_parts as $key => $value )
{
2012-07-02 19:30:37 +02:00
// If entity is defined
2012-03-03 20:19:21 +01:00
if ( is_array ( $value ) && isset ( $value [ 'entity' ])) $entity = $value [ 'entity' ];
2012-03-13 09:38:00 +01:00
2012-02-23 15:42:03 +01:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " const " ;
$sql .= " WHERE " . $this -> db -> decrypt ( 'name' ) . " LIKE ' " . $this -> const_name . " _ " . strtoupper ( $key ) . " ' " ;
2012-03-03 20:19:21 +01:00
$sql .= " AND entity = " . $entity ;
2012-02-24 11:06:05 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete_const_ " . $key . " " , LOG_DEBUG );
2012-02-23 15:42:03 +01:00
if ( ! $this -> db -> query ( $sql ))
{
$this -> error = $this -> db -> lasterror ();
$err ++ ;
}
}
}
2012-02-24 11:06:05 +01:00
2012-02-23 15:42:03 +01:00
return $err ;
}
2009-04-29 23:39:31 +02:00
2003-11-15 18:41:07 +01:00
}