2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2022-04-21 04:44:34 +12:00
|
|
|
use Appwrite\Utopia\Request;
|
2023-08-24 05:12:43 +12:00
|
|
|
use Appwrite\Utopia\Response;
|
2022-04-24 16:51:28 +12:00
|
|
|
use MaxMind\Db\Reader;
|
2019-05-09 18:54:39 +12:00
|
|
|
use Utopia\App;
|
2020-06-30 17:27:52 +12:00
|
|
|
use Utopia\Config\Config;
|
2022-04-24 16:51:28 +12:00
|
|
|
use Utopia\Database\Document;
|
2022-04-21 04:44:34 +12:00
|
|
|
use Utopia\Locale\Locale;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-29 05:31:21 +12:00
|
|
|
App::get('/v1/locale')
|
2019-05-09 18:54:39 +12:00
|
|
|
->desc('Get User Locale')
|
2020-06-26 06:32:12 +12:00
|
|
|
->groups(['api', 'locale'])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('scope', 'locale.read')
|
2021-04-16 19:22:17 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
2020-01-31 05:18:46 +13:00
|
|
|
->label('sdk.method', 'get')
|
2019-10-08 20:09:35 +13:00
|
|
|
->label('sdk.description', '/docs/references/locale/get-locale.md')
|
2020-11-12 10:02:24 +13:00
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_LOCALE)
|
2023-04-17 14:21:24 +12:00
|
|
|
->label('sdk.offline.model', '/localed')
|
2023-02-10 14:06:33 +13:00
|
|
|
->label('sdk.offline.key', 'current')
|
2020-12-27 04:13:42 +13:00
|
|
|
->inject('request')
|
|
|
|
->inject('response')
|
|
|
|
->inject('locale')
|
2023-04-17 20:53:52 +12:00
|
|
|
->inject('geodb')
|
2022-04-21 04:19:50 +12:00
|
|
|
->action(function (Request $request, Response $response, Locale $locale, Reader $geodb) {
|
2020-06-30 17:27:52 +12:00
|
|
|
$eu = Config::getParam('locale-eu');
|
|
|
|
$currencies = Config::getParam('locale-currencies');
|
2020-06-30 09:43:34 +12:00
|
|
|
$output = [];
|
|
|
|
$ip = $request->getIP();
|
|
|
|
$time = (60 * 60 * 24 * 45); // 45 days cache
|
2020-10-31 08:53:27 +13:00
|
|
|
|
2020-06-30 09:43:34 +12:00
|
|
|
$output['ip'] = $ip;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-30 09:43:34 +12:00
|
|
|
$currency = null;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-10-25 22:27:07 +13:00
|
|
|
$record = $geodb->get($ip);
|
2020-10-25 19:15:36 +13:00
|
|
|
|
2020-10-27 13:11:24 +13:00
|
|
|
if ($record) {
|
2020-10-24 06:36:52 +13:00
|
|
|
$output['countryCode'] = $record['country']['iso_code'];
|
2023-08-24 05:12:43 +12:00
|
|
|
$output['country'] = $locale->getText('countries.'.strtolower($record['country']['iso_code']), $locale->getText('locale.country.unknown'));
|
|
|
|
$output['continent'] = $locale->getText('continents.'.strtolower($record['continent']['code']), $locale->getText('locale.country.unknown'));
|
2020-10-24 06:36:52 +13:00
|
|
|
$output['continentCode'] = $record['continent']['code'];
|
|
|
|
$output['eu'] = (\in_array($record['country']['iso_code'], $eu)) ? true : false;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-30 09:43:34 +12:00
|
|
|
foreach ($currencies as $code => $element) {
|
2020-10-24 06:36:52 +13:00
|
|
|
if (isset($element['locations']) && isset($element['code']) && \in_array($record['country']['iso_code'], $element['locations'])) {
|
2020-06-30 09:43:34 +12:00
|
|
|
$currency = $element['code'];
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 09:43:34 +12:00
|
|
|
$output['currency'] = $currency;
|
2020-10-25 22:27:07 +13:00
|
|
|
} else {
|
2020-06-30 09:43:34 +12:00
|
|
|
$output['countryCode'] = '--';
|
|
|
|
$output['country'] = $locale->getText('locale.country.unknown');
|
|
|
|
$output['continent'] = $locale->getText('locale.country.unknown');
|
|
|
|
$output['continentCode'] = '--';
|
|
|
|
$output['eu'] = false;
|
|
|
|
$output['currency'] = $currency;
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
2020-06-30 09:43:34 +12:00
|
|
|
|
|
|
|
$response
|
2023-08-24 05:12:43 +12:00
|
|
|
->addHeader('Cache-Control', 'public, max-age='.$time)
|
|
|
|
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $time).' GMT') // 45 days cache
|
|
|
|
;
|
2020-10-31 08:53:27 +13:00
|
|
|
$response->dynamic(new Document($output), Response::MODEL_LOCALE);
|
2020-12-27 04:13:42 +13:00
|
|
|
});
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2023-04-17 14:10:17 +12:00
|
|
|
App::get('/v1/locale/codes')
|
2023-04-18 14:11:08 +12:00
|
|
|
->desc('List Locale Codes')
|
2023-04-17 14:10:17 +12:00
|
|
|
->groups(['api', 'locale'])
|
|
|
|
->label('scope', 'locale.read')
|
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
|
|
|
->label('sdk.namespace', 'locale')
|
2023-04-17 14:21:24 +12:00
|
|
|
->label('sdk.method', 'listCodes')
|
|
|
|
->label('sdk.description', '/docs/references/locale/list-locale-codes.md')
|
2023-04-17 14:10:17 +12:00
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
2023-04-18 14:11:08 +12:00
|
|
|
->label('sdk.response.model', Response::MODEL_LOCALE_CODE_LIST)
|
2023-04-17 14:21:24 +12:00
|
|
|
->label('sdk.offline.model', '/locale/localeCode')
|
2023-04-17 14:10:17 +12:00
|
|
|
->label('sdk.offline.key', 'current')
|
|
|
|
->inject('response')
|
|
|
|
->action(function (Response $response) {
|
|
|
|
$codes = Config::getParam('locale-codes');
|
|
|
|
$response->dynamic(new Document([
|
|
|
|
'localeCodes' => $codes,
|
|
|
|
'total' => count($codes),
|
|
|
|
]), Response::MODEL_LOCALE_CODE_LIST);
|
|
|
|
});
|
|
|
|
|
2020-06-29 05:31:21 +12:00
|
|
|
App::get('/v1/locale/countries')
|
2019-05-09 18:54:39 +12:00
|
|
|
->desc('List Countries')
|
2020-06-26 06:32:12 +12:00
|
|
|
->groups(['api', 'locale'])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('scope', 'locale.read')
|
2021-04-16 19:22:17 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
2022-09-14 10:18:02 +12:00
|
|
|
->label('sdk.method', 'listCountries')
|
|
|
|
->label('sdk.description', '/docs/references/locale/list-countries.md')
|
2020-11-12 10:02:24 +13:00
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_COUNTRY_LIST)
|
2023-02-10 14:06:33 +13:00
|
|
|
->label('sdk.offline.model', '/locale/countries')
|
|
|
|
->label('sdk.offline.response.key', 'code')
|
2020-12-27 04:13:42 +13:00
|
|
|
->inject('response')
|
|
|
|
->inject('locale')
|
2022-04-21 04:19:50 +12:00
|
|
|
->action(function (Response $response, Locale $locale) {
|
2021-07-23 08:15:01 +12:00
|
|
|
$list = Config::getParam('locale-countries'); /* @var $list array */
|
2020-10-31 08:53:27 +13:00
|
|
|
$output = [];
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2021-07-23 08:15:01 +12:00
|
|
|
foreach ($list as $value) {
|
2020-10-31 08:53:27 +13:00
|
|
|
$output[] = new Document([
|
2023-08-24 05:12:43 +12:00
|
|
|
'name' => $locale->getText('countries.'.strtolower($value)),
|
2021-07-23 08:15:01 +12:00
|
|
|
'code' => $value,
|
2020-10-31 08:53:27 +13:00
|
|
|
]);
|
|
|
|
}
|
2020-06-30 09:43:34 +12:00
|
|
|
|
2021-07-26 19:05:08 +12:00
|
|
|
usort($output, function ($a, $b) {
|
|
|
|
return strcmp($a->getAttribute('name'), $b->getAttribute('name'));
|
|
|
|
});
|
|
|
|
|
2022-02-27 22:57:09 +13:00
|
|
|
$response->dynamic(new Document(['countries' => $output, 'total' => \count($output)]), Response::MODEL_COUNTRY_LIST);
|
2020-12-27 04:13:42 +13:00
|
|
|
});
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-29 05:31:21 +12:00
|
|
|
App::get('/v1/locale/countries/eu')
|
2019-05-09 18:54:39 +12:00
|
|
|
->desc('List EU Countries')
|
2020-06-26 06:32:12 +12:00
|
|
|
->groups(['api', 'locale'])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('scope', 'locale.read')
|
2021-04-16 19:22:17 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
2022-09-14 10:18:02 +12:00
|
|
|
->label('sdk.method', 'listCountriesEU')
|
|
|
|
->label('sdk.description', '/docs/references/locale/list-countries-eu.md')
|
2020-11-12 10:02:24 +13:00
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
2020-12-27 04:13:42 +13:00
|
|
|
->label('sdk.response.model', Response::MODEL_COUNTRY_LIST)
|
2023-02-10 14:06:33 +13:00
|
|
|
->label('sdk.offline.model', '/locale/countries/eu')
|
|
|
|
->label('sdk.offline.response.key', 'code')
|
2020-12-27 04:13:42 +13:00
|
|
|
->inject('response')
|
|
|
|
->inject('locale')
|
2022-04-21 04:19:50 +12:00
|
|
|
->action(function (Response $response, Locale $locale) {
|
2020-06-30 17:27:52 +12:00
|
|
|
$eu = Config::getParam('locale-eu');
|
2020-10-31 08:53:27 +13:00
|
|
|
$output = [];
|
|
|
|
|
2020-06-30 09:43:34 +12:00
|
|
|
foreach ($eu as $code) {
|
2023-08-24 05:12:43 +12:00
|
|
|
if ($locale->getText('countries.'.strtolower($code), false) !== false) {
|
2020-10-31 08:53:27 +13:00
|
|
|
$output[] = new Document([
|
2023-08-24 05:12:43 +12:00
|
|
|
'name' => $locale->getText('countries.'.strtolower($code)),
|
2020-10-31 08:53:27 +13:00
|
|
|
'code' => $code,
|
|
|
|
]);
|
2020-06-30 09:43:34 +12:00
|
|
|
}
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
2020-06-30 09:43:34 +12:00
|
|
|
|
2021-07-26 19:05:08 +12:00
|
|
|
usort($output, function ($a, $b) {
|
|
|
|
return strcmp($a->getAttribute('name'), $b->getAttribute('name'));
|
|
|
|
});
|
|
|
|
|
2022-02-27 22:57:09 +13:00
|
|
|
$response->dynamic(new Document(['countries' => $output, 'total' => \count($output)]), Response::MODEL_COUNTRY_LIST);
|
2020-12-27 04:13:42 +13:00
|
|
|
});
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-29 05:31:21 +12:00
|
|
|
App::get('/v1/locale/countries/phones')
|
2019-05-09 18:54:39 +12:00
|
|
|
->desc('List Countries Phone Codes')
|
2020-06-26 06:32:12 +12:00
|
|
|
->groups(['api', 'locale'])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('scope', 'locale.read')
|
2021-04-16 19:22:17 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
2019-05-09 18:54:39 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
2022-09-14 10:18:02 +12:00
|
|
|
->label('sdk.method', 'listCountriesPhones')
|
|
|
|
->label('sdk.description', '/docs/references/locale/list-countries-phones.md')
|
2020-11-12 10:02:24 +13:00
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_PHONE_LIST)
|
2023-02-10 14:06:33 +13:00
|
|
|
->label('sdk.offline.model', '/locale/countries/phones')
|
|
|
|
->label('sdk.offline.response.key', 'countryCode')
|
2020-12-27 04:13:42 +13:00
|
|
|
->inject('response')
|
|
|
|
->inject('locale')
|
2022-04-21 04:19:50 +12:00
|
|
|
->action(function (Response $response, Locale $locale) {
|
2020-06-30 17:27:52 +12:00
|
|
|
$list = Config::getParam('locale-phones'); /* @var $list array */
|
2020-10-31 08:53:27 +13:00
|
|
|
$output = [];
|
2021-07-23 08:15:01 +12:00
|
|
|
|
2020-10-31 08:53:27 +13:00
|
|
|
\asort($list);
|
2020-06-30 09:43:34 +12:00
|
|
|
|
|
|
|
foreach ($list as $code => $name) {
|
2023-08-24 05:12:43 +12:00
|
|
|
if ($locale->getText('countries.'.strtolower($code), false) !== false) {
|
2020-10-31 08:53:27 +13:00
|
|
|
$output[] = new Document([
|
2023-08-24 05:12:43 +12:00
|
|
|
'code' => '+'.$list[$code],
|
2020-10-31 08:53:27 +13:00
|
|
|
'countryCode' => $code,
|
2023-08-24 05:12:43 +12:00
|
|
|
'countryName' => $locale->getText('countries.'.strtolower($code)),
|
2020-10-31 08:53:27 +13:00
|
|
|
]);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
2020-06-30 09:43:34 +12:00
|
|
|
}
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2022-02-27 22:57:09 +13:00
|
|
|
$response->dynamic(new Document(['phones' => $output, 'total' => \count($output)]), Response::MODEL_PHONE_LIST);
|
2020-12-27 04:13:42 +13:00
|
|
|
});
|
2019-08-19 22:31:06 +12:00
|
|
|
|
2020-06-29 05:31:21 +12:00
|
|
|
App::get('/v1/locale/continents')
|
2020-04-13 20:18:09 +12:00
|
|
|
->desc('List Continents')
|
2020-06-26 06:32:12 +12:00
|
|
|
->groups(['api', 'locale'])
|
2019-10-21 21:14:32 +13:00
|
|
|
->label('scope', 'locale.read')
|
2021-04-16 19:22:17 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
2019-10-21 21:14:32 +13:00
|
|
|
->label('sdk.namespace', 'locale')
|
2022-09-14 10:18:02 +12:00
|
|
|
->label('sdk.method', 'listContinents')
|
|
|
|
->label('sdk.description', '/docs/references/locale/list-continents.md')
|
2020-11-12 10:02:24 +13:00
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_CONTINENT_LIST)
|
2023-02-10 14:06:33 +13:00
|
|
|
->label('sdk.offline.model', '/locale/continents')
|
|
|
|
->label('sdk.offline.response.key', 'code')
|
2020-12-27 04:13:42 +13:00
|
|
|
->inject('response')
|
|
|
|
->inject('locale')
|
2022-04-21 14:47:51 +12:00
|
|
|
->action(function (Response $response, Locale $locale) {
|
2022-05-09 03:10:56 +12:00
|
|
|
$list = Config::getParam('locale-continents');
|
2019-10-21 21:14:32 +13:00
|
|
|
|
2022-05-09 03:10:56 +12:00
|
|
|
foreach ($list as $value) {
|
2020-10-31 08:53:27 +13:00
|
|
|
$output[] = new Document([
|
2023-08-24 05:12:43 +12:00
|
|
|
'name' => $locale->getText('continents.'.strtolower($value)),
|
2021-07-24 02:35:21 +12:00
|
|
|
'code' => $value,
|
2020-10-31 08:53:27 +13:00
|
|
|
]);
|
|
|
|
}
|
2020-06-30 09:43:34 +12:00
|
|
|
|
2021-07-26 19:05:08 +12:00
|
|
|
usort($output, function ($a, $b) {
|
|
|
|
return strcmp($a->getAttribute('name'), $b->getAttribute('name'));
|
|
|
|
});
|
|
|
|
|
2022-02-27 22:57:09 +13:00
|
|
|
$response->dynamic(new Document(['continents' => $output, 'total' => \count($output)]), Response::MODEL_CONTINENT_LIST);
|
2020-12-27 04:13:42 +13:00
|
|
|
});
|
2019-10-21 21:14:32 +13:00
|
|
|
|
2020-06-29 05:31:21 +12:00
|
|
|
App::get('/v1/locale/currencies')
|
2019-11-10 18:24:51 +13:00
|
|
|
->desc('List Currencies')
|
2020-06-26 06:32:12 +12:00
|
|
|
->groups(['api', 'locale'])
|
2019-08-19 22:31:06 +12:00
|
|
|
->label('scope', 'locale.read')
|
2021-04-16 19:22:17 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
2019-08-19 22:31:06 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
2022-09-14 10:18:02 +12:00
|
|
|
->label('sdk.method', 'listCurrencies')
|
|
|
|
->label('sdk.description', '/docs/references/locale/list-currencies.md')
|
2020-11-12 10:02:24 +13:00
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_CURRENCY_LIST)
|
2023-02-10 14:06:33 +13:00
|
|
|
->label('sdk.offline.model', '/locale/currencies')
|
|
|
|
->label('sdk.offline.response.key', 'code')
|
2020-12-27 04:13:42 +13:00
|
|
|
->inject('response')
|
2022-04-21 04:19:50 +12:00
|
|
|
->action(function (Response $response) {
|
2020-10-31 08:53:27 +13:00
|
|
|
$list = Config::getParam('locale-currencies');
|
|
|
|
|
2022-05-09 03:10:56 +12:00
|
|
|
$list = array_map(fn ($node) => new Document($node), $list);
|
2020-06-30 09:43:34 +12:00
|
|
|
|
2022-02-27 22:57:09 +13:00
|
|
|
$response->dynamic(new Document(['currencies' => $list, 'total' => \count($list)]), Response::MODEL_CURRENCY_LIST);
|
2020-12-27 04:13:42 +13:00
|
|
|
});
|
2020-05-25 00:20:32 +12:00
|
|
|
|
2020-06-29 05:31:21 +12:00
|
|
|
App::get('/v1/locale/languages')
|
2020-05-25 00:20:32 +12:00
|
|
|
->desc('List Languages')
|
2020-06-26 06:32:12 +12:00
|
|
|
->groups(['api', 'locale'])
|
2020-05-25 00:20:32 +12:00
|
|
|
->label('scope', 'locale.read')
|
2021-04-16 19:22:17 +12:00
|
|
|
->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT])
|
2020-05-25 00:20:32 +12:00
|
|
|
->label('sdk.namespace', 'locale')
|
2022-09-14 10:18:02 +12:00
|
|
|
->label('sdk.method', 'listLanguages')
|
|
|
|
->label('sdk.description', '/docs/references/locale/list-languages.md')
|
2020-11-12 10:02:24 +13:00
|
|
|
->label('sdk.response.code', Response::STATUS_CODE_OK)
|
|
|
|
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
|
|
|
|
->label('sdk.response.model', Response::MODEL_LANGUAGE_LIST)
|
2023-02-10 14:06:33 +13:00
|
|
|
->label('sdk.offline.model', '/locale/languages')
|
|
|
|
->label('sdk.offline.response.key', 'code')
|
2020-12-27 04:13:42 +13:00
|
|
|
->inject('response')
|
2022-04-21 04:19:50 +12:00
|
|
|
->action(function (Response $response) {
|
2020-10-31 08:53:27 +13:00
|
|
|
$list = Config::getParam('locale-languages');
|
|
|
|
|
2021-12-28 01:45:23 +13:00
|
|
|
$list = array_map(fn ($node) => new Document($node), $list);
|
2020-06-30 09:43:34 +12:00
|
|
|
|
2022-02-27 22:57:09 +13:00
|
|
|
$response->dynamic(new Document(['languages' => $list, 'total' => \count($list)]), Response::MODEL_LANGUAGE_LIST);
|
2022-05-09 03:10:56 +12:00
|
|
|
});
|