Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php if ( ! defined('EVENT_ESPRESSO_VERSION')) exit('No direct script access allowed'); |
||
14 | class EEM_Registration extends EEM_Soft_Delete_Base { |
||
15 | |||
16 | // private instance of the Registration object |
||
17 | protected static $_instance = NULL; |
||
18 | |||
19 | /** |
||
20 | * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values |
||
21 | * are status codes (eg, approved, cancelled, etc) |
||
22 | * @var array |
||
23 | */ |
||
24 | private static $_reg_status; |
||
25 | |||
26 | /** |
||
27 | * The value of REG_count for a primary registrant |
||
28 | */ |
||
29 | const PRIMARY_REGISTRANT_COUNT = 1; |
||
30 | |||
31 | /** |
||
32 | * Status ID (STS_ID on esp_status table) to indicate an INCOMPLETE registration. |
||
33 | * Initial status for registrations when they are first created |
||
34 | * Payments are NOT allowed. |
||
35 | * Automatically toggled to whatever the default Event registration status is upon completion of the attendee information reg step |
||
36 | * NO space reserved. |
||
37 | * Registration is NOT active |
||
38 | */ |
||
39 | const status_id_incomplete = 'RIC'; |
||
40 | |||
41 | /** |
||
42 | * Status ID (STS_ID on esp_status table) to indicate an UNAPPROVED registration. |
||
43 | * Payments are NOT allowed. |
||
44 | * Event Admin must manually toggle STS_ID for it to change |
||
45 | * No space reserved. |
||
46 | * Registration is active |
||
47 | */ |
||
48 | const status_id_not_approved = 'RNA'; |
||
49 | |||
50 | /** |
||
51 | * Status ID (STS_ID on esp_status table) to indicate registration is PENDING_PAYMENT . |
||
52 | * Payments are allowed. |
||
53 | * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee |
||
54 | * No space reserved. |
||
55 | * Registration is active |
||
56 | */ |
||
57 | const status_id_pending_payment = 'RPP'; |
||
58 | |||
59 | /** |
||
60 | * Status ID (STS_ID on esp_status table) to indicate an APPROVED registration. |
||
61 | * the TXN may or may not be completed ( paid in full ) |
||
62 | * Payments are allowed. |
||
63 | * A space IS reserved. |
||
64 | * Registration is active |
||
65 | */ |
||
66 | const status_id_approved = 'RAP'; |
||
67 | |||
68 | /** |
||
69 | * Status ID (STS_ID on esp_status table) to indicate a registration was CANCELLED by the attendee. |
||
70 | * Payments are NOT allowed. |
||
71 | * NO space reserved. |
||
72 | * Registration is NOT active |
||
73 | */ |
||
74 | const status_id_cancelled = 'RCN'; |
||
75 | |||
76 | /** |
||
77 | * Status ID (STS_ID on esp_status table) to indicate a registration was DECLINED by the Event Admin |
||
78 | * Payments are NOT allowed. |
||
79 | * No space reserved. |
||
80 | * Registration is NOT active |
||
81 | */ |
||
82 | const status_id_declined = 'RDC'; |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * private constructor to prevent direct creation |
||
88 | * |
||
89 | * @Constructor |
||
90 | * @access protected |
||
91 | * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved). |
||
92 | * Note this just sends the timezone info to the date time model field objects. Default is NULL (and will be assumed using the set timezone in the 'timezone_string' wp option) |
||
93 | * @return \EEM_Registration |
||
|
|||
94 | */ |
||
95 | protected function __construct( $timezone ) { |
||
96 | $this->singular_item = __('Registration','event_espresso'); |
||
97 | $this->plural_item = __('Registrations','event_espresso'); |
||
98 | |||
99 | $this->_tables = array( |
||
100 | 'Registration'=>new EE_Primary_Table('esp_registration','REG_ID') |
||
101 | ); |
||
102 | $this->_fields = array( |
||
103 | 'Registration'=>array( |
||
104 | 'REG_ID'=>new EE_Primary_Key_Int_Field('REG_ID', __('Registration ID','event_espresso')), |
||
105 | 'EVT_ID'=>new EE_Foreign_Key_Int_Field('EVT_ID', __('Event ID','event_espresso'), false, 0, 'Event'), |
||
106 | 'ATT_ID'=>new EE_Foreign_Key_Int_Field('ATT_ID', __('Attendee ID','event_espresso'), false, 0, 'Attendee'), |
||
107 | 'TXN_ID'=>new EE_Foreign_Key_Int_Field('TXN_ID', __('Transaction ID','event_espresso'), false, 0, 'Transaction'), |
||
108 | 'TKT_ID'=>new EE_Foreign_Key_Int_Field('TKT_ID', __('Ticket ID','event_espresso'), false, 0, 'Ticket'), |
||
109 | 'STS_ID'=>new EE_Foreign_Key_String_Field('STS_ID', __('Status ID','event_espresso'), false, EEM_Registration::status_id_incomplete, 'Status'), |
||
110 | 'REG_date'=>new EE_Datetime_Field('REG_date', __('Time registration occurred','event_espresso'), false, time(), $timezone ), |
||
111 | 'REG_final_price'=>new EE_Money_Field('REG_final_price', __('Registration\'s share of the transaction total','event_espresso'), false, 0), |
||
112 | 'REG_paid'=>new EE_Money_Field('REG_paid', __('Amount paid to date towards registration','event_espresso'), false, 0), |
||
113 | 'REG_session'=>new EE_Plain_Text_Field('REG_session', __('Session ID of registration','event_espresso'), false, ''), |
||
114 | 'REG_code'=>new EE_Plain_Text_Field('REG_code', __('Unique Code for this registration','event_espresso'), false, ''), |
||
115 | 'REG_url_link'=>new EE_Plain_Text_Field('REG_url_link', __('String to be used in URL for identifying registration','event_espresso'), false, ''), |
||
116 | 'REG_count'=>new EE_Integer_Field('REG_count', __('Count of this registration in the group registration ','event_espresso'), true, 1), |
||
117 | 'REG_group_size'=>new EE_Integer_Field('REG_group_size', __('Number of registrations on this group','event_espresso'), false, 1), |
||
118 | 'REG_att_is_going'=>new EE_Boolean_Field('REG_att_is_going', __('Flag indicating the registrant plans on attending','event_espresso'), false, false), |
||
119 | 'REG_deleted' => new EE_Trashed_Flag_Field('REG_deleted', __('Flag indicating if registration has been archived or not.', 'event_espresso'), false, false ) |
||
120 | ) |
||
121 | ); |
||
122 | $this->_model_relations = array( |
||
123 | 'Event'=>new EE_Belongs_To_Relation(), |
||
124 | 'Attendee'=>new EE_Belongs_To_Relation(), |
||
125 | 'Transaction'=>new EE_Belongs_To_Relation(), |
||
126 | 'Ticket'=>new EE_Belongs_To_Relation(), |
||
127 | 'Status'=>new EE_Belongs_To_Relation(), |
||
128 | 'Answer'=>new EE_Has_Many_Relation(), |
||
129 | 'Checkin'=>new EE_Has_Many_Relation(), |
||
130 | 'Registration_Payment' => new EE_Has_Many_Relation(), |
||
131 | 'Payment'=>new EE_HABTM_Relation( 'Registration_Payment' ), |
||
132 | ); |
||
133 | $this->_model_chain_to_wp_user = 'Event'; |
||
134 | |||
135 | parent::__construct( $timezone ); |
||
136 | } |
||
137 | |||
138 | |||
139 | |||
140 | /** |
||
141 | * reg_statuses_that_allow_payment |
||
142 | * a filterable list of registration statuses that allow a registrant to make a payment |
||
143 | * |
||
144 | * @access public |
||
145 | * @return array |
||
146 | */ |
||
147 | public static function reg_statuses_that_allow_payment() { |
||
148 | return apply_filters( |
||
149 | 'FHEE__EEM_Registration__reg_statuses_that_allow_payment', |
||
150 | array( |
||
151 | EEM_Registration::status_id_approved, |
||
152 | EEM_Registration::status_id_pending_payment, |
||
153 | ) |
||
154 | ); |
||
155 | } |
||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * get list of registration statuses |
||
161 | * |
||
162 | * |
||
163 | * @access public |
||
164 | * @param array $exclude The status ids to exclude from the returned results |
||
165 | * @param bool $translated If true will return the values as singular localized strings |
||
166 | * @return array |
||
167 | */ |
||
168 | public static function reg_status_array( $exclude = array(), $translated = FALSE ) { |
||
172 | |||
173 | |||
174 | |||
175 | /** |
||
176 | * get list of registration statuses |
||
177 | * @access private |
||
178 | * @param array $exclude |
||
179 | * @return array |
||
180 | */ |
||
181 | private function _get_registration_status_array( $exclude = array() ) { |
||
199 | |||
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * This returns a wpdb->results array of all registration date month and years matching the incoming query params and grouped by month and year. |
||
205 | * @param array $where_params Array of query_params as described in the comments for EEM_Base::get_all() |
||
206 | * @return wpdb results array |
||
207 | */ |
||
208 | public function get_reg_months_and_years( $where_params ) { |
||
218 | |||
219 | |||
220 | |||
221 | |||
222 | /** |
||
223 | * retrieve ALL registrations for a particular Attendee from db |
||
224 | * @access public |
||
225 | * @param int $ATT_ID |
||
226 | * @return EE_Registration[] |
||
227 | */ |
||
228 | public function get_all_registrations_for_attendee( $ATT_ID = 0 ) { |
||
234 | |||
235 | |||
236 | |||
237 | /** |
||
238 | * Gets a registration given their REG_url_link. Yes, this should usually |
||
239 | * be passed via a GET parameter. |
||
240 | * @param string $REG_url_link |
||
241 | * @return EE_Registration |
||
242 | */ |
||
243 | public function get_registration_for_reg_url_link($REG_url_link){ |
||
249 | |||
250 | |||
251 | |||
252 | |||
253 | /** |
||
254 | * retrieve registration for a specific transaction attendee from db |
||
255 | * |
||
256 | * @access public |
||
257 | * @param int $TXN_ID |
||
258 | * @param int $ATT_ID |
||
259 | * @param int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the attendee number is required |
||
260 | * @return mixed array on success, FALSE on fail |
||
261 | */ |
||
262 | public function get_registration_for_transaction_attendee( $TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0 ) { |
||
271 | |||
272 | |||
273 | /** |
||
274 | * get the number of registrations per day for the Registration Admin page Reports Tab. |
||
275 | * (doesn't utilize models because it's a fairly specialized query) |
||
276 | * @access public |
||
277 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
278 | * @return stdClass[] with properties regDate and total |
||
279 | */ |
||
280 | public function get_registrations_per_day_report( $period = '-1 month' ) { |
||
305 | |||
306 | |||
307 | /** |
||
308 | * Get the number of registrations per day including the count of registrations for each Registration Status. |
||
309 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
310 | * |
||
311 | * @param string $period |
||
312 | * |
||
313 | * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID |
||
314 | * (i.e. RAP) |
||
315 | */ |
||
316 | public function get_registrations_per_day_and_per_status_report( $period = '-1 month' ) { |
||
317 | global $wpdb; |
||
318 | $registration_table = $wpdb->prefix . 'esp_registration'; |
||
319 | $event_table = $wpdb->posts; |
||
320 | $sql_date = date("Y-m-d H:i:s", strtotime($period) ); |
||
321 | |||
322 | //prepare the query interval for displaying offset |
||
323 | EE_Registry::instance()->load_helper( 'DTT_Helper' ); |
||
324 | $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset( $this->get_timezone(), 'dates.REG_date' ); |
||
325 | |||
326 | //inner date query |
||
327 | $inner_date_query = "SELECT DISTINCT REG_date from $registration_table "; |
||
328 | $inner_where = " WHERE"; |
||
329 | //exclude events not authored by user if permissions in effect |
||
330 | View Code Duplication | if ( ! EE_Registry::instance()->CAP->current_user_can( 'ee_read_others_registrations', 'reg_per_event_report' ) ) { |
|
331 | $inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID"; |
||
332 | $inner_where .= " post_author = " . get_current_user_id() . " AND"; |
||
333 | } |
||
334 | $inner_where .= " REG_date >= '$sql_date'"; |
||
335 | $inner_date_query .= $inner_where; |
||
336 | |||
337 | //start main query |
||
338 | $select = "SELECT DATE($query_interval) as Registration_REG_date, "; |
||
339 | $join = ''; |
||
340 | $join_parts = array(); |
||
341 | $select_parts = array(); |
||
342 | |||
343 | //loop through registration stati to do parts for each status. |
||
344 | View Code Duplication | foreach ( EEM_Registration::reg_status_array() as $STS_ID => $STS_code ) { |
|
345 | if ( $STS_ID === EEM_Registration::status_id_incomplete ) { |
||
346 | continue; |
||
347 | } |
||
348 | $select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID"; |
||
349 | $join_parts[] = "$registration_table AS $STS_code ON $STS_code.REG_date = dates.REG_date AND $STS_code.STS_ID = '$STS_ID'"; |
||
350 | } |
||
351 | |||
352 | //setup the selects |
||
353 | $select .= implode(', ', $select_parts ); |
||
354 | $select .= " FROM ($inner_date_query) AS dates LEFT JOIN "; |
||
355 | |||
356 | //setup the joins |
||
357 | $join .= implode( " LEFT JOIN ", $join_parts ); |
||
358 | |||
359 | //now let's put it all together |
||
360 | $query = $select . $join . ' GROUP BY Registration_REG_date'; |
||
361 | |||
362 | //and execute it |
||
363 | $results = $wpdb->get_results( |
||
364 | $query, |
||
365 | ARRAY_A |
||
366 | ); |
||
367 | return $results; |
||
368 | } |
||
369 | |||
370 | |||
371 | |||
372 | |||
373 | |||
374 | |||
375 | /** |
||
376 | * get the number of registrations per event for the Registration Admin page Reports Tab |
||
377 | * @access public |
||
378 | * @param $period string which can be passed to php's strtotime function (eg "-1 month") |
||
379 | * @return stdClass[] each with properties event_name, reg_limit, and total |
||
380 | */ |
||
381 | public function get_registrations_per_event_report( $period = '-1 month' ) { |
||
404 | |||
405 | |||
406 | /** |
||
407 | * Get the number of registrations per event grouped by registration status. |
||
408 | * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results. |
||
409 | * |
||
410 | * @param string $period |
||
411 | * |
||
412 | * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID |
||
413 | * (i.e. RAP) |
||
414 | */ |
||
415 | public function get_registrations_per_event_and_per_status_report( $period = '-1 month' ) { |
||
416 | global $wpdb; |
||
417 | $registration_table = $wpdb->prefix . 'esp_registration'; |
||
418 | $event_table = $wpdb->posts; |
||
419 | $sql_date = date("Y-m-d H:i:s", strtotime($period) ); |
||
420 | |||
421 | //inner date query |
||
422 | $inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table "; |
||
423 | $inner_where = " WHERE"; |
||
424 | //exclude events not authored by user if permissions in effect |
||
425 | View Code Duplication | if ( ! EE_Registry::instance()->CAP->current_user_can( 'ee_read_others_registrations', 'reg_per_event_report' ) ) { |
|
426 | $inner_date_query .= "LEFT JOIN $event_table ON ID = EVT_ID"; |
||
427 | $inner_where .= " post_author = " . get_current_user_id() . " AND"; |
||
428 | } |
||
429 | $inner_where .= " REG_date >= '$sql_date'"; |
||
430 | $inner_date_query .= $inner_where; |
||
431 | |||
432 | //build main query |
||
433 | $select = "SELECT Event.post_title as Registration_Event, "; |
||
434 | $join = ''; |
||
435 | $join_parts = array(); |
||
436 | $select_parts = array(); |
||
437 | |||
438 | //loop through registration stati to do parts for each status. |
||
439 | View Code Duplication | foreach ( EEM_Registration::reg_status_array() as $STS_ID => $STS_code ) { |
|
440 | if ( $STS_ID === EEM_Registration::status_id_incomplete ) { |
||
441 | continue; |
||
442 | } |
||
443 | $select_parts[] = "COUNT($STS_code.REG_ID) as $STS_ID"; |
||
444 | $join_parts[] = "$registration_table AS $STS_code ON $STS_code.EVT_ID = dates.EVT_ID AND $STS_code.STS_ID = '$STS_ID' AND $STS_code.REG_date = dates.REG_date"; |
||
445 | } |
||
446 | |||
447 | //setup the selects |
||
448 | $select .= implode( ', ', $select_parts ); |
||
449 | $select .= " FROM ($inner_date_query) AS dates LEFT JOIN $event_table as Event ON Event.ID = dates.EVT_ID LEFT JOIN "; |
||
450 | |||
451 | //setup remaining joins |
||
452 | $join .= implode( " LEFT JOIN ", $join_parts ); |
||
453 | |||
454 | //now put it all together |
||
455 | $query = $select . $join . ' GROUP BY Registration_Event'; |
||
456 | |||
457 | //and execute |
||
458 | $results = $wpdb->get_results( |
||
459 | $query, |
||
460 | ARRAY_A |
||
461 | ); |
||
462 | return $results; |
||
463 | } |
||
464 | |||
465 | |||
466 | /** |
||
467 | * Returns the EE_Registration of the primary attendee on the transaction id provided |
||
468 | * @param int $TXN_ID |
||
469 | * @return EE_Registration |
||
470 | */ |
||
471 | public function get_primary_registration_for_transaction_ID( $TXN_ID = 0){ |
||
477 | |||
478 | |||
479 | /** |
||
480 | * get_event_registration_count |
||
481 | * |
||
482 | * @access public |
||
483 | * @param int $EVT_ID |
||
484 | * @param boolean $for_incomplete_payments |
||
485 | * @return int |
||
486 | */ |
||
487 | public function get_event_registration_count ( $EVT_ID, $for_incomplete_payments = FALSE ) { |
||
496 | |||
497 | /** |
||
498 | * Deletes all registrations with no transactions. Note that this needs to be very efficient |
||
499 | * and so it uses wpdb directly |
||
500 | * @global WPDB $wpdb |
||
501 | * @return int number deleted |
||
502 | */ |
||
503 | public function delete_registrations_with_no_transaction() { |
||
509 | |||
510 | /** |
||
511 | * Count registrations checked into (or out of) a datetime |
||
512 | * |
||
513 | * @param int $DTT_ID datetime ID |
||
514 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
515 | * @return int |
||
516 | */ |
||
517 | public function count_registrations_checked_into_datetime( $DTT_ID, $checked_in = true) { |
||
540 | |||
541 | /** |
||
542 | * Count registrations checked into (or out of) an event. |
||
543 | * |
||
544 | * @param int $EVT_ID event ID |
||
545 | * @param boolean $checked_in whether to count registrations checked IN or OUT |
||
546 | * @return int |
||
547 | */ |
||
548 | public function count_registrations_checked_into_event( $EVT_ID, $checked_in = true ) { |
||
573 | |||
574 | |||
575 | |||
576 | |||
577 | |||
578 | /** |
||
579 | * The purpose of this method is to retrieve an array of |
||
580 | * EE_Registration objects that represent the latest registration |
||
581 | * for each ATT_ID given in the function argument. |
||
582 | * |
||
583 | * @param array $attendee_ids |
||
584 | * @return EE_Registration[] |
||
585 | */ |
||
586 | public function get_latest_registration_for_each_of_given_contacts( $attendee_ids = array() ) { |
||
638 | |||
639 | |||
640 | |||
641 | } |
||
642 | // End of file EEM_Registration.model.php |
||
644 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.