dolibarr 19.0.4
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++;
121 $this->errors[] = "Error ".$this->db->lasterror();
122 }
123
124 if (!$error) {
125 $this->id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
126
127 if (!$notrigger) {
128 // Uncomment this and change MYOBJECT to your own tag if you
129 // want this action calls a trigger.
130
132 //$result=$this->call_trigger('MYOBJECT_CREATE',$user);
133 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
135 }
136 }
137
138 // Commit or rollback
139 if ($error) {
140 foreach ($this->errors as $errmsg) {
141 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
142 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
143 }
144 $this->db->rollback();
145 return -1 * $error;
146 } else {
147 $this->db->commit();
148 return $this->id;
149 }
150 }
151
152
159 public function fetch($id)
160 {
161 $sql = "SELECT type, description, parameters, fk_variable, update_interval, next_update, last_status";
162 $sql .= " FROM ".$this->db->prefix().$this->table_element;
163 $sql .= " WHERE rowid = ".((int) $id);
164
165 dol_syslog(__METHOD__);
166 $resql = $this->db->query($sql);
167 if ($resql) {
168 $obj = $this->db->fetch_object($resql);
169 if ($obj) {
170 $this->id = $id;
171 $this->type = $obj->type;
172 $this->description = $obj->description;
173 $this->parameters = $obj->parameters;
174 $this->fk_variable = $obj->fk_variable;
175 $this->update_interval = $obj->update_interval;
176 $this->next_update = $obj->next_update;
177 $this->last_status = $obj->last_status;
178 $this->checkParameters();
179 return 1;
180 } else {
181 return 0;
182 }
183 } else {
184 $this->error = "Error ".$this->db->lasterror();
185 return -1;
186 }
187 }
188
196 public function update($user = 0, $notrigger = 0)
197 {
198 $error = 0;
199
200 $this->checkParameters();
201
202 // Update request
203 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
204 $sql .= " type = ".((int) $this->type).",";
205 $sql .= " description = ".(isset($this->description) ? "'".$this->db->escape($this->description)."'" : "''").",";
206 $sql .= " parameters = ".(isset($this->parameters) ? "'".$this->db->escape($this->parameters)."'" : "''").",";
207 $sql .= " fk_variable = ".((int) $this->fk_variable).",";
208 $sql .= " update_interval = ".((int) $this->update_interval).",";
209 $sql .= " next_update = ".((int) $this->next_update).",";
210 $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
211 $sql .= " WHERE rowid = ".((int) $this->id);
212
213 $this->db->begin();
214
215 dol_syslog(__METHOD__);
216 $resql = $this->db->query($sql);
217 if (!$resql) {
218 $error++;
219 $this->errors[] = "Error ".$this->db->lasterror();
220 }
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 foreach ($this->errors as $errmsg) {
239 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
240 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
241 }
242 $this->db->rollback();
243 return -1 * $error;
244 } else {
245 $this->db->commit();
246 return 1;
247 }
248 }
249
258 public function delete($rowid, $user, $notrigger = 0)
259 {
260 $error = 0;
261
262 $this->db->begin();
263
264 //if (! $error)
265 //{
266 // if (! $notrigger)
267 // {
268 // Uncomment this and change MYOBJECT to your own tag if you
269 // want this action calls a trigger.
270
272 //$result=$this->call_trigger('MYOBJECT_DELETE',$user);
273 //if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
275 // }
276 //}
277
278 if (!$error) {
279 $sql = "DELETE FROM ".$this->db->prefix().$this->table_element;
280 $sql .= " WHERE rowid = ".((int) $rowid);
281
282 dol_syslog(__METHOD__);
283 $resql = $this->db->query($sql);
284 if (!$resql) {
285 $error++;
286 $this->errors[] = "Error ".$this->db->lasterror();
287 }
288 }
289
290 // Commit or rollback
291 if ($error) {
292 foreach ($this->errors as $errmsg) {
293 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
294 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
295 }
296 $this->db->rollback();
297 return -1 * $error;
298 } else {
299 $this->db->commit();
300 return 1;
301 }
302 }
303
310 public function initAsSpecimen()
311 {
312 $this->id = 0;
313 $this->type = 0;
314 $this->description = '';
315 $this->parameters = '';
316 $this->fk_variable = 0;
317 $this->update_interval = 0;
318 $this->next_update = 0;
319 $this->last_status = '';
320 }
321
327 public function getLastUpdated()
328 {
329 global $langs;
330 $last = $this->next_update - ($this->update_interval * 60);
331 if ($last < 1) {
332 return $langs->trans("Never");
333 }
334 $status = empty($this->last_status) ? $langs->trans("CorrectlyUpdated") : $this->last_status;
335 return $status.'<br>'.dol_print_date($last, '%d/%m/%Y %H:%M:%S');
336 }
337
343 public function checkParameters()
344 {
345 // Clean parameters
346 if (isset($this->description)) {
347 $this->description = trim($this->description);
348 }
349 if (isset($this->parameters)) {
350 $this->parameters = trim($this->parameters);
351 } else {
352 $this->parameters = "";
353 }
354 if (isset($this->last_status)) {
355 $this->last_status = trim($this->last_status);
356 }
357
358 // Check parameters
359 if (empty($this->type) || !is_numeric($this->type) || !in_array($this->type, $this->types)) {
360 $this->type = 0;
361 }
362 if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) {
363 $this->update_interval = $this->update_min;
364 }
365 if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) {
366 $this->next_update = 0;
367 }
368 }
369
375 public function listUpdaters()
376 {
377 $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
378 $sql .= " FROM ".$this->db->prefix().$this->table_element;
379
380 dol_syslog(__METHOD__, LOG_DEBUG);
381 $resql = $this->db->query($sql);
382 if ($resql) {
383 $retarray = array();
384
385 while ($record = $this->db->fetch_array($resql)) {
386 $updater_obj = new PriceGlobalVariableUpdater($this->db);
387 $updater_obj->id = $record["rowid"];
388 $updater_obj->type = $record["type"];
389 $updater_obj->description = $record["description"];
390 $updater_obj->parameters = $record["parameters"];
391 $updater_obj->fk_variable = $record["fk_variable"];
392 $updater_obj->update_interval = $record["update_interval"];
393 $updater_obj->next_update = $record["next_update"];
394 $updater_obj->last_status = $record["last_status"];
395 $updater_obj->checkParameters();
396 $retarray[] = $updater_obj;
397 }
398
399 $this->db->free($resql);
400 return $retarray;
401 } else {
402 $this->error = $this->db->error();
403 return -1;
404 }
405 }
406
412 public function listPendingUpdaters()
413 {
414 $sql = "SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
415 $sql .= " FROM ".$this->db->prefix().$this->table_element;
416 $sql .= " WHERE next_update < ".dol_now();
417
418 dol_syslog(__METHOD__, LOG_DEBUG);
419 $resql = $this->db->query($sql);
420 if ($resql) {
421 $retarray = array();
422
423 while ($record = $this->db->fetch_array($resql)) {
424 $updater_obj = new PriceGlobalVariableUpdater($this->db);
425 $updater_obj->id = $record["rowid"];
426 $updater_obj->type = $record["type"];
427 $updater_obj->description = $record["description"];
428 $updater_obj->parameters = $record["parameters"];
429 $updater_obj->fk_variable = $record["fk_variable"];
430 $updater_obj->update_interval = $record["update_interval"];
431 $updater_obj->next_update = $record["next_update"];
432 $updater_obj->last_status = $record["last_status"];
433 $updater_obj->checkParameters();
434 $retarray[] = $updater_obj;
435 }
436
437 $this->db->free($resql);
438 return $retarray;
439 } else {
440 $this->error = $this->db->error();
441 return -1;
442 }
443 }
444
450 public function process()
451 {
452 global $langs, $user;
453 $langs->load("errors");
454 dol_syslog(__METHOD__, LOG_DEBUG);
455
456 $this->error = null;
457 $this->checkParameters();
458
459 //Try to load the target global variable and abort if fails
460 if ($this->fk_variable < 1) {
461 $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
462 return 0;
463 }
464 $price_globals = new PriceGlobalVariable($this->db);
465 $res = $price_globals->fetch($this->fk_variable);
466 if ($res < 1) {
467 $this->error = $langs->trans("ErrorGlobalVariableUpdater5");
468 return 0;
469 }
470
471 //Process depending of type
472 if ($this->type == 0 || $this->type == 1) {
473 //Get and check if required parameters are present
474 $parameters = json_decode($this->parameters, true);
475 if (!isset($parameters)) {
476 $this->error = $langs->trans("ErrorGlobalVariableUpdater1", $this->parameters);
477 return -1;
478 }
479 $url = $parameters['URL'];
480 if (!isset($url)) {
481 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'URL');
482 return -1;
483 }
484 $value = $parameters['VALUE'];
485 if (!isset($value)) {
486 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'VALUE');
487 return -1;
488 }
489 $result = "";
490 if ($this->type == 0) {
491 // Call JSON request
492 include_once DOL_DOCUMENT_ROOT.'/core/lib/geturl.lib.php';
493 $tmpresult = getURLContent($url, 'GET', '', 1, array(), array('http', 'https'), 0);
494 $code = $tmpresult['http_code'];
495 $result = $tmpresult['content'];
496
497 if (!isset($result)) {
498 $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response");
499 return -1;
500 }
501 if ($code !== 200) {
502 $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code.' '.$tmpresult['curl_error_msg']);
503 return -1;
504 }
505
506 //Decode returned response
507 $result = json_decode($result, true);
508 } elseif ($this->type == 1) {
509 $ns = $parameters['NS'];
510 if (!isset($ns)) {
511 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'NS');
512 return -1;
513 }
514 $method = $parameters['METHOD'];
515 if (!isset($method)) {
516 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'METHOD');
517 return -1;
518 }
519 $data = $parameters['DATA'];
520 if (!isset($data)) {
521 $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'DATA');
522 return -1;
523 }
524
525 //SOAP client
526 require_once NUSOAP_PATH.'/nusoap.php';
527 $soap_client = new nusoap_client($url);
528 $soap_client->soap_defencoding = 'UTF-8';
529 $soap_client->decodeUTF8(false);
530 $result = $soap_client->call($method, $data, $ns, '');
531
532 //Check if result is a error
533 if ($result === false) {
534 $this->error = $langs->trans("ErrorGlobalVariableUpdater4", $soap_client->error_str);
535 return -1;
536 }
537 }
538
539 //Explode value and walk for each key in value array to get the relevant key
540 $value = explode(',', $value);
541 foreach ($value as $key) {
542 $result = $result[$key];
543 }
544 if (!isset($result)) {
545 $this->error = $langs->trans("ErrorGlobalVariableUpdater3");
546 return -1;
547 }
548
549 //Save data to global and update it
550 $price_globals->value = $result;
551 $price_globals->update($user);
552 }
553 return 1;
554 }
555
556 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
565 public function update_next_update($next_update, $user = 0, $notrigger = 0)
566 {
567 // phpcs:enable
568 $error = 0;
569
570 $this->next_update = $next_update;
571 $this->checkParameters();
572
573 // Update request
574 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
575 $sql .= " next_update = ".$this->next_update;
576 $sql .= " WHERE rowid = ".((int) $this->id);
577
578 $this->db->begin();
579
580 dol_syslog(__METHOD__);
581 $resql = $this->db->query($sql);
582 if (!$resql) {
583 $error++;
584 $this->errors[] = "Error ".$this->db->lasterror();
585 }
586
587 // Commit or rollback
588 if ($error) {
589 foreach ($this->errors as $errmsg) {
590 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
591 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
592 }
593 $this->db->rollback();
594 return -1 * $error;
595 } else {
596 $this->db->commit();
597 return 1;
598 }
599 }
600
601 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
610 public function update_status($last_status, $user = 0, $notrigger = 0)
611 {
612 // phpcs:enable
613 $error = 0;
614
615 $this->last_status = $last_status;
616 $this->checkParameters();
617
618 // Update request
619 $sql = "UPDATE ".$this->db->prefix().$this->table_element." SET";
620 $sql .= " last_status = ".(isset($this->last_status) ? "'".$this->db->escape($this->last_status)."'" : "''");
621 $sql .= " WHERE rowid = ".((int) $this->id);
622
623 $this->db->begin();
624
625 dol_syslog(__METHOD__);
626 $resql = $this->db->query($sql);
627 if (!$resql) {
628 $error++;
629 $this->errors[] = "Error ".$this->db->lasterror();
630 }
631
632 // Commit or rollback
633 if ($error) {
634 foreach ($this->errors as $errmsg) {
635 dol_syslog(__METHOD__." ".$errmsg, LOG_ERR);
636 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
637 }
638 $this->db->rollback();
639 return -1 * $error;
640 } else {
641 $this->db->commit();
642 return 1;
643 }
644 }
645}
Class for accesing price global variables table.
Class for price global variable updaters table.
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_status($last_status, $user=0, $notrigger=0)
Update last_status into database.
update($user=0, $notrigger=0)
Update object into database.
update_next_update($next_update, $user=0, $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.
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='', $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:121