Completed
Branch rest-authorization (c7240a)
by
unknown
13:01 queued 10:45
created
core/db_models/EEM_Currency_Payment_Method.model.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -34,7 +34,7 @@
 block discarded – undo
34 34
             'Payment_Method' => new EE_Belongs_To_Relation()
35 35
         );
36 36
         // this model is generally available for reading
37
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public();
37
+        $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public();
38 38
         $this->_caps_slug = 'payment_methods';
39 39
         parent::__construct($timezone);
40 40
     }
Please login to merge, or discard this patch.
Indentation   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -11,31 +11,31 @@
 block discarded – undo
11 11
  */
12 12
 class EEM_Currency_Payment_Method extends EEM_Base
13 13
 {
14
-    // private instance of the Attendee object
15
-    protected static $_instance = null;
14
+	// private instance of the Attendee object
15
+	protected static $_instance = null;
16 16
 
17 17
 
18
-    protected function __construct($timezone = null)
19
-    {
20
-        $this->singular_item = esc_html__('Currency Usable by Payment Method', 'event_espresso');
21
-        $this->plural_item = esc_html__('Currencies Usable by Payment Methods', 'event_espresso');
22
-        $this->_tables = array(
23
-            'Currency_Payment_Method' => new EE_Primary_Table('esp_currency_payment_method', 'CPM_ID')
24
-        );
25
-        $this->_fields = array(
26
-            'Currency_Payment_Method' => array(
27
-                'CPM_ID' => new EE_Primary_Key_Int_Field('CPM_ID', esc_html__('Currency to Payment Method LInk ID', 'event_espresso')),
28
-                'CUR_code' => new EE_Foreign_Key_String_Field('CUR_code', esc_html__('Currency Code', 'event_espresso'), false, '', 'Currency'),
29
-                'PMD_ID' => new EE_Foreign_Key_Int_Field('PMD_ID', esc_html__('Paymetn Method ID', 'event_espresso'), false, 0, 'Payment_Method')
30
-            )
31
-        );
32
-        $this->_model_relations = array(
33
-            'Currency' => new EE_Belongs_To_Relation(),
34
-            'Payment_Method' => new EE_Belongs_To_Relation()
35
-        );
36
-        // this model is generally available for reading
37
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public();
38
-        $this->_caps_slug = 'payment_methods';
39
-        parent::__construct($timezone);
40
-    }
18
+	protected function __construct($timezone = null)
19
+	{
20
+		$this->singular_item = esc_html__('Currency Usable by Payment Method', 'event_espresso');
21
+		$this->plural_item = esc_html__('Currencies Usable by Payment Methods', 'event_espresso');
22
+		$this->_tables = array(
23
+			'Currency_Payment_Method' => new EE_Primary_Table('esp_currency_payment_method', 'CPM_ID')
24
+		);
25
+		$this->_fields = array(
26
+			'Currency_Payment_Method' => array(
27
+				'CPM_ID' => new EE_Primary_Key_Int_Field('CPM_ID', esc_html__('Currency to Payment Method LInk ID', 'event_espresso')),
28
+				'CUR_code' => new EE_Foreign_Key_String_Field('CUR_code', esc_html__('Currency Code', 'event_espresso'), false, '', 'Currency'),
29
+				'PMD_ID' => new EE_Foreign_Key_Int_Field('PMD_ID', esc_html__('Paymetn Method ID', 'event_espresso'), false, 0, 'Payment_Method')
30
+			)
31
+		);
32
+		$this->_model_relations = array(
33
+			'Currency' => new EE_Belongs_To_Relation(),
34
+			'Payment_Method' => new EE_Belongs_To_Relation()
35
+		);
36
+		// this model is generally available for reading
37
+		$this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public();
38
+		$this->_caps_slug = 'payment_methods';
39
+		parent::__construct($timezone);
40
+	}
41 41
 }
Please login to merge, or discard this patch.
core/db_models/helpers/EE_Model_Parser.php 2 patches
Indentation   +144 added lines, -144 removed lines patch added patch discarded remove patch
@@ -9,153 +9,153 @@
 block discarded – undo
9 9
  */
10 10
 class EE_Model_Parser
11 11
 {
12
-    const table_alias_model_relation_chain_separator = '__';
13
-    const table_alias_model_relation_chain_prefix_end = '___';
14
-    /**
15
-     * Adds a period onto the front and end of the string. This often helps in searching.
16
-     * For example, if we want to find the model name "Event", it can be tricky when the following are possible
17
-     * "","Event.EVT_ID","Event","Event_Venue.Venue.VNU_ID",etc. It's easier to look for ".Event." in
18
-     * "..",".Event.EVT_ID.", ".Event.", and ".Event_Venue.Venue.VNU_ID", especially when the last example should NOT
19
-     * be found because the "Event" model isn't mentioned- it's just a string that has a model name that coincidentally
20
-     * has it as a substring
21
-     * @param string $string_to_pad
22
-     * @return string
23
-     */
24
-    public static function pad_with_periods($string_to_pad)
25
-    {
26
-        return "." . $string_to_pad . ".";
27
-    }
28
-    /**
29
-     * Basically undoes _pad_with_periods
30
-     * @param string $string_to_trim
31
-     * @return string
32
-     */
33
-    public static function trim_periods($string_to_trim)
34
-    {
35
-        return trim($string_to_trim, '.');
36
-    }
12
+	const table_alias_model_relation_chain_separator = '__';
13
+	const table_alias_model_relation_chain_prefix_end = '___';
14
+	/**
15
+	 * Adds a period onto the front and end of the string. This often helps in searching.
16
+	 * For example, if we want to find the model name "Event", it can be tricky when the following are possible
17
+	 * "","Event.EVT_ID","Event","Event_Venue.Venue.VNU_ID",etc. It's easier to look for ".Event." in
18
+	 * "..",".Event.EVT_ID.", ".Event.", and ".Event_Venue.Venue.VNU_ID", especially when the last example should NOT
19
+	 * be found because the "Event" model isn't mentioned- it's just a string that has a model name that coincidentally
20
+	 * has it as a substring
21
+	 * @param string $string_to_pad
22
+	 * @return string
23
+	 */
24
+	public static function pad_with_periods($string_to_pad)
25
+	{
26
+		return "." . $string_to_pad . ".";
27
+	}
28
+	/**
29
+	 * Basically undoes _pad_with_periods
30
+	 * @param string $string_to_trim
31
+	 * @return string
32
+	 */
33
+	public static function trim_periods($string_to_trim)
34
+	{
35
+		return trim($string_to_trim, '.');
36
+	}
37 37
 
38 38
 
39 39
 
40
-    /**
41
-     * Gets the calculated table's alias
42
-     * @param string $model_relation_chain or query param
43
-     * @param        $this_model_name
44
-     * @return string which can be added onto table aliases to make them unique
45
-     */
46
-    public static function extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this_model_name)
47
-    {
48
-        // eg $model_relation_chain = 'Venue.Event_Venue.Event.Registration", and $this_model_name = 'Event'
49
-        $model_relation_chain = self::pad_with_periods($model_relation_chain);
50
-        $this_model_name = self::pad_with_periods($this_model_name);
51
-        // eg '.Venue.Event_Venue.Event.Registration." and '.Event.'
52
-        // remove this model name and everything afterwards
53
-        $pos_of_model_name = strpos($model_relation_chain, $this_model_name);
54
-        $model_relation_chain = substr($model_relation_chain, 0, $pos_of_model_name);
55
-        // eg '.Venue.Event_Venue.'
56
-        // trim periods
57
-        $model_relation_chain = self::trim_periods($model_relation_chain);
58
-        // eg 'Venue.Event_Venue'
59
-        // replace periods with double-underscores
60
-        $model_relation_chain = str_replace(".", self::table_alias_model_relation_chain_separator, $model_relation_chain);
61
-        // eg 'Venue__Event_Venue'
62
-        if ($model_relation_chain != '') {
63
-            $model_relation_chain = $model_relation_chain . self::table_alias_model_relation_chain_prefix_end;
64
-        }
65
-        // eg 'Venue_Event_Venue___'
66
-        return $model_relation_chain;
67
-    }
68
-    /**
69
-     * Gets the table's alias (without prefix or anything)
70
-     * @param string $table_alias_with_model_relation_chain_prefix which CAN have a table alias model relation chain prefix (or not)
71
-     * @return string
72
-     */
73
-    public static function remove_table_alias_model_relation_chain_prefix($table_alias_with_model_relation_chain_prefix)
74
-    {
75
-        // does this actually have a table alias model relation chain prefix?
76
-        $pos = strpos($table_alias_with_model_relation_chain_prefix, self::table_alias_model_relation_chain_prefix_end);
77
-        if ($pos !== false) {
78
-            // yes
79
-            // find that triple underscore and remove it and everything before it
80
-            $table_alias = substr($table_alias_with_model_relation_chain_prefix, $pos + strlen(self::table_alias_model_relation_chain_prefix_end));
81
-        } else {
82
-            $table_alias = $table_alias_with_model_relation_chain_prefix;
83
-        }
84
-        return $table_alias;
85
-    }
86
-    /**
87
-     * Gets the table alias model relation chain prefix from the table alias already containing it
88
-     * @param string $table_alias_with_model_relation_chain_prefix
89
-     * @return string
90
-     */
91
-    public static function get_prefix_from_table_alias_with_model_relation_chain_prefix($table_alias_with_model_relation_chain_prefix)
92
-    {
93
-        // does this actually have a table alias model relation chain prefix?
94
-        $pos = strpos($table_alias_with_model_relation_chain_prefix, self::table_alias_model_relation_chain_prefix_end);
95
-        if ($pos !== false) {
96
-            // yes
97
-            // find that triple underscore and remove it and everything before it
98
-            $prefix = substr($table_alias_with_model_relation_chain_prefix, 0, $pos + strlen(self::table_alias_model_relation_chain_prefix_end));
99
-        } else {
100
-            $prefix = '';
101
-        }
102
-        return $prefix;
103
-    }
40
+	/**
41
+	 * Gets the calculated table's alias
42
+	 * @param string $model_relation_chain or query param
43
+	 * @param        $this_model_name
44
+	 * @return string which can be added onto table aliases to make them unique
45
+	 */
46
+	public static function extract_table_alias_model_relation_chain_prefix($model_relation_chain, $this_model_name)
47
+	{
48
+		// eg $model_relation_chain = 'Venue.Event_Venue.Event.Registration", and $this_model_name = 'Event'
49
+		$model_relation_chain = self::pad_with_periods($model_relation_chain);
50
+		$this_model_name = self::pad_with_periods($this_model_name);
51
+		// eg '.Venue.Event_Venue.Event.Registration." and '.Event.'
52
+		// remove this model name and everything afterwards
53
+		$pos_of_model_name = strpos($model_relation_chain, $this_model_name);
54
+		$model_relation_chain = substr($model_relation_chain, 0, $pos_of_model_name);
55
+		// eg '.Venue.Event_Venue.'
56
+		// trim periods
57
+		$model_relation_chain = self::trim_periods($model_relation_chain);
58
+		// eg 'Venue.Event_Venue'
59
+		// replace periods with double-underscores
60
+		$model_relation_chain = str_replace(".", self::table_alias_model_relation_chain_separator, $model_relation_chain);
61
+		// eg 'Venue__Event_Venue'
62
+		if ($model_relation_chain != '') {
63
+			$model_relation_chain = $model_relation_chain . self::table_alias_model_relation_chain_prefix_end;
64
+		}
65
+		// eg 'Venue_Event_Venue___'
66
+		return $model_relation_chain;
67
+	}
68
+	/**
69
+	 * Gets the table's alias (without prefix or anything)
70
+	 * @param string $table_alias_with_model_relation_chain_prefix which CAN have a table alias model relation chain prefix (or not)
71
+	 * @return string
72
+	 */
73
+	public static function remove_table_alias_model_relation_chain_prefix($table_alias_with_model_relation_chain_prefix)
74
+	{
75
+		// does this actually have a table alias model relation chain prefix?
76
+		$pos = strpos($table_alias_with_model_relation_chain_prefix, self::table_alias_model_relation_chain_prefix_end);
77
+		if ($pos !== false) {
78
+			// yes
79
+			// find that triple underscore and remove it and everything before it
80
+			$table_alias = substr($table_alias_with_model_relation_chain_prefix, $pos + strlen(self::table_alias_model_relation_chain_prefix_end));
81
+		} else {
82
+			$table_alias = $table_alias_with_model_relation_chain_prefix;
83
+		}
84
+		return $table_alias;
85
+	}
86
+	/**
87
+	 * Gets the table alias model relation chain prefix from the table alias already containing it
88
+	 * @param string $table_alias_with_model_relation_chain_prefix
89
+	 * @return string
90
+	 */
91
+	public static function get_prefix_from_table_alias_with_model_relation_chain_prefix($table_alias_with_model_relation_chain_prefix)
92
+	{
93
+		// does this actually have a table alias model relation chain prefix?
94
+		$pos = strpos($table_alias_with_model_relation_chain_prefix, self::table_alias_model_relation_chain_prefix_end);
95
+		if ($pos !== false) {
96
+			// yes
97
+			// find that triple underscore and remove it and everything before it
98
+			$prefix = substr($table_alias_with_model_relation_chain_prefix, 0, $pos + strlen(self::table_alias_model_relation_chain_prefix_end));
99
+		} else {
100
+			$prefix = '';
101
+		}
102
+		return $prefix;
103
+	}
104 104
 
105
-    /**
106
-     * Gets the table alias model relation chain prefix (ie, what can be prepended onto
107
-     * EE_Model_Field::get_qualified_column() to get the proper column name for that field
108
-     * in a specific query) from teh query param (eg 'Registration.Event.EVT_ID').
109
-     *
110
-     * @param string $model_name of the model on which the related query param was found to be belong
111
-     * @param string $original_query_param
112
-     * @return string
113
-     */
114
-    public static function extract_table_alias_model_relation_chain_from_query_param($model_name, $original_query_param)
115
-    {
116
-        $relation_chain = self::extract_model_relation_chain($model_name, $original_query_param);
117
-        $table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($relation_chain, $model_name);
118
-        return $table_alias_with_model_relation_chain_prefix;
119
-    }
120
-    /**
121
-     * Gets the model relation chain to $model_name from the $original_query_param.
122
-     * Eg, if $model_name were 'Payment', and $original_query_param were 'Registration.Transaction.Payment.PAY_ID',
123
-     * this would return 'Registration.Transaction.Payment'. Also if the query param were 'Registration.Transaction.Payment'
124
-     * and $model_name were 'Payment', it should return 'Registration.Transaction.Payment'
125
-     * @param string $model_name
126
-     * @param string $original_query_param
127
-     * @return string
128
-     */
129
-    public static function extract_model_relation_chain($model_name, $original_query_param)
130
-    {
131
-        // prefix and postfix both with a period, as this facilitates searching
132
-        $model_name = EE_Model_Parser::pad_with_periods($model_name);
133
-        $original_query_param = EE_Model_Parser::pad_with_periods($original_query_param);
134
-        $pos_of_model_string = strpos($original_query_param, $model_name);
135
-        // eg, if we're looking for the model relation chain from Event to Payment, the original query param is probably something like
136
-        // "Registration.Transaction.Payment.PAY_ID", $pos_of_model_string points to the 'P' or Payment. We want the string
137
-        // "Registration.Transaction.Payment"
138
-        $model_relation_chain = substr($original_query_param, 0, $pos_of_model_string + strlen($model_name));
139
-        return EE_Model_Parser::trim_periods($model_relation_chain);
140
-    }
105
+	/**
106
+	 * Gets the table alias model relation chain prefix (ie, what can be prepended onto
107
+	 * EE_Model_Field::get_qualified_column() to get the proper column name for that field
108
+	 * in a specific query) from teh query param (eg 'Registration.Event.EVT_ID').
109
+	 *
110
+	 * @param string $model_name of the model on which the related query param was found to be belong
111
+	 * @param string $original_query_param
112
+	 * @return string
113
+	 */
114
+	public static function extract_table_alias_model_relation_chain_from_query_param($model_name, $original_query_param)
115
+	{
116
+		$relation_chain = self::extract_model_relation_chain($model_name, $original_query_param);
117
+		$table_alias_with_model_relation_chain_prefix = EE_Model_Parser::extract_table_alias_model_relation_chain_prefix($relation_chain, $model_name);
118
+		return $table_alias_with_model_relation_chain_prefix;
119
+	}
120
+	/**
121
+	 * Gets the model relation chain to $model_name from the $original_query_param.
122
+	 * Eg, if $model_name were 'Payment', and $original_query_param were 'Registration.Transaction.Payment.PAY_ID',
123
+	 * this would return 'Registration.Transaction.Payment'. Also if the query param were 'Registration.Transaction.Payment'
124
+	 * and $model_name were 'Payment', it should return 'Registration.Transaction.Payment'
125
+	 * @param string $model_name
126
+	 * @param string $original_query_param
127
+	 * @return string
128
+	 */
129
+	public static function extract_model_relation_chain($model_name, $original_query_param)
130
+	{
131
+		// prefix and postfix both with a period, as this facilitates searching
132
+		$model_name = EE_Model_Parser::pad_with_periods($model_name);
133
+		$original_query_param = EE_Model_Parser::pad_with_periods($original_query_param);
134
+		$pos_of_model_string = strpos($original_query_param, $model_name);
135
+		// eg, if we're looking for the model relation chain from Event to Payment, the original query param is probably something like
136
+		// "Registration.Transaction.Payment.PAY_ID", $pos_of_model_string points to the 'P' or Payment. We want the string
137
+		// "Registration.Transaction.Payment"
138
+		$model_relation_chain = substr($original_query_param, 0, $pos_of_model_string + strlen($model_name));
139
+		return EE_Model_Parser::trim_periods($model_relation_chain);
140
+	}
141 141
 
142
-    /**
143
-     * Replaces the specified model in teh model relation chain with teh join model.
144
-     * Eg EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain(
145
-     * "Ticket", "Datetime_Ticket", "Datetime.Ticket" ) will return
146
-     * "Datetime.Datetime_Ticket" which can be used to find the table alias model relation chain prefix
147
-     * using EE_Model_Parser::extract_table_alias_model_relation_chain_prefix
148
-     * @param string $model_name
149
-     * @param string $join_model_name
150
-     * @param string $model_relation_chain
151
-     * @return string
152
-     */
153
-    public static function replace_model_name_with_join_model_name_in_model_relation_chain($model_name, $join_model_name, $model_relation_chain)
154
-    {
155
-        $model_name = EE_Model_Parser::pad_with_periods($model_name);
156
-        $join_model_name = EE_Model_Parser::pad_with_periods($join_model_name);
157
-        $model_relation_chain = EE_Model_Parser::pad_with_periods($model_relation_chain);
158
-        $replaced_with_periods = str_replace($model_name, $join_model_name, $model_relation_chain);
159
-        return EE_Model_Parser::trim_periods($replaced_with_periods);
160
-    }
142
+	/**
143
+	 * Replaces the specified model in teh model relation chain with teh join model.
144
+	 * Eg EE_Model_Parser::replace_model_name_with_join_model_name_in_model_relation_chain(
145
+	 * "Ticket", "Datetime_Ticket", "Datetime.Ticket" ) will return
146
+	 * "Datetime.Datetime_Ticket" which can be used to find the table alias model relation chain prefix
147
+	 * using EE_Model_Parser::extract_table_alias_model_relation_chain_prefix
148
+	 * @param string $model_name
149
+	 * @param string $join_model_name
150
+	 * @param string $model_relation_chain
151
+	 * @return string
152
+	 */
153
+	public static function replace_model_name_with_join_model_name_in_model_relation_chain($model_name, $join_model_name, $model_relation_chain)
154
+	{
155
+		$model_name = EE_Model_Parser::pad_with_periods($model_name);
156
+		$join_model_name = EE_Model_Parser::pad_with_periods($join_model_name);
157
+		$model_relation_chain = EE_Model_Parser::pad_with_periods($model_relation_chain);
158
+		$replaced_with_periods = str_replace($model_name, $join_model_name, $model_relation_chain);
159
+		return EE_Model_Parser::trim_periods($replaced_with_periods);
160
+	}
161 161
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -23,7 +23,7 @@  discard block
 block discarded – undo
