Completed
Push — master ( ac7d8e...ec16eb )
by Stephanie
05:53 queued 02:59
created

FrmFieldValue   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 211
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 211
rs 10
c 0
b 0
f 0
wmc 26
lcom 1
cbo 4

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 4
A init_saved_value() 0 11 3
A prepare_displayed_value() 0 5 1
A get_field_option() 0 3 1
A get_field_label() 0 3 1
A get_field_id() 0 3 1
A get_field_key() 0 3 1
A get_field_type() 0 3 1
A get_saved_value() 0 3 1
A get_displayed_value() 0 7 2
A generate_displayed_value_for_field_type() 0 6 2
B filter_displayed_value() 0 27 4
A clean_saved_value() 0 10 4
1
<?php
2
3
/**
4
 * @since 2.04
5
 */
6
class FrmFieldValue {
7
8
	/**
9
	 * @since 2.04
10
	 *
11
	 * @var stdClass
12
	 */
13
	protected $field = null;
14
15
	/**
16
	 * @since 2.04
17
	 *
18
	 * @var int
19
	 */
20
	protected $entry_id = 0;
21
22
	/**
23
	 * @since 2.04
24
	 *
25
	 * @var mixed
26
	 */
27
	protected $saved_value = '';
28
29
	/**
30
	 * @since 2.04
31
	 *
32
	 * @var mixed
33
	 */
34
	protected $displayed_value = 'frm_not_prepared';
35
36
	/**
37
	 * FrmFieldValue constructor.
38
	 *
39
	 * @param stdClass $field
40
	 * @param stdClass $entry
41
	 */
42
	public function __construct( $field, $entry ) {
43
		if ( ! is_object( $field ) || ! is_object( $entry ) || ! isset( $entry->metas ) ) {
44
			return;
45
		}
46
47
		$this->entry_id = $entry->id;
48
		$this->field = $field;
49
		$this->init_saved_value( $entry );
50
	}
51
52
	/**
53
	 * Initialize the saved_value property
54
	 *
55
	 * @since 2.04
56
	 *
57
	 * @param stdClass $entry
58
	 */
59
	protected function init_saved_value( $entry ) {
60
		if ( $this->field->type === 'html' ) {
61
			$this->saved_value = $this->field->description;
62
		} else if ( isset( $entry->metas[ $this->field->id ] ) ) {
63
			$this->saved_value = $entry->metas[ $this->field->id ];
64
		} else {
65
			$this->saved_value = '';
66
		}
67
68
		$this->clean_saved_value();
69
	}
70
71
	/**
72
	 * Prepare the display value
73
	 *
74
	 * @since 2.05
75
	 *
76
	 * @param array $atts
77
	 */
78
	public function prepare_displayed_value( $atts = array() ) {
79
		$this->displayed_value = $this->saved_value;
80
		$this->generate_displayed_value_for_field_type( $atts );
81
		$this->filter_displayed_value( $atts );
82
	}
83
84
	/**
85
	 * Get a value from the field settings
86
	 * @since 2.05.06
87
	 */
88
	public function get_field_option( $value ) {
89
		return FrmField::get_option( $this->field, $value );
90
	}
91
92
	/**
93
	 * Get the field property's label
94
	 *
95
	 * @since 2.04
96
	 */
97
	public function get_field_label() {
98
		return $this->field->name;
99
	}
100
101
	/**
102
	 * Get the field property's id
103
	 *
104
	 * @since 2.05
105
	 */
106
	public function get_field_id() {
107
		return $this->field->id;
108
	}
109
110
	/**
111
	 * Get the field property's key
112
	 *
113
	 * @since 2.04
114
	 */
115
	public function get_field_key() {
116
		return $this->field->field_key;
117
	}
118
119
	/**
120
	 * Get the field property's type
121
	 *
122
	 * @since 2.04
123
	 */
124
	public function get_field_type() {
125
		return $this->field->type;
126
	}
127
128
	/**
129
	 * Get the saved_value property
130
	 *
131
	 * @since 2.04
132
	 */
133
	public function get_saved_value() {
134
		return $this->saved_value;
135
	}
136
137
	/**
138
	 * Get the displayed_value property
139
	 *
140
	 * @since 2.04
141
	 */
142
	public function get_displayed_value() {
143
		if ( $this->displayed_value === 'frm_not_prepared' ) {
144
			return __( 'The display value has not been prepared. Please use the prepare_display_value() method before calling get_displayed_value().', 'formidable' );
145
		}
146
147
		return $this->displayed_value;
148
	}
149
150
	/**
151
	 * Get the displayed value for different field types
152
	 *
153
	 * @since 3.0
154
	 *
155
	 * @param array $atts
156
	 *
157
	 * @return mixed
158
	 */
159
	protected function generate_displayed_value_for_field_type( $atts ) {
160
		if ( ! FrmAppHelper::is_empty_value( $this->displayed_value, '' ) ) {
161
			$field_obj = FrmFieldFactory::get_field_object( $this->field );
162
			$this->displayed_value = $field_obj->get_display_value( $this->displayed_value, $atts );
163
		}
164
	}
165
166
	/**
167
	 * Filter the displayed_value property
168
	 *
169
	 * @since 2.04
170
	 *
171
	 * @param array $atts
172
	 */
173
	protected function filter_displayed_value( $atts ) {
174
		$entry = FrmEntry::getOne( $this->entry_id, true );
175
176
		// TODO: maybe change from 'source' to 'run_filters' = 'email'
177
		if ( isset( $atts['source'] ) && $atts['source'] === 'entry_formatter' ) {
178
			// Deprecated frm_email_value hook
179
			$meta                  = array(
180
				'item_id'    => $entry->id,
181
				'field_id'   => $this->field->id,
182
				'meta_value' => $this->saved_value,
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
183
				'field_type' => $this->field->type,
184
			);
185
186
			if ( has_filter( 'frm_email_value' ) ) {
187
				_deprecated_function( 'The frm_email_value filter', '2.04', 'the frm_display_{fieldtype}_value_custom filter' );
188
				$this->displayed_value = apply_filters( 'frm_email_value', $this->displayed_value, (object) $meta, $entry, array(
189
					'field' => $this->field,
190
				) );
191
			}
192
		}
193
194
		// frm_display_{fieldtype}_value_custom hook
195
		$this->displayed_value = apply_filters( 'frm_display_' . $this->field->type . '_value_custom', $this->displayed_value, array(
196
			'field' => $this->field,
197
			'entry' => $entry,
198
		) );
199
	}
200
201
	/**
202
	 * Clean a field's saved value
203
	 *
204
	 * @since 2.04
205
	 */
206
	protected function clean_saved_value() {
207
		if ( $this->saved_value !== '' ) {
208
209
			$this->saved_value = maybe_unserialize( $this->saved_value );
210
211
			if ( is_array( $this->saved_value ) && empty( $this->saved_value ) ) {
212
				$this->saved_value = '';
213
			}
214
		}
215
	}
216
}
217