1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: use Xoops\Core\Service\AbstractContract;
13: use Xoops\Core\Service\Contract\CountryflagInterface;
14: use Xoops\Core\Service\Response;
15: use Xoops\Html\Img;
16:
17: /**
18: * Qrcode provider for service manager
19: *
20: * @category ServiceProvider
21: * @package CountryFlagProvider
22: * @author Richard Griffith <richard@geekwright.com>
23: * @copyright 2013-2014 XOOPS Project (http://xoops.org)
24: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
25: * @link http://xoops.org
26: * @since 2.6.0
27: */
28: class CountryFlagProvider extends AbstractContract implements CountryflagInterface
29: {
30: /** @var string $renderScript */
31: protected $flagSource = 'media/xoops/images/flags/';
32:
33: /**
34: * getName - get a short name for this service provider. This should be unique within the
35: * scope of the named service, so using module dirname is suggested.
36: *
37: * @return string - a unique name for the service provider
38: */
39: public function getName()
40: {
41: return 'system';
42: }
43:
44: /**
45: * getDescription - get human readable description of the service provider
46: *
47: * @return string
48: */
49: public function getDescription()
50: {
51: return 'Built in CountryFlag provider';
52: }
53:
54:
55: /**
56: * getFlagUrl
57: *
58: * @param string $countryCode ISO 3166-1 alpha-2 code to select flag
59: * @param string $size 'small', 'medium' or 'large'
60: *
61: * @return string URL to obtain Flag image for country code
62: */
63: private function getFlagUrl($countryCode, $size)
64: {
65: $countryCode = $this->getCountryCodeOverride($countryCode);
66: $size = strtolower(substr($size, 0, 1));
67: $sizeDir = '64';
68: switch ($size) {
69: case 's':
70: $sizeDir = '16';
71: break;
72: case 'm':
73: $sizeDir = '32';
74: break;
75: }
76:
77: $xoops = \Xoops::getInstance();
78: $flagDir = $this->flagSource . $sizeDir . '/';
79: $flagFile = $flagDir . $countryCode . '.png';
80:
81: $file = $xoops->path($flagFile);
82: // switch to unknown if file is not readable
83: if (!is_readable($file)) {
84: $flagFile = $flagDir . '_unknown.png';
85: }
86: $url = $xoops->url($flagFile);
87: return $url;
88: }
89:
90: /**
91: * Some CLDR (Unicode Common Locale Data Repository) territory codes are not officially
92: * assigned codes, exceptional reservations*, or are a disjoined territory of a county.
93: * These will need to be mappped to a (hopefully) suitable flag.
94: *
95: * @var string[]
96: */
97: private $overrideMap = array (
98: 'AC' => 'SH', // *Ascension Island part of Saint Helena, Ascension and Tristan da Cunha
99: 'BQ' => 'NL', // Caribbean Netherlands
100: 'BV' => 'NO', // Bouvet Island, dependency of Norway
101: 'CP' => 'FR', // *Clipperton Island, overseas possession of France
102: 'DG' => 'GB', // *Diego Garcia, British Indian Ocean Territory disputed sovereignty
103: 'EA' => 'ES', // *Ceuta & Melilla, Spanish cities on the north coast of Africa
104: 'GF' => 'FR', // French Guiana, overseas region of France
105: 'GP' => 'FR', // Guadeloupe, overseas region of France
106: 'HM' => 'AU', // Heard & McDonald Islands, Australian external territory
107: 'IO' => 'GB', // British Indian Ocean Territory
108: 'PM' => 'FR', // St. Pierre & Miquelon, territorial overseas collectivity of France
109: 'RE' => 'FR', // RĂ©union, overseas region of France
110: 'SJ' => 'NO', // Svalbard & Jan Mayen, integrated parts of Norway not allocated to counties
111: 'SX' => 'NL', // Sint Maarten, constituent country of the Kingdom of the Netherlands
112: 'TA' => 'SH', // *Tristan da Cunha part of Saint Helena, Ascension and Tristan da Cunha
113: 'UM' => 'US', // U.S. Outlying Islands
114: 'XK' => '_kosovo', // (User-assigned range) temporary assigned code
115: );
116:
117: /**
118: * getCountryOverride
119: *
120: * @param string $countryCode ISO 3166-1 alpha-2 code
121: *
122: * @return string possibly overridden country code
123: */
124: private function getCountryCodeOverride($countryCode)
125: {
126: $countryCode = (isset($this->overrideMap[$countryCode]))
127: ? $this->overrideMap[$countryCode]
128: : $countryCode;
129: return $countryCode;
130: }
131:
132: /**
133: * getImgTag - get a full HTML tag or string to display a flag based on county code
134: *
135: * @param Response $response \Xoops\Core\Service\Response object
136: * @param string $countryCode ISO 3166-1 alpha-2 code to select flag
137: * @param array $attributes array of attribute name => value pairs for img tag
138: * @param string $size 'small', 'medium' or 'large'
139: *
140: * @return void - response->value set to image tag
141: */
142: public function getImgTag(
143: Response $response,
144: $countryCode,
145: $attributes = array(),
146: $size = 'large'
147: ) {
148: $url = $this->getFlagUrl($countryCode, $size);
149: if (!is_array($attributes)) {
150: $attributes = array();
151: }
152:
153: $imgTag = new Img(array('src' => $url, 'alt' => $countryCode));
154: $imgTag->setMerge($attributes);
155: $response->setValue($imgTag->render());
156: }
157: }
158: