mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 08:27:34 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			1481 lines
		
	
	
		
			34 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			1481 lines
		
	
	
		
			34 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
namespace App\Libraries;
 | 
						|
 | 
						|
use App\Models\AccountGateway;
 | 
						|
use Auth;
 | 
						|
use Cache;
 | 
						|
use App;
 | 
						|
use Schema;
 | 
						|
use Session;
 | 
						|
use Exception;
 | 
						|
use View;
 | 
						|
use DateTimeZone;
 | 
						|
use Input;
 | 
						|
use Log;
 | 
						|
use DateTime;
 | 
						|
use stdClass;
 | 
						|
use Carbon;
 | 
						|
use WePay;
 | 
						|
 | 
						|
class Utils
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * Weekdays in English from Sunday to Saturday, 0 to 6
 | 
						|
     *
 | 
						|
     * @var array
 | 
						|
     */
 | 
						|
    private static $weekdayNames = [
 | 
						|
        "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",
 | 
						|
    ];
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isRegistered()
 | 
						|
    {
 | 
						|
        return Auth::check() && Auth::user()->registered;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isConfirmed()
 | 
						|
    {
 | 
						|
        return Auth::check() && Auth::user()->confirmed;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isDatabaseSetup()
 | 
						|
    {
 | 
						|
        try {
 | 
						|
            if (Schema::hasTable('accounts')) {
 | 
						|
                return true;
 | 
						|
            }
 | 
						|
        } catch (Exception $e) {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isDownForMaintenance()
 | 
						|
    {
 | 
						|
        return file_exists(storage_path() . '/framework/down');
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isCron()
 | 
						|
    {
 | 
						|
        return php_sapi_name() == 'cli';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isTravis()
 | 
						|
    {
 | 
						|
        return env('TRAVIS') == 'true';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isNinja()
 | 
						|
    {
 | 
						|
        return self::isNinjaProd() || self::isNinjaDev();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isNinjaProd()
 | 
						|
    {
 | 
						|
        if (static::isReseller()) {
 | 
						|
            return true;
 | 
						|
        }
 | 
						|
 | 
						|
        return env('NINJA_PROD') == 'true';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isNinjaDev()
 | 
						|
    {
 | 
						|
        return env('NINJA_DEV') == 'true';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function requireHTTPS()
 | 
						|
    {
 | 
						|
        if (app('request')->root() === 'http://ninja.dev' || app('request')->root() === 'http://ninja.dev:8000') {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
 | 
						|
        return static::isNinjaProd() || env('REQUIRE_HTTPS');
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isReseller()
 | 
						|
    {
 | 
						|
        return self::getResellerType() ? true : false;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return array|bool
 | 
						|
     */
 | 
						|
    public static function getResellerType()
 | 
						|
    {
 | 
						|
        return isset($_ENV['RESELLER_TYPE']) ? $_ENV['RESELLER_TYPE'] : false;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isOAuthEnabled()
 | 
						|
    {
 | 
						|
        $providers = [
 | 
						|
            SOCIAL_GOOGLE,
 | 
						|
            SOCIAL_FACEBOOK,
 | 
						|
            SOCIAL_GITHUB,
 | 
						|
            SOCIAL_LINKEDIN
 | 
						|
        ];
 | 
						|
 | 
						|
        foreach ($providers as $provider) {
 | 
						|
            $key = strtoupper($provider) . '_CLIENT_ID';
 | 
						|
            if (isset($_ENV[$key]) && $_ENV[$key]) {
 | 
						|
                return true;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function allowNewAccounts()
 | 
						|
    {
 | 
						|
        return static::isNinja() || Auth::check();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isPro()
 | 
						|
    {
 | 
						|
        return Auth::check() && Auth::user()->isPro();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $feature
 | 
						|
     *
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function hasFeature($feature)
 | 
						|
    {
 | 
						|
        return Auth::check() && Auth::user()->hasFeature($feature);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isAdmin()
 | 
						|
    {
 | 
						|
        return Auth::check() && Auth::user()->is_admin;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $permission
 | 
						|
     * @param bool $requireAll
 | 
						|
     *
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function hasPermission($permission, $requireAll = false)
 | 
						|
    {
 | 
						|
        return Auth::check() && Auth::user()->hasPermission($permission, $requireAll);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $permission
 | 
						|
     *
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function hasAllPermissions($permission)
 | 
						|
    {
 | 
						|
        return Auth::check() && Auth::user()->hasPermission($permission);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isTrial()
 | 
						|
    {
 | 
						|
        return Auth::check() && Auth::user()->isTrial();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isEnglish()
 | 
						|
    {
 | 
						|
        return App::getLocale() == 'en';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getLocaleRegion()
 | 
						|
    {
 | 
						|
        $parts = explode('_', App::getLocale());
 | 
						|
 | 
						|
        return count($parts) ? $parts[0] : 'en';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function getUserType()
 | 
						|
    {
 | 
						|
        if (static::isNinja()) {
 | 
						|
            return USER_TYPE_CLOUD_HOST;
 | 
						|
        }
 | 
						|
 | 
						|
        return USER_TYPE_SELF_HOST;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return array|bool
 | 
						|
     */
 | 
						|
    public static function getDemoAccountId()
 | 
						|
    {
 | 
						|
        return isset($_ENV[DEMO_ACCOUNT_ID]) ? $_ENV[DEMO_ACCOUNT_ID] : false;
 | 
						|
    }
 | 
						|
 | 
						|
    public static function getNewsFeedResponse($userType = false)
 | 
						|
    {
 | 
						|
        if (!$userType) {
 | 
						|
            $userType = static::getUserType();
 | 
						|
        }
 | 
						|
 | 
						|
        $response = new stdClass();
 | 
						|
        $response->message = isset($_ENV["{$userType}_MESSAGE"]) ? $_ENV["{$userType}_MESSAGE"] : '';
 | 
						|
        $response->id = isset($_ENV["{$userType}_ID"]) ? $_ENV["{$userType}_ID"] : '';
 | 
						|
        $response->version = env('NINJA_SELF_HOST_VERSION', NINJA_VERSION);
 | 
						|
 | 
						|
        return $response;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getLastURL()
 | 
						|
    {
 | 
						|
        if (!count(Session::get(RECENTLY_VIEWED))) {
 | 
						|
            return '#';
 | 
						|
        }
 | 
						|
 | 
						|
        $history = Session::get(RECENTLY_VIEWED);
 | 
						|
        $last = $history[0];
 | 
						|
        $penultimate = count($history) > 1 ? $history[1] : $last;
 | 
						|
 | 
						|
        return app('request')->url() == $last->url ? $penultimate->url : $last->url;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $feature
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getProLabel($feature)
 | 
						|
    {
 | 
						|
        if (Auth::check()
 | 
						|
                && !Auth::user()->isPro()
 | 
						|
                && $feature == ACCOUNT_ADVANCED_SETTINGS) {
 | 
						|
            return ' <sup class="pro-label">PRO</sup>';
 | 
						|
        }
 | 
						|
 | 
						|
        return '';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param array $plan
 | 
						|
     *
 | 
						|
     * @return int
 | 
						|
     */
 | 
						|
    public static function getPlanPrice(array $plan)
 | 
						|
    {
 | 
						|
        $term = $plan['term'];
 | 
						|
        $numUsers = $plan['num_users'];
 | 
						|
        $plan = $plan['plan'];
 | 
						|
 | 
						|
        if ($plan == PLAN_FREE) {
 | 
						|
            $price = 0;
 | 
						|
        } elseif ($plan == PLAN_PRO) {
 | 
						|
            $price = PLAN_PRICE_PRO_MONTHLY;
 | 
						|
        } elseif ($plan == PLAN_ENTERPRISE) {
 | 
						|
            if ($numUsers <= 2) {
 | 
						|
                $price = PLAN_PRICE_ENTERPRISE_MONTHLY_2;
 | 
						|
            } elseif ($numUsers <= 5) {
 | 
						|
                $price = PLAN_PRICE_ENTERPRISE_MONTHLY_5;
 | 
						|
            } elseif ($numUsers <= 10) {
 | 
						|
                $price = PLAN_PRICE_ENTERPRISE_MONTHLY_10;
 | 
						|
            } else {
 | 
						|
                static::fatalError('Invalid number of users: ' . $numUsers);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($term == PLAN_TERM_YEARLY) {
 | 
						|
            $price = $price * 10;
 | 
						|
        }
 | 
						|
 | 
						|
        return $price;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $max
 | 
						|
     *
 | 
						|
     * @return int
 | 
						|
     */
 | 
						|
    public static function getMinNumUsers($max)
 | 
						|
    {
 | 
						|
        if ($max <= 2) {
 | 
						|
            return 1;
 | 
						|
        } elseif ($max <= 5) {
 | 
						|
            return 3;
 | 
						|
        } else {
 | 
						|
            return 6;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function basePath()
 | 
						|
    {
 | 
						|
        return substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/') + 1);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $input
 | 
						|
     *
 | 
						|
     * @return array
 | 
						|
     */
 | 
						|
    public static function trans($input)
 | 
						|
    {
 | 
						|
        $data = [];
 | 
						|
 | 
						|
        foreach ($input as $field) {
 | 
						|
            if ($field == 'checkbox') {
 | 
						|
                $data[] = $field;
 | 
						|
            } elseif ($field) {
 | 
						|
                $data[] = trans("texts.$field");
 | 
						|
            } else {
 | 
						|
                $data[] = '';
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return $data;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param bool $message
 | 
						|
     * @param bool $exception
 | 
						|
     *
 | 
						|
     * @return $this
 | 
						|
     */
 | 
						|
    public static function fatalError($message = false, $exception = false)
 | 
						|
    {
 | 
						|
        if (!$message) {
 | 
						|
            $message = 'An error occurred, please try again later.';
 | 
						|
        }
 | 
						|
 | 
						|
        static::logError($message.' '.$exception);
 | 
						|
 | 
						|
        $data = [
 | 
						|
            'showBreadcrumbs' => false,
 | 
						|
            'hideHeader' => true,
 | 
						|
        ];
 | 
						|
 | 
						|
        return View::make('error', $data)->with('error', $message);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $exception
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getErrorString($exception)
 | 
						|
    {
 | 
						|
        $class = get_class($exception);
 | 
						|
        $code = method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : $exception->getCode();
 | 
						|
        return  "***{$class}*** [{$code}] : {$exception->getFile()} [Line {$exception->getLine()}] => {$exception->getMessage()}";
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $error
 | 
						|
     * @param string $context
 | 
						|
     * @param bool $info
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function logError($error, $context = 'PHP', $info = false)
 | 
						|
    {
 | 
						|
        if ($error instanceof Exception) {
 | 
						|
            $error = self::getErrorString($error);
 | 
						|
        }
 | 
						|
 | 
						|
        $count = Session::get('error_count', 0);
 | 
						|
        Session::put('error_count', ++$count);
 | 
						|
        if ($count > 200) {
 | 
						|
            return 'logged';
 | 
						|
        }
 | 
						|
 | 
						|
        $data = [
 | 
						|
            'context' => $context,
 | 
						|
            'user_id' => Auth::check() ? Auth::user()->id : 0,
 | 
						|
            'account_id' => Auth::check() ? Auth::user()->account_id : 0,
 | 
						|
            'user_name' => Auth::check() ? Auth::user()->getDisplayName() : '',
 | 
						|
            'method' => app('request')->method(),
 | 
						|
            'url' => Input::get('url', app('request')->url()),
 | 
						|
            'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
 | 
						|
            'ip' => app('request')->getClientIp(),
 | 
						|
            'count' => Session::get('error_count', 0),
 | 
						|
        ];
 | 
						|
 | 
						|
        if ($info) {
 | 
						|
            Log::info($error."\n", $data);
 | 
						|
        } else {
 | 
						|
            Log::error($error."\n", $data);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $value
 | 
						|
     *
 | 
						|
     * @return float
 | 
						|
     */
 | 
						|
    public static function parseFloat($value)
 | 
						|
    {
 | 
						|
        $value = preg_replace('/[^0-9\.\-]/', '', $value);
 | 
						|
 | 
						|
        return floatval($value);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $value
 | 
						|
     *
 | 
						|
     * @return int
 | 
						|
     */
 | 
						|
    public static function parseInt($value)
 | 
						|
    {
 | 
						|
        $value = preg_replace('/[^0-9]/', '', $value);
 | 
						|
 | 
						|
        return intval($value);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $id
 | 
						|
     * @param $type
 | 
						|
     *
 | 
						|
     * @return null
 | 
						|
     */
 | 
						|
    public static function getFromCache($id, $type) {
 | 
						|
        $cache = Cache::get($type);
 | 
						|
 | 
						|
        if ( ! $cache) {
 | 
						|
            static::logError("Cache for {$type} is not set");
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
 | 
						|
       return $cache->filter(function($item) use ($id) {
 | 
						|
            return $item->id == $id;
 | 
						|
        })->first();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $value
 | 
						|
     * @param bool $currencyId
 | 
						|
     * @param bool $countryId
 | 
						|
     * @param bool $showCode
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function formatMoney($value, $currencyId = false, $countryId = false, $showCode = false)
 | 
						|
    {
 | 
						|
        if (!$value) {
 | 
						|
            $value = 0;
 | 
						|
        }
 | 
						|
 | 
						|
        if (!$currencyId) {
 | 
						|
            $currencyId = Session::get(SESSION_CURRENCY, DEFAULT_CURRENCY);
 | 
						|
        }
 | 
						|
 | 
						|
        if (!$countryId && Auth::check()) {
 | 
						|
            $countryId = Auth::user()->account->country_id;
 | 
						|
        }
 | 
						|
 | 
						|
        $currency = self::getFromCache($currencyId, 'currencies');
 | 
						|
        $thousand = $currency->thousand_separator;
 | 
						|
        $decimal = $currency->decimal_separator;
 | 
						|
        $precision = $currency->precision;
 | 
						|
        $code = $currency->code;
 | 
						|
        $swapSymbol = $currency->swap_currency_symbol;
 | 
						|
 | 
						|
        if ($countryId && $currencyId == CURRENCY_EURO) {
 | 
						|
            $country = self::getFromCache($countryId, 'countries');
 | 
						|
            $swapSymbol = $country->swap_currency_symbol;
 | 
						|
            if ($country->thousand_separator) {
 | 
						|
                $thousand = $country->thousand_separator;
 | 
						|
            }
 | 
						|
            if ($country->decimal_separator) {
 | 
						|
                $decimal = $country->decimal_separator;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        $value = number_format($value, $precision, $decimal, $thousand);
 | 
						|
        $symbol = $currency->symbol;
 | 
						|
 | 
						|
        if ($showCode || !$symbol) {
 | 
						|
            return "{$value} {$code}";
 | 
						|
        } elseif ($swapSymbol) {
 | 
						|
            return "{$value} " . trim($symbol);
 | 
						|
        } else {
 | 
						|
            return "{$symbol}{$value}";
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $string
 | 
						|
     * @param $count
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function pluralize($string, $count)
 | 
						|
    {
 | 
						|
        $field = $count == 1 ? $string : $string.'s';
 | 
						|
        $string = trans("texts.$field", ['count' => $count]);
 | 
						|
 | 
						|
        return $string;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $type
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function pluralizeEntityType($type)
 | 
						|
    {
 | 
						|
        if ($type === ENTITY_EXPENSE_CATEGORY) {
 | 
						|
            return 'expense_categories';
 | 
						|
        } else {
 | 
						|
            return $type . 's';
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $value
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function maskAccountNumber($value)
 | 
						|
    {
 | 
						|
        $length = strlen($value);
 | 
						|
        if ($length < 4) {
 | 
						|
            str_repeat('*', 16);
 | 
						|
        }
 | 
						|
 | 
						|
        $lastDigits = substr($value, -4);
 | 
						|
        return str_repeat('*', $length - 4) . $lastDigits;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * http://wephp.co/detect-credit-card-type-php/
 | 
						|
     *
 | 
						|
     * @param $number
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getCardType($number)
 | 
						|
    {
 | 
						|
        $number = preg_replace('/[^\d]/', '', $number);
 | 
						|
 | 
						|
        if (preg_match('/^3[47][0-9]{13}$/', $number)) {
 | 
						|
            return 'American Express';
 | 
						|
        } elseif (preg_match('/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/', $number)) {
 | 
						|
            return 'Diners Club';
 | 
						|
        } elseif (preg_match('/^6(?:011|5[0-9][0-9])[0-9]{12}$/', $number)) {
 | 
						|
            return 'Discover';
 | 
						|
        } elseif (preg_match('/^(?:2131|1800|35\d{3})\d{11}$/', $number)) {
 | 
						|
            return 'JCB';
 | 
						|
        } elseif (preg_match('/^5[1-5][0-9]{14}$/', $number)) {
 | 
						|
            return 'MasterCard';
 | 
						|
        } elseif (preg_match('/^4[0-9]{12}(?:[0-9]{3})?$/', $number)) {
 | 
						|
            return 'Visa';
 | 
						|
        }
 | 
						|
 | 
						|
        return 'Unknown';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $data
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function toArray($data)
 | 
						|
    {
 | 
						|
        return json_decode(json_encode((array) $data), true);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $string
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function toSpaceCase($string)
 | 
						|
    {
 | 
						|
        return preg_replace('/([a-z])([A-Z])/s', '$1 $2', $string);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $string
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function toSnakeCase($string)
 | 
						|
    {
 | 
						|
        return preg_replace('/([a-z])([A-Z])/s', '$1_$2', $string);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $string
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function toCamelCase($string)
 | 
						|
    {
 | 
						|
        return lcfirst(static::toClassCase($string));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $string
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function toClassCase($string)
 | 
						|
    {
 | 
						|
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $timestamp
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function timestampToDateTimeString($timestamp)
 | 
						|
    {
 | 
						|
        $timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
 | 
						|
        $format = Session::get(SESSION_DATETIME_FORMAT, DEFAULT_DATETIME_FORMAT);
 | 
						|
 | 
						|
        return self::timestampToString($timestamp, $timezone, $format);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $timestamp
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function timestampToDateString($timestamp)
 | 
						|
    {
 | 
						|
        $timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
 | 
						|
        $format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
 | 
						|
 | 
						|
        return self::timestampToString($timestamp, $timezone, $format);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $date
 | 
						|
     *
 | 
						|
     * @return bool|string
 | 
						|
     */
 | 
						|
    public static function dateToString($date)
 | 
						|
    {
 | 
						|
        if (!$date) {
 | 
						|
            return false;
 | 
						|
        }
 | 
						|
 | 
						|
        if ($date instanceof DateTime) {
 | 
						|
            $dateTime = $date;
 | 
						|
        } else {
 | 
						|
            $dateTime = new DateTime($date);
 | 
						|
        }
 | 
						|
 | 
						|
        $timestamp = $dateTime->getTimestamp();
 | 
						|
        $format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
 | 
						|
 | 
						|
        return static::timestampToString($timestamp, false, $format);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $timestamp
 | 
						|
     * @param bool $timezone
 | 
						|
     * @param $format
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function timestampToString($timestamp, $timezone = false, $format)
 | 
						|
    {
 | 
						|
        if (!$timestamp) {
 | 
						|
            return '';
 | 
						|
        }
 | 
						|
        $date = Carbon::createFromTimeStamp($timestamp);
 | 
						|
        if ($timezone) {
 | 
						|
            $date->tz = $timezone;
 | 
						|
        }
 | 
						|
        if ($date->year < 1900) {
 | 
						|
            return '';
 | 
						|
        }
 | 
						|
 | 
						|
        return $date->format($format);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $date
 | 
						|
     * @param bool $formatResult
 | 
						|
     *
 | 
						|
     * @return DateTime|string|void
 | 
						|
     */
 | 
						|
    public static function toSqlDate($date, $formatResult = true)
 | 
						|
    {
 | 
						|
        if (!$date) {
 | 
						|
            return;
 | 
						|
        }
 | 
						|
 | 
						|
        $format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
 | 
						|
        $dateTime = DateTime::createFromFormat($format, $date);
 | 
						|
 | 
						|
        if(!$dateTime)
 | 
						|
            return $date;
 | 
						|
        else
 | 
						|
            return $formatResult ? $dateTime->format('Y-m-d') : $dateTime;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $date
 | 
						|
     * @param bool $formatResult
 | 
						|
     *
 | 
						|
     * @return DateTime|string
 | 
						|
     */
 | 
						|
    public static function fromSqlDate($date, $formatResult = true)
 | 
						|
    {
 | 
						|
        if (!$date || $date == '0000-00-00') {
 | 
						|
            return '';
 | 
						|
        }
 | 
						|
 | 
						|
        $format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
 | 
						|
        $dateTime = DateTime::createFromFormat('Y-m-d', $date);
 | 
						|
 | 
						|
        if(!$dateTime)
 | 
						|
            return $date;
 | 
						|
        else
 | 
						|
            return $formatResult ? $dateTime->format($format) : $dateTime;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $date
 | 
						|
     * @param bool $formatResult
 | 
						|
     *
 | 
						|
     * @return DateTime|string
 | 
						|
     */
 | 
						|
    public static function fromSqlDateTime($date, $formatResult = true)
 | 
						|
    {
 | 
						|
        if (!$date || $date == '0000-00-00 00:00:00') {
 | 
						|
            return '';
 | 
						|
        }
 | 
						|
 | 
						|
        $timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
 | 
						|
        $format = Session::get(SESSION_DATETIME_FORMAT, DEFAULT_DATETIME_FORMAT);
 | 
						|
 | 
						|
        $dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $date);
 | 
						|
        $dateTime->setTimeZone(new DateTimeZone($timezone));
 | 
						|
 | 
						|
        return $formatResult ? $dateTime->format($format) : $dateTime;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $time
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function formatTime($time)
 | 
						|
    {
 | 
						|
        // http://stackoverflow.com/a/3172665
 | 
						|
        $f = ':';
 | 
						|
        return sprintf('%02d%s%02d%s%02d', floor($time/3600), $f, ($time/60)%60, $f, $time%60);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param bool $formatResult
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function today($formatResult = true)
 | 
						|
    {
 | 
						|
        $timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
 | 
						|
        $format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
 | 
						|
 | 
						|
        $date = date_create(null, new DateTimeZone($timezone));
 | 
						|
 | 
						|
        if ($formatResult) {
 | 
						|
            return $date->format($format);
 | 
						|
        } else {
 | 
						|
            return $date;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $name
 | 
						|
     * @param $type
 | 
						|
     * @param bool $url
 | 
						|
     */
 | 
						|
    public static function trackViewed($name, $type, $url = false)
 | 
						|
    {
 | 
						|
        if (!$url) {
 | 
						|
            $url = app('request')->url();
 | 
						|
        }
 | 
						|
 | 
						|
        $viewed = Session::get(RECENTLY_VIEWED);
 | 
						|
 | 
						|
        if (!$viewed) {
 | 
						|
            $viewed = [];
 | 
						|
        }
 | 
						|
 | 
						|
        $object = new stdClass();
 | 
						|
        $object->accountId = Auth::user()->account_id;
 | 
						|
        $object->url = $url;
 | 
						|
        $object->name = ucwords($type).': '.$name;
 | 
						|
 | 
						|
        $data = [];
 | 
						|
        $counts = [];
 | 
						|
 | 
						|
        for ($i = 0; $i<count($viewed); $i++) {
 | 
						|
            $item = $viewed[$i];
 | 
						|
 | 
						|
            if ($object->url == $item->url || $object->name == $item->name) {
 | 
						|
                continue;
 | 
						|
            }
 | 
						|
 | 
						|
            array_push($data, $item);
 | 
						|
 | 
						|
            if (isset($counts[$item->accountId])) {
 | 
						|
                $counts[$item->accountId]++;
 | 
						|
            } else {
 | 
						|
                $counts[$item->accountId] = 1;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        array_unshift($data, $object);
 | 
						|
 | 
						|
        if (isset($counts[Auth::user()->account_id]) && $counts[Auth::user()->account_id] > RECENTLY_VIEWED_LIMIT) {
 | 
						|
            array_pop($data);
 | 
						|
        }
 | 
						|
 | 
						|
        Session::put(RECENTLY_VIEWED, $data);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $str
 | 
						|
     *
 | 
						|
     * @return mixed|string
 | 
						|
     */
 | 
						|
    public static function processVariables($str)
 | 
						|
    {
 | 
						|
        if (!$str) {
 | 
						|
            return '';
 | 
						|
        }
 | 
						|
 | 
						|
        $variables = ['MONTH', 'QUARTER', 'YEAR'];
 | 
						|
        for ($i = 0; $i<count($variables); $i++) {
 | 
						|
            $variable = $variables[$i];
 | 
						|
            $regExp = '/:'.$variable.'[+-]?[\d]*/';
 | 
						|
            preg_match_all($regExp, $str, $matches);
 | 
						|
            $matches = $matches[0];
 | 
						|
            if (count($matches) == 0) {
 | 
						|
                continue;
 | 
						|
            }
 | 
						|
            usort($matches, function($a, $b) {
 | 
						|
                return strlen($b) - strlen($a);
 | 
						|
            });
 | 
						|
            foreach ($matches as $match) {
 | 
						|
                $offset = 0;
 | 
						|
                $addArray = explode('+', $match);
 | 
						|
                $minArray = explode('-', $match);
 | 
						|
                if (count($addArray) > 1) {
 | 
						|
                    $offset = intval($addArray[1]);
 | 
						|
                } elseif (count($minArray) > 1) {
 | 
						|
                    $offset = intval($minArray[1]) * -1;
 | 
						|
                }
 | 
						|
 | 
						|
                $val = self::getDatePart($variable, $offset);
 | 
						|
                $str = str_replace($match, $val, $str);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return $str;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $part
 | 
						|
     * @param $offset
 | 
						|
     *
 | 
						|
     * @return int|string|\Symfony\Component\Translation\TranslatorInterface
 | 
						|
     */
 | 
						|
    private static function getDatePart($part, $offset)
 | 
						|
    {
 | 
						|
        $offset = intval($offset);
 | 
						|
        if ($part == 'MONTH') {
 | 
						|
            return self::getMonth($offset);
 | 
						|
        } elseif ($part == 'QUARTER') {
 | 
						|
            return self::getQuarter($offset);
 | 
						|
        } elseif ($part == 'YEAR') {
 | 
						|
            return self::getYear($offset);
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $offset
 | 
						|
     *
 | 
						|
     * @return string|\Symfony\Component\Translation\TranslatorInterface
 | 
						|
     */
 | 
						|
    private static function getMonth($offset)
 | 
						|
    {
 | 
						|
        $months = ['january', 'february', 'march', 'april', 'may', 'june',
 | 
						|
            'july', 'august', 'september', 'october', 'november', 'december', ];
 | 
						|
 | 
						|
        $month = intval(date('n')) - 1;
 | 
						|
 | 
						|
        $month += $offset;
 | 
						|
        $month = $month % 12;
 | 
						|
 | 
						|
        if ($month < 0) {
 | 
						|
            $month += 12;
 | 
						|
        }
 | 
						|
 | 
						|
        return trans('texts.' . $months[$month]);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $offset
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    private static function getQuarter($offset)
 | 
						|
    {
 | 
						|
        $month = intval(date('n')) - 1;
 | 
						|
        $quarter = floor(($month + 3) / 3);
 | 
						|
        $quarter += $offset;
 | 
						|
        $quarter = $quarter % 4;
 | 
						|
        if ($quarter == 0) {
 | 
						|
            $quarter = 4;
 | 
						|
        }
 | 
						|
 | 
						|
        return 'Q'.$quarter;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $offset
 | 
						|
     *
 | 
						|
     * @return int
 | 
						|
     */
 | 
						|
    private static function getYear($offset)
 | 
						|
    {
 | 
						|
        $year = intval(date('Y'));
 | 
						|
 | 
						|
        return $year + $offset;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $entityType
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getEntityClass($entityType)
 | 
						|
    {
 | 
						|
        return 'App\\Models\\' . static::getEntityName($entityType);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $entityType
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getEntityName($entityType)
 | 
						|
    {
 | 
						|
        return ucwords(static::toCamelCase($entityType));
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $model
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getClientDisplayName($model)
 | 
						|
    {
 | 
						|
        if ($model->client_name) {
 | 
						|
            return $model->client_name;
 | 
						|
        } elseif ($model->first_name || $model->last_name) {
 | 
						|
            return $model->first_name.' '.$model->last_name;
 | 
						|
        } else {
 | 
						|
            return $model->email;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $model
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getVendorDisplayName($model)
 | 
						|
    {
 | 
						|
        if(is_null($model)) {
 | 
						|
            return '';
 | 
						|
        }
 | 
						|
 | 
						|
        if($model->vendor_name) {
 | 
						|
            return $model->vendor_name;
 | 
						|
        }
 | 
						|
 | 
						|
        return 'No vendor name';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $firstName
 | 
						|
     * @param $lastName
 | 
						|
     * @param $email
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getPersonDisplayName($firstName, $lastName, $email)
 | 
						|
    {
 | 
						|
        if ($firstName || $lastName) {
 | 
						|
            return $firstName.' '.$lastName;
 | 
						|
        } elseif ($email) {
 | 
						|
            return $email;
 | 
						|
        } else {
 | 
						|
            return trans('texts.guest');
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function generateLicense()
 | 
						|
    {
 | 
						|
        $parts = [];
 | 
						|
        for ($i = 0; $i<5; $i++) {
 | 
						|
            $parts[] = strtoupper(str_random(4));
 | 
						|
        }
 | 
						|
 | 
						|
        return implode('-', $parts);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param string $eventName
 | 
						|
     *
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function lookupEventId($eventName)
 | 
						|
    {
 | 
						|
        if ($eventName == 'create_client') {
 | 
						|
            return EVENT_CREATE_CLIENT;
 | 
						|
        } elseif ($eventName == 'create_invoice') {
 | 
						|
            return EVENT_CREATE_INVOICE;
 | 
						|
        } elseif ($eventName == 'create_quote') {
 | 
						|
            return EVENT_CREATE_QUOTE;
 | 
						|
        } elseif ($eventName == 'create_payment') {
 | 
						|
            return EVENT_CREATE_PAYMENT;
 | 
						|
        } elseif ($eventName == 'create_vendor') {
 | 
						|
            return EVENT_CREATE_VENDOR;
 | 
						|
        }
 | 
						|
 | 
						|
        return false;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $subscription
 | 
						|
     * @param $data
 | 
						|
     */
 | 
						|
    public static function notifyZapier($subscription, $data)
 | 
						|
    {
 | 
						|
        $curl = curl_init();
 | 
						|
        $jsonEncodedData = json_encode($data);
 | 
						|
 | 
						|
        $opts = [
 | 
						|
            CURLOPT_URL => $subscription->target_url,
 | 
						|
            CURLOPT_RETURNTRANSFER => true,
 | 
						|
            CURLOPT_CUSTOMREQUEST => 'POST',
 | 
						|
            CURLOPT_POST => 1,
 | 
						|
            CURLOPT_POSTFIELDS => $jsonEncodedData,
 | 
						|
            CURLOPT_HTTPHEADER  => ['Content-Type: application/json', 'Content-Length: '.strlen($jsonEncodedData)],
 | 
						|
        ];
 | 
						|
 | 
						|
        curl_setopt_array($curl, $opts);
 | 
						|
 | 
						|
        $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
 | 
						|
 | 
						|
        curl_close($curl);
 | 
						|
 | 
						|
        if ($status == 410) {
 | 
						|
            $subscription->delete();
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param int $count
 | 
						|
     *
 | 
						|
     * @return array
 | 
						|
     */
 | 
						|
    public static function getApiHeaders($count = 0)
 | 
						|
    {
 | 
						|
        return [
 | 
						|
          'Content-Type' => 'application/json',
 | 
						|
          'X-Total-Count' => $count,
 | 
						|
          'X-Ninja-Version' => NINJA_VERSION,
 | 
						|
        ];
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $value
 | 
						|
     *
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function isEmpty($value)
 | 
						|
    {
 | 
						|
        return !$value || $value == '0' || $value == '0.00' || $value == '0,00';
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $haystack
 | 
						|
     * @param $needle
 | 
						|
     *
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function startsWith($haystack, $needle)
 | 
						|
    {
 | 
						|
        return $needle === '' || strpos($haystack, $needle) === 0;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $haystack
 | 
						|
     * @param $needle
 | 
						|
     *
 | 
						|
     * @return bool
 | 
						|
     */
 | 
						|
    public static function endsWith($haystack, $needle)
 | 
						|
    {
 | 
						|
        return $needle === '' || substr($haystack, -strlen($needle)) === $needle;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $model
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getEntityRowClass($model)
 | 
						|
    {
 | 
						|
        $str = '';
 | 
						|
 | 
						|
        if (property_exists($model, 'is_deleted')) {
 | 
						|
            $str = $model->is_deleted || ($model->deleted_at && $model->deleted_at != '0000-00-00') ? 'DISABLED ' : '';
 | 
						|
 | 
						|
            if ($model->is_deleted) {
 | 
						|
                $str .= 'ENTITY_DELETED ';
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($model->deleted_at && $model->deleted_at != '0000-00-00') {
 | 
						|
            $str .= 'ENTITY_ARCHIVED ';
 | 
						|
        }
 | 
						|
 | 
						|
        return $str;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $output
 | 
						|
     * @param $data
 | 
						|
     * @param bool $headers
 | 
						|
     */
 | 
						|
    public static function exportData($output, $data, $headers = false)
 | 
						|
    {
 | 
						|
        if ($headers) {
 | 
						|
            fputcsv($output, $headers);
 | 
						|
        } elseif (count($data) > 0) {
 | 
						|
            fputcsv($output, array_keys($data[0]));
 | 
						|
        }
 | 
						|
 | 
						|
        foreach ($data as $record) {
 | 
						|
            fputcsv($output, $record);
 | 
						|
        }
 | 
						|
 | 
						|
        fwrite($output, "\n");
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $values
 | 
						|
     *
 | 
						|
     * @return bool|mixed
 | 
						|
     */
 | 
						|
    public static function getFirst($values)
 | 
						|
    {
 | 
						|
        if (is_array($values)) {
 | 
						|
            return count($values) ? $values[0] : false;
 | 
						|
        }
 | 
						|
 | 
						|
        return $values;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getSubdomainPlaceholder()
 | 
						|
    {
 | 
						|
        $parts = parse_url(SITE_URL);
 | 
						|
        $subdomain = '';
 | 
						|
        if (isset($parts['host'])) {
 | 
						|
            $host = explode('.', $parts['host']);
 | 
						|
            if (count($host) > 2) {
 | 
						|
                $subdomain = $host[0];
 | 
						|
            }
 | 
						|
        }
 | 
						|
        return $subdomain;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function getDomainPlaceholder()
 | 
						|
    {
 | 
						|
        $parts = parse_url(SITE_URL);
 | 
						|
        $domain = '';
 | 
						|
        if (isset($parts['host'])) {
 | 
						|
            $host = explode('.', $parts['host']);
 | 
						|
            if (count($host) > 2) {
 | 
						|
                array_shift($host);
 | 
						|
                $domain .= implode('.', $host);
 | 
						|
            } else {
 | 
						|
                $domain .= $parts['host'];
 | 
						|
            }
 | 
						|
        }
 | 
						|
        if (isset($parts['path'])) {
 | 
						|
            $domain .= $parts['path'];
 | 
						|
        }
 | 
						|
        return $domain;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param string $domain
 | 
						|
     * @param $subdomain
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function replaceSubdomain($domain, $subdomain)
 | 
						|
    {
 | 
						|
        $parsedUrl = parse_url($domain);
 | 
						|
        $host = explode('.', $parsedUrl['host']);
 | 
						|
        if (count($host) > 0) {
 | 
						|
            $oldSubdomain = $host[0];
 | 
						|
            $domain = str_replace("://{$oldSubdomain}.", "://{$subdomain}.", $domain);
 | 
						|
        }
 | 
						|
        return $domain;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param string $name
 | 
						|
     *
 | 
						|
     * @return array
 | 
						|
     */
 | 
						|
    public static function splitName($name)
 | 
						|
    {
 | 
						|
        $name = trim($name);
 | 
						|
        $lastName = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
 | 
						|
        $firstName = trim(preg_replace('#'.$lastName.'#', '', $name));
 | 
						|
        return [$firstName, $lastName];
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param string $string
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function decodePDF($string)
 | 
						|
    {
 | 
						|
        $string = str_replace('data:application/pdf;base64,', '', $string);
 | 
						|
        return base64_decode($string);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $city
 | 
						|
     * @param $state
 | 
						|
     * @param $postalCode
 | 
						|
     * @param $swap
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function cityStateZip($city, $state, $postalCode, $swap)
 | 
						|
    {
 | 
						|
        $str = $city;
 | 
						|
 | 
						|
        if ($state) {
 | 
						|
            if ($str) {
 | 
						|
                $str .= ', ';
 | 
						|
            }
 | 
						|
            $str .= $state;
 | 
						|
        }
 | 
						|
 | 
						|
        if ($swap) {
 | 
						|
            return $postalCode . ' ' . $str;
 | 
						|
        }
 | 
						|
 | 
						|
        return $str . ' ' . $postalCode;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $website
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function formatWebsite($website)
 | 
						|
    {
 | 
						|
        if (!$website) {
 | 
						|
            return '';
 | 
						|
        }
 | 
						|
 | 
						|
        $link = $website;
 | 
						|
        $title = $website;
 | 
						|
        $prefix = 'http://';
 | 
						|
 | 
						|
        if (strlen($link) > 7 && substr($link, 0, 7) === $prefix) {
 | 
						|
            $title = substr($title, 7);
 | 
						|
        } else {
 | 
						|
            $link = $prefix.$link;
 | 
						|
        }
 | 
						|
 | 
						|
        return link_to($link, $title, ['target' => '_blank']);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $adjustment
 | 
						|
     * @param $currencyId
 | 
						|
     * @param $countryId
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function wrapAdjustment($adjustment, $currencyId, $countryId)
 | 
						|
    {
 | 
						|
        $class = $adjustment <= 0 ? 'success' : 'default';
 | 
						|
        $adjustment = self::formatMoney($adjustment, $currencyId, $countryId);
 | 
						|
        return "<h4><div class=\"label label-{$class}\">$adjustment</div></h4>";
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param $entity1
 | 
						|
     * @param $entity2
 | 
						|
     *
 | 
						|
     * @return mixed
 | 
						|
     */
 | 
						|
    public static function copyContext($entity1, $entity2)
 | 
						|
    {
 | 
						|
        if (!$entity2) {
 | 
						|
            return $entity1;
 | 
						|
        }
 | 
						|
 | 
						|
        $fields = [
 | 
						|
            'contact_id',
 | 
						|
            'payment_id',
 | 
						|
            'invoice_id',
 | 
						|
            'credit_id',
 | 
						|
            'invitation_id'
 | 
						|
        ];
 | 
						|
 | 
						|
        $fields2 = $entity2->getAttributes();
 | 
						|
 | 
						|
        foreach ($fields as $field) {
 | 
						|
            if (isset($fields2[$field]) && $fields2[$field]) {
 | 
						|
                $entity1->$field = $entity2->$field;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        return $entity1;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param string $url
 | 
						|
     *
 | 
						|
     * @return string
 | 
						|
     */
 | 
						|
    public static function addHttp($url)
 | 
						|
    {
 | 
						|
        if (!preg_match('~^(?:f|ht)tps?://~i', $url)) {
 | 
						|
            $url = 'http://' . $url;
 | 
						|
        }
 | 
						|
 | 
						|
        return $url;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * @param AccountGateway|null $accountGateway
 | 
						|
     *
 | 
						|
     * @return WePay
 | 
						|
     */
 | 
						|
    public static function setupWePay(AccountGateway $accountGateway = null)
 | 
						|
    {
 | 
						|
        if (WePay::getEnvironment() == 'none') {
 | 
						|
            if (WEPAY_ENVIRONMENT == WEPAY_STAGE) {
 | 
						|
                WePay::useStaging(WEPAY_CLIENT_ID, WEPAY_CLIENT_SECRET);
 | 
						|
            } else {
 | 
						|
                WePay::useProduction(WEPAY_CLIENT_ID, WEPAY_CLIENT_SECRET);
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        if ($accountGateway) {
 | 
						|
            return new WePay($accountGateway->getConfig()->accessToken);
 | 
						|
        }
 | 
						|
 | 
						|
        return new WePay(null);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Gets an array of weekday names (in English)
 | 
						|
     *
 | 
						|
     * @see getTranslatedWeekdayNames()
 | 
						|
     *
 | 
						|
     * @return \Illuminate\Support\Collection
 | 
						|
     */
 | 
						|
    public static function getWeekdayNames()
 | 
						|
    {
 | 
						|
        return collect(static::$weekdayNames);
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * Gets an array of translated weekday names
 | 
						|
     *
 | 
						|
     * @return \Illuminate\Support\Collection
 | 
						|
     */
 | 
						|
    public static function getTranslatedWeekdayNames()
 | 
						|
    {
 | 
						|
        return collect(static::$weekdayNames)->transform(function ($day) {
 | 
						|
             return trans('texts.'.strtolower($day));
 | 
						|
        });
 | 
						|
    }
 | 
						|
}
 |