Completed
Branch EDTR/master (dbc914)
by
unknown
09:15 queued 40s
created

EE_Attendee::bio()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
use EventEspresso\core\exceptions\InvalidDataTypeException;
4
use EventEspresso\core\exceptions\InvalidInterfaceException;
5
6
/**
7
 * Event Espresso
8
 * Event Registration and Management Plugin for WordPress
9
 * @ package            Event Espresso
10
 * @ author                Seth Shoultes
11
 * @ copyright        (c) 2008-2011 Event Espresso  All Rights Reserved.
12
 * @ license            {@link http://eventespresso.com/support/terms-conditions/}   * see Plugin Licensing *
13
 * @ link                    {@link http://www.eventespresso.com}
14
 * @ since                4.0
15
 */
16
17
18
/**
19
 * EE_Attendee class
20
 *
21
 * @package               Event Espresso
22
 * @subpackage            includes/classes/EE_Transaction.class.php
23
 * @author                Mike Nelson
24
 */
25
class EE_Attendee extends EE_CPT_Base implements EEI_Contact, EEI_Address, EEI_Admin_Links, EEI_Attendee
26
{
27
28
    /**
29
     * Sets some dynamic defaults
30
     *
31
     * @param array  $fieldValues
32
     * @param bool   $bydb
33
     * @param string $timezone
34
     * @param array  $date_formats
35
     * @throws EE_Error
36
     */
37
    protected function __construct($fieldValues = null, $bydb = false, $timezone = null, $date_formats = array())
38
    {
39
        if (! isset($fieldValues['ATT_full_name'])) {
40
            $fname = isset($fieldValues['ATT_fname']) ? $fieldValues['ATT_fname'] . ' ' : '';
41
            $lname = isset($fieldValues['ATT_lname']) ? $fieldValues['ATT_lname'] : '';
42
            $fieldValues['ATT_full_name'] = $fname . $lname;
43
        }
44
        if (! isset($fieldValues['ATT_slug'])) {
45
            // $fieldValues['ATT_slug'] = sanitize_key(wp_generate_password(20));
46
            $fieldValues['ATT_slug'] = sanitize_title($fieldValues['ATT_full_name']);
47
        }
48
        if (! isset($fieldValues['ATT_short_bio']) && isset($fieldValues['ATT_bio'])) {
49
            $fieldValues['ATT_short_bio'] = substr($fieldValues['ATT_bio'], 0, 50);
50
        }
51
        parent::__construct($fieldValues, $bydb, $timezone, $date_formats);
52
    }
53
54
55
    /**
56
     * @param array  $props_n_values          incoming values
57
     * @param string $timezone                incoming timezone (if not set the timezone set for the website will be
58
     *                                        used.)
59
     * @param array  $date_formats            incoming date_formats in an array where the first value is the
60
     *                                        date_format and the second value is the time format
61
     * @return EE_Attendee
62
     * @throws EE_Error
63
     */
64
    public static function new_instance($props_n_values = array(), $timezone = null, $date_formats = array())
65
    {
66
        $has_object = parent::_check_for_object($props_n_values, __CLASS__, $timezone, $date_formats);
67
        return $has_object ? $has_object : new self($props_n_values, false, $timezone, $date_formats);
68
    }
69
70
71
    /**
72
     * @param array  $props_n_values  incoming values from the database
73
     * @param string $timezone        incoming timezone as set by the model.  If not set the timezone for
74
     *                                the website will be used.
75
     * @return EE_Attendee
76
     */
77
    public static function new_instance_from_db($props_n_values = array(), $timezone = null)
78
    {
79
        return new self($props_n_values, true, $timezone);
80
    }
81
82
83
    /**
84
     *        Set Attendee First Name
85
     *
86
     * @access        public
87
     * @param string $fname
88
     * @throws EE_Error
89
     */
90
    public function set_fname($fname = '')
91
    {
92
        $this->set('ATT_fname', $fname);
93
    }
94
95
96
    /**
97
     *        Set Attendee Last Name
98
     *
99
     * @access        public
100
     * @param string $lname
101
     * @throws EE_Error
102
     */
103
    public function set_lname($lname = '')
104
    {
105
        $this->set('ATT_lname', $lname);
106
    }
107
108
109
    /**
110
     *        Set Attendee Address
111
     *
112
     * @access        public
113
     * @param string $address
114
     * @throws EE_Error
115
     */
116
    public function set_address($address = '')
117
    {
118
        $this->set('ATT_address', $address);
119
    }
120
121
122
    /**
123
     *        Set Attendee Address2
124
     *
125
     * @access        public
126
     * @param        string $address2
127
     * @throws EE_Error
128
     */
129
    public function set_address2($address2 = '')
130
    {
131
        $this->set('ATT_address2', $address2);
132
    }
133
134
135
    /**
136
     *        Set Attendee City
137
     *
138
     * @access        public
139
     * @param        string $city
140
     * @throws EE_Error
141
     */
142
    public function set_city($city = '')
143
    {
144
        $this->set('ATT_city', $city);
145
    }
146
147
148
    /**
149
     *        Set Attendee State ID
150
     *
151
     * @access        public
152
     * @param        int $STA_ID
153
     * @throws EE_Error
154
     */
155
    public function set_state($STA_ID = 0)
156
    {
157
        $this->set('STA_ID', $STA_ID);
158
    }
159
160
161
    /**
162
     *        Set Attendee Country ISO Code
163
     *
164
     * @access        public
165
     * @param        string $CNT_ISO
166
     * @throws EE_Error
167
     */
168
    public function set_country($CNT_ISO = '')
169
    {
170
        $this->set('CNT_ISO', $CNT_ISO);
171
    }
172
173
174
    /**
175
     *        Set Attendee Zip/Postal Code
176
     *
177
     * @access        public
178
     * @param        string $zip
179
     * @throws EE_Error
180
     */
181
    public function set_zip($zip = '')
182
    {
183
        $this->set('ATT_zip', $zip);
184
    }
185
186
187
    /**
188
     *        Set Attendee Email Address
189
     *
190
     * @access        public
191
     * @param        string $email
192
     * @throws EE_Error
193
     */
194
    public function set_email($email = '')
195
    {
196
        $this->set('ATT_email', $email);
197
    }
198
199
200
    /**
201
     *        Set Attendee Phone
202
     *
203
     * @access        public
204
     * @param        string $phone
205
     * @throws EE_Error
206
     */
207
    public function set_phone($phone = '')
208
    {
209
        $this->set('ATT_phone', $phone);
210
    }
211
212
213
    /**
214
     *        set deleted
215
     *
216
     * @access        public
217
     * @param        bool $ATT_deleted
218
     * @throws EE_Error
219
     */
220
    public function set_deleted($ATT_deleted = false)
221
    {
222
        $this->set('ATT_deleted', $ATT_deleted);
223
    }
224
225
226
    /**
227
     * Returns the value for the post_author id saved with the cpt
228
     *
229
     * @since 4.5.0
230
     * @return int
231
     * @throws EE_Error
232
     */
233
    public function wp_user()
234
    {
235
        return $this->get('ATT_author');
236
    }
237
238
239
    /**
240
     *        get Attendee First Name
241
     *
242
     * @access        public
243
     * @return string
244
     * @throws EE_Error
245
     */
246
    public function fname()
247
    {
248
        return $this->get('ATT_fname');
249
    }
250
251
252
    /**
253
     * echoes out the attendee's first name
254
     *
255
     * @return void
256
     * @throws EE_Error
257
     */
258
    public function e_full_name()
259
    {
260
        echo $this->full_name();
261
    }
262
263
264
    /**
265
     * Returns the first and last name concatenated together with a space.
266
     *
267
     * @param bool $apply_html_entities
268
     * @return string
269
     * @throws EE_Error
270
     */
271
    public function full_name($apply_html_entities = false)
272
    {
273
        $full_name = array(
274
            $this->fname(),
275
            $this->lname(),
276
        );
277
        $full_name = array_filter($full_name);
278
        $full_name = implode(' ', $full_name);
279
        return $apply_html_entities ? htmlentities($full_name, ENT_QUOTES, 'UTF-8') : $full_name;
280
    }
281
282
283
    /**
284
     * This returns the value of the `ATT_full_name` field which is usually equivalent to calling `full_name()` unless
285
     * the post_title field has been directly modified in the db for the post (espresso_attendees post type) for this
286
     * attendee.
287
     *
288
     * @param bool $apply_html_entities
289
     * @return string
290
     * @throws EE_Error
291
     */
292
    public function ATT_full_name($apply_html_entities = false)
293
    {
294
        return $apply_html_entities
295
            ? htmlentities($this->get('ATT_full_name'), ENT_QUOTES, 'UTF-8')
296
            : $this->get('ATT_full_name');
297
    }
298
299
300
    /**
301
     *        get Attendee Last Name
302
     *
303
     * @access        public
304
     * @return string
305
     * @throws EE_Error
306
     */
307
    public function lname()
308
    {
309
        return $this->get('ATT_lname');
310
    }
311
312
313
    /**
314
     * get Attendee bio
315
     *
316
     * @access public
317
     * @return string
318
     * @throws EE_Error
319
     */
320
    public function bio()
321
    {
322
        return $this->get('ATT_bio');
323
    }
324
325
326
    /**
327
     * get Attendee short bio
328
     *
329
     * @access public
330
     * @return string
331
     * @throws EE_Error
332
     */
333
    public function short_bio()
334
    {
335
        return $this->get('ATT_short_bio');
336
    }
337
338
339
    /**
340
     * Gets the attendee's full address as an array so client code can decide hwo to display it
341
     *
342
     * @return array numerically indexed, with each part of the address that is known.
343
     * Eg, if the user only responded to state and country,
344
     * it would be array(0=>'Alabama',1=>'USA')
345
     * @return array
346
     * @throws EE_Error
347
     */
348
    public function full_address_as_array()
349
    {
350
        $full_address_array = array();
351
        $initial_address_fields = array('ATT_address', 'ATT_address2', 'ATT_city',);
352
        foreach ($initial_address_fields as $address_field_name) {
353
            $address_fields_value = $this->get($address_field_name);
354
            if (! empty($address_fields_value)) {
355
                $full_address_array[] = $address_fields_value;
356
            }
357
        }
358
        // now handle state and country
359
        $state_obj = $this->state_obj();
360
        if ($state_obj instanceof EE_State) {
361
            $full_address_array[] = $state_obj->name();
362
        }
363
        $country_obj = $this->country_obj();
364
        if ($country_obj instanceof EE_Country) {
365
            $full_address_array[] = $country_obj->name();
366
        }
367
        // lastly get the xip
368
        $zip_value = $this->zip();
369
        if (! empty($zip_value)) {
370
            $full_address_array[] = $zip_value;
371
        }
372
        return $full_address_array;
373
    }
374
375
376
    /**
377
     *        get Attendee Address
378
     *
379
     * @return string
380
     * @throws EE_Error
381
     */
382
    public function address()
383
    {
384
        return $this->get('ATT_address');
385
    }
386
387
388
    /**
389
     *        get Attendee Address2
390
     *
391
     * @return string
392
     * @throws EE_Error
393
     */
394
    public function address2()
395
    {
396
        return $this->get('ATT_address2');
397
    }
398
399
400
    /**
401
     *        get Attendee City
402
     *
403
     * @return string
404
     * @throws EE_Error
405
     */
406
    public function city()
407
    {
408
        return $this->get('ATT_city');
409
    }
410
411
412
    /**
413
     *        get Attendee State ID
414
     *
415
     * @return string
416
     * @throws EE_Error
417
     */
418
    public function state_ID()
419
    {
420
        return $this->get('STA_ID');
421
    }
422
423
424
    /**
425
     * @return string
426
     * @throws EE_Error
427
     */
428
    public function state_abbrev()
429
    {
430
        return $this->state_obj() instanceof EE_State ? $this->state_obj()->abbrev() : '';
431
    }
432
433
434
    /**
435
     * Gets the state set to this attendee
436
     *
437
     * @return EE_State
438
     * @throws EE_Error
439
     */
440
    public function state_obj()
441
    {
442
        return $this->get_first_related('State');
443
    }
444
445
446
    /**
447
     * Returns the state's name, otherwise 'Unknown'
448
     *
449
     * @return string
450
     * @throws EE_Error
451
     */
452
    public function state_name()
453
    {
454
        if ($this->state_obj()) {
455
            return $this->state_obj()->name();
456
        } else {
457
            return '';
458
        }
459
    }
460
461
462
    /**
463
     * either displays the state abbreviation or the state name, as determined
464
     * by the "FHEE__EEI_Address__state__use_abbreviation" filter.
465
     * defaults to abbreviation
466
     *
467
     * @return string
468
     * @throws EE_Error
469
     */
470
    public function state()
471
    {
472
        if (apply_filters('FHEE__EEI_Address__state__use_abbreviation', true, $this->state_obj())) {
473
            return $this->state_abbrev();
474
        }
475
        return $this->state_name();
476
    }
477
478
479
    /**
480
     *    get Attendee Country ISO Code
481
     *
482
     * @return string
483
     * @throws EE_Error
484
     */
485
    public function country_ID()
486
    {
487
        return $this->get('CNT_ISO');
488
    }
489
490
491
    /**
492
     * Gets country set for this attendee
493
     *
494
     * @return EE_Country
495
     * @throws EE_Error
496
     */
497
    public function country_obj()
498
    {
499
        return $this->get_first_related('Country');
500
    }
501
502
503
    /**
504
     * Returns the country's name if known, otherwise 'Unknown'
505
     *
506
     * @return string
507
     * @throws EE_Error
508
     */
509
    public function country_name()
510
    {
511
        if ($this->country_obj()) {
512
            return $this->country_obj()->name();
513
        }
514
        return '';
515
    }
516
517
518
    /**
519
     * either displays the country ISO2 code or the country name, as determined
520
     * by the "FHEE__EEI_Address__country__use_abbreviation" filter.
521
     * defaults to abbreviation
522
     *
523
     * @return string
524
     * @throws EE_Error
525
     */
526
    public function country()
527
    {
528
        if (apply_filters('FHEE__EEI_Address__country__use_abbreviation', true, $this->country_obj())) {
529
            return $this->country_ID();
530
        }
531
        return $this->country_name();
532
    }
533
534
535
    /**
536
     *        get Attendee Zip/Postal Code
537
     *
538
     * @return string
539
     * @throws EE_Error
540
     */
541
    public function zip()
542
    {
543
        return $this->get('ATT_zip');
544
    }
545
546
547
    /**
548
     *        get Attendee Email Address
549
     *
550
     * @return string
551
     * @throws EE_Error
552
     */
553
    public function email()
554
    {
555
        return $this->get('ATT_email');
556
    }
557
558
559
    /**
560
     *        get Attendee Phone #
561
     *
562
     * @return string
563
     * @throws EE_Error
564
     */
565
    public function phone()
566
    {
567
        return $this->get('ATT_phone');
568
    }
569
570
571
    /**
572
     *    get deleted
573
     *
574
     * @return        bool
575
     * @throws EE_Error
576
     */
577
    public function deleted()
578
    {
579
        return $this->get('ATT_deleted');
580
    }
581
582
583
    /**
584
     * Gets registrations of this attendee
585
     *
586
     * @param array $query_params
587
     * @return EE_Registration[]
588
     * @throws EE_Error
589
     */
590
    public function get_registrations($query_params = array())
591
    {
592
        return $this->get_many_related('Registration', $query_params);
593
    }
594
595
596
    /**
597
     * Gets the most recent registration of this attendee
598
     *
599
     * @return EE_Registration
600
     * @throws EE_Error
601
     */
602
    public function get_most_recent_registration()
603
    {
604
        return $this->get_first_related(
605
            'Registration',
606
            array('order_by' => array('REG_date' => 'DESC'))
607
        ); // null, 'REG_date', 'DESC', '=', 'OBJECT_K');
608
    }
609
610
611
    /**
612
     * Gets the most recent registration for this attend at this event
613
     *
614
     * @param int $event_id
615
     * @return EE_Registration
616
     * @throws EE_Error
617
     */
618
    public function get_most_recent_registration_for_event($event_id)
619
    {
620
        return $this->get_first_related(
621
            'Registration',
622
            array(array('EVT_ID' => $event_id), 'order_by' => array('REG_date' => 'DESC'))
623
        );
624
    }
625
626
627
    /**
628
     * returns any events attached to this attendee ($_Event property);
629
     *
630
     * @return array
631
     * @throws EE_Error
632
     */
633
    public function events()
634
    {
635
        return $this->get_many_related('Event');
636
    }
637
638
639
    /**
640
     * Gets the billing info array where keys match espresso_reg_page_billing_inputs(),
641
     * and keys are their cleaned values. @see EE_Attendee::save_and_clean_billing_info_for_payment_method() which was
642
     * used to save the billing info
643
     *
644
     * @param EE_Payment_Method $payment_method the _gateway_name property on the gateway class
645
     * @return EE_Form_Section_Proper|null
646
     * @throws EE_Error
647
     */
648
    public function billing_info_for_payment_method($payment_method)
649
    {
650
        $pm_type = $payment_method->type_obj();
651
        if (! $pm_type instanceof EE_PMT_Base) {
652
            return null;
653
        }
654
        $billing_info = $this->get_post_meta($this->get_billing_info_postmeta_name($payment_method), true);
655
        if (! $billing_info) {
656
            return null;
657
        }
658
        $billing_form = $pm_type->billing_form();
659
        // double-check the form isn't totally hidden, in which case pretend there is no form
660
        $form_totally_hidden = true;
661
        foreach ($billing_form->inputs_in_subsections() as $input) {
662
            if (! $input->get_display_strategy() instanceof EE_Hidden_Display_Strategy) {
663
                $form_totally_hidden = false;
664
                break;
665
            }
666
        }
667
        if ($form_totally_hidden) {
668
            return null;
669
        }
670
        if ($billing_form instanceof EE_Form_Section_Proper) {
671
            $billing_form->receive_form_submission(array($billing_form->name() => $billing_info), false);
672
        }
673
674
        return $billing_form;
675
    }
676
677
678
    /**
679
     * Gets the postmeta key that holds this attendee's billing info for the
680
     * specified payment method
681
     *
682
     * @param EE_Payment_Method $payment_method
683
     * @return string
684
     * @throws EE_Error
685
     */
686
    public function get_billing_info_postmeta_name($payment_method)
687
    {
688
        if ($payment_method->type_obj() instanceof EE_PMT_Base) {
689
            return 'billing_info_' . $payment_method->type_obj()->system_name();
690
        }
691
        return null;
692
    }
693
694
695
    /**
696
     * Saves the billing info to the attendee. @see EE_Attendee::billing_info_for_payment_method() which is used to
697
     * retrieve it
698
     *
699
     * @param EE_Billing_Attendee_Info_Form $billing_form
700
     * @param EE_Payment_Method             $payment_method
701
     * @return boolean
702
     * @throws EE_Error
703
     */
704
    public function save_and_clean_billing_info_for_payment_method($billing_form, $payment_method)
705
    {
706
        if (! $billing_form instanceof EE_Billing_Attendee_Info_Form) {
707
            EE_Error::add_error(__('Cannot save billing info because there is none.', 'event_espresso'));
708
            return false;
709
        }
710
        $billing_form->clean_sensitive_data();
711
        return update_post_meta(
712
            $this->ID(),
713
            $this->get_billing_info_postmeta_name($payment_method),
714
            $billing_form->input_values(true)
715
        );
716
    }
717
718
719
    /**
720
     * Return the link to the admin details for the object.
721
     *
722
     * @return string
723
     * @throws EE_Error
724
     * @throws InvalidArgumentException
725
     * @throws InvalidDataTypeException
726
     * @throws InvalidInterfaceException
727
     * @throws ReflectionException
728
     */
729
    public function get_admin_details_link()
730
    {
731
        return $this->get_admin_edit_link();
732
    }
733
734
735
    /**
736
     * Returns the link to the editor for the object.  Sometimes this is the same as the details.
737
     *
738
     * @return string
739
     * @throws EE_Error
740
     * @throws InvalidArgumentException
741
     * @throws ReflectionException
742
     * @throws InvalidDataTypeException
743
     * @throws InvalidInterfaceException
744
     */
745 View Code Duplication
    public function get_admin_edit_link()
746
    {
747
        EE_Registry::instance()->load_helper('URL');
748
        return EEH_URL::add_query_args_and_nonce(
749
            array(
750
                'page'   => 'espresso_registrations',
751
                'action' => 'edit_attendee',
752
                'post'   => $this->ID(),
753
            ),
754
            admin_url('admin.php')
755
        );
756
    }
757
758
759
    /**
760
     * Returns the link to a settings page for the object.
761
     *
762
     * @return string
763
     * @throws EE_Error
764
     * @throws InvalidArgumentException
765
     * @throws InvalidDataTypeException
766
     * @throws InvalidInterfaceException
767
     * @throws ReflectionException
768
     */
769
    public function get_admin_settings_link()
770
    {
771
        return $this->get_admin_edit_link();
772
    }
773
774
775
    /**
776
     * Returns the link to the "overview" for the object (typically the "list table" view).
777
     *
778
     * @return string
779
     * @throws EE_Error
780
     * @throws InvalidArgumentException
781
     * @throws ReflectionException
782
     * @throws InvalidDataTypeException
783
     * @throws InvalidInterfaceException
784
     */
785 View Code Duplication
    public function get_admin_overview_link()
786
    {
787
        EE_Registry::instance()->load_helper('URL');
788
        return EEH_URL::add_query_args_and_nonce(
789
            array(
790
                'page'   => 'espresso_registrations',
791
                'action' => 'contact_list',
792
            ),
793
            admin_url('admin.php')
794
        );
795
    }
796
}
797