39function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
'', $addfieldentry = array(), $delfieldentry =
'')
43 if (empty($objectname)) {
46 if (empty($readdir)) {
50 if (!empty($addfieldentry[
'arrayofkeyval']) && !is_array($addfieldentry[
'arrayofkeyval'])) {
51 dol_print_error(
null,
'Bad parameter addfieldentry with a property arrayofkeyval defined but that is not an array.');
58 if (is_array($addfieldentry) && count($addfieldentry) > 0) {
59 if (empty($addfieldentry[
'name'])) {
60 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Name")),
null,
'errors');
63 if (empty($addfieldentry[
'label'])) {
64 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Label")),
null,
'errors');
67 if (!preg_match(
'/^(integer|price|sellist|varchar|double|text|html|duration)/', $addfieldentry[
'type'])
68 && !preg_match(
'/^(boolean|smallint|real|date|datetime|timestamp|phone|mail|url|ip|password)$/', $addfieldentry[
'type'])) {
69 setEventMessages($langs->trans(
'BadValueForType', $addfieldentry[
'type']),
null,
'errors');
74 $pathoffiletoeditsrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
75 $pathoffiletoedittarget = $destdir.
'/class/'.strtolower($objectname).
'.class.php'.($readdir != $destdir ?
'.new' :
'');
77 $langs->load(
"errors");
78 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
86 include_once $pathoffiletoeditsrc;
87 if (class_exists($objectname)) {
92 '@phan-var-force CommonObject $object';
95 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
98 $contentclass = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
102 if (is_array($addfieldentry) && count($addfieldentry)) {
103 $name = $addfieldentry[
'name'];
104 unset($addfieldentry[
'name']);
106 $object->fields[$name] = $addfieldentry;
108 if (!empty($delfieldentry)) {
109 $name = $delfieldentry;
117 $texttoinsert =
'// BEGIN MODULEBUILDER PROPERTIES'.
"\n";
118 $texttoinsert .=
"\t".
'
121 $texttoinsert .=
"\t".
'public $fields=array('.
"\n";
124 foreach (
$object->fields as $key => $val) {
126 $texttoinsert .=
"\t\t".
'"'.$key.
'" => array(';
127 $texttoinsert .=
'"type"=>"'.dol_escape_php($val[
'type']).
'",';
128 $texttoinsert .=
' "label"=>"'.dol_escape_php($val[
'label']).
'",';
129 if (!empty($val[
'picto'])) {
130 $texttoinsert .=
' "picto"=>"'.dol_escape_php($val[
'picto']).
'",';
132 $texttoinsert .=
' "enabled"=>"'.($val[
'enabled'] !==
'' ?
dol_escape_php($val[
'enabled']) : 1).
'",';
133 $texttoinsert .=
" 'position'=>".($val[
'position'] !==
'' ? (int) $val[
'position'] : 50).
",";
134 $texttoinsert .=
" 'notnull'=>".(empty($val[
'notnull']) ? 0 : (int) $val[
'notnull']).
",";
135 $texttoinsert .=
' "visible"=>"'.($val[
'visible'] !==
'' ?
dol_escape_js($val[
'visible']) : -1).
'",';
136 if (!empty($val[
'noteditable'])) {
137 $texttoinsert .=
' "noteditable"=>"'.dol_escape_php($val[
'noteditable']).
'",';
139 if (!empty($val[
'alwayseditable'])) {
140 $texttoinsert .=
' "alwayseditable"=>"'.dol_escape_php($val[
'alwayseditable']).
'",';
142 if (array_key_exists(
'default', $val) && (!empty($val[
'default']) || $val[
'default'] ===
'0')) {
143 $texttoinsert .=
' "default"=>"'.dol_escape_php($val[
'default']).
'",';
145 if (!empty($val[
'index'])) {
146 $texttoinsert .=
' "index"=>"'.(int) $val[
'index'].
'",';
148 if (!empty($val[
'foreignkey'])) {
149 $texttoinsert .=
' "foreignkey"=>"'.(int) $val[
'foreignkey'].
'",';
151 if (!empty($val[
'searchall'])) {
152 $texttoinsert .=
' "searchall"=>"'.(int) $val[
'searchall'].
'",';
154 if (!empty($val[
'isameasure'])) {
155 $texttoinsert .=
' "isameasure"=>"'.(int) $val[
'isameasure'].
'",';
157 if (!empty($val[
'css'])) {
158 $texttoinsert .=
' "css"=>"'.dol_escape_php($val[
'css']).
'",';
160 if (!empty($val[
'cssview'])) {
161 $texttoinsert .=
' "cssview"=>"'.dol_escape_php($val[
'cssview']).
'",';
163 if (!empty($val[
'csslist'])) {
164 $texttoinsert .=
' "csslist"=>"'.dol_escape_php($val[
'csslist']).
'",';
166 if (!empty($val[
'help'])) {
167 $texttoinsert .=
' "help"=>"'.dol_escape_php($val[
'help']).
'",';
169 if (!empty($val[
'showoncombobox'])) {
170 $texttoinsert .=
' "showoncombobox"=>"'.(int) $val[
'showoncombobox'].
'",';
172 if (!empty($val[
'disabled'])) {
173 $texttoinsert .=
' "disabled"=>"'.(int) $val[
'disabled'].
'",';
175 if (!empty($val[
'autofocusoncreate'])) {
176 $texttoinsert .=
' "autofocusoncreate"=>"'.(int) $val[
'autofocusoncreate'].
'",';
178 if (!empty($val[
'arrayofkeyval'])) {
179 $texttoinsert .=
' "arrayofkeyval"=>array(';
181 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
183 $texttoinsert .=
", ";
185 $texttoinsert .=
'"'.dol_escape_php($key2).
'" => "'.
dol_escape_php($val2).
'"';
188 $texttoinsert .=
'),';
190 if (!empty($val[
'validate'])) {
191 $texttoinsert .=
' "validate"=>"'.(int) $val[
'validate'].
'",';
193 if (!empty($val[
'comment'])) {
194 $texttoinsert .=
' "comment"=>"'.dol_escape_php($val[
'comment']).
'"';
197 $texttoinsert .=
"),\n";
202 $texttoinsert .=
"\t".
');'.
"\n";
208 foreach (
$object->fields as $key => $val) {
211 $texttoinsert .=
"\t".
'public $'.$key.
";";
216 $texttoinsert .=
"\n";
220 $texttoinsert .=
"\t".
'// END MODULEBUILDER PROPERTIES';
224 $contentclass = preg_replace(
'/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
227 dol_mkdir(dirname($pathoffiletoedittarget));
230 $result = file_put_contents(
dol_osencode($pathoffiletoedittarget), $contentclass);
233 dolChmod($pathoffiletoedittarget, $newmask);
240 print $e->getMessage();
264 if (empty($objectname)) {
267 if (empty($readdir)) {
271 $pathoffiletoclasssrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
274 if ($moduletype ==
'internal') {
275 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
276 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
277 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
278 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
279 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
280 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
281 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'.sql';
286 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
287 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
288 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
289 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
290 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
291 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
292 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'.sql';
299 $pathoffiletoedittarget = $destdir.$pathoffiletoeditsrc.($readdir != $destdir ?
'.new' :
'');
300 $pathoffiletoeditsrc = $readdir.$pathoffiletoeditsrc;
303 $langs->load(
"errors");
304 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
311 include_once $pathoffiletoclasssrc;
312 if (class_exists($objectname)) {
313 $object =
new $objectname($db);
319 print $e->getMessage();
323 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
325 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
328 $texttoinsert =
'-- BEGIN MODULEBUILDER FIELDS'.
"\n";
330 foreach (
$object->fields as $key => $val) {
333 $type = $val[
'type'];
334 $type = preg_replace(
'/:.*$/',
'', $type);
336 if ($type ==
'html') {
338 } elseif ($type ==
'price') {
340 } elseif (in_array($type, array(
'link',
'sellist',
'duration'))) {
342 } elseif ($type ==
'mail') {
343 $type =
'varchar(128)';
344 } elseif ($type ==
'phone') {
345 $type =
'varchar(20)';
346 } elseif ($type ==
'ip') {
347 $type =
'varchar(32)';
350 $texttoinsert .=
"\t".$key.
" ".$type;
351 if ($key ==
'rowid') {
352 $texttoinsert .=
' AUTO_INCREMENT PRIMARY KEY';
353 } elseif ($type ==
'timestamp') {
354 $texttoinsert .=
' DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
356 if ($key ==
'entity') {
357 $texttoinsert .=
' DEFAULT 1';
359 if (!empty($val[
'default'])) {
360 if (preg_match(
'/^null$/i', $val[
'default'])) {
361 $texttoinsert .=
" DEFAULT NULL";
362 } elseif (preg_match(
'/varchar/', $type)) {
363 $texttoinsert .=
" DEFAULT '".$db->escape($val[
'default']).
"'";
365 $texttoinsert .= (($val[
'default'] > 0) ?
' DEFAULT '.$val[
'default'] :
'');
369 $texttoinsert .= ((!empty($val[
'notnull']) && $val[
'notnull'] > 0) ?
' NOT NULL' :
'');
370 if ($i < count(
$object->fields)) {
371 $texttoinsert .=
", ";
373 $texttoinsert .=
"\n";
376 $texttoinsert .=
"\t".
'-- END MODULEBUILDER FIELDS';
378 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
380 $result = file_put_contents($pathoffiletoedittarget, $contentsql);
382 dolChmod($pathoffiletoedittarget, $newmask);
385 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
389 $pathoffiletoeditsrc = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoeditsrc);
390 $pathoffiletoedittarget = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoedittarget);
391 $pathoffiletoedittarget = preg_replace(
'/\.sql.new$/',
'.key.sql.new', $pathoffiletoedittarget);
393 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
396 $texttoinsert =
'-- BEGIN MODULEBUILDER INDEXES'.
"\n";
398 foreach (
$object->fields as $key => $val) {
400 if (!empty($val[
'index'])) {
401 $texttoinsert .=
"ALTER TABLE llx_".strtolower($module).
'_'.strtolower($objectname).
" ADD INDEX idx_".strtolower($module).
'_'.strtolower($objectname).
"_".$key.
" (".$key.
");";
402 $texttoinsert .=
"\n";
404 if (!empty($val[
'foreignkey'])) {
405 $tmp = explode(
'.', $val[
'foreignkey']);
406 if (!empty($tmp[0]) && !empty($tmp[1])) {
407 $texttoinsert .=
"ALTER TABLE llx_".strtolower($module).
'_'.strtolower($objectname).
" ADD CONSTRAINT llx_".strtolower($module).
'_'.strtolower($objectname).
"_".$key.
" FOREIGN KEY (".$key.
") REFERENCES llx_".preg_replace(
'/^llx_/',
'', $tmp[0]).
"(".$tmp[1].
");";
408 $texttoinsert .=
"\n";
413 $texttoinsert .=
'-- END MODULEBUILDER INDEXES';
415 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
417 dol_mkdir(dirname($pathoffiletoedittarget));
419 $result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
421 dolChmod($pathoffiletoedittarget, $newmask);
424 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
427 return $error ? -1 : 1;
439 $listofobject =
dol_dir_list($destdir.
'/class',
'files', 0,
'\.class\.php$');
440 foreach ($listofobject as $fileobj) {
441 if (preg_match(
'/^api_/', $fileobj[
'name'])) {
444 if (preg_match(
'/^actions_/', $fileobj[
'name'])) {
448 $tmpcontent = file_get_contents($fileobj[
'fullname']);
450 if (preg_match(
'/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) {
451 $objectnameloop = $reg[1];
452 $objects[$fileobj[
'fullname']] = $objectnameloop;
455 if (count($objects) > 0) {
471 if (!file_exists($file)) {
475 $content = file_get_contents($file);
478 if (strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU MYOBJECT */') !==
false
479 || strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU */') !==
false) {
482 if (strpos($content,
'/* END MODULEBUILDER TOPMENU MYOBJECT */') !==
false
483 || strpos($content,
'/* END MODULEBUILDER TOPMENU */') !==
false) {
486 if (strpos($content,
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
489 if (strpos($content,
'/* END MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
496 } elseif ($number === 1) {
497 if (strpos($content,
'/* BEGIN MODULEBUILDER PERMISSIONS */') !==
false && strpos($content,
'/* END MODULEBUILDER PERMISSIONS */') !==
false) {
500 } elseif ($number == 2) {
501 if (strpos($content,
'/* BEGIN MODULEBUILDER DICTIONARIES */') !==
false && strpos($content,
'/* END MODULEBUILDER DICTIONARIES */') !==
false) {
515 $start =
"/* BEGIN MODULEBUILDER PERMISSIONS */";
516 $end =
"/* END MODULEBUILDER PERMISSIONS */";
519 $lines = file($file);
521 foreach ($lines as $i => $line) {
522 if (strpos($line, $start) !==
false) {
523 $start_line = $i + 1;
526 while (($line = $lines[++$i]) !==
false) {
527 if (strpos($line, $end) !==
false) {
536 $allContent = implode(
"", $array);
548 return strcmp($a[0], $b[0]);
567 array_splice($permissions, array_search($permissions[$key], $permissions), 1);
568 } elseif ($action == 1) {
569 array_push($permissions, $right);
570 } elseif ($action == 2 && !empty($right)) {
572 array_splice($permissions, array_search($permissions[$key], $permissions), 1, $right);
573 } elseif ($action == -1 && !empty($objectname)) {
577 foreach ($permissions as $perms) {
578 if ($perms[4] === strtolower($objectname)) {
579 array_splice($permissions, array_search($perms, $permissions), 1);
582 } elseif ($action == -2 && !empty($objectname) && !empty($module)) {
585 $objectOfRights = array();
587 foreach ($permissions as $right) {
588 $objectOfRights[] = $right[4];
590 if (in_array(strtolower($objectname), $objectOfRights)) {
593 $permsToadd = array();
595 'read' =>
'Read '.$objectname.
' object of '.ucfirst($module),
596 'write' =>
'Create/Update '.$objectname.
' object of '.ucfirst($module),
597 'delete' =>
'Delete '.$objectname.
' object of '.ucfirst($module)
600 foreach ($perms as $index => $value) {
601 $permsToadd[$i][0] =
'';
602 $permsToadd[$i][1] = $value;
603 $permsToadd[$i][4] = strtolower($objectname);
604 $permsToadd[$i][5] = $index;
605 array_push($permissions, $permsToadd[$i]);
612 '@phan-var-force array<int,string[]> $permissions';
615 $count_perms = count($permissions);
616 foreach (array_keys($permissions) as $i) {
617 $permissions[$i][0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1)";
618 $permissions[$i][1] =
"\$this->rights[\$r][1] = '".$permissions[$i][1].
"'";
619 $permissions[$i][4] =
"\$this->rights[\$r][4] = '".$permissions[$i][4].
"'";
620 $permissions[$i][5] =
"\$this->rights[\$r][5] = '".$permissions[$i][5].
"';\n\t\t";
623 $perms_grouped = array();
624 foreach ($permissions as $perms) {
626 if (!isset($perms_grouped[
$object])) {
629 $perms_grouped[
$object][] = $perms;
632 $permissions = $perms_grouped;
637 foreach ($permissions as &
$object) {
641 if (str_contains($obj[5],
'read')) {
642 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 0 + 1)";
643 } elseif (str_contains($obj[5],
'write')) {
644 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 1 + 1)";
645 } elseif (str_contains($obj[5],
'delete')) {
646 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 2 + 1)";
648 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + ".$p.
" + 1)";
652 usort(
$object,
'compareFirstValue');
657 foreach ($permissions as $perms) {
658 foreach ($perms as $per) {
659 $rights[] = implode(
";\n\t\t", $per);
660 $rights[] =
"\$r++;\n\t\t";
663 $rights_str = implode(
"", $rights);
667 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER PERMISSIONS */' =>
'/* BEGIN MODULEBUILDER PERMISSIONS */'.
"\n\t\t".$rights_str));
682 $string = str_replace(
"'",
'', $string);
685 preg_match_all(
'/\s*([^\s=>]+)\s*=>\s*([^,]+),?/', $string, $matches, PREG_SET_ORDER);
688 foreach ($matches as $match) {
689 $key = trim($match[1]);
690 $value = trim($match[2]);
692 if (strpos($value,
'array(') === 0) {
693 $nestedArray = substr($value, 6);
695 $value = $nestedArray;
696 } elseif (strpos($value,
'"Id")') !==
false) {
697 $value = str_replace(
')',
'', $value);
699 if (is_numeric($value)) {
700 if (strpos($value,
'.') !==
false) {
701 $value = (float) $value;
703 $value = (int) $value;
706 if ($value ===
'true') {
708 } elseif ($value ===
'false') {
713 $propertyArray[$key] = $value;
716 return $propertyArray;
730 $attributesUnique = array(
'type',
'label',
'enabled',
'position',
'notnull',
'visible',
'noteditable',
'index',
'default' ,
'foreignkey',
'arrayofkeyval',
'alwayseditable',
'validate',
'searchall',
'comment',
'isameasure',
'css',
'cssview',
'csslist',
'help',
'showoncombobox',
'picto' );
732 $start =
"public \$fields=array(";
736 $lines = file($file);
738 foreach ($lines as $i => $line) {
739 if (strpos($line, $start) !==
false) {
741 while (($line = $lines[++$i]) !==
false) {
742 if (strpos($line, $end) !==
false) {
751 $table =
"== DATA SPECIFICATIONS\n";
752 $table .=
"=== Table of fields with properties for object *$objectname* : \n";
753 $table .=
"[options='header',grid=rows,frame=topbot,width=100%,caption=Organisation]\n";
757 foreach ($attributesUnique as $attUnique) {
758 $table .=
"|".$attUnique;
761 $valuesModif = array();
762 foreach ($keys as $string) {
763 $string = trim($string,
"'");
764 $string = rtrim($string,
",");
770 foreach ($array as $key => $value) {
771 if (is_array($value)) {
775 $array[$code][$key] = $value;
780 if (!is_array($array)) {
783 $field = array_keys($array);
784 if ($field[0] ===
'') {
787 $values = array_values($array)[0];
790 foreach ($attributesUnique as $attUnique) {
791 if ($attUnique ==
'type' && $field[0] ===
'label') {
792 $values[$attUnique] =
'varchar(255)';
794 if (!array_key_exists($attUnique, $values)) {
795 $valuesModif[$attUnique] =
'';
797 $valuesModif[$attUnique] = $values[$attUnique];
800 $table .=
"|*" . $field[0] .
"*|";
801 $table .= implode(
"|", $valuesModif) .
"\n";
806 $table .=
"__ end table for object $objectname\n";
809 $writeInFile =
dolReplaceInFile($destfile, array(
'== DATA SPECIFICATIONS' => $table));
810 if ($writeInFile < 0) {
827 $start =
"== Table of fields and their properties for object *".ucfirst($objectname).
"* : ";
828 $end =
"__ end table for object ".ucfirst($objectname);
830 $str = file_get_contents($file);
832 $search =
'/' . preg_quote($start,
'/') .
'(.*?)' . preg_quote($end,
'/') .
'/s';
833 $new_contents = preg_replace($search,
'', $str);
834 file_put_contents($file, $new_contents);
837 $perms =
"|*".strtolower($objectname).
"*|";
838 $search_pattern_perms =
'/' . preg_quote($perms,
'/') .
'.*?\n/';
839 $new_contents = preg_replace($search_pattern_perms,
'', $new_contents);
840 file_put_contents($file, $new_contents);
856function getFromFile($file, $start, $end, $excludestart =
'', $includese = 0)
865 while ($line = fgets($fhandle)) {
866 if (strpos($line, $start) !==
false && (empty($excludestart) || strpos($line, $excludestart) ===
false)) {
871 while (($line = fgets($fhandle)) !==
false) {
872 if (strpos($line, $end) !==
false) {
886 $content = implode(
"", $keys);
900 $start =
'/* BEGIN MODULEBUILDER PERMISSIONS */';
901 $end =
'/* END MODULEBUILDER PERMISSIONS */';
903 if (empty($content)) {
907 $string =
"[options='header',grid=rows,width=60%,caption=Organisation]\n";
910 $header = array($langs->trans(
'Objects'),$langs->trans(
'Permission'));
911 foreach ($header as $h) {
916 $array = explode(
";", $content);
917 $permissions = array_filter($array);
919 $permissions = str_replace(
'$r++',
'1', $permissions);
922 foreach ($permissions as $i => $element) {
924 unset($permissions[$i]);
926 if (str_contains($element,
'$this->numero')) {
927 unset($permissions[$i]);
929 if (str_contains($element,
'$this->rights[$r][5]')) {
930 unset($permissions[$i]);
934 foreach ($permissions as $key => $element) {
935 $element = str_replace(
" '",
'', $element);
936 $element = trim($element,
"'");
937 $permsN[] = substr($element, strpos($element,
"=") + 1);
944 while ($index < count($permsN)) {
945 $temp_array = [$permsN[$index], $permsN[$index + 1]];
946 $final_array[] = $temp_array;
951 foreach ($final_array as $subarray) {
955 $result[$key][] = $subarray;
957 foreach ($result as $i => $pems) {
958 $string .=
"|*".$i.
"*|";
959 foreach ($pems as $tab) {
960 $string .= $tab[0].
" , ";
965 $string .=
"\n|===\n";
967 $write =
dolReplaceInFile($destfile, array(
'__DATA_PERMISSIONS__' => $string));
985 global $langs, $user;
987 if (!file_exists($file)) {
992 $content = file($file);
994 $includeClass =
"dol_include_once\(\'\/\w+\/class\/\w+\.class\.php\'\);";
995 $props =
'public\s+\$\w+;';
996 $varcommented =
'@var\s+\w+\s+\$\w+\s+{@type\s+\w+}';
997 $constructObj =
'\$this->\w+\s+=\s+new\s+\w+\(\$this->db\);';
1000 foreach ($content as $lineNumber => &$lineContent) {
1001 if (preg_match(
'/'.$varcommented.
'/', $lineContent)) {
1003 foreach ($objects as $objectname) {
1004 $lineContent .=
"\t * @var ".$objectname.
" \$".strtolower($objectname).
" {@type ".$objectname.
"}". PHP_EOL;
1007 } elseif (preg_match(
'/'.$props.
'/', $lineContent)) {
1009 foreach ($objects as $objectname) {
1010 $lineContent .=
"\tpublic \$".strtolower($objectname).
";". PHP_EOL;
1012 } elseif (preg_match(
'/'.$constructObj.
'/', $lineContent)) {
1014 foreach ($objects as $objectname) {
1015 $lineContent .=
"\t\t\$this->".strtolower($objectname).
" = new ".$objectname.
"(\$this->db);". PHP_EOL;
1017 } elseif (preg_match(
'/'.$includeClass.
'/', $lineContent)) {
1019 foreach ($objects as $objectname) {
1020 $lineContent .=
"dol_include_once('/".strtolower($modulename).
"/class/".strtolower($objectname).
".class.php');". PHP_EOL;
1025 $allContent = implode(
"", $content);
1026 file_put_contents($file, $allContent);
1029 $allContent =
getFromFile($srcfile,
'/* BEGIN MODULEBUILDER API MYOBJECT */',
'/* END MODULEBUILDER API MYOBJECT */');
1030 foreach ($objects as $objectname) {
1031 $arrayreplacement = array(
1032 'mymodule' => strtolower($modulename),
1033 'MyModule' => $modulename,
1034 'MYMODULE' => strtoupper($modulename),
1035 'My module' => $modulename,
1036 'my module' => $modulename,
1037 'Mon module' => $modulename,
1038 'mon module' => $modulename,
1039 'htdocs/modulebuilder/template' => strtolower($modulename),
1040 'myobject' => strtolower($objectname),
1041 'MyObject' => $objectname,
1042 'MYOBJECT' => strtoupper($objectname),
1043 '---Put here your own copyright and developer email---' =>
dol_print_date($now,
'%Y').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
'')
1049 '/* BEGIN MODULEBUILDER API MYOBJECT */' =>
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */'.$contentReplaced.
"\t".
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */'.
"\n\n\n\t".
'/* BEGIN MODULEBUILDER API MYOBJECT */'
1070 if (!file_exists($file)) {
1074 $content = file($file);
1076 $includeClass =
"dol_include_once\(\'\/\w+\/class\/".strtolower($objectname).
"\.class\.php\'\);";
1077 $props =
'public\s+\$'.strtolower($objectname);
1078 $varcommented =
'@var\s+\w+\s+\$'.strtolower($objectname).
'\s+{@type\s+\w+}';
1079 $constructObj =
'\$this->'.strtolower($objectname).
'\s+=\s+new\s+\w+\(\$this->db\);';
1082 foreach ($content as $lineNumber => &$lineContent) {
1083 if (preg_match(
'/'.$varcommented.
'/i', $lineContent)) {
1085 } elseif (preg_match(
'/'.$props.
'/i', $lineContent)) {
1087 } elseif (preg_match(
'/'.$constructObj.
'/i', $lineContent)) {
1089 } elseif (preg_match(
'/'.$includeClass.
'/i', $lineContent)) {
1094 $allContent = implode(
"", $content);
1095 file_put_contents($file, $allContent);
1098 $begin =
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */';
1099 $end =
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */';
1122 if (!file_exists($file)) {
1126 if ($action == 0 && !empty($key)) {
1128 array_splice($menus, array_search($menus[$key], $menus), 1);
1129 } elseif ($action == 1) {
1131 array_push($menus, $menuWantTo);
1132 } elseif ($action == 2 && !empty($key) && !empty($menuWantTo)) {
1136 foreach ($menus as $index => $menu) {
1137 if ($index !== $key) {
1138 if ($menu[
'type'] === $menuWantTo[
'type']) {
1139 if (strcasecmp(str_replace(
' ',
'', $menu[
'titre']), str_replace(
' ',
'', $menuWantTo[
'titre'])) === 0) {
1142 if (strcasecmp(str_replace(
' ',
'', $menu[
'url']), str_replace(
' ',
'', $menuWantTo[
'url'])) === 0) {
1148 if (!$counter && $urlCounter < 2) {
1149 $menus[$key] = $menuWantTo;
1153 } elseif ($action == -1 && !empty($menuWantTo)) {
1155 foreach ($menus as $index => $menu) {
1156 if ((strpos(strtolower($menu[
'fk_menu']), strtolower($menuWantTo)) !==
false) || (strpos(strtolower($menu[
'leftmenu']), strtolower($menuWantTo)) !==
false)) {
1157 array_splice($menus, array_search($menu, $menus), 1);
1165 $beginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU';
1166 $excludeBeginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT';
1167 $endMenu =
'/* END MODULEBUILDER LEFTMENU';
1169 while ($protection <= 1000 && $allMenus =
getFromFile($file, $beginMenu, $endMenu, $excludeBeginMenu, 1)) {
1176 foreach ($menus as $index => $menu) {
1177 $menu[
'position'] =
"1000 + \$r";
1178 if ($menu[
'type'] ===
'left') {
1179 $start =
"\t\t".
'/* BEGIN MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1180 $end =
"\t\t".
'/* END MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1182 $val_actuel = $menu;
1183 $next_val = empty($menus[$index + 1]) ? null : $menus[$index + 1];
1186 $str_menu .= $start.
"\n";
1187 $str_menu .=
"\t\t\$this->menu[\$r++]=array(\n";
1188 $str_menu .=
"\t\t\t 'fk_menu' => '".dol_escape_php($menu[
'fk_menu'], 1).
"',\n";
1189 $str_menu .=
"\t\t\t 'type' => '".dol_escape_php($menu[
'type'], 1).
"',\n";
1190 $str_menu .=
"\t\t\t 'titre' => '".dol_escape_php($menu[
'titre'], 1).
"',\n";
1191 $str_menu .=
"\t\t\t 'mainmenu' => '".dol_escape_php($menu[
'mainmenu'], 1).
"',\n";
1192 $str_menu .=
"\t\t\t 'leftmenu' => '".dol_escape_php($menu[
'leftmenu'], 1).
"',\n";
1193 $str_menu .=
"\t\t\t 'url' => '".dol_escape_php($menu[
'url'], 1).
"',\n";
1194 $str_menu .=
"\t\t\t 'langs' => '".dol_escape_php($menu[
'langs'], 1).
"',\n";
1195 $str_menu .=
"\t\t\t 'position' => ".((int) $menu[
'position']).
",\n";
1196 $str_menu .=
"\t\t\t 'enabled' => '".dol_escape_php($menu[
'enabled'], 1).
"',\n";
1197 $str_menu .=
"\t\t\t 'perms' => '".dol_escape_php($menu[
'perms'], 1).
"',\n";
1198 $str_menu .=
"\t\t\t 'target' => '".dol_escape_php($menu[
'target'], 1).
"',\n";
1199 $str_menu .=
"\t\t\t 'user' => ".((int) $menu[
'user']).
",\n";
1200 $str_menu .=
"\t\t\t 'object' => '".dol_escape_php($menu[
'object'], 1).
"',\n";
1201 $str_menu .=
"\t\t);\n";
1203 if (is_null($next_val) || $val_actuel[
'leftmenu'] !== $next_val[
'leftmenu']) {
1204 $str_menu .= $end.
"\n";
1209 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */' => $str_menu.
"\n\t\t/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */"));
1226 $dicData =
"\t\t\$this->dictionaries=array(\n";
1227 $module = strtolower($module);
1228 foreach ($dicts as $key => $value) {
1229 if (empty($value)) {
1231 $dicData =
"\t\t\$this->dictionaries=array();";
1235 $dicData .=
"\t\t\t'$key'=>";
1237 if ($key ===
'tabcond') {
1238 $conditions = array_map(
1243 function ($val) use ($module) {
1244 return is_bool($val) ?
"isModEnabled('$module')" : $val;
1248 $dicData .=
"array(" . implode(
",", $conditions) .
")";
1249 } elseif ($key ===
'tabhelp') {
1250 $helpItems = array();
1251 foreach ($value as $helpValue) {
1252 $helpItems[] =
"array('code'=>\$langs->trans('".$helpValue[
'code'].
"'), 'field2' => 'field2tooltip')";
1254 $dicData .=
"array(" . implode(
",", $helpItems) .
")";
1256 if (is_array($value)) {
1257 $dicData .=
"array(" . implode(
1264 static function ($val) {
1271 $dicData .=
"'$value'";
1276 $dicData .= (!$isEmpty ?
"\t\t);" :
'');
1278 $stringDic =
getFromFile($file,
'/* BEGIN MODULEBUILDER DICTIONARIES */',
'/* END MODULEBUILDER DICTIONARIES */');
1279 $writeInfile =
dolReplaceInFile($file, array($stringDic => $dicData.
"\n"));
1281 return $writeInfile;
1300 if (empty($namedic)) {
1304 if (!file_exists($file)) {
1307 $modulename = strtolower($modulename);
1309 if (empty($dictionnaires)) {
1310 $dictionnaires = array(
'langs' =>
'',
'tabname' => array(),
'tablib' => array(),
'tabsql' => array(),
'tabsqlsort' => array(),
'tabfield' => array(),
'tabfieldvalue' => array(),
'tabfieldinsert' => array(),
'tabrowid' => array(),
'tabcond' => array(),
'tabhelp' => array());
1314 'rowid' => array(
'type' =>
'integer',
'value' => 11,
'extra' =>
'AUTO_INCREMENT'),
1315 'code' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1316 'label' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1317 'position' => array(
'type' =>
'integer',
'value' => 11,
'null' =>
'NULL'),
1318 'use_default' => array(
'type' =>
'varchar',
'value' => 11,
'default' =>
'1'),
1319 'active' => array(
'type' =>
'integer',
'value' => 3)
1322 $primaryKey =
'rowid';
1323 foreach ($columns as $key => $value) {
1324 if ($key ===
'rowid') {
1325 $primaryKey =
'rowid';
1328 if (!array_key_exists(
'rowid', $columns)) {
1329 $primaryKey = array_key_first($columns);
1335 $checkTable = $db->DDLDescTable(MAIN_DB_PREFIX.strtolower($namedic));
1336 if ($checkTable && $db->num_rows($checkTable) > 0) {
1337 setEventMessages($langs->trans(
"ErrorTableExist", $namedic),
null,
'errors');
1340 $_results = $db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey,
"");
1341 if ($_results < 0) {
1343 $langs->load(
"errors");
1344 setEventMessages($langs->trans(
"ErrorTableNotFound", $namedic),
null,
'errors');
1349 $dictionnaires[
'langs'] = $modulename.
'@'.$modulename;
1350 $dictionnaires[
'tabname'][] = strtolower($namedic);
1351 $dictionnaires[
'tablib'][] = ucfirst(substr($namedic, 2));
1352 $dictionnaires[
'tabsql'][] =
'SELECT t.rowid as rowid, t.code, t.label, t.active FROM '.MAIN_DB_PREFIX.strtolower($namedic).
' as t';
1353 $dictionnaires[
'tabsqlsort'][] = (array_key_exists(
'label', $columns) ?
'label ASC' :
'');
1354 $dictionnaires[
'tabfield'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1355 $dictionnaires[
'tabfieldvalue'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1356 $dictionnaires[
'tabfieldinsert'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1357 $dictionnaires[
'tabrowid'][] = $primaryKey;
1358 $dictionnaires[
'tabcond'][] = isModEnabled(
'$modulename');
1359 $dictionnaires[
'tabhelp'][] = (array_key_exists(
'code', $columns) ? array(
'code' => $langs->trans(
'CodeTooltipHelp'),
'field2' =>
'field2tooltip') :
'');
1363 if ($writeInfile > 0) {
1364 setEventMessages($langs->trans(
"DictionariesCreated", ucfirst(substr($namedic, 2))),
null);
1383 $string =
getFromFile($file_api,
'/*begin methods CRUD*/',
'/*end methods CRUD*/');
1384 $extractUrls = explode(
"\n", $string);
1388 foreach ($extractUrls as $key => $line) {
1389 $lineWithoutTabsSpaces = preg_replace(
'/^[\t\s]+/',
'', $line);
1390 if (strpos($lineWithoutTabsSpaces,
'* @url') === 0) {
1391 $urlValue = trim(substr($lineWithoutTabsSpaces, strlen(
'* @url')));
1392 $urlValues[] = $urlValue;
1397 $str = $_SERVER[
'HTTP_HOST'].
'/api/index.php/';
1399 foreach ($urlValues as $url) {
1400 if (preg_match(
'/(?:GET|POST|PUT|DELETE) (\w+)s/', $url, $matches)) {
1401 $objectName = $matches[1];
1402 $url = $str.trim(strstr($url,
' '));
1403 $groupedUrls[$objectName][] = $url;
1406 if (empty($groupedUrls)) {
1412 $asciiDocTable =
"[options=\"header\"]\n|===\n|Object | URLs\n";
1413 foreach ($groupedUrls as $objectName => $urls) {
1414 $urlsList = implode(
" +\n*", $urls);
1415 $asciiDocTable .=
"|$objectName | \n*$urlsList +\n";
1417 $asciiDocTable .=
"|===\n";
1418 $file_write =
dolReplaceInFile($file_doc, array(
'__API_DOC__' =>
'__API_DOC__'.
"\n".$asciiDocTable));
1419 if ($file_write < 0) {
1436 if (!is_dir($path)) {
1440 $allFilesAndDirs = scandir($path);
1443 foreach ($allFilesAndDirs as $item) {
1444 if ($item !=
'.' && $item !=
'..') {
1445 if ($type == 1 && is_file($path . DIRECTORY_SEPARATOR . $item) && strpos($item,
'.back') ===
false) {
1447 } elseif ($type == 2 && is_dir($path . DIRECTORY_SEPARATOR . $item)) {
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
dol_copy($srcfile, $destfile, $newmask='0', $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
dol_is_file($pathoffile)
Return if path is a file.
dol_dir_list($utf8_path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0, $nbsecondsold=0)
Scan a directory and return a list of files/directories.
dolReplaceInFile($srcfile, $arrayreplacement, $destfile='', $newmask='0', $indexdatabase=0, $arrayreplacementisregex=0)
Make replacement of strings into a file.
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_now($mode='auto')
Return date for now.
dol_escape_php($stringtoescape, $stringforquotes=2)
Returns text escaped for inclusion into a php string, build with double quotes " or '.
dol_escape_js($stringtoescape, $mode=0, $noescapebackslashn=0)
Returns text escaped for inclusion into javascript code.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_sort_array(&$array, $index, $order='asc', $natsort=0, $case_sensitive=0, $keepindex=0)
Advanced sort array by the value of a given key, which produces ascending (default) or descending out...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
updateDictionaryInFile($module, $file, $dicts)
Updates a dictionary in a module descriptor file.
deletePropsAndPermsFromDoc($file, $objectname)
Delete property and permissions from documentation ascii file if we delete an object.
createNewDictionnary($modulename, $file, $namedic, $dictionnaires=null)
Creates a new dictionary table.
countItemsInDirectory($path, $type=1)
count directories or files in modulebuilder folder
writePermsInAsciiDoc($file, $destfile)
Write all permissions of each object in AsciiDoc format.
reWriteAllMenus($file, $menus, $menuWantTo, $key, $action)
parsePropertyString($string)
Converts a formatted properties string into an associative array.
rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='', $object=null, $moduletype='external')
Save data into a memory area shared by all users, all sessions on server.
writeApiUrlsInDoc($file_api, $file_doc)
Generate Urls and add them to documentation module.
getFromFile($file, $start, $end, $excludestart='', $includese=0)
Search a string and return all lines needed from file.
dolGetListOfObjectClasses($destdir)
Get list of existing objects from a directory.
deletePerms($file)
Delete all permissions.
writePropsInAsciiDoc($file, $objectname, $destfile)
Write all properties of the object in AsciiDoc format.
compareFirstValue($a, $b)
Compare two value.
removeObjectFromApiFile($file, $objects, $objectname)
Remove Object variables and methods from API_Module File.
rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir='', $addfieldentry=array(), $delfieldentry='')
Regenerate files .class.php.
reWriteAllPermissions($file, $permissions, $key, $right, $objectname, $module, $action)
Rewriting all permissions after any actions.
checkExistComment($file, $number)
Function to check if comment BEGIN and END exists in modMyModule class.
addObjectsToApiFile($srcfile, $file, $objects, $modulename)
Add Object in ModuleApi File.