524 lines
15 KiB
PHP
524 lines
15 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\UserHistory;
|
|
use Illuminate\Support\Str;
|
|
use Request;
|
|
use Yard;
|
|
|
|
class Util
|
|
{
|
|
private static $postRoute = 'base.';
|
|
|
|
public static function getToken()
|
|
{
|
|
return hash_hmac('sha256', Str::random(40), config('app.key'));
|
|
}
|
|
|
|
public static function uuidToken()
|
|
{
|
|
$uuid = (string) Str::uuid();
|
|
$e_uuid = explode('-', $uuid);
|
|
if (isset($e_uuid[0]) && $e_uuid[1]) {
|
|
return $e_uuid[0] . '-' . $e_uuid[1];
|
|
}
|
|
|
|
return $uuid;
|
|
}
|
|
|
|
public static function formatDate()
|
|
{
|
|
if (\App::getLocale() === 'en') {
|
|
return 'yyyy-mm-dd';
|
|
}
|
|
|
|
return 'dd.mm.yyyy';
|
|
}
|
|
|
|
public static function formatDateDB()
|
|
{
|
|
if (\App::getLocale() === 'en') {
|
|
return 'Y-m-d';
|
|
}
|
|
|
|
return 'd.m.Y';
|
|
}
|
|
|
|
public static function formatDateTimeDB()
|
|
{
|
|
if (\App::getLocale() === 'en') {
|
|
return 'Y-m-d - H:i';
|
|
}
|
|
|
|
return 'd.m.Y - H:i';
|
|
}
|
|
|
|
public static function _format_number($value)
|
|
{
|
|
// Erlaubt Zahlen, Komma, Punkt und Minus (für Dezimalzahlen in DE und EN Format)
|
|
return preg_replace('/[^0-9,.-]/', '', $value);
|
|
}
|
|
|
|
public static function _thousands_separator()
|
|
{
|
|
return \App::getLocale() === 'en' ? ',' : '.';
|
|
}
|
|
|
|
public static function _decimal_separator()
|
|
{
|
|
return \App::getLocale() === 'en' ? '.' : ',';
|
|
}
|
|
|
|
public static function maxStrLength($str, $length = 40)
|
|
{
|
|
|
|
if (strlen($str) > $length) {
|
|
$str = substr($str, 0, $length);
|
|
// $str = substr($str, 0, strrpos($str, " "));
|
|
$str = $str . ' ...';
|
|
}
|
|
|
|
return $str;
|
|
}
|
|
|
|
public static function reFormatNumber($value)
|
|
{
|
|
// Wenn bereits ein Float/Int, direkt zurückgeben
|
|
if (is_numeric($value) && ! is_string($value)) {
|
|
return (float) $value;
|
|
}
|
|
|
|
$value = (string) $value;
|
|
|
|
// Entferne alle nicht-numerischen Zeichen außer Punkt, Komma und Minus
|
|
$value = preg_replace('/[^0-9,.\-]/', '', $value);
|
|
|
|
if ($value === '') {
|
|
return 0.0;
|
|
}
|
|
|
|
// Erkenne das Zahlenformat anhand der Position des letzten Trennzeichens
|
|
$lastComma = strrpos($value, ',');
|
|
$lastDot = strrpos($value, '.');
|
|
|
|
if ($lastComma !== false && ($lastDot === false || $lastComma > $lastDot)) {
|
|
// Deutsches Format: "1.000,50" -> Punkt entfernen (Tausender), Komma zu Punkt
|
|
$value = str_replace('.', '', $value);
|
|
$value = str_replace(',', '.', $value);
|
|
} else {
|
|
// Englisches Format: "1,000.50" -> Komma entfernen (Tausender)
|
|
$value = str_replace(',', '', $value);
|
|
}
|
|
|
|
return (float) $value;
|
|
}
|
|
|
|
public static function formatNumber($value, $dec = 2)
|
|
{
|
|
// Wenn der Wert bereits numerisch ist (int/float), direkt formatieren
|
|
if (is_numeric($value) && ! is_string($value)) {
|
|
return number_format((float) $value, $dec, self::_decimal_separator(), self::_thousands_separator());
|
|
}
|
|
|
|
// Bei String-Eingaben: deutsches Format (mit Komma) zu Float konvertieren
|
|
$value = self::reFormatNumber($value);
|
|
|
|
return number_format($value, $dec, self::_decimal_separator(), self::_thousands_separator());
|
|
}
|
|
|
|
public static function cleanIntegerFromString($value)
|
|
{
|
|
// Entferne alle nicht-numerischen Zeichen außer Minus
|
|
$cleanStr = preg_replace('/[^0-9-]/', '', $value);
|
|
|
|
// Konvertiere zu Integer und entferne führende Nullen
|
|
$number = (int) $cleanStr;
|
|
|
|
return $number;
|
|
}
|
|
|
|
public static function cleanNumberFormat($num = 0, $dec = 2, $fullzero = false)
|
|
{
|
|
|
|
if ($fullzero && $num == 0) {
|
|
return number_format($num, $dec, self::_decimal_separator(), self::_thousands_separator());
|
|
}
|
|
|
|
return rtrim(rtrim(number_format($num, $dec, self::_decimal_separator(), self::_thousands_separator()), '0'), self::_decimal_separator());
|
|
}
|
|
|
|
public static function utf8ize($mixed)
|
|
{
|
|
if (is_array($mixed)) {
|
|
foreach ($mixed as $key => $value) {
|
|
$mixed[$key] = self::utf8ize($value);
|
|
}
|
|
} elseif (is_string($mixed)) {
|
|
return mb_convert_encoding($mixed, 'UTF-8', 'UTF-8');
|
|
}
|
|
|
|
return $mixed;
|
|
}
|
|
|
|
public static function getPostRoute()
|
|
{
|
|
return self::$postRoute;
|
|
}
|
|
|
|
public static function setPostRoute($postRoute)
|
|
{
|
|
self::$postRoute = $postRoute;
|
|
}
|
|
|
|
public static function getUserShop()
|
|
{
|
|
$shop = session('user_shop');
|
|
if (empty($shop) || ! is_object($shop)) {
|
|
return null;
|
|
}
|
|
|
|
return $shop;
|
|
}
|
|
|
|
public static function getDefaultUserShop()
|
|
{
|
|
$user = \App\User::find(6);
|
|
if ($user && $user->shop) {
|
|
return $user->shop;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function getAuthUser()
|
|
{
|
|
if (\Session::has('auth_user')) {
|
|
if ($auth_user = \Session::get('auth_user')) {
|
|
return $auth_user;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function getUserShopIdentifier()
|
|
{
|
|
if (\Session::has('user_shop_identifier')) {
|
|
if ($user_shop_identifier = \Session::get('user_shop_identifier')) {
|
|
return $user_shop_identifier;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function getInstanceStatus()
|
|
{
|
|
$identifier = self::getUserShopIdentifier();
|
|
if ($identifier && \Session::has('user_shop_payment') && \Session::get('user_shop_payment') === 6) {
|
|
return OrderPaymentService::getInstanceStatus($identifier);
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function setInstanceStatus($status, $lower = true)
|
|
{
|
|
$identifier = self::getUserShopIdentifier();
|
|
if ($identifier && \Session::has('user_shop_payment') && \Session::get('user_shop_payment') === 6) {
|
|
OrderPaymentService::updateInstanceStatus($identifier, $status, $lower);
|
|
}
|
|
}
|
|
|
|
public static function setInstanceStatusByPayment($shopping_payment, $status, $lower = true)
|
|
{
|
|
if ($shopping_payment->identifier) {
|
|
OrderPaymentService::updateInstanceStatus($shopping_payment->identifier, $status, $lower);
|
|
}
|
|
}
|
|
|
|
public static function getShoppingInstance()
|
|
{
|
|
if (\Session::has('shopping_instance')) {
|
|
return \Session::get('shopping_instance');
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function getUserHistory()
|
|
{
|
|
$auth_user = self::getAuthUser();
|
|
$user_shop_identifier = self::getUserShopIdentifier();
|
|
if ($user_shop_identifier && $auth_user) {
|
|
return UserHistory::whereUserId($auth_user->id)->whereIdentifier($user_shop_identifier)->get()->last();
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function setUserHistoryValue($values = [])
|
|
{
|
|
if ($user_history = self::getUserHistory()) {
|
|
foreach ($values as $key => $val) {
|
|
$user_history->{$key} = $val;
|
|
}
|
|
$user_history->save();
|
|
}
|
|
}
|
|
|
|
public static function getUserHistoryValue($key)
|
|
{
|
|
if ($user_history = self::getUserHistory()) {
|
|
return $user_history->{$key};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function getUserShoppingMode()
|
|
{
|
|
if ($auth_user = self::getAuthUser()) {
|
|
if ($auth_user->isTestMode()) {
|
|
return 'test';
|
|
}
|
|
}
|
|
|
|
return config('app.mode');
|
|
}
|
|
|
|
public static function addRoute($p = [])
|
|
{
|
|
$b = [];
|
|
if (\Session::has('user_shop')) {
|
|
if ($user_shop = \Session::get('user_shop')) {
|
|
$b = ['subdomain' => $user_shop->slug];
|
|
}
|
|
}
|
|
|
|
return array_merge($p, $b);
|
|
}
|
|
|
|
public static function checkUserLandIsNot($user)
|
|
{
|
|
|
|
if (isset($user->account->country_id)) {
|
|
// ch schweiz is out
|
|
if ($user->account->country_id === 6) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function getCustomerUserShopDomain()
|
|
{
|
|
if (\Auth::guard('customers')->check()) {
|
|
if (\Auth::guard('customers')->user()->user_shop_domain) {
|
|
return \Auth::guard('customers')->user()->user_shop_domain;
|
|
}
|
|
}
|
|
|
|
return self::getMyMivitaShopUrl();
|
|
}
|
|
|
|
public static function getMyMivitaShopUrl($add_url = '')
|
|
{
|
|
if (\Session::has('user_shop_domain')) {
|
|
$url = \Session::get('user_shop_domain') . $add_url;
|
|
if (! str_starts_with($url, 'http')) {
|
|
$url = 'https://' . ltrim($url, '/');
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
// alois sein shop
|
|
$user = \App\User::find(6);
|
|
if ($user && $user->shop) {
|
|
return config('app.protocol') . $user->shop->slug . '.' . config('app.domain') . config('app.tld_care') . $add_url;
|
|
}
|
|
}
|
|
|
|
public static function getMyMivitaPortalUrl($protocol = true)
|
|
{
|
|
$pro = $protocol ? config('app.protocol') : '';
|
|
|
|
return $pro . config('app.pre_url_portal') . config('app.domain') . config('app.tld_care');
|
|
}
|
|
|
|
public static function getMyMivitaUrl($protocol = true)
|
|
{
|
|
$pro = $protocol ? config('app.protocol') : '';
|
|
|
|
return $pro . config('app.pre_url_crm') . config('app.domain') . config('app.tld_care');
|
|
}
|
|
|
|
public static function getUserPaymentFor($instance = 'shopping')
|
|
{
|
|
if (Yard::instance($instance)->getYardExtra('user_shop_payment')) {
|
|
return Yard::instance($instance)->getYardExtra('user_shop_payment');
|
|
}
|
|
if (\Session::has('user_shop_payment')) {
|
|
return \Session::get('user_shop_payment');
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function getUserShopBackUrl($reference = '')
|
|
{
|
|
|
|
if (\Session::has('user_shop')) {
|
|
if (\Session::has('user_shop_domain')) {
|
|
return \Session::get('user_shop_domain');
|
|
}
|
|
if ($user_shop = \Session::get('user_shop')) {
|
|
return config('app.protocol') . $user_shop->slug . '.' . config('app.domain') . config('app.tld_care') . '/back/to/shop/' . $reference;
|
|
}
|
|
}
|
|
|
|
return config('app.protocol') . config('app.domain') . config('app.tld_care');
|
|
}
|
|
|
|
public static function getUserCardBackUrl($uri, $instance = 'shopping')
|
|
{
|
|
|
|
if (\Session::has('user_shop')) {
|
|
if (\Session::has('user_shop_domain')) {
|
|
if (\Session::has('back_link')) {
|
|
return \Session::get('back_link');
|
|
}
|
|
if (self::getUserPaymentFor($instance) === 3) {
|
|
return \Session::get('user_shop_domain') . '/user/membership';
|
|
}
|
|
if (self::getUserPaymentFor($instance) === 2) {
|
|
return \Session::get('user_shop_domain') . '/user/orders';
|
|
}
|
|
|
|
return \Session::get('user_shop_domain');
|
|
}
|
|
if ($user_shop = \Session::get('user_shop')) {
|
|
return config('app.protocol') . $user_shop->slug . '.' . config('app.domain') . config('app.tld_care') . $uri;
|
|
}
|
|
}
|
|
|
|
return config('app.protocol') . config('app.domain') . config('app.tld_care');
|
|
}
|
|
|
|
public static function isMivitaShop()
|
|
{
|
|
if (Request::getHost() === 'checkout.' . config('app.domain') . config('app.tld_care')) {
|
|
if ($user_shop = \Session::get('user_shop')) {
|
|
if ($user_shop->slug === 'aloevera' || $user_shop->slug === 'naturcosmetic') {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
if (Request::getHost() === 'naturcosmetic.' . config('app.domain') . config('app.tld_care')) {
|
|
return true;
|
|
}
|
|
|
|
return \Config::get('app.url') === config('app.domain') . config('app.tld_shop');
|
|
}
|
|
|
|
public static function isTestSystem($dev = false)
|
|
{
|
|
if (\Config::get('app.tld_care') === '.test' || \Config::get('app.tld_shop') === '.lshop') {
|
|
if ($dev && config('app.debug') !== true) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static function formatBytes($size, $precision = 2)
|
|
{
|
|
if ($size > 0) {
|
|
$size = (int) $size;
|
|
$base = log($size) / log(1024);
|
|
$suffixes = [' bytes', ' KB', ' MB', ' GB', ' TB'];
|
|
|
|
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
|
|
} else {
|
|
return $size;
|
|
}
|
|
}
|
|
|
|
public static function formatTextWithLineBreaks($text, $translate = false)
|
|
{
|
|
if ($translate) {
|
|
$translated = ['payment.commission_shop', 'payment.commission_payline', 'payment.commission_growth_bonus'];
|
|
foreach ($translated as $value) {
|
|
// $text = str_replace($key, trans($value), $text);
|
|
$text = str_replace($value, __($value), $text);
|
|
}
|
|
}
|
|
|
|
return nl2br($text);
|
|
}
|
|
|
|
public static function sanitize($string, $force_lowercase = true, $anal = false, $substr = false)
|
|
{
|
|
$strip = [
|
|
'~',
|
|
'`',
|
|
'!',
|
|
'@',
|
|
'#',
|
|
'$',
|
|
'%',
|
|
'^',
|
|
'&',
|
|
'*',
|
|
'(',
|
|
')',
|
|
'_',
|
|
'=',
|
|
'+',
|
|
'[',
|
|
'{',
|
|
']',
|
|
'}',
|
|
'\\',
|
|
'|',
|
|
';',
|
|
':',
|
|
'"',
|
|
"'",
|
|
'‘',
|
|
'’',
|
|
'“',
|
|
'”',
|
|
'–',
|
|
'—',
|
|
'—',
|
|
'–',
|
|
',',
|
|
'<',
|
|
'.',
|
|
'>',
|
|
'/',
|
|
'?',
|
|
];
|
|
$clean = trim(str_replace($strip, '', strip_tags($string)));
|
|
$clean = preg_replace('/\s+/', '_', $clean);
|
|
$clean = ($anal) ? preg_replace('/[^a-zA-Z0-9]/', '', $clean) : $clean;
|
|
|
|
if ($substr) {
|
|
$clean = (strlen($clean) > 20) ? substr($clean, -20) : $clean;
|
|
}
|
|
|
|
return ($force_lowercase) ?
|
|
(function_exists('mb_strtolower')) ?
|
|
mb_strtolower($clean, 'UTF-8') :
|
|
strtolower($clean) :
|
|
$clean;
|
|
}
|
|
}
|