| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
die( 'You are not allowed to call this page directly.' ); |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* @since 6.30 This is copied from FrmProFieldQuantity.php in Formidable Pro. |
| 9 |
*/ |
| 10 |
class FrmFieldQuantity extends FrmFieldNumber { |
| 11 |
|
| 12 |
protected $type = 'quantity'; |
| 13 |
|
| 14 |
protected function field_settings_for_type() { |
| 15 |
$settings = parent::field_settings_for_type(); |
| 16 |
$settings['unique'] = false; |
| 17 |
$settings['format'] = false; |
| 18 |
|
| 19 |
return $settings; |
| 20 |
} |
| 21 |
|
| 22 |
protected function extra_field_opts() { |
| 23 |
return array_merge( |
| 24 |
parent::extra_field_opts(), |
| 25 |
array( |
| 26 |
'product_field' => array(), |
| 27 |
'step' => '1', |
| 28 |
) |
| 29 |
); |
| 30 |
} |
| 31 |
|
| 32 |
protected function new_field_settings() { |
| 33 |
return array( |
| 34 |
'default_value' => 1, |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* @param array $args - Includes 'field', 'display', and 'values'. |
| 40 |
*/ |
| 41 |
public function show_primary_options( $args ) { |
| 42 |
$field = $args['field']; |
| 43 |
// Cast to array because of existing fields that are already using single product fields in production |
| 44 |
$field['product_field'] = $field['product_field'] ? (array) $field['product_field'] : array(); |
| 45 |
include FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/quantity-options.php'; |
| 46 |
|
| 47 |
parent::show_primary_options( $args ); |
| 48 |
} |
| 49 |
|
| 50 |
protected function html5_input_type() { |
| 51 |
return 'number'; |
| 52 |
} |
| 53 |
|
| 54 |
public function validate( $args ) { |
| 55 |
global $frm_products; |
| 56 |
$parent_errors = parent::validate( $args ); |
| 57 |
|
| 58 |
if ( $parent_errors ) { |
| 59 |
return $parent_errors; |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! $frm_products ) { |
| 63 |
$frm_products = array(); |
| 64 |
} |
| 65 |
|
| 66 |
$value = trim( $args['value'] ); |
| 67 |
$value = is_numeric( $value ) ? $value : 0; |
| 68 |
|
| 69 |
$product_fields = FrmField::get_option( $this->field, 'product_field' ); |
| 70 |
|
| 71 |
if ( $product_fields ) { |
| 72 |
// Cast to array because of existing fields that are already using single product fields in production |
| 73 |
$product_fields = (array) $product_fields; |
| 74 |
|
| 75 |
foreach ( $product_fields as $product_field ) { |
| 76 |
$product_field_key = trim( $product_field ) . '_' . $args['parent_field_id'] . '_' . $args['key_pointer']; |
| 77 |
|
| 78 |
if ( ! isset( $frm_products[ $product_field_key ] ) || ! is_array( $frm_products[ $product_field_key ] ) ) { |
| 79 |
$frm_products[ $product_field_key ] = array(); |
| 80 |
} |
| 81 |
$frm_products[ $product_field_key ]['quantity'] = $value; |
| 82 |
} |
| 83 |
} elseif ( ! empty( $args['parent_field_id'] ) ) { |
| 84 |
// Get it ready for the corresponding product field in the row that will use it to calc total |
| 85 |
if ( ! isset( $frm_products['repeat_quantity_fields'] ) || ! is_array( $frm_products['repeat_quantity_fields'] ) ) { |
| 86 |
$frm_products['repeat_quantity_fields'] = array(); |
| 87 |
} |
| 88 |
$frm_products['repeat_quantity_fields'][ $args['parent_field_id'] . '_' . $args['key_pointer'] ] = $value; |
| 89 |
} else { |
| 90 |
// This quantity field is in the form but not in a repeater |
| 91 |
if ( ! isset( $frm_products['quantity_fields'] ) || ! is_array( $frm_products['quantity_fields'] ) ) { |
| 92 |
$frm_products['quantity_fields'] = array(); |
| 93 |
} |
| 94 |
$frm_products['quantity_fields'][] = $value; |
| 95 |
}//end if |
| 96 |
|
| 97 |
return array(); |
| 98 |
} |
| 99 |
} |
| 100 |
|