2017-12-10 11:56:26 +08:00
|
|
|
<?php
|
|
|
|
|
2019-05-12 00:37:06 +08:00
|
|
|
use App\Exceptions\SettingNotFound;
|
2019-08-28 03:08:42 +08:00
|
|
|
use Carbon\Carbon;
|
2019-05-12 00:37:06 +08:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
|
|
|
|
2019-12-12 04:12:31 +08:00
|
|
|
/*
|
|
|
|
* array_key_first only exists in PHP 7.3+
|
|
|
|
*/
|
|
|
|
if (!function_exists('array_key_first')) {
|
|
|
|
function array_key_first(array $arr)
|
|
|
|
{
|
|
|
|
foreach ($arr as $key => $unused) {
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 10:21:35 +08:00
|
|
|
if (!function_exists('in_mask')) {
|
|
|
|
/**
|
|
|
|
* Return true/false if a value exists in a mask
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 10:21:35 +08:00
|
|
|
* @param $mask
|
|
|
|
* @param $value
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-23 10:21:35 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function in_mask($mask, $value)
|
|
|
|
{
|
|
|
|
return ($mask & $value) === $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!function_exists('get_truth_state')) {
|
2018-01-22 02:43:32 +08:00
|
|
|
/**
|
|
|
|
* Check if the passed state matches any of the states that
|
|
|
|
* we regard as being true or false
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-22 02:43:32 +08:00
|
|
|
* @param $state
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-22 02:43:32 +08:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
function get_truth_state($state)
|
|
|
|
{
|
|
|
|
$enabledStates = [
|
2018-01-22 02:52:55 +08:00
|
|
|
'yes',
|
|
|
|
'y',
|
2018-01-22 02:43:32 +08:00
|
|
|
'on',
|
|
|
|
'true',
|
|
|
|
'1',
|
|
|
|
true,
|
|
|
|
];
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (is_string($state)) {
|
2018-01-22 02:52:55 +08:00
|
|
|
$state = strtolower($state);
|
|
|
|
}
|
|
|
|
|
2019-10-30 01:07:53 +08:00
|
|
|
return in_array($state, $enabledStates, false);
|
2018-01-22 02:43:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!function_exists('list_to_assoc')) {
|
2018-01-22 02:36:03 +08:00
|
|
|
/**
|
|
|
|
* Converts a straight list into an assoc array with
|
|
|
|
* key and value being the same. Mainly for a select box
|
|
|
|
*
|
|
|
|
* e.g.:
|
|
|
|
* [ 0 => 'item1', 1 => 'item2']
|
|
|
|
* to:
|
|
|
|
* ['item1' => 'item1', 'item2' => 'item2']
|
|
|
|
*
|
|
|
|
* @param array $list
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-01-22 02:59:51 +08:00
|
|
|
* @return array
|
2018-01-22 02:36:03 +08:00
|
|
|
*/
|
|
|
|
function list_to_assoc(array $list)
|
|
|
|
{
|
2018-01-22 02:59:51 +08:00
|
|
|
$ret = [];
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($list as $item) {
|
|
|
|
if (substr_count($item, '=')) {
|
2018-02-27 10:23:48 +08:00
|
|
|
[$item, $title] = explode('=', $item);
|
|
|
|
} else {
|
|
|
|
$title = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
$ret[$item] = $title;
|
2018-01-22 02:59:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
2018-01-22 02:36:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!function_exists('list_to_editable')) {
|
2018-03-06 11:24:49 +08:00
|
|
|
/**
|
|
|
|
* Convert a list (select box) into an editable list
|
|
|
|
* https://vitalets.github.io/x-editable/docs.html#select
|
|
|
|
* Takes a list of:
|
|
|
|
* [value => text, valueN => textN, ...]
|
|
|
|
* Return:
|
|
|
|
* [{value: 1, text: "text1"}, {value: 2, text: "text2"}, ...]
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 11:24:49 +08:00
|
|
|
* @param array $list
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-06 11:24:49 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function list_to_editable(array $list)
|
|
|
|
{
|
|
|
|
$editable = [];
|
2018-03-20 09:50:40 +08:00
|
|
|
foreach ($list as $value => $key) {
|
2018-03-06 11:24:49 +08:00
|
|
|
$editable[] = [
|
|
|
|
'text' => $key,
|
|
|
|
'value' => $value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $editable;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-12 11:21:33 +08:00
|
|
|
if (!function_exists('skin_view')) {
|
|
|
|
/**
|
|
|
|
* Render a skin
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-20 09:50:40 +08:00
|
|
|
* @param $template
|
2018-02-04 08:18:09 +08:00
|
|
|
* @param array $vars
|
|
|
|
* @param array $merge_data
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2019-05-12 00:37:06 +08:00
|
|
|
* @return Factory|\Illuminate\View\View
|
2018-01-12 11:21:33 +08:00
|
|
|
*/
|
2018-08-27 02:50:08 +08:00
|
|
|
function skin_view($template, array $vars = [], array $merge_data = [])
|
2018-01-12 11:21:33 +08:00
|
|
|
{
|
2018-08-27 00:40:04 +08:00
|
|
|
// Add the current skin name so we don't need to hardcode it in the templates
|
|
|
|
// Makes it a bit easier to create a new skin by modifying an existing one
|
2018-03-20 09:50:40 +08:00
|
|
|
if (View::exists($template)) {
|
2018-03-11 23:55:20 +08:00
|
|
|
return view($template, $vars, $merge_data);
|
|
|
|
}
|
2018-02-04 08:18:09 +08:00
|
|
|
|
2019-11-19 23:06:07 +08:00
|
|
|
$tpl = 'layouts/'.setting('general.theme', 'default').'/'.$template;
|
2018-03-20 09:50:40 +08:00
|
|
|
|
2018-01-12 11:21:33 +08:00
|
|
|
return view($tpl, $vars, $merge_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
/*
|
2017-12-10 11:56:26 +08:00
|
|
|
* Shortcut for retrieving a setting value
|
|
|
|
*/
|
2018-01-01 02:55:29 +08:00
|
|
|
if (!function_exists('setting')) {
|
2019-11-19 23:06:07 +08:00
|
|
|
/**
|
|
|
|
* Read a setting from the settings table
|
|
|
|
*
|
|
|
|
* @param $key
|
|
|
|
* @param mixed $default
|
|
|
|
*
|
2020-01-30 23:54:51 +08:00
|
|
|
* @throws \Exception
|
|
|
|
*
|
2019-11-19 23:06:07 +08:00
|
|
|
* @return mixed|null
|
|
|
|
*/
|
2018-01-03 03:17:22 +08:00
|
|
|
function setting($key, $default = null)
|
2017-12-10 11:56:26 +08:00
|
|
|
{
|
2018-01-20 01:46:30 +08:00
|
|
|
$settingRepo = app('setting');
|
2018-08-27 00:40:04 +08:00
|
|
|
|
2018-01-20 01:46:30 +08:00
|
|
|
try {
|
|
|
|
$value = $settingRepo->retrieve($key);
|
2019-05-12 00:37:06 +08:00
|
|
|
} catch (SettingNotFound $e) {
|
2018-01-20 01:46:30 +08:00
|
|
|
return $default;
|
2020-01-30 23:54:51 +08:00
|
|
|
} catch (Exception $e) {
|
|
|
|
return $default;
|
2018-01-20 01:46:30 +08:00
|
|
|
}
|
2017-12-13 07:09:26 +08:00
|
|
|
|
2018-01-20 01:46:30 +08:00
|
|
|
return $value;
|
2017-12-10 11:56:26 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-18 06:58:53 +08:00
|
|
|
|
2019-10-30 01:07:53 +08:00
|
|
|
/*
|
|
|
|
* Shortcut for retrieving a setting value
|
|
|
|
*/
|
|
|
|
if (!function_exists('setting_save')) {
|
|
|
|
function setting_save($key, $value)
|
|
|
|
{
|
|
|
|
$settingRepo = app('setting');
|
|
|
|
$settingRepo->save($key, $value);
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
/*
|
2017-12-18 06:58:53 +08:00
|
|
|
* Wrap the asset URL in the publicBaseUrl that's been
|
|
|
|
* set
|
|
|
|
*/
|
2018-01-01 02:55:29 +08:00
|
|
|
if (!function_exists('public_asset')) {
|
2019-08-28 03:08:42 +08:00
|
|
|
function public_asset($path, array $parameters = [])
|
2017-12-18 06:58:53 +08:00
|
|
|
{
|
|
|
|
$publicBaseUrl = app()->publicUrlPath();
|
2018-03-20 09:50:40 +08:00
|
|
|
$path = $publicBaseUrl.$path;
|
2017-12-18 06:58:53 +08:00
|
|
|
|
2017-12-25 02:10:51 +08:00
|
|
|
$path = str_replace('//', '/', $path);
|
|
|
|
|
2019-08-28 03:08:42 +08:00
|
|
|
return url($path, $parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Call mix() and then prepend the proper public URL
|
|
|
|
*/
|
2019-08-28 22:52:35 +08:00
|
|
|
if (!function_exists('public_mix')) {
|
|
|
|
function public_mix($path, array $parameters = [])
|
2019-08-28 03:08:42 +08:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$path = mix($path);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
return public_asset($path, $parameters);
|
2017-12-18 06:58:53 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-24 01:58:17 +08:00
|
|
|
|
2020-01-31 00:06:51 +08:00
|
|
|
/**
|
|
|
|
* Wrap a call to url() and append the public folder before it
|
|
|
|
*/
|
|
|
|
if (!function_exists('public_url')) {
|
|
|
|
function public_url($path, array $parameters = [])
|
|
|
|
{
|
|
|
|
$publicBaseUrl = app()->publicUrlPath();
|
|
|
|
$path = $publicBaseUrl.$path;
|
|
|
|
|
|
|
|
$path = str_replace('//', '/', $path);
|
|
|
|
|
|
|
|
return url($path, $parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
/*
|
2017-12-24 01:58:17 +08:00
|
|
|
* Show a date/time in the proper timezone for a user
|
|
|
|
*/
|
2018-03-20 09:50:40 +08:00
|
|
|
if (!function_exists('show_datetime')) {
|
2017-12-24 01:58:17 +08:00
|
|
|
/**
|
|
|
|
* Format the a Carbon date into the datetime string
|
|
|
|
* but convert it into the user's timezone
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2019-08-28 03:08:42 +08:00
|
|
|
* @param Carbon $date
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-24 01:58:17 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-08-28 03:08:42 +08:00
|
|
|
function show_datetime(Carbon $date = null)
|
2017-12-24 01:58:17 +08:00
|
|
|
{
|
2018-08-27 02:50:08 +08:00
|
|
|
if ($date === null) {
|
2018-01-03 00:59:44 +08:00
|
|
|
return '-';
|
|
|
|
}
|
|
|
|
|
2017-12-24 01:58:17 +08:00
|
|
|
$timezone = 'UTC';
|
|
|
|
if (Auth::check()) {
|
|
|
|
$timezone = Auth::user()->timezone ?: $timezone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $date->timezone($timezone)->toDayDateTimeString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
/*
|
2017-12-24 01:58:17 +08:00
|
|
|
* Show a date/time in the proper timezone for a user
|
|
|
|
*/
|
|
|
|
if (!function_exists('show_date')) {
|
|
|
|
/**
|
|
|
|
* Format the a Carbon date into the datetime string
|
|
|
|
* but convert it into the user's timezone
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-24 01:58:17 +08:00
|
|
|
* @param \Carbon\Carbon $date
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2017-12-24 01:58:17 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-08-28 03:08:42 +08:00
|
|
|
function show_date(Carbon $date)
|
2017-12-24 01:58:17 +08:00
|
|
|
{
|
|
|
|
$timezone = 'UTC';
|
|
|
|
if (Auth::check()) {
|
|
|
|
$timezone = Auth::user()->timezone ?: $timezone;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $date->timezone($timezone)->toFormattedDateString();
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 04:57:30 +08:00
|
|
|
|
|
|
|
if (!function_exists('_fmt')) {
|
|
|
|
/**
|
|
|
|
* Replace strings
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-01 04:57:30 +08:00
|
|
|
* @param $line "Hi, my name is :name"
|
|
|
|
* @param array $replace ['name' => 'Nabeel']
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-01 04:57:30 +08:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
function _fmt($line, array $replace)
|
|
|
|
{
|
|
|
|
if (empty($replace)) {
|
|
|
|
return $line;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($replace as $key => $value) {
|
|
|
|
$key = strtolower($key);
|
|
|
|
$line = str_replace(
|
|
|
|
[':'.$key],
|
|
|
|
[$value],
|
|
|
|
$line
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $line;
|
|
|
|
}
|
2018-08-27 00:40:04 +08:00
|
|
|
}
|