Cachet/app/Http/helpers.php

120 lines
2.8 KiB
PHP
Raw Normal View History

2015-03-20 18:30:45 -06:00
<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2015-03-20 18:30:45 -06:00
use CachetHQ\Cachet\Facades\Setting;
2015-11-21 22:47:09 +00:00
use Illuminate\Support\Facades\Config;
2015-03-20 18:30:45 -06:00
use Illuminate\Support\Facades\Request;
2015-05-07 09:09:03 -05:00
use Jenssegers\Date\Date;
2015-03-20 18:30:45 -06:00
if (!function_exists('set_active')) {
/**
* Set active class if request is in path.
*
* @param string $path
* @param array $classes
* @param string $active
*
* @return string
*/
function set_active($path, array $classes = [], $active = 'active')
{
if (Request::is($path)) {
$classes[] = $active;
}
$class = e(implode(' ', $classes));
2015-03-20 18:30:45 -06:00
return empty($classes) ? '' : "class=\"{$class}\"";
2015-03-20 18:30:45 -06:00
}
}
2015-05-07 09:09:03 -05:00
if (!function_exists('formatted_date')) {
/**
* Formats a date with the user timezone and the selected format.
*
* @param string $date
*
* @return \Jenssegers\Date\Date
*/
function formatted_date($date)
{
2015-05-20 12:56:59 -05:00
$dateFormat = Setting::get('date_format', 'jS F Y');
2015-05-07 09:09:03 -05:00
2015-05-07 10:37:32 -05:00
return (new Date($date))->format($dateFormat);
2015-05-07 09:09:03 -05:00
}
}
if (!function_exists('subscribers_enabled')) {
/**
* Is the subscriber functionality enabled and configured.
*
* @return bool
*/
function subscribers_enabled()
{
$isEnabled = Setting::get('enable_subscribers', false);
2015-11-21 22:47:09 +00:00
$mailAddress = Config::get('mail.from.address', false);
$mailFrom = Config::get('mail.from.name', false);
return $isEnabled && $mailAddress && $mailFrom;
}
}
if (!function_exists('color_darken')) {
/**
* Darken a color.
*
* @param string $hex
* @param int $percent
*
* @return string
*/
function color_darken($hex, $percent)
{
$hex = preg_replace('/[^0-9a-f]/i', '', $hex);
$new_hex = '#';
if (strlen($hex) < 6) {
$hex = $hex[0] + $hex[0] + $hex[1] + $hex[1] + $hex[2] + $hex[2];
}
for ($i = 0; $i < 3; $i++) {
$dec = hexdec(substr($hex, $i * 2, 2));
$dec = min(max(0, $dec + $dec * $percent), 255);
$new_hex .= str_pad(dechex($dec), 2, 0, STR_PAD_LEFT);
}
return $new_hex;
}
}
if (!function_exists('color_contrast')) {
/**
* Calculates colour contrast.
*
* https://24ways.org/2010/calculating-color-contrast/
*
* @param string $hexcolor
*
* @return string
*/
2015-10-09 09:42:10 -04:00
function color_contrast($hexcolor)
{
$r = hexdec(substr($hexcolor, 0, 2));
$g = hexdec(substr($hexcolor, 2, 2));
$b = hexdec(substr($hexcolor, 4, 2));
$yiq = (($r * 100) + ($g * 400) + ($b * 114)) / 1000;
return ($yiq >= 128) ? 'black' : 'white';
}
}