23 23
      */
24 24
     public static function pad_with_periods($string_to_pad)
25 25
     {
26
-        return "." . $string_to_pad . ".";
26
+        return ".".$string_to_pad.".";
27 27
     }
28 28
     /**
29 29
      * Basically undoes _pad_with_periods
@@ -60,7 +60,7 @@  discard block
 block discarded – undo
60 60
         $model_relation_chain = str_replace(".", self::table_alias_model_relation_chain_separator, $model_relation_chain);
61 61
         // eg 'Venue__Event_Venue'
62 62
         if ($model_relation_chain != '') {
63
-            $model_relation_chain = $model_relation_chain . self::table_alias_model_relation_chain_prefix_end;
63
+            $model_relation_chain = $model_relation_chain.self::table_alias_model_relation_chain_prefix_end;
64 64
         }
65 65
         // eg 'Venue_Event_Venue___'
66 66
         return $model_relation_chain;
Please login to merge, or discard this patch.
core/db_models/helpers/EE_Secondary_Table.php 2 patches
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -8,118 +8,118 @@
 block discarded – undo
8 8
  */
9 9
 class EE_Secondary_Table extends EE_Table_Base
10 10
 {
11
-    protected $_extra_join_conditions;
11
+	protected $_extra_join_conditions;
12 12
 
13
-    /**
14
-     *
15
-     * @global type $wpdb
16
-     * @param string $table_name with or without wpdb prefix
17
-     * @param string $pk_column name of primary key column on THIS table
18
-     * @param string $fk_column the name of the COLUMN that is a foreign key to the primary table's primary key
19
-     * @param string $extra_join_conditions string for additional SQL to add onto the join statement's ON condition
20
-     * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install,
21
-     *                  or whether each site on a multisite install has a copy of this table
22
-     */
23
-    public function __construct($table_name, $pk_column, $fk_column = null, $extra_join_conditions = null, $global = false)
24
-    {
25
-        $this->_fk_on_table = $fk_column;
26
-        $this->_extra_join_conditions = $extra_join_conditions;
27
-        parent::__construct($table_name, $pk_column, $global);
28
-    }
29
-    public function get_fk_on_table()
30
-    {
31
-        return $this->_fk_on_table;
32
-    }
33
-    public function _construct_finalize_set_table_to_join_with(EE_Table_Base $table)
34
-    {
35
-        $this->_table_to_join_with = $table;
36
-    }
37
-    /**
38
-     *
39
-     * @return string of sql like "Event.post_type = 'event'", which gets added to
40
-     * the end of the join statement with the primary table
41
-     */
42
-    public function get_extra_join_conditions()
43
-    {
44
-        return $this->_extra_join_conditions;
45
-    }
46
-    /**
47
-     *
48
-     * @return EE_Primary_Table
49
-     */
50
-    public function get_table_to_join_with()
51
-    {
52
-        return $this->_table_to_join_with;
53
-    }
54
-    /**
55
-     * creates join statement FROM primary table
56
-     * gets SQL like "LEFT JOIN table_name AS table_alias ON other_table_alias.pk = table_alias.fk
57
-     *
58
-     * @param string $table allows us to set special conditions on the $table_name portion of the join query (i.e. doing a subquery)
59
-     * @return string of SQL
60
-     */
61
-    public function get_join_sql($primary_table_alias_with_model_chain_prefix)
62
-    {
13
+	/**
14
+	 *
15
+	 * @global type $wpdb
16
+	 * @param string $table_name with or without wpdb prefix
17
+	 * @param string $pk_column name of primary key column on THIS table
18
+	 * @param string $fk_column the name of the COLUMN that is a foreign key to the primary table's primary key
19
+	 * @param string $extra_join_conditions string for additional SQL to add onto the join statement's ON condition
20
+	 * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install,
21
+	 *                  or whether each site on a multisite install has a copy of this table
22
+	 */
23
+	public function __construct($table_name, $pk_column, $fk_column = null, $extra_join_conditions = null, $global = false)
24
+	{
25
+		$this->_fk_on_table = $fk_column;
26
+		$this->_extra_join_conditions = $extra_join_conditions;
27
+		parent::__construct($table_name, $pk_column, $global);
28
+	}
29
+	public function get_fk_on_table()
30
+	{
31
+		return $this->_fk_on_table;
32
+	}
33
+	public function _construct_finalize_set_table_to_join_with(EE_Table_Base $table)
34
+	{
35
+		$this->_table_to_join_with = $table;
36
+	}
37
+	/**
38
+	 *
39
+	 * @return string of sql like "Event.post_type = 'event'", which gets added to
40
+	 * the end of the join statement with the primary table
41
+	 */
42
+	public function get_extra_join_conditions()
43
+	{
44
+		return $this->_extra_join_conditions;
45
+	}
46
+	/**
47
+	 *
48
+	 * @return EE_Primary_Table
49
+	 */
50
+	public function get_table_to_join_with()
51
+	{
52
+		return $this->_table_to_join_with;
53
+	}
54
+	/**
55
+	 * creates join statement FROM primary table
56
+	 * gets SQL like "LEFT JOIN table_name AS table_alias ON other_table_alias.pk = table_alias.fk
57
+	 *
58
+	 * @param string $table allows us to set special conditions on the $table_name portion of the join query (i.e. doing a subquery)
59
+	 * @return string of SQL
60
+	 */
61
+	public function get_join_sql($primary_table_alias_with_model_chain_prefix)
62
+	{
63 63
 
64
-        $table_name = $this->get_table_name();
65
-        $secondary_table_alias = EE_Model_Parser::get_prefix_from_table_alias_with_model_relation_chain_prefix($primary_table_alias_with_model_chain_prefix) . $this->get_table_alias();
66
-        $other_table_pk = $this->get_table_to_join_with()->get_pk_column();
67
-        $fk = $this->get_fk_on_table();
68
-        $join_sql = " LEFT JOIN $table_name AS $secondary_table_alias ON $primary_table_alias_with_model_chain_prefix.$other_table_pk = $secondary_table_alias.$fk ";
69
-        if ($this->get_extra_join_conditions()) {
70
-            $join_sql .= "AND " . $this->get_extra_join_conditions();
71
-        }
72
-        return $join_sql;
73
-    }
64
+		$table_name = $this->get_table_name();
65
+		$secondary_table_alias = EE_Model_Parser::get_prefix_from_table_alias_with_model_relation_chain_prefix($primary_table_alias_with_model_chain_prefix) . $this->get_table_alias();
66
+		$other_table_pk = $this->get_table_to_join_with()->get_pk_column();
67
+		$fk = $this->get_fk_on_table();
68
+		$join_sql = " LEFT JOIN $table_name AS $secondary_table_alias ON $primary_table_alias_with_model_chain_prefix.$other_table_pk = $secondary_table_alias.$fk ";
69
+		if ($this->get_extra_join_conditions()) {
70
+			$join_sql .= "AND " . $this->get_extra_join_conditions();
71
+		}
72
+		return $join_sql;
73
+	}
74 74
 
75 75
 
76
-    /**
77
-     * Produces join SQL like get_join_sql, except instead of joining the primary table to the
78
-     * secondary table, does the inverse: joins the secondary table to the primary one. (Eg, isntead of
79
-     * " LEFT JOIN secondary_table_table AS Secondary ON ..." like get_join_sql, this function returns
80
-     * " LEFT JOIN primary_table AS Primary ON ...".
81
-     * This is useful if the secondary table is already included in the SQL, but the primary table is not yet.
82
-     * @return string
83
-     */
84
-    public function get_inverse_join_sql($secondary_table_alias_with_model_chain_prefix)
85
-    {
86
-        $primary_table_name = $this->get_table_to_join_with()->get_table_name();
87
-        $primary_table_alias = EE_Model_Parser::get_prefix_from_table_alias_with_model_relation_chain_prefix($secondary_table_alias_with_model_chain_prefix) . $this->get_table_to_join_with()->get_table_alias();
88
-        $primary_table_pk = $this->get_table_to_join_with()->get_pk_column();// $this->get_pk_column();
89
-        $fk = $this->get_fk_on_table();
90
-        $join_sql = " LEFT JOIN $primary_table_name AS $primary_table_alias ON $primary_table_alias.$primary_table_pk = $secondary_table_alias_with_model_chain_prefix.$fk ";
91
-        if ($this->get_extra_join_conditions()) {
92
-            $join_sql .= "AND " . $this->get_extra_join_conditions();
93
-        }
94
-        return $join_sql;
95
-    }
76
+	/**
77
+	 * Produces join SQL like get_join_sql, except instead of joining the primary table to the
78
+	 * secondary table, does the inverse: joins the secondary table to the primary one. (Eg, isntead of
79
+	 * " LEFT JOIN secondary_table_table AS Secondary ON ..." like get_join_sql, this function returns
80
+	 * " LEFT JOIN primary_table AS Primary ON ...".
81
+	 * This is useful if the secondary table is already included in the SQL, but the primary table is not yet.
82
+	 * @return string
83
+	 */
84
+	public function get_inverse_join_sql($secondary_table_alias_with_model_chain_prefix)
85
+	{
86
+		$primary_table_name = $this->get_table_to_join_with()->get_table_name();
87
+		$primary_table_alias = EE_Model_Parser::get_prefix_from_table_alias_with_model_relation_chain_prefix($secondary_table_alias_with_model_chain_prefix) . $this->get_table_to_join_with()->get_table_alias();
88
+		$primary_table_pk = $this->get_table_to_join_with()->get_pk_column();// $this->get_pk_column();
89
+		$fk = $this->get_fk_on_table();
90
+		$join_sql = " LEFT JOIN $primary_table_name AS $primary_table_alias ON $primary_table_alias.$primary_table_pk = $secondary_table_alias_with_model_chain_prefix.$fk ";
91
+		if ($this->get_extra_join_conditions()) {
92
+			$join_sql .= "AND " . $this->get_extra_join_conditions();
93
+		}
94
+		return $join_sql;
95
+	}
96 96
 
97
-    /**
98
-     * This prepares the join on the other table using a select with a internal limit.
99
-     * @param  mixed (array|string) $limit limit
100
-     * @return string                       SQL to return
101
-     */
102
-    public function get_select_join_limit_join($limit)
103
-    {
104
-        // first get the select
105
-        $select = $this->get_select_join_limit($limit);
106
-        $join_sql = $this->get_join_sql($select);
107
-        return $join_sql;
108
-    }
97
+	/**
98
+	 * This prepares the join on the other table using a select with a internal limit.
99
+	 * @param  mixed (array|string) $limit limit
100
+	 * @return string                       SQL to return
101
+	 */
102
+	public function get_select_join_limit_join($limit)
103
+	{
104
+		// first get the select
105
+		$select = $this->get_select_join_limit($limit);
106
+		$join_sql = $this->get_join_sql($select);
107
+		return $join_sql;
108
+	}
109 109
 
110 110
 
111 111
 
112
-    public function get_fully_qualified_fk_column()
113
-    {
114
-        $table_alias = $this->get_table_alias();
115
-        $fk = $this->get_fk_on_table();
116
-        return $table_alias . '.' . $fk;
117
-    }
112
+	public function get_fully_qualified_fk_column()
113
+	{
114
+		$table_alias = $this->get_table_alias();
115
+		$fk = $this->get_fk_on_table();
116
+		return $table_alias . '.' . $fk;
117
+	}
118 118
 
119
-    public function get_fully_qualified_pk_on_fk_table()
120
-    {
121
-        $table_alias = $this->get_table_to_join_with()->get_table_alias();
122
-        $pk = $this->get_table_to_join_with()->get_pk_column();
123
-        return $table_alias . '.' . $pk;
124
-    }
119
+	public function get_fully_qualified_pk_on_fk_table()
120
+	{
121
+		$table_alias = $this->get_table_to_join_with()->get_table_alias();
122
+		$pk = $this->get_table_to_join_with()->get_pk_column();
123
+		return $table_alias . '.' . $pk;
124
+	}
125 125
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -62,12 +62,12 @@  discard block
 block discarded – undo
62 62
     {
63 63
 
64 64
         $table_name = $this->get_table_name();
65
-        $secondary_table_alias = EE_Model_Parser::get_prefix_from_table_alias_with_model_relation_chain_prefix($primary_table_alias_with_model_chain_prefix) . $this->get_table_alias();
65
+        $secondary_table_alias = EE_Model_Parser::get_prefix_from_table_alias_with_model_relation_chain_prefix($primary_table_alias_with_model_chain_prefix).$this->get_table_alias();
66 66
         $other_table_pk = $this->get_table_to_join_with()->get_pk_column();
67 67
         $fk = $this->get_fk_on_table();
68 68
         $join_sql = " LEFT JOIN $table_name AS $secondary_table_alias ON $primary_table_alias_with_model_chain_prefix.$other_table_pk = $secondary_table_alias.$fk ";
69 69
         if ($this->get_extra_join_conditions()) {
70
-            $join_sql .= "AND " . $this->get_extra_join_conditions();
70
+            $join_sql .= "AND ".$this->get_extra_join_conditions();
71 71
         }
72 72
         return $join_sql;
73 73
     }
@@ -84,12 +84,12 @@  discard block
 block discarded – undo
84 84
     public function get_inverse_join_sql($secondary_table_alias_with_model_chain_prefix)
85 85
     {
86 86
         $primary_table_name = $this->get_table_to_join_with()->get_table_name();
87
-        $primary_table_alias = EE_Model_Parser::get_prefix_from_table_alias_with_model_relation_chain_prefix($secondary_table_alias_with_model_chain_prefix) . $this->get_table_to_join_with()->get_table_alias();
88
-        $primary_table_pk = $this->get_table_to_join_with()->get_pk_column();// $this->get_pk_column();
87
+        $primary_table_alias = EE_Model_Parser::get_prefix_from_table_alias_with_model_relation_chain_prefix($secondary_table_alias_with_model_chain_prefix).$this->get_table_to_join_with()->get_table_alias();
88
+        $primary_table_pk = $this->get_table_to_join_with()->get_pk_column(); // $this->get_pk_column();
89 89
         $fk = $this->get_fk_on_table();
90 90
         $join_sql = " LEFT JOIN $primary_table_name AS $primary_table_alias ON $primary_table_alias.$primary_table_pk = $secondary_table_alias_with_model_chain_prefix.$fk ";
91 91
         if ($this->get_extra_join_conditions()) {
92
-            $join_sql .= "AND " . $this->get_extra_join_conditions();
92
+            $join_sql .= "AND ".$this->get_extra_join_conditions();
93 93
         }
94 94
         return $join_sql;
95 95
     }
@@ -113,13 +113,13 @@  discard block
 block discarded – undo
113 113
     {
114 114
         $table_alias = $this->get_table_alias();
115 115
         $fk = $this->get_fk_on_table();
116
-        return $table_alias . '.' . $fk;
116
+        return $table_alias.'.'.$fk;
117 117
     }
118 118
 
119 119
     public function get_fully_qualified_pk_on_fk_table()
120 120
     {
121 121
         $table_alias = $this->get_table_to_join_with()->get_table_alias();
122 122
         $pk = $this->get_table_to_join_with()->get_pk_column();
123
-        return $table_alias . '.' . $pk;
123
+        return $table_alias.'.'.$pk;
124 124
     }
125 125
 }
Please login to merge, or discard this patch.
core/db_models/helpers/EE_Index.php 2 patches
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -6,45 +6,45 @@
 block discarded – undo
6 6
  */
7 7
 class EE_Index
8 8
 {
9
-    protected $_name;
10
-    protected $_field_names;
11
-    protected $_model_name;
12
-    public function __construct($fields)
13
-    {
14
-        $this->_field_names = $fields;
15
-    }
16
-    public function _construct_finalize($name, $model_name)
17
-    {
18
-        $this->_name = $name;
19
-        $this->_model_name = $model_name;
20
-    }
21
-    public function field_names()
22
-    {
23
-        return $this->_field_names;
24
-    }
25
-    /**
26
-     * Internally used by get_this_model() and get_other_model()
27
-     * @param string $model_name like Event, Question_Group, etc. omit the EEM_
28
-     * @return EEM_Base
29
-     */
30
-    protected function _get_model($model_name)
31
-    {
32
-        $modelInstance = call_user_func("EEM_" . $model_name . "::instance");
33
-        return $modelInstance;
34
-    }
35
-    /**
36
-     * Gets all the fields for this index
37
-     * @return EE_Model_Field_Base[]
38
-     */
39
-    public function fields()
40
-    {
41
-        $fields = array();
42
-        $model = $this->_get_model($this->_model_name);
43
-        foreach ($model->field_settings() as $field_name => $field_obj) {
44
-            if (in_array($field_name, $this->field_names())) {
45
-                $fields[ $field_name ] = $field_obj;
46
-            }
47
-        }
48
-        return $fields;
49
-    }
9
+	protected $_name;
10
+	protected $_field_names;
11
+	protected $_model_name;
12
+	public function __construct($fields)
13
+	{
14
+		$this->_field_names = $fields;
15
+	}
16
+	public function _construct_finalize($name, $model_name)
17
+	{
18
+		$this->_name = $name;
19
+		$this->_model_name = $model_name;
20
+	}
21
+	public function field_names()
22
+	{
23
+		return $this->_field_names;
24
+	}
25
+	/**
26
+	 * Internally used by get_this_model() and get_other_model()
27
+	 * @param string $model_name like Event, Question_Group, etc. omit the EEM_
28
+	 * @return EEM_Base
29
+	 */
30
+	protected function _get_model($model_name)
31
+	{
32
+		$modelInstance = call_user_func("EEM_" . $model_name . "::instance");
33
+		return $modelInstance;
34
+	}
35
+	/**
36
+	 * Gets all the fields for this index
37
+	 * @return EE_Model_Field_Base[]
38
+	 */
39
+	public function fields()
40
+	{
41
+		$fields = array();
42
+		$model = $this->_get_model($this->_model_name);
43
+		foreach ($model->field_settings() as $field_name => $field_obj) {
44
+			if (in_array($field_name, $this->field_names())) {
45
+				$fields[ $field_name ] = $field_obj;
46
+			}
47
+		}
48
+		return $fields;
49
+	}
50 50
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
      */
30 30
     protected function _get_model($model_name)
31 31
     {
32
-        $modelInstance = call_user_func("EEM_" . $model_name . "::instance");
32
+        $modelInstance = call_user_func("EEM_".$model_name."::instance");
33 33
         return $modelInstance;
34 34
     }
35 35
     /**
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
         $model = $this->_get_model($this->_model_name);
43 43
         foreach ($model->field_settings() as $field_name => $field_obj) {
44 44
             if (in_array($field_name, $this->field_names())) {
45
-                $fields[ $field_name ] = $field_obj;
45
+                $fields[$field_name] = $field_obj;
46 46
             }
47 47
         }
48 48
         return $fields;
Please login to merge, or discard this patch.
core/db_models/helpers/EE_Primary_Table.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -25,6 +25,6 @@
 block discarded – undo
25 25
      */
26 26
     public function get_table_sql()
27 27
     {
28
-        return " " . $this->get_table_name() . " AS " . $this->get_table_alias() . " ";
28
+        return " ".$this->get_table_name()." AS ".$this->get_table_alias()." ";
29 29
     }
30 30
 }
