26require_once DOL_DOCUMENT_ROOT .
'/modulebuilder/class/NamingContract.class.php';
41function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
'', $addfieldentry = array(), $delfieldentry =
'')
45 if (empty($objectname)) {
48 if (empty($readdir)) {
52 if (!empty($addfieldentry[
'arrayofkeyval']) && !is_array($addfieldentry[
'arrayofkeyval'])) {
53 dol_print_error(
null,
'Bad parameter addfieldentry with a property arrayofkeyval defined but that is not an array.');
60 if (is_array($addfieldentry) && count($addfieldentry) > 0) {
61 if (empty($addfieldentry[
'name'])) {
62 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Name")),
null,
'errors');
65 if (empty($addfieldentry[
'label'])) {
66 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Label")),
null,
'errors');
69 if (!preg_match(
'/^(integer|price|sellist|varchar|double|text|html|duration|stars)/', $addfieldentry[
'type'])
70 && !preg_match(
'/^(boolean|smallint|real|date|datetime|timestamp|phone|email|url|ip|password)$/', $addfieldentry[
'type'])) {
71 setEventMessages($langs->trans(
'BadValueForType', $addfieldentry[
'type']),
null,
'errors');
76 if (preg_match(
'/^stars\((.+)\)$/', $addfieldentry[
'type'], $matches)) {
77 if (!ctype_digit($matches[1]) || $matches[1] < 1 || $matches[1] > 10) {
78 setEventMessages($langs->trans(
'BadValueForType', $addfieldentry[
'type']),
null,
'errors');
84 $pathoffiletoeditsrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
85 $pathoffiletoedittarget = $destdir.
'/class/'.strtolower($objectname).
'.class.php'.($readdir != $destdir ?
'.new' :
'');
87 $langs->load(
"errors");
88 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
96 include_once $pathoffiletoeditsrc;
97 if (class_exists($objectname)) {
102 '@phan-var-force CommonObject $object';
105 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
108 $contentclass = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
112 if (is_array($addfieldentry) && count($addfieldentry)) {
113 $name = $addfieldentry[
'name'];
114 unset($addfieldentry[
'name']);
116 $object->fields[$name] = $addfieldentry;
118 if (!empty($delfieldentry)) {
119 $name = $delfieldentry;
127 $texttoinsert =
'// BEGIN MODULEBUILDER PROPERTIES'.
"\n";
128 $texttoinsert .=
"\t".
'
132 $texttoinsert .=
"\t".
'public $fields = array('.
"\n";
135 foreach (
$object->fields as $key => $val) {
137 $texttoinsert .=
"\t\t".
'"'.$key.
'" => array(';
138 $texttoinsert .=
'"type" => "'.dol_escape_php($val[
'type']).
'",';
139 $texttoinsert .=
' "label" => "'.dol_escape_php($val[
'label']).
'",';
140 if (!empty($val[
'picto'])) {
141 $texttoinsert .=
' "picto" => "'.dol_escape_php($val[
'picto']).
'",';
143 $texttoinsert .=
' "enabled" => "'.($val[
'enabled'] !==
'' ?
dol_escape_php($val[
'enabled']) : 1).
'",';
144 $texttoinsert .=
" 'position' => ".($val[
'position'] !==
'' ? (int) $val[
'position'] : 50).
",";
145 $texttoinsert .=
" 'notnull' => ".(empty($val[
'notnull']) ? 0 : (int) $val[
'notnull']).
",";
146 $texttoinsert .=
' "visible" => "'.($val[
'visible'] !==
'' ?
dol_escape_js($val[
'visible']) : -1).
'",';
147 if (!empty($val[
'noteditable'])) {
148 $texttoinsert .=
' "noteditable" => "'.dol_escape_php((
string) $val[
'noteditable']).
'",';
150 if (!empty($val[
'alwayseditable'])) {
151 $texttoinsert .=
' "alwayseditable" => "'.dol_escape_php((
string) $val[
'alwayseditable']).
'",';
153 if (array_key_exists(
'default', $val) && (!empty($val[
'default']) || $val[
'default'] ===
'0')) {
154 $texttoinsert .=
' "default" => "'.dol_escape_php($val[
'default']).
'",';
156 if (!empty($val[
'index'])) {
157 $texttoinsert .=
' "index" => "'.(int) $val[
'index'].
'",';
159 if (!empty($val[
'foreignkey'])) {
160 $texttoinsert .=
' "foreignkey" => "'.(int) $val[
'foreignkey'].
'",';
162 if (!empty($val[
'searchall'])) {
163 $texttoinsert .=
' "searchall" => "'.(int) $val[
'searchall'].
'",';
165 if (!empty($val[
'isameasure'])) {
166 $texttoinsert .=
' "isameasure" => "'.(int) $val[
'isameasure'].
'",';
168 if (!empty($val[
'css'])) {
169 $texttoinsert .=
' "css" => "'.dol_escape_php($val[
'css']).
'",';
171 if (!empty($val[
'cssview'])) {
172 $texttoinsert .=
' "cssview" => "'.dol_escape_php($val[
'cssview']).
'",';
174 if (!empty($val[
'csslist'])) {
175 $texttoinsert .=
' "csslist" => "'.dol_escape_php($val[
'csslist']).
'",';
177 if (!empty($val[
'help'])) {
178 $texttoinsert .=
' "help" => "'.dol_escape_php($val[
'help']).
'",';
180 if (!empty($val[
'showoncombobox'])) {
181 $texttoinsert .=
' "showoncombobox" => "'.(int) $val[
'showoncombobox'].
'",';
183 if (!empty($val[
'disabled'])) {
184 $texttoinsert .=
' "disabled" => "'.(int) $val[
'disabled'].
'",';
186 if (!empty($val[
'autofocusoncreate'])) {
187 $texttoinsert .=
' "autofocusoncreate" => "'.(int) $val[
'autofocusoncreate'].
'",';
189 if (!empty($val[
'arrayofkeyval'])) {
190 $texttoinsert .=
' "arrayofkeyval" => array(';
192 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
194 $texttoinsert .=
", ";
196 $texttoinsert .=
'"'.dol_escape_php($key2).
'" => "'.
dol_escape_php($val2).
'"';
199 $texttoinsert .=
'),';
201 if (!empty($val[
'validate'])) {
202 $texttoinsert .=
' "validate" => "'.(int) $val[
'validate'].
'",';
204 if (!empty($val[
'comment'])) {
205 $texttoinsert .=
' "comment" => "'.dol_escape_php($val[
'comment']).
'"';
208 $texttoinsert .=
"),\n";
213 $texttoinsert .=
"\t".
');'.
"\n";
219 foreach (
$object->fields as $key => $val) {
222 $texttoinsert .=
"\t".
'public $'.$key.
";";
227 $texttoinsert .=
"\n";
231 $texttoinsert .=
"\t".
'// END MODULEBUILDER PROPERTIES';
235 $contentclass = preg_replace(
'/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
238 dol_mkdir(dirname($pathoffiletoedittarget));
241 $result = file_put_contents(
dol_osencode($pathoffiletoedittarget), $contentclass);
244 dolChmod($pathoffiletoedittarget, $newmask);
251 print $e->getMessage();
275 if (empty($objectname)) {
278 if (empty($readdir)) {
282 $pathoffiletoclasssrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
285 if ($moduletype ==
'internal') {
286 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
287 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
288 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
289 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
290 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
291 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
292 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'.sql';
297 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
298 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
299 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
300 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
301 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
302 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
303 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'.sql';
310 $pathoffiletoedittarget = $destdir.$pathoffiletoeditsrc.($readdir != $destdir ?
'.new' :
'');
311 $pathoffiletoeditsrc = $readdir.$pathoffiletoeditsrc;
314 $langs->load(
"errors");
315 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
322 include_once $pathoffiletoclasssrc;
323 if (class_exists($objectname)) {
330 print $e->getMessage();
334 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
336 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
339 $texttoinsert =
'-- BEGIN MODULEBUILDER FIELDS'.
"\n";
341 foreach (
$object->fields as $key => $val) {
344 $type = $val[
'type'];
345 $type = preg_replace(
'/:.*$/',
'', $type);
347 if ($type ==
'html') {
349 } elseif ($type ==
'price') {
351 } elseif (in_array($type, array(
'link',
'sellist',
'duration'))) {
353 } elseif ($type ==
'chkbxlst') {
354 $type =
'varchar(128)';
355 } elseif ($type ==
'mail' || $type ==
'email') {
356 $type =
'varchar(128)';
357 } elseif (strpos($type,
'stars(') === 0) {
359 } elseif ($type ==
'phone') {
360 $type =
'varchar(20)';
361 } elseif ($type ==
'ip') {
362 $type =
'varchar(32)';
363 } elseif ($type ==
'url') {
364 $type =
'varchar(255)';
367 $texttoinsert .=
"\t".$key.
" ".$type;
368 if ($key ==
'rowid') {
369 $texttoinsert .=
' AUTO_INCREMENT PRIMARY KEY';
370 } elseif ($type ==
'timestamp') {
371 $texttoinsert .=
' DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
373 if ($key ==
'entity') {
374 $texttoinsert .=
' DEFAULT 1';
376 if (!empty($val[
'default'])) {
377 if (preg_match(
'/^null$/i', $val[
'default'])) {
378 $texttoinsert .=
" DEFAULT NULL";
379 } elseif (preg_match(
'/varchar/', $type)) {
380 $texttoinsert .=
" DEFAULT '".$db->escape($val[
'default']).
"'";
382 $texttoinsert .= (($val[
'default'] > 0) ?
' DEFAULT '.$val[
'default'] :
'');
386 $texttoinsert .= ((!empty($val[
'notnull']) && $val[
'notnull'] > 0) ?
' NOT NULL' :
'');
387 if ($i < count(
$object->fields)) {
388 $texttoinsert .=
", ";
390 $texttoinsert .=
"\n";
393 $texttoinsert .=
"\t".
'-- END MODULEBUILDER FIELDS';
395 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
397 $result = file_put_contents($pathoffiletoedittarget, $contentsql);
399 dolChmod($pathoffiletoedittarget, $newmask);
402 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
406 $pathoffiletoeditsrc = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoeditsrc);
407 $pathoffiletoedittarget = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoedittarget);
408 $pathoffiletoedittarget = preg_replace(
'/\.sql.new$/',
'.key.sql.new', $pathoffiletoedittarget);
410 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
413 $texttoinsert =
'-- BEGIN MODULEBUILDER INDEXES'.
"\n";
415 foreach (
$object->fields as $key => $val) {
417 if (!empty($val[
'index'])) {
418 $texttoinsert .=
"ALTER TABLE llx_".strtolower($module).
'_'.strtolower($objectname).
" ADD ".($key ==
'ref' ?
"UNIQUE INDEX uk_" :
"INDEX idx_").strtolower($module).
'_'.strtolower($objectname).
"_".$key.
" (".$key.($key ==
'ref' && array_key_exists(
'entity',
$object->fields) ?
", entity" :
"").
");";
419 $texttoinsert .=
"\n";
421 if (!empty($val[
'foreignkey'])) {
422 $tmp = explode(
'.', $val[
'foreignkey']);
423 if (!empty($tmp[0]) && !empty($tmp[1])) {
424 $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].
");";
425 $texttoinsert .=
"\n";
430 $texttoinsert .=
'-- END MODULEBUILDER INDEXES';
432 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
434 dol_mkdir(dirname($pathoffiletoedittarget));
436 $result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
438 dolChmod($pathoffiletoedittarget, $newmask);
441 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
444 return $error ? -1 : 1;
456 $listofobject =
dol_dir_list($destdir.
'/class',
'files', 0,
'\.class\.php$');
457 foreach ($listofobject as $fileobj) {
458 if (preg_match(
'/^api_/', $fileobj[
'name'])) {
461 if (preg_match(
'/^actions_/', $fileobj[
'name'])) {
465 $tmpcontent = file_get_contents($fileobj[
'fullname']);
467 if (preg_match(
'/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) {
468 $objectnameloop = $reg[1];
469 $objects[$fileobj[
'fullname']] = $objectnameloop;
472 if (count($objects) > 0) {
488 if (!file_exists($file)) {
492 $content = file_get_contents($file);
495 if (strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU MYOBJECT */') !==
false
496 || strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU */') !==
false) {
499 if (strpos($content,
'/* END MODULEBUILDER TOPMENU MYOBJECT */') !==
false
500 || strpos($content,
'/* END MODULEBUILDER TOPMENU */') !==
false) {
503 if (strpos($content,
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
506 if (strpos($content,
'/* END MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
513 } elseif ($number === 1) {
514 if (strpos($content,
'/* BEGIN MODULEBUILDER PERMISSIONS */') !==
false && strpos($content,
'/* END MODULEBUILDER PERMISSIONS */') !==
false) {
517 } elseif ($number == 2) {
518 if (strpos($content,
'/* BEGIN MODULEBUILDER DICTIONARIES */') !==
false && strpos($content,
'/* END MODULEBUILDER DICTIONARIES */') !==
false) {
532 $start =
"/* BEGIN MODULEBUILDER PERMISSIONS */";
533 $end =
"/* END MODULEBUILDER PERMISSIONS */";
536 $lines = file($file);
538 foreach ($lines as $i => $line) {
539 if (strpos($line, $start) !==
false) {
540 $start_line = $i + 1;
543 while (($line = $lines[++$i]) !==
false) {
544 if (strpos($line, $end) !==
false) {
553 $allContent = implode(
"", $array);
565 return strcmp($a[0], $b[0]);
582 if ($action == 0 && $key !==
null) {
584 array_splice($permissions, array_search($permissions[$key], $permissions), 1);
585 } elseif ($action == 1) {
586 array_push($permissions, $right);
587 } elseif ($action == 2 && !empty($right) && $key !==
null) {
589 array_splice($permissions, array_search($permissions[$key], $permissions), 1, $right);
590 } elseif ($action == -1 && !empty($objectname)) {
594 foreach ($permissions as $perms) {
595 if ($perms[4] === strtolower($objectname)) {
596 array_splice($permissions, array_search($perms, $permissions), 1);
599 } elseif ($action == -2 && !empty($objectname) && !empty($module)) {
602 $objectOfRights = array();
604 foreach ($permissions as $right) {
605 $objectOfRights[] = $right[4];
607 if (in_array(strtolower($objectname), $objectOfRights)) {
610 $permsToadd = array();
612 'read' =>
'Read '.$objectname.
' object of '.ucfirst($module),
613 'write' =>
'Create/Update '.$objectname.
' object of '.ucfirst($module),
614 'delete' =>
'Delete '.$objectname.
' object of '.ucfirst($module)
617 foreach ($perms as $index => $value) {
618 $permsToadd[$i][0] =
'';
619 $permsToadd[$i][1] = $value;
620 $permsToadd[$i][4] = strtolower($objectname);
621 $permsToadd[$i][5] = $index;
622 array_push($permissions, $permsToadd[$i]);
629 '@phan-var-force array<int,string[]> $permissions';
632 foreach (array_keys($permissions) as $i) {
633 $permissions[$i][0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1)";
634 $permissions[$i][1] =
"\$this->rights[\$r][1] = '".$permissions[$i][1].
"'";
635 $permissions[$i][4] =
"\$this->rights[\$r][4] = '".$permissions[$i][4].
"'";
636 $permissions[$i][5] =
"\$this->rights[\$r][5] = '".$permissions[$i][5].
"';\n\t\t";
639 $perms_grouped = array();
640 foreach ($permissions as $perms) {
642 if (!isset($perms_grouped[
$object])) {
643 $perms_grouped[
$object] = array();
645 $perms_grouped[
$object][] = $perms;
648 $permissions = $perms_grouped;
653 foreach ($permissions as &
$object) {
657 if (str_contains($obj[5],
'read')) {
658 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 0 + 1)";
659 } elseif (str_contains($obj[5],
'write')) {
660 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 1 + 1)";
661 } elseif (str_contains($obj[5],
'delete')) {
662 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + 2 + 1)";
664 $obj[0] =
"\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o.
" * 10) + ".$p.
" + 1)";
668 usort(
$object,
'compareFirstValue');
673 foreach ($permissions as $perms) {
674 foreach ($perms as $per) {
675 $rights[] = implode(
";\n\t\t", $per).
"\$r++;\n";
678 $rights_str = implode(
"\t\t", $rights);
682 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER PERMISSIONS */' =>
'/* BEGIN MODULEBUILDER PERMISSIONS */'.
"\n\t\t".$rights_str));
697 $string = str_replace(
"'",
'', $string);
700 preg_match_all(
'/\s*([^\s=>]+)\s*=>\s*([^,]+),?/', $string, $matches, PREG_SET_ORDER);
701 $propertyArray = array();
703 foreach ($matches as $match) {
704 $key = trim($match[1]);
705 $value = trim($match[2]);
707 if (strpos($value,
'array(') === 0) {
708 $nestedArray = substr($value, 6);
710 $value = $nestedArray;
711 } elseif (strpos($value,
'"Id")') !==
false) {
712 $value = str_replace(
')',
'', $value);
714 if (is_numeric($value)) {
715 if (strpos($value,
'.') !==
false) {
716 $value = (float) $value;
718 $value = (int) $value;
721 if ($value ===
'true') {
723 } elseif ($value ===
'false') {
728 $propertyArray[$key] = $value;
731 return $propertyArray;
745 $attributesUnique = array(
'type',
'label',
'enabled',
'position',
'notnull',
'visible',
'noteditable',
'index',
'default' ,
'foreignkey',
'arrayofkeyval',
'alwayseditable',
'validate',
'searchall',
'comment',
'isameasure',
'css',
'cssview',
'csslist',
'help',
'showoncombobox',
'picto' );
747 $start =
"public \$fields = array(";
751 $lines = file($file);
753 foreach ($lines as $i => $line) {
754 if (strpos($line, $start) !==
false) {
756 while (($line = $lines[++$i]) !==
false) {
757 if (strpos($line, $end) !==
false) {
766 $table =
"== DATA SPECIFICATIONS\n";
767 $table .=
"=== Table of fields with properties for object *$objectname* : \n";
768 $table .=
"[options='header',grid=rows,frame=topbot,width=100%,caption=Organisation]\n";
772 foreach ($attributesUnique as $attUnique) {
773 $table .=
"|".$attUnique;
776 $valuesModif = array();
777 foreach ($keys as $string) {
778 $string = trim($string,
"'");
779 $string = rtrim($string,
",");
785 foreach ($array as $key => $value) {
786 if (is_array($value)) {
790 $array[$code][$key] = $value;
795 if (!is_array($array)) {
798 $field = array_keys($array);
799 if ($field[0] ===
'') {
802 $values = array_values($array)[0];
805 foreach ($attributesUnique as $attUnique) {
806 if ($attUnique ==
'type' && $field[0] ===
'label') {
807 $values[$attUnique] =
'varchar(255)';
809 if (!array_key_exists($attUnique, $values)) {
810 $valuesModif[$attUnique] =
'';
812 $valuesModif[$attUnique] = $values[$attUnique];
815 $table .=
"|*" . $field[0] .
"*|";
816 $table .= implode(
"|", $valuesModif) .
"\n";
821 $table .=
"__ end table for object $objectname\n";
824 $writeInFile =
dolReplaceInFile($destfile, array(
'== DATA SPECIFICATIONS' => $table));
825 if ($writeInFile < 0) {
842 $start =
"== Table of fields and their properties for object *".ucfirst($objectname).
"* : ";
843 $end =
"__ end table for object ".ucfirst($objectname);
845 $str = file_get_contents($file);
847 $search =
'/' . preg_quote($start,
'/') .
'(.*?)' . preg_quote($end,
'/') .
'/s';
848 $new_contents = preg_replace($search,
'', $str);
849 file_put_contents($file, $new_contents);
853 $perms =
"|*".strtolower($objectname).
"*|";
854 $search_pattern_perms =
'/' . preg_quote($perms,
'/') .
'.*?\n/';
855 $new_contents = preg_replace($search_pattern_perms,
'', $new_contents);
856 file_put_contents($file, $new_contents);
873function getFromFile($file, $start, $end, $excludestart =
'', $includese = 0)
882 while ($line = fgets($fhandle)) {
883 if (strpos($line, $start) !==
false && (empty($excludestart) || strpos($line, $excludestart) ===
false)) {
888 while (($line = fgets($fhandle)) !==
false) {
889 if (strpos($line, $end) !==
false) {
903 $content = implode(
"", $keys);
917 $start =
'/* BEGIN MODULEBUILDER PERMISSIONS */';
918 $end =
'/* END MODULEBUILDER PERMISSIONS */';
920 if (empty($content)) {
924 $string =
"[options='header',grid=rows,width=60%,caption=Organisation]\n";
927 $header = array($langs->trans(
'Objects'),$langs->trans(
'Permission'));
928 foreach ($header as $h) {
933 $array = explode(
";", $content);
934 $permissions = array_filter($array);
936 $permissions = str_replace(
'$r++',
'1', $permissions);
939 foreach ($permissions as $i => $element) {
941 unset($permissions[$i]);
943 if (str_contains($element,
'$this->numero')) {
944 unset($permissions[$i]);
946 if (str_contains($element,
'$this->rights[$r][5]')) {
947 unset($permissions[$i]);
951 foreach ($permissions as $key => $element) {
952 $element = str_replace(
" '",
'', $element);
953 $element = trim($element,
"'");
954 $permsN[] = substr($element, strpos($element,
"=") + 1);
959 $final_array = array();
961 while ($index < count($permsN)) {
962 $temp_array = array($permsN[$index], $permsN[$index + 1]);
963 $final_array[] = $temp_array;
968 foreach ($final_array as $subarray) {
972 $result[$key][] = $subarray;
974 foreach ($result as $i => $pems) {
975 $string .=
"|*".$i.
"*|";
976 foreach ($pems as $tab) {
977 $string .= $tab[0].
" , ";
982 $string .=
"\n|===\n";
984 $write =
dolReplaceInFile($destfile, array(
'__DATA_PERMISSIONS__' => $string));
1002 global $langs, $user;
1004 if (!file_exists($file)) {
1009 $content = file($file);
1011 $includeClass =
"dol_include_once\(\'\/\w+\/class\/\w+\.class\.php\'\);";
1012 $props =
'public\s+\$\w+;';
1013 $varcommented =
'@var\s+\w+\s+\$\w+\s+{@type\s+\w+}';
1014 $constructObj =
'\$this->\w+\s+=\s+new\s+\w+\(\$this->db\);';
1017 foreach ($content as $lineNumber => &$lineContent) {
1018 if (preg_match(
'/'.$varcommented.
'/', $lineContent)) {
1020 foreach ($objects as $objectname) {
1021 $lineContent .=
"\t * @var ".$objectname.
" \$".strtolower($objectname).
" {@type ".$objectname.
"}". PHP_EOL;
1024 } elseif (preg_match(
'/'.$props.
'/', $lineContent)) {
1026 foreach ($objects as $objectname) {
1027 $lineContent .=
"\t/*".PHP_EOL.
"\t * @var mixed TODO: set type".PHP_EOL.
"\t */".PHP_EOL.
"\tpublic \$".strtolower($objectname).
";". PHP_EOL;
1029 } elseif (preg_match(
'/'.$constructObj.
'/', $lineContent)) {
1031 foreach ($objects as $objectname) {
1032 $lineContent .=
"\t\t\$this->".strtolower($objectname).
" = new ".$objectname.
"(\$this->db);". PHP_EOL;
1034 } elseif (preg_match(
'/'.$includeClass.
'/', $lineContent)) {
1036 foreach ($objects as $objectname) {
1037 $lineContent .=
"dol_include_once('/".strtolower($modulename).
"/class/".strtolower($objectname).
".class.php');". PHP_EOL;
1042 $allContent = implode(
"", $content);
1043 file_put_contents($file, $allContent);
1047 $allContent =
getFromFile($srcfile,
'/* BEGIN MODULEBUILDER API MYOBJECT */',
'/* END MODULEBUILDER API MYOBJECT */');
1048 foreach ($objects as $objectname) {
1049 if (strtolower($modulename) === strtolower($objectname)) {
1050 dol_syslog(
'addObjectsToApiFile: skipping object "' . $objectname .
'" — name collides with module "' . $modulename .
'"', LOG_WARNING);
1055 'htdocs/modulebuilder/template' => $nc->moduleNameLower,
1056 '---Replace with your own copyright and developer email---' =>
dol_print_date($now,
'%Y') .
' ' . $user->getFullName($langs) . ($user->email ?
' <' . $user->email .
'>' :
''),
1058 $fullMap = array_merge($nc->getSubstitutionMap(), $extraMap);
1059 $contentReplaced = str_replace(array_keys($fullMap), array_values($fullMap), $allContent);
1062 '/* BEGIN MODULEBUILDER API MYOBJECT */' =>
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */'.$contentReplaced.
"\t".
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */'.
"\n\n\n\t".
'/* BEGIN MODULEBUILDER API MYOBJECT */'
1083 if (!file_exists($file)) {
1087 $content = file($file);
1089 $includeClass =
"dol_include_once\(\'\/\w+\/class\/".strtolower($objectname).
"\.class\.php\'\);";
1090 $props =
'public\s+\$'.strtolower($objectname);
1091 $varcommented =
'@var\s+\w+\s+\$'.strtolower($objectname).
'\s+{@type\s+\w+}';
1092 $constructObj =
'\$this->'.strtolower($objectname).
'\s+=\s+new\s+\w+\(\$this->db\);';
1095 foreach ($content as $lineNumber => &$lineContent) {
1096 if (preg_match(
'/'.$varcommented.
'/i', $lineContent)) {
1098 } elseif (preg_match(
'/'.$props.
'/i', $lineContent)) {
1100 } elseif (preg_match(
'/'.$constructObj.
'/i', $lineContent)) {
1102 } elseif (preg_match(
'/'.$includeClass.
'/i', $lineContent)) {
1107 $allContent = implode(
"", $content);
1108 file_put_contents($file, $allContent);
1112 $begin =
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */';
1113 $end =
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */';
1136 if (!file_exists($file)) {
1140 if ($action == 0 && !empty($key)) {
1142 array_splice($menus, array_search($menus[$key], $menus), 1);
1143 } elseif ($action == 1) {
1145 array_push($menus, $menuWantTo);
1146 } elseif ($action == 2 && !empty($key) && !empty($menuWantTo)) {
1150 foreach ($menus as $index => $menu) {
1151 if ($index !== $key) {
1152 if ($menu[
'type'] === $menuWantTo[
'type']) {
1153 if (strcasecmp(str_replace(
' ',
'', $menu[
'titre']), str_replace(
' ',
'', $menuWantTo[
'titre'])) === 0) {
1156 if (strcasecmp(str_replace(
' ',
'', $menu[
'url']), str_replace(
' ',
'', $menuWantTo[
'url'])) === 0) {
1162 if (!$counter && $urlCounter < 2) {
1163 $menus[$key] = $menuWantTo;
1167 } elseif ($action == -1 && !empty($menuWantTo) && is_string($menuWantTo)) {
1169 foreach ($menus as $index => $menu) {
1170 if ((strpos(strtolower($menu[
'fk_menu']), strtolower($menuWantTo)) !==
false) || (strpos(strtolower($menu[
'leftmenu']), strtolower($menuWantTo)) !==
false)) {
1171 array_splice($menus, array_search($menu, $menus), 1);
1179 $beginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU';
1180 $excludeBeginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT';
1181 $endMenu =
'/* END MODULEBUILDER LEFTMENU';
1183 while ($protection <= 1000 && $allMenus =
getFromFile($file, $beginMenu, $endMenu, $excludeBeginMenu, 1)) {
1190 foreach ($menus as $index => $menu) {
1191 $menu[
'position'] =
"1000 + \$r";
1192 if ($menu[
'type'] ===
'left') {
1193 $start =
"\t\t".
'/* BEGIN MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1194 $end =
"\t\t".
'/* END MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1196 $val_actuel = $menu;
1197 $next_val = empty($menus[$index + 1]) ? null : $menus[$index + 1];
1200 $str_menu .= $start.
"\n";
1201 $str_menu .=
"\t\t\$this->menu[\$r++] = array(\n";
1202 $str_menu .=
"\t\t\t'fk_menu' => '".dol_escape_php($menu[
'fk_menu'], 1).
"',\n";
1203 $str_menu .=
"\t\t\t'type' => '".dol_escape_php($menu[
'type'], 1).
"',\n";
1204 $str_menu .=
"\t\t\t'titre' => '".dol_escape_php($menu[
'titre'], 1).
"',\n";
1205 $str_menu .=
"\t\t\t'mainmenu' => '".dol_escape_php($menu[
'mainmenu'], 1).
"',\n";
1206 $str_menu .=
"\t\t\t'leftmenu' => '".dol_escape_php($menu[
'leftmenu'], 1).
"',\n";
1207 $str_menu .=
"\t\t\t'url' => '".dol_escape_php($menu[
'url'], 1).
"',\n";
1208 $str_menu .=
"\t\t\t'langs' => '".dol_escape_php($menu[
'langs'], 1).
"',\n";
1209 $str_menu .=
"\t\t\t'position' => ".((int) $menu[
'position']).
",\n";
1210 $str_menu .=
"\t\t\t'enabled' => '".dol_escape_php((
string) $menu[
'enabled'], 1).
"',\n";
1211 $str_menu .=
"\t\t\t'perms' => '".dol_escape_php($menu[
'perms'], 1).
"',\n";
1212 $str_menu .=
"\t\t\t'target' => '".dol_escape_php($menu[
'target'], 1).
"',\n";
1213 $str_menu .=
"\t\t\t'user' => ".((int) $menu[
'user']).
",\n";
1214 $str_menu .=
"\t\t\t'object' => '".dol_escape_php($menu[
'object'], 1).
"',\n";
1215 $str_menu .=
"\t\t);\n";
1217 if (is_null($next_val) || $val_actuel[
'leftmenu'] !== $next_val[
'leftmenu']) {
1218 $str_menu .= $end.
"\n";
1223 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */' => $str_menu.
"\n\t\t/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */"));
1240 $dicData =
"\t\t\$this->dictionaries = array(\n";
1241 $module = strtolower($module);
1242 foreach ($dicts as $key => $value) {
1243 if (empty($value)) {
1245 $dicData =
"\t\t\$this->dictionaries = array();";
1249 $dicData .=
"\t\t\t'$key' => ";
1251 if ($key ===
'tabcond') {
1252 $conditions = array_map(
1257 static function ($val) use ($module) {
1258 return is_bool($val) ?
"isModEnabled('$module')" : $val;
1262 $dicData .=
"array(" . implode(
", ", $conditions) .
")";
1263 } elseif ($key ===
'tabhelp') {
1264 $helpItems = array();
1265 foreach ($value as $helpValue) {
1266 $helpItems[] =
"array('code' => \$langs->trans('".$helpValue[
'code'].
"'), 'field2' => 'field2tooltip')";
1268 $dicData .=
"array(" . implode(
",", $helpItems) .
")";
1270 if (is_array($value)) {
1271 $dicData .=
"array(" . implode(
1278 static function ($val) {
1285 $dicData .=
"'$value'";
1290 $dicData .= (!$isEmpty ?
"\t\t);" :
'');
1292 $stringDic =
getFromFile($file,
'/* BEGIN MODULEBUILDER DICTIONARIES */',
'/* END MODULEBUILDER DICTIONARIES */');
1293 $writeInfile =
dolReplaceInFile($file, array($stringDic => $dicData.
"\n"));
1295 return $writeInfile;
1314 if (empty($namedic)) {
1318 if (!file_exists($file)) {
1321 $modulename = strtolower($modulename);
1323 if (empty($dictionnaires)) {
1324 $dictionnaires = array(
'langs' =>
'',
'tabname' => array(),
'tablib' => array(),
'tabsql' => array(),
'tabsqlsort' => array(),
'tabfield' => array(),
'tabfieldvalue' => array(),
'tabfieldinsert' => array(),
'tabrowid' => array(),
'tabcond' => array(),
'tabhelp' => array());
1328 'rowid' => array(
'type' =>
'integer',
'value' => 11,
'extra' =>
'AUTO_INCREMENT'),
1329 'code' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1330 'label' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1331 'position' => array(
'type' =>
'integer',
'value' => 11,
'null' =>
'NULL'),
1332 'use_default' => array(
'type' =>
'varchar',
'value' => 11,
'default' =>
'1'),
1333 'active' => array(
'type' =>
'integer',
'value' => 3)
1336 $primaryKey =
'rowid';
1337 foreach ($columns as $key => $value) {
1338 if ($key ===
'rowid') {
1339 $primaryKey =
'rowid';
1342 if (!array_key_exists(
'rowid', $columns)) {
1343 $primaryKey = array_key_first($columns);
1349 $checkTable =
$db->DDLDescTable(MAIN_DB_PREFIX.strtolower($namedic));
1350 if ($checkTable &&
$db->num_rows($checkTable) > 0) {
1351 setEventMessages($langs->trans(
"ErrorTableExist", $namedic),
null,
'errors');
1354 $_results =
$db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey,
"");
1355 if ($_results < 0) {
1357 $langs->load(
"errors");
1358 setEventMessages($langs->trans(
"ErrorTableNotFound", $namedic),
null,
'errors');
1363 $dictionnaires[
'langs'] = $modulename.
'@'.$modulename;
1364 $dictionnaires[
'tabname'][] = strtolower($namedic);
1365 $dictionnaires[
'tablib'][] = ucfirst(substr($namedic, 2));
1366 $dictionnaires[
'tabsql'][] =
'SELECT t.rowid as rowid, t.code, t.label, t.active FROM '.MAIN_DB_PREFIX.strtolower($namedic).
' as t';
1367 $dictionnaires[
'tabsqlsort'][] = (array_key_exists(
'label', $columns) ?
'label ASC' :
'');
1368 $dictionnaires[
'tabfield'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1369 $dictionnaires[
'tabfieldvalue'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1370 $dictionnaires[
'tabfieldinsert'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1371 $dictionnaires[
'tabrowid'][] = $primaryKey;
1372 $dictionnaires[
'tabcond'][] =
isModEnabled(
'$modulename');
1373 $dictionnaires[
'tabhelp'][] = (array_key_exists(
'code', $columns) ? array(
'code' => $langs->trans(
'CodeTooltipHelp'),
'field2' =>
'field2tooltip') :
'');
1377 if ($writeInfile > 0) {
1378 setEventMessages($langs->trans(
"DictionariesCreated", ucfirst(substr($namedic, 2))),
null);
1397 $string =
getFromFile($file_api,
'/*begin methods CRUD*/',
'/*end methods CRUD*/');
1398 $extractUrls = explode(
"\n", $string);
1401 $urlValues = array();
1402 foreach ($extractUrls as $key => $line) {
1403 $lineWithoutTabsSpaces = preg_replace(
'/^[\t\s]+/',
'', $line);
1404 if (strpos($lineWithoutTabsSpaces,
'* @url') === 0) {
1405 $urlValue = trim(substr($lineWithoutTabsSpaces, strlen(
'* @url')));
1406 $urlValues[] = $urlValue;
1411 $str = $_SERVER[
'HTTP_HOST'].
'/api/index.php/';
1412 $groupedUrls = array();
1413 foreach ($urlValues as $url) {
1414 if (preg_match(
'/(?:GET|POST|PUT|DELETE) (\w+)s/', $url, $matches)) {
1415 $objectName = $matches[1];
1416 $url = $str.trim(strstr($url,
' '));
1417 $groupedUrls[$objectName][] = $url;
1420 if (empty($groupedUrls)) {
1426 $asciiDocTable =
"[options=\"header\"]\n|===\n|Object | URLs\n";
1427 foreach ($groupedUrls as $objectName => $urls) {
1428 $urlsList = implode(
" +\n*", $urls);
1429 $asciiDocTable .=
"|$objectName | \n*$urlsList +\n";
1431 $asciiDocTable .=
"|===\n";
1432 $file_write =
dolReplaceInFile($file_doc, array(
'__API_DOC__' =>
'__API_DOC__'.
"\n".$asciiDocTable));
1433 if ($file_write < 0) {
1450 if (!is_dir($path)) {
1454 $allFilesAndDirs = scandir($path);
1457 foreach ($allFilesAndDirs as $item) {
1458 if ($item !=
'.' && $item !=
'..') {
1459 if ($type == 1 && is_file($path . DIRECTORY_SEPARATOR . $item) && strpos($item,
'.back') ===
false) {
1461 } elseif ($type == 2 && is_dir($path . DIRECTORY_SEPARATOR . $item)) {
1479 'contact' => array(
'file' =>
'myobject_contact.php',
'var' =>
'showtabofpagecontact',
'marker' =>
'CONTACT',
'label' =>
'Contacts'),
1480 'note' => array(
'file' =>
'myobject_note.php',
'var' =>
'showtabofpagenote',
'marker' =>
'NOTE',
'label' =>
'Notes'),
1481 'document' => array(
'file' =>
'myobject_document.php',
'var' =>
'showtabofpagedocument',
'marker' =>
'DOCUMENT',
'label' =>
'Documents'),
1482 'agenda' => array(
'file' =>
'myobject_agenda.php',
'var' =>
'showtabofpageagenda',
'marker' =>
'AGENDA',
'label' =>
'Events'),
1497 if (!is_array($requested) || empty($requested)) {
1500 foreach (array_keys($map) as $tabkey) {
1501 if (in_array($tabkey, $requested,
true)) {
1520 if (!file_exists($file)) {
1523 $content = file_get_contents($file);
1524 if ($content ===
false) {
1529 $foundmarkers = array();
1530 preg_match_all(
'/\/\*\s*(?:BEGIN|END) MODULEBUILDER [^*]*\*\//', $content, $foundmarkers);
1531 $sentinels = array();
1532 foreach (array_values(array_unique($foundmarkers[0])) as $index => $marker) {
1533 $key =
"\0MODULEBUILDERMARKER".$index.
"\0";
1534 $sentinels[$key] = $marker;
1535 $content = str_replace($marker, $key, $content);
1538 $content = str_replace(array_keys($arrayreplacement), array_values($arrayreplacement), $content);
1541 if (!empty($sentinels)) {
1542 $content = strtr($content, $sentinels);
1545 if (file_put_contents($file, $content) ===
false) {
if(! $sortfield) if(! $sortorder) $object
Immutable value object holding all case variants of a module/object pair.
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_now($mode='gmt')
Return date for now.
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_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_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...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
isModEnabled($module)
Is Dolibarr module enabled.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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.
filterEnabledTabs($requested, $map)
Filter a list of requested tab keys against the known optional tabs map.
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.
dolReplaceInFilePreservingModuleBuilderMarkers($file, $arrayreplacement)
Apply substitutions to a module descriptor file while preserving the MODULEBUILDER comment markers.
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.
getModuleBuilderObjectTabs()
Return the map of optional tabs that can be generated for a ModuleBuilder object.
addObjectsToApiFile($srcfile, $file, $objects, $modulename)
Add Object in ModuleApi File.