47 public $errors = array();
78 public $table_element =
"c_price_global_variable_updater";
98 public function create($user, $notrigger = 0)
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).
",";
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).
"'" :
"''");
120 $resql = $this->db->query($sql);
123 $this->errors[] =
"Error ".$this->db->lasterror();
127 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
142 foreach ($this->errors as $errmsg) {
144 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
146 $this->db->rollback();
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);
168 $resql = $this->db->query($sql);
170 $obj = $this->db->fetch_object($resql);
173 $this->
type = $obj->type;
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;
186 $this->error =
"Error ".$this->db->lasterror();
198 public function update($user =
null, $notrigger = 0)
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);
218 $resql = $this->db->query($sql);
221 $this->errors[] =
"Error ".$this->db->lasterror();
240 foreach ($this->errors as $errmsg) {
241 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
242 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
244 $this->db->rollback();
260 public function delete($rowid, $user, $notrigger = 0)
281 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
282 $sql .=
" WHERE rowid = ".((int) $rowid);
285 $resql = $this->db->query($sql);
288 $this->errors[] =
"Error ".$this->db->lasterror();
294 foreach ($this->errors as $errmsg) {
296 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
298 $this->db->rollback();
317 $this->parameters =
'';
318 $this->fk_variable = 0;
319 $this->update_interval = 0;
320 $this->next_update = 0;
321 $this->last_status =
'';
334 $last = $this->next_update - ($this->update_interval * 60);
336 return $langs->trans(
"Never");
338 $status = empty($this->last_status) ? $langs->trans(
"CorrectlyUpdated") : $this->last_status;
353 if (isset($this->parameters)) {
354 $this->parameters = trim($this->parameters);
356 $this->parameters =
"";
358 if (isset($this->last_status)) {
359 $this->last_status = trim($this->last_status);
363 if (empty($this->
type) || !is_numeric($this->
type) || !in_array($this->
type, $this->types)) {
366 if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) {
369 if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) {
370 $this->next_update = 0;
381 $sql =
"SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
382 $sql .=
" FROM ".$this->db->prefix().$this->table_element;
385 $resql = $this->db->query($sql);
389 while ($record = $this->db->fetch_array($resql)) {
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;
403 $this->db->free($resql);
406 $this->error = $this->db->error();
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();
423 $resql = $this->db->query($sql);
427 while ($record = $this->db->fetch_array($resql)) {
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;
441 $this->db->free($resql);
444 $this->error = $this->db->error();
456 global $langs, $user;
457 $langs->load(
"errors");
464 if ($this->fk_variable < 1) {
465 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater5");
469 $res = $price_globals->fetch($this->fk_variable);
471 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater5");
476 if ($this->
type == 0 || $this->
type == 1) {
478 $parameters = json_decode($this->parameters,
true);
479 if (!isset($parameters)) {
480 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater1", $this->parameters);
483 $url = $parameters[
'URL'];
485 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'URL');
488 $value = $parameters[
'VALUE'];
489 if (!isset($value)) {
490 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'VALUE');
494 if ($this->
type == 0) {
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'];
501 if (!isset($result)) {
502 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater0",
"empty response");
506 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater0", $code.
' '.$tmpresult[
'curl_error_msg']);
511 $result = json_decode($result,
true);
512 } elseif ($this->
type == 1) {
513 $ns = $parameters[
'NS'];
515 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'NS');
518 $method = $parameters[
'METHOD'];
519 if (!isset($method)) {
520 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'METHOD');
523 $data = $parameters[
'DATA'];
525 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'DATA');
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,
'');
537 if ($result ===
false) {
538 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater4", $soap_client->error_str);
544 $value = explode(
',', $value);
545 foreach ($value as $key) {
546 $result = $result[$key];
548 if (!isset($result)) {
549 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater3");
554 $price_globals->value = $result;
555 $price_globals->update($user);
578 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
579 $sql .=
" next_update = ".$this->next_update;
580 $sql .=
" WHERE rowid = ".((int) $this->
id);
585 $resql = $this->db->query($sql);
588 $this->errors[] =
"Error ".$this->db->lasterror();
593 foreach ($this->errors as $errmsg) {
595 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
597 $this->db->rollback();
619 $this->last_status = $last_status;
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);
630 $resql = $this->db->query($sql);
633 $this->errors[] =
"Error ".$this->db->lasterror();
638 foreach ($this->errors as $errmsg) {
640 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
642 $this->db->rollback();
Class for accessing price global variables table.
Class for price global variable updaters table.
__construct($db)
Constructor.
update_status($last_status, $user=null, $notrigger=0)
Update last_status into database.
$update_min
Minimal update rate.
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.
$update_interval
Interval in mins.
$next_update
Next update timestamp.
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