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
|
|
|
use gplcart\core\Module, |
13
|
|
|
gplcart\core\Config; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Main class for Country flags module |
17
|
|
|
*/ |
18
|
|
|
class CountryFlag extends Module |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param Config $config |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Config $config) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($config); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/* --------------------------- Hooks --------------------------- */ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Implements hook "library.list" |
33
|
|
|
* @param array $libraries |
34
|
|
|
*/ |
35
|
|
|
public function hookLibraryList(array &$libraries) |
36
|
|
|
{ |
37
|
|
|
$libraries['flag-icon-css'] = array( |
38
|
|
|
'name' => /* @text */'Country flags', |
39
|
|
|
'description' => /* @text */'A collection of all country flags in SVG', |
40
|
|
|
'type' => 'asset', |
41
|
|
|
'module' => 'country_flag', |
42
|
|
|
'url' => 'https://github.com/lipis/flag-icon-css', |
43
|
|
|
'download' => 'https://github.com/lipis/flag-icon-css/archive/2.8.0.zip', |
44
|
|
|
'version_source' => array( |
45
|
|
|
'file' => 'vendor/flag-icon-css/package.json' |
46
|
|
|
), |
47
|
|
|
'files' => array( |
48
|
|
|
'vendor/flag-icon-css/css/flag-icon.min.css' |
49
|
|
|
), |
50
|
|
|
); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Implements hook "country.get.after" |
55
|
|
|
* @param string $code |
56
|
|
|
* @param array $result |
57
|
|
|
*/ |
58
|
|
|
public function hookCountryGetAfter($code, &$result) |
59
|
|
|
{ |
60
|
|
|
$this->setCountryFlag($code, $result); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Implements hook "country.list" |
65
|
|
|
* @param array $countries |
66
|
|
|
*/ |
67
|
|
|
public function hookCountryList(array &$countries) |
68
|
|
|
{ |
69
|
|
|
$this->setCountryFlags($countries); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Implements hook "module.enable.after" |
74
|
|
|
*/ |
75
|
|
|
public function hookModuleEnableAfter() |
76
|
|
|
{ |
77
|
|
|
$this->getLibrary()->clearCache(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Implements hook "module.disable.after" |
82
|
|
|
*/ |
83
|
|
|
public function hookModuleDisableAfter() |
84
|
|
|
{ |
85
|
|
|
$this->getLibrary()->clearCache(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Implements hook "module.install.after" |
90
|
|
|
*/ |
91
|
|
|
public function hookModuleInstallAfter() |
92
|
|
|
{ |
93
|
|
|
$this->getLibrary()->clearCache(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Implements hook "module.uninstall.after" |
98
|
|
|
*/ |
99
|
|
|
public function hookModuleUninstallAfter() |
100
|
|
|
{ |
101
|
|
|
$this->getLibrary()->clearCache(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/* --------------------------- API --------------------------- */ |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Returns an array of country flag images keyed by country code |
108
|
|
|
*/ |
109
|
|
|
public function getCountryFlags() |
110
|
|
|
{ |
111
|
|
|
$list = &gplcart_static(__METHOD__); |
112
|
|
|
|
113
|
|
|
if (isset($list)) { |
114
|
|
|
return $list; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$list = array(); |
118
|
|
|
foreach (glob(__DIR__ . '/vendor/flag-icon-css/flags/*/*.svg') as $file) { |
119
|
|
|
$info = pathinfo($file); |
120
|
|
|
$list[$info['filename']][basename($info['dirname'])] = $file; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $list; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/* --------------------------- Helpers --------------------------- */ |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Sets a country flag image |
130
|
|
|
* @param string $code |
131
|
|
|
* @param array $country |
132
|
|
|
*/ |
133
|
|
|
protected function setCountryFlag($code, array &$country) |
134
|
|
|
{ |
135
|
|
|
$code = strtolower($code); |
136
|
|
|
$images = $this->getCountryFlags(); |
137
|
|
|
if (isset($images[$code]['1x1']) && !isset($country['image'])) { |
138
|
|
|
$country['image'] = $images[$code]['1x1']; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sets flags for an array of images |
144
|
|
|
* @param array $countries |
145
|
|
|
*/ |
146
|
|
|
protected function setCountryFlags(array &$countries) |
147
|
|
|
{ |
148
|
|
|
$images = $this->getCountryFlags(); |
149
|
|
|
foreach ($countries as $code => &$country) { |
150
|
|
|
$code = strtolower($code); |
151
|
|
|
if (isset($images[$code]['1x1']) && !isset($country['image'])) { |
152
|
|
|
$country['image'] = $images[$code]['1x1']; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
} |
158
|
|
|
|