@@ -15,22 +15,22 @@ |
||
15 | 15 | abstract class ConnectionBase implements ConnectionInterface |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var EEM_Base $model |
|
20 | - */ |
|
21 | - protected $model; |
|
18 | + /** |
|
19 | + * @var EEM_Base $model |
|
20 | + */ |
|
21 | + protected $model; |
|
22 | 22 | |
23 | - /** |
|
24 | - * @var string $namespace The graphql namespace/prefix. |
|
25 | - */ |
|
26 | - protected $namespace = 'Espresso'; |
|
23 | + /** |
|
24 | + * @var string $namespace The graphql namespace/prefix. |
|
25 | + */ |
|
26 | + protected $namespace = 'Espresso'; |
|
27 | 27 | |
28 | 28 | |
29 | - /** |
|
30 | - * @param EEM_Base $model |
|
31 | - */ |
|
32 | - public function __construct(EEM_Base $model) |
|
33 | - { |
|
34 | - $this->model = $model; |
|
35 | - } |
|
29 | + /** |
|
30 | + * @param EEM_Base $model |
|
31 | + */ |
|
32 | + public function __construct(EEM_Base $model) |
|
33 | + { |
|
34 | + $this->model = $model; |
|
35 | + } |
|
36 | 36 | } |
@@ -15,12 +15,12 @@ |
||
15 | 15 | interface GQLDataDomainInterface |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @param array $loaders The loaders accessible in the AppContext |
|
20 | - * @param AppContext $context The AppContext |
|
21 | - * @return array |
|
22 | - * @return array |
|
23 | - * @since $VID:$ |
|
24 | - */ |
|
25 | - public function registerLoaders(array $loaders, AppContext $context): array; |
|
18 | + /** |
|
19 | + * @param array $loaders The loaders accessible in the AppContext |
|
20 | + * @param AppContext $context The AppContext |
|
21 | + * @return array |
|
22 | + * @return array |
|
23 | + * @since $VID:$ |
|
24 | + */ |
|
25 | + public function registerLoaders(array $loaders, AppContext $context): array; |
|
26 | 26 | } |
@@ -24,79 +24,79 @@ |
||
24 | 24 | interface TypeInterface |
25 | 25 | { |
26 | 26 | |
27 | - /** |
|
28 | - * @return string |
|
29 | - */ |
|
30 | - public function name(): string; |
|
31 | - |
|
32 | - |
|
33 | - /** |
|
34 | - * @return string |
|
35 | - */ |
|
36 | - public function description(): string; |
|
37 | - |
|
38 | - |
|
39 | - /** |
|
40 | - * @return GraphQLFieldInterface[] |
|
41 | - * @since $VID:$ |
|
42 | - */ |
|
43 | - public function fields(): array; |
|
44 | - |
|
45 | - |
|
46 | - /** |
|
47 | - * Creates a key map for internal resolver. |
|
48 | - * |
|
49 | - * @return array |
|
50 | - * @since $VID:$ |
|
51 | - */ |
|
52 | - public function getFieldsForResolver(): array; |
|
53 | - |
|
54 | - |
|
55 | - /** |
|
56 | - * @return bool |
|
57 | - */ |
|
58 | - public function isCustomPostType(): bool; |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * @param int|float $value |
|
63 | - * @return int |
|
64 | - * @since $VID:$ |
|
65 | - */ |
|
66 | - public function parseInfiniteValue($value): int; |
|
67 | - |
|
68 | - |
|
69 | - /** |
|
70 | - * @param mixed $source The source that's passed down the GraphQL queries |
|
71 | - * @param array $args The inputArgs on the field |
|
72 | - * @param AppContext $context The AppContext passed down the GraphQL tree |
|
73 | - * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
|
74 | - * @return mixed |
|
75 | - * @throws EE_Error |
|
76 | - * @throws InvalidDataTypeException |
|
77 | - * @throws InvalidInterfaceException |
|
78 | - * @throws UnexpectedEntityException |
|
79 | - * @throws UserError |
|
80 | - * @throws InvalidArgumentException |
|
81 | - * @throws ReflectionException |
|
82 | - * @since $VID:$ |
|
83 | - */ |
|
84 | - public function resolveField($source, array $args, AppContext $context, ResolveInfo $info); |
|
85 | - |
|
86 | - |
|
87 | - /** |
|
88 | - * @param mixed $payload The payload returned after mutation |
|
89 | - * @param array $args The inputArgs on the field |
|
90 | - * @param AppContext $context The AppContext passed down the GraphQL tree |
|
91 | - * @return string|null |
|
92 | - * @throws EE_Error |
|
93 | - * @throws InvalidDataTypeException |
|
94 | - * @throws InvalidInterfaceException |
|
95 | - * @throws UnexpectedEntityException |
|
96 | - * @throws UserError |
|
97 | - * @throws InvalidArgumentException |
|
98 | - * @throws ReflectionException |
|
99 | - * @since $VID:$ |
|
100 | - */ |
|
101 | - public function resolveFromPayload($payload, array $args, AppContext $context); |
|
27 | + /** |
|
28 | + * @return string |
|
29 | + */ |
|
30 | + public function name(): string; |
|
31 | + |
|
32 | + |
|
33 | + /** |
|
34 | + * @return string |
|
35 | + */ |
|
36 | + public function description(): string; |
|
37 | + |
|
38 | + |
|
39 | + /** |
|
40 | + * @return GraphQLFieldInterface[] |
|
41 | + * @since $VID:$ |
|
42 | + */ |
|
43 | + public function fields(): array; |
|
44 | + |
|
45 | + |
|
46 | + /** |
|
47 | + * Creates a key map for internal resolver. |
|
48 | + * |
|
49 | + * @return array |
|
50 | + * @since $VID:$ |
|
51 | + */ |
|
52 | + public function getFieldsForResolver(): array; |
|
53 | + |
|
54 | + |
|
55 | + /** |
|
56 | + * @return bool |
|
57 | + */ |
|
58 | + public function isCustomPostType(): bool; |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * @param int|float $value |
|
63 | + * @return int |
|
64 | + * @since $VID:$ |
|
65 | + */ |
|
66 | + public function parseInfiniteValue($value): int; |
|
67 | + |
|
68 | + |
|
69 | + /** |
|
70 | + * @param mixed $source The source that's passed down the GraphQL queries |
|
71 | + * @param array $args The inputArgs on the field |
|
72 | + * @param AppContext $context The AppContext passed down the GraphQL tree |
|
73 | + * @param ResolveInfo $info The ResolveInfo passed down the GraphQL tree |
|
74 | + * @return mixed |
|
75 | + * @throws EE_Error |
|
76 | + * @throws InvalidDataTypeException |
|
77 | + * @throws InvalidInterfaceException |
|
78 | + * @throws UnexpectedEntityException |
|
79 | + * @throws UserError |
|
80 | + * @throws InvalidArgumentException |
|
81 | + * @throws ReflectionException |
|
82 | + * @since $VID:$ |
|
83 | + */ |
|
84 | + public function resolveField($source, array $args, AppContext $context, ResolveInfo $info); |
|
85 | + |
|
86 | + |
|
87 | + /** |
|
88 | + * @param mixed $payload The payload returned after mutation |
|
89 | + * @param array $args The inputArgs on the field |
|
90 | + * @param AppContext $context The AppContext passed down the GraphQL tree |
|
91 | + * @return string|null |
|
92 | + * @throws EE_Error |
|
93 | + * @throws InvalidDataTypeException |
|
94 | + * @throws InvalidInterfaceException |
|
95 | + * @throws UnexpectedEntityException |
|
96 | + * @throws UserError |
|
97 | + * @throws InvalidArgumentException |
|
98 | + * @throws ReflectionException |
|
99 | + * @since $VID:$ |
|
100 | + */ |
|
101 | + public function resolveFromPayload($payload, array $args, AppContext $context); |
|
102 | 102 | } |
@@ -14,337 +14,337 @@ |
||
14 | 14 | */ |
15 | 15 | class JsonDataHandler |
16 | 16 | { |
17 | - const DATA_TYPE_ARRAY = 'array'; |
|
18 | - |
|
19 | - const DATA_TYPE_OBJECT = 'object'; |
|
20 | - |
|
21 | - const DATA_TYPE_USE_FLAGS = 'flags'; |
|
22 | - |
|
23 | - const NO_ERROR_MSG = 'No error'; |
|
24 | - |
|
25 | - /** |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - private $data_type; |
|
29 | - |
|
30 | - /** |
|
31 | - * @var array|stdClass |
|
32 | - */ |
|
33 | - private $decoded_data; |
|
34 | - |
|
35 | - /** |
|
36 | - * JSON_BIGINT_AS_STRING, |
|
37 | - * JSON_INVALID_UTF8_IGNORE, |
|
38 | - * JSON_INVALID_UTF8_SUBSTITUTE, |
|
39 | - * JSON_OBJECT_AS_ARRAY, |
|
40 | - * JSON_THROW_ON_ERROR |
|
41 | - * |
|
42 | - * @var int |
|
43 | - */ |
|
44 | - private $decode_flags; |
|
45 | - |
|
46 | - /** |
|
47 | - * @var int |
|
48 | - */ |
|
49 | - private $depth; |
|
50 | - |
|
51 | - /** |
|
52 | - * @var string |
|
53 | - */ |
|
54 | - private $encoded_data; |
|
55 | - |
|
56 | - /** |
|
57 | - * JSON_FORCE_OBJECT, |
|
58 | - * JSON_HEX_QUOT, |
|
59 | - * JSON_HEX_TAG, |
|
60 | - * JSON_HEX_AMP, |
|
61 | - * JSON_HEX_APOS, |
|
62 | - * JSON_INVALID_UTF8_IGNORE, |
|
63 | - * JSON_INVALID_UTF8_SUBSTITUTE, |
|
64 | - * JSON_NUMERIC_CHECK, |
|
65 | - * JSON_PARTIAL_OUTPUT_ON_ERROR, |
|
66 | - * JSON_PRESERVE_ZERO_FRACTION, |
|
67 | - * JSON_PRETTY_PRINT, |
|
68 | - * JSON_UNESCAPED_LINE_TERMINATORS, |
|
69 | - * JSON_UNESCAPED_SLASHES, |
|
70 | - * JSON_UNESCAPED_UNICODE, |
|
71 | - * JSON_THROW_ON_ERROR. |
|
72 | - * |
|
73 | - * @var int |
|
74 | - */ |
|
75 | - private $encode_flags; |
|
76 | - |
|
77 | - /** |
|
78 | - * @var int |
|
79 | - */ |
|
80 | - private $last_error_code = JSON_ERROR_NONE; |
|
81 | - |
|
82 | - /** |
|
83 | - * @var string |
|
84 | - */ |
|
85 | - private $last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
|
86 | - |
|
87 | - |
|
88 | - /** |
|
89 | - * JsonDataHandler constructor. |
|
90 | - */ |
|
91 | - public function __construct() |
|
92 | - { |
|
93 | - if (! defined('JSON_INVALID_UTF8_IGNORE')) { |
|
94 | - define('JSON_INVALID_UTF8_IGNORE', 1048576); |
|
95 | - } |
|
96 | - if (! defined('JSON_INVALID_UTF8_SUBSTITUTE')) { |
|
97 | - define('JSON_INVALID_UTF8_SUBSTITUTE', 2097152); |
|
98 | - } |
|
99 | - if (! defined('JSON_THROW_ON_ERROR')) { |
|
100 | - define('JSON_THROW_ON_ERROR', 4194304); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * set $data_type, $decode_flags, $encode_flags, and depth all in one shot |
|
107 | - * |
|
108 | - * @param string $data_type |
|
109 | - * @param int $decode_flags |
|
110 | - * @param int $encode_flags |
|
111 | - * @param int $depth |
|
112 | - */ |
|
113 | - public function configure( |
|
114 | - string $data_type = JsonDataHandler::DATA_TYPE_USE_FLAGS, |
|
115 | - int $decode_flags = 0, |
|
116 | - int $encode_flags = 0, |
|
117 | - int $depth = 512 |
|
118 | - ) { |
|
119 | - $this->setDataType($data_type); |
|
120 | - $this->setDecodeFlags($decode_flags); |
|
121 | - $this->setDepth($depth); |
|
122 | - $this->setEncodeFlags($encode_flags); |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * @param string $data_type |
|
128 | - */ |
|
129 | - public function setDataType(string $data_type): void |
|
130 | - { |
|
131 | - $this->data_type = $data_type === JsonDataHandler::DATA_TYPE_ARRAY |
|
132 | - || $data_type === JsonDataHandler::DATA_TYPE_OBJECT |
|
133 | - || $data_type === JsonDataHandler::DATA_TYPE_USE_FLAGS |
|
134 | - ? $data_type |
|
135 | - : JsonDataHandler::DATA_TYPE_USE_FLAGS; |
|
136 | - } |
|
137 | - |
|
138 | - |
|
139 | - /** |
|
140 | - * One or more Bitmask values: |
|
141 | - * JSON_BIGINT_AS_STRING, |
|
142 | - * JSON_INVALID_UTF8_IGNORE, PHP >= 7.2 |
|
143 | - * JSON_INVALID_UTF8_SUBSTITUTE, PHP >= 7.2 |
|
144 | - * JSON_OBJECT_AS_ARRAY, |
|
145 | - * JSON_THROW_ON_ERROR PHP >= 7.3 |
|
146 | - * |
|
147 | - * pass multiple values separated with | |
|
148 | - * ex: JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE | JSON_OBJECT_AS_ARRAY |
|
149 | - * |
|
150 | - * @param int $decode_flags |
|
151 | - */ |
|
152 | - public function setDecodeFlags(int $decode_flags): void |
|
153 | - { |
|
154 | - $this->decode_flags = $decode_flags === JSON_BIGINT_AS_STRING |
|
155 | - || $decode_flags === JSON_OBJECT_AS_ARRAY |
|
156 | - // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound |
|
157 | - || $decode_flags === JSON_INVALID_UTF8_IGNORE |
|
158 | - // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound |
|
159 | - || $decode_flags === JSON_INVALID_UTF8_SUBSTITUTE |
|
160 | - // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound |
|
161 | - || $decode_flags === JSON_THROW_ON_ERROR |
|
162 | - ? $decode_flags |
|
163 | - : 0; |
|
164 | - } |
|
165 | - |
|
166 | - |
|
167 | - /** |
|
168 | - * @param int $depth |
|
169 | - */ |
|
170 | - public function setDepth(int $depth): void |
|
171 | - { |
|
172 | - $depth = absint($depth); |
|
173 | - $this->depth = $depth ?: 512; |
|
174 | - } |
|
175 | - |
|
176 | - |
|
177 | - /** |
|
178 | - * One or more Bitmask values: |
|
179 | - * JSON_FORCE_OBJECT, |
|
180 | - * JSON_HEX_QUOT, |
|
181 | - * JSON_HEX_TAG, |
|
182 | - * JSON_HEX_AMP, |
|
183 | - * JSON_HEX_APOS, |
|
184 | - * JSON_INVALID_UTF8_IGNORE, PHP >= 7.2 |
|
185 | - * JSON_INVALID_UTF8_SUBSTITUTE, PHP >= 7.2 |
|
186 | - * JSON_NUMERIC_CHECK, |
|
187 | - * JSON_PARTIAL_OUTPUT_ON_ERROR, |
|
188 | - * JSON_PRESERVE_ZERO_FRACTION, |
|
189 | - * JSON_PRETTY_PRINT, |
|
190 | - * JSON_UNESCAPED_LINE_TERMINATORS, |
|
191 | - * JSON_UNESCAPED_SLASHES, |
|
192 | - * JSON_UNESCAPED_UNICODE, |
|
193 | - * JSON_THROW_ON_ERROR. PHP >= 7.3 |
|
194 | - * |
|
195 | - * pass multiple values separated with | |
|
196 | - * ex: JSON_FORCE_OBJECT | JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR |
|
197 | - * |
|
198 | - * @param int $encode_flags |
|
199 | - */ |
|
200 | - public function setEncodeFlags(int $encode_flags): void |
|
201 | - { |
|
202 | - $this->encode_flags = $encode_flags === JSON_FORCE_OBJECT |
|
203 | - || $encode_flags === JSON_HEX_QUOT |
|
204 | - || $encode_flags === JSON_HEX_TAG |
|
205 | - || $encode_flags === JSON_HEX_AMP |
|
206 | - || $encode_flags === JSON_HEX_APOS |
|
207 | - || $encode_flags === JSON_NUMERIC_CHECK |
|
208 | - || $encode_flags === JSON_PARTIAL_OUTPUT_ON_ERROR |
|
209 | - || $encode_flags === JSON_PRESERVE_ZERO_FRACTION |
|
210 | - || $encode_flags === JSON_PRETTY_PRINT |
|
211 | - || $encode_flags === JSON_UNESCAPED_LINE_TERMINATORS |
|
212 | - || $encode_flags === JSON_UNESCAPED_SLASHES |
|
213 | - || $encode_flags === JSON_UNESCAPED_UNICODE |
|
214 | - // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound |
|
215 | - || $encode_flags === JSON_INVALID_UTF8_IGNORE |
|
216 | - // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound |
|
217 | - || $encode_flags === JSON_INVALID_UTF8_SUBSTITUTE |
|
218 | - // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound |
|
219 | - || $encode_flags === JSON_THROW_ON_ERROR |
|
220 | - ? $encode_flags |
|
221 | - : 0; |
|
222 | - } |
|
223 | - |
|
224 | - |
|
225 | - /** |
|
226 | - * @return bool|null |
|
227 | - */ |
|
228 | - private function asAssociative(): ?bool |
|
229 | - { |
|
230 | - switch ($this->data_type) { |
|
231 | - case JsonDataHandler::DATA_TYPE_ARRAY: |
|
232 | - return true; |
|
233 | - case JsonDataHandler::DATA_TYPE_OBJECT: |
|
234 | - return false; |
|
235 | - case JsonDataHandler::DATA_TYPE_USE_FLAGS: |
|
236 | - return null; |
|
237 | - } |
|
238 | - return null; |
|
239 | - } |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * @param array|string $json |
|
244 | - * @return array|mixed|stdClass |
|
245 | - */ |
|
246 | - public function decodeJson($json) |
|
247 | - { |
|
248 | - $this->resetErrors(); |
|
249 | - if ($this->isJson($json)) { |
|
250 | - $this->decoded_data = json_decode($json, $this->asAssociative(), $this->depth, $this->decode_flags); |
|
251 | - $this->last_error_code = json_last_error(); |
|
252 | - $this->last_error_msg = json_last_error_msg(); |
|
253 | - } else { |
|
254 | - $this->decoded_data = $json; |
|
255 | - $this->last_error_code = JSON_ERROR_NONE; |
|
256 | - $this->last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
|
257 | - } |
|
258 | - return $this->decoded_data; |
|
259 | - } |
|
260 | - |
|
261 | - |
|
262 | - /** |
|
263 | - * @param $data |
|
264 | - * @return string |
|
265 | - */ |
|
266 | - public function encodeData($data): string |
|
267 | - { |
|
268 | - $this->resetErrors(); |
|
269 | - if ($this->isJson($data)) { |
|
270 | - $this->encoded_data = $data; |
|
271 | - $this->last_error_code = JSON_ERROR_NONE; |
|
272 | - $this->last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
|
273 | - } else { |
|
274 | - $this->encoded_data = json_encode($data, $this->encode_flags, $this->depth); |
|
275 | - $this->last_error_code = json_last_error(); |
|
276 | - $this->last_error_msg = json_last_error_msg(); |
|
277 | - } |
|
278 | - return $this->encoded_data ?: '{}'; |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * @return array|stdClass |
|
284 | - */ |
|
285 | - public function getDecodedData() |
|
286 | - { |
|
287 | - return $this->decoded_data; |
|
288 | - } |
|
289 | - |
|
290 | - |
|
291 | - /** |
|
292 | - * @return string |
|
293 | - */ |
|
294 | - public function getEncodedData(): string |
|
295 | - { |
|
296 | - return $this->encoded_data; |
|
297 | - } |
|
298 | - |
|
299 | - |
|
300 | - /** |
|
301 | - * @param bool $reset |
|
302 | - * @return int |
|
303 | - */ |
|
304 | - public function getLastErrorCode(bool $reset = false): int |
|
305 | - { |
|
306 | - $last_error = $this->last_error_code; |
|
307 | - if ($reset) { |
|
308 | - $this->resetErrors(); |
|
309 | - } |
|
310 | - return $last_error; |
|
311 | - } |
|
312 | - |
|
313 | - |
|
314 | - /** |
|
315 | - * @param bool $reset |
|
316 | - * @return string |
|
317 | - */ |
|
318 | - public function getLastErrorMessage(bool $reset = false): string |
|
319 | - { |
|
320 | - $last_error = $this->last_error_msg; |
|
321 | - if ($reset) { |
|
322 | - $this->resetErrors(); |
|
323 | - } |
|
324 | - return $last_error; |
|
325 | - } |
|
326 | - |
|
327 | - |
|
328 | - /** |
|
329 | - * @param array|string $maybe_json |
|
330 | - * @return bool |
|
331 | - */ |
|
332 | - public function isJson($maybe_json): bool |
|
333 | - { |
|
334 | - if (! is_string($maybe_json)) { |
|
335 | - return false; |
|
336 | - } |
|
337 | - $decoded = json_decode($maybe_json, $this->asAssociative(), $this->depth, $this->decode_flags); |
|
338 | - return json_last_error() === JSON_ERROR_NONE && ! ($decoded === null && ! empty($maybe_json)); |
|
339 | - } |
|
340 | - |
|
341 | - |
|
342 | - /** |
|
343 | - * @since $VID:$ |
|
344 | - */ |
|
345 | - public function resetErrors() |
|
346 | - { |
|
347 | - $this->last_error_code = JSON_ERROR_NONE; |
|
348 | - $this->last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
|
349 | - } |
|
17 | + const DATA_TYPE_ARRAY = 'array'; |
|
18 | + |
|
19 | + const DATA_TYPE_OBJECT = 'object'; |
|
20 | + |
|
21 | + const DATA_TYPE_USE_FLAGS = 'flags'; |
|
22 | + |
|
23 | + const NO_ERROR_MSG = 'No error'; |
|
24 | + |
|
25 | + /** |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + private $data_type; |
|
29 | + |
|
30 | + /** |
|
31 | + * @var array|stdClass |
|
32 | + */ |
|
33 | + private $decoded_data; |
|
34 | + |
|
35 | + /** |
|
36 | + * JSON_BIGINT_AS_STRING, |
|
37 | + * JSON_INVALID_UTF8_IGNORE, |
|
38 | + * JSON_INVALID_UTF8_SUBSTITUTE, |
|
39 | + * JSON_OBJECT_AS_ARRAY, |
|
40 | + * JSON_THROW_ON_ERROR |
|
41 | + * |
|
42 | + * @var int |
|
43 | + */ |
|
44 | + private $decode_flags; |
|
45 | + |
|
46 | + /** |
|
47 | + * @var int |
|
48 | + */ |
|
49 | + private $depth; |
|
50 | + |
|
51 | + /** |
|
52 | + * @var string |
|
53 | + */ |
|
54 | + private $encoded_data; |
|
55 | + |
|
56 | + /** |
|
57 | + * JSON_FORCE_OBJECT, |
|
58 | + * JSON_HEX_QUOT, |
|
59 | + * JSON_HEX_TAG, |
|
60 | + * JSON_HEX_AMP, |
|
61 | + * JSON_HEX_APOS, |
|
62 | + * JSON_INVALID_UTF8_IGNORE, |
|
63 | + * JSON_INVALID_UTF8_SUBSTITUTE, |
|
64 | + * JSON_NUMERIC_CHECK, |
|
65 | + * JSON_PARTIAL_OUTPUT_ON_ERROR, |
|
66 | + * JSON_PRESERVE_ZERO_FRACTION, |
|
67 | + * JSON_PRETTY_PRINT, |
|
68 | + * JSON_UNESCAPED_LINE_TERMINATORS, |
|
69 | + * JSON_UNESCAPED_SLASHES, |
|
70 | + * JSON_UNESCAPED_UNICODE, |
|
71 | + * JSON_THROW_ON_ERROR. |
|
72 | + * |
|
73 | + * @var int |
|
74 | + */ |
|
75 | + private $encode_flags; |
|
76 | + |
|
77 | + /** |
|
78 | + * @var int |
|
79 | + */ |
|
80 | + private $last_error_code = JSON_ERROR_NONE; |
|
81 | + |
|
82 | + /** |
|
83 | + * @var string |
|
84 | + */ |
|
85 | + private $last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
|
86 | + |
|
87 | + |
|
88 | + /** |
|
89 | + * JsonDataHandler constructor. |
|
90 | + */ |
|
91 | + public function __construct() |
|
92 | + { |
|
93 | + if (! defined('JSON_INVALID_UTF8_IGNORE')) { |
|
94 | + define('JSON_INVALID_UTF8_IGNORE', 1048576); |
|
95 | + } |
|
96 | + if (! defined('JSON_INVALID_UTF8_SUBSTITUTE')) { |
|
97 | + define('JSON_INVALID_UTF8_SUBSTITUTE', 2097152); |
|
98 | + } |
|
99 | + if (! defined('JSON_THROW_ON_ERROR')) { |
|
100 | + define('JSON_THROW_ON_ERROR', 4194304); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * set $data_type, $decode_flags, $encode_flags, and depth all in one shot |
|
107 | + * |
|
108 | + * @param string $data_type |
|
109 | + * @param int $decode_flags |
|
110 | + * @param int $encode_flags |
|
111 | + * @param int $depth |
|
112 | + */ |
|
113 | + public function configure( |
|
114 | + string $data_type = JsonDataHandler::DATA_TYPE_USE_FLAGS, |
|
115 | + int $decode_flags = 0, |
|
116 | + int $encode_flags = 0, |
|
117 | + int $depth = 512 |
|
118 | + ) { |
|
119 | + $this->setDataType($data_type); |
|
120 | + $this->setDecodeFlags($decode_flags); |
|
121 | + $this->setDepth($depth); |
|
122 | + $this->setEncodeFlags($encode_flags); |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * @param string $data_type |
|
128 | + */ |
|
129 | + public function setDataType(string $data_type): void |
|
130 | + { |
|
131 | + $this->data_type = $data_type === JsonDataHandler::DATA_TYPE_ARRAY |
|
132 | + || $data_type === JsonDataHandler::DATA_TYPE_OBJECT |
|
133 | + || $data_type === JsonDataHandler::DATA_TYPE_USE_FLAGS |
|
134 | + ? $data_type |
|
135 | + : JsonDataHandler::DATA_TYPE_USE_FLAGS; |
|
136 | + } |
|
137 | + |
|
138 | + |
|
139 | + /** |
|
140 | + * One or more Bitmask values: |
|
141 | + * JSON_BIGINT_AS_STRING, |
|
142 | + * JSON_INVALID_UTF8_IGNORE, PHP >= 7.2 |
|
143 | + * JSON_INVALID_UTF8_SUBSTITUTE, PHP >= 7.2 |
|
144 | + * JSON_OBJECT_AS_ARRAY, |
|
145 | + * JSON_THROW_ON_ERROR PHP >= 7.3 |
|
146 | + * |
|
147 | + * pass multiple values separated with | |
|
148 | + * ex: JSON_BIGINT_AS_STRING | JSON_INVALID_UTF8_IGNORE | JSON_OBJECT_AS_ARRAY |
|
149 | + * |
|
150 | + * @param int $decode_flags |
|
151 | + */ |
|
152 | + public function setDecodeFlags(int $decode_flags): void |
|
153 | + { |
|
154 | + $this->decode_flags = $decode_flags === JSON_BIGINT_AS_STRING |
|
155 | + || $decode_flags === JSON_OBJECT_AS_ARRAY |
|
156 | + // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound |
|
157 | + || $decode_flags === JSON_INVALID_UTF8_IGNORE |
|
158 | + // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound |
|
159 | + || $decode_flags === JSON_INVALID_UTF8_SUBSTITUTE |
|
160 | + // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound |
|
161 | + || $decode_flags === JSON_THROW_ON_ERROR |
|
162 | + ? $decode_flags |
|
163 | + : 0; |
|
164 | + } |
|
165 | + |
|
166 | + |
|
167 | + /** |
|
168 | + * @param int $depth |
|
169 | + */ |
|
170 | + public function setDepth(int $depth): void |
|
171 | + { |
|
172 | + $depth = absint($depth); |
|
173 | + $this->depth = $depth ?: 512; |
|
174 | + } |
|
175 | + |
|
176 | + |
|
177 | + /** |
|
178 | + * One or more Bitmask values: |
|
179 | + * JSON_FORCE_OBJECT, |
|
180 | + * JSON_HEX_QUOT, |
|
181 | + * JSON_HEX_TAG, |
|
182 | + * JSON_HEX_AMP, |
|
183 | + * JSON_HEX_APOS, |
|
184 | + * JSON_INVALID_UTF8_IGNORE, PHP >= 7.2 |
|
185 | + * JSON_INVALID_UTF8_SUBSTITUTE, PHP >= 7.2 |
|
186 | + * JSON_NUMERIC_CHECK, |
|
187 | + * JSON_PARTIAL_OUTPUT_ON_ERROR, |
|
188 | + * JSON_PRESERVE_ZERO_FRACTION, |
|
189 | + * JSON_PRETTY_PRINT, |
|
190 | + * JSON_UNESCAPED_LINE_TERMINATORS, |
|
191 | + * JSON_UNESCAPED_SLASHES, |
|
192 | + * JSON_UNESCAPED_UNICODE, |
|
193 | + * JSON_THROW_ON_ERROR. PHP >= 7.3 |
|
194 | + * |
|
195 | + * pass multiple values separated with | |
|
196 | + * ex: JSON_FORCE_OBJECT | JSON_INVALID_UTF8_IGNORE | JSON_THROW_ON_ERROR |
|
197 | + * |
|
198 | + * @param int $encode_flags |
|
199 | + */ |
|
200 | + public function setEncodeFlags(int $encode_flags): void |
|
201 | + { |
|
202 | + $this->encode_flags = $encode_flags === JSON_FORCE_OBJECT |
|
203 | + || $encode_flags === JSON_HEX_QUOT |
|
204 | + || $encode_flags === JSON_HEX_TAG |
|
205 | + || $encode_flags === JSON_HEX_AMP |
|
206 | + || $encode_flags === JSON_HEX_APOS |
|
207 | + || $encode_flags === JSON_NUMERIC_CHECK |
|
208 | + || $encode_flags === JSON_PARTIAL_OUTPUT_ON_ERROR |
|
209 | + || $encode_flags === JSON_PRESERVE_ZERO_FRACTION |
|
210 | + || $encode_flags === JSON_PRETTY_PRINT |
|
211 | + || $encode_flags === JSON_UNESCAPED_LINE_TERMINATORS |
|
212 | + || $encode_flags === JSON_UNESCAPED_SLASHES |
|
213 | + || $encode_flags === JSON_UNESCAPED_UNICODE |
|
214 | + // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_ignoreFound |
|
215 | + || $encode_flags === JSON_INVALID_UTF8_IGNORE |
|
216 | + // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_invalid_utf8_substituteFound |
|
217 | + || $encode_flags === JSON_INVALID_UTF8_SUBSTITUTE |
|
218 | + // phpcs:ignore PHPCompatibility.Constants.NewConstants.json_throw_on_errorFound |
|
219 | + || $encode_flags === JSON_THROW_ON_ERROR |
|
220 | + ? $encode_flags |
|
221 | + : 0; |
|
222 | + } |
|
223 | + |
|
224 | + |
|
225 | + /** |
|
226 | + * @return bool|null |
|
227 | + */ |
|
228 | + private function asAssociative(): ?bool |
|
229 | + { |
|
230 | + switch ($this->data_type) { |
|
231 | + case JsonDataHandler::DATA_TYPE_ARRAY: |
|
232 | + return true; |
|
233 | + case JsonDataHandler::DATA_TYPE_OBJECT: |
|
234 | + return false; |
|
235 | + case JsonDataHandler::DATA_TYPE_USE_FLAGS: |
|
236 | + return null; |
|
237 | + } |
|
238 | + return null; |
|
239 | + } |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * @param array|string $json |
|
244 | + * @return array|mixed|stdClass |
|
245 | + */ |
|
246 | + public function decodeJson($json) |
|
247 | + { |
|
248 | + $this->resetErrors(); |
|
249 | + if ($this->isJson($json)) { |
|
250 | + $this->decoded_data = json_decode($json, $this->asAssociative(), $this->depth, $this->decode_flags); |
|
251 | + $this->last_error_code = json_last_error(); |
|
252 | + $this->last_error_msg = json_last_error_msg(); |
|
253 | + } else { |
|
254 | + $this->decoded_data = $json; |
|
255 | + $this->last_error_code = JSON_ERROR_NONE; |
|
256 | + $this->last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
|
257 | + } |
|
258 | + return $this->decoded_data; |
|
259 | + } |
|
260 | + |
|
261 | + |
|
262 | + /** |
|
263 | + * @param $data |
|
264 | + * @return string |
|
265 | + */ |
|
266 | + public function encodeData($data): string |
|
267 | + { |
|
268 | + $this->resetErrors(); |
|
269 | + if ($this->isJson($data)) { |
|
270 | + $this->encoded_data = $data; |
|
271 | + $this->last_error_code = JSON_ERROR_NONE; |
|
272 | + $this->last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
|
273 | + } else { |
|
274 | + $this->encoded_data = json_encode($data, $this->encode_flags, $this->depth); |
|
275 | + $this->last_error_code = json_last_error(); |
|
276 | + $this->last_error_msg = json_last_error_msg(); |
|
277 | + } |
|
278 | + return $this->encoded_data ?: '{}'; |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * @return array|stdClass |
|
284 | + */ |
|
285 | + public function getDecodedData() |
|
286 | + { |
|
287 | + return $this->decoded_data; |
|
288 | + } |
|
289 | + |
|
290 | + |
|
291 | + /** |
|
292 | + * @return string |
|
293 | + */ |
|
294 | + public function getEncodedData(): string |
|
295 | + { |
|
296 | + return $this->encoded_data; |
|
297 | + } |
|
298 | + |
|
299 | + |
|
300 | + /** |
|
301 | + * @param bool $reset |
|
302 | + * @return int |
|
303 | + */ |
|
304 | + public function getLastErrorCode(bool $reset = false): int |
|
305 | + { |
|
306 | + $last_error = $this->last_error_code; |
|
307 | + if ($reset) { |
|
308 | + $this->resetErrors(); |
|
309 | + } |
|
310 | + return $last_error; |
|
311 | + } |
|
312 | + |
|
313 | + |
|
314 | + /** |
|
315 | + * @param bool $reset |
|
316 | + * @return string |
|
317 | + */ |
|
318 | + public function getLastErrorMessage(bool $reset = false): string |
|
319 | + { |
|
320 | + $last_error = $this->last_error_msg; |
|
321 | + if ($reset) { |
|
322 | + $this->resetErrors(); |
|
323 | + } |
|
324 | + return $last_error; |
|
325 | + } |
|
326 | + |
|
327 | + |
|
328 | + /** |
|
329 | + * @param array|string $maybe_json |
|
330 | + * @return bool |
|
331 | + */ |
|
332 | + public function isJson($maybe_json): bool |
|
333 | + { |
|
334 | + if (! is_string($maybe_json)) { |
|
335 | + return false; |
|
336 | + } |
|
337 | + $decoded = json_decode($maybe_json, $this->asAssociative(), $this->depth, $this->decode_flags); |
|
338 | + return json_last_error() === JSON_ERROR_NONE && ! ($decoded === null && ! empty($maybe_json)); |
|
339 | + } |
|
340 | + |
|
341 | + |
|
342 | + /** |
|
343 | + * @since $VID:$ |
|
344 | + */ |
|
345 | + public function resetErrors() |
|
346 | + { |
|
347 | + $this->last_error_code = JSON_ERROR_NONE; |
|
348 | + $this->last_error_msg = JsonDataHandler::NO_ERROR_MSG; |
|
349 | + } |
|
350 | 350 | } |
@@ -90,13 +90,13 @@ discard block |
||
90 | 90 | */ |
91 | 91 | public function __construct() |
92 | 92 | { |
93 | - if (! defined('JSON_INVALID_UTF8_IGNORE')) { |
|
93 | + if ( ! defined('JSON_INVALID_UTF8_IGNORE')) { |
|
94 | 94 | define('JSON_INVALID_UTF8_IGNORE', 1048576); |
95 | 95 | } |
96 | - if (! defined('JSON_INVALID_UTF8_SUBSTITUTE')) { |
|
96 | + if ( ! defined('JSON_INVALID_UTF8_SUBSTITUTE')) { |
|
97 | 97 | define('JSON_INVALID_UTF8_SUBSTITUTE', 2097152); |
98 | 98 | } |
99 | - if (! defined('JSON_THROW_ON_ERROR')) { |
|
99 | + if ( ! defined('JSON_THROW_ON_ERROR')) { |
|
100 | 100 | define('JSON_THROW_ON_ERROR', 4194304); |
101 | 101 | } |
102 | 102 | } |
@@ -331,7 +331,7 @@ discard block |
||
331 | 331 | */ |
332 | 332 | public function isJson($maybe_json): bool |
333 | 333 | { |
334 | - if (! is_string($maybe_json)) { |
|
334 | + if ( ! is_string($maybe_json)) { |
|
335 | 335 | return false; |
336 | 336 | } |
337 | 337 | $decoded = json_decode($maybe_json, $this->asAssociative(), $this->depth, $this->decode_flags); |
@@ -5,152 +5,152 @@ |
||
5 | 5 | class EE_Form_Submission extends EE_Base_Class |
6 | 6 | { |
7 | 7 | |
8 | - /** |
|
9 | - * @param array $props_n_values |
|
10 | - * @return EE_Form_Submission |
|
11 | - * @throws EE_Error |
|
12 | - * @throws ReflectionException |
|
13 | - */ |
|
14 | - public static function new_instance(array $props_n_values = []): EE_Form_Submission |
|
15 | - { |
|
16 | - $props_n_values['FSB_submitted'] = new DateTime(); |
|
17 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
18 | - return $has_object ?: new self($props_n_values); |
|
19 | - } |
|
20 | - |
|
21 | - |
|
22 | - /** |
|
23 | - * @param array $props_n_values |
|
24 | - * @return EE_Form_Submission |
|
25 | - * @throws EE_Error |
|
26 | - * @throws ReflectionException |
|
27 | - */ |
|
28 | - public static function new_instance_from_db(array $props_n_values = []): EE_Form_Submission |
|
29 | - { |
|
30 | - return new self($props_n_values, true); |
|
31 | - } |
|
32 | - |
|
33 | - |
|
34 | - /** |
|
35 | - * Form Section UUID (universally unique identifier) |
|
36 | - * |
|
37 | - * @return string |
|
38 | - * @throws EE_Error |
|
39 | - * @throws ReflectionException |
|
40 | - */ |
|
41 | - public function UUID(): string |
|
42 | - { |
|
43 | - return $this->get('FSB_UUID'); |
|
44 | - } |
|
45 | - |
|
46 | - |
|
47 | - /** |
|
48 | - * @param string $UUID |
|
49 | - * @throws EE_Error |
|
50 | - * @throws ReflectionException |
|
51 | - */ |
|
52 | - public function setUUID(string $UUID) |
|
53 | - { |
|
54 | - $this->set('FSB_UUID', $UUID); |
|
55 | - } |
|
56 | - |
|
57 | - |
|
58 | - /** |
|
59 | - * UUID or ID of related entity this form submission belongs to. |
|
60 | - * |
|
61 | - * @return string |
|
62 | - * @throws EE_Error |
|
63 | - * @throws ReflectionException |
|
64 | - */ |
|
65 | - public function formSection(): string |
|
66 | - { |
|
67 | - return $this->get('FSC_UUID'); |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * @param string $form_section_UUID |
|
73 | - * @throws EE_Error |
|
74 | - * @throws ReflectionException |
|
75 | - */ |
|
76 | - public function setFormSection(string $form_section_UUID) |
|
77 | - { |
|
78 | - $this->set('FSC_UUID', $form_section_UUID); |
|
79 | - } |
|
80 | - |
|
81 | - |
|
82 | - /** |
|
83 | - * Returns the related EE_Transaction this form submission belongs to. |
|
84 | - * |
|
85 | - * @return EE_Transaction |
|
86 | - * @throws EE_Error |
|
87 | - * @throws EntityNotFoundException |
|
88 | - * @throws ReflectionException |
|
89 | - */ |
|
90 | - public function transaction(): EE_Transaction |
|
91 | - { |
|
92 | - $transaction = $this->get_first_related('Transaction'); |
|
93 | - if (! $transaction instanceof EE_Transaction) { |
|
94 | - throw new EntityNotFoundException('Transaction ID', $this->transactionID()); |
|
95 | - } |
|
96 | - return $transaction; |
|
97 | - } |
|
98 | - |
|
99 | - |
|
100 | - /** |
|
101 | - * @return int |
|
102 | - * @throws EE_Error |
|
103 | - * @throws ReflectionException |
|
104 | - */ |
|
105 | - public function transactionID(): int |
|
106 | - { |
|
107 | - return $this->get('TXN_ID'); |
|
108 | - } |
|
109 | - |
|
110 | - |
|
111 | - /** |
|
112 | - * @param int $TXN_ID |
|
113 | - * @throws EE_Error |
|
114 | - * @throws ReflectionException |
|
115 | - */ |
|
116 | - public function setTransactionID(int $TXN_ID = 0) |
|
117 | - { |
|
118 | - $this->set('TXN_ID', $TXN_ID); |
|
119 | - } |
|
120 | - |
|
121 | - |
|
122 | - /** |
|
123 | - * @return mixed |
|
124 | - * @throws EE_Error |
|
125 | - * @throws ReflectionException |
|
126 | - */ |
|
127 | - public function data() |
|
128 | - { |
|
129 | - return $this->get('FSB_data'); |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * @param array|string $data |
|
135 | - * @throws EE_Error |
|
136 | - * @throws ReflectionException |
|
137 | - */ |
|
138 | - public function setData($data) |
|
139 | - { |
|
140 | - $this->set('FSB_data', $data); |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * @param bool $raw |
|
146 | - * @return int |
|
147 | - * @throws EE_Error |
|
148 | - * @throws ReflectionException |
|
149 | - */ |
|
150 | - public function submitted(bool $raw = false): int |
|
151 | - { |
|
152 | - return $raw |
|
153 | - ? $this->get_raw('FSB_submitted') |
|
154 | - : $this->get('FSB_submitted'); |
|
155 | - } |
|
8 | + /** |
|
9 | + * @param array $props_n_values |
|
10 | + * @return EE_Form_Submission |
|
11 | + * @throws EE_Error |
|
12 | + * @throws ReflectionException |
|
13 | + */ |
|
14 | + public static function new_instance(array $props_n_values = []): EE_Form_Submission |
|
15 | + { |
|
16 | + $props_n_values['FSB_submitted'] = new DateTime(); |
|
17 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
18 | + return $has_object ?: new self($props_n_values); |
|
19 | + } |
|
20 | + |
|
21 | + |
|
22 | + /** |
|
23 | + * @param array $props_n_values |
|
24 | + * @return EE_Form_Submission |
|
25 | + * @throws EE_Error |
|
26 | + * @throws ReflectionException |
|
27 | + */ |
|
28 | + public static function new_instance_from_db(array $props_n_values = []): EE_Form_Submission |
|
29 | + { |
|
30 | + return new self($props_n_values, true); |
|
31 | + } |
|
32 | + |
|
33 | + |
|
34 | + /** |
|
35 | + * Form Section UUID (universally unique identifier) |
|
36 | + * |
|
37 | + * @return string |
|
38 | + * @throws EE_Error |
|
39 | + * @throws ReflectionException |
|
40 | + */ |
|
41 | + public function UUID(): string |
|
42 | + { |
|
43 | + return $this->get('FSB_UUID'); |
|
44 | + } |
|
45 | + |
|
46 | + |
|
47 | + /** |
|
48 | + * @param string $UUID |
|
49 | + * @throws EE_Error |
|
50 | + * @throws ReflectionException |
|
51 | + */ |
|
52 | + public function setUUID(string $UUID) |
|
53 | + { |
|
54 | + $this->set('FSB_UUID', $UUID); |
|
55 | + } |
|
56 | + |
|
57 | + |
|
58 | + /** |
|
59 | + * UUID or ID of related entity this form submission belongs to. |
|
60 | + * |
|
61 | + * @return string |
|
62 | + * @throws EE_Error |
|
63 | + * @throws ReflectionException |
|
64 | + */ |
|
65 | + public function formSection(): string |
|
66 | + { |
|
67 | + return $this->get('FSC_UUID'); |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * @param string $form_section_UUID |
|
73 | + * @throws EE_Error |
|
74 | + * @throws ReflectionException |
|
75 | + */ |
|
76 | + public function setFormSection(string $form_section_UUID) |
|
77 | + { |
|
78 | + $this->set('FSC_UUID', $form_section_UUID); |
|
79 | + } |
|
80 | + |
|
81 | + |
|
82 | + /** |
|
83 | + * Returns the related EE_Transaction this form submission belongs to. |
|
84 | + * |
|
85 | + * @return EE_Transaction |
|
86 | + * @throws EE_Error |
|
87 | + * @throws EntityNotFoundException |
|
88 | + * @throws ReflectionException |
|
89 | + */ |
|
90 | + public function transaction(): EE_Transaction |
|
91 | + { |
|
92 | + $transaction = $this->get_first_related('Transaction'); |
|
93 | + if (! $transaction instanceof EE_Transaction) { |
|
94 | + throw new EntityNotFoundException('Transaction ID', $this->transactionID()); |
|
95 | + } |
|
96 | + return $transaction; |
|
97 | + } |
|
98 | + |
|
99 | + |
|
100 | + /** |
|
101 | + * @return int |
|
102 | + * @throws EE_Error |
|
103 | + * @throws ReflectionException |
|
104 | + */ |
|
105 | + public function transactionID(): int |
|
106 | + { |
|
107 | + return $this->get('TXN_ID'); |
|
108 | + } |
|
109 | + |
|
110 | + |
|
111 | + /** |
|
112 | + * @param int $TXN_ID |
|
113 | + * @throws EE_Error |
|
114 | + * @throws ReflectionException |
|
115 | + */ |
|
116 | + public function setTransactionID(int $TXN_ID = 0) |
|
117 | + { |
|
118 | + $this->set('TXN_ID', $TXN_ID); |
|
119 | + } |
|
120 | + |
|
121 | + |
|
122 | + /** |
|
123 | + * @return mixed |
|
124 | + * @throws EE_Error |
|
125 | + * @throws ReflectionException |
|
126 | + */ |
|
127 | + public function data() |
|
128 | + { |
|
129 | + return $this->get('FSB_data'); |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * @param array|string $data |
|
135 | + * @throws EE_Error |
|
136 | + * @throws ReflectionException |
|
137 | + */ |
|
138 | + public function setData($data) |
|
139 | + { |
|
140 | + $this->set('FSB_data', $data); |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * @param bool $raw |
|
146 | + * @return int |
|
147 | + * @throws EE_Error |
|
148 | + * @throws ReflectionException |
|
149 | + */ |
|
150 | + public function submitted(bool $raw = false): int |
|
151 | + { |
|
152 | + return $raw |
|
153 | + ? $this->get_raw('FSB_submitted') |
|
154 | + : $this->get('FSB_submitted'); |
|
155 | + } |
|
156 | 156 | } |
@@ -90,7 +90,7 @@ |
||
90 | 90 | public function transaction(): EE_Transaction |
91 | 91 | { |
92 | 92 | $transaction = $this->get_first_related('Transaction'); |
93 | - if (! $transaction instanceof EE_Transaction) { |
|
93 | + if ( ! $transaction instanceof EE_Transaction) { |
|
94 | 94 | throw new EntityNotFoundException('Transaction ID', $this->transactionID()); |
95 | 95 | } |
96 | 96 | return $transaction; |
@@ -131,7 +131,7 @@ discard block |
||
131 | 131 | public function addHtmlClass(string $htmlClass): void |
132 | 132 | { |
133 | 133 | $htmlClass = sanitize_key($htmlClass); |
134 | - if (! in_array($htmlClass, $this->htmlClasses, true)) { |
|
134 | + if ( ! in_array($htmlClass, $this->htmlClasses, true)) { |
|
135 | 135 | $this->htmlClasses[] = $htmlClass; |
136 | 136 | } |
137 | 137 | } |
@@ -143,7 +143,7 @@ discard block |
||
143 | 143 | public function removeHtmlClass(string $htmlClass): void |
144 | 144 | { |
145 | 145 | $htmlClass = sanitize_key($htmlClass); |
146 | - unset($this->htmlClasses[ $htmlClass ]); |
|
146 | + unset($this->htmlClasses[$htmlClass]); |
|
147 | 147 | } |
148 | 148 | |
149 | 149 |
@@ -15,141 +15,141 @@ |
||
15 | 15 | class HelpText implements JsonableInterface |
16 | 16 | { |
17 | 17 | |
18 | - /** |
|
19 | - * @var JsonDataHandler |
|
20 | - */ |
|
21 | - private $json_data_handler; |
|
22 | - |
|
23 | - /** |
|
24 | - * Additional text displayed alongside a form input to assist users with completing the form. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - private $helpText; |
|
29 | - |
|
30 | - /** |
|
31 | - * Custom HTML classes to be applied to this form input's help text. |
|
32 | - * |
|
33 | - * @var array |
|
34 | - */ |
|
35 | - private $htmlClasses; |
|
36 | - |
|
37 | - |
|
38 | - /** |
|
39 | - * HelpText constructor. |
|
40 | - * |
|
41 | - * @param JsonDataHandler $json_data_handler |
|
42 | - * @param array $htmlClass |
|
43 | - * @param string $helpText |
|
44 | - */ |
|
45 | - public function __construct( |
|
46 | - JsonDataHandler $json_data_handler, |
|
47 | - array $htmlClass, |
|
48 | - string $helpText |
|
49 | - ) { |
|
50 | - $this->json_data_handler = $json_data_handler; |
|
51 | - $this->setHtmlClasses($htmlClass); |
|
52 | - $this->setHelpText($helpText); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * @param string $json |
|
58 | - * @return HelpText |
|
59 | - */ |
|
60 | - public static function fromJson(string $json): HelpText |
|
61 | - { |
|
62 | - $json_data_handler = new JsonDataHandler(); |
|
63 | - $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
64 | - $data = $json_data_handler->decodeJson($json); |
|
65 | - $htmlClass = (array) ($data->htmlClass ?? []); |
|
66 | - $helpText = $data->helpText ?? ''; |
|
67 | - return new HelpText($json_data_handler, $htmlClass, $helpText); |
|
68 | - } |
|
69 | - |
|
70 | - |
|
71 | - /** |
|
72 | - * @return array |
|
73 | - */ |
|
74 | - public function toArray(): array |
|
75 | - { |
|
76 | - return [ |
|
77 | - 'helpText' => $this->helpText, |
|
78 | - 'htmlClass' => $this->htmlClasses(), |
|
79 | - ]; |
|
80 | - } |
|
81 | - |
|
82 | - |
|
83 | - /** |
|
84 | - * @return string |
|
85 | - */ |
|
86 | - public function toJson(): string |
|
87 | - { |
|
88 | - return $this->json_data_handler->encodeData($this->toArray()); |
|
89 | - } |
|
90 | - |
|
91 | - |
|
92 | - /** |
|
93 | - * Input label displayed on public forms, ie: the actual question text. |
|
94 | - * |
|
95 | - * @return string |
|
96 | - */ |
|
97 | - public function helpText(): string |
|
98 | - { |
|
99 | - return $this->helpText; |
|
100 | - } |
|
101 | - |
|
102 | - |
|
103 | - /** |
|
104 | - * @param string $helpText |
|
105 | - */ |
|
106 | - public function setHelpText(string $helpText): void |
|
107 | - { |
|
108 | - $this->helpText = sanitize_text_field($helpText); |
|
109 | - } |
|
110 | - |
|
111 | - |
|
112 | - /** |
|
113 | - * Custom HTML classes to be applied to this form input's help text. |
|
114 | - * returns a concatenated string unless $as_array is set to true |
|
115 | - * |
|
116 | - * @return array|string |
|
117 | - */ |
|
118 | - public function htmlClasses($as_array = false) |
|
119 | - { |
|
120 | - return $as_array |
|
121 | - ? $this->htmlClasses |
|
122 | - : implode(' ', $this->htmlClasses); |
|
123 | - } |
|
124 | - |
|
125 | - |
|
126 | - /** |
|
127 | - * @param string $htmlClass |
|
128 | - */ |
|
129 | - public function addHtmlClass(string $htmlClass): void |
|
130 | - { |
|
131 | - $htmlClass = sanitize_key($htmlClass); |
|
132 | - if (! in_array($htmlClass, $this->htmlClasses, true)) { |
|
133 | - $this->htmlClasses[] = $htmlClass; |
|
134 | - } |
|
135 | - } |
|
136 | - |
|
137 | - |
|
138 | - /** |
|
139 | - * @param string $htmlClass |
|
140 | - */ |
|
141 | - public function removeHtmlClass(string $htmlClass): void |
|
142 | - { |
|
143 | - $htmlClass = sanitize_key($htmlClass); |
|
144 | - unset($this->htmlClasses[ $htmlClass ]); |
|
145 | - } |
|
146 | - |
|
147 | - |
|
148 | - /** |
|
149 | - * @param array $htmlClasses |
|
150 | - */ |
|
151 | - public function setHtmlClasses(array $htmlClasses): void |
|
152 | - { |
|
153 | - $this->htmlClasses = array_map('sanitize_key', $htmlClasses); |
|
154 | - } |
|
18 | + /** |
|
19 | + * @var JsonDataHandler |
|
20 | + */ |
|
21 | + private $json_data_handler; |
|
22 | + |
|
23 | + /** |
|
24 | + * Additional text displayed alongside a form input to assist users with completing the form. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + private $helpText; |
|
29 | + |
|
30 | + /** |
|
31 | + * Custom HTML classes to be applied to this form input's help text. |
|
32 | + * |
|
33 | + * @var array |
|
34 | + */ |
|
35 | + private $htmlClasses; |
|
36 | + |
|
37 | + |
|
38 | + /** |
|
39 | + * HelpText constructor. |
|
40 | + * |
|
41 | + * @param JsonDataHandler $json_data_handler |
|
42 | + * @param array $htmlClass |
|
43 | + * @param string $helpText |
|
44 | + */ |
|
45 | + public function __construct( |
|
46 | + JsonDataHandler $json_data_handler, |
|
47 | + array $htmlClass, |
|
48 | + string $helpText |
|
49 | + ) { |
|
50 | + $this->json_data_handler = $json_data_handler; |
|
51 | + $this->setHtmlClasses($htmlClass); |
|
52 | + $this->setHelpText($helpText); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * @param string $json |
|
58 | + * @return HelpText |
|
59 | + */ |
|
60 | + public static function fromJson(string $json): HelpText |
|
61 | + { |
|
62 | + $json_data_handler = new JsonDataHandler(); |
|
63 | + $json_data_handler->configure(JsonDataHandler::DATA_TYPE_OBJECT); |
|
64 | + $data = $json_data_handler->decodeJson($json); |
|
65 | + $htmlClass = (array) ($data->htmlClass ?? []); |
|
66 | + $helpText = $data->helpText ?? ''; |
|
67 | + return new HelpText($json_data_handler, $htmlClass, $helpText); |
|
68 | + } |
|
69 | + |
|
70 | + |
|
71 | + /** |
|
72 | + * @return array |
|
73 | + */ |
|
74 | + public function toArray(): array |
|
75 | + { |
|
76 | + return [ |
|
77 | + 'helpText' => $this->helpText, |
|
78 | + 'htmlClass' => $this->htmlClasses(), |
|
79 | + ]; |
|
80 | + } |
|
81 | + |
|
82 | + |
|
83 | + /** |
|
84 | + * @return string |
|
85 | + */ |
|
86 | + public function toJson(): string |
|
87 | + { |
|
88 | + return $this->json_data_handler->encodeData($this->toArray()); |
|
89 | + } |
|
90 | + |
|
91 | + |
|
92 | + /** |
|
93 | + * Input label displayed on public forms, ie: the actual question text. |
|
94 | + * |
|
95 | + * @return string |
|
96 | + */ |
|
97 | + public function helpText(): string |
|
98 | + { |
|
99 | + return $this->helpText; |
|
100 | + } |
|
101 | + |
|
102 | + |
|
103 | + /** |
|
104 | + * @param string $helpText |
|
105 | + */ |
|
106 | + public function setHelpText(string $helpText): void |
|
107 | + { |
|
108 | + $this->helpText = sanitize_text_field($helpText); |
|
109 | + } |
|
110 | + |
|
111 | + |
|
112 | + /** |
|
113 | + * Custom HTML classes to be applied to this form input's help text. |
|
114 | + * returns a concatenated string unless $as_array is set to true |
|
115 | + * |
|
116 | + * @return array|string |
|
117 | + */ |
|
118 | + public function htmlClasses($as_array = false) |
|
119 | + { |
|
120 | + return $as_array |
|
121 | + ? $this->htmlClasses |
|
122 | + : implode(' ', $this->htmlClasses); |
|
123 | + } |
|
124 | + |
|
125 | + |
|
126 | + /** |
|
127 | + * @param string $htmlClass |
|
128 | + */ |
|
129 | + public function addHtmlClass(string $htmlClass): void |
|
130 | + { |
|
131 | + $htmlClass = sanitize_key($htmlClass); |
|
132 | + if (! in_array($htmlClass, $this->htmlClasses, true)) { |
|
133 | + $this->htmlClasses[] = $htmlClass; |
|
134 | + } |
|
135 | + } |
|
136 | + |
|
137 | + |
|
138 | + /** |
|
139 | + * @param string $htmlClass |
|
140 | + */ |
|
141 | + public function removeHtmlClass(string $htmlClass): void |
|
142 | + { |
|
143 | + $htmlClass = sanitize_key($htmlClass); |
|
144 | + unset($this->htmlClasses[ $htmlClass ]); |
|
145 | + } |
|
146 | + |
|
147 | + |
|
148 | + /** |
|
149 | + * @param array $htmlClasses |
|
150 | + */ |
|
151 | + public function setHtmlClasses(array $htmlClasses): void |
|
152 | + { |
|
153 | + $this->htmlClasses = array_map('sanitize_key', $htmlClasses); |
|
154 | + } |
|
155 | 155 | } |
@@ -10,145 +10,145 @@ |
||
10 | 10 | class EEM_Answer extends EEM_Base |
11 | 11 | { |
12 | 12 | |
13 | - /** |
|
14 | - * private instance of the EEM_Answer object |
|
15 | - * @type EEM_Answer |
|
16 | - */ |
|
17 | - protected static $_instance = null; |
|
13 | + /** |
|
14 | + * private instance of the EEM_Answer object |
|
15 | + * @type EEM_Answer |
|
16 | + */ |
|
17 | + protected static $_instance = null; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Mapping from system question ids to attendee field names |
|
21 | - * @type array |
|
22 | - * @deprecated since version 4.8.8 |
|
23 | - */ |
|
24 | - protected $_question_id_to_att_field_map = array( |
|
25 | - EEM_Attendee::fname_question_id => 'ATT_fname', |
|
26 | - EEM_Attendee::lname_question_id => 'ATT_lname', |
|
27 | - EEM_Attendee::email_question_id => 'ATT_email', |
|
28 | - EEM_Attendee::address_question_id => 'ATT_address', |
|
29 | - EEM_Attendee::address2_question_id => 'ATT_address2', |
|
30 | - EEM_Attendee::city_question_id => 'ATT_city', |
|
31 | - EEM_Attendee::state_question_id => 'STA_ID', |
|
32 | - EEM_Attendee::country_question_id => 'CNT_ISO', |
|
33 | - EEM_Attendee::zip_question_id => 'ATT_zip', |
|
34 | - EEM_Attendee::phone_question_id => 'ATT_phone' |
|
35 | - ); |
|
19 | + /** |
|
20 | + * Mapping from system question ids to attendee field names |
|
21 | + * @type array |
|
22 | + * @deprecated since version 4.8.8 |
|
23 | + */ |
|
24 | + protected $_question_id_to_att_field_map = array( |
|
25 | + EEM_Attendee::fname_question_id => 'ATT_fname', |
|
26 | + EEM_Attendee::lname_question_id => 'ATT_lname', |
|
27 | + EEM_Attendee::email_question_id => 'ATT_email', |
|
28 | + EEM_Attendee::address_question_id => 'ATT_address', |
|
29 | + EEM_Attendee::address2_question_id => 'ATT_address2', |
|
30 | + EEM_Attendee::city_question_id => 'ATT_city', |
|
31 | + EEM_Attendee::state_question_id => 'STA_ID', |
|
32 | + EEM_Attendee::country_question_id => 'CNT_ISO', |
|
33 | + EEM_Attendee::zip_question_id => 'ATT_zip', |
|
34 | + EEM_Attendee::phone_question_id => 'ATT_phone' |
|
35 | + ); |
|
36 | 36 | |
37 | 37 | |
38 | 38 | |
39 | - /** |
|
40 | - * constructor |
|
41 | - */ |
|
42 | - protected function __construct($timezone = null) |
|
43 | - { |
|
44 | - $this->singular_item = __('Answer', 'event_espresso'); |
|
45 | - $this->plural_item = __('Answers', 'event_espresso'); |
|
46 | - $this->_tables = array( |
|
47 | - 'Answer' => new EE_Primary_Table('esp_answer', 'ANS_ID') |
|
48 | - ); |
|
49 | - $this->_fields = array( |
|
50 | - 'Answer' => array( |
|
51 | - 'ANS_ID' => new EE_Primary_Key_Int_Field('ANS_ID', __('Answer ID', 'event_espresso')), |
|
52 | - 'REG_ID' => new EE_Foreign_Key_Int_Field('REG_ID', __('Registration ID', 'event_espresso'), false, 0, 'Registration'), |
|
53 | - 'QST_ID' => new EE_Foreign_Key_Int_Field('QST_ID', __('Question ID', 'event_espresso'), false, 0, 'Question'), |
|
54 | - 'ANS_value' => new EE_Maybe_Serialized_Simple_HTML_Field('ANS_value', __('Answer Value', 'event_espresso'), false, '') |
|
55 | - )); |
|
56 | - $this->_model_relations = array( |
|
57 | - 'Registration' => new EE_Belongs_To_Relation(), |
|
58 | - 'Question' => new EE_Belongs_To_Relation() |
|
59 | - ); |
|
60 | - $this->_model_chain_to_wp_user = 'Registration.Event'; |
|
61 | - $this->_caps_slug = 'registrations'; |
|
62 | - parent::__construct($timezone); |
|
63 | - } |
|
39 | + /** |
|
40 | + * constructor |
|
41 | + */ |
|
42 | + protected function __construct($timezone = null) |
|
43 | + { |
|
44 | + $this->singular_item = __('Answer', 'event_espresso'); |
|
45 | + $this->plural_item = __('Answers', 'event_espresso'); |
|
46 | + $this->_tables = array( |
|
47 | + 'Answer' => new EE_Primary_Table('esp_answer', 'ANS_ID') |
|
48 | + ); |
|
49 | + $this->_fields = array( |
|
50 | + 'Answer' => array( |
|
51 | + 'ANS_ID' => new EE_Primary_Key_Int_Field('ANS_ID', __('Answer ID', 'event_espresso')), |
|
52 | + 'REG_ID' => new EE_Foreign_Key_Int_Field('REG_ID', __('Registration ID', 'event_espresso'), false, 0, 'Registration'), |
|
53 | + 'QST_ID' => new EE_Foreign_Key_Int_Field('QST_ID', __('Question ID', 'event_espresso'), false, 0, 'Question'), |
|
54 | + 'ANS_value' => new EE_Maybe_Serialized_Simple_HTML_Field('ANS_value', __('Answer Value', 'event_espresso'), false, '') |
|
55 | + )); |
|
56 | + $this->_model_relations = array( |
|
57 | + 'Registration' => new EE_Belongs_To_Relation(), |
|
58 | + 'Question' => new EE_Belongs_To_Relation() |
|
59 | + ); |
|
60 | + $this->_model_chain_to_wp_user = 'Registration.Event'; |
|
61 | + $this->_caps_slug = 'registrations'; |
|
62 | + parent::__construct($timezone); |
|
63 | + } |
|
64 | 64 | |
65 | 65 | |
66 | 66 | |
67 | - /** |
|
68 | - * Gets the string answer to the question for this registration (it could either be stored |
|
69 | - * on the attendee or in the answer table. This function finds its value regardless) |
|
70 | - * @param EE_Registration $registration |
|
71 | - * @param int $question_id |
|
72 | - * @param boolean $pretty_answer whether to call 'pretty_value' or just 'value' |
|
73 | - * @return string |
|
74 | - */ |
|
75 | - public function get_answer_value_to_question(EE_Registration $registration, $question_id = null, $pretty_answer = false) |
|
76 | - { |
|
77 | - $value = $this->get_attendee_property_answer_value($registration, $question_id, $pretty_answer); |
|
78 | - if ($value === null) { |
|
79 | - $answer_obj = $this->get_registration_question_answer_object($registration, $question_id); |
|
80 | - if ($answer_obj instanceof EE_Answer) { |
|
81 | - if ($pretty_answer) { |
|
82 | - $value = $answer_obj->pretty_value(); |
|
83 | - } else { |
|
84 | - $value = $answer_obj->value(); |
|
85 | - } |
|
86 | - } |
|
87 | - } |
|
88 | - return apply_filters('FHEE__EEM_Answer__get_answer_value_to_question__answer_value', $value, $registration, $question_id); |
|
89 | - } |
|
67 | + /** |
|
68 | + * Gets the string answer to the question for this registration (it could either be stored |
|
69 | + * on the attendee or in the answer table. This function finds its value regardless) |
|
70 | + * @param EE_Registration $registration |
|
71 | + * @param int $question_id |
|
72 | + * @param boolean $pretty_answer whether to call 'pretty_value' or just 'value' |
|
73 | + * @return string |
|
74 | + */ |
|
75 | + public function get_answer_value_to_question(EE_Registration $registration, $question_id = null, $pretty_answer = false) |
|
76 | + { |
|
77 | + $value = $this->get_attendee_property_answer_value($registration, $question_id, $pretty_answer); |
|
78 | + if ($value === null) { |
|
79 | + $answer_obj = $this->get_registration_question_answer_object($registration, $question_id); |
|
80 | + if ($answer_obj instanceof EE_Answer) { |
|
81 | + if ($pretty_answer) { |
|
82 | + $value = $answer_obj->pretty_value(); |
|
83 | + } else { |
|
84 | + $value = $answer_obj->value(); |
|
85 | + } |
|
86 | + } |
|
87 | + } |
|
88 | + return apply_filters('FHEE__EEM_Answer__get_answer_value_to_question__answer_value', $value, $registration, $question_id); |
|
89 | + } |
|
90 | 90 | |
91 | 91 | |
92 | 92 | |
93 | - /** |
|
94 | - * Gets the EE_Answer object for the question for this registration (if it exists) |
|
95 | - * @param EE_Registration $registration |
|
96 | - * @param int $question_id |
|
97 | - * @return EE_Answer |
|
98 | - */ |
|
99 | - public function get_registration_question_answer_object(EE_Registration $registration, $question_id = null) |
|
100 | - { |
|
101 | - $answer_obj = $this->get_one(array( array( 'QST_ID' => $question_id, 'REG_ID' => $registration->ID() ))); |
|
102 | - return apply_filters('FHEE__EEM_Answer__get_registration_question_answer_object__answer_obj', $answer_obj, $registration, $question_id); |
|
103 | - } |
|
93 | + /** |
|
94 | + * Gets the EE_Answer object for the question for this registration (if it exists) |
|
95 | + * @param EE_Registration $registration |
|
96 | + * @param int $question_id |
|
97 | + * @return EE_Answer |
|
98 | + */ |
|
99 | + public function get_registration_question_answer_object(EE_Registration $registration, $question_id = null) |
|
100 | + { |
|
101 | + $answer_obj = $this->get_one(array( array( 'QST_ID' => $question_id, 'REG_ID' => $registration->ID() ))); |
|
102 | + return apply_filters('FHEE__EEM_Answer__get_registration_question_answer_object__answer_obj', $answer_obj, $registration, $question_id); |
|
103 | + } |
|
104 | 104 | |
105 | 105 | |
106 | - /** |
|
107 | - * Gets the string answer to the question for this registration's attendee |
|
108 | - * |
|
109 | - * @param EE_Registration $registration |
|
110 | - * @param int|string $question_system_id if an INT this is understood to be the question's ID; if a string |
|
111 | - * then it should be its QST_system value. Passing in the QST_system |
|
112 | - * value is more efficient |
|
113 | - * @param boolean $pretty_answer |
|
114 | - * @return string|null (if the registration has no attendee, or the question_system_id is not a QST_ID or |
|
115 | - * QST_system for a question corresponding to an attendee field, returns |
|
116 | - * null) |
|
117 | - * @throws EE_Error |
|
118 | - * @throws ReflectionException |
|
119 | - */ |
|
120 | - public function get_attendee_property_answer_value(EE_Registration $registration, $question_system_id = null, $pretty_answer = false) |
|
121 | - { |
|
122 | - $value = null; |
|
123 | - // backward compat: we still want to find the question's ID |
|
124 | - if (is_numeric($question_system_id)) { |
|
125 | - // find this question's QST_system value |
|
126 | - $question_id = $question_system_id; |
|
127 | - $question_system_id = EEM_Question::instance()->get_var(array( array( 'QST_ID' => $question_system_id ) ), 'QST_system'); |
|
128 | - } else { |
|
129 | - $question_id = (int) EEM_Question::instance()->get_var(array( array( 'QST_system' => $question_system_id ) ), 'QST_ID'); |
|
130 | - } |
|
131 | - // only bother checking if the registration has an attendee |
|
132 | - if ($registration->attendee() instanceof EE_Attendee) { |
|
133 | - $field_name = EEM_Attendee::instance()->get_attendee_field_for_system_question($question_system_id); |
|
134 | - if ($field_name) { |
|
135 | - if ($pretty_answer) { |
|
136 | - if ($field_name === 'STA_ID') { |
|
137 | - $state = $registration->attendee()->state_obj(); |
|
138 | - $value = $state instanceof EE_State ? $state->name() : sprintf(__('Unknown State (%s)', 'event_espresso'), $registration->attendee()->state_ID()); |
|
139 | - } elseif ($field_name === 'CNT_ISO') { |
|
140 | - $country = $registration->attendee()->country_obj(); |
|
141 | - $value = $country instanceof EE_Country ? $country->name() : sprintf(__('Unknown Country (%s)', "event_espresso"), $registration->attendee()->country_ID()); |
|
142 | - } else { |
|
143 | - $value = $registration->attendee()->get_pretty($field_name); |
|
144 | - } |
|
145 | - // if field name is blank, leave the value as null too |
|
146 | - } else { |
|
147 | - $value = $registration->attendee()->get($field_name); |
|
148 | - } |
|
149 | - } |
|
150 | - // if no field was found, leave value blank |
|
151 | - } |
|
152 | - return apply_filters('FHEE__EEM_Answer__get_attendee_question_answer_value__answer_value', $value, $registration, $question_id, $question_system_id); |
|
153 | - } |
|
106 | + /** |
|
107 | + * Gets the string answer to the question for this registration's attendee |
|
108 | + * |
|
109 | + * @param EE_Registration $registration |
|
110 | + * @param int|string $question_system_id if an INT this is understood to be the question's ID; if a string |
|
111 | + * then it should be its QST_system value. Passing in the QST_system |
|
112 | + * value is more efficient |
|
113 | + * @param boolean $pretty_answer |
|
114 | + * @return string|null (if the registration has no attendee, or the question_system_id is not a QST_ID or |
|
115 | + * QST_system for a question corresponding to an attendee field, returns |
|
116 | + * null) |
|
117 | + * @throws EE_Error |
|
118 | + * @throws ReflectionException |
|
119 | + */ |
|
120 | + public function get_attendee_property_answer_value(EE_Registration $registration, $question_system_id = null, $pretty_answer = false) |
|
121 | + { |
|
122 | + $value = null; |
|
123 | + // backward compat: we still want to find the question's ID |
|
124 | + if (is_numeric($question_system_id)) { |
|
125 | + // find this question's QST_system value |
|
126 | + $question_id = $question_system_id; |
|
127 | + $question_system_id = EEM_Question::instance()->get_var(array( array( 'QST_ID' => $question_system_id ) ), 'QST_system'); |
|
128 | + } else { |
|
129 | + $question_id = (int) EEM_Question::instance()->get_var(array( array( 'QST_system' => $question_system_id ) ), 'QST_ID'); |
|
130 | + } |
|
131 | + // only bother checking if the registration has an attendee |
|
132 | + if ($registration->attendee() instanceof EE_Attendee) { |
|
133 | + $field_name = EEM_Attendee::instance()->get_attendee_field_for_system_question($question_system_id); |
|
134 | + if ($field_name) { |
|
135 | + if ($pretty_answer) { |
|
136 | + if ($field_name === 'STA_ID') { |
|
137 | + $state = $registration->attendee()->state_obj(); |
|
138 | + $value = $state instanceof EE_State ? $state->name() : sprintf(__('Unknown State (%s)', 'event_espresso'), $registration->attendee()->state_ID()); |
|
139 | + } elseif ($field_name === 'CNT_ISO') { |
|
140 | + $country = $registration->attendee()->country_obj(); |
|
141 | + $value = $country instanceof EE_Country ? $country->name() : sprintf(__('Unknown Country (%s)', "event_espresso"), $registration->attendee()->country_ID()); |
|
142 | + } else { |
|
143 | + $value = $registration->attendee()->get_pretty($field_name); |
|
144 | + } |
|
145 | + // if field name is blank, leave the value as null too |
|
146 | + } else { |
|
147 | + $value = $registration->attendee()->get($field_name); |
|
148 | + } |
|
149 | + } |
|
150 | + // if no field was found, leave value blank |
|
151 | + } |
|
152 | + return apply_filters('FHEE__EEM_Answer__get_attendee_question_answer_value__answer_value', $value, $registration, $question_id, $question_system_id); |
|
153 | + } |
|
154 | 154 | } |
@@ -152,10 +152,10 @@ |
||
152 | 152 | ]; |
153 | 153 | // this model is generally available for reading |
154 | 154 | $restrictions = []; |
155 | - $restrictions[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
156 | - $restrictions[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
157 | - $restrictions[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
158 | - $restrictions[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
155 | + $restrictions[EEM_Base::caps_read] = new EE_Restriction_Generator_Public(); |
|
156 | + $restrictions[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
157 | + $restrictions[EEM_Base::caps_edit] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
158 | + $restrictions[EEM_Base::caps_delete] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
159 | 159 | $this->_cap_restriction_generators = $restrictions; |
160 | 160 | parent::__construct($timezone); |
161 | 161 | $this->request = $this->getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
@@ -21,232 +21,232 @@ |
||
21 | 21 | */ |
22 | 22 | class EEM_Form_Section extends EEM_Base |
23 | 23 | { |
24 | - public const APPLIES_TO_ALL = 'all'; |
|
25 | - |
|
26 | - public const APPLIES_TO_PRIMARY = 'primary'; |
|
27 | - |
|
28 | - public const APPLIES_TO_PURCHASER = 'purchaser'; |
|
29 | - |
|
30 | - public const APPLIES_TO_REGISTRANTS = 'registrants'; |
|
31 | - |
|
32 | - /** |
|
33 | - * @var EEM_Form_Section |
|
34 | - */ |
|
35 | - protected static $_instance; |
|
36 | - |
|
37 | - /** |
|
38 | - * @var RequestInterface |
|
39 | - */ |
|
40 | - private $request; |
|
41 | - |
|
42 | - /** |
|
43 | - * @var array |
|
44 | - */ |
|
45 | - private $valid_applies_to_options; |
|
46 | - |
|
47 | - |
|
48 | - /** |
|
49 | - * EEM_Form_Section constructor. |
|
50 | - * |
|
51 | - * @param FormStatus $form_status |
|
52 | - * @param string|null $timezone |
|
53 | - * @throws EE_Error |
|
54 | - */ |
|
55 | - protected function __construct(FormStatus $form_status, ?string $timezone) |
|
56 | - { |
|
57 | - $this->valid_applies_to_options = apply_filters( |
|
58 | - 'FHEE__EEM_Form_Section__valid_applies_to_options', |
|
59 | - [ |
|
60 | - EEM_Form_Section::APPLIES_TO_ALL => esc_html__('All Registrants', 'event_espresso'), |
|
61 | - EEM_Form_Section::APPLIES_TO_PRIMARY => esc_html__('Primary Registrant Only', 'event_espresso'), |
|
62 | - EEM_Form_Section::APPLIES_TO_PURCHASER => esc_html__('Purchasing Agent', 'event_espresso'), |
|
63 | - EEM_Form_Section::APPLIES_TO_REGISTRANTS => esc_html__('Additional Registrants', 'event_espresso'), |
|
64 | - ] |
|
65 | - ); |
|
66 | - |
|
67 | - $this->singular_item = esc_html__('Form Section', 'event_espresso'); |
|
68 | - $this->plural_item = esc_html__('Form Sections', 'event_espresso'); |
|
69 | - |
|
70 | - $this->_tables = [ |
|
71 | - 'Form_Section' => new EE_Primary_Table('esp_form_section', 'FSC_UUID'), |
|
72 | - ]; |
|
73 | - $this->_fields = [ |
|
74 | - 'Form_Section' => [ |
|
75 | - 'FSC_UUID' => new EE_Primary_Key_String_Field( |
|
76 | - 'FSC_UUID', |
|
77 | - esc_html__('Form Section UUID (universally unique identifier)', 'event_espresso') |
|
78 | - ), |
|
79 | - 'FSC_appliesTo' => new EE_Enum_Text_Field( |
|
80 | - 'FSC_appliesTo', |
|
81 | - esc_html( |
|
82 | - sprintf( |
|
83 | - /* translators: 1 class name */ |
|
84 | - __( |
|
85 | - 'Form user type that this form section should be presented to. Values correspond to the %s constants.', |
|
86 | - 'event_espresso' |
|
87 | - ), |
|
88 | - 'EEM_Form_Section::APPLIES_TO_*' |
|
89 | - ) |
|
90 | - ), |
|
91 | - false, |
|
92 | - EEM_Form_Section::APPLIES_TO_ALL, |
|
93 | - $this->valid_applies_to_options |
|
94 | - ), |
|
95 | - 'FSC_attributes' => new EE_JSON_Field( |
|
96 | - 'FSC_attributes', |
|
97 | - esc_html__( |
|
98 | - 'JSON string of HTML attributes, such as class, to be applied to this form section\'s container.', |
|
99 | - 'event_espresso' |
|
100 | - ), |
|
101 | - true, |
|
102 | - '{}' |
|
103 | - ), |
|
104 | - 'FSC_belongsTo' => new EE_Plain_Text_Field( |
|
105 | - 'FSC_belongsTo', |
|
106 | - esc_html__('UUID of parent form section that this one belongs to.', 'event_espresso'), |
|
107 | - true, |
|
108 | - null |
|
109 | - ), |
|
110 | - 'FSC_label' => new EE_JSON_Field( |
|
111 | - 'FSC_label', |
|
112 | - esc_html__( |
|
113 | - 'JSON string of properties pertaining to a form section\'s label.', |
|
114 | - 'event_espresso' |
|
115 | - ), |
|
116 | - true, |
|
117 | - '{}' |
|
118 | - ), |
|
119 | - 'FSC_order' => new EE_Integer_Field( |
|
120 | - 'FSC_order', |
|
121 | - esc_html__('Order in which form section appears in a form.', 'event_espresso'), |
|
122 | - false, |
|
123 | - 0 |
|
124 | - ), |
|
125 | - 'FSC_status' => new EE_Enum_Text_Field( |
|
126 | - 'FSC_status', |
|
127 | - esc_html( |
|
128 | - sprintf( |
|
129 | - /* translators: 1 class name */ |
|
130 | - __( |
|
131 | - 'Whether form section is active, archived, shared, trashed, or used as a default on new forms. Values correspond to the %1$s class constants.', |
|
132 | - 'event_espresso' |
|
133 | - ), |
|
134 | - 'EventEspresso\core\services\form\meta\FormStatus' |
|
135 | - ) |
|
136 | - ), |
|
137 | - false, |
|
138 | - FormStatus::ACTIVE, |
|
139 | - $form_status->validStatusOptions() |
|
140 | - ), |
|
141 | - 'FSC_wpUser' => new EE_WP_User_Field( |
|
142 | - 'FSC_wpUser', |
|
143 | - esc_html__('ID of the WP User that created this form section.', 'event_espresso'), |
|
144 | - false |
|
145 | - ), |
|
146 | - ], |
|
147 | - ]; |
|
148 | - $this->_model_relations = [ |
|
149 | - 'Form_Element' => new EE_Has_Many_Relation(), |
|
150 | - 'Form_Submission' => new EE_Has_Many_Relation(), |
|
151 | - 'WP_User' => new EE_Belongs_To_Relation(), |
|
152 | - ]; |
|
153 | - // this model is generally available for reading |
|
154 | - $restrictions = []; |
|
155 | - $restrictions[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
156 | - $restrictions[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
157 | - $restrictions[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
158 | - $restrictions[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
159 | - $this->_cap_restriction_generators = $restrictions; |
|
160 | - parent::__construct($timezone); |
|
161 | - $this->request = $this->getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
162 | - } |
|
163 | - |
|
164 | - |
|
165 | - /** |
|
166 | - * @param array $query_params |
|
167 | - * @return array |
|
168 | - */ |
|
169 | - private function addDefaultWhereConditions(array $query_params): array |
|
170 | - { |
|
171 | - // might need to add a way to identify GQL requests for admin domains |
|
172 | - $admin_request = $this->request->isAdmin() || $this->request->isAdminAjax(); |
|
173 | - $query_params['default_where_conditions'] = $admin_request |
|
174 | - ? EEM_Base::default_where_conditions_none |
|
175 | - : EEM_Base::default_where_conditions_all; |
|
176 | - return $query_params; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * form sections should always be sorted in ascending order via the FSC_order field |
|
182 | - * |
|
183 | - * @param array $query_params |
|
184 | - * @return array |
|
185 | - */ |
|
186 | - private function addOrderByQueryParams(array $query_params): array |
|
187 | - { |
|
188 | - $query_params['order_by'] = ['FSC_order' => 'ASC']; |
|
189 | - return $query_params; |
|
190 | - } |
|
191 | - |
|
192 | - |
|
193 | - /** |
|
194 | - * returns an array of Form Sections for the specified parent Form Section |
|
195 | - * |
|
196 | - * @param string $FSC_UUID |
|
197 | - * @return EE_Form_Section[] |
|
198 | - * @throws EE_Error |
|
199 | - */ |
|
200 | - public function getChildFormSections(string $FSC_UUID): array |
|
201 | - { |
|
202 | - return $this->getFormSections(['FSC_belongsTo' => $FSC_UUID]); |
|
203 | - } |
|
204 | - |
|
205 | - |
|
206 | - /** |
|
207 | - * @return EE_Form_Section[] |
|
208 | - * @throws EE_Error |
|
209 | - */ |
|
210 | - public function getFormSections(array $where_params): array |
|
211 | - { |
|
212 | - $query_params = $this->addDefaultWhereConditions([$where_params]); |
|
213 | - $query_params = $this->addOrderByQueryParams($query_params); |
|
214 | - return $this->get_all($query_params); |
|
215 | - } |
|
216 | - |
|
217 | - |
|
218 | - /** |
|
219 | - * returns an array of Form Sections for the specified Event |
|
220 | - * |
|
221 | - * @param EE_Event $event |
|
222 | - * @return EE_Form_Section[] |
|
223 | - * @throws EE_Error |
|
224 | - * @throws ReflectionException |
|
225 | - */ |
|
226 | - public function getFormSectionsForEvent(EE_Event $event): array |
|
227 | - { |
|
228 | - $FSC_UUID = $event->registrationFormUuid(); |
|
229 | - return ! empty($FSC_UUID) |
|
230 | - ? $this->getFormSections( |
|
231 | - [ |
|
232 | - 'OR' => [ |
|
233 | - 'FSC_UUID' => $FSC_UUID, // top level form |
|
234 | - 'FSC_belongsTo' => $FSC_UUID, // child form sections |
|
235 | - ] |
|
236 | - ] |
|
237 | - ) |
|
238 | - : []; |
|
239 | - } |
|
240 | - |
|
241 | - |
|
242 | - /** |
|
243 | - * @param bool $constants_only |
|
244 | - * @return array |
|
245 | - */ |
|
246 | - public function validAppliesToOptions(bool $constants_only = false): array |
|
247 | - { |
|
248 | - return $constants_only |
|
249 | - ? array_keys($this->valid_applies_to_options) |
|
250 | - : $this->valid_applies_to_options; |
|
251 | - } |
|
24 | + public const APPLIES_TO_ALL = 'all'; |
|
25 | + |
|
26 | + public const APPLIES_TO_PRIMARY = 'primary'; |
|
27 | + |
|
28 | + public const APPLIES_TO_PURCHASER = 'purchaser'; |
|
29 | + |
|
30 | + public const APPLIES_TO_REGISTRANTS = 'registrants'; |
|
31 | + |
|
32 | + /** |
|
33 | + * @var EEM_Form_Section |
|
34 | + */ |
|
35 | + protected static $_instance; |
|
36 | + |
|
37 | + /** |
|
38 | + * @var RequestInterface |
|
39 | + */ |
|
40 | + private $request; |
|
41 | + |
|
42 | + /** |
|
43 | + * @var array |
|
44 | + */ |
|
45 | + private $valid_applies_to_options; |
|
46 | + |
|
47 | + |
|
48 | + /** |
|
49 | + * EEM_Form_Section constructor. |
|
50 | + * |
|
51 | + * @param FormStatus $form_status |
|
52 | + * @param string|null $timezone |
|
53 | + * @throws EE_Error |
|
54 | + */ |
|
55 | + protected function __construct(FormStatus $form_status, ?string $timezone) |
|
56 | + { |
|
57 | + $this->valid_applies_to_options = apply_filters( |
|
58 | + 'FHEE__EEM_Form_Section__valid_applies_to_options', |
|
59 | + [ |
|
60 | + EEM_Form_Section::APPLIES_TO_ALL => esc_html__('All Registrants', 'event_espresso'), |
|
61 | + EEM_Form_Section::APPLIES_TO_PRIMARY => esc_html__('Primary Registrant Only', 'event_espresso'), |
|
62 | + EEM_Form_Section::APPLIES_TO_PURCHASER => esc_html__('Purchasing Agent', 'event_espresso'), |
|
63 | + EEM_Form_Section::APPLIES_TO_REGISTRANTS => esc_html__('Additional Registrants', 'event_espresso'), |
|
64 | + ] |
|
65 | + ); |
|
66 | + |
|
67 | + $this->singular_item = esc_html__('Form Section', 'event_espresso'); |
|
68 | + $this->plural_item = esc_html__('Form Sections', 'event_espresso'); |
|
69 | + |
|
70 | + $this->_tables = [ |
|
71 | + 'Form_Section' => new EE_Primary_Table('esp_form_section', 'FSC_UUID'), |
|
72 | + ]; |
|
73 | + $this->_fields = [ |
|
74 | + 'Form_Section' => [ |
|
75 | + 'FSC_UUID' => new EE_Primary_Key_String_Field( |
|
76 | + 'FSC_UUID', |
|
77 | + esc_html__('Form Section UUID (universally unique identifier)', 'event_espresso') |
|
78 | + ), |
|
79 | + 'FSC_appliesTo' => new EE_Enum_Text_Field( |
|
80 | + 'FSC_appliesTo', |
|
81 | + esc_html( |
|
82 | + sprintf( |
|
83 | + /* translators: 1 class name */ |
|
84 | + __( |
|
85 | + 'Form user type that this form section should be presented to. Values correspond to the %s constants.', |
|
86 | + 'event_espresso' |
|
87 | + ), |
|
88 | + 'EEM_Form_Section::APPLIES_TO_*' |
|
89 | + ) |
|
90 | + ), |
|
91 | + false, |
|
92 | + EEM_Form_Section::APPLIES_TO_ALL, |
|
93 | + $this->valid_applies_to_options |
|
94 | + ), |
|
95 | + 'FSC_attributes' => new EE_JSON_Field( |
|
96 | + 'FSC_attributes', |
|
97 | + esc_html__( |
|
98 | + 'JSON string of HTML attributes, such as class, to be applied to this form section\'s container.', |
|
99 | + 'event_espresso' |
|
100 | + ), |
|
101 | + true, |
|
102 | + '{}' |
|
103 | + ), |
|
104 | + 'FSC_belongsTo' => new EE_Plain_Text_Field( |
|
105 | + 'FSC_belongsTo', |
|
106 | + esc_html__('UUID of parent form section that this one belongs to.', 'event_espresso'), |
|
107 | + true, |
|
108 | + null |
|
109 | + ), |
|
110 | + 'FSC_label' => new EE_JSON_Field( |
|
111 | + 'FSC_label', |
|
112 | + esc_html__( |
|
113 | + 'JSON string of properties pertaining to a form section\'s label.', |
|
114 | + 'event_espresso' |
|
115 | + ), |
|
116 | + true, |
|
117 | + '{}' |
|
118 | + ), |
|
119 | + 'FSC_order' => new EE_Integer_Field( |
|
120 | + 'FSC_order', |
|
121 | + esc_html__('Order in which form section appears in a form.', 'event_espresso'), |
|
122 | + false, |
|
123 | + 0 |
|
124 | + ), |
|
125 | + 'FSC_status' => new EE_Enum_Text_Field( |
|
126 | + 'FSC_status', |
|
127 | + esc_html( |
|
128 | + sprintf( |
|
129 | + /* translators: 1 class name */ |
|
130 | + __( |
|
131 | + 'Whether form section is active, archived, shared, trashed, or used as a default on new forms. Values correspond to the %1$s class constants.', |
|
132 | + 'event_espresso' |
|
133 | + ), |
|
134 | + 'EventEspresso\core\services\form\meta\FormStatus' |
|
135 | + ) |
|
136 | + ), |
|
137 | + false, |
|
138 | + FormStatus::ACTIVE, |
|
139 | + $form_status->validStatusOptions() |
|
140 | + ), |
|
141 | + 'FSC_wpUser' => new EE_WP_User_Field( |
|
142 | + 'FSC_wpUser', |
|
143 | + esc_html__('ID of the WP User that created this form section.', 'event_espresso'), |
|
144 | + false |
|
145 | + ), |
|
146 | + ], |
|
147 | + ]; |
|
148 | + $this->_model_relations = [ |
|
149 | + 'Form_Element' => new EE_Has_Many_Relation(), |
|
150 | + 'Form_Submission' => new EE_Has_Many_Relation(), |
|
151 | + 'WP_User' => new EE_Belongs_To_Relation(), |
|
152 | + ]; |
|
153 | + // this model is generally available for reading |
|
154 | + $restrictions = []; |
|
155 | + $restrictions[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
156 | + $restrictions[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
157 | + $restrictions[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
158 | + $restrictions[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Reg_Form('FSC_applies_to'); |
|
159 | + $this->_cap_restriction_generators = $restrictions; |
|
160 | + parent::__construct($timezone); |
|
161 | + $this->request = $this->getLoader()->getShared('EventEspresso\core\services\request\RequestInterface'); |
|
162 | + } |
|
163 | + |
|
164 | + |
|
165 | + /** |
|
166 | + * @param array $query_params |
|
167 | + * @return array |
|
168 | + */ |
|
169 | + private function addDefaultWhereConditions(array $query_params): array |
|
170 | + { |
|
171 | + // might need to add a way to identify GQL requests for admin domains |
|
172 | + $admin_request = $this->request->isAdmin() || $this->request->isAdminAjax(); |
|
173 | + $query_params['default_where_conditions'] = $admin_request |
|
174 | + ? EEM_Base::default_where_conditions_none |
|
175 | + : EEM_Base::default_where_conditions_all; |
|
176 | + return $query_params; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * form sections should always be sorted in ascending order via the FSC_order field |
|
182 | + * |
|
183 | + * @param array $query_params |
|
184 | + * @return array |
|
185 | + */ |
|
186 | + private function addOrderByQueryParams(array $query_params): array |
|
187 | + { |
|
188 | + $query_params['order_by'] = ['FSC_order' => 'ASC']; |
|
189 | + return $query_params; |
|
190 | + } |
|
191 | + |
|
192 | + |
|
193 | + /** |
|
194 | + * returns an array of Form Sections for the specified parent Form Section |
|
195 | + * |
|
196 | + * @param string $FSC_UUID |
|
197 | + * @return EE_Form_Section[] |
|
198 | + * @throws EE_Error |
|
199 | + */ |
|
200 | + public function getChildFormSections(string $FSC_UUID): array |
|
201 | + { |
|
202 | + return $this->getFormSections(['FSC_belongsTo' => $FSC_UUID]); |
|
203 | + } |
|
204 | + |
|
205 | + |
|
206 | + /** |
|
207 | + * @return EE_Form_Section[] |
|
208 | + * @throws EE_Error |
|
209 | + */ |
|
210 | + public function getFormSections(array $where_params): array |
|
211 | + { |
|
212 | + $query_params = $this->addDefaultWhereConditions([$where_params]); |
|
213 | + $query_params = $this->addOrderByQueryParams($query_params); |
|
214 | + return $this->get_all($query_params); |
|
215 | + } |
|
216 | + |
|
217 | + |
|
218 | + /** |
|
219 | + * returns an array of Form Sections for the specified Event |
|
220 | + * |
|
221 | + * @param EE_Event $event |
|
222 | + * @return EE_Form_Section[] |
|
223 | + * @throws EE_Error |
|
224 | + * @throws ReflectionException |
|
225 | + */ |
|
226 | + public function getFormSectionsForEvent(EE_Event $event): array |
|
227 | + { |
|
228 | + $FSC_UUID = $event->registrationFormUuid(); |
|
229 | + return ! empty($FSC_UUID) |
|
230 | + ? $this->getFormSections( |
|
231 | + [ |
|
232 | + 'OR' => [ |
|
233 | + 'FSC_UUID' => $FSC_UUID, // top level form |
|
234 | + 'FSC_belongsTo' => $FSC_UUID, // child form sections |
|
235 | + ] |
|
236 | + ] |
|
237 | + ) |
|
238 | + : []; |
|
239 | + } |
|
240 | + |
|
241 | + |
|
242 | + /** |
|
243 | + * @param bool $constants_only |
|
244 | + * @return array |
|
245 | + */ |
|
246 | + public function validAppliesToOptions(bool $constants_only = false): array |
|
247 | + { |
|
248 | + return $constants_only |
|
249 | + ? array_keys($this->valid_applies_to_options) |
|
250 | + : $this->valid_applies_to_options; |
|
251 | + } |
|
252 | 252 | } |
@@ -160,7 +160,7 @@ discard block |
||
160 | 160 | */ |
161 | 161 | public function attributes(): ?Attributes |
162 | 162 | { |
163 | - if (! $this->attributes instanceof Attributes) { |
|
163 | + if ( ! $this->attributes instanceof Attributes) { |
|
164 | 164 | $this->attributes = Attributes::fromJson($this->get('FSC_attributes')); |
165 | 165 | } |
166 | 166 | return $this->attributes; |
@@ -226,7 +226,7 @@ discard block |
||
226 | 226 | $form_elements = $this->get_many_related('Form_Element', ['order_by' => ['FIN_order' => 'ASC']]); |
227 | 227 | foreach ($form_elements as $form_element) { |
228 | 228 | if ($form_element instanceof EE_Form_Element) { |
229 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
229 | + $this->form_elements[$form_element->UUID()] = $form_element; |
|
230 | 230 | } |
231 | 231 | } |
232 | 232 | return $this->form_elements; |
@@ -242,7 +242,7 @@ discard block |
||
242 | 242 | { |
243 | 243 | foreach ($form_elements as $form_element) { |
244 | 244 | if ($form_element instanceof EE_Form_Element) { |
245 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
245 | + $this->form_elements[$form_element->UUID()] = $form_element; |
|
246 | 246 | } |
247 | 247 | } |
248 | 248 | } |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | public function formElementFilter(): Closure |
274 | 274 | { |
275 | 275 | $FSC_UUID = strtolower($this->UUID()); |
276 | - return function ($form_element) use ($FSC_UUID) { |
|
276 | + return function($form_element) use ($FSC_UUID) { |
|
277 | 277 | return $form_element instanceof EE_Form_Element && strtolower($form_element->belongsTo()) === $FSC_UUID; |
278 | 278 | }; |
279 | 279 | } |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | */ |
289 | 289 | public function label(): ?FormLabel |
290 | 290 | { |
291 | - if (! $this->label instanceof FormLabel) { |
|
291 | + if ( ! $this->label instanceof FormLabel) { |
|
292 | 292 | $this->label = FormLabel::fromJson($this->get('FSC_label')); |
293 | 293 | } |
294 | 294 | return $this->label; |
@@ -23,469 +23,469 @@ |
||
23 | 23 | class EE_Form_Section extends EE_Base_Class |
24 | 24 | { |
25 | 25 | |
26 | - /** |
|
27 | - * @var Attributes |
|
28 | - */ |
|
29 | - private $attributes; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var EE_Form_Element[] |
|
33 | - */ |
|
34 | - private $form_elements = []; |
|
35 | - |
|
36 | - /** |
|
37 | - * @var FormLabel |
|
38 | - */ |
|
39 | - private $label; |
|
40 | - |
|
41 | - |
|
42 | - |
|
43 | - /** |
|
44 | - * @param array $props_n_values |
|
45 | - * @return EE_Form_Section |
|
46 | - * @throws EE_Error |
|
47 | - * @throws ReflectionException |
|
48 | - */ |
|
49 | - public static function new_instance(array $props_n_values = []): EE_Form_Section |
|
50 | - { |
|
51 | - $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
52 | - return $has_object ?: new self($props_n_values); |
|
53 | - } |
|
54 | - |
|
55 | - |
|
56 | - /** |
|
57 | - * @param array $props_n_values |
|
58 | - * @return EE_Form_Section |
|
59 | - * @throws EE_Error |
|
60 | - * @throws ReflectionException |
|
61 | - */ |
|
62 | - public static function new_instance_from_db(array $props_n_values = []): EE_Form_Section |
|
63 | - { |
|
64 | - return new self($props_n_values, true); |
|
65 | - } |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * Form Section UUID (universally unique identifier) |
|
70 | - * |
|
71 | - * @return string |
|
72 | - * @throws EE_Error |
|
73 | - * @throws ReflectionException |
|
74 | - */ |
|
75 | - public function UUID(): string |
|
76 | - { |
|
77 | - return $this->get('FSC_UUID'); |
|
78 | - } |
|
79 | - |
|
80 | - |
|
81 | - /** |
|
82 | - * last 8 characters of the UUID |
|
83 | - * |
|
84 | - * @return string |
|
85 | - * @throws EE_Error |
|
86 | - * @throws ReflectionException |
|
87 | - */ |
|
88 | - public function uuidSlug(): string |
|
89 | - { |
|
90 | - return substr($this->UUID(), -8); |
|
91 | - } |
|
92 | - |
|
93 | - |
|
94 | - /** |
|
95 | - * @param string $UUID |
|
96 | - * @throws EE_Error |
|
97 | - * @throws ReflectionException |
|
98 | - */ |
|
99 | - public function setUUID(string $UUID) |
|
100 | - { |
|
101 | - $this->set('FSC_UUID', $UUID); |
|
102 | - } |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * Form user types that this form section should be presented to. |
|
107 | - * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
108 | - * |
|
109 | - * @return string |
|
110 | - * @throws EE_Error |
|
111 | - * @throws ReflectionException |
|
112 | - */ |
|
113 | - public function appliesTo(): string |
|
114 | - { |
|
115 | - return $this->get('FSC_appliesTo'); |
|
116 | - } |
|
117 | - |
|
118 | - |
|
119 | - /** |
|
120 | - * Form user types that this form section should be presented to. |
|
121 | - * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
122 | - * |
|
123 | - * @param EE_Registration|string $registrant |
|
124 | - * @return bool |
|
125 | - * @throws EE_Error |
|
126 | - * @throws ReflectionException |
|
127 | - */ |
|
128 | - public function appliesToRegistrant($registrant): bool |
|
129 | - { |
|
130 | - switch ($this->appliesTo()) { |
|
131 | - case EEM_Form_Section::APPLIES_TO_PRIMARY: |
|
132 | - return $registrant instanceof EE_Registration && $registrant->is_primary_registrant(); |
|
133 | - case EEM_Form_Section::APPLIES_TO_PURCHASER: |
|
134 | - return $registrant === 'purchaser'; |
|
135 | - case EEM_Form_Section::APPLIES_TO_REGISTRANTS: |
|
136 | - return $registrant instanceof EE_Registration && ! $registrant->is_primary_registrant(); |
|
137 | - case EEM_Form_Section::APPLIES_TO_ALL: |
|
138 | - default: |
|
139 | - return true; |
|
140 | - } |
|
141 | - } |
|
142 | - |
|
143 | - |
|
144 | - /** |
|
145 | - * @param string $user_type |
|
146 | - * @throws EE_Error |
|
147 | - * @throws ReflectionException |
|
148 | - */ |
|
149 | - public function setAppliesTo(string $user_type) |
|
150 | - { |
|
151 | - $this->set('FSC_appliesTo', $user_type); |
|
152 | - } |
|
153 | - |
|
154 | - |
|
155 | - /** |
|
156 | - * JSON string of HTML attributes, such as class, to be applied to this form section\'s container. |
|
157 | - * |
|
158 | - * @return Attributes |
|
159 | - * @throws EE_Error |
|
160 | - * @throws ReflectionException |
|
161 | - */ |
|
162 | - public function attributes(): ?Attributes |
|
163 | - { |
|
164 | - if (! $this->attributes instanceof Attributes) { |
|
165 | - $this->attributes = Attributes::fromJson($this->get('FSC_attributes')); |
|
166 | - } |
|
167 | - return $this->attributes; |
|
168 | - } |
|
169 | - |
|
170 | - |
|
171 | - /** |
|
172 | - * @param Attributes $attributes |
|
173 | - * @throws EE_Error |
|
174 | - * @throws ReflectionException |
|
175 | - */ |
|
176 | - public function setAttributes(Attributes $attributes) |
|
177 | - { |
|
178 | - // set local object |
|
179 | - $this->attributes = $attributes; |
|
180 | - // then pass to model as an array which will get converted to JSON by the model field |
|
181 | - $this->set('FSC_attributes', $attributes->toArray()); |
|
182 | - } |
|
183 | - |
|
184 | - |
|
185 | - /** |
|
186 | - * UUID or ID of related entity this form section belongs to. |
|
187 | - * |
|
188 | - * @return string |
|
189 | - * @throws EE_Error |
|
190 | - * @throws ReflectionException |
|
191 | - */ |
|
192 | - public function belongsTo(): ?string |
|
193 | - { |
|
194 | - return $this->get('FSC_belongsTo'); |
|
195 | - } |
|
196 | - |
|
197 | - |
|
198 | - /** |
|
199 | - * @param string $parent_UUID |
|
200 | - * @throws EE_Error |
|
201 | - * @throws ReflectionException |
|
202 | - */ |
|
203 | - public function setBelongsTo(string $parent_UUID) |
|
204 | - { |
|
205 | - $this->set('FSC_belongsTo', $parent_UUID); |
|
206 | - } |
|
207 | - |
|
208 | - |
|
209 | - /** |
|
210 | - * @return EE_Form_Element[] |
|
211 | - * @throws EE_Error |
|
212 | - * @throws ReflectionException |
|
213 | - */ |
|
214 | - public function formElements(): array |
|
215 | - { |
|
216 | - return $this->form_elements ?: $this->getFormElements(); |
|
217 | - } |
|
218 | - |
|
219 | - |
|
220 | - /** |
|
221 | - * @return EE_Form_Element[] |
|
222 | - * @throws EE_Error |
|
223 | - * @throws ReflectionException |
|
224 | - */ |
|
225 | - private function getFormElements(): array |
|
226 | - { |
|
227 | - $form_elements = $this->get_many_related('Form_Element', ['order_by' => ['FIN_order' => 'ASC']]); |
|
228 | - foreach ($form_elements as $form_element) { |
|
229 | - if ($form_element instanceof EE_Form_Element) { |
|
230 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
231 | - } |
|
232 | - } |
|
233 | - return $this->form_elements; |
|
234 | - } |
|
235 | - |
|
236 | - |
|
237 | - /** |
|
238 | - * @param EE_Form_Element[] $form_elements |
|
239 | - * @throws EE_Error |
|
240 | - * @throws ReflectionException |
|
241 | - */ |
|
242 | - public function setFormElements(array $form_elements): void |
|
243 | - { |
|
244 | - foreach ($form_elements as $form_element) { |
|
245 | - if ($form_element instanceof EE_Form_Element) { |
|
246 | - $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
247 | - } |
|
248 | - } |
|
249 | - } |
|
250 | - |
|
251 | - |
|
252 | - /** |
|
253 | - * @param EE_Form_Element[] $all_form_elements |
|
254 | - * @return EE_Form_Element[] |
|
255 | - * @throws EE_Error |
|
256 | - * @throws ReflectionException |
|
257 | - */ |
|
258 | - public function filterFormElements(array $all_form_elements): array |
|
259 | - { |
|
260 | - return array_filter($all_form_elements, $this->formElementFilter()); |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - /** |
|
265 | - * returns a closure that can be used to filter form elements for this form section |
|
266 | - * usage: |
|
267 | - * $filter = EEM_Form_Element::formElementFilter(); |
|
268 | - * $filtered_form_elements = array_filter( $all_form_elements, $filter ); |
|
269 | - * |
|
270 | - * @return Closure |
|
271 | - * @throws EE_Error |
|
272 | - * @throws ReflectionException |
|
273 | - */ |
|
274 | - public function formElementFilter(): Closure |
|
275 | - { |
|
276 | - $FSC_UUID = strtolower($this->UUID()); |
|
277 | - return function ($form_element) use ($FSC_UUID) { |
|
278 | - return $form_element instanceof EE_Form_Element && strtolower($form_element->belongsTo()) === $FSC_UUID; |
|
279 | - }; |
|
280 | - } |
|
281 | - |
|
282 | - |
|
283 | - /** |
|
284 | - * returns a FormLabel object for managing form section labels, ie: the form section heading |
|
285 | - * |
|
286 | - * @return FormLabel |
|
287 | - * @throws EE_Error |
|
288 | - * @throws ReflectionException |
|
289 | - */ |
|
290 | - public function label(): ?FormLabel |
|
291 | - { |
|
292 | - if (! $this->label instanceof FormLabel) { |
|
293 | - $this->label = FormLabel::fromJson($this->get('FSC_label')); |
|
294 | - } |
|
295 | - return $this->label; |
|
296 | - } |
|
297 | - |
|
298 | - |
|
299 | - /** |
|
300 | - * @param FormLabel $label |
|
301 | - * @throws EE_Error |
|
302 | - * @throws ReflectionException |
|
303 | - */ |
|
304 | - public function setLabel(FormLabel $label) |
|
305 | - { |
|
306 | - // set local object |
|
307 | - $this->label = $label; |
|
308 | - // then pass to model as an array which will get converted to JSON by the model field |
|
309 | - $this->set('FSC_label', $label->toJson()); |
|
310 | - } |
|
311 | - |
|
312 | - |
|
313 | - /** |
|
314 | - * Order in which form section appears in a form. |
|
315 | - * |
|
316 | - * @return int |
|
317 | - * @throws EE_Error |
|
318 | - * @throws ReflectionException |
|
319 | - */ |
|
320 | - public function order(): int |
|
321 | - { |
|
322 | - return $this->get('FSC_order'); |
|
323 | - } |
|
324 | - |
|
325 | - |
|
326 | - /** |
|
327 | - * @param int $order |
|
328 | - * @throws EE_Error |
|
329 | - * @throws ReflectionException |
|
330 | - */ |
|
331 | - public function setOrder(int $order) |
|
332 | - { |
|
333 | - $this->set('FSC_order', $order); |
|
334 | - } |
|
335 | - |
|
336 | - |
|
337 | - /** |
|
338 | - * combination of public label and UUID slug for use in identifiers |
|
339 | - * |
|
340 | - * @return string |
|
341 | - * @throws EE_Error |
|
342 | - * @throws ReflectionException |
|
343 | - */ |
|
344 | - public function slug(): ?string |
|
345 | - { |
|
346 | - $label = sanitize_title($this->label()->publicLabel()); |
|
347 | - return "{$label}-{$this->uuidSlug()}"; |
|
348 | - } |
|
349 | - |
|
350 | - |
|
351 | - /** |
|
352 | - * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
353 | - * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
354 | - * |
|
355 | - * @return string |
|
356 | - * @throws EE_Error |
|
357 | - * @throws ReflectionException |
|
358 | - */ |
|
359 | - public function status(): ?string |
|
360 | - { |
|
361 | - return $this->get('FSC_status'); |
|
362 | - } |
|
363 | - |
|
364 | - |
|
365 | - /** |
|
366 | - * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
367 | - * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
368 | - * |
|
369 | - * @param string $status |
|
370 | - * @throws EE_Error |
|
371 | - * @throws ReflectionException |
|
372 | - */ |
|
373 | - public function setStatus(string $status) |
|
374 | - { |
|
375 | - $this->set('FSC_status', $status); |
|
376 | - } |
|
377 | - |
|
378 | - |
|
379 | - /** |
|
380 | - * returns the id the wordpress user who created this question |
|
381 | - * |
|
382 | - * @return int |
|
383 | - * @throws EE_Error |
|
384 | - * @throws ReflectionException |
|
385 | - */ |
|
386 | - public function wp_user(): int |
|
387 | - { |
|
388 | - return $this->get('FSC_wpUser'); |
|
389 | - } |
|
390 | - |
|
391 | - |
|
392 | - /** |
|
393 | - * @param int $wp_user |
|
394 | - * @throws EE_Error |
|
395 | - * @throws ReflectionException |
|
396 | - */ |
|
397 | - public function setWpUser(int $wp_user) |
|
398 | - { |
|
399 | - $this->set('FSC_wpUser', $wp_user); |
|
400 | - } |
|
401 | - |
|
402 | - |
|
403 | - /** |
|
404 | - * @param array $set_cols_n_values |
|
405 | - * @return bool|int|string |
|
406 | - * @throws EE_Error |
|
407 | - * @throws ReflectionException |
|
408 | - */ |
|
409 | - public function save($set_cols_n_values = []) |
|
410 | - { |
|
411 | - // make sure internal versions for all composite objects are updated |
|
412 | - $this->set('FSC_attributes', $this->attributes()->toArray()); |
|
413 | - $this->set('FSC_label', $this->label()->toArray()); |
|
414 | - return parent::save($set_cols_n_values); |
|
415 | - } |
|
416 | - |
|
417 | - /** |
|
418 | - * Whether the section is active. |
|
419 | - * |
|
420 | - * @return boolean TRUE if is active, FALSE if not. |
|
421 | - * @throws ReflectionException |
|
422 | - * @throws InvalidArgumentException |
|
423 | - * @throws InvalidInterfaceException |
|
424 | - * @throws InvalidDataTypeException |
|
425 | - * @throws EE_Error |
|
426 | - */ |
|
427 | - public function isActive() |
|
428 | - { |
|
429 | - return $this->status() === FormStatus::ACTIVE; |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * Whether the section is archived. |
|
434 | - * |
|
435 | - * @return boolean TRUE if is archived, FALSE if not. |
|
436 | - * @throws ReflectionException |
|
437 | - * @throws InvalidArgumentException |
|
438 | - * @throws InvalidInterfaceException |
|
439 | - * @throws InvalidDataTypeException |
|
440 | - * @throws EE_Error |
|
441 | - */ |
|
442 | - public function isArchived() |
|
443 | - { |
|
444 | - return $this->status() === FormStatus::ARCHIVED; |
|
445 | - } |
|
446 | - |
|
447 | - /** |
|
448 | - * Whether the section is a default one. |
|
449 | - * |
|
450 | - * @return boolean TRUE if is default, FALSE if not. |
|
451 | - * @throws ReflectionException |
|
452 | - * @throws InvalidArgumentException |
|
453 | - * @throws InvalidInterfaceException |
|
454 | - * @throws InvalidDataTypeException |
|
455 | - * @throws EE_Error |
|
456 | - */ |
|
457 | - public function isDefault() |
|
458 | - { |
|
459 | - return $this->status() === FormStatus::DEFAULT; |
|
460 | - } |
|
461 | - |
|
462 | - /** |
|
463 | - * Whether the section is a shared one. |
|
464 | - * |
|
465 | - * @return boolean TRUE if is shared, FALSE if not. |
|
466 | - * @throws ReflectionException |
|
467 | - * @throws InvalidArgumentException |
|
468 | - * @throws InvalidInterfaceException |
|
469 | - * @throws InvalidDataTypeException |
|
470 | - * @throws EE_Error |
|
471 | - */ |
|
472 | - public function isShared() |
|
473 | - { |
|
474 | - return $this->status() === FormStatus::SHARED; |
|
475 | - } |
|
476 | - |
|
477 | - /** |
|
478 | - * Whether the section is trashed. |
|
479 | - * |
|
480 | - * @return boolean TRUE if is trashed, FALSE if not. |
|
481 | - * @throws ReflectionException |
|
482 | - * @throws InvalidArgumentException |
|
483 | - * @throws InvalidInterfaceException |
|
484 | - * @throws InvalidDataTypeException |
|
485 | - * @throws EE_Error |
|
486 | - */ |
|
487 | - public function isTrashed() |
|
488 | - { |
|
489 | - return $this->status() === FormStatus::TRASHED; |
|
490 | - } |
|
26 | + /** |
|
27 | + * @var Attributes |
|
28 | + */ |
|
29 | + private $attributes; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var EE_Form_Element[] |
|
33 | + */ |
|
34 | + private $form_elements = []; |
|
35 | + |
|
36 | + /** |
|
37 | + * @var FormLabel |
|
38 | + */ |
|
39 | + private $label; |
|
40 | + |
|
41 | + |
|
42 | + |
|
43 | + /** |
|
44 | + * @param array $props_n_values |
|
45 | + * @return EE_Form_Section |
|
46 | + * @throws EE_Error |
|
47 | + * @throws ReflectionException |
|
48 | + */ |
|
49 | + public static function new_instance(array $props_n_values = []): EE_Form_Section |
|
50 | + { |
|
51 | + $has_object = parent::_check_for_object($props_n_values, __CLASS__); |
|
52 | + return $has_object ?: new self($props_n_values); |
|
53 | + } |
|
54 | + |
|
55 | + |
|
56 | + /** |
|
57 | + * @param array $props_n_values |
|
58 | + * @return EE_Form_Section |
|
59 | + * @throws EE_Error |
|
60 | + * @throws ReflectionException |
|
61 | + */ |
|
62 | + public static function new_instance_from_db(array $props_n_values = []): EE_Form_Section |
|
63 | + { |
|
64 | + return new self($props_n_values, true); |
|
65 | + } |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * Form Section UUID (universally unique identifier) |
|
70 | + * |
|
71 | + * @return string |
|
72 | + * @throws EE_Error |
|
73 | + * @throws ReflectionException |
|
74 | + */ |
|
75 | + public function UUID(): string |
|
76 | + { |
|
77 | + return $this->get('FSC_UUID'); |
|
78 | + } |
|
79 | + |
|
80 | + |
|
81 | + /** |
|
82 | + * last 8 characters of the UUID |
|
83 | + * |
|
84 | + * @return string |
|
85 | + * @throws EE_Error |
|
86 | + * @throws ReflectionException |
|
87 | + */ |
|
88 | + public function uuidSlug(): string |
|
89 | + { |
|
90 | + return substr($this->UUID(), -8); |
|
91 | + } |
|
92 | + |
|
93 | + |
|
94 | + /** |
|
95 | + * @param string $UUID |
|
96 | + * @throws EE_Error |
|
97 | + * @throws ReflectionException |
|
98 | + */ |
|
99 | + public function setUUID(string $UUID) |
|
100 | + { |
|
101 | + $this->set('FSC_UUID', $UUID); |
|
102 | + } |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * Form user types that this form section should be presented to. |
|
107 | + * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
108 | + * |
|
109 | + * @return string |
|
110 | + * @throws EE_Error |
|
111 | + * @throws ReflectionException |
|
112 | + */ |
|
113 | + public function appliesTo(): string |
|
114 | + { |
|
115 | + return $this->get('FSC_appliesTo'); |
|
116 | + } |
|
117 | + |
|
118 | + |
|
119 | + /** |
|
120 | + * Form user types that this form section should be presented to. |
|
121 | + * Values correspond to the EEM_Form_Section::APPLIES_TO_* constants. |
|
122 | + * |
|
123 | + * @param EE_Registration|string $registrant |
|
124 | + * @return bool |
|
125 | + * @throws EE_Error |
|
126 | + * @throws ReflectionException |
|
127 | + */ |
|
128 | + public function appliesToRegistrant($registrant): bool |
|
129 | + { |
|
130 | + switch ($this->appliesTo()) { |
|
131 | + case EEM_Form_Section::APPLIES_TO_PRIMARY: |
|
132 | + return $registrant instanceof EE_Registration && $registrant->is_primary_registrant(); |
|
133 | + case EEM_Form_Section::APPLIES_TO_PURCHASER: |
|
134 | + return $registrant === 'purchaser'; |
|
135 | + case EEM_Form_Section::APPLIES_TO_REGISTRANTS: |
|
136 | + return $registrant instanceof EE_Registration && ! $registrant->is_primary_registrant(); |
|
137 | + case EEM_Form_Section::APPLIES_TO_ALL: |
|
138 | + default: |
|
139 | + return true; |
|
140 | + } |
|
141 | + } |
|
142 | + |
|
143 | + |
|
144 | + /** |
|
145 | + * @param string $user_type |
|
146 | + * @throws EE_Error |
|
147 | + * @throws ReflectionException |
|
148 | + */ |
|
149 | + public function setAppliesTo(string $user_type) |
|
150 | + { |
|
151 | + $this->set('FSC_appliesTo', $user_type); |
|
152 | + } |
|
153 | + |
|
154 | + |
|
155 | + /** |
|
156 | + * JSON string of HTML attributes, such as class, to be applied to this form section\'s container. |
|
157 | + * |
|
158 | + * @return Attributes |
|
159 | + * @throws EE_Error |
|
160 | + * @throws ReflectionException |
|
161 | + */ |
|
162 | + public function attributes(): ?Attributes |
|
163 | + { |
|
164 | + if (! $this->attributes instanceof Attributes) { |
|
165 | + $this->attributes = Attributes::fromJson($this->get('FSC_attributes')); |
|
166 | + } |
|
167 | + return $this->attributes; |
|
168 | + } |
|
169 | + |
|
170 | + |
|
171 | + /** |
|
172 | + * @param Attributes $attributes |
|
173 | + * @throws EE_Error |
|
174 | + * @throws ReflectionException |
|
175 | + */ |
|
176 | + public function setAttributes(Attributes $attributes) |
|
177 | + { |
|
178 | + // set local object |
|
179 | + $this->attributes = $attributes; |
|
180 | + // then pass to model as an array which will get converted to JSON by the model field |
|
181 | + $this->set('FSC_attributes', $attributes->toArray()); |
|
182 | + } |
|
183 | + |
|
184 | + |
|
185 | + /** |
|
186 | + * UUID or ID of related entity this form section belongs to. |
|
187 | + * |
|
188 | + * @return string |
|
189 | + * @throws EE_Error |
|
190 | + * @throws ReflectionException |
|
191 | + */ |
|
192 | + public function belongsTo(): ?string |
|
193 | + { |
|
194 | + return $this->get('FSC_belongsTo'); |
|
195 | + } |
|
196 | + |
|
197 | + |
|
198 | + /** |
|
199 | + * @param string $parent_UUID |
|
200 | + * @throws EE_Error |
|
201 | + * @throws ReflectionException |
|
202 | + */ |
|
203 | + public function setBelongsTo(string $parent_UUID) |
|
204 | + { |
|
205 | + $this->set('FSC_belongsTo', $parent_UUID); |
|
206 | + } |
|
207 | + |
|
208 | + |
|
209 | + /** |
|
210 | + * @return EE_Form_Element[] |
|
211 | + * @throws EE_Error |
|
212 | + * @throws ReflectionException |
|
213 | + */ |
|
214 | + public function formElements(): array |
|
215 | + { |
|
216 | + return $this->form_elements ?: $this->getFormElements(); |
|
217 | + } |
|
218 | + |
|
219 | + |
|
220 | + /** |
|
221 | + * @return EE_Form_Element[] |
|
222 | + * @throws EE_Error |
|
223 | + * @throws ReflectionException |
|
224 | + */ |
|
225 | + private function getFormElements(): array |
|
226 | + { |
|
227 | + $form_elements = $this->get_many_related('Form_Element', ['order_by' => ['FIN_order' => 'ASC']]); |
|
228 | + foreach ($form_elements as $form_element) { |
|
229 | + if ($form_element instanceof EE_Form_Element) { |
|
230 | + $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
231 | + } |
|
232 | + } |
|
233 | + return $this->form_elements; |
|
234 | + } |
|
235 | + |
|
236 | + |
|
237 | + /** |
|
238 | + * @param EE_Form_Element[] $form_elements |
|
239 | + * @throws EE_Error |
|
240 | + * @throws ReflectionException |
|
241 | + */ |
|
242 | + public function setFormElements(array $form_elements): void |
|
243 | + { |
|
244 | + foreach ($form_elements as $form_element) { |
|
245 | + if ($form_element instanceof EE_Form_Element) { |
|
246 | + $this->form_elements[ $form_element->UUID() ] = $form_element; |
|
247 | + } |
|
248 | + } |
|
249 | + } |
|
250 | + |
|
251 | + |
|
252 | + /** |
|
253 | + * @param EE_Form_Element[] $all_form_elements |
|
254 | + * @return EE_Form_Element[] |
|
255 | + * @throws EE_Error |
|
256 | + * @throws ReflectionException |
|
257 | + */ |
|
258 | + public function filterFormElements(array $all_form_elements): array |
|
259 | + { |
|
260 | + return array_filter($all_form_elements, $this->formElementFilter()); |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + /** |
|
265 | + * returns a closure that can be used to filter form elements for this form section |
|
266 | + * usage: |
|
267 | + * $filter = EEM_Form_Element::formElementFilter(); |
|
268 | + * $filtered_form_elements = array_filter( $all_form_elements, $filter ); |
|
269 | + * |
|
270 | + * @return Closure |
|
271 | + * @throws EE_Error |
|
272 | + * @throws ReflectionException |
|
273 | + */ |
|
274 | + public function formElementFilter(): Closure |
|
275 | + { |
|
276 | + $FSC_UUID = strtolower($this->UUID()); |
|
277 | + return function ($form_element) use ($FSC_UUID) { |
|
278 | + return $form_element instanceof EE_Form_Element && strtolower($form_element->belongsTo()) === $FSC_UUID; |
|
279 | + }; |
|
280 | + } |
|
281 | + |
|
282 | + |
|
283 | + /** |
|
284 | + * returns a FormLabel object for managing form section labels, ie: the form section heading |
|
285 | + * |
|
286 | + * @return FormLabel |
|
287 | + * @throws EE_Error |
|
288 | + * @throws ReflectionException |
|
289 | + */ |
|
290 | + public function label(): ?FormLabel |
|
291 | + { |
|
292 | + if (! $this->label instanceof FormLabel) { |
|
293 | + $this->label = FormLabel::fromJson($this->get('FSC_label')); |
|
294 | + } |
|
295 | + return $this->label; |
|
296 | + } |
|
297 | + |
|
298 | + |
|
299 | + /** |
|
300 | + * @param FormLabel $label |
|
301 | + * @throws EE_Error |
|
302 | + * @throws ReflectionException |
|
303 | + */ |
|
304 | + public function setLabel(FormLabel $label) |
|
305 | + { |
|
306 | + // set local object |
|
307 | + $this->label = $label; |
|
308 | + // then pass to model as an array which will get converted to JSON by the model field |
|
309 | + $this->set('FSC_label', $label->toJson()); |
|
310 | + } |
|
311 | + |
|
312 | + |
|
313 | + /** |
|
314 | + * Order in which form section appears in a form. |
|
315 | + * |
|
316 | + * @return int |
|
317 | + * @throws EE_Error |
|
318 | + * @throws ReflectionException |
|
319 | + */ |
|
320 | + public function order(): int |
|
321 | + { |
|
322 | + return $this->get('FSC_order'); |
|
323 | + } |
|
324 | + |
|
325 | + |
|
326 | + /** |
|
327 | + * @param int $order |
|
328 | + * @throws EE_Error |
|
329 | + * @throws ReflectionException |
|
330 | + */ |
|
331 | + public function setOrder(int $order) |
|
332 | + { |
|
333 | + $this->set('FSC_order', $order); |
|
334 | + } |
|
335 | + |
|
336 | + |
|
337 | + /** |
|
338 | + * combination of public label and UUID slug for use in identifiers |
|
339 | + * |
|
340 | + * @return string |
|
341 | + * @throws EE_Error |
|
342 | + * @throws ReflectionException |
|
343 | + */ |
|
344 | + public function slug(): ?string |
|
345 | + { |
|
346 | + $label = sanitize_title($this->label()->publicLabel()); |
|
347 | + return "{$label}-{$this->uuidSlug()}"; |
|
348 | + } |
|
349 | + |
|
350 | + |
|
351 | + /** |
|
352 | + * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
353 | + * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
354 | + * |
|
355 | + * @return string |
|
356 | + * @throws EE_Error |
|
357 | + * @throws ReflectionException |
|
358 | + */ |
|
359 | + public function status(): ?string |
|
360 | + { |
|
361 | + return $this->get('FSC_status'); |
|
362 | + } |
|
363 | + |
|
364 | + |
|
365 | + /** |
|
366 | + * Whether form section is active, archived, trashed, or used as a default on new forms. |
|
367 | + * Values correspond to the EEM_Form_Section::STATUS_* constants. |
|
368 | + * |
|
369 | + * @param string $status |
|
370 | + * @throws EE_Error |
|
371 | + * @throws ReflectionException |
|
372 | + */ |
|
373 | + public function setStatus(string $status) |
|
374 | + { |
|
375 | + $this->set('FSC_status', $status); |
|
376 | + } |
|
377 | + |
|
378 | + |
|
379 | + /** |
|
380 | + * returns the id the wordpress user who created this question |
|
381 | + * |
|
382 | + * @return int |
|
383 | + * @throws EE_Error |
|
384 | + * @throws ReflectionException |
|
385 | + */ |
|
386 | + public function wp_user(): int |
|
387 | + { |
|
388 | + return $this->get('FSC_wpUser'); |
|
389 | + } |
|
390 | + |
|
391 | + |
|
392 | + /** |
|
393 | + * @param int $wp_user |
|
394 | + * @throws EE_Error |
|
395 | + * @throws ReflectionException |
|
396 | + */ |
|
397 | + public function setWpUser(int $wp_user) |
|
398 | + { |
|
399 | + $this->set('FSC_wpUser', $wp_user); |
|
400 | + } |
|
401 | + |
|
402 | + |
|
403 | + /** |
|
404 | + * @param array $set_cols_n_values |
|
405 | + * @return bool|int|string |
|
406 | + * @throws EE_Error |
|
407 | + * @throws ReflectionException |
|
408 | + */ |
|
409 | + public function save($set_cols_n_values = []) |
|
410 | + { |
|
411 | + // make sure internal versions for all composite objects are updated |
|
412 | + $this->set('FSC_attributes', $this->attributes()->toArray()); |
|
413 | + $this->set('FSC_label', $this->label()->toArray()); |
|
414 | + return parent::save($set_cols_n_values); |
|
415 | + } |
|
416 | + |
|
417 | + /** |
|
418 | + * Whether the section is active. |
|
419 | + * |
|
420 | + * @return boolean TRUE if is active, FALSE if not. |
|
421 | + * @throws ReflectionException |
|
422 | + * @throws InvalidArgumentException |
|
423 | + * @throws InvalidInterfaceException |
|
424 | + * @throws InvalidDataTypeException |
|
425 | + * @throws EE_Error |
|
426 | + */ |
|
427 | + public function isActive() |
|
428 | + { |
|
429 | + return $this->status() === FormStatus::ACTIVE; |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * Whether the section is archived. |
|
434 | + * |
|
435 | + * @return boolean TRUE if is archived, FALSE if not. |
|
436 | + * @throws ReflectionException |
|
437 | + * @throws InvalidArgumentException |
|
438 | + * @throws InvalidInterfaceException |
|
439 | + * @throws InvalidDataTypeException |
|
440 | + * @throws EE_Error |
|
441 | + */ |
|
442 | + public function isArchived() |
|
443 | + { |
|
444 | + return $this->status() === FormStatus::ARCHIVED; |
|
445 | + } |
|
446 | + |
|
447 | + /** |
|
448 | + * Whether the section is a default one. |
|
449 | + * |
|
450 | + * @return boolean TRUE if is default, FALSE if not. |
|
451 | + * @throws ReflectionException |
|
452 | + * @throws InvalidArgumentException |
|
453 | + * @throws InvalidInterfaceException |
|
454 | + * @throws InvalidDataTypeException |
|
455 | + * @throws EE_Error |
|
456 | + */ |
|
457 | + public function isDefault() |
|
458 | + { |
|
459 | + return $this->status() === FormStatus::DEFAULT; |
|
460 | + } |
|
461 | + |
|
462 | + /** |
|
463 | + * Whether the section is a shared one. |
|
464 | + * |
|
465 | + * @return boolean TRUE if is shared, FALSE if not. |
|
466 | + * @throws ReflectionException |
|
467 | + * @throws InvalidArgumentException |
|
468 | + * @throws InvalidInterfaceException |
|
469 | + * @throws InvalidDataTypeException |
|
470 | + * @throws EE_Error |
|
471 | + */ |
|
472 | + public function isShared() |
|
473 | + { |
|
474 | + return $this->status() === FormStatus::SHARED; |
|
475 | + } |
|
476 | + |
|
477 | + /** |
|
478 | + * Whether the section is trashed. |
|
479 | + * |
|
480 | + * @return boolean TRUE if is trashed, FALSE if not. |
|
481 | + * @throws ReflectionException |
|
482 | + * @throws InvalidArgumentException |
|
483 | + * @throws InvalidInterfaceException |
|
484 | + * @throws InvalidDataTypeException |
|
485 | + * @throws EE_Error |
|
486 | + */ |
|
487 | + public function isTrashed() |
|
488 | + { |
|
489 | + return $this->status() === FormStatus::TRASHED; |
|
490 | + } |
|
491 | 491 | } |