Plugin Directory

Changeset 3454222


Ignore:
Timestamp:
02/05/2026 02:53:57 AM (2 weeks ago)
Author:
shift8
Message:

Fix validation bug

Location:
shift8-integration-for-gravity-forms-and-sap-business-one/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • shift8-integration-for-gravity-forms-and-sap-business-one/trunk/cli-test-submission.php

    r3375274 r3454222  
    11281128
    11291129WP_CLI::add_command('shift8-gravitysap-itemcodes', 'Shift8_GravitySAP_ItemCode_Test_Command');
     1130
     1131/**
     1132 * Query SAP B1 master data (Groups, Currencies, Price Lists, etc.)
     1133 */
     1134class Shift8_GravitySAP_MasterData_Command {
     1135   
     1136    /**
     1137     * List Business Partner Groups from SAP B1
     1138     *
     1139     * ## EXAMPLES
     1140     *
     1141     *     wp shift8-gravitysap-masterdata groups
     1142     *
     1143     * @param array $args
     1144     * @param array $assoc_args
     1145     */
     1146    public function groups($args, $assoc_args) {
     1147        $this->query_sap_endpoint(
     1148            '/BusinessPartnerGroups',
     1149            'Business Partner Groups',
     1150            array('Code', 'Name', 'Type'),
     1151            function($item) {
     1152                return sprintf(
     1153                    "Code: %-6s | Name: %s | Type: %s",
     1154                    $item['Code'] ?? 'N/A',
     1155                    $item['Name'] ?? 'N/A',
     1156                    $item['Type'] ?? 'N/A'
     1157                );
     1158            }
     1159        );
     1160    }
     1161   
     1162    /**
     1163     * List Currencies from SAP B1
     1164     *
     1165     * ## EXAMPLES
     1166     *
     1167     *     wp shift8-gravitysap-masterdata currencies
     1168     *
     1169     * @param array $args
     1170     * @param array $assoc_args
     1171     */
     1172    public function currencies($args, $assoc_args) {
     1173        $this->query_sap_endpoint(
     1174            '/Currencies',
     1175            'Currencies',
     1176            array('Code', 'Name'),
     1177            function($item) {
     1178                return sprintf(
     1179                    "Code: %-5s | Name: %s",
     1180                    $item['Code'] ?? 'N/A',
     1181                    $item['Name'] ?? 'N/A'
     1182                );
     1183            }
     1184        );
     1185    }
     1186   
     1187    /**
     1188     * List Price Lists from SAP B1
     1189     *
     1190     * ## EXAMPLES
     1191     *
     1192     *     wp shift8-gravitysap-masterdata pricelists
     1193     *
     1194     * @param array $args
     1195     * @param array $assoc_args
     1196     */
     1197    public function pricelists($args, $assoc_args) {
     1198        $this->query_sap_endpoint(
     1199            '/PriceLists',
     1200            'Price Lists',
     1201            array('PriceListNo', 'PriceListName', 'BasePriceList'),
     1202            function($item) {
     1203                return sprintf(
     1204                    "PriceListNo: %-4s | Name: %-30s | BasePriceList: %s",
     1205                    $item['PriceListNo'] ?? 'N/A',
     1206                    $item['PriceListName'] ?? 'N/A',
     1207                    $item['BasePriceList'] ?? 'N/A'
     1208                );
     1209            }
     1210        );
     1211    }
     1212   
     1213    /**
     1214     * List all master data types available
     1215     *
     1216     * ## EXAMPLES
     1217     *
     1218     *     wp shift8-gravitysap-masterdata list
     1219     *
     1220     * @param array $args
     1221     * @param array $assoc_args
     1222     */
     1223    public function list($args, $assoc_args) {
     1224        WP_CLI::line('');
     1225        WP_CLI::line('Available SAP B1 Master Data Commands:');
     1226        WP_CLI::line('======================================');
     1227        WP_CLI::line('');
     1228        WP_CLI::line('  wp shift8-gravitysap-masterdata groups      - List Business Partner Groups (for GroupCode field)');
     1229        WP_CLI::line('  wp shift8-gravitysap-masterdata currencies  - List Currencies (for Currency field)');
     1230        WP_CLI::line('  wp shift8-gravitysap-masterdata pricelists  - List Price Lists (for PriceListNum field)');
     1231        WP_CLI::line('');
     1232        WP_CLI::line('These commands show you the actual SAP codes to use in your form field mappings.');
     1233        WP_CLI::line('');
     1234    }
     1235   
     1236    /**
     1237     * Generic method to query SAP endpoint and display results
     1238     *
     1239     * @param string $endpoint SAP API endpoint
     1240     * @param string $title Display title
     1241     * @param array $select_fields Fields to select
     1242     * @param callable $formatter Function to format each item
     1243     */
     1244    private function query_sap_endpoint($endpoint, $title, $select_fields, $formatter) {
     1245        WP_CLI::line('');
     1246        WP_CLI::line("Querying SAP B1 for {$title}...");
     1247        WP_CLI::line('');
     1248       
     1249        try {
     1250            // Get SAP settings
     1251            $sap_settings = get_option('shift8_gravitysap_settings', array());
     1252           
     1253            if (empty($sap_settings['sap_endpoint']) || empty($sap_settings['sap_username']) || empty($sap_settings['sap_password'])) {
     1254                WP_CLI::error('SAP connection settings not configured. Please configure in WordPress Admin > Settings > Shift8 GravitySAP.');
     1255                return;
     1256            }
     1257           
     1258            // Decrypt password
     1259            $sap_settings['sap_password'] = shift8_gravitysap_decrypt_password($sap_settings['sap_password']);
     1260           
     1261            // Create SAP service
     1262            require_once plugin_dir_path(__FILE__) . 'includes/class-shift8-gravitysap-sap-service.php';
     1263            $sap_service = new Shift8_GravitySAP_SAP_Service($sap_settings);
     1264           
     1265            // Authenticate
     1266            $reflection = new ReflectionClass($sap_service);
     1267            $auth_method = $reflection->getMethod('ensure_authenticated');
     1268            $auth_method->setAccessible(true);
     1269           
     1270            if (!$auth_method->invoke($sap_service)) {
     1271                WP_CLI::error('Failed to authenticate with SAP B1');
     1272                return;
     1273            }
     1274           
     1275            // Build query with $select
     1276            $select_param = implode(',', $select_fields);
     1277            $query_endpoint = "{$endpoint}?\$select={$select_param}";
     1278           
     1279            // Make request
     1280            $request_method = $reflection->getMethod('make_request');
     1281            $request_method->setAccessible(true);
     1282           
     1283            $response = $request_method->invoke($sap_service, 'GET', $query_endpoint);
     1284           
     1285            if (is_wp_error($response)) {
     1286                WP_CLI::error('SAP API Error: ' . $response->get_error_message());
     1287                return;
     1288            }
     1289           
     1290            $code = wp_remote_retrieve_response_code($response);
     1291            if ($code !== 200) {
     1292                WP_CLI::error("SAP API returned HTTP {$code}");
     1293                return;
     1294            }
     1295           
     1296            $body = wp_remote_retrieve_body($response);
     1297            $data = json_decode($body, true);
     1298           
     1299            if (!isset($data['value']) || empty($data['value'])) {
     1300                WP_CLI::warning("No {$title} found in SAP B1");
     1301                return;
     1302            }
     1303           
     1304            WP_CLI::success("Found " . count($data['value']) . " {$title}:");
     1305            WP_CLI::line('');
     1306            WP_CLI::line(str_repeat('-', 80));
     1307           
     1308            foreach ($data['value'] as $item) {
     1309                WP_CLI::line($formatter($item));
     1310            }
     1311           
     1312            WP_CLI::line(str_repeat('-', 80));
     1313            WP_CLI::line('');
     1314            WP_CLI::line("Use the 'Code' values (not the Names) when mapping form fields to SAP.");
     1315            WP_CLI::line('');
     1316           
     1317        } catch (Exception $e) {
     1318            WP_CLI::error('Error: ' . $e->getMessage());
     1319        }
     1320    }
     1321}
     1322
     1323WP_CLI::add_command('shift8-gravitysap-masterdata', 'Shift8_GravitySAP_MasterData_Command');
  • shift8-integration-for-gravity-forms-and-sap-business-one/trunk/readme.txt

    r3454205 r3454222  
    55* Requires at least: 5.0
    66* Tested up to: 6.8
    7 * Stable tag: 1.3.6
     7* Stable tag: 1.3.7
    88* Requires PHP: 7.4
    99* License: GPLv3
     
    104104
    105105== Changelog ==
     106
     107= 1.3.7 =
     108* **FIX**: Fixed validation error display - fields now properly highlighted when SAP validation fails
     109* **ENHANCEMENT**: Improved validation error messages with detailed information (submitted value, character count, max allowed, hints)
     110* **ENHANCEMENT**: Added summary of all SAP validation errors at top of form
     111* **NEW**: Added WP-CLI commands to query SAP master data (groups, currencies, pricelists)
    106112
    107113= 1.3.6 =
  • shift8-integration-for-gravity-forms-and-sap-business-one/trunk/shift8-gravitysap.php

    r3454205 r3454222  
    44 * Plugin URI: https://github.com/stardothosting/shift8-gravitysap
    55 * Description: Integrates Gravity Forms with SAP Business One, automatically creating Business Partners from form submissions.
    6  * Version: 1.3.6
     6 * Version: 1.3.7
    77 * Author: Shift8 Web
    88 * Author URI: https://shift8web.ca
     
    2828
    2929// Plugin constants
    30 define('SHIFT8_GRAVITYSAP_VERSION', '1.3.6');
     30define('SHIFT8_GRAVITYSAP_VERSION', '1.3.7');
    3131define('SHIFT8_GRAVITYSAP_PLUGIN_FILE', __FILE__);
    3232define('SHIFT8_GRAVITYSAP_PLUGIN_DIR', plugin_dir_path(__FILE__));
     
    184184     */
    185185    private static $instance = null;
     186
     187    /**
     188     * SAP validation errors for display in form validation message
     189     *
     190     * @since 1.3.7
     191     * @var array
     192     */
     193    private $sap_validation_errors = array();
    186194   
    187195    /**
     
    30313039        $sap_field_limits = $this->get_sap_field_limits();
    30323040        $validation_errors = 0;
     3041        $error_details = array(); // Collect detailed error messages for summary
    30333042
    30343043        foreach ($field_mapping as $sap_field => $field_id) {
     
    30383047            }
    30393048           
    3040             $field = RGFormsModel::get_field($form, $field_id);
    3041             if (!$field) {
    3042                 shift8_gravitysap_debug_log('Could not find form field', array('field_id' => $field_id));
     3049            // Get the base field ID (handle subfield IDs like "1.3")
     3050            $base_field_id = intval($field_id);
     3051           
     3052            // Find the field index in the form's fields array - we need to modify it directly
     3053            $field_index = null;
     3054            $field = null;
     3055            foreach ($validation_result['form']['fields'] as $index => $form_field) {
     3056                if ($form_field->id == $base_field_id) {
     3057                    $field_index = $index;
     3058                    $field = $form_field;
     3059                    break;
     3060                }
     3061            }
     3062           
     3063            if ($field === null) {
     3064                shift8_gravitysap_debug_log('Could not find form field', array('field_id' => $field_id, 'base_field_id' => $base_field_id));
    30433065                continue;
    30443066            }
     
    31443166            // Check required fields
    31453167            if ($limits['required'] && empty($field_value)) {
    3146                 $validation_result['is_valid'] = false;
    3147                 $field->failed_validation = true;
    3148                 $field->validation_message = sprintf(
     3168                $error_msg = sprintf(
    31493169                    esc_html__('%s is required for SAP Business Partner creation.', 'shift8-gravity-forms-sap-b1-integration'),
    31503170                    esc_html($field_label)
    31513171                );
     3172                $validation_result['is_valid'] = false;
     3173                $validation_result['form']['fields'][$field_index]->failed_validation = true;
     3174                $validation_result['form']['fields'][$field_index]->validation_message = $error_msg;
     3175                $error_details[] = $error_msg;
    31523176                $validation_errors++;
    31533177                shift8_gravitysap_debug_log('Required field validation failed', array('field' => $field_label));
     
    31673191            if ($field_length > $max_length) {
    31683192                $validation_result['is_valid'] = false;
    3169                 $field->failed_validation = true;
    3170                
     3193                $validation_result['form']['fields'][$field_index]->failed_validation = true;
     3194               
     3195                // Truncate displayed value if too long for readability
     3196                $display_value = strlen($field_value) > 50 ? substr($field_value, 0, 47) . '...' : $field_value;
    31713197                $custom_message = isset($limits['validation_message']) ? $limits['validation_message'] : '';
     3198               
    31723199                if ($custom_message) {
    3173                     $field->validation_message = sprintf(
    3174                         esc_html__('%s: %s (Current: %d chars, Max: %d)', 'shift8-gravity-forms-sap-b1-integration'),
     3200                    $error_msg = sprintf(
     3201                        /* translators: 1: field label, 2: submitted value, 3: character count, 4: max allowed, 5: custom hint */
     3202                        esc_html__('SAP Validation Error - %1$s: You entered "%2$s" (%3$d chars) but max is %4$d chars. Hint: %5$s', 'shift8-gravity-forms-sap-b1-integration'),
    31753203                        esc_html($field_label),
    3176                         esc_html($custom_message),
     3204                        esc_html($display_value),
     3205                        $field_length,
     3206                        $max_length,
     3207                        esc_html($custom_message)
     3208                    );
     3209                } else {
     3210                    $error_msg = sprintf(
     3211                        /* translators: 1: field label, 2: submitted value, 3: character count, 4: max allowed */
     3212                        esc_html__('SAP Validation Error - %1$s: You entered "%2$s" (%3$d chars) but max is %4$d chars.', 'shift8-gravity-forms-sap-b1-integration'),
     3213                        esc_html($field_label),
     3214                        esc_html($display_value),
    31773215                        $field_length,
    31783216                        $max_length
    31793217                    );
    3180                 } else {
    3181                     $field->validation_message = sprintf(
    3182                         esc_html__('%s cannot exceed %d characters (currently %d characters).', 'shift8-gravity-forms-sap-b1-integration'),
    3183                         esc_html($field_label),
    3184                         $max_length,
    3185                         $field_length
    3186                     );
    31873218                }
     3219               
     3220                $validation_result['form']['fields'][$field_index]->validation_message = $error_msg;
     3221                $error_details[] = $error_msg;
    31883222                $validation_errors++;
    31893223                shift8_gravitysap_debug_log('Length validation failed', array(
    31903224                    'field' => $field_label,
     3225                    'submitted_value' => $field_value,
    31913226                    'current_length' => $field_length,
    31923227                    'max_length' => $max_length
     
    31973232            // Check email format
    31983233            if (isset($limits['format']) && $limits['format'] === 'email' && !is_email($field_value)) {
     3234                $error_msg = sprintf(
     3235                    /* translators: 1: field label, 2: submitted value */
     3236                    esc_html__('SAP Validation Error - %1$s: "%2$s" is not a valid email address.', 'shift8-gravity-forms-sap-b1-integration'),
     3237                    esc_html($field_label),
     3238                    esc_html($field_value)
     3239                );
    31993240                $validation_result['is_valid'] = false;
    3200                 $field->failed_validation = true;
    3201                 $field->validation_message = sprintf(
    3202                     esc_html__('%s must be a valid email address.', 'shift8-gravity-forms-sap-b1-integration'),
    3203                     esc_html($field_label)
    3204                 );
     3241                $validation_result['form']['fields'][$field_index]->failed_validation = true;
     3242                $validation_result['form']['fields'][$field_index]->validation_message = $error_msg;
     3243                $error_details[] = $error_msg;
    32053244                $validation_errors++;
    32063245                shift8_gravitysap_debug_log('Email validation failed', array('field' => $field_label, 'value' => $field_value));
     
    32103249            // Check URL format
    32113250            if (isset($limits['format']) && $limits['format'] === 'url' && !filter_var($field_value, FILTER_VALIDATE_URL)) {
     3251                $error_msg = sprintf(
     3252                    /* translators: 1: field label, 2: submitted value */
     3253                    esc_html__('SAP Validation Error - %1$s: "%2$s" is not a valid URL. Must start with http:// or https://', 'shift8-gravity-forms-sap-b1-integration'),
     3254                    esc_html($field_label),
     3255                    esc_html($field_value)
     3256                );
    32123257                $validation_result['is_valid'] = false;
    3213                 $field->failed_validation = true;
    3214                 $field->validation_message = sprintf(
    3215                     esc_html__('%s must be a valid URL.', 'shift8-gravity-forms-sap-b1-integration'),
    3216                     esc_html($field_label)
    3217                 );
     3258                $validation_result['form']['fields'][$field_index]->failed_validation = true;
     3259                $validation_result['form']['fields'][$field_index]->validation_message = $error_msg;
     3260                $error_details[] = $error_msg;
    32183261                $validation_errors++;
    32193262                shift8_gravitysap_debug_log('URL validation failed', array('field' => $field_label, 'value' => $field_value));
     
    32263269        shift8_gravitysap_debug_log('=== SAP FIELD VALIDATION COMPLETED ===', array(
    32273270            'total_errors' => $validation_errors,
    3228             'is_valid' => $validation_result['is_valid']
     3271            'is_valid' => $validation_result['is_valid'],
     3272            'error_details' => $error_details
    32293273        ));
    32303274
     3275        // Store error details for the validation message filter
     3276        if (!empty($error_details)) {
     3277            $this->sap_validation_errors = $error_details;
     3278            add_filter('gform_validation_message', array($this, 'customize_sap_validation_message'), 10, 2);
     3279        }
     3280
    32313281        return $validation_result;
     3282    }
     3283
     3284    /**
     3285     * Customize the validation message to show detailed SAP errors
     3286     *
     3287     * @param string $message The default validation message
     3288     * @param array $form The form array
     3289     * @return string The customized validation message
     3290     */
     3291    public function customize_sap_validation_message($message, $form) {
     3292        // Only modify if we have SAP validation errors
     3293        if (empty($this->sap_validation_errors)) {
     3294            return $message;
     3295        }
     3296
     3297        $error_list = '<ul style="margin: 10px 0; padding-left: 20px;">';
     3298        foreach ($this->sap_validation_errors as $error) {
     3299            $error_list .= '<li style="margin: 5px 0;">' . $error . '</li>';
     3300        }
     3301        $error_list .= '</ul>';
     3302
     3303        $custom_message = '<div class="validation_error" role="alert">';
     3304        $custom_message .= '<strong>' . esc_html__('SAP Integration Validation Failed:', 'shift8-gravity-forms-sap-b1-integration') . '</strong>';
     3305        $custom_message .= $error_list;
     3306        $custom_message .= '<p style="margin-top: 10px; font-size: 0.9em;">' . esc_html__('Please correct the highlighted fields below to meet SAP Business One requirements.', 'shift8-gravity-forms-sap-b1-integration') . '</p>';
     3307        $custom_message .= '</div>';
     3308
     3309        // Clear the errors after displaying
     3310        $this->sap_validation_errors = array();
     3311
     3312        return $custom_message;
    32323313    }
    32333314}
Note: See TracChangeset for help on using the changeset viewer.