Skip to content

Commit 0c78028

Browse files
committed
new: [ui] Added country flag in some places
- Currently in: - /organisations/[index/view] - /users/[index/view]
1 parent aae584f commit 0c78028

File tree

268 files changed

+945
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

268 files changed

+945
-2
lines changed

src/View/AppView.php

+1
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,6 @@ public function initialize(): void
4545
$this->loadHelper('Paginator', ['templates' => 'cerebrate-pagination-templates']);
4646
$this->loadHelper('Tags.Tag');
4747
$this->loadHelper('ACL');
48+
$this->loadHelper('Flag');
4849
}
4950
}

src/View/Helper/FlagHelper.php

+253
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,253 @@
1+
<?php
2+
namespace App\View\Helper;
3+
4+
use Cake\Utility\Inflector;
5+
use Cake\View\Helper;
6+
7+
// This helper helps determining the brightness of a colour (initially only used for the tagging) in order to decide
8+
// what text colour to use against the background (black or white)
9+
class FlagHelper extends Helper {
10+
11+
public $helpers = [
12+
'Bootstrap',
13+
];
14+
15+
/**
16+
* @param string $countryCode ISO 3166-1 alpha-2 two-letter country code
17+
* @param string $countryName Full country name for title
18+
* @return string
19+
*/
20+
public function countryFlag($countryCode, $countryName = null, $small = false)
21+
{
22+
if (strlen($countryCode) !== 2) {
23+
return '';
24+
}
25+
26+
$output = [];
27+
foreach (str_split(strtolower($countryCode)) as $letter) {
28+
$letterCode = ord($letter);
29+
if ($letterCode < 97 || $letterCode > 122) {
30+
return ''; // invalid letter
31+
}
32+
$output[] = "1f1" . dechex(0xe6 + ($letterCode - 97));
33+
}
34+
35+
$countryNamePretty = Inflector::humanize($countryName ? h($countryName) : $countryCode);
36+
$baseurl = $this->getView()->get('baseurl');
37+
$title = __('Flag of %s', $countryNamePretty);
38+
$html = '<img src="' . $baseurl . '/img/flags/' . implode('-', $output) . '.svg" title="' . $title .'" alt="' . $title . '" aria-label="' . $title . '" style="height: 18px" />';
39+
if (!$small) {
40+
$html = $this->Bootstrap->node('span', [
41+
'class' => 'd-flex align-items-center'
42+
], $html . '&nbsp;' . $countryNamePretty);
43+
}
44+
return $html;
45+
}
46+
47+
public function flag($countryName, $small = false) {
48+
$countryNameLow = strtolower($countryName);
49+
if (!empty(self::countries[$countryNameLow])) {
50+
$countryCode = self::countries[$countryNameLow];
51+
return $this->countryFlag($countryCode, $countryName, $small);
52+
}
53+
return '';
54+
}
55+
56+
private const countries = [
57+
"afghanistan" => "AF",
58+
"albania" => "AL",
59+
"algeria" => "DZ",
60+
"andorra" => "AD",
61+
"angola" => "AO",
62+
"antigua and barbuda" => "AG",
63+
"argentina" => "AR",
64+
"armenia" => "AM",
65+
"australia" => "AU",
66+
"austria" => "AT",
67+
"azerbaijan" => "AZ",
68+
"bahamas" => "BS",
69+
"bahrain" => "BH",
70+
"bangladesh" => "BD",
71+
"barbados" => "BB",
72+
"belarus" => "BY",
73+
"belgium" => "BE",
74+
"belize" => "BZ",
75+
"benin" => "BJ",
76+
"bhutan" => "BT",
77+
"bolivia" => "BO",
78+
"bosnia and herzegovina" => "BA",
79+
"botswana" => "BW",
80+
"brazil" => "BR",
81+
"brunei" => "BN",
82+
"bulgaria" => "BG",
83+
"burkina faso" => "BF",
84+
"burundi" => "BI",
85+
"cabo verde" => "CV",
86+
"cambodia" => "KH",
87+
"cameroon" => "CM",
88+
"canada" => "CA",
89+
"central african republic" => "CF",
90+
"chad" => "TD",
91+
"chile" => "CL",
92+
"china" => "CN",
93+
"colombia" => "CO",
94+
"comoros" => "KM",
95+
"congo (brazzaville)" => "CG",
96+
"congo (kinshasa)" => "CD",
97+
"costa rica" => "CR",
98+
"croatia" => "HR",
99+
"cuba" => "CU",
100+
"cyprus" => "CY",
101+
"czechia" => "CZ",
102+
"denmark" => "DK",
103+
"djibouti" => "DJ",
104+
"dominica" => "DM",
105+
"dominican republic" => "DO",
106+
"ecuador" => "EC",
107+
"egypt" => "EG",
108+
"el salvador" => "SV",
109+
"equatorial guinea" => "GQ",
110+
"eritrea" => "ER",
111+
"estonia" => "EE",
112+
"eswatini" => "SZ",
113+
"ethiopia" => "ET",
114+
"fiji" => "FJ",
115+
"finland" => "FI",
116+
"france" => "FR",
117+
"gabon" => "GA",
118+
"gambia" => "GM",
119+
"georgia" => "GE",
120+
"germany" => "DE",
121+
"ghana" => "GH",
122+
"greece" => "GR",
123+
"grenada" => "GD",
124+
"guatemala" => "GT",
125+
"guinea" => "GN",
126+
"guinea-bissau" => "GW",
127+
"guyana" => "GY",
128+
"haiti" => "HT",
129+
"honduras" => "HN",
130+
"hungary" => "HU",
131+
"iceland" => "IS",
132+
"india" => "IN",
133+
"indonesia" => "ID",
134+
"iran" => "IR",
135+
"iraq" => "IQ",
136+
"ireland" => "IE",
137+
"israel" => "IL",
138+
"italy" => "IT",
139+
"jamaica" => "JM",
140+
"japan" => "JP",
141+
"jordan" => "JO",
142+
"kazakhstan" => "KZ",
143+
"kenya" => "KE",
144+
"kiribati" => "KI",
145+
"korea (north)" => "KP",
146+
"korea (south)" => "KR",
147+
"kuwait" => "KW",
148+
"kyrgyzstan" => "KG",
149+
"laos" => "LA",
150+
"latvia" => "LV",
151+
"lebanon" => "LB",
152+
"lesotho" => "LS",
153+
"liberia" => "LR",
154+
"libya" => "LY",
155+
"liechtenstein" => "LI",
156+
"lithuania" => "LT",
157+
"luxembourg" => "LU",
158+
"madagascar" => "MG",
159+
"malawi" => "MW",
160+
"malaysia" => "MY",
161+
"maldives" => "MV",
162+
"mali" => "ML",
163+
"malta" => "MT",
164+
"marshall islands" => "MH",
165+
"mauritania" => "MR",
166+
"mauritius" => "MU",
167+
"mexico" => "MX",
168+
"micronesia" => "FM",
169+
"moldova" => "MD",
170+
"monaco" => "MC",
171+
"mongolia" => "MN",
172+
"montenegro" => "ME",
173+
"morocco" => "MA",
174+
"mozambique" => "MZ",
175+
"myanmar" => "MM",
176+
"namibia" => "NA",
177+
"nauru" => "NR",
178+
"nepal" => "NP",
179+
"netherlands" => "NL",
180+
"new zealand" => "NZ",
181+
"nicaragua" => "NI",
182+
"niger" => "NE",
183+
"nigeria" => "NG",
184+
"north macedonia" => "MK",
185+
"norway" => "NO",
186+
"oman" => "OM",
187+
"pakistan" => "PK",
188+
"palau" => "PW",
189+
"panama" => "PA",
190+
"papua new guinea" => "PG",
191+
"paraguay" => "PY",
192+
"peru" => "PE",
193+
"philippines" => "PH",
194+
"poland" => "PL",
195+
"portugal" => "PT",
196+
"qatar" => "QA",
197+
"romania" => "RO",
198+
"russia" => "RU",
199+
"rwanda" => "RW",
200+
"saint kitts and nevis" => "KN",
201+
"saint lucia" => "LC",
202+
"saint vincent and the grenadines" => "VC",
203+
"samoa" => "WS",
204+
"san marino" => "SM",
205+
"sao tome and principe" => "ST",
206+
"saudi arabia" => "SA",
207+
"senegal" => "SN",
208+
"serbia" => "RS",
209+
"seychelles" => "SC",
210+
"sierra leone" => "SL",
211+
"singapore" => "SG",
212+
"slovakia" => "SK",
213+
"slovenia" => "SI",
214+
"solomon islands" => "SB",
215+
"somalia" => "SO",
216+
"south africa" => "ZA",
217+
"south sudan" => "SS",
218+
"spain" => "ES",
219+
"sri lanka" => "LK",
220+
"sudan" => "SD",
221+
"suriname" => "SR",
222+
"sweden" => "SE",
223+
"switzerland" => "CH",
224+
"syria" => "SY",
225+
"taiwan" => "TW",
226+
"tajikistan" => "TJ",
227+
"tanzania" => "TZ",
228+
"thailand" => "TH",
229+
"timor-leste" => "TL",
230+
"togo" => "TG",
231+
"tonga" => "TO",
232+
"trinidad and tobago" => "TT",
233+
"tunisia" => "TN",
234+
"turkey" => "TR",
235+
"turkmenistan" => "TM",
236+
"tuvalu" => "TV",
237+
"uganda" => "UG",
238+
"ukraine" => "UA",
239+
"united arab emirates" => "AE",
240+
"united kingdom" => "GB",
241+
"united states" => "US",
242+
"uruguay" => "UY",
243+
"uzbekistan" => "UZ",
244+
"vanuatu" => "VU",
245+
"vatican city" => "VA",
246+
"venezuela" => "VE",
247+
"vietnam" => "VN",
248+
"yemen" => "YE",
249+
"zambia" => "ZM",
250+
"zimbabwe" => "ZW"
251+
];
252+
253+
}

