45 public $errors = array();
76 public $table_element =
"c_price_global_variable_updater";
96 public function create($user, $notrigger = 0)
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).
",";
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).
"'" :
"''");
118 $resql = $this->db->query($sql);
121 $this->errors[] =
"Error ".$this->db->lasterror();
125 $this->
id = $this->db->last_insert_id($this->db->prefix().$this->table_element);
140 foreach ($this->errors as $errmsg) {
142 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
144 $this->db->rollback();
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);
166 $resql = $this->db->query($sql);
168 $obj = $this->db->fetch_object($resql);
171 $this->
type = $obj->type;
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;
184 $this->error =
"Error ".$this->db->lasterror();
196 public function update($user = 0, $notrigger = 0)
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);
216 $resql = $this->db->query($sql);
219 $this->errors[] =
"Error ".$this->db->lasterror();
238 foreach ($this->errors as $errmsg) {
239 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
240 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
242 $this->db->rollback();
258 public function delete($rowid, $user, $notrigger = 0)
279 $sql =
"DELETE FROM ".$this->db->prefix().$this->table_element;
280 $sql .=
" WHERE rowid = ".((int) $rowid);
283 $resql = $this->db->query($sql);
286 $this->errors[] =
"Error ".$this->db->lasterror();
292 foreach ($this->errors as $errmsg) {
294 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
296 $this->db->rollback();
315 $this->parameters =
'';
316 $this->fk_variable = 0;
317 $this->update_interval = 0;
318 $this->next_update = 0;
319 $this->last_status =
'';
330 $last = $this->next_update - ($this->update_interval * 60);
332 return $langs->trans(
"Never");
334 $status = empty($this->last_status) ? $langs->trans(
"CorrectlyUpdated") : $this->last_status;
349 if (isset($this->parameters)) {
350 $this->parameters = trim($this->parameters);
352 $this->parameters =
"";
354 if (isset($this->last_status)) {
355 $this->last_status = trim($this->last_status);
359 if (empty($this->
type) || !is_numeric($this->
type) || !in_array($this->
type, $this->types)) {
362 if (empty($this->update_interval) || !is_numeric($this->update_interval) || $this->update_interval < 1) {
365 if (empty($this->next_update) || !is_numeric($this->next_update) || $this->next_update < 0) {
366 $this->next_update = 0;
377 $sql =
"SELECT rowid, type, description, parameters, fk_variable, update_interval, next_update, last_status";
378 $sql .=
" FROM ".$this->db->prefix().$this->table_element;
381 $resql = $this->db->query($sql);
385 while ($record = $this->db->fetch_array($resql)) {
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;
399 $this->db->free($resql);
402 $this->error = $this->db->error();
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();
419 $resql = $this->db->query($sql);
423 while ($record = $this->db->fetch_array($resql)) {
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;
437 $this->db->free($resql);
440 $this->error = $this->db->error();
452 global $langs, $user;
453 $langs->load(
"errors");
460 if ($this->fk_variable < 1) {
461 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater5");
465 $res = $price_globals->fetch($this->fk_variable);
467 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater5");
472 if ($this->
type == 0 || $this->
type == 1) {
474 $parameters = json_decode($this->parameters,
true);
475 if (!isset($parameters)) {
476 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater1", $this->parameters);
479 $url = $parameters[
'URL'];
481 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'URL');
484 $value = $parameters[
'VALUE'];
485 if (!isset($value)) {
486 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'VALUE');
490 if ($this->
type == 0) {
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'];
497 if (!isset($result)) {
498 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater0",
"empty response");
502 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater0", $code.
' '.$tmpresult[
'curl_error_msg']);
507 $result = json_decode($result,
true);
508 } elseif ($this->
type == 1) {
509 $ns = $parameters[
'NS'];
511 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'NS');
514 $method = $parameters[
'METHOD'];
515 if (!isset($method)) {
516 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'METHOD');
519 $data = $parameters[
'DATA'];
521 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater2",
'DATA');
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,
'');
533 if ($result ===
false) {
534 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater4", $soap_client->error_str);
540 $value = explode(
',', $value);
541 foreach ($value as $key) {
542 $result = $result[$key];
544 if (!isset($result)) {
545 $this->error = $langs->trans(
"ErrorGlobalVariableUpdater3");
550 $price_globals->value = $result;
551 $price_globals->update($user);
574 $sql =
"UPDATE ".$this->db->prefix().$this->table_element.
" SET";
575 $sql .=
" next_update = ".$this->next_update;
576 $sql .=
" WHERE rowid = ".((int) $this->
id);
581 $resql = $this->db->query($sql);
584 $this->errors[] =
"Error ".$this->db->lasterror();
589 foreach ($this->errors as $errmsg) {
591 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
593 $this->db->rollback();
615 $this->last_status = $last_status;
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);
626 $resql = $this->db->query($sql);
629 $this->errors[] =
"Error ".$this->db->lasterror();
634 foreach ($this->errors as $errmsg) {
636 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
638 $this->db->rollback();
Class for accesing price global variables table.
Class for price global variable updaters table.
__construct($db)
Constructor.
$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_interval
Interval in mins.
update_status($last_status, $user=0, $notrigger=0)
Update last_status into database.
$next_update
Next update timestamp.
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