38 function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir =
'', $addfieldentry = array(), $delfieldentry =
'')
42 if (empty($objectname)) {
45 if (empty($readdir)) {
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.');
57 if (is_array($addfieldentry) && count($addfieldentry) > 0) {
58 if (empty($addfieldentry[
'name'])) {
59 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Name")),
null,
'errors');
62 if (empty($addfieldentry[
'label'])) {
63 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Label")),
null,
'errors');
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');
73 $pathoffiletoeditsrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
74 $pathoffiletoedittarget = $destdir.
'/class/'.strtolower($objectname).
'.class.php'.($readdir != $destdir ?
'.new' :
'');
76 $langs->load(
"errors");
77 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
85 include_once $pathoffiletoeditsrc;
86 if (class_exists($objectname)) {
87 $object =
new $objectname($db);
93 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
96 $contentclass = file_get_contents(
dol_osencode($pathoffiletoeditsrc),
'r');
99 if (count($object->fields)) {
100 if (is_array($addfieldentry) && count($addfieldentry)) {
101 $name = $addfieldentry[
'name'];
102 unset($addfieldentry[
'name']);
104 $object->fields[$name] = $addfieldentry;
106 if (!empty($delfieldentry)) {
107 $name = $delfieldentry;
108 unset($object->fields[$name]);
115 $texttoinsert =
'// BEGIN MODULEBUILDER PROPERTIES'.
"\n";
116 $texttoinsert .=
"\t".
''.
"\n";
119 $texttoinsert .=
"\t".
'public $fields=array('.
"\n";
121 if (count($object->fields)) {
122 foreach ($object->fields as $key => $val) {
124 $texttoinsert .=
"\t\t'".$key.
"' => array('type'=>'".$val[
'type'].
"',";
125 $texttoinsert .=
" 'label'=>'".$val[
'label'].
"',";
126 if (!empty($val[
'picto'])) {
127 $texttoinsert .=
" 'picto'=>'".$val[
'picto'].
"',";
129 $texttoinsert .=
" 'enabled'=>'".($val[
'enabled'] !==
'' ? $val[
'enabled'] : 1).
"',";
130 $texttoinsert .=
" 'position'=>".($val[
'position'] !==
'' ? $val[
'position'] : 50).
",";
131 $texttoinsert .=
" 'notnull'=>".(empty($val[
'notnull']) ? 0 : $val[
'notnull']).
",";
132 $texttoinsert .=
" 'visible'=>".($val[
'visible'] !==
'' ? $val[
'visible'] : -1).
",";
133 if (!empty($val[
'noteditable'])) {
134 $texttoinsert .=
" 'noteditable'=>'".$val[
'noteditable'].
"',";
136 if (!empty($val[
'alwayseditable'])) {
137 $texttoinsert .=
" 'alwayseditable'=>'".$val[
'alwayseditable'].
"',";
139 if (!empty($val[
'default']) || (isset($val[
'default']) && $val[
'default'] ===
'0')) {
140 $texttoinsert .=
" 'default'=>'".$val[
'default'].
"',";
142 if (!empty($val[
'index'])) {
143 $texttoinsert .=
" 'index'=>".$val[
'index'].
",";
145 if (!empty($val[
'foreignkey'])) {
146 $texttoinsert .=
" 'foreignkey'=>'".$val[
'foreignkey'].
"',";
148 if (!empty($val[
'searchall'])) {
149 $texttoinsert .=
" 'searchall'=>".$val[
'searchall'].
",";
151 if (!empty($val[
'isameasure'])) {
152 $texttoinsert .=
" 'isameasure'=>'".$val[
'isameasure'].
"',";
154 if (!empty($val[
'css'])) {
155 $texttoinsert .=
" 'css'=>'".$val[
'css'].
"',";
157 if (!empty($val[
'cssview'])) {
158 $texttoinsert .=
" 'cssview'=>'".$val[
'cssview'].
"',";
160 if (!empty($val[
'csslist'])) {
161 $texttoinsert .=
" 'csslist'=>'".$val[
'csslist'].
"',";
163 if (!empty($val[
'help'])) {
164 $texttoinsert .=
" 'help'=>\"".preg_replace(
'/"/',
'', $val[
'help']).
"\",";
166 if (!empty($val[
'showoncombobox'])) {
167 $texttoinsert .=
" 'showoncombobox'=>'".$val[
'showoncombobox'].
"',";
169 if (!empty($val[
'disabled'])) {
170 $texttoinsert .=
" 'disabled'=>'".$val[
'disabled'].
"',";
172 if (!empty($val[
'autofocusoncreate'])) {
173 $texttoinsert .=
" 'autofocusoncreate'=>'".$val[
'autofocusoncreate'].
"',";
175 if (!empty($val[
'arrayofkeyval'])) {
176 $texttoinsert .=
" 'arrayofkeyval'=>array(";
178 foreach ($val[
'arrayofkeyval'] as $key2 => $val2) {
180 $texttoinsert .=
", ";
182 $texttoinsert .=
"'".$key2.
"'=>'".$val2.
"'";
185 $texttoinsert .=
"),";
187 if (!empty($val[
'validate'])) {
188 $texttoinsert .=
" 'validate'=>'".$val[
'validate'].
"',";
190 if (!empty($val[
'comment'])) {
191 $texttoinsert .=
" 'comment'=>\"".preg_replace(
'/"/',
'', $val[
'comment']).
"\"";
194 $texttoinsert .=
"),\n";
199 $texttoinsert .=
"\t".
');'.
"\n";
202 if (count($object->fields)) {
205 foreach ($object->fields as $key => $val) {
208 $texttoinsert .=
"\t".
'public $'.$key.
";";
213 $texttoinsert .=
"\n";
217 $texttoinsert .=
"\t".
'// END MODULEBUILDER PROPERTIES';
221 $contentclass = preg_replace(
'/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
224 dol_mkdir(dirname($pathoffiletoedittarget));
227 $result = file_put_contents(
dol_osencode($pathoffiletoedittarget), $contentclass);
230 @chmod($pathoffiletoedittarget, octdec($newmask));
235 return $error ? -1 : $object;
237 print $e->getMessage();
255 function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir =
'', $object =
null, $moduletype =
'external')
261 if (empty($objectname)) {
264 if (empty($readdir)) {
268 $pathoffiletoclasssrc = $readdir.
'/class/'.strtolower($objectname).
'.class.php';
271 if ($moduletype ==
'internal') {
272 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
273 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
274 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
275 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
276 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
277 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
278 $pathoffiletoeditsrc =
'/../install/mysql/tables/llx_'.strtolower($module).
'.sql';
283 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'.sql';
284 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
285 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'_'.strtolower($objectname).
'-'.strtolower($module).
'.sql';
286 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
287 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'-'.strtolower($module).
'.sql';
288 if (!
dol_is_file($readdir.$pathoffiletoeditsrc)) {
289 $pathoffiletoeditsrc =
'/sql/llx_'.strtolower($module).
'.sql';
296 $pathoffiletoedittarget = $destdir.$pathoffiletoeditsrc.($readdir != $destdir ?
'.new' :
'');
297 $pathoffiletoeditsrc = $readdir.$pathoffiletoeditsrc;
300 $langs->load(
"errors");
301 setEventMessages($langs->trans(
"ErrorFileNotFound", $pathoffiletoeditsrc),
null,
'errors');
307 if (!is_object($object)) {
308 include_once $pathoffiletoclasssrc;
309 if (class_exists($objectname)) {
310 $object =
new $objectname($db);
316 print $e->getMessage();
320 dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.
'.back', $newmask, 1);
322 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc),
'r');
325 $texttoinsert =
'-- BEGIN MODULEBUILDER FIELDS'.
"\n";
326 if (count($object->fields)) {
327 foreach ($object->fields as $key => $val) {
330 $type = $val[
'type'];
331 $type = preg_replace(
'/:.*$/',
'', $type);
333 if ($type ==
'html') {
335 } elseif ($type ==
'price') {
337 } elseif (in_array($type, array(
'link',
'sellist',
'duration'))) {
340 $texttoinsert .=
"\t".$key.
" ".$type;
341 if ($key ==
'rowid') {
342 $texttoinsert .=
' AUTO_INCREMENT PRIMARY KEY';
343 } elseif ($type ==
'timestamp') {
344 $texttoinsert .=
' DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP';
346 if ($key ==
'entity') {
347 $texttoinsert .=
' DEFAULT 1';
349 if (!empty($val[
'default'])) {
350 if (preg_match(
'/^null$/i', $val[
'default'])) {
351 $texttoinsert .=
" DEFAULT NULL";
352 } elseif (preg_match(
'/varchar/', $type)) {
353 $texttoinsert .=
" DEFAULT '".$db->escape($val[
'default']).
"'";
355 $texttoinsert .= (($val[
'default'] > 0) ?
' DEFAULT '.$val[
'default'] :
'');
359 $texttoinsert .= ((!empty($val[
'notnull']) && $val[
'notnull'] > 0) ?
' NOT NULL' :
'');
360 if ($i < count($object->fields)) {
361 $texttoinsert .=
", ";
363 $texttoinsert .=
"\n";
366 $texttoinsert .=
"\t".
'-- END MODULEBUILDER FIELDS';
368 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
370 $result = file_put_contents($pathoffiletoedittarget, $contentsql);
372 @chmod($pathoffiletoedittarget, octdec($newmask));
375 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
379 $pathoffiletoeditsrc = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoeditsrc);
380 $pathoffiletoedittarget = preg_replace(
'/\.sql$/',
'.key.sql', $pathoffiletoedittarget);
381 $pathoffiletoedittarget = preg_replace(
'/\.sql.new$/',
'.key.sql.new', $pathoffiletoedittarget);
383 $contentsql = file_get_contents(
dol_osencode($pathoffiletoeditsrc),
'r');
386 $texttoinsert =
'-- BEGIN MODULEBUILDER INDEXES'.
"\n";
387 if (count($object->fields)) {
388 foreach ($object->fields as $key => $val) {
390 if (!empty($val[
'index'])) {
391 $texttoinsert .=
"ALTER TABLE llx_".strtolower($module).
'_'.strtolower($objectname).
" ADD INDEX idx_".strtolower($module).
'_'.strtolower($objectname).
"_".$key.
" (".$key.
");";
392 $texttoinsert .=
"\n";
394 if (!empty($val[
'foreignkey'])) {
395 $tmp = explode(
'.', $val[
'foreignkey']);
396 if (!empty($tmp[0]) && !empty($tmp[1])) {
397 $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].
");";
398 $texttoinsert .=
"\n";
403 $texttoinsert .=
'-- END MODULEBUILDER INDEXES';
405 $contentsql = preg_replace(
'/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
407 dol_mkdir(dirname($pathoffiletoedittarget));
409 $result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
411 @chmod($pathoffiletoedittarget, octdec($newmask));
414 setEventMessages($langs->trans(
"ErrorFailToCreateFile", $pathoffiletoedittarget),
null,
'errors');
417 return $error ? -1 : 1;
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
Copy a file to another file.
dol_is_file($pathoffile)
Return if path is a file.
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...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
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...
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='', $object=null, $moduletype='external')
Save data into a memory area shared by all users, all sessions on server.
rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir='', $addfieldentry=array(), $delfieldentry='')
Regenerate files .class.php.