require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_global_variable.class.php'; require_once DOL_DOCUMENT_ROOT . '/product/dynamic_price/class/price_global_variable_updater.class.php'; $langs->load("products"); $id = GETPOST('id', 'int'); $action = GETPOST('action', 'alpha'); $save = GETPOST('save', 'alpha'); $cancel = GETPOST('cancel', 'alpha'); $selection = GETPOST('selection', 'int'); // Security check if (!$user->admin) { accessforbidden(); } //Objects $price_globals = new PriceGlobalVariable($db); if ($action == 'edit_variable') { $res = $price_globals->fetch($selection); if ($res < 1) { setEventMessage($price_globals->error, 'errors'); } } $price_updaters = new PriceGlobalVariableUpdater($db); if ($action == 'edit_updater') { $res = $price_updaters->fetch($selection); if ($res < 1) { setEventMessage($price_updaters->error, 'errors'); } } /* * Actions */ if (!empty($action) && empty($cancel)) {
/** * Handles the processing of this updater * * @return int <0 if KO, 0 if OK but no global variable found, >0 if OK */ function process() { global $langs, $user; $langs->load("errors"); $this->error = null; $this->checkParameters(); //Try to load the target global variable and abort if fails if ($this->fk_variable < 1) { $this->error = $langs->trans("ErrorGlobalVariableUpdater5"); return 0; } $price_globals = new PriceGlobalVariable($this->db); $res = $price_globals->fetch($this->fk_variable); if ($res < 1) { $this->error = $langs->trans("ErrorGlobalVariableUpdater5"); return 0; } //Process depending of type if ($this->type == 0 || $this->type == 1) { //Get and check if required parameters are present $parameters = json_decode($this->parameters, true); if (!isset($parameters)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater1", $this->parameters); return -1; } $url = $parameters['URL']; if (!isset($url)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'URL'); return -1; } $value = $parameters['VALUE']; if (!isset($value)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'VALUE'); return -1; } $result = ""; if ($this->type == 0) { //CURL client $handle = curl_init(); curl_setopt_array($handle, array(CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 5, CURLOPT_POST => false, CURLOPT_HEADER => false)); $result = curl_exec($handle); $code = curl_getinfo($handle, CURLINFO_HTTP_CODE); if (!isset($result)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater0", "empty response"); return -1; } if ($code !== 200) { $this->error = $langs->trans("ErrorGlobalVariableUpdater0", $code); return -1; } //Decode returned response $result = json_decode($result, true); } elseif ($this->type == 1) { $ns = $parameters['NS']; if (!isset($ns)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'NS'); return -1; } $method = $parameters['METHOD']; if (!isset($method)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'METHOD'); return -1; } $data = $parameters['DATA']; if (!isset($data)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater2", 'DATA'); return -1; } //SOAP client require_once NUSOAP_PATH . '/nusoap.php'; $soap_client = new nusoap_client($url); $soap_client->soap_defencoding = 'UTF-8'; $soap_client->decodeUTF8(false); $result = $soap_client->call($method, $data, $ns, ''); //Check if result is a error if ($result === false) { $this->error = $langs->trans("ErrorGlobalVariableUpdater4", $soap_client->error_str); return -1; } } //Explode value and walk for each key in value array to get the relevant key $value = explode(',', $value); foreach ($value as $key) { $result = $result[$key]; } if (!isset($result)) { $this->error = $langs->trans("ErrorGlobalVariableUpdater3"); return -1; } //Save data to global and update it $price_globals->value = $result; $price_globals->update($user); } return 1; }