40function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
'', $addfieldentry = array(), $delfieldentry =
'')
44 if (empty($objectname)) {
47 if (empty($readdir)) {
51 if (!empty($addfieldentry[
'arrayofkeyval']) && !is_array($addfieldentry[
'arrayofkeyval'])) {
52 dol_print_error(
null,
'Bad parameter addfieldentry with a property arrayofkeyval defined but that is not an array.');
59 if (is_array($addfieldentry) && count($addfieldentry) > 0) {
60 if (empty($addfieldentry[
'name'])) {
61 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Name")),
null,
'errors');
64 if (empty($addfieldentry[
'label'])) {
65 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Label")),
null,
'errors');
68 if (!preg_match(
'/^(integer|price|sellist|varchar|double|text|html|duration|stars)/', $addfieldentry[
'type'])
69 && !preg_match(
'/^(boolean|smallint|real|date|datetime|timestamp|phone|mail|url|ip|password)$/', $addfieldentry[
'type'])) {
70 setEventMessages($langs->trans(
'BadValueForType', $addfieldentry[
'type']),
null,
'errors');
74 if (preg_match(
'/^stars\((.+)\)$/', $addfieldentry[
'type'], $matches)) {
75 if (!ctype_digit($matches[1]) || $matches[1] < 1 || $matches[1] > 10) {
76 setEventMessages($langs->trans(
'BadValueForType', $addfieldentry[
'type']),
null,
'errors');
82 $pathoffiletoeditsrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
83 $pathoffiletoedittarget = $destdir.
'/class/'.strtolower($objectname).
'.class.php'.($readdir != $destdir ?
'.new' :
'');
85 $langs->load(
"errors");
86 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
94 include_once $pathoffiletoeditsrc;
95 if (class_exists($objectname)) {
100 '@phan-var-force CommonObject $object';
103 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
106 $contentclass = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
110 if (is_array($addfieldentry) && count($addfieldentry)) {
111 $name = $addfieldentry[
'name'];
112 unset($addfieldentry[
'name']);
114 $object->fields[$name] = $addfieldentry;
116 if (!empty($delfieldentry)) {
117 $name = $delfieldentry;
125 $texttoinsert =
'// BEGIN MODULEBUILDER PROPERTIES'.
"\n";
126 $texttoinsert .=
"\t".
'
130 $texttoinsert .=
"\t".
'public $fields = array('.
"\n";
133 foreach (
$object->fields as $key => $val) {
135 $texttoinsert .=
"\t\t".
'"'.$key.
'" => array(';
136 $texttoinsert .=
'"type" => "'.dol_escape_php($val[
'type']).
'",';
137 $texttoinsert .=
' "label" => "'.dol_escape_php($val[
'label']).
'",';
138 if (!empty($val[
'picto'])) {
139 $texttoinsert .=
' "picto" => "'.dol_escape_php($val[
'picto']).
'",';
141 $texttoinsert .=
' "enabled" => "'.($val[
'enabled'] !==
'' ?
dol_escape_php($val[
'enabled']) : 1).
'",';
142 $texttoinsert .=
" 'position' => ".($val[
'position'] !==
'' ? (int) $val[
'position'] : 50).
",";
143 $texttoinsert .=
" 'notnull' => ".(empty($val[
'notnull']) ? 0 : (int) $val[
'notnull']).
",";
144 $texttoinsert .=
' "visible" => "'.($val[
'visible'] !==
'' ?
dol_escape_js($val[
'visible']) : -1).
'",';
145 if (!empty($val[
'noteditable'])) {
146 $texttoinsert .=
' "noteditable" => "'.dol_escape_php($val[
'noteditable']).
'",';
148 if (!empty($val[
'alwayseditable'])) {
149 $texttoinsert .=
' "alwayseditable" => "'.dol_escape_php($val[
'alwayseditable']).
'",';
151 if (array_key_exists(
'default', $val) && (!empty($val[
'default']) || $val[
'default'] ===
'0')) {
152 $texttoinsert .=
' "default" => "'.dol_escape_php($val[
'default']).
'",';
154 if (!empty($val[
'index'])) {
155 $texttoinsert .=
' "index" => "'.(int) $val[
'index'].
'",';
157 if (!empty($val[
'foreignkey'])) {
158 $texttoinsert .=
' "foreignkey" => "'.(int) $val[
'foreignkey'].
'",';
160 if (!empty($val[
'searchall'])) {
161 $texttoinsert .=
' "searchall" => "'.(int) $val[
'searchall'].
'",';
163 if (!empty($val[
'isameasure'])) {
164 $texttoinsert .=
' "isameasure" => "'.(int) $val[
'isameasure'].
'",';
166 if (!empty($val[
'css'])) {
167 $texttoinsert .=
' "css" => "'.dol_escape_php($val[
'css']).
'",';
169 if (!empty($val[
'cssview'])) {
170 $texttoinsert .=
' "cssview" => "'.dol_escape_php($val[
'cssview']).
'",';
172 if (!empty($val[
'csslist'])) {
173 $texttoinsert .=
' "csslist" => "'.dol_escape_php($val[
'csslist']).
'",';
175 if (!empty($val[
'help'])) {
176 $texttoinsert .=
' "help" => "'.dol_escape_php($val[
'help']).
'",';
178 if (!empty($val[
'showoncombobox'])) {
179 $texttoinsert .=
' "showoncombobox" => "'.(int) $val[
'showoncombobox'].
'",';
181 if (!empty($val[
'disabled'])) {
182 $texttoinsert .=
' "disabled" => "'.(int) $val[
'disabled'].
'",';
184 if (!empty($val[
'autofocusoncreate'])) {
185 $texttoinsert .=
' "autofocusoncreate" => "'.(int) $val[
'autofocusoncreate'].
'",';
187 if (!empty($val[
'arrayofkeyval'])) {
188 $texttoinsert .=
' "arrayofkeyval" => array(';
190 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
192 $texttoinsert .=
", ";
194 $texttoinsert .=
'"'.dol_escape_php($key2).
'" => "'.
dol_escape_php($val2).
'"';
197 $texttoinsert .=
'),';
199 if (!empty($val[
'validate'])) {
200 $texttoinsert .=
' "validate" => "'.(int) $val[
'validate'].
'",';
202 if (!empty($val[
'comment'])) {
203 $texttoinsert .=
' "comment" => "'.dol_escape_php($val[
'comment']).
'"';
206 $texttoinsert .=
"),\n";
211 $texttoinsert .=
"\t".
');'.
"\n";
217 foreach (
$object->fields as $key => $val) {
220 $texttoinsert .=
"\t".
'public $'.$key.
";";
225 $texttoinsert .=
"\n";
229 $texttoinsert .=
"\t".
'// END MODULEBUILDER PROPERTIES';
233 $contentclass = preg_replace(
'/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
236 dol_mkdir(dirname($pathoffiletoedittarget));
239 $result = file_put_contents(
dol_osencode($pathoffiletoedittarget), $contentclass);
242 dolChmod($pathoffiletoedittarget, $newmask);
249 print $e->getMessage();
273 if (empty($objectname)) {
276 if (empty($readdir)) {
280 $pathoffiletoclasssrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
283 if ($moduletype ==
'internal') {
284 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
285 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
286 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
287 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
288 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
289 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
290 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'.sql';
295 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
296 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
297 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
298 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
299 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
300 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
301 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'.sql';
308 $pathoffiletoedittarget = $destdir.$pathoffiletoeditsrc.($readdir != $destdir ?
'.new' :
'');
309 $pathoffiletoeditsrc = $readdir.$pathoffiletoeditsrc;
312 $langs->load(
"errors");
313 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
320 include_once $pathoffiletoclasssrc;
321 if (class_exists($objectname)) {
322 $object =
new $objectname($db);
328 print $e->getMessage();
332 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
334 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
337 $texttoinsert =
'-- BEGIN MODULEBUILDER FIELDS'.
"\n";
339 foreach (
$object->fields as $key => $val) {
342 $type = $val[
'type'];
343 $type = preg_replace(
'/:.*$/',
'', $type);
345 if ($type ==
'html') {
347 } elseif ($type ==
'price') {
349 } elseif (in_array($type, array(
'link',
'sellist',
'duration'))) {
351 } elseif ($type ==
'mail') {
352 $type =
'varchar(128)';
353 } elseif (strpos($type,
'stars(') === 0) {
355 } elseif ($type ==
'phone') {
356 $type =
'varchar(20)';
357 } elseif ($type ==
'ip') {
358 $type =
'varchar(32)';
361 $texttoinsert .=
"\t".$key.
" ".$type;
362 if ($key ==
'rowid') {
363 $texttoinsert .=
' AUTO_INCREMENT PRIMARY KEY';
364 } elseif ($type ==
'timestamp') {
365 $texttoinsert .=
' DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
367 if ($key ==
'entity') {
368 $texttoinsert .=
' DEFAULT 1';
370 if (!empty($val[
'default'])) {
371 if (preg_match(
'/^null$/i', $val[
'default'])) {
372 $texttoinsert .=
" DEFAULT NULL";
373 } elseif (preg_match(
'/varchar/', $type)) {
374 $texttoinsert .=
" DEFAULT '".$db->escape($val[
'default']).
"'";
376 $texttoinsert .= (($val[
'default'] > 0) ?
' DEFAULT '.$val[
'default'] :
'');
380 $texttoinsert .= ((!empty($val[
'notnull']) && $val[
'notnull'] > 0) ?
' NOT NULL' :
'');
381 if ($i < count(
$object->fields)) {
382 $texttoinsert .=
", ";
384 $texttoinsert .=
"\n";
387 $texttoinsert .=
"\t".
'-- END MODULEBUILDER FIELDS';
389 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
391 $result = file_put_contents($pathoffiletoedittarget, $contentsql);
393 dolChmod($pathoffiletoedittarget, $newmask);
396 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
400 $pathoffiletoeditsrc = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoeditsrc);
401 $pathoffiletoedittarget = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoedittarget);
402 $pathoffiletoedittarget = preg_replace(
'/\.sql.new$/',
'.key.sql.new', $pathoffiletoedittarget);
404 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
407 $texttoinsert =
'-- BEGIN MODULEBUILDER INDEXES'.
"\n";
409 foreach (
$object->fields as $key => $val) {
411 if (!empty($val[
'index'])) {
412 $texttoinsert .=
"ALTER TABLE llx_".strtolower($module).
'_'.strtolower($objectname).
" ADD INDEX idx_".strtolower($module).
'_'.strtolower($objectname).
"_".$key.
" (".$key.
");";
413 $texttoinsert .=
"\n";
415 if (!empty($val[
'foreignkey'])) {
416 $tmp = explode(
'.', $val[
'foreignkey']);
417 if (!empty($tmp[0]) && !empty($tmp[1])) {
418 $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].
");";
419 $texttoinsert .=
"\n";
424 $texttoinsert .=
'-- END MODULEBUILDER INDEXES';
426 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
428 dol_mkdir(dirname($pathoffiletoedittarget));
430 $result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
432 dolChmod($pathoffiletoedittarget, $newmask);
435 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
438 return $error ? -1 : 1;
450 $listofobject =
dol_dir_list($destdir.
'/class',
'files', 0,
'\.class\.php$');
451 foreach ($listofobject as $fileobj) {
452 if (preg_match(
'/^api_/', $fileobj[
'name'])) {
455 if (preg_match(
'/^actions_/', $fileobj[
'name'])) {
459 $tmpcontent = file_get_contents($fileobj[
'fullname']);
461 if (preg_match(
'/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) {
462 $objectnameloop = $reg[1];
463 $objects[$fileobj[
'fullname']] = $objectnameloop;
466 if (count($objects) > 0) {
482 if (!file_exists($file)) {
486 $content = file_get_contents($file);
489 if (strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU MYOBJECT */') !==
false
490 || strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU */') !==
false) {
493 if (strpos($content,
'/* END MODULEBUILDER TOPMENU MYOBJECT */') !==
false
494 || strpos($content,
'/* END MODULEBUILDER TOPMENU */') !==
false) {
497 if (strpos($content,
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
500 if (strpos($content,
'/* END MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
507 } elseif ($number === 1) {
508 if (strpos($content,
'/* BEGIN MODULEBUILDER PERMISSIONS */') !==
false && strpos($content,
'/* END MODULEBUILDER PERMISSIONS */') !==
false) {
511 } elseif ($number == 2) {
512 if (strpos($content,
'/* BEGIN MODULEBUILDER DICTIONARIES */') !==
false && strpos($content,
'/* END MODULEBUILDER DICTIONARIES */') !==
false) {
526 $start =
"/* BEGIN MODULEBUILDER PERMISSIONS */";
527 $end =
"/* END MODULEBUILDER PERMISSIONS */";
530 $lines = file($file);
532 foreach ($lines as $i => $line) {
533 if (strpos($line, $start) !==
false) {
534 $start_line = $i + 1;
537 while (($line = $lines[++$i]) !==
false) {
538 if (strpos($line, $end) !==
false) {
547 $allContent = implode(
"", $array);
559 return strcmp($a[0], $b[0]);
576 if ($action == 0 && $key !==
null) {
578 array_splice($permissions, array_search($permissions[$key], $permissions), 1);
579 } elseif ($action == 1) {
580 array_push($permissions, $right);
581 } elseif ($action == 2 && !empty($right) && $key !==
null) {
583 array_splice($permissions, array_search($permissions[$key], $permissions), 1, $right);
584 } elseif ($action == -1 && !empty($objectname)) {
588 foreach ($permissions as $perms) {
589 if ($perms[4] === strtolower($objectname)) {
590 array_splice($permissions, array_search($perms, $permissions), 1);
593 } elseif ($action == -2 && !empty($objectname) && !empty($module)) {
596 $objectOfRights = array();
598 foreach ($permissions as $right) {
599 $objectOfRights[] = $right[4];
601 if (in_array(strtolower($objectname), $objectOfRights)) {
604 $permsToadd = array();
606 'read' =>
'Read '.$objectname.
' object of '.ucfirst($module),
607 'write' =>
'Create/Update '.$objectname.
' object of '.ucfirst($module),
608 'delete' =>
'Delete '.$objectname.
' object of '.ucfirst($module)
611 foreach ($perms as $index => $value) {
612 $permsToadd[$i][0] =
'';
613 $permsToadd[$i][1] = $value;
614 $permsToadd[$i][4] = strtolower($objectname);
615 $permsToadd[$i][5] = $index;
616 array_push($permissions, $permsToadd[$i]);
623 '@phan-var-force array<int,string[]> $permissions';
626 $count_perms = count($permissions);
627 foreach (array_keys($permissions) as $i) {
628 $permissions[$i][0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1)";
629 $permissions[$i][1] =
"\$this->rights[\$r][1] = '".$permissions[$i][1].
"'";
630 $permissions[$i][4] =
"\$this->rights[\$r][4] = '".$permissions[$i][4].
"'";
631 $permissions[$i][5] =
"\$this->rights[\$r][5] = '".$permissions[$i][5].
"';\n\t\t";
634 $perms_grouped = array();
635 foreach ($permissions as $perms) {
637 if (!isset($perms_grouped[
$object])) {
638 $perms_grouped[
$object] = array();
640 $perms_grouped[
$object][] = $perms;
643 $permissions = $perms_grouped;
648 foreach ($permissions as &
$object) {
652 if (str_contains($obj[5],
'read')) {
653 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 0 + 1)";
654 } elseif (str_contains($obj[5],
'write')) {
655 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 1 + 1)";
656 } elseif (str_contains($obj[5],
'delete')) {
657 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 2 + 1)";
659 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + ".$p.
" + 1)";
663 usort(
$object,
'compareFirstValue');
668 foreach ($permissions as $perms) {
669 foreach ($perms as $per) {
670 $rights[] = implode(
";\n\t\t", $per).
"\$r++;\n";
673 $rights_str = implode(
"\t\t", $rights);
677 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER PERMISSIONS */' =>
'/* BEGIN MODULEBUILDER PERMISSIONS */'.
"\n\t\t".$rights_str));
692 $string = str_replace(
"'",
'', $string);
695 preg_match_all(
'/\s*([^\s=>]+)\s*=>\s*([^,]+),?/', $string, $matches, PREG_SET_ORDER);
696 $propertyArray = array();
698 foreach ($matches as $match) {
699 $key = trim($match[1]);
700 $value = trim($match[2]);
702 if (strpos($value,
'array(') === 0) {
703 $nestedArray = substr($value, 6);
705 $value = $nestedArray;
706 } elseif (strpos($value,
'"Id")') !==
false) {
707 $value = str_replace(
')',
'', $value);
709 if (is_numeric($value)) {
710 if (strpos($value,
'.') !==
false) {
711 $value = (float) $value;
713 $value = (int) $value;
716 if ($value ===
'true') {
718 } elseif ($value ===
'false') {
723 $propertyArray[$key] = $value;
726 return $propertyArray;
740 $attributesUnique = array(
'type',
'label',
'enabled',
'position',
'notnull',
'visible',
'noteditable',
'index',
'default' ,
'foreignkey',
'arrayofkeyval',
'alwayseditable',
'validate',
'searchall',
'comment',
'isameasure',
'css',
'cssview',
'csslist',
'help',
'showoncombobox',
'picto' );
742 $start =
"public \$fields = array(";
746 $lines = file($file);
748 foreach ($lines as $i => $line) {
749 if (strpos($line, $start) !==
false) {
751 while (($line = $lines[++$i]) !==
false) {
752 if (strpos($line, $end) !==
false) {
761 $table =
"== DATA SPECIFICATIONS\n";
762 $table .=
"=== Table of fields with properties for object *$objectname* : \n";
763 $table .=
"[options='header',grid=rows,frame=topbot,width=100%,caption=Organisation]\n";
767 foreach ($attributesUnique as $attUnique) {
768 $table .=
"|".$attUnique;
771 $valuesModif = array();
772 foreach ($keys as $string) {
773 $string = trim($string,
"'");
774 $string = rtrim($string,
",");
780 foreach ($array as $key => $value) {
781 if (is_array($value)) {
785 $array[$code][$key] = $value;
790 if (!is_array($array)) {
793 $field = array_keys($array);
794 if ($field[0] ===
'') {
797 $values = array_values($array)[0];
800 foreach ($attributesUnique as $attUnique) {
801 if ($attUnique ==
'type' && $field[0] ===
'label') {
802 $values[$attUnique] =
'varchar(255)';
804 if (!array_key_exists($attUnique, $values)) {
805 $valuesModif[$attUnique] =
'';
807 $valuesModif[$attUnique] = $values[$attUnique];
810 $table .=
"|*" . $field[0] .
"*|";
811 $table .= implode(
"|", $valuesModif) .
"\n";
816 $table .=
"__ end table for object $objectname\n";
819 $writeInFile =
dolReplaceInFile($destfile, array(
'== DATA SPECIFICATIONS' => $table));
820 if ($writeInFile < 0) {
837 $start =
"== Table of fields and their properties for object *".ucfirst($objectname).
"* : ";
838 $end =
"__ end table for object ".ucfirst($objectname);
840 $str = file_get_contents($file);
842 $search =
'/' . preg_quote($start,
'/') .
'(.*?)' . preg_quote($end,
'/') .
'/s';
843 $new_contents = preg_replace($search,
'', $str);
844 file_put_contents($file, $new_contents);
847 $perms =
"|*".strtolower($objectname).
"*|";
848 $search_pattern_perms =
'/' . preg_quote($perms,
'/') .
'.*?\n/';
849 $new_contents = preg_replace($search_pattern_perms,
'', $new_contents);
850 file_put_contents($file, $new_contents);
866function getFromFile($file, $start, $end, $excludestart =
'', $includese = 0)
875 while ($line = fgets($fhandle)) {
876 if (strpos($line, $start) !==
false && (empty($excludestart) || strpos($line, $excludestart) ===
false)) {
881 while (($line = fgets($fhandle)) !==
false) {
882 if (strpos($line, $end) !==
false) {
896 $content = implode(
"", $keys);
910 $start =
'/* BEGIN MODULEBUILDER PERMISSIONS */';
911 $end =
'/* END MODULEBUILDER PERMISSIONS */';
913 if (empty($content)) {
917 $string =
"[options='header',grid=rows,width=60%,caption=Organisation]\n";
920 $header = array($langs->trans(
'Objects'),$langs->trans(
'Permission'));
921 foreach ($header as $h) {
926 $array = explode(
";", $content);
927 $permissions = array_filter($array);
929 $permissions = str_replace(
'$r++',
'1', $permissions);
932 foreach ($permissions as $i => $element) {
934 unset($permissions[$i]);
936 if (str_contains($element,
'$this->numero')) {
937 unset($permissions[$i]);
939 if (str_contains($element,
'$this->rights[$r][5]')) {
940 unset($permissions[$i]);
944 foreach ($permissions as $key => $element) {
945 $element = str_replace(
" '",
'', $element);
946 $element = trim($element,
"'");
947 $permsN[] = substr($element, strpos($element,
"=") + 1);
952 $final_array = array();
954 while ($index < count($permsN)) {
955 $temp_array = array($permsN[$index], $permsN[$index + 1]);
956 $final_array[] = $temp_array;
961 foreach ($final_array as $subarray) {
965 $result[$key][] = $subarray;
967 foreach ($result as $i => $pems) {
968 $string .=
"|*".$i.
"*|";
969 foreach ($pems as $tab) {
970 $string .= $tab[0].
" , ";
975 $string .=
"\n|===\n";
977 $write =
dolReplaceInFile($destfile, array(
'__DATA_PERMISSIONS__' => $string));
995 global $langs, $user;
997 if (!file_exists($file)) {
1002 $content = file($file);
1004 $includeClass =
"dol_include_once\(\'\/\w+\/class\/\w+\.class\.php\'\);";
1005 $props =
'public\s+\$\w+;';
1006 $varcommented =
'@var\s+\w+\s+\$\w+\s+{@type\s+\w+}';
1007 $constructObj =
'\$this->\w+\s+=\s+new\s+\w+\(\$this->db\);';
1010 foreach ($content as $lineNumber => &$lineContent) {
1011 if (preg_match(
'/'.$varcommented.
'/', $lineContent)) {
1013 foreach ($objects as $objectname) {
1014 $lineContent .=
"\t * @var ".$objectname.
" \$".strtolower($objectname).
" {@type ".$objectname.
"}". PHP_EOL;
1017 } elseif (preg_match(
'/'.$props.
'/', $lineContent)) {
1019 foreach ($objects as $objectname) {
1020 $lineContent .=
"\t/*".PHP_EOL.
"\t * @var mixed TODO: set type".PHP_EOL.
"\t */".PHP_EOL.
"\tpublic \$".strtolower($objectname).
";". PHP_EOL;
1022 } elseif (preg_match(
'/'.$constructObj.
'/', $lineContent)) {
1024 foreach ($objects as $objectname) {
1025 $lineContent .=
"\t\t\$this->".strtolower($objectname).
" = new ".$objectname.
"(\$this->db);". PHP_EOL;
1027 } elseif (preg_match(
'/'.$includeClass.
'/', $lineContent)) {
1029 foreach ($objects as $objectname) {
1030 $lineContent .=
"dol_include_once('/".strtolower($modulename).
"/class/".strtolower($objectname).
".class.php');". PHP_EOL;
1035 $allContent = implode(
"", $content);
1036 file_put_contents($file, $allContent);
1039 $allContent =
getFromFile($srcfile,
'/* BEGIN MODULEBUILDER API MYOBJECT */',
'/* END MODULEBUILDER API MYOBJECT */');
1040 foreach ($objects as $objectname) {
1041 $arrayreplacement = array(
1042 'mymodule' => strtolower($modulename),
1043 'MyModule' => $modulename,
1044 'MYMODULE' => strtoupper($modulename),
1045 'My module' => $modulename,
1046 'my module' => $modulename,
1047 'Mon module' => $modulename,
1048 'mon module' => $modulename,
1049 'htdocs/modulebuilder/template' => strtolower($modulename),
1050 'myobject' => strtolower($objectname),
1051 'MyObject' => $objectname,
1052 'MYOBJECT' => strtoupper($objectname),
1053 '---Replace with your own copyright and developer email---' =>
dol_print_date($now,
'%Y').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
'')
1059 '/* BEGIN MODULEBUILDER API MYOBJECT */' =>
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */'.$contentReplaced.
"\t".
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */'.
"\n\n\n\t".
'/* BEGIN MODULEBUILDER API MYOBJECT */'
1080 if (!file_exists($file)) {
1084 $content = file($file);
1086 $includeClass =
"dol_include_once\(\'\/\w+\/class\/".strtolower($objectname).
"\.class\.php\'\);";
1087 $props =
'public\s+\$'.strtolower($objectname);
1088 $varcommented =
'@var\s+\w+\s+\$'.strtolower($objectname).
'\s+{@type\s+\w+}';
1089 $constructObj =
'\$this->'.strtolower($objectname).
'\s+=\s+new\s+\w+\(\$this->db\);';
1092 foreach ($content as $lineNumber => &$lineContent) {
1093 if (preg_match(
'/'.$varcommented.
'/i', $lineContent)) {
1095 } elseif (preg_match(
'/'.$props.
'/i', $lineContent)) {
1097 } elseif (preg_match(
'/'.$constructObj.
'/i', $lineContent)) {
1099 } elseif (preg_match(
'/'.$includeClass.
'/i', $lineContent)) {
1104 $allContent = implode(
"", $content);
1105 file_put_contents($file, $allContent);
1108 $begin =
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */';
1109 $end =
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */';
1132 if (!file_exists($file)) {
1136 if ($action == 0 && !empty($key)) {
1138 array_splice($menus, array_search($menus[$key], $menus), 1);
1139 } elseif ($action == 1) {
1141 array_push($menus, $menuWantTo);
1142 } elseif ($action == 2 && !empty($key) && !empty($menuWantTo)) {
1146 foreach ($menus as $index => $menu) {
1147 if ($index !== $key) {
1148 if ($menu[
'type'] === $menuWantTo[
'type']) {
1149 if (strcasecmp(str_replace(
' ',
'', $menu[
'titre']), str_replace(
' ',
'', $menuWantTo[
'titre'])) === 0) {
1152 if (strcasecmp(str_replace(
' ',
'', $menu[
'url']), str_replace(
' ',
'', $menuWantTo[
'url'])) === 0) {
1158 if (!$counter && $urlCounter < 2) {
1159 $menus[$key] = $menuWantTo;
1163 } elseif ($action == -1 && !empty($menuWantTo) && is_string($menuWantTo)) {
1165 foreach ($menus as $index => $menu) {
1166 if ((strpos(strtolower($menu[
'fk_menu']), strtolower($menuWantTo)) !==
false) || (strpos(strtolower($menu[
'leftmenu']), strtolower($menuWantTo)) !==
false)) {
1167 array_splice($menus, array_search($menu, $menus), 1);
1175 $beginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU';
1176 $excludeBeginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT';
1177 $endMenu =
'/* END MODULEBUILDER LEFTMENU';
1179 while ($protection <= 1000 && $allMenus =
getFromFile($file, $beginMenu, $endMenu, $excludeBeginMenu, 1)) {
1186 foreach ($menus as $index => $menu) {
1187 $menu[
'position'] =
"1000 + \$r";
1188 if ($menu[
'type'] ===
'left') {
1189 $start =
"\t\t".
'/* BEGIN MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1190 $end =
"\t\t".
'/* END MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1192 $val_actuel = $menu;
1193 $next_val = empty($menus[$index + 1]) ? null : $menus[$index + 1];
1196 $str_menu .= $start.
"\n";
1197 $str_menu .=
"\t\t\$this->menu[\$r++] = array(\n";
1198 $str_menu .=
"\t\t\t'fk_menu' => '".dol_escape_php($menu[
'fk_menu'], 1).
"',\n";
1199 $str_menu .=
"\t\t\t'type' => '".dol_escape_php($menu[
'type'], 1).
"',\n";
1200 $str_menu .=
"\t\t\t'titre' => '".dol_escape_php($menu[
'titre'], 1).
"',\n";
1201 $str_menu .=
"\t\t\t'mainmenu' => '".dol_escape_php($menu[
'mainmenu'], 1).
"',\n";
1202 $str_menu .=
"\t\t\t'leftmenu' => '".dol_escape_php($menu[
'leftmenu'], 1).
"',\n";
1203 $str_menu .=
"\t\t\t'url' => '".dol_escape_php($menu[
'url'], 1).
"',\n";
1204 $str_menu .=
"\t\t\t'langs' => '".dol_escape_php($menu[
'langs'], 1).
"',\n";
1205 $str_menu .=
"\t\t\t'position' => ".((int) $menu[
'position']).
",\n";
1206 $str_menu .=
"\t\t\t'enabled' => '".dol_escape_php((
string) $menu[
'enabled'], 1).
"',\n";
1207 $str_menu .=
"\t\t\t'perms' => '".dol_escape_php($menu[
'perms'], 1).
"',\n";
1208 $str_menu .=
"\t\t\t'target' => '".dol_escape_php($menu[
'target'], 1).
"',\n";
1209 $str_menu .=
"\t\t\t'user' => ".((int) $menu[
'user']).
",\n";
1210 $str_menu .=
"\t\t\t'object' => '".dol_escape_php($menu[
'object'], 1).
"',\n";
1211 $str_menu .=
"\t\t);\n";
1213 if (is_null($next_val) || $val_actuel[
'leftmenu'] !== $next_val[
'leftmenu']) {
1214 $str_menu .= $end.
"\n";
1219 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */' => $str_menu.
"\n\t\t/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */"));
1236 $dicData =
"\t\t\$this->dictionaries = array(\n";
1237 $module = strtolower($module);
1238 foreach ($dicts as $key => $value) {
1239 if (empty($value)) {
1241 $dicData =
"\t\t\$this->dictionaries = array();";
1245 $dicData .=
"\t\t\t'$key' => ";
1247 if ($key ===
'tabcond') {
1248 $conditions = array_map(
1253 static function ($val) use ($module) {
1254 return is_bool($val) ?
"isModEnabled('$module')" : $val;
1258 $dicData .=
"array(" . implode(
", ", $conditions) .
")";
1259 } elseif ($key ===
'tabhelp') {
1260 $helpItems = array();
1261 foreach ($value as $helpValue) {
1262 $helpItems[] =
"array('code' => \$langs->trans('".$helpValue[
'code'].
"'), 'field2' => 'field2tooltip')";
1264 $dicData .=
"array(" . implode(
",", $helpItems) .
")";
1266 if (is_array($value)) {
1267 $dicData .=
"array(" . implode(
1274 static function ($val) {
1281 $dicData .=
"'$value'";
1286 $dicData .= (!$isEmpty ?
"\t\t);" :
'');
1288 $stringDic =
getFromFile($file,
'/* BEGIN MODULEBUILDER DICTIONARIES */',
'/* END MODULEBUILDER DICTIONARIES */');
1289 $writeInfile =
dolReplaceInFile($file, array($stringDic => $dicData.
"\n"));
1291 return $writeInfile;
1310 if (empty($namedic)) {
1314 if (!file_exists($file)) {
1317 $modulename = strtolower($modulename);
1319 if (empty($dictionnaires)) {
1320 $dictionnaires = array(
'langs' =>
'',
'tabname' => array(),
'tablib' => array(),
'tabsql' => array(),
'tabsqlsort' => array(),
'tabfield' => array(),
'tabfieldvalue' => array(),
'tabfieldinsert' => array(),
'tabrowid' => array(),
'tabcond' => array(),
'tabhelp' => array());
1324 'rowid' => array(
'type' =>
'integer',
'value' => 11,
'extra' =>
'AUTO_INCREMENT'),
1325 'code' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1326 'label' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1327 'position' => array(
'type' =>
'integer',
'value' => 11,
'null' =>
'NULL'),
1328 'use_default' => array(
'type' =>
'varchar',
'value' => 11,
'default' =>
'1'),
1329 'active' => array(
'type' =>
'integer',
'value' => 3)
1332 $primaryKey =
'rowid';
1333 foreach ($columns as $key => $value) {
1334 if ($key ===
'rowid') {
1335 $primaryKey =
'rowid';
1338 if (!array_key_exists(
'rowid', $columns)) {
1339 $primaryKey = array_key_first($columns);
1345 $checkTable = $db->DDLDescTable(MAIN_DB_PREFIX.strtolower($namedic));
1346 if ($checkTable && $db->num_rows($checkTable) > 0) {
1347 setEventMessages($langs->trans(
"ErrorTableExist", $namedic),
null,
'errors');
1350 $_results = $db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey,
"");
1351 if ($_results < 0) {
1353 $langs->load(
"errors");
1354 setEventMessages($langs->trans(
"ErrorTableNotFound", $namedic),
null,
'errors');
1359 $dictionnaires[
'langs'] = $modulename.
'@'.$modulename;
1360 $dictionnaires[
'tabname'][] = strtolower($namedic);
1361 $dictionnaires[
'tablib'][] = ucfirst(substr($namedic, 2));
1362 $dictionnaires[
'tabsql'][] =
'SELECT t.rowid as rowid, t.code, t.label, t.active FROM '.MAIN_DB_PREFIX.strtolower($namedic).
' as t';
1363 $dictionnaires[
'tabsqlsort'][] = (array_key_exists(
'label', $columns) ?
'label ASC' :
'');
1364 $dictionnaires[
'tabfield'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1365 $dictionnaires[
'tabfieldvalue'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1366 $dictionnaires[
'tabfieldinsert'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1367 $dictionnaires[
'tabrowid'][] = $primaryKey;
1368 $dictionnaires[
'tabcond'][] = isModEnabled(
'$modulename');
1369 $dictionnaires[
'tabhelp'][] = (array_key_exists(
'code', $columns) ? array(
'code' => $langs->trans(
'CodeTooltipHelp'),
'field2' =>
'field2tooltip') :
'');
1373 if ($writeInfile > 0) {
1374 setEventMessages($langs->trans(
"DictionariesCreated", ucfirst(substr($namedic, 2))),
null);
1393 $string =
getFromFile($file_api,
'/*begin methods CRUD*/',
'/*end methods CRUD*/');
1394 $extractUrls = explode(
"\n", $string);
1397 $urlValues = array();
1398 foreach ($extractUrls as $key => $line) {
1399 $lineWithoutTabsSpaces = preg_replace(
'/^[\t\s]+/',
'', $line);
1400 if (strpos($lineWithoutTabsSpaces,
'* @url') === 0) {
1401 $urlValue = trim(substr($lineWithoutTabsSpaces, strlen(
'* @url')));
1402 $urlValues[] = $urlValue;
1407 $str = $_SERVER[
'HTTP_HOST'].
'/api/index.php/';
1408 $groupedUrls = array();
1409 foreach ($urlValues as $url) {
1410 if (preg_match(
'/(?:GET|POST|PUT|DELETE) (\w+)s/', $url, $matches)) {
1411 $objectName = $matches[1];
1412 $url = $str.trim(strstr($url,
' '));
1413 $groupedUrls[$objectName][] = $url;
1416 if (empty($groupedUrls)) {
1422 $asciiDocTable =
"[options=\"header\"]\n|===\n|Object | URLs\n";
1423 foreach ($groupedUrls as $objectName => $urls) {
1424 $urlsList = implode(
" +\n*", $urls);
1425 $asciiDocTable .=
"|$objectName | \n*$urlsList +\n";
1427 $asciiDocTable .=
"|===\n";
1428 $file_write =
dolReplaceInFile($file_doc, array(
'__API_DOC__' =>
'__API_DOC__'.
"\n".$asciiDocTable));
1429 if ($file_write < 0) {
1446 if (!is_dir($path)) {
1450 $allFilesAndDirs = scandir($path);
1453 foreach ($allFilesAndDirs as $item) {
1454 if ($item !=
'.' && $item !=
'..') {
1455 if ($type == 1 && is_file($path . DIRECTORY_SEPARATOR . $item) && strpos($item,
'.back') ===
false) {
1457 } 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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
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...
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)
Create 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 values.
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.