Skip to content

Commit

Permalink
Merge pull request #6 from bu-ist/mini-form
Browse files Browse the repository at this point in the history
Mini form
  • Loading branch information
jdub233 authored Nov 2, 2016
2 parents a1e3c16 + 11171df commit 00ea88b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 5 deletions.
34 changes: 34 additions & 0 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,38 @@ function bu_liaison_inquiry_options_page_html() {
</form>
</div>
<?php
// If there is already a key set, use it to fetch and display a field inventory.
$options = get_option( 'bu_liaison_inquiry_options' );
if ( isset( $options['APIKey'] ) && ('' != $options['APIKey']) ) {
echo bu_liaison_inquiry_field_guide( $options['APIKey'] );
}
}


function bu_liaison_inquiry_field_guide( $api_key ) {
// Should be rewritten with a single method for form retrieval both for the admin and shortcode functions.
$api_query = BU_LIAISON_INQUIRY::API_URL . BU_LIAISON_INQUIRY::REQUIREMENTS_PATH . '?IQS-API-KEY=' . $api_key;
$api_response = wp_remote_get( $api_query );
if ( is_wp_error( $api_response ) ) {
error_log( 'Liaison form API call failed: ' . $api_response->get_error_message() );
return 'Form Error: ' . $api_response->get_error_message();
}
$inquiry_form_decode = json_decode( $api_response['body'] );

// Check that the response from the API contains actual form data.
if ( ! isset( $inquiry_form_decode->data ) ) {
error_log( 'Bad response from Liaison API server: ' . $inquiry_form_decode->message );
return 'Error in Form API response';
}

$inquiry_form = $inquiry_form_decode->data;

$fields_html = '<h2>Field inventory</h2>';

foreach ( $inquiry_form->sections as $section ) {
foreach ( $section->fields as $field_key => $field ) {
$fields_html .= sprintf( '<p>%s = %s</p>', $field->displayName, $field->id );
}
}
return $fields_html;
}
65 changes: 61 additions & 4 deletions bu-liaison-inquiry.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ class BU_Liaison_Inquiry {
const CLIENT_RULES_PATH = 'field_rules/client_rules';
const FIELD_OPTIONS_PATH = 'field_rules/field_options';

// Setup dummy value for required fields that aren't part of the mini form.
const MINI_DUMMY_VALUE = 'mini-form';

/**
* Path to plugin directory.
*
Expand Down Expand Up @@ -98,8 +101,14 @@ function liaison_inquiry_form( $atts ) {
$api_key = $options['APIKey'];
$client_id = $options['ClientID'];

// Optionally override the API key with the shortcode attribute if present.
if ( isset( $atts['api_key'] ) ) { $api_key = $atts['api_key']; }
// Assign any preset field ids in the shortcode attributes.
$presets = array();
foreach ( $atts as $att_key => $att ) {
// Look for integer numbers, these are field ids.
if ( intval( $att_key ) === $att_key ) {
$presets[ $att_key ] = $att;
}
}

// Get info from EMP about the fields that should be displayed for the form.
$api_query = self::$requirements_url . '?IQS-API-KEY=' . $api_key;
Expand Down Expand Up @@ -128,8 +137,20 @@ function liaison_inquiry_form( $atts ) {
wp_enqueue_script( 'field_rules_form_library' );
wp_enqueue_script( 'field_rules_handler' );

// Setup field ids if a restricted field set was specified in the shortcode.
$field_ids = array();
if ( isset( $atts['fields'] ) ) {
// Parse fields attribute.
$fields = explode( ',', $atts['fields'] );
foreach ( $fields as $field ) {
// Only use integer values.
if ( ctype_digit( $field ) ) {
$field_ids[] = $field;
}
}
}

$inquiry_form = $inquiry_form_decode->data;
$inquiry_form = $this->process_form_definition( $inquiry_form_decode->data, $field_ids, $presets );

// Setup nonce for form to protect against various possible attacks.
$nonce = wp_nonce_field( 'liaison_inquiry', 'liaison_inquiry_nonce', false, false );
Expand All @@ -144,7 +165,40 @@ function liaison_inquiry_form( $atts ) {
}

/**
* Shortcode definition that creates the Liaison inquiry form.
* Takes the form definition returned by the Liaison API, strips out any unspecified fields for the mini form, and applies other formatting
*
* @param array $inquiry_form Parsed JSON data from Liaison API.
* @param array $field_ids List of fields to show. If not specified, the full form is returned.
* @param array $presets Array of preset field ids and values.
* @return array Returns a data array of the processed form data to be passed to the template
*/
function process_form_definition( $inquiry_form, $field_ids, $presets ) {
// If field_ids are specified, remove any fields that aren't in the specified set.
if ( 0 < count( $field_ids ) ) {
foreach ( $inquiry_form->sections as $section ) {
foreach ( $section->fields as $field_key => $field ) {
// Field by field processing.
if ( ! in_array( $field->id, $field_ids ) ) {
// If a field isn't listed and isn't required, just remove it.
if ( '1' != $field->required ) {
unset( $section->fields[ $field_key ] );
} else {
$field->hidden = true;
if ( isset( $presets[ $field->id ] ) ) {
$field->hidden_value = $presets[ $field->id ];
} else {
$field->hidden_value = self::MINI_DUMMY_VALUE;
}
}
}
}
}
}
return $inquiry_form;
}

/**
* Handle incoming form data
*
* @return string Returns the result of the form submission as a JSON formatted array for the javascript validation script
*/
Expand All @@ -158,6 +212,9 @@ function handle_liaison_inquiry() {
return;
}

// Clear the verified nonce from $_POST so that it doesn't get passed on to Liaison.
unset( $_POST['liaison_inquiry_nonce'] );

// Necessary to get the API key from the options, can't expose the key by passing it through the form.
$options = get_option( 'bu_liaison_inquiry_options' );
// Check for a valid API key value.
Expand Down
9 changes: 8 additions & 1 deletion templates/form-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,15 @@
$label = '';
}
//end setup
?>

<?php
// Mini form needs to pass dummy values to otherwise required fields; insert them here as hidden inputs.
if ( isset( $field->hidden ) && $field->hidden ) : ?>
<input type="hidden" name="<?php echo $field->id;?>" value="<?php echo $field->hidden_value;?>">
<?php
//begin 2 types of html elements: input-text or select
if ( $field->htmlElement == 'input-text' ) :
elseif ( $field->htmlElement == 'input-text' ) :
//begin input text
$class = '';

Expand Down

0 comments on commit 00ea88b

Please sign in to comment.