dolibarr  7.0.0-beta
modulebuilder.lib.php
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 <http://www.gnu.org/licenses/>.
16  * or see http://www.gnu.org/
17  */
18 
40 function rebuildObjectClass($destdir, $module, $objectname, $newmask, $readdir='', $addfieldentry=array() ,$delfieldentry='')
41 {
42  global $db, $langs;
43 
44  if (empty($objectname)) return -1;
45  if (empty($readdir)) $readdir=$destdir;
46 
47  if (! empty($addfieldentry['arrayofkeyval']) && ! is_array($addfieldentry['arrayofkeyval']))
48  {
49  dol_print_error('', 'Bad parameter addfieldentry with a property arrayofkeyval defined but that is not an array.');
50  return -1;
51  }
52 
53  // Check parameters
54  if (count($addfieldentry) > 0)
55  {
56  if (empty($addfieldentry['name']))
57  {
58  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Name")), null, 'errors');
59  return -2;
60  }
61  if (empty($addfieldentry['label']))
62  {
63  setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Label")), null, 'errors');
64  return -2;
65  }
66  if (! preg_match('/^(integer|date|timestamp|varchar|double)/', $addfieldentry['type']))
67  {
68  setEventMessages($langs->trans('BadFormatForType', $objectname), 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  {
77  $langs->load("errors");
78  setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors');
79  return -3;
80  }
81 
82  //$pathoffiletoedittmp=$destdir.'/class/'.strtolower($objectname).'.class.php.tmp';
83  //dol_delete_file($pathoffiletoedittmp, 0, 1, 1);
84 
85  try
86  {
87  include_once $pathoffiletoeditsrc;
88  if (class_exists($objectname)) $object=new $objectname($db);
89  else return -4;
90 
91  // Backup old file
92  dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.'.back', $newmask, 1);
93 
94  // Edit class files
95  $contentclass = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
96 
97  // Update ->fields (add or remove entries)
98  if (count($object->fields))
99  {
100  if (is_array($addfieldentry) && count($addfieldentry))
101  {
102  $name=$addfieldentry['name'];
103  unset($addfieldentry['name']);
104 
105  $object->fields[$name]=$addfieldentry;
106  }
107  if (! empty($delfieldentry))
108  {
109  $name=$delfieldentry;
110  unset($object->fields[$name]);
111  }
112  }
113 
114  dol_sort_array($object->fields, 'position');
115 
116  $i=0;
117  $texttoinsert = '// BEGIN MODULEBUILDER PROPERTIES'."\n";
118  $texttoinsert.= "\t".''."\n";
121  $texttoinsert.= "\t".'public $fields=array('."\n";
122 
123  if (count($object->fields))
124  {
125 
126  foreach($object->fields as $key => $val)
127  {
128  $i++;
129  $texttoinsert.= "\t\t'".$key."' => array('type'=>'".$val['type']."', 'label'=>'".$val['label']."',";
130  $texttoinsert.= " 'visible'=>".($val['visible']!=''?$val['visible']:-1).",";
131  $texttoinsert.= " 'enabled'=>".($val['enabled']!=''?$val['enabled']:1).",";
132  $texttoinsert.= " 'position'=>".($val['position']!=''?$val['position']:50).",";
133  $texttoinsert.= " 'notnull'=>".($val['notnull']!=''?$val['notnull']:-1).",";
134  if ($val['index']) $texttoinsert.= " 'index'=>".$val['index'].",";
135  if ($val['searchall']) $texttoinsert.= " 'searchall'=>".$val['searchall'].",";
136  if ($val['comment']) $texttoinsert.= " 'comment'=>\"".preg_replace('/"/', '', $val['comment'])."\","; // addslashes is escape for PHP
137  if ($val['isameasure']) $texttoinsert.= " 'isameasure'=>'".$val['isameasure']."',";
138  if ($val['help']) $texttoinsert.= " 'help'=>\"".preg_replace('/"/', '', $val['help'])."\","; // addslashes is escape for PHP
139  if ($val['arrayofkeyval'])
140  {
141  $texttoinsert.= " 'arrayofkeyval'=>array(";
142  $i=0;
143  foreach($val['arrayofkeyval'] as $key2 => $val2)
144  {
145  if ($i) $texttoinsert.=", ";
146  $texttoinsert.="'".$key2."'=>'".$val2."'";
147  $i++;
148  }
149  $texttoinsert.= ")";
150  }
151  $texttoinsert.= "),\n";
152  }
153  }
154 
155  $texttoinsert.= "\t".');'."\n";
156  //print ($texttoinsert);exit;
157 
158  if (count($object->fields))
159  {
160  $typetotypephp=array('integer'=>'integer', 'varchar'=>'string');
161 
162  foreach($object->fields as $key => $val)
163  {
164  $i++;
165  //$typephp=$typetotypephp[$val['type']];
166  $texttoinsert.= "\t".'public $'.$key.";";
167  //if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY';
168  //if ($key == 'entity') $texttoinsert.= ' DEFAULT 1';
169  //$texttoinsert.= ($val['notnull']?' NOT NULL':'');
170  //if ($i < count($object->fields)) $texttoinsert.=";";
171  $texttoinsert.= "\n";
172  }
173  }
174 
175  $texttoinsert.= "\t".'// END MODULEBUILDER PROPERTIES';
176 
177  //print($texttoinsert);exit;
178 
179  $contentclass = preg_replace('/\/\/ BEGIN MODULEBUILDER PROPERTIES.*END MODULEBUILDER PROPERTIES/ims', $texttoinsert, $contentclass);
180 
181  dol_mkdir(dirname($pathoffiletoedittarget));
182 
183  //file_put_contents($pathoffiletoedittmp, $contentclass);
184  file_put_contents(dol_osencode($pathoffiletoedittarget), $contentclass);
185  @chmod($pathoffiletoedittarget, octdec($newmask));
186 
187  return $object;
188  }
189  catch(Exception $e)
190  {
191  print $e->getMessage();
192  return -5;
193  }
194 }
195 
208 function rebuildObjectSql($destdir, $module, $objectname, $newmask, $readdir='', $object=null)
209 {
210  global $db, $langs;
211 
212  $error = 0;
213 
214  if (empty($objectname)) return -1;
215  if (empty($readdir)) $readdir=$destdir;
216 
217  $pathoffiletoclasssrc=$readdir.'/class/'.strtolower($objectname).'.class.php';
218 
219  // Edit .sql file
220  $pathoffiletoeditsrc=$readdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql';
221  $pathoffiletoedittarget=$destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.sql'.($readdir != $destdir ? '.new' : '');
222  if (! dol_is_file($pathoffiletoeditsrc))
223  {
224  $langs->load("errors");
225  setEventMessages($langs->trans("ErrorFileNotFound", $pathoffiletoeditsrc), null, 'errors');
226  return -1;
227  }
228 
229  // Load object from myobject.class.php
230  try
231  {
232  if (! is_object($object))
233  {
234  include_once $pathoffiletoclasssrc;
235  if (class_exists($objectname)) $object=new $objectname($db);
236  else return -1;
237  }
238  }
239  catch(Exception $e)
240  {
241  print $e->getMessage();
242  }
243 
244  // Backup old file
245  dol_copy($pathoffiletoedittarget, $pathoffiletoedittarget.'.back', $newmask, 1);
246 
247  $contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
248 
249  $i=0;
250  $texttoinsert = '-- BEGIN MODULEBUILDER FIELDS'."\n";
251  if (count($object->fields))
252  {
253  foreach($object->fields as $key => $val)
254  {
255  $i++;
256 
257  $type = $val['type'];
258  $type = preg_replace('/:.*$/', '', $type); // For case type = 'integer:Societe:societe/class/societe.class.php'
259  if ($type == 'html') $type = 'text'; // html modulebuilder type is a text type in database
260  $texttoinsert.= "\t".$key." ".$type;
261  if ($key == 'rowid') $texttoinsert.= ' AUTO_INCREMENT PRIMARY KEY';
262  if ($key == 'entity') $texttoinsert.= ' DEFAULT 1';
263  else
264  {
265  if ($val['default'] != '')
266  {
267  if (preg_match('/^null$/i', $val['default'])) $texttoinsert.= " DEFAULT NULL";
268  else if (preg_match('/varchar/', $val['type'])) $texttoinsert.= " DEFAULT '".$db->escape($val['default'])."'";
269  else $texttoinsert.= (($val['default'] > 0)?' DEFAULT '.$val['default']:'');
270  }
271  }
272  $texttoinsert.= (($val['notnull'] > 0)?' NOT NULL':'');
273  if ($i < count($object->fields)) $texttoinsert.=", ";
274  $texttoinsert.= "\n";
275  }
276  }
277  $texttoinsert.= "\t".'-- END MODULEBUILDER FIELDS';
278 
279  $contentsql = preg_replace('/-- BEGIN MODULEBUILDER FIELDS.*END MODULEBUILDER FIELDS/ims', $texttoinsert, $contentsql);
280 
281  $result = file_put_contents($pathoffiletoedittarget, $contentsql);
282  if ($result)
283  {
284  @chmod($pathoffiletoedittarget, octdec($newmask));
285  }
286  else
287  {
288  $error++;
289  }
290 
291  // Edit .key.sql file
292  $pathoffiletoeditsrc=$destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.key.sql';
293  $pathoffiletoedittarget=$destdir.'/sql/llx_'.strtolower($module).'_'.strtolower($objectname).'.key.sql'.($readdir != $destdir ? '.new' : '');
294 
295  $contentsql = file_get_contents(dol_osencode($pathoffiletoeditsrc), 'r');
296 
297  $i=0;
298  $texttoinsert = '-- BEGIN MODULEBUILDER INDEXES'."\n";
299  if (count($object->fields))
300  {
301  foreach($object->fields as $key => $val)
302  {
303  $i++;
304  if ($val['index'])
305  {
306  $texttoinsert.= "ALTER TABLE llx_".strtolower($module).'_'.strtolower($objectname)." ADD INDEX idx_".strtolower($module).'_'.strtolower($objectname)."_".$key." (".$key.");";
307  $texttoinsert.= "\n";
308  }
309  }
310  }
311  $texttoinsert.= '-- END MODULEBUILDER INDEXES';
312 
313  $contentsql = preg_replace('/-- BEGIN MODULEBUILDER INDEXES.*END MODULEBUILDER INDEXES/ims', $texttoinsert, $contentsql);
314 
315  dol_mkdir(dirname($pathoffiletoedittarget));
316 
317  $result2 = file_put_contents($pathoffiletoedittarget, $contentsql);
318  if ($result)
319  {
320  @chmod($pathoffiletoedittarget, octdec($newmask));
321  }
322  else
323  {
324  $error++;
325  }
326 
327  return $error ? -1 : 1;
328 }
329 
330 
dol_osencode($str)
Return a string encoded into OS filesystem encoding.
dol_copy($srcfile, $destfile, $newmask=0, $overwriteifexists=1)
Copy a file to another file.
Definition: files.lib.php:631
setEventMessages($mesg, $mesgs, $style='mesgs')
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_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
dol_is_file($pathoffile)
Return if path is a file.
Definition: files.lib.php:427
print
Draft customers invoices.
Definition: index.php:91
dol_mkdir($dir, $dataroot='', $newmask=null)
Creation of a directory (this can create recursive subdir)