dolibarr 21.0.0-alpha
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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
33{
37 public $db;
38
42 public $error = '';
43
47 public $errors = array();
48
49 public $types = array(0, 1);
50 public $update_min = 5;
51
55 public $id;
56
57 public $type;
58
62 public $description;
63
64 public $parameters;
65
69 public $fk_variable;
70
72 public $next_update;
73 public $last_status;
74
78 public $table_element = "c_price_global_variable_updater";
79
85 public function __construct($db)
86 {
87 $this->db = $db;
88 }
89
90
98 public function create($user, $notrigger = 0)
99 {
100 $error = 0;
101
102 $this->checkParameters();
103
104 // Insert request
105 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
106 $sql .= "type, description, parameters, fk_variable, update_interval, next_update, last_status";
107 $sql .= ") VALUES (";
108 $sql .= " ".((int) $this->type).",";
109 $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
110 $sql .= " ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
111 $sql .= " ".((int) $this->fk_variable).",";
112 $sql .= " ".((int) $this->update_interval).",";
113 $sql .= " ".((int) $this->next_update).",";
114 $sql .= " ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
115 $sql .= ")";
116
117 $this->db->begin();
118
119 dol_syslog(__METHOD__, LOG_DEBUG);
120 $resql = $this->db->query($sql);
121 if (!$resql) {
122 $error++;
123 $this->errors[] = "Error ".$this->db->lasterror();
124 }
125
126 if (!$error) {
127 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
128
129 if (!$notrigger) {
130 // Uncomment this and change MYOBJECT to your own tag if you
131 // want this action calls a trigger.
132
134 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
135 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
137 }
138 }
139
140 // Commit or rollback
141 if ($error) {
142 foreach ($this->errors as $errmsg) {
143 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
144 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
145 }
146 $this->db->rollback();
147 return -1 * $error;
148 } else {
149 $this->db->commit();
150 return $this->id;
151 }
152 }
153
154
161 public function fetch($id)
162 {
163 $sql = "SELECT type, description, parameters, fk_variable, update_interval, next_update, last_status";
164 $sql .= " FROM ".$this->db->prefix().$this->table_element;
165 $sql .= " WHERE rowid = ".((int) $id);
166
167 dol_syslog(__METHOD__);
168 $resql = $this->db->query($sql);
169 if ($resql) {
170 $obj = $this->db->fetch_object($resql);
171 if ($obj) {
172 $this->id = $id;
173 $this->type = $obj->type;
174 $this->description = $obj->description;
175 $this->parameters = $obj->parameters;
176 $this->fk_variable = $obj->fk_variable;
177 $this->update_interval = $obj->update_interval;
178 $this->next_update = $obj->next_update;
179 $this->last_status = $obj->last_status;
180 $this->checkParameters();
181 return 1;
182 } else {
183 return 0;
184 }
185 } else {
186 $this->error = "Error ".$this->db->lasterror();
187 return -1;
188 }
189 }
190
198 public function update($user = null, $notrigger = 0)
199 {
200 $error = 0;
201
202 $this->checkParameters();
203
204 // Update request
205 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
206 $sql .= " type = ".((int) $this->type).",";
207 $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
208 $sql .= " parameters = ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
209 $sql .= " fk_variable = ".((int) $this->fk_variable).",";
210 $sql .= " update_interval = ".((int) $this->update_interval).",";
211 $sql .= " next_update = ".((int) $this->next_update).",";
212 $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
213 $sql .= " WHERE rowid = ".((int) $this->id);
214
215 $this->db->begin();
216
217 dol_syslog(__METHOD__);
218 $resql = $this->db->query($sql);
219 if (!$resql) {
220 $error++;
221 $this->errors[] = "Error ".$this->db->lasterror();
222 }
223
224 // if (! $error)
225 // {
226 // if (! $notrigger)
227 // {
228 // // Uncomment this and change MYOBJECT to your own tag if you
229 // // want this action calls a trigger.
230
231 // //// Call triggers
232 // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
233 // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
234 // //// End call triggers
235 // }
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
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 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
282 $sql .= " WHERE rowid = ".((int) $rowid);
283
284 dol_syslog(__METHOD__);
285 $resql = $this->db->query($sql);
286 if (!$resql) {
287 $error++;
288 $this->errors[] = "Error ".$this->db->lasterror();
289 }
290 }
291
292 // Commit or rollback
293 if ($error) {
294 foreach ($this->errors as $errmsg) {
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 return 1;
324 }
325
331 public function getLastUpdated()
332 {
333 global $langs;
334 $last = $this->next_update - ($this->update_interval * 60);
335 if ($last < 1) {
336 return $langs->trans("Never");
337 }
338 $status = empty($this->last_status) ? $langs->trans("CorrectlyUpdated") : $this->last_status;
339 return $status.'<br>'.dol_print_date($last, '%d/%m/%Y %H:%M:%S');
340 }
341
347 public function checkParameters()
348 {
349 // Clean parameters
350 if (isset($this->description)) {
351 $this->description = trim($this->description);
352 }
353 if (isset($this->parameters)) {
354 $this->parameters = trim($this->parameters);
355 } else {
356 $this->parameters = "";
357 }
358 if (isset($this->last_status)) {
359 $this->last_status = trim($this->last_status);
360 }
361
362 // Check parameters
363 if (empty($this->type) || !is_numeric($this->type) || !in_array($this->type, $this->types)) {
364 $this->type = 0;
365 }
366 if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) {
367 $this->update_interval = $this->update_min;
368 }
369 if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) {
370 $this->next_update = 0;
371 }
372 }
373
379 public function listUpdaters()
380 {
381 $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
382 $sql .= " FROM ".$this->db->prefix().$this->table_element;
383
384 dol_syslog(__METHOD__, LOG_DEBUG);
385 $resql = $this->db->query($sql);
386 if ($resql) {
387 $retarray = array();
388
389 while ($record = $this->db->fetch_array($resql)) {
390 $updater_obj = new PriceGlobalVariableUpdater($this->db);
391 $updater_obj->id = $record["rowid"];
392 $updater_obj->type = $record["type"];
393 $updater_obj->description = $record["description"];
394 $updater_obj->parameters = $record["parameters"];
395 $updater_obj->fk_variable = $record["fk_variable"];
396 $updater_obj->update_interval = $record["update_interval"];
397 $updater_obj->next_update = $record["next_update"];
398 $updater_obj->last_status = $record["last_status"];
399 $updater_obj->checkParameters();
400 $retarray[] = $updater_obj;
401 }
402
403 $this->db->free($resql);
404 return $retarray;
405 } else {
406 $this->error = $this->db->error();
407 return -1;
408 }
409 }
410
416 public function listPendingUpdaters()
417 {
418 $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
419 $sql .= " FROM ".$this->db->prefix().$this->table_element;
420 $sql .= " WHERE next_update < ".dol_now();
421
422 dol_syslog(__METHOD__, LOG_DEBUG);
423 $resql = $this->db->query($sql);
424 if ($resql) {
425 $retarray = array();
426
427 while ($record = $this->db->fetch_array($resql)) {
428 $updater_obj = new PriceGlobalVariableUpdater($this->db);
429 $updater_obj->id = $record["rowid"];
430 $updater_obj->type = $record["type"];
431 $updater_obj->description = $record["description"];
432 $updater_obj->parameters = $record["parameters"];
433 $updater_obj->fk_variable = $record["fk_variable"];
434 $updater_obj->update_interval = $record["update_interval"];
435 $updater_obj->next_update = $record["next_update"];
436 $updater_obj->last_status = $record["last_status"];
437 $updater_obj->checkParameters();
438 $retarray[] = $updater_obj;
439 }
440
441 $this->db->free($resql);
442 return $retarray;
443 } else {
444 $this->error = $this->db->error();
445 return -1;
446 }
447 }
448
454 public function process()
455 {
456 global $langs, $user;
457 $langs->load("errors");
458 dol_syslog(__METHOD__, LOG_DEBUG);
459
460 $this->error = '';
461 $this->checkParameters();
462
463 //Try to load the target global variable and abort if fails
464 if ($this->fk_variable < 1) {
465 $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
466 return 0;
467 }
468 $price_globals = new PriceGlobalVariable($this->db);
469 $res = $price_globals->fetch($this->fk_variable);
470 if ($res < 1) {
471 $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
472 return 0;
473 }
474
475 //Process depending of type
476 if ($this->type == 0 || $this->type == 1) {
477 //Get and check if required parameters are present
478 $parameters = json_decode($this->parameters, true);
479 if (!isset($parameters)) {
480 $this->error = $langs->trans("ErrorGlobalVariableUpdater1", $this->parameters);
481 return -1;
482 }
483 $url = $parameters['URL'];
484 if (!isset($url)) {
485 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'URL');
486 return -1;
487 }
488 $value = $parameters['VALUE'];
489 if (!isset($value)) {
490 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'VALUE');
491 return -1;
492 }
493 $result = "";
494 if ($this->type == 0) {
495 // Call JSON request
496 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
497 $tmpresult = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 0);
498 $code = $tmpresult['http_code'];
499 $result = $tmpresult['content'];
500
501 if (!isset($result)) {
502 $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response");
503 return -1;
504 }
505 if ($code !== 200) {
506 $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code.' '.$tmpresult['curl_error_msg']);
507 return -1;
508 }
509
510 //Decode returned response
511 $result = json_decode($result, true);
512 } elseif ($this->type == 1) {
513 $ns = $parameters['NS'];
514 if (!isset($ns)) {
515 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'NS');
516 return -1;
517 }
518 $method = $parameters['METHOD'];
519 if (!isset($method)) {
520 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'METHOD');
521 return -1;
522 }
523 $data = $parameters['DATA'];
524 if (!isset($data)) {
525 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'DATA');
526 return -1;
527 }
528
529 //SOAP client
530 require_once NUSOAP_PATH.'/nusoap.php';
531 $soap_client = new nusoap_client($url);
532 $soap_client->soap_defencoding = 'UTF-8';
533 $soap_client->decodeUTF8(false);
534 $result = $soap_client->call($method, $data, $ns, '');
535
536 //Check if result is a error
537 if ($result === false) {
538 $this->error = $langs->trans("ErrorGlobalVariableUpdater4", $soap_client->error_str);
539 return -1;
540 }
541 }
542
543 //Explode value and walk for each key in value array to get the relevant key
544 $value = explode(',', $value);
545 foreach ($value as $key) {
546 $result = $result[$key];
547 }
548 if (!isset($result)) {
549 $this->error = $langs->trans("ErrorGlobalVariableUpdater3");
550 return -1;
551 }
552
553 //Save data to global and update it
554 $price_globals->value = $result;
555 $price_globals->update($user);
556 }
557 return 1;
558 }
559
560 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
569 public function update_next_update($next_update, $user = null, $notrigger = 0)
570 {
571 // phpcs:enable
572 $error = 0;
573
574 $this->next_update = $next_update;
575 $this->checkParameters();
576
577 // Update request
578 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
579 $sql .= " next_update = ".$this->next_update;
580 $sql .= " WHERE rowid = ".((int) $this->id);
581
582 $this->db->begin();
583
584 dol_syslog(__METHOD__);
585 $resql = $this->db->query($sql);
586 if (!$resql) {
587 $error++;
588 $this->errors[] = "Error ".$this->db->lasterror();
589 }
590
591 // Commit or rollback
592 if ($error) {
593 foreach ($this->errors as $errmsg) {
594 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
595 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
596 }
597 $this->db->rollback();
598 return -1 * $error;
599 } else {
600 $this->db->commit();
601 return 1;
602 }
603 }
604
605 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
614 public function update_status($last_status, $user = null, $notrigger = 0)
615 {
616 // phpcs:enable
617 $error = 0;
618
619 $this->last_status = $last_status;
620 $this->checkParameters();
621
622 // Update request
623 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
624 $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
625 $sql .= " WHERE rowid = ".((int) $this->id);
626
627 $this->db->begin();
628
629 dol_syslog(__METHOD__);
630 $resql = $this->db->query($sql);
631 if (!$resql) {
632 $error++;
633 $this->errors[] = "Error ".$this->db->lasterror();
634 }
635
636 // Commit or rollback
637 if ($error) {
638 foreach ($this->errors as $errmsg) {
639 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
640 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
641 }
642 $this->db->rollback();
643 return -1 * $error;
644 } else {
645 $this->db->commit();
646 return 1;
647 }
648 }
649}
Class for accessing price global variables table.
Class for price global variable updaters table.
update_status($last_status, $user=null, $notrigger=0)
Update last_status into database.
getLastUpdated()
Returns the last updated time in string html format, returns "never" if its less than 1.
checkParameters()
Checks if all parameters are in order.
update_next_update($next_update, $user=null, $notrigger=0)
Update next_update into database.
listUpdaters()
List all price global variables.
listPendingUpdaters()
List all updaters which need to be processed.
process()
Handles the processing of this updater.
initAsSpecimen()
Initialise object with example values Id must be 0 if object instance is a specimen.
update($user=null, $notrigger=0)
Update object into database.
create($user, $notrigger=0)
Create object into database.
fetch($id)
Load object in memory from the database.
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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).
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:139