1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package Country flags |
5
|
|
|
* @author Iurii Makukh <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace gplcart\modules\country_flag; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Main class for Country flags module |
14
|
|
|
*/ |
15
|
|
|
class Main |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Implements hook "library.list" |
20
|
|
|
* @param array $libraries |
21
|
|
|
*/ |
22
|
|
|
public function hookLibraryList(array &$libraries) |
23
|
|
|
{ |
24
|
|
|
$libraries['flag-icon-css'] = array( |
25
|
|
|
'name' => 'Country flags', // @text |
26
|
|
|
'description' => 'A collection of all country flags in SVG', // @text |
27
|
|
|
'type' => 'asset', |
28
|
|
|
'module' => 'country_flag', |
29
|
|
|
'url' => 'https://github.com/lipis/flag-icon-css', |
30
|
|
|
'download' => 'https://github.com/lipis/flag-icon-css/archive/2.8.0.zip', |
31
|
|
|
'version' => '2.8.0', |
32
|
|
|
'files' => array( |
33
|
|
|
'css/flag-icon.min.css' |
34
|
|
|
), |
35
|
|
|
); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Implements hook "country.get.after" |
40
|
|
|
* @param string $code |
41
|
|
|
* @param array $result |
42
|
|
|
*/ |
43
|
|
|
public function hookCountryGetAfter($code, &$result) |
44
|
|
|
{ |
45
|
|
|
$this->setCountryFlag($code, $result); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Implements hook "country.list" |
50
|
|
|
* @param array $countries |
51
|
|
|
*/ |
52
|
|
|
public function hookCountryList(array &$countries) |
53
|
|
|
{ |
54
|
|
|
$this->setCountryFlags($countries); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Returns an array of country flag images keyed by country code |
59
|
|
|
*/ |
60
|
|
|
public function getCountryFlags() |
61
|
|
|
{ |
62
|
|
|
$list = &gplcart_static(__METHOD__); |
63
|
|
|
|
64
|
|
|
if (isset($list)) { |
65
|
|
|
return $list; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$list = array(); |
69
|
|
|
|
70
|
|
|
foreach (glob(GC_DIR_VENDOR_ASSET . '/flag-icon-css/flags/*/*.svg') as $file) { |
71
|
|
|
$info = pathinfo($file); |
72
|
|
|
$list[$info['filename']][basename($info['dirname'])] = $file; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $list; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets a country flag image |
80
|
|
|
* @param string $code |
81
|
|
|
* @param array $country |
82
|
|
|
*/ |
83
|
|
|
protected function setCountryFlag($code, array &$country) |
84
|
|
|
{ |
85
|
|
|
$lower_code = strtolower($code); |
86
|
|
|
$images = $this->getCountryFlags(); |
87
|
|
|
|
88
|
|
|
if (isset($images[$lower_code]['1x1']) && !isset($country['image'])) { |
89
|
|
|
$country['image'] = $images[$lower_code]['1x1']; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Sets flags for an array of images |
95
|
|
|
* @param array $countries |
96
|
|
|
*/ |
97
|
|
|
protected function setCountryFlags(array &$countries) |
98
|
|
|
{ |
99
|
|
|
$images = $this->getCountryFlags(); |
100
|
|
|
|
101
|
|
|
foreach ($countries as $code => &$country) { |
102
|
|
|
$code = strtolower($code); |
103
|
|
|
if (isset($images[$code]['1x1']) && !isset($country['image'])) { |
104
|
|
|
$country['image'] = $images[$code]['1x1']; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
} |
110
|
|
|
|