CalculationsReact.php
3.66 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
<?php
namespace NinjaForms\Includes\Admin\Metaboxes;
if (!defined('ABSPATH')) exit;
/**
* Construct Calculations metabox for the React Submissions page
*
* Class must have a public function handle that can receive the $extraValue
* and a NF submission.
*
* Output of handle method is a \NinjaForms\Includes\Entities\MetaboxOutputEntity with two properties:
*
* 'title' (string output of metabox title/header)
* 'labelValueCollection' – indexed array of label values
*
* Each label value array has three keys:
* 'label' – label of the output
* 'value' – value of that being output
* 'styling' – currently accepts 'alert' to add an 'alert' class for CSS styling
*/
class CalculationsReact
{
/**
* Given submission '$extra' data and the complete submission, return array
* construct for metabox If nothing to output, then return null
*
* If the '$extra' data contains all required information, then simply
* construct that as label/value/styling arrays.
*
* If your output requires other information from the submission, use the
* $nfSub to extract the required information.
*
* Note that in this example, we want additional information from the
* submission for output so we disregard the $extraValue and work directly
* with the $nfSub to extract the information.
*
* @param mixed $extraValue
* @param NF_Database_Models_Submission $nfSub
* @return \NinjaForms\Includes\Entities\MetaboxOutputEntity|null
*/
public function handle($extraValue, $nfSub): ?\NinjaForms\Includes\Entities\MetaboxOutputEntity
{
$return = null;
$debug = $this->isDebugSet();
// extract/construct the label/value/styling arrays
$labelValueCollection = self::extractResponses($extraValue,$debug);
if (!empty($labelValueCollection)) {
$array = [
// Set a translatable title for your metabox
'title' => __('Calculations', 'ninja-forms'),
// set the label/value/styling
'labelValueCollection' => $labelValueCollection
];
$return = \NinjaForms\Includes\Entities\MetaboxOutputEntity::fromArray($array);
}
return $return;
}
/**
* Construct calculations output
*/
protected static function extractResponses($calculations, ?bool $debug=false ): array
{
// Initialize collection of label/value/styling arrays
$return = [];
if (is_array($calculations)) {
foreach ($calculations as $name => $contents) {
$result = [
'label' => \esc_html($name),
'value' => $contents['value']
];
$return[] = $result;
if(!$debug){
continue;
}
$raw = [
'label' => \esc_html($name). __(' - Raw', 'ninja-forms'),
'value' => $contents['raw']
];
$return[] = $raw;
$parsed = [
'label' => \esc_html($name) . __(' - Parsed', 'ninja-forms'),
'value' => $contents['parsed']
];
$return[] = $parsed;
}
}
return $return;
}
/**
* Determine/return if calc debug is set
*
* Checks for string `&calcs_debug` in URI
*
* @return boolean
*/
protected function isDebugSet( ): bool
{
$referer= filter_input(INPUT_SERVER,'HTTP_REFERER');
$return = strpos($referer,'calcs_debug')>0?TRUE:FALSE;
return $return;
}
}