Completed
Branch FET-9222-rest-api-writes (9a0487)
by
unknown
71:42 queued 58:38
created
modules/single_page_checkout/inc/EE_SPCO_Reg_Step.class.php 2 patches
Indentation   +632 added lines, -632 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php if (! defined('EVENT_ESPRESSO_VERSION')) {
2
-    exit('No direct script access allowed');
2
+	exit('No direct script access allowed');
3 3
 }
4 4
 
5 5
 
@@ -16,672 +16,672 @@  discard block
 block discarded – undo
16 16
 abstract class EE_SPCO_Reg_Step
17 17
 {
18 18
 
19
-    /**
20
-     *    $_completed - TRUE if this step has fully completed it's duties
21
-     *
22
-     * @access protected
23
-     * @type bool $_completed
24
-     */
25
-    protected $_completed = false;
26
-
27
-    /**
28
-     *    $_is_current_step - TRUE if this is the current step
29
-     *
30
-     * @access protected
31
-     * @type bool $_is_current_step
32
-     */
33
-    protected $_is_current_step = false;
34
-
35
-    /**
36
-     *    $_order - when the reg step should be run relative to other steps
37
-     *
38
-     * @access protected
39
-     * @type int $_template
40
-     */
41
-    protected $_order = 0;
42
-
43
-    /**
44
-     *    $_slug - URL param for this step
45
-     *
46
-     * @access protected
47
-     * @type string $_slug
48
-     */
49
-    protected $_slug;
50
-
51
-    /**
52
-     *    $_name - Step Name - translatable string
53
-     *
54
-     * @access protected
55
-     * @type string $_slug
56
-     */
57
-    protected $_name;
58
-
59
-    /**
60
-     *    $_submit_button_text - translatable string that appears on this step's submit button
61
-     *
62
-     * @access protected
63
-     * @type string $_slug
64
-     */
65
-    protected $_submit_button_text;
66
-
67
-    /**
68
-     *    $_template - template name
69
-     *
70
-     * @access protected
71
-     * @type string $_template
72
-     */
73
-    protected $_template;
74
-
75
-    /**
76
-     *    $_reg_form_name - the form input name and id attribute
77
-     *
78
-     * @access protected
79
-     * @var string $_reg_form_name
80
-     */
81
-    protected $_reg_form_name;
82
-
83
-    /**
84
-     *    $_success_message - text to display upon successful form submission
85
-     *
86
-     * @access private
87
-     * @var string $_success_message
88
-     */
89
-    protected $_success_message;
90
-
91
-    /**
92
-     *    $_instructions - a brief description of how to complete the reg step.
93
-     *    Usually displayed in conjunction with the previous step's success message.
94
-     *
95
-     * @access private
96
-     * @var string $_instructions
97
-     */
98
-    protected $_instructions;
99
-
100
-    /**
101
-     *    $_valid_data - the normalized and validated data for this step
102
-     *
103
-     * @access public
104
-     * @var array $_valid_data
105
-     */
106
-    protected $_valid_data = array();
107
-
108
-    /**
109
-     *    $reg_form - the registration form for this step
110
-     *
111
-     * @access public
112
-     * @var EE_Form_Section_Proper $reg_form
113
-     */
114
-    public $reg_form;
115
-
116
-    /**
117
-     *    $checkout - EE_Checkout object for handling the properties of the current checkout process
118
-     *
119
-     * @access public
120
-     * @var EE_Checkout $checkout
121
-     */
122
-    public $checkout;
123
-
124
-
125
-
126
-    /**
127
-     * @return void
128
-     */
129
-    abstract public function translate_js_strings();
130
-
131
-
19
+	/**
20
+	 *    $_completed - TRUE if this step has fully completed it's duties
21
+	 *
22
+	 * @access protected
23
+	 * @type bool $_completed
24
+	 */
25
+	protected $_completed = false;
26
+
27
+	/**
28
+	 *    $_is_current_step - TRUE if this is the current step
29
+	 *
30
+	 * @access protected
31
+	 * @type bool $_is_current_step
32
+	 */
33
+	protected $_is_current_step = false;
34
+
35
+	/**
36
+	 *    $_order - when the reg step should be run relative to other steps
37
+	 *
38
+	 * @access protected
39
+	 * @type int $_template
40
+	 */
41
+	protected $_order = 0;
42
+
43
+	/**
44
+	 *    $_slug - URL param for this step
45
+	 *
46
+	 * @access protected
47
+	 * @type string $_slug
48
+	 */
49
+	protected $_slug;
50
+
51
+	/**
52
+	 *    $_name - Step Name - translatable string
53
+	 *
54
+	 * @access protected
55
+	 * @type string $_slug
56
+	 */
57
+	protected $_name;
58
+
59
+	/**
60
+	 *    $_submit_button_text - translatable string that appears on this step's submit button
61
+	 *
62
+	 * @access protected
63
+	 * @type string $_slug
64
+	 */
65
+	protected $_submit_button_text;
66
+
67
+	/**
68
+	 *    $_template - template name
69
+	 *
70
+	 * @access protected
71
+	 * @type string $_template
72
+	 */
73
+	protected $_template;
74
+
75
+	/**
76
+	 *    $_reg_form_name - the form input name and id attribute
77
+	 *
78
+	 * @access protected
79
+	 * @var string $_reg_form_name
80
+	 */
81
+	protected $_reg_form_name;
82
+
83
+	/**
84
+	 *    $_success_message - text to display upon successful form submission
85
+	 *
86
+	 * @access private
87
+	 * @var string $_success_message
88
+	 */
89
+	protected $_success_message;
90
+
91
+	/**
92
+	 *    $_instructions - a brief description of how to complete the reg step.
93
+	 *    Usually displayed in conjunction with the previous step's success message.
94
+	 *
95
+	 * @access private
96
+	 * @var string $_instructions
97
+	 */
98
+	protected $_instructions;
99
+
100
+	/**
101
+	 *    $_valid_data - the normalized and validated data for this step
102
+	 *
103
+	 * @access public
104
+	 * @var array $_valid_data
105
+	 */
106
+	protected $_valid_data = array();
107
+
108
+	/**
109
+	 *    $reg_form - the registration form for this step
110
+	 *
111
+	 * @access public
112
+	 * @var EE_Form_Section_Proper $reg_form
113
+	 */
114
+	public $reg_form;
115
+
116
+	/**
117
+	 *    $checkout - EE_Checkout object for handling the properties of the current checkout process
118
+	 *
119
+	 * @access public
120
+	 * @var EE_Checkout $checkout
121
+	 */
122
+	public $checkout;
123
+
124
+
125
+
126
+	/**
127
+	 * @return void
128
+	 */
129
+	abstract public function translate_js_strings();
130
+
131
+
132 132
 
133
-    /**
134
-     * @return void
135
-     */
136
-    abstract public function enqueue_styles_and_scripts();
137
-
138
-
139
-
140
-    /**
141
-     * @return boolean
142
-     */
143
-    abstract public function initialize_reg_step();
144
-
145
-
146
-
147
-    /**
148
-     * @return string
149
-     */
150
-    abstract public function generate_reg_form();
151
-
152
-
153
-
154
-    /**
155
-     * @return boolean
156
-     */
157
-    abstract public function process_reg_step();
158
-
159
-
160
-
161
-    /**
162
-     * @return boolean
163
-     */
164
-    abstract public function update_reg_step();
165
-
166
-
167
-
168
-    /**
169
-     * @return boolean
170
-     */
171
-    public function completed()
172
-    {
173
-        return $this->_completed;
174
-    }
133
+	/**
134
+	 * @return void
135
+	 */
136
+	abstract public function enqueue_styles_and_scripts();
137
+
138
+
139
+
140
+	/**
141
+	 * @return boolean
142
+	 */
143
+	abstract public function initialize_reg_step();
144
+
145
+
146
+
147
+	/**
148
+	 * @return string
149
+	 */
150
+	abstract public function generate_reg_form();
151
+
152
+
153
+
154
+	/**
155
+	 * @return boolean
156
+	 */
157
+	abstract public function process_reg_step();
158
+
159
+
160
+
161
+	/**
162
+	 * @return boolean
163
+	 */
164
+	abstract public function update_reg_step();
165
+
166
+
167
+
168
+	/**
169
+	 * @return boolean
170
+	 */
171
+	public function completed()
172
+	{
173
+		return $this->_completed;
174
+	}
175 175
 
176 176
 
177 177
 
178
-    /**
179
-     * set_completed - toggles $_completed to TRUE
180
-     */
181
-    public function set_completed()
182
-    {
183
-        // DEBUG LOG
184
-        //$this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ );
185
-        $this->_completed = apply_filters('FHEE__EE_SPCO_Reg_Step__set_completed___completed', true, $this);
186
-    }
178
+	/**
179
+	 * set_completed - toggles $_completed to TRUE
180
+	 */
181
+	public function set_completed()
182
+	{
183
+		// DEBUG LOG
184
+		//$this->checkout->log( __CLASS__, __FUNCTION__, __LINE__ );
185
+		$this->_completed = apply_filters('FHEE__EE_SPCO_Reg_Step__set_completed___completed', true, $this);
186
+	}
187 187
 
188 188
 
189 189
 
190
-    /**
191
-     * set_completed - toggles $_completed to FALSE
192
-     */
193
-    public function set_not_completed()
194
-    {
195
-        $this->_completed = false;
196
-    }
190
+	/**
191
+	 * set_completed - toggles $_completed to FALSE
192
+	 */
193
+	public function set_not_completed()
194
+	{
195
+		$this->_completed = false;
196
+	}
197 197
 
198 198
 
199 199
 
200
-    /**
201
-     * @return string
202
-     */
203
-    public function name()
204
-    {
205
-        return $this->_name;
206
-    }
200
+	/**
201
+	 * @return string
202
+	 */
203
+	public function name()
204
+	{
205
+		return $this->_name;
206
+	}
207 207
 
208 208
 
209 209
 
210
-    /**
211
-     * @return string
212
-     */
213
-    public function slug()
214
-    {
215
-        return $this->_slug;
216
-    }
210
+	/**
211
+	 * @return string
212
+	 */
213
+	public function slug()
214
+	{
215
+		return $this->_slug;
216
+	}
217 217
 
218 218
 
219 219
 
220
-    /**
221
-     * submit_button_text
222
-     * the text that appears on the reg step form submit button
223
-     *
224
-     * @return string
225
-     */
226
-    public function submit_button_text()
227
-    {
228
-        return $this->_submit_button_text;
229
-    }
220
+	/**
221
+	 * submit_button_text
222
+	 * the text that appears on the reg step form submit button
223
+	 *
224
+	 * @return string
225
+	 */
226
+	public function submit_button_text()
227
+	{
228
+		return $this->_submit_button_text;
229
+	}
230 230
 
231 231
 
232 232
 
233
-    /**
234
-     * set_submit_button_text
235
-     * sets the text that appears on the reg step form submit button
236
-     *
237
-     * @param string $submit_button_text
238
-     */
239
-    public function set_submit_button_text($submit_button_text = '')
240
-    {
241
-        if (! empty($submit_button_text)) {
242
-            $this->_submit_button_text = $submit_button_text;
243
-        } else if ($this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
244
-            if ($this->checkout->revisit) {
245
-                $this->_submit_button_text = sprintf(
246
-                    __('Update %s', 'event_espresso'),
247
-                    $this->checkout->current_step->name()
248
-                );
249
-            } else {
250
-                $this->_submit_button_text = sprintf(
251
-                    __('Proceed to %s', 'event_espresso'),
252
-                    $this->checkout->next_step->name()
253
-                );
254
-            }
255
-        }
256
-        // filters the submit button text
257
-        $this->_submit_button_text = apply_filters(
258
-            'FHEE__EE_SPCO_Reg_Step__set_submit_button_text___submit_button_text',
259
-            $this->_submit_button_text,
260
-            $this->checkout
261
-        );
262
-    }
233
+	/**
234
+	 * set_submit_button_text
235
+	 * sets the text that appears on the reg step form submit button
236
+	 *
237
+	 * @param string $submit_button_text
238
+	 */
239
+	public function set_submit_button_text($submit_button_text = '')
240
+	{
241
+		if (! empty($submit_button_text)) {
242
+			$this->_submit_button_text = $submit_button_text;
243
+		} else if ($this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
244
+			if ($this->checkout->revisit) {
245
+				$this->_submit_button_text = sprintf(
246
+					__('Update %s', 'event_espresso'),
247
+					$this->checkout->current_step->name()
248
+				);
249
+			} else {
250
+				$this->_submit_button_text = sprintf(
251
+					__('Proceed to %s', 'event_espresso'),
252
+					$this->checkout->next_step->name()
253
+				);
254
+			}
255
+		}
256
+		// filters the submit button text
257
+		$this->_submit_button_text = apply_filters(
258
+			'FHEE__EE_SPCO_Reg_Step__set_submit_button_text___submit_button_text',
259
+			$this->_submit_button_text,
260
+			$this->checkout
261
+		);
262
+	}
263 263
 
264 264
 
265 265
 
266
-    /**
267
-     * @param boolean $is_current_step
268
-     */
269
-    public function set_is_current_step($is_current_step)
270
-    {
271
-        $this->_is_current_step = $is_current_step;
272
-    }
266
+	/**
267
+	 * @param boolean $is_current_step
268
+	 */
269
+	public function set_is_current_step($is_current_step)
270
+	{
271
+		$this->_is_current_step = $is_current_step;
272
+	}
273 273
 
274 274
 
275 275
 
276
-    /**
277
-     * @return boolean
278
-     */
279
-    public function is_current_step()
280
-    {
281
-        return $this->_is_current_step;
282
-    }
276
+	/**
277
+	 * @return boolean
278
+	 */
279
+	public function is_current_step()
280
+	{
281
+		return $this->_is_current_step;
282
+	}
283 283
 
284 284
 
285 285
 
286
-    /**
287
-     * @return boolean
288
-     */
289
-    public function is_final_step()
290
-    {
291
-        return $this instanceof EE_SPCO_Reg_Step_Finalize_Registration ? true : false;
292
-    }
286
+	/**
287
+	 * @return boolean
288
+	 */
289
+	public function is_final_step()
290
+	{
291
+		return $this instanceof EE_SPCO_Reg_Step_Finalize_Registration ? true : false;
292
+	}
293 293
 
294 294
 
295 295
 
296
-    /**
297
-     * @param int $order
298
-     */
299
-    public function set_order($order)
300
-    {
301
-        $this->_order = $order;
302
-    }
303
-
304
-
296
+	/**
297
+	 * @param int $order
298
+	 */
299
+	public function set_order($order)
300
+	{
301
+		$this->_order = $order;
302
+	}
303
+
304
+
305 305
 
306
-    /**
307
-     * @return int
308
-     */
309
-    public function order()
310
-    {
311
-        return $this->_order;
312
-    }
313
-
314
-
315
-
316
-    /**
317
-     * @return string
318
-     */
319
-    public function template()
320
-    {
321
-        return $this->_template;
322
-    }
323
-
324
-
325
-
326
-    /**
327
-     * @return string
328
-     */
329
-    public function success_message()
330
-    {
331
-        return $this->_success_message;
332
-    }
333
-
334
-
335
-
336
-    /**
337
-     * _set_success_message
338
-     *
339
-     * @param string $success_message
340
-     */
341
-    protected function _set_success_message($success_message)
342
-    {
343
-        $this->_success_message = $success_message;
344
-    }
345
-
346
-
347
-
348
-    /**
349
-     * _reset_success_message
350
-     *
351
-     * @return void
352
-     */
353
-    protected function _reset_success_message()
354
-    {
355
-        $this->_success_message = '';
356
-    }
306
+	/**
307
+	 * @return int
308
+	 */
309
+	public function order()
310
+	{
311
+		return $this->_order;
312
+	}
313
+
314
+
315
+
316
+	/**
317
+	 * @return string
318
+	 */
319
+	public function template()
320
+	{
321
+		return $this->_template;
322
+	}
323
+
324
+
325
+
326
+	/**
327
+	 * @return string
328
+	 */
329
+	public function success_message()
330
+	{
331
+		return $this->_success_message;
332
+	}
333
+
334
+
335
+
336
+	/**
337
+	 * _set_success_message
338
+	 *
339
+	 * @param string $success_message
340
+	 */
341
+	protected function _set_success_message($success_message)
342
+	{
343
+		$this->_success_message = $success_message;
344
+	}
345
+
346
+
347
+
348
+	/**
349
+	 * _reset_success_message
350
+	 *
351
+	 * @return void
352
+	 */
353
+	protected function _reset_success_message()
354
+	{
355
+		$this->_success_message = '';
356
+	}
357 357
 
358 358
 
359 359
 
360
-    /**
361
-     * @return string
362
-     */
363
-    public function _instructions()
364
-    {
365
-        return $this->_instructions;
366
-    }
367
-
368
-
369
-
370
-    /**
371
-     * @param string $instructions
372
-     */
373
-    public function set_instructions($instructions)
374
-    {
375
-        $this->_instructions = apply_filters(
376
-            'FHEE__EE_SPCO_Reg_Step__set_instructions__instructions',
377
-            $instructions,
378
-            $this
379
-        );
380
-    }
381
-
382
-
383
-
384
-    /**
385
-     * @param array $valid_data
386
-     */
387
-    public function set_valid_data($valid_data)
388
-    {
389
-        $this->_valid_data = $valid_data;
390
-    }
391
-
392
-
393
-
394
-    /**
395
-     * @return array
396
-     */
397
-    public function valid_data()
398
-    {
399
-        if (empty($this->_valid_data)) {
400
-            $this->_valid_data = $this->reg_form->valid_data();
401
-        }
402
-        return $this->_valid_data;
403
-    }
404
-
405
-
406
-
407
-    /**
408
-     * @return string
409
-     */
410
-    public function reg_form_name()
411
-    {
412
-        if (empty($this->_reg_form_name)) {
413
-            $this->set_reg_form_name('ee-spco-' . $this->slug() . '-reg-step-form');
414
-        }
415
-        return $this->_reg_form_name;
416
-    }
417
-
418
-
419
-
420
-    /**
421
-     * @param string $reg_form_name
422
-     */
423
-    protected function set_reg_form_name($reg_form_name)
424
-    {
425
-        $this->_reg_form_name = $reg_form_name;
426
-    }
427
-
428
-
429
-
430
-    /**
431
-     * reg_step_url
432
-     *
433
-     * @param string $action
434
-     * @return string
435
-     */
436
-    public function reg_step_url($action = '')
437
-    {
438
-        $query_args = array('step' => $this->slug());
439
-        if (! empty($action)) {
440
-            $query_args['action'] = $action;
441
-        }
442
-        // final step has no display
443
-        if ($this instanceof EE_SPCO_Reg_Step_Finalize_Registration && $action === 'display_spco_reg_step') {
444
-            $query_args['action'] = 'process_reg_step';
445
-        }
446
-        if ($this->checkout->revisit) {
447
-            $query_args['revisit'] = true;
448
-        }
449
-        if ($this->checkout->reg_url_link) {
450
-            $query_args['e_reg_url_link'] = $this->checkout->reg_url_link;
451
-        }
452
-        return add_query_arg($query_args, $this->checkout->reg_page_base_url);
453
-    }
454
-
455
-
456
-
457
-    /**
458
-     * creates the default hidden inputs section
459
-     *
460
-     * @return EE_Form_Section_Proper
461
-     * @throws \EE_Error
462
-     */
463
-    public function reg_step_hidden_inputs()
464
-    {
465
-        // hidden inputs for admin registrations
466
-        if ($this->checkout->admin_request) {
467
-            return new EE_Form_Section_Proper(
468
-                array(
469
-                    'layout_strategy' => new EE_Div_Per_Section_Layout(),
470
-                    'html_id'         => 'ee-' . $this->slug() . '-hidden-inputs',
471
-                    'subsections'     => array(
472
-                        'next_step' => new EE_Fixed_Hidden_Input(
473
-                            array(
474
-                                'html_name' => 'next_step',
475
-                                'html_id'   => 'spco-' . $this->slug() . '-next-step',
476
-                                'default'   => $this->checkout->next_step instanceof EE_SPCO_Reg_Step
477
-                                    ? $this->checkout->next_step->slug()
478
-                                    : '',
479
-                            )
480
-                        ),
481
-                    ),
482
-                )
483
-            );
484
-        } else {
485
-            $default_form_action = apply_filters(
486
-                'FHEE__EE_SPCO_Reg_Step__reg_step_hidden_inputs__default_form_action',
487
-                empty($this->checkout->reg_url_link)
488
-                    ? 'process_reg_step'
489
-                    : 'update_reg_step',
490
-                $this
491
-            );
492
-            // hidden inputs for frontend registrations
493
-            return new EE_Form_Section_Proper(
494
-                array(
495
-                    'layout_strategy' => new EE_Div_Per_Section_Layout(),
496
-                    'html_id'         => 'ee-' . $this->slug() . '-hidden-inputs',
497
-                    'subsections'     => array(
498
-                        'action'         => new EE_Fixed_Hidden_Input(
499
-                            array(
500
-                                'html_name' => 'action',
501
-                                'html_id'   => 'spco-' . $this->slug() . '-action',
502
-                                'default'   => $default_form_action,
503
-                            )
504
-                        ),
505
-                        'next_step'      => new EE_Fixed_Hidden_Input(
506
-                            array(
507
-                                'html_name' => 'next_step',
508
-                                'html_id'   => 'spco-' . $this->slug() . '-next-step',
509
-                                'default'   => $this->checkout->next_step instanceof EE_SPCO_Reg_Step
510
-                                    ? $this->checkout->next_step->slug()
511
-                                    : '',
512
-                            )
513
-                        ),
514
-                        'e_reg_url_link' => new EE_Fixed_Hidden_Input(
515
-                            array(
516
-                                'html_name' => 'e_reg_url_link',
517
-                                'html_id'   => 'spco-reg_url_link',
518
-                                'default'   => $this->checkout->reg_url_link,
519
-                            )
520
-                        ),
521
-                        'revisit'        => new EE_Fixed_Hidden_Input(
522
-                            array(
523
-                                'html_name' => 'revisit',
524
-                                'html_id'   => 'spco-revisit',
525
-                                'default'   => $this->checkout->revisit,
526
-                            )
527
-                        ),
528
-                    ),
529
-                )
530
-            );
531
-        }
532
-    }
533
-
534
-
535
-
536
-    /**
537
-     * generate_reg_form_for_actions
538
-     *
539
-     * @param array $actions
540
-     * @return void
541
-     */
542
-    public function generate_reg_form_for_actions($actions = array())
543
-    {
544
-        $actions = array_merge(
545
-            array(
546
-                'generate_reg_form',
547
-                'display_spco_reg_step',
548
-                'process_reg_step',
549
-                'update_reg_step',
550
-            ),
551
-            $actions
552
-        );
553
-        $this->checkout->generate_reg_form = in_array($this->checkout->action, $actions, true) ? true : false;
554
-    }
555
-
556
-
557
-
558
-    /**
559
-     * @return string
560
-     * @throws \EE_Error
561
-     */
562
-    public function display_reg_form()
563
-    {
564
-        $html = '';
565
-        if ($this->reg_form instanceof EE_Form_Section_Proper) {
566
-            $html .= ! $this->checkout->admin_request ? $this->reg_form->form_open($this->reg_step_url()) : '';
567
-            if (EE_Registry::instance()->REQ->ajax) {
568
-                $this->reg_form->localize_validation_rules();
569
-                $this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization());
570
-            }
571
-            $html .= $this->reg_form->get_html();
572
-            $html .= ! $this->checkout->admin_request ? $this->reg_step_submit_button() : '';
573
-            $html .= ! $this->checkout->admin_request ? $this->reg_form->form_close() : '';
574
-        }
575
-        return $html;
576
-    }
577
-
578
-
579
-
580
-    /**
581
-     * div_class - returns nothing for current step, but a css class of "hidden" for others
582
-     *
583
-     * @return string
584
-     * @throws \EE_Error
585
-     */
586
-    public function reg_step_submit_button()
587
-    {
588
-        if (! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
589
-            return '';
590
-        }
591
-        ob_start();
592
-        do_action(
593
-            'AHEE__before_spco_whats_next_buttons',
594
-            $this->slug(),
595
-            $this->checkout->next_step->slug(),
596
-            $this->checkout
597
-        );
598
-        $html = ob_get_clean();
599
-        // generate submit button
600
-        $sbmt_btn = new EE_Submit_Input(array(
601
-            'html_name'             => 'spco-go-to-step-' . $this->checkout->next_step->slug(),
602
-            'html_id'               => 'spco-go-to-step-' . $this->checkout->next_step->slug(),
603
-            'html_class'            => 'spco-next-step-btn',
604
-            'other_html_attributes' => ' rel="' . $this->slug() . '"',
605
-            'default'               => $this->submit_button_text(),
606
-        ));
607
-        $sbmt_btn->set_button_css_attributes(true, 'large');
608
-        $sbmt_btn_html = $sbmt_btn->get_html_for_input();
609
-        $html .= EEH_HTML::div(
610
-            apply_filters('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', $sbmt_btn_html, $this),
611
-            'spco-' . $this->slug() . '-whats-next-buttons-dv',
612
-            'spco-whats-next-buttons'
613
-        );
614
-        return $html;
615
-    }
616
-
617
-
618
-
619
-    /**
620
-     * div_class - returns nothing for current step, but a css class of "hidden" for others
621
-     *
622
-     * @return string
623
-     */
624
-    public function div_class()
625
-    {
626
-        return $this->is_current_step() ? '' : ' hidden';
627
-    }
628
-
629
-
630
-
631
-    /**
632
-     * div_class - returns  a css class of "hidden" for current step, but nothing for others
633
-     *
634
-     * @return string
635
-     */
636
-    public function edit_lnk_url()
637
-    {
638
-        return add_query_arg(array('step' => $this->slug()), $this->checkout->reg_page_base_url);
639
-    }
640
-
641
-
642
-
643
-    /**
644
-     * div_class - returns  a css class of "hidden" for current step, but nothing for others
645
-     *
646
-     * @return string
647
-     */
648
-    public function edit_link_class()
649
-    {
650
-        return $this->is_current_step() ? ' hidden' : '';
651
-    }
652
-
653
-
654
-
655
-    /**
656
-     * update_checkout with changes that have been made to the cart
657
-     *
658
-     * @return void
659
-     * @throws \EE_Error
660
-     */
661
-    public function update_checkout()
662
-    {
663
-        // grab the cart grand total and reset TXN total
664
-        $this->checkout->transaction->set_total($this->checkout->cart->get_cart_grand_total());
665
-        $this->checkout->stash_transaction_and_checkout();
666
-    }
667
-
668
-
669
-
670
-
671
-
672
-    /**
673
-     *    __sleep
674
-     * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon
675
-     * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the
676
-     * reg form, because if needed, it will be regenerated anyways
677
-     *
678
-     * @return array
679
-     */
680
-    public function __sleep()
681
-    {
682
-        // remove the reg form and the checkout
683
-        return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout'));
684
-    }
360
+	/**
361
+	 * @return string
362
+	 */
363
+	public function _instructions()
364
+	{
365
+		return $this->_instructions;
366
+	}
367
+
368
+
369
+
370
+	/**
371
+	 * @param string $instructions
372
+	 */
373
+	public function set_instructions($instructions)
374
+	{
375
+		$this->_instructions = apply_filters(
376
+			'FHEE__EE_SPCO_Reg_Step__set_instructions__instructions',
377
+			$instructions,
378
+			$this
379
+		);
380
+	}
381
+
382
+
383
+
384
+	/**
385
+	 * @param array $valid_data
386
+	 */
387
+	public function set_valid_data($valid_data)
388
+	{
389
+		$this->_valid_data = $valid_data;
390
+	}
391
+
392
+
393
+
394
+	/**
395
+	 * @return array
396
+	 */
397
+	public function valid_data()
398
+	{
399
+		if (empty($this->_valid_data)) {
400
+			$this->_valid_data = $this->reg_form->valid_data();
401
+		}
402
+		return $this->_valid_data;
403
+	}
404
+
405
+
406
+
407
+	/**
408
+	 * @return string
409
+	 */
410
+	public function reg_form_name()
411
+	{
412
+		if (empty($this->_reg_form_name)) {
413
+			$this->set_reg_form_name('ee-spco-' . $this->slug() . '-reg-step-form');
414
+		}
415
+		return $this->_reg_form_name;
416
+	}
417
+
418
+
419
+
420
+	/**
421
+	 * @param string $reg_form_name
422
+	 */
423
+	protected function set_reg_form_name($reg_form_name)
424
+	{
425
+		$this->_reg_form_name = $reg_form_name;
426
+	}
427
+
428
+
429
+
430
+	/**
431
+	 * reg_step_url
432
+	 *
433
+	 * @param string $action
434
+	 * @return string
435
+	 */
436
+	public function reg_step_url($action = '')
437
+	{
438
+		$query_args = array('step' => $this->slug());
439
+		if (! empty($action)) {
440
+			$query_args['action'] = $action;
441
+		}
442
+		// final step has no display
443
+		if ($this instanceof EE_SPCO_Reg_Step_Finalize_Registration && $action === 'display_spco_reg_step') {
444
+			$query_args['action'] = 'process_reg_step';
445
+		}
446
+		if ($this->checkout->revisit) {
447
+			$query_args['revisit'] = true;
448
+		}
449
+		if ($this->checkout->reg_url_link) {
450
+			$query_args['e_reg_url_link'] = $this->checkout->reg_url_link;
451
+		}
452
+		return add_query_arg($query_args, $this->checkout->reg_page_base_url);
453
+	}
454
+
455
+
456
+
457
+	/**
458
+	 * creates the default hidden inputs section
459
+	 *
460
+	 * @return EE_Form_Section_Proper
461
+	 * @throws \EE_Error
462
+	 */
463
+	public function reg_step_hidden_inputs()
464
+	{
465
+		// hidden inputs for admin registrations
466
+		if ($this->checkout->admin_request) {
467
+			return new EE_Form_Section_Proper(
468
+				array(
469
+					'layout_strategy' => new EE_Div_Per_Section_Layout(),
470
+					'html_id'         => 'ee-' . $this->slug() . '-hidden-inputs',
471
+					'subsections'     => array(
472
+						'next_step' => new EE_Fixed_Hidden_Input(
473
+							array(
474
+								'html_name' => 'next_step',
475
+								'html_id'   => 'spco-' . $this->slug() . '-next-step',
476
+								'default'   => $this->checkout->next_step instanceof EE_SPCO_Reg_Step
477
+									? $this->checkout->next_step->slug()
478
+									: '',
479
+							)
480
+						),
481
+					),
482
+				)
483
+			);
484
+		} else {
485
+			$default_form_action = apply_filters(
486
+				'FHEE__EE_SPCO_Reg_Step__reg_step_hidden_inputs__default_form_action',
487
+				empty($this->checkout->reg_url_link)
488
+					? 'process_reg_step'
489
+					: 'update_reg_step',
490
+				$this
491
+			);
492
+			// hidden inputs for frontend registrations
493
+			return new EE_Form_Section_Proper(
494
+				array(
495
+					'layout_strategy' => new EE_Div_Per_Section_Layout(),
496
+					'html_id'         => 'ee-' . $this->slug() . '-hidden-inputs',
497
+					'subsections'     => array(
498
+						'action'         => new EE_Fixed_Hidden_Input(
499
+							array(
500
+								'html_name' => 'action',
501
+								'html_id'   => 'spco-' . $this->slug() . '-action',
502
+								'default'   => $default_form_action,
503
+							)
504
+						),
505
+						'next_step'      => new EE_Fixed_Hidden_Input(
506
+							array(
507
+								'html_name' => 'next_step',
508
+								'html_id'   => 'spco-' . $this->slug() . '-next-step',
509
+								'default'   => $this->checkout->next_step instanceof EE_SPCO_Reg_Step
510
+									? $this->checkout->next_step->slug()
511
+									: '',
512
+							)
513
+						),
514
+						'e_reg_url_link' => new EE_Fixed_Hidden_Input(
515
+							array(
516
+								'html_name' => 'e_reg_url_link',
517
+								'html_id'   => 'spco-reg_url_link',
518
+								'default'   => $this->checkout->reg_url_link,
519
+							)
520
+						),
521
+						'revisit'        => new EE_Fixed_Hidden_Input(
522
+							array(
523
+								'html_name' => 'revisit',
524
+								'html_id'   => 'spco-revisit',
525
+								'default'   => $this->checkout->revisit,
526
+							)
527
+						),
528
+					),
529
+				)
530
+			);
531
+		}
532
+	}
533
+
534
+
535
+
536
+	/**
537
+	 * generate_reg_form_for_actions
538
+	 *
539
+	 * @param array $actions
540
+	 * @return void
541
+	 */
542
+	public function generate_reg_form_for_actions($actions = array())
543
+	{
544
+		$actions = array_merge(
545
+			array(
546
+				'generate_reg_form',
547
+				'display_spco_reg_step',
548
+				'process_reg_step',
549
+				'update_reg_step',
550
+			),
551
+			$actions
552
+		);
553
+		$this->checkout->generate_reg_form = in_array($this->checkout->action, $actions, true) ? true : false;
554
+	}
555
+
556
+
557
+
558
+	/**
559
+	 * @return string
560
+	 * @throws \EE_Error
561
+	 */
562
+	public function display_reg_form()
563
+	{
564
+		$html = '';
565
+		if ($this->reg_form instanceof EE_Form_Section_Proper) {
566
+			$html .= ! $this->checkout->admin_request ? $this->reg_form->form_open($this->reg_step_url()) : '';
567
+			if (EE_Registry::instance()->REQ->ajax) {
568
+				$this->reg_form->localize_validation_rules();
569
+				$this->checkout->json_response->add_validation_rules(EE_Form_Section_Proper::js_localization());
570
+			}
571
+			$html .= $this->reg_form->get_html();
572
+			$html .= ! $this->checkout->admin_request ? $this->reg_step_submit_button() : '';
573
+			$html .= ! $this->checkout->admin_request ? $this->reg_form->form_close() : '';
574
+		}
575
+		return $html;
576
+	}
577
+
578
+
579
+
580
+	/**
581
+	 * div_class - returns nothing for current step, but a css class of "hidden" for others
582
+	 *
583
+	 * @return string
584
+	 * @throws \EE_Error
585
+	 */
586
+	public function reg_step_submit_button()
587
+	{
588
+		if (! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
589
+			return '';
590
+		}
591
+		ob_start();
592
+		do_action(
593
+			'AHEE__before_spco_whats_next_buttons',
594
+			$this->slug(),
595
+			$this->checkout->next_step->slug(),
596
+			$this->checkout
597
+		);
598
+		$html = ob_get_clean();
599
+		// generate submit button
600
+		$sbmt_btn = new EE_Submit_Input(array(
601
+			'html_name'             => 'spco-go-to-step-' . $this->checkout->next_step->slug(),
602
+			'html_id'               => 'spco-go-to-step-' . $this->checkout->next_step->slug(),
603
+			'html_class'            => 'spco-next-step-btn',
604
+			'other_html_attributes' => ' rel="' . $this->slug() . '"',
605
+			'default'               => $this->submit_button_text(),
606
+		));
607
+		$sbmt_btn->set_button_css_attributes(true, 'large');
608
+		$sbmt_btn_html = $sbmt_btn->get_html_for_input();
609
+		$html .= EEH_HTML::div(
610
+			apply_filters('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', $sbmt_btn_html, $this),
611
+			'spco-' . $this->slug() . '-whats-next-buttons-dv',
612
+			'spco-whats-next-buttons'
613
+		);
614
+		return $html;
615
+	}
616
+
617
+
618
+
619
+	/**
620
+	 * div_class - returns nothing for current step, but a css class of "hidden" for others
621
+	 *
622
+	 * @return string
623
+	 */
624
+	public function div_class()
625
+	{
626
+		return $this->is_current_step() ? '' : ' hidden';
627
+	}
628
+
629
+
630
+
631
+	/**
632
+	 * div_class - returns  a css class of "hidden" for current step, but nothing for others
633
+	 *
634
+	 * @return string
635
+	 */
636
+	public function edit_lnk_url()
637
+	{
638
+		return add_query_arg(array('step' => $this->slug()), $this->checkout->reg_page_base_url);
639
+	}
640
+
641
+
642
+
643
+	/**
644
+	 * div_class - returns  a css class of "hidden" for current step, but nothing for others
645
+	 *
646
+	 * @return string
647
+	 */
648
+	public function edit_link_class()
649
+	{
650
+		return $this->is_current_step() ? ' hidden' : '';
651
+	}
652
+
653
+
654
+
655
+	/**
656
+	 * update_checkout with changes that have been made to the cart
657
+	 *
658
+	 * @return void
659
+	 * @throws \EE_Error
660
+	 */
661
+	public function update_checkout()
662
+	{
663
+		// grab the cart grand total and reset TXN total
664
+		$this->checkout->transaction->set_total($this->checkout->cart->get_cart_grand_total());
665
+		$this->checkout->stash_transaction_and_checkout();
666
+	}
667
+
668
+
669
+
670
+
671
+
672
+	/**
673
+	 *    __sleep
674
+	 * to conserve db space, let's remove the reg_form and the EE_Checkout object from EE_SPCO_Reg_Step objects upon
675
+	 * serialization EE_Checkout will handle the reimplementation of itself upon waking, but we won't bother with the
676
+	 * reg form, because if needed, it will be regenerated anyways
677
+	 *
678
+	 * @return array
679
+	 */
680
+	public function __sleep()
681
+	{
682
+		// remove the reg form and the checkout
683
+		return array_diff(array_keys(get_object_vars($this)), array('reg_form', 'checkout'));
684
+	}
685 685
 
686 686
 
687 687
 
Please login to merge, or discard this patch.
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@  discard block
 block discarded – undo
1
-<?php if (! defined('EVENT_ESPRESSO_VERSION')) {
1
+<?php if ( ! defined('EVENT_ESPRESSO_VERSION')) {
2 2
     exit('No direct script access allowed');
3 3
 }
4 4
 
@@ -238,7 +238,7 @@  discard block
 block discarded – undo
238 238
      */
239 239
     public function set_submit_button_text($submit_button_text = '')
240 240
     {
241
-        if (! empty($submit_button_text)) {
241
+        if ( ! empty($submit_button_text)) {
242 242
             $this->_submit_button_text = $submit_button_text;
243 243
         } else if ($this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
244 244
             if ($this->checkout->revisit) {
@@ -410,7 +410,7 @@  discard block
 block discarded – undo
410 410
     public function reg_form_name()
411 411
     {
412 412
         if (empty($this->_reg_form_name)) {
413
-            $this->set_reg_form_name('ee-spco-' . $this->slug() . '-reg-step-form');
413
+            $this->set_reg_form_name('ee-spco-'.$this->slug().'-reg-step-form');
414 414
         }
415 415
         return $this->_reg_form_name;
416 416
     }
@@ -436,7 +436,7 @@  discard block
 block discarded – undo
436 436
     public function reg_step_url($action = '')
437 437
     {
438 438
         $query_args = array('step' => $this->slug());
439
-        if (! empty($action)) {
439
+        if ( ! empty($action)) {
440 440
             $query_args['action'] = $action;
441 441
         }
442 442
         // final step has no display
@@ -467,12 +467,12 @@  discard block
 block discarded – undo
467 467
             return new EE_Form_Section_Proper(
468 468
                 array(
469 469
                     'layout_strategy' => new EE_Div_Per_Section_Layout(),
470
-                    'html_id'         => 'ee-' . $this->slug() . '-hidden-inputs',
470
+                    'html_id'         => 'ee-'.$this->slug().'-hidden-inputs',
471 471
                     'subsections'     => array(
472 472
                         'next_step' => new EE_Fixed_Hidden_Input(
473 473
                             array(
474 474
                                 'html_name' => 'next_step',
475
-                                'html_id'   => 'spco-' . $this->slug() . '-next-step',
475
+                                'html_id'   => 'spco-'.$this->slug().'-next-step',
476 476
                                 'default'   => $this->checkout->next_step instanceof EE_SPCO_Reg_Step
477 477
                                     ? $this->checkout->next_step->slug()
478 478
                                     : '',
@@ -493,19 +493,19 @@  discard block
 block discarded – undo
493 493
             return new EE_Form_Section_Proper(
494 494
                 array(
495 495
                     'layout_strategy' => new EE_Div_Per_Section_Layout(),
496
-                    'html_id'         => 'ee-' . $this->slug() . '-hidden-inputs',
496
+                    'html_id'         => 'ee-'.$this->slug().'-hidden-inputs',
497 497
                     'subsections'     => array(
498 498
                         'action'         => new EE_Fixed_Hidden_Input(
499 499
                             array(
500 500
                                 'html_name' => 'action',
501
-                                'html_id'   => 'spco-' . $this->slug() . '-action',
501
+                                'html_id'   => 'spco-'.$this->slug().'-action',
502 502
                                 'default'   => $default_form_action,
503 503
                             )
504 504
                         ),
505 505
                         'next_step'      => new EE_Fixed_Hidden_Input(
506 506
                             array(
507 507
                                 'html_name' => 'next_step',
508
-                                'html_id'   => 'spco-' . $this->slug() . '-next-step',
508
+                                'html_id'   => 'spco-'.$this->slug().'-next-step',
509 509
                                 'default'   => $this->checkout->next_step instanceof EE_SPCO_Reg_Step
510 510
                                     ? $this->checkout->next_step->slug()
511 511
                                     : '',
@@ -585,7 +585,7 @@  discard block
 block discarded – undo
585 585
      */
586 586
     public function reg_step_submit_button()
587 587
     {
588
-        if (! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
588
+        if ( ! $this->checkout->next_step instanceof EE_SPCO_Reg_Step) {
589 589
             return '';
590 590
         }
591 591
         ob_start();
@@ -598,17 +598,17 @@  discard block
 block discarded – undo
598 598
         $html = ob_get_clean();
599 599
         // generate submit button
600 600
         $sbmt_btn = new EE_Submit_Input(array(
601
-            'html_name'             => 'spco-go-to-step-' . $this->checkout->next_step->slug(),
602
-            'html_id'               => 'spco-go-to-step-' . $this->checkout->next_step->slug(),
601
+            'html_name'             => 'spco-go-to-step-'.$this->checkout->next_step->slug(),
602
+            'html_id'               => 'spco-go-to-step-'.$this->checkout->next_step->slug(),
603 603
             'html_class'            => 'spco-next-step-btn',
604
-            'other_html_attributes' => ' rel="' . $this->slug() . '"',
604
+            'other_html_attributes' => ' rel="'.$this->slug().'"',
605 605
             'default'               => $this->submit_button_text(),
606 606
         ));
607 607
         $sbmt_btn->set_button_css_attributes(true, 'large');
608 608
         $sbmt_btn_html = $sbmt_btn->get_html_for_input();
609 609
         $html .= EEH_HTML::div(
610 610
             apply_filters('FHEE__EE_SPCO_Reg_Step__reg_step_submit_button__sbmt_btn_html', $sbmt_btn_html, $this),
611
-            'spco-' . $this->slug() . '-whats-next-buttons-dv',
611
+            'spco-'.$this->slug().'-whats-next-buttons-dv',
612 612
             'spco-whats-next-buttons'
613 613
         );
614 614
         return $html;
Please login to merge, or discard this patch.
core/services/commands/registration/CreateRegistrationCommand.php 1 patch
Indentation   +157 added lines, -157 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@  discard block
 block discarded – undo
13 13
 use EventEspresso\core\services\commands\CommandRequiresCapCheckInterface;
14 14
 
15 15
 if ( ! defined('EVENT_ESPRESSO_VERSION')) {
16
-    exit('No direct script access allowed');
16
+	exit('No direct script access allowed');
17 17
 }
18 18
 
19 19
 
@@ -29,162 +29,162 @@  discard block
 block discarded – undo
29 29
 class CreateRegistrationCommand extends Command implements CommandRequiresCapCheckInterface
30 30
 {
31 31
 
32
-    /**
33
-     * @var EE_Transaction $transaction
34
-     */
35
-    private $transaction;
36
-
37
-    /**
38
-     * @var EE_Ticket $ticket
39
-     */
40
-    private $ticket;
41
-
42
-    /**
43
-     * @var EE_Line_Item $ticket_line_item
44
-     */
45
-    private $ticket_line_item;
46
-
47
-    /**
48
-     * @var int $reg_count
49
-     */
50
-    private $reg_count;
51
-
52
-    /**
53
-     * @var int $reg_group_size
54
-     */
55
-    private $reg_group_size;
56
-
57
-    /**
58
-     * @var string $reg_status
59
-     */
60
-    private $reg_status;
61
-
62
-    /**
63
-     * @var EE_Registration $registration
64
-     */
65
-    protected $registration;
66
-
67
-
68
-
69
-    /**
70
-     * CreateRegistrationCommand constructor.
71
-     *
72
-     * @param EE_Transaction $transaction
73
-     * @param EE_Line_Item   $ticket_line_item
74
-     * @param int             $reg_count
75
-     * @param int             $reg_group_size
76
-     * @param string          $reg_status
77
-     * @throws InvalidEntityException
78
-     */
79
-    public function __construct(
80
-        EE_Transaction $transaction,
81
-        EE_Line_Item $ticket_line_item,
82
-        $reg_count = 1,
83
-        $reg_group_size = 0,
84
-        $reg_status = EEM_Registration::status_id_incomplete
85
-    ) {
86
-        // grab the related ticket object for this line_item
87
-        $this->ticket = $ticket_line_item->ticket();
88
-        if ( ! $this->ticket instanceof EE_Ticket) {
89
-            throw new InvalidEntityException(
90
-                is_object($this->ticket) ? get_class($this->ticket) : gettype($this->ticket),
91
-                'EE_Ticket',
92
-                sprintf(
93
-                    __('Line item %s did not contain a valid ticket', 'event_espresso'),
94
-                    $ticket_line_item->ID()
95
-                )
96
-            );
97
-        }
98
-        $this->transaction = $transaction;
99
-        $this->ticket_line_item = $ticket_line_item;
100
-        $this->reg_count = absint($reg_count);
101
-        $this->reg_group_size = absint($reg_group_size);
102
-        $this->reg_status = $reg_status;
103
-    }
104
-
105
-
106
-
107
-    /**
108
-     * @return \EventEspresso\core\domain\services\capabilities\CapCheckInterface
109
-     * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
110
-     */
111
-    public function getCapCheck()
112
-    {
113
-        if ( ! $this->cap_check instanceof CapCheckInterface) {
114
-            return new CapCheck('ee_edit_registrations', 'create_new_registration');
115
-        }
116
-        return $this->cap_check;
117
-    }
118
-
119
-
120
-
121
-    /**
122
-     * @return EE_Transaction
123
-     */
124
-    public function transaction()
125
-    {
126
-        return $this->transaction;
127
-    }
128
-
129
-
130
-
131
-    /**
132
-     * @return EE_Ticket
133
-     */
134
-    public function ticket()
135
-    {
136
-        return $this->ticket;
137
-    }
138
-
139
-
140
-
141
-    /**
142
-     * @return EE_Line_Item
143
-     */
144
-    public function ticketLineItem()
145
-    {
146
-        return $this->ticket_line_item;
147
-    }
148
-
149
-
150
-
151
-    /**
152
-     * @return int
153
-     */
154
-    public function regCount()
155
-    {
156
-        return $this->reg_count;
157
-    }
158
-
159
-
160
-
161
-    /**
162
-     * @return int
163
-     */
164
-    public function regGroupSize()
165
-    {
166
-        return $this->reg_group_size;
167
-    }
168
-
169
-
170
-
171
-    /**
172
-     * @return string
173
-     */
174
-    public function regStatus()
175
-    {
176
-        return $this->reg_status;
177
-    }
178
-
179
-
180
-
181
-    /**
182
-     * @return EE_Registration
183
-     */
184
-    public function registration()
185
-    {
186
-        return $this->registration;
187
-    }
32
+	/**
33
+	 * @var EE_Transaction $transaction
34
+	 */
35
+	private $transaction;
36
+
37
+	/**
38
+	 * @var EE_Ticket $ticket
39
+	 */
40
+	private $ticket;
41
+
42
+	/**
43
+	 * @var EE_Line_Item $ticket_line_item
44
+	 */
45
+	private $ticket_line_item;
46
+
47
+	/**
48
+	 * @var int $reg_count
49
+	 */
50
+	private $reg_count;
51
+
52
+	/**
53
+	 * @var int $reg_group_size
54
+	 */
55
+	private $reg_group_size;
56
+
57
+	/**
58
+	 * @var string $reg_status
59
+	 */
60
+	private $reg_status;
61
+
62
+	/**
63
+	 * @var EE_Registration $registration
64
+	 */
65
+	protected $registration;
66
+
67
+
68
+
69
+	/**
70
+	 * CreateRegistrationCommand constructor.
71
+	 *
72
+	 * @param EE_Transaction $transaction
73
+	 * @param EE_Line_Item   $ticket_line_item
74
+	 * @param int             $reg_count
75
+	 * @param int             $reg_group_size
76
+	 * @param string          $reg_status
77
+	 * @throws InvalidEntityException
78
+	 */
79
+	public function __construct(
80
+		EE_Transaction $transaction,
81
+		EE_Line_Item $ticket_line_item,
82
+		$reg_count = 1,
83
+		$reg_group_size = 0,
84
+		$reg_status = EEM_Registration::status_id_incomplete
85
+	) {
86
+		// grab the related ticket object for this line_item
87
+		$this->ticket = $ticket_line_item->ticket();
88
+		if ( ! $this->ticket instanceof EE_Ticket) {
89
+			throw new InvalidEntityException(
90
+				is_object($this->ticket) ? get_class($this->ticket) : gettype($this->ticket),
91
+				'EE_Ticket',
92
+				sprintf(
93
+					__('Line item %s did not contain a valid ticket', 'event_espresso'),
94
+					$ticket_line_item->ID()
95
+				)
96
+			);
97
+		}
98
+		$this->transaction = $transaction;
99
+		$this->ticket_line_item = $ticket_line_item;
100
+		$this->reg_count = absint($reg_count);
101
+		$this->reg_group_size = absint($reg_group_size);
102
+		$this->reg_status = $reg_status;
103
+	}
104
+
105
+
106
+
107
+	/**
108
+	 * @return \EventEspresso\core\domain\services\capabilities\CapCheckInterface
109
+	 * @throws \EventEspresso\core\exceptions\InvalidDataTypeException
110
+	 */
111
+	public function getCapCheck()
112
+	{
113
+		if ( ! $this->cap_check instanceof CapCheckInterface) {
114
+			return new CapCheck('ee_edit_registrations', 'create_new_registration');
115
+		}
116
+		return $this->cap_check;
117
+	}
118
+
119
+
120
+
121
+	/**
122
+	 * @return EE_Transaction
123
+	 */
124
+	public function transaction()
125
+	{
126
+		return $this->transaction;
127
+	}
128
+
129
+
130
+
131
+	/**
132
+	 * @return EE_Ticket
133
+	 */
134
+	public function ticket()
135
+	{
136
+		return $this->ticket;
137
+	}
138
+
139
+
140
+
141
+	/**
142
+	 * @return EE_Line_Item
143
+	 */
144
+	public function ticketLineItem()
145
+	{
146
+		return $this->ticket_line_item;
147
+	}
148
+
149
+
150
+
151
+	/**
152
+	 * @return int
153
+	 */
154
+	public function regCount()
155
+	{
156
+		return $this->reg_count;
157
+	}
158
+
159
+
160
+
161
+	/**
162
+	 * @return int
163
+	 */
164
+	public function regGroupSize()
165
+	{
166
+		return $this->reg_group_size;
167
+	}
168
+
169
+
170
+
171
+	/**
172
+	 * @return string
173
+	 */
174
+	public function regStatus()
175
+	{
176
+		return $this->reg_status;
177
+	}
178
+
179
+
180
+
181
+	/**
182
+	 * @return EE_Registration
183
+	 */
184
+	public function registration()
185
+	{
186
+		return $this->registration;
187
+	}
188 188
 
189 189
 
190 190
 
Please login to merge, or discard this patch.
core/domain/entities/RegUrlLink.php 2 patches
Indentation   +96 added lines, -96 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
 use InvalidArgumentException;
8 8
 
9 9
 if ( ! defined('EVENT_ESPRESSO_VERSION')) {
10
-    exit('No direct script access allowed');
10
+	exit('No direct script access allowed');
11 11
 }
12 12
 
13 13
 
@@ -23,103 +23,103 @@  discard block
 block discarded – undo
23 23
 class RegUrlLink
24 24
 {
25 25
 
26
-    /*
26
+	/*
27 27
      * @var string $reg_url_link
28 28
      */
29
-    private $reg_url_link;
30
-
31
-
32
-
33
-    /**
34
-     * @param string $reg_url_link
35
-     * @return RegUrlLink
36
-     * @throws InvalidArgumentException
37
-     */
38
-    public static function fromRegUrlLinkString($reg_url_link)
39
-    {
40
-        if (empty($reg_url_link) || ! is_string($reg_url_link)) {
41
-            throw new InvalidArgumentException(
42
-                __(
43
-                    'You must supply a valid non-empty string to generate a reg_url_link.',
44
-                    'event_espresso'
45
-                )
46
-            );
47
-        }
48
-        return new RegUrlLink(1, '', $reg_url_link);
49
-    }
50
-
51
-
52
-
53
-    /**
54
-     * @param EE_Registration $registration
55
-     * @return RegUrlLink
56
-     * @throws EntityNotFoundException
57
-     * @throws EE_Error
58
-     * @throws InvalidArgumentException
59
-     */
60
-    public static function fromRegistration(EE_Registration $registration)
61
-    {
62
-        return new RegUrlLink(
63
-            $registration->count(),
64
-            $registration->ticket_line_item()
65
-        );
66
-    }
67
-
68
-
69
-
70
-    /**
71
-     * CreateRegUrlLinkCommand constructor.
72
-     *
73
-     * @param int    $reg_count
74
-     * @param mixed  $base_code
75
-     * @param string $reg_url_link
76
-     * @throws InvalidArgumentException
77
-     */
78
-    public function __construct(
79
-        $reg_count = 1,
80
-        $base_code = '',
81
-        $reg_url_link = ''
82
-    ) {
83
-        if ( ! empty($reg_url_link) && is_string($reg_url_link)) {
84
-            $this->reg_url_link = apply_filters(
85
-                'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link',
86
-                $reg_url_link,
87
-                $reg_count,
88
-                $base_code,
89
-                $reg_url_link
90
-            );
91
-            return;
92
-        }
93
-        $reg_count = max(1, absint($reg_count));
94
-        $base_code = $base_code instanceof \EE_Line_Item ? $base_code->code() : $base_code;
95
-        if (empty($base_code) || ! is_string($base_code)) {
96
-            throw new InvalidArgumentException(
97
-                __(
98
-                    'You must supply a valid EE_Line_Item or a non-empty string to generate a reg_url_link.',
99
-                    'event_espresso'
100
-                )
101
-            );
102
-        }
103
-        $this->reg_url_link = (string) apply_filters(
104
-            'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link',
105
-            $reg_count . '-' . md5($base_code . microtime()),
106
-            $reg_count,
107
-            $base_code,
108
-            $reg_url_link
109
-        );
110
-    }
111
-
112
-
113
-
114
-    /**
115
-     * Return the object as a string
116
-     *
117
-     * @return string
118
-     */
119
-    public function __toString()
120
-    {
121
-        return $this->reg_url_link;
122
-    }
29
+	private $reg_url_link;
30
+
31
+
32
+
33
+	/**
34
+	 * @param string $reg_url_link
35
+	 * @return RegUrlLink
36
+	 * @throws InvalidArgumentException
37
+	 */
38
+	public static function fromRegUrlLinkString($reg_url_link)
39
+	{
40
+		if (empty($reg_url_link) || ! is_string($reg_url_link)) {
41
+			throw new InvalidArgumentException(
42
+				__(
43
+					'You must supply a valid non-empty string to generate a reg_url_link.',
44
+					'event_espresso'
45
+				)
46
+			);
47
+		}
48
+		return new RegUrlLink(1, '', $reg_url_link);
49
+	}
50
+
51
+
52
+
53
+	/**
54
+	 * @param EE_Registration $registration
55
+	 * @return RegUrlLink
56
+	 * @throws EntityNotFoundException
57
+	 * @throws EE_Error
58
+	 * @throws InvalidArgumentException
59
+	 */
60
+	public static function fromRegistration(EE_Registration $registration)
61
+	{
62
+		return new RegUrlLink(
63
+			$registration->count(),
64
+			$registration->ticket_line_item()
65
+		);
66
+	}
67
+
68
+
69
+
70
+	/**
71
+	 * CreateRegUrlLinkCommand constructor.
72
+	 *
73
+	 * @param int    $reg_count
74
+	 * @param mixed  $base_code
75
+	 * @param string $reg_url_link
76
+	 * @throws InvalidArgumentException
77
+	 */
78
+	public function __construct(
79
+		$reg_count = 1,
80
+		$base_code = '',
81
+		$reg_url_link = ''
82
+	) {
83
+		if ( ! empty($reg_url_link) && is_string($reg_url_link)) {
84
+			$this->reg_url_link = apply_filters(
85
+				'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link',
86
+				$reg_url_link,
87
+				$reg_count,
88
+				$base_code,
89
+				$reg_url_link
90
+			);
91
+			return;
92
+		}
93
+		$reg_count = max(1, absint($reg_count));
94
+		$base_code = $base_code instanceof \EE_Line_Item ? $base_code->code() : $base_code;
95
+		if (empty($base_code) || ! is_string($base_code)) {
96
+			throw new InvalidArgumentException(
97
+				__(
98
+					'You must supply a valid EE_Line_Item or a non-empty string to generate a reg_url_link.',
99
+					'event_espresso'
100
+				)
101
+			);
102
+		}
103
+		$this->reg_url_link = (string) apply_filters(
104
+			'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link',
105
+			$reg_count . '-' . md5($base_code . microtime()),
106
+			$reg_count,
107
+			$base_code,
108
+			$reg_url_link
109
+		);
110
+	}
111
+
112
+
113
+
114
+	/**
115
+	 * Return the object as a string
116
+	 *
117
+	 * @return string
118
+	 */
119
+	public function __toString()
120
+	{
121
+		return $this->reg_url_link;
122
+	}
123 123
 }
124 124
 // End of file RegUrlLink.php
125 125
 // Location: /RegUrlLink.php
126 126
\ No newline at end of file
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -102,7 +102,7 @@
 block discarded – undo
102 102
         }
103 103
         $this->reg_url_link = (string) apply_filters(
104 104
             'FHEE__\EventEspresso\core\domain\entities\RegUrlLink__construct__reg_url_link',
105
-            $reg_count . '-' . md5($base_code . microtime()),
105
+            $reg_count.'-'.md5($base_code.microtime()),
106 106
             $reg_count,
107 107
             $base_code,
108 108
             $reg_url_link
Please login to merge, or discard this patch.
core/libraries/form_sections/base/EE_Form_Section_Base.form.php 2 patches
Spacing   +15 added lines, -15 removed lines patch added patch discarded remove patch
@@ -1,7 +1,7 @@  discard block
 block discarded – undo
1 1
 <?php
2 2
 use EventEspresso\core\libraries\form_sections\strategies\filter\FormHtmlFilter;
3 3
 
4
-if (! defined('EVENT_ESPRESSO_VERSION')) {
4
+if ( ! defined('EVENT_ESPRESSO_VERSION')) {
5 5
     exit('No direct script access allowed');
6 6
 }
7 7
 
@@ -99,7 +99,7 @@  discard block
 block discarded – undo
99 99
         // used by display strategies
100 100
         // assign incoming values to properties
101 101
         foreach ($options_array as $key => $value) {
102
-            $key = '_' . $key;
102
+            $key = '_'.$key;
103 103
             if (property_exists($this, $key) && empty($this->{$key})) {
104 104
                 $this->{$key} = $value;
105 105
             }
@@ -136,7 +136,7 @@  discard block
 block discarded – undo
136 136
      */
137 137
     public function ensure_construct_finalized_called()
138 138
     {
139
-        if (! $this->_construction_finalized) {
139
+        if ( ! $this->_construction_finalized) {
140 140
             $this->_construct_finalize($this->_parent_section, $this->_name);
141 141
         }
142 142
     }
@@ -199,7 +199,7 @@  discard block
 block discarded – undo
199 199
      */
200 200
     protected function _set_default_html_id_if_empty()
201 201
     {
202
-        if (! $this->_html_id) {
202
+        if ( ! $this->_html_id) {
203 203
             if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) {
204 204
                 $this->_html_id = $this->_parent_section->html_id()
205 205
                                   . '-'
@@ -258,7 +258,7 @@  discard block
 block discarded – undo
258 258
     public function html_id($add_pound_sign = false)
259 259
     {
260 260
         $this->_set_default_html_id_if_empty();
261
-        return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id;
261
+        return $add_pound_sign ? '#'.$this->_html_id : $this->_html_id;
262 262
     }
263 263
 
264 264
 
@@ -341,7 +341,7 @@  discard block
 block discarded – undo
341 341
      */
342 342
     public function name()
343 343
     {
344
-        if (! $this->_construction_finalized) {
344
+        if ( ! $this->_construction_finalized) {
345 345
             throw new EE_Error(sprintf(__('You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'',
346 346
                 'event_espresso'), get_class($this)));
347 347
         }
@@ -372,17 +372,17 @@  discard block
 block discarded – undo
372 372
      */
373 373
     public function form_open($action = '', $method = '', $other_attributes = '')
374 374
     {
375
-        if (! empty($action)) {
375
+        if ( ! empty($action)) {
376 376
             $this->set_action($action);
377 377
         }
378
-        if (! empty($method)) {
378
+        if ( ! empty($method)) {
379 379
             $this->set_method($method);
380 380
         }
381
-        $html = EEH_HTML::nl(1, 'form') . '<form';
382
-        $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : '';
383
-        $html .= ' action="' . $this->action() . '"';
384
-        $html .= ' method="' . $this->method() . '"';
385
-        $html .= $other_attributes . '>';
381
+        $html = EEH_HTML::nl(1, 'form').'<form';
382
+        $html .= $this->html_id() !== '' ? ' id="'.$this->get_html_id_for_form($this->html_id()).'"' : '';
383
+        $html .= ' action="'.$this->action().'"';
384
+        $html .= ' method="'.$this->method().'"';
385
+        $html .= $other_attributes.'>';
386 386
         return $html;
387 387
     }
388 388
 
@@ -398,9 +398,9 @@  discard block
 block discarded – undo
398 398
     protected function get_html_id_for_form($html_id)
399 399
     {
400 400
         $strlen = strlen($html_id);
401
-        $html_id = strpos($html_id, '-form') === $strlen-5 || strpos($html_id, '-frm') === $strlen - 4
401
+        $html_id = strpos($html_id, '-form') === $strlen - 5 || strpos($html_id, '-frm') === $strlen - 4
402 402
             ? $html_id
403
-            : $html_id . '-frm';
403
+            : $html_id.'-frm';
404 404
         return $html_id;
405 405
     }
406 406
 
Please login to merge, or discard this patch.
Indentation   +464 added lines, -464 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@  discard block
 block discarded – undo
2 2
 use EventEspresso\core\libraries\form_sections\strategies\filter\FormHtmlFilter;
3 3
 
4 4
 if (! defined('EVENT_ESPRESSO_VERSION')) {
5
-    exit('No direct script access allowed');
5
+	exit('No direct script access allowed');
6 6
 }
7 7
 
8 8
 
@@ -19,480 +19,480 @@  discard block
 block discarded – undo
19 19
 abstract class EE_Form_Section_Base
20 20
 {
21 21
 
22
-    /**
23
-     * the URL the form is submitted to
24
-     *
25
-     * @var string
26
-     */
27
-    protected $_action;
28
-
29
-    /**
30
-     * POST (default) or GET
31
-     *
32
-     * @var string
33
-     */
34
-    protected $_method;
35
-
36
-    /**
37
-     * html_id and html_name are derived from this by default
38
-     *
39
-     * @var string
40
-     */
41
-    protected $_name;
42
-
43
-    /**
44
-     * $_html_id
45
-     * @var string
46
-     */
47
-    protected $_html_id;
48
-
49
-    /**
50
-     * $_html_class
51
-     * @var string
52
-     */
53
-    protected $_html_class;
54
-
55
-    /**
56
-     * $_html_style
57
-     * @var string
58
-     */
59
-    protected $_html_style;
60
-
61
-    /**
62
-     * $_other_html_attributes
63
-     * @var string
64
-     */
65
-    protected $_other_html_attributes;
66
-
67
-    /**
68
-     * The form section of which this form section is a part
69
-     *
70
-     * @var EE_Form_Section_Proper
71
-     */
72
-    protected $_parent_section;
73
-
74
-    /**
75
-     * flag indicating that _construct_finalize has been called.
76
-     * If it has not been called and we try to use functions which require it, we call it
77
-     * with no parameters. But normally, _construct_finalize should be called by the instantiating class
78
-     *
79
-     * @var boolean
80
-     */
81
-    protected $_construction_finalized;
82
-
83
-    /**
84
-     * Strategy for parsing the form HTML upon display
85
-     *
86
-     * @var FormHtmlFilter
87
-     */
88
-    protected $_form_html_filter;
89
-
90
-
91
-
92
-    /**
93
-     * @param array $options_array {
94
-     * @type        $name          string the name for this form section, if you want to explicitly define it
95
-     *                             }
96
-     */
97
-    public function __construct($options_array = array())
98
-    {
99
-        // used by display strategies
100
-        // assign incoming values to properties
101
-        foreach ($options_array as $key => $value) {
102
-            $key = '_' . $key;
103
-            if (property_exists($this, $key) && empty($this->{$key})) {
104
-                $this->{$key} = $value;
105
-            }
106
-        }
107
-        // set parser which allows the form section's rendered HTML to be filtered
108
-        if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) {
109
-            $this->_form_html_filter = $options_array['form_html_filter'];
110
-        }
111
-    }
112
-
113
-
114
-
115
-    /**
116
-     * @param $parent_form_section
117
-     * @param $name
118
-     * @throws \EE_Error
119
-     */
120
-    protected function _construct_finalize($parent_form_section, $name)
121
-    {
122
-        $this->_construction_finalized = true;
123
-        $this->_parent_section = $parent_form_section;
124
-        if ($name !== null) {
125
-            $this->_name = $name;
126
-        }
127
-    }
128
-
129
-
130
-
131
-    /**
132
-     * make sure construction finalized was called, otherwise children might not be ready
133
-     *
134
-     * @return void
135
-     * @throws \EE_Error
136
-     */
137
-    public function ensure_construct_finalized_called()
138
-    {
139
-        if (! $this->_construction_finalized) {
140
-            $this->_construct_finalize($this->_parent_section, $this->_name);
141
-        }
142
-    }
143
-
144
-
145
-
146
-    /**
147
-     * @return string
148
-     */
149
-    public function action()
150
-    {
151
-        return $this->_action;
152
-    }
153
-
154
-
155
-
156
-    /**
157
-     * @param string $action
158
-     */
159
-    public function set_action($action)
160
-    {
161
-        $this->_action = $action;
162
-    }
163
-
164
-
165
-
166
-    /**
167
-     * @return string
168
-     */
169
-    public function method()
170
-    {
171
-        return ! empty($this->_method) ? $this->_method : 'POST';
172
-    }
173
-
174
-
175
-
176
-    /**
177
-     * @param string $method
178
-     */
179
-    public function set_method($method)
180
-    {
181
-        switch ($method) {
182
-            case 'get' :
183
-            case 'GET' :
184
-                $this->_method = 'GET';
185
-                break;
186
-            default :
187
-                $this->_method = 'POST';
188
-        }
189
-    }
190
-
191
-
192
-
193
-    /**
194
-     * Sets the html_id to its default value, if none was specified in the constructor.
195
-     * Calculation involves using the name and the parent's html id
196
-     * return void
197
-     *
198
-     * @throws \EE_Error
199
-     */
200
-    protected function _set_default_html_id_if_empty()
201
-    {
202
-        if (! $this->_html_id) {
203
-            if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) {
204
-                $this->_html_id = $this->_parent_section->html_id()
205
-                                  . '-'
206
-                                  . $this->_prep_name_for_html_id($this->name());
207
-            } else {
208
-                $this->_html_id = $this->_prep_name_for_html_id($this->name());
209
-            }
210
-        }
211
-    }
212
-
213
-
214
-
215
-    /**
216
-     * _prep_name_for_html_id
217
-     *
218
-     * @param $name
219
-     * @return string
220
-     */
221
-    private function _prep_name_for_html_id($name)
222
-    {
223
-        return sanitize_key(str_replace(array('&nbsp;', ' ', '_'), '-', $name));
224
-    }
22
+	/**
23
+	 * the URL the form is submitted to
24
+	 *
25
+	 * @var string
26
+	 */
27
+	protected $_action;
28
+
29
+	/**
30
+	 * POST (default) or GET
31
+	 *
32
+	 * @var string
33
+	 */
34
+	protected $_method;
35
+
36
+	/**
37
+	 * html_id and html_name are derived from this by default
38
+	 *
39
+	 * @var string
40
+	 */
41
+	protected $_name;
42
+
43
+	/**
44
+	 * $_html_id
45
+	 * @var string
46
+	 */
47
+	protected $_html_id;
48
+
49
+	/**
50
+	 * $_html_class
51
+	 * @var string
52
+	 */
53
+	protected $_html_class;
54
+
55
+	/**
56
+	 * $_html_style
57
+	 * @var string
58
+	 */
59
+	protected $_html_style;
60
+
61
+	/**
62
+	 * $_other_html_attributes
63
+	 * @var string
64
+	 */
65
+	protected $_other_html_attributes;
66
+
67
+	/**
68
+	 * The form section of which this form section is a part
69
+	 *
70
+	 * @var EE_Form_Section_Proper
71
+	 */
72
+	protected $_parent_section;
73
+
74
+	/**
75
+	 * flag indicating that _construct_finalize has been called.
76
+	 * If it has not been called and we try to use functions which require it, we call it
77
+	 * with no parameters. But normally, _construct_finalize should be called by the instantiating class
78
+	 *
79
+	 * @var boolean
80
+	 */
81
+	protected $_construction_finalized;
82
+
83
+	/**
84
+	 * Strategy for parsing the form HTML upon display
85
+	 *
86
+	 * @var FormHtmlFilter
87
+	 */
88
+	protected $_form_html_filter;
89
+
90
+
91
+
92
+	/**
93
+	 * @param array $options_array {
94
+	 * @type        $name          string the name for this form section, if you want to explicitly define it
95
+	 *                             }
96
+	 */
97
+	public function __construct($options_array = array())
98
+	{
99
+		// used by display strategies
100
+		// assign incoming values to properties
101
+		foreach ($options_array as $key => $value) {
102
+			$key = '_' . $key;
103
+			if (property_exists($this, $key) && empty($this->{$key})) {
104
+				$this->{$key} = $value;
105
+			}
106
+		}
107
+		// set parser which allows the form section's rendered HTML to be filtered
108
+		if (isset($options_array['form_html_filter']) && $options_array['form_html_filter'] instanceof FormHtmlFilter) {
109
+			$this->_form_html_filter = $options_array['form_html_filter'];
110
+		}
111
+	}
112
+
113
+
114
+
115
+	/**
116
+	 * @param $parent_form_section
117
+	 * @param $name
118
+	 * @throws \EE_Error
119
+	 */
120
+	protected function _construct_finalize($parent_form_section, $name)
121
+	{
122
+		$this->_construction_finalized = true;
123
+		$this->_parent_section = $parent_form_section;
124
+		if ($name !== null) {
125
+			$this->_name = $name;
126
+		}
127
+	}
128
+
129
+
130
+
131
+	/**
132
+	 * make sure construction finalized was called, otherwise children might not be ready
133
+	 *
134
+	 * @return void
135
+	 * @throws \EE_Error
136
+	 */
137
+	public function ensure_construct_finalized_called()
138
+	{
139
+		if (! $this->_construction_finalized) {
140
+			$this->_construct_finalize($this->_parent_section, $this->_name);
141
+		}
142
+	}
143
+
144
+
145
+
146
+	/**
147
+	 * @return string
148
+	 */
149
+	public function action()
150
+	{
151
+		return $this->_action;
152
+	}
153
+
154
+
155
+
156
+	/**
157
+	 * @param string $action
158
+	 */
159
+	public function set_action($action)
160
+	{
161
+		$this->_action = $action;
162
+	}
163
+
164
+
165
+
166
+	/**
167
+	 * @return string
168
+	 */
169
+	public function method()
170
+	{
171
+		return ! empty($this->_method) ? $this->_method : 'POST';
172
+	}
173
+
174
+
175
+
176
+	/**
177
+	 * @param string $method
178
+	 */
179
+	public function set_method($method)
180
+	{
181
+		switch ($method) {
182
+			case 'get' :
183
+			case 'GET' :
184
+				$this->_method = 'GET';
185
+				break;
186
+			default :
187
+				$this->_method = 'POST';
188
+		}
189
+	}
190
+
191
+
192
+
193
+	/**
194
+	 * Sets the html_id to its default value, if none was specified in the constructor.
195
+	 * Calculation involves using the name and the parent's html id
196
+	 * return void
197
+	 *
198
+	 * @throws \EE_Error
199
+	 */
200
+	protected function _set_default_html_id_if_empty()
201
+	{
202
+		if (! $this->_html_id) {
203
+			if ($this->_parent_section && $this->_parent_section instanceof EE_Form_Section_Proper) {
204
+				$this->_html_id = $this->_parent_section->html_id()
205
+								  . '-'
206
+								  . $this->_prep_name_for_html_id($this->name());
207
+			} else {
208
+				$this->_html_id = $this->_prep_name_for_html_id($this->name());
209
+			}
210
+		}
211
+	}
212
+
213
+
214
+
215
+	/**
216
+	 * _prep_name_for_html_id
217
+	 *
218
+	 * @param $name
219
+	 * @return string
220
+	 */
221
+	private function _prep_name_for_html_id($name)
222
+	{
223
+		return sanitize_key(str_replace(array('&nbsp;', ' ', '_'), '-', $name));
224
+	}
225 225
 
226 226
 
227 227
 
228
-    /**
229
-     * Returns the HTML, JS, and CSS necessary to display this form section on a page.
230
-     * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action,
231
-     * and call get_html when you want to output the html. Calling get_html_and_js after
232
-     * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary
233
-     * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/
234
-     * and so might stop working anytime.
235
-     *
236
-     * @return string
237
-     */
238
-    public function get_html_and_js()
239
-    {
240
-        return $this->get_html();
241
-    }
228
+	/**
229
+	 * Returns the HTML, JS, and CSS necessary to display this form section on a page.
230
+	 * Note however, it's recommended that you instead call enqueue_js on the "wp_enqueue_scripts" action,
231
+	 * and call get_html when you want to output the html. Calling get_html_and_js after
232
+	 * "wp_enqueue_scripts" has already fired seems to work for now, but is contrary
233
+	 * to the instructions on https://developer.wordpress.org/reference/functions/wp_enqueue_script/
234
+	 * and so might stop working anytime.
235
+	 *
236
+	 * @return string
237
+	 */
238
+	public function get_html_and_js()
239
+	{
240
+		return $this->get_html();
241
+	}
242 242
 
243 243
 
244 244
 
245
-    /**
246
-     * Gets the HTML for displaying this form section
247
-     *
248
-     * @return string
249
-     */
250
-    public abstract function get_html();
245
+	/**
246
+	 * Gets the HTML for displaying this form section
247
+	 *
248
+	 * @return string
249
+	 */
250
+	public abstract function get_html();
251 251
 
252 252
 
253 253
 
254
-    /**
255
-     * @param bool $add_pound_sign
256
-     * @return string
257
-     */
258
-    public function html_id($add_pound_sign = false)
259
-    {
260
-        $this->_set_default_html_id_if_empty();
261
-        return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id;
262
-    }
254
+	/**
255
+	 * @param bool $add_pound_sign
256
+	 * @return string
257
+	 */
258
+	public function html_id($add_pound_sign = false)
259
+	{
260
+		$this->_set_default_html_id_if_empty();
261
+		return $add_pound_sign ? '#' . $this->_html_id : $this->_html_id;
262
+	}
263 263
 
264 264
 
265
-
266
-    /**
267
-     * @return string
268
-     */
269
-    public function html_class()
270
-    {
271
-        return $this->_html_class;
272
-    }
273
-
274
-
275
-
276
-    /**
277
-     * @return string
278
-     */
279
-    public function html_style()
280
-    {
281
-        return $this->_html_style;
282
-    }
283
-
284
-
285
-
286
-    /**
287
-     * @param mixed $html_class
288
-     */
289
-    public function set_html_class($html_class)
290
-    {
291
-        $this->_html_class = $html_class;
292
-    }
293
-
294
-
295
-
296
-    /**
297
-     * @param mixed $html_id
298
-     */
299
-    public function set_html_id($html_id)
300
-    {
301
-        $this->_html_id = $html_id;
302
-    }
303
-
304
-
305
-
306
-    /**
307
-     * @param mixed $html_style
308
-     */
309
-    public function set_html_style($html_style)
310
-    {
311
-        $this->_html_style = $html_style;
312
-    }
313
-
314
-
315
-
316
-    /**
317
-     * @param string $other_html_attributes
318
-     */
319
-    public function set_other_html_attributes($other_html_attributes)
320
-    {
321
-        $this->_other_html_attributes = $other_html_attributes;
322
-    }
323
-
324
-
325
-
326
-    /**
327
-     * @return string
328
-     */
329
-    public function other_html_attributes()
330
-    {
331
-        return $this->_other_html_attributes;
332
-    }
333
-
334
-
335
-
336
-    /**
337
-     * Gets the name of the form section. This is not the same as the HTML name.
338
-     *
339
-     * @throws EE_Error
340
-     * @return string
341
-     */
342
-    public function name()
343
-    {
344
-        if (! $this->_construction_finalized) {
345
-            throw new EE_Error(sprintf(__('You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'',
346
-                'event_espresso'), get_class($this)));
347
-        }
348
-        return $this->_name;
349
-    }
350
-
351
-
352
-
353
-    /**
354
-     * Gets the parent section
355
-     *
356
-     * @return EE_Form_Section_Proper
357
-     */
358
-    public function parent_section()
359
-    {
360
-        return $this->_parent_section;
361
-    }
362
-
363
-
364
-
365
-    /**
366
-     * returns HTML for generating the opening form HTML tag (<form>)
367
-     *
368
-     * @param string $action           the URL the form is submitted to
369
-     * @param string $method           POST (default) or GET
370
-     * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML
371
-     * @return string
372
-     */
373
-    public function form_open($action = '', $method = '', $other_attributes = '')
374
-    {
375
-        if (! empty($action)) {
376
-            $this->set_action($action);
377
-        }
378
-        if (! empty($method)) {
379
-            $this->set_method($method);
380
-        }
381
-        $html = EEH_HTML::nl(1, 'form') . '<form';
382
-        $html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : '';
383
-        $html .= ' action="' . $this->action() . '"';
384
-        $html .= ' method="' . $this->method() . '"';
385
-        $html .= $other_attributes . '>';
386
-        return $html;
387
-    }
388
-
389
-
390
-
391
-    /**
392
-     * ensures that html id for form either ends in "-form" or "-frm"
393
-     * so that id doesn't conflict/collide with other elements
394
-     *
395
-     * @param string $html_id
396
-     * @return string
397
-     */
398
-    protected function get_html_id_for_form($html_id)
399
-    {
400
-        $strlen = strlen($html_id);
401
-        $html_id = strpos($html_id, '-form') === $strlen-5 || strpos($html_id, '-frm') === $strlen - 4
402
-            ? $html_id
403
-            : $html_id . '-frm';
404
-        return $html_id;
405
-    }
406
-
407
-
408
-
409
-    /**
410
-     * returns HTML for generating the closing form HTML tag (</form>)
411
-     *
412
-     * @return string
413
-     */
414
-    public function form_close()
415
-    {
416
-        return EEH_HTML::nl(-1, 'form')
417
-               . '</form>'
418
-               . EEH_HTML::nl()
419
-               . '<!-- end of ee-'
420
-               . $this->html_id()
421
-               . '-form -->'
422
-               . EEH_HTML::nl();
423
-    }
424
-
425
-
426
-
427
-    /**
428
-     * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and
429
-     * wp_enqueue_style; the scripts could have optionally been registered earlier)
430
-     * Default does nothing, but child classes can override
431
-     *
432
-     * @return void
433
-     */
434
-    public function enqueue_js()
435
-    {
436
-        //defaults to enqueue NO js or css
437
-    }
438
-
439
-
440
-
441
-    /**
442
-     * Adds any extra data needed by js. Eventually we'll call wp_localize_script
443
-     * with it, and it will be on each form section's 'other_data' property.
444
-     * By default nothing is added, but child classes can extend this method to add something.
445
-     * Eg, if you have an input that will cause a modal dialog to appear,
446
-     * here you could add an entry like 'modal_dialog_inputs' to this array
447
-     * to map between the input's html ID and the modal dialogue's ID, so that
448
-     * your JS code will know where to find the modal dialog when the input is pressed.
449
-     * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id';
450
-     *
451
-     * @param array $form_other_js_data
452
-     * @return array
453
-     */
454
-    public function get_other_js_data($form_other_js_data = array())
455
-    {
456
-        return $form_other_js_data;
457
-    }
458
-
459
-
460
-
461
-    /**
462
-     * This isn't just the name of an input, it's a path pointing to an input. The
463
-     * path is similar to a folder path: slash (/) means to descend into a subsection,
464
-     * dot-dot-slash (../) means to ascend into the parent section.
465
-     * After a series of slashes and dot-dot-slashes, there should be the name of an input,
466
-     * which will be returned.
467
-     * Eg, if you want the related input to be conditional on a sibling input name 'foobar'
468
-     * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name
469
-     * 'baz', use '../baz'. If you want it to be conditional on a cousin input,
470
-     * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'.
471
-     * Etc
472
-     *
473
-     * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false
474
-     * @return EE_Form_Section_Base
475
-     */
476
-    public function find_section_from_path($form_section_path)
477
-    {
478
-        if (strpos($form_section_path, '/') === 0) {
479
-            $form_section_path = substr($form_section_path, strlen('/'));
480
-        }
481
-        if (empty($form_section_path)) {
482
-            return $this;
483
-        }
484
-        if (strpos($form_section_path, '../') === 0) {
485
-            $parent = $this->parent_section();
486
-            $form_section_path = substr($form_section_path, strlen('../'));
487
-            if ($parent instanceof EE_Form_Section_Base) {
488
-                return $parent->find_section_from_path($form_section_path);
489
-            } elseif (empty($form_section_path)) {
490
-                return $this;
491
-            }
492
-        }
493
-        //couldn't find it using simple parent following
494
-        return null;
495
-    }
265
+
266
+	/**
267
+	 * @return string
268
+	 */
269
+	public function html_class()
270
+	{
271
+		return $this->_html_class;
272
+	}
273
+
274
+
275
+
276
+	/**
277
+	 * @return string
278
+	 */
279
+	public function html_style()
280
+	{
281
+		return $this->_html_style;
282
+	}
283
+
284
+
285
+
286
+	/**
287
+	 * @param mixed $html_class
288
+	 */
289
+	public function set_html_class($html_class)
290
+	{
291
+		$this->_html_class = $html_class;
292
+	}
293
+
294
+
295
+
296
+	/**
297
+	 * @param mixed $html_id
298
+	 */
299
+	public function set_html_id($html_id)
300
+	{
301
+		$this->_html_id = $html_id;
302
+	}
303
+
304
+
305
+
306
+	/**
307
+	 * @param mixed $html_style
308
+	 */
309
+	public function set_html_style($html_style)
310
+	{
311
+		$this->_html_style = $html_style;
312
+	}
313
+
314
+
315
+
316
+	/**
317
+	 * @param string $other_html_attributes
318
+	 */
319
+	public function set_other_html_attributes($other_html_attributes)
320
+	{
321
+		$this->_other_html_attributes = $other_html_attributes;
322
+	}
323
+
324
+
325
+
326
+	/**
327
+	 * @return string
328
+	 */
329
+	public function other_html_attributes()
330
+	{
331
+		return $this->_other_html_attributes;
332
+	}
333
+
334
+
335
+
336
+	/**
337
+	 * Gets the name of the form section. This is not the same as the HTML name.
338
+	 *
339
+	 * @throws EE_Error
340
+	 * @return string
341
+	 */
342
+	public function name()
343
+	{
344
+		if (! $this->_construction_finalized) {
345
+			throw new EE_Error(sprintf(__('You cannot use the form section\s name until _construct_finalize has been called on it (when we set the name). It was called on a form section of type \'s\'',
346
+				'event_espresso'), get_class($this)));
347
+		}
348
+		return $this->_name;
349
+	}
350
+
351
+
352
+
353
+	/**
354
+	 * Gets the parent section
355
+	 *
356
+	 * @return EE_Form_Section_Proper
357
+	 */
358
+	public function parent_section()
359
+	{
360
+		return $this->_parent_section;
361
+	}
362
+
363
+
364
+
365
+	/**
366
+	 * returns HTML for generating the opening form HTML tag (<form>)
367
+	 *
368
+	 * @param string $action           the URL the form is submitted to
369
+	 * @param string $method           POST (default) or GET
370
+	 * @param string $other_attributes anything else added to the form open tag, MUST BE VALID HTML
371
+	 * @return string
372
+	 */
373
+	public function form_open($action = '', $method = '', $other_attributes = '')
374
+	{
375
+		if (! empty($action)) {
376
+			$this->set_action($action);
377
+		}
378
+		if (! empty($method)) {
379
+			$this->set_method($method);
380
+		}
381
+		$html = EEH_HTML::nl(1, 'form') . '<form';
382
+		$html .= $this->html_id() !== '' ? ' id="' . $this->get_html_id_for_form($this->html_id()) . '"' : '';
383
+		$html .= ' action="' . $this->action() . '"';
384
+		$html .= ' method="' . $this->method() . '"';
385
+		$html .= $other_attributes . '>';
386
+		return $html;
387
+	}
388
+
389
+
390
+
391
+	/**
392
+	 * ensures that html id for form either ends in "-form" or "-frm"
393
+	 * so that id doesn't conflict/collide with other elements
394
+	 *
395
+	 * @param string $html_id
396
+	 * @return string
397
+	 */
398
+	protected function get_html_id_for_form($html_id)
399
+	{
400
+		$strlen = strlen($html_id);
401
+		$html_id = strpos($html_id, '-form') === $strlen-5 || strpos($html_id, '-frm') === $strlen - 4
402
+			? $html_id
403
+			: $html_id . '-frm';
404
+		return $html_id;
405
+	}
406
+
407
+
408
+
409
+	/**
410
+	 * returns HTML for generating the closing form HTML tag (</form>)
411
+	 *
412
+	 * @return string
413
+	 */
414
+	public function form_close()
415
+	{
416
+		return EEH_HTML::nl(-1, 'form')
417
+			   . '</form>'
418
+			   . EEH_HTML::nl()
419
+			   . '<!-- end of ee-'
420
+			   . $this->html_id()
421
+			   . '-form -->'
422
+			   . EEH_HTML::nl();
423
+	}
424
+
425
+
426
+
427
+	/**
428
+	 * enqueues JS (and CSS) for the form (ie immediately call wp_enqueue_script and
429
+	 * wp_enqueue_style; the scripts could have optionally been registered earlier)
430
+	 * Default does nothing, but child classes can override
431
+	 *
432
+	 * @return void
433
+	 */
434
+	public function enqueue_js()
435
+	{
436
+		//defaults to enqueue NO js or css
437
+	}
438
+
439
+
440
+
441
+	/**
442
+	 * Adds any extra data needed by js. Eventually we'll call wp_localize_script
443
+	 * with it, and it will be on each form section's 'other_data' property.
444
+	 * By default nothing is added, but child classes can extend this method to add something.
445
+	 * Eg, if you have an input that will cause a modal dialog to appear,
446
+	 * here you could add an entry like 'modal_dialog_inputs' to this array
447
+	 * to map between the input's html ID and the modal dialogue's ID, so that
448
+	 * your JS code will know where to find the modal dialog when the input is pressed.
449
+	 * Eg $form_other_js_data['modal_dialog_inputs']['some-input-id']='modal-dialog-id';
450
+	 *
451
+	 * @param array $form_other_js_data
452
+	 * @return array
453
+	 */
454
+	public function get_other_js_data($form_other_js_data = array())
455
+	{
456
+		return $form_other_js_data;
457
+	}
458
+
459
+
460
+
461
+	/**
462
+	 * This isn't just the name of an input, it's a path pointing to an input. The
463
+	 * path is similar to a folder path: slash (/) means to descend into a subsection,
464
+	 * dot-dot-slash (../) means to ascend into the parent section.
465
+	 * After a series of slashes and dot-dot-slashes, there should be the name of an input,
466
+	 * which will be returned.
467
+	 * Eg, if you want the related input to be conditional on a sibling input name 'foobar'
468
+	 * just use 'foobar'. If you want it to be conditional on an aunt/uncle input name
469
+	 * 'baz', use '../baz'. If you want it to be conditional on a cousin input,
470
+	 * the child of 'baz_section' named 'baz_child', use '../baz_section/baz_child'.
471
+	 * Etc
472
+	 *
473
+	 * @param string|false $form_section_path we accept false also because substr( '../', '../' ) = false
474
+	 * @return EE_Form_Section_Base
475
+	 */
476
+	public function find_section_from_path($form_section_path)
477
+	{
478
+		if (strpos($form_section_path, '/') === 0) {
479
+			$form_section_path = substr($form_section_path, strlen('/'));
480
+		}
481
+		if (empty($form_section_path)) {
482
+			return $this;
483
+		}
484
+		if (strpos($form_section_path, '../') === 0) {
485
+			$parent = $this->parent_section();
486
+			$form_section_path = substr($form_section_path, strlen('../'));
487
+			if ($parent instanceof EE_Form_Section_Base) {
488
+				return $parent->find_section_from_path($form_section_path);
489
+			} elseif (empty($form_section_path)) {
490
+				return $this;
491
+			}
492
+		}
493
+		//couldn't find it using simple parent following
494
+		return null;
495
+	}
496 496
 
497 497
 
498 498
 }
Please login to merge, or discard this patch.
core/db_classes/EE_Base_Class.class.php 3 patches
Doc Comments   +10 added lines, -9 removed lines patch added patch discarded remove patch
@@ -609,7 +609,7 @@  discard block
 block discarded – undo
609 609
      *
610 610
      * @param \EE_Datetime_Field $datetime_field
611 611
      * @param bool               $pretty
612
-     * @param null $date_or_time
612
+     * @param string|null $date_or_time
613 613
      * @return void
614 614
      * @throws \EE_Error
615 615
      */
@@ -946,7 +946,7 @@  discard block
 block discarded – undo
946 946
      *
947 947
      * @param null  $field_to_order_by  What field is being used as the reference point.
948 948
      * @param array $query_params       Any additional conditions on the query.
949
-     * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
949
+     * @param string  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
950 950
      *                                  you can indicate just the columns you want returned
951 951
      * @return array|EE_Base_Class
952 952
      * @throws \EE_Error
@@ -971,7 +971,7 @@  discard block
 block discarded – undo
971 971
      *
972 972
      * @param null  $field_to_order_by  What field is being used as the reference point.
973 973
      * @param array $query_params       Any additional conditions on the query.
974
-     * @param null  $columns_to_select  If left null, then an EE_Base_Class object is returned, otherwise
974
+     * @param string  $columns_to_select  If left null, then an EE_Base_Class object is returned, otherwise
975 975
      *                                  you can indicate just the column you want returned
976 976
      * @return array|EE_Base_Class
977 977
      * @throws \EE_Error
@@ -1044,7 +1044,7 @@  discard block
 block discarded – undo
1044 1044
      * This method simply returns the RAW unprocessed value for the given property in this class
1045 1045
      *
1046 1046
      * @param  string $field_name A valid fieldname
1047
-     * @return mixed              Whatever the raw value stored on the property is.
1047
+     * @return integer|null              Whatever the raw value stored on the property is.
1048 1048
      * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist.
1049 1049
      */
1050 1050
     public function get_raw($field_name)
@@ -1321,7 +1321,7 @@  discard block
 block discarded – undo
1321 1321
      * sets the time on a datetime property
1322 1322
      *
1323 1323
      * @access protected
1324
-     * @param string|Datetime $time      a valid time string for php datetime functions (or DateTime object)
1324
+     * @param string $time      a valid time string for php datetime functions (or DateTime object)
1325 1325
      * @param string          $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field)
1326 1326
      * @throws \EE_Error
1327 1327
      */
@@ -1336,7 +1336,7 @@  discard block
 block discarded – undo
1336 1336
      * sets the date on a datetime property
1337 1337
      *
1338 1338
      * @access protected
1339
-     * @param string|DateTime $date      a valid date string for php datetime functions ( or DateTime object)
1339
+     * @param string $date      a valid date string for php datetime functions ( or DateTime object)
1340 1340
      * @param string          $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field)
1341 1341
      * @throws \EE_Error
1342 1342
      */
@@ -1399,6 +1399,7 @@  discard block
 block discarded – undo
1399 1399
      * @param mixed (array|string) $args       This is the arguments that will be passed to the callback.
1400 1400
      * @param string               $prepend    You can include something to prepend on the timestamp
1401 1401
      * @param string               $append     You can include something to append on the timestamp
1402
+     * @param string $args
1402 1403
      * @throws EE_Error
1403 1404
      * @return string timestamp
1404 1405
      */
@@ -1790,7 +1791,7 @@  discard block
 block discarded – undo
1790 1791
      *
1791 1792
      * @param  array  $props_n_values   incoming array of properties and their values
1792 1793
      * @param  string $classname        the classname of the child class
1793
-     * @param null    $timezone
1794
+     * @param string|null    $timezone
1794 1795
      * @param array   $date_formats     incoming date_formats in an array where the first value is the
1795 1796
      *                                  date_format and the second value is the time format
1796 1797
      * @return mixed (EE_Base_Class|bool)
@@ -1868,7 +1869,7 @@  discard block
 block discarded – undo
1868 1869
      * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee)
1869 1870
      *
1870 1871
      * @param string $model_classname
1871
-     * @param null   $timezone
1872
+     * @param string|null   $timezone
1872 1873
      * @return EEM_Base
1873 1874
      */
1874 1875
     protected static function _get_model_instance_with_name($model_classname, $timezone = null)
@@ -2367,7 +2368,7 @@  discard block
 block discarded – undo
2367 2368
      *
2368 2369
      * @param string $meta_key
2369 2370
      * @param mixed  $meta_value
2370
-     * @param mixed  $previous_value
2371
+     * @param boolean  $previous_value
2371 2372
      * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row)
2372 2373
      * @throws \EE_Error
2373 2374
      * NOTE: if the values haven't changed, returns 0
Please login to merge, or discard this patch.
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -155,8 +155,8 @@  discard block
 block discarded – undo
155 155
             list($this->_dt_frmt, $this->_tm_frmt) = $date_formats;
156 156
         } else {
157 157
             //set default formats for date and time
158
-            $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d');
159
-            $this->_tm_frmt = (string)get_option('time_format', 'g:i a');
158
+            $this->_dt_frmt = (string) get_option('date_format', 'Y-m-d');
159
+            $this->_tm_frmt = (string) get_option('time_format', 'g:i a');
160 160
         }
161 161
         //if db model is instantiating
162 162
         if ($bydb) {
@@ -478,7 +478,7 @@  discard block
 block discarded – undo
478 478
      */
479 479
     public function get_format($full = true)
480 480
     {
481
-        return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt);
481
+        return $full ? $this->_dt_frmt.' '.$this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt);
482 482
     }
483 483
 
484 484
 
@@ -587,7 +587,7 @@  discard block
 block discarded – undo
587 587
         $model = $this->get_model();
588 588
         $model->field_settings_for($fieldname);
589 589
         $cache_type = $pretty ? 'pretty' : 'standard';
590
-        $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : '';
590
+        $cache_type .= ! empty($extra_cache_ref) ? '_'.$extra_cache_ref : '';
591 591
         if (isset($this->_cached_properties[$fieldname][$cache_type])) {
592 592
             return $this->_cached_properties[$fieldname][$cache_type];
593 593
         }
@@ -802,7 +802,7 @@  discard block
 block discarded – undo
802 802
         $current_cache_id = ''
803 803
     ) {
804 804
         // verify that incoming object is of the correct type
805
-        $obj_class = 'EE_' . $relationName;
805
+        $obj_class = 'EE_'.$relationName;
806 806
         if ($newly_saved_object instanceof $obj_class) {
807 807
             /* @type EE_Base_Class $newly_saved_object */
808 808
             // now get the type of relation
@@ -1288,7 +1288,7 @@  discard block
 block discarded – undo
1288 1288
      */
1289 1289
     public function get_i18n_datetime($field_name, $format = '')
1290 1290
     {
1291
-        $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format;
1291
+        $format = empty($format) ? $this->_dt_frmt.' '.$this->_tm_frmt : $format;
1292 1292
         return date_i18n(
1293 1293
             $format,
1294 1294
             EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone)
@@ -1426,8 +1426,8 @@  discard block
 block discarded – undo
1426 1426
         }
1427 1427
         $original_timezone = $this->_timezone;
1428 1428
         $this->set_timezone($timezone);
1429
-        $fn = (array)$field_name;
1430
-        $args = array_merge($fn, (array)$args);
1429
+        $fn = (array) $field_name;
1430
+        $args = array_merge($fn, (array) $args);
1431 1431
         if ( ! method_exists($this, $callback)) {
1432 1432
             throw new EE_Error(
1433 1433
                 sprintf(
@@ -1439,8 +1439,8 @@  discard block
 block discarded – undo
1439 1439
                 )
1440 1440
             );
1441 1441
         }
1442
-        $args = (array)$args;
1443
-        $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append;
1442
+        $args = (array) $args;
1443
+        $return = $prepend.call_user_func_array(array($this, $callback), $args).$append;
1444 1444
         $this->set_timezone($original_timezone);
1445 1445
         return $return;
1446 1446
     }
@@ -1579,14 +1579,14 @@  discard block
 block discarded – undo
1579 1579
          * @param array         $set_cols_n_values
1580 1580
          * @param EE_Base_Class $model_object
1581 1581
          */
1582
-        $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values,
1582
+        $set_cols_n_values = (array) apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values,
1583 1583
             $this);
1584 1584
         //set attributes as provided in $set_cols_n_values
1585 1585
         foreach ($set_cols_n_values as $column => $value) {
1586 1586
             $this->set($column, $value);
1587 1587
         }
1588 1588
         // no changes ? then don't do anything
1589
-        if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) {
1589
+        if ( ! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) {
1590 1590
             return 0;
1591 1591
         }
1592 1592
         /**
@@ -1638,8 +1638,8 @@  discard block
 block discarded – undo
1638 1638
                                 __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s',
1639 1639
                                     'event_espresso'),
1640 1640
                                 get_class($this),
1641
-                                get_class($model) . '::instance()->add_to_entity_map()',
1642
-                                get_class($model) . '::instance()->get_one_by_ID()',
1641
+                                get_class($model).'::instance()->add_to_entity_map()',
1642
+                                get_class($model).'::instance()->get_one_by_ID()',
1643 1643
                                 '<br />'
1644 1644
                             )
1645 1645
                         );
@@ -1772,7 +1772,7 @@  discard block
 block discarded – undo
1772 1772
      */
1773 1773
     public function get_model()
1774 1774
     {
1775
-        if( ! $this->_model){
1775
+        if ( ! $this->_model) {
1776 1776
             $modelName = self::_get_model_classname(get_class($this));
1777 1777
             $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone);
1778 1778
         } else {
@@ -1915,7 +1915,7 @@  discard block
 block discarded – undo
1915 1915
         if (strpos($model_name, "EE_") === 0) {
1916 1916
             $model_classname = str_replace("EE_", "EEM_", $model_name);
1917 1917
         } else {
1918
-            $model_classname = "EEM_" . $model_name;
1918
+            $model_classname = "EEM_".$model_name;
1919 1919
         }
1920 1920
         return $model_classname;
1921 1921
     }
@@ -2302,7 +2302,7 @@  discard block
 block discarded – undo
2302 2302
      */
2303 2303
     protected function _property_exists($properties)
2304 2304
     {
2305
-        foreach ((array)$properties as $property_name) {
2305
+        foreach ((array) $properties as $property_name) {
2306 2306
             //first make sure this property exists
2307 2307
             if ( ! $this->_fields[$property_name]) {
2308 2308
                 throw new EE_Error(
@@ -2632,8 +2632,8 @@  discard block
 block discarded – undo
2632 2632
                         __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.',
2633 2633
                             'event_espresso'),
2634 2634
                         $this->ID(),
2635
-                        get_class($this->get_model()) . '::instance()->add_to_entity_map()',
2636
-                        get_class($this->get_model()) . '::instance()->refresh_entity_map()'
2635
+                        get_class($this->get_model()).'::instance()->add_to_entity_map()',
2636
+                        get_class($this->get_model()).'::instance()->refresh_entity_map()'
2637 2637
                     )
2638 2638
                 );
2639 2639
             }
@@ -2693,7 +2693,7 @@  discard block
 block discarded – undo
2693 2693
         $model = $this->get_model();
2694 2694
         foreach ($model->relation_settings() as $relation_name => $relation_obj) {
2695 2695
             if ($relation_obj instanceof EE_Belongs_To_Relation) {
2696
-                $classname = 'EE_' . $model->get_this_model_name();
2696
+                $classname = 'EE_'.$model->get_this_model_name();
2697 2697
                 if (
2698 2698
                     $this->get_one_from_cache($relation_name) instanceof $classname
2699 2699
                     && $this->get_one_from_cache($relation_name)->ID()
Please login to merge, or discard this patch.
Indentation   +2709 added lines, -2709 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) {
2
-    exit('No direct script access allowed');
2
+	exit('No direct script access allowed');
3 3
 }
4 4
 do_action('AHEE_log', __FILE__, ' FILE LOADED', '');
5 5
 
@@ -25,2714 +25,2714 @@  discard block
 block discarded – undo
25 25
 abstract class EE_Base_Class
26 26
 {
27 27
 
28
-    /**
29
-     * This is an array of the original properties and values provided during construction
30
-     * of this model object. (keys are model field names, values are their values).
31
-     * This list is important to remember so that when we are merging data from the db, we know
32
-     * which values to override and which to not override.
33
-     *
34
-     * @var array
35
-     */
36
-    protected $_props_n_values_provided_in_constructor;
37
-
38
-    /**
39
-     * Timezone
40
-     * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in.
41
-     * This can also be used before a get to set what timezone you want strings coming out of the object to be in.  NOT
42
-     * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have
43
-     * access to it.
44
-     *
45
-     * @var string
46
-     */
47
-    protected $_timezone;
48
-
49
-
50
-
51
-    /**
52
-     * date format
53
-     * pattern or format for displaying dates
54
-     *
55
-     * @var string $_dt_frmt
56
-     */
57
-    protected $_dt_frmt;
58
-
59
-
60
-
61
-    /**
62
-     * time format
63
-     * pattern or format for displaying time
64
-     *
65
-     * @var string $_tm_frmt
66
-     */
67
-    protected $_tm_frmt;
68
-
69
-
70
-
71
-    /**
72
-     * This property is for holding a cached array of object properties indexed by property name as the key.
73
-     * The purpose of this is for setting a cache on properties that may have calculated values after a
74
-     * prepare_for_get.  That way the cache can be checked first and the calculated property returned instead of having
75
-     * to recalculate. Used by _set_cached_property() and _get_cached_property() methods.
76
-     *
77
-     * @var array
78
-     */
79
-    protected $_cached_properties = array();
80
-
81
-    /**
82
-     * An array containing keys of the related model, and values are either an array of related mode objects or a
83
-     * single
84
-     * related model object. see the model's _model_relations. The keys should match those specified. And if the
85
-     * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object,
86
-     * all others have an array)
87
-     *
88
-     * @var array
89
-     */
90
-    protected $_model_relations = array();
91
-
92
-    /**
93
-     * Array where keys are field names (see the model's _fields property) and values are their values. To see what
94
-     * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods)
95
-     *
96
-     * @var array
97
-     */
98
-    protected $_fields = array();
99
-
100
-    /**
101
-     * @var boolean indicating whether or not this model object is intended to ever be saved
102
-     * For example, we might create model objects intended to only be used for the duration
103
-     * of this request and to be thrown away, and if they were accidentally saved
104
-     * it would be a bug.
105
-     */
106
-    protected $_allow_persist = true;
107
-
108
-    /**
109
-     * @var boolean indicating whether or not this model object's properties have changed since construction
110
-     */
111
-    protected $_has_changes = false;
112
-
113
-    /**
114
-     * @var EEM_Base
115
-     */
116
-    protected $_model;
117
-
118
-
119
-
120
-    /**
121
-     * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children
122
-     * play nice
123
-     *
124
-     * @param array   $fieldValues                             where each key is a field (ie, array key in the 2nd
125
-     *                                                         layer of the model's _fields array, (eg, EVT_ID,
126
-     *                                                         TXN_amount, QST_name, etc) and values are their values
127
-     * @param boolean $bydb                                    a flag for setting if the class is instantiated by the
128
-     *                                                         corresponding db model or not.
129
-     * @param string  $timezone                                indicate what timezone you want any datetime fields to
130
-     *                                                         be in when instantiating a EE_Base_Class object.
131
-     * @param array   $date_formats                            An array of date formats to set on construct where first
132
-     *                                                         value is the date_format and second value is the time
133
-     *                                                         format.
134
-     * @throws EE_Error
135
-     */
136
-    protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array())
137
-    {
138
-        $className = get_class($this);
139
-        do_action("AHEE__{$className}__construct", $this, $fieldValues);
140
-        $model = $this->get_model();
141
-        $model_fields = $model->field_settings(false);
142
-        // ensure $fieldValues is an array
143
-        $fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues);
144
-        // EEH_Debug_Tools::printr( $fieldValues, '$fieldValues  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
145
-        // verify client code has not passed any invalid field names
146
-        foreach ($fieldValues as $field_name => $field_value) {
147
-            if ( ! isset($model_fields[$field_name])) {
148
-                throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s",
149
-                    "event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields))));
150
-            }
151
-        }
152
-        // EEH_Debug_Tools::printr( $model_fields, '$model_fields  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
153
-        $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
154
-        if ( ! empty($date_formats) && is_array($date_formats)) {
155
-            list($this->_dt_frmt, $this->_tm_frmt) = $date_formats;
156
-        } else {
157
-            //set default formats for date and time
158
-            $this->_dt_frmt = (string)get_option('date_format', 'Y-m-d');
159
-            $this->_tm_frmt = (string)get_option('time_format', 'g:i a');
160
-        }
161
-        //if db model is instantiating
162
-        if ($bydb) {
163
-            //client code has indicated these field values are from the database
164
-            foreach ($model_fields as $fieldName => $field) {
165
-                $this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null);
166
-            }
167
-        } else {
168
-            //we're constructing a brand
169
-            //new instance of the model object. Generally, this means we'll need to do more field validation
170
-            foreach ($model_fields as $fieldName => $field) {
171
-                $this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true);
172
-            }
173
-        }
174
-        //remember what values were passed to this constructor
175
-        $this->_props_n_values_provided_in_constructor = $fieldValues;
176
-        //remember in entity mapper
177
-        if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) {
178
-            $model->add_to_entity_map($this);
179
-        }
180
-        //setup all the relations
181
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
182
-            if ($relation_obj instanceof EE_Belongs_To_Relation) {
183
-                $this->_model_relations[$relation_name] = null;
184
-            } else {
185
-                $this->_model_relations[$relation_name] = array();
186
-            }
187
-        }
188
-        /**
189
-         * Action done at the end of each model object construction
190
-         *
191
-         * @param EE_Base_Class $this the model object just created
192
-         */
193
-        do_action('AHEE__EE_Base_Class__construct__finished', $this);
194
-    }
195
-
196
-
197
-
198
-    /**
199
-     * Gets whether or not this model object is allowed to persist/be saved to the database.
200
-     *
201
-     * @return boolean
202
-     */
203
-    public function allow_persist()
204
-    {
205
-        return $this->_allow_persist;
206
-    }
207
-
208
-
209
-
210
-    /**
211
-     * Sets whether or not this model object should be allowed to be saved to the DB.
212
-     * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless
213
-     * you got new information that somehow made you change your mind.
214
-     *
215
-     * @param boolean $allow_persist
216
-     * @return boolean
217
-     */
218
-    public function set_allow_persist($allow_persist)
219
-    {
220
-        return $this->_allow_persist = $allow_persist;
221
-    }
222
-
223
-
224
-
225
-    /**
226
-     * Gets the field's original value when this object was constructed during this request.
227
-     * This can be helpful when determining if a model object has changed or not
228
-     *
229
-     * @param string $field_name
230
-     * @return mixed|null
231
-     * @throws \EE_Error
232
-     */
233
-    public function get_original($field_name)
234
-    {
235
-        if (isset($this->_props_n_values_provided_in_constructor[$field_name])
236
-            && $field_settings = $this->get_model()->field_settings_for($field_name)
237
-        ) {
238
-            return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]);
239
-        } else {
240
-            return null;
241
-        }
242
-    }
243
-
244
-
245
-
246
-    /**
247
-     * @param EE_Base_Class $obj
248
-     * @return string
249
-     */
250
-    public function get_class($obj)
251
-    {
252
-        return get_class($obj);
253
-    }
254
-
255
-
256
-
257
-    /**
258
-     * Overrides parent because parent expects old models.
259
-     * This also doesn't do any validation, and won't work for serialized arrays
260
-     *
261
-     * @param    string $field_name
262
-     * @param    mixed  $field_value
263
-     * @param bool      $use_default
264
-     * @throws \EE_Error
265
-     */
266
-    public function set($field_name, $field_value, $use_default = false)
267
-    {
268
-        // if not using default and nothing has changed, and object has already been setup (has ID),
269
-        // then don't do anything
270
-        if (
271
-            ! $use_default
272
-            && $this->_fields[$field_name] === $field_value
273
-            && $this->ID()
274
-        ) {
275
-            return;
276
-        }
277
-        $model = $this->get_model();
278
-        $this->_has_changes = true;
279
-        $field_obj = $model->field_settings_for($field_name);
280
-        if ($field_obj instanceof EE_Model_Field_Base) {
281
-            //			if ( method_exists( $field_obj, 'set_timezone' )) {
282
-            if ($field_obj instanceof EE_Datetime_Field) {
283
-                $field_obj->set_timezone($this->_timezone);
284
-                $field_obj->set_date_format($this->_dt_frmt);
285
-                $field_obj->set_time_format($this->_tm_frmt);
286
-            }
287
-            $holder_of_value = $field_obj->prepare_for_set($field_value);
288
-            //should the value be null?
289
-            if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) {
290
-                $this->_fields[$field_name] = $field_obj->get_default_value();
291
-                /**
292
-                 * To save having to refactor all the models, if a default value is used for a
293
-                 * EE_Datetime_Field, and that value is not null nor is it a DateTime
294
-                 * object.  Then let's do a set again to ensure that it becomes a DateTime
295
-                 * object.
296
-                 *
297
-                 * @since 4.6.10+
298
-                 */
299
-                if (
300
-                    $field_obj instanceof EE_Datetime_Field
301
-                    && $this->_fields[$field_name] !== null
302
-                    && ! $this->_fields[$field_name] instanceof DateTime
303
-                ) {
304
-                    empty($this->_fields[$field_name])
305
-                        ? $this->set($field_name, time())
306
-                        : $this->set($field_name, $this->_fields[$field_name]);
307
-                }
308
-            } else {
309
-                $this->_fields[$field_name] = $holder_of_value;
310
-            }
311
-            //if we're not in the constructor...
312
-            //now check if what we set was a primary key
313
-            if (
314
-                //note: props_n_values_provided_in_constructor is only set at the END of the constructor
315
-                $this->_props_n_values_provided_in_constructor
316
-                && $field_value
317
-                && $field_name === $model->primary_key_name()
318
-            ) {
319
-                //if so, we want all this object's fields to be filled either with
320
-                //what we've explicitly set on this model
321
-                //or what we have in the db
322
-                // echo "setting primary key!";
323
-                $fields_on_model = self::_get_model(get_class($this))->field_settings();
324
-                $obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value);
325
-                foreach ($fields_on_model as $field_obj) {
326
-                    if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor)
327
-                         && $field_obj->get_name() !== $field_name
328
-                    ) {
329
-                        $this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name()));
330
-                    }
331
-                }
332
-                //oh this model object has an ID? well make sure its in the entity mapper
333
-                $model->add_to_entity_map($this);
334
-            }
335
-            //let's unset any cache for this field_name from the $_cached_properties property.
336
-            $this->_clear_cached_property($field_name);
337
-        } else {
338
-            throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s",
339
-                "event_espresso"), $field_name));
340
-        }
341
-    }
342
-
343
-
344
-
345
-    /**
346
-     * This sets the field value on the db column if it exists for the given $column_name or
347
-     * saves it to EE_Extra_Meta if the given $column_name does not match a db column.
348
-     *
349
-     * @see EE_message::get_column_value for related documentation on the necessity of this method.
350
-     * @param string $field_name  Must be the exact column name.
351
-     * @param mixed  $field_value The value to set.
352
-     * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs.
353
-     * @throws \EE_Error
354
-     */
355
-    public function set_field_or_extra_meta($field_name, $field_value)
356
-    {
357
-        if ($this->get_model()->has_field($field_name)) {
358
-            $this->set($field_name, $field_value);
359
-            return true;
360
-        } else {
361
-            //ensure this object is saved first so that extra meta can be properly related.
362
-            $this->save();
363
-            return $this->update_extra_meta($field_name, $field_value);
364
-        }
365
-    }
366
-
367
-
368
-
369
-    /**
370
-     * This retrieves the value of the db column set on this class or if that's not present
371
-     * it will attempt to retrieve from extra_meta if found.
372
-     * Example Usage:
373
-     * Via EE_Message child class:
374
-     * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to",
375
-     * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may
376
-     * also have additional main fields specific to the messenger.  The system accommodates those extra
377
-     * fields through the EE_Extra_Meta table.  This method allows for EE_messengers to retrieve the
378
-     * value for those extra fields dynamically via the EE_message object.
379
-     *
380
-     * @param  string $field_name expecting the fully qualified field name.
381
-     * @return mixed|null  value for the field if found.  null if not found.
382
-     * @throws \EE_Error
383
-     */
384
-    public function get_field_or_extra_meta($field_name)
385
-    {
386
-        if ($this->get_model()->has_field($field_name)) {
387
-            $column_value = $this->get($field_name);
388
-        } else {
389
-            //This isn't a column in the main table, let's see if it is in the extra meta.
390
-            $column_value = $this->get_extra_meta($field_name, true, null);
391
-        }
392
-        return $column_value;
393
-    }
394
-
395
-
396
-
397
-    /**
398
-     * See $_timezone property for description of what the timezone property is for.  This SETS the timezone internally
399
-     * for being able to reference what timezone we are running conversions on when converting TO the internal timezone
400
-     * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is
401
-     * available to all child classes that may be using the EE_Datetime_Field for a field data type.
402
-     *
403
-     * @access public
404
-     * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php
405
-     * @return void
406
-     * @throws \EE_Error
407
-     */
408
-    public function set_timezone($timezone = '')
409
-    {
410
-        $this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
411
-        //make sure we clear all cached properties because they won't be relevant now
412
-        $this->_clear_cached_properties();
413
-        //make sure we update field settings and the date for all EE_Datetime_Fields
414
-        $model_fields = $this->get_model()->field_settings(false);
415
-        foreach ($model_fields as $field_name => $field_obj) {
416
-            if ($field_obj instanceof EE_Datetime_Field) {
417
-                $field_obj->set_timezone($this->_timezone);
418
-                if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) {
419
-                    $this->_fields[$field_name]->setTimezone(new DateTimeZone($this->_timezone));
420
-                }
421
-            }
422
-        }
423
-    }
424
-
425
-
426
-
427
-    /**
428
-     * This just returns whatever is set for the current timezone.
429
-     *
430
-     * @access public
431
-     * @return string timezone string
432
-     */
433
-    public function get_timezone()
434
-    {
435
-        return $this->_timezone;
436
-    }
437
-
438
-
439
-
440
-    /**
441
-     * This sets the internal date format to what is sent in to be used as the new default for the class
442
-     * internally instead of wp set date format options
443
-     *
444
-     * @since 4.6
445
-     * @param string $format should be a format recognizable by PHP date() functions.
446
-     */
447
-    public function set_date_format($format)
448
-    {
449
-        $this->_dt_frmt = $format;
450
-        //clear cached_properties because they won't be relevant now.
451
-        $this->_clear_cached_properties();
452
-    }
453
-
454
-
455
-
456
-    /**
457
-     * This sets the internal time format string to what is sent in to be used as the new default for the
458
-     * class internally instead of wp set time format options.
459
-     *
460
-     * @since 4.6
461
-     * @param string $format should be a format recognizable by PHP date() functions.
462
-     */
463
-    public function set_time_format($format)
464
-    {
465
-        $this->_tm_frmt = $format;
466
-        //clear cached_properties because they won't be relevant now.
467
-        $this->_clear_cached_properties();
468
-    }
469
-
470
-
471
-
472
-    /**
473
-     * This returns the current internal set format for the date and time formats.
474
-     *
475
-     * @param bool $full           if true (default), then return the full format.  Otherwise will return an array
476
-     *                             where the first value is the date format and the second value is the time format.
477
-     * @return mixed string|array
478
-     */
479
-    public function get_format($full = true)
480
-    {
481
-        return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt);
482
-    }
483
-
484
-
485
-
486
-    /**
487
-     * cache
488
-     * stores the passed model object on the current model object.
489
-     * In certain circumstances, we can use this cached model object instead of querying for another one entirely.
490
-     *
491
-     * @param string        $relationName    one of the keys in the _model_relations array on the model. Eg
492
-     *                                       'Registration' associated with this model object
493
-     * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction,
494
-     *                                       that could be a payment or a registration)
495
-     * @param null          $cache_id        a string or number that will be used as the key for any Belongs_To_Many
496
-     *                                       items which will be stored in an array on this object
497
-     * @throws EE_Error
498
-     * @return mixed    index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one
499
-     *                  related thing, no array)
500
-     */
501
-    public function cache($relationName = '', $object_to_cache = null, $cache_id = null)
502
-    {
503
-        // its entirely possible that there IS no related object yet in which case there is nothing to cache.
504
-        if ( ! $object_to_cache instanceof EE_Base_Class) {
505
-            return false;
506
-        }
507
-        // also get "how" the object is related, or throw an error
508
-        if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) {
509
-            throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'),
510
-                $relationName, get_class($this)));
511
-        }
512
-        // how many things are related ?
513
-        if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
514
-            // if it's a "belongs to" relationship, then there's only one related model object  eg, if this is a registration, there's only 1 attendee for it
515
-            // so for these model objects just set it to be cached
516
-            $this->_model_relations[$relationName] = $object_to_cache;
517
-            $return = true;
518
-        } else {
519
-            // otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type.
520
-            // eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array
521
-            if ( ! is_array($this->_model_relations[$relationName])) {
522
-                // if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array
523
-                $this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class
524
-                    ? array($this->_model_relations[$relationName]) : array();
525
-            }
526
-            // first check for a cache_id which is normally empty
527
-            if ( ! empty($cache_id)) {
528
-                // if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on
529
-                $this->_model_relations[$relationName][$cache_id] = $object_to_cache;
530
-                $return = $cache_id;
531
-            } elseif ($object_to_cache->ID()) {
532
-                // OR the cached object originally came from the db, so let's just use it's PK for an ID
533
-                $this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache;
534
-                $return = $object_to_cache->ID();
535
-            } else {
536
-                // OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID
537
-                $this->_model_relations[$relationName][] = $object_to_cache;
538
-                // move the internal pointer to the end of the array
539
-                end($this->_model_relations[$relationName]);
540
-                // and grab the key so that we can return it
541
-                $return = key($this->_model_relations[$relationName]);
542
-            }
543
-        }
544
-        return $return;
545
-    }
546
-
547
-
548
-
549
-    /**
550
-     * For adding an item to the cached_properties property.
551
-     *
552
-     * @access protected
553
-     * @param string      $fieldname the property item the corresponding value is for.
554
-     * @param mixed       $value     The value we are caching.
555
-     * @param string|null $cache_type
556
-     * @return void
557
-     * @throws \EE_Error
558
-     */
559
-    protected function _set_cached_property($fieldname, $value, $cache_type = null)
560
-    {
561
-        //first make sure this property exists
562
-        $this->get_model()->field_settings_for($fieldname);
563
-        $cache_type = empty($cache_type) ? 'standard' : $cache_type;
564
-        $this->_cached_properties[$fieldname][$cache_type] = $value;
565
-    }
566
-
567
-
568
-
569
-    /**
570
-     * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist.
571
-     * This also SETS the cache if we return the actual property!
572
-     *
573
-     * @param string $fieldname        the name of the property we're trying to retrieve
574
-     * @param bool   $pretty
575
-     * @param string $extra_cache_ref  This allows the user to specify an extra cache ref for the given property
576
-     *                                 (in cases where the same property may be used for different outputs
577
-     *                                 - i.e. datetime, money etc.)
578
-     *                                 It can also accept certain pre-defined "schema" strings
579
-     *                                 to define how to output the property.
580
-     *                                 see the field's prepare_for_pretty_echoing for what strings can be used
581
-     * @return mixed                   whatever the value for the property is we're retrieving
582
-     * @throws \EE_Error
583
-     */
584
-    protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null)
585
-    {
586
-        //verify the field exists
587
-        $model = $this->get_model();
588
-        $model->field_settings_for($fieldname);
589
-        $cache_type = $pretty ? 'pretty' : 'standard';
590
-        $cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : '';
591
-        if (isset($this->_cached_properties[$fieldname][$cache_type])) {
592
-            return $this->_cached_properties[$fieldname][$cache_type];
593
-        }
594
-        $value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref);
595
-        $this->_set_cached_property($fieldname, $value, $cache_type);
596
-        return $value;
597
-    }
598
-
599
-
600
-
601
-    /**
602
-     * If the cache didn't fetch the needed item, this fetches it.
603
-     * @param string $fieldname
604
-     * @param bool $pretty
605
-     * @param string $extra_cache_ref
606
-     * @return mixed
607
-     */
608
-    protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null)
609
-    {
610
-        $field_obj = $this->get_model()->field_settings_for($fieldname);
611
-        // If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct
612
-        if ($field_obj instanceof EE_Datetime_Field) {
613
-            $this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref);
614
-        }
615
-        if ( ! isset($this->_fields[$fieldname])) {
616
-            $this->_fields[$fieldname] = null;
617
-        }
618
-        $value = $pretty
619
-            ? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref)
620
-            : $field_obj->prepare_for_get($this->_fields[$fieldname]);
621
-        return $value;
622
-    }
623
-
624
-
625
-
626
-    /**
627
-     * set timezone, formats, and output for EE_Datetime_Field objects
628
-     *
629
-     * @param \EE_Datetime_Field $datetime_field
630
-     * @param bool               $pretty
631
-     * @param null $date_or_time
632
-     * @return void
633
-     * @throws \EE_Error
634
-     */
635
-    protected function _prepare_datetime_field(
636
-        EE_Datetime_Field $datetime_field,
637
-        $pretty = false,
638
-        $date_or_time = null
639
-    ) {
640
-        $datetime_field->set_timezone($this->_timezone);
641
-        $datetime_field->set_date_format($this->_dt_frmt, $pretty);
642
-        $datetime_field->set_time_format($this->_tm_frmt, $pretty);
643
-        //set the output returned
644
-        switch ($date_or_time) {
645
-            case 'D' :
646
-                $datetime_field->set_date_time_output('date');
647
-                break;
648
-            case 'T' :
649
-                $datetime_field->set_date_time_output('time');
650
-                break;
651
-            default :
652
-                $datetime_field->set_date_time_output();
653
-        }
654
-    }
655
-
656
-
657
-
658
-    /**
659
-     * This just takes care of clearing out the cached_properties
660
-     *
661
-     * @return void
662
-     */
663
-    protected function _clear_cached_properties()
664
-    {
665
-        $this->_cached_properties = array();
666
-    }
667
-
668
-
669
-
670
-    /**
671
-     * This just clears out ONE property if it exists in the cache
672
-     *
673
-     * @param  string $property_name the property to remove if it exists (from the _cached_properties array)
674
-     * @return void
675
-     */
676
-    protected function _clear_cached_property($property_name)
677
-    {
678
-        if (isset($this->_cached_properties[$property_name])) {
679
-            unset($this->_cached_properties[$property_name]);
680
-        }
681
-    }
682
-
683
-
684
-
685
-    /**
686
-     * Ensures that this related thing is a model object.
687
-     *
688
-     * @param mixed  $object_or_id EE_base_Class/int/string either a related model object, or its ID
689
-     * @param string $model_name   name of the related thing, eg 'Attendee',
690
-     * @return EE_Base_Class
691
-     * @throws \EE_Error
692
-     */
693
-    protected function ensure_related_thing_is_model_obj($object_or_id, $model_name)
694
-    {
695
-        $other_model_instance = self::_get_model_instance_with_name(
696
-            self::_get_model_classname($model_name),
697
-            $this->_timezone
698
-        );
699
-        return $other_model_instance->ensure_is_obj($object_or_id);
700
-    }
701
-
702
-
703
-
704
-    /**
705
-     * Forgets the cached model of the given relation Name. So the next time we request it,
706
-     * we will fetch it again from the database. (Handy if you know it's changed somehow).
707
-     * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM,
708
-     * then only remove that one object from our cached array. Otherwise, clear the entire list
709
-     *
710
-     * @param string $relationName                         one of the keys in the _model_relations array on the model.
711
-     *                                                     Eg 'Registration'
712
-     * @param mixed  $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL
713
-     *                                                     if you intend to use $clear_all = TRUE, or the relation only
714
-     *                                                     has 1 object anyways (ie, it's a BelongsToRelation)
715
-     * @param bool   $clear_all                            This flags clearing the entire cache relation property if
716
-     *                                                     this is HasMany or HABTM.
717
-     * @throws EE_Error
718
-     * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a
719
-     *                       relation from all
720
-     */
721
-    public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false)
722
-    {
723
-        $relationship_to_model = $this->get_model()->related_settings_for($relationName);
724
-        $index_in_cache = '';
725
-        if ( ! $relationship_to_model) {
726
-            throw new EE_Error(
727
-                sprintf(
728
-                    __("There is no relationship to %s on a %s. Cannot clear that cache", 'event_espresso'),
729
-                    $relationName,
730
-                    get_class($this)
731
-                )
732
-            );
733
-        }
734
-        if ($clear_all) {
735
-            $obj_removed = true;
736
-            $this->_model_relations[$relationName] = null;
737
-        } elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) {
738
-            $obj_removed = $this->_model_relations[$relationName];
739
-            $this->_model_relations[$relationName] = null;
740
-        } else {
741
-            if ($object_to_remove_or_index_into_array instanceof EE_Base_Class
742
-                && $object_to_remove_or_index_into_array->ID()
743
-            ) {
744
-                $index_in_cache = $object_to_remove_or_index_into_array->ID();
745
-                if (is_array($this->_model_relations[$relationName])
746
-                    && ! isset($this->_model_relations[$relationName][$index_in_cache])
747
-                ) {
748
-                    $index_found_at = null;
749
-                    //find this object in the array even though it has a different key
750
-                    foreach ($this->_model_relations[$relationName] as $index => $obj) {
751
-                        if (
752
-                            $obj instanceof EE_Base_Class
753
-                            && (
754
-                                $obj == $object_to_remove_or_index_into_array
755
-                                || $obj->ID() === $object_to_remove_or_index_into_array->ID()
756
-                            )
757
-                        ) {
758
-                            $index_found_at = $index;
759
-                            break;
760
-                        }
761
-                    }
762
-                    if ($index_found_at) {
763
-                        $index_in_cache = $index_found_at;
764
-                    } else {
765
-                        //it wasn't found. huh. well obviously it doesn't need to be removed from teh cache
766
-                        //if it wasn't in it to begin with. So we're done
767
-                        return $object_to_remove_or_index_into_array;
768
-                    }
769
-                }
770
-            } elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) {
771
-                //so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it!
772
-                foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) {
773
-                    if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) {
774
-                        $index_in_cache = $index;
775
-                    }
776
-                }
777
-            } else {
778
-                $index_in_cache = $object_to_remove_or_index_into_array;
779
-            }
780
-            //supposedly we've found it. But it could just be that the client code
781
-            //provided a bad index/object
782
-            if (
783
-            isset(
784
-                $this->_model_relations[$relationName],
785
-                $this->_model_relations[$relationName][$index_in_cache]
786
-            )
787
-            ) {
788
-                $obj_removed = $this->_model_relations[$relationName][$index_in_cache];
789
-                unset($this->_model_relations[$relationName][$index_in_cache]);
790
-            } else {
791
-                //that thing was never cached anyways.
792
-                $obj_removed = null;
793
-            }
794
-        }
795
-        return $obj_removed;
796
-    }
797
-
798
-
799
-
800
-    /**
801
-     * update_cache_after_object_save
802
-     * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has
803
-     * obtained after being saved to the db
804
-     *
805
-     * @param string         $relationName       - the type of object that is cached
806
-     * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached
807
-     * @param string         $current_cache_id   - the ID that was used when originally caching the object
808
-     * @return boolean TRUE on success, FALSE on fail
809
-     * @throws \EE_Error
810
-     */
811
-    public function update_cache_after_object_save(
812
-        $relationName,
813
-        EE_Base_Class $newly_saved_object,
814
-        $current_cache_id = ''
815
-    ) {
816
-        // verify that incoming object is of the correct type
817
-        $obj_class = 'EE_' . $relationName;
818
-        if ($newly_saved_object instanceof $obj_class) {
819
-            /* @type EE_Base_Class $newly_saved_object */
820
-            // now get the type of relation
821
-            $relationship_to_model = $this->get_model()->related_settings_for($relationName);
822
-            // if this is a 1:1 relationship
823
-            if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
824
-                // then just replace the cached object with the newly saved object
825
-                $this->_model_relations[$relationName] = $newly_saved_object;
826
-                return true;
827
-                // or if it's some kind of sordid feral polyamorous relationship...
828
-            } elseif (is_array($this->_model_relations[$relationName])
829
-                      && isset($this->_model_relations[$relationName][$current_cache_id])
830
-            ) {
831
-                // then remove the current cached item
832
-                unset($this->_model_relations[$relationName][$current_cache_id]);
833
-                // and cache the newly saved object using it's new ID
834
-                $this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object;
835
-                return true;
836
-            }
837
-        }
838
-        return false;
839
-    }
840
-
841
-
842
-
843
-    /**
844
-     * Fetches a single EE_Base_Class on that relation. (If the relation is of type
845
-     * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects)
846
-     *
847
-     * @param string $relationName
848
-     * @return EE_Base_Class
849
-     */
850
-    public function get_one_from_cache($relationName)
851
-    {
852
-        $cached_array_or_object = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName]
853
-            : null;
854
-        if (is_array($cached_array_or_object)) {
855
-            return array_shift($cached_array_or_object);
856
-        } else {
857
-            return $cached_array_or_object;
858
-        }
859
-    }
860
-
861
-
862
-
863
-    /**
864
-     * Fetches a single EE_Base_Class on that relation. (If the relation is of type
865
-     * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects)
866
-     *
867
-     * @param string $relationName
868
-     * @throws \EE_Error
869
-     * @return EE_Base_Class[] NOT necessarily indexed by primary keys
870
-     */
871
-    public function get_all_from_cache($relationName)
872
-    {
873
-        $objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array();
874
-        // if the result is not an array, but exists, make it an array
875
-        $objects = is_array($objects) ? $objects : array($objects);
876
-        //bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143
877
-        //basically, if this model object was stored in the session, and these cached model objects
878
-        //already have IDs, let's make sure they're in their model's entity mapper
879
-        //otherwise we will have duplicates next time we call
880
-        // EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() );
881
-        $model = EE_Registry::instance()->load_model($relationName);
882
-        foreach ($objects as $model_object) {
883
-            if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) {
884
-                //ensure its in the map if it has an ID; otherwise it will be added to the map when its saved
885
-                if ($model_object->ID()) {
886
-                    $model->add_to_entity_map($model_object);
887
-                }
888
-            } else {
889
-                throw new EE_Error(
890
-                    sprintf(
891
-                        __(
892
-                            'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object',
893
-                            'event_espresso'
894
-                        ),
895
-                        $relationName,
896
-                        gettype($model_object)
897
-                    )
898
-                );
899
-            }
900
-        }
901
-        return $objects;
902
-    }
903
-
904
-
905
-
906
-    /**
907
-     * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database
908
-     * matching the given query conditions.
909
-     *
910
-     * @param null  $field_to_order_by  What field is being used as the reference point.
911
-     * @param int   $limit              How many objects to return.
912
-     * @param array $query_params       Any additional conditions on the query.
913
-     * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
914
-     *                                  you can indicate just the columns you want returned
915
-     * @return array|EE_Base_Class[]
916
-     * @throws \EE_Error
917
-     */
918
-    public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null)
919
-    {
920
-        $model = $this->get_model();
921
-        $field = empty($field_to_order_by) && $model->has_primary_key_field()
922
-            ? $model->get_primary_key_field()->get_name()
923
-            : $field_to_order_by;
924
-        $current_value = ! empty($field) ? $this->get($field) : null;
925
-        if (empty($field) || empty($current_value)) {
926
-            return array();
927
-        }
928
-        return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select);
929
-    }
930
-
931
-
932
-
933
-    /**
934
-     * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database
935
-     * matching the given query conditions.
936
-     *
937
-     * @param null  $field_to_order_by  What field is being used as the reference point.
938
-     * @param int   $limit              How many objects to return.
939
-     * @param array $query_params       Any additional conditions on the query.
940
-     * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
941
-     *                                  you can indicate just the columns you want returned
942
-     * @return array|EE_Base_Class[]
943
-     * @throws \EE_Error
944
-     */
945
-    public function previous_x(
946
-        $field_to_order_by = null,
947
-        $limit = 1,
948
-        $query_params = array(),
949
-        $columns_to_select = null
950
-    ) {
951
-        $model = $this->get_model();
952
-        $field = empty($field_to_order_by) && $model->has_primary_key_field()
953
-            ? $model->get_primary_key_field()->get_name()
954
-            : $field_to_order_by;
955
-        $current_value = ! empty($field) ? $this->get($field) : null;
956
-        if (empty($field) || empty($current_value)) {
957
-            return array();
958
-        }
959
-        return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select);
960
-    }
961
-
962
-
963
-
964
-    /**
965
-     * Returns the next EE_Base_Class object in sequence from this object as found in the database
966
-     * matching the given query conditions.
967
-     *
968
-     * @param null  $field_to_order_by  What field is being used as the reference point.
969
-     * @param array $query_params       Any additional conditions on the query.
970
-     * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
971
-     *                                  you can indicate just the columns you want returned
972
-     * @return array|EE_Base_Class
973
-     * @throws \EE_Error
974
-     */
975
-    public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null)
976
-    {
977
-        $model = $this->get_model();
978
-        $field = empty($field_to_order_by) && $model->has_primary_key_field()
979
-            ? $model->get_primary_key_field()->get_name()
980
-            : $field_to_order_by;
981
-        $current_value = ! empty($field) ? $this->get($field) : null;
982
-        if (empty($field) || empty($current_value)) {
983
-            return array();
984
-        }
985
-        return $model->next($current_value, $field, $query_params, $columns_to_select);
986
-    }
987
-
988
-
989
-
990
-    /**
991
-     * Returns the previous EE_Base_Class object in sequence from this object as found in the database
992
-     * matching the given query conditions.
993
-     *
994
-     * @param null  $field_to_order_by  What field is being used as the reference point.
995
-     * @param array $query_params       Any additional conditions on the query.
996
-     * @param null  $columns_to_select  If left null, then an EE_Base_Class object is returned, otherwise
997
-     *                                  you can indicate just the column you want returned
998
-     * @return array|EE_Base_Class
999
-     * @throws \EE_Error
1000
-     */
1001
-    public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null)
1002
-    {
1003
-        $model = $this->get_model();
1004
-        $field = empty($field_to_order_by) && $model->has_primary_key_field()
1005
-            ? $model->get_primary_key_field()->get_name()
1006
-            : $field_to_order_by;
1007
-        $current_value = ! empty($field) ? $this->get($field) : null;
1008
-        if (empty($field) || empty($current_value)) {
1009
-            return array();
1010
-        }
1011
-        return $model->previous($current_value, $field, $query_params, $columns_to_select);
1012
-    }
1013
-
1014
-
1015
-
1016
-    /**
1017
-     * Overrides parent because parent expects old models.
1018
-     * This also doesn't do any validation, and won't work for serialized arrays
1019
-     *
1020
-     * @param string $field_name
1021
-     * @param mixed  $field_value_from_db
1022
-     * @throws \EE_Error
1023
-     */
1024
-    public function set_from_db($field_name, $field_value_from_db)
1025
-    {
1026
-        $field_obj = $this->get_model()->field_settings_for($field_name);
1027
-        if ($field_obj instanceof EE_Model_Field_Base) {
1028
-            //you would think the DB has no NULLs for non-null label fields right? wrong!
1029
-            //eg, a CPT model object could have an entry in the posts table, but no
1030
-            //entry in the meta table. Meaning that all its columns in the meta table
1031
-            //are null! yikes! so when we find one like that, use defaults for its meta columns
1032
-            if ($field_value_from_db === null) {
1033
-                if ($field_obj->is_nullable()) {
1034
-                    //if the field allows nulls, then let it be null
1035
-                    $field_value = null;
1036
-                } else {
1037
-                    $field_value = $field_obj->get_default_value();
1038
-                }
1039
-            } else {
1040
-                $field_value = $field_obj->prepare_for_set_from_db($field_value_from_db);
1041
-            }
1042
-            $this->_fields[$field_name] = $field_value;
1043
-            $this->_clear_cached_property($field_name);
1044
-        }
1045
-    }
1046
-
1047
-
1048
-
1049
-    /**
1050
-     * verifies that the specified field is of the correct type
1051
-     *
1052
-     * @param string $field_name
1053
-     * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1054
-     *                                (in cases where the same property may be used for different outputs
1055
-     *                                - i.e. datetime, money etc.)
1056
-     * @return mixed
1057
-     * @throws \EE_Error
1058
-     */
1059
-    public function get($field_name, $extra_cache_ref = null)
1060
-    {
1061
-        return $this->_get_cached_property($field_name, false, $extra_cache_ref);
1062
-    }
1063
-
1064
-
1065
-
1066
-    /**
1067
-     * This method simply returns the RAW unprocessed value for the given property in this class
1068
-     *
1069
-     * @param  string $field_name A valid fieldname
1070
-     * @return mixed              Whatever the raw value stored on the property is.
1071
-     * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist.
1072
-     */
1073
-    public function get_raw($field_name)
1074
-    {
1075
-        $field_settings = $this->get_model()->field_settings_for($field_name);
1076
-        return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime
1077
-            ? $this->_fields[$field_name]->format('U')
1078
-            : $this->_fields[$field_name];
1079
-    }
1080
-
1081
-
1082
-
1083
-    /**
1084
-     * This is used to return the internal DateTime object used for a field that is a
1085
-     * EE_Datetime_Field.
1086
-     *
1087
-     * @param string $field_name               The field name retrieving the DateTime object.
1088
-     * @return mixed null | false | DateTime  If the requested field is NOT a EE_Datetime_Field then
1089
-     * @throws \EE_Error
1090
-     *                                         an error is set and false returned.  If the field IS an
1091
-     *                                         EE_Datetime_Field and but the field value is null, then
1092
-     *                                         just null is returned (because that indicates that likely
1093
-     *                                         this field is nullable).
1094
-     */
1095
-    public function get_DateTime_object($field_name)
1096
-    {
1097
-        $field_settings = $this->get_model()->field_settings_for($field_name);
1098
-        if ( ! $field_settings instanceof EE_Datetime_Field) {
1099
-            EE_Error::add_error(
1100
-                sprintf(
1101
-                    __(
1102
-                        'The field %s is not an EE_Datetime_Field field.  There is no DateTime object stored on this field type.',
1103
-                        'event_espresso'
1104
-                    ),
1105
-                    $field_name
1106
-                ),
1107
-                __FILE__,
1108
-                __FUNCTION__,
1109
-                __LINE__
1110
-            );
1111
-            return false;
1112
-        }
1113
-        return $this->_fields[$field_name];
1114
-    }
1115
-
1116
-
1117
-
1118
-    /**
1119
-     * To be used in template to immediately echo out the value, and format it for output.
1120
-     * Eg, should call stripslashes and whatnot before echoing
1121
-     *
1122
-     * @param string $field_name      the name of the field as it appears in the DB
1123
-     * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1124
-     *                                (in cases where the same property may be used for different outputs
1125
-     *                                - i.e. datetime, money etc.)
1126
-     * @return void
1127
-     * @throws \EE_Error
1128
-     */
1129
-    public function e($field_name, $extra_cache_ref = null)
1130
-    {
1131
-        echo $this->get_pretty($field_name, $extra_cache_ref);
1132
-    }
1133
-
1134
-
1135
-
1136
-    /**
1137
-     * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it
1138
-     * can be easily used as the value of form input.
1139
-     *
1140
-     * @param string $field_name
1141
-     * @return void
1142
-     * @throws \EE_Error
1143
-     */
1144
-    public function f($field_name)
1145
-    {
1146
-        $this->e($field_name, 'form_input');
1147
-    }
1148
-
1149
-
1150
-
1151
-    /**
1152
-     * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this.
1153
-     * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class
1154
-     * to see what options are available.
1155
-     * @param string $field_name
1156
-     * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1157
-     *                                (in cases where the same property may be used for different outputs
1158
-     *                                - i.e. datetime, money etc.)
1159
-     * @return mixed
1160
-     * @throws \EE_Error
1161
-     */
1162
-    public function get_pretty($field_name, $extra_cache_ref = null)
1163
-    {
1164
-        return $this->_get_cached_property($field_name, true, $extra_cache_ref);
1165
-    }
1166
-
1167
-
1168
-
1169
-    /**
1170
-     * This simply returns the datetime for the given field name
1171
-     * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions
1172
-     * (and the equivalent e_date, e_time, e_datetime).
1173
-     *
1174
-     * @access   protected
1175
-     * @param string   $field_name   Field on the instantiated EE_Base_Class child object
1176
-     * @param string   $dt_frmt      valid datetime format used for date
1177
-     *                               (if '' then we just use the default on the field,
1178
-     *                               if NULL we use the last-used format)
1179
-     * @param string   $tm_frmt      Same as above except this is for time format
1180
-     * @param string   $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time.
1181
-     * @param  boolean $echo         Whether the dtt is echoing using pretty echoing or just returned using vanilla get
1182
-     * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown
1183
-     *                               if field is not a valid dtt field, or void if echoing
1184
-     * @throws \EE_Error
1185
-     */
1186
-    protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false)
1187
-    {
1188
-        // clear cached property
1189
-        $this->_clear_cached_property($field_name);
1190
-        //reset format properties because they are used in get()
1191
-        $this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt;
1192
-        $this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt;
1193
-        if ($echo) {
1194
-            $this->e($field_name, $date_or_time);
1195
-            return '';
1196
-        }
1197
-        return $this->get($field_name, $date_or_time);
1198
-    }
1199
-
1200
-
1201
-
1202
-    /**
1203
-     * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date
1204
-     * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1205
-     * other echoes the pretty value for dtt)
1206
-     *
1207
-     * @param  string $field_name name of model object datetime field holding the value
1208
-     * @param  string $format     format for the date returned (if NULL we use default in dt_frmt property)
1209
-     * @return string            datetime value formatted
1210
-     * @throws \EE_Error
1211
-     */
1212
-    public function get_date($field_name, $format = '')
1213
-    {
1214
-        return $this->_get_datetime($field_name, $format, null, 'D');
1215
-    }
1216
-
1217
-
1218
-
1219
-    /**
1220
-     * @param      $field_name
1221
-     * @param string $format
1222
-     * @throws \EE_Error
1223
-     */
1224
-    public function e_date($field_name, $format = '')
1225
-    {
1226
-        $this->_get_datetime($field_name, $format, null, 'D', true);
1227
-    }
1228
-
1229
-
1230
-
1231
-    /**
1232
-     * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time
1233
-     * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1234
-     * other echoes the pretty value for dtt)
1235
-     *
1236
-     * @param  string $field_name name of model object datetime field holding the value
1237
-     * @param  string $format     format for the time returned ( if NULL we use default in tm_frmt property)
1238
-     * @return string             datetime value formatted
1239
-     * @throws \EE_Error
1240
-     */
1241
-    public function get_time($field_name, $format = '')
1242
-    {
1243
-        return $this->_get_datetime($field_name, null, $format, 'T');
1244
-    }
1245
-
1246
-
1247
-
1248
-    /**
1249
-     * @param      $field_name
1250
-     * @param string $format
1251
-     * @throws \EE_Error
1252
-     */
1253
-    public function e_time($field_name, $format = '')
1254
-    {
1255
-        $this->_get_datetime($field_name, null, $format, 'T', true);
1256
-    }
1257
-
1258
-
1259
-
1260
-    /**
1261
-     * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND
1262
-     * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1263
-     * other echoes the pretty value for dtt)
1264
-     *
1265
-     * @param  string $field_name name of model object datetime field holding the value
1266
-     * @param  string $dt_frmt    format for the date returned (if NULL we use default in dt_frmt property)
1267
-     * @param  string $tm_frmt    format for the time returned (if NULL we use default in tm_frmt property)
1268
-     * @return string             datetime value formatted
1269
-     * @throws \EE_Error
1270
-     */
1271
-    public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '')
1272
-    {
1273
-        return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt);
1274
-    }
1275
-
1276
-
1277
-
1278
-    /**
1279
-     * @param string $field_name
1280
-     * @param string $dt_frmt
1281
-     * @param string $tm_frmt
1282
-     * @throws \EE_Error
1283
-     */
1284
-    public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '')
1285
-    {
1286
-        $this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true);
1287
-    }
1288
-
1289
-
1290
-
1291
-    /**
1292
-     * Get the i8ln value for a date using the WordPress @see date_i18n function.
1293
-     *
1294
-     * @param string $field_name The EE_Datetime_Field reference for the date being retrieved.
1295
-     * @param string $format     PHP valid date/time string format.  If none is provided then the internal set format
1296
-     *                           on the object will be used.
1297
-     * @return string Date and time string in set locale or false if no field exists for the given
1298
-     * @throws \EE_Error
1299
-     *                           field name.
1300
-     */
1301
-    public function get_i18n_datetime($field_name, $format = '')
1302
-    {
1303
-        $format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format;
1304
-        return date_i18n(
1305
-            $format,
1306
-            EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone)
1307
-        );
1308
-    }
1309
-
1310
-
1311
-
1312
-    /**
1313
-     * This method validates whether the given field name is a valid field on the model object as well as it is of a
1314
-     * type EE_Datetime_Field.  On success there will be returned the field settings.  On fail an EE_Error exception is
1315
-     * thrown.
1316
-     *
1317
-     * @param  string $field_name The field name being checked
1318
-     * @throws EE_Error
1319
-     * @return EE_Datetime_Field
1320
-     */
1321
-    protected function _get_dtt_field_settings($field_name)
1322
-    {
1323
-        $field = $this->get_model()->field_settings_for($field_name);
1324
-        //check if field is dtt
1325
-        if ($field instanceof EE_Datetime_Field) {
1326
-            return $field;
1327
-        } else {
1328
-            throw new EE_Error(sprintf(__('The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field.  Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor',
1329
-                'event_espresso'), $field_name, self::_get_model_classname(get_class($this))));
1330
-        }
1331
-    }
1332
-
1333
-
1334
-
1335
-
1336
-    /**
1337
-     * NOTE ABOUT BELOW:
1338
-     * These convenience date and time setters are for setting date and time independently.  In other words you might
1339
-     * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand
1340
-     * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value)
1341
-     * method and make sure you send the entire datetime value for setting.
1342
-     */
1343
-    /**
1344
-     * sets the time on a datetime property
1345
-     *
1346
-     * @access protected
1347
-     * @param string|Datetime $time      a valid time string for php datetime functions (or DateTime object)
1348
-     * @param string          $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field)
1349
-     * @throws \EE_Error
1350
-     */
1351
-    protected function _set_time_for($time, $fieldname)
1352
-    {
1353
-        $this->_set_date_time('T', $time, $fieldname);
1354
-    }
1355
-
1356
-
1357
-
1358
-    /**
1359
-     * sets the date on a datetime property
1360
-     *
1361
-     * @access protected
1362
-     * @param string|DateTime $date      a valid date string for php datetime functions ( or DateTime object)
1363
-     * @param string          $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field)
1364
-     * @throws \EE_Error
1365
-     */
1366
-    protected function _set_date_for($date, $fieldname)
1367
-    {
1368
-        $this->_set_date_time('D', $date, $fieldname);
1369
-    }
1370
-
1371
-
1372
-
1373
-    /**
1374
-     * This takes care of setting a date or time independently on a given model object property. This method also
1375
-     * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field
1376
-     *
1377
-     * @access protected
1378
-     * @param string          $what           "T" for time, 'B' for both, 'D' for Date.
1379
-     * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object)
1380
-     * @param string          $fieldname      the name of the field the date OR time is being set on (must match a
1381
-     *                                        EE_Datetime_Field property)
1382
-     * @throws \EE_Error
1383
-     */
1384
-    protected function _set_date_time($what = 'T', $datetime_value, $fieldname)
1385
-    {
1386
-        $field = $this->_get_dtt_field_settings($fieldname);
1387
-        $field->set_timezone($this->_timezone);
1388
-        $field->set_date_format($this->_dt_frmt);
1389
-        $field->set_time_format($this->_tm_frmt);
1390
-        switch ($what) {
1391
-            case 'T' :
1392
-                $this->_fields[$fieldname] = $field->prepare_for_set_with_new_time(
1393
-                    $datetime_value,
1394
-                    $this->_fields[$fieldname]
1395
-                );
1396
-                break;
1397
-            case 'D' :
1398
-                $this->_fields[$fieldname] = $field->prepare_for_set_with_new_date(
1399
-                    $datetime_value,
1400
-                    $this->_fields[$fieldname]
1401
-                );
1402
-                break;
1403
-            case 'B' :
1404
-                $this->_fields[$fieldname] = $field->prepare_for_set($datetime_value);
1405
-                break;
1406
-        }
1407
-        $this->_clear_cached_property($fieldname);
1408
-    }
1409
-
1410
-
1411
-
1412
-    /**
1413
-     * This will return a timestamp for the website timezone but ONLY when the current website timezone is different
1414
-     * than the timezone set for the website. NOTE, this currently only works well with methods that return values.  If
1415
-     * you use it with methods that echo values the $_timestamp property may not get reset to its original value and
1416
-     * that could lead to some unexpected results!
1417
-     *
1418
-     * @access public
1419
-     * @param string               $field_name This is the name of the field on the object that contains the date/time
1420
-     *                                         value being returned.
1421
-     * @param string               $callback   must match a valid method in this class (defaults to get_datetime)
1422
-     * @param mixed (array|string) $args       This is the arguments that will be passed to the callback.
1423
-     * @param string               $prepend    You can include something to prepend on the timestamp
1424
-     * @param string               $append     You can include something to append on the timestamp
1425
-     * @throws EE_Error
1426
-     * @return string timestamp
1427
-     */
1428
-    public function display_in_my_timezone(
1429
-        $field_name,
1430
-        $callback = 'get_datetime',
1431
-        $args = null,
1432
-        $prepend = '',
1433
-        $append = ''
1434
-    ) {
1435
-        $timezone = EEH_DTT_Helper::get_timezone();
1436
-        if ($timezone === $this->_timezone) {
1437
-            return '';
1438
-        }
1439
-        $original_timezone = $this->_timezone;
1440
-        $this->set_timezone($timezone);
1441
-        $fn = (array)$field_name;
1442
-        $args = array_merge($fn, (array)$args);
1443
-        if ( ! method_exists($this, $callback)) {
1444
-            throw new EE_Error(
1445
-                sprintf(
1446
-                    __(
1447
-                        'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist.  Please check your spelling',
1448
-                        'event_espresso'
1449
-                    ),
1450
-                    $callback
1451
-                )
1452
-            );
1453
-        }
1454
-        $args = (array)$args;
1455
-        $return = $prepend . call_user_func_array(array($this, $callback), $args) . $append;
1456
-        $this->set_timezone($original_timezone);
1457
-        return $return;
1458
-    }
1459
-
1460
-
1461
-
1462
-    /**
1463
-     * Deletes this model object.
1464
-     * This calls the `EE_Base_Class::_delete` method.  Child classes wishing to change default behaviour should
1465
-     * override
1466
-     * `EE_Base_Class::_delete` NOT this class.
1467
-     *
1468
-     * @return boolean | int
1469
-     * @throws \EE_Error
1470
-     */
1471
-    public function delete()
1472
-    {
1473
-        /**
1474
-         * Called just before the `EE_Base_Class::_delete` method call.
1475
-         * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions
1476
-         * should be aware that `_delete` may not always result in a permanent delete.  For example, `EE_Soft_Delete_Base_Class::_delete`
1477
-         * soft deletes (trash) the object and does not permanently delete it.
1478
-         *
1479
-         * @param EE_Base_Class $model_object about to be 'deleted'
1480
-         */
1481
-        do_action('AHEE__EE_Base_Class__delete__before', $this);
1482
-        $result = $this->_delete();
1483
-        /**
1484
-         * Called just after the `EE_Base_Class::_delete` method call.
1485
-         * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions
1486
-         * should be aware that `_delete` may not always result in a permanent delete.  For example `EE_Soft_Base_Class::_delete`
1487
-         * soft deletes (trash) the object and does not permanently delete it.
1488
-         *
1489
-         * @param EE_Base_Class $model_object that was just 'deleted'
1490
-         * @param boolean       $result
1491
-         */
1492
-        do_action('AHEE__EE_Base_Class__delete__end', $this, $result);
1493
-        return $result;
1494
-    }
1495
-
1496
-
1497
-
1498
-    /**
1499
-     * Calls the specific delete method for the instantiated class.
1500
-     * This method is called by the public `EE_Base_Class::delete` method.  Any child classes desiring to override
1501
-     * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT
1502
-     * `EE_Base_Class::delete`
1503
-     *
1504
-     * @return bool|int
1505
-     * @throws \EE_Error
1506
-     */
1507
-    protected function _delete()
1508
-    {
1509
-        return $this->delete_permanently();
1510
-    }
1511
-
1512
-
1513
-
1514
-    /**
1515
-     * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an
1516
-     * error)
1517
-     *
1518
-     * @return bool | int
1519
-     * @throws \EE_Error
1520
-     */
1521
-    public function delete_permanently()
1522
-    {
1523
-        /**
1524
-         * Called just before HARD deleting a model object
1525
-         *
1526
-         * @param EE_Base_Class $model_object about to be 'deleted'
1527
-         */
1528
-        do_action('AHEE__EE_Base_Class__delete_permanently__before', $this);
1529
-        $model = $this->get_model();
1530
-        $result = $model->delete_permanently_by_ID($this->ID());
1531
-        $this->refresh_cache_of_related_objects();
1532
-        /**
1533
-         * Called just after HARD deleting a model object
1534
-         *
1535
-         * @param EE_Base_Class $model_object that was just 'deleted'
1536
-         * @param boolean       $result
1537
-         */
1538
-        do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result);
1539
-        return $result;
1540
-    }
1541
-
1542
-
1543
-
1544
-    /**
1545
-     * When this model object is deleted, it may still be cached on related model objects. This clears the cache of
1546
-     * related model objects
1547
-     *
1548
-     * @throws \EE_Error
1549
-     */
1550
-    public function refresh_cache_of_related_objects()
1551
-    {
1552
-        $model = $this->get_model();
1553
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1554
-            if ( ! empty($this->_model_relations[$relation_name])) {
1555
-                $related_objects = $this->_model_relations[$relation_name];
1556
-                if ($relation_obj instanceof EE_Belongs_To_Relation) {
1557
-                    //this relation only stores a single model object, not an array
1558
-                    //but let's make it consistent
1559
-                    $related_objects = array($related_objects);
1560
-                }
1561
-                foreach ($related_objects as $related_object) {
1562
-                    //only refresh their cache if they're in memory
1563
-                    if ($related_object instanceof EE_Base_Class) {
1564
-                        $related_object->clear_cache($model->get_this_model_name(), $this);
1565
-                    }
1566
-                }
1567
-            }
1568
-        }
1569
-    }
1570
-
1571
-
1572
-
1573
-    /**
1574
-     *        Saves this object to the database. An array may be supplied to set some values on this
1575
-     * object just before saving.
1576
-     *
1577
-     * @access public
1578
-     * @param array $set_cols_n_values keys are field names, values are their new values,
1579
-     *                                 if provided during the save() method (often client code will change the fields'
1580
-     *                                 values before calling save)
1581
-     * @throws \EE_Error
1582
-     * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object
1583
-     *                                 isn't allowed to persist (as determined by EE_Base_Class::allow_persist())
1584
-     */
1585
-    public function save($set_cols_n_values = array())
1586
-    {
1587
-        $model = $this->get_model();
1588
-        /**
1589
-         * Filters the fields we're about to save on the model object
1590
-         *
1591
-         * @param array         $set_cols_n_values
1592
-         * @param EE_Base_Class $model_object
1593
-         */
1594
-        $set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values,
1595
-            $this);
1596
-        //set attributes as provided in $set_cols_n_values
1597
-        foreach ($set_cols_n_values as $column => $value) {
1598
-            $this->set($column, $value);
1599
-        }
1600
-        // no changes ? then don't do anything
1601
-        if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) {
1602
-            return 0;
1603
-        }
1604
-        /**
1605
-         * Saving a model object.
1606
-         * Before we perform a save, this action is fired.
1607
-         *
1608
-         * @param EE_Base_Class $model_object the model object about to be saved.
1609
-         */
1610
-        do_action('AHEE__EE_Base_Class__save__begin', $this);
1611
-        if ( ! $this->allow_persist()) {
1612
-            return 0;
1613
-        }
1614
-        //now get current attribute values
1615
-        $save_cols_n_values = $this->_fields;
1616
-        //if the object already has an ID, update it. Otherwise, insert it
1617
-        //also: change the assumption about values passed to the model NOT being prepare dby the model object. They have been
1618
-        $old_assumption_concerning_value_preparation = $model
1619
-                                                            ->get_assumption_concerning_values_already_prepared_by_model_object();
1620
-        $model->assume_values_already_prepared_by_model_object(true);
1621
-        //does this model have an autoincrement PK?
1622
-        if ($model->has_primary_key_field()) {
1623
-            if ($model->get_primary_key_field()->is_auto_increment()) {
1624
-                //ok check if it's set, if so: update; if not, insert
1625
-                if ( ! empty($save_cols_n_values[$model->primary_key_name()])) {
1626
-                    $results = $model->update_by_ID($save_cols_n_values, $this->ID());
1627
-                } else {
1628
-                    unset($save_cols_n_values[$model->primary_key_name()]);
1629
-                    $results = $model->insert($save_cols_n_values);
1630
-                    if ($results) {
1631
-                        //if successful, set the primary key
1632
-                        //but don't use the normal SET method, because it will check if
1633
-                        //an item with the same ID exists in the mapper & db, then
1634
-                        //will find it in the db (because we just added it) and THAT object
1635
-                        //will get added to the mapper before we can add this one!
1636
-                        //but if we just avoid using the SET method, all that headache can be avoided
1637
-                        $pk_field_name = $model->primary_key_name();
1638
-                        $this->_fields[$pk_field_name] = $results;
1639
-                        $this->_clear_cached_property($pk_field_name);
1640
-                        $model->add_to_entity_map($this);
1641
-                        $this->_update_cached_related_model_objs_fks();
1642
-                    }
1643
-                }
1644
-            } else {//PK is NOT auto-increment
1645
-                //so check if one like it already exists in the db
1646
-                if ($model->exists_by_ID($this->ID())) {
1647
-                    if (WP_DEBUG && ! $this->in_entity_map()) {
1648
-                        throw new EE_Error(
1649
-                            sprintf(
1650
-                                __('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s',
1651
-                                    'event_espresso'),
1652
-                                get_class($this),
1653
-                                get_class($model) . '::instance()->add_to_entity_map()',
1654
-                                get_class($model) . '::instance()->get_one_by_ID()',
1655
-                                '<br />'
1656
-                            )
1657
-                        );
1658
-                    }
1659
-                    $results = $model->update_by_ID($save_cols_n_values, $this->ID());
1660
-                } else {
1661
-                    $results = $model->insert($save_cols_n_values);
1662
-                    $this->_update_cached_related_model_objs_fks();
1663
-                }
1664
-            }
1665
-        } else {//there is NO primary key
1666
-            $already_in_db = false;
1667
-            foreach ($model->unique_indexes() as $index) {
1668
-                $uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields());
1669
-                if ($model->exists(array($uniqueness_where_params))) {
1670
-                    $already_in_db = true;
1671
-                }
1672
-            }
1673
-            if ($already_in_db) {
1674
-                $combined_pk_fields_n_values = array_intersect_key($save_cols_n_values,
1675
-                    $model->get_combined_primary_key_fields());
1676
-                $results = $model->update($save_cols_n_values, $combined_pk_fields_n_values);
1677
-            } else {
1678
-                $results = $model->insert($save_cols_n_values);
1679
-            }
1680
-        }
1681
-        //restore the old assumption about values being prepared by the model object
1682
-        $model
1683
-             ->assume_values_already_prepared_by_model_object($old_assumption_concerning_value_preparation);
1684
-        /**
1685
-         * After saving the model object this action is called
1686
-         *
1687
-         * @param EE_Base_Class $model_object which was just saved
1688
-         * @param boolean|int   $results      if it were updated, TRUE or FALSE; if it were newly inserted
1689
-         *                                    the new ID (or 0 if an error occurred and it wasn't updated)
1690
-         */
1691
-        do_action('AHEE__EE_Base_Class__save__end', $this, $results);
1692
-        $this->_has_changes = false;
1693
-        return $results;
1694
-    }
1695
-
1696
-
1697
-
1698
-    /**
1699
-     * Updates the foreign key on related models objects pointing to this to have this model object's ID
1700
-     * as their foreign key.  If the cached related model objects already exist in the db, saves them (so that the DB
1701
-     * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its
1702
-     * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't
1703
-     * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the
1704
-     * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether
1705
-     * or not they exist in the DB (if they do, their DB records will be automatically updated)
1706
-     *
1707
-     * @return void
1708
-     * @throws \EE_Error
1709
-     */
1710
-    protected function _update_cached_related_model_objs_fks()
1711
-    {
1712
-        $model = $this->get_model();
1713
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1714
-            if ($relation_obj instanceof EE_Has_Many_Relation) {
1715
-                foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) {
1716
-                    $fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to(
1717
-                        $model->get_this_model_name()
1718
-                    );
1719
-                    $related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID());
1720
-                    if ($related_model_obj_in_cache->ID()) {
1721
-                        $related_model_obj_in_cache->save();
1722
-                    }
1723
-                }
1724
-            }
1725
-        }
1726
-    }
1727
-
1728
-
1729
-
1730
-    /**
1731
-     * Saves this model object and its NEW cached relations to the database.
1732
-     * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB.
1733
-     * In order for that to work, we would need to mark model objects as dirty/clean...
1734
-     * because otherwise, there's a potential for infinite looping of saving
1735
-     * Saves the cached related model objects, and ensures the relation between them
1736
-     * and this object and properly setup
1737
-     *
1738
-     * @return int ID of new model object on save; 0 on failure+
1739
-     * @throws \EE_Error
1740
-     */
1741
-    public function save_new_cached_related_model_objs()
1742
-    {
1743
-        //make sure this has been saved
1744
-        if ( ! $this->ID()) {
1745
-            $id = $this->save();
1746
-        } else {
1747
-            $id = $this->ID();
1748
-        }
1749
-        //now save all the NEW cached model objects  (ie they don't exist in the DB)
1750
-        foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) {
1751
-            if ($this->_model_relations[$relationName]) {
1752
-                //is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation)
1753
-                //or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)?
1754
-                if ($relationObj instanceof EE_Belongs_To_Relation) {
1755
-                    //add a relation to that relation type (which saves the appropriate thing in the process)
1756
-                    //but ONLY if it DOES NOT exist in the DB
1757
-                    /* @var $related_model_obj EE_Base_Class */
1758
-                    $related_model_obj = $this->_model_relations[$relationName];
1759
-                    //					if( ! $related_model_obj->ID()){
1760
-                    $this->_add_relation_to($related_model_obj, $relationName);
1761
-                    $related_model_obj->save_new_cached_related_model_objs();
1762
-                    //					}
1763
-                } else {
1764
-                    foreach ($this->_model_relations[$relationName] as $related_model_obj) {
1765
-                        //add a relation to that relation type (which saves the appropriate thing in the process)
1766
-                        //but ONLY if it DOES NOT exist in the DB
1767
-                        //						if( ! $related_model_obj->ID()){
1768
-                        $this->_add_relation_to($related_model_obj, $relationName);
1769
-                        $related_model_obj->save_new_cached_related_model_objs();
1770
-                        //						}
1771
-                    }
1772
-                }
1773
-            }
1774
-        }
1775
-        return $id;
1776
-    }
1777
-
1778
-
1779
-
1780
-    /**
1781
-     * for getting a model while instantiated.
1782
-     *
1783
-     * @return \EEM_Base | \EEM_CPT_Base
1784
-     */
1785
-    public function get_model()
1786
-    {
1787
-        if( ! $this->_model){
1788
-            $modelName = self::_get_model_classname(get_class($this));
1789
-            $this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone);
1790
-        } else {
1791
-            $this->_model->set_timezone($this->_timezone);
1792
-        }
1793
-
1794
-        return $this->_model;
1795
-    }
1796
-
1797
-
1798
-
1799
-    /**
1800
-     * @param $props_n_values
1801
-     * @param $classname
1802
-     * @return mixed bool|EE_Base_Class|EEM_CPT_Base
1803
-     * @throws \EE_Error
1804
-     */
1805
-    protected static function _get_object_from_entity_mapper($props_n_values, $classname)
1806
-    {
1807
-        //TODO: will not work for Term_Relationships because they have no PK!
1808
-        $primary_id_ref = self::_get_primary_key_name($classname);
1809
-        if (array_key_exists($primary_id_ref, $props_n_values) && ! empty($props_n_values[$primary_id_ref])) {
1810
-            $id = $props_n_values[$primary_id_ref];
1811
-            return self::_get_model($classname)->get_from_entity_map($id);
1812
-        }
1813
-        return false;
1814
-    }
1815
-
1816
-
1817
-
1818
-    /**
1819
-     * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for
1820
-     * the primary key (if present in incoming values). If there is a key in the incoming array that matches the
1821
-     * primary key for the model AND it is not null, then we check the db. If there's a an object we return it.  If not
1822
-     * we return false.
1823
-     *
1824
-     * @param  array  $props_n_values   incoming array of properties and their values
1825
-     * @param  string $classname        the classname of the child class
1826
-     * @param null    $timezone
1827
-     * @param array   $date_formats     incoming date_formats in an array where the first value is the
1828
-     *                                  date_format and the second value is the time format
1829
-     * @return mixed (EE_Base_Class|bool)
1830
-     * @throws \EE_Error
1831
-     */
1832
-    protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array())
1833
-    {
1834
-        $existing = null;
1835
-        $model = self::_get_model($classname, $timezone);
1836
-        if ($model->has_primary_key_field()) {
1837
-            $primary_id_ref = self::_get_primary_key_name($classname);
1838
-            if (array_key_exists($primary_id_ref, $props_n_values)
1839
-                && ! empty($props_n_values[$primary_id_ref])
1840
-            ) {
1841
-                $existing = $model->get_one_by_ID(
1842
-                    $props_n_values[$primary_id_ref]
1843
-                );
1844
-            }
1845
-        } elseif ($model->has_all_combined_primary_key_fields($props_n_values)) {
1846
-            //no primary key on this model, but there's still a matching item in the DB
1847
-            $existing = self::_get_model($classname, $timezone)->get_one_by_ID(
1848
-                self::_get_model($classname, $timezone)->get_index_primary_key_string($props_n_values)
1849
-            );
1850
-        }
1851
-        if ($existing) {
1852
-            //set date formats if present before setting values
1853
-            if ( ! empty($date_formats) && is_array($date_formats)) {
1854
-                $existing->set_date_format($date_formats[0]);
1855
-                $existing->set_time_format($date_formats[1]);
1856
-            } else {
1857
-                //set default formats for date and time
1858
-                $existing->set_date_format(get_option('date_format'));
1859
-                $existing->set_time_format(get_option('time_format'));
1860
-            }
1861
-            foreach ($props_n_values as $property => $field_value) {
1862
-                $existing->set($property, $field_value);
1863
-            }
1864
-            return $existing;
1865
-        } else {
1866
-            return false;
1867
-        }
1868
-    }
1869
-
1870
-
1871
-
1872
-    /**
1873
-     * Gets the EEM_*_Model for this class
1874
-     *
1875
-     * @access public now, as this is more convenient
1876
-     * @param      $classname
1877
-     * @param null $timezone
1878
-     * @throws EE_Error
1879
-     * @return EEM_Base
1880
-     */
1881
-    protected static function _get_model($classname, $timezone = null)
1882
-    {
1883
-        //find model for this class
1884
-        if ( ! $classname) {
1885
-            throw new EE_Error(
1886
-                sprintf(
1887
-                    __(
1888
-                        "What were you thinking calling _get_model(%s)?? You need to specify the class name",
1889
-                        "event_espresso"
1890
-                    ),
1891
-                    $classname
1892
-                )
1893
-            );
1894
-        }
1895
-        $modelName = self::_get_model_classname($classname);
1896
-        return self::_get_model_instance_with_name($modelName, $timezone);
1897
-    }
1898
-
1899
-
1900
-
1901
-    /**
1902
-     * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee)
1903
-     *
1904
-     * @param string $model_classname
1905
-     * @param null   $timezone
1906
-     * @return EEM_Base
1907
-     */
1908
-    protected static function _get_model_instance_with_name($model_classname, $timezone = null)
1909
-    {
1910
-        $model_classname = str_replace('EEM_', '', $model_classname);
1911
-        $model = EE_Registry::instance()->load_model($model_classname);
1912
-        $model->set_timezone($timezone);
1913
-        return $model;
1914
-    }
1915
-
1916
-
1917
-
1918
-    /**
1919
-     * If a model name is provided (eg Registration), gets the model classname for that model.
1920
-     * Also works if a model class's classname is provided (eg EE_Registration).
1921
-     *
1922
-     * @param null $model_name
1923
-     * @return string like EEM_Attendee
1924
-     */
1925
-    private static function _get_model_classname($model_name = null)
1926
-    {
1927
-        if (strpos($model_name, "EE_") === 0) {
1928
-            $model_classname = str_replace("EE_", "EEM_", $model_name);
1929
-        } else {
1930
-            $model_classname = "EEM_" . $model_name;
1931
-        }
1932
-        return $model_classname;
1933
-    }
1934
-
1935
-
1936
-
1937
-    /**
1938
-     * returns the name of the primary key attribute
1939
-     *
1940
-     * @param null $classname
1941
-     * @throws EE_Error
1942
-     * @return string
1943
-     */
1944
-    protected static function _get_primary_key_name($classname = null)
1945
-    {
1946
-        if ( ! $classname) {
1947
-            throw new EE_Error(
1948
-                sprintf(
1949
-                    __("What were you thinking calling _get_primary_key_name(%s)", "event_espresso"),
1950
-                    $classname
1951
-                )
1952
-            );
1953
-        }
1954
-        return self::_get_model($classname)->get_primary_key_field()->get_name();
1955
-    }
1956
-
1957
-
1958
-
1959
-    /**
1960
-     * Gets the value of the primary key.
1961
-     * If the object hasn't yet been saved, it should be whatever the model field's default was
1962
-     * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value
1963
-     * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL).
1964
-     *
1965
-     * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string
1966
-     * @throws \EE_Error
1967
-     */
1968
-    public function ID()
1969
-    {
1970
-        $model = $this->get_model();
1971
-        //now that we know the name of the variable, use a variable variable to get its value and return its
1972
-        if ($model->has_primary_key_field()) {
1973
-            return $this->_fields[$model->primary_key_name()];
1974
-        } else {
1975
-            return $model->get_index_primary_key_string($this->_fields);
1976
-        }
1977
-    }
1978
-
1979
-
1980
-
1981
-    /**
1982
-     * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current
1983
-     * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE
1984
-     * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing
1985
-     *
1986
-     * @param mixed  $otherObjectModelObjectOrID       EE_Base_Class or the ID of the other object
1987
-     * @param string $relationName                     eg 'Events','Question',etc.
1988
-     *                                                 an attendee to a group, you also want to specify which role they
1989
-     *                                                 will have in that group. So you would use this parameter to
1990
-     *                                                 specify array('role-column-name'=>'role-id')
1991
-     * @param array  $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that
1992
-     *                                                 allow you to further constrict the relation to being added.
1993
-     *                                                 However, keep in mind that the columns (keys) given must match a
1994
-     *                                                 column on the JOIN table and currently only the HABTM models
1995
-     *                                                 accept these additional conditions.  Also remember that if an
1996
-     *                                                 exact match isn't found for these extra cols/val pairs, then a
1997
-     *                                                 NEW row is created in the join table.
1998
-     * @param null   $cache_id
1999
-     * @throws EE_Error
2000
-     * @return EE_Base_Class the object the relation was added to
2001
-     */
2002
-    public function _add_relation_to(
2003
-        $otherObjectModelObjectOrID,
2004
-        $relationName,
2005
-        $extra_join_model_fields_n_values = array(),
2006
-        $cache_id = null
2007
-    ) {
2008
-        $model = $this->get_model();
2009
-        //if this thing exists in the DB, save the relation to the DB
2010
-        if ($this->ID()) {
2011
-            $otherObject = $model
2012
-                                ->add_relationship_to($this, $otherObjectModelObjectOrID, $relationName,
2013
-                                    $extra_join_model_fields_n_values);
2014
-            //clear cache so future get_many_related and get_first_related() return new results.
2015
-            $this->clear_cache($relationName, $otherObject, true);
2016
-            if ($otherObject instanceof EE_Base_Class) {
2017
-                $otherObject->clear_cache($model->get_this_model_name(), $this);
2018
-            }
2019
-        } else {
2020
-            //this thing doesn't exist in the DB,  so just cache it
2021
-            if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) {
2022
-                throw new EE_Error(sprintf(
2023
-                    __('Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s',
2024
-                        'event_espresso'),
2025
-                    $otherObjectModelObjectOrID,
2026
-                    get_class($this)
2027
-                ));
2028
-            } else {
2029
-                $otherObject = $otherObjectModelObjectOrID;
2030
-            }
2031
-            $this->cache($relationName, $otherObjectModelObjectOrID, $cache_id);
2032
-        }
2033
-        if ($otherObject instanceof EE_Base_Class) {
2034
-            //fix the reciprocal relation too
2035
-            if ($otherObject->ID()) {
2036
-                //its saved so assumed relations exist in the DB, so we can just
2037
-                //clear the cache so future queries use the updated info in the DB
2038
-                $otherObject->clear_cache($model->get_this_model_name(), null, true);
2039
-            } else {
2040
-                //it's not saved, so it caches relations like this
2041
-                $otherObject->cache($model->get_this_model_name(), $this);
2042
-            }
2043
-        }
2044
-        return $otherObject;
2045
-    }
2046
-
2047
-
2048
-
2049
-    /**
2050
-     * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current
2051
-     * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE
2052
-     * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing
2053
-     * from the cache
2054
-     *
2055
-     * @param mixed  $otherObjectModelObjectOrID
2056
-     *                EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved
2057
-     *                to the DB yet
2058
-     * @param string $relationName
2059
-     * @param array  $where_query
2060
-     *                You can optionally include an array of key=>value pairs that allow you to further constrict the
2061
-     *                relation to being added. However, keep in mind that the columns (keys) given must match a column
2062
-     *                on the JOIN table and currently only the HABTM models accept these additional conditions. Also
2063
-     *                remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is
2064
-     *                created in the join table.
2065
-     * @return EE_Base_Class the relation was removed from
2066
-     * @throws \EE_Error
2067
-     */
2068
-    public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array())
2069
-    {
2070
-        if ($this->ID()) {
2071
-            //if this exists in the DB, save the relation change to the DB too
2072
-            $otherObject = $this->get_model()
2073
-                                ->remove_relationship_to($this, $otherObjectModelObjectOrID, $relationName,
2074
-                                    $where_query);
2075
-            $this->clear_cache($relationName, $otherObject);
2076
-        } else {
2077
-            //this doesn't exist in the DB, just remove it from the cache
2078
-            $otherObject = $this->clear_cache($relationName, $otherObjectModelObjectOrID);
2079
-        }
2080
-        if ($otherObject instanceof EE_Base_Class) {
2081
-            $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this);
2082
-        }
2083
-        return $otherObject;
2084
-    }
2085
-
2086
-
2087
-
2088
-    /**
2089
-     * Removes ALL the related things for the $relationName.
2090
-     *
2091
-     * @param string $relationName
2092
-     * @param array  $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions)
2093
-     * @return EE_Base_Class
2094
-     * @throws \EE_Error
2095
-     */
2096
-    public function _remove_relations($relationName, $where_query_params = array())
2097
-    {
2098
-        if ($this->ID()) {
2099
-            //if this exists in the DB, save the relation change to the DB too
2100
-            $otherObjects = $this->get_model()->remove_relations($this, $relationName, $where_query_params);
2101
-            $this->clear_cache($relationName, null, true);
2102
-        } else {
2103
-            //this doesn't exist in the DB, just remove it from the cache
2104
-            $otherObjects = $this->clear_cache($relationName, null, true);
2105
-        }
2106
-        if (is_array($otherObjects)) {
2107
-            foreach ($otherObjects as $otherObject) {
2108
-                $otherObject->clear_cache($this->get_model()->get_this_model_name(), $this);
2109
-            }
2110
-        }
2111
-        return $otherObjects;
2112
-    }
2113
-
2114
-
2115
-
2116
-    /**
2117
-     * Gets all the related model objects of the specified type. Eg, if the current class if
2118
-     * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the
2119
-     * EE_Registration objects which related to this event. Note: by default, we remove the "default query params"
2120
-     * because we want to get even deleted items etc.
2121
-     *
2122
-     * @param string $relationName key in the model's _model_relations array
2123
-     * @param array  $query_params like EEM_Base::get_all
2124
-     * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys
2125
-     * @throws \EE_Error
2126
-     *                             or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if
2127
-     *                             you want IDs
2128
-     */
2129
-    public function get_many_related($relationName, $query_params = array())
2130
-    {
2131
-        if ($this->ID()) {
2132
-            //this exists in the DB, so get the related things from either the cache or the DB
2133
-            //if there are query parameters, forget about caching the related model objects.
2134
-            if ($query_params) {
2135
-                $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params);
2136
-            } else {
2137
-                //did we already cache the result of this query?
2138
-                $cached_results = $this->get_all_from_cache($relationName);
2139
-                if ( ! $cached_results) {
2140
-                    $related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params);
2141
-                    //if no query parameters were passed, then we got all the related model objects
2142
-                    //for that relation. We can cache them then.
2143
-                    foreach ($related_model_objects as $related_model_object) {
2144
-                        $this->cache($relationName, $related_model_object);
2145
-                    }
2146
-                } else {
2147
-                    $related_model_objects = $cached_results;
2148
-                }
2149
-            }
2150
-        } else {
2151
-            //this doesn't exist in the DB, so just get the related things from the cache
2152
-            $related_model_objects = $this->get_all_from_cache($relationName);
2153
-        }
2154
-        return $related_model_objects;
2155
-    }
2156
-
2157
-
2158
-
2159
-    /**
2160
-     * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default,
2161
-     * unless otherwise specified in the $query_params
2162
-     *
2163
-     * @param string $relation_name  model_name like 'Event', or 'Registration'
2164
-     * @param array  $query_params   like EEM_Base::get_all's
2165
-     * @param string $field_to_count name of field to count by. By default, uses primary key
2166
-     * @param bool   $distinct       if we want to only count the distinct values for the column then you can trigger
2167
-     *                               that by the setting $distinct to TRUE;
2168
-     * @return int
2169
-     */
2170
-    public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false)
2171
-    {
2172
-        return $this->get_model()->count_related($this, $relation_name, $query_params, $field_to_count, $distinct);
2173
-    }
2174
-
2175
-
2176
-
2177
-    /**
2178
-     * Instead of getting the related model objects, simply sums up the values of the specified field.
2179
-     * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params
2180
-     *
2181
-     * @param string $relation_name model_name like 'Event', or 'Registration'
2182
-     * @param array  $query_params  like EEM_Base::get_all's
2183
-     * @param string $field_to_sum  name of field to count by.
2184
-     *                              By default, uses primary key (which doesn't make much sense, so you should probably
2185
-     *                              change it)
2186
-     * @return int
2187
-     */
2188
-    public function sum_related($relation_name, $query_params = array(), $field_to_sum = null)
2189
-    {
2190
-        return $this->get_model()->sum_related($this, $relation_name, $query_params, $field_to_sum);
2191
-    }
2192
-
2193
-
2194
-
2195
-    /**
2196
-     * Gets the first (ie, one) related model object of the specified type.
2197
-     *
2198
-     * @param string $relationName key in the model's _model_relations array
2199
-     * @param array  $query_params like EEM_Base::get_all
2200
-     * @return EE_Base_Class (not an array, a single object)
2201
-     * @throws \EE_Error
2202
-     */
2203
-    public function get_first_related($relationName, $query_params = array())
2204
-    {
2205
-        $model = $this->get_model();
2206
-        if ($this->ID()) {//this exists in the DB, get from the cache OR the DB
2207
-            //if they've provided some query parameters, don't bother trying to cache the result
2208
-            //also make sure we're not caching the result of get_first_related
2209
-            //on a relation which should have an array of objects (because the cache might have an array of objects)
2210
-            if ($query_params
2211
-                || ! $model->related_settings_for($relationName)
2212
-                     instanceof
2213
-                     EE_Belongs_To_Relation
2214
-            ) {
2215
-                $related_model_object = $model->get_first_related($this, $relationName, $query_params);
2216
-            } else {
2217
-                //first, check if we've already cached the result of this query
2218
-                $cached_result = $this->get_one_from_cache($relationName);
2219
-                if ( ! $cached_result) {
2220
-                    $related_model_object = $model->get_first_related($this, $relationName, $query_params);
2221
-                    $this->cache($relationName, $related_model_object);
2222
-                } else {
2223
-                    $related_model_object = $cached_result;
2224
-                }
2225
-            }
2226
-        } else {
2227
-            $related_model_object = null;
2228
-            //this doesn't exist in the Db, but maybe the relation is of type belongs to, and so the related thing might
2229
-            if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) {
2230
-                $related_model_object = $model->get_first_related($this, $relationName, $query_params);
2231
-            }
2232
-            //this doesn't exist in the DB and apparently the thing it belongs to doesn't either, just get what's cached on this object
2233
-            if ( ! $related_model_object) {
2234
-                $related_model_object = $this->get_one_from_cache($relationName);
2235
-            }
2236
-        }
2237
-        return $related_model_object;
2238
-    }
2239
-
2240
-
2241
-
2242
-    /**
2243
-     * Does a delete on all related objects of type $relationName and removes
2244
-     * the current model object's relation to them. If they can't be deleted (because
2245
-     * of blocking related model objects) does nothing. If the related model objects are
2246
-     * soft-deletable, they will be soft-deleted regardless of related blocking model objects.
2247
-     * If this model object doesn't exist yet in the DB, just removes its related things
2248
-     *
2249
-     * @param string $relationName
2250
-     * @param array  $query_params like EEM_Base::get_all's
2251
-     * @return int how many deleted
2252
-     * @throws \EE_Error
2253
-     */
2254
-    public function delete_related($relationName, $query_params = array())
2255
-    {
2256
-        if ($this->ID()) {
2257
-            $count = $this->get_model()->delete_related($this, $relationName, $query_params);
2258
-        } else {
2259
-            $count = count($this->get_all_from_cache($relationName));
2260
-            $this->clear_cache($relationName, null, true);
2261
-        }
2262
-        return $count;
2263
-    }
2264
-
2265
-
2266
-
2267
-    /**
2268
-     * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes
2269
-     * the current model object's relation to them. If they can't be deleted (because
2270
-     * of blocking related model objects) just does a soft delete on it instead, if possible.
2271
-     * If the related thing isn't a soft-deletable model object, this function is identical
2272
-     * to delete_related(). If this model object doesn't exist in the DB, just remove its related things
2273
-     *
2274
-     * @param string $relationName
2275
-     * @param array  $query_params like EEM_Base::get_all's
2276
-     * @return int how many deleted (including those soft deleted)
2277
-     * @throws \EE_Error
2278
-     */
2279
-    public function delete_related_permanently($relationName, $query_params = array())
2280
-    {
2281
-        if ($this->ID()) {
2282
-            $count = $this->get_model()->delete_related_permanently($this, $relationName, $query_params);
2283
-        } else {
2284
-            $count = count($this->get_all_from_cache($relationName));
2285
-        }
2286
-        $this->clear_cache($relationName, null, true);
2287
-        return $count;
2288
-    }
2289
-
2290
-
2291
-
2292
-    /**
2293
-     * is_set
2294
-     * Just a simple utility function children can use for checking if property exists
2295
-     *
2296
-     * @access  public
2297
-     * @param  string $field_name property to check
2298
-     * @return bool                              TRUE if existing,FALSE if not.
2299
-     */
2300
-    public function is_set($field_name)
2301
-    {
2302
-        return isset($this->_fields[$field_name]);
2303
-    }
2304
-
2305
-
2306
-
2307
-    /**
2308
-     * Just a simple utility function children can use for checking if property (or properties) exists and throwing an
2309
-     * EE_Error exception if they don't
2310
-     *
2311
-     * @param  mixed (string|array) $properties properties to check
2312
-     * @throws EE_Error
2313
-     * @return bool                              TRUE if existing, throw EE_Error if not.
2314
-     */
2315
-    protected function _property_exists($properties)
2316
-    {
2317
-        foreach ((array)$properties as $property_name) {
2318
-            //first make sure this property exists
2319
-            if ( ! $this->_fields[$property_name]) {
2320
-                throw new EE_Error(
2321
-                    sprintf(
2322
-                        __(
2323
-                            'Trying to retrieve a non-existent property (%s).  Double check the spelling please',
2324
-                            'event_espresso'
2325
-                        ),
2326
-                        $property_name
2327
-                    )
2328
-                );
2329
-            }
2330
-        }
2331
-        return true;
2332
-    }
2333
-
2334
-
2335
-
2336
-    /**
2337
-     * This simply returns an array of model fields for this object
2338
-     *
2339
-     * @return array
2340
-     * @throws \EE_Error
2341
-     */
2342
-    public function model_field_array()
2343
-    {
2344
-        $fields = $this->get_model()->field_settings(false);
2345
-        $properties = array();
2346
-        //remove prepended underscore
2347
-        foreach ($fields as $field_name => $settings) {
2348
-            $properties[$field_name] = $this->get($field_name);
2349
-        }
2350
-        return $properties;
2351
-    }
2352
-
2353
-
2354
-
2355
-    /**
2356
-     * Very handy general function to allow for plugins to extend any child of EE_Base_Class.
2357
-     * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called
2358
-     * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of
2359
-     * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that
2360
-     * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg,
2361
-     * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function
2362
-     * was called, and an array of the original arguments passed to the function. Whatever their callback function
2363
-     * returns will be returned by this function. Example: in functions.php (or in a plugin):
2364
-     * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function
2365
-     * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){
2366
-     * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray);
2367
-     *        return $previousReturnValue.$returnString;
2368
-     * }
2369
-     * require('EE_Answer.class.php');
2370
-     * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42'));
2371
-     * echo $answer->my_callback('monkeys',100);
2372
-     * //will output "you called my_callback! and passed args:monkeys,100"
2373
-     *
2374
-     * @param string $methodName name of method which was called on a child of EE_Base_Class, but which
2375
-     * @param array  $args       array of original arguments passed to the function
2376
-     * @throws EE_Error
2377
-     * @return mixed whatever the plugin which calls add_filter decides
2378
-     */
2379
-    public function __call($methodName, $args)
2380
-    {
2381
-        $className = get_class($this);
2382
-        $tagName = "FHEE__{$className}__{$methodName}";
2383
-        if ( ! has_filter($tagName)) {
2384
-            throw new EE_Error(
2385
-                sprintf(
2386
-                    __(
2387
-                        "Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}",
2388
-                        "event_espresso"
2389
-                    ),
2390
-                    $methodName,
2391
-                    $className,
2392
-                    $tagName
2393
-                )
2394
-            );
2395
-        }
2396
-        return apply_filters($tagName, null, $this, $args);
2397
-    }
2398
-
2399
-
2400
-
2401
-    /**
2402
-     * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value.
2403
-     * A $previous_value can be specified in case there are many meta rows with the same key
2404
-     *
2405
-     * @param string $meta_key
2406
-     * @param mixed  $meta_value
2407
-     * @param mixed  $previous_value
2408
-     * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row)
2409
-     * @throws \EE_Error
2410
-     * NOTE: if the values haven't changed, returns 0
2411
-     */
2412
-    public function update_extra_meta($meta_key, $meta_value, $previous_value = null)
2413
-    {
2414
-        $query_params = array(
2415
-            array(
2416
-                'EXM_key'  => $meta_key,
2417
-                'OBJ_ID'   => $this->ID(),
2418
-                'EXM_type' => $this->get_model()->get_this_model_name(),
2419
-            ),
2420
-        );
2421
-        if ($previous_value !== null) {
2422
-            $query_params[0]['EXM_value'] = $meta_value;
2423
-        }
2424
-        $existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params);
2425
-        if ( ! $existing_rows_like_that) {
2426
-            return $this->add_extra_meta($meta_key, $meta_value);
2427
-        }
2428
-        foreach ($existing_rows_like_that as $existing_row) {
2429
-            $existing_row->save(array('EXM_value' => $meta_value));
2430
-        }
2431
-        return count($existing_rows_like_that);
2432
-    }
2433
-
2434
-
2435
-
2436
-    /**
2437
-     * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check
2438
-     * no other extra meta for this model object have the same key. Returns TRUE if the
2439
-     * extra meta row was entered, false if not
2440
-     *
2441
-     * @param string  $meta_key
2442
-     * @param string  $meta_value
2443
-     * @param boolean $unique
2444
-     * @return boolean
2445
-     * @throws \EE_Error
2446
-     */
2447
-    public function add_extra_meta($meta_key, $meta_value, $unique = false)
2448
-    {
2449
-        if ($unique) {
2450
-            $existing_extra_meta = EEM_Extra_Meta::instance()->get_one(
2451
-                array(
2452
-                    array(
2453
-                        'EXM_key'  => $meta_key,
2454
-                        'OBJ_ID'   => $this->ID(),
2455
-                        'EXM_type' => $this->get_model()->get_this_model_name(),
2456
-                    ),
2457
-                )
2458
-            );
2459
-            if ($existing_extra_meta) {
2460
-                return false;
2461
-            }
2462
-        }
2463
-        $new_extra_meta = EE_Extra_Meta::new_instance(
2464
-            array(
2465
-                'EXM_key'   => $meta_key,
2466
-                'EXM_value' => $meta_value,
2467
-                'OBJ_ID'    => $this->ID(),
2468
-                'EXM_type'  => $this->get_model()->get_this_model_name(),
2469
-            )
2470
-        );
2471
-        $new_extra_meta->save();
2472
-        return true;
2473
-    }
2474
-
2475
-
2476
-
2477
-    /**
2478
-     * Deletes all the extra meta rows for this record as specified by key. If $meta_value
2479
-     * is specified, only deletes extra meta records with that value.
2480
-     *
2481
-     * @param string $meta_key
2482
-     * @param string $meta_value
2483
-     * @return int number of extra meta rows deleted
2484
-     * @throws \EE_Error
2485
-     */
2486
-    public function delete_extra_meta($meta_key, $meta_value = null)
2487
-    {
2488
-        $query_params = array(
2489
-            array(
2490
-                'EXM_key'  => $meta_key,
2491
-                'OBJ_ID'   => $this->ID(),
2492
-                'EXM_type' => $this->get_model()->get_this_model_name(),
2493
-            ),
2494
-        );
2495
-        if ($meta_value !== null) {
2496
-            $query_params[0]['EXM_value'] = $meta_value;
2497
-        }
2498
-        return EEM_Extra_Meta::instance()->delete($query_params);
2499
-    }
2500
-
2501
-
2502
-
2503
-    /**
2504
-     * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise
2505
-     * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation.
2506
-     * You can specify $default is case you haven't found the extra meta
2507
-     *
2508
-     * @param string  $meta_key
2509
-     * @param boolean $single
2510
-     * @param mixed   $default if we don't find anything, what should we return?
2511
-     * @return mixed single value if $single; array if ! $single
2512
-     * @throws \EE_Error
2513
-     */
2514
-    public function get_extra_meta($meta_key, $single = false, $default = null)
2515
-    {
2516
-        if ($single) {
2517
-            $result = $this->get_first_related('Extra_Meta', array(array('EXM_key' => $meta_key)));
2518
-            if ($result instanceof EE_Extra_Meta) {
2519
-                return $result->value();
2520
-            } else {
2521
-                return $default;
2522
-            }
2523
-        } else {
2524
-            $results = $this->get_many_related('Extra_Meta', array(array('EXM_key' => $meta_key)));
2525
-            if ($results) {
2526
-                $values = array();
2527
-                foreach ($results as $result) {
2528
-                    if ($result instanceof EE_Extra_Meta) {
2529
-                        $values[$result->ID()] = $result->value();
2530
-                    }
2531
-                }
2532
-                return $values;
2533
-            } else {
2534
-                return $default;
2535
-            }
2536
-        }
2537
-    }
2538
-
2539
-
2540
-
2541
-    /**
2542
-     * Returns a simple array of all the extra meta associated with this model object.
2543
-     * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the
2544
-     * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with
2545
-     * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123))
2546
-     * If $one_of_each_key is false, it will return an array with the top-level keys being
2547
-     * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and
2548
-     * finally the extra meta's value as each sub-value. (eg
2549
-     * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123)))
2550
-     *
2551
-     * @param boolean $one_of_each_key
2552
-     * @return array
2553
-     * @throws \EE_Error
2554
-     */
2555
-    public function all_extra_meta_array($one_of_each_key = true)
2556
-    {
2557
-        $return_array = array();
2558
-        if ($one_of_each_key) {
2559
-            $extra_meta_objs = $this->get_many_related('Extra_Meta', array('group_by' => 'EXM_key'));
2560
-            foreach ($extra_meta_objs as $extra_meta_obj) {
2561
-                if ($extra_meta_obj instanceof EE_Extra_Meta) {
2562
-                    $return_array[$extra_meta_obj->key()] = $extra_meta_obj->value();
2563
-                }
2564
-            }
2565
-        } else {
2566
-            $extra_meta_objs = $this->get_many_related('Extra_Meta');
2567
-            foreach ($extra_meta_objs as $extra_meta_obj) {
2568
-                if ($extra_meta_obj instanceof EE_Extra_Meta) {
2569
-                    if ( ! isset($return_array[$extra_meta_obj->key()])) {
2570
-                        $return_array[$extra_meta_obj->key()] = array();
2571
-                    }
2572
-                    $return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value();
2573
-                }
2574
-            }
2575
-        }
2576
-        return $return_array;
2577
-    }
2578
-
2579
-
2580
-
2581
-    /**
2582
-     * Gets a pretty nice displayable nice for this model object. Often overridden
2583
-     *
2584
-     * @return string
2585
-     * @throws \EE_Error
2586
-     */
2587
-    public function name()
2588
-    {
2589
-        //find a field that's not a text field
2590
-        $field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base');
2591
-        if ($field_we_can_use) {
2592
-            return $this->get($field_we_can_use->get_name());
2593
-        } else {
2594
-            $first_few_properties = $this->model_field_array();
2595
-            $first_few_properties = array_slice($first_few_properties, 0, 3);
2596
-            $name_parts = array();
2597
-            foreach ($first_few_properties as $name => $value) {
2598
-                $name_parts[] = "$name:$value";
2599
-            }
2600
-            return implode(",", $name_parts);
2601
-        }
2602
-    }
2603
-
2604
-
2605
-
2606
-    /**
2607
-     * in_entity_map
2608
-     * Checks if this model object has been proven to already be in the entity map
2609
-     *
2610
-     * @return boolean
2611
-     * @throws \EE_Error
2612
-     */
2613
-    public function in_entity_map()
2614
-    {
2615
-        if ($this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this) {
2616
-            //well, if we looked, did we find it in the entity map?
2617
-            return true;
2618
-        } else {
2619
-            return false;
2620
-        }
2621
-    }
2622
-
2623
-
2624
-
2625
-    /**
2626
-     * refresh_from_db
2627
-     * Makes sure the fields and values on this model object are in-sync with what's in the database.
2628
-     *
2629
-     * @throws EE_Error if this model object isn't in the entity mapper (because then you should
2630
-     * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE
2631
-     */
2632
-    public function refresh_from_db()
2633
-    {
2634
-        if ($this->ID() && $this->in_entity_map()) {
2635
-            $this->get_model()->refresh_entity_map_from_db($this->ID());
2636
-        } else {
2637
-            //if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database)
2638
-            //if it has an ID but it's not in the map, and you're asking me to refresh it
2639
-            //that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's
2640
-            //absolutely nothing in it for this ID
2641
-            if (WP_DEBUG) {
2642
-                throw new EE_Error(
2643
-                    sprintf(
2644
-                        __('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.',
2645
-                            'event_espresso'),
2646
-                        $this->ID(),
2647
-                        get_class($this->get_model()) . '::instance()->add_to_entity_map()',
2648
-                        get_class($this->get_model()) . '::instance()->refresh_entity_map()'
2649
-                    )
2650
-                );
2651
-            }
2652
-        }
2653
-    }
2654
-
2655
-
2656
-
2657
-    /**
2658
-     * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method
2659
-     * (probably a bad assumption they have made, oh well)
2660
-     *
2661
-     * @return string
2662
-     */
2663
-    public function __toString()
2664
-    {
2665
-        try {
2666
-            return sprintf('%s (%s)', $this->name(), $this->ID());
2667
-        } catch (Exception $e) {
2668
-            EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
2669
-            return '';
2670
-        }
2671
-    }
2672
-
2673
-
2674
-
2675
-    /**
2676
-     * Clear related model objects if they're already in the DB, because otherwise when we
2677
-     * UN-serialize this model object we'll need to be careful to add them to the entity map.
2678
-     * This means if we have made changes to those related model objects, and want to unserialize
2679
-     * the this model object on a subsequent request, changes to those related model objects will be lost.
2680
-     * Instead, those related model objects should be directly serialized and stored.
2681
-     * Eg, the following won't work:
2682
-     * $reg = EEM_Registration::instance()->get_one_by_ID( 123 );
2683
-     * $att = $reg->attendee();
2684
-     * $att->set( 'ATT_fname', 'Dirk' );
2685
-     * update_option( 'my_option', serialize( $reg ) );
2686
-     * //END REQUEST
2687
-     * //START NEXT REQUEST
2688
-     * $reg = get_option( 'my_option' );
2689
-     * $reg->attendee()->save();
2690
-     * And would need to be replace with:
2691
-     * $reg = EEM_Registration::instance()->get_one_by_ID( 123 );
2692
-     * $att = $reg->attendee();
2693
-     * $att->set( 'ATT_fname', 'Dirk' );
2694
-     * update_option( 'my_option', serialize( $reg ) );
2695
-     * //END REQUEST
2696
-     * //START NEXT REQUEST
2697
-     * $att = get_option( 'my_option' );
2698
-     * $att->save();
2699
-     *
2700
-     * @return array
2701
-     * @throws \EE_Error
2702
-     */
2703
-    public function __sleep()
2704
-    {
2705
-        $model = $this->get_model();
2706
-        foreach ($model->relation_settings() as $relation_name => $relation_obj) {
2707
-            if ($relation_obj instanceof EE_Belongs_To_Relation) {
2708
-                $classname = 'EE_' . $model->get_this_model_name();
2709
-                if (
2710
-                    $this->get_one_from_cache($relation_name) instanceof $classname
2711
-                    && $this->get_one_from_cache($relation_name)->ID()
2712
-                ) {
2713
-                    $this->clear_cache($relation_name, $this->get_one_from_cache($relation_name)->ID());
2714
-                }
2715
-            }
2716
-        }
2717
-        $this->_props_n_values_provided_in_constructor = array();
2718
-        $properties_to_serialize = get_object_vars($this);
2719
-        //don't serialize the model. It's big and that risks recursion
2720
-        unset($properties_to_serialize['_model']);
2721
-        return array_keys($properties_to_serialize);
2722
-    }
2723
-
2724
-
2725
-
2726
-    /**
2727
-     * restore _props_n_values_provided_in_constructor
2728
-     * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization,
2729
-     * and therefore should NOT be used to determine if state change has occurred since initial construction.
2730
-     * At best, you would only be able to detect if state change has occurred during THIS request.
2731
-     */
2732
-    public function __wakeup()
2733
-    {
2734
-        $this->_props_n_values_provided_in_constructor = $this->_fields;
2735
-    }
28
+	/**
29
+	 * This is an array of the original properties and values provided during construction
30
+	 * of this model object. (keys are model field names, values are their values).
31
+	 * This list is important to remember so that when we are merging data from the db, we know
32
+	 * which values to override and which to not override.
33
+	 *
34
+	 * @var array
35
+	 */
36
+	protected $_props_n_values_provided_in_constructor;
37
+
38
+	/**
39
+	 * Timezone
40
+	 * This gets set by the "set_timezone()" method so that we know what timezone incoming strings|timestamps are in.
41
+	 * This can also be used before a get to set what timezone you want strings coming out of the object to be in.  NOT
42
+	 * all EE_Base_Class child classes use this property but any that use a EE_Datetime_Field data type will have
43
+	 * access to it.
44
+	 *
45
+	 * @var string
46
+	 */
47
+	protected $_timezone;
48
+
49
+
50
+
51
+	/**
52
+	 * date format
53
+	 * pattern or format for displaying dates
54
+	 *
55
+	 * @var string $_dt_frmt
56
+	 */
57
+	protected $_dt_frmt;
58
+
59
+
60
+
61
+	/**
62
+	 * time format
63
+	 * pattern or format for displaying time
64
+	 *
65
+	 * @var string $_tm_frmt
66
+	 */
67
+	protected $_tm_frmt;
68
+
69
+
70
+
71
+	/**
72
+	 * This property is for holding a cached array of object properties indexed by property name as the key.
73
+	 * The purpose of this is for setting a cache on properties that may have calculated values after a
74
+	 * prepare_for_get.  That way the cache can be checked first and the calculated property returned instead of having
75
+	 * to recalculate. Used by _set_cached_property() and _get_cached_property() methods.
76
+	 *
77
+	 * @var array
78
+	 */
79
+	protected $_cached_properties = array();
80
+
81
+	/**
82
+	 * An array containing keys of the related model, and values are either an array of related mode objects or a
83
+	 * single
84
+	 * related model object. see the model's _model_relations. The keys should match those specified. And if the
85
+	 * relation is of type EE_Belongs_To (or one of its children), then there should only be ONE related model object,
86
+	 * all others have an array)
87
+	 *
88
+	 * @var array
89
+	 */
90
+	protected $_model_relations = array();
91
+
92
+	/**
93
+	 * Array where keys are field names (see the model's _fields property) and values are their values. To see what
94
+	 * their types should be, look at what that field object returns on its prepare_for_get and prepare_for_set methods)
95
+	 *
96
+	 * @var array
97
+	 */
98
+	protected $_fields = array();
99
+
100
+	/**
101
+	 * @var boolean indicating whether or not this model object is intended to ever be saved
102
+	 * For example, we might create model objects intended to only be used for the duration
103
+	 * of this request and to be thrown away, and if they were accidentally saved
104
+	 * it would be a bug.
105
+	 */
106
+	protected $_allow_persist = true;
107
+
108
+	/**
109
+	 * @var boolean indicating whether or not this model object's properties have changed since construction
110
+	 */
111
+	protected $_has_changes = false;
112
+
113
+	/**
114
+	 * @var EEM_Base
115
+	 */
116
+	protected $_model;
117
+
118
+
119
+
120
+	/**
121
+	 * basic constructor for Event Espresso classes, performs any necessary initialization, and verifies it's children
122
+	 * play nice
123
+	 *
124
+	 * @param array   $fieldValues                             where each key is a field (ie, array key in the 2nd
125
+	 *                                                         layer of the model's _fields array, (eg, EVT_ID,
126
+	 *                                                         TXN_amount, QST_name, etc) and values are their values
127
+	 * @param boolean $bydb                                    a flag for setting if the class is instantiated by the
128
+	 *                                                         corresponding db model or not.
129
+	 * @param string  $timezone                                indicate what timezone you want any datetime fields to
130
+	 *                                                         be in when instantiating a EE_Base_Class object.
131
+	 * @param array   $date_formats                            An array of date formats to set on construct where first
132
+	 *                                                         value is the date_format and second value is the time
133
+	 *                                                         format.
134
+	 * @throws EE_Error
135
+	 */
136
+	protected function __construct($fieldValues = array(), $bydb = false, $timezone = '', $date_formats = array())
137
+	{
138
+		$className = get_class($this);
139
+		do_action("AHEE__{$className}__construct", $this, $fieldValues);
140
+		$model = $this->get_model();
141
+		$model_fields = $model->field_settings(false);
142
+		// ensure $fieldValues is an array
143
+		$fieldValues = is_array($fieldValues) ? $fieldValues : array($fieldValues);
144
+		// EEH_Debug_Tools::printr( $fieldValues, '$fieldValues  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
145
+		// verify client code has not passed any invalid field names
146
+		foreach ($fieldValues as $field_name => $field_value) {
147
+			if ( ! isset($model_fields[$field_name])) {
148
+				throw new EE_Error(sprintf(__("Invalid field (%s) passed to constructor of %s. Allowed fields are :%s",
149
+					"event_espresso"), $field_name, get_class($this), implode(", ", array_keys($model_fields))));
150
+			}
151
+		}
152
+		// EEH_Debug_Tools::printr( $model_fields, '$model_fields  <br /><span style="font-size:10px;font-weight:normal;">' . __FILE__ . '<br />line no: ' . __LINE__ . '</span>', 'auto' );
153
+		$this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
154
+		if ( ! empty($date_formats) && is_array($date_formats)) {
155
+			list($this->_dt_frmt, $this->_tm_frmt) = $date_formats;
156
+		} else {
157
+			//set default formats for date and time
158
+			$this->_dt_frmt = (string)get_option('date_format', 'Y-m-d');
159
+			$this->_tm_frmt = (string)get_option('time_format', 'g:i a');
160
+		}
161
+		//if db model is instantiating
162
+		if ($bydb) {
163
+			//client code has indicated these field values are from the database
164
+			foreach ($model_fields as $fieldName => $field) {
165
+				$this->set_from_db($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null);
166
+			}
167
+		} else {
168
+			//we're constructing a brand
169
+			//new instance of the model object. Generally, this means we'll need to do more field validation
170
+			foreach ($model_fields as $fieldName => $field) {
171
+				$this->set($fieldName, isset($fieldValues[$fieldName]) ? $fieldValues[$fieldName] : null, true);
172
+			}
173
+		}
174
+		//remember what values were passed to this constructor
175
+		$this->_props_n_values_provided_in_constructor = $fieldValues;
176
+		//remember in entity mapper
177
+		if ( ! $bydb && $model->has_primary_key_field() && $this->ID()) {
178
+			$model->add_to_entity_map($this);
179
+		}
180
+		//setup all the relations
181
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
182
+			if ($relation_obj instanceof EE_Belongs_To_Relation) {
183
+				$this->_model_relations[$relation_name] = null;
184
+			} else {
185
+				$this->_model_relations[$relation_name] = array();
186
+			}
187
+		}
188
+		/**
189
+		 * Action done at the end of each model object construction
190
+		 *
191
+		 * @param EE_Base_Class $this the model object just created
192
+		 */
193
+		do_action('AHEE__EE_Base_Class__construct__finished', $this);
194
+	}
195
+
196
+
197
+
198
+	/**
199
+	 * Gets whether or not this model object is allowed to persist/be saved to the database.
200
+	 *
201
+	 * @return boolean
202
+	 */
203
+	public function allow_persist()
204
+	{
205
+		return $this->_allow_persist;
206
+	}
207
+
208
+
209
+
210
+	/**
211
+	 * Sets whether or not this model object should be allowed to be saved to the DB.
212
+	 * Normally once this is set to FALSE you wouldn't set it back to TRUE, unless
213
+	 * you got new information that somehow made you change your mind.
214
+	 *
215
+	 * @param boolean $allow_persist
216
+	 * @return boolean
217
+	 */
218
+	public function set_allow_persist($allow_persist)
219
+	{
220
+		return $this->_allow_persist = $allow_persist;
221
+	}
222
+
223
+
224
+
225
+	/**
226
+	 * Gets the field's original value when this object was constructed during this request.
227
+	 * This can be helpful when determining if a model object has changed or not
228
+	 *
229
+	 * @param string $field_name
230
+	 * @return mixed|null
231
+	 * @throws \EE_Error
232
+	 */
233
+	public function get_original($field_name)
234
+	{
235
+		if (isset($this->_props_n_values_provided_in_constructor[$field_name])
236
+			&& $field_settings = $this->get_model()->field_settings_for($field_name)
237
+		) {
238
+			return $field_settings->prepare_for_get($this->_props_n_values_provided_in_constructor[$field_name]);
239
+		} else {
240
+			return null;
241
+		}
242
+	}
243
+
244
+
245
+
246
+	/**
247
+	 * @param EE_Base_Class $obj
248
+	 * @return string
249
+	 */
250
+	public function get_class($obj)
251
+	{
252
+		return get_class($obj);
253
+	}
254
+
255
+
256
+
257
+	/**
258
+	 * Overrides parent because parent expects old models.
259
+	 * This also doesn't do any validation, and won't work for serialized arrays
260
+	 *
261
+	 * @param    string $field_name
262
+	 * @param    mixed  $field_value
263
+	 * @param bool      $use_default
264
+	 * @throws \EE_Error
265
+	 */
266
+	public function set($field_name, $field_value, $use_default = false)
267
+	{
268
+		// if not using default and nothing has changed, and object has already been setup (has ID),
269
+		// then don't do anything
270
+		if (
271
+			! $use_default
272
+			&& $this->_fields[$field_name] === $field_value
273
+			&& $this->ID()
274
+		) {
275
+			return;
276
+		}
277
+		$model = $this->get_model();
278
+		$this->_has_changes = true;
279
+		$field_obj = $model->field_settings_for($field_name);
280
+		if ($field_obj instanceof EE_Model_Field_Base) {
281
+			//			if ( method_exists( $field_obj, 'set_timezone' )) {
282
+			if ($field_obj instanceof EE_Datetime_Field) {
283
+				$field_obj->set_timezone($this->_timezone);
284
+				$field_obj->set_date_format($this->_dt_frmt);
285
+				$field_obj->set_time_format($this->_tm_frmt);
286
+			}
287
+			$holder_of_value = $field_obj->prepare_for_set($field_value);
288
+			//should the value be null?
289
+			if (($field_value === null || $holder_of_value === null || $holder_of_value === '') && $use_default) {
290
+				$this->_fields[$field_name] = $field_obj->get_default_value();
291
+				/**
292
+				 * To save having to refactor all the models, if a default value is used for a
293
+				 * EE_Datetime_Field, and that value is not null nor is it a DateTime
294
+				 * object.  Then let's do a set again to ensure that it becomes a DateTime
295
+				 * object.
296
+				 *
297
+				 * @since 4.6.10+
298
+				 */
299
+				if (
300
+					$field_obj instanceof EE_Datetime_Field
301
+					&& $this->_fields[$field_name] !== null
302
+					&& ! $this->_fields[$field_name] instanceof DateTime
303
+				) {
304
+					empty($this->_fields[$field_name])
305
+						? $this->set($field_name, time())
306
+						: $this->set($field_name, $this->_fields[$field_name]);
307
+				}
308
+			} else {
309
+				$this->_fields[$field_name] = $holder_of_value;
310
+			}
311
+			//if we're not in the constructor...
312
+			//now check if what we set was a primary key
313
+			if (
314
+				//note: props_n_values_provided_in_constructor is only set at the END of the constructor
315
+				$this->_props_n_values_provided_in_constructor
316
+				&& $field_value
317
+				&& $field_name === $model->primary_key_name()
318
+			) {
319
+				//if so, we want all this object's fields to be filled either with
320
+				//what we've explicitly set on this model
321
+				//or what we have in the db
322
+				// echo "setting primary key!";
323
+				$fields_on_model = self::_get_model(get_class($this))->field_settings();
324
+				$obj_in_db = self::_get_model(get_class($this))->get_one_by_ID($field_value);
325
+				foreach ($fields_on_model as $field_obj) {
326
+					if ( ! array_key_exists($field_obj->get_name(), $this->_props_n_values_provided_in_constructor)
327
+						 && $field_obj->get_name() !== $field_name
328
+					) {
329
+						$this->set($field_obj->get_name(), $obj_in_db->get($field_obj->get_name()));
330
+					}
331
+				}
332
+				//oh this model object has an ID? well make sure its in the entity mapper
333
+				$model->add_to_entity_map($this);
334
+			}
335
+			//let's unset any cache for this field_name from the $_cached_properties property.
336
+			$this->_clear_cached_property($field_name);
337
+		} else {
338
+			throw new EE_Error(sprintf(__("A valid EE_Model_Field_Base could not be found for the given field name: %s",
339
+				"event_espresso"), $field_name));
340
+		}
341
+	}
342
+
343
+
344
+
345
+	/**
346
+	 * This sets the field value on the db column if it exists for the given $column_name or
347
+	 * saves it to EE_Extra_Meta if the given $column_name does not match a db column.
348
+	 *
349
+	 * @see EE_message::get_column_value for related documentation on the necessity of this method.
350
+	 * @param string $field_name  Must be the exact column name.
351
+	 * @param mixed  $field_value The value to set.
352
+	 * @return int|bool @see EE_Base_Class::update_extra_meta() for return docs.
353
+	 * @throws \EE_Error
354
+	 */
355
+	public function set_field_or_extra_meta($field_name, $field_value)
356
+	{
357
+		if ($this->get_model()->has_field($field_name)) {
358
+			$this->set($field_name, $field_value);
359
+			return true;
360
+		} else {
361
+			//ensure this object is saved first so that extra meta can be properly related.
362
+			$this->save();
363
+			return $this->update_extra_meta($field_name, $field_value);
364
+		}
365
+	}
366
+
367
+
368
+
369
+	/**
370
+	 * This retrieves the value of the db column set on this class or if that's not present
371
+	 * it will attempt to retrieve from extra_meta if found.
372
+	 * Example Usage:
373
+	 * Via EE_Message child class:
374
+	 * Due to the dynamic nature of the EE_messages system, EE_messengers will always have a "to",
375
+	 * "from", "subject", and "content" field (as represented in the EE_Message schema), however they may
376
+	 * also have additional main fields specific to the messenger.  The system accommodates those extra
377
+	 * fields through the EE_Extra_Meta table.  This method allows for EE_messengers to retrieve the
378
+	 * value for those extra fields dynamically via the EE_message object.
379
+	 *
380
+	 * @param  string $field_name expecting the fully qualified field name.
381
+	 * @return mixed|null  value for the field if found.  null if not found.
382
+	 * @throws \EE_Error
383
+	 */
384
+	public function get_field_or_extra_meta($field_name)
385
+	{
386
+		if ($this->get_model()->has_field($field_name)) {
387
+			$column_value = $this->get($field_name);
388
+		} else {
389
+			//This isn't a column in the main table, let's see if it is in the extra meta.
390
+			$column_value = $this->get_extra_meta($field_name, true, null);
391
+		}
392
+		return $column_value;
393
+	}
394
+
395
+
396
+
397
+	/**
398
+	 * See $_timezone property for description of what the timezone property is for.  This SETS the timezone internally
399
+	 * for being able to reference what timezone we are running conversions on when converting TO the internal timezone
400
+	 * (UTC Unix Timestamp) for the object OR when converting FROM the internal timezone (UTC Unix Timestamp). This is
401
+	 * available to all child classes that may be using the EE_Datetime_Field for a field data type.
402
+	 *
403
+	 * @access public
404
+	 * @param string $timezone A valid timezone string as described by @link http://www.php.net/manual/en/timezones.php
405
+	 * @return void
406
+	 * @throws \EE_Error
407
+	 */
408
+	public function set_timezone($timezone = '')
409
+	{
410
+		$this->_timezone = EEH_DTT_Helper::get_valid_timezone_string($timezone);
411
+		//make sure we clear all cached properties because they won't be relevant now
412
+		$this->_clear_cached_properties();
413
+		//make sure we update field settings and the date for all EE_Datetime_Fields
414
+		$model_fields = $this->get_model()->field_settings(false);
415
+		foreach ($model_fields as $field_name => $field_obj) {
416
+			if ($field_obj instanceof EE_Datetime_Field) {
417
+				$field_obj->set_timezone($this->_timezone);
418
+				if (isset($this->_fields[$field_name]) && $this->_fields[$field_name] instanceof DateTime) {
419
+					$this->_fields[$field_name]->setTimezone(new DateTimeZone($this->_timezone));
420
+				}
421
+			}
422
+		}
423
+	}
424
+
425
+
426
+
427
+	/**
428
+	 * This just returns whatever is set for the current timezone.
429
+	 *
430
+	 * @access public
431
+	 * @return string timezone string
432
+	 */
433
+	public function get_timezone()
434
+	{
435
+		return $this->_timezone;
436
+	}
437
+
438
+
439
+
440
+	/**
441
+	 * This sets the internal date format to what is sent in to be used as the new default for the class
442
+	 * internally instead of wp set date format options
443
+	 *
444
+	 * @since 4.6
445
+	 * @param string $format should be a format recognizable by PHP date() functions.
446
+	 */
447
+	public function set_date_format($format)
448
+	{
449
+		$this->_dt_frmt = $format;
450
+		//clear cached_properties because they won't be relevant now.
451
+		$this->_clear_cached_properties();
452
+	}
453
+
454
+
455
+
456
+	/**
457
+	 * This sets the internal time format string to what is sent in to be used as the new default for the
458
+	 * class internally instead of wp set time format options.
459
+	 *
460
+	 * @since 4.6
461
+	 * @param string $format should be a format recognizable by PHP date() functions.
462
+	 */
463
+	public function set_time_format($format)
464
+	{
465
+		$this->_tm_frmt = $format;
466
+		//clear cached_properties because they won't be relevant now.
467
+		$this->_clear_cached_properties();
468
+	}
469
+
470
+
471
+
472
+	/**
473
+	 * This returns the current internal set format for the date and time formats.
474
+	 *
475
+	 * @param bool $full           if true (default), then return the full format.  Otherwise will return an array
476
+	 *                             where the first value is the date format and the second value is the time format.
477
+	 * @return mixed string|array
478
+	 */
479
+	public function get_format($full = true)
480
+	{
481
+		return $full ? $this->_dt_frmt . ' ' . $this->_tm_frmt : array($this->_dt_frmt, $this->_tm_frmt);
482
+	}
483
+
484
+
485
+
486
+	/**
487
+	 * cache
488
+	 * stores the passed model object on the current model object.
489
+	 * In certain circumstances, we can use this cached model object instead of querying for another one entirely.
490
+	 *
491
+	 * @param string        $relationName    one of the keys in the _model_relations array on the model. Eg
492
+	 *                                       'Registration' associated with this model object
493
+	 * @param EE_Base_Class $object_to_cache that has a relation to this model object. (Eg, if this is a Transaction,
494
+	 *                                       that could be a payment or a registration)
495
+	 * @param null          $cache_id        a string or number that will be used as the key for any Belongs_To_Many
496
+	 *                                       items which will be stored in an array on this object
497
+	 * @throws EE_Error
498
+	 * @return mixed    index into cache, or just TRUE if the relation is of type Belongs_To (because there's only one
499
+	 *                  related thing, no array)
500
+	 */
501
+	public function cache($relationName = '', $object_to_cache = null, $cache_id = null)
502
+	{
503
+		// its entirely possible that there IS no related object yet in which case there is nothing to cache.
504
+		if ( ! $object_to_cache instanceof EE_Base_Class) {
505
+			return false;
506
+		}
507
+		// also get "how" the object is related, or throw an error
508
+		if ( ! $relationship_to_model = $this->get_model()->related_settings_for($relationName)) {
509
+			throw new EE_Error(sprintf(__('There is no relationship to %s on a %s. Cannot cache it', 'event_espresso'),
510
+				$relationName, get_class($this)));
511
+		}
512
+		// how many things are related ?
513
+		if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
514
+			// if it's a "belongs to" relationship, then there's only one related model object  eg, if this is a registration, there's only 1 attendee for it
515
+			// so for these model objects just set it to be cached
516
+			$this->_model_relations[$relationName] = $object_to_cache;
517
+			$return = true;
518
+		} else {
519
+			// otherwise, this is the "many" side of a one to many relationship, so we'll add the object to the array of related objects for that type.
520
+			// eg: if this is an event, there are many registrations for that event, so we cache the registrations in an array
521
+			if ( ! is_array($this->_model_relations[$relationName])) {
522
+				// if for some reason, the cached item is a model object, then stick that in the array, otherwise start with an empty array
523
+				$this->_model_relations[$relationName] = $this->_model_relations[$relationName] instanceof EE_Base_Class
524
+					? array($this->_model_relations[$relationName]) : array();
525
+			}
526
+			// first check for a cache_id which is normally empty
527
+			if ( ! empty($cache_id)) {
528
+				// if the cache_id exists, then it means we are purposely trying to cache this with a known key that can then be used to retrieve the object later on
529
+				$this->_model_relations[$relationName][$cache_id] = $object_to_cache;
530
+				$return = $cache_id;
531
+			} elseif ($object_to_cache->ID()) {
532
+				// OR the cached object originally came from the db, so let's just use it's PK for an ID
533
+				$this->_model_relations[$relationName][$object_to_cache->ID()] = $object_to_cache;
534
+				$return = $object_to_cache->ID();
535
+			} else {
536
+				// OR it's a new object with no ID, so just throw it in the array with an auto-incremented ID
537
+				$this->_model_relations[$relationName][] = $object_to_cache;
538
+				// move the internal pointer to the end of the array
539
+				end($this->_model_relations[$relationName]);
540
+				// and grab the key so that we can return it
541
+				$return = key($this->_model_relations[$relationName]);
542
+			}
543
+		}
544
+		return $return;
545
+	}
546
+
547
+
548
+
549
+	/**
550
+	 * For adding an item to the cached_properties property.
551
+	 *
552
+	 * @access protected
553
+	 * @param string      $fieldname the property item the corresponding value is for.
554
+	 * @param mixed       $value     The value we are caching.
555
+	 * @param string|null $cache_type
556
+	 * @return void
557
+	 * @throws \EE_Error
558
+	 */
559
+	protected function _set_cached_property($fieldname, $value, $cache_type = null)
560
+	{
561
+		//first make sure this property exists
562
+		$this->get_model()->field_settings_for($fieldname);
563
+		$cache_type = empty($cache_type) ? 'standard' : $cache_type;
564
+		$this->_cached_properties[$fieldname][$cache_type] = $value;
565
+	}
566
+
567
+
568
+
569
+	/**
570
+	 * This returns the value cached property if it exists OR the actual property value if the cache doesn't exist.
571
+	 * This also SETS the cache if we return the actual property!
572
+	 *
573
+	 * @param string $fieldname        the name of the property we're trying to retrieve
574
+	 * @param bool   $pretty
575
+	 * @param string $extra_cache_ref  This allows the user to specify an extra cache ref for the given property
576
+	 *                                 (in cases where the same property may be used for different outputs
577
+	 *                                 - i.e. datetime, money etc.)
578
+	 *                                 It can also accept certain pre-defined "schema" strings
579
+	 *                                 to define how to output the property.
580
+	 *                                 see the field's prepare_for_pretty_echoing for what strings can be used
581
+	 * @return mixed                   whatever the value for the property is we're retrieving
582
+	 * @throws \EE_Error
583
+	 */
584
+	protected function _get_cached_property($fieldname, $pretty = false, $extra_cache_ref = null)
585
+	{
586
+		//verify the field exists
587
+		$model = $this->get_model();
588
+		$model->field_settings_for($fieldname);
589
+		$cache_type = $pretty ? 'pretty' : 'standard';
590
+		$cache_type .= ! empty($extra_cache_ref) ? '_' . $extra_cache_ref : '';
591
+		if (isset($this->_cached_properties[$fieldname][$cache_type])) {
592
+			return $this->_cached_properties[$fieldname][$cache_type];
593
+		}
594
+		$value = $this->_get_fresh_property($fieldname, $pretty, $extra_cache_ref);
595
+		$this->_set_cached_property($fieldname, $value, $cache_type);
596
+		return $value;
597
+	}
598
+
599
+
600
+
601
+	/**
602
+	 * If the cache didn't fetch the needed item, this fetches it.
603
+	 * @param string $fieldname
604
+	 * @param bool $pretty
605
+	 * @param string $extra_cache_ref
606
+	 * @return mixed
607
+	 */
608
+	protected function _get_fresh_property($fieldname, $pretty = false, $extra_cache_ref = null)
609
+	{
610
+		$field_obj = $this->get_model()->field_settings_for($fieldname);
611
+		// If this is an EE_Datetime_Field we need to make sure timezone, formats, and output are correct
612
+		if ($field_obj instanceof EE_Datetime_Field) {
613
+			$this->_prepare_datetime_field($field_obj, $pretty, $extra_cache_ref);
614
+		}
615
+		if ( ! isset($this->_fields[$fieldname])) {
616
+			$this->_fields[$fieldname] = null;
617
+		}
618
+		$value = $pretty
619
+			? $field_obj->prepare_for_pretty_echoing($this->_fields[$fieldname], $extra_cache_ref)
620
+			: $field_obj->prepare_for_get($this->_fields[$fieldname]);
621
+		return $value;
622
+	}
623
+
624
+
625
+
626
+	/**
627
+	 * set timezone, formats, and output for EE_Datetime_Field objects
628
+	 *
629
+	 * @param \EE_Datetime_Field $datetime_field
630
+	 * @param bool               $pretty
631
+	 * @param null $date_or_time
632
+	 * @return void
633
+	 * @throws \EE_Error
634
+	 */
635
+	protected function _prepare_datetime_field(
636
+		EE_Datetime_Field $datetime_field,
637
+		$pretty = false,
638
+		$date_or_time = null
639
+	) {
640
+		$datetime_field->set_timezone($this->_timezone);
641
+		$datetime_field->set_date_format($this->_dt_frmt, $pretty);
642
+		$datetime_field->set_time_format($this->_tm_frmt, $pretty);
643
+		//set the output returned
644
+		switch ($date_or_time) {
645
+			case 'D' :
646
+				$datetime_field->set_date_time_output('date');
647
+				break;
648
+			case 'T' :
649
+				$datetime_field->set_date_time_output('time');
650
+				break;
651
+			default :
652
+				$datetime_field->set_date_time_output();
653
+		}
654
+	}
655
+
656
+
657
+
658
+	/**
659
+	 * This just takes care of clearing out the cached_properties
660
+	 *
661
+	 * @return void
662
+	 */
663
+	protected function _clear_cached_properties()
664
+	{
665
+		$this->_cached_properties = array();
666
+	}
667
+
668
+
669
+
670
+	/**
671
+	 * This just clears out ONE property if it exists in the cache
672
+	 *
673
+	 * @param  string $property_name the property to remove if it exists (from the _cached_properties array)
674
+	 * @return void
675
+	 */
676
+	protected function _clear_cached_property($property_name)
677
+	{
678
+		if (isset($this->_cached_properties[$property_name])) {
679
+			unset($this->_cached_properties[$property_name]);
680
+		}
681
+	}
682
+
683
+
684
+
685
+	/**
686
+	 * Ensures that this related thing is a model object.
687
+	 *
688
+	 * @param mixed  $object_or_id EE_base_Class/int/string either a related model object, or its ID
689
+	 * @param string $model_name   name of the related thing, eg 'Attendee',
690
+	 * @return EE_Base_Class
691
+	 * @throws \EE_Error
692
+	 */
693
+	protected function ensure_related_thing_is_model_obj($object_or_id, $model_name)
694
+	{
695
+		$other_model_instance = self::_get_model_instance_with_name(
696
+			self::_get_model_classname($model_name),
697
+			$this->_timezone
698
+		);
699
+		return $other_model_instance->ensure_is_obj($object_or_id);
700
+	}
701
+
702
+
703
+
704
+	/**
705
+	 * Forgets the cached model of the given relation Name. So the next time we request it,
706
+	 * we will fetch it again from the database. (Handy if you know it's changed somehow).
707
+	 * If a specific object is supplied, and the relationship to it is either a HasMany or HABTM,
708
+	 * then only remove that one object from our cached array. Otherwise, clear the entire list
709
+	 *
710
+	 * @param string $relationName                         one of the keys in the _model_relations array on the model.
711
+	 *                                                     Eg 'Registration'
712
+	 * @param mixed  $object_to_remove_or_index_into_array or an index into the array of cached things, or NULL
713
+	 *                                                     if you intend to use $clear_all = TRUE, or the relation only
714
+	 *                                                     has 1 object anyways (ie, it's a BelongsToRelation)
715
+	 * @param bool   $clear_all                            This flags clearing the entire cache relation property if
716
+	 *                                                     this is HasMany or HABTM.
717
+	 * @throws EE_Error
718
+	 * @return EE_Base_Class | boolean from which was cleared from the cache, or true if we requested to remove a
719
+	 *                       relation from all
720
+	 */
721
+	public function clear_cache($relationName, $object_to_remove_or_index_into_array = null, $clear_all = false)
722
+	{
723
+		$relationship_to_model = $this->get_model()->related_settings_for($relationName);
724
+		$index_in_cache = '';
725
+		if ( ! $relationship_to_model) {
726
+			throw new EE_Error(
727
+				sprintf(
728
+					__("There is no relationship to %s on a %s. Cannot clear that cache", 'event_espresso'),
729
+					$relationName,
730
+					get_class($this)
731
+				)
732
+			);
733
+		}
734
+		if ($clear_all) {
735
+			$obj_removed = true;
736
+			$this->_model_relations[$relationName] = null;
737
+		} elseif ($relationship_to_model instanceof EE_Belongs_To_Relation) {
738
+			$obj_removed = $this->_model_relations[$relationName];
739
+			$this->_model_relations[$relationName] = null;
740
+		} else {
741
+			if ($object_to_remove_or_index_into_array instanceof EE_Base_Class
742
+				&& $object_to_remove_or_index_into_array->ID()
743
+			) {
744
+				$index_in_cache = $object_to_remove_or_index_into_array->ID();
745
+				if (is_array($this->_model_relations[$relationName])
746
+					&& ! isset($this->_model_relations[$relationName][$index_in_cache])
747
+				) {
748
+					$index_found_at = null;
749
+					//find this object in the array even though it has a different key
750
+					foreach ($this->_model_relations[$relationName] as $index => $obj) {
751
+						if (
752
+							$obj instanceof EE_Base_Class
753
+							&& (
754
+								$obj == $object_to_remove_or_index_into_array
755
+								|| $obj->ID() === $object_to_remove_or_index_into_array->ID()
756
+							)
757
+						) {
758
+							$index_found_at = $index;
759
+							break;
760
+						}
761
+					}
762
+					if ($index_found_at) {
763
+						$index_in_cache = $index_found_at;
764
+					} else {
765
+						//it wasn't found. huh. well obviously it doesn't need to be removed from teh cache
766
+						//if it wasn't in it to begin with. So we're done
767
+						return $object_to_remove_or_index_into_array;
768
+					}
769
+				}
770
+			} elseif ($object_to_remove_or_index_into_array instanceof EE_Base_Class) {
771
+				//so they provided a model object, but it's not yet saved to the DB... so let's go hunting for it!
772
+				foreach ($this->get_all_from_cache($relationName) as $index => $potentially_obj_we_want) {
773
+					if ($potentially_obj_we_want == $object_to_remove_or_index_into_array) {
774
+						$index_in_cache = $index;
775
+					}
776
+				}
777
+			} else {
778
+				$index_in_cache = $object_to_remove_or_index_into_array;
779
+			}
780
+			//supposedly we've found it. But it could just be that the client code
781
+			//provided a bad index/object
782
+			if (
783
+			isset(
784
+				$this->_model_relations[$relationName],
785
+				$this->_model_relations[$relationName][$index_in_cache]
786
+			)
787
+			) {
788
+				$obj_removed = $this->_model_relations[$relationName][$index_in_cache];
789
+				unset($this->_model_relations[$relationName][$index_in_cache]);
790
+			} else {
791
+				//that thing was never cached anyways.
792
+				$obj_removed = null;
793
+			}
794
+		}
795
+		return $obj_removed;
796
+	}
797
+
798
+
799
+
800
+	/**
801
+	 * update_cache_after_object_save
802
+	 * Allows a cached item to have it's cache ID (within the array of cached items) reset using the new ID it has
803
+	 * obtained after being saved to the db
804
+	 *
805
+	 * @param string         $relationName       - the type of object that is cached
806
+	 * @param \EE_Base_Class $newly_saved_object - the newly saved object to be re-cached
807
+	 * @param string         $current_cache_id   - the ID that was used when originally caching the object
808
+	 * @return boolean TRUE on success, FALSE on fail
809
+	 * @throws \EE_Error
810
+	 */
811
+	public function update_cache_after_object_save(
812
+		$relationName,
813
+		EE_Base_Class $newly_saved_object,
814
+		$current_cache_id = ''
815
+	) {
816
+		// verify that incoming object is of the correct type
817
+		$obj_class = 'EE_' . $relationName;
818
+		if ($newly_saved_object instanceof $obj_class) {
819
+			/* @type EE_Base_Class $newly_saved_object */
820
+			// now get the type of relation
821
+			$relationship_to_model = $this->get_model()->related_settings_for($relationName);
822
+			// if this is a 1:1 relationship
823
+			if ($relationship_to_model instanceof EE_Belongs_To_Relation) {
824
+				// then just replace the cached object with the newly saved object
825
+				$this->_model_relations[$relationName] = $newly_saved_object;
826
+				return true;
827
+				// or if it's some kind of sordid feral polyamorous relationship...
828
+			} elseif (is_array($this->_model_relations[$relationName])
829
+					  && isset($this->_model_relations[$relationName][$current_cache_id])
830
+			) {
831
+				// then remove the current cached item
832
+				unset($this->_model_relations[$relationName][$current_cache_id]);
833
+				// and cache the newly saved object using it's new ID
834
+				$this->_model_relations[$relationName][$newly_saved_object->ID()] = $newly_saved_object;
835
+				return true;
836
+			}
837
+		}
838
+		return false;
839
+	}
840
+
841
+
842
+
843
+	/**
844
+	 * Fetches a single EE_Base_Class on that relation. (If the relation is of type
845
+	 * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects)
846
+	 *
847
+	 * @param string $relationName
848
+	 * @return EE_Base_Class
849
+	 */
850
+	public function get_one_from_cache($relationName)
851
+	{
852
+		$cached_array_or_object = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName]
853
+			: null;
854
+		if (is_array($cached_array_or_object)) {
855
+			return array_shift($cached_array_or_object);
856
+		} else {
857
+			return $cached_array_or_object;
858
+		}
859
+	}
860
+
861
+
862
+
863
+	/**
864
+	 * Fetches a single EE_Base_Class on that relation. (If the relation is of type
865
+	 * BelongsTo, it will only ever have 1 object. However, other relations could have an array of objects)
866
+	 *
867
+	 * @param string $relationName
868
+	 * @throws \EE_Error
869
+	 * @return EE_Base_Class[] NOT necessarily indexed by primary keys
870
+	 */
871
+	public function get_all_from_cache($relationName)
872
+	{
873
+		$objects = isset($this->_model_relations[$relationName]) ? $this->_model_relations[$relationName] : array();
874
+		// if the result is not an array, but exists, make it an array
875
+		$objects = is_array($objects) ? $objects : array($objects);
876
+		//bugfix for https://events.codebasehq.com/projects/event-espresso/tickets/7143
877
+		//basically, if this model object was stored in the session, and these cached model objects
878
+		//already have IDs, let's make sure they're in their model's entity mapper
879
+		//otherwise we will have duplicates next time we call
880
+		// EE_Registry::instance()->load_model( $relationName )->get_one_by_ID( $result->ID() );
881
+		$model = EE_Registry::instance()->load_model($relationName);
882
+		foreach ($objects as $model_object) {
883
+			if ($model instanceof EEM_Base && $model_object instanceof EE_Base_Class) {
884
+				//ensure its in the map if it has an ID; otherwise it will be added to the map when its saved
885
+				if ($model_object->ID()) {
886
+					$model->add_to_entity_map($model_object);
887
+				}
888
+			} else {
889
+				throw new EE_Error(
890
+					sprintf(
891
+						__(
892
+							'Error retrieving related model objects. Either $1%s is not a model or $2%s is not a model object',
893
+							'event_espresso'
894
+						),
895
+						$relationName,
896
+						gettype($model_object)
897
+					)
898
+				);
899
+			}
900
+		}
901
+		return $objects;
902
+	}
903
+
904
+
905
+
906
+	/**
907
+	 * Returns the next x number of EE_Base_Class objects in sequence from this object as found in the database
908
+	 * matching the given query conditions.
909
+	 *
910
+	 * @param null  $field_to_order_by  What field is being used as the reference point.
911
+	 * @param int   $limit              How many objects to return.
912
+	 * @param array $query_params       Any additional conditions on the query.
913
+	 * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
914
+	 *                                  you can indicate just the columns you want returned
915
+	 * @return array|EE_Base_Class[]
916
+	 * @throws \EE_Error
917
+	 */
918
+	public function next_x($field_to_order_by = null, $limit = 1, $query_params = array(), $columns_to_select = null)
919
+	{
920
+		$model = $this->get_model();
921
+		$field = empty($field_to_order_by) && $model->has_primary_key_field()
922
+			? $model->get_primary_key_field()->get_name()
923
+			: $field_to_order_by;
924
+		$current_value = ! empty($field) ? $this->get($field) : null;
925
+		if (empty($field) || empty($current_value)) {
926
+			return array();
927
+		}
928
+		return $model->next_x($current_value, $field, $limit, $query_params, $columns_to_select);
929
+	}
930
+
931
+
932
+
933
+	/**
934
+	 * Returns the previous x number of EE_Base_Class objects in sequence from this object as found in the database
935
+	 * matching the given query conditions.
936
+	 *
937
+	 * @param null  $field_to_order_by  What field is being used as the reference point.
938
+	 * @param int   $limit              How many objects to return.
939
+	 * @param array $query_params       Any additional conditions on the query.
940
+	 * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
941
+	 *                                  you can indicate just the columns you want returned
942
+	 * @return array|EE_Base_Class[]
943
+	 * @throws \EE_Error
944
+	 */
945
+	public function previous_x(
946
+		$field_to_order_by = null,
947
+		$limit = 1,
948
+		$query_params = array(),
949
+		$columns_to_select = null
950
+	) {
951
+		$model = $this->get_model();
952
+		$field = empty($field_to_order_by) && $model->has_primary_key_field()
953
+			? $model->get_primary_key_field()->get_name()
954
+			: $field_to_order_by;
955
+		$current_value = ! empty($field) ? $this->get($field) : null;
956
+		if (empty($field) || empty($current_value)) {
957
+			return array();
958
+		}
959
+		return $model->previous_x($current_value, $field, $limit, $query_params, $columns_to_select);
960
+	}
961
+
962
+
963
+
964
+	/**
965
+	 * Returns the next EE_Base_Class object in sequence from this object as found in the database
966
+	 * matching the given query conditions.
967
+	 *
968
+	 * @param null  $field_to_order_by  What field is being used as the reference point.
969
+	 * @param array $query_params       Any additional conditions on the query.
970
+	 * @param null  $columns_to_select  If left null, then an array of EE_Base_Class objects is returned, otherwise
971
+	 *                                  you can indicate just the columns you want returned
972
+	 * @return array|EE_Base_Class
973
+	 * @throws \EE_Error
974
+	 */
975
+	public function next($field_to_order_by = null, $query_params = array(), $columns_to_select = null)
976
+	{
977
+		$model = $this->get_model();
978
+		$field = empty($field_to_order_by) && $model->has_primary_key_field()
979
+			? $model->get_primary_key_field()->get_name()
980
+			: $field_to_order_by;
981
+		$current_value = ! empty($field) ? $this->get($field) : null;
982
+		if (empty($field) || empty($current_value)) {
983
+			return array();
984
+		}
985
+		return $model->next($current_value, $field, $query_params, $columns_to_select);
986
+	}
987
+
988
+
989
+
990
+	/**
991
+	 * Returns the previous EE_Base_Class object in sequence from this object as found in the database
992
+	 * matching the given query conditions.
993
+	 *
994
+	 * @param null  $field_to_order_by  What field is being used as the reference point.
995
+	 * @param array $query_params       Any additional conditions on the query.
996
+	 * @param null  $columns_to_select  If left null, then an EE_Base_Class object is returned, otherwise
997
+	 *                                  you can indicate just the column you want returned
998
+	 * @return array|EE_Base_Class
999
+	 * @throws \EE_Error
1000
+	 */
1001
+	public function previous($field_to_order_by = null, $query_params = array(), $columns_to_select = null)
1002
+	{
1003
+		$model = $this->get_model();
1004
+		$field = empty($field_to_order_by) && $model->has_primary_key_field()
1005
+			? $model->get_primary_key_field()->get_name()
1006
+			: $field_to_order_by;
1007
+		$current_value = ! empty($field) ? $this->get($field) : null;
1008
+		if (empty($field) || empty($current_value)) {
1009
+			return array();
1010
+		}
1011
+		return $model->previous($current_value, $field, $query_params, $columns_to_select);
1012
+	}
1013
+
1014
+
1015
+
1016
+	/**
1017
+	 * Overrides parent because parent expects old models.
1018
+	 * This also doesn't do any validation, and won't work for serialized arrays
1019
+	 *
1020
+	 * @param string $field_name
1021
+	 * @param mixed  $field_value_from_db
1022
+	 * @throws \EE_Error
1023
+	 */
1024
+	public function set_from_db($field_name, $field_value_from_db)
1025
+	{
1026
+		$field_obj = $this->get_model()->field_settings_for($field_name);
1027
+		if ($field_obj instanceof EE_Model_Field_Base) {
1028
+			//you would think the DB has no NULLs for non-null label fields right? wrong!
1029
+			//eg, a CPT model object could have an entry in the posts table, but no
1030
+			//entry in the meta table. Meaning that all its columns in the meta table
1031
+			//are null! yikes! so when we find one like that, use defaults for its meta columns
1032
+			if ($field_value_from_db === null) {
1033
+				if ($field_obj->is_nullable()) {
1034
+					//if the field allows nulls, then let it be null
1035
+					$field_value = null;
1036
+				} else {
1037
+					$field_value = $field_obj->get_default_value();
1038
+				}
1039
+			} else {
1040
+				$field_value = $field_obj->prepare_for_set_from_db($field_value_from_db);
1041
+			}
1042
+			$this->_fields[$field_name] = $field_value;
1043
+			$this->_clear_cached_property($field_name);
1044
+		}
1045
+	}
1046
+
1047
+
1048
+
1049
+	/**
1050
+	 * verifies that the specified field is of the correct type
1051
+	 *
1052
+	 * @param string $field_name
1053
+	 * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1054
+	 *                                (in cases where the same property may be used for different outputs
1055
+	 *                                - i.e. datetime, money etc.)
1056
+	 * @return mixed
1057
+	 * @throws \EE_Error
1058
+	 */
1059
+	public function get($field_name, $extra_cache_ref = null)
1060
+	{
1061
+		return $this->_get_cached_property($field_name, false, $extra_cache_ref);
1062
+	}
1063
+
1064
+
1065
+
1066
+	/**
1067
+	 * This method simply returns the RAW unprocessed value for the given property in this class
1068
+	 *
1069
+	 * @param  string $field_name A valid fieldname
1070
+	 * @return mixed              Whatever the raw value stored on the property is.
1071
+	 * @throws EE_Error if fieldSettings is misconfigured or the field doesn't exist.
1072
+	 */
1073
+	public function get_raw($field_name)
1074
+	{
1075
+		$field_settings = $this->get_model()->field_settings_for($field_name);
1076
+		return $field_settings instanceof EE_Datetime_Field && $this->_fields[$field_name] instanceof DateTime
1077
+			? $this->_fields[$field_name]->format('U')
1078
+			: $this->_fields[$field_name];
1079
+	}
1080
+
1081
+
1082
+
1083
+	/**
1084
+	 * This is used to return the internal DateTime object used for a field that is a
1085
+	 * EE_Datetime_Field.
1086
+	 *
1087
+	 * @param string $field_name               The field name retrieving the DateTime object.
1088
+	 * @return mixed null | false | DateTime  If the requested field is NOT a EE_Datetime_Field then
1089
+	 * @throws \EE_Error
1090
+	 *                                         an error is set and false returned.  If the field IS an
1091
+	 *                                         EE_Datetime_Field and but the field value is null, then
1092
+	 *                                         just null is returned (because that indicates that likely
1093
+	 *                                         this field is nullable).
1094
+	 */
1095
+	public function get_DateTime_object($field_name)
1096
+	{
1097
+		$field_settings = $this->get_model()->field_settings_for($field_name);
1098
+		if ( ! $field_settings instanceof EE_Datetime_Field) {
1099
+			EE_Error::add_error(
1100
+				sprintf(
1101
+					__(
1102
+						'The field %s is not an EE_Datetime_Field field.  There is no DateTime object stored on this field type.',
1103
+						'event_espresso'
1104
+					),
1105
+					$field_name
1106
+				),
1107
+				__FILE__,
1108
+				__FUNCTION__,
1109
+				__LINE__
1110
+			);
1111
+			return false;
1112
+		}
1113
+		return $this->_fields[$field_name];
1114
+	}
1115
+
1116
+
1117
+
1118
+	/**
1119
+	 * To be used in template to immediately echo out the value, and format it for output.
1120
+	 * Eg, should call stripslashes and whatnot before echoing
1121
+	 *
1122
+	 * @param string $field_name      the name of the field as it appears in the DB
1123
+	 * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1124
+	 *                                (in cases where the same property may be used for different outputs
1125
+	 *                                - i.e. datetime, money etc.)
1126
+	 * @return void
1127
+	 * @throws \EE_Error
1128
+	 */
1129
+	public function e($field_name, $extra_cache_ref = null)
1130
+	{
1131
+		echo $this->get_pretty($field_name, $extra_cache_ref);
1132
+	}
1133
+
1134
+
1135
+
1136
+	/**
1137
+	 * Exactly like e(), echoes out the field, but sets its schema to 'form_input', so that it
1138
+	 * can be easily used as the value of form input.
1139
+	 *
1140
+	 * @param string $field_name
1141
+	 * @return void
1142
+	 * @throws \EE_Error
1143
+	 */
1144
+	public function f($field_name)
1145
+	{
1146
+		$this->e($field_name, 'form_input');
1147
+	}
1148
+
1149
+
1150
+
1151
+	/**
1152
+	 * Gets a pretty view of the field's value. $extra_cache_ref can specify different formats for this.
1153
+	 * The $extra_cache_ref will be passed to the model field's prepare_for_pretty_echoing, so consult the field's class
1154
+	 * to see what options are available.
1155
+	 * @param string $field_name
1156
+	 * @param string $extra_cache_ref This allows the user to specify an extra cache ref for the given property
1157
+	 *                                (in cases where the same property may be used for different outputs
1158
+	 *                                - i.e. datetime, money etc.)
1159
+	 * @return mixed
1160
+	 * @throws \EE_Error
1161
+	 */
1162
+	public function get_pretty($field_name, $extra_cache_ref = null)
1163
+	{
1164
+		return $this->_get_cached_property($field_name, true, $extra_cache_ref);
1165
+	}
1166
+
1167
+
1168
+
1169
+	/**
1170
+	 * This simply returns the datetime for the given field name
1171
+	 * Note: this protected function is called by the wrapper get_date or get_time or get_datetime functions
1172
+	 * (and the equivalent e_date, e_time, e_datetime).
1173
+	 *
1174
+	 * @access   protected
1175
+	 * @param string   $field_name   Field on the instantiated EE_Base_Class child object
1176
+	 * @param string   $dt_frmt      valid datetime format used for date
1177
+	 *                               (if '' then we just use the default on the field,
1178
+	 *                               if NULL we use the last-used format)
1179
+	 * @param string   $tm_frmt      Same as above except this is for time format
1180
+	 * @param string   $date_or_time if NULL then both are returned, otherwise "D" = only date and "T" = only time.
1181
+	 * @param  boolean $echo         Whether the dtt is echoing using pretty echoing or just returned using vanilla get
1182
+	 * @return string|bool|EE_Error string on success, FALSE on fail, or EE_Error Exception is thrown
1183
+	 *                               if field is not a valid dtt field, or void if echoing
1184
+	 * @throws \EE_Error
1185
+	 */
1186
+	protected function _get_datetime($field_name, $dt_frmt = '', $tm_frmt = '', $date_or_time = '', $echo = false)
1187
+	{
1188
+		// clear cached property
1189
+		$this->_clear_cached_property($field_name);
1190
+		//reset format properties because they are used in get()
1191
+		$this->_dt_frmt = $dt_frmt !== '' ? $dt_frmt : $this->_dt_frmt;
1192
+		$this->_tm_frmt = $tm_frmt !== '' ? $tm_frmt : $this->_tm_frmt;
1193
+		if ($echo) {
1194
+			$this->e($field_name, $date_or_time);
1195
+			return '';
1196
+		}
1197
+		return $this->get($field_name, $date_or_time);
1198
+	}
1199
+
1200
+
1201
+
1202
+	/**
1203
+	 * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the date
1204
+	 * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1205
+	 * other echoes the pretty value for dtt)
1206
+	 *
1207
+	 * @param  string $field_name name of model object datetime field holding the value
1208
+	 * @param  string $format     format for the date returned (if NULL we use default in dt_frmt property)
1209
+	 * @return string            datetime value formatted
1210
+	 * @throws \EE_Error
1211
+	 */
1212
+	public function get_date($field_name, $format = '')
1213
+	{
1214
+		return $this->_get_datetime($field_name, $format, null, 'D');
1215
+	}
1216
+
1217
+
1218
+
1219
+	/**
1220
+	 * @param      $field_name
1221
+	 * @param string $format
1222
+	 * @throws \EE_Error
1223
+	 */
1224
+	public function e_date($field_name, $format = '')
1225
+	{
1226
+		$this->_get_datetime($field_name, $format, null, 'D', true);
1227
+	}
1228
+
1229
+
1230
+
1231
+	/**
1232
+	 * below are wrapper functions for the various datetime outputs that can be obtained for JUST returning the time
1233
+	 * portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1234
+	 * other echoes the pretty value for dtt)
1235
+	 *
1236
+	 * @param  string $field_name name of model object datetime field holding the value
1237
+	 * @param  string $format     format for the time returned ( if NULL we use default in tm_frmt property)
1238
+	 * @return string             datetime value formatted
1239
+	 * @throws \EE_Error
1240
+	 */
1241
+	public function get_time($field_name, $format = '')
1242
+	{
1243
+		return $this->_get_datetime($field_name, null, $format, 'T');
1244
+	}
1245
+
1246
+
1247
+
1248
+	/**
1249
+	 * @param      $field_name
1250
+	 * @param string $format
1251
+	 * @throws \EE_Error
1252
+	 */
1253
+	public function e_time($field_name, $format = '')
1254
+	{
1255
+		$this->_get_datetime($field_name, null, $format, 'T', true);
1256
+	}
1257
+
1258
+
1259
+
1260
+	/**
1261
+	 * below are wrapper functions for the various datetime outputs that can be obtained for returning the date AND
1262
+	 * time portion of a datetime value. (note the only difference between get_ and e_ is one returns the value and the
1263
+	 * other echoes the pretty value for dtt)
1264
+	 *
1265
+	 * @param  string $field_name name of model object datetime field holding the value
1266
+	 * @param  string $dt_frmt    format for the date returned (if NULL we use default in dt_frmt property)
1267
+	 * @param  string $tm_frmt    format for the time returned (if NULL we use default in tm_frmt property)
1268
+	 * @return string             datetime value formatted
1269
+	 * @throws \EE_Error
1270
+	 */
1271
+	public function get_datetime($field_name, $dt_frmt = '', $tm_frmt = '')
1272
+	{
1273
+		return $this->_get_datetime($field_name, $dt_frmt, $tm_frmt);
1274
+	}
1275
+
1276
+
1277
+
1278
+	/**
1279
+	 * @param string $field_name
1280
+	 * @param string $dt_frmt
1281
+	 * @param string $tm_frmt
1282
+	 * @throws \EE_Error
1283
+	 */
1284
+	public function e_datetime($field_name, $dt_frmt = '', $tm_frmt = '')
1285
+	{
1286
+		$this->_get_datetime($field_name, $dt_frmt, $tm_frmt, null, true);
1287
+	}
1288
+
1289
+
1290
+
1291
+	/**
1292
+	 * Get the i8ln value for a date using the WordPress @see date_i18n function.
1293
+	 *
1294
+	 * @param string $field_name The EE_Datetime_Field reference for the date being retrieved.
1295
+	 * @param string $format     PHP valid date/time string format.  If none is provided then the internal set format
1296
+	 *                           on the object will be used.
1297
+	 * @return string Date and time string in set locale or false if no field exists for the given
1298
+	 * @throws \EE_Error
1299
+	 *                           field name.
1300
+	 */
1301
+	public function get_i18n_datetime($field_name, $format = '')
1302
+	{
1303
+		$format = empty($format) ? $this->_dt_frmt . ' ' . $this->_tm_frmt : $format;
1304
+		return date_i18n(
1305
+			$format,
1306
+			EEH_DTT_Helper::get_timestamp_with_offset($this->get_raw($field_name), $this->_timezone)
1307
+		);
1308
+	}
1309
+
1310
+
1311
+
1312
+	/**
1313
+	 * This method validates whether the given field name is a valid field on the model object as well as it is of a
1314
+	 * type EE_Datetime_Field.  On success there will be returned the field settings.  On fail an EE_Error exception is
1315
+	 * thrown.
1316
+	 *
1317
+	 * @param  string $field_name The field name being checked
1318
+	 * @throws EE_Error
1319
+	 * @return EE_Datetime_Field
1320
+	 */
1321
+	protected function _get_dtt_field_settings($field_name)
1322
+	{
1323
+		$field = $this->get_model()->field_settings_for($field_name);
1324
+		//check if field is dtt
1325
+		if ($field instanceof EE_Datetime_Field) {
1326
+			return $field;
1327
+		} else {
1328
+			throw new EE_Error(sprintf(__('The field name "%s" has been requested for the EE_Base_Class datetime functions and it is not a valid EE_Datetime_Field.  Please check the spelling of the field and make sure it has been setup as a EE_Datetime_Field in the %s model constructor',
1329
+				'event_espresso'), $field_name, self::_get_model_classname(get_class($this))));
1330
+		}
1331
+	}
1332
+
1333
+
1334
+
1335
+
1336
+	/**
1337
+	 * NOTE ABOUT BELOW:
1338
+	 * These convenience date and time setters are for setting date and time independently.  In other words you might
1339
+	 * want to change the time on a datetime_field but leave the date the same (or vice versa). IF on the other hand
1340
+	 * you want to set both date and time at the same time, you can just use the models default set($fieldname,$value)
1341
+	 * method and make sure you send the entire datetime value for setting.
1342
+	 */
1343
+	/**
1344
+	 * sets the time on a datetime property
1345
+	 *
1346
+	 * @access protected
1347
+	 * @param string|Datetime $time      a valid time string for php datetime functions (or DateTime object)
1348
+	 * @param string          $fieldname the name of the field the time is being set on (must match a EE_Datetime_Field)
1349
+	 * @throws \EE_Error
1350
+	 */
1351
+	protected function _set_time_for($time, $fieldname)
1352
+	{
1353
+		$this->_set_date_time('T', $time, $fieldname);
1354
+	}
1355
+
1356
+
1357
+
1358
+	/**
1359
+	 * sets the date on a datetime property
1360
+	 *
1361
+	 * @access protected
1362
+	 * @param string|DateTime $date      a valid date string for php datetime functions ( or DateTime object)
1363
+	 * @param string          $fieldname the name of the field the date is being set on (must match a EE_Datetime_Field)
1364
+	 * @throws \EE_Error
1365
+	 */
1366
+	protected function _set_date_for($date, $fieldname)
1367
+	{
1368
+		$this->_set_date_time('D', $date, $fieldname);
1369
+	}
1370
+
1371
+
1372
+
1373
+	/**
1374
+	 * This takes care of setting a date or time independently on a given model object property. This method also
1375
+	 * verifies that the given fieldname matches a model object property and is for a EE_Datetime_Field field
1376
+	 *
1377
+	 * @access protected
1378
+	 * @param string          $what           "T" for time, 'B' for both, 'D' for Date.
1379
+	 * @param string|DateTime $datetime_value A valid Date or Time string (or DateTime object)
1380
+	 * @param string          $fieldname      the name of the field the date OR time is being set on (must match a
1381
+	 *                                        EE_Datetime_Field property)
1382
+	 * @throws \EE_Error
1383
+	 */
1384
+	protected function _set_date_time($what = 'T', $datetime_value, $fieldname)
1385
+	{
1386
+		$field = $this->_get_dtt_field_settings($fieldname);
1387
+		$field->set_timezone($this->_timezone);
1388
+		$field->set_date_format($this->_dt_frmt);
1389
+		$field->set_time_format($this->_tm_frmt);
1390
+		switch ($what) {
1391
+			case 'T' :
1392
+				$this->_fields[$fieldname] = $field->prepare_for_set_with_new_time(
1393
+					$datetime_value,
1394
+					$this->_fields[$fieldname]
1395
+				);
1396
+				break;
1397
+			case 'D' :
1398
+				$this->_fields[$fieldname] = $field->prepare_for_set_with_new_date(
1399
+					$datetime_value,
1400
+					$this->_fields[$fieldname]
1401
+				);
1402
+				break;
1403
+			case 'B' :
1404
+				$this->_fields[$fieldname] = $field->prepare_for_set($datetime_value);
1405
+				break;
1406
+		}
1407
+		$this->_clear_cached_property($fieldname);
1408
+	}
1409
+
1410
+
1411
+
1412
+	/**
1413
+	 * This will return a timestamp for the website timezone but ONLY when the current website timezone is different
1414
+	 * than the timezone set for the website. NOTE, this currently only works well with methods that return values.  If
1415
+	 * you use it with methods that echo values the $_timestamp property may not get reset to its original value and
1416
+	 * that could lead to some unexpected results!
1417
+	 *
1418
+	 * @access public
1419
+	 * @param string               $field_name This is the name of the field on the object that contains the date/time
1420
+	 *                                         value being returned.
1421
+	 * @param string               $callback   must match a valid method in this class (defaults to get_datetime)
1422
+	 * @param mixed (array|string) $args       This is the arguments that will be passed to the callback.
1423
+	 * @param string               $prepend    You can include something to prepend on the timestamp
1424
+	 * @param string               $append     You can include something to append on the timestamp
1425
+	 * @throws EE_Error
1426
+	 * @return string timestamp
1427
+	 */
1428
+	public function display_in_my_timezone(
1429
+		$field_name,
1430
+		$callback = 'get_datetime',
1431
+		$args = null,
1432
+		$prepend = '',
1433
+		$append = ''
1434
+	) {
1435
+		$timezone = EEH_DTT_Helper::get_timezone();
1436
+		if ($timezone === $this->_timezone) {
1437
+			return '';
1438
+		}
1439
+		$original_timezone = $this->_timezone;
1440
+		$this->set_timezone($timezone);
1441
+		$fn = (array)$field_name;
1442
+		$args = array_merge($fn, (array)$args);
1443
+		if ( ! method_exists($this, $callback)) {
1444
+			throw new EE_Error(
1445
+				sprintf(
1446
+					__(
1447
+						'The method named "%s" given as the callback param in "display_in_my_timezone" does not exist.  Please check your spelling',
1448
+						'event_espresso'
1449
+					),
1450
+					$callback
1451
+				)
1452
+			);
1453
+		}
1454
+		$args = (array)$args;
1455
+		$return = $prepend . call_user_func_array(array($this, $callback), $args) . $append;
1456
+		$this->set_timezone($original_timezone);
1457
+		return $return;
1458
+	}
1459
+
1460
+
1461
+
1462
+	/**
1463
+	 * Deletes this model object.
1464
+	 * This calls the `EE_Base_Class::_delete` method.  Child classes wishing to change default behaviour should
1465
+	 * override
1466
+	 * `EE_Base_Class::_delete` NOT this class.
1467
+	 *
1468
+	 * @return boolean | int
1469
+	 * @throws \EE_Error
1470
+	 */
1471
+	public function delete()
1472
+	{
1473
+		/**
1474
+		 * Called just before the `EE_Base_Class::_delete` method call.
1475
+		 * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions
1476
+		 * should be aware that `_delete` may not always result in a permanent delete.  For example, `EE_Soft_Delete_Base_Class::_delete`
1477
+		 * soft deletes (trash) the object and does not permanently delete it.
1478
+		 *
1479
+		 * @param EE_Base_Class $model_object about to be 'deleted'
1480
+		 */
1481
+		do_action('AHEE__EE_Base_Class__delete__before', $this);
1482
+		$result = $this->_delete();
1483
+		/**
1484
+		 * Called just after the `EE_Base_Class::_delete` method call.
1485
+		 * Note: `EE_Base_Class::_delete` might be overridden by child classes so any client code hooking into these actions
1486
+		 * should be aware that `_delete` may not always result in a permanent delete.  For example `EE_Soft_Base_Class::_delete`
1487
+		 * soft deletes (trash) the object and does not permanently delete it.
1488
+		 *
1489
+		 * @param EE_Base_Class $model_object that was just 'deleted'
1490
+		 * @param boolean       $result
1491
+		 */
1492
+		do_action('AHEE__EE_Base_Class__delete__end', $this, $result);
1493
+		return $result;
1494
+	}
1495
+
1496
+
1497
+
1498
+	/**
1499
+	 * Calls the specific delete method for the instantiated class.
1500
+	 * This method is called by the public `EE_Base_Class::delete` method.  Any child classes desiring to override
1501
+	 * default functionality for "delete" (which is to call `permanently_delete`) should override this method NOT
1502
+	 * `EE_Base_Class::delete`
1503
+	 *
1504
+	 * @return bool|int
1505
+	 * @throws \EE_Error
1506
+	 */
1507
+	protected function _delete()
1508
+	{
1509
+		return $this->delete_permanently();
1510
+	}
1511
+
1512
+
1513
+
1514
+	/**
1515
+	 * Deletes this model object permanently from db (but keep in mind related models my block the delete and return an
1516
+	 * error)
1517
+	 *
1518
+	 * @return bool | int
1519
+	 * @throws \EE_Error
1520
+	 */
1521
+	public function delete_permanently()
1522
+	{
1523
+		/**
1524
+		 * Called just before HARD deleting a model object
1525
+		 *
1526
+		 * @param EE_Base_Class $model_object about to be 'deleted'
1527
+		 */
1528
+		do_action('AHEE__EE_Base_Class__delete_permanently__before', $this);
1529
+		$model = $this->get_model();
1530
+		$result = $model->delete_permanently_by_ID($this->ID());
1531
+		$this->refresh_cache_of_related_objects();
1532
+		/**
1533
+		 * Called just after HARD deleting a model object
1534
+		 *
1535
+		 * @param EE_Base_Class $model_object that was just 'deleted'
1536
+		 * @param boolean       $result
1537
+		 */
1538
+		do_action('AHEE__EE_Base_Class__delete_permanently__end', $this, $result);
1539
+		return $result;
1540
+	}
1541
+
1542
+
1543
+
1544
+	/**
1545
+	 * When this model object is deleted, it may still be cached on related model objects. This clears the cache of
1546
+	 * related model objects
1547
+	 *
1548
+	 * @throws \EE_Error
1549
+	 */
1550
+	public function refresh_cache_of_related_objects()
1551
+	{
1552
+		$model = $this->get_model();
1553
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1554
+			if ( ! empty($this->_model_relations[$relation_name])) {
1555
+				$related_objects = $this->_model_relations[$relation_name];
1556
+				if ($relation_obj instanceof EE_Belongs_To_Relation) {
1557
+					//this relation only stores a single model object, not an array
1558
+					//but let's make it consistent
1559
+					$related_objects = array($related_objects);
1560
+				}
1561
+				foreach ($related_objects as $related_object) {
1562
+					//only refresh their cache if they're in memory
1563
+					if ($related_object instanceof EE_Base_Class) {
1564
+						$related_object->clear_cache($model->get_this_model_name(), $this);
1565
+					}
1566
+				}
1567
+			}
1568
+		}
1569
+	}
1570
+
1571
+
1572
+
1573
+	/**
1574
+	 *        Saves this object to the database. An array may be supplied to set some values on this
1575
+	 * object just before saving.
1576
+	 *
1577
+	 * @access public
1578
+	 * @param array $set_cols_n_values keys are field names, values are their new values,
1579
+	 *                                 if provided during the save() method (often client code will change the fields'
1580
+	 *                                 values before calling save)
1581
+	 * @throws \EE_Error
1582
+	 * @return int , 1 on a successful update, the ID of the new entry on insert; 0 on failure or if the model object
1583
+	 *                                 isn't allowed to persist (as determined by EE_Base_Class::allow_persist())
1584
+	 */
1585
+	public function save($set_cols_n_values = array())
1586
+	{
1587
+		$model = $this->get_model();
1588
+		/**
1589
+		 * Filters the fields we're about to save on the model object
1590
+		 *
1591
+		 * @param array         $set_cols_n_values
1592
+		 * @param EE_Base_Class $model_object
1593
+		 */
1594
+		$set_cols_n_values = (array)apply_filters('FHEE__EE_Base_Class__save__set_cols_n_values', $set_cols_n_values,
1595
+			$this);
1596
+		//set attributes as provided in $set_cols_n_values
1597
+		foreach ($set_cols_n_values as $column => $value) {
1598
+			$this->set($column, $value);
1599
+		}
1600
+		// no changes ? then don't do anything
1601
+		if (! $this->_has_changes && $this->ID() && $model->get_primary_key_field()->is_auto_increment()) {
1602
+			return 0;
1603
+		}
1604
+		/**
1605
+		 * Saving a model object.
1606
+		 * Before we perform a save, this action is fired.
1607
+		 *
1608
+		 * @param EE_Base_Class $model_object the model object about to be saved.
1609
+		 */
1610
+		do_action('AHEE__EE_Base_Class__save__begin', $this);
1611
+		if ( ! $this->allow_persist()) {
1612
+			return 0;
1613
+		}
1614
+		//now get current attribute values
1615
+		$save_cols_n_values = $this->_fields;
1616
+		//if the object already has an ID, update it. Otherwise, insert it
1617
+		//also: change the assumption about values passed to the model NOT being prepare dby the model object. They have been
1618
+		$old_assumption_concerning_value_preparation = $model
1619
+															->get_assumption_concerning_values_already_prepared_by_model_object();
1620
+		$model->assume_values_already_prepared_by_model_object(true);
1621
+		//does this model have an autoincrement PK?
1622
+		if ($model->has_primary_key_field()) {
1623
+			if ($model->get_primary_key_field()->is_auto_increment()) {
1624
+				//ok check if it's set, if so: update; if not, insert
1625
+				if ( ! empty($save_cols_n_values[$model->primary_key_name()])) {
1626
+					$results = $model->update_by_ID($save_cols_n_values, $this->ID());
1627
+				} else {
1628
+					unset($save_cols_n_values[$model->primary_key_name()]);
1629
+					$results = $model->insert($save_cols_n_values);
1630
+					if ($results) {
1631
+						//if successful, set the primary key
1632
+						//but don't use the normal SET method, because it will check if
1633
+						//an item with the same ID exists in the mapper & db, then
1634
+						//will find it in the db (because we just added it) and THAT object
1635
+						//will get added to the mapper before we can add this one!
1636
+						//but if we just avoid using the SET method, all that headache can be avoided
1637
+						$pk_field_name = $model->primary_key_name();
1638
+						$this->_fields[$pk_field_name] = $results;
1639
+						$this->_clear_cached_property($pk_field_name);
1640
+						$model->add_to_entity_map($this);
1641
+						$this->_update_cached_related_model_objs_fks();
1642
+					}
1643
+				}
1644
+			} else {//PK is NOT auto-increment
1645
+				//so check if one like it already exists in the db
1646
+				if ($model->exists_by_ID($this->ID())) {
1647
+					if (WP_DEBUG && ! $this->in_entity_map()) {
1648
+						throw new EE_Error(
1649
+							sprintf(
1650
+								__('Using a model object %1$s that is NOT in the entity map, can lead to unexpected errors. You should either: %4$s 1. Put it in the entity mapper by calling %2$s %4$s 2. Discard this model object and use what is in the entity mapper %4$s 3. Fetch from the database using %3$s',
1651
+									'event_espresso'),
1652
+								get_class($this),
1653
+								get_class($model) . '::instance()->add_to_entity_map()',
1654
+								get_class($model) . '::instance()->get_one_by_ID()',
1655
+								'<br />'
1656
+							)
1657
+						);
1658
+					}
1659
+					$results = $model->update_by_ID($save_cols_n_values, $this->ID());
1660
+				} else {
1661
+					$results = $model->insert($save_cols_n_values);
1662
+					$this->_update_cached_related_model_objs_fks();
1663
+				}
1664
+			}
1665
+		} else {//there is NO primary key
1666
+			$already_in_db = false;
1667
+			foreach ($model->unique_indexes() as $index) {
1668
+				$uniqueness_where_params = array_intersect_key($save_cols_n_values, $index->fields());
1669
+				if ($model->exists(array($uniqueness_where_params))) {
1670
+					$already_in_db = true;
1671
+				}
1672
+			}
1673
+			if ($already_in_db) {
1674
+				$combined_pk_fields_n_values = array_intersect_key($save_cols_n_values,
1675
+					$model->get_combined_primary_key_fields());
1676
+				$results = $model->update($save_cols_n_values, $combined_pk_fields_n_values);
1677
+			} else {
1678
+				$results = $model->insert($save_cols_n_values);
1679
+			}
1680
+		}
1681
+		//restore the old assumption about values being prepared by the model object
1682
+		$model
1683
+			 ->assume_values_already_prepared_by_model_object($old_assumption_concerning_value_preparation);
1684
+		/**
1685
+		 * After saving the model object this action is called
1686
+		 *
1687
+		 * @param EE_Base_Class $model_object which was just saved
1688
+		 * @param boolean|int   $results      if it were updated, TRUE or FALSE; if it were newly inserted
1689
+		 *                                    the new ID (or 0 if an error occurred and it wasn't updated)
1690
+		 */
1691
+		do_action('AHEE__EE_Base_Class__save__end', $this, $results);
1692
+		$this->_has_changes = false;
1693
+		return $results;
1694
+	}
1695
+
1696
+
1697
+
1698
+	/**
1699
+	 * Updates the foreign key on related models objects pointing to this to have this model object's ID
1700
+	 * as their foreign key.  If the cached related model objects already exist in the db, saves them (so that the DB
1701
+	 * is consistent) Especially useful in case we JUST added this model object ot the database and we want to let its
1702
+	 * cached relations with foreign keys to it know about that change. Eg: we've created a transaction but haven't
1703
+	 * saved it to the db. We also create a registration and don't save it to the DB, but we DO cache it on the
1704
+	 * transaction. Now, when we save the transaction, the registration's TXN_ID will be automatically updated, whether
1705
+	 * or not they exist in the DB (if they do, their DB records will be automatically updated)
1706
+	 *
1707
+	 * @return void
1708
+	 * @throws \EE_Error
1709
+	 */
1710
+	protected function _update_cached_related_model_objs_fks()
1711
+	{
1712
+		$model = $this->get_model();
1713
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
1714
+			if ($relation_obj instanceof EE_Has_Many_Relation) {
1715
+				foreach ($this->get_all_from_cache($relation_name) as $related_model_obj_in_cache) {
1716
+					$fk_to_this = $related_model_obj_in_cache->get_model()->get_foreign_key_to(
1717
+						$model->get_this_model_name()
1718
+					);
1719
+					$related_model_obj_in_cache->set($fk_to_this->get_name(), $this->ID());
1720
+					if ($related_model_obj_in_cache->ID()) {
1721
+						$related_model_obj_in_cache->save();
1722
+					}
1723
+				}
1724
+			}
1725
+		}
1726
+	}
1727
+
1728
+
1729
+
1730
+	/**
1731
+	 * Saves this model object and its NEW cached relations to the database.
1732
+	 * (Meaning, for now, IT DOES NOT WORK if the cached items already exist in the DB.
1733
+	 * In order for that to work, we would need to mark model objects as dirty/clean...
1734
+	 * because otherwise, there's a potential for infinite looping of saving
1735
+	 * Saves the cached related model objects, and ensures the relation between them
1736
+	 * and this object and properly setup
1737
+	 *
1738
+	 * @return int ID of new model object on save; 0 on failure+
1739
+	 * @throws \EE_Error
1740
+	 */
1741
+	public function save_new_cached_related_model_objs()
1742
+	{
1743
+		//make sure this has been saved
1744
+		if ( ! $this->ID()) {
1745
+			$id = $this->save();
1746
+		} else {
1747
+			$id = $this->ID();
1748
+		}
1749
+		//now save all the NEW cached model objects  (ie they don't exist in the DB)
1750
+		foreach ($this->get_model()->relation_settings() as $relationName => $relationObj) {
1751
+			if ($this->_model_relations[$relationName]) {
1752
+				//is this a relation where we should expect just ONE related object (ie, EE_Belongs_To_relation)
1753
+				//or MANY related objects (ie, EE_HABTM_Relation or EE_Has_Many_Relation)?
1754
+				if ($relationObj instanceof EE_Belongs_To_Relation) {
1755
+					//add a relation to that relation type (which saves the appropriate thing in the process)
1756
+					//but ONLY if it DOES NOT exist in the DB
1757
+					/* @var $related_model_obj EE_Base_Class */
1758
+					$related_model_obj = $this->_model_relations[$relationName];
1759
+					//					if( ! $related_model_obj->ID()){
1760
+					$this->_add_relation_to($related_model_obj, $relationName);
1761
+					$related_model_obj->save_new_cached_related_model_objs();
1762
+					//					}
1763
+				} else {
1764
+					foreach ($this->_model_relations[$relationName] as $related_model_obj) {
1765
+						//add a relation to that relation type (which saves the appropriate thing in the process)
1766
+						//but ONLY if it DOES NOT exist in the DB
1767
+						//						if( ! $related_model_obj->ID()){
1768
+						$this->_add_relation_to($related_model_obj, $relationName);
1769
+						$related_model_obj->save_new_cached_related_model_objs();
1770
+						//						}
1771
+					}
1772
+				}
1773
+			}
1774
+		}
1775
+		return $id;
1776
+	}
1777
+
1778
+
1779
+
1780
+	/**
1781
+	 * for getting a model while instantiated.
1782
+	 *
1783
+	 * @return \EEM_Base | \EEM_CPT_Base
1784
+	 */
1785
+	public function get_model()
1786
+	{
1787
+		if( ! $this->_model){
1788
+			$modelName = self::_get_model_classname(get_class($this));
1789
+			$this->_model = self::_get_model_instance_with_name($modelName, $this->_timezone);
1790
+		} else {
1791
+			$this->_model->set_timezone($this->_timezone);
1792
+		}
1793
+
1794
+		return $this->_model;
1795
+	}
1796
+
1797
+
1798
+
1799
+	/**
1800
+	 * @param $props_n_values
1801
+	 * @param $classname
1802
+	 * @return mixed bool|EE_Base_Class|EEM_CPT_Base
1803
+	 * @throws \EE_Error
1804
+	 */
1805
+	protected static function _get_object_from_entity_mapper($props_n_values, $classname)
1806
+	{
1807
+		//TODO: will not work for Term_Relationships because they have no PK!
1808
+		$primary_id_ref = self::_get_primary_key_name($classname);
1809
+		if (array_key_exists($primary_id_ref, $props_n_values) && ! empty($props_n_values[$primary_id_ref])) {
1810
+			$id = $props_n_values[$primary_id_ref];
1811
+			return self::_get_model($classname)->get_from_entity_map($id);
1812
+		}
1813
+		return false;
1814
+	}
1815
+
1816
+
1817
+
1818
+	/**
1819
+	 * This is called by child static "new_instance" method and we'll check to see if there is an existing db entry for
1820
+	 * the primary key (if present in incoming values). If there is a key in the incoming array that matches the
1821
+	 * primary key for the model AND it is not null, then we check the db. If there's a an object we return it.  If not
1822
+	 * we return false.
1823
+	 *
1824
+	 * @param  array  $props_n_values   incoming array of properties and their values
1825
+	 * @param  string $classname        the classname of the child class
1826
+	 * @param null    $timezone
1827
+	 * @param array   $date_formats     incoming date_formats in an array where the first value is the
1828
+	 *                                  date_format and the second value is the time format
1829
+	 * @return mixed (EE_Base_Class|bool)
1830
+	 * @throws \EE_Error
1831
+	 */
1832
+	protected static function _check_for_object($props_n_values, $classname, $timezone = null, $date_formats = array())
1833
+	{
1834
+		$existing = null;
1835
+		$model = self::_get_model($classname, $timezone);
1836
+		if ($model->has_primary_key_field()) {
1837
+			$primary_id_ref = self::_get_primary_key_name($classname);
1838
+			if (array_key_exists($primary_id_ref, $props_n_values)
1839
+				&& ! empty($props_n_values[$primary_id_ref])
1840
+			) {
1841
+				$existing = $model->get_one_by_ID(
1842
+					$props_n_values[$primary_id_ref]
1843
+				);
1844
+			}
1845
+		} elseif ($model->has_all_combined_primary_key_fields($props_n_values)) {
1846
+			//no primary key on this model, but there's still a matching item in the DB
1847
+			$existing = self::_get_model($classname, $timezone)->get_one_by_ID(
1848
+				self::_get_model($classname, $timezone)->get_index_primary_key_string($props_n_values)
1849
+			);
1850
+		}
1851
+		if ($existing) {
1852
+			//set date formats if present before setting values
1853
+			if ( ! empty($date_formats) && is_array($date_formats)) {
1854
+				$existing->set_date_format($date_formats[0]);
1855
+				$existing->set_time_format($date_formats[1]);
1856
+			} else {
1857
+				//set default formats for date and time
1858
+				$existing->set_date_format(get_option('date_format'));
1859
+				$existing->set_time_format(get_option('time_format'));
1860
+			}
1861
+			foreach ($props_n_values as $property => $field_value) {
1862
+				$existing->set($property, $field_value);
1863
+			}
1864
+			return $existing;
1865
+		} else {
1866
+			return false;
1867
+		}
1868
+	}
1869
+
1870
+
1871
+
1872
+	/**
1873
+	 * Gets the EEM_*_Model for this class
1874
+	 *
1875
+	 * @access public now, as this is more convenient
1876
+	 * @param      $classname
1877
+	 * @param null $timezone
1878
+	 * @throws EE_Error
1879
+	 * @return EEM_Base
1880
+	 */
1881
+	protected static function _get_model($classname, $timezone = null)
1882
+	{
1883
+		//find model for this class
1884
+		if ( ! $classname) {
1885
+			throw new EE_Error(
1886
+				sprintf(
1887
+					__(
1888
+						"What were you thinking calling _get_model(%s)?? You need to specify the class name",
1889
+						"event_espresso"
1890
+					),
1891
+					$classname
1892
+				)
1893
+			);
1894
+		}
1895
+		$modelName = self::_get_model_classname($classname);
1896
+		return self::_get_model_instance_with_name($modelName, $timezone);
1897
+	}
1898
+
1899
+
1900
+
1901
+	/**
1902
+	 * Gets the model instance (eg instance of EEM_Attendee) given its classname (eg EE_Attendee)
1903
+	 *
1904
+	 * @param string $model_classname
1905
+	 * @param null   $timezone
1906
+	 * @return EEM_Base
1907
+	 */
1908
+	protected static function _get_model_instance_with_name($model_classname, $timezone = null)
1909
+	{
1910
+		$model_classname = str_replace('EEM_', '', $model_classname);
1911
+		$model = EE_Registry::instance()->load_model($model_classname);
1912
+		$model->set_timezone($timezone);
1913
+		return $model;
1914
+	}
1915
+
1916
+
1917
+
1918
+	/**
1919
+	 * If a model name is provided (eg Registration), gets the model classname for that model.
1920
+	 * Also works if a model class's classname is provided (eg EE_Registration).
1921
+	 *
1922
+	 * @param null $model_name
1923
+	 * @return string like EEM_Attendee
1924
+	 */
1925
+	private static function _get_model_classname($model_name = null)
1926
+	{
1927
+		if (strpos($model_name, "EE_") === 0) {
1928
+			$model_classname = str_replace("EE_", "EEM_", $model_name);
1929
+		} else {
1930
+			$model_classname = "EEM_" . $model_name;
1931
+		}
1932
+		return $model_classname;
1933
+	}
1934
+
1935
+
1936
+
1937
+	/**
1938
+	 * returns the name of the primary key attribute
1939
+	 *
1940
+	 * @param null $classname
1941
+	 * @throws EE_Error
1942
+	 * @return string
1943
+	 */
1944
+	protected static function _get_primary_key_name($classname = null)
1945
+	{
1946
+		if ( ! $classname) {
1947
+			throw new EE_Error(
1948
+				sprintf(
1949
+					__("What were you thinking calling _get_primary_key_name(%s)", "event_espresso"),
1950
+					$classname
1951
+				)
1952
+			);
1953
+		}
1954
+		return self::_get_model($classname)->get_primary_key_field()->get_name();
1955
+	}
1956
+
1957
+
1958
+
1959
+	/**
1960
+	 * Gets the value of the primary key.
1961
+	 * If the object hasn't yet been saved, it should be whatever the model field's default was
1962
+	 * (eg, if this were the EE_Event class, look at the primary key field on EEM_Event and see what its default value
1963
+	 * is. Usually defaults for integer primary keys are 0; string primary keys are usually NULL).
1964
+	 *
1965
+	 * @return mixed, if the primary key is of type INT it'll be an int. Otherwise it could be a string
1966
+	 * @throws \EE_Error
1967
+	 */
1968
+	public function ID()
1969
+	{
1970
+		$model = $this->get_model();
1971
+		//now that we know the name of the variable, use a variable variable to get its value and return its
1972
+		if ($model->has_primary_key_field()) {
1973
+			return $this->_fields[$model->primary_key_name()];
1974
+		} else {
1975
+			return $model->get_index_primary_key_string($this->_fields);
1976
+		}
1977
+	}
1978
+
1979
+
1980
+
1981
+	/**
1982
+	 * Adds a relationship to the specified EE_Base_Class object, given the relationship's name. Eg, if the current
1983
+	 * model is related to a group of events, the $relationName should be 'Event', and should be a key in the EE
1984
+	 * Model's $_model_relations array. If this model object doesn't exist in the DB, just caches the related thing
1985
+	 *
1986
+	 * @param mixed  $otherObjectModelObjectOrID       EE_Base_Class or the ID of the other object
1987
+	 * @param string $relationName                     eg 'Events','Question',etc.
1988
+	 *                                                 an attendee to a group, you also want to specify which role they
1989
+	 *                                                 will have in that group. So you would use this parameter to
1990
+	 *                                                 specify array('role-column-name'=>'role-id')
1991
+	 * @param array  $extra_join_model_fields_n_values You can optionally include an array of key=>value pairs that
1992
+	 *                                                 allow you to further constrict the relation to being added.
1993
+	 *                                                 However, keep in mind that the columns (keys) given must match a
1994
+	 *                                                 column on the JOIN table and currently only the HABTM models
1995
+	 *                                                 accept these additional conditions.  Also remember that if an
1996
+	 *                                                 exact match isn't found for these extra cols/val pairs, then a
1997
+	 *                                                 NEW row is created in the join table.
1998
+	 * @param null   $cache_id
1999
+	 * @throws EE_Error
2000
+	 * @return EE_Base_Class the object the relation was added to
2001
+	 */
2002
+	public function _add_relation_to(
2003
+		$otherObjectModelObjectOrID,
2004
+		$relationName,
2005
+		$extra_join_model_fields_n_values = array(),
2006
+		$cache_id = null
2007
+	) {
2008
+		$model = $this->get_model();
2009
+		//if this thing exists in the DB, save the relation to the DB
2010
+		if ($this->ID()) {
2011
+			$otherObject = $model
2012
+								->add_relationship_to($this, $otherObjectModelObjectOrID, $relationName,
2013
+									$extra_join_model_fields_n_values);
2014
+			//clear cache so future get_many_related and get_first_related() return new results.
2015
+			$this->clear_cache($relationName, $otherObject, true);
2016
+			if ($otherObject instanceof EE_Base_Class) {
2017
+				$otherObject->clear_cache($model->get_this_model_name(), $this);
2018
+			}
2019
+		} else {
2020
+			//this thing doesn't exist in the DB,  so just cache it
2021
+			if ( ! $otherObjectModelObjectOrID instanceof EE_Base_Class) {
2022
+				throw new EE_Error(sprintf(
2023
+					__('Before a model object is saved to the database, calls to _add_relation_to must be passed an actual object, not just an ID. You provided %s as the model object to a %s',
2024
+						'event_espresso'),
2025
+					$otherObjectModelObjectOrID,
2026
+					get_class($this)
2027
+				));
2028
+			} else {
2029
+				$otherObject = $otherObjectModelObjectOrID;
2030
+			}
2031
+			$this->cache($relationName, $otherObjectModelObjectOrID, $cache_id);
2032
+		}
2033
+		if ($otherObject instanceof EE_Base_Class) {
2034
+			//fix the reciprocal relation too
2035
+			if ($otherObject->ID()) {
2036
+				//its saved so assumed relations exist in the DB, so we can just
2037
+				//clear the cache so future queries use the updated info in the DB
2038
+				$otherObject->clear_cache($model->get_this_model_name(), null, true);
2039
+			} else {
2040
+				//it's not saved, so it caches relations like this
2041
+				$otherObject->cache($model->get_this_model_name(), $this);
2042
+			}
2043
+		}
2044
+		return $otherObject;
2045
+	}
2046
+
2047
+
2048
+
2049
+	/**
2050
+	 * Removes a relationship to the specified EE_Base_Class object, given the relationships' name. Eg, if the current
2051
+	 * model is related to a group of events, the $relationName should be 'Events', and should be a key in the EE
2052
+	 * Model's $_model_relations array. If this model object doesn't exist in the DB, just removes the related thing
2053
+	 * from the cache
2054
+	 *
2055
+	 * @param mixed  $otherObjectModelObjectOrID
2056
+	 *                EE_Base_Class or the ID of the other object, OR an array key into the cache if this isn't saved
2057
+	 *                to the DB yet
2058
+	 * @param string $relationName
2059
+	 * @param array  $where_query
2060
+	 *                You can optionally include an array of key=>value pairs that allow you to further constrict the
2061
+	 *                relation to being added. However, keep in mind that the columns (keys) given must match a column
2062
+	 *                on the JOIN table and currently only the HABTM models accept these additional conditions. Also
2063
+	 *                remember that if an exact match isn't found for these extra cols/val pairs, then a NEW row is
2064
+	 *                created in the join table.
2065
+	 * @return EE_Base_Class the relation was removed from
2066
+	 * @throws \EE_Error
2067
+	 */
2068
+	public function _remove_relation_to($otherObjectModelObjectOrID, $relationName, $where_query = array())
2069
+	{
2070
+		if ($this->ID()) {
2071
+			//if this exists in the DB, save the relation change to the DB too
2072
+			$otherObject = $this->get_model()
2073
+								->remove_relationship_to($this, $otherObjectModelObjectOrID, $relationName,
2074
+									$where_query);
2075
+			$this->clear_cache($relationName, $otherObject);
2076
+		} else {
2077
+			//this doesn't exist in the DB, just remove it from the cache
2078
+			$otherObject = $this->clear_cache($relationName, $otherObjectModelObjectOrID);
2079
+		}
2080
+		if ($otherObject instanceof EE_Base_Class) {
2081
+			$otherObject->clear_cache($this->get_model()->get_this_model_name(), $this);
2082
+		}
2083
+		return $otherObject;
2084
+	}
2085
+
2086
+
2087
+
2088
+	/**
2089
+	 * Removes ALL the related things for the $relationName.
2090
+	 *
2091
+	 * @param string $relationName
2092
+	 * @param array  $where_query_params like EEM_Base::get_all's $query_params[0] (where conditions)
2093
+	 * @return EE_Base_Class
2094
+	 * @throws \EE_Error
2095
+	 */
2096
+	public function _remove_relations($relationName, $where_query_params = array())
2097
+	{
2098
+		if ($this->ID()) {
2099
+			//if this exists in the DB, save the relation change to the DB too
2100
+			$otherObjects = $this->get_model()->remove_relations($this, $relationName, $where_query_params);
2101
+			$this->clear_cache($relationName, null, true);
2102
+		} else {
2103
+			//this doesn't exist in the DB, just remove it from the cache
2104
+			$otherObjects = $this->clear_cache($relationName, null, true);
2105
+		}
2106
+		if (is_array($otherObjects)) {
2107
+			foreach ($otherObjects as $otherObject) {
2108
+				$otherObject->clear_cache($this->get_model()->get_this_model_name(), $this);
2109
+			}
2110
+		}
2111
+		return $otherObjects;
2112
+	}
2113
+
2114
+
2115
+
2116
+	/**
2117
+	 * Gets all the related model objects of the specified type. Eg, if the current class if
2118
+	 * EE_Event, you could call $this->get_many_related('Registration') to get an array of all the
2119
+	 * EE_Registration objects which related to this event. Note: by default, we remove the "default query params"
2120
+	 * because we want to get even deleted items etc.
2121
+	 *
2122
+	 * @param string $relationName key in the model's _model_relations array
2123
+	 * @param array  $query_params like EEM_Base::get_all
2124
+	 * @return EE_Base_Class[] Results not necessarily indexed by IDs, because some results might not have primary keys
2125
+	 * @throws \EE_Error
2126
+	 *                             or might not be saved yet. Consider using EEM_Base::get_IDs() on these results if
2127
+	 *                             you want IDs
2128
+	 */
2129
+	public function get_many_related($relationName, $query_params = array())
2130
+	{
2131
+		if ($this->ID()) {
2132
+			//this exists in the DB, so get the related things from either the cache or the DB
2133
+			//if there are query parameters, forget about caching the related model objects.
2134
+			if ($query_params) {
2135
+				$related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params);
2136
+			} else {
2137
+				//did we already cache the result of this query?
2138
+				$cached_results = $this->get_all_from_cache($relationName);
2139
+				if ( ! $cached_results) {
2140
+					$related_model_objects = $this->get_model()->get_all_related($this, $relationName, $query_params);
2141
+					//if no query parameters were passed, then we got all the related model objects
2142
+					//for that relation. We can cache them then.
2143
+					foreach ($related_model_objects as $related_model_object) {
2144
+						$this->cache($relationName, $related_model_object);
2145
+					}
2146
+				} else {
2147
+					$related_model_objects = $cached_results;
2148
+				}
2149
+			}
2150
+		} else {
2151
+			//this doesn't exist in the DB, so just get the related things from the cache
2152
+			$related_model_objects = $this->get_all_from_cache($relationName);
2153
+		}
2154
+		return $related_model_objects;
2155
+	}
2156
+
2157
+
2158
+
2159
+	/**
2160
+	 * Instead of getting the related model objects, simply counts them. Ignores default_where_conditions by default,
2161
+	 * unless otherwise specified in the $query_params
2162
+	 *
2163
+	 * @param string $relation_name  model_name like 'Event', or 'Registration'
2164
+	 * @param array  $query_params   like EEM_Base::get_all's
2165
+	 * @param string $field_to_count name of field to count by. By default, uses primary key
2166
+	 * @param bool   $distinct       if we want to only count the distinct values for the column then you can trigger
2167
+	 *                               that by the setting $distinct to TRUE;
2168
+	 * @return int
2169
+	 */
2170
+	public function count_related($relation_name, $query_params = array(), $field_to_count = null, $distinct = false)
2171
+	{
2172
+		return $this->get_model()->count_related($this, $relation_name, $query_params, $field_to_count, $distinct);
2173
+	}
2174
+
2175
+
2176
+
2177
+	/**
2178
+	 * Instead of getting the related model objects, simply sums up the values of the specified field.
2179
+	 * Note: ignores default_where_conditions by default, unless otherwise specified in the $query_params
2180
+	 *
2181
+	 * @param string $relation_name model_name like 'Event', or 'Registration'
2182
+	 * @param array  $query_params  like EEM_Base::get_all's
2183
+	 * @param string $field_to_sum  name of field to count by.
2184
+	 *                              By default, uses primary key (which doesn't make much sense, so you should probably
2185
+	 *                              change it)
2186
+	 * @return int
2187
+	 */
2188
+	public function sum_related($relation_name, $query_params = array(), $field_to_sum = null)
2189
+	{
2190
+		return $this->get_model()->sum_related($this, $relation_name, $query_params, $field_to_sum);
2191
+	}
2192
+
2193
+
2194
+
2195
+	/**
2196
+	 * Gets the first (ie, one) related model object of the specified type.
2197
+	 *
2198
+	 * @param string $relationName key in the model's _model_relations array
2199
+	 * @param array  $query_params like EEM_Base::get_all
2200
+	 * @return EE_Base_Class (not an array, a single object)
2201
+	 * @throws \EE_Error
2202
+	 */
2203
+	public function get_first_related($relationName, $query_params = array())
2204
+	{
2205
+		$model = $this->get_model();
2206
+		if ($this->ID()) {//this exists in the DB, get from the cache OR the DB
2207
+			//if they've provided some query parameters, don't bother trying to cache the result
2208
+			//also make sure we're not caching the result of get_first_related
2209
+			//on a relation which should have an array of objects (because the cache might have an array of objects)
2210
+			if ($query_params
2211
+				|| ! $model->related_settings_for($relationName)
2212
+					 instanceof
2213
+					 EE_Belongs_To_Relation
2214
+			) {
2215
+				$related_model_object = $model->get_first_related($this, $relationName, $query_params);
2216
+			} else {
2217
+				//first, check if we've already cached the result of this query
2218
+				$cached_result = $this->get_one_from_cache($relationName);
2219
+				if ( ! $cached_result) {
2220
+					$related_model_object = $model->get_first_related($this, $relationName, $query_params);
2221
+					$this->cache($relationName, $related_model_object);
2222
+				} else {
2223
+					$related_model_object = $cached_result;
2224
+				}
2225
+			}
2226
+		} else {
2227
+			$related_model_object = null;
2228
+			//this doesn't exist in the Db, but maybe the relation is of type belongs to, and so the related thing might
2229
+			if ($model->related_settings_for($relationName) instanceof EE_Belongs_To_Relation) {
2230
+				$related_model_object = $model->get_first_related($this, $relationName, $query_params);
2231
+			}
2232
+			//this doesn't exist in the DB and apparently the thing it belongs to doesn't either, just get what's cached on this object
2233
+			if ( ! $related_model_object) {
2234
+				$related_model_object = $this->get_one_from_cache($relationName);
2235
+			}
2236
+		}
2237
+		return $related_model_object;
2238
+	}
2239
+
2240
+
2241
+
2242
+	/**
2243
+	 * Does a delete on all related objects of type $relationName and removes
2244
+	 * the current model object's relation to them. If they can't be deleted (because
2245
+	 * of blocking related model objects) does nothing. If the related model objects are
2246
+	 * soft-deletable, they will be soft-deleted regardless of related blocking model objects.
2247
+	 * If this model object doesn't exist yet in the DB, just removes its related things
2248
+	 *
2249
+	 * @param string $relationName
2250
+	 * @param array  $query_params like EEM_Base::get_all's
2251
+	 * @return int how many deleted
2252
+	 * @throws \EE_Error
2253
+	 */
2254
+	public function delete_related($relationName, $query_params = array())
2255
+	{
2256
+		if ($this->ID()) {
2257
+			$count = $this->get_model()->delete_related($this, $relationName, $query_params);
2258
+		} else {
2259
+			$count = count($this->get_all_from_cache($relationName));
2260
+			$this->clear_cache($relationName, null, true);
2261
+		}
2262
+		return $count;
2263
+	}
2264
+
2265
+
2266
+
2267
+	/**
2268
+	 * Does a hard delete (ie, removes the DB row) on all related objects of type $relationName and removes
2269
+	 * the current model object's relation to them. If they can't be deleted (because
2270
+	 * of blocking related model objects) just does a soft delete on it instead, if possible.
2271
+	 * If the related thing isn't a soft-deletable model object, this function is identical
2272
+	 * to delete_related(). If this model object doesn't exist in the DB, just remove its related things
2273
+	 *
2274
+	 * @param string $relationName
2275
+	 * @param array  $query_params like EEM_Base::get_all's
2276
+	 * @return int how many deleted (including those soft deleted)
2277
+	 * @throws \EE_Error
2278
+	 */
2279
+	public function delete_related_permanently($relationName, $query_params = array())
2280
+	{
2281
+		if ($this->ID()) {
2282
+			$count = $this->get_model()->delete_related_permanently($this, $relationName, $query_params);
2283
+		} else {
2284
+			$count = count($this->get_all_from_cache($relationName));
2285
+		}
2286
+		$this->clear_cache($relationName, null, true);
2287
+		return $count;
2288
+	}
2289
+
2290
+
2291
+
2292
+	/**
2293
+	 * is_set
2294
+	 * Just a simple utility function children can use for checking if property exists
2295
+	 *
2296
+	 * @access  public
2297
+	 * @param  string $field_name property to check
2298
+	 * @return bool                              TRUE if existing,FALSE if not.
2299
+	 */
2300
+	public function is_set($field_name)
2301
+	{
2302
+		return isset($this->_fields[$field_name]);
2303
+	}
2304
+
2305
+
2306
+
2307
+	/**
2308
+	 * Just a simple utility function children can use for checking if property (or properties) exists and throwing an
2309
+	 * EE_Error exception if they don't
2310
+	 *
2311
+	 * @param  mixed (string|array) $properties properties to check
2312
+	 * @throws EE_Error
2313
+	 * @return bool                              TRUE if existing, throw EE_Error if not.
2314
+	 */
2315
+	protected function _property_exists($properties)
2316
+	{
2317
+		foreach ((array)$properties as $property_name) {
2318
+			//first make sure this property exists
2319
+			if ( ! $this->_fields[$property_name]) {
2320
+				throw new EE_Error(
2321
+					sprintf(
2322
+						__(
2323
+							'Trying to retrieve a non-existent property (%s).  Double check the spelling please',
2324
+							'event_espresso'
2325
+						),
2326
+						$property_name
2327
+					)
2328
+				);
2329
+			}
2330
+		}
2331
+		return true;
2332
+	}
2333
+
2334
+
2335
+
2336
+	/**
2337
+	 * This simply returns an array of model fields for this object
2338
+	 *
2339
+	 * @return array
2340
+	 * @throws \EE_Error
2341
+	 */
2342
+	public function model_field_array()
2343
+	{
2344
+		$fields = $this->get_model()->field_settings(false);
2345
+		$properties = array();
2346
+		//remove prepended underscore
2347
+		foreach ($fields as $field_name => $settings) {
2348
+			$properties[$field_name] = $this->get($field_name);
2349
+		}
2350
+		return $properties;
2351
+	}
2352
+
2353
+
2354
+
2355
+	/**
2356
+	 * Very handy general function to allow for plugins to extend any child of EE_Base_Class.
2357
+	 * If a method is called on a child of EE_Base_Class that doesn't exist, this function is called
2358
+	 * (http://www.garfieldtech.com/blog/php-magic-call) and passed the method's name and arguments. Instead of
2359
+	 * requiring a plugin to extend the EE_Base_Class (which works fine is there's only 1 plugin, but when will that
2360
+	 * happen?) they can add a hook onto 'filters_hook_espresso__{className}__{methodName}' (eg,
2361
+	 * filters_hook_espresso__EE_Answer__my_great_function) and accepts 2 arguments: the object on which the function
2362
+	 * was called, and an array of the original arguments passed to the function. Whatever their callback function
2363
+	 * returns will be returned by this function. Example: in functions.php (or in a plugin):
2364
+	 * add_filter('FHEE__EE_Answer__my_callback','my_callback',10,3); function
2365
+	 * my_callback($previousReturnValue,EE_Base_Class $object,$argsArray){
2366
+	 * $returnString= "you called my_callback! and passed args:".implode(",",$argsArray);
2367
+	 *        return $previousReturnValue.$returnString;
2368
+	 * }
2369
+	 * require('EE_Answer.class.php');
2370
+	 * $answer= EE_Answer::new_instance(array('REG_ID' => 2,'QST_ID' => 3,'ANS_value' => The answer is 42'));
2371
+	 * echo $answer->my_callback('monkeys',100);
2372
+	 * //will output "you called my_callback! and passed args:monkeys,100"
2373
+	 *
2374
+	 * @param string $methodName name of method which was called on a child of EE_Base_Class, but which
2375
+	 * @param array  $args       array of original arguments passed to the function
2376
+	 * @throws EE_Error
2377
+	 * @return mixed whatever the plugin which calls add_filter decides
2378
+	 */
2379
+	public function __call($methodName, $args)
2380
+	{
2381
+		$className = get_class($this);
2382
+		$tagName = "FHEE__{$className}__{$methodName}";
2383
+		if ( ! has_filter($tagName)) {
2384
+			throw new EE_Error(
2385
+				sprintf(
2386
+					__(
2387
+						"Method %s on class %s does not exist! You can create one with the following code in functions.php or in a plugin: add_filter('%s','my_callback',10,3);function my_callback(\$previousReturnValue,EE_Base_Class \$object, \$argsArray){/*function body*/return \$whatever;}",
2388
+						"event_espresso"
2389
+					),
2390
+					$methodName,
2391
+					$className,
2392
+					$tagName
2393
+				)
2394
+			);
2395
+		}
2396
+		return apply_filters($tagName, null, $this, $args);
2397
+	}
2398
+
2399
+
2400
+
2401
+	/**
2402
+	 * Similar to insert_post_meta, adds a record in the Extra_Meta model's table with the given key and value.
2403
+	 * A $previous_value can be specified in case there are many meta rows with the same key
2404
+	 *
2405
+	 * @param string $meta_key
2406
+	 * @param mixed  $meta_value
2407
+	 * @param mixed  $previous_value
2408
+	 * @return bool|int # of records updated (or BOOLEAN if we actually ended up inserting the extra meta row)
2409
+	 * @throws \EE_Error
2410
+	 * NOTE: if the values haven't changed, returns 0
2411
+	 */
2412
+	public function update_extra_meta($meta_key, $meta_value, $previous_value = null)
2413
+	{
2414
+		$query_params = array(
2415
+			array(
2416
+				'EXM_key'  => $meta_key,
2417
+				'OBJ_ID'   => $this->ID(),
2418
+				'EXM_type' => $this->get_model()->get_this_model_name(),
2419
+			),
2420
+		);
2421
+		if ($previous_value !== null) {
2422
+			$query_params[0]['EXM_value'] = $meta_value;
2423
+		}
2424
+		$existing_rows_like_that = EEM_Extra_Meta::instance()->get_all($query_params);
2425
+		if ( ! $existing_rows_like_that) {
2426
+			return $this->add_extra_meta($meta_key, $meta_value);
2427
+		}
2428
+		foreach ($existing_rows_like_that as $existing_row) {
2429
+			$existing_row->save(array('EXM_value' => $meta_value));
2430
+		}
2431
+		return count($existing_rows_like_that);
2432
+	}
2433
+
2434
+
2435
+
2436
+	/**
2437
+	 * Adds a new extra meta record. If $unique is set to TRUE, we'll first double-check
2438
+	 * no other extra meta for this model object have the same key. Returns TRUE if the
2439
+	 * extra meta row was entered, false if not
2440
+	 *
2441
+	 * @param string  $meta_key
2442
+	 * @param string  $meta_value
2443
+	 * @param boolean $unique
2444
+	 * @return boolean
2445
+	 * @throws \EE_Error
2446
+	 */
2447
+	public function add_extra_meta($meta_key, $meta_value, $unique = false)
2448
+	{
2449
+		if ($unique) {
2450
+			$existing_extra_meta = EEM_Extra_Meta::instance()->get_one(
2451
+				array(
2452
+					array(
2453
+						'EXM_key'  => $meta_key,
2454
+						'OBJ_ID'   => $this->ID(),
2455
+						'EXM_type' => $this->get_model()->get_this_model_name(),
2456
+					),
2457
+				)
2458
+			);
2459
+			if ($existing_extra_meta) {
2460
+				return false;
2461
+			}
2462
+		}
2463
+		$new_extra_meta = EE_Extra_Meta::new_instance(
2464
+			array(
2465
+				'EXM_key'   => $meta_key,
2466
+				'EXM_value' => $meta_value,
2467
+				'OBJ_ID'    => $this->ID(),
2468
+				'EXM_type'  => $this->get_model()->get_this_model_name(),
2469
+			)
2470
+		);
2471
+		$new_extra_meta->save();
2472
+		return true;
2473
+	}
2474
+
2475
+
2476
+
2477
+	/**
2478
+	 * Deletes all the extra meta rows for this record as specified by key. If $meta_value
2479
+	 * is specified, only deletes extra meta records with that value.
2480
+	 *
2481
+	 * @param string $meta_key
2482
+	 * @param string $meta_value
2483
+	 * @return int number of extra meta rows deleted
2484
+	 * @throws \EE_Error
2485
+	 */
2486
+	public function delete_extra_meta($meta_key, $meta_value = null)
2487
+	{
2488
+		$query_params = array(
2489
+			array(
2490
+				'EXM_key'  => $meta_key,
2491
+				'OBJ_ID'   => $this->ID(),
2492
+				'EXM_type' => $this->get_model()->get_this_model_name(),
2493
+			),
2494
+		);
2495
+		if ($meta_value !== null) {
2496
+			$query_params[0]['EXM_value'] = $meta_value;
2497
+		}
2498
+		return EEM_Extra_Meta::instance()->delete($query_params);
2499
+	}
2500
+
2501
+
2502
+
2503
+	/**
2504
+	 * Gets the extra meta with the given meta key. If you specify "single" we just return 1, otherwise
2505
+	 * an array of everything found. Requires that this model actually have a relation of type EE_Has_Many_Any_Relation.
2506
+	 * You can specify $default is case you haven't found the extra meta
2507
+	 *
2508
+	 * @param string  $meta_key
2509
+	 * @param boolean $single
2510
+	 * @param mixed   $default if we don't find anything, what should we return?
2511
+	 * @return mixed single value if $single; array if ! $single
2512
+	 * @throws \EE_Error
2513
+	 */
2514
+	public function get_extra_meta($meta_key, $single = false, $default = null)
2515
+	{
2516
+		if ($single) {
2517
+			$result = $this->get_first_related('Extra_Meta', array(array('EXM_key' => $meta_key)));
2518
+			if ($result instanceof EE_Extra_Meta) {
2519
+				return $result->value();
2520
+			} else {
2521
+				return $default;
2522
+			}
2523
+		} else {
2524
+			$results = $this->get_many_related('Extra_Meta', array(array('EXM_key' => $meta_key)));
2525
+			if ($results) {
2526
+				$values = array();
2527
+				foreach ($results as $result) {
2528
+					if ($result instanceof EE_Extra_Meta) {
2529
+						$values[$result->ID()] = $result->value();
2530
+					}
2531
+				}
2532
+				return $values;
2533
+			} else {
2534
+				return $default;
2535
+			}
2536
+		}
2537
+	}
2538
+
2539
+
2540
+
2541
+	/**
2542
+	 * Returns a simple array of all the extra meta associated with this model object.
2543
+	 * If $one_of_each_key is true (Default), it will be an array of simple key-value pairs, keys being the
2544
+	 * extra meta's key, and teh value being its value. However, if there are duplicate extra meta rows with
2545
+	 * the same key, only one will be used. (eg array('foo'=>'bar','monkey'=>123))
2546
+	 * If $one_of_each_key is false, it will return an array with the top-level keys being
2547
+	 * the extra meta keys, but their values are also arrays, which have the extra-meta's ID as their sub-key, and
2548
+	 * finally the extra meta's value as each sub-value. (eg
2549
+	 * array('foo'=>array(1=>'bar',2=>'bill'),'monkey'=>array(3=>123)))
2550
+	 *
2551
+	 * @param boolean $one_of_each_key
2552
+	 * @return array
2553
+	 * @throws \EE_Error
2554
+	 */
2555
+	public function all_extra_meta_array($one_of_each_key = true)
2556
+	{
2557
+		$return_array = array();
2558
+		if ($one_of_each_key) {
2559
+			$extra_meta_objs = $this->get_many_related('Extra_Meta', array('group_by' => 'EXM_key'));
2560
+			foreach ($extra_meta_objs as $extra_meta_obj) {
2561
+				if ($extra_meta_obj instanceof EE_Extra_Meta) {
2562
+					$return_array[$extra_meta_obj->key()] = $extra_meta_obj->value();
2563
+				}
2564
+			}
2565
+		} else {
2566
+			$extra_meta_objs = $this->get_many_related('Extra_Meta');
2567
+			foreach ($extra_meta_objs as $extra_meta_obj) {
2568
+				if ($extra_meta_obj instanceof EE_Extra_Meta) {
2569
+					if ( ! isset($return_array[$extra_meta_obj->key()])) {
2570
+						$return_array[$extra_meta_obj->key()] = array();
2571
+					}
2572
+					$return_array[$extra_meta_obj->key()][$extra_meta_obj->ID()] = $extra_meta_obj->value();
2573
+				}
2574
+			}
2575
+		}
2576
+		return $return_array;
2577
+	}
2578
+
2579
+
2580
+
2581
+	/**
2582
+	 * Gets a pretty nice displayable nice for this model object. Often overridden
2583
+	 *
2584
+	 * @return string
2585
+	 * @throws \EE_Error
2586
+	 */
2587
+	public function name()
2588
+	{
2589
+		//find a field that's not a text field
2590
+		$field_we_can_use = $this->get_model()->get_a_field_of_type('EE_Text_Field_Base');
2591
+		if ($field_we_can_use) {
2592
+			return $this->get($field_we_can_use->get_name());
2593
+		} else {
2594
+			$first_few_properties = $this->model_field_array();
2595
+			$first_few_properties = array_slice($first_few_properties, 0, 3);
2596
+			$name_parts = array();
2597
+			foreach ($first_few_properties as $name => $value) {
2598
+				$name_parts[] = "$name:$value";
2599
+			}
2600
+			return implode(",", $name_parts);
2601
+		}
2602
+	}
2603
+
2604
+
2605
+
2606
+	/**
2607
+	 * in_entity_map
2608
+	 * Checks if this model object has been proven to already be in the entity map
2609
+	 *
2610
+	 * @return boolean
2611
+	 * @throws \EE_Error
2612
+	 */
2613
+	public function in_entity_map()
2614
+	{
2615
+		if ($this->ID() && $this->get_model()->get_from_entity_map($this->ID()) === $this) {
2616
+			//well, if we looked, did we find it in the entity map?
2617
+			return true;
2618
+		} else {
2619
+			return false;
2620
+		}
2621
+	}
2622
+
2623
+
2624
+
2625
+	/**
2626
+	 * refresh_from_db
2627
+	 * Makes sure the fields and values on this model object are in-sync with what's in the database.
2628
+	 *
2629
+	 * @throws EE_Error if this model object isn't in the entity mapper (because then you should
2630
+	 * just use what's in the entity mapper and refresh it) and WP_DEBUG is TRUE
2631
+	 */
2632
+	public function refresh_from_db()
2633
+	{
2634
+		if ($this->ID() && $this->in_entity_map()) {
2635
+			$this->get_model()->refresh_entity_map_from_db($this->ID());
2636
+		} else {
2637
+			//if it doesn't have ID, you shouldn't be asking to refresh it from teh database (because its not in the database)
2638
+			//if it has an ID but it's not in the map, and you're asking me to refresh it
2639
+			//that's kinda dangerous. You should just use what's in the entity map, or add this to the entity map if there's
2640
+			//absolutely nothing in it for this ID
2641
+			if (WP_DEBUG) {
2642
+				throw new EE_Error(
2643
+					sprintf(
2644
+						__('Trying to refresh a model object with ID "%1$s" that\'s not in the entity map? First off: you should put it in the entity map by calling %2$s. Second off, if you want what\'s in the database right now, you should just call %3$s yourself and discard this model object.',
2645
+							'event_espresso'),
2646
+						$this->ID(),
2647
+						get_class($this->get_model()) . '::instance()->add_to_entity_map()',
2648
+						get_class($this->get_model()) . '::instance()->refresh_entity_map()'
2649
+					)
2650
+				);
2651
+			}
2652
+		}
2653
+	}
2654
+
2655
+
2656
+
2657
+	/**
2658
+	 * Because some other plugins, like Advanced Cron Manager, expect all objects to have this method
2659
+	 * (probably a bad assumption they have made, oh well)
2660
+	 *
2661
+	 * @return string
2662
+	 */
2663
+	public function __toString()
2664
+	{
2665
+		try {
2666
+			return sprintf('%s (%s)', $this->name(), $this->ID());
2667
+		} catch (Exception $e) {
2668
+			EE_Error::add_error($e->getMessage(), __FILE__, __FUNCTION__, __LINE__);
2669
+			return '';
2670
+		}
2671
+	}
2672
+
2673
+
2674
+
2675
+	/**
2676
+	 * Clear related model objects if they're already in the DB, because otherwise when we
2677
+	 * UN-serialize this model object we'll need to be careful to add them to the entity map.
2678
+	 * This means if we have made changes to those related model objects, and want to unserialize
2679
+	 * the this model object on a subsequent request, changes to those related model objects will be lost.
2680
+	 * Instead, those related model objects should be directly serialized and stored.
2681
+	 * Eg, the following won't work:
2682
+	 * $reg = EEM_Registration::instance()->get_one_by_ID( 123 );
2683
+	 * $att = $reg->attendee();
2684
+	 * $att->set( 'ATT_fname', 'Dirk' );
2685
+	 * update_option( 'my_option', serialize( $reg ) );
2686
+	 * //END REQUEST
2687
+	 * //START NEXT REQUEST
2688
+	 * $reg = get_option( 'my_option' );
2689
+	 * $reg->attendee()->save();
2690
+	 * And would need to be replace with:
2691
+	 * $reg = EEM_Registration::instance()->get_one_by_ID( 123 );
2692
+	 * $att = $reg->attendee();
2693
+	 * $att->set( 'ATT_fname', 'Dirk' );
2694
+	 * update_option( 'my_option', serialize( $reg ) );
2695
+	 * //END REQUEST
2696
+	 * //START NEXT REQUEST
2697
+	 * $att = get_option( 'my_option' );
2698
+	 * $att->save();
2699
+	 *
2700
+	 * @return array
2701
+	 * @throws \EE_Error
2702
+	 */
2703
+	public function __sleep()
2704
+	{
2705
+		$model = $this->get_model();
2706
+		foreach ($model->relation_settings() as $relation_name => $relation_obj) {
2707
+			if ($relation_obj instanceof EE_Belongs_To_Relation) {
2708
+				$classname = 'EE_' . $model->get_this_model_name();
2709
+				if (
2710
+					$this->get_one_from_cache($relation_name) instanceof $classname
2711
+					&& $this->get_one_from_cache($relation_name)->ID()
2712
+				) {
2713
+					$this->clear_cache($relation_name, $this->get_one_from_cache($relation_name)->ID());
2714
+				}
2715
+			}
2716
+		}
2717
+		$this->_props_n_values_provided_in_constructor = array();
2718
+		$properties_to_serialize = get_object_vars($this);
2719
+		//don't serialize the model. It's big and that risks recursion
2720
+		unset($properties_to_serialize['_model']);
2721
+		return array_keys($properties_to_serialize);
2722
+	}
2723
+
2724
+
2725
+
2726
+	/**
2727
+	 * restore _props_n_values_provided_in_constructor
2728
+	 * PLZ NOTE: this will reset the array to whatever fields values were present prior to serialization,
2729
+	 * and therefore should NOT be used to determine if state change has occurred since initial construction.
2730
+	 * At best, you would only be able to detect if state change has occurred during THIS request.
2731
+	 */
2732
+	public function __wakeup()
2733
+	{
2734
+		$this->_props_n_values_provided_in_constructor = $this->_fields;
2735
+	}
2736 2736
 
2737 2737
 
2738 2738
 
Please login to merge, or discard this patch.
core/services/commands/CommandBus.php 1 patch
Indentation   +73 added lines, -73 removed lines patch added patch discarded remove patch
@@ -7,7 +7,7 @@  discard block
 block discarded – undo
7 7
 use EventEspresso\core\services\commands\middleware\InvalidCommandBusMiddlewareException;
8 8
 
9 9
 if (! defined('EVENT_ESPRESSO_VERSION')) {
10
-    exit('No direct script access allowed');
10
+	exit('No direct script access allowed');
11 11
 }
12 12
 
13 13
 
@@ -40,78 +40,78 @@  discard block
 block discarded – undo
40 40
 class CommandBus implements CommandBusInterface
41 41
 {
42 42
 
43
-    /**
44
-     * @type CommandHandlerManagerInterface $command_handler_manager
45
-     */
46
-    private $command_handler_manager;
47
-
48
-    /**
49
-     * @type CommandBusMiddlewareInterface[] $command_bus_middleware
50
-     */
51
-    private $command_bus_middleware;
52
-
53
-
54
-
55
-    /**
56
-     * CommandBus constructor
57
-     *
58
-     * @param CommandHandlerManagerInterface  $command_handler_manager
59
-     * @param CommandBusMiddlewareInterface[] $command_bus_middleware
60
-     */
61
-    public function __construct(
62
-        CommandHandlerManagerInterface $command_handler_manager,
63
-        array $command_bus_middleware = array()
64
-    ) {
65
-        $this->command_handler_manager = $command_handler_manager;
66
-        $this->command_bus_middleware = is_array($command_bus_middleware)
67
-            ? $command_bus_middleware
68
-            : array($command_bus_middleware);
69
-    }
70
-
71
-
72
-
73
-    /**
74
-     * @return CommandHandlerManagerInterface
75
-     */
76
-    public function getCommandHandlerManager()
77
-    {
78
-        return $this->command_handler_manager;
79
-    }
80
-
81
-
82
-
83
-    /**
84
-     * @param CommandInterface $command
85
-     * @return mixed
86
-     * @throws InvalidDataTypeException
87
-     * @throws InvalidCommandBusMiddlewareException
88
-     */
89
-    public function execute($command)
90
-    {
91
-        if (! $command instanceof CommandInterface) {
92
-            throw new InvalidDataTypeException(__METHOD__ . '( $command )', $command, 'CommandInterface');
93
-        }
94
-        // we're going to add the Command Handler as a callable
95
-        // that will get run at the end of our middleware stack
96
-        // can't pass $this to a Closure, so use a named variable
97
-        $command_bus = $this;
98
-        $middleware = function ($command) use ($command_bus) {
99
-            return $command_bus->getCommandHandlerManager()
100
-                ->getCommandHandler($command, $command_bus)
101
-                ->handle($command);
102
-        };
103
-        // now build the rest of the middleware stack
104
-        while ($command_bus_middleware = array_pop($this->command_bus_middleware)) {
105
-            if (! $command_bus_middleware instanceof CommandBusMiddlewareInterface) {
106
-                throw new InvalidCommandBusMiddlewareException($command_bus_middleware);
107
-            }
108
-            $middleware = function ($command) use ($command_bus_middleware, $middleware) {
109
-                return $command_bus_middleware->handle($command, $middleware);
110
-            };
111
-        }
112
-        // and finally, pass the command into the stack and return the results
113
-        return $middleware($command);
114
-    }
43
+	/**
44
+	 * @type CommandHandlerManagerInterface $command_handler_manager
45
+	 */
46
+	private $command_handler_manager;
47
+
48
+	/**
49
+	 * @type CommandBusMiddlewareInterface[] $command_bus_middleware
50
+	 */
51
+	private $command_bus_middleware;
52
+
53
+
54
+
55
+	/**
56
+	 * CommandBus constructor
57
+	 *
58
+	 * @param CommandHandlerManagerInterface  $command_handler_manager
59
+	 * @param CommandBusMiddlewareInterface[] $command_bus_middleware
60
+	 */
61
+	public function __construct(
62
+		CommandHandlerManagerInterface $command_handler_manager,
63
+		array $command_bus_middleware = array()
64
+	) {
65
+		$this->command_handler_manager = $command_handler_manager;
66
+		$this->command_bus_middleware = is_array($command_bus_middleware)
67
+			? $command_bus_middleware
68
+			: array($command_bus_middleware);
69
+	}
70
+
71
+
72
+
73
+	/**
74
+	 * @return CommandHandlerManagerInterface
75
+	 */
76
+	public function getCommandHandlerManager()
77
+	{
78
+		return $this->command_handler_manager;
79
+	}
80
+
81
+
82
+
83
+	/**
84
+	 * @param CommandInterface $command
85
+	 * @return mixed
86
+	 * @throws InvalidDataTypeException
87
+	 * @throws InvalidCommandBusMiddlewareException
88
+	 */
89
+	public function execute($command)
90
+	{
91
+		if (! $command instanceof CommandInterface) {
92
+			throw new InvalidDataTypeException(__METHOD__ . '( $command )', $command, 'CommandInterface');
93
+		}
94
+		// we're going to add the Command Handler as a callable
95
+		// that will get run at the end of our middleware stack
96
+		// can't pass $this to a Closure, so use a named variable
97
+		$command_bus = $this;
98
+		$middleware = function ($command) use ($command_bus) {
99
+			return $command_bus->getCommandHandlerManager()
100
+				->getCommandHandler($command, $command_bus)
101
+				->handle($command);
102
+		};
103
+		// now build the rest of the middleware stack
104
+		while ($command_bus_middleware = array_pop($this->command_bus_middleware)) {
105
+			if (! $command_bus_middleware instanceof CommandBusMiddlewareInterface) {
106
+				throw new InvalidCommandBusMiddlewareException($command_bus_middleware);
107
+			}
108
+			$middleware = function ($command) use ($command_bus_middleware, $middleware) {
109
+				return $command_bus_middleware->handle($command, $middleware);
110
+			};
111
+		}
112
+		// and finally, pass the command into the stack and return the results
113
+		return $middleware($command);
114
+	}
115 115
 
116 116
 
117 117
 }
Please login to merge, or discard this patch.
core/services/notices/NoticesContainer.php 1 patch
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -18,151 +18,151 @@
 block discarded – undo
18 18
 {
19 19
 
20 20
 
21
-    /**
22
-     * @var NoticeInterface[] $attention
23
-     */
24
-    private $attention = array();
21
+	/**
22
+	 * @var NoticeInterface[] $attention
23
+	 */
24
+	private $attention = array();
25 25
 
26 26
 
27
-    /**
28
-     * @var NoticeInterface[] $error
29
-     */
30
-    private $error = array();
27
+	/**
28
+	 * @var NoticeInterface[] $error
29
+	 */
30
+	private $error = array();
31 31
 
32 32
 
33
-    /**
34
-     * @var NoticeInterface[] $success
35
-     */
36
-    private $success = array();
33
+	/**
34
+	 * @var NoticeInterface[] $success
35
+	 */
36
+	private $success = array();
37 37
 
38 38
 
39 39
 
40
-    /**
41
-     * @param string $notice
42
-     * @param string $file
43
-     * @param string $func
44
-     * @param string $line
45
-     */
46
-    public function addAttention($notice, $file = '', $func = '', $line = '')
47
-    {
48
-        $this->attention[] = new Notice(Notice::ATTENTION, $notice, $file, $func, $line);
49
-    }
40
+	/**
41
+	 * @param string $notice
42
+	 * @param string $file
43
+	 * @param string $func
44
+	 * @param string $line
45
+	 */
46
+	public function addAttention($notice, $file = '', $func = '', $line = '')
47
+	{
48
+		$this->attention[] = new Notice(Notice::ATTENTION, $notice, $file, $func, $line);
49
+	}
50 50
 
51 51
 
52 52
 
53
-    /**
54
-     * @param string $notice
55
-     * @param string $file
56
-     * @param string $func
57
-     * @param string $line
58
-     */
59
-    public function addError($notice, $file, $func, $line)
60
-    {
61
-        $this->error[] = new Notice(Notice::ERROR, $notice, $file, $func, $line);
62
-    }
53
+	/**
54
+	 * @param string $notice
55
+	 * @param string $file
56
+	 * @param string $func
57
+	 * @param string $line
58
+	 */
59
+	public function addError($notice, $file, $func, $line)
60
+	{
61
+		$this->error[] = new Notice(Notice::ERROR, $notice, $file, $func, $line);
62
+	}
63 63
 
64 64
 
65 65
 
66
-    /**
67
-     * @param string $notice
68
-     * @param string $file
69
-     * @param string $func
70
-     * @param string $line
71
-     */
72
-    public function addSuccess($notice, $file = '', $func = '', $line = '')
73
-    {
74
-        $this->success[] = new Notice(Notice::SUCCESS, $notice, $file, $func, $line);
75
-    }
66
+	/**
67
+	 * @param string $notice
68
+	 * @param string $file
69
+	 * @param string $func
70
+	 * @param string $line
71
+	 */
72
+	public function addSuccess($notice, $file = '', $func = '', $line = '')
73
+	{
74
+		$this->success[] = new Notice(Notice::SUCCESS, $notice, $file, $func, $line);
75
+	}
76 76
 
77 77
 
78 78
 
79
-    /**
80
-     * @return boolean
81
-     */
82
-    public function hasAttention()
83
-    {
84
-        return ! empty($this->attention);
85
-    }
79
+	/**
80
+	 * @return boolean
81
+	 */
82
+	public function hasAttention()
83
+	{
84
+		return ! empty($this->attention);
85
+	}
86 86
 
87 87
 
88 88
 
89
-    /**
90
-     * @return boolean
91
-     */
92
-    public function hasError()
93
-    {
94
-        return ! empty($this->error);
95
-    }
89
+	/**
90
+	 * @return boolean
91
+	 */
92
+	public function hasError()
93
+	{
94
+		return ! empty($this->error);
95
+	}
96 96
 
97 97
 
98 98
 
99
-    /**
100
-     * @return boolean
101
-     */
102
-    public function hasSuccess()
103
-    {
104
-        return ! empty($this->success);
105
-    }
99
+	/**
100
+	 * @return boolean
101
+	 */
102
+	public function hasSuccess()
103
+	{
104
+		return ! empty($this->success);
105
+	}
106 106
 
107 107
 
108 108
 
109
-    /**
110
-     * @return int
111
-     */
112
-    public function countAttention()
113
-    {
114
-        return count($this->attention);
115
-    }
109
+	/**
110
+	 * @return int
111
+	 */
112
+	public function countAttention()
113
+	{
114
+		return count($this->attention);
115
+	}
116 116
 
117 117
 
118 118
 
119
-    /**
120
-     * @return int
121
-     */
122
-    public function countError()
123
-    {
124
-        return count($this->error);
125
-    }
119
+	/**
120
+	 * @return int
121
+	 */
122
+	public function countError()
123
+	{
124
+		return count($this->error);
125
+	}
126 126
 
127 127
 
128 128
 
129
-    /**
130
-     * @return int
131
-     */
132
-    public function countSuccess()
133
-    {
134
-        return count($this->success);
135
-    }
129
+	/**
130
+	 * @return int
131
+	 */
132
+	public function countSuccess()
133
+	{
134
+		return count($this->success);
135
+	}
136 136
 
137 137
 
138 138
 
139
-    /**
140
-     * @return NoticeInterface[]
141
-     */
142
-    public function getAttention()
143
-    {
144
-        return $this->attention;
145
-    }
139
+	/**
140
+	 * @return NoticeInterface[]
141
+	 */
142
+	public function getAttention()
143
+	{
144
+		return $this->attention;
145
+	}
146 146
 
147 147
 
148 148
 
149
-    /**
150
-     * @return NoticeInterface[]
151
-     */
152
-    public function getError()
153
-    {
154
-        return $this->error;
155
-    }
149
+	/**
150
+	 * @return NoticeInterface[]
151
+	 */
152
+	public function getError()
153
+	{
154
+		return $this->error;
155
+	}
156 156
 
157 157
 
158 158
 
159
-    /**
160
-     * @return NoticeInterface[]
161
-     */
162
-    public function getSuccess()
163
-    {
164
-        return $this->success;
165
-    }
159
+	/**
160
+	 * @return NoticeInterface[]
161
+	 */
162
+	public function getSuccess()
163
+	{
164
+		return $this->success;
165
+	}
166 166
 
167 167
 
168 168
 }
Please login to merge, or discard this patch.
core/services/notices/Notice.php 1 patch
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -17,108 +17,108 @@
 block discarded – undo
17 17
 class Notice implements NoticeInterface
18 18
 {
19 19
 
20
-    const ERROR = 'error';
21
-    const ATTENTION = 'attention';
22
-    const SUCCESS = 'success';
20
+	const ERROR = 'error';
21
+	const ATTENTION = 'attention';
22
+	const SUCCESS = 'success';
23 23
 
24
-    /**
25
-     * @var string $type
26
-     */
27
-    private $type;
24
+	/**
25
+	 * @var string $type
26
+	 */
27
+	private $type;
28 28
 
29 29
 
30
-    /**
31
-     * @var string $message
32
-     */
33
-    private $message;
30
+	/**
31
+	 * @var string $message
32
+	 */
33
+	private $message;
34 34
 
35 35
 
36
-    /**
37
-     * @var string $file
38
-     */
39
-    private $file;
36
+	/**
37
+	 * @var string $file
38
+	 */
39
+	private $file;
40 40
 
41 41
 
42
-    /**
43
-     * @var string $func
44
-     */
45
-    private $func;
42
+	/**
43
+	 * @var string $func
44
+	 */
45
+	private $func;
46 46
 
47 47
 
48
-    /**
49
-     * @var string $line
50
-     */
51
-    private $line;
48
+	/**
49
+	 * @var string $line
50
+	 */
51
+	private $line;
52 52
 
53 53
 
54 54
 
55
-    /**
56
-     * Notice constructor.
57
-     *
58
-     * @param string $type
59
-     * @param string $message
60
-     * @param string $file
61
-     * @param string $func
62
-     * @param string $line
63
-     */
64
-    public function __construct($type, $message, $file, $func, $line)
65
-    {
66
-        $this->type = $type;
67
-        $this->message = $message;
68
-        $this->file = $file;
69
-        $this->func = $func;
70
-        $this->line = $line;
71
-    }
55
+	/**
56
+	 * Notice constructor.
57
+	 *
58
+	 * @param string $type
59
+	 * @param string $message
60
+	 * @param string $file
61
+	 * @param string $func
62
+	 * @param string $line
63
+	 */
64
+	public function __construct($type, $message, $file, $func, $line)
65
+	{
66
+		$this->type = $type;
67
+		$this->message = $message;
68
+		$this->file = $file;
69
+		$this->func = $func;
70
+		$this->line = $line;
71
+	}
72 72
 
73 73
 
74 74
 
75
-    /**
76
-     * @return string
77
-     */
78
-    public function type()
79
-    {
80
-        return $this->type;
81
-    }
75
+	/**
76
+	 * @return string
77
+	 */
78
+	public function type()
79
+	{
80
+		return $this->type;
81
+	}
82 82
 
83 83
 
84 84
 
85
-    /**
86
-     * @return string
87
-     */
88
-    public function message()
89
-    {
90
-        return $this->message;
91
-    }
85
+	/**
86
+	 * @return string
87
+	 */
88
+	public function message()
89
+	{
90
+		return $this->message;
91
+	}
92 92
 
93 93
 
94 94
 
95
-    /**
96
-     * @return string
97
-     */
98
-    public function file()
99
-    {
100
-        return $this->file;
101
-    }
95
+	/**
96
+	 * @return string
97
+	 */
98
+	public function file()
99
+	{
100
+		return $this->file;
101
+	}
102 102
 
103 103
 
104 104
 
105
-    /**
106
-     * @return string
107
-     */
108
-    public function func()
109
-    {
110
-        return $this->func;
111
-    }
105
+	/**
106
+	 * @return string
107
+	 */
108
+	public function func()
109
+	{
110
+		return $this->func;
111
+	}
112 112
 
113 113
 
114 114
 
115
-    /**
116
-     * @return string
117
-     */
118
-    public function line()
119
-    {
120
-        return $this->line;
121
-    }
115
+	/**
116
+	 * @return string
117
+	 */
118
+	public function line()
119
+	{
120
+		return $this->line;
121
+	}
122 122
 
123 123
 
124 124
 
Please login to merge, or discard this patch.
core/services/notices/NoticesContainerInterface.php 1 patch
Indentation   +57 added lines, -57 removed lines patch added patch discarded remove patch
@@ -16,94 +16,94 @@
 block discarded – undo
16 16
 interface NoticesContainerInterface
17 17
 {
18 18
 
19
-    /**
20
-     * @param string $notice
21
-     * @param string $file
22
-     * @param string $func
23
-     * @param string $line
24
-     */
25
-    public function addAttention($notice, $file = '', $func = '', $line = '');
19
+	/**
20
+	 * @param string $notice
21
+	 * @param string $file
22
+	 * @param string $func
23
+	 * @param string $line
24
+	 */
25
+	public function addAttention($notice, $file = '', $func = '', $line = '');
26 26
 
27 27
 
28 28
 
29
-    /**
30
-     * @param string $notice
31
-     * @param string $file
32
-     * @param string $func
33
-     * @param string $line
34
-     */
35
-    public function addError($notice, $file, $func, $line);
29
+	/**
30
+	 * @param string $notice
31
+	 * @param string $file
32
+	 * @param string $func
33
+	 * @param string $line
34
+	 */
35
+	public function addError($notice, $file, $func, $line);
36 36
 
37 37
 
38 38
 
39
-    /**
40
-     * @param string $notice
41
-     * @param string $file
42
-     * @param string $func
43
-     * @param string $line
44
-     */
45
-    public function addSuccess($notice, $file = '', $func = '', $line = '');
39
+	/**
40
+	 * @param string $notice
41
+	 * @param string $file
42
+	 * @param string $func
43
+	 * @param string $line
44
+	 */
45
+	public function addSuccess($notice, $file = '', $func = '', $line = '');
46 46
 
47 47
 
48 48
 
49
-    /**
50
-     * @return boolean
51
-     */
52
-    public function hasAttention();
49
+	/**
50
+	 * @return boolean
51
+	 */
52
+	public function hasAttention();
53 53
 
54 54
 
55 55
 
56
-    /**
57
-     * @return boolean
58
-     */
59
-    public function hasError();
56
+	/**
57
+	 * @return boolean
58
+	 */
59
+	public function hasError();
60 60
 
61 61
 
62 62
 
63
-    /**
64
-     * @return boolean
65
-     */
66
-    public function hasSuccess();
63
+	/**
64
+	 * @return boolean
65
+	 */
66
+	public function hasSuccess();
67 67
 
68 68
 
69 69
 
70
-    /**
71
-     * @return int
72
-     */
73
-    public function countAttention();
70
+	/**
71
+	 * @return int
72
+	 */
73
+	public function countAttention();
74 74
 
75 75
 
76 76
 
77
-    /**
78
-     * @return int
79
-     */
80
-    public function countError();
77
+	/**
78
+	 * @return int
79
+	 */
80
+	public function countError();
81 81
 
82 82
 
83 83
 
84
-    /**
85
-     * @return int
86
-     */
87
-    public function countSuccess();
84
+	/**
85
+	 * @return int
86
+	 */
87
+	public function countSuccess();
88 88
 
89 89
 
90 90
 
91
-    /**
92
-     * @return NoticeInterface[]
93
-     */
94
-    public function getAttention();
91
+	/**
92
+	 * @return NoticeInterface[]
93
+	 */
94
+	public function getAttention();
95 95
 
96 96
 
97 97
 
98
-    /**
99
-     * @return NoticeInterface[]
100
-     */
101
-    public function getError();
98
+	/**
99
+	 * @return NoticeInterface[]
100
+	 */
101
+	public function getError();
102 102
 
103 103
 
104 104
 
105
-    /**
106
-     * @return NoticeInterface[]
107
-     */
108
-    public function getSuccess();
105
+	/**
106
+	 * @return NoticeInterface[]
107
+	 */
108
+	public function getSuccess();
109 109
 }
110 110
\ No newline at end of file
Please login to merge, or discard this patch.