-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
DolibarrModules.class.php
1375 lines (1170 loc) · 46.2 KB
/
DolibarrModules.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* 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>
* Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \file htdocs/core/modules/DolibarrModules.class.php
* \brief File of parent class of module descriptor class files
*/
/**
* Parent class of module descriptor class files
*/
abstract class DolibarrModules
{
//! Database handler
var $db;
//! Relative path to module style sheet
var $style_sheet = ''; // deprecated
//! 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();
//! Module parts array
var $module_parts=array();
//! Tableau des documents ???
var $docs;
var $dbversion = "-";
/**
* Method to enable a module. Insert into database all constants, boxes of module
*
* @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
*/
function _init($array_sql, $options='')
{
global $conf, $langs;
$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();
// Insert activation of module's parts
if (! $err) $err+=$this->insert_module_parts();
// 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
if (! $err && ! preg_match('/noboxes/',$options)) $err+=$this->insert_boxes($options);
// 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);
// Insert specific menus entries into database
if (! $err) $err+=$this->insert_menus();
// Create module's directories
if (! $err) $err+=$this->create_dirs();
// Execute addons requests
$num=count($array_sql);
for ($i = 0; $i < $num; $i++)
{
if (! $err)
{
$val=$array_sql[$i];
$sql='';
$ignoreerror=0;
if (is_array($val))
{
$sql=$val['sql'];
$ignoreerror=$val['ignoreerror'];
}
else
{
$sql=$val;
}
// Add current entity id
$sql=str_replace('__ENTITY__', $conf->entity, $sql);
dol_syslog(get_class($this)."::_init ignoreerror=".$ignoreerror."", LOG_DEBUG);
$result=$this->db->query($sql, $ignoreerror);
if (! $result)
{
if (! $ignoreerror)
{
$this->error=$this->db->lasterror();
$err++;
}
else
{
dol_syslog(get_class($this)."::_init Warning ".$this->db->lasterror(), LOG_WARNING);
}
}
}
}
// 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
*
* @param array $array_sql Array of SQL requests to execute when disable module
* @param string $options String with options when disabling module ('newboxdefonly|noboxes')
* @return int 1 if OK, 0 if KO
*/
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();
// Remove activation of module's parts (MAIN_MODULE_MYMODULE_XXX in llx_const)
if (! $err) $err+=$this->delete_module_parts();
// Remove constants defined by modules
if (! $err) $err+=$this->delete_const();
// 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 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
$num=count($array_sql);
for ($i = 0; $i < $num; $i++)
{
if (! $err)
{
dol_syslog(get_class($this)."::_remove", LOG_DEBUG);
$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;
}
}
/**
* Retourne le nom traduit du module si la traduction existe dans admin.lang,
* sinon le nom defini par defaut dans le module.
*
* @return string Nom du module traduit
*/
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;
}
}
/**
* Retourne la description traduite du module si la traduction existe dans admin.lang,
* sinon la description definie par defaut dans le module
*
* @return string Nom du module traduit
*/
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;
}
}
/**
* Return module version.
* 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
*
* @return string Version du module
*/
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");
}
/**
* 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';
}
/**
* Return list of lang files related to module
*
* @return array Array of lang files
*/
function getLangFilesArray()
{
return $this->langfiles;
}
/**
* Return translated label of a export dataset
*
* @param int $r Index of dataset
* @return string Label of databaset
*/
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
*
* @param int $r Index of dataset
* @return string Label of databaset
*/
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
*
* @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";
$sql.= " WHERE ".$this->db->decrypt('name')." = '".$this->const_name."'";
$sql.= " AND entity IN (0, ".$entity.")";
dol_syslog(get_class($this)."::_active", LOG_DEBUG);
$resql=$this->db->query($sql);
if (! $resql) $err++;
$sql = "INSERT INTO ".MAIN_DB_PREFIX."const (name,value,visible,entity) VALUES";
$sql.= " (".$this->db->encrypt($this->const_name,1);
$sql.= ",".$this->db->encrypt('1',1);
$sql.= ",0,".$entity.")";
dol_syslog(get_class($this)."::_active", LOG_DEBUG);
$resql=$this->db->query($sql);
if (! $resql) $err++;
return $err;
}
/**
* Remove activation line
*
* @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."'";
$sql.= " AND entity IN (0, ".$entity.")";
dol_syslog(get_class($this)."::_unactive", LOG_DEBUG);
$this->db->query($sql);
return $err;
}
/**
* 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
*
* @param string $reldir Relative directory where to scan files
* @return int <=0 if KO, >0 if OK
*/
function _load_tables($reldir)
{
global $db,$conf;
$error=0;
$dirfound=0;
if (empty($reldir)) return 1;
include_once DOL_DOCUMENT_ROOT .'/core/lib/admin.lib.php';
$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))
{
$dirfound++;
// Run llx_mytable.sql files
while (($file = readdir($handle))!==false)
{
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++;
}
}
rewinddir($handle);
// Run llx_mytable.key.sql files (Must be done after llx_mytable.sql)
while (($file = readdir($handle))!==false)
{
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++;
}
}
rewinddir($handle);
// Run data_xxx.sql files (Must be done after llx_mytable.key.sql)
while (($file = readdir($handle))!==false)
{
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++;
}
}
rewinddir($handle);
// Run update_xxx.sql files
while (($file = readdir($handle))!==false)
{
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++;
}
}
closedir($handle);
}
if ($error == 0)
{
$ok = 1;
}
}
}
if (! $dirfound) dol_syslog("A module ask to load sql files into ".$reldir." but this directory was not found.", LOG_WARNING);
return $ok;
}
/**
* Insert boxes into llx_boxes_def
*
* @param string $option String with options when disabling module ('newboxdefonly'=insert only boxes definition)
* @return int Nb of errors (0 if OK)
*/
function insert_boxes($option='')
{
require_once DOL_DOCUMENT_ROOT.'/core/class/infobox.class.php';
global $conf;
$err=0;
if (is_array($this->boxes))
{
$pos_name = InfoBox::getListOfPagesForBoxes();
foreach ($this->boxes as $key => $value)
{
$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
// Search if boxes def already present
$sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."boxes_def";
$sql.= " WHERE file = '".$this->db->escape($file)."'";
$sql.= " AND entity = ".$conf->entity;
if ($note) $sql.=" AND note ='".$this->db->escape($note)."'";
dol_syslog(get_class($this)."::insert_boxes", 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."boxes_def (file, entity, note)";
$sql.= " VALUES ('".$this->db->escape($file)."', ";
$sql.= $conf->entity.", ";
$sql.= $note?"'".$this->db->escape($note)."'":"null";
$sql.= ")";
dol_syslog(get_class($this)."::insert_boxes", LOG_DEBUG);
$resql=$this->db->query($sql);
if (! $resql) $err++;
}
if (! $err && ! preg_match('/newboxdefonly/',$option))
{
$lastid=$this->db->last_insert_id(MAIN_DB_PREFIX."boxes_def","rowid");
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.
$sql = "INSERT INTO ".MAIN_DB_PREFIX."boxes (box_id,position,box_order,fk_user,entity)";
$sql.= " VALUES (".$lastid.", ".$key2.", '0', 0, ".$conf->entity.")";
dol_syslog(get_class($this)."::insert_boxes onto page ".$key2."=".$val2."", 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;
}
/**
* Delete boxes
*
* @return int Nb of errors (0 if OK)
*/
function delete_boxes()
{
global $conf;
$err=0;
if (is_array($this->boxes))
{
foreach ($this->boxes as $key => $value)
{
//$titre = $this->boxes[$key][0];
$file = $this->boxes[$key]['file'];
//$note = $this->boxes[$key][2];
if (empty($file)) $file = isset($this->boxes[$key][1])?$this->boxes[$key][1]:''; // For backward compatibility
$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)."'";
$sql.= " AND ".MAIN_DB_PREFIX."boxes.entity = ".$conf->entity;
dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
$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;
dol_syslog(get_class($this)."::delete_boxes", LOG_DEBUG);
$resql=$this->db->query($sql);
if (! $resql)
{
$this->error=$this->db->lasterror();
$err++;
}
}
}
return $err;
}
/**
* Remove links to new module page present in llx_const
*
* @return int Nb of errors (0 if OK)
*/
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;
dol_syslog(get_class($this)."::delete_tabs", LOG_DEBUG);
if (! $this->db->query($sql))
{
$this->error=$this->db->lasterror();
$err++;
}
return $err;
}
/**
* Add links of new pages from modules in llx_const
*
* @return int Number of errors (0 if ok)
*/
function insert_tabs()
{
global $conf;
$err=0;
if (! empty($this->tabs))
{
$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.= ")";
dol_syslog(get_class($this)."::insert_tabs", LOG_DEBUG);
$resql=$this->db->query($sql);
/* Allow duplicate key
if (! $resql)
{
$err++;
}
*/
}
$i++;
}
}
return $err;
}
/**
* Insert constants defined into $this->const array into table llx_const
*
* @return int Number of errors (0 if OK)
*/
function insert_const()
{
global $conf;
$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.= ")";
dol_syslog(get_class($this)."::insert_const", LOG_DEBUG);
if (! $this->db->query($sql) )
{
$err++;
}
}
else
{
dol_syslog(get_class($this)."::insert_const constant '".$name."' already exists", LOG_WARNING);
}
}
else
{
$err++;
}
}
return $err;
}
/**
* Remove constants with tags deleteonunactive
*
* @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.")";
dol_syslog(get_class($this)."::delete_const", LOG_DEBUG);
if (! $this->db->query($sql))
{
$this->error=$this->db->lasterror();
$err++;
}
}
}
return $err;
}
/**
* Insert permissions definitions related to the module into llx_rights_def
*
* @param int $reinitadminperms If 1, we also grant them to all admin users
* @param int $force_entity Force current entity
* @return int Number of error (0 if OK)
*/
function insert_permissions($reinitadminperms=0, $force_entity=null)
{
global $conf,$user;
$err=0;
$entity=(! empty($force_entity) ? $force_entity : $conf->entity);
// Test if module is activated
$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."'";
$sql_del.= " AND entity IN (0,".$entity.")";
dol_syslog(get_class($this)."::insert_permissions", LOG_DEBUG);
$resql=$this->db->query($sql_del);
if ($resql)
{
$obj=$this->db->fetch_object($resql);
if (! empty($obj->value) && ! empty($this->rights))
{
// Si module actif
foreach ($this->rights as $key => $value)
{
$r_id = $this->rights[$key][0];
$r_desc = $this->rights[$key][1];
$r_type = isset($this->rights[$key][2])?$this->rights[$key][2]:'';
$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';
// 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);
$obj = $this->db->fetch_object($resqlselect);
if ($obj->nb == 0)
{
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);
}
$this->db->free($resqlselect);
// If we want to init permissions on admin users
if ($reinitadminperms)
{
if (! class_exists('User')) {
require DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
}
$sql="SELECT rowid FROM ".MAIN_DB_PREFIX."user WHERE admin = 1";
dol_syslog(get_class($this)."::insert_permissions Search all admin users", LOG_DEBUG);
$resqlseladmin=$this->db->query($sql,1);
if ($resqlseladmin)
{
$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);
if (!empty($tmpuser->id)) {
$tmpuser->addrights($r_id);
}
$i++;
}
if (! empty($user->admin)) // Reload permission for current user if defined
{
// We reload permissions
$user->clearrights();
$user->getrights();
}
}
else dol_print_error($this->db);
}
}
}
$this->db->free($resql);
}
else
{
$this->error=$this->db->lasterror();
$err++;
}
return $err;
}
/**
* Delete permissions
*
* @return int Nb of errors (0 if OK)
*/
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;
dol_syslog(get_class($this)."::delete_permissions", LOG_DEBUG);
if (! $this->db->query($sql))
{
$this->error=$this->db->lasterror();
$err++;
}
return $err;
}
/**
* Insert menus entries found into $this->menu into llx_menu*