@@ -11,209 +11,209 @@ |
||
11 | 11 | class EEM_Country extends EEM_Base |
12 | 12 | { |
13 | 13 | |
14 | - // private instance of the Attendee object |
|
15 | - protected static $_instance = null; |
|
16 | - |
|
17 | - // array of all countries |
|
18 | - private static $_all_countries = false; |
|
19 | - |
|
20 | - // array of all active countries |
|
21 | - private static $_active_countries = false; |
|
22 | - |
|
23 | - |
|
24 | - /** |
|
25 | - * Resets the country |
|
26 | - * |
|
27 | - * @param string $timezone |
|
28 | - * @return EEM_Country|EEM_Base|null |
|
29 | - * @throws EE_Error |
|
30 | - * @throws ReflectionException |
|
31 | - */ |
|
32 | - public static function reset($timezone = null) |
|
33 | - { |
|
34 | - self::$_active_countries = null; |
|
35 | - self::$_all_countries = null; |
|
36 | - return parent::reset($timezone); |
|
37 | - } |
|
38 | - |
|
39 | - |
|
40 | - protected function __construct(?string $timezone = '') |
|
41 | - { |
|
42 | - $this->singular_item = esc_html__('Country', 'event_espresso'); |
|
43 | - $this->plural_item = esc_html__('Countries', 'event_espresso'); |
|
44 | - |
|
45 | - $this->_tables = [ |
|
46 | - 'Country' => new EE_Primary_Table('esp_country', 'CNT_ISO'), |
|
47 | - ]; |
|
48 | - |
|
49 | - $this->_fields = [ |
|
50 | - 'Country' => [ |
|
51 | - 'CNT_active' => new EE_Boolean_Field( |
|
52 | - 'CNT_active', |
|
53 | - esc_html__('Country Appears in Dropdown Select Lists', 'event_espresso'), |
|
54 | - false, |
|
55 | - true |
|
56 | - ), |
|
57 | - 'CNT_ISO' => new EE_Primary_Key_String_Field( |
|
58 | - 'CNT_ISO', |
|
59 | - esc_html__('Country ISO Code', 'event_espresso') |
|
60 | - ), |
|
61 | - 'CNT_ISO3' => new EE_All_Caps_Text_Field( |
|
62 | - 'CNT_ISO3', |
|
63 | - esc_html__('Country ISO3 Code', 'event_espresso'), |
|
64 | - false, |
|
65 | - '' |
|
66 | - ), |
|
67 | - 'RGN_ID' => new EE_Integer_Field( |
|
68 | - 'RGN_ID', |
|
69 | - esc_html__('Region ID', 'event_espresso'), |
|
70 | - false, |
|
71 | - 0 |
|
72 | - ), |
|
73 | - // should be a foreign key, but no region table exists yet |
|
74 | - 'CNT_name' => new EE_Plain_Text_Field( |
|
75 | - 'CNT_name', |
|
76 | - esc_html__('Country Name', 'event_espresso'), |
|
77 | - false, |
|
78 | - '' |
|
79 | - ), |
|
80 | - 'CNT_cur_code' => new EE_All_Caps_Text_Field( |
|
81 | - 'CNT_cur_code', |
|
82 | - esc_html__('Country Currency Code', 'event_espresso'), |
|
83 | - false |
|
84 | - ), |
|
85 | - 'CNT_cur_single' => new EE_Plain_Text_Field( |
|
86 | - 'CNT_cur_single', |
|
87 | - esc_html__('Currency Name Singular', 'event_espresso'), |
|
88 | - false |
|
89 | - ), |
|
90 | - 'CNT_cur_plural' => new EE_Plain_Text_Field( |
|
91 | - 'CNT_cur_plural', |
|
92 | - esc_html__('Currency Name Plural', 'event_espresso'), |
|
93 | - false |
|
94 | - ), |
|
95 | - 'CNT_cur_sign' => new EE_Plain_Text_Field( |
|
96 | - 'CNT_cur_sign', |
|
97 | - esc_html__('Currency Sign', 'event_espresso'), |
|
98 | - false |
|
99 | - ), |
|
100 | - 'CNT_cur_sign_b4' => new EE_Boolean_Field( |
|
101 | - 'CNT_cur_sign_b4', |
|
102 | - esc_html__('Currency Sign Before Number', 'event_espresso'), |
|
103 | - false, |
|
104 | - true |
|
105 | - ), |
|
106 | - 'CNT_cur_dec_plc' => new EE_Integer_Field( |
|
107 | - 'CNT_cur_dec_plc', |
|
108 | - esc_html__('Currency Decimal Places', 'event_espresso'), |
|
109 | - false, |
|
110 | - 2 |
|
111 | - ), |
|
112 | - 'CNT_cur_dec_mrk' => new EE_Plain_Text_Field( |
|
113 | - 'CNT_cur_dec_mrk', |
|
114 | - esc_html__('Currency Decimal Mark', 'event_espresso'), |
|
115 | - false, |
|
116 | - '.' |
|
117 | - ), |
|
118 | - 'CNT_cur_thsnds' => new EE_Plain_Text_Field( |
|
119 | - 'CNT_cur_thsnds', |
|
120 | - esc_html__('Currency Thousands Separator', 'event_espresso'), |
|
121 | - false, |
|
122 | - ',' |
|
123 | - ), |
|
124 | - 'CNT_tel_code' => new EE_Plain_Text_Field( |
|
125 | - 'CNT_tel_code', |
|
126 | - esc_html__('Country Telephone Code', 'event_espresso'), |
|
127 | - false, |
|
128 | - '' |
|
129 | - ), |
|
130 | - 'CNT_is_EU' => new EE_Boolean_Field( |
|
131 | - 'CNT_is_EU', |
|
132 | - esc_html__('Country is Member of EU', 'event_espresso'), |
|
133 | - false, |
|
134 | - false |
|
135 | - ), |
|
136 | - ], |
|
137 | - ]; |
|
138 | - $this->_model_relations = [ |
|
139 | - 'Attendee' => new EE_Has_Many_Relation(), |
|
140 | - 'State' => new EE_Has_Many_Relation(), |
|
141 | - 'Venue' => new EE_Has_Many_Relation(), |
|
142 | - ]; |
|
143 | - // only anyone to view, but only those with the default role can do anything |
|
144 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
145 | - |
|
146 | - parent::__construct($timezone); |
|
147 | - } |
|
148 | - |
|
149 | - |
|
150 | - /** |
|
151 | - * @return EE_Country[] |
|
152 | - * @throws EE_Error |
|
153 | - */ |
|
154 | - public function get_all_countries(): array |
|
155 | - { |
|
156 | - if (! self::$_all_countries) { |
|
157 | - self::$_all_countries = $this->get_all(['order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
|
158 | - } |
|
159 | - return self::$_all_countries; |
|
160 | - } |
|
161 | - |
|
162 | - |
|
163 | - /** |
|
164 | - * Gets and caches the list of active countries. If you know the list of active countries |
|
165 | - * has changed during this request, first use EEM_Country::reset() to flush the cache |
|
166 | - * |
|
167 | - * @return EE_Country[] |
|
168 | - * @throws EE_Error |
|
169 | - */ |
|
170 | - public function get_all_active_countries(): array |
|
171 | - { |
|
172 | - if (! self::$_active_countries) { |
|
173 | - self::$_active_countries = |
|
174 | - $this->get_all([['CNT_active' => true], 'order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
|
175 | - } |
|
176 | - return self::$_active_countries; |
|
177 | - } |
|
178 | - |
|
179 | - |
|
180 | - /** |
|
181 | - * Gets the country's name by its ISO |
|
182 | - * |
|
183 | - * @param string $country_ISO |
|
184 | - * @return string |
|
185 | - * @throws EE_Error |
|
186 | - */ |
|
187 | - public function get_country_name_by_ISO(string $country_ISO): string |
|
188 | - { |
|
189 | - $countries = $this->get_all_countries(); |
|
190 | - if (isset($countries[ $country_ISO ]) && $countries[ $country_ISO ] instanceof EE_Country) { |
|
191 | - return $countries[ $country_ISO ]->name(); |
|
192 | - } |
|
193 | - $names = $this->get_col([['CNT_ISO' => $country_ISO], 'limit' => 1], 'CNT_name'); |
|
194 | - if (is_array($names) && ! empty($names)) { |
|
195 | - return reset($names); |
|
196 | - } else { |
|
197 | - return ''; |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * Gets the country's name by its name |
|
204 | - * |
|
205 | - * @param string $country_name |
|
206 | - * @return EE_Country|null |
|
207 | - * @throws EE_Error |
|
208 | - */ |
|
209 | - public function getCountryByName(string $country_name): ?EE_Country |
|
210 | - { |
|
211 | - $countries = $this->get_all_countries(); |
|
212 | - foreach ($countries as $country) { |
|
213 | - if ($country instanceof EE_Country && $country->name() === $country_name) { |
|
214 | - return $country; |
|
215 | - } |
|
216 | - } |
|
217 | - return $this->get_one([['CNT_name' => $country_name]]); |
|
218 | - } |
|
14 | + // private instance of the Attendee object |
|
15 | + protected static $_instance = null; |
|
16 | + |
|
17 | + // array of all countries |
|
18 | + private static $_all_countries = false; |
|
19 | + |
|
20 | + // array of all active countries |
|
21 | + private static $_active_countries = false; |
|
22 | + |
|
23 | + |
|
24 | + /** |
|
25 | + * Resets the country |
|
26 | + * |
|
27 | + * @param string $timezone |
|
28 | + * @return EEM_Country|EEM_Base|null |
|
29 | + * @throws EE_Error |
|
30 | + * @throws ReflectionException |
|
31 | + */ |
|
32 | + public static function reset($timezone = null) |
|
33 | + { |
|
34 | + self::$_active_countries = null; |
|
35 | + self::$_all_countries = null; |
|
36 | + return parent::reset($timezone); |
|
37 | + } |
|
38 | + |
|
39 | + |
|
40 | + protected function __construct(?string $timezone = '') |
|
41 | + { |
|
42 | + $this->singular_item = esc_html__('Country', 'event_espresso'); |
|
43 | + $this->plural_item = esc_html__('Countries', 'event_espresso'); |
|
44 | + |
|
45 | + $this->_tables = [ |
|
46 | + 'Country' => new EE_Primary_Table('esp_country', 'CNT_ISO'), |
|
47 | + ]; |
|
48 | + |
|
49 | + $this->_fields = [ |
|
50 | + 'Country' => [ |
|
51 | + 'CNT_active' => new EE_Boolean_Field( |
|
52 | + 'CNT_active', |
|
53 | + esc_html__('Country Appears in Dropdown Select Lists', 'event_espresso'), |
|
54 | + false, |
|
55 | + true |
|
56 | + ), |
|
57 | + 'CNT_ISO' => new EE_Primary_Key_String_Field( |
|
58 | + 'CNT_ISO', |
|
59 | + esc_html__('Country ISO Code', 'event_espresso') |
|
60 | + ), |
|
61 | + 'CNT_ISO3' => new EE_All_Caps_Text_Field( |
|
62 | + 'CNT_ISO3', |
|
63 | + esc_html__('Country ISO3 Code', 'event_espresso'), |
|
64 | + false, |
|
65 | + '' |
|
66 | + ), |
|
67 | + 'RGN_ID' => new EE_Integer_Field( |
|
68 | + 'RGN_ID', |
|
69 | + esc_html__('Region ID', 'event_espresso'), |
|
70 | + false, |
|
71 | + 0 |
|
72 | + ), |
|
73 | + // should be a foreign key, but no region table exists yet |
|
74 | + 'CNT_name' => new EE_Plain_Text_Field( |
|
75 | + 'CNT_name', |
|
76 | + esc_html__('Country Name', 'event_espresso'), |
|
77 | + false, |
|
78 | + '' |
|
79 | + ), |
|
80 | + 'CNT_cur_code' => new EE_All_Caps_Text_Field( |
|
81 | + 'CNT_cur_code', |
|
82 | + esc_html__('Country Currency Code', 'event_espresso'), |
|
83 | + false |
|
84 | + ), |
|
85 | + 'CNT_cur_single' => new EE_Plain_Text_Field( |
|
86 | + 'CNT_cur_single', |
|
87 | + esc_html__('Currency Name Singular', 'event_espresso'), |
|
88 | + false |
|
89 | + ), |
|
90 | + 'CNT_cur_plural' => new EE_Plain_Text_Field( |
|
91 | + 'CNT_cur_plural', |
|
92 | + esc_html__('Currency Name Plural', 'event_espresso'), |
|
93 | + false |
|
94 | + ), |
|
95 | + 'CNT_cur_sign' => new EE_Plain_Text_Field( |
|
96 | + 'CNT_cur_sign', |
|
97 | + esc_html__('Currency Sign', 'event_espresso'), |
|
98 | + false |
|
99 | + ), |
|
100 | + 'CNT_cur_sign_b4' => new EE_Boolean_Field( |
|
101 | + 'CNT_cur_sign_b4', |
|
102 | + esc_html__('Currency Sign Before Number', 'event_espresso'), |
|
103 | + false, |
|
104 | + true |
|
105 | + ), |
|
106 | + 'CNT_cur_dec_plc' => new EE_Integer_Field( |
|
107 | + 'CNT_cur_dec_plc', |
|
108 | + esc_html__('Currency Decimal Places', 'event_espresso'), |
|
109 | + false, |
|
110 | + 2 |
|
111 | + ), |
|
112 | + 'CNT_cur_dec_mrk' => new EE_Plain_Text_Field( |
|
113 | + 'CNT_cur_dec_mrk', |
|
114 | + esc_html__('Currency Decimal Mark', 'event_espresso'), |
|
115 | + false, |
|
116 | + '.' |
|
117 | + ), |
|
118 | + 'CNT_cur_thsnds' => new EE_Plain_Text_Field( |
|
119 | + 'CNT_cur_thsnds', |
|
120 | + esc_html__('Currency Thousands Separator', 'event_espresso'), |
|
121 | + false, |
|
122 | + ',' |
|
123 | + ), |
|
124 | + 'CNT_tel_code' => new EE_Plain_Text_Field( |
|
125 | + 'CNT_tel_code', |
|
126 | + esc_html__('Country Telephone Code', 'event_espresso'), |
|
127 | + false, |
|
128 | + '' |
|
129 | + ), |
|
130 | + 'CNT_is_EU' => new EE_Boolean_Field( |
|
131 | + 'CNT_is_EU', |
|
132 | + esc_html__('Country is Member of EU', 'event_espresso'), |
|
133 | + false, |
|
134 | + false |
|
135 | + ), |
|
136 | + ], |
|
137 | + ]; |
|
138 | + $this->_model_relations = [ |
|
139 | + 'Attendee' => new EE_Has_Many_Relation(), |
|
140 | + 'State' => new EE_Has_Many_Relation(), |
|
141 | + 'Venue' => new EE_Has_Many_Relation(), |
|
142 | + ]; |
|
143 | + // only anyone to view, but only those with the default role can do anything |
|
144 | + $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
145 | + |
|
146 | + parent::__construct($timezone); |
|
147 | + } |
|
148 | + |
|
149 | + |
|
150 | + /** |
|
151 | + * @return EE_Country[] |
|
152 | + * @throws EE_Error |
|
153 | + */ |
|
154 | + public function get_all_countries(): array |
|
155 | + { |
|
156 | + if (! self::$_all_countries) { |
|
157 | + self::$_all_countries = $this->get_all(['order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
|
158 | + } |
|
159 | + return self::$_all_countries; |
|
160 | + } |
|
161 | + |
|
162 | + |
|
163 | + /** |
|
164 | + * Gets and caches the list of active countries. If you know the list of active countries |
|
165 | + * has changed during this request, first use EEM_Country::reset() to flush the cache |
|
166 | + * |
|
167 | + * @return EE_Country[] |
|
168 | + * @throws EE_Error |
|
169 | + */ |
|
170 | + public function get_all_active_countries(): array |
|
171 | + { |
|
172 | + if (! self::$_active_countries) { |
|
173 | + self::$_active_countries = |
|
174 | + $this->get_all([['CNT_active' => true], 'order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
|
175 | + } |
|
176 | + return self::$_active_countries; |
|
177 | + } |
|
178 | + |
|
179 | + |
|
180 | + /** |
|
181 | + * Gets the country's name by its ISO |
|
182 | + * |
|
183 | + * @param string $country_ISO |
|
184 | + * @return string |
|
185 | + * @throws EE_Error |
|
186 | + */ |
|
187 | + public function get_country_name_by_ISO(string $country_ISO): string |
|
188 | + { |
|
189 | + $countries = $this->get_all_countries(); |
|
190 | + if (isset($countries[ $country_ISO ]) && $countries[ $country_ISO ] instanceof EE_Country) { |
|
191 | + return $countries[ $country_ISO ]->name(); |
|
192 | + } |
|
193 | + $names = $this->get_col([['CNT_ISO' => $country_ISO], 'limit' => 1], 'CNT_name'); |
|
194 | + if (is_array($names) && ! empty($names)) { |
|
195 | + return reset($names); |
|
196 | + } else { |
|
197 | + return ''; |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * Gets the country's name by its name |
|
204 | + * |
|
205 | + * @param string $country_name |
|
206 | + * @return EE_Country|null |
|
207 | + * @throws EE_Error |
|
208 | + */ |
|
209 | + public function getCountryByName(string $country_name): ?EE_Country |
|
210 | + { |
|
211 | + $countries = $this->get_all_countries(); |
|
212 | + foreach ($countries as $country) { |
|
213 | + if ($country instanceof EE_Country && $country->name() === $country_name) { |
|
214 | + return $country; |
|
215 | + } |
|
216 | + } |
|
217 | + return $this->get_one([['CNT_name' => $country_name]]); |
|
218 | + } |
|
219 | 219 | } |
@@ -46,7 +46,7 @@ discard block |
||
46 | 46 | 'Country' => new EE_Primary_Table('esp_country', 'CNT_ISO'), |
47 | 47 | ]; |
48 | 48 | |
49 | - $this->_fields = [ |
|
49 | + $this->_fields = [ |
|
50 | 50 | 'Country' => [ |
51 | 51 | 'CNT_active' => new EE_Boolean_Field( |
52 | 52 | 'CNT_active', |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | 'Venue' => new EE_Has_Many_Relation(), |
142 | 142 | ]; |
143 | 143 | // only anyone to view, but only those with the default role can do anything |
144 | - $this->_cap_restriction_generators[ EEM_Base::caps_read ] = new EE_Restriction_Generator_Public(); |
|
144 | + $this->_cap_restriction_generators[EEM_Base::caps_read] = new EE_Restriction_Generator_Public(); |
|
145 | 145 | |
146 | 146 | parent::__construct($timezone); |
147 | 147 | } |
@@ -153,7 +153,7 @@ discard block |
||
153 | 153 | */ |
154 | 154 | public function get_all_countries(): array |
155 | 155 | { |
156 | - if (! self::$_all_countries) { |
|
156 | + if ( ! self::$_all_countries) { |
|
157 | 157 | self::$_all_countries = $this->get_all(['order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
158 | 158 | } |
159 | 159 | return self::$_all_countries; |
@@ -169,7 +169,7 @@ discard block |
||
169 | 169 | */ |
170 | 170 | public function get_all_active_countries(): array |
171 | 171 | { |
172 | - if (! self::$_active_countries) { |
|
172 | + if ( ! self::$_active_countries) { |
|
173 | 173 | self::$_active_countries = |
174 | 174 | $this->get_all([['CNT_active' => true], 'order_by' => ['CNT_name' => 'ASC'], 'limit' => [0, 99999]]); |
175 | 175 | } |
@@ -187,8 +187,8 @@ discard block |
||
187 | 187 | public function get_country_name_by_ISO(string $country_ISO): string |
188 | 188 | { |
189 | 189 | $countries = $this->get_all_countries(); |
190 | - if (isset($countries[ $country_ISO ]) && $countries[ $country_ISO ] instanceof EE_Country) { |
|
191 | - return $countries[ $country_ISO ]->name(); |
|
190 | + if (isset($countries[$country_ISO]) && $countries[$country_ISO] instanceof EE_Country) { |
|
191 | + return $countries[$country_ISO]->name(); |
|
192 | 192 | } |
193 | 193 | $names = $this->get_col([['CNT_ISO' => $country_ISO], 'limit' => 1], 'CNT_name'); |
194 | 194 | if (is_array($names) && ! empty($names)) { |