440 lines
12 KiB
PHP
440 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Country;
|
|
use App\Models\ShippingCountry;
|
|
use App\Models\UserHistory;
|
|
use App\Models\UserShop;
|
|
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)
|
|
{
|
|
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)
|
|
{
|
|
return (float) str_replace(',', '.', self::_format_number($value));
|
|
}
|
|
|
|
public static function formatNumber($value, $dec = 2)
|
|
{
|
|
$value = floatval(str_replace(',', '', $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 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 = array(' bytes', ' KB', ' MB', ' GB', ' TB');
|
|
|
|
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
|
|
} else {
|
|
return $size;
|
|
}
|
|
}
|
|
|
|
public static function sanitize($string, $force_lowercase = true, $anal = false, $substr = false)
|
|
{
|
|
$strip = array(
|
|
"~",
|
|
"`",
|
|
"!",
|
|
"@",
|
|
"#",
|
|
"$",
|
|
"%",
|
|
"^",
|
|
"&",
|
|
"*",
|
|
"(",
|
|
")",
|
|
"_",
|
|
"=",
|
|
"+",
|
|
"[",
|
|
"{",
|
|
"]",
|
|
"}",
|
|
"\\",
|
|
"|",
|
|
";",
|
|
":",
|
|
"\"",
|
|
"'",
|
|
"‘",
|
|
"’",
|
|
"“",
|
|
"”",
|
|
"–",
|
|
"—",
|
|
"—",
|
|
"–",
|
|
",",
|
|
"<",
|
|
".",
|
|
">",
|
|
"/",
|
|
"?"
|
|
);
|
|
$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;
|
|
}
|
|
}
|