316 lines
No EOL
9.5 KiB
PHP
316 lines
No EOL
9.5 KiB
PHP
<?php
|
|
namespace App\Services;
|
|
|
|
use Yard;
|
|
use App\Models\Country;
|
|
use App\Models\UserHistory;
|
|
use Illuminate\Support\Str;
|
|
use App\Models\ShippingCountry;
|
|
use Request;
|
|
|
|
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 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(){
|
|
if(\Session::has('user_shop')){
|
|
if($user_shop = \Session::get('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 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 'live';
|
|
}
|
|
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 isCheckout(){
|
|
|
|
if(\Session::has('isCheckout')){
|
|
if(\Session::get('isCheckout') == true){
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
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 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(){
|
|
if(Yard::instance('shopping')->getYardExtra('user_shop_payment')){
|
|
return Yard::instance('shopping')->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 url("/");
|
|
}
|
|
|
|
public static function getUserCardBackUrl($uri){
|
|
|
|
if(\Session::has('user_shop')){
|
|
if(\Session::has('user_shop_domain')){
|
|
if(\Session::has('back_link')){
|
|
return \Session::get('back_link');
|
|
}
|
|
if(self::getUserPaymentFor() === 3){
|
|
return \Session::get('user_shop_domain')."/user/membership";
|
|
}
|
|
if(self::getUserPaymentFor() === 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 url($uri);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
} |