Please login to merge, or discard this patch.
Indentation   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -6,24 +6,24 @@
 block discarded – undo
6 6
  */
7 7
 class EE_Primary_Table extends EE_Table_Base
8 8
 {
9
-    /**
10
-     *
11
-     * @global type $wpdb
12
-     * @param string $table_name with or without wpdb prefix
13
-     * @param string $pk_column name of primary key column
14
-     * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install,
15
-     *                  or whether each site on a multisite install has a copy of this table
16
-     */
17
-    public function __construct($table_name, $pk_column = null, $global = false)
18
-    {
19
-        parent::__construct($table_name, $pk_column, $global);
20
-    }
21
-    /**
22
-     * Gets SQL for this table and assigning it an alias. Eg " wp_esp_attendee AS Attendee "
23
-     * @return string
24
-     */
25
-    public function get_table_sql()
26
-    {
27
-        return " " . $this->get_table_name() . " AS " . $this->get_table_alias() . " ";
28
-    }
9
+	/**
10
+	 *
11
+	 * @global type $wpdb
12
+	 * @param string $table_name with or without wpdb prefix
13
+	 * @param string $pk_column name of primary key column
14
+	 * @param boolean $global whether the table is "global" as in there is only 1 table on an entire multisite install,
15
+	 *                  or whether each site on a multisite install has a copy of this table
16
+	 */
17
+	public function __construct($table_name, $pk_column = null, $global = false)
18
+	{
19
+		parent::__construct($table_name, $pk_column, $global);
20
+	}
21
+	/**
22
+	 * Gets SQL for this table and assigning it an alias. Eg " wp_esp_attendee AS Attendee "
23
+	 * @return string
24
+	 */
25
+	public function get_table_sql()
26
+	{
27
+		return " " . $this->get_table_name() . " AS " . $this->get_table_alias() . " ";
28
+	}
29 29
 }
Please login to merge, or discard this patch.
core/db_models/EEM_State.model.php 2 patches
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -42,7 +42,7 @@  discard block
 block discarded – undo
42 42
             'Venue' => new EE_Has_Many_Relation(),
43 43
         );
44 44
         // this model is generally available for reading
45
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public();
45
+        $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public();
46 46
         // @todo: only show STA_active
47 47
         parent::__construct($timezone);
48 48
     }
@@ -73,8 +73,8 @@  discard block
 block discarded – undo
73 73
     */
74 74
     public function get_all_states()
