dolibarr  17.0.3
cstate.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2016 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  */
17 
27 class Cstate // extends CommonObject
28 {
32  public $db;
33 
37  public $error = '';
38 
42  public $errors = array();
43 
44  //var $element='cstate'; //!< Id that identify managed objects
45  //var $table_element='cstate'; //!< Name of table without prefix where object is stored
46 
50  public $id;
51 
52  public $code_departement;
53  public $code;
54 
58  public $name = '';
59 
65  public $nom = '';
66 
67  public $label;
68 
69  public $active;
70 
71 
72 
73 
79  public function __construct($db)
80  {
81  $this->db = $db;
82  }
83 
84 
92  public function create($user, $notrigger = 0)
93  {
94  $error = 0;
95 
96  // Clean parameters
97  if (isset($this->code_departement)) {
98  $this->code_departement = trim($this->code_departement);
99  }
100  if (isset($this->nom)) {
101  $this->nom = trim($this->nom);
102  }
103  if (isset($this->active)) {
104  $this->active = trim($this->active);
105  }
106 
107  // Check parameters
108  // Put here code to add control on parameters values
109 
110  // Insert request
111  $sql = "INSERT INTO ".$this->db->prefix()."c_departements(";
112  $sql .= "rowid,";
113  $sql .= "code_departement,";
114  $sql .= "nom,";
115  $sql .= "active";
116  $sql .= ") VALUES (";
117  $sql .= " ".(!isset($this->rowid) ? 'NULL' : "'".$this->db->escape($this->rowid)."'").",";
118  $sql .= " ".(!isset($this->code_departement) ? 'NULL' : "'".$this->db->escape($this->code_departement)."'").",";
119  $sql .= " ".(!isset($this->nom) ? 'NULL' : "'".$this->db->escape($this->nom)."'").",";
120  $sql .= " ".(!isset($this->active) ? 'NULL' : "'".$this->db->escape($this->active)."'")."";
121  $sql .= ")";
122 
123  $this->db->begin();
124 
125  dol_syslog(get_class($this)."::create", LOG_DEBUG);
126  $resql = $this->db->query($sql);
127  if (!$resql) {
128  $error++;
129  $this->errors[] = "Error ".$this->db->lasterror();
130  }
131 
132  if (!$error) {
133  $this->id = $this->db->last_insert_id($this->db->prefix()."c_departements");
134  }
135 
136  // Commit or rollback
137  if ($error) {
138  foreach ($this->errors as $errmsg) {
139  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
140  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
141  }
142  $this->db->rollback();
143  return -1 * $error;
144  } else {
145  $this->db->commit();
146  return $this->id;
147  }
148  }
149 
150 
158  public function fetch($id, $code = '')
159  {
160  $sql = "SELECT";
161  $sql .= " t.rowid,";
162  $sql .= " t.code_departement,";
163  $sql .= " t.nom,";
164  $sql .= " t.active";
165  $sql .= " FROM ".$this->db->prefix()."c_departements as t";
166  if ($id) {
167  $sql .= " WHERE t.rowid = ".((int) $id);
168  } elseif ($code) {
169  $sql .= " WHERE t.code_departement = '".$this->db->escape($code)."'";
170  }
171 
172  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
173  $resql = $this->db->query($sql);
174  if ($resql) {
175  if ($this->db->num_rows($resql)) {
176  $obj = $this->db->fetch_object($resql);
177 
178  $this->id = $obj->rowid;
179  $this->code_departement = $obj->code_departement; //deprecated
180  $this->code = $obj->code_departement;
181  $this->nom = $obj->nom; //deprecated
182  $this->name = $obj->nom;
183  $this->active = $obj->active;
184  }
185  $this->db->free($resql);
186 
187  return 1;
188  } else {
189  $this->error = "Error ".$this->db->lasterror();
190  return -1;
191  }
192  }
193 
194 
202  public function update($user = null, $notrigger = 0)
203  {
204  global $conf, $langs;
205  $error = 0;
206 
207  // Clean parameters
208  if (isset($this->code_departement)) {
209  $this->code_departement = trim($this->code_departement);
210  }
211  if (isset($this->nom)) {
212  $this->nom = trim($this->nom);
213  }
214  if (isset($this->active)) {
215  $this->active = trim($this->active);
216  }
217 
218 
219  // Check parameters
220  // Put here code to add control on parameters values
221 
222  // Update request
223  $sql = "UPDATE ".$this->db->prefix()."c_departements SET";
224  $sql .= " code_departement=".(isset($this->code_departement) ? "'".$this->db->escape($this->code_departement)."'" : "null").",";
225  $sql .= " nom=".(isset($this->nom) ? "'".$this->db->escape($this->nom)."'" : "null").",";
226  $sql .= " active=".(isset($this->active) ? $this->active : "null")."";
227  $sql .= " WHERE rowid=".((int) $this->id);
228 
229  $this->db->begin();
230 
231  dol_syslog(get_class($this)."::update", LOG_DEBUG);
232  $resql = $this->db->query($sql);
233  if (!$resql) {
234  $error++;
235  $this->errors[] = "Error ".$this->db->lasterror();
236  }
237 
238  // Commit or rollback
239  if ($error) {
240  foreach ($this->errors as $errmsg) {
241  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
242  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
243  }
244  $this->db->rollback();
245  return -1 * $error;
246  } else {
247  $this->db->commit();
248  return 1;
249  }
250  }
251 
259  public function delete($user, $notrigger = 0)
260  {
261  global $conf, $langs;
262  $error = 0;
263 
264  $sql = "DELETE FROM ".$this->db->prefix()."c_departements";
265  $sql .= " WHERE rowid=".((int) $this->id);
266 
267  $this->db->begin();
268 
269  dol_syslog(get_class($this)."::delete", LOG_DEBUG);
270  $resql = $this->db->query($sql);
271  if (!$resql) {
272  $error++;
273  $this->errors[] = "Error ".$this->db->lasterror();
274  }
275 
276  // Commit or rollback
277  if ($error) {
278  foreach ($this->errors as $errmsg) {
279  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
280  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
281  }
282  $this->db->rollback();
283  return -1 * $error;
284  } else {
285  $this->db->commit();
286  return 1;
287  }
288  }
289 }
Cstate\create
create($user, $notrigger=0)
Create object into database.
Definition: cstate.class.php:92
db
$conf db
API class for accounts.
Definition: inc.php:41
Cstate\fetch
fetch($id, $code='')
Load object in memory from database.
Definition: cstate.class.php:158
Cstate\update
update($user=null, $notrigger=0)
Update object into database.
Definition: cstate.class.php:202
rowid
print *****$script_file(".$version.") pid c cd cd cd description as p label as s rowid
Definition: email_expire_services_to_representatives.php:79
name
$conf db name
Definition: repair.php:122
code
print *****$script_file(".$version.") pid code
! Closing after partial payment: discount_vat, badcustomer or badsupplier, bankcharge,...
Definition: sync_members_ldap2dolibarr.php:60
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1628
Cstate
Class to manage dictionary States (used by imports)
Definition: cstate.class.php:27
Cstate\__construct
__construct($db)
Constructor.
Definition: cstate.class.php:79