dolibarr 21.0.0-beta
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
52 public $types = array(0, 1);
56 public $update_min = 5;
57
61 public $id;
62
66 public $type;
67
71 public $description;
72
76 public $parameters;
77
81 public $fk_variable;
82
86 public $update_interval;
90 public $next_update;
94 public $last_status;
95
99 public $table_element = "c_price_global_variable_updater";
100
106 public function __construct($db)
107 {
108 $this->db = $db;
109 }
110
111
119 public function create($user, $notrigger = 0)
120 {
121 $error = 0;
122
123 $this->checkParameters();
124
125 // Insert request
126 $sql = "INSERT INTO ".$this->db->prefix().$this->table_element." (";
127 $sql .= "type, description, parameters, fk_variable, update_interval, next_update, last_status";
128 $sql .= ") VALUES (";
129 $sql .= " ".((int) $this->type).",";
130 $sql .= " ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
131 $sql .= " ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
132 $sql .= " ".((int) $this->fk_variable).",";
133 $sql .= " ".((int) $this->update_interval).",";
134 $sql .= " ".((int) $this->next_update).",";
135 $sql .= " ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
136 $sql .= ")";
137
138 $this->db->begin();
139
140 dol_syslog(__METHOD__, LOG_DEBUG);
141 $resql = $this->db->query($sql);
142 if (!$resql) {
143 $error++;
144 $this->errors[] = "Error ".$this->db->lasterror();
145 }
146
147 if (!$error) {
148 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
149
150 /*
151 if (!$notrigger) {
152 // Uncomment this and change MYOBJECT to your own tag if you
153 // want this action calls a trigger.
154
156 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
157 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
159 }
160 */
161 }
162
163 // Commit or rollback
164 if ($error) {
165 foreach ($this->errors as $errmsg) {
166 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
167 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
168 }
169 $this->db->rollback();
170 return -1 * $error;
171 } else {
172 $this->db->commit();
173 return $this->id;
174 }
175 }
176
177
184 public function fetch($id)
185 {
186 $sql = "SELECT type, description, parameters, fk_variable, update_interval, next_update, last_status";
187 $sql .= " FROM ".$this->db->prefix().$this->table_element;
188 $sql .= " WHERE rowid = ".((int) $id);
189
190 dol_syslog(__METHOD__);
191 $resql = $this->db->query($sql);
192 if ($resql) {
193 $obj = $this->db->fetch_object($resql);
194 if ($obj) {
195 $this->id = $id;
196 $this->type = $obj->type;
197 $this->description = $obj->description;
198 $this->parameters = $obj->parameters;
199 $this->fk_variable = $obj->fk_variable;
200 $this->update_interval = $obj->update_interval;
201 $this->next_update = $obj->next_update;
202 $this->last_status = $obj->last_status;
203 $this->checkParameters();
204 return 1;
205 } else {
206 return 0;
207 }
208 } else {
209 $this->error = "Error ".$this->db->lasterror();
210 return -1;
211 }
212 }
213
221 public function update($user = null, $notrigger = 0)
222 {
223 $error = 0;
224
225 $this->checkParameters();
226
227 // Update request
228 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
229 $sql .= " type = ".((int) $this->type).",";
230 $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
231 $sql .= " parameters = ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
232 $sql .= " fk_variable = ".((int) $this->fk_variable).",";
233 $sql .= " update_interval = ".((int) $this->update_interval).",";
234 $sql .= " next_update = ".((int) $this->next_update).",";
235 $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
236 $sql .= " WHERE rowid = ".((int) $this->id);
237
238 $this->db->begin();
239
240 dol_syslog(__METHOD__);
241 $resql = $this->db->query($sql);
242 if (!$resql) {
243 $error++;
244 $this->errors[] = "Error ".$this->db->lasterror();
245 }
246
247 // if (! $error)
248 // {
249 // if (! $notrigger)
250 // {
251 // // Uncomment this and change MYOBJECT to your own tag if you
252 // // want this action calls a trigger.
253
254 // //// Call triggers
255 // //$result=$this->call_trigger('MYOBJECT_MODIFY',$user);
256 // //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
257 // //// End call triggers
258 // }
259 // }
260
261 // Commit or rollback
262 if ($error) {
263 foreach ($this->errors as $errmsg) {
264 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
265 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
266 }
267 $this->db->rollback();
268 return -1 * $error;
269 } else {
270 $this->db->commit();
271 return 1;
272 }
273 }
274
283 public function delete($rowid, $user, $notrigger = 0)
284 {
285 $error = 0;
286
287 $this->db->begin();
288
289 //if (! $error)
290 //{
291 // if (! $notrigger)
292 // {
293 // Uncomment this and change MYOBJECT to your own tag if you
294 // want this action calls a trigger.
295
297 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
298 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
300 // }
301 //}
302
303 if (!$error) {
304 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
305 $sql .= " WHERE rowid = ".((int) $rowid);
306
307 dol_syslog(__METHOD__);
308 $resql = $this->db->query($sql);
309 if (!$resql) {
310 $error++;
311 $this->errors[] = "Error ".$this->db->lasterror();
312 }
313 }
314
315 // Commit or rollback
316 if ($error) {
317 foreach ($this->errors as $errmsg) {
318 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
319 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
320 }
321 $this->db->rollback();
322 return -1 * $error;
323 } else {
324 $this->db->commit();
325 return 1;
326 }
327 }
328
335 public function initAsSpecimen()
336 {
337 $this->id = 0;
338 $this->type = 0;
339 $this->description = '';
340 $this->parameters = '';
341 $this->fk_variable = 0;
342 $this->update_interval = 0;
343 $this->next_update = 0;
344 $this->last_status = '';
345
346 return 1;
347 }
348
354 public function getLastUpdated()
355 {
356 global $langs;
357 $last = $this->next_update - ($this->update_interval * 60);
358 if ($last < 1) {
359 return $langs->trans("Never");
360 }
361 $status = empty($this->last_status) ? $langs->trans("CorrectlyUpdated") : $this->last_status;
362 return $status.'<br>'.dol_print_date($last, '%d/%m/%Y %H:%M:%S');
363 }
364
370 public function checkParameters()
371 {
372 // Clean parameters
373 if (isset($this->description)) {
374 $this->description = trim($this->description);
375 }
376 if (isset($this->parameters)) {
377 $this->parameters = trim($this->parameters);
378 } else {
379 $this->parameters = "";
380 }
381 if (isset($this->last_status)) {
382 $this->last_status = trim($this->last_status);
383 }
384
385 // Check parameters
386 if (empty($this->type) || !is_numeric($this->type) || !in_array($this->type, $this->types)) {
387 $this->type = 0;
388 }
389 if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) {
390 $this->update_interval = $this->update_min;
391 }
392 if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) {
393 $this->next_update = 0;
394 }
395 }
396
402 public function listUpdaters()
403 {
404 $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
405 $sql .= " FROM ".$this->db->prefix().$this->table_element;
406
407 dol_syslog(__METHOD__, LOG_DEBUG);
408 $resql = $this->db->query($sql);
409 if ($resql) {
410 $retarray = array();
411
412 while ($record = $this->db->fetch_array($resql)) {
413 $updater_obj = new PriceGlobalVariableUpdater($this->db);
414 $updater_obj->id = $record["rowid"];
415 $updater_obj->type = $record["type"];
416 $updater_obj->description = $record["description"];
417 $updater_obj->parameters = $record["parameters"];
418 $updater_obj->fk_variable = $record["fk_variable"];
419 $updater_obj->update_interval = $record["update_interval"];
420 $updater_obj->next_update = $record["next_update"];
421 $updater_obj->last_status = $record["last_status"];
422 $updater_obj->checkParameters();
423 $retarray[] = $updater_obj;
424 }
425
426 $this->db->free($resql);
427 return $retarray;
428 } else {
429 $this->error = $this->db->error();
430 return -1;
431 }
432 }
433
439 public function listPendingUpdaters()
440 {
441 $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
442 $sql .= " FROM ".$this->db->prefix().$this->table_element;
443 $sql .= " WHERE next_update < ".dol_now();
444
445 dol_syslog(__METHOD__, LOG_DEBUG);
446 $resql = $this->db->query($sql);
447 if ($resql) {
448 $retarray = array();
449
450 while ($record = $this->db->fetch_array($resql)) {
451 $updater_obj = new PriceGlobalVariableUpdater($this->db);
452 $updater_obj->id = $record["rowid"];
453 $updater_obj->type = $record["type"];
454 $updater_obj->description = $record["description"];
455 $updater_obj->parameters = $record["parameters"];
456 $updater_obj->fk_variable = $record["fk_variable"];
457 $updater_obj->update_interval = $record["update_interval"];
458 $updater_obj->next_update = $record["next_update"];
459 $updater_obj->last_status = $record["last_status"];
460 $updater_obj->checkParameters();
461 $retarray[] = $updater_obj;
462 }
463
464 $this->db->free($resql);
465 return $retarray;
466 } else {
467 $this->error = $this->db->error();
468 return -1;
469 }
470 }
471
477 public function process()
478 {
479 global $langs, $user;
480 $langs->load("errors");
481 dol_syslog(__METHOD__, LOG_DEBUG);
482
483 $this->error = '';
484 $this->checkParameters();
485
486 //Try to load the target global variable and abort if fails
487 if ($this->fk_variable < 1) {
488 $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
489 return 0;
490 }
491 $price_globals = new PriceGlobalVariable($this->db);
492 $res = $price_globals->fetch($this->fk_variable);
493 if ($res < 1) {
494 $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
495 return 0;
496 }
497
498 //Process depending of type
499 if ($this->type == 0 || $this->type == 1) {
500 //Get and check if required parameters are present
501 $parameters = json_decode($this->parameters, true);
502 if (!isset($parameters)) {
503 $this->error = $langs->trans("ErrorGlobalVariableUpdater1", $this->parameters);
504 return -1;
505 }
506 $url = $parameters['URL'];
507 if (!isset($url)) {
508 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'URL');
509 return -1;
510 }
511 $value = $parameters['VALUE'];
512 if (!isset($value)) {
513 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'VALUE');
514 return -1;
515 }
516 $result = "";
517 if ($this->type == 0) {
518 // Call JSON request
519 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
520 $tmpresult = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 0);
521 $code = $tmpresult['http_code'];
522 $result = $tmpresult['content'];
523
524 if (!isset($result)) {
525 $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response");
526 return -1;
527 }
528 if ($code !== 200) {
529 $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code.' '.$tmpresult['curl_error_msg']);
530 return -1;
531 }
532
533 //Decode returned response
534 $result = json_decode($result, true);
535 } elseif ($this->type == 1) {
536 $ns = $parameters['NS'];
537 if (!isset($ns)) {
538 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'NS');
539 return -1;
540 }
541 $method = $parameters['METHOD'];
542 if (!isset($method)) {
543 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'METHOD');
544 return -1;
545 }
546 $data = $parameters['DATA'];
547 if (!isset($data)) {
548 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'DATA');
549 return -1;
550 }
551
552 //SOAP client
553 require_once NUSOAP_PATH.'/nusoap.php';
554 $soap_client = new nusoap_client($url);
555 $soap_client->soap_defencoding = 'UTF-8';
556 $soap_client->decodeUTF8(false);
557 $result = $soap_client->call($method, $data, $ns, '');
558
559 //Check if result is a error
560 if ($result === false) {
561 $this->error = $langs->trans("ErrorGlobalVariableUpdater4", $soap_client->error_str);
562 return -1;
563 }
564 }
565
566 //Explode value and walk for each key in value array to get the relevant key
567 $value = explode(',', $value);
568 foreach ($value as $key) {
569 $result = $result[$key];
570 }
571 if (!isset($result)) {
572 $this->error = $langs->trans("ErrorGlobalVariableUpdater3");
573 return -1;
574 }
575
576 //Save data to global and update it
577 $price_globals->value = $result;
578 $price_globals->update($user);
579 }
580 return 1;
581 }
582
583 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
592 public function update_next_update($next_update, $user = null, $notrigger = 0)
593 {
594 // phpcs:enable
595 $error = 0;
596
597 $this->next_update = $next_update;
598 $this->checkParameters();
599
600 // Update request
601 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
602 $sql .= " next_update = ".((int) $this->next_update);
603 $sql .= " WHERE rowid = ".((int) $this->id);
604
605 $this->db->begin();
606
607 dol_syslog(__METHOD__);
608 $resql = $this->db->query($sql);
609 if (!$resql) {
610 $error++;
611 $this->errors[] = "Error ".$this->db->lasterror();
612 }
613
614 // Commit or rollback
615 if ($error) {
616 foreach ($this->errors as $errmsg) {
617 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
618 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
619 }
620 $this->db->rollback();
621 return -1 * $error;
622 } else {
623 $this->db->commit();
624 return 1;
625 }
626 }
627
628 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
637 public function update_status($last_status, $user = null, $notrigger = 0)
638 {
639 // phpcs:enable
640 $error = 0;
641
642 $this->last_status = $last_status;
643 $this->checkParameters();
644
645 // Update request
646 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
647 $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
648 $sql .= " WHERE rowid = ".((int) $this->id);
649
650 $this->db->begin();
651
652 dol_syslog(__METHOD__);
653 $resql = $this->db->query($sql);
654 if (!$resql) {
655 $error++;
656 $this->errors[] = "Error ".$this->db->lasterror();
657 }
658
659 // Commit or rollback
660 if ($error) {
661 foreach ($this->errors as $errmsg) {
662 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
663 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
664 }
665 $this->db->rollback();
666 return -1 * $error;
667 } else {
668 $this->db->commit();
669 return 1;
670 }
671 }
672}
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:149