dolibarr 19.0.3
modulebuilder.lib.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2009-2010 Laurent Destailleur <eldy@users.sourceforge.net>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 * or see https://www.gnu.org/
17 */
18
38function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir = '', $addfieldentry = array(), $delfieldentry = '')
39{
40 global $db, $langs;
41
42 if (empty($objectname)) {
43 return -6;
44 }
45 if (empty($readdir)) {
46 $readdir = $destdir;
47 }
48
49 if (!empty($addfieldentry['arrayofkeyval']) && !is_array($addfieldentry['arrayofkeyval'])) {
50 dol_print_error('', 'Bad parameter addfieldentry with a property arrayofkeyval defined but that is not an array.');
51 return -7;
52 }
53
54 $error = 0;
55
56 // Check parameters
57 if (is_array($addfieldentry) && count($addfieldentry) > 0) {
58 if (empty($addfieldentry['name'])) {
59 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Name")), null, 'errors');
60 return -2;
61 }
62 if (empty($addfieldentry['label'])) {
63 setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Label")), null, 'errors');
64 return -2;
65 }
66 if (!preg_match('/^(integer|price|sellist|varchar|double|text|html|duration)/', $addfieldentry['type'])
67 && !preg_match('/^(boolean|smallint|real|date|datetime|timestamp|phone|mail|url|ip|password)$/', $addfieldentry['type'])) {
68 setEventMessages($langs->trans('BadValueForType', $addfieldentry['type']), null, 'errors');
69 return -2;
70 }
71 }
72
73 $pathoffiletoeditsrc = $readdir.'/class/'.strtolower($objectname).'.class.php';
74 $pathoffiletoedittarget = $destdir.'/class/'.strtolower($objectname).'.class.php'.($readdir != $destdir ? '.new' : '');
75 if (!dol_is_file($pathoffiletoeditsrc)) {
76 $langs->load("errors");
77 setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors');
78 return -3;
79 }
80
81 //$pathoffiletoedittmp=$destdir.'/class/'.strtolower($objectname).'.class.php.tmp';
82 //dol_delete_file($pathoffiletoedittmp, 0, 1, 1);
83
84 try {
85 include_once $pathoffiletoeditsrc;
86 if (class_exists($objectname)) {
87 $object = new $objectname($db);
88 } else {
89 return -4;
90 }
91
92 // Backup old file
93 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.'.back', $newmask, 1);
94
95 // Edit class files
96 $contentclass = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
97
98 // Update ->fields (to add or remove entries defined into $addfieldentry)
99 if (count($object->fields)) {
100 if (is_array($addfieldentry) && count($addfieldentry)) {
101 $name = $addfieldentry['name'];
102 unset($addfieldentry['name']);
103
104 $object->fields[$name] = $addfieldentry;
105 }
106 if (!empty($delfieldentry)) {
107 $name = $delfieldentry;
108 unset($object->fields[$name]);
109 }
110 }
111
112 dol_sort_array($object->fields, 'position');
113
114 $i = 0;
115 $texttoinsert = '// BEGIN MODULEBUILDER PROPERTIES'."\n";
116 $texttoinsert .= "\t".'
118'."\n";
119 $texttoinsert .= "\t".'public $fields=array('."\n";
120
121 if (count($object->fields)) {
122 foreach ($object->fields as $key => $val) {
123 $i++;
124 $texttoinsert .= "\t\t".'"'.$key.'" => array(';
125 $texttoinsert .= '"type"=>"'.dol_escape_php($val['type']).'",';
126 $texttoinsert .= ' "label"=>"'.dol_escape_php($val['label']).'",';
127 if (!empty($val['picto'])) {
128 $texttoinsert .= ' "picto"=>"'.dol_escape_php($val['picto']).'",';
129 }
130 $texttoinsert .= ' "enabled"=>"'.($val['enabled'] !== '' ? dol_escape_php($val['enabled']) : 1).'",';
131 $texttoinsert .= " 'position'=>".($val['position'] !== '' ? (int) $val['position'] : 50).",";
132 $texttoinsert .= " 'notnull'=>".(empty($val['notnull']) ? 0 : (int) $val['notnull']).",";
133 $texttoinsert .= ' "visible"=>"'.($val['visible'] !== '' ? dol_escape_js($val['visible']) : -1).'",';
134 if (!empty($val['noteditable'])) {
135 $texttoinsert .= ' "noteditable"=>"'.dol_escape_php($val['noteditable']).'",';
136 }
137 if (!empty($val['alwayseditable'])) {
138 $texttoinsert .= ' "alwayseditable"=>"'.dol_escape_php($val['alwayseditable']).'",';
139 }
140 if (!empty($val['default']) || (isset($val['default']) && $val['default'] === '0')) {
141 $texttoinsert .= ' "default"=>"'.dol_escape_php($val['default']).'",';
142 }
143 if (!empty($val['index'])) {
144 $texttoinsert .= ' "index"=>"'.(int) $val['index'].'",';
145 }
146 if (!empty($val['foreignkey'])) {
147 $texttoinsert .= ' "foreignkey"=>"'.(int) $val['foreignkey'].'",';
148 }
149 if (!empty($val['searchall'])) {
150 $texttoinsert .= ' "searchall"=>"'.(int) $val['searchall'].'",';
151 }
152 if (!empty($val['isameasure'])) {
153 $texttoinsert .= ' "isameasure"=>"'.(int) $val['isameasure'].'",';
154 }
155 if (!empty($val['css'])) {
156 $texttoinsert .= ' "css"=>"'.dol_escape_php($val['css']).'",';
157 }
158 if (!empty($val['cssview'])) {
159 $texttoinsert .= ' "cssview"=>"'.dol_escape_php($val['cssview']).'",';
160 }
161 if (!empty($val['csslist'])) {
162 $texttoinsert .= ' "csslist"=>"'.dol_escape_php($val['csslist']).'",';
163 }
164 if (!empty($val['help'])) {
165 $texttoinsert .= ' "help"=>"'.dol_escape_php($val['help']).'",';
166 }
167 if (!empty($val['showoncombobox'])) {
168 $texttoinsert .= ' "showoncombobox"=>"'.(int) $val['showoncombobox'].'",';
169 }
170 if (!empty($val['disabled'])) {
171 $texttoinsert .= ' "disabled"=>"'.(int) $val['disabled'].'",';
172 }
173 if (!empty($val['autofocusoncreate'])) {
174 $texttoinsert .= ' "autofocusoncreate"=>"'.(int) $val['autofocusoncreate'].'",';
175 }
176 if (!empty($val['arrayofkeyval'])) {
177 $texttoinsert .= ' "arrayofkeyval"=>array(';
178 $i = 0;
179 foreach ($val['arrayofkeyval'] as $key2 => $val2) {
180 if ($i) {
181 $texttoinsert .= ", ";
182 }
183 $texttoinsert .= '"'.dol_escape_php($key2).'" => "'.dol_escape_php($val2).'"';
184 $i++;
185 }
186 $texttoinsert .= '),';
187 }
188 if (!empty($val['validate'])) {
189 $texttoinsert .= ' "validate"=>"'.(int) $val['validate'].'",';
190 }
191 if (!empty($val['comment'])) {
192 $texttoinsert .= ' "comment"=>"'.dol_escape_php($val['comment']).'"';
193 }
194
195 $texttoinsert .= "),\n";
196 //print $texttoinsert;
197 }
198 }
199
200 $texttoinsert .= "\t".');'."\n";
201 //print ($texttoinsert);exit;
202
203 if (count($object->fields)) {
204 //$typetotypephp=array('integer'=>'integer', 'duration'=>'integer', 'varchar'=>'string');
205
206 foreach ($object->fields as $key => $val) {
207 $i++;
208 //$typephp=$typetotypephp[$val['type']];
209 $texttoinsert .= "\t".'public $'.$key.";";
210 //if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY';
211 //if ($key == 'entity') $texttoinsert.= ' DEFAULT 1';
212 //$texttoinsert.= ($val['notnull']?' NOT NULL':'');
213 //if ($i < count($object->fields)) $texttoinsert.=";";
214 $texttoinsert .= "\n";
215 }
216 }
217
218 $texttoinsert .= "\t".'// END MODULEBUILDER PROPERTIES';
219
220 //print($texttoinsert);
221
222 $contentclass = preg_replace('/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
223 //print $contentclass;
224
225 dol_mkdir(dirname($pathoffiletoedittarget));
226
227 //file_put_contents($pathoffiletoedittmp, $contentclass);
228 $result = file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass);
229
230 if ($result) {
231 dolChmod($pathoffiletoedittarget, $newmask);
232 } else {
233 $error++;
234 }
235
236 return $error ? -1 : $object;
237 } catch (Exception $e) {
238 print $e->getMessage();
239 return -5;
240 }
241}
242
256function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir = '', $object = null, $moduletype = 'external')
257{
258 global $db, $langs;
259
260 $error = 0;
261
262 if (empty($objectname)) {
263 return -1;
264 }
265 if (empty($readdir)) {
266 $readdir = $destdir;
267 }
268
269 $pathoffiletoclasssrc = $readdir.'/class/'.strtolower($objectname).'.class.php';
270
271 // Edit .sql file
272 if ($moduletype == 'internal') {
273 $pathoffiletoeditsrc = '/../install/mysql/tables/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql';
274 if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
275 $pathoffiletoeditsrc = '/../install/mysql/tables/llx_'.strtolower($module).'_'.strtolower($objectname).'-'.strtolower($module).'.sql';
276 if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
277 $pathoffiletoeditsrc = '/../install/mysql/tables/llx_'.strtolower($module).'-'.strtolower($module).'.sql';
278 if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
279 $pathoffiletoeditsrc = '/../install/mysql/tables/llx_'.strtolower($module).'.sql';
280 }
281 }
282 }
283 } else {
284 $pathoffiletoeditsrc = '/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql';
285 if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
286 $pathoffiletoeditsrc = '/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'-'.strtolower($module).'.sql';
287 if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
288 $pathoffiletoeditsrc = '/sql/llx_'.strtolower($module).'-'.strtolower($module).'.sql';
289 if (! dol_is_file($readdir.$pathoffiletoeditsrc)) {
290 $pathoffiletoeditsrc = '/sql/llx_'.strtolower($module).'.sql';
291 }
292 }
293 }
294 }
295
296 // Complete path to be full path
297 $pathoffiletoedittarget = $destdir.$pathoffiletoeditsrc.($readdir != $destdir ? '.new' : '');
298 $pathoffiletoeditsrc = $readdir.$pathoffiletoeditsrc;
299
300 if (!dol_is_file($pathoffiletoeditsrc)) {
301 $langs->load("errors");
302 setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors');
303 return -1;
304 }
305
306 // Load object from myobject.class.php
307 try {
308 if (!is_object($object)) {
309 include_once $pathoffiletoclasssrc;
310 if (class_exists($objectname)) {
311 $object = new $objectname($db);
312 } else {
313 return -1;
314 }
315 }
316 } catch (Exception $e) {
317 print $e->getMessage();
318 }
319
320 // Backup old file
321 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.'.back', $newmask, 1);
322
323 $contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
324
325 $i = 0;
326 $texttoinsert = '-- BEGIN MODULEBUILDER FIELDS'."\n";
327 if (count($object->fields)) {
328 foreach ($object->fields as $key => $val) {
329 $i++;
330
331 $type = $val['type'];
332 $type = preg_replace('/:.*$/', '', $type); // For case type = 'integer:Societe:societe/class/societe.class.php'
333
334 if ($type == 'html') {
335 $type = 'text'; // html modulebuilder type is a text type in database
336 } elseif ($type == 'price') {
337 $type = 'double'; // html modulebuilder type is a text type in database
338 } elseif (in_array($type, array('link', 'sellist', 'duration'))) {
339 $type = 'integer';
340 } elseif ($type == 'mail') {
341 $type = 'varchar(128)';
342 } elseif ($type == 'phone') {
343 $type = 'varchar(20)';
344 } elseif ($type == 'ip') {
345 $type = 'varchar(32)';
346 }
347
348 $texttoinsert .= "\t".$key." ".$type;
349 if ($key == 'rowid') {
350 $texttoinsert .= ' AUTO_INCREMENT PRIMARY KEY';
351 } elseif ($type == 'timestamp') {
352 $texttoinsert .= ' DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
353 }
354 if ($key == 'entity') {
355 $texttoinsert .= ' DEFAULT 1';
356 } else {
357 if (!empty($val['default'])) {
358 if (preg_match('/^null$/i', $val['default'])) {
359 $texttoinsert .= " DEFAULT NULL";
360 } elseif (preg_match('/varchar/', $type)) {
361 $texttoinsert .= " DEFAULT '".$db->escape($val['default'])."'";
362 } else {
363 $texttoinsert .= (($val['default'] > 0) ? ' DEFAULT '.$val['default'] : '');
364 }
365 }
366 }
367 $texttoinsert .= ((!empty($val['notnull']) && $val['notnull'] > 0) ? ' NOT NULL' : '');
368 if ($i < count($object->fields)) {
369 $texttoinsert .= ", ";
370 }
371 $texttoinsert .= "\n";
372 }
373 }
374 $texttoinsert .= "\t".'-- END MODULEBUILDER FIELDS';
375
376 $contentsql = preg_replace('/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
377
378 $result = file_put_contents($pathoffiletoedittarget, $contentsql);
379 if ($result) {
380 dolChmod($pathoffiletoedittarget, $newmask);
381 } else {
382 $error++;
383 setEventMessages($langs->trans("ErrorFailToCreateFile", $pathoffiletoedittarget), null, 'errors');
384 }
385
386 // Edit .key.sql file
387 $pathoffiletoeditsrc = preg_replace('/\.sql$/', '.key.sql', $pathoffiletoeditsrc);
388 $pathoffiletoedittarget = preg_replace('/\.sql$/', '.key.sql', $pathoffiletoedittarget);
389 $pathoffiletoedittarget = preg_replace('/\.sql.new$/', '.key.sql.new', $pathoffiletoedittarget);
390
391 $contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
392
393 $i = 0;
394 $texttoinsert = '-- BEGIN MODULEBUILDER INDEXES'."\n";
395 if (count($object->fields)) {
396 foreach ($object->fields as $key => $val) {
397 $i++;
398 if (!empty($val['index'])) {
399 $texttoinsert .= "ALTER TABLE llx_".strtolower($module).'_'.strtolower($objectname)." ADD INDEX idx_".strtolower($module).'_'.strtolower($objectname)."_".$key." (".$key.");";
400 $texttoinsert .= "\n";
401 }
402 if (!empty($val['foreignkey'])) {
403 $tmp = explode('.', $val['foreignkey']);
404 if (!empty($tmp[0]) && !empty($tmp[1])) {
405 $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].");";
406 $texttoinsert .= "\n";
407 }
408 }
409 }
410 }
411 $texttoinsert .= '-- END MODULEBUILDER INDEXES';
412
413 $contentsql = preg_replace('/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
414
415 dol_mkdir(dirname($pathoffiletoedittarget));
416
417 $result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
418 if ($result2) {
419 dolChmod($pathoffiletoedittarget, $newmask);
420 } else {
421 $error++;
422 setEventMessages($langs->trans("ErrorFailToCreateFile", $pathoffiletoedittarget), null, 'errors');
423 }
424
425 return $error ? -1 : 1;
426}
427
435{
436 $objects = array();
437 $listofobject = dol_dir_list($destdir.'/class', 'files', 0, '\.class\.php$');
438 foreach ($listofobject as $fileobj) {
439 if (preg_match('/^api_/', $fileobj['name'])) {
440 continue;
441 }
442 if (preg_match('/^actions_/', $fileobj['name'])) {
443 continue;
444 }
445
446 $tmpcontent = file_get_contents($fileobj['fullname']);
447 $reg = array();
448 if (preg_match('/class\s+([^\s]*)\s+extends\s+CommonObject/ims', $tmpcontent, $reg)) {
449 $objectnameloop = $reg[1];
450 $objects[$fileobj['fullname']] = $objectnameloop;
451 }
452 }
453 if (count($objects)>0) {
454 return $objects;
455 }
456
457 return -1;
458}
465function checkExistComment($file, $number)
466{
467 if (!file_exists($file)) {
468 return -1;
469 }
470 $content = file_get_contents($file);
471 if ($number === 0) {
472 if (strpos($content, '/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */') !== false && strpos($content, '/* END MODULEBUILDER LEFTMENU MYOBJECT */') !== false) {
473 return 1;
474 }
475 } elseif ($number === 1) {
476 if (strpos($content, '/* BEGIN MODULEBUILDER PERMISSIONS */') !== false && strpos($content, '/* END MODULEBUILDER PERMISSIONS */') !== false) {
477 return 1;
478 }
479 } elseif ($number == 2) {
480 if (strpos($content, '/* BEGIN MODULEBUILDER DICTIONARIES */') !== false && strpos($content, '/* END MODULEBUILDER DICTIONARIES */') !== false) {
481 return 1;
482 }
483 }
484 return -1;
485}
492function deletePerms($file)
493{
494 $start = "/* BEGIN MODULEBUILDER PERMISSIONS */";
495 $end = "/* END MODULEBUILDER PERMISSIONS */";
496 $i = 1;
497 $array = array();
498 $lines = file($file);
499 // Search for start and end lines
500 foreach ($lines as $i => $line) {
501 if (strpos($line, $start) !== false) {
502 $start_line = $i + 1;
503
504 // Copy lines until the end on array
505 while (($line = $lines[++$i]) !== false) {
506 if (strpos($line, $end) !== false) {
507 $end_line = $i + 1;
508 break;
509 }
510 $array[] = $line;
511 }
512 break;
513 }
514 }
515 $allContent = implode("", $array);
516 dolReplaceInFile($file, array($allContent => ''));
517}
518
525function compareFirstValue($a, $b)
526{
527 return strcmp($a[0], $b[0]);
528}
540function reWriteAllPermissions($file, $permissions, $key, $right, $objectname, $module, $action)
541{
542 $error = 0;
543 $rights = array();
544 if ($action == 0) {
545 // delete right from permissions array
546 array_splice($permissions, array_search($permissions[$key], $permissions), 1);
547 } elseif ($action == 1) {
548 array_push($permissions, $right);
549 } elseif ($action == 2 && !empty($right)) {
550 // update right from permissions array
551 array_splice($permissions, array_search($permissions[$key], $permissions), 1, $right);
552 } elseif ($action == -1 && !empty($objectname)) {
553 // when delete object
554 $key = null;
555 $right = null;
556 foreach ($permissions as $perms) {
557 if ($perms[4] === strtolower($objectname)) {
558 array_splice($permissions, array_search($perms, $permissions), 1);
559 }
560 }
561 } elseif ($action == -2 && !empty($objectname) && !empty($module)) {
562 $key= null;
563 $right = null;
564 $objectOfRights = array();
565 //check if object already declared in rights file
566 foreach ($permissions as $right) {
567 $objectOfRights[]= $right[4];
568 }
569 if (in_array(strtolower($objectname), $objectOfRights)) {
570 $error++;
571 } else {
572 $permsToadd = array();
573 $perms = array(
574 'read' => 'Read '.$objectname.' object of '.ucfirst($module),
575 'write' => 'Create/Update '.$objectname.' object of '.ucfirst($module),
576 'delete' => 'Delete '.$objectname.' object of '.ucfirst($module)
577 );
578 $i = 0;
579 foreach ($perms as $index => $value) {
580 $permsToadd[$i][0] = '';
581 $permsToadd[$i][1] = $value;
582 $permsToadd[$i][4] = strtolower($objectname);
583 $permsToadd[$i][5] = $index;
584 array_push($permissions, $permsToadd[$i]);
585 $i++;
586 }
587 }
588 } else {
589 $error++;
590 }
591 if (!$error) {
592 // prepare permissions array
593 $count_perms = count($permissions);
594 for ($i = 0;$i<$count_perms;$i++) {
595 $permissions[$i][0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', \$r + 1)";
596 $permissions[$i][1] = "\$this->rights[\$r][1] = '".$permissions[$i][1]."'";
597 $permissions[$i][4] = "\$this->rights[\$r][4] = '".$permissions[$i][4]."'";
598 $permissions[$i][5] = "\$this->rights[\$r][5] = '".$permissions[$i][5]."';\n\t\t";
599 }
600 // for group permissions by object
601 $perms_grouped = array();
602 foreach ($permissions as $perms) {
603 $object = $perms[4];
604 if (!isset($perms_grouped[$object])) {
605 $perms_grouped[$object] = [];
606 }
607 $perms_grouped[$object][] = $perms;
608 }
609 //$perms_grouped = array_values($perms_grouped);
610 $permissions = $perms_grouped;
611
612
613 // parcourir les objets
614 $o=0;
615 foreach ($permissions as &$object) {
616 // récupérer la permission de l'objet
617 $p = 1;
618 foreach ($object as &$obj) {
619 if (str_contains($obj[5], 'read')) {
620 $obj[0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o." * 10) + 0 + 1)";
621 } elseif (str_contains($obj[5], 'write')) {
622 $obj[0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o." * 10) + 1 + 1)";
623 } elseif (str_contains($obj[5], 'delete')) {
624 $obj[0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o." * 10) + 2 + 1)";
625 } else {
626 $obj[0] = "\$this->rights[\$r][0] = \$this->numero . sprintf('%02d', (".$o." * 10) + ".$p." + 1)";
627 $p++;
628 }
629 }
630 usort($object, 'compareFirstValue');
631 $o++;
632 }
633
634 //convert to string
635 foreach ($permissions as $perms) {
636 foreach ($perms as $per) {
637 $rights[] = implode(";\n\t\t", $per);
638 $rights[] = "\$r++;\n\t\t";
639 }
640 }
641 $rights_str = implode("", $rights);
642 // delete all permission from file
643 deletePerms($file);
644 // rewrite all permission again
645 dolReplaceInFile($file, array('/* BEGIN MODULEBUILDER PERMISSIONS */' => '/* BEGIN MODULEBUILDER PERMISSIONS */'."\n\t\t".$rights_str));
646 return 1;
647 } else {
648 return -1;
649 }
650}
651
658function parsePropertyString($string)
659{
660 $string = str_replace("'", '', $string);
661
662 // Uses a regular expression to capture keys and values
663 preg_match_all('/\s*([^\s=>]+)\s*=>\s*([^,]+),?/', $string, $matches, PREG_SET_ORDER);
664 $propertyArray = [];
665
666 foreach ($matches as $match) {
667 $key = trim($match[1]);
668 $value = trim($match[2]);
669
670 if (strpos($value, 'array(') === 0) {
671 $nestedArray = substr($value, 6);
672 $nestedArray = parsePropertyString($nestedArray);
673 $value = $nestedArray;
674 } elseif (strpos($value, '"Id")') !== false) {
675 $value = str_replace(')', '', $value);
676 } else {
677 if (is_numeric($value)) {
678 if (strpos($value, '.') !== false) {
679 $value = (float) $value;
680 } else {
681 $value = (int) $value;
682 }
683 } else {
684 if ($value === 'true') {
685 $value = true;
686 } elseif ($value === 'false') {
687 $value = false;
688 }
689 }
690 }
691 $propertyArray[$key] = $value;
692 }
693
694 return $propertyArray;
695}
696
704function writePropsInAsciiDoc($file, $objectname, $destfile)
705{
706
707 // stock all properties in array
708 $attributesUnique = array('type','label', 'enabled', 'position', 'notnull', 'visible', 'noteditable', 'index', 'default' , 'foreignkey', 'arrayofkeyval', 'alwayseditable','validate', 'searchall','comment', 'isameasure', 'css', 'cssview','csslist', 'help', 'showoncombobox','picto' );
709
710 $start = "public \$fields=array(";
711 $end = ");";
712 $i = 1;
713 $keys = array();
714 $lines = file($file);
715 // Search for start and end lines
716 foreach ($lines as $i => $line) {
717 if (strpos($line, $start) !== false) {
718 // Copy lines until the end on array
719 while (($line = $lines[++$i]) !== false) {
720 if (strpos($line, $end) !== false) {
721 break;
722 }
723 $keys[] = $line;
724 }
725 break;
726 }
727 }
728 // write the begin of table with specifics options
729 $table = "== DATA SPECIFICATIONS\n";
730 $table .= "=== Table of fields with properties for object *$objectname* : \n";
731 $table .= "[options='header',grid=rows,frame=topbot,width=100%,caption=Organisation]\n";
732 $table .= "|===\n";
733 $table .= "|code";
734 // write all properties in the header of the table
735 foreach ($attributesUnique as $attUnique) {
736 $table .= "|".$attUnique;
737 }
738 $table .="\n";
739 $valuesModif = array();
740 foreach ($keys as $string) {
741 $string = trim($string, "'");
742 $string = rtrim($string, ",");
743
744 $array = parsePropertyString($string);
745
746 // Iterate through the array to merge all key to one array
747 $code = '';
748 foreach ($array as $key => $value) {
749 if (is_array($value)) {
750 $code = $key;
751 continue;
752 } else {
753 $array[$code][$key] = $value;
754 unset($array[$key]);
755 }
756 }
757 // check if is array after parsing the string
758 if (!is_array($array)) {
759 return -1;
760 }
761 $field = array_keys($array);
762 if ($field[0] === '') {
763 $field[0] = 'label';
764 }
765 $values = array_values($array)[0];
766
767 // check each field has all properties and add it if missed
768 foreach ($attributesUnique as $attUnique) {
769 if ($attUnique == 'type' && $field[0] === 'label') {
770 $values[$attUnique] = 'varchar(255)';
771 }
772 if (!array_key_exists($attUnique, $values)) {
773 $valuesModif[$attUnique] = '';
774 } else {
775 $valuesModif[$attUnique] = $values[$attUnique];
776 }
777 }
778 $table .= "|*" . $field[0] . "*|";
779 $table .= implode("|", $valuesModif) . "\n";
780 }
781
782 // end table
783 $table .= "|===\n";
784 $table .= "__ end table for object $objectname\n";
785
786 //write in file
787 $writeInFile = dolReplaceInFile($destfile, array('== DATA SPECIFICATIONS' => $table));
788 if ($writeInFile<0) {
789 return -1;
790 }
791 return 1;
792}
793
794
802function deletePropsAndPermsFromDoc($file, $objectname)
803{
804 if (dol_is_file($file)) {
805 $start = "== Table of fields and their properties for object *".ucfirst($objectname)."* : ";
806 $end = "__ end table for object ".ucfirst($objectname);
807
808 $str = file_get_contents($file);
809
810 $search = '/' . preg_quote($start, '/') . '(.*?)' . preg_quote($end, '/') . '/s';
811 $new_contents = preg_replace($search, '', $str);
812 file_put_contents($file, $new_contents);
813
814 //perms If Exist
815 $perms = "|*".strtolower($objectname)."*|";
816 $search_pattern_perms = '/' . preg_quote($perms, '/') . '.*?\n/';
817 $new_contents = preg_replace($search_pattern_perms, '', $new_contents);
818 file_put_contents($file, $new_contents);
819 }
820}
821
822
823
831function getFromFile($file, $start, $end)
832{
833 $i = 1;
834 $keys = array();
835 $lines = file($file);
836 // Search for start and end lines
837 foreach ($lines as $i => $line) {
838 if (strpos($line, $start) !== false) {
839 // Copy lines until the end on array
840 while (($line = $lines[++$i]) !== false) {
841 if (strpos($line, $end) !== false) {
842 break;
843 }
844 $keys[] = $line;
845 }
846 break;
847 }
848 }
849 $content = implode("", $keys);
850 return $content;
851}
852
859function writePermsInAsciiDoc($file, $destfile)
860{
861 global $langs;
862 //search and get all permssion in stirng
863 $start = '/* BEGIN MODULEBUILDER PERMISSIONS */';
864 $end = '/* END MODULEBUILDER PERMISSIONS */';
865 $content = getFromFile($file, $start, $end);
866 if (empty($content)) {
867 return -1;
868 }
869 //prepare table
870 $string = "[options='header',grid=rows,width=60%,caption=Organisation]\n";
871 $string .= "|===\n";
872 // header for table
873 $header = array($langs->trans('Objects'),$langs->trans('Permission'));
874 foreach ($header as $h) {
875 $string .= "|".$h;
876 }
877 $string .= "\n";
878 //content table
879 $array = explode(";", $content);
880 $permissions = array_filter($array);
881 // delete occurrences "$r++" and ID
882 $permissions = str_replace('$r++', 1, $permissions);
883
884 $permsN = array();
885 foreach ($permissions as $i => $element) {
886 if ($element == 1) {
887 unset($permissions[$i]);
888 }
889 if (str_contains($element, '$this->numero')) {
890 unset($permissions[$i]);
891 }
892 if (str_contains($element, '$this->rights[$r][5]')) {
893 unset($permissions[$i]);
894 }
895 }
896 // cleaning the string on each element
897 foreach ($permissions as $key => $element) {
898 $element = str_replace(" '", '', $element);
899 $element = trim($element, "'");
900 $permsN[] = substr($element, strpos($element, "=")+1);
901 }
902 array_pop($permsN);
903
904 // Group permissions by Object and add it to string
905 $final_array = [];
906 $index = 0;
907 while ($index < count($permsN)) {
908 $temp_array = [$permsN[$index], $permsN[$index + 1]];
909 $final_array[] = $temp_array;
910 $index += 2;
911 }
912
913 $result = array();
914 foreach ($final_array as $subarray) {
915 // found object
916 $key = $subarray[1];
917 // add sub array to object
918 $result[$key][] = $subarray;
919 }
920 foreach ($result as $i => $pems) {
921 $string .= "|*".$i."*|";
922 foreach ($pems as $tab) {
923 $string .= $tab[0]." , ";
924 }
925 $string .= "\n";
926 }
927 // end table
928 $string .= "\n|===\n";
929 $write = dolReplaceInFile($destfile, array('__DATA_PERMISSIONS__'=> $string));
930 if ($write<0) {
931 return -1;
932 }
933 return 1;
934}
935
943function addObjectsToApiFile($file, $objects, $modulename)
944{
945 if (!file_exists($file)) {
946 return -1;
947 }
948 $content = file($file);
949 $includeClass = "dol_include_once('/mymodule/class/myobject.class.php');";
950 $props = "public \$myobject;";
951 $varcomented = "@var MyObject \$myobject {@type MyObject}";
952 $constructObj = "\$this->myobject = new MyObject(\$this->db);";
953
954 // add properties and declare them in consturctor
955 foreach ($content as $lineNumber => &$lineContent) {
956 if (strpos($lineContent, $varcomented) !== false) {
957 $lineContent = '';
958 foreach ($objects as $object) {
959 $lineContent .= "\t * @var ".$object." \$".strtolower($object)." {@type ".$object."}". PHP_EOL;
960 }
961 //var_dump($lineContent);exit;
962 }
963 if (strpos($lineContent, $props) !== false) {
964 $lineContent = '';
965 foreach ($objects as $object) {
966 $lineContent .= "\tpublic \$".strtolower($object).";". PHP_EOL;
967 }
968 }
969 if (strpos($lineContent, $constructObj) !== false) {
970 $lineContent = '';
971 foreach ($objects as $object) {
972 $lineContent .= "\t\t\$this->".strtolower($object)." = new ".$object."(\$this->db);". PHP_EOL;
973 }
974 }
975 if (strpos($lineContent, $includeClass) !== false) {
976 $lineContent = '';
977 foreach ($objects as $object) {
978 $lineContent .= "dol_include_once('/".strtolower($modulename)."/class/".strtolower($object).".class.php');". PHP_EOL;
979 }
980 }
981 }
982 $allContent = implode("", $content);
983 file_put_contents($file, $allContent);
984
985 //add methods for each object
986 $allContent = getFromFile($file, '/*begin methods CRUD*/', '/*end methods CRUD*/');
987 foreach ($objects as $object) {
988 $contentReplaced =str_replace(["myobject","MyObject"], [strtolower($object),$object], $allContent);
989 dolReplaceInFile($file, array('/*end methods CRUD*/' => '/*CRUD FOR '.strtoupper($object).'*/'."\n".$contentReplaced."\n\t".'/*END CRUD FOR '.strtoupper($object).'*/'."\n\t".'/*end methods CRUD*/'));
990 }
991 dolReplaceInFile($file, array($allContent => '','MyModule' => ucfirst($modulename)));
992 return 1;
993}
994
1002function removeObjectFromApiFile($file, $objectname, $modulename)
1003{
1004 $begin = '/*CRUD FOR '.strtoupper($objectname).'*/';
1005 $end = '/*END CRUD FOR '.strtoupper($objectname).'*/';
1006 $includeClass = "dol_include_once('/".strtolower($modulename)."/class/".strtolower($objectname).".class.php');";
1007 $varcomentedDel = "\t * @var ".$objectname." \$".strtolower($objectname)." {@type ".$objectname."}";
1008 $propsDel = "\tpublic \$".strtolower($objectname).";";
1009 $constructObjDel = "\t\t\$this->".strtolower($objectname)." = new ".$objectname."(\$this->db);";
1010
1011 if (!file_exists($file)) {
1012 return -1;
1013 }
1014 $content = file($file);
1015 // for delete property and the initialization from the construct
1016 foreach ($content as $lineNumber => &$lineContent) {
1017 if (strpos($lineContent, $includeClass) !== false) {
1018 $lineContent = '';
1019 }
1020 if (strpos($lineContent, $varcomentedDel) !== false) {
1021 $lineContent = '';
1022 }
1023 if (strpos($lineContent, $propsDel) !== false) {
1024 $lineContent = '';
1025 }
1026 if (strpos($lineContent, $constructObjDel) !== false) {
1027 $lineContent = '';
1028 }
1029 }
1030 $allContent = implode("", $content);
1031 file_put_contents($file, $allContent);
1032 // for delete methods of object
1033 $allContent = getFromFile($file, $begin, $end);
1034 $check = dolReplaceInFile($file, array($allContent => ''));
1035 if ($check) {
1036 dolReplaceInFile($file, array($begin => '', $end => ''));
1037 }
1038 return 1;
1039}
1040
1041
1050function reWriteAllMenus($file, $menus, $menuWantTo, $key, $action)
1051{
1052 $errors =0;
1053 $counter = 0;
1054 if (!file_exists($file)) {
1055 return -1;
1056 }
1057 if ($action == 0 && !empty($key)) {
1058 // delete menu manuelly
1059 array_splice($menus, array_search($menus[$key], $menus), 1);
1060 } elseif ($action == 1) {
1061 // add menu manualy
1062 array_push($menus, $menuWantTo);
1063 } elseif ($action == 2 && !empty($key) && !empty($menuWantTo)) {
1064 // update right from permissions array
1065 $urlCounter=0;
1066 // check if the values already exists
1067 foreach ($menus as $index => $menu) {
1068 if ($index !== $key) {
1069 if ($menu['type'] === $menuWantTo['type']) {
1070 if (strcasecmp(str_replace(' ', '', $menu['titre']), str_replace(' ', '', $menuWantTo['titre'])) === 0) {
1071 $counter++;
1072 }
1073 if (strcasecmp(str_replace(' ', '', $menu['url']), str_replace(' ', '', $menuWantTo['url'])) === 0) {
1074 $urlCounter++;
1075 }
1076 }
1077 }
1078 }
1079 if (!$counter && $urlCounter < 2) {
1080 $menus[$key] = $menuWantTo;
1081 } else {
1082 $errors++;
1083 }
1084 } elseif ($action == -1 && !empty($menuWantTo)) {
1085 // delete menus when delete Object
1086 foreach ($menus as $index => $menu) {
1087 if ((strpos(strtolower($menu['fk_menu']), strtolower($menuWantTo)) !== false) || (strpos(strtolower($menu['leftmenu']), strtolower($menuWantTo)) !== false)) {
1088 array_splice($menus, array_search($menu, $menus), 1);
1089 }
1090 }
1091 } else {
1092 $errors++;
1093 }
1094 if (!$errors) {
1095 // delete All LEFT Menus
1096 $beginMenu = '/* BEGIN MODULEBUILDER LEFTMENU MYOBJECT */';
1097 $endMenu = '/* END MODULEBUILDER LEFTMENU MYOBJECT */';
1098 $allMenus = getFromFile($file, $beginMenu, $endMenu);
1099 dolReplaceInFile($file, array($allMenus => ''));
1100
1101 //prepare each menu and stock them in string
1102 $str_menu = "";
1103 foreach ($menus as $index => $menu) {
1104 $menu['position'] = "1000 + \$r";
1105 if ($menu['type'] === 'left') {
1106 $start = "\t\t".'/* LEFTMENU '.strtoupper($menu['titre']).' */';
1107 $end = "\t\t".'/* END LEFTMENU '.strtoupper($menu['titre']).' */';
1108
1109 $val_actuel = $menu;
1110 $next_val = empty($menus[$index + 1]) ? null : $menus[$index + 1];
1111 //var_dump(dol_escape_php($menu['perms'], 1)); exit;
1112
1113 $str_menu .= $start."\n";
1114 $str_menu.= "\t\t\$this->menu[\$r++]=array(\n";
1115 $str_menu.= "\t\t\t 'fk_menu' => '".dol_escape_php($menu['fk_menu'], 1)."',\n";
1116 $str_menu.= "\t\t\t 'type' => '".dol_escape_php($menu['type'], 1)."',\n";
1117 $str_menu.= "\t\t\t 'titre' => '".dol_escape_php($menu['titre'], 1)."',\n";
1118 $str_menu.= "\t\t\t 'mainmenu' => '".dol_escape_php($menu['mainmenu'], 1)."',\n";
1119 $str_menu.= "\t\t\t 'leftmenu' => '".dol_escape_php($menu['leftmenu'], 1)."',\n";
1120 $str_menu.= "\t\t\t 'url' => '".dol_escape_php($menu['url'], 1)."',\n";
1121 $str_menu.= "\t\t\t 'langs' => '".dol_escape_php($menu['langs'], 1)."',\n";
1122 $str_menu.= "\t\t\t 'position' => ".((int) $menu['position']).",\n";
1123 $str_menu.= "\t\t\t 'enabled' => '".dol_escape_php($menu['enabled'], 1)."',\n";
1124 $str_menu.= "\t\t\t 'perms' => '".dol_escape_php($menu['perms'], 1)."',\n";
1125 $str_menu.= "\t\t\t 'target' => '".dol_escape_php($menu['target'], 1)."',\n";
1126 $str_menu.= "\t\t\t 'user' => ".((int) $menu['user']).",\n";
1127 $str_menu.= "\t\t);\n";
1128
1129 if (is_null($next_val) || $val_actuel['leftmenu'] !== $next_val['leftmenu']) {
1130 $str_menu .= $end."\n";
1131 }
1132 }
1133 }
1134
1135 dolReplaceInFile($file, array($beginMenu => $beginMenu."\n".$str_menu."\n"));
1136 return 1;
1137 }
1138 return -1;
1139}
1140
1149function updateDictionaryInFile($module, $file, $dicts)
1150{
1151 $isEmpty = false;
1152 $dicData = "\t\t\$this->dictionaries=array(\n";
1153 $module = strtolower($module);
1154 foreach ($dicts as $key => $value) {
1155 if (empty($value)) {
1156 $isEmpty = true;
1157 $dicData = "\t\t\$this->dictionaries=array();";
1158 break;
1159 }
1160
1161 $dicData .= "\t\t\t'$key'=>";
1162
1163 if ($key === 'tabcond') {
1164 $conditions = array_map(function ($val) use ($module) {
1165 return ($val === true || $val === false) ? "isModEnabled('$module')" : $val;
1166 }, $value);
1167 $dicData .= "array(" . implode(",", $conditions) . ")";
1168 } elseif ($key === 'tabhelp') {
1169 $helpItems = array();
1170 foreach ($value as $helpValue) {
1171 $helpItems[] = "array('code'=>\$langs->trans('".$helpValue['code']."'), 'field2' => 'field2tooltip')";
1172 }
1173 $dicData .= "array(" . implode(",", $helpItems) . ")";
1174 } else {
1175 if (is_array($value)) {
1176 $dicData .= "array(" . implode(",", array_map(function ($val) {
1177 return "'$val'";
1178 }, $value)) . ")";
1179 } else {
1180 $dicData .= "'$value'";
1181 }
1182 }
1183 $dicData .= ",\n";
1184 }
1185 $dicData .= (!$isEmpty ? "\t\t);" : '');
1186
1187 $stringDic = getFromFile($file, '/* BEGIN MODULEBUILDER DICTIONARIES */', '/* END MODULEBUILDER DICTIONARIES */');
1188 $writeInfile = dolReplaceInFile($file, array($stringDic => $dicData."\n"));
1189
1190 return $writeInfile;
1191}
1192
1205function createNewDictionnary($modulename, $file, $namedic, $dictionnaires = null)
1206{
1207 global $db, $langs;
1208
1209 if (empty($namedic)) {
1210 setEventMessages($langs->trans("ErrorEmptyNameDic"), null, 'errors');
1211 return;
1212 }
1213 if (!file_exists($file)) {
1214 return -1;
1215 }
1216 $modulename = strtolower($modulename);
1217
1218 if (empty($dictionnaires)) {
1219 $dictionnaires = array('langs' => '', 'tabname' => array(), 'tablib' => array(), 'tabsql' => array(), 'tabsqlsort' => array(), 'tabfield' => array(), 'tabfieldvalue' => array(), 'tabfieldinsert' => array(), 'tabrowid' => array(), 'tabcond' => array(), 'tabhelp' => array());
1220 }
1221
1222 $columns = array(
1223 'rowid' => array('type' => 'integer(11)'),
1224 'code' => array('type' => 'varchar(255) NOT NULL'),
1225 'label' => array('type' => 'varchar(255) NOT NULL'),
1226 'position' => array('type' => 'integer(11) NULL'),
1227 'use_default' => array('type' => 'varchar(255) DEFAULT 1'),
1228 'active' => array('type' => 'integer')
1229 );
1230
1231
1232 $primaryKey = 'rowid';
1233 foreach ($columns as $key => $value) {
1234 if ($key === 'rowid') {
1235 $primaryKey = 'rowid';
1236 break;
1237 }
1238 if (!array_key_exists('rowid', $columns)) {
1239 $primaryKey = array_key_first($columns);
1240 break;
1241 }
1242 }
1243 // check if tablename exist in Database and create it if not
1244 $query = "SHOW TABLES LIKE '" . MAIN_DB_PREFIX.strtolower($namedic) . "'";
1245 $checkTable = $db->query($query);
1246 if ($checkTable && $db->num_rows($checkTable) > 0) {
1247 setEventMessages($langs->trans("ErrorTableExist", $namedic), null, 'errors');
1248 return;
1249 } else {
1250 $_results = $db->DDLCreateTable(MAIN_DB_PREFIX.strtolower($namedic), $columns, $primaryKey, "InnoDB");
1251 if ($_results < 0) {
1252 dol_print_error($db);
1253 $langs->load("errors");
1254 setEventMessages($langs->trans("ErrorTableNotFound", $namedic), null, 'errors');
1255 }
1256 }
1257
1258 // rewrite dictionnary if
1259 $dictionnaires['langs'] = $modulename.'@'.$modulename;
1260 $dictionnaires['tabname'][] = strtolower($namedic);
1261 $dictionnaires['tablib'][] = ucfirst(substr($namedic, 2));
1262 $dictionnaires['tabsql'][] = 'SELECT t.rowid as rowid, t.code, t.label, t.active FROM '.MAIN_DB_PREFIX.strtolower($namedic).' as t';
1263 $dictionnaires['tabsqlsort'][] = (array_key_exists('label', $columns) ? 'label ASC' : '');
1264 $dictionnaires['tabfield'][] = (array_key_exists('code', $columns) && array_key_exists('label', $columns) ? 'code,label' : '');
1265 $dictionnaires['tabfieldvalue'][] = (array_key_exists('code', $columns) && array_key_exists('label', $columns) ? 'code,label' : '');
1266 $dictionnaires['tabfieldinsert'][] = (array_key_exists('code', $columns) && array_key_exists('label', $columns) ? 'code,label' : '');
1267 $dictionnaires['tabrowid'][] = $primaryKey;
1268 $dictionnaires['tabcond'][] = isModEnabled('$modulename');
1269 $dictionnaires['tabhelp'][] = (array_key_exists('code', $columns) ? array('code'=>$langs->trans('CodeTooltipHelp'), 'field2' => 'field2tooltip') : '');
1270
1271 // Build the dictionary string
1272 $writeInfile = updateDictionaryInFile($modulename, $file, $dictionnaires);
1273 if ($writeInfile > 0) {
1274 setEventMessages($langs->trans("DictionariesCreated", ucfirst(substr($namedic, 2))), null);
1275 }
1276}
1277
1285function writeApiUrlsInDoc($file_api, $file_doc)
1286{
1287 $error = 0;
1288 if (!dol_is_file($file_api) || !dol_is_file($file_doc)) {
1289 $error++;
1290 }
1291 $string = getFromFile($file_api, '/*begin methods CRUD*/', '/*end methods CRUD*/');
1292 $extractUrls = explode("\n", $string);
1293
1294 // extract urls from file
1295 $urlValues = [];
1296 foreach ($extractUrls as $key => $line) {
1297 $lineWithoutTabsSpaces = preg_replace('/^[\t\s]+/', '', $line);
1298 if (strpos($lineWithoutTabsSpaces, '* @url') === 0) {
1299 $urlValue = trim(substr($lineWithoutTabsSpaces, strlen('* @url')));
1300 $urlValues[] = $urlValue;
1301 }
1302 }
1303
1304 // get urls by object
1305 $str = $_SERVER['HTTP_HOST'].'/api/index.php/';
1306 $groupedUrls = [];
1307 foreach ($urlValues as $url) {
1308 if (preg_match('/(?:GET|POST|PUT|DELETE) (\w+)s/', $url, $matches)) {
1309 $objectName = $matches[1];
1310 $url = $str.trim(strstr($url, ' '));
1311 $groupedUrls[$objectName][] = $url;
1312 }
1313 }
1314 if (empty($groupedUrls)) {
1315 $error++;
1316 }
1317
1318 // buil format asciidoc for urls in table
1319 if (!$error) {
1320 $asciiDocTable = "[options=\"header\"]\n|===\n|Objet | URLs\n";
1321 foreach ($groupedUrls as $objectName => $urls) {
1322 $urlsList = implode(" +\n*", $urls);
1323 $asciiDocTable .= "|$objectName | \n*$urlsList +\n";
1324 }
1325 $asciiDocTable .= "|===\n";
1326 $file_write = dolReplaceInFile($file_doc, array('__API_DOC__' => '__API_DOC__'."\n".$asciiDocTable));
1327 if ($file_write < 0) {
1328 return -1;
1329 }
1330 return 1;
1331 }
1332 return -1;
1333}
1334
1335
1342function countItemsInDirectory($path, $type = 1)
1343{
1344 if (!is_dir($path)) {
1345 return false;
1346 }
1347
1348 $allFilesAndDirs = scandir($path);
1349 $count = 0;
1350
1351 foreach ($allFilesAndDirs as $item) {
1352 if ($item != '.' && $item != '..') {
1353 if ($type == 1 && is_file($path . DIRECTORY_SEPARATOR . $item) && strpos($item, '.back') === false) {
1354 $count++;
1355 } elseif ($type == 2 && is_dir($path . DIRECTORY_SEPARATOR . $item)) {
1356 $count++;
1357 }
1358 }
1359 }
1360 return $count;
1361}
dol_is_file($pathoffile)
Return if path is a file.
dolReplaceInFile($srcfile, $arrayreplacement, $destfile='', $newmask=0, $indexdatabase=0, $arrayreplacementisregex=0)
Make replacement of strings into a file.
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1, $testvirus=0, $indexdatabase=0)
Copy a file to another file.
dol_dir_list($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.
Definition files.lib.php:62
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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 second index function, which produces ascending (default) or descending output...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
updateDictionaryInFile($module, $file, $dicts)
Updates a dictionary in a module descriptor file.
deletePropsAndPermsFromDoc($file, $objectname)
Delete property and permissions from documentation ascii file if we delete an object.
createNewDictionnary($modulename, $file, $namedic, $dictionnaires=null)
Creates a new dictionary table.
countItemsInDirectory($path, $type=1)
count directories or files in modulebuilder folder
writePermsInAsciiDoc($file, $destfile)
Write all permissions of each object in AsciiDoc format.
reWriteAllMenus($file, $menus, $menuWantTo, $key, $action)
parsePropertyString($string)
Converts a formatted properties string into an associative array.
rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='', $object=null, $moduletype='external')
Save data into a memory area shared by all users, all sessions on server.
writeApiUrlsInDoc($file_api, $file_doc)
Generate Urls and add them to documentaion module.
addObjectsToApiFile($file, $objects, $modulename)
Add Object in ModuleApi File.
dolGetListOfObjectClasses($destdir)
Get list of existing objects from directory.
deletePerms($file)
Delete all permissions.
writePropsInAsciiDoc($file, $objectname, $destfile)
Write all properties of the object in AsciiDoc format.
compareFirstValue($a, $b)
Compare two value.
removeObjectFromApiFile($file, $objectname, $modulename)
Remove Object variables and methods from API_Module File.
rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir='', $addfieldentry=array(), $delfieldentry='')
Regenerate files .class.php.
getFromFile($file, $start, $end)
Search a string and return all lines needed from file.
reWriteAllPermissions($file, $permissions, $key, $right, $objectname, $module, $action)
Rewriting all permissions after any actions.
checkExistComment($file, $number)
function for check if comment begin an end exist in modMyModule class