phpvms/app/Support/Timezonelist.php

173 lines
5.8 KiB
PHP
Raw Normal View History

2018-08-24 23:16:23 +08:00
<?php
/**
* MIT License
* Copyright (c) 2017 Anh Đỗ
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
2018-08-24 23:16:23 +08:00
namespace App\Support;
use DateTimeZone;
use Illuminate\Support\Facades\Log;
2018-08-24 23:16:23 +08:00
/**
* This library is from:
* https://github.com/JackieDo/Timezone-List
*
* With some changes to suite the formatting, etc that we need.
* Also changed it to provide all static methods instead of through a facade
*/
class Timezonelist
2018-08-24 23:16:23 +08:00
{
/**
* Whitespace seperate
*/
const WHITESPACE_SEP = '&nbsp;&nbsp;&nbsp;&nbsp;';
/**
* Popular timezones
*
* @var array
*/
protected static $popularTimezones = [
'GMT' => 'GMT timezone',
'UTC' => 'UTC timezone',
];
/**
* All continents of the world
*
* @var array
*/
protected static $continents = [
'Africa' => DateTimeZone::AFRICA,
'America' => DateTimeZone::AMERICA,
'Antarctica' => DateTimeZone::ANTARCTICA,
'Arctic' => DateTimeZone::ARCTIC,
'Asia' => DateTimeZone::ASIA,
'Atlantic' => DateTimeZone::ATLANTIC,
'Australia' => DateTimeZone::AUSTRALIA,
'Europe' => DateTimeZone::EUROPE,
'Indian' => DateTimeZone::INDIAN,
'Pacific' => DateTimeZone::PACIFIC,
];
2018-08-24 23:16:23 +08:00
/**
* Format to display timezones
*
2018-08-27 00:40:04 +08:00
* @param string $timezone
* @param string $continent
* @param bool $htmlencode
*
2018-08-27 02:51:47 +08:00
* @return string
2018-08-24 23:16:23 +08:00
*/
public static function formatTimezone($timezone, $continent, $htmlencode = true)
2018-08-24 23:16:23 +08:00
{
try {
$time = new \DateTimeImmutable(null, new DateTimeZone($timezone));
} catch (\Exception $e) {
Log::error($e->getMessage());
return '';
}
2018-08-24 23:16:23 +08:00
$offset = $time->format('P');
if ($htmlencode) {
2018-08-27 02:51:47 +08:00
$offset = str_replace(['-', '+'], [' &minus; ', ' &plus; '], $offset);
2018-08-24 23:16:23 +08:00
}
2018-08-27 02:50:08 +08:00
$timezone = substr($timezone, \strlen($continent) + 1);
2018-08-27 02:51:47 +08:00
$timezone = str_replace(['St_', '_'], ['St. ', ' '], $timezone);
2018-08-27 02:50:08 +08:00
return '(GMT/UTC'.$offset.')'.self::WHITESPACE_SEP.$timezone;
2018-08-24 23:16:23 +08:00
}
/**
* Create a GMT timezone select element for form
*
* @param string $name
* @param string $selected
2018-08-27 00:40:04 +08:00
* @param mixed $attr
* @param bool $htmlencode
*
* @throws \Exception
*
2018-08-24 23:16:23 +08:00
* @return string
*/
public static function create($name, $selected = '', $attr = '', $htmlencode = true)
2018-08-24 23:16:23 +08:00
{
// Attributes for select element
$attrSet = '';
2018-08-24 23:16:23 +08:00
if (!empty($attr)) {
2018-08-27 02:50:08 +08:00
if (\is_array($attr)) {
2018-08-24 23:16:23 +08:00
foreach ($attr as $attr_name => $attr_value) {
2018-08-27 00:40:04 +08:00
$attrSet .= ' '.$attr_name.'="'.$attr_value.'"';
2018-08-24 23:16:23 +08:00
}
} else {
2018-08-27 00:40:04 +08:00
$attrSet = ' '.$attr;
2018-08-24 23:16:23 +08:00
}
}
// start select element
2018-08-27 00:40:04 +08:00
$listbox = '<select name="'.$name.'"'.$attrSet.'>';
2018-08-24 23:16:23 +08:00
// Add popular timezones
$listbox .= '<optgroup label="General">';
foreach (self::$popularTimezones as $key => $value) {
2018-08-27 02:50:08 +08:00
$selected_attr = ($selected === $key) ? ' selected="selected"' : '';
$listbox .= '<option value="'.$key.'" '.$selected_attr.'>'.$value.'</option>';
2018-08-24 23:16:23 +08:00
}
$listbox .= '</optgroup>';
// Add all timezone of continents
foreach (self::$continents as $continent => $mask) {
2018-08-24 23:16:23 +08:00
$timezones = DateTimeZone::listIdentifiers($mask);
// start optgroup tag
2018-08-27 00:40:04 +08:00
$listbox .= '<optgroup label="'.$continent.'">';
2018-08-24 23:16:23 +08:00
// create option tags
foreach ($timezones as $timezone) {
2018-08-27 02:50:08 +08:00
$selected_attr = ($selected === $timezone) ? ' selected="selected"' : '';
2018-08-27 00:40:04 +08:00
$listbox .= '<option value="'.$timezone.'"'.$selected_attr.'>';
$listbox .= static::formatTimezone($timezone, $continent, $htmlencode);
2018-08-24 23:16:23 +08:00
$listbox .= '</option>';
}
// end optgroup tag
$listbox .= '</optgroup>';
}
// end select element
$listbox .= '</select>';
return $listbox;
}
/**
* Create a timezone array
*
* @param bool $htmlencode
2018-08-27 00:40:04 +08:00
*
2018-08-24 23:16:23 +08:00
* @return mixed
*/
public static function toArray($htmlencode = false)
2018-08-24 23:16:23 +08:00
{
$list = [];
// Add popular timezones to list
foreach (self::$popularTimezones as $key => $value) {
2018-08-24 23:16:23 +08:00
$list['General'][$key] = $value;
}
// Add all timezone of continents to list
foreach (self::$continents as $continent => $mask) {
2018-08-24 23:16:23 +08:00
$timezones = DateTimeZone::listIdentifiers($mask);
foreach ($timezones as $timezone) {
$list[$continent][$timezone] = self::formatTimezone($timezone, $continent, $htmlencode);
2018-08-24 23:16:23 +08:00
}
}
return $list;
}
2018-08-27 00:40:04 +08:00
}