75 75
     {
76
-        if (! self::$_all_states) {
77
-            self::$_all_states = $this->get_all(array( 'order_by' => array( 'STA_name' => 'ASC' ), 'limit' => array( 0, 99999 )));
76
+        if ( ! self::$_all_states) {
77
+            self::$_all_states = $this->get_all(array('order_by' => array('STA_name' => 'ASC'), 'limit' => array(0, 99999)));
78 78
         }
79 79
         return self::$_all_states;
80 80
     }
@@ -91,13 +91,13 @@  discard block
 block discarded – undo
91 91
      */
92 92
     public function get_all_active_states($countries = array(), $flush_cache = false)
93 93
     {
94
-        if (! self::$_active_states || $flush_cache) {
94
+        if ( ! self::$_active_states || $flush_cache) {
95 95
             $countries = is_array($countries) && ! empty($countries) ? $countries : EEM_Country::instance()->get_all_active_countries();
96
-            self::$_active_states =  $this->get_all(array(
97
-                array( 'STA_active' => true, 'CNT_ISO' => array( 'IN', array_keys($countries))),
98
-                'order_by' => array( 'STA_name' => 'ASC' ),
99
-                'limit' => array( 0, 99999 ),
100
-                'force_join' => array( 'Country' )
96
+            self::$_active_states = $this->get_all(array(
97
+                array('STA_active' => true, 'CNT_ISO' => array('IN', array_keys($countries))),
98
+                'order_by' => array('STA_name' => 'ASC'),
99
+                'limit' => array(0, 99999),
100
+                'force_join' => array('Country')
101 101
             ));
102 102
         }
103 103
         return self::$_active_states;
@@ -111,7 +111,7 @@  discard block
 block discarded – undo
111 111
      */
112 112
     public function get_all_states_of_active_countries()
113 113
     {
114
-        if ($states = $this->get_all(array( array( 'Country.CNT_active' => true, 'STA_active' => true ),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
114
+        if ($states = $this->get_all(array(array('Country.CNT_active' => true, 'STA_active' => true), 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC')))) {
115 115
             return $states;
116 116
         }
117 117
         return false;
@@ -125,10 +125,10 @@  discard block
 block discarded – undo
125 125
      */
126 126
     public function get_all_active_states_for_these_countries($countries)
127 127
     {
128
-        if (! $countries) {
128
+        if ( ! $countries) {
129 129
             return false;
130 130
         }
131
-        if ($states = $this->get_all(array(  array( 'Country.CNT_ISO' => array( 'IN', array_keys($countries)), 'STA_active' => true ),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
131
+        if ($states = $this->get_all(array(array('Country.CNT_ISO' => array('IN', array_keys($countries)), 'STA_active' => true), 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC')))) {
132 132
             return $states;
133 133
         }
134 134
         return false;
@@ -142,10 +142,10 @@  discard block
 block discarded – undo
142 142
      */
143 143
     public function get_all_states_for_these_countries($countries)
144 144
     {
145
-        if (! $countries) {
145
+        if ( ! $countries) {
146 146
             return false;
147 147
         }
148
-        if ($states = $this->get_all(array( array( 'Country.CNT_ISO' => array( 'IN', array_keys($countries))),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
148
+        if ($states = $this->get_all(array(array('Country.CNT_ISO' => array('IN', array_keys($countries))), 'order_by' => array('Country.CNT_name' => 'ASC', 'STA_name' => 'ASC')))) {
149 149
             return $states;
150 150
         }
151 151
         return false;
@@ -159,12 +159,12 @@  discard block
 block discarded – undo
159 159
     public function get_state_name_by_ID($state_ID)
160 160
     {
161 161
         if (
162
-            isset(self::$_all_states[ $state_ID ]) &&
163
-                self::$_all_states[ $state_ID ] instanceof EE_State
162
+            isset(self::$_all_states[$state_ID]) &&
163
+                self::$_all_states[$state_ID] instanceof EE_State
164 164
         ) {
165
-            return self::$_all_states[ $state_ID ]->name();
165
+            return self::$_all_states[$state_ID]->name();
166 166
         }
167
-        $names = $this->get_col(array( array( 'STA_ID' => $state_ID ), 'limit' => 1), 'STA_name');
167
+        $names = $this->get_col(array(array('STA_ID' => $state_ID), 'limit' => 1), 'STA_name');
168 168
         if (is_array($names) && ! empty($names)) {
169 169
             return reset($names);
170 170
         } else {
Please login to merge, or discard this patch.
Indentation   +159 added lines, -159 removed lines patch added patch discarded remove patch
@@ -11,163 +11,163 @@
 block discarded – undo
11 11
  */
12 12
 class EEM_State extends EEM_Base
13 13
 {
14
-    // private instance of the Attendee object
15
-    protected static $_instance = null;
16
-    // array of all states
17
-    private static $_all_states = false;
18
-    // array of all active states
19
-    private static $_active_states = false;
20
-
21
-    protected function __construct($timezone = null)
22
-    {
23
-        $this->singular_item = esc_html__('State/Province', 'event_espresso');
24
-        $this->plural_item = esc_html__('States/Provinces', 'event_espresso');
25
-
26
-        $this->_tables = array(
27
-            'State' => new EE_Primary_Table('esp_state', 'STA_ID')
28
-        );
29
-
30
-        $this->_fields = array(
31
-            'State' => array(
32
-                'STA_ID' => new EE_Primary_Key_Int_Field('STA_ID', esc_html__('State ID', 'event_espresso')),
33
-                'CNT_ISO' => new EE_Foreign_Key_String_Field('CNT_ISO', esc_html__('Country ISO Code', 'event_espresso'), false, null, 'Country'),
34
-                'STA_abbrev' => new EE_Plain_Text_Field('STA_abbrev', esc_html__('State Abbreviation', 'event_espresso'), false, ''),
35
-                'STA_name' => new EE_Plain_Text_Field('STA_name', esc_html__('State Name', 'event_espresso'), false, ''),
36
-                'STA_active' => new EE_Boolean_Field('STA_active', esc_html__('State Active Flag', 'event_espresso'), false, false)
37
-                ));
38
-        $this->_model_relations = array(
39
-            'Attendee' => new EE_Has_Many_Relation(),
40
-            'Country' => new EE_Belongs_To_Relation(),
41
-            'Venue' => new EE_Has_Many_Relation(),
42
-        );
43
-        // this model is generally available for reading
44
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public();
45
-        // @todo: only show STA_active
46
-        parent::__construct($timezone);
47
-    }
48
-
49
-
50
-
51
-
52
-    /**
53
-    *   reset_cached_states
54
-    *
55
-    *   @access     private
56
-    *   @return         void
57
-    */
58
-    public function reset_cached_states()
59
-    {
60
-        EEM_State::$_active_states = array();
61
-        EEM_State::$_all_states = array();
62
-    }
63
-
64
-
65
-
66
-
67
-    /**
68
-    *       _get_states
69
-    *
70
-    *       @access     private
71
-    *       @return         array
72
-    */
73
-    public function get_all_states()
74
-    {
75
-        if (! self::$_all_states) {
76
-            self::$_all_states = $this->get_all(array( 'order_by' => array( 'STA_name' => 'ASC' ), 'limit' => array( 0, 99999 )));
77
-        }
78
-        return self::$_all_states;
79
-    }
80
-
81
-
82
-
83
-    /**
84
-     *        _get_states
85
-     *
86
-     * @access        public
87
-     * @param array $countries
88
-     * @param bool  $flush_cache
89
-     * @return        array
90
-     */
91
-    public function get_all_active_states($countries = array(), $flush_cache = false)
92
-    {
93
-        if (! self::$_active_states || $flush_cache) {
94
-            $countries = is_array($countries) && ! empty($countries) ? $countries : EEM_Country::instance()->get_all_active_countries();
95
-            self::$_active_states =  $this->get_all(array(
96
-                array( 'STA_active' => true, 'CNT_ISO' => array( 'IN', array_keys($countries))),
97
-                'order_by' => array( 'STA_name' => 'ASC' ),
98
-                'limit' => array( 0, 99999 ),
99
-                'force_join' => array( 'Country' )
100
-            ));
101
-        }
102
-        return self::$_active_states;
103
-    }
104
-
105
-
106
-
107
-    /**
108
-     *  get_all_states_of_active_countries
109
-     * @return array
110
-     */
111
-    public function get_all_states_of_active_countries()
112
-    {
113
-        if ($states = $this->get_all(array( array( 'Country.CNT_active' => true, 'STA_active' => true ),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
114
-            return $states;
115
-        }
116
-        return false;
117
-    }
118
-
119
-
120
-
121
-    /**
122
-     *  get_all_states_of_active_countries
123
-     * @return array
124
-     */
125
-    public function get_all_active_states_for_these_countries($countries)
126
-    {
127
-        if (! $countries) {
128
-            return false;
129
-        }
130
-        if ($states = $this->get_all(array(  array( 'Country.CNT_ISO' => array( 'IN', array_keys($countries)), 'STA_active' => true ),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
131
-            return $states;
132
-        }
133
-        return false;
134
-    }
135
-
136
-
137
-
138
-    /**
139
-     *  get_all_states_of_active_countries
140
-     * @return array
141
-     */
142
-    public function get_all_states_for_these_countries($countries)
143
-    {
144
-        if (! $countries) {
145
-            return false;
146
-        }
147
-        if ($states = $this->get_all(array( array( 'Country.CNT_ISO' => array( 'IN', array_keys($countries))),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
148
-            return $states;
149
-        }
150
-        return false;
151
-    }
152
-
153
-    /**
154
-     * Gets the state's name by its ID
155
-     * @param string $state_ID
156
-     * @return string
157
-     */
158
-    public function get_state_name_by_ID($state_ID)
159
-    {
160
-        if (
161
-            isset(self::$_all_states[ $state_ID ]) &&
162
-                self::$_all_states[ $state_ID ] instanceof EE_State
163
-        ) {
164
-            return self::$_all_states[ $state_ID ]->name();
165
-        }
166
-        $names = $this->get_col(array( array( 'STA_ID' => $state_ID ), 'limit' => 1), 'STA_name');
167
-        if (is_array($names) && ! empty($names)) {
168
-            return reset($names);
169
-        } else {
170
-            return '';
171
-        }
172
-    }
14
+	// private instance of the Attendee object
15
+	protected static $_instance = null;
16
+	// array of all states
17
+	private static $_all_states = false;
18
+	// array of all active states
19
+	private static $_active_states = false;
20
+
21
+	protected function __construct($timezone = null)
22
+	{
23
+		$this->singular_item = esc_html__('State/Province', 'event_espresso');
24
+		$this->plural_item = esc_html__('States/Provinces', 'event_espresso');
25
+
26
+		$this->_tables = array(
27
+			'State' => new EE_Primary_Table('esp_state', 'STA_ID')
28
+		);
29
+
30
+		$this->_fields = array(
31
+			'State' => array(
32
+				'STA_ID' => new EE_Primary_Key_Int_Field('STA_ID', esc_html__('State ID', 'event_espresso')),
33
+				'CNT_ISO' => new EE_Foreign_Key_String_Field('CNT_ISO', esc_html__('Country ISO Code', 'event_espresso'), false, null, 'Country'),
34
+				'STA_abbrev' => new EE_Plain_Text_Field('STA_abbrev', esc_html__('State Abbreviation', 'event_espresso'), false, ''),
35
+				'STA_name' => new EE_Plain_Text_Field('STA_name', esc_html__('State Name', 'event_espresso'), false, ''),
36
+				'STA_active' => new EE_Boolean_Field('STA_active', esc_html__('State Active Flag', 'event_espresso'), false, false)
37
+				));
38
+		$this->_model_relations = array(
39
+			'Attendee' => new EE_Has_Many_Relation(),
40
+			'Country' => new EE_Belongs_To_Relation(),
41
+			'Venue' => new EE_Has_Many_Relation(),
42
+		);
43
+		// this model is generally available for reading
44
+		$this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public();
45
+		// @todo: only show STA_active
46
+		parent::__construct($timezone);
47
+	}
48
+
49
+
50
+
51
+
52
+	/**
53
+	 *   reset_cached_states
54
+	 *
55
+	 *   @access     private
56
+	 *   @return         void
57
+	 */
58
+	public function reset_cached_states()
59
+	{
60
+		EEM_State::$_active_states = array();
61
+		EEM_State::$_all_states = array();
62
+	}
63
+
64
+
65
+
66
+
67
+	/**
68
+	 *       _get_states
69
+	 *
70
+	 *       @access     private
71
+	 *       @return         array
72
+	 */
73
+	public function get_all_states()
74
+	{
75
+		if (! self::$_all_states) {
76
+			self::$_all_states = $this->get_all(array( 'order_by' => array( 'STA_name' => 'ASC' ), 'limit' => array( 0, 99999 )));
77
+		}
78
+		return self::$_all_states;
79
+	}
80
+
81
+
82
+
83
+	/**
84
+	 *        _get_states
85
+	 *
86
+	 * @access        public
87
+	 * @param array $countries
88
+	 * @param bool  $flush_cache
89
+	 * @return        array
90
+	 */
91
+	public function get_all_active_states($countries = array(), $flush_cache = false)
92
+	{
93
+		if (! self::$_active_states || $flush_cache) {
94
+			$countries = is_array($countries) && ! empty($countries) ? $countries : EEM_Country::instance()->get_all_active_countries();
95
+			self::$_active_states =  $this->get_all(array(
96
+				array( 'STA_active' => true, 'CNT_ISO' => array( 'IN', array_keys($countries))),
97
+				'order_by' => array( 'STA_name' => 'ASC' ),
98
+				'limit' => array( 0, 99999 ),
99
+				'force_join' => array( 'Country' )
100
+			));
101
+		}
102
+		return self::$_active_states;
103
+	}
104
+
105
+
106
+
107
+	/**
108
+	 *  get_all_states_of_active_countries
109
+	 * @return array
110
+	 */
111
+	public function get_all_states_of_active_countries()
112
+	{
113
+		if ($states = $this->get_all(array( array( 'Country.CNT_active' => true, 'STA_active' => true ),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
114
+			return $states;
115
+		}
116
+		return false;
117
+	}
118
+
119
+
120
+
121
+	/**
122
+	 *  get_all_states_of_active_countries
123
+	 * @return array
124
+	 */
125
+	public function get_all_active_states_for_these_countries($countries)
126
+	{
127
+		if (! $countries) {
128
+			return false;
129
+		}
130
+		if ($states = $this->get_all(array(  array( 'Country.CNT_ISO' => array( 'IN', array_keys($countries)), 'STA_active' => true ),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
131
+			return $states;
132
+		}
133
+		return false;
134
+	}
135
+
136
+
137
+
138
+	/**
139
+	 *  get_all_states_of_active_countries
140
+	 * @return array
141
+	 */
142
+	public function get_all_states_for_these_countries($countries)
143
+	{
144
+		if (! $countries) {
145
+			return false;
146
+		}
147
+		if ($states = $this->get_all(array( array( 'Country.CNT_ISO' => array( 'IN', array_keys($countries))),  'order_by' => array( 'Country.CNT_name' => 'ASC', 'STA_name' => 'ASC' )))) {
148
+			return $states;
149
+		}
150
+		return false;
151
+	}
152
+
153
+	/**
154
+	 * Gets the state's name by its ID
155
+	 * @param string $state_ID
156
+	 * @return string
157
+	 */
158
+	public function get_state_name_by_ID($state_ID)
159
+	{
160
+		if (
161
+			isset(self::$_all_states[ $state_ID ]) &&
162
+				self::$_all_states[ $state_ID ] instanceof EE_State
163
+		) {
164
+			return self::$_all_states[ $state_ID ]->name();
165
+		}
166
+		$names = $this->get_col(array( array( 'STA_ID' => $state_ID ), 'limit' => 1), 'STA_name');
167
+		if (is_array($names) && ! empty($names)) {
168
+			return reset($names);
169
+		} else {
170
+			return '';
171
+		}
172
+	}
173 173
 }
Please login to merge, or discard this patch.
core/db_models/EEM_Ticket_Template.model.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -43,11 +43,11 @@
 block discarded – undo
43 43
             'Ticket' => new EE_Has_Many_Relation()
44 44
         );
45 45
         $this->_model_chain_to_wp_user = 'Ticket';
46
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Default_Public('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
46
+        $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Default_Public('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
47 47
         // account for default tickets in the caps
48
-        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
49
-        $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
50
-        $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
48
+        $this->_cap_restriction_generators[EEM_Base::caps_read_admin] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
49
+        $this->_cap_restriction_generators[EEM_Base::caps_edit] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
50
+        $this->_cap_restriction_generators[EEM_Base::caps_delete] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
51 51
         parent::__construct($timezone);
52 52
     }
53 53
 }
Please login to merge, or discard this patch.
Indentation   +34 added lines, -34 removed lines patch added patch discarded remove patch
@@ -11,40 +11,40 @@
 block discarded – undo
11 11
  */
12 12
 class EEM_Ticket_Template extends EEM_Base
13 13
 {
14
-    // private instance of the EEM_Ticket_Template object
15
-    protected static $_instance = null;
14
+	// private instance of the EEM_Ticket_Template object
15
+	protected static $_instance = null;
16 16
 
17
-    /**
18
-     *      private constructor to prevent direct creation
19
-     *      @Constructor
20
-     *      @access private
21
-     *      @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved).  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)
22
-     *      @return void
23
-     */
24
-    protected function __construct($timezone)
25
-    {
26
-        $this->singular_item = esc_html__('Ticket Template', 'event_espresso');
27
-        $this->plural_item = esc_html__('Ticket Templates', 'event_espresso');
17
+	/**
18
+	 *      private constructor to prevent direct creation
19
+	 *      @Constructor
20
+	 *      @access private
21
+	 *      @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any incoming timezone data that gets saved).  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)
22
+	 *      @return void
23
+	 */
24
+	protected function __construct($timezone)
25
+	{
26
+		$this->singular_item = esc_html__('Ticket Template', 'event_espresso');
27
+		$this->plural_item = esc_html__('Ticket Templates', 'event_espresso');
28 28
 
29
-        $this->_tables = array(
30
-            'Ticket_Template' => new EE_Primary_Table('esp_ticket_template', 'TTM_ID')
31
-        );
32
-        $this->_fields = array(
33
-            'Ticket_Template' => array(
34
-                'TTM_ID' => new EE_Primary_Key_Int_Field('TTM_ID', esc_html__('Ticket Template ID', 'event_espresso')),
35
-                'TTM_name' => new EE_Plain_Text_Field('TTM_name', esc_html__('The name of the ticket template', 'event_espresso'), false, ''),
36
-                'TTM_description' => new EE_Plain_Text_Field('TTM_description', esc_html__('The description for the ticket template', 'event_espresso'), true, ''),
37
-                'TTM_file' => new EE_Plain_Text_Field('TTM_file', esc_html__('The file name for the actual template file saved on disk', 'event_espresso'), true, ''),
38
-            ));
39
-        $this->_model_relations = array(
40
-            'Ticket' => new EE_Has_Many_Relation()
41
-        );
42
-        $this->_model_chain_to_wp_user = 'Ticket';
43
-        $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Default_Public('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
44
-        // account for default tickets in the caps
45
-        $this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
46
-        $this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
47
-        $this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
48
-        parent::__construct($timezone);
49
-    }
29
+		$this->_tables = array(
30
+			'Ticket_Template' => new EE_Primary_Table('esp_ticket_template', 'TTM_ID')
31
+		);
32
+		$this->_fields = array(
33
+			'Ticket_Template' => array(
34
+				'TTM_ID' => new EE_Primary_Key_Int_Field('TTM_ID', esc_html__('Ticket Template ID', 'event_espresso')),
35
+				'TTM_name' => new EE_Plain_Text_Field('TTM_name', esc_html__('The name of the ticket template', 'event_espresso'), false, ''),
36
+				'TTM_description' => new EE_Plain_Text_Field('TTM_description', esc_html__('The description for the ticket template', 'event_espresso'), true, ''),
37
+				'TTM_file' => new EE_Plain_Text_Field('TTM_file', esc_html__('The file name for the actual template file saved on disk', 'event_espresso'), true, ''),
38
+			));
39
+		$this->_model_relations = array(
40
+			'Ticket' => new EE_Has_Many_Relation()
41
+		);
42
+		$this->_model_chain_to_wp_user = 'Ticket';
43
+		$this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Default_Public('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
44
+		// account for default tickets in the caps
45
+		$this->_cap_restriction_generators[ EEM_Base::caps_read_admin ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
46
+		$this->_cap_restriction_generators[ EEM_Base::caps_edit ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
47
+		$this->_cap_restriction_generators[ EEM_Base::caps_delete ] = new EE_Restriction_Generator_Default_Protected('Ticket.TKT_is_default', 'Ticket.Datetime.Event');
48
+		parent::__construct($timezone);
49
+	}
50 50
 }
Please login to merge, or discard this patch.
core/db_models/EEM_Answer.model.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
      */
99 99
     public function get_registration_question_answer_object(EE_Registration $registration, $question_id = null)
100 100
     {
101
-        $answer_obj = $this->get_one(array( array( 'QST_ID' => $question_id, 'REG_ID' => $registration->ID() )));
101
+        $answer_obj = $this->get_one(array(array('QST_ID' => $question_id, 'REG_ID' => $registration->ID())));
102 102
         return apply_filters('FHEE__EEM_Answer__get_registration_question_answer_object__answer_obj', $answer_obj, $registration, $question_id);
103 103
     }
104 104
 
@@ -121,9 +121,9 @@  discard block
 block discarded – undo
121 121
         if (is_numeric($question_system_id)) {
122 122
             // find this question's QST_system value
123 123
             $question_id = $question_system_id;
124
-            $question_system_id = EEM_Question::instance()->get_var(array( array( 'QST_ID' => $question_system_id ) ), 'QST_system');
124
+            $question_system_id = EEM_Question::instance()->get_var(array(array('QST_ID' => $question_system_id)), 'QST_system');
125 125
         } else {
126
-            $question_id = (int) EEM_Question::instance()->get_var(array( array( 'QST_system' => $question_system_id ) ), 'QST_ID');
126
+            $question_id = (int) EEM_Question::instance()->get_var(array(array('QST_system' => $question_system_id)), 'QST_ID');
127 127
         }
128 128
         // only bother checking if the registration has an attendee
129 129
         if ($registration->attendee() instanceof EE_Attendee) {
Please login to merge, or discard this patch.
Indentation   +125 added lines, -125 removed lines patch added patch discarded remove patch
@@ -9,142 +9,142 @@
 block discarded – undo
9 9
  */
10 10
 class EEM_Answer extends EEM_Base
11 11
 {
12
-    /**
13
-     * private instance of the EEM_Answer object
14
-     * @type EEM_Answer
15
-     */
16
-    protected static $_instance = null;
12
+	/**
13
+	 * private instance of the EEM_Answer object
14
+	 * @type EEM_Answer
15
+	 */
16
+	protected static $_instance = null;
17 17
 
18
-    /**
19
-     * Mapping from system question ids to attendee field names
20
-     * @type array
21
-     * @deprecated since version 4.8.8
22
-     */
23
-    protected $_question_id_to_att_field_map = array(
24
-        EEM_Attendee::fname_question_id => 'ATT_fname',
25
-        EEM_Attendee::lname_question_id => 'ATT_lname',
26
-        EEM_Attendee::email_question_id => 'ATT_email',
27
-        EEM_Attendee::address_question_id => 'ATT_address',
28
-        EEM_Attendee::address2_question_id => 'ATT_address2',
29
-        EEM_Attendee::city_question_id => 'ATT_city',
30
-        EEM_Attendee::state_question_id => 'STA_ID',
31
-        EEM_Attendee::country_question_id => 'CNT_ISO',
32
-        EEM_Attendee::zip_question_id => 'ATT_zip',
33
-        EEM_Attendee::phone_question_id => 'ATT_phone'
34
-    );
18
+	/**
19
+	 * Mapping from system question ids to attendee field names
20
+	 * @type array
21
+	 * @deprecated since version 4.8.8
22
+	 */
23
+	protected $_question_id_to_att_field_map = array(
24
+		EEM_Attendee::fname_question_id => 'ATT_fname',
25
+		EEM_Attendee::lname_question_id => 'ATT_lname',
26
+		EEM_Attendee::email_question_id => 'ATT_email',
27
+		EEM_Attendee::address_question_id => 'ATT_address',
28
+		EEM_Attendee::address2_question_id => 'ATT_address2',
29
+		EEM_Attendee::city_question_id => 'ATT_city',
30
+		EEM_Attendee::state_question_id => 'STA_ID',
31
+		EEM_Attendee::country_question_id => 'CNT_ISO',
32
+		EEM_Attendee::zip_question_id => 'ATT_zip',
33
+		EEM_Attendee::phone_question_id => 'ATT_phone'
34
+	);
35 35
 
36 36
 
37 37
 
38
-    /**
39
-     *  constructor
40
-     */
41
-    protected function __construct($timezone = null)
42
-    {
43
-        $this->singular_item = esc_html__('Answer', 'event_espresso');
44
-        $this->plural_item = esc_html__('Answers', 'event_espresso');
45
-        $this->_tables = array(
46
-            'Answer' => new EE_Primary_Table('esp_answer', 'ANS_ID')
47
-        );
48
-        $this->_fields = array(
49
-            'Answer' => array(
50
-                'ANS_ID' => new EE_Primary_Key_Int_Field('ANS_ID', esc_html__('Answer ID', 'event_espresso')),
51
-                'REG_ID' => new EE_Foreign_Key_Int_Field('REG_ID', esc_html__('Registration ID', 'event_espresso'), false, 0, 'Registration'),
52
-                'QST_ID' => new EE_Foreign_Key_Int_Field('QST_ID', esc_html__('Question ID', 'event_espresso'), false, 0, 'Question'),
53
-                'ANS_value' => new EE_Maybe_Serialized_Simple_HTML_Field('ANS_value', esc_html__('Answer Value', 'event_espresso'), false, '')
54
-            ));
55
-        $this->_model_relations = array(
56
-            'Registration' => new EE_Belongs_To_Relation(),
57
-            'Question' => new EE_Belongs_To_Relation()
58
-        );
59
-        $this->_model_chain_to_wp_user = 'Registration.Event';
60
-        $this->_caps_slug = 'registrations';
61
-        parent::__construct($timezone);
62
-    }
38
+	/**
39
+	 *  constructor
40
+	 */
41
+	protected function __construct($timezone = null)
42
+	{
43
+		$this->singular_item = esc_html__('Answer', 'event_espresso');
44
+		$this->plural_item = esc_html__('Answers', 'event_espresso');
45
+		$this->_tables = array(
46
+			'Answer' => new EE_Primary_Table('esp_answer', 'ANS_ID')
47
+		);
48
+		$this->_fields = array(
49
+			'Answer' => array(
50
+				'ANS_ID' => new EE_Primary_Key_Int_Field('ANS_ID', esc_html__('Answer ID', 'event_espresso')),
51
+				'REG_ID' => new EE_Foreign_Key_Int_Field('REG_ID', esc_html__('Registration ID', 'event_espresso'), false, 0, 'Registration'),
52
+				'QST_ID' => new EE_Foreign_Key_Int_Field('QST_ID', esc_html__('Question ID', 'event_espresso'), false, 0, 'Question'),
53
+				'ANS_value' => new EE_Maybe_Serialized_Simple_HTML_Field('ANS_value', esc_html__('Answer Value', 'event_espresso'), false, '')
54
+			));
55
+		$this->_model_relations = array(
56
+			'Registration' => new EE_Belongs_To_Relation(),
57
+			'Question' => new EE_Belongs_To_Relation()
58
+		);
59
+		$this->_model_chain_to_wp_user = 'Registration.Event';
60
+		$this->_caps_slug = 'registrations';
61
+		parent::__construct($timezone);
62
+	}
63 63
 
64 64
 
65 65
 
66
-    /**
67
-     * Gets the string answer to the question for this registration (it could either be stored
68
-     * on the attendee or in the answer table. This function finds its value regardless)
69
-     * @param EE_Registration $registration
70
-     * @param int $question_id
71
-     * @param boolean $pretty_answer whether to call 'pretty_value' or just 'value'
72
-     * @return string
73
-     */
74
-    public function get_answer_value_to_question(EE_Registration $registration, $question_id = null, $pretty_answer = false)
75
-    {
76
-        $value = $this->get_attendee_property_answer_value($registration, $question_id, $pretty_answer);
77
-        if ($value === null) {
78
-            $answer_obj = $this->get_registration_question_answer_object($registration, $question_id);
79
-            if ($answer_obj instanceof EE_Answer) {
80
-                if ($pretty_answer) {
81
-                    $value = $answer_obj->pretty_value();
82
-                } else {
83
-                    $value = $answer_obj->value();
84
-                }
85
-            }
86
-        }
87
-        return apply_filters('FHEE__EEM_Answer__get_answer_value_to_question__answer_value', $value, $registration, $question_id);
88
-    }
66
+	/**
67
+	 * Gets the string answer to the question for this registration (it could either be stored
68
+	 * on the attendee or in the answer table. This function finds its value regardless)
69
+	 * @param EE_Registration $registration
70
+	 * @param int $question_id
71
+	 * @param boolean $pretty_answer whether to call 'pretty_value' or just 'value'
72
+	 * @return string
73
+	 */
74
+	public function get_answer_value_to_question(EE_Registration $registration, $question_id = null, $pretty_answer = false)
75
+	{
76
+		$value = $this->get_attendee_property_answer_value($registration, $question_id, $pretty_answer);
77
+		if ($value === null) {
78
+			$answer_obj = $this->get_registration_question_answer_object($registration, $question_id);
79
+			if ($answer_obj instanceof EE_Answer) {
80
+				if ($pretty_answer) {
81
+					$value = $answer_obj->pretty_value();
82
+				} else {
83
+					$value = $answer_obj->value();
84
+				}
85
+			}
86
+		}
87
+		return apply_filters('FHEE__EEM_Answer__get_answer_value_to_question__answer_value', $value, $registration, $question_id);
88
+	}
89 89
 
90 90
 
91 91
 
92
-    /**
93
-     * Gets the EE_Answer object for the question for this registration (if it exists)
94
-     * @param EE_Registration $registration
95
-     * @param int $question_id
96
-     * @return EE_Answer
97
-     */
98
-    public function get_registration_question_answer_object(EE_Registration $registration, $question_id = null)
99
-    {
100
-        $answer_obj = $this->get_one(array( array( 'QST_ID' => $question_id, 'REG_ID' => $registration->ID() )));
101
-        return apply_filters('FHEE__EEM_Answer__get_registration_question_answer_object__answer_obj', $answer_obj, $registration, $question_id);
102
-    }
92
+	/**
93
+	 * Gets the EE_Answer object for the question for this registration (if it exists)
94
+	 * @param EE_Registration $registration
95
+	 * @param int $question_id
96
+	 * @return EE_Answer
97
+	 */
98
+	public function get_registration_question_answer_object(EE_Registration $registration, $question_id = null)
99
+	{
100
+		$answer_obj = $this->get_one(array( array( 'QST_ID' => $question_id, 'REG_ID' => $registration->ID() )));
101
+		return apply_filters('FHEE__EEM_Answer__get_registration_question_answer_object__answer_obj', $answer_obj, $registration, $question_id);
102
+	}
103 103
 
104 104
 
105 105
 
106
-    /**
107
-     * Gets the string answer to the question for this registration's attendee
108
-     * @param EE_Registration $registration
109
-     * @param int|string $question_system_id if an INT this is understood to be the question's ID; if a string then it should be its QST_system value.
110
-     *  Passing in the QST_system value is more efficient
111
-     * @param boolean $pretty_answer
112
-     * @return string|null (if the registration has no attendee, or the question_system_id is not a QST_ID or QST_system for
113
-     * a question corresponding to an attendee field, returns null)
114
-     */
115
-    public function get_attendee_property_answer_value(EE_Registration $registration, $question_system_id = null, $pretty_answer = false)
116
-    {
117
-        $field_name = null;
118
-        $value = null;
119
-                // backward compat: we still want to find the question's ID
120
-        if (is_numeric($question_system_id)) {
121
-            // find this question's QST_system value
122
-            $question_id = $question_system_id;
123
-            $question_system_id = EEM_Question::instance()->get_var(array( array( 'QST_ID' => $question_system_id ) ), 'QST_system');
124
-        } else {
125
-            $question_id = (int) EEM_Question::instance()->get_var(array( array( 'QST_system' => $question_system_id ) ), 'QST_ID');
126
-        }
127
-        // only bother checking if the registration has an attendee
128
-        if ($registration->attendee() instanceof EE_Attendee) {
129
-            $field_name = EEM_Attendee::instance()->get_attendee_field_for_system_question($question_system_id);
130
-            if ($field_name) {
131
-                if ($pretty_answer) {
132
-                    if ($field_name === 'STA_ID') {
133
-                        $state = $registration->attendee()->state_obj();
134
-                        $value = $state instanceof EE_State ? $state->name() : sprintf(esc_html__('Unknown State (%s)', 'event_espresso'), $registration->attendee()->state_ID());
135
-                    } elseif ($field_name === 'CNT_ISO') {
136
-                        $country = $registration->attendee()->country_obj();
137
-                        $value = $country instanceof EE_Country ? $country->name() : sprintf(esc_html__('Unknown Country (%s)', "event_espresso"), $registration->attendee()->country_ID());
138
-                    } else {
139
-                        $value = $registration->attendee()->get_pretty($field_name);
140
-                    }
141
-                    // if field name is blank, leave the value as null too
142
-                } else {
143
-                    $value = $registration->attendee()->get($field_name);
144
-                }
145
-            }
146
-            // if no field was found, leave value blank
147
-        }
148
-        return apply_filters('FHEE__EEM_Answer__get_attendee_question_answer_value__answer_value', $value, $registration, $question_id, $question_system_id);
149
-    }
106
+	/**
107
+	 * Gets the string answer to the question for this registration's attendee
108
+	 * @param EE_Registration $registration
109
+	 * @param int|string $question_system_id if an INT this is understood to be the question's ID; if a string then it should be its QST_system value.
110
+	 *  Passing in the QST_system value is more efficient
111
+	 * @param boolean $pretty_answer
112
+	 * @return string|null (if the registration has no attendee, or the question_system_id is not a QST_ID or QST_system for
113
+	 * a question corresponding to an attendee field, returns null)
114
+	 */
115
+	public function get_attendee_property_answer_value(EE_Registration $registration, $question_system_id = null, $pretty_answer = false)
116
+	{
117
+		$field_name = null;
118
+		$value = null;
119
+				// backward compat: we still want to find the question's ID
120
+		if (is_numeric($question_system_id)) {
121
+			// find this question's QST_system value
122
+			$question_id = $question_system_id;
123
+			$question_system_id = EEM_Question::instance()->get_var(array( array( 'QST_ID' => $question_system_id ) ), 'QST_system');
124
+		} else {
125
+			$question_id = (int) EEM_Question::instance()->get_var(array( array( 'QST_system' => $question_system_id ) ), 'QST_ID');
126
+		}
127
+		// only bother checking if the registration has an attendee
128
+		if ($registration->attendee() instanceof EE_Attendee) {
129
+			$field_name = EEM_Attendee::instance()->get_attendee_field_for_system_question($question_system_id);
130
+			if ($field_name) {
131
+				if ($pretty_answer) {
132
+					if ($field_name === 'STA_ID') {
133
+						$state = $registration->attendee()->state_obj();
134
+						$value = $state instanceof EE_State ? $state->name() : sprintf(esc_html__('Unknown State (%s)', 'event_espresso'), $registration->attendee()->state_ID());
135
+					} elseif ($field_name === 'CNT_ISO') {
136
+						$country = $registration->attendee()->country_obj();
137
+						$value = $country instanceof EE_Country ? $country->name() : sprintf(esc_html__('Unknown Country (%s)', "event_espresso"), $registration->attendee()->country_ID());
138
+					} else {
139
+						$value = $registration->attendee()->get_pretty($field_name);
140
+					}
141
+					// if field name is blank, leave the value as null too
142
+				} else {
143
+					$value = $registration->attendee()->get($field_name);
144
+				}
145
+			}
146
+			// if no field was found, leave value blank
147
+		}
148
+		return apply_filters('FHEE__EEM_Answer__get_attendee_question_answer_value__answer_value', $value, $registration, $question_id, $question_system_id);
149
+	}
150 150
 }
Please login to merge, or discard this patch.
core/db_models/EEM_Registration.model.php 2 patches
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -370,14 +370,14 @@  discard block
 block discarded – undo
370 370
         // and the table hasn't actually been created, this could have an error
371 371
         /** @type WPDB $wpdb */
372 372
         global $wpdb;
373
-        if ($this->_get_table_analysis()->tableExists($wpdb->prefix . 'esp_status')) {
373
+        if ($this->_get_table_analysis()->tableExists($wpdb->prefix.'esp_status')) {
374 374
             $results = $wpdb->get_results(
375 375
                 "SELECT STS_ID, STS_code FROM {$wpdb->prefix}esp_status WHERE STS_type = 'registration'"
376 376
             );
377 377
             self::$_reg_status = array();
378 378
             foreach ($results as $status) {
379
-                if (!in_array($status->STS_ID, $exclude, true)) {
380
-                    self::$_reg_status[ $status->STS_ID ] = $status->STS_code;
379
+                if ( ! in_array($status->STS_ID, $exclude, true)) {
380
+                    self::$_reg_status[$status->STS_ID] = $status->STS_code;
381 381
                 }
382 382
             }
383 383
         }
@@ -434,7 +434,7 @@  discard block
 block discarded – undo
434 434
      */
435 435
     public function get_all_registrations_for_attendee($ATT_ID = 0)
436 436
     {
437
-        if (!$ATT_ID) {
437
+        if ( ! $ATT_ID) {
438 438
             return null;
439 439
         }
440 440
         return $this->get_all(array(array('ATT_ID' => $ATT_ID)));
@@ -451,7 +451,7 @@  discard block
 block discarded – undo
451 451
      */
452 452
     public function get_registration_for_reg_url_link($REG_url_link)
453 453
     {
454
-        if (!$REG_url_link) {
454
+        if ( ! $REG_url_link) {
455 455
             return null;
456 456
         }
457 457
         return $this->get_one(array(array('REG_url_link' => $REG_url_link)));
@@ -502,7 +502,7 @@  discard block
 block discarded – undo
502 502
             'REG_date' => array('>=', $sql_date),
503 503
             'STS_ID' => array('!=', EEM_Registration::status_id_incomplete),
504 504
         );
505
-        if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_day_report')) {
505
+        if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_day_report')) {
506 506
             $where['Event.EVT_wp_user'] = get_current_user_id();
507 507
         }
508 508
         $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'REG_date');
@@ -514,7 +514,7 @@  discard block
 block discarded – undo
514 514
             ),
515 515
             OBJECT,
516 516
             array(
517
-                'regDate' => array('DATE(' . $query_interval . ')', '%s'),
517
+                'regDate' => array('DATE('.$query_interval.')', '%s'),
518 518
                 'total' => array('count(REG_ID)', '%d'),
519 519
             )
520 520
         );
@@ -534,7 +534,7 @@  discard block
 block discarded – undo
534 534
     public function get_registrations_per_day_and_per_status_report($period = '-1 month')
535 535
     {
536 536
         global $wpdb;
537
-        $registration_table = $wpdb->prefix . 'esp_registration';
537
+        $registration_table = $wpdb->prefix.'esp_registration';
538 538
         $event_table = $wpdb->posts;
539 539
         $sql_date = date('Y-m-d H:i:s', strtotime($period));
540 540
         // prepare the query interval for displaying offset
@@ -543,9 +543,9 @@  discard block
 block discarded – undo
543 543
         $inner_date_query = "SELECT DISTINCT REG_date from {$registration_table} ";
544 544
         $inner_where = ' WHERE';
545 545
         // exclude events not authored by user if permissions in effect
546
-        if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
546
+        if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
547 547
             $inner_date_query .= "LEFT JOIN {$event_table} ON ID = EVT_ID";
548
-            $inner_where .= ' post_author = ' . get_current_user_id() . ' AND';
548
+            $inner_where .= ' post_author = '.get_current_user_id().' AND';
549 549
         }
550 550
         $inner_where .= " REG_date >= '{$sql_date}'";
551 551
         $inner_date_query .= $inner_where;
@@ -568,7 +568,7 @@  discard block
 block discarded – undo
568 568
         // setup the joins
569 569
         $join .= implode(' LEFT JOIN ', $join_parts);
570 570
         // now let's put it all together
571
-        $query = $select . $join . ' GROUP BY Registration_REG_date';
571
+        $query = $select.$join.' GROUP BY Registration_REG_date';
572 572
         // and execute it
573 573
         return $wpdb->get_results($query, ARRAY_A);
574 574
     }
@@ -595,7 +595,7 @@  discard block
 block discarded – undo
595 595
             'STS_ID' => array('!=', EEM_Registration::status_id_incomplete),
596 596
         );
597 597
         if (
598
-            !EE_Registry::instance()->CAP->current_user_can(
598
+            ! EE_Registry::instance()->CAP->current_user_can(
599 599
                 'ee_read_others_registrations',
600 600
                 'reg_per_event_report'
601 601
             )
@@ -631,16 +631,16 @@  discard block
 block discarded – undo
631 631
     public function get_registrations_per_event_and_per_status_report($period = '-1 month')
632 632
     {
633 633
         global $wpdb;
634
-        $registration_table = $wpdb->prefix . 'esp_registration';
634
+        $registration_table = $wpdb->prefix.'esp_registration';
635 635
         $event_table = $wpdb->posts;
636 636
         $sql_date = date('Y-m-d H:i:s', strtotime($period));
637 637
         // inner date query
638 638
         $inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table ";
639 639
         $inner_where = ' WHERE';
640 640
         // exclude events not authored by user if permissions in effect
641
-        if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
641
+        if ( ! EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
642 642
             $inner_date_query .= "LEFT JOIN {$event_table} ON ID = EVT_ID";
643
-            $inner_where .= ' post_author = ' . get_current_user_id() . ' AND';
643
+            $inner_where .= ' post_author = '.get_current_user_id().' AND';
644 644
         }
645 645
         $inner_where .= " REG_date >= '{$sql_date}'";
646 646
         $inner_date_query .= $inner_where;
@@ -663,7 +663,7 @@  discard block
 block discarded – undo
663 663
         // setup remaining joins
664 664
         $join .= implode(' LEFT JOIN ', $join_parts);
665 665
         // now put it all together
666
-        $query = $select . $join . ' GROUP BY Registration_Event';
666
+        $query = $select.$join.' GROUP BY Registration_Event';
667 667
         // and execute
668 668
         return $wpdb->get_results($query, ARRAY_A);
669 669
     }
@@ -678,7 +678,7 @@  discard block
 block discarded – undo
678 678
      */
679 679
     public function get_primary_registration_for_transaction_ID($TXN_ID = 0)
680 680
     {
681
-        if (!$TXN_ID) {
681
+        if ( ! $TXN_ID) {
682 682
             return null;
683 683
         }
684 684
         return $this->get_one(array(
@@ -748,11 +748,11 @@  discard block
 block discarded – undo
748 748
         $query = $wpdb->prepare(
749 749
             'SELECT '
750 750
             . 'COUNT( DISTINCT checkins.REG_ID ) '
751
-            . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN'
751
+            . 'FROM '.EEM_Checkin::instance()->table().' AS checkins INNER JOIN'
752 752
             . '( SELECT '
753 753
             . 'max( CHK_timestamp ) AS latest_checkin, '
754 754
             . 'REG_ID AS REG_ID '
755
-            . 'FROM ' . EEM_Checkin::instance()->table() . ' '
755
+            . 'FROM '.EEM_Checkin::instance()->table().' '
756 756
             . 'WHERE DTT_ID=%d '
757 757
             . 'GROUP BY REG_ID'
758 758
             . ') AS most_recent_checkin_per_reg '
@@ -782,12 +782,12 @@  discard block
 block discarded – undo
782 782
         $query = $wpdb->prepare(
783 783
             'SELECT '
784 784
             . 'COUNT( DISTINCT checkins.REG_ID ) '
785
-            . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN'
785
+            . 'FROM '.EEM_Checkin::instance()->table().' AS checkins INNER JOIN'
786 786
             . '( SELECT '
787 787
             . 'max( CHK_timestamp ) AS latest_checkin, '
788 788
             . 'REG_ID AS REG_ID '
789
-            . 'FROM ' . EEM_Checkin::instance()->table() . ' AS c '
790
-            . 'INNER JOIN ' . EEM_Datetime::instance()->table() . ' AS d '
789
+            . 'FROM '.EEM_Checkin::instance()->table().' AS c '
790
+            . 'INNER JOIN '.EEM_Datetime::instance()->table().' AS d '
791 791
             . 'ON c.DTT_ID=d.DTT_ID '
792 792
             . 'WHERE d.EVT_ID=%d '
793 793
             . 'GROUP BY REG_ID'
@@ -816,7 +816,7 @@  discard block
 block discarded – undo
816 816
     {
817 817
         // first do a native wp_query to get the latest REG_ID's matching these attendees.
818 818
         global $wpdb;
819
-        $registration_table = $wpdb->prefix . 'esp_registration';
819
+        $registration_table = $wpdb->prefix.'esp_registration';
820 820
         $attendee_table = $wpdb->posts;
821 821
         $attendee_ids = is_array($attendee_ids)
822 822
             ? array_map('absint', $attendee_ids)
@@ -871,7 +871,7 @@  discard block
 block discarded – undo
871 871
     public function event_reg_count_for_statuses($EVT_ID, $statuses = array())
872 872
     {
873 873
         $EVT_ID = absint($EVT_ID);
874
-        if (! $EVT_ID) {
874
+        if ( ! $EVT_ID) {
875 875
             throw new InvalidArgumentException(
876 876
                 esc_html__('An invalid Event ID was supplied.', 'event_espresso')
877 877
             );
@@ -880,7 +880,7 @@  discard block
 block discarded – undo
880 880
         $statuses = ! empty($statuses) ? $statuses : array(EEM_Registration::status_id_approved);
881 881
         $valid_reg_statuses = EEM_Registration::reg_statuses();
882 882
         foreach ($statuses as $status) {
883
-            if (! in_array($status, $valid_reg_statuses, true)) {
883
+            if ( ! in_array($status, $valid_reg_statuses, true)) {
884 884
                 throw new InvalidStatusException($status, esc_html__('Registration', 'event_espresso'));
885 885
             }
886 886
         }
Please login to merge, or discard this patch.
Indentation   +868 added lines, -868 removed lines patch added patch discarded remove patch
@@ -13,817 +13,817 @@  discard block
 block discarded – undo
13 13
  */
14 14
 class EEM_Registration extends EEM_Soft_Delete_Base
15 15
 {
16
-    /**
17
-     * @var EEM_Registration $_instance
18
-     */
19
-    protected static $_instance;
20
-
21
-    /**
22
-     * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values
23
-     * are status codes (eg, approved, cancelled, etc)
24
-     *
25
-     * @var array
26
-     */
27
-    private static $_reg_status;
28
-
29
-    /**
30
-     * The value of REG_count for a primary registrant
31
-     */
32
-    const PRIMARY_REGISTRANT_COUNT = 1;
33
-
34
-    /**
35
-     * Status ID (STS_ID on esp_status table) to indicate an INCOMPLETE registration.
36
-     * Initial status for registrations when they are first created
37
-     * Payments are NOT allowed.
38
-     * Automatically toggled to whatever the default Event registration status is upon completion of the attendee
39
-     * information reg step NO space reserved. Registration is NOT active
40
-     */
41
-    const status_id_incomplete = 'RIC';
42
-
43
-    /**
44
-     * Status ID (STS_ID on esp_status table) to indicate an UNAPPROVED registration.
45
-     * Payments are NOT allowed.
46
-     * Event Admin must manually toggle STS_ID for it to change
47
-     * No space reserved.
48
-     * Registration is active
49
-     */
50
-    const status_id_not_approved = 'RNA';
51
-
52
-    /**
53
-     * Status ID (STS_ID on esp_status table) to indicate registration is PENDING_PAYMENT .
54
-     * Payments are allowed.
55
-     * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee
56
-     * No space reserved.
57
-     * Registration is active
58
-     */
59
-    const status_id_pending_payment = 'RPP';
60
-
61
-    /**
62
-     * Status ID (STS_ID on esp_status table) to indicate registration is on the WAIT_LIST .
63
-     * Payments are allowed.
64
-     * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee
65
-     * No space reserved.
66
-     * Registration is active
67
-     */
68
-    const status_id_wait_list = 'RWL';
69
-
70
-    /**
71
-     * Status ID (STS_ID on esp_status table) to indicate an APPROVED registration.
72
-     * the TXN may or may not be completed ( paid in full )
73
-     * Payments are allowed.
74
-     * A space IS reserved.
75
-     * Registration is active
76
-     */
77
-    const status_id_approved = 'RAP';
78
-
79
-    /**
80
-     * Status ID (STS_ID on esp_status table) to indicate a registration was CANCELLED by the attendee.
81
-     * Payments are NOT allowed.
82
-     * NO space reserved.
83
-     * Registration is NOT active
84
-     */
85
-    const status_id_cancelled = 'RCN';
86
-
87
-    /**
88
-     * Status ID (STS_ID on esp_status table) to indicate a registration was DECLINED by the Event Admin
89
-     * Payments are NOT allowed.
90
-     * No space reserved.
91
-     * Registration is NOT active
92
-     */
93
-    const status_id_declined = 'RDC';
94
-
95
-    /**
96
-     * @var TableAnalysis $table_analysis
97
-     */
98
-    protected $_table_analysis;
99
-
100
-
101
-    /**
102
-     *    private constructor to prevent direct creation
103
-     *
104
-     * @Constructor
105
-     * @access protected
106
-     * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any
107
-     *                         incoming timezone data that gets saved). Note this just sends the timezone info to the
108
-     *                         date time model field objects.  Default is NULL (and will be assumed using the set
109
-     *                         timezone in the 'timezone_string' wp option)
110
-     * @throws EE_Error
111
-     */
112
-    protected function __construct($timezone = null)
113
-    {
114
-        $this->_table_analysis = EE_Registry::instance()->create('TableAnalysis', array(), true);
115
-        $this->singular_item = esc_html__('Registration', 'event_espresso');
116
-        $this->plural_item = esc_html__('Registrations', 'event_espresso');
117
-        $this->_tables = array(
118
-            'Registration' => new EE_Primary_Table('esp_registration', 'REG_ID'),
119
-        );
120
-        $this->_fields = array(
121
-            'Registration' => array(
122
-                'REG_ID' => new EE_Primary_Key_Int_Field(
123
-                    'REG_ID',
124
-                    esc_html__('Registration ID', 'event_espresso')
125
-                ),
126
-                'EVT_ID' => new EE_Foreign_Key_Int_Field(
127
-                    'EVT_ID',
128
-                    esc_html__('Event ID', 'event_espresso'),
129
-                    false,
130
-                    0,
131
-                    'Event'
132
-                ),
133
-                'ATT_ID' => new EE_Foreign_Key_Int_Field(
134
-                    'ATT_ID',
135
-                    esc_html__('Attendee ID', 'event_espresso'),
136
-                    false,
137
-                    0,
138
-                    'Attendee'
139
-                ),
140
-                'TXN_ID' => new EE_Foreign_Key_Int_Field(
141
-                    'TXN_ID',
142
-                    esc_html__('Transaction ID', 'event_espresso'),
143
-                    false,
144
-                    0,
145
-                    'Transaction'
146
-                ),
147
-                'TKT_ID' => new EE_Foreign_Key_Int_Field(
148
-                    'TKT_ID',
149
-                    esc_html__('Ticket ID', 'event_espresso'),
150
-                    false,
151
-                    0,
152
-                    'Ticket'
153
-                ),
154
-                'STS_ID' => new EE_Foreign_Key_String_Field(
155
-                    'STS_ID',
156
-                    esc_html__('Status ID', 'event_espresso'),
157
-                    false,
158
-                    EEM_Registration::status_id_incomplete,
159
-                    'Status'
160
-                ),
161
-                'REG_date' => new EE_Datetime_Field(
162
-                    'REG_date',
163
-                    esc_html__('Time registration occurred', 'event_espresso'),
164
-                    false,
165
-                    EE_Datetime_Field::now,
166
-                    $timezone
167
-                ),
168
-                'REG_final_price' => new EE_Money_Field(
169
-                    'REG_final_price',
170
-                    esc_html__('Registration\'s share of the transaction total', 'event_espresso'),
171
-                    false,
172
-                    0
173
-                ),
174
-                'REG_paid' => new EE_Money_Field(
175
-                    'REG_paid',
176
-                    esc_html__('Amount paid to date towards registration', 'event_espresso'),
177
-                    false,
178
-                    0
179
-                ),
180
-                'REG_session' => new EE_Plain_Text_Field(
181
-                    'REG_session',
182
-                    esc_html__('Session ID of registration', 'event_espresso'),
183
-                    false,
184
-                    ''
185
-                ),
186
-                'REG_code' => new EE_Plain_Text_Field(
187
-                    'REG_code',
188
-                    esc_html__('Unique Code for this registration', 'event_espresso'),
189
-                    false,
190
-                    ''
191
-                ),
192
-                'REG_url_link' => new EE_Plain_Text_Field(
193
-                    'REG_url_link',
194
-                    esc_html__('String to be used in URL for identifying registration', 'event_espresso'),
195
-                    false,
196
-                    ''
197
-                ),
198
-                'REG_count' => new EE_Integer_Field(
199
-                    'REG_count',
200
-                    esc_html__('Count of this registration in the group registration ', 'event_espresso'),
201
-                    true,
202
-                    1
203
-                ),
204
-                'REG_group_size' => new EE_Integer_Field(
205
-                    'REG_group_size',
206
-                    esc_html__('Number of registrations on this group', 'event_espresso'),
207
-                    false,
208
-                    1
209
-                ),
210
-                'REG_att_is_going' => new EE_Boolean_Field(
211
-                    'REG_att_is_going',
212
-                    esc_html__('Flag indicating the registrant plans on attending', 'event_espresso'),
213
-                    false,
214
-                    false
215
-                ),
216
-                'REG_deleted' => new EE_Trashed_Flag_Field(
217
-                    'REG_deleted',
218
-                    esc_html__('Flag indicating if registration has been archived or not.', 'event_espresso'),
219
-                    false,
220
-                    false
221
-                ),
222
-            ),
223
-        );
224
-        $this->_model_relations = array(
225
-            'Event' => new EE_Belongs_To_Relation(),
226
-            'Attendee' => new EE_Belongs_To_Relation(),
227
-            'Transaction' => new EE_Belongs_To_Relation(),
228
-            'Ticket' => new EE_Belongs_To_Relation(),
229
-            'Status' => new EE_Belongs_To_Relation(),
230
-            'Answer' => new EE_Has_Many_Relation(),
231
-            'Checkin' => new EE_Has_Many_Relation(),
232
-            'Registration_Payment' => new EE_Has_Many_Relation(),
233
-            'Payment' => new EE_HABTM_Relation('Registration_Payment'),
234
-            'Message' => new EE_Has_Many_Any_Relation(false)
235
-            // allow deletes even if there are messages in the queue related
236
-        );
237
-        $this->_model_chain_to_wp_user = 'Event';
238
-        parent::__construct($timezone);
239
-    }
240
-
241
-
242
-    /**
243
-     * a list of ALL valid registration statuses currently in use within the system
244
-     * generated by combining the filterable active and inactive reg status arrays
245
-     *
246
-     * @return array
247
-     */
248
-    public static function reg_statuses()
249
-    {
250
-        return array_unique(
251
-            array_merge(
252
-                EEM_Registration::active_reg_statuses(),
253
-                EEM_Registration::inactive_reg_statuses()
254
-            )
255
-        );
256
-    }
257
-
258
-
259
-    /**
260
-     * reg_statuses_that_allow_payment
261
-     * a filterable list of registration statuses that allow a registrant to make a payment
262
-     *
263
-     * @access public
264
-     * @return array
265
-     */
266
-    public static function reg_statuses_that_allow_payment()
267
-    {
268
-        return apply_filters(
269
-            'FHEE__EEM_Registration__reg_statuses_that_allow_payment',
270
-            array(
271
-                EEM_Registration::status_id_approved,
272
-                EEM_Registration::status_id_pending_payment,
273
-            )
274
-        );
275
-    }
276
-
277
-
278
-    /**
279
-     * active_reg_statuses
280
-     * a filterable list of registration statuses that are considered active
281
-     *
282
-     * @access public
283
-     * @return array
284
-     */
285
-    public static function active_reg_statuses()
286
-    {
287
-        return apply_filters(
288
-            'FHEE__EEM_Registration__active_reg_statuses',
289
-            array(
290
-                EEM_Registration::status_id_approved,
291
-                EEM_Registration::status_id_pending_payment,
292
-                EEM_Registration::status_id_wait_list,
293
-                EEM_Registration::status_id_not_approved,
294
-            )
295
-        );
296
-    }
297
-
298
-
299
-    /**
300
-     * inactive_reg_statuses
301
-     * a filterable list of registration statuses that are not considered active
302
-     *
303
-     * @access public
304
-     * @return array
305
-     */
306
-    public static function inactive_reg_statuses()
307
-    {
308
-        return apply_filters(
309
-            'FHEE__EEM_Registration__inactive_reg_statuses',
310
-            array(
311
-                EEM_Registration::status_id_incomplete,
312
-                EEM_Registration::status_id_cancelled,
313
-                EEM_Registration::status_id_declined,
314
-            )
315
-        );
316
-    }
317
-
318
-
319
-    /**
320
-     *    closed_reg_statuses
321
-     *    a filterable list of registration statuses that are considered "closed"
322
-     * meaning they should not be considered in any calculations involving monies owing
323
-     *
324
-     * @access public
325
-     * @return array
326
-     */
327
-    public static function closed_reg_statuses()
328
-    {
329
-        return apply_filters(
330
-            'FHEE__EEM_Registration__closed_reg_statuses',
331
-            array(
332
-                EEM_Registration::status_id_cancelled,
333
-                EEM_Registration::status_id_declined,
334
-                EEM_Registration::status_id_wait_list,
335
-            )
336
-        );
337
-    }
338
-
339
-
340
-    /**
341
-     *        get list of registration statuses
342
-     *
343
-     * @access public
344
-     * @param array $exclude The status ids to exclude from the returned results
345
-     * @param bool $translated If true will return the values as singular localized strings
346
-     * @return array
347
-     * @throws EE_Error
348
-     */
349
-    public static function reg_status_array($exclude = array(), $translated = false)
350
-    {
351
-        EEM_Registration::instance()->_get_registration_status_array($exclude);
352
-        return $translated
353
-            ? EEM_Status::instance()->localized_status(self::$_reg_status, false, 'sentence')
354
-            : self::$_reg_status;
355
-    }
356
-
357
-
358
-    /**
359
-     *    get list of registration statuses
360
-     *
361
-     * @access private
362
-     * @param array $exclude
363
-     * @return void
364
-     * @throws EE_Error
365
-     */
366
-    private function _get_registration_status_array($exclude = array())
367
-    {
368
-        // in the very rare circumstance that we are deleting a model's table's data
369
-        // and the table hasn't actually been created, this could have an error
370
-        /** @type WPDB $wpdb */
371
-        global $wpdb;
372
-        if ($this->_get_table_analysis()->tableExists($wpdb->prefix . 'esp_status')) {
373
-            $results = $wpdb->get_results(
374
-                "SELECT STS_ID, STS_code FROM {$wpdb->prefix}esp_status WHERE STS_type = 'registration'"
375
-            );
376
-            self::$_reg_status = array();
377
-            foreach ($results as $status) {
378
-                if (!in_array($status->STS_ID, $exclude, true)) {
379
-                    self::$_reg_status[ $status->STS_ID ] = $status->STS_code;
380
-                }
381
-            }
382
-        }
383
-    }
384
-
385
-
386
-    /**
387
-     * Gets the injected table analyzer, or throws an exception
388
-     *
389
-     * @return TableAnalysis
390
-     * @throws EE_Error
391
-     */
392
-    protected function _get_table_analysis()
393
-    {
394
-        if ($this->_table_analysis instanceof TableAnalysis) {
395
-            return $this->_table_analysis;
396
-        }
397
-        throw new EE_Error(
398
-            sprintf(
399
-                esc_html__('Table analysis class on class %1$s is not set properly.', 'event_espresso'),
400
-                get_class($this)
401
-            )
402
-        );
403
-    }
404
-
405
-
406
-    /**
407
-     * This returns a wpdb->results array of all registration date month and years matching the incoming query params
408
-     * and grouped by month and year.
409
-     *
410
-     * @param  array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions
411
-     * @return array
412
-     * @throws EE_Error
413
-     */
414
-    public function get_reg_months_and_years($where_params)
415
-    {
416
-        $query_params[0] = $where_params;
417
-        $query_params['group_by'] = array('reg_year', 'reg_month');
418
-        $query_params['order_by'] = array('REG_date' => 'DESC');
419
-        $columns_to_select = array(
420
-            'reg_year' => array('YEAR(REG_date)', '%s'),
421
-            'reg_month' => array('MONTHNAME(REG_date)', '%s'),
422
-        );
423
-        return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select);
424
-    }
425
-
426
-
427
-    /**
428
-     * retrieve ALL registrations for a particular Attendee from db
429
-     *
430
-     * @param int $ATT_ID
431
-     * @return EE_Base_Class[]|EE_Registration[]|null
432
-     * @throws EE_Error
433
-     */
434
-    public function get_all_registrations_for_attendee($ATT_ID = 0)
435
-    {
436
-        if (!$ATT_ID) {
437
-            return null;
438
-        }
439
-        return $this->get_all(array(array('ATT_ID' => $ATT_ID)));
440
-    }
441
-
442
-
443
-    /**
444
-     * Gets a registration given their REG_url_link. Yes, this should usually
445
-     * be passed via a GET parameter.
446
-     *
447
-     * @param string $REG_url_link
448
-     * @return EE_Base_Class|EE_Registration|null
449
-     * @throws EE_Error
450
-     */
451
-    public function get_registration_for_reg_url_link($REG_url_link)
452
-    {
453
-        if (!$REG_url_link) {
454
-            return null;
455
-        }
456
-        return $this->get_one(array(array('REG_url_link' => $REG_url_link)));
457
-    }
458
-
459
-
460
-    /**
461
-     *        retrieve registration for a specific transaction attendee from db
462
-     *
463
-     * @access        public
464
-     * @param    int $TXN_ID
465
-     * @param    int $ATT_ID
466
-     * @param    int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the
467
-     *                         attendee number is required
468
-     * @return        mixed        array on success, FALSE on fail
469
-     * @throws EE_Error
470
-     */
471
-    public function get_registration_for_transaction_attendee($TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0)
472
-    {
473
-        return $this->get_one(array(
474
-            array(
475
-                'TXN_ID' => $TXN_ID,
476
-                'ATT_ID' => $ATT_ID,
477
-            ),
478
-            'limit' => array(min($att_nmbr - 1, 0), 1),
479
-        ));
480
-    }
481
-
482
-
483
-    /**
484
-     *        get the number of registrations per day  for the Registration Admin page Reports Tab.
485
-     *        (doesn't utilize models because it's a fairly specialized query)
486
-     *
487
-     * @access        public
488
-     * @param $period string which can be passed to php's strtotime function (eg "-1 month")
489
-     * @return stdClass[] with properties regDate and total
490
-     * @throws EE_Error
491
-     */
492
-    public function get_registrations_per_day_report($period = '-1 month')
493
-    {
494
-        $sql_date = $this->convert_datetime_for_query(
495
-            'REG_date',
496
-            date('Y-m-d H:i:s', strtotime($period)),
497
-            'Y-m-d H:i:s',
498
-            'UTC'
499
-        );
500
-        $where = array(
501
-            'REG_date' => array('>=', $sql_date),
502
-            'STS_ID' => array('!=', EEM_Registration::status_id_incomplete),
503
-        );
504
-        if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_day_report')) {
505
-            $where['Event.EVT_wp_user'] = get_current_user_id();
506
-        }
507
-        $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'REG_date');
508
-        $results = $this->_get_all_wpdb_results(
509
-            array(
510
-                $where,
511
-                'group_by' => 'regDate',
512
-                'order_by' => array('REG_date' => 'ASC'),
513
-            ),
514
-            OBJECT,
515
-            array(
516
-                'regDate' => array('DATE(' . $query_interval . ')', '%s'),
517
-                'total' => array('count(REG_ID)', '%d'),
518
-            )
519
-        );
520
-        return $results;
521
-    }
522
-
523
-
524
-    /**
525
-     * Get the number of registrations per day including the count of registrations for each Registration Status.
526
-     * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results.
527
-     *
528
-     * @param string $period
529
-     * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID
530
-     * @throws EE_Error
531
-     *                    (i.e. RAP)
532
-     */
533
-    public function get_registrations_per_day_and_per_status_report($period = '-1 month')
534
-    {
535
-        global $wpdb;
536
-        $registration_table = $wpdb->prefix . 'esp_registration';
537
-        $event_table = $wpdb->posts;
538
-        $sql_date = date('Y-m-d H:i:s', strtotime($period));
539
-        // prepare the query interval for displaying offset
540
-        $query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'dates.REG_date');
541
-        // inner date query
542
-        $inner_date_query = "SELECT DISTINCT REG_date from {$registration_table} ";
543
-        $inner_where = ' WHERE';
544
-        // exclude events not authored by user if permissions in effect
545
-        if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
546
-            $inner_date_query .= "LEFT JOIN {$event_table} ON ID = EVT_ID";
547
-            $inner_where .= ' post_author = ' . get_current_user_id() . ' AND';
548
-        }
549
-        $inner_where .= " REG_date >= '{$sql_date}'";
550
-        $inner_date_query .= $inner_where;
551
-        // start main query
552
-        $select = "SELECT DATE({$query_interval}) as Registration_REG_date, ";
553
-        $join = '';
554
-        $join_parts = array();
555
-        $select_parts = array();
556
-        // loop through registration stati to do parts for each status.
557
-        foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) {
558
-            if ($STS_ID === EEM_Registration::status_id_incomplete) {
559
-                continue;
560
-            }
561
-            $select_parts[] = "COUNT({$STS_code}.REG_ID) as {$STS_ID}";
562
-            $join_parts[] = "{$registration_table} AS {$STS_code} ON {$STS_code}.REG_date = dates.REG_date AND {$STS_code}.STS_ID = '{$STS_ID}'";
563
-        }
564
-        // setup the selects
565
-        $select .= implode(', ', $select_parts);
566
-        $select .= " FROM ($inner_date_query) AS dates LEFT JOIN ";
567
-        // setup the joins
568
-        $join .= implode(' LEFT JOIN ', $join_parts);
569
-        // now let's put it all together
570
-        $query = $select . $join . ' GROUP BY Registration_REG_date';
571
-        // and execute it
572
-        return $wpdb->get_results($query, ARRAY_A);
573
-    }
574
-
575
-
576
-    /**
577
-     *        get the number of registrations per event  for the Registration Admin page Reports Tab
578
-     *
579
-     * @access        public
580
-     * @param $period string which can be passed to php's strtotime function (eg "-1 month")
581
-     * @return stdClass[] each with properties event_name, reg_limit, and total
582
-     * @throws EE_Error
583
-     */
584
-    public function get_registrations_per_event_report($period = '-1 month')
585
-    {
586
-        $date_sql = $this->convert_datetime_for_query(
587
-            'REG_date',
588
-            date('Y-m-d H:i:s', strtotime($period)),
589
-            'Y-m-d H:i:s',
590
-            'UTC'
591
-        );
592
-        $where = array(
593
-            'REG_date' => array('>=', $date_sql),
594
-            'STS_ID' => array('!=', EEM_Registration::status_id_incomplete),
595
-        );
596
-        if (
597
-            !EE_Registry::instance()->CAP->current_user_can(
598
-                'ee_read_others_registrations',
599
-                'reg_per_event_report'
600
-            )
601
-        ) {
602
-            $where['Event.EVT_wp_user'] = get_current_user_id();
603
-        }
604
-        $results = $this->_get_all_wpdb_results(
605
-            array(
606
-            $where,
607
-            'group_by' => 'Event.EVT_name',
608
-            'order_by' => 'Event.EVT_name',
609
-            'limit' => array(0, 24),
610
-            ),
611
-            OBJECT,
612
-            array(
613
-                'event_name' => array('Event_CPT.post_title', '%s'),
614
-                'total' => array('COUNT(REG_ID)', '%s'),
615
-            )
616
-        );
617
-        return $results;
618
-    }
619
-
620
-
621
-    /**
622
-     * Get the number of registrations per event grouped by registration status.
623
-     * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results.
624
-     *
625
-     * @param string $period
626
-     * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID
627
-     * @throws EE_Error
628
-     *                    (i.e. RAP)
629
-     */
630
-    public function get_registrations_per_event_and_per_status_report($period = '-1 month')
631
-    {
632
-        global $wpdb;
633
-        $registration_table = $wpdb->prefix . 'esp_registration';
634
-        $event_table = $wpdb->posts;
635
-        $sql_date = date('Y-m-d H:i:s', strtotime($period));
636
-        // inner date query
637
-        $inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table ";
638
-        $inner_where = ' WHERE';
639
-        // exclude events not authored by user if permissions in effect
640
-        if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
641
-            $inner_date_query .= "LEFT JOIN {$event_table} ON ID = EVT_ID";
642
-            $inner_where .= ' post_author = ' . get_current_user_id() . ' AND';
643
-        }
644
-        $inner_where .= " REG_date >= '{$sql_date}'";
645
-        $inner_date_query .= $inner_where;
646
-        // build main query
647
-        $select = 'SELECT Event.post_title as Registration_Event, ';
648
-        $join = '';
649
-        $join_parts = array();
650
-        $select_parts = array();
651
-        // loop through registration stati to do parts for each status.
652
-        foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) {
653
-            if ($STS_ID === EEM_Registration::status_id_incomplete) {
654
-                continue;
655
-            }
656
-            $select_parts[] = "COUNT({$STS_code}.REG_ID) as {$STS_ID}";
657
-            $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";
658
-        }
659
-        // setup the selects
660
-        $select .= implode(', ', $select_parts);
661
-        $select .= " FROM ($inner_date_query) AS dates LEFT JOIN $event_table as Event ON Event.ID = dates.EVT_ID LEFT JOIN ";
662
-        // setup remaining joins
663
-        $join .= implode(' LEFT JOIN ', $join_parts);
664
-        // now put it all together
665
-        $query = $select . $join . ' GROUP BY Registration_Event';
666
-        // and execute
667
-        return $wpdb->get_results($query, ARRAY_A);
668
-    }
669
-
670
-
671
-    /**
672
-     * Returns the EE_Registration of the primary attendee on the transaction id provided
673
-     *
674
-     * @param int $TXN_ID
675
-     * @return EE_Base_Class|EE_Registration|null
676
-     * @throws EE_Error
677
-     */
678
-    public function get_primary_registration_for_transaction_ID($TXN_ID = 0)
679
-    {
680
-        if (!$TXN_ID) {
681
-            return null;
682
-        }
683
-        return $this->get_one(array(
684
-            array(
685
-                'TXN_ID' => $TXN_ID,
686
-                'REG_count' => EEM_Registration::PRIMARY_REGISTRANT_COUNT,
687
-            ),
688
-        ));
689
-    }
690
-
691
-
692
-    /**
693
-     *        get_event_registration_count
694
-     *
695
-     * @access public
696
-     * @param int $EVT_ID
697
-     * @param boolean $for_incomplete_payments
698
-     * @return int
699
-     * @throws EE_Error
700
-     */
701
-    public function get_event_registration_count($EVT_ID, $for_incomplete_payments = false)
702
-    {
703
-        // we only count approved registrations towards registration limits
704
-        $query_params = array(array('EVT_ID' => $EVT_ID, 'STS_ID' => self::status_id_approved));
705
-        if ($for_incomplete_payments) {
706
-            $query_params[0]['Transaction.STS_ID'] = array('!=', EEM_Transaction::complete_status_code);
707
-        }
708
-        return $this->count($query_params);
709
-    }
710
-
711
-
712
-    /**
713
-     * Deletes all registrations with no transactions. Note that this needs to be very efficient
714
-     * and so it uses wpdb directly. Also, we can't put a limit on this because MySQL doesn't allow a limit on a delete
715
-     * when joining tables like this.
716
-     *
717
-     * @global WPDB $wpdb
718
-     * @return int number deleted
719
-     * @throws EE_Error
720
-     */
721
-    public function delete_registrations_with_no_transaction()
722
-    {
723
-        /** @type WPDB $wpdb */
724
-        global $wpdb;
725
-        return $wpdb->query(
726
-            'DELETE r FROM '
727
-            . $this->table()
728
-            . ' r LEFT JOIN '
729
-            . EEM_Transaction::instance()->table()
730
-            . ' t ON r.TXN_ID = t.TXN_ID WHERE t.TXN_ID IS NULL'
731
-        );
732
-    }
733
-
734
-
735
-    /**
736
-     *  Count registrations checked into (or out of) a datetime
737
-     *
738
-     * @param int $DTT_ID datetime ID
739
-     * @param boolean $checked_in whether to count registrations checked IN or OUT
740
-     * @return int
741
-     * @throws EE_Error
742
-     */
743
-    public function count_registrations_checked_into_datetime($DTT_ID, $checked_in = true)
744
-    {
745
-        global $wpdb;
746
-        // subquery to get latest checkin
747
-        $query = $wpdb->prepare(
748
-            'SELECT '
749
-            . 'COUNT( DISTINCT checkins.REG_ID ) '
750
-            . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN'
751
-            . '( SELECT '
752
-            . 'max( CHK_timestamp ) AS latest_checkin, '
753
-            . 'REG_ID AS REG_ID '
754
-            . 'FROM ' . EEM_Checkin::instance()->table() . ' '
755
-            . 'WHERE DTT_ID=%d '
756
-            . 'GROUP BY REG_ID'
757
-            . ') AS most_recent_checkin_per_reg '
758
-            . 'ON checkins.REG_ID=most_recent_checkin_per_reg.REG_ID '
759
-            . 'AND checkins.CHK_timestamp = most_recent_checkin_per_reg.latest_checkin '
760
-            . 'WHERE '
761
-            . 'checkins.CHK_in=%d',
762
-            $DTT_ID,
763
-            $checked_in
764
-        );
765
-        return (int) $wpdb->get_var($query);
766
-    }
767
-
768
-
769
-    /**
770
-     *  Count registrations checked into (or out of) an event.
771
-     *
772
-     * @param int $EVT_ID event ID
773
-     * @param boolean $checked_in whether to count registrations checked IN or OUT
774
-     * @return int
775
-     * @throws EE_Error
776
-     */
777
-    public function count_registrations_checked_into_event($EVT_ID, $checked_in = true)
778
-    {
779
-        global $wpdb;
780
-        // subquery to get latest checkin
781
-        $query = $wpdb->prepare(
782
-            'SELECT '
783
-            . 'COUNT( DISTINCT checkins.REG_ID ) '
784
-            . 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN'
785
-            . '( SELECT '
786
-            . 'max( CHK_timestamp ) AS latest_checkin, '
787
-            . 'REG_ID AS REG_ID '
788
-            . 'FROM ' . EEM_Checkin::instance()->table() . ' AS c '
789
-            . 'INNER JOIN ' . EEM_Datetime::instance()->table() . ' AS d '
790
-            . 'ON c.DTT_ID=d.DTT_ID '
791
-            . 'WHERE d.EVT_ID=%d '
792
-            . 'GROUP BY REG_ID'
793
-            . ') AS most_recent_checkin_per_reg '
794
-            . 'ON checkins.REG_ID=most_recent_checkin_per_reg.REG_ID '
795
-            . 'AND checkins.CHK_timestamp = most_recent_checkin_per_reg.latest_checkin '
796
-            . 'WHERE '
797
-            . 'checkins.CHK_in=%d',
798
-            $EVT_ID,
799
-            $checked_in
800
-        );
801
-        return (int) $wpdb->get_var($query);
802
-    }
803
-
804
-
805
-    /**
806
-     * The purpose of this method is to retrieve an array of
807
-     * EE_Registration objects that represent the latest registration
808
-     * for each ATT_ID given in the function argument.
809
-     *
810
-     * @param array $attendee_ids
811
-     * @return EE_Base_Class[]|EE_Registration[]
812
-     * @throws EE_Error
813
-     */
814
-    public function get_latest_registration_for_each_of_given_contacts($attendee_ids = array())
815
-    {
816
-        // first do a native wp_query to get the latest REG_ID's matching these attendees.
817
-        global $wpdb;
818
-        $registration_table = $wpdb->prefix . 'esp_registration';
819
-        $attendee_table = $wpdb->posts;
820
-        $attendee_ids = is_array($attendee_ids)
821
-            ? array_map('absint', $attendee_ids)
822
-            : array((int) $attendee_ids);
823
-        $ATT_IDs = implode(',', $attendee_ids);
824
-        // first we do a query to get the registration ids
825
-        // (because a group by before order by causes the order by to be ignored.)
826
-        $registration_id_query = "
16
+	/**
17
+	 * @var EEM_Registration $_instance
18
+	 */
19
+	protected static $_instance;
20
+
21
+	/**
22
+	 * Keys are the status IDs for registrations (eg, RAP, RCN, etc), and the values
23
+	 * are status codes (eg, approved, cancelled, etc)
24
+	 *
25
+	 * @var array
26
+	 */
27
+	private static $_reg_status;
28
+
29
+	/**
30
+	 * The value of REG_count for a primary registrant
31
+	 */
32
+	const PRIMARY_REGISTRANT_COUNT = 1;
33
+
34
+	/**
35
+	 * Status ID (STS_ID on esp_status table) to indicate an INCOMPLETE registration.
36
+	 * Initial status for registrations when they are first created
37
+	 * Payments are NOT allowed.
38
+	 * Automatically toggled to whatever the default Event registration status is upon completion of the attendee
39
+	 * information reg step NO space reserved. Registration is NOT active
40
+	 */
41
+	const status_id_incomplete = 'RIC';
42
+
43
+	/**
44
+	 * Status ID (STS_ID on esp_status table) to indicate an UNAPPROVED registration.
45
+	 * Payments are NOT allowed.
46
+	 * Event Admin must manually toggle STS_ID for it to change
47
+	 * No space reserved.
48
+	 * Registration is active
49
+	 */
50
+	const status_id_not_approved = 'RNA';
51
+
52
+	/**
53
+	 * Status ID (STS_ID on esp_status table) to indicate registration is PENDING_PAYMENT .
54
+	 * Payments are allowed.
55
+	 * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee
56
+	 * No space reserved.
57
+	 * Registration is active
58
+	 */
59
+	const status_id_pending_payment = 'RPP';
60
+
61
+	/**
62
+	 * Status ID (STS_ID on esp_status table) to indicate registration is on the WAIT_LIST .
63
+	 * Payments are allowed.
64
+	 * STS_ID will automatically be toggled to RAP if payment is made in full by the attendee
65
+	 * No space reserved.
66
+	 * Registration is active
67
+	 */
68
+	const status_id_wait_list = 'RWL';
69
+
70
+	/**
71
+	 * Status ID (STS_ID on esp_status table) to indicate an APPROVED registration.
72
+	 * the TXN may or may not be completed ( paid in full )
73
+	 * Payments are allowed.
74
+	 * A space IS reserved.
75
+	 * Registration is active
76
+	 */
77
+	const status_id_approved = 'RAP';
78
+
79
+	/**
80
+	 * Status ID (STS_ID on esp_status table) to indicate a registration was CANCELLED by the attendee.
81
+	 * Payments are NOT allowed.
82
+	 * NO space reserved.
83
+	 * Registration is NOT active
84
+	 */
85
+	const status_id_cancelled = 'RCN';
86
+
87
+	/**
88
+	 * Status ID (STS_ID on esp_status table) to indicate a registration was DECLINED by the Event Admin
89
+	 * Payments are NOT allowed.
90
+	 * No space reserved.
91
+	 * Registration is NOT active
92
+	 */
93
+	const status_id_declined = 'RDC';
94
+
95
+	/**
96
+	 * @var TableAnalysis $table_analysis
97
+	 */
98
+	protected $_table_analysis;
99
+
100
+
101
+	/**
102
+	 *    private constructor to prevent direct creation
103
+	 *
104
+	 * @Constructor
105
+	 * @access protected
106
+	 * @param string $timezone string representing the timezone we want to set for returned Date Time Strings (and any
107
+	 *                         incoming timezone data that gets saved). Note this just sends the timezone info to the
108
+	 *                         date time model field objects.  Default is NULL (and will be assumed using the set
109
+	 *                         timezone in the 'timezone_string' wp option)
110
+	 * @throws EE_Error
111
+	 */
112
+	protected function __construct($timezone = null)
113
+	{
114
+		$this->_table_analysis = EE_Registry::instance()->create('TableAnalysis', array(), true);
115
+		$this->singular_item = esc_html__('Registration', 'event_espresso');
116
+		$this->plural_item = esc_html__('Registrations', 'event_espresso');
117
+		$this->_tables = array(
118
+			'Registration' => new EE_Primary_Table('esp_registration', 'REG_ID'),
119
+		);
120
+		$this->_fields = array(
121
+			'Registration' => array(
122
+				'REG_ID' => new EE_Primary_Key_Int_Field(
123
+					'REG_ID',
124
+					esc_html__('Registration ID', 'event_espresso')
125
+				),
126
+				'EVT_ID' => new EE_Foreign_Key_Int_Field(
127
+					'EVT_ID',
128
+					esc_html__('Event ID', 'event_espresso'),
129
+					false,
130
+					0,
131
+					'Event'
132
+				),
133
+				'ATT_ID' => new EE_Foreign_Key_Int_Field(
134
+					'ATT_ID',
135
+					esc_html__('Attendee ID', 'event_espresso'),
136
+					false,
137
+					0,
138
+					'Attendee'
139
+				),
140
+				'TXN_ID' => new EE_Foreign_Key_Int_Field(
141
+					'TXN_ID',
142
+					esc_html__('Transaction ID', 'event_espresso'),
143
+					false,
144
+					0,
145
+					'Transaction'
146
+				),
147
+				'TKT_ID' => new EE_Foreign_Key_Int_Field(
148
+					'TKT_ID',
149
+					esc_html__('Ticket ID', 'event_espresso'),
150
+					false,
151
+					0,
152
+					'Ticket'
153
+				),
154
+				'STS_ID' => new EE_Foreign_Key_String_Field(
155
+					'STS_ID',
156
+					esc_html__('Status ID', 'event_espresso'),
157
+					false,
158
+					EEM_Registration::status_id_incomplete,
159
+					'Status'
160
+				),
161
+				'REG_date' => new EE_Datetime_Field(
162
+					'REG_date',
163
+					esc_html__('Time registration occurred', 'event_espresso'),
164
+					false,
165
+					EE_Datetime_Field::now,
166
+					$timezone
167
+				),
168
+				'REG_final_price' => new EE_Money_Field(
169
+					'REG_final_price',
170
+					esc_html__('Registration\'s share of the transaction total', 'event_espresso'),
171
+					false,
172
+					0
173
+				),
174
+				'REG_paid' => new EE_Money_Field(
175
+					'REG_paid',
176
+					esc_html__('Amount paid to date towards registration', 'event_espresso'),
177
+					false,
178
+					0
179
+				),
180
+				'REG_session' => new EE_Plain_Text_Field(
181
+					'REG_session',
182
+					esc_html__('Session ID of registration', 'event_espresso'),
183
+					false,
184
+					''
185
+				),
186
+				'REG_code' => new EE_Plain_Text_Field(
187
+					'REG_code',
188
+					esc_html__('Unique Code for this registration', 'event_espresso'),
189
+					false,
190
+					''
191
+				),
192
+				'REG_url_link' => new EE_Plain_Text_Field(
193
+					'REG_url_link',
194
+					esc_html__('String to be used in URL for identifying registration', 'event_espresso'),
195
+					false,
196
+					''
197
+				),
198
+				'REG_count' => new EE_Integer_Field(
199
+					'REG_count',
200
+					esc_html__('Count of this registration in the group registration ', 'event_espresso'),
201
+					true,
202
+					1
203
+				),
204
+				'REG_group_size' => new EE_Integer_Field(
205
+					'REG_group_size',
206
+					esc_html__('Number of registrations on this group', 'event_espresso'),
207
+					false,
208
+					1
209
+				),
210
+				'REG_att_is_going' => new EE_Boolean_Field(
211
+					'REG_att_is_going',
212
+					esc_html__('Flag indicating the registrant plans on attending', 'event_espresso'),
213
+					false,
214
+					false
215
+				),
216
+				'REG_deleted' => new EE_Trashed_Flag_Field(
217
+					'REG_deleted',
218
+					esc_html__('Flag indicating if registration has been archived or not.', 'event_espresso'),
219
+					false,
220
+					false
221
+				),
222
+			),
223
+		);
224
+		$this->_model_relations = array(
225
+			'Event' => new EE_Belongs_To_Relation(),
226
+			'Attendee' => new EE_Belongs_To_Relation(),
227
+			'Transaction' => new EE_Belongs_To_Relation(),
228
+			'Ticket' => new EE_Belongs_To_Relation(),
229
+			'Status' => new EE_Belongs_To_Relation(),
230
+			'Answer' => new EE_Has_Many_Relation(),
231
+			'Checkin' => new EE_Has_Many_Relation(),
232
+			'Registration_Payment' => new EE_Has_Many_Relation(),
233
+			'Payment' => new EE_HABTM_Relation('Registration_Payment'),
234
+			'Message' => new EE_Has_Many_Any_Relation(false)
235
+			// allow deletes even if there are messages in the queue related
236
+		);
237
+		$this->_model_chain_to_wp_user = 'Event';
238
+		parent::__construct($timezone);
239
+	}
240
+
241
+
242
+	/**
243
+	 * a list of ALL valid registration statuses currently in use within the system
244
+	 * generated by combining the filterable active and inactive reg status arrays
245
+	 *
246
+	 * @return array
247
+	 */
248
+	public static function reg_statuses()
249
+	{
250
+		return array_unique(
251
+			array_merge(
252
+				EEM_Registration::active_reg_statuses(),
253
+				EEM_Registration::inactive_reg_statuses()
254
+			)
255
+		);
256
+	}
257
+
258
+
259
+	/**
260
+	 * reg_statuses_that_allow_payment
261
+	 * a filterable list of registration statuses that allow a registrant to make a payment
262
+	 *
263
+	 * @access public
264
+	 * @return array
265
+	 */
266
+	public static function reg_statuses_that_allow_payment()
267
+	{
268
+		return apply_filters(
269
+			'FHEE__EEM_Registration__reg_statuses_that_allow_payment',
270
+			array(
271
+				EEM_Registration::status_id_approved,
272
+				EEM_Registration::status_id_pending_payment,
273
+			)
274
+		);
275
+	}
276
+
277
+
278
+	/**
279
+	 * active_reg_statuses
280
+	 * a filterable list of registration statuses that are considered active
281
+	 *
282
+	 * @access public
283
+	 * @return array
284
+	 */
285
+	public static function active_reg_statuses()
286
+	{
287
+		return apply_filters(
288
+			'FHEE__EEM_Registration__active_reg_statuses',
289
+			array(
290
+				EEM_Registration::status_id_approved,
291
+				EEM_Registration::status_id_pending_payment,
292
+				EEM_Registration::status_id_wait_list,
293
+				EEM_Registration::status_id_not_approved,
294
+			)
295
+		);
296
+	}
297
+
298
+
299
+	/**
300
+	 * inactive_reg_statuses
301
+	 * a filterable list of registration statuses that are not considered active
302
+	 *
303
+	 * @access public
304
+	 * @return array
305
+	 */
306
+	public static function inactive_reg_statuses()
307
+	{
308
+		return apply_filters(
309
+			'FHEE__EEM_Registration__inactive_reg_statuses',
310
+			array(
311
+				EEM_Registration::status_id_incomplete,
312
+				EEM_Registration::status_id_cancelled,
313
+				EEM_Registration::status_id_declined,
314
+			)
315
+		);
316
+	}
317
+
318
+
319
+	/**
320
+	 *    closed_reg_statuses
321
+	 *    a filterable list of registration statuses that are considered "closed"
322
+	 * meaning they should not be considered in any calculations involving monies owing
323
+	 *
324
+	 * @access public
325
+	 * @return array
326
+	 */
327
+	public static function closed_reg_statuses()
328
+	{
329
+		return apply_filters(
330
+			'FHEE__EEM_Registration__closed_reg_statuses',
331
+			array(
332
+				EEM_Registration::status_id_cancelled,
333
+				EEM_Registration::status_id_declined,
334
+				EEM_Registration::status_id_wait_list,
335
+			)
336
+		);
337
+	}
338
+
339
+
340
+	/**
341
+	 *        get list of registration statuses
342
+	 *
343
+	 * @access public
344
+	 * @param array $exclude The status ids to exclude from the returned results
345
+	 * @param bool $translated If true will return the values as singular localized strings
346
+	 * @return array
347
+	 * @throws EE_Error
348
+	 */
349
+	public static function reg_status_array($exclude = array(), $translated = false)
350
+	{
351
+		EEM_Registration::instance()->_get_registration_status_array($exclude);
352
+		return $translated
353
+			? EEM_Status::instance()->localized_status(self::$_reg_status, false, 'sentence')
354
+			: self::$_reg_status;
355
+	}
356
+
357
+
358
+	/**
359
+	 *    get list of registration statuses
360
+	 *
361
+	 * @access private
362
+	 * @param array $exclude
363
+	 * @return void
364
+	 * @throws EE_Error
365
+	 */
366
+	private function _get_registration_status_array($exclude = array())
367
+	{
368
+		// in the very rare circumstance that we are deleting a model's table's data
369
+		// and the table hasn't actually been created, this could have an error
370
+		/** @type WPDB $wpdb */
371
+		global $wpdb;
372
+		if ($this->_get_table_analysis()->tableExists($wpdb->prefix . 'esp_status')) {
373
+			$results = $wpdb->get_results(
374
+				"SELECT STS_ID, STS_code FROM {$wpdb->prefix}esp_status WHERE STS_type = 'registration'"
375
+			);
376
+			self::$_reg_status = array();
377
+			foreach ($results as $status) {
378
+				if (!in_array($status->STS_ID, $exclude, true)) {
379
+					self::$_reg_status[ $status->STS_ID ] = $status->STS_code;
380
+				}
381
+			}
382
+		}
383
+	}
384
+
385
+
386
+	/**
387
+	 * Gets the injected table analyzer, or throws an exception
388
+	 *
389
+	 * @return TableAnalysis
390
+	 * @throws EE_Error
391
+	 */
392
+	protected function _get_table_analysis()
393
+	{
394
+		if ($this->_table_analysis instanceof TableAnalysis) {
395
+			return $this->_table_analysis;
396
+		}
397
+		throw new EE_Error(
398
+			sprintf(
399
+				esc_html__('Table analysis class on class %1$s is not set properly.', 'event_espresso'),
400
+				get_class($this)
401
+			)
402
+		);
403
+	}
404
+
405
+
406
+	/**
407
+	 * This returns a wpdb->results array of all registration date month and years matching the incoming query params
408
+	 * and grouped by month and year.
409
+	 *
410
+	 * @param  array $where_params @see https://github.com/eventespresso/event-espresso-core/tree/master/docs/G--Model-System/model-query-params.md#0-where-conditions
411
+	 * @return array
412
+	 * @throws EE_Error
413
+	 */
414
+	public function get_reg_months_and_years($where_params)
415
+	{
416
+		$query_params[0] = $where_params;
417
+		$query_params['group_by'] = array('reg_year', 'reg_month');
418
+		$query_params['order_by'] = array('REG_date' => 'DESC');
419
+		$columns_to_select = array(
420
+			'reg_year' => array('YEAR(REG_date)', '%s'),
421
+			'reg_month' => array('MONTHNAME(REG_date)', '%s'),
422
+		);
423
+		return $this->_get_all_wpdb_results($query_params, OBJECT, $columns_to_select);
424
+	}
425
+
426
+
427
+	/**
428
+	 * retrieve ALL registrations for a particular Attendee from db
429
+	 *
430
+	 * @param int $ATT_ID
431
+	 * @return EE_Base_Class[]|EE_Registration[]|null
432
+	 * @throws EE_Error
433
+	 */
434
+	public function get_all_registrations_for_attendee($ATT_ID = 0)
435
+	{
436
+		if (!$ATT_ID) {
437
+			return null;
438
+		}
439
+		return $this->get_all(array(array('ATT_ID' => $ATT_ID)));
440
+	}
441
+
442
+
443
+	/**
444
+	 * Gets a registration given their REG_url_link. Yes, this should usually
445
+	 * be passed via a GET parameter.
446
+	 *
447
+	 * @param string $REG_url_link
448
+	 * @return EE_Base_Class|EE_Registration|null
449
+	 * @throws EE_Error
450
+	 */
451
+	public function get_registration_for_reg_url_link($REG_url_link)
452
+	{
453
+		if (!$REG_url_link) {
454
+			return null;
455
+		}
456
+		return $this->get_one(array(array('REG_url_link' => $REG_url_link)));
457
+	}
458
+
459
+
460
+	/**
461
+	 *        retrieve registration for a specific transaction attendee from db
462
+	 *
463
+	 * @access        public
464
+	 * @param    int $TXN_ID
465
+	 * @param    int $ATT_ID
466
+	 * @param    int $att_nmbr in case the ATT_ID is the same for multiple registrations (same details used) then the
467
+	 *                         attendee number is required
468
+	 * @return        mixed        array on success, FALSE on fail
469
+	 * @throws EE_Error
470
+	 */
471
+	public function get_registration_for_transaction_attendee($TXN_ID = 0, $ATT_ID = 0, $att_nmbr = 0)
472
+	{
473
+		return $this->get_one(array(
474
+			array(
475
+				'TXN_ID' => $TXN_ID,
476
+				'ATT_ID' => $ATT_ID,
477
+			),
478
+			'limit' => array(min($att_nmbr - 1, 0), 1),
479
+		));
480
+	}
481
+
482
+
483
+	/**
484
+	 *        get the number of registrations per day  for the Registration Admin page Reports Tab.
485
+	 *        (doesn't utilize models because it's a fairly specialized query)
486
+	 *
487
+	 * @access        public
488
+	 * @param $period string which can be passed to php's strtotime function (eg "-1 month")
489
+	 * @return stdClass[] with properties regDate and total
490
+	 * @throws EE_Error
491
+	 */
492
+	public function get_registrations_per_day_report($period = '-1 month')
493
+	{
494
+		$sql_date = $this->convert_datetime_for_query(
495
+			'REG_date',
496
+			date('Y-m-d H:i:s', strtotime($period)),
497
+			'Y-m-d H:i:s',
498
+			'UTC'
499
+		);
500
+		$where = array(
501
+			'REG_date' => array('>=', $sql_date),
502
+			'STS_ID' => array('!=', EEM_Registration::status_id_incomplete),
503
+		);
504
+		if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_day_report')) {
505
+			$where['Event.EVT_wp_user'] = get_current_user_id();
506
+		}
507
+		$query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'REG_date');
508
+		$results = $this->_get_all_wpdb_results(
509
+			array(
510
+				$where,
511
+				'group_by' => 'regDate',
512
+				'order_by' => array('REG_date' => 'ASC'),
513
+			),
514
+			OBJECT,
515
+			array(
516
+				'regDate' => array('DATE(' . $query_interval . ')', '%s'),
517
+				'total' => array('count(REG_ID)', '%d'),
518
+			)
519
+		);
520
+		return $results;
521
+	}
522
+
523
+
524
+	/**
525
+	 * Get the number of registrations per day including the count of registrations for each Registration Status.
526
+	 * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results.
527
+	 *
528
+	 * @param string $period
529
+	 * @return stdClass[] with properties Registration_REG_date and a column for each registration status as the STS_ID
530
+	 * @throws EE_Error
531
+	 *                    (i.e. RAP)
532
+	 */
533
+	public function get_registrations_per_day_and_per_status_report($period = '-1 month')
534
+	{
535
+		global $wpdb;
536
+		$registration_table = $wpdb->prefix . 'esp_registration';
537
+		$event_table = $wpdb->posts;
538
+		$sql_date = date('Y-m-d H:i:s', strtotime($period));
539
+		// prepare the query interval for displaying offset
540
+		$query_interval = EEH_DTT_Helper::get_sql_query_interval_for_offset($this->get_timezone(), 'dates.REG_date');
541
+		// inner date query
542
+		$inner_date_query = "SELECT DISTINCT REG_date from {$registration_table} ";
543
+		$inner_where = ' WHERE';
544
+		// exclude events not authored by user if permissions in effect
545
+		if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
546
+			$inner_date_query .= "LEFT JOIN {$event_table} ON ID = EVT_ID";
547
+			$inner_where .= ' post_author = ' . get_current_user_id() . ' AND';
548
+		}
549
+		$inner_where .= " REG_date >= '{$sql_date}'";
550
+		$inner_date_query .= $inner_where;
551
+		// start main query
552
+		$select = "SELECT DATE({$query_interval}) as Registration_REG_date, ";
553
+		$join = '';
554
+		$join_parts = array();
555
+		$select_parts = array();
556
+		// loop through registration stati to do parts for each status.
557
+		foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) {
558
+			if ($STS_ID === EEM_Registration::status_id_incomplete) {
559
+				continue;
560
+			}
561
+			$select_parts[] = "COUNT({$STS_code}.REG_ID) as {$STS_ID}";
562
+			$join_parts[] = "{$registration_table} AS {$STS_code} ON {$STS_code}.REG_date = dates.REG_date AND {$STS_code}.STS_ID = '{$STS_ID}'";
563
+		}
564
+		// setup the selects
565
+		$select .= implode(', ', $select_parts);
566
+		$select .= " FROM ($inner_date_query) AS dates LEFT JOIN ";
567
+		// setup the joins
568
+		$join .= implode(' LEFT JOIN ', $join_parts);
569
+		// now let's put it all together
570
+		$query = $select . $join . ' GROUP BY Registration_REG_date';
571
+		// and execute it
572
+		return $wpdb->get_results($query, ARRAY_A);
573
+	}
574
+
575
+
576
+	/**
577
+	 *        get the number of registrations per event  for the Registration Admin page Reports Tab
578
+	 *
579
+	 * @access        public
580
+	 * @param $period string which can be passed to php's strtotime function (eg "-1 month")
581
+	 * @return stdClass[] each with properties event_name, reg_limit, and total
582
+	 * @throws EE_Error
583
+	 */
584
+	public function get_registrations_per_event_report($period = '-1 month')
585
+	{
586
+		$date_sql = $this->convert_datetime_for_query(
587
+			'REG_date',
588
+			date('Y-m-d H:i:s', strtotime($period)),
589
+			'Y-m-d H:i:s',
590
+			'UTC'
591
+		);
592
+		$where = array(
593
+			'REG_date' => array('>=', $date_sql),
594
+			'STS_ID' => array('!=', EEM_Registration::status_id_incomplete),
595
+		);
596
+		if (
597
+			!EE_Registry::instance()->CAP->current_user_can(
598
+				'ee_read_others_registrations',
599
+				'reg_per_event_report'
600
+			)
601
+		) {
602
+			$where['Event.EVT_wp_user'] = get_current_user_id();
603
+		}
604
+		$results = $this->_get_all_wpdb_results(
605
+			array(
606
+			$where,
607
+			'group_by' => 'Event.EVT_name',
608
+			'order_by' => 'Event.EVT_name',
609
+			'limit' => array(0, 24),
610
+			),
611
+			OBJECT,
612
+			array(
613
+				'event_name' => array('Event_CPT.post_title', '%s'),
614
+				'total' => array('COUNT(REG_ID)', '%s'),
615
+			)
616
+		);
617
+		return $results;
618
+	}
619
+
620
+
621
+	/**
622
+	 * Get the number of registrations per event grouped by registration status.
623
+	 * Note: EEM_Registration::status_id_incomplete registrations are excluded from the results.
624
+	 *
625
+	 * @param string $period
626
+	 * @return stdClass[] with properties `Registration_Event` and a column for each registration status as the STS_ID
627
+	 * @throws EE_Error
628
+	 *                    (i.e. RAP)
629
+	 */
630
+	public function get_registrations_per_event_and_per_status_report($period = '-1 month')
631
+	{
632
+		global $wpdb;
633
+		$registration_table = $wpdb->prefix . 'esp_registration';
634
+		$event_table = $wpdb->posts;
635
+		$sql_date = date('Y-m-d H:i:s', strtotime($period));
636
+		// inner date query
637
+		$inner_date_query = "SELECT DISTINCT EVT_ID, REG_date from $registration_table ";
638
+		$inner_where = ' WHERE';
639
+		// exclude events not authored by user if permissions in effect
640
+		if (!EE_Registry::instance()->CAP->current_user_can('ee_read_others_registrations', 'reg_per_event_report')) {
641
+			$inner_date_query .= "LEFT JOIN {$event_table} ON ID = EVT_ID";
642
+			$inner_where .= ' post_author = ' . get_current_user_id() . ' AND';
643
+		}
644
+		$inner_where .= " REG_date >= '{$sql_date}'";
645
+		$inner_date_query .= $inner_where;
646
+		// build main query
647
+		$select = 'SELECT Event.post_title as Registration_Event, ';
648
+		$join = '';
649
+		$join_parts = array();
650
+		$select_parts = array();
651
+		// loop through registration stati to do parts for each status.
652
+		foreach (EEM_Registration::reg_status_array() as $STS_ID => $STS_code) {
653
+			if ($STS_ID === EEM_Registration::status_id_incomplete) {
654
+				continue;
655
+			}
656
+			$select_parts[] = "COUNT({$STS_code}.REG_ID) as {$STS_ID}";
657
+			$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";
658
+		}
659
+		// setup the selects
660
+		$select .= implode(', ', $select_parts);
661
+		$select .= " FROM ($inner_date_query) AS dates LEFT JOIN $event_table as Event ON Event.ID = dates.EVT_ID LEFT JOIN ";
662
+		// setup remaining joins
663
+		$join .= implode(' LEFT JOIN ', $join_parts);
664
+		// now put it all together
665
+		$query = $select . $join . ' GROUP BY Registration_Event';
666
+		// and execute
667
+		return $wpdb->get_results($query, ARRAY_A);
668
+	}
669
+
670
+
671
+	/**
672
+	 * Returns the EE_Registration of the primary attendee on the transaction id provided
673
+	 *
674
+	 * @param int $TXN_ID
675
+	 * @return EE_Base_Class|EE_Registration|null
676
+	 * @throws EE_Error
677
+	 */
678
+	public function get_primary_registration_for_transaction_ID($TXN_ID = 0)
679
+	{
680
+		if (!$TXN_ID) {
681
+			return null;
682
+		}
683
+		return $this->get_one(array(
684
+			array(
685
+				'TXN_ID' => $TXN_ID,
686
+				'REG_count' => EEM_Registration::PRIMARY_REGISTRANT_COUNT,
687
+			),
688
+		));
689
+	}
690
+
691
+
692
+	/**
693
+	 *        get_event_registration_count
694
+	 *
695
+	 * @access public
696
+	 * @param int $EVT_ID
697
+	 * @param boolean $for_incomplete_payments
698
+	 * @return int
699
+	 * @throws EE_Error
700
+	 */
701
+	public function get_event_registration_count($EVT_ID, $for_incomplete_payments = false)
702
+	{
703
+		// we only count approved registrations towards registration limits
704
+		$query_params = array(array('EVT_ID' => $EVT_ID, 'STS_ID' => self::status_id_approved));
705
+		if ($for_incomplete_payments) {
706
+			$query_params[0]['Transaction.STS_ID'] = array('!=', EEM_Transaction::complete_status_code);
707
+		}
708
+		return $this->count($query_params);
709
+	}
710
+
711
+
712
+	/**
713
+	 * Deletes all registrations with no transactions. Note that this needs to be very efficient
714
+	 * and so it uses wpdb directly. Also, we can't put a limit on this because MySQL doesn't allow a limit on a delete
715
+	 * when joining tables like this.
716
+	 *
717
+	 * @global WPDB $wpdb
718
+	 * @return int number deleted
719
+	 * @throws EE_Error
720
+	 */
721
+	public function delete_registrations_with_no_transaction()
722
+	{
723
+		/** @type WPDB $wpdb */
724
+		global $wpdb;
725
+		return $wpdb->query(
726
+			'DELETE r FROM '
727
+			. $this->table()
728
+			. ' r LEFT JOIN '
729
+			. EEM_Transaction::instance()->table()
730
+			. ' t ON r.TXN_ID = t.TXN_ID WHERE t.TXN_ID IS NULL'
731
+		);
732
+	}
733
+
734
+
735
+	/**
736
+	 *  Count registrations checked into (or out of) a datetime
737
+	 *
738
+	 * @param int $DTT_ID datetime ID
739
+	 * @param boolean $checked_in whether to count registrations checked IN or OUT
740
+	 * @return int
741
+	 * @throws EE_Error
742
+	 */
743
+	public function count_registrations_checked_into_datetime($DTT_ID, $checked_in = true)
744
+	{
745
+		global $wpdb;
746
+		// subquery to get latest checkin
747
+		$query = $wpdb->prepare(
748
+			'SELECT '
749
+			. 'COUNT( DISTINCT checkins.REG_ID ) '
750
+			. 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN'
751
+			. '( SELECT '
752
+			. 'max( CHK_timestamp ) AS latest_checkin, '
753
+			. 'REG_ID AS REG_ID '
754
+			. 'FROM ' . EEM_Checkin::instance()->table() . ' '
755
+			. 'WHERE DTT_ID=%d '
756
+			. 'GROUP BY REG_ID'
757
+			. ') AS most_recent_checkin_per_reg '
758
+			. 'ON checkins.REG_ID=most_recent_checkin_per_reg.REG_ID '
759
+			. 'AND checkins.CHK_timestamp = most_recent_checkin_per_reg.latest_checkin '
760
+			. 'WHERE '
761
+			. 'checkins.CHK_in=%d',
762
+			$DTT_ID,
763
+			$checked_in
764
+		);
765
+		return (int) $wpdb->get_var($query);
766
+	}
767
+
768
+
769
+	/**
770
+	 *  Count registrations checked into (or out of) an event.
771
+	 *
772
+	 * @param int $EVT_ID event ID
773
+	 * @param boolean $checked_in whether to count registrations checked IN or OUT
774
+	 * @return int
775
+	 * @throws EE_Error
776
+	 */
777
+	public function count_registrations_checked_into_event($EVT_ID, $checked_in = true)
778
+	{
779
+		global $wpdb;
780
+		// subquery to get latest checkin
781
+		$query = $wpdb->prepare(
782
+			'SELECT '
783
+			. 'COUNT( DISTINCT checkins.REG_ID ) '
784
+			. 'FROM ' . EEM_Checkin::instance()->table() . ' AS checkins INNER JOIN'
785
+			. '( SELECT '
786
+			. 'max( CHK_timestamp ) AS latest_checkin, '
787
+			. 'REG_ID AS REG_ID '
788
+			. 'FROM ' . EEM_Checkin::instance()->table() . ' AS c '
789
+			. 'INNER JOIN ' . EEM_Datetime::instance()->table() . ' AS d '
790
+			. 'ON c.DTT_ID=d.DTT_ID '
791
+			. 'WHERE d.EVT_ID=%d '
792
+			. 'GROUP BY REG_ID'
793
+			. ') AS most_recent_checkin_per_reg '
794
+			. 'ON checkins.REG_ID=most_recent_checkin_per_reg.REG_ID '
795
+			. 'AND checkins.CHK_timestamp = most_recent_checkin_per_reg.latest_checkin '
796
+			. 'WHERE '
797
+			. 'checkins.CHK_in=%d',
798
+			$EVT_ID,
799
+			$checked_in
800
+		);
801
+		return (int) $wpdb->get_var($query);
802
+	}
803
+
804
+
805
+	/**
806
+	 * The purpose of this method is to retrieve an array of
807
+	 * EE_Registration objects that represent the latest registration
808
+	 * for each ATT_ID given in the function argument.
809
+	 *
810
+	 * @param array $attendee_ids
811
+	 * @return EE_Base_Class[]|EE_Registration[]
812
+	 * @throws EE_Error
813
+	 */
814
+	public function get_latest_registration_for_each_of_given_contacts($attendee_ids = array())
815
+	{
816
+		// first do a native wp_query to get the latest REG_ID's matching these attendees.
817
+		global $wpdb;
818
+		$registration_table = $wpdb->prefix . 'esp_registration';
819
+		$attendee_table = $wpdb->posts;
820
+		$attendee_ids = is_array($attendee_ids)
821
+			? array_map('absint', $attendee_ids)
822
+			: array((int) $attendee_ids);
823
+		$ATT_IDs = implode(',', $attendee_ids);
824
+		// first we do a query to get the registration ids
825
+		// (because a group by before order by causes the order by to be ignored.)
826
+		$registration_id_query = "
827 827
 			SELECT registrations.registration_ids as registration_id
828 828
 			FROM (
829 829
 				SELECT
@@ -837,61 +837,61 @@  discard block
 block discarded – undo
837 837
 			  ) AS registrations
838 838
 			  GROUP BY registrations.attendee_ids
839 839
 		";
840
-        $registration_ids = $wpdb->get_results($registration_id_query, ARRAY_A);
841
-        if (empty($registration_ids)) {
842
-            return array();
843
-        }
844
-        $ids_for_model_query = array();
845
-        // let's flatten the ids so they can be used in the model query.
846
-        foreach ($registration_ids as $registration_id) {
847
-            if (isset($registration_id['registration_id'])) {
848
-                $ids_for_model_query[] = $registration_id['registration_id'];
849
-            }
850
-        }
851
-        // construct query
852
-        $_where = array(
853
-            'REG_ID' => array('IN', $ids_for_model_query),
854
-        );
855
-        return $this->get_all(array($_where));
856
-    }
857
-
858
-
859
-
860
-    /**
861
-     * returns a count of registrations for the supplied event having the status as specified
862
-     *
863
-     * @param int $EVT_ID
864
-     * @param array $statuses
865
-     * @return int
866
-     * @throws InvalidArgumentException
867
-     * @throws InvalidStatusException
868
-     * @throws EE_Error
869
-     */
870
-    public function event_reg_count_for_statuses($EVT_ID, $statuses = array())
871
-    {
872
-        $EVT_ID = absint($EVT_ID);
873
-        if (! $EVT_ID) {
874
-            throw new InvalidArgumentException(
875
-                esc_html__('An invalid Event ID was supplied.', 'event_espresso')
876
-            );
877
-        }
878
-        $statuses = is_array($statuses) ? $statuses : array($statuses);
879
-        $statuses = ! empty($statuses) ? $statuses : array(EEM_Registration::status_id_approved);
880
-        $valid_reg_statuses = EEM_Registration::reg_statuses();
881
-        foreach ($statuses as $status) {
882
-            if (! in_array($status, $valid_reg_statuses, true)) {
883
-                throw new InvalidStatusException($status, esc_html__('Registration', 'event_espresso'));
884
-            }
885
-        }
886
-        return $this->count(
887
-            array(
888
-                array(
889
-                    'EVT_ID' => $EVT_ID,
890
-                    'STS_ID' => array('IN', $statuses),
891
-                ),
892
-            ),
893
-            'REG_ID',
894
-            true
895
-        );
896
-    }
840
+		$registration_ids = $wpdb->get_results($registration_id_query, ARRAY_A);
841
+		if (empty($registration_ids)) {
842
+			return array();
843
+		}
844
+		$ids_for_model_query = array();
845
+		// let's flatten the ids so they can be used in the model query.
846
+		foreach ($registration_ids as $registration_id) {
847
+			if (isset($registration_id['registration_id'])) {
848
+				$ids_for_model_query[] = $registration_id['registration_id'];
849
+			}
850
+		}
851
+		// construct query
852
+		$_where = array(
853
+			'REG_ID' => array('IN', $ids_for_model_query),
854
+		);
855
+		return $this->get_all(array($_where));
856
+	}
857
+
858
+
859
+
860
+	/**
861
+	 * returns a count of registrations for the supplied event having the status as specified
862
+	 *
863
+	 * @param int $EVT_ID
864
+	 * @param array $statuses
865
+	 * @return int
866
+	 * @throws InvalidArgumentException
867
+	 * @throws InvalidStatusException
868
+	 * @throws EE_Error
869
+	 */
870
+	public function event_reg_count_for_statuses($EVT_ID, $statuses = array())
871
+	{
872
+		$EVT_ID = absint($EVT_ID);
873
+		if (! $EVT_ID) {
874
+			throw new InvalidArgumentException(
875
+				esc_html__('An invalid Event ID was supplied.', 'event_espresso')
876
+			);
877
+		}
878
+		$statuses = is_array($statuses) ? $statuses : array($statuses);
879
+		$statuses = ! empty($statuses) ? $statuses : array(EEM_Registration::status_id_approved);
880
+		$valid_reg_statuses = EEM_Registration::reg_statuses();
881
+		foreach ($statuses as $status) {
882
+			if (! in_array($status, $valid_reg_statuses, true)) {
883
+				throw new InvalidStatusException($status, esc_html__('Registration', 'event_espresso'));
884
+			}
885
+		}
886
+		return $this->count(
887
+			array(
888
+				array(
889
+					'EVT_ID' => $EVT_ID,
890
+					'STS_ID' => array('IN', $statuses),
891
+				),
892
+			),
893
+			'REG_ID',
894
+			true
895
+		);
896
+	}
897 897
 }
Please login to merge, or discard this patch.