| 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 FrmProFieldTotal.php in Formidable Pro. |
| 9 |
*/ |
| 10 |
class FrmFieldTotal extends FrmFieldText { |
| 11 |
|
| 12 |
protected $type = 'total'; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var array|string |
| 16 |
*/ |
| 17 |
private $posted_value; |
| 18 |
|
| 19 |
protected $has_input = false; |
| 20 |
|
| 21 |
public function default_html() { |
| 22 |
$input = $this->input_html(); |
| 23 |
|
| 24 |
return <<<DEFAULT_HTML |
| 25 |
<div id="frm_field_[id]_container" class="frm_form_field form-field [required_class][error_class]"> |
| 26 |
<div id="field_[key]_label" class="frm_primary_label">[field_name]</div> |
| 27 |
<p class="frm_total_formatted"></p> |
| 28 |
$input |
| 29 |
[if description]<div class="frm_description" id="frm_desc_field_[key]">[description]</div>[/if description] |
| 30 |
[if error]<div class="frm_error" role="alert" id="frm_error_field_[key]">[error]</div>[/if error] |
| 31 |
</div> |
| 32 |
DEFAULT_HTML; |
| 33 |
} |
| 34 |
|
| 35 |
protected function field_settings_for_type() { |
| 36 |
$settings = parent::field_settings_for_type(); |
| 37 |
|
| 38 |
$settings['read_only'] = false; |
| 39 |
$settings['default'] = false; |
| 40 |
$settings['required'] = false; |
| 41 |
$settings['unique'] = false; |
| 42 |
$settings['size'] = false; |
| 43 |
$settings['clear_on_focus'] = false; |
| 44 |
$settings['format'] = false; |
| 45 |
$settings['autopopulate'] = false; |
| 46 |
$settings['max'] = false; |
| 47 |
|
| 48 |
return $settings; |
| 49 |
} |
| 50 |
|
| 51 |
protected function html5_input_type() { |
| 52 |
return 'hidden'; |
| 53 |
} |
| 54 |
|
| 55 |
protected function include_form_builder_file() { |
| 56 |
return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-total.php'; |
| 57 |
} |
| 58 |
|
| 59 |
public function validate( $args ) { |
| 60 |
$parent_errors = parent::validate( $args ); |
| 61 |
|
| 62 |
if ( $parent_errors ) { |
| 63 |
return $parent_errors; |
| 64 |
} |
| 65 |
|
| 66 |
// In order to be sure that the total is only validated after all |
| 67 |
// product prices and quantities have been gathered, we use a hook. |
| 68 |
$this->posted_value = $args['value'] ?? ''; |
| 69 |
add_filter( 'frm_entries_before_create', array( $this, 'validate_total' ), 10, 2 ); |
| 70 |
|
| 71 |
return array(); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Check to make sure the total hasn't been tampered with. |
| 76 |
* |
| 77 |
* @param array $errors All form errors. |
| 78 |
* @param array|stdClass $form |
| 79 |
* |
| 80 |
* @return array |
| 81 |
*/ |
| 82 |
public function validate_total( $errors, $form ) { |
| 83 |
global $frm_products; |
| 84 |
|
| 85 |
$sum = 0.0; |
| 86 |
$currency = FrmCurrencyHelper::get_currency(); |
| 87 |
|
| 88 |
// Set a decimal separator for currency if no default for it. |
| 89 |
// first remove unnecessary space(s). |
| 90 |
$currency['decimal_separator'] = trim( $currency['decimal_separator'] ); |
| 91 |
|
| 92 |
if ( ! strlen( $currency['decimal_separator'] ) ) { |
| 93 |
$currency['decimal_separator'] = '.'; |
| 94 |
} |
| 95 |
|
| 96 |
if ( ! isset( $frm_products ) || ! is_array( $frm_products ) ) { |
| 97 |
$frm_products = array(); |
| 98 |
} |
| 99 |
|
| 100 |
foreach ( $frm_products as $key => $price_quantity ) { |
| 101 |
if ( in_array( $key, array( 'repeat_quantity_fields', 'quantity_fields' ), true ) || ! isset( $price_quantity['price'] ) ) { |
| 102 |
continue; |
| 103 |
} |
| 104 |
|
| 105 |
$quantity = $this->get_quantity_for_validation( $price_quantity ); |
| 106 |
|
| 107 |
if ( ! is_array( $price_quantity['price'] ) ) { |
| 108 |
$price = FrmCurrencyHelper::prepare_price( $price_quantity['price'], $currency ); |
| 109 |
$sum += (float) $price * (float) $quantity; |
| 110 |
continue; |
| 111 |
} |
| 112 |
|
| 113 |
// checkboxes |
| 114 |
|
| 115 |
foreach ( $price_quantity['price'] as $price ) { |
| 116 |
$price = FrmCurrencyHelper::prepare_price( $price, $currency ); |
| 117 |
$sum += (float) $price * (float) $quantity; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
$sum = $currency['decimals'] > 0 ? round( $sum, $currency['decimals'] ) : ceil( $sum ); |
| 122 |
|
| 123 |
/** |
| 124 |
* @since 6.30 |
| 125 |
* |
| 126 |
* @param float $sum |
| 127 |
* @param array|object $field |
| 128 |
* @param array|stdClass $form |
| 129 |
*/ |
| 130 |
$sum = apply_filters( 'frm_field_total_expected_sum', $sum, $this->field, $form ); |
| 131 |
$posted = (float) FrmCurrencyHelper::prepare_price( $this->posted_value, $currency ); |
| 132 |
|
| 133 |
if ( $posted === $sum ) { |
| 134 |
return $errors; |
| 135 |
} |
| 136 |
|
| 137 |
$error_key = 'field' . $this->get_field_column( 'id' ); |
| 138 |
$errors[ $error_key ] = FrmFieldsHelper::get_error_msg( $this->posted_value, 'invalid' ); |
| 139 |
|
| 140 |
return $errors; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @param array $price_quantity |
| 145 |
* |
| 146 |
* @return int |
| 147 |
*/ |
| 148 |
private function get_quantity_for_validation( $price_quantity ) { |
| 149 |
global $frm_products; |
| 150 |
|
| 151 |
// If there is no quantity field, assume 1. |
| 152 |
$quantity = 1; |
| 153 |
|
| 154 |
if ( isset( $price_quantity['quantity'] ) && is_numeric( $price_quantity['quantity'] ) ) { |
| 155 |
$quantity = $price_quantity['quantity']; |
| 156 |
} elseif ( ! empty( trim( $price_quantity['parent_field_id'] ) ) ) { |
| 157 |
if ( ! empty( $frm_products['repeat_quantity_fields'] ) ) { |
| 158 |
$k = $price_quantity['parent_field_id'] . '_' . $price_quantity['key_pointer']; |
| 159 |
$quantity = $frm_products['repeat_quantity_fields'][ $k ] ?? 1; |
| 160 |
} |
| 161 |
} elseif ( ! empty( $frm_products['quantity_fields'] ) && 1 === count( $frm_products['quantity_fields'] ) ) { |
| 162 |
$quantity = $frm_products['quantity_fields'][0]; |
| 163 |
} |
| 164 |
|
| 165 |
return (int) $quantity; |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @param array|string $value |
| 170 |
* @param array $atts |
| 171 |
* |
| 172 |
* @return array|string |
| 173 |
*/ |
| 174 |
protected function prepare_display_value( $value, $atts ) { |
| 175 |
if ( ! empty( $atts['format'] ) && 'number' === $atts['format'] ) { |
| 176 |
return $value; |
| 177 |
} |
| 178 |
|
| 179 |
return FrmCurrencyHelper::format_price( $value ); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* @return string |
| 184 |
*/ |
| 185 |
public function get_builder_display_value() { |
| 186 |
$display_value = $this->prepare_display_value( '0', array() ); |
| 187 |
|
| 188 |
if ( ! is_array( $display_value ) ) { |
| 189 |
return $display_value; |
| 190 |
} |
| 191 |
|
| 192 |
// This is to fix the PHPStan error. |
| 193 |
return implode( ', ', $display_value ); |
| 194 |
} |
| 195 |
} |
| 196 |
|