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|email|url|ip|password)$/', $addfieldentry[
'type'])) {
70 setEventMessages($langs->trans(
'BadValueForType', $addfieldentry[
'type']),
null,
'errors');
75 if (preg_match(
'/^stars\((.+)\)$/', $addfieldentry[
'type'], $matches)) {
76 if (!ctype_digit($matches[1]) || $matches[1] < 1 || $matches[1] > 10) {
77 setEventMessages($langs->trans(
'BadValueForType', $addfieldentry[
'type']),
null,
'errors');
83 $pathoffiletoeditsrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
84 $pathoffiletoedittarget = $destdir.
'/class/'.strtolower($objectname).
'.class.php'.($readdir != $destdir ?
'.new' :
'');
86 $langs->load(
"errors");
87 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
95 include_once $pathoffiletoeditsrc;
96 if (class_exists($objectname)) {
101 '@phan-var-force CommonObject $object';
104 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
107 $contentclass = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
111 if (is_array($addfieldentry) && count($addfieldentry)) {
112 $name = $addfieldentry[
'name'];
113 unset($addfieldentry[
'name']);
115 $object->fields[$name] = $addfieldentry;
117 if (!empty($delfieldentry)) {
118 $name = $delfieldentry;
126 $texttoinsert =
'// BEGIN MODULEBUILDER PROPERTIES'.
"\n";
127 $texttoinsert .=
"\t".
'
131 $texttoinsert .=
"\t".
'public $fields = array('.
"\n";
134 foreach (
$object->fields as $key => $val) {
136 $texttoinsert .=
"\t\t".
'"'.$key.
'" => array(';
137 $texttoinsert .=
'"type" => "'.dol_escape_php($val[
'type']).
'",';
138 $texttoinsert .=
' "label" => "'.dol_escape_php($val[
'label']).
'",';
139 if (!empty($val[
'picto'])) {
140 $texttoinsert .=
' "picto" => "'.dol_escape_php($val[
'picto']).
'",';
142 $texttoinsert .=
' "enabled" => "'.($val[
'enabled'] !==
'' ?
dol_escape_php($val[
'enabled']) : 1).
'",';
143 $texttoinsert .=
" 'position' => ".($val[
'position'] !==
'' ? (int) $val[
'position'] : 50).
",";
144 $texttoinsert .=
" 'notnull' => ".(empty($val[
'notnull']) ? 0 : (int) $val[
'notnull']).
",";
145 $texttoinsert .=
' "visible" => "'.($val[
'visible'] !==
'' ?
dol_escape_js($val[
'visible']) : -1).
'",';
146 if (!empty($val[
'noteditable'])) {
147 $texttoinsert .=
' "noteditable" => "'.dol_escape_php((
string) $val[
'noteditable']).
'",';
149 if (!empty($val[
'alwayseditable'])) {
150 $texttoinsert .=
' "alwayseditable" => "'.dol_escape_php((
string) $val[
'alwayseditable']).
'",';
152 if (array_key_exists(
'default', $val) && (!empty($val[
'default']) || $val[
'default'] ===
'0')) {
153 $texttoinsert .=
' "default" => "'.dol_escape_php($val[
'default']).
'",';
155 if (!empty($val[
'index'])) {
156 $texttoinsert .=
' "index" => "'.(int) $val[
'index'].
'",';
158 if (!empty($val[
'foreignkey'])) {
159 $texttoinsert .=
' "foreignkey" => "'.(int) $val[
'foreignkey'].
'",';
161 if (!empty($val[
'searchall'])) {
162 $texttoinsert .=
' "searchall" => "'.(int) $val[
'searchall'].
'",';
164 if (!empty($val[
'isameasure'])) {
165 $texttoinsert .=
' "isameasure" => "'.(int) $val[
'isameasure'].
'",';
167 if (!empty($val[
'css'])) {
168 $texttoinsert .=
' "css" => "'.dol_escape_php($val[
'css']).
'",';
170 if (!empty($val[
'cssview'])) {
171 $texttoinsert .=
' "cssview" => "'.dol_escape_php($val[
'cssview']).
'",';
173 if (!empty($val[
'csslist'])) {
174 $texttoinsert .=
' "csslist" => "'.dol_escape_php($val[
'csslist']).
'",';
176 if (!empty($val[
'help'])) {
177 $texttoinsert .=
' "help" => "'.dol_escape_php($val[
'help']).
'",';
179 if (!empty($val[
'showoncombobox'])) {
180 $texttoinsert .=
' "showoncombobox" => "'.(int) $val[
'showoncombobox'].
'",';
182 if (!empty($val[
'disabled'])) {
183 $texttoinsert .=
' "disabled" => "'.(int) $val[
'disabled'].
'",';
185 if (!empty($val[
'autofocusoncreate'])) {
186 $texttoinsert .=
' "autofocusoncreate" => "'.(int) $val[
'autofocusoncreate'].
'",';
188 if (!empty($val[
'arrayofkeyval'])) {
189 $texttoinsert .=
' "arrayofkeyval" => array(';
191 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
193 $texttoinsert .=
", ";
195 $texttoinsert .=
'"'.dol_escape_php($key2).
'" => "'.
dol_escape_php($val2).
'"';
198 $texttoinsert .=
'),';
200 if (!empty($val[
'validate'])) {
201 $texttoinsert .=
' "validate" => "'.(int) $val[
'validate'].
'",';
203 if (!empty($val[
'comment'])) {
204 $texttoinsert .=
' "comment" => "'.dol_escape_php($val[
'comment']).
'"';
207 $texttoinsert .=
"),\n";
212 $texttoinsert .=
"\t".
');'.
"\n";
218 foreach (
$object->fields as $key => $val) {
221 $texttoinsert .=
"\t".
'public $'.$key.
";";
226 $texttoinsert .=
"\n";
230 $texttoinsert .=
"\t".
'// END MODULEBUILDER PROPERTIES';
234 $contentclass = preg_replace(
'/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
237 dol_mkdir(dirname($pathoffiletoedittarget));
240 $result = file_put_contents(
dol_osencode($pathoffiletoedittarget), $contentclass);
243 dolChmod($pathoffiletoedittarget, $newmask);
250 print $e->getMessage();
274 if (empty($objectname)) {
277 if (empty($readdir)) {
281 $pathoffiletoclasssrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
284 if ($moduletype ==
'internal') {
285 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
286 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
287 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
288 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
289 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
290 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
291 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'.sql';
296 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
297 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
298 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
299 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
300 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
301 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
302 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'.sql';
309 $pathoffiletoedittarget = $destdir.$pathoffiletoeditsrc.($readdir != $destdir ?
'.new' :
'');
310 $pathoffiletoeditsrc = $readdir.$pathoffiletoeditsrc;
313 $langs->load(
"errors");
314 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
321 include_once $pathoffiletoclasssrc;
322 if (class_exists($objectname)) {
323 $object =
new $objectname($db);
329 print $e->getMessage();
333 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
335 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
338 $texttoinsert =
'-- BEGIN MODULEBUILDER FIELDS'.
"\n";
340 foreach (
$object->fields as $key => $val) {
343 $type = $val[
'type'];
344 $type = preg_replace(
'/:.*$/',
'', $type);
346 if ($type ==
'html') {
348 } elseif ($type ==
'price') {
350 } elseif (in_array($type, array(
'link',
'sellist',
'duration'))) {
352 } elseif ($type ==
'chkbxlst') {
353 $type =
'varchar(128)';
354 } elseif ($type ==
'mail' || $type ==
'email') {
355 $type =
'varchar(128)';
356 } elseif (strpos($type,
'stars(') === 0) {
358 } elseif ($type ==
'phone') {
359 $type =
'varchar(20)';
360 } elseif ($type ==
'ip') {
361 $type =
'varchar(32)';
362 } elseif ($type ==
'url') {
363 $type =
'varchar(255)';
366 $texttoinsert .=
"\t".$key.
" ".$type;
367 if ($key ==
'rowid') {
368 $texttoinsert .=
' AUTO_INCREMENT PRIMARY KEY';
369 } elseif ($type ==
'timestamp') {
370 $texttoinsert .=
' DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
372 if ($key ==
'entity') {
373 $texttoinsert .=
' DEFAULT 1';
375 if (!empty($val[
'default'])) {
376 if (preg_match(
'/^null$/i', $val[
'default'])) {
377 $texttoinsert .=
" DEFAULT NULL";
378 } elseif (preg_match(
'/varchar/', $type)) {
379 $texttoinsert .=
" DEFAULT '".$db->escape($val[
'default']).
"'";
381 $texttoinsert .= (($val[
'default'] > 0) ?
' DEFAULT '.$val[
'default'] :
'');
385 $texttoinsert .= ((!empty($val[
'notnull']) && $val[
'notnull'] > 0) ?
' NOT NULL' :
'');
386 if ($i < count(
$object->fields)) {
387 $texttoinsert .=
", ";
389 $texttoinsert .=
"\n";
392 $texttoinsert .=
"\t".
'-- END MODULEBUILDER FIELDS';
394 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
396 $result = file_put_contents($pathoffiletoedittarget, $contentsql);
398 dolChmod($pathoffiletoedittarget, $newmask);
401 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
405 $pathoffiletoeditsrc = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoeditsrc);
406 $pathoffiletoedittarget = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoedittarget);
407 $pathoffiletoedittarget = preg_replace(
'/\.sql.new$/',
'.key.sql.new', $pathoffiletoedittarget);
409 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc));
412 $texttoinsert =
'-- BEGIN MODULEBUILDER INDEXES'.
"\n";
414 foreach (
$object->fields as $key => $val) {
416 if (!empty($val[
'index'])) {
417 $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" :
"").
");";
418 $texttoinsert .=
"\n";
420 if (!empty($val[
'foreignkey'])) {
421 $tmp = explode(
'.', $val[
'foreignkey']);
422 if (!empty($tmp[0]) && !empty($tmp[1])) {
423 $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].
");";
424 $texttoinsert .=
"\n";
429 $texttoinsert .=
'-- END MODULEBUILDER INDEXES';
431 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
433 dol_mkdir(dirname($pathoffiletoedittarget));
435 $result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
437 dolChmod($pathoffiletoedittarget, $newmask);
440 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
443 return $error ? -1 : 1;
455 $listofobject =
dol_dir_list($destdir.
'/class',
'files', 0,
'\.class\.php$');
456 foreach ($listofobject as $fileobj) {
457 if (preg_match(
'/^api_/', $fileobj[
'name'])) {
460 if (preg_match(
'/^actions_/', $fileobj[
'name'])) {
464 $tmpcontent = file_get_contents($fileobj[
'fullname']);
466 if (preg_match(
'/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) {
467 $objectnameloop = $reg[1];
468 $objects[$fileobj[
'fullname']] = $objectnameloop;
471 if (count($objects) > 0) {
487 if (!file_exists($file)) {
491 $content = file_get_contents($file);
494 if (strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU MYOBJECT */') !==
false
495 || strpos($content,
'/* BEGIN MODULEBUILDER TOPMENU */') !==
false) {
498 if (strpos($content,
'/* END MODULEBUILDER TOPMENU MYOBJECT */') !==
false
499 || strpos($content,
'/* END MODULEBUILDER TOPMENU */') !==
false) {
502 if (strpos($content,
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
505 if (strpos($content,
'/* END MODULEBUILDER LEFTMENU MYOBJECT */') !==
false) {
512 } elseif ($number === 1) {
513 if (strpos($content,
'/* BEGIN MODULEBUILDER PERMISSIONS */') !==
false && strpos($content,
'/* END MODULEBUILDER PERMISSIONS */') !==
false) {
516 } elseif ($number == 2) {
517 if (strpos($content,
'/* BEGIN MODULEBUILDER DICTIONARIES */') !==
false && strpos($content,
'/* END MODULEBUILDER DICTIONARIES */') !==
false) {
531 $start =
"/* BEGIN MODULEBUILDER PERMISSIONS */";
532 $end =
"/* END MODULEBUILDER PERMISSIONS */";
535 $lines = file($file);
537 foreach ($lines as $i => $line) {
538 if (strpos($line, $start) !==
false) {
539 $start_line = $i + 1;
542 while (($line = $lines[++$i]) !==
false) {
543 if (strpos($line, $end) !==
false) {
552 $allContent = implode(
"", $array);
564 return strcmp($a[0], $b[0]);
581 if ($action == 0 && $key !==
null) {
583 array_splice($permissions, array_search($permissions[$key], $permissions), 1);
584 } elseif ($action == 1) {
585 array_push($permissions, $right);
586 } elseif ($action == 2 && !empty($right) && $key !==
null) {
588 array_splice($permissions, array_search($permissions[$key], $permissions), 1, $right);
589 } elseif ($action == -1 && !empty($objectname)) {
593 foreach ($permissions as $perms) {
594 if ($perms[4] === strtolower($objectname)) {
595 array_splice($permissions, array_search($perms, $permissions), 1);
598 } elseif ($action == -2 && !empty($objectname) && !empty($module)) {
601 $objectOfRights = array();
603 foreach ($permissions as $right) {
604 $objectOfRights[] = $right[4];
606 if (in_array(strtolower($objectname), $objectOfRights)) {
609 $permsToadd = array();
611 'read' =>
'Read '.$objectname.
' object of '.ucfirst($module),
612 'write' =>
'Create/Update '.$objectname.
' object of '.ucfirst($module),
613 'delete' =>
'Delete '.$objectname.
' object of '.ucfirst($module)
616 foreach ($perms as $index => $value) {
617 $permsToadd[$i][0] =
'';
618 $permsToadd[$i][1] = $value;
619 $permsToadd[$i][4] = strtolower($objectname);
620 $permsToadd[$i][5] = $index;
621 array_push($permissions, $permsToadd[$i]);
628 '@phan-var-force array<int,string[]> $permissions';
631 $count_perms = count($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);
852 $perms =
"|*".strtolower($objectname).
"*|";
853 $search_pattern_perms =
'/' . preg_quote($perms,
'/') .
'.*?\n/';
854 $new_contents = preg_replace($search_pattern_perms,
'', $new_contents);
855 file_put_contents($file, $new_contents);
871function getFromFile($file, $start, $end, $excludestart =
'', $includese = 0)
880 while ($line = fgets($fhandle)) {
881 if (strpos($line, $start) !==
false && (empty($excludestart) || strpos($line, $excludestart) ===
false)) {
886 while (($line = fgets($fhandle)) !==
false) {
887 if (strpos($line, $end) !==
false) {
901 $content = implode(
"", $keys);
915 $start =
'/* BEGIN MODULEBUILDER PERMISSIONS */';
916 $end =
'/* END MODULEBUILDER PERMISSIONS */';
918 if (empty($content)) {
922 $string =
"[options='header',grid=rows,width=60%,caption=Organisation]\n";
925 $header = array($langs->trans(
'Objects'),$langs->trans(
'Permission'));
926 foreach ($header as $h) {
931 $array = explode(
";", $content);
932 $permissions = array_filter($array);
934 $permissions = str_replace(
'$r++',
'1', $permissions);
937 foreach ($permissions as $i => $element) {
939 unset($permissions[$i]);
941 if (str_contains($element,
'$this->numero')) {
942 unset($permissions[$i]);
944 if (str_contains($element,
'$this->rights[$r][5]')) {
945 unset($permissions[$i]);
949 foreach ($permissions as $key => $element) {
950 $element = str_replace(
" '",
'', $element);
951 $element = trim($element,
"'");
952 $permsN[] = substr($element, strpos($element,
"=") + 1);
957 $final_array = array();
959 while ($index < count($permsN)) {
960 $temp_array = array($permsN[$index], $permsN[$index + 1]);
961 $final_array[] = $temp_array;
966 foreach ($final_array as $subarray) {
970 $result[$key][] = $subarray;
972 foreach ($result as $i => $pems) {
973 $string .=
"|*".$i.
"*|";
974 foreach ($pems as $tab) {
975 $string .= $tab[0].
" , ";
980 $string .=
"\n|===\n";
982 $write =
dolReplaceInFile($destfile, array(
'__DATA_PERMISSIONS__' => $string));
1000 global $langs, $user;
1002 if (!file_exists($file)) {
1007 $content = file($file);
1009 $includeClass =
"dol_include_once\(\'\/\w+\/class\/\w+\.class\.php\'\);";
1010 $props =
'public\s+\$\w+;';
1011 $varcommented =
'@var\s+\w+\s+\$\w+\s+{@type\s+\w+}';
1012 $constructObj =
'\$this->\w+\s+=\s+new\s+\w+\(\$this->db\);';
1015 foreach ($content as $lineNumber => &$lineContent) {
1016 if (preg_match(
'/'.$varcommented.
'/', $lineContent)) {
1018 foreach ($objects as $objectname) {
1019 $lineContent .=
"\t * @var ".$objectname.
" \$".strtolower($objectname).
" {@type ".$objectname.
"}". PHP_EOL;
1022 } elseif (preg_match(
'/'.$props.
'/', $lineContent)) {
1024 foreach ($objects as $objectname) {
1025 $lineContent .=
"\t/*".PHP_EOL.
"\t * @var mixed TODO: set type".PHP_EOL.
"\t */".PHP_EOL.
"\tpublic \$".strtolower($objectname).
";". PHP_EOL;
1027 } elseif (preg_match(
'/'.$constructObj.
'/', $lineContent)) {
1029 foreach ($objects as $objectname) {
1030 $lineContent .=
"\t\t\$this->".strtolower($objectname).
" = new ".$objectname.
"(\$this->db);". PHP_EOL;
1032 } elseif (preg_match(
'/'.$includeClass.
'/', $lineContent)) {
1034 foreach ($objects as $objectname) {
1035 $lineContent .=
"dol_include_once('/".strtolower($modulename).
"/class/".strtolower($objectname).
".class.php');". PHP_EOL;
1040 $allContent = implode(
"", $content);
1041 file_put_contents($file, $allContent);
1044 $allContent =
getFromFile($srcfile,
'/* BEGIN MODULEBUILDER API MYOBJECT */',
'/* END MODULEBUILDER API MYOBJECT */');
1045 foreach ($objects as $objectname) {
1046 $arrayreplacement = array(
1047 'mymodule' => strtolower($modulename),
1048 'MyModule' => $modulename,
1049 'MYMODULE' => strtoupper($modulename),
1050 'My module' => $modulename,
1051 'my module' => $modulename,
1052 'Mon module' => $modulename,
1053 'mon module' => $modulename,
1054 'htdocs/modulebuilder/template' => strtolower($modulename),
1055 'myobject' => strtolower($objectname),
1056 'MyObject' => $objectname,
1057 'MYOBJECT' => strtoupper($objectname),
1058 '---Replace with your own copyright and developer email---' =>
dol_print_date($now,
'%Y').
' '.$user->getFullName($langs).($user->email ?
' <'.$user->email.
'>' :
'')
1064 '/* BEGIN MODULEBUILDER API MYOBJECT */' =>
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */'.$contentReplaced.
"\t".
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */'.
"\n\n\n\t".
'/* BEGIN MODULEBUILDER API MYOBJECT */'
1085 if (!file_exists($file)) {
1089 $content = file($file);
1091 $includeClass =
"dol_include_once\(\'\/\w+\/class\/".strtolower($objectname).
"\.class\.php\'\);";
1092 $props =
'public\s+\$'.strtolower($objectname);
1093 $varcommented =
'@var\s+\w+\s+\$'.strtolower($objectname).
'\s+{@type\s+\w+}';
1094 $constructObj =
'\$this->'.strtolower($objectname).
'\s+=\s+new\s+\w+\(\$this->db\);';
1097 foreach ($content as $lineNumber => &$lineContent) {
1098 if (preg_match(
'/'.$varcommented.
'/i', $lineContent)) {
1100 } elseif (preg_match(
'/'.$props.
'/i', $lineContent)) {
1102 } elseif (preg_match(
'/'.$constructObj.
'/i', $lineContent)) {
1104 } elseif (preg_match(
'/'.$includeClass.
'/i', $lineContent)) {
1109 $allContent = implode(
"", $content);
1110 file_put_contents($file, $allContent);
1113 $begin =
'/* BEGIN MODULEBUILDER API '.strtoupper($objectname).
' */';
1114 $end =
'/* END MODULEBUILDER API '.strtoupper($objectname).
' */';
1137 if (!file_exists($file)) {
1141 if ($action == 0 && !empty($key)) {
1143 array_splice($menus, array_search($menus[$key], $menus), 1);
1144 } elseif ($action == 1) {
1146 array_push($menus, $menuWantTo);
1147 } elseif ($action == 2 && !empty($key) && !empty($menuWantTo)) {
1151 foreach ($menus as $index => $menu) {
1152 if ($index !== $key) {
1153 if ($menu[
'type'] === $menuWantTo[
'type']) {
1154 if (strcasecmp(str_replace(
' ',
'', $menu[
'titre']), str_replace(
' ',
'', $menuWantTo[
'titre'])) === 0) {
1157 if (strcasecmp(str_replace(
' ',
'', $menu[
'url']), str_replace(
' ',
'', $menuWantTo[
'url'])) === 0) {
1163 if (!$counter && $urlCounter < 2) {
1164 $menus[$key] = $menuWantTo;
1168 } elseif ($action == -1 && !empty($menuWantTo) && is_string($menuWantTo)) {
1170 foreach ($menus as $index => $menu) {
1171 if ((strpos(strtolower($menu[
'fk_menu']), strtolower($menuWantTo)) !==
false) || (strpos(strtolower($menu[
'leftmenu']), strtolower($menuWantTo)) !==
false)) {
1172 array_splice($menus, array_search($menu, $menus), 1);
1180 $beginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU';
1181 $excludeBeginMenu =
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT';
1182 $endMenu =
'/* END MODULEBUILDER LEFTMENU';
1184 while ($protection <= 1000 && $allMenus =
getFromFile($file, $beginMenu, $endMenu, $excludeBeginMenu, 1)) {
1191 foreach ($menus as $index => $menu) {
1192 $menu[
'position'] =
"1000 + \$r";
1193 if ($menu[
'type'] ===
'left') {
1194 $start =
"\t\t".
'/* BEGIN MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1195 $end =
"\t\t".
'/* END MODULEBUILDER LEFTMENU '.strtoupper(empty($menu[
'object']) ? $menu[
'titre'] : $menu[
'object']).
' */';
1197 $val_actuel = $menu;
1198 $next_val = empty($menus[$index + 1]) ? null : $menus[$index + 1];
1201 $str_menu .= $start.
"\n";
1202 $str_menu .=
"\t\t\$this->menu[\$r++] = array(\n";
1203 $str_menu .=
"\t\t\t'fk_menu' => '".dol_escape_php($menu[
'fk_menu'], 1).
"',\n";
1204 $str_menu .=
"\t\t\t'type' => '".dol_escape_php($menu[
'type'], 1).
"',\n";
1205 $str_menu .=
"\t\t\t'titre' => '".dol_escape_php($menu[
'titre'], 1).
"',\n";
1206 $str_menu .=
"\t\t\t'mainmenu' => '".dol_escape_php($menu[
'mainmenu'], 1).
"',\n";
1207 $str_menu .=
"\t\t\t'leftmenu' => '".dol_escape_php($menu[
'leftmenu'], 1).
"',\n";
1208 $str_menu .=
"\t\t\t'url' => '".dol_escape_php($menu[
'url'], 1).
"',\n";
1209 $str_menu .=
"\t\t\t'langs' => '".dol_escape_php($menu[
'langs'], 1).
"',\n";
1210 $str_menu .=
"\t\t\t'position' => ".((int) $menu[
'position']).
",\n";
1211 $str_menu .=
"\t\t\t'enabled' => '".dol_escape_php((
string) $menu[
'enabled'], 1).
"',\n";
1212 $str_menu .=
"\t\t\t'perms' => '".dol_escape_php($menu[
'perms'], 1).
"',\n";
1213 $str_menu .=
"\t\t\t'target' => '".dol_escape_php($menu[
'target'], 1).
"',\n";
1214 $str_menu .=
"\t\t\t'user' => ".((int) $menu[
'user']).
",\n";
1215 $str_menu .=
"\t\t\t'object' => '".dol_escape_php($menu[
'object'], 1).
"',\n";
1216 $str_menu .=
"\t\t);\n";
1218 if (is_null($next_val) || $val_actuel[
'leftmenu'] !== $next_val[
'leftmenu']) {
1219 $str_menu .= $end.
"\n";
1224 dolReplaceInFile($file, array(
'/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */' => $str_menu.
"\n\t\t/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */"));
1241 $dicData =
"\t\t\$this->dictionaries = array(\n";
1242 $module = strtolower($module);
1243 foreach ($dicts as $key => $value) {
1244 if (empty($value)) {
1246 $dicData =
"\t\t\$this->dictionaries = array();";
1250 $dicData .=
"\t\t\t'$key' => ";
1252 if ($key ===
'tabcond') {
1253 $conditions = array_map(
1258 static function ($val) use ($module) {
1259 return is_bool($val) ?
"isModEnabled('$module')" : $val;
1263 $dicData .=
"array(" . implode(
", ", $conditions) .
")";
1264 } elseif ($key ===
'tabhelp') {
1265 $helpItems = array();
1266 foreach ($value as $helpValue) {
1267 $helpItems[] =
"array('code' => \$langs->trans('".$helpValue[
'code'].
"'), 'field2' => 'field2tooltip')";
1269 $dicData .=
"array(" . implode(
",", $helpItems) .
")";
1271 if (is_array($value)) {
1272 $dicData .=
"array(" . implode(
1279 static function ($val) {
1286 $dicData .=
"'$value'";
1291 $dicData .= (!$isEmpty ?
"\t\t);" :
'');
1293 $stringDic =
getFromFile($file,
'/* BEGIN MODULEBUILDER DICTIONARIES */',
'/* END MODULEBUILDER DICTIONARIES */');
1294 $writeInfile =
dolReplaceInFile($file, array($stringDic => $dicData.
"\n"));
1296 return $writeInfile;
1315 if (empty($namedic)) {
1319 if (!file_exists($file)) {
1322 $modulename = strtolower($modulename);
1324 if (empty($dictionnaires)) {
1325 $dictionnaires = array(
'langs' =>
'',
'tabname' => array(),
'tablib' => array(),
'tabsql' => array(),
'tabsqlsort' => array(),
'tabfield' => array(),
'tabfieldvalue' => array(),
'tabfieldinsert' => array(),
'tabrowid' => array(),
'tabcond' => array(),
'tabhelp' => array());
1329 'rowid' => array(
'type' =>
'integer',
'value' => 11,
'extra' =>
'AUTO_INCREMENT'),
1330 'code' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1331 'label' => array(
'type' =>
'varchar',
'value' => 255,
'null' =>
'NOT NULL'),
1332 'position' => array(
'type' =>
'integer',
'value' => 11,
'null' =>
'NULL'),
1333 'use_default' => array(
'type' =>
'varchar',
'value' => 11,
'default' =>
'1'),
1334 'active' => array(
'type' =>
'integer',
'value' => 3)
1337 $primaryKey =
'rowid';
1338 foreach ($columns as $key => $value) {
1339 if ($key ===
'rowid') {
1340 $primaryKey =
'rowid';
1343 if (!array_key_exists(
'rowid', $columns)) {
1344 $primaryKey = array_key_first($columns);
1350 $checkTable = $db->DDLDescTable(MAIN_DB_PREFIX.strtolower($namedic));
1351 if ($checkTable && $db->num_rows($checkTable) > 0) {
1352 setEventMessages($langs->trans(
"ErrorTableExist", $namedic),
null,
'errors');
1355 $_results = $db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey,
"");
1356 if ($_results < 0) {
1358 $langs->load(
"errors");
1359 setEventMessages($langs->trans(
"ErrorTableNotFound", $namedic),
null,
'errors');
1364 $dictionnaires[
'langs'] = $modulename.
'@'.$modulename;
1365 $dictionnaires[
'tabname'][] = strtolower($namedic);
1366 $dictionnaires[
'tablib'][] = ucfirst(substr($namedic, 2));
1367 $dictionnaires[
'tabsql'][] =
'SELECT t.rowid as rowid, t.code, t.label, t.active FROM '.MAIN_DB_PREFIX.strtolower($namedic).
' as t';
1368 $dictionnaires[
'tabsqlsort'][] = (array_key_exists(
'label', $columns) ?
'label ASC' :
'');
1369 $dictionnaires[
'tabfield'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1370 $dictionnaires[
'tabfieldvalue'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1371 $dictionnaires[
'tabfieldinsert'][] = (array_key_exists(
'code', $columns) && array_key_exists(
'label', $columns) ?
'code,label' :
'');
1372 $dictionnaires[
'tabrowid'][] = $primaryKey;
1373 $dictionnaires[
'tabcond'][] = isModEnabled(
'$modulename');
1374 $dictionnaires[
'tabhelp'][] = (array_key_exists(
'code', $columns) ? array(
'code' => $langs->trans(
'CodeTooltipHelp'),
'field2' =>
'field2tooltip') :
'');
1378 if ($writeInfile > 0) {
1379 setEventMessages($langs->trans(
"DictionariesCreated", ucfirst(substr($namedic, 2))),
null);
1398 $string =
getFromFile($file_api,
'/*begin methods CRUD*/',
'/*end methods CRUD*/');
1399 $extractUrls = explode(
"\n", $string);
1402 $urlValues = array();
1403 foreach ($extractUrls as $key => $line) {
1404 $lineWithoutTabsSpaces = preg_replace(
'/^[\t\s]+/',
'', $line);
1405 if (strpos($lineWithoutTabsSpaces,
'* @url') === 0) {
1406 $urlValue = trim(substr($lineWithoutTabsSpaces, strlen(
'* @url')));
1407 $urlValues[] = $urlValue;
1412 $str = $_SERVER[
'HTTP_HOST'].
'/api/index.php/';
1413 $groupedUrls = array();
1414 foreach ($urlValues as $url) {
1415 if (preg_match(
'/(?:GET|POST|PUT|DELETE) (\w+)s/', $url, $matches)) {
1416 $objectName = $matches[1];
1417 $url = $str.trim(strstr($url,
' '));
1418 $groupedUrls[$objectName][] = $url;
1421 if (empty($groupedUrls)) {
1427 $asciiDocTable =
"[options=\"header\"]\n|===\n|Object | URLs\n";
1428 foreach ($groupedUrls as $objectName => $urls) {
1429 $urlsList = implode(
" +\n*", $urls);
1430 $asciiDocTable .=
"|$objectName | \n*$urlsList +\n";
1432 $asciiDocTable .=
"|===\n";
1433 $file_write =
dolReplaceInFile($file_doc, array(
'__API_DOC__' =>
'__API_DOC__'.
"\n".$asciiDocTable));
1434 if ($file_write < 0) {
1451 if (!is_dir($path)) {
1455 $allFilesAndDirs = scandir($path);
1458 foreach ($allFilesAndDirs as $item) {
1459 if ($item !=
'.' && $item !=
'..') {
1460 if ($type == 1 && is_file($path . DIRECTORY_SEPARATOR . $item) && strpos($item,
'.back') ===
false) {
1462 } 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.