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)/', $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');
75 $pathoffiletoeditsrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
76 $pathoffiletoedittarget = $destdir.
'/class/'.strtolower($objectname).
'.class.php'.($readdir != $destdir ?
'.new' :
'');
78 $langs->load(
"errors");
79 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
87 include_once $pathoffiletoeditsrc;
88 if (class_exists($objectname)) {
93 '@phan-var-force CommonObject $object';
96 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
99 $contentclass = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
103 if (is_array($addfieldentry) && count($addfieldentry)) {
104 $name = $addfieldentry[
'name'];
105 unset($addfieldentry[
'name']);
107 $object->fields[$name] = $addfieldentry;
109 if (!empty($delfieldentry)) {
110 $name = $delfieldentry;
118 $texttoinsert =
'// BEGIN MODULEBUILDER PROPERTIES'.
"\n";
119 $texttoinsert .=
"\t".
'
123 $texttoinsert .=
"\t".
'public $fields = array('.
"\n";
126 foreach (
$object->fields as $key => $val) {
128 $texttoinsert .=
"\t\t".
'"'.$key.
'" => array(';
129 $texttoinsert .=
'"type" => "'.dol_escape_php($val[
'type']).
'",';
130 $texttoinsert .=
' "label" => "'.dol_escape_php($val[
'label']).
'",';
131 if (!empty($val[
'picto'])) {
132 $texttoinsert .=
' "picto" => "'.dol_escape_php($val[
'picto']).
'",';
134 $texttoinsert .=
' "enabled" => "'.($val[
'enabled'] !==
'' ?
dol_escape_php($val[
'enabled']) : 1).
'",';
135 $texttoinsert .=
" 'position' => ".($val[
'position'] !==
'' ? (int) $val[
'position'] : 50).
",";
136 $texttoinsert .=
" 'notnull' => ".(empty($val[
'notnull']) ? 0 : (int) $val[
'notnull']).
",";
137 $texttoinsert .=
' "visible" => "'.($val[
'visible'] !==
'' ?
dol_escape_js($val[
'visible']) : -1).
'",';
138 if (!empty($val[
'noteditable'])) {
139 $texttoinsert .=
' "noteditable" => "'.dol_escape_php($val[
'noteditable']).
'",';
141 if (!empty($val[
'alwayseditable'])) {
142 $texttoinsert .=
' "alwayseditable" => "'.dol_escape_php($val[
'alwayseditable']).
'",';
144 if (array_key_exists(
'default', $val) && (!empty($val[
'default']) || $val[
'default'] ===
'0')) {
145 $texttoinsert .=
' "default" => "'.dol_escape_php($val[
'default']).
'",';
147 if (!empty($val[
'index'])) {
148 $texttoinsert .=
' "index" => "'.(int) $val[
'index'].
'",';
150 if (!empty($val[
'foreignkey'])) {
151 $texttoinsert .=
' "foreignkey" => "'.(int) $val[
'foreignkey'].
'",';
153 if (!empty($val[
'searchall'])) {
154 $texttoinsert .=
' "searchall" => "'.(int) $val[
'searchall'].
'",';
156 if (!empty($val[
'isameasure'])) {
157 $texttoinsert .=
' "isameasure" => "'.(int) $val[
'isameasure'].
'",';
159 if (!empty($val[
'css'])) {
160 $texttoinsert .=
' "css" => "'.dol_escape_php($val[
'css']).
'",';
162 if (!empty($val[
'cssview'])) {
163 $texttoinsert .=
' "cssview" => "'.dol_escape_php($val[
'cssview']).
'",';
165 if (!empty($val[
'csslist'])) {
166 $texttoinsert .=
' "csslist" => "'.dol_escape_php($val[
'csslist']).
'",';
168 if (!empty($val[
'help'])) {
169 $texttoinsert .=
' "help" => "'.dol_escape_php($val[
'help']).
'",';
171 if (!empty($val[
'showoncombobox'])) {
172 $texttoinsert .=
' "showoncombobox" => "'.(int) $val[
'showoncombobox'].
'",';
174 if (!empty($val[
'disabled'])) {
175 $texttoinsert .=
' "disabled" => "'.(int) $val[
'disabled'].
'",';
177 if (!empty($val[
'autofocusoncreate'])) {
178 $texttoinsert .=
' "autofocusoncreate" => "'.(int) $val[
'autofocusoncreate'].
'",';
180 if (!empty($val[
'arrayofkeyval'])) {
181 $texttoinsert .=
' "arrayofkeyval" => array(';
183 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
185 $texttoinsert .=
", ";
187 $texttoinsert .=
'"'.dol_escape_php($key2).
'" => "'.
dol_escape_php($val2).
'"';
190 $texttoinsert .=
'),';
192 if (!empty($val[
'validate'])) {
193 $texttoinsert .=
' "validate" => "'.(int) $val[
'validate'].
'",';
195 if (!empty($val[
'comment'])) {
196 $texttoinsert .=
' "comment" => "'.dol_escape_php($val[
'comment']).
'"';
199 $texttoinsert .=
"),\n";
204 $texttoinsert .=
"\t".
');'.
"\n";
210 foreach (
$object->fields as $key => $val) {
213 $texttoinsert .=
"\t".
'public $'.$key.
";";
218 $texttoinsert .=
"\n";
222 $texttoinsert .=
"\t".
'// END MODULEBUILDER PROPERTIES';
226 $contentclass = preg_replace(
'/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
229 dol_mkdir(dirname($pathoffiletoedittarget));
232 $result = file_put_contents(
dol_osencode($pathoffiletoedittarget), $contentclass);
235 dolChmod($pathoffiletoedittarget, $newmask);
242 print $e->getMessage();
266 if (empty($objectname)) {
269 if (empty($readdir)) {
273 $pathoffiletoclasssrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
276 if ($moduletype ==
'internal') {
277 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
278 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
279 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
280 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
281 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
282 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
283 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'.sql';
288 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
289 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
290 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
291 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
292 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
293 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
294 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'.sql';
301 $pathoffiletoedittarget = $destdir.$pathoffiletoeditsrc.($readdir != $destdir ?
'.new' :
'');
302 $pathoffiletoeditsrc = $readdir.$pathoffiletoeditsrc;
305 $langs->load(
"errors");
306 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
313 include_once $pathoffiletoclasssrc;
314 if (class_exists($objectname)) {
315 $object =
new $objectname($db);
321 print $e->getMessage();
325 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
327 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
330 $texttoinsert =
'-- BEGIN MODULEBUILDER FIELDS'.
"\n";
332 foreach (
$object->fields as $key => $val) {
335 $type = $val[
'type'];
336 $type = preg_replace(
'/:.*$/',
'', $type);
338 if ($type ==
'html') {
340 } elseif ($type ==
'price') {
342 } elseif (in_array($type, array(
'link',
'sellist',
'duration'))) {
344 } elseif ($type ==
'mail') {
345 $type =
'varchar(128)';
346 } elseif ($type ==
'phone') {
347 $type =
'varchar(20)';
348 } elseif ($type ==
'ip') {
349 $type =
'varchar(32)';
352 $texttoinsert .=
"\t".$key.
" ".$type;
353 if ($key ==
'rowid') {
354 $texttoinsert .=
' AUTO_INCREMENT PRIMARY KEY';
355 } elseif ($type ==
'timestamp') {
356 $texttoinsert .=
' DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
358 if ($key ==
'entity') {
359 $texttoinsert .=
' DEFAULT 1';
361 if (!empty($val[
'default'])) {
362 if (preg_match(
'/^null$/i', $val[
'default'])) {
363 $texttoinsert .=
" DEFAULT NULL";
364 } elseif (preg_match(
'/varchar/', $type)) {
365 $texttoinsert .=
" DEFAULT '".$db->escape($val[
'default']).
"'";
367 $texttoinsert .= (($val[
'default'] > 0) ?
' DEFAULT '.$val[
'default'] :
'');
371 $texttoinsert .= ((!empty($val[
'notnull']) && $val[
'notnull'] > 0) ?
' NOT NULL' :
'');
372 if ($i < count(
$object->fields)) {
373 $texttoinsert .=
", ";
375 $texttoinsert .=
"\n";
378 $texttoinsert .=
"\t".
'-- END MODULEBUILDER FIELDS';
380 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
382 $result = file_put_contents($pathoffiletoedittarget, $contentsql);
384 dolChmod($pathoffiletoedittarget, $newmask);
387 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
391 $pathoffiletoeditsrc = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoeditsrc);
392 $pathoffiletoedittarget = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoedittarget);
393 $pathoffiletoedittarget = preg_replace(
'/\.sql.new$/',
'.key.sql.new', $pathoffiletoedittarget);
395 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
398 $texttoinsert =
'-- BEGIN MODULEBUILDER INDEXES'.
"\n";
400 foreach (
$object->fields as $key => $val) {
402 if (!empty($val[
'index'])) {
403 $texttoinsert .=
"ALTER TABLE llx_".strtolower($module).
'_'.strtolower($objectname).
" ADD INDEX idx_".strtolower($module).
'_'.strtolower($objectname).
"_".$key.
" (".$key.
");";
404 $texttoinsert .=
"\n";
406 if (!empty($val[
'foreignkey'])) {
407 $tmp = explode(
'.', $val[
'foreignkey']);
408 if (!empty($tmp[0]) && !empty($tmp[1])) {
409 $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].
");";
410 $texttoinsert .=
"\n";
415 $texttoinsert .=
'-- END MODULEBUILDER INDEXES';
417 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
419 dol_mkdir(dirname($pathoffiletoedittarget));
421 $result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
423 dolChmod($pathoffiletoedittarget, $newmask);
426 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
429 return $error ? -1 : 1;
441 $listofobject =
dol_dir_list($destdir.
'/class',
'files', 0,
'\.class\.php$');
442 foreach ($listofobject as $fileobj) {
443 if (preg_match(
'/^api_/', $fileobj[
'name'])) {
446 if (preg_match(
'/^actions_/', $fileobj[
'name'])) {
450 $tmpcontent = file_get_contents($fileobj[
'fullname']);
452 if (preg_match(
'/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) {
453 $objectnameloop = $reg[1];
454 $objects[$fileobj[
'fullname']] = $objectnameloop;
457 if (count($objects) > 0) {
473 if (!file_exists($file)) {
477 $content = file_get_contents($file);
480 if (strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU MYOBJECT */') !==
false
481 || strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU */') !==
false) {
484 if (strpos($content,
'/* END MODULEBUILDER TOPMENU MYOBJECT */') !==
false
485 || strpos($content,
'/* END MODULEBUILDER TOPMENU */') !==
false) {
488 if (strpos($content,
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
491 if (strpos($content,
'/* END MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
498 } elseif ($number === 1) {
499 if (strpos($content,
'/* BEGIN MODULEBUILDER PERMISSIONS */') !==
false && strpos($content,
'/* END MODULEBUILDER PERMISSIONS */') !==
false) {
502 } elseif ($number == 2) {
503 if (strpos($content,
'/* BEGIN MODULEBUILDER DICTIONARIES */') !==
false && strpos($content,
'/* END MODULEBUILDER DICTIONARIES */') !==
false) {
517 $start =
"/* BEGIN MODULEBUILDER PERMISSIONS */";
518 $end =
"/* END MODULEBUILDER PERMISSIONS */";
521 $lines = file($file);
523 foreach ($lines as $i => $line) {
524 if (strpos($line, $start) !==
false) {
525 $start_line = $i + 1;
528 while (($line = $lines[++$i]) !==
false) {
529 if (strpos($line, $end) !==
false) {
538 $allContent = implode(
"", $array);
550 return strcmp($a[0], $b[0]);
567 if ($action == 0 && $key !==
null) {
569 array_splice($permissions, array_search($permissions[$key], $permissions), 1);
570 } elseif ($action == 1) {
571 array_push($permissions, $right);
572 } elseif ($action == 2 && !empty($right) && $key !==
null) {
574 array_splice($permissions, array_search($permissions[$key], $permissions), 1, $right);
575 } elseif ($action == -1 && !empty($objectname)) {
579 foreach ($permissions as $perms) {
580 if ($perms[4] === strtolower($objectname)) {
581 array_splice($permissions, array_search($perms, $permissions), 1);
584 } elseif ($action == -2 && !empty($objectname) && !empty($module)) {
587 $objectOfRights = array();
589 foreach ($permissions as $right) {
590 $objectOfRights[] = $right[4];
592 if (in_array(strtolower($objectname), $objectOfRights)) {
595 $permsToadd = array();
597 'read' =>
'Read '.$objectname.
' object of '.ucfirst($module),
598 'write' =>
'Create/Update '.$objectname.
' object of '.ucfirst($module),
599 'delete' =>
'Delete '.$objectname.
' object of '.ucfirst($module)
602 foreach ($perms as $index => $value) {
603 $permsToadd[$i][0] =
'';
604 $permsToadd[$i][1] = $value;
605 $permsToadd[$i][4] = strtolower($objectname);
606 $permsToadd[$i][5] = $index;
607 array_push($permissions, $permsToadd[$i]);
614 '@phan-var-force array<int,string[]> $permissions';
617 $count_perms = count($permissions);
618 foreach (array_keys($permissions) as $i) {
619 $permissions[$i][0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1)";
620 $permissions[$i][1] =
"\$this->rights[\$r][1] = '".$permissions[$i][1].
"'";
621 $permissions[$i][4] =
"\$this->rights[\$r][4] = '".$permissions[$i][4].
"'";
622 $permissions[$i][5] =
"\$this->rights[\$r][5] = '".$permissions[$i][5].
"';\n\t\t";
625 $perms_grouped = array();
626 foreach ($permissions as $perms) {
628 if (!isset($perms_grouped[
$object])) {
629 $perms_grouped[
$object] = array();
631 $perms_grouped[
$object][] = $perms;
634 $permissions = $perms_grouped;
639 foreach ($permissions as &
$object) {
643 if (str_contains($obj[5],
'read')) {
644 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 0 + 1)";
645 } elseif (str_contains($obj[5],
'write')) {
646 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 1 + 1)";
647 } elseif (str_contains($obj[5],
'delete')) {
648 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 2 + 1)";
650 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + ".$p.
" + 1)";
654 usort(
$object,
'compareFirstValue');
659 foreach ($permissions as $perms) {
660 foreach ($perms as $per) {
661 $rights[] = implode(
";\n\t\t", $per).
"\$r++;\n";
664 $rights_str = implode(
"\t\t", $rights);
668 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER PERMISSIONS */' =>
'/* BEGIN MODULEBUILDER PERMISSIONS */'.
"\n\t\t".$rights_str));
683 $string = str_replace(
"'",
'', $string);
686 preg_match_all(
'/\s*([^\s=>]+)\s*=>\s*([^,]+),?/', $string, $matches, PREG_SET_ORDER);
687 $propertyArray = array();
689 foreach ($matches as $match) {
690 $key = trim($match[1]);
691 $value = trim($match[2]);
693 if (strpos($value,
'array(') === 0) {
694 $nestedArray = substr($value, 6);
696 $value = $nestedArray;
697 } elseif (strpos($value,
'"Id")') !==
false) {
698 $value = str_replace(
')',
'', $value);
700 if (is_numeric($value)) {
701 if (strpos($value,
'.') !==
false) {
702 $value = (float) $value;
704 $value = (int) $value;
707 if ($value ===
'true') {
709 } elseif ($value ===
'false') {
714 $propertyArray[$key] = $value;
717 return $propertyArray;
731 $attributesUnique = array(
'type',
'label',
'enabled',
'position',
'notnull',
'visible',
'noteditable',
'index',
'default' ,
'foreignkey',
'arrayofkeyval',
'alwayseditable',
'validate',
'searchall',
'comment',
'isameasure',
'css',
'cssview',
'csslist',
'help',
'showoncombobox',
'picto' );
733 $start =
"public \$fields = array(";
737 $lines = file($file);
739 foreach ($lines as $i => $line) {
740 if (strpos($line, $start) !==
false) {
742 while (($line = $lines[++$i]) !==
false) {
743 if (strpos($line, $end) !==
false) {
752 $table =
"== DATA SPECIFICATIONS\n";
753 $table .=
"=== Table of fields with properties for object *$objectname* : \n";
754 $table .=
"[options='header',grid=rows,frame=topbot,width=100%,caption=Organisation]\n";
758 foreach ($attributesUnique as $attUnique) {
759 $table .=
"|".$attUnique;
762 $valuesModif = array();
763 foreach ($keys as $string) {
764 $string = trim($string,
"'");
765 $string = rtrim($string,
",");
771 foreach ($array as $key => $value) {
772 if (is_array($value)) {
776 $array[$code][$key] = $value;
781 if (!is_array($array)) {
784 $field = array_keys($array);
785 if ($field[0] ===
'') {
788 $values = array_values($array)[0];
791 foreach ($attributesUnique as $attUnique) {
792 if ($attUnique ==
'type' && $field[0] ===
'label') {
793 $values[$attUnique] =
'varchar(255)';
795 if (!array_key_exists($attUnique, $values)) {
796 $valuesModif[$attUnique] =
'';
798 $valuesModif[$attUnique] = $values[$attUnique];
801 $table .=
"|*" . $field[0] .
"*|";
802 $table .= implode(
"|", $valuesModif) .
"\n";
807 $table .=
"__ end table for object $objectname\n";
810 $writeInFile =
dolReplaceInFile($destfile, array(
'== DATA SPECIFICATIONS' => $table));
811 if ($writeInFile < 0) {
828 $start =
"== Table of fields and their properties for object *".ucfirst($objectname).
"* : ";
829 $end =
"__ end table for object ".ucfirst($objectname);
831 $str = file_get_contents($file);
833 $search =
'/' . preg_quote($start,
'/') .
'(.*?)' . preg_quote($end,
'/') .
'/s';
834 $new_contents = preg_replace($search,
'', $str);
835 file_put_contents($file, $new_contents);
838 $perms =
"|*".strtolower($objectname).
"*|";
839 $search_pattern_perms =
'/' . preg_quote($perms,
'/') .
'.*?\n/';
840 $new_contents = preg_replace($search_pattern_perms,
'', $new_contents);
841 file_put_contents($file, $new_contents);
857function getFromFile($file, $start, $end, $excludestart =
'', $includese = 0)
866 while ($line = fgets($fhandle)) {
867 if (strpos($line, $start) !==
false && (empty($excludestart) || strpos($line, $excludestart) ===
false)) {
872 while (($line = fgets($fhandle)) !==
false) {
873 if (strpos($line, $end) !==
false) {
887 $content = implode(
"", $keys);
901 $start =
'/* BEGIN MODULEBUILDER PERMISSIONS */';
902 $end =
'/* END MODULEBUILDER PERMISSIONS */';
904 if (empty($content)) {
908 $string =
"[options='header',grid=rows,width=60%,caption=Organisation]\n";
911 $header = array($langs->trans(
'Objects'),$langs->trans(
'Permission'));
912 foreach ($header as $h) {
917 $array = explode(
";", $content);
918 $permissions = array_filter($array);
920 $permissions = str_replace(
'$r++',
'1', $permissions);
923 foreach ($permissions as $i => $element) {
925 unset($permissions[$i]);
927 if (str_contains($element,
'$this->numero')) {
928 unset($permissions[$i]);
930 if (str_contains($element,
'$this->rights[$r][5]')) {
931 unset($permissions[$i]);
935 foreach ($permissions as $key => $element) {
936 $element = str_replace(
" '",
'', $element);
937 $element = trim($element,
"'");
938 $permsN[] = substr($element, strpos($element,
"=") + 1);
943 $final_array = array();
945 while ($index < count($permsN)) {
946 $temp_array = array($permsN[$index], $permsN[$index + 1]);
947 $final_array[] = $temp_array;
952 foreach ($final_array as $subarray) {
956 $result[$key][] = $subarray;
958 foreach ($result as $i => $pems) {
959 $string .=
"|*".$i.
"*|";
960 foreach ($pems as $tab) {
961 $string .= $tab[0].
" , ";
966 $string .=
"\n|===\n";
968 $write =
dolReplaceInFile($destfile, array(
'__DATA_PERMISSIONS__' => $string));
986 global $langs, $user;
988 if (!file_exists($file)) {
993 $content = file($file);
995 $includeClass =
"dol_include_once\(\'\/\w+\/class\/\w+\.class\.php\'\);";
996 $props =
'public\s+\$\w+;';
997 $varcommented =
'@var\s+\w+\s+\$\w+\s+{@type\s+\w+}';
998 $constructObj =
'\$this->\w+\s+=\s+new\s+\w+\(\$this->db\);';
1001 foreach ($content as $lineNumber => &$lineContent) {
1002 if (preg_match(
'/'.$varcommented.
'/', $lineContent)) {
1004 foreach ($objects as $objectname) {
1005 $lineContent .=
"\t * @var ".$objectname.
" \$".strtolower($objectname).
" {@type ".$objectname.
"}". PHP_EOL;
1008 } elseif (preg_match(
'/'.$props.
'/', $lineContent)) {
1010 foreach ($objects as $objectname) {
1011 $lineContent .=
"\t/*".PHP_EOL.
"\t * @var mixed TODO: set type".PHP_EOL.
"\t */".PHP_EOL.
"\tpublic \$".strtolower($objectname).
";". PHP_EOL;
1013 } elseif (preg_match(
'/'.$constructObj.
'/', $lineContent)) {
1015 foreach ($objects as $objectname) {
1016 $lineContent .=
"\t\t\$this->".strtolower($objectname).
" = new ".$objectname.
"(\$this->db);". PHP_EOL;
1018 } elseif (preg_match(
'/'.$includeClass.
'/', $lineContent)) {
1020 foreach ($objects as $objectname) {
1021 $lineContent .=
"dol_include_once('/".strtolower($modulename).
"/class/".strtolower($objectname).
".class.php');". PHP_EOL;
1026 $allContent = implode(
"", $content);
1027 file_put_contents($file, $allContent);
1030 $allContent =
getFromFile($srcfile,
'/* BEGIN MODULEBUILDER API MYOBJECT */',
'/* END MODULEBUILDER API MYOBJECT */');
1031 foreach ($objects as $objectname) {
1032 $arrayreplacement = array(
1033 'mymodule' => strtolower($modulename),
1034 'MyModule' => $modulename,
1035 'MYMODULE' => strtoupper($modulename),
1036 'My module' => $modulename,
1037 'my module' => $modulename,
1038 'Mon module' => $modulename,
1039 'mon module' => $modulename,
1040 'htdocs/modulebuilder/template' => strtolower($modulename),
1041 'myobject' => strtolower($objectname),
1042 'MyObject' => $objectname,
1043 'MYOBJECT' => strtoupper($objectname),
1044 '---Replace with your own copyright and developer email---' =>
dol_print_date($now,
'%Y').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
'')
1050 '/* BEGIN MODULEBUILDER API MYOBJECT */' =>
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */'.$contentReplaced.
"\t".
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */'.
"\n\n\n\t".
'/* BEGIN MODULEBUILDER API MYOBJECT */'
1071 if (!file_exists($file)) {
1075 $content = file($file);
1077 $includeClass =
"dol_include_once\(\'\/\w+\/class\/".strtolower($objectname).
"\.class\.php\'\);";
1078 $props =
'public\s+\$'.strtolower($objectname);
1079 $varcommented =
'@var\s+\w+\s+\$'.strtolower($objectname).
'\s+{@type\s+\w+}';
1080 $constructObj =
'\$this->'.strtolower($objectname).
'\s+=\s+new\s+\w+\(\$this->db\);';
1083 foreach ($content as $lineNumber => &$lineContent) {
1084 if (preg_match(
'/'.$varcommented.
'/i', $lineContent)) {
1086 } elseif (preg_match(
'/'.$props.
'/i', $lineContent)) {
1088 } elseif (preg_match(
'/'.$constructObj.
'/i', $lineContent)) {
1090 } elseif (preg_match(
'/'.$includeClass.
'/i', $lineContent)) {
1095 $allContent = implode(
"", $content);
1096 file_put_contents($file, $allContent);
1099 $begin =
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */';
1100 $end =
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */';
1123 if (!file_exists($file)) {
1127 if ($action == 0 && !empty($key)) {
1129 array_splice($menus, array_search($menus[$key], $menus), 1);
1130 } elseif ($action == 1) {
1132 array_push($menus, $menuWantTo);
1133 } elseif ($action == 2 && !empty($key) && !empty($menuWantTo)) {
1137 foreach ($menus as $index => $menu) {
1138 if ($index !== $key) {
1139 if ($menu[
'type'] === $menuWantTo[
'type']) {
1140 if (strcasecmp(str_replace(
' ',
'', $menu[
'titre']), str_replace(
' ',
'', $menuWantTo[
'titre'])) === 0) {
1143 if (strcasecmp(str_replace(
' ',
'', $menu[
'url']), str_replace(
' ',
'', $menuWantTo[
'url'])) === 0) {
1149 if (!$counter && $urlCounter < 2) {
1150 $menus[$key] = $menuWantTo;
1154 } elseif ($action == -1 && !empty($menuWantTo) && is_string($menuWantTo)) {
1156 foreach ($menus as $index => $menu) {
1157 if ((strpos(strtolower($menu[
'fk_menu']), strtolower($menuWantTo)) !==
false) || (strpos(strtolower($menu[
'leftmenu']), strtolower($menuWantTo)) !==
false)) {
1158 array_splice($menus, array_search($menu, $menus), 1);
1166 $beginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU';
1167 $excludeBeginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT';
1168 $endMenu =
'/* END MODULEBUILDER LEFTMENU';
1170 while ($protection <= 1000 && $allMenus =
getFromFile($file, $beginMenu, $endMenu, $excludeBeginMenu, 1)) {
1177 foreach ($menus as $index => $menu) {
1178 $menu[
'position'] =
"1000 + \$r";
1179 if ($menu[
'type'] ===
'left') {
1180 $start =
"\t\t".
'/* BEGIN MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1181 $end =
"\t\t".
'/* END MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1183 $val_actuel = $menu;
1184 $next_val = empty($menus[$index + 1]) ? null : $menus[$index + 1];
1187 $str_menu .= $start.
"\n";
1188 $str_menu .=
"\t\t\$this->menu[\$r++] = array(\n";
1189 $str_menu .=
"\t\t\t'fk_menu' => '".dol_escape_php($menu[
'fk_menu'], 1).
"',\n";
1190 $str_menu .=
"\t\t\t'type' => '".dol_escape_php($menu[
'type'], 1).
"',\n";
1191 $str_menu .=
"\t\t\t'titre' => '".dol_escape_php($menu[
'titre'], 1).
"',\n";
1192 $str_menu .=
"\t\t\t'mainmenu' => '".dol_escape_php($menu[
'mainmenu'], 1).
"',\n";
1193 $str_menu .=
"\t\t\t'leftmenu' => '".dol_escape_php($menu[
'leftmenu'], 1).
"',\n";
1194 $str_menu .=
"\t\t\t'url' => '".dol_escape_php($menu[
'url'], 1).
"',\n";
1195 $str_menu .=
"\t\t\t'langs' => '".dol_escape_php($menu[
'langs'], 1).
"',\n";
1196 $str_menu .=
"\t\t\t'position' => ".((int) $menu[
'position']).
",\n";
1197 $str_menu .=
"\t\t\t'enabled' => '".dol_escape_php((
string) $menu[
'enabled'], 1).
"',\n";
1198 $str_menu .=
"\t\t\t'perms' => '".dol_escape_php($menu[
'perms'], 1).
"',\n";
1199 $str_menu .=
"\t\t\t'target' => '".dol_escape_php($menu[
'target'], 1).
"',\n";
1200 $str_menu .=
"\t\t\t'user' => ".((int) $menu[
'user']).
",\n";
1201 $str_menu .=
"\t\t\t'object' => '".dol_escape_php($menu[
'object'], 1).
"',\n";
1202 $str_menu .=
"\t\t);\n";
1204 if (is_null($next_val) || $val_actuel[
'leftmenu'] !== $next_val[
'leftmenu']) {
1205 $str_menu .= $end.
"\n";
1210 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */' => $str_menu.
"\n\t\t/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */"));
1227 $dicData =
"\t\t\$this->dictionaries = array(\n";
1228 $module = strtolower($module);
1229 foreach ($dicts as $key => $value) {
1230 if (empty($value)) {
1232 $dicData =
"\t\t\$this->dictionaries = array();";
1236 $dicData .=
"\t\t\t'$key' => ";
1238 if ($key ===
'tabcond') {
1239 $conditions = array_map(
1244 static function ($val) use ($module) {
1245 return is_bool($val) ?
"isModEnabled('$module')" : $val;
1249 $dicData .=
"array(" . implode(
", ", $conditions) .
")";
1250 } elseif ($key ===
'tabhelp') {
1251 $helpItems = array();
1252 foreach ($value as $helpValue) {
1253 $helpItems[] =
"array('code' => \$langs->trans('".$helpValue[
'code'].
"'), 'field2' => 'field2tooltip')";
1255 $dicData .=
"array(" . implode(
",", $helpItems) .
")";
1257 if (is_array($value)) {
1258 $dicData .=
"array(" . implode(
1265 static function ($val) {
1272 $dicData .=
"'$value'";
1277 $dicData .= (!$isEmpty ?
"\t\t);" :
'');
1279 $stringDic =
getFromFile($file,
'/* BEGIN MODULEBUILDER DICTIONARIES */',
'/* END MODULEBUILDER DICTIONARIES */');
1280 $writeInfile =
dolReplaceInFile($file, array($stringDic => $dicData.
"\n"));
1282 return $writeInfile;
1301 if (empty($namedic)) {
1305 if (!file_exists($file)) {
1308 $modulename = strtolower($modulename);
1310 if (empty($dictionnaires)) {
1311 $dictionnaires = array(
'langs' =>
'',
'tabname' => array(),
'tablib' => array(),
'tabsql' => array(),
'tabsqlsort' => array(),
'tabfield' => array(),
'tabfieldvalue' => array(),
'tabfieldinsert' => array(),
'tabrowid' => array(),
'tabcond' => array(),
'tabhelp' => array());
1315 'rowid' => array(
'type' =>
'integer',
'value' => 11,
'extra' =>
'AUTO_INCREMENT'),
1316 'code' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1317 'label' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1318 'position' => array(
'type' =>
'integer',
'value' => 11,
'null' =>
'NULL'),
1319 'use_default' => array(
'type' =>
'varchar',
'value' => 11,
'default' =>
'1'),
1320 'active' => array(
'type' =>
'integer',
'value' => 3)
1323 $primaryKey =
'rowid';
1324 foreach ($columns as $key => $value) {
1325 if ($key ===
'rowid') {
1326 $primaryKey =
'rowid';
1329 if (!array_key_exists(
'rowid', $columns)) {
1330 $primaryKey = array_key_first($columns);
1336 $checkTable = $db->DDLDescTable(MAIN_DB_PREFIX.strtolower($namedic));
1337 if ($checkTable && $db->num_rows($checkTable) > 0) {
1338 setEventMessages($langs->trans(
"ErrorTableExist", $namedic),
null,
'errors');
1341 $_results = $db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey,
"");
1342 if ($_results < 0) {
1344 $langs->load(
"errors");
1345 setEventMessages($langs->trans(
"ErrorTableNotFound", $namedic),
null,
'errors');
1350 $dictionnaires[
'langs'] = $modulename.
'@'.$modulename;
1351 $dictionnaires[
'tabname'][] = strtolower($namedic);
1352 $dictionnaires[
'tablib'][] = ucfirst(substr($namedic, 2));
1353 $dictionnaires[
'tabsql'][] =
'SELECT t.rowid as rowid, t.code, t.label, t.active FROM '.MAIN_DB_PREFIX.strtolower($namedic).
' as t';
1354 $dictionnaires[
'tabsqlsort'][] = (array_key_exists(
'label', $columns) ?
'label ASC' :
'');
1355 $dictionnaires[
'tabfield'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1356 $dictionnaires[
'tabfieldvalue'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1357 $dictionnaires[
'tabfieldinsert'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1358 $dictionnaires[
'tabrowid'][] = $primaryKey;
1359 $dictionnaires[
'tabcond'][] = isModEnabled(
'$modulename');
1360 $dictionnaires[
'tabhelp'][] = (array_key_exists(
'code', $columns) ? array(
'code' => $langs->trans(
'CodeTooltipHelp'),
'field2' =>
'field2tooltip') :
'');
1364 if ($writeInfile > 0) {
1365 setEventMessages($langs->trans(
"DictionariesCreated", ucfirst(substr($namedic, 2))),
null);
1384 $string =
getFromFile($file_api,
'/*begin methods CRUD*/',
'/*end methods CRUD*/');
1385 $extractUrls = explode(
"\n", $string);
1388 $urlValues = array();
1389 foreach ($extractUrls as $key => $line) {
1390 $lineWithoutTabsSpaces = preg_replace(
'/^[\t\s]+/',
'', $line);
1391 if (strpos($lineWithoutTabsSpaces,
'* @url') === 0) {
1392 $urlValue = trim(substr($lineWithoutTabsSpaces, strlen(
'* @url')));
1393 $urlValues[] = $urlValue;
1398 $str = $_SERVER[
'HTTP_HOST'].
'/api/index.php/';
1399 $groupedUrls = array();
1400 foreach ($urlValues as $url) {
1401 if (preg_match(
'/(?:GET|POST|PUT|DELETE) (\w+)s/', $url, $matches)) {
1402 $objectName = $matches[1];
1403 $url = $str.trim(strstr($url,
' '));
1404 $groupedUrls[$objectName][] = $url;
1407 if (empty($groupedUrls)) {
1413 $asciiDocTable =
"[options=\"header\"]\n|===\n|Object | URLs\n";
1414 foreach ($groupedUrls as $objectName => $urls) {
1415 $urlsList = implode(
" +\n*", $urls);
1416 $asciiDocTable .=
"|$objectName | \n*$urlsList +\n";
1418 $asciiDocTable .=
"|===\n";
1419 $file_write =
dolReplaceInFile($file_doc, array(
'__API_DOC__' =>
'__API_DOC__'.
"\n".$asciiDocTable));
1420 if ($file_write < 0) {
1437 if (!is_dir($path)) {
1441 $allFilesAndDirs = scandir($path);
1444 foreach ($allFilesAndDirs as $item) {
1445 if ($item !=
'.' && $item !=
'..') {
1446 if ($type == 1 && is_file($path . DIRECTORY_SEPARATOR . $item) && strpos($item,
'.back') ===
false) {
1448 } 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.