/** * Prepare query string and request body using variables to be sent in get and request_structure to be sent as JSON in body * from method definition. * * @param bool $params Receives parameters to be used in GET and scope array to create JSON body after parsing $_definition['request_structure'] object * * @return array|bool Returns an array with data to be used in request to server or false on error */ public function prepare_for_request($params = false) { if (!$this->validate_definition()) { return false; } if (empty($params) or !is_array($params)) { $params = array(); } if (empty($params['skip_regexps'])) { $params['skip_regexps'] = false; } else { $params['skip_regexps'] = !empty($params['skip_regexps']) ? true : false; } if (empty($params['allow_remote_calls'])) { $params['allow_remote_calls'] = false; } if (empty($params['get_variables']) or !is_array($params['get_variables'])) { $params['get_variables'] = array(); } if (empty($params['method_params']) or !is_array($params['method_params'])) { $params['method_params'] = array(); } if (empty($params['custom_validators']) or !is_array($params['custom_validators'])) { $params['custom_validators'] = array(); } if ($internal_params = $this->request_parameters()) { if (!empty($internal_params['get_variables']) and is_array($internal_params['get_variables'])) { $params['get_variables'] = array_merge($internal_params['get_variables'], $params['get_variables']); } if (!empty($internal_params['method_params']) and is_array($internal_params['method_params'])) { $params['method_params'] = array_merge($internal_params['method_params'], $params['method_params']); } } $return_arr = array(); $return_arr['func'] = $this->_functionality; $return_arr['full_query'] = $this->_definition['url_suffix']; $return_arr['http_method'] = $this->_definition['http_method']; $return_arr['query_string'] = ''; $return_arr['url_variables'] = array(); $return_arr['get_variables'] = array(); $return_arr['request_body'] = ''; if (!empty($this->_definition['get_variables']) and is_array($this->_definition['get_variables'])) { $value_source_obj = new S2P_SDK_Values_Source(); if (!empty($params['allow_remote_calls'])) { $value_source_obj->remote_calls(true); } else { $value_source_obj->remote_calls(false); } foreach ($this->_definition['get_variables'] as $get_var) { if (!array_key_exists($get_var['name'], $params['get_variables'])) { if (!empty($get_var['mandatory'])) { $this->set_error(self::ERR_MANDATORY, self::s2p_t('Variable %s is mandatory for method %s.', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name'], $this->_definition['name'])); return false; } continue; } $var_value = S2P_SDK_Scope_Variable::scalar_value($get_var['type'], $params['get_variables'][$get_var['name']], $get_var['array_type'], $get_var['array_numeric_keys']); $default_var_value = null; if (array_key_exists('default', $get_var)) { $default_var_value = S2P_SDK_Scope_Variable::scalar_value($get_var['type'], $get_var['default'], $get_var['array_type'], $get_var['array_numeric_keys']); } if (!empty($get_var['skip_if_default']) and $var_value === $default_var_value) { continue; } if (empty($params['skip_regexps']) and !empty($get_var['regexp']) and !preg_match('/' . $get_var['regexp'] . '/', $var_value)) { $this->set_error(self::ERR_REGEXP, self::s2p_t('Get variable %s is invalid.', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name']), sprintf('Get variable [%s] failed regular exp [%s].', $get_var['name'], $get_var['regexp'])); return false; } if (!empty($get_var['value_source']) and $value_source_obj::valid_type($get_var['value_source'])) { $value_source_obj->source_type($get_var['value_source']); if (!$value_source_obj->valid_value($var_value)) { $this->set_error(self::ERR_VALUE_SOURCE, self::s2p_t('Variable %s contains invalid value [%s].', !empty($get_var['display_name']) ? $get_var['display_name'] : $get_var['name'], $var_value)); return false; } } if (!empty($get_var['move_in_url'])) { $return_arr['url_variables'][$get_var['external_name']] = $var_value; } else { $return_arr['get_variables'][$get_var['external_name']] = $var_value; } } } // Replace any URL variables, even if we don't currently have $return_arr['url_variables'] set if ($replacement_result = $this->replace_url_variables($return_arr['full_query'], $return_arr['url_variables'])) { if (!empty($replacement_result['url_variables']) and is_array($replacement_result['url_variables'])) { $return_arr['get_variables'] = array_merge($return_arr['get_variables'], $replacement_result['url_variables']); } $return_arr['full_query'] = $replacement_result['url']; } if (!empty($return_arr['get_variables'])) { $return_arr['query_string'] = http_build_query($return_arr['get_variables']); } if (!empty($return_arr['query_string'])) { if (strstr($return_arr['full_query'], '?') === false) { $return_arr['full_query'] .= '?'; } $return_arr['full_query'] .= $return_arr['query_string']; } if (!empty($this->_definition['request_structure'])) { /** @var S2P_SDK_Scope_Structure $request_structure */ $request_structure = $this->_definition['request_structure']; $request_to_array_params = array(); $request_to_array_params['output_null_values'] = false; $request_to_array_params['nullify_full_object'] = false; if (!($json_array = $request_structure->prepare_info_for_request_to_array($params['method_params'], $request_to_array_params)) or !is_array($json_array)) { if ($parsing_error = $request_structure->get_parsing_error()) { $this->copy_error_from_array($parsing_error); } else { $this->set_error(self::ERR_REQUEST_DATA, self::s2p_t('Couldn\'t extract request data or request data is empty.')); } return false; } if (!empty($this->_definition['mandatory_in_request']) and is_array($this->_definition['mandatory_in_request'])) { if (!$this->check_mandatory_fields($json_array, $this->_definition['mandatory_in_request'], array('scope_arr_type' => 'request', 'structure_obj' => $request_structure))) { if (!$this->has_error()) { $this->set_error(self::ERR_REQUEST_MANDATORY, self::s2p_t('Mandatory fields not found in request.')); } return false; } } if (!empty($this->_definition['hide_in_request']) and is_array($this->_definition['hide_in_request'])) { $json_array = $this->remove_fields($json_array, $this->_definition['hide_in_request'], array('scope_arr_type' => 'request')); } $return_arr['request_body'] = @json_encode($json_array); } return $return_arr; }
public function get_form_method_parameters_fields_detailed($structure_definition, $mandatory_arr, $hide_keys_arr, $post_arr, $form_arr, $params = false) { if (empty($params) or !is_array($params)) { $params = array(); } if (empty($params['path'])) { $params['path'] = ''; } if (empty($params['read_path'])) { $params['read_path'] = ''; } if (empty($params['name'])) { $params['name'] = ''; } if (!isset($params['blob_array_index'])) { $params['blob_array_index'] = false; } if (!isset($params['blob_array_index_read'])) { $params['blob_array_index_read'] = false; } if (empty($params['level'])) { $params['level'] = -1; } if (empty($params['blob_array_index_read']) and !empty($params['blob_array_index'])) { $params['blob_array_index_read'] = $params['blob_array_index']; } if (empty($mandatory_arr) or !is_array($mandatory_arr)) { $mandatory_arr = array(); } if (empty($hide_keys_arr) or !is_array($hide_keys_arr)) { $hide_keys_arr = array(); } if (empty($structure_definition) or !is_array($structure_definition) or array_key_exists($structure_definition['name'], $hide_keys_arr) and !is_array($hide_keys_arr[$structure_definition['name']])) { return ''; } $params['path'] .= (!empty($params['path']) ? '.' : '') . ($params['blob_array_index'] !== false ? $params['blob_array_index'] . '.' : '') . $structure_definition['name']; $params['read_path'] .= (!empty($params['read_path']) ? '.' : '') . ($params['blob_array_index_read'] !== false ? $params['blob_array_index_read'] . '.' : '') . $structure_definition['name']; $params['name'] .= ($params['blob_array_index'] !== false ? '[' . $params['blob_array_index'] . ']' : '') . '[' . $structure_definition['name'] . ']'; $params['level']++; if (empty($structure_definition['structure']) or !is_array($structure_definition['structure'])) { // display single element... $field_id = str_replace(array('.', '[', ']'), '_', $params['path']); $field_name = 'mparams' . $params['name']; $field_value = self::extract_field_value($post_arr['mparams'], $params['read_path']); if ($field_value === null) { if (!empty($structure_definition['check_constant']) and defined($structure_definition['check_constant'])) { $field_value = constant($structure_definition['check_constant']); } else { $field_value = ''; } } $field_mandatory = false; if (array_key_exists($structure_definition['name'], $mandatory_arr)) { $field_mandatory = true; } ob_start(); ?> <div class="form_field"> <label for="<?php echo $field_id; ?> " title="<?php echo (!empty($structure_definition['display_name']) ? $structure_definition['display_name'] . ' - ' : '') . self::form_str($params['path']); ?> " class="<?php echo !empty($field_mandatory) ? 'mandatory' : ''; ?> "><?php echo $structure_definition['name']; ?> </label> <div class="form_input"><?php if (empty($structure_definition['value_source']) or !S2P_SDK_Values_Source::valid_type($structure_definition['value_source']) or !($value_source_obj = new S2P_SDK_Values_Source($structure_definition['value_source'])) or $value_source_obj->remote_calls(self::ALLOW_REMOTE_CALLS) and false or !($options_value = $value_source_obj->get_option_values()) or !is_array($options_value)) { $options_value = array(); } if ($structure_definition['type'] == S2P_SDK_Scope_Variable::TYPE_BOOL) { ?> <input type="checkbox" id="<?php echo $field_id; ?> " name="<?php echo $field_name; ?> " value="1" <?php echo !empty($field_value) ? 'checked="checked"' : ''; ?> /><?php } elseif ($structure_definition['type'] == S2P_SDK_Scope_Variable::TYPE_DATETIME) { ?> <input type="text" id="<?php echo $field_id; ?> " name="<?php echo $field_name; ?> " value="<?php echo self::form_str($field_value); ?> " class="datepicker" /><?php } elseif ($structure_definition['type'] == S2P_SDK_Scope_Variable::TYPE_ARRAY) { if (empty($field_value) or !is_array($field_value)) { $field_value = array(); } if (empty($field_value['keys']) or !is_array($field_value['keys'])) { $field_value['keys'] = array(); } if (empty($field_value['vals']) or !is_array($field_value['vals'])) { $field_value['vals'] = array(); } ?> <input type="hidden" name="mparams_arrays[]" value="<?php echo self::form_str($params['path']); ?> " /> <div id="<?php echo $field_id; ?> ___container"> <?php foreach ($field_value['vals'] as $key => $val) { $field_key = ''; if (!empty($field_value['keys'][$key])) { $field_key = $field_value['keys'][$key]; } ?> <div class="form_input_array"> <?php if (empty($structure_definition['array_numeric_keys'])) { ?> <input type="text" name="<?php echo $field_name; ?> [keys][]" value="<?php echo self::form_str($field_key); ?> " placeholder="<?php echo self::form_str(self::s2p_t('Key')); ?> " /><?php } $options_params = array(); $options_params['field_id'] = false; $options_params['field_name'] = $field_name . '[vals][]'; $options_params['field_value'] = $val; if (!empty($options_value) and ($select_buf = self::display_select_options($options_value, $options_params)) !== false) { echo $select_buf; } else { if (empty($structure_definition['array_type']) or !($field_type_arr = S2P_SDK_Scope_Variable::valid_type($structure_definition['array_type']))) { $field_type_arr = array('title' => 'string'); } ?> <input type="text" name="<?php echo $field_name; ?> [vals][]" value="<?php echo self::form_str($val); ?> " placeholder="<?php echo self::form_str(self::s2p_t('Value')); ?> " /> (<?php echo $field_type_arr['title']; ?> ) <?php } ?> <a href="javascript:void(0);" onclick="remove_methods_array_element( $(this), '<?php echo $field_id; ?> ' )"><?php echo self::s2p_t('Remove'); ?> </a> </div> <?php } ?> </div> <div class="form_input_array input_disabler_container" id="<?php echo $field_id; ?> ___template" style="display: none;"> <?php if (empty($structure_definition['array_numeric_keys'])) { ?> <input type="text" name="<?php echo $field_name; ?> [keys][]" value="" placeholder="<?php echo self::form_str(self::s2p_t('Key')); ?> " /><?php } $options_params = array(); $options_params['field_id'] = false; $options_params['field_name'] = $field_name . '[vals][]'; $options_params['field_value'] = ''; if (!empty($options_value) and ($select_buf = self::display_select_options($options_value, $options_params)) !== false) { echo $select_buf; } else { if (empty($structure_definition['array_type']) or !($field_type_arr = S2P_SDK_Scope_Variable::valid_type($structure_definition['array_type']))) { $field_type_arr = array('title' => 'string'); } ?> <input type="text" name="<?php echo $field_name; ?> [vals][]" value="" placeholder="<?php echo self::form_str(self::s2p_t('Value')); ?> " /> (<?php echo $field_type_arr['title']; ?> ) <?php } ?> <a href="javascript:void(0);" onclick="remove_methods_array_element( $(this), '<?php echo $field_id; ?> ' )"><?php echo self::s2p_t('Remove'); ?> </a> </div> <div id="<?php echo $field_id; ?> " class="field_adder_container"><a href="javascript:void(0);" onclick="add_methods_array_element( '<?php echo $field_id; ?> ' )"><?php echo self::s2p_t('Add value'); ?> </a></div> <?php } else { $options_params = array(); $options_params['field_id'] = $field_id; $options_params['field_name'] = $field_name; $options_params['field_value'] = $field_value; if (!empty($options_value) and ($select_buf = self::display_select_options($options_value, $options_params)) !== false) { echo $select_buf; } else { if (!($field_type_arr = S2P_SDK_Scope_Variable::valid_type($structure_definition['type']))) { $field_type_arr = array('title' => '[undefined]'); } ?> <input type="text" id="<?php echo $field_id; ?> " name="<?php echo $field_name; ?> " value="<?php echo self::form_str($field_value); ?> " /><?php echo ' (' . $field_type_arr['title'] . ')'; } } if ($structure_definition['type'] == S2P_SDK_Scope_Variable::TYPE_DATETIME) { echo ' - yyyymmddhhmmss'; } if (!empty($structure_definition['regexp'])) { echo ' <span class="form_input_regexp"><a href="javascript:void(0);" onclick="toggle_regexp( $(this) )" tabindex="10000">RExp</a><span class="form_input_regexp_exp">' . $structure_definition['regexp'] . '</span></span>'; } ?> </div> </div> <?php $buf = ob_get_clean(); return $buf; } $field_id = str_replace('.', '_', $params['path']); ob_start(); ?> <fieldset id="mparam_<?php echo $field_id; ?> "> <label for="mparam_<?php echo $field_id; ?> "><a href="javascript:void(0);" onclick="toggle_container( 'mparam_container_<?php echo $field_id; ?> ' )"><strong><?php echo $params['path']; ?> </strong></a></label> <div id="mparam_container_<?php echo $field_id; ?> " style="display: block;"> <?php $new_mandatory_arr = array(); if (array_key_exists($structure_definition['name'], $mandatory_arr)) { $new_mandatory_arr = $mandatory_arr[$structure_definition['name']]; } $new_hide_keys_arr = array(); if (array_key_exists($structure_definition['name'], $hide_keys_arr)) { $new_hide_keys_arr = $hide_keys_arr[$structure_definition['name']]; } if ($structure_definition['type'] == S2P_SDK_Scope_Variable::TYPE_BLOB_ARRAY) { $blob_array_count = 0; if ($blob_array_value = self::extract_field_value($post_arr['mparams'], $params['path']) and is_array($blob_array_value)) { $elements_params = $params; $elements_params['blob_array_index'] = 0; $elements_params['blob_array_index_read'] = 0; foreach ($blob_array_value as $element_key => $element_arr) { $elements_params['blob_array_index_read'] = $element_key; ?> <div class="form_input_blob_array"><?php foreach ($structure_definition['structure'] as $element_definition) { if ($element_buffer = $this->get_form_method_parameters_fields_detailed($element_definition, $new_mandatory_arr, $new_hide_keys_arr, $post_arr, $form_arr, $elements_params)) { echo $element_buffer; } } ?> <a href="javascript:void(0);" onclick="remove_methods_blob_array_element( $(this), '<?php echo $field_id; ?> ' )"><?php echo self::s2p_t('Remove'); ?> </a> </div> <?php $elements_params['blob_array_index']++; $blob_array_count++; } } ?> </div> <div id="mparam_container_<?php echo $field_id; ?> ___template" class="form_input_blob_array input_disabler_container" style="display: none;"> <?php } if ($structure_definition['type'] == S2P_SDK_Scope_Variable::TYPE_BLOB_ARRAY) { $params['name'] .= '[{*BLOB_ARRAY_INDEX*}]'; } foreach ($structure_definition['structure'] as $element_definition) { if ($element_buffer = $this->get_form_method_parameters_fields_detailed($element_definition, $new_mandatory_arr, $new_hide_keys_arr, $post_arr, $form_arr, $params)) { echo $element_buffer; } } if ($structure_definition['type'] == S2P_SDK_Scope_Variable::TYPE_BLOB_ARRAY) { ?> <a href="javascript:void(0);" onclick="remove_methods_blob_array_element( $(this), '<?php echo $field_id; ?> ' )"><?php echo self::s2p_t('Remove'); ?> </a> <?php } ?> </div><?php if ($structure_definition['type'] == S2P_SDK_Scope_Variable::TYPE_BLOB_ARRAY) { ?> <div class="field_adder_container"><a href="javascript:void(0);" onclick="add_methods_blob_array_element( '<?php echo $field_id; ?> ' )"><?php echo self::s2p_t('Add value'); ?> </a></div> <?php } ?> </fieldset><?php $structure_buffer = ob_get_clean(); return $structure_buffer; }