Repository Access Links:

This URL has Read-Only access.
Statistics
| Branch: | Revision:

dolibarr-module-template / class / myclass.class.php @ ef37f43f

History | View | Annotate | Download (8.6 kB)

1
<?php
2
/* <one line to give the program's name and a brief idea of what it does.>
3
 * Copyright (C) <year>  <name of author>
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 3 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, see <http://www.gnu.org/licenses/>.
17
 */
18

    
19
/**
20
 * \file    class/myclass.class.php
21
 * \ingroup mymodule
22
 * \brief   Example CRUD (Create/Read/Update/Delete) class.
23
 *
24
 * Put detailed description here.
25
 */
26

    
27
/** Includes */
28
//require_once DOL_DOCUMENT_ROOT."/core/class/commonobject.class.php";
29
//require_once DOL_DOCUMENT_ROOT."/societe/class/societe.class.php";
30
//require_once DOL_DOCUMENT_ROOT."/product/class/product.class.php";
31

    
32
/**
33
 * Put your class' description here
34
 */
35
class MyClass // extends CommonObject
36
{
37

    
38
    /** @var DoliDb Database handler */
39
        private $db;
40
    /** @var string Error code or message */
41
        public $error;
42
    /** @var array Several error codes or messages */
43
        public $errors = array();
44
    /** @var string Id to identify managed object */
45
        //public $element='myelement';
46
    /** @var string Name of table without prefix where object is stored */
47
        //public $table_element='mytable';
48
    /** @var int An example ID */
49
        public $id;
50
    /** @var mixed An example property */
51
        public $prop1;
52
    /** @var mixed An example property */
53
        public $prop2;
54

    
55
        /**
56
         * Constructor
57
         *
58
         * @param DoliDb $db Database handler
59
         */
60
        public function __construct($db)
61
        {
62
                $this->db = $db;
63

    
64
                return 1;
65
        }
66

    
67
        /**
68
         * Create object into database
69
         *
70
         * @param User $user User that create
71
         * @param int $notrigger 0=launch triggers after, 1=disable triggers
72
         * @return int <0 if KO, Id of created object if OK
73
         */
74
        public function create($user, $notrigger = 0)
75
        {
76
                global $conf, $langs;
77
                $error = 0;
78

    
79
                // Clean parameters
80
                if (isset($this->prop1)) {
81
                        $this->prop1 = trim($this->prop1);
82
                }
83
                if (isset($this->prop2)) {
84
                        $this->prop2 = trim($this->prop2);
85
                }
86

    
87
                // Check parameters
88
                // Put here code to add control on parameters values
89
                // Insert request
90
                $sql = "INSERT INTO " . MAIN_DB_PREFIX . "mytable(";
91
                $sql.= " field1,";
92
                $sql.= " field2";
93

    
94
                $sql.= ") VALUES (";
95
                $sql.= " '" . $this->prop1 . "',";
96
                $sql.= " '" . $this->prop2 . "'";
97

    
98
                $sql.= ")";
99

    
100
                $this->db->begin();
101

    
102
                dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
103
                $resql = $this->db->query($sql);
104
                if (! $resql) {
105
                        $error ++;
106
                        $this->errors[] = "Error " . $this->db->lasterror();
107
                }
108

    
109
                if (! $error) {
110
                        $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "mytable");
111

    
112
                        if (! $notrigger) {
113
                                // Uncomment this and change MYOBJECT to your own tag if you
114
                                // want this action call a trigger.
115
                                //// Call triggers
116
                                //include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
117
                                //$interface=new Interfaces($this->db);
118
                                //$result=$interface->run_triggers('MYOBJECT_CREATE',$this,$user,$langs,$conf);
119
                                //if ($result < 0) { $error++; $this->errors=$interface->errors; }
120
                                //// End call triggers
121
                        }
122
                }
123

    
124
                // Commit or rollback
125
                if ($error) {
126
                        foreach ($this->errors as $errmsg) {
127
                                dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR);
128
                                $this->error.=($this->error ? ', ' . $errmsg : $errmsg);
129
                        }
130
                        $this->db->rollback();
131

    
132
                        return -1 * $error;
133
                } else {
134
                        $this->db->commit();
135

    
136
                        return $this->id;
137
                }
138
        }
139

    
140
        /**
141
         * Load object in memory from database
142
         *
143
         * @param int $id Id object
144
         * @return int <0 if KO, >0 if OK
145
         */
146
        public function fetch($id)
147
        {
148
                global $langs;
149
                $sql = "SELECT";
150
                $sql.= " t.rowid,";
151
                $sql.= " t.field1,";
152
                $sql.= " t.field2";
153
                //...
154
                $sql.= " FROM " . MAIN_DB_PREFIX . "mytable as t";
155
                $sql.= " WHERE t.rowid = " . $id;
156

    
157
                dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
158
                $resql = $this->db->query($sql);
159
                if ($resql) {
160
                        if ($this->db->num_rows($resql)) {
161
                                $obj = $this->db->fetch_object($resql);
162

    
163
                                $this->id = $obj->rowid;
164
                                $this->prop1 = $obj->field1;
165
                                $this->prop2 = $obj->field2;
166
                                //...
167
                        }
168
                        $this->db->free($resql);
169

    
170
                        return 1;
171
                } else {
172
                        $this->error = "Error " . $this->db->lasterror();
173
                        dol_syslog(__METHOD__ . " " . $this->error, LOG_ERR);
174

    
175
                        return -1;
176
                }
177
        }
178

    
179
        /**
180
         * Update object into database
181
         *
182
         * @param User $user User that modify
183
         * @param int $notrigger 0=launch triggers after, 1=disable triggers
184
         * @return int <0 if KO, >0 if OK
185
         */
186
        public function update($user = 0, $notrigger = 0)
187
        {
188
                global $conf, $langs;
189
                $error = 0;
190

    
191
                // Clean parameters
192
                if (isset($this->prop1)) {
193
                        $this->prop1 = trim($this->prop1);
194
                }
195
                if (isset($this->prop2)) {
196
                        $this->prop2 = trim($this->prop2);
197
                }
198

    
199
                // Check parameters
200
                // Put here code to add control on parameters values
201
                // Update request
202
                $sql = "UPDATE " . MAIN_DB_PREFIX . "mytable SET";
203
                $sql.= " field1=" . (isset($this->field1) ? "'" . $this->db->escape($this->field1) . "'" : "null") . ",";
204
                $sql.= " field2=" . (isset($this->field2) ? "'" . $this->db->escape($this->field2) . "'" : "null") . "";
205

    
206
                $sql.= " WHERE rowid=" . $this->id;
207

    
208
                $this->db->begin();
209

    
210
                dol_syslog(__METHOD__ . " sql=" . $sql, LOG_DEBUG);
211
                $resql = $this->db->query($sql);
212
                if (! $resql) {
213
                        $error ++;
214
                        $this->errors[] = "Error " . $this->db->lasterror();
215
                }
216

    
217
                if (! $error) {
218
                        if (! $notrigger) {
219
                                // Uncomment this and change MYOBJECT to your own tag if you
220
                                // want this action call a trigger.
221
                                //// Call triggers
222
                                //include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
223
                                //$interface=new Interfaces($this->db);
224
                                //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
225
                                //if ($result < 0) { $error++; $this->errors=$interface->errors; }
226
                                //// End call triggers
227
                        }
228
                }
229

    
230
                // Commit or rollback
231
                if ($error) {
232
                        foreach ($this->errors as $errmsg) {
233
                                dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR);
234
                                $this->error.=($this->error ? ', ' . $errmsg : $errmsg);
235
                        }
236
                        $this->db->rollback();
237

    
238
                        return -1 * $error;
239
                } else {
240
                        $this->db->commit();
241

    
242
                        return 1;
243
                }
244
        }
245

    
246
        /**
247
         * Delete object in database
248
         *
249
         * @param User $user User that delete
250
         * @param int $notrigger 0=launch triggers after, 1=disable triggers
251
         * @return int <0 if KO, >0 if OK
252
         */
253
        public function delete($user, $notrigger = 0)
254
        {
255
                global $conf, $langs;
256
                $error = 0;
257

    
258
                $this->db->begin();
259

    
260
                if (! $error) {
261
                        if (! $notrigger) {
262
                                // Uncomment this and change MYOBJECT to your own tag if you
263
                                // want this action call a trigger.
264
                                //// Call triggers
265
                                //include_once DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php";
266
                                //$interface=new Interfaces($this->db);
267
                                //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
268
                                //if ($result < 0) { $error++; $this->errors=$interface->errors; }
269
                                //// End call triggers
270
                        }
271
                }
272

    
273
                if (! $error) {
274
                        $sql = "DELETE FROM " . MAIN_DB_PREFIX . "mytable";
275
                        $sql.= " WHERE rowid=" . $this->id;
276

    
277
                        dol_syslog(__METHOD__ . " sql=" . $sql);
278
                        $resql = $this->db->query($sql);
279
                        if (! $resql) {
280
                                $error ++;
281
                                $this->errors[] = "Error " . $this->db->lasterror();
282
                        }
283
                }
284

    
285
                // Commit or rollback
286
                if ($error) {
287
                        foreach ($this->errors as $errmsg) {
288
                                dol_syslog(__METHOD__ . " " . $errmsg, LOG_ERR);
289
                                $this->error.=($this->error ? ', ' . $errmsg : $errmsg);
290
                        }
291
                        $this->db->rollback();
292

    
293
                        return -1 * $error;
294
                } else {
295
                        $this->db->commit();
296

    
297
                        return 1;
298
                }
299
        }
300

    
301
        /**
302
         * Load an object from its id and create a new one in database
303
         *
304
         * @param int $fromid Id of object to clone
305
         * @return int New id of clone
306
         */
307
        public function createFromClone($fromid)
308
        {
309
                global $user, $langs;
310

    
311
                $error = 0;
312

    
313
                $object = new SkeletonClass($this->db);
314

    
315
                $this->db->begin();
316

    
317
                // Load source object
318
                $object->fetch($fromid);
319
                $object->id = 0;
320
                $object->statut = 0;
321

    
322
                // Clear fields
323
                // ...
324
                // Create clone
325
                $result = $object->create($user);
326

    
327
                // Other options
328
                if ($result < 0) {
329
                        $this->error = $object->error;
330
                        $error ++;
331
                }
332

    
333
                if (! $error) {
334
                        // Do something
335
                }
336

    
337
                // End
338
                if (! $error) {
339
                        $this->db->commit();
340

    
341
                        return $object->id;
342
                } else {
343
                        $this->db->rollback();
344

    
345
                        return -1;
346
                }
347
        }
348

    
349
        /**
350
         * Initialise object with example values
351
         * Id must be 0 if object instance is a specimen
352
         *
353
         * @return void
354
         */
355
        public function initAsSpecimen()
356
        {
357
                $this->id = 0;
358
                $this->prop1 = 'prop1';
359
                $this->prop2 = 'prop2';
360
        }
361
}