dolibarr  13.0.2
price_global_variable_updater.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2007-2012 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
4 /* Copyright (C) 2015 Ion Agorria <ion@agorria.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <https://www.gnu.org/licenses/>.
18  */
19 
31 {
35  public $db;
36 
40  public $error = '';
41 
45  public $errors = array();
46 
47  public $types = array(0, 1);
48  public $update_min = 5;
49 
53  public $id;
54 
55  public $type;
56 
60  public $description;
61 
62  public $parameters;
63 
67  public $fk_variable;
68 
70  public $next_update;
71  public $last_status;
72 
76  public $table_element = "c_price_global_variable_updater";
77 
83  public function __construct($db)
84  {
85  $this->db = $db;
86  }
87 
88 
96  public function create($user, $notrigger = 0)
97  {
98  $error = 0;
99 
100  $this->checkParameters();
101 
102  // Insert request
103  $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (";
104  $sql .= "type, description, parameters, fk_variable, update_interval, next_update, last_status";
105  $sql .= ") VALUES (";
106  $sql .= " ".$this->type.",";
107  $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
108  $sql .= " ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
109  $sql .= " ".$this->fk_variable.",";
110  $sql .= " ".$this->update_interval.",";
111  $sql .= " ".$this->next_update.",";
112  $sql .= " ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
113  $sql .= ")";
114 
115  $this->db->begin();
116 
117  dol_syslog(__METHOD__, LOG_DEBUG);
118  $resql = $this->db->query($sql);
119  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
120 
121  if (!$error)
122  {
123  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
124 
125  if (!$notrigger)
126  {
127  // Uncomment this and change MYOBJECT to your own tag if you
128  // want this action calls a trigger.
129 
131  //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
132  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
134  }
135  }
136 
137  // Commit or rollback
138  if ($error)
139  {
140  foreach ($this->errors as $errmsg)
141  {
142  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
143  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
144  }
145  $this->db->rollback();
146  return -1 * $error;
147  } else {
148  $this->db->commit();
149  return $this->id;
150  }
151  }
152 
153 
160  public function fetch($id)
161  {
162  $sql = "SELECT type, description, parameters, fk_variable, update_interval, next_update, last_status";
163  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
164  $sql .= " WHERE rowid = ".$id;
165 
166  dol_syslog(__METHOD__);
167  $resql = $this->db->query($sql);
168  if ($resql)
169  {
170  $obj = $this->db->fetch_object($resql);
171  if ($obj)
172  {
173  $this->id = $id;
174  $this->type = $obj->type;
175  $this->description = $obj->description;
176  $this->parameters = $obj->parameters;
177  $this->fk_variable = $obj->fk_variable;
178  $this->update_interval = $obj->update_interval;
179  $this->next_update = $obj->next_update;
180  $this->last_status = $obj->last_status;
181  $this->checkParameters();
182  return 1;
183  } else {
184  return 0;
185  }
186  } else {
187  $this->error = "Error ".$this->db->lasterror();
188  return -1;
189  }
190  }
191 
199  public function update($user = 0, $notrigger = 0)
200  {
201  $error = 0;
202 
203  $this->checkParameters();
204 
205  // Update request
206  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
207  $sql .= " type = ".$this->type.",";
208  $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
209  $sql .= " parameters = ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
210  $sql .= " fk_variable = ".$this->fk_variable.",";
211  $sql .= " update_interval = ".$this->update_interval.",";
212  $sql .= " next_update = ".$this->next_update.",";
213  $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
214  $sql .= " WHERE rowid = ".$this->id;
215 
216  $this->db->begin();
217 
218  dol_syslog(__METHOD__);
219  $resql = $this->db->query($sql);
220  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
221 
222  // if (! $error)
223  // {
224  // if (! $notrigger)
225  // {
226  // // Uncomment this and change MYOBJECT to your own tag if you
227  // // want this action calls a trigger.
228 
229  // //// Call triggers
230  // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
231  // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
232  // //// End call triggers
233  // }
234  // }
235 
236  // Commit or rollback
237  if ($error)
238  {
239  foreach ($this->errors as $errmsg)
240  {
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 
260  public function delete($rowid, $user, $notrigger = 0)
261  {
262  $error = 0;
263 
264  $this->db->begin();
265 
266  //if (! $error)
267  //{
268  // if (! $notrigger)
269  // {
270  // Uncomment this and change MYOBJECT to your own tag if you
271  // want this action calls a trigger.
272 
274  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
275  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
277  // }
278  //}
279 
280  if (!$error)
281  {
282  $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
283  $sql .= " WHERE rowid = ".$rowid;
284 
285  dol_syslog(__METHOD__);
286  $resql = $this->db->query($sql);
287  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
288  }
289 
290  // Commit or rollback
291  if ($error)
292  {
293  foreach ($this->errors as $errmsg)
294  {
295  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
296  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
297  }
298  $this->db->rollback();
299  return -1 * $error;
300  } else {
301  $this->db->commit();
302  return 1;
303  }
304  }
305 
312  public function initAsSpecimen()
313  {
314  $this->id = 0;
315  $this->type = 0;
316  $this->description = '';
317  $this->parameters = '';
318  $this->fk_variable = 0;
319  $this->update_interval = 0;
320  $this->next_update = 0;
321  $this->last_status = '';
322  }
323 
329  public function getLastUpdated()
330  {
331  global $langs;
332  $last = $this->next_update - ($this->update_interval * 60);
333  if ($last < 1) {
334  return $langs->trans("Never");
335  }
336  $status = empty($this->last_status) ? $langs->trans("CorrectlyUpdated") : $this->last_status;
337  return $status.'<br>'.dol_print_date($last, '%d/%m/%Y %H:%M:%S');
338  }
339 
345  public function checkParameters()
346  {
347  // Clean parameters
348  if (isset($this->description)) $this->description = trim($this->description);
349  if (isset($this->parameters)) $this->parameters = trim($this->parameters);
350  else $this->parameters = "";
351  if (isset($this->last_status)) $this->last_status = trim($this->last_status);
352 
353  // Check parameters
354  if (empty($this->type) || !is_numeric($this->type) || !in_array($this->type, $this->types)) $this->type = 0;
355  if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) $this->update_interval = $this->update_min;
356  if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) $this->next_update = 0;
357  }
358 
364  public function listUpdaters()
365  {
366  $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
367  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
368 
369  dol_syslog(__METHOD__, LOG_DEBUG);
370  $resql = $this->db->query($sql);
371  if ($resql)
372  {
373  $retarray = array();
374 
375  while ($record = $this->db->fetch_array($resql))
376  {
377  $updater_obj = new PriceGlobalVariableUpdater($this->db);
378  $updater_obj->id = $record["rowid"];
379  $updater_obj->type = $record["type"];
380  $updater_obj->description = $record["description"];
381  $updater_obj->parameters = $record["parameters"];
382  $updater_obj->fk_variable = $record["fk_variable"];
383  $updater_obj->update_interval = $record["update_interval"];
384  $updater_obj->next_update = $record["next_update"];
385  $updater_obj->last_status = $record["last_status"];
386  $updater_obj->checkParameters();
387  $retarray[] = $updater_obj;
388  }
389 
390  $this->db->free($resql);
391  return $retarray;
392  } else {
393  $this->error = $this->db->error();
394  return -1;
395  }
396  }
397 
403  public function listPendingUpdaters()
404  {
405  $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
406  $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element;
407  $sql .= " WHERE next_update < ".dol_now();
408 
409  dol_syslog(__METHOD__, LOG_DEBUG);
410  $resql = $this->db->query($sql);
411  if ($resql)
412  {
413  $retarray = array();
414 
415  while ($record = $this->db->fetch_array($resql))
416  {
417  $updater_obj = new PriceGlobalVariableUpdater($this->db);
418  $updater_obj->id = $record["rowid"];
419  $updater_obj->type = $record["type"];
420  $updater_obj->description = $record["description"];
421  $updater_obj->parameters = $record["parameters"];
422  $updater_obj->fk_variable = $record["fk_variable"];
423  $updater_obj->update_interval = $record["update_interval"];
424  $updater_obj->next_update = $record["next_update"];
425  $updater_obj->last_status = $record["last_status"];
426  $updater_obj->checkParameters();
427  $retarray[] = $updater_obj;
428  }
429 
430  $this->db->free($resql);
431  return $retarray;
432  } else {
433  $this->error = $this->db->error();
434  return -1;
435  }
436  }
437 
443  public function process()
444  {
445  global $langs, $user;
446  $langs->load("errors");
447  dol_syslog(__METHOD__, LOG_DEBUG);
448 
449  $this->error = null;
450  $this->checkParameters();
451 
452  //Try to load the target global variable and abort if fails
453  if ($this->fk_variable < 1) {
454  $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
455  return 0;
456  }
457  $price_globals = new PriceGlobalVariable($this->db);
458  $res = $price_globals->fetch($this->fk_variable);
459  if ($res < 1) {
460  $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
461  return 0;
462  }
463 
464  //Process depending of type
465  if ($this->type == 0 || $this->type == 1) {
466  //Get and check if required parameters are present
467  $parameters = json_decode($this->parameters, true);
468  if (!isset($parameters)) {
469  $this->error = $langs->trans("ErrorGlobalVariableUpdater1", $this->parameters);
470  return -1;
471  }
472  $url = $parameters['URL'];
473  if (!isset($url)) {
474  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'URL');
475  return -1;
476  }
477  $value = $parameters['VALUE'];
478  if (!isset($value)) {
479  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'VALUE');
480  return -1;
481  }
482  $result = "";
483  if ($this->type == 0) {
484  // Call JSON request
485  include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
486  $tmpresult = getURLContent($url);
487  $code = $tmpresult['http_code'];
488  $result = $tmpresult['content'];
489 
490  if (!isset($result)) {
491  $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response");
492  return -1;
493  }
494  if ($code !== 200) {
495  $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code.' '.$tmpresult['curl_error_msg']);
496  return -1;
497  }
498 
499  //Decode returned response
500  $result = json_decode($result, true);
501  } elseif ($this->type == 1) {
502  $ns = $parameters['NS'];
503  if (!isset($ns)) {
504  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'NS');
505  return -1;
506  }
507  $method = $parameters['METHOD'];
508  if (!isset($method)) {
509  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'METHOD');
510  return -1;
511  }
512  $data = $parameters['DATA'];
513  if (!isset($data)) {
514  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'DATA');
515  return -1;
516  }
517 
518  //SOAP client
519  require_once NUSOAP_PATH.'/nusoap.php';
520  $soap_client = new nusoap_client($url);
521  $soap_client->soap_defencoding = 'UTF-8';
522  $soap_client->decodeUTF8(false);
523  $result = $soap_client->call($method, $data, $ns, '');
524 
525  //Check if result is a error
526  if ($result === false) {
527  $this->error = $langs->trans("ErrorGlobalVariableUpdater4", $soap_client->error_str);
528  return -1;
529  }
530  }
531 
532  //Explode value and walk for each key in value array to get the relevant key
533  $value = explode(',', $value);
534  foreach ($value as $key) {
535  $result = $result[$key];
536  }
537  if (!isset($result)) {
538  $this->error = $langs->trans("ErrorGlobalVariableUpdater3");
539  return -1;
540  }
541 
542  //Save data to global and update it
543  $price_globals->value = $result;
544  $price_globals->update($user);
545  }
546  return 1;
547  }
548 
549  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
558  public function update_next_update($next_update, $user = 0, $notrigger = 0)
559  {
560  // phpcs:enable
561  $error = 0;
562 
563  $this->next_update = $next_update;
564  $this->checkParameters();
565 
566  // Update request
567  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
568  $sql .= " next_update = ".$this->next_update;
569  $sql .= " WHERE rowid = ".$this->id;
570 
571  $this->db->begin();
572 
573  dol_syslog(__METHOD__);
574  $resql = $this->db->query($sql);
575  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
576 
577  // Commit or rollback
578  if ($error)
579  {
580  foreach ($this->errors as $errmsg)
581  {
582  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
583  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
584  }
585  $this->db->rollback();
586  return -1 * $error;
587  } else {
588  $this->db->commit();
589  return 1;
590  }
591  }
592 
593  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
602  public function update_status($last_status, $user = 0, $notrigger = 0)
603  {
604  // phpcs:enable
605  $error = 0;
606 
607  $this->last_status = $last_status;
608  $this->checkParameters();
609 
610  // Update request
611  $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
612  $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
613  $sql .= " WHERE rowid = ".$this->id;
614 
615  $this->db->begin();
616 
617  dol_syslog(__METHOD__);
618  $resql = $this->db->query($sql);
619  if (!$resql) { $error++; $this->errors[] = "Error ".$this->db->lasterror(); }
620 
621  // Commit or rollback
622  if ($error)
623  {
624  foreach ($this->errors as $errmsg)
625  {
626  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
627  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
628  }
629  $this->db->rollback();
630  return -1 * $error;
631  } else {
632  $this->db->commit();
633  return 1;
634  }
635  }
636 }
update($user=0, $notrigger=0)
Update object into database.
update_status($last_status, $user=0, $notrigger=0)
Update last_status into database.
fetch($id)
Load object in memory from the database.
checkParameters()
Checks if all parameters are in order.
</td > param sortfield sortorder printFieldListOption< tdclass="liste_titremaxwidthsearchright"></td ></tr >< trclass="liste_titre">< inputtype="checkbox"onClick="toggle(this)"/> Ref p ref Label p label Duration p duration center DesiredStock p desiredstock right StockLimitShort p seuil_stock_alerte right stock_physique right stock_real_warehouse right Ordered right StockToBuy right SupplierRef right param sortfield sortorder printFieldListTitle warehouseinternal SELECT description FROM product_lang WHERE qty< br > qty qty qty StockTooLow StockTooLow help help help< trclass="oddeven">< td >< inputtype="checkbox"class="check"name="choose'.$i.'"></td >< tdclass="nowrap"> stock</td >< td >< inputtype="hidden"name="desc'.$i.'"value="'.dol_escape_htmltag($objp-> description
Only used if Module[ID]Desc translation string is not found.
Definition: replenish.php:750
process()
Handles the processing of this updater.
create($user, $notrigger=0)
Create object into database.
getURLContent($url, $postorget= 'GET', $param= '', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0)
Function to get a content from an URL (use proxy if proxy defined).
Definition: geturl.lib.php:38
$conf db
API class for accounts.
Definition: inc.php:54
getLastUpdated()
Returns the last updated time in string html format, returns &quot;never&quot; if its less than 1...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
update_next_update($next_update, $user=0, $notrigger=0)
Update next_update into database.
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
Class for accesing price global variables table.
listUpdaters()
List all price global variables.
listPendingUpdaters()
List all updaters which need to be processed.
Class for price global variable updaters table.
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:105