Calculations.php
4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
final class NF_Conversion_Calculations implements NF_Conversion
{
private $operations = array(
'add' => '+',
'subtract' => '-',
'multiply' => '*',
'divide' => '/'
);
private $form = array();
private $tax_rate;
public function __construct( $form_data )
{
$this->form = $form_data;
}
public function run()
{
// Extract Calculations from Fields
foreach( $this->form[ 'fields' ] as $key => $field ){
if( 'tax' == $field[ 'type' ] ){
$this->set_tax( $field[ 'default_value' ] );
continue;
}
if( 'calc' != $field[ 'type' ] ) continue;
$calculation = array(
'order' => $key,
'name' => $field[ 'key' ],
'eq' => '',
'dec' => $field[ 'calc_places' ]
);
switch( $field[ 'calc_method' ] ){
case 'eq':
$calculation[ 'eq' ] = $field[ 'calc_eq' ];
break;
case 'fields':
$calculation[ 'eq' ] = trim( array_reduce( $field[ 'calc' ], array( $this, 'reduce_operations' ), '' ) );
break;
case 'auto':
$calculation[ 'eq' ] = trim( array_reduce( $this->form[ 'fields' ], array( $this, 'reduce_auto_total' ), '' ) );
break;
}
// Remove any leading `+`.
$calculation[ 'eq' ] = ltrim( $calculation[ 'eq' ], '+' );
// Handle opinionated "Total" calc and tax field.
if( 'total' == $field[ 'calc_name' ] && isset( $this->tax_rate ) ){
$calculation[ 'eq' ] = "( {$calculation[ 'eq' ]} ) * {$this->tax_rate}";
}
$this->form[ 'settings' ][ 'calculations' ][] = $calculation;
}
// Replace Field IDs with Merge Tags
if( isset( $this->form[ 'settings' ][ 'calculations' ] ) ) {
foreach ($this->form['fields'] as $field) {
if( ! isset( $field[ 'id' ] ) ) continue;
$search = 'field_' . $field['id'];
$replace = $this->merge_tag( $field );
foreach ($this->form['settings']['calculations'] as $key => $calculation) {
$this->form['settings']['calculations'][ $key ]['eq'] = str_replace($search, $replace, $calculation['eq']);
}
}
}
// Convert Calc Fields to HTML Fields for displaying Calculations
foreach( $this->form[ 'fields' ] as $key => $field ){
if( 'tax' == $field[ 'type' ] ){
$this->form[ 'fields' ][ $key ][ 'type' ] = 'html';
$this->form[ 'fields' ][ $key ][ 'default' ] = "<strong>{$field[ 'label' ]}</strong><br /> {$field[ 'default_value' ]}";
continue;
}
if( 'calc' != $field[ 'type' ] ) continue;
$this->form[ 'fields' ][ $key ][ 'type' ] = 'html';
if( 'html' == $field[ 'calc_display_type' ] ){
// TODO: HTML Output fields seem to loose the label.
$search = '[ninja_forms_calc]';
$replace = $this->merge_tag( $field );
$subject = $field[ 'calc_display_html' ];
$this->form[ 'fields' ][ $key ][ 'default' ] = str_replace( $search, $replace, $subject );
} else {
$this->form[ 'fields' ][ $key ][ 'default' ] = '<strong>' . $field[ 'label' ] . '</strong><br />' . $this->merge_tag( $field );
}
}
return $this->form;
}
private function reduce_operations( $eq, $calc )
{
$operation = $calc[ 'op' ];
return ' ' . $eq . $this->operations[ $operation ] . ' field_' . $calc[ 'field' ] . ' ';
}
private function reduce_auto_total( $eq, $field )
{
if( ! isset( $field[ 'calc_auto_include' ] ) || 1 != $field[ 'calc_auto_include' ] ) return $eq;
return $eq . '+ {field:' . $field[ 'key' ] . ':calc} ';
}
private function merge_tag( $field )
{
$tag = $field[ 'key' ];
if( 'calc' == $field[ 'type' ] ){
return '{calc:' . $tag . '}';
} else {
return '{field:' . $tag . ':calc}';
}
}
/**
* Sets a float formatted tax rate.
* @param string|int|float $tax
*
* @return float|int
*/
private function set_tax( $tax )
{
// ex 15% -> 1.15
return $this->tax_rate = ( floatval( $tax ) + 100 ) / 100;
}
}
add_filter( 'ninja_forms_after_upgrade_settings', 'ninja_forms_conversion_calculations' );
function ninja_forms_conversion_calculations( $form_data ){
$conversion = new NF_Conversion_Calculations( $form_data );
return $conversion->run();
}