Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 370 lines (310 sloc) 9.954 kb
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
1 <?php
7ebf31e @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
2 /* Copyright (C) 2007-2008 Laurent Destailleur <eldy@users.sourceforge.net>
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
3 * Copyright (C) ---Put here your own copyright and developer email---
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 /**
2a6ad76 @eldy Add createFromClone function in skeleton
eldy authored
21 * \file dev/skeletons/skeleton_class.class.php
22 * \ingroup mymodule othermodule1 othermodule2
23 * \brief This file is an example for a CRUD class file (Create/Read/Update/Delete)
24 * \version $Id$
25 * \author Put author name here
26 * \remarks Put here some comments
27 */
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
28
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
29 // Put here all includes required by your class file
b68a359 @grandoc according to new tree directory
grandoc authored
30 //require_once(DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php");
671ce8a @grandoc move societe class file to societe directory
grandoc authored
31 //require_once(DOL_DOCUMENT_ROOT."/societe/societe.class.php");
b65ddfa @grandoc according to new tree directory
grandoc authored
32 //require_once(DOL_DOCUMENT_ROOT."/product/class/product.class.php");
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
33
34
35 /**
2a6ad76 @eldy Add createFromClone function in skeleton
eldy authored
36 * \class Skeleton_class
37 * \brief Put here description of your class
38 * \remarks Put here some comments
39 */
da48926 @eldy New: Fin de script build_class_from_table qui cr�e une classe d'acc�s…
eldy authored
40 class Skeleton_class // extends CommonObject
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
41 {
7be3510 @eldy Doc: Doxygen
eldy authored
42 var $db; //!< To store db handler
43 var $error; //!< To return error code (or message)
44 var $errors=array(); //!< To return several error codes (or messages)
45 //var $element='skeleton'; //!< Id that identify managed objects
46 //var $table_element='skeleton'; //!< Name of table without prefix where object is stored
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
47
48 var $id;
49 var $prop1;
50 var $prop2;
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
51 //...
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
52
53
54 /**
55 * \brief Constructor
56 * \param DB Database handler
57 */
58 function Skeleton_class($DB)
59 {
60 $this->db = $DB;
61 return 1;
62 }
63
64
65 /**
66 * \brief Create in database
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
67 * \param user User that create
68 * \param notrigger 0=launch triggers after, 1=disable triggers
69 * \return int <0 if KO, Id of created object if OK
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
70 */
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
71 function create($user, $notrigger=0)
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
72 {
73 global $conf, $langs;
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
74 $error=0;
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
75
76 // Clean parameters
3153f3b @eldy Fix: Code generator also build a php script
eldy authored
77 if (isset($this->prop1)) $this->prop1=trim($this->prop1);
78 if (isset($this->prop2)) $this->prop2=trim($this->prop2);
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
79 //...
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
80
6094903 @eldy New: Fin de script build_class_from_table qui cr�e une classe d'acc�s…
eldy authored
81 // Check parameters
82 // Put here code to add control on parameters values
83
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
84 // Insert request
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
85 $sql = "INSERT INTO ".MAIN_DB_PREFIX."mytable(";
86 $sql.= " field1,";
87 $sql.= " field2";
88 //...
89 $sql.= ") VALUES (";
6094903 @eldy New: Fin de script build_class_from_table qui cr�e une classe d'acc�s…
eldy authored
90 $sql.= " '".$this->prop1."',";
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
91 $sql.= " '".$this->prop2."'";
92 //...
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
93 $sql.= ")";
94
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
95 $this->db->begin();
96
0823322 @eldy Qual: All call to dolibarr_ functions are made on dol_ functions.
eldy authored
97 dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
98 $resql=$this->db->query($sql);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
99 if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
100
101 if (! $error)
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
102 {
103 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."mytable");
104
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
105 if (! $notrigger)
106 {
107 // Uncomment this and change MYOBJECT to your own tag if you
108 // want this action call a trigger.
109
110 //// Call triggers
b6bc922 @grandoc according to new tree directory
grandoc authored
111 //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
112 //$interface=new Interfaces($this->db);
335c038 Fix: regression
Regis Houssin authored
113 //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
114 //if ($result < 0) { $error++; $this->errors=$interface->errors; }
115 //// End call triggers
116 }
117 }
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
118
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
119 // Commit or rollback
120 if ($error)
121 {
122 foreach($this->errors as $errmsg)
123 {
0823322 @eldy Qual: All call to dolibarr_ functions are made on dol_ functions.
eldy authored
124 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
125 $this->error.=($this->error?', '.$errmsg:$errmsg);
126 }
127 $this->db->rollback();
128 return -1*$error;
129 }
130 else
131 {
132 $this->db->commit();
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
133 return $this->id;
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
134 }
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
135 }
136
3153f3b @eldy Fix: Code generator also build a php script
eldy authored
137
138 /**
139 * \brief Load object in memory from database
140 * \param id id object
141 * \return int <0 if KO, >0 if OK
142 */
143 function fetch($id)
144 {
145 global $langs;
146 $sql = "SELECT";
147 $sql.= " t.rowid,";
148 $sql.= " t.field1,";
149 $sql.= " t.field2";
150 //...
151 $sql.= " FROM ".MAIN_DB_PREFIX."mytable as t";
152 $sql.= " WHERE t.rowid = ".$id;
153
0823322 @eldy Qual: All call to dolibarr_ functions are made on dol_ functions.
eldy authored
154 dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
3153f3b @eldy Fix: Code generator also build a php script
eldy authored
155 $resql=$this->db->query($sql);
156 if ($resql)
157 {
158 if ($this->db->num_rows($resql))
159 {
160 $obj = $this->db->fetch_object($resql);
161
162 $this->id = $obj->rowid;
163 $this->prop1 = $obj->field1;
164 $this->prop2 = $obj->field2;
165 //...
166 }
167 $this->db->free($resql);
168
169 return 1;
170 }
171 else
172 {
173 $this->error="Error ".$this->db->lasterror();
0823322 @eldy Qual: All call to dolibarr_ functions are made on dol_ functions.
eldy authored
174 dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
3153f3b @eldy Fix: Code generator also build a php script
eldy authored
175 return -1;
176 }
177 }
178
179
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
180 /**
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
181 * \brief Update database
182 * \param user User that modify
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
183 * \param notrigger 0=launch triggers after, 1=disable triggers
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
184 * \return int <0 if KO, >0 if OK
185 */
186 function update($user=0, $notrigger=0)
187 {
188 global $conf, $langs;
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
189 $error=0;
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
190
191 // Clean parameters
3153f3b @eldy Fix: Code generator also build a php script
eldy authored
192 if (isset($this->prop1)) $this->prop1=trim($this->prop1);
193 if (isset($this->prop2)) $this->prop2=trim($this->prop2);
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
194 //...
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
195
6094903 @eldy New: Fin de script build_class_from_table qui cr�e une classe d'acc�s…
eldy authored
196 // Check parameters
197 // Put here code to add control on parameters values
198
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
199 // Update request
200 $sql = "UPDATE ".MAIN_DB_PREFIX."mytable SET";
3153f3b @eldy Fix: Code generator also build a php script
eldy authored
201 $sql.= " field1=".(isset($this->field1)?"'".addslashes($this->field1)."'":"null").",";
202 $sql.= " field2=".(isset($this->field2)?"'".addslashes($this->field2)."'":"null")."";
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
203 //...
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
204 $sql.= " WHERE rowid=".$this->id;
3c71b2b @eldy New: Ajout d'un debut de script build_class_from_table qui cr�e une c…
eldy authored
205
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
206 $this->db->begin();
207
0823322 @eldy Qual: All call to dolibarr_ functions are made on dol_ functions.
eldy authored
208 dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
209 $resql = $this->db->query($sql);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
210 if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
211
212 if (! $error)
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
213 {
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
214 if (! $notrigger)
215 {
216 // Uncomment this and change MYOBJECT to your own tag if you
217 // want this action call a trigger.
218
219 //// Call triggers
b6bc922 @grandoc according to new tree directory
grandoc authored
220 //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
221 //$interface=new Interfaces($this->db);
335c038 Fix: regression
Regis Houssin authored
222 //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
223 //if ($result < 0) { $error++; $this->errors=$interface->errors; }
224 //// End call triggers
225 }
226 }
227
228 // Commit or rollback
229 if ($error)
230 {
231 foreach($this->errors as $errmsg)
232 {
0823322 @eldy Qual: All call to dolibarr_ functions are made on dol_ functions.
eldy authored
233 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
234 $this->error.=($this->error?', '.$errmsg:$errmsg);
235 }
236 $this->db->rollback();
237 return -1*$error;
238 }
239 else
240 {
241 $this->db->commit();
242 return 1;
243 }
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
244 }
245
246
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
247 /**
2a6ad76 @eldy Add createFromClone function in skeleton
eldy authored
248 * \brief Delete object in database
249 * \param user User that delete
250 * \param notrigger 0=launch triggers after, 1=disable triggers
251 * \return int <0 if KO, >0 if OK
252 */
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
253 function delete($user, $notrigger=0)
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
254 {
255 global $conf, $langs;
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
256 $error=0;
257
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
258 $sql = "DELETE FROM ".MAIN_DB_PREFIX."mytable";
259 $sql.= " WHERE rowid=".$this->id;
260
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
261 $this->db->begin();
262
0823322 @eldy Qual: All call to dolibarr_ functions are made on dol_ functions.
eldy authored
263 dol_syslog(get_class($this)."::delete sql=".$sql);
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
264 $resql = $this->db->query($sql);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
265 if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
266
267 if (! $error)
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
268 {
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
269 if (! $notrigger)
270 {
271 // Uncomment this and change MYOBJECT to your own tag if you
272 // want this action call a trigger.
273
274 //// Call triggers
b6bc922 @grandoc according to new tree directory
grandoc authored
275 //include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php");
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
276 //$interface=new Interfaces($this->db);
335c038 Fix: regression
Regis Houssin authored
277 //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
278 //if ($result < 0) { $error++; $this->errors=$interface->errors; }
279 //// End call triggers
280 }
281 }
282
283 // Commit or rollback
284 if ($error)
285 {
286 foreach($this->errors as $errmsg)
287 {
0823322 @eldy Qual: All call to dolibarr_ functions are made on dol_ functions.
eldy authored
288 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
1a9e1a8 @eldy Fix: Correct errors in code generator
eldy authored
289 $this->error.=($this->error?', '.$errmsg:$errmsg);
290 }
291 $this->db->rollback();
292 return -1*$error;
293 }
294 else
295 {
296 $this->db->commit();
297 return 1;
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
298 }
299 }
300
2a6ad76 @eldy Add createFromClone function in skeleton
eldy authored
301
302
303 /**
304 * \brief Load an object from its id and create a new one in database
305 * \param fromid Id of object to clone
306 * \return int New id of clone
307 */
da779e3 @eldy Add createFromClone function in skeleton
eldy authored
308 function createFromClone($fromid)
2a6ad76 @eldy Add createFromClone function in skeleton
eldy authored
309 {
310 global $user,$langs;
311
312 $error=0;
313
314 $object=new Skeleton_class($this->db);
315
316 $this->db->begin();
317
318 // Load source object
319 $object->fetch($fromid);
320 $object->id=0;
321 $object->statut=0;
322
323 // Clear fields
324 // ...
325
326 // Create clone
327 $result=$object->create($user);
328
329 // Other options
330 if ($result < 0)
331 {
332 $this->error=$object->error;
333 $error++;
334 }
335
336 if (! $error)
337 {
338
339
340
341 }
342
343 // End
344 if (! $error)
345 {
346 $this->db->commit();
347 return $object->id;
348 }
349 else
350 {
351 $this->db->rollback();
352 return -1;
353 }
354 }
355
356
9a0c1b9 @eldy New: Ajout de 3 squelettes de code example:
eldy authored
357 /**
358 * \brief Initialise object with example values
359 * \remarks id must be 0 if object instance is a specimen.
360 */
361 function initAsSpecimen()
362 {
363 $this->id=0;
364 $this->prop1='prop1';
365 $this->prop2='prop2';
366 }
367
368 }
369 ?>
Something went wrong with that request. Please try again.