dolibarr  16.0.5
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 ".$this->db->prefix().$this->table_element." (";
104  $sql .= "type, description, parameters, fk_variable, update_interval, next_update, last_status";
105  $sql .= ") VALUES (";
106  $sql .= " ".((int) $this->type).",";
107  $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
108  $sql .= " ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
109  $sql .= " ".((int) $this->fk_variable).",";
110  $sql .= " ".((int) $this->update_interval).",";
111  $sql .= " ".((int) $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) {
120  $error++; $this->errors[] = "Error ".$this->db->lasterror();
121  }
122 
123  if (!$error) {
124  $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
125 
126  if (!$notrigger) {
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  foreach ($this->errors as $errmsg) {
140  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
141  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
142  }
143  $this->db->rollback();
144  return -1 * $error;
145  } else {
146  $this->db->commit();
147  return $this->id;
148  }
149  }
150 
151 
158  public function fetch($id)
159  {
160  $sql = "SELECT type, description, parameters, fk_variable, update_interval, next_update, last_status";
161  $sql .= " FROM ".$this->db->prefix().$this->table_element;
162  $sql .= " WHERE rowid = ".((int) $id);
163 
164  dol_syslog(__METHOD__);
165  $resql = $this->db->query($sql);
166  if ($resql) {
167  $obj = $this->db->fetch_object($resql);
168  if ($obj) {
169  $this->id = $id;
170  $this->type = $obj->type;
171  $this->description = $obj->description;
172  $this->parameters = $obj->parameters;
173  $this->fk_variable = $obj->fk_variable;
174  $this->update_interval = $obj->update_interval;
175  $this->next_update = $obj->next_update;
176  $this->last_status = $obj->last_status;
177  $this->checkParameters();
178  return 1;
179  } else {
180  return 0;
181  }
182  } else {
183  $this->error = "Error ".$this->db->lasterror();
184  return -1;
185  }
186  }
187 
195  public function update($user = 0, $notrigger = 0)
196  {
197  $error = 0;
198 
199  $this->checkParameters();
200 
201  // Update request
202  $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
203  $sql .= " type = ".((int) $this->type).",";
204  $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
205  $sql .= " parameters = ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
206  $sql .= " fk_variable = ".((int) $this->fk_variable).",";
207  $sql .= " update_interval = ".((int) $this->update_interval).",";
208  $sql .= " next_update = ".((int) $this->next_update).",";
209  $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
210  $sql .= " WHERE rowid = ".((int) $this->id);
211 
212  $this->db->begin();
213 
214  dol_syslog(__METHOD__);
215  $resql = $this->db->query($sql);
216  if (!$resql) {
217  $error++; $this->errors[] = "Error ".$this->db->lasterror();
218  }
219 
220  // if (! $error)
221  // {
222  // if (! $notrigger)
223  // {
224  // // Uncomment this and change MYOBJECT to your own tag if you
225  // // want this action calls a trigger.
226 
227  // //// Call triggers
228  // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
229  // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
230  // //// End call triggers
231  // }
232  // }
233 
234  // Commit or rollback
235  if ($error) {
236  foreach ($this->errors as $errmsg) {
237  dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
238  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
239  }
240  $this->db->rollback();
241  return -1 * $error;
242  } else {
243  $this->db->commit();
244  return 1;
245  }
246  }
247 
256  public function delete($rowid, $user, $notrigger = 0)
257  {
258  $error = 0;
259 
260  $this->db->begin();
261 
262  //if (! $error)
263  //{
264  // if (! $notrigger)
265  // {
266  // Uncomment this and change MYOBJECT to your own tag if you
267  // want this action calls a trigger.
268 
270  //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
271  //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
273  // }
274  //}
275 
276  if (!$error) {
277  $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
278  $sql .= " WHERE rowid = ".((int) $rowid);
279 
280  dol_syslog(__METHOD__);
281  $resql = $this->db->query($sql);
282  if (!$resql) {
283  $error++; $this->errors[] = "Error ".$this->db->lasterror();
284  }
285  }
286 
287  // Commit or rollback
288  if ($error) {
289  foreach ($this->errors as $errmsg) {
290  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
291  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
292  }
293  $this->db->rollback();
294  return -1 * $error;
295  } else {
296  $this->db->commit();
297  return 1;
298  }
299  }
300 
307  public function initAsSpecimen()
308  {
309  $this->id = 0;
310  $this->type = 0;
311  $this->description = '';
312  $this->parameters = '';
313  $this->fk_variable = 0;
314  $this->update_interval = 0;
315  $this->next_update = 0;
316  $this->last_status = '';
317  }
318 
324  public function getLastUpdated()
325  {
326  global $langs;
327  $last = $this->next_update - ($this->update_interval * 60);
328  if ($last < 1) {
329  return $langs->trans("Never");
330  }
331  $status = empty($this->last_status) ? $langs->trans("CorrectlyUpdated") : $this->last_status;
332  return $status.'<br>'.dol_print_date($last, '%d/%m/%Y %H:%M:%S');
333  }
334 
340  public function checkParameters()
341  {
342  // Clean parameters
343  if (isset($this->description)) {
344  $this->description = trim($this->description);
345  }
346  if (isset($this->parameters)) {
347  $this->parameters = trim($this->parameters);
348  } else {
349  $this->parameters = "";
350  }
351  if (isset($this->last_status)) {
352  $this->last_status = trim($this->last_status);
353  }
354 
355  // Check parameters
356  if (empty($this->type) || !is_numeric($this->type) || !in_array($this->type, $this->types)) {
357  $this->type = 0;
358  }
359  if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) {
360  $this->update_interval = $this->update_min;
361  }
362  if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) {
363  $this->next_update = 0;
364  }
365  }
366 
372  public function listUpdaters()
373  {
374  $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
375  $sql .= " FROM ".$this->db->prefix().$this->table_element;
376 
377  dol_syslog(__METHOD__, LOG_DEBUG);
378  $resql = $this->db->query($sql);
379  if ($resql) {
380  $retarray = array();
381 
382  while ($record = $this->db->fetch_array($resql)) {
383  $updater_obj = new PriceGlobalVariableUpdater($this->db);
384  $updater_obj->id = $record["rowid"];
385  $updater_obj->type = $record["type"];
386  $updater_obj->description = $record["description"];
387  $updater_obj->parameters = $record["parameters"];
388  $updater_obj->fk_variable = $record["fk_variable"];
389  $updater_obj->update_interval = $record["update_interval"];
390  $updater_obj->next_update = $record["next_update"];
391  $updater_obj->last_status = $record["last_status"];
392  $updater_obj->checkParameters();
393  $retarray[] = $updater_obj;
394  }
395 
396  $this->db->free($resql);
397  return $retarray;
398  } else {
399  $this->error = $this->db->error();
400  return -1;
401  }
402  }
403 
409  public function listPendingUpdaters()
410  {
411  $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
412  $sql .= " FROM ".$this->db->prefix().$this->table_element;
413  $sql .= " WHERE next_update < ".dol_now();
414 
415  dol_syslog(__METHOD__, LOG_DEBUG);
416  $resql = $this->db->query($sql);
417  if ($resql) {
418  $retarray = array();
419 
420  while ($record = $this->db->fetch_array($resql)) {
421  $updater_obj = new PriceGlobalVariableUpdater($this->db);
422  $updater_obj->id = $record["rowid"];
423  $updater_obj->type = $record["type"];
424  $updater_obj->description = $record["description"];
425  $updater_obj->parameters = $record["parameters"];
426  $updater_obj->fk_variable = $record["fk_variable"];
427  $updater_obj->update_interval = $record["update_interval"];
428  $updater_obj->next_update = $record["next_update"];
429  $updater_obj->last_status = $record["last_status"];
430  $updater_obj->checkParameters();
431  $retarray[] = $updater_obj;
432  }
433 
434  $this->db->free($resql);
435  return $retarray;
436  } else {
437  $this->error = $this->db->error();
438  return -1;
439  }
440  }
441 
447  public function process()
448  {
449  global $langs, $user;
450  $langs->load("errors");
451  dol_syslog(__METHOD__, LOG_DEBUG);
452 
453  $this->error = null;
454  $this->checkParameters();
455 
456  //Try to load the target global variable and abort if fails
457  if ($this->fk_variable < 1) {
458  $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
459  return 0;
460  }
461  $price_globals = new PriceGlobalVariable($this->db);
462  $res = $price_globals->fetch($this->fk_variable);
463  if ($res < 1) {
464  $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
465  return 0;
466  }
467 
468  //Process depending of type
469  if ($this->type == 0 || $this->type == 1) {
470  //Get and check if required parameters are present
471  $parameters = json_decode($this->parameters, true);
472  if (!isset($parameters)) {
473  $this->error = $langs->trans("ErrorGlobalVariableUpdater1", $this->parameters);
474  return -1;
475  }
476  $url = $parameters['URL'];
477  if (!isset($url)) {
478  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'URL');
479  return -1;
480  }
481  $value = $parameters['VALUE'];
482  if (!isset($value)) {
483  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'VALUE');
484  return -1;
485  }
486  $result = "";
487  if ($this->type == 0) {
488  // Call JSON request
489  include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
490  $tmpresult = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 0);
491  $code = $tmpresult['http_code'];
492  $result = $tmpresult['content'];
493 
494  if (!isset($result)) {
495  $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response");
496  return -1;
497  }
498  if ($code !== 200) {
499  $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code.' '.$tmpresult['curl_error_msg']);
500  return -1;
501  }
502 
503  //Decode returned response
504  $result = json_decode($result, true);
505  } elseif ($this->type == 1) {
506  $ns = $parameters['NS'];
507  if (!isset($ns)) {
508  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'NS');
509  return -1;
510  }
511  $method = $parameters['METHOD'];
512  if (!isset($method)) {
513  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'METHOD');
514  return -1;
515  }
516  $data = $parameters['DATA'];
517  if (!isset($data)) {
518  $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'DATA');
519  return -1;
520  }
521 
522  //SOAP client
523  require_once NUSOAP_PATH.'/nusoap.php';
524  $soap_client = new nusoap_client($url);
525  $soap_client->soap_defencoding = 'UTF-8';
526  $soap_client->decodeUTF8(false);
527  $result = $soap_client->call($method, $data, $ns, '');
528 
529  //Check if result is a error
530  if ($result === false) {
531  $this->error = $langs->trans("ErrorGlobalVariableUpdater4", $soap_client->error_str);
532  return -1;
533  }
534  }
535 
536  //Explode value and walk for each key in value array to get the relevant key
537  $value = explode(',', $value);
538  foreach ($value as $key) {
539  $result = $result[$key];
540  }
541  if (!isset($result)) {
542  $this->error = $langs->trans("ErrorGlobalVariableUpdater3");
543  return -1;
544  }
545 
546  //Save data to global and update it
547  $price_globals->value = $result;
548  $price_globals->update($user);
549  }
550  return 1;
551  }
552 
553  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
562  public function update_next_update($next_update, $user = 0, $notrigger = 0)
563  {
564  // phpcs:enable
565  $error = 0;
566 
567  $this->next_update = $next_update;
568  $this->checkParameters();
569 
570  // Update request
571  $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
572  $sql .= " next_update = ".$this->next_update;
573  $sql .= " WHERE rowid = ".((int) $this->id);
574 
575  $this->db->begin();
576 
577  dol_syslog(__METHOD__);
578  $resql = $this->db->query($sql);
579  if (!$resql) {
580  $error++; $this->errors[] = "Error ".$this->db->lasterror();
581  }
582 
583  // Commit or rollback
584  if ($error) {
585  foreach ($this->errors as $errmsg) {
586  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
587  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
588  }
589  $this->db->rollback();
590  return -1 * $error;
591  } else {
592  $this->db->commit();
593  return 1;
594  }
595  }
596 
597  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
606  public function update_status($last_status, $user = 0, $notrigger = 0)
607  {
608  // phpcs:enable
609  $error = 0;
610 
611  $this->last_status = $last_status;
612  $this->checkParameters();
613 
614  // Update request
615  $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
616  $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
617  $sql .= " WHERE rowid = ".((int) $this->id);
618 
619  $this->db->begin();
620 
621  dol_syslog(__METHOD__);
622  $resql = $this->db->query($sql);
623  if (!$resql) {
624  $error++; $this->errors[] = "Error ".$this->db->lasterror();
625  }
626 
627  // Commit or rollback
628  if ($error) {
629  foreach ($this->errors as $errmsg) {
630  dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
631  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
632  }
633  $this->db->rollback();
634  return -1 * $error;
635  } else {
636  $this->db->commit();
637  return 1;
638  }
639  }
640 }
db
$conf db
API class for accounts.
Definition: inc.php:41
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
PriceGlobalVariableUpdater\create
create($user, $notrigger=0)
Create object into database.
Definition: price_global_variable_updater.class.php:96
PriceGlobalVariableUpdater
Class for price global variable updaters table.
Definition: price_global_variable_updater.class.php:30
PriceGlobalVariableUpdater\update_status
update_status($last_status, $user=0, $notrigger=0)
Update last_status into database.
Definition: price_global_variable_updater.class.php:606
getURLContent
getURLContent($url, $postorget='GET', $param='', $followlocation=1, $addheaders=array(), $allowedschemes=array('http', 'https'), $localurl=0, $ssl_verifypeer=-1)
Function to get a content from an URL (use proxy if proxy defined).
Definition: geturl.lib.php:41
PriceGlobalVariableUpdater\listUpdaters
listUpdaters()
List all price global variables.
Definition: price_global_variable_updater.class.php:372
PriceGlobalVariableUpdater\listPendingUpdaters
listPendingUpdaters()
List all updaters which need to be processed.
Definition: price_global_variable_updater.class.php:409
PriceGlobalVariableUpdater\$update_interval
$update_interval
Interval in mins.
Definition: price_global_variable_updater.class.php:69
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
PriceGlobalVariableUpdater\update_next_update
update_next_update($next_update, $user=0, $notrigger=0)
Update next_update into database.
Definition: price_global_variable_updater.class.php:562
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
PriceGlobalVariableUpdater\getLastUpdated
getLastUpdated()
Returns the last updated time in string html format, returns "never" if its less than 1.
Definition: price_global_variable_updater.class.php:324
PriceGlobalVariableUpdater\process
process()
Handles the processing of this updater.
Definition: price_global_variable_updater.class.php:447
PriceGlobalVariableUpdater\$types
$types
Updater types.
Definition: price_global_variable_updater.class.php:47
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->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->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
PriceGlobalVariableUpdater\update
update($user=0, $notrigger=0)
Update object into database.
Definition: price_global_variable_updater.class.php:195
PriceGlobalVariableUpdater\$update_min
$update_min
Minimal update rate.
Definition: price_global_variable_updater.class.php:48
PriceGlobalVariableUpdater\fetch
fetch($id)
Load object in memory from the database.
Definition: price_global_variable_updater.class.php:158
PriceGlobalVariableUpdater\checkParameters
checkParameters()
Checks if all parameters are in order.
Definition: price_global_variable_updater.class.php:340
type
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition: repair.php:119
PriceGlobalVariableUpdater\__construct
__construct($db)
Constructor.
Definition: price_global_variable_updater.class.php:83
PriceGlobalVariable
Class for accesing price global variables table.
Definition: price_global_variable.class.php:30
PriceGlobalVariableUpdater\$next_update
$next_update
Next update timestamp.
Definition: price_global_variable_updater.class.php:70
PriceGlobalVariableUpdater\initAsSpecimen
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
Definition: price_global_variable_updater.class.php:307