templates/Organisations/index.php

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
'name' => __('Country'),
7979
'data_path' => 'nationality',
8080
'sort' => 'nationality',
81+
'element' => 'country',
8182
],
8283
[
8384
'name' => __('Sector'),

templates/Organisations/view.php

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
],
2020
[
2121
'key' => __('Country'),
22+
'type' => 'country',
2223
'path' => 'nationality'
2324
],
2425
[

templates/Users/index.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@
124124
[
125125
'name' => __('Country'),
126126
'sort' => 'organisation.nationality',
127-
'data_path' => 'organisation.nationality'
127+
'data_path' => 'organisation.nationality',
128+
'element' => 'country',
128129
],
129130
[
130131
'name' => __('# User Settings'),

templates/Users/view.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@
4646
],
4747
[
4848
'key' => __('Country'),
49-
'path' => 'organisation.nationality'
49+
'path' => 'organisation.nationality',
50+
'type' => 'country',
5051
],
5152
[
5253
'key' => __('Alignments'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
$country = $this->Hash->get($row, $field['data_path']);
3+
$small = !empty($field['flag_small']);
4+
$html = '';
5+
if (!is_null($country)) {
6+
$html .= $this->Flag->flag($country, $small);
7+
}
8+
echo $html;
9+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
echo $this->element('genericElements/IndexTable/Fields/country', ['field' => [
3+
'data_path' => $field['path'],
4+
'flag_small' => $field['flag_small'],
5+
], 'row' => $data]);
6+
?>

webroot/img/flags/1f1e6-1f1e8.svg

+1
Loading

webroot/img/flags/1f1e6-1f1e9.svg

+1
Loading

webroot/img/flags/1f1e6-1f1ea.svg

+1
Loading

webroot/img/flags/1f1e6-1f1eb.svg

+1
Loading

webroot/img/flags/1f1e6-1f1ec.svg

+1
Loading

webroot/img/flags/1f1e6-1f1ee.svg

+1
Loading

0 commit comments

Comments
 (0)