1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-08 20:22:42 +01:00
invoiceninja/app/Libraries/Utils.php

993 lines
27 KiB
PHP
Raw Normal View History

2015-03-16 22:45:25 +01:00
<?php namespace App\Libraries;
2015-03-24 08:25:31 +01:00
use Auth;
2015-03-26 06:13:31 +01:00
use Cache;
2015-03-17 02:30:56 +01:00
use DB;
2015-08-03 21:08:07 +02:00
use App;
2015-03-24 08:25:31 +01:00
use Schema;
2015-03-26 06:13:31 +01:00
use Session;
2015-03-27 07:06:14 +01:00
use Request;
2015-12-08 11:10:20 +01:00
use Exception;
2015-03-27 07:06:14 +01:00
use View;
use DateTimeZone;
2015-03-30 21:45:10 +02:00
use Input;
use Log;
2015-03-31 11:38:24 +02:00
use DateTime;
use stdClass;
2015-03-31 19:42:37 +02:00
use Carbon;
2015-03-31 11:38:24 +02:00
use App\Models\Currency;
2015-03-16 22:45:25 +01:00
class Utils
{
public static function isRegistered()
{
return Auth::check() && Auth::user()->registered;
}
public static function isConfirmed()
{
return Auth::check() && Auth::user()->confirmed;
}
public static function isDatabaseSetup()
{
try {
if (Schema::hasTable('accounts')) {
return true;
}
2015-12-08 11:10:20 +01:00
} catch (Exception $e) {
2015-03-16 22:45:25 +01:00
return false;
}
}
2015-09-29 20:19:18 +02:00
public static function isDownForMaintenance()
{
return file_exists(storage_path() . '/framework/down');
}
public static function isCron()
{
return php_sapi_name() == 'cli';
}
2016-05-04 08:53:43 +02:00
public static function isTravis()
{
return env('TRAVIS') == 'true';
}
2015-03-16 22:45:25 +01:00
public static function isNinja()
{
return self::isNinjaProd() || self::isNinjaDev();
}
public static function isNinjaProd()
{
2016-01-28 13:04:55 +01:00
if (Utils::isReseller()) {
return true;
}
2016-02-18 22:05:30 +01:00
return env('NINJA_PROD') == 'true';
2015-03-16 22:45:25 +01:00
}
public static function isNinjaDev()
{
2016-02-18 22:05:30 +01:00
return env('NINJA_DEV') == 'true';
2015-06-16 21:35:35 +02:00
}
2015-11-24 20:45:38 +01:00
public static function requireHTTPS()
{
2016-02-27 21:51:22 +01:00
if (Request::root() === 'http://ninja.dev' || Request::root() === 'http://ninja.dev:8000') {
return false;
}
2015-11-24 20:45:38 +01:00
return Utils::isNinjaProd() || (isset($_ENV['REQUIRE_HTTPS']) && $_ENV['REQUIRE_HTTPS'] == 'true');
}
2016-01-28 13:04:55 +01:00
public static function isReseller()
{
return Utils::getResllerType() ? true : false;
}
public static function getResllerType()
{
return isset($_ENV['RESELLER_TYPE']) ? $_ENV['RESELLER_TYPE'] : false;
}
2015-11-01 19:21:11 +01:00
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;
}
2015-06-16 21:35:35 +02:00
public static function allowNewAccounts()
{
return Utils::isNinja() || Auth::check();
2015-03-16 22:45:25 +01:00
}
public static function isPro()
{
return Auth::check() && Auth::user()->isPro();
}
public static function hasFeature($feature)
{
return Auth::check() && Auth::user()->hasFeature($feature);
}
2016-03-16 00:08:00 +01:00
public static function isAdmin()
{
return Auth::check() && Auth::user()->is_admin;
}
public static function hasPermission($permission, $requireAll = false)
{
return Auth::check() && Auth::user()->hasPermission($permission, $requireAll);
}
public static function hasAllPermissions($permission)
{
return Auth::check() && Auth::user()->hasPermissions($permission);
}
public static function isTrial()
{
return Auth::check() && Auth::user()->isTrial();
}
2015-08-03 21:08:07 +02:00
public static function isEnglish()
{
return App::getLocale() == 'en';
}
2016-03-03 21:24:27 +01:00
public static function getLocaleRegion()
{
$parts = explode('_', App::getLocale());
return count($parts) ? $parts[0] : 'en';
}
2015-08-03 21:08:07 +02:00
2015-03-16 22:45:25 +01:00
public static function getUserType()
{
if (Utils::isNinja()) {
return USER_TYPE_CLOUD_HOST;
} else {
return USER_TYPE_SELF_HOST;
}
}
public static function getDemoAccountId()
{
return isset($_ENV[DEMO_ACCOUNT_ID]) ? $_ENV[DEMO_ACCOUNT_ID] : false;
}
public static function getNewsFeedResponse($userType = false)
{
if (!$userType) {
$userType = Utils::getUserType();
}
$response = new stdClass();
$response->message = isset($_ENV["{$userType}_MESSAGE"]) ? $_ENV["{$userType}_MESSAGE"] : '';
$response->id = isset($_ENV["{$userType}_ID"]) ? $_ENV["{$userType}_ID"] : '';
$response->version = NINJA_VERSION;
return $response;
}
2015-11-11 17:24:48 +01:00
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;
2016-01-19 14:01:19 +01:00
2015-11-11 17:24:48 +01:00
return Request::url() == $last->url ? $penultimate->url : $last->url;
}
2015-03-16 22:45:25 +01:00
public static function getProLabel($feature)
{
if (Auth::check()
&& !Auth::user()->isPro()
&& $feature == ACCOUNT_ADVANCED_SETTINGS) {
return '&nbsp;<sup class="pro-label">PRO</sup>';
} else {
return '';
}
}
public static function basePath()
{
return substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], '/') + 1);
}
public static function trans($input)
{
$data = [];
foreach ($input as $field) {
if ($field == "checkbox") {
$data[] = $field;
2015-11-06 00:14:00 +01:00
} elseif ($field) {
2015-03-16 22:45:25 +01:00
$data[] = trans("texts.$field");
2015-11-06 00:14:00 +01:00
} else {
$data[] = '';
2015-03-16 22:45:25 +01:00
}
}
return $data;
}
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);
}
public static function getErrorString($exception)
{
2015-04-27 14:28:40 +02:00
$class = get_class($exception);
$code = method_exists($exception, 'getStatusCode') ? $exception->getStatusCode() : $exception->getCode();
return "***{$class}*** [{$code}] : {$exception->getFile()} [Line {$exception->getLine()}] => {$exception->getMessage()}";
2015-03-16 22:45:25 +01:00
}
2016-03-22 16:14:40 +01:00
public static function logError($error, $context = 'PHP', $info = false)
2015-03-16 22:45:25 +01:00
{
2015-12-08 11:10:20 +01:00
if ($error instanceof Exception) {
$error = self::getErrorString($error);
}
2015-03-16 22:45:25 +01:00
$count = Session::get('error_count', 0);
Session::put('error_count', ++$count);
2016-01-31 20:36:50 +01:00
if ($count > 200) {
2015-03-16 22:45:25 +01:00
return 'logged';
}
$data = [
'context' => $context,
'user_id' => Auth::check() ? Auth::user()->id : 0,
2015-11-16 20:07:23 +01:00
'account_id' => Auth::check() ? Auth::user()->account_id : 0,
2015-03-16 22:45:25 +01:00
'user_name' => Auth::check() ? Auth::user()->getDisplayName() : '',
2015-12-31 12:31:50 +01:00
'method' => Request::method(),
2015-03-16 22:45:25 +01:00
'url' => Input::get('url', Request::url()),
'user_agent' => isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '',
'ip' => Request::getClientIp(),
'count' => Session::get('error_count', 0),
];
2016-03-22 16:14:40 +01:00
if ($info) {
Log::info($error."\n", $data);
} else {
Log::error($error."\n", $data);
}
2015-03-16 22:45:25 +01:00
/*
Mail::queue('emails.error', ['message'=>$error.' '.json_encode($data)], function($message)
{
$message->to($email)->subject($subject);
});
*/
}
public static function parseFloat($value)
{
$value = preg_replace('/[^0-9\.\-]/', '', $value);
return floatval($value);
}
2015-11-29 11:41:32 +01:00
public static function parseInt($value)
{
$value = preg_replace('/[^0-9]/', '', $value);
return intval($value);
}
public static function getFromCache($id, $type) {
2016-03-07 16:22:20 +01:00
$cache = Cache::get($type);
if ( ! $cache) {
static::logError("Cache for {$type} is not set");
return null;
}
$data = $cache->filter(function($item) use ($id) {
return $item->id == $id;
});
2016-01-19 14:01:19 +01:00
return $data->first();
}
2016-01-21 23:29:10 +01:00
public static function formatMoney($value, $currencyId = false, $countryId = false, $showCode = false)
2015-03-16 22:45:25 +01:00
{
if (!$value) {
$value = 0;
}
2015-03-16 22:45:25 +01:00
if (!$currencyId) {
2015-06-10 10:34:20 +02:00
$currencyId = Session::get(SESSION_CURRENCY, DEFAULT_CURRENCY);
2015-03-16 22:45:25 +01:00
}
if (!$countryId && Auth::check()) {
$countryId = Auth::user()->account->country_id;
2015-05-05 11:48:23 +02:00
}
$currency = self::getFromCache($currencyId, 'currencies');
$thousand = $currency->thousand_separator;
$decimal = $currency->decimal_separator;
2016-01-18 15:35:05 +01:00
$code = $currency->code;
$swapSymbol = false;
2015-03-16 22:45:25 +01:00
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, $currency->precision, $decimal, $thousand);
$symbol = $currency->symbol;
2016-01-21 23:29:10 +01:00
if ($showCode || !$symbol) {
2016-01-18 15:35:05 +01:00
return "{$value} {$code}";
} elseif ($swapSymbol) {
return "{$value} " . trim($symbol);
} else {
return "{$symbol}{$value}";
2015-11-18 22:37:11 +01:00
}
2015-03-16 22:45:25 +01:00
}
public static function pluralize($string, $count)
{
$field = $count == 1 ? $string : $string.'s';
$string = trans("texts.$field", ['count' => $count]);
return $string;
}
2016-01-20 00:07:31 +01:00
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/
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';
} else {
return 'Unknown';
}
}
2015-03-16 22:45:25 +01:00
public static function toArray($data)
{
return json_decode(json_encode((array) $data), true);
}
2015-12-02 14:26:06 +01:00
public static function toSpaceCase($string)
2015-03-16 22:45:25 +01:00
{
2015-12-02 14:26:06 +01:00
return preg_replace('/([a-z])([A-Z])/s', '$1 $2', $string);
}
public static function toSnakeCase($string)
{
return preg_replace('/([a-z])([A-Z])/s', '$1_$2', $string);
}
public static function toCamelCase($string)
{
return lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $string))));
2015-03-16 22:45:25 +01:00
}
public static function timestampToDateTimeString($timestamp)
{
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
$format = Session::get(SESSION_DATETIME_FORMAT, DEFAULT_DATETIME_FORMAT);
return Utils::timestampToString($timestamp, $timezone, $format);
}
public static function timestampToDateString($timestamp)
{
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
return Utils::timestampToString($timestamp, $timezone, $format);
}
public static function dateToString($date)
{
2015-10-15 08:19:41 +02:00
if (!$date) {
return false;
}
2016-01-19 14:01:19 +01:00
2016-04-17 00:34:39 +02:00
if ($date instanceof DateTime) {
$dateTime = $date;
} else {
$dateTime = new DateTime($date);
}
2015-03-16 22:45:25 +01:00
$timestamp = $dateTime->getTimestamp();
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
return Utils::timestampToString($timestamp, false, $format);
}
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);
}
public static function toSqlDate($date, $formatResult = true)
{
if (!$date) {
return;
}
2015-03-30 21:45:10 +02:00
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
2015-08-07 08:14:29 +02:00
$dateTime = DateTime::createFromFormat($format, $date);
2015-03-16 22:45:25 +01:00
2016-01-07 23:56:06 +01:00
if(!$dateTime)
return $date;
else
return $formatResult ? $dateTime->format('Y-m-d') : $dateTime;
2015-03-16 22:45:25 +01:00
}
public static function fromSqlDate($date, $formatResult = true)
{
if (!$date || $date == '0000-00-00') {
return '';
}
2015-03-30 21:45:10 +02:00
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
2015-05-27 18:52:10 +02:00
$dateTime = DateTime::createFromFormat('Y-m-d', $date);
if(!$dateTime)
return $date;
else
return $formatResult ? $dateTime->format($format) : $dateTime;
2015-05-27 18:52:10 +02:00
}
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);
2015-05-27 18:52:10 +02:00
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $date);
$dateTime->setTimeZone(new DateTimeZone($timezone));
2015-03-16 22:45:25 +01:00
return $formatResult ? $dateTime->format($format) : $dateTime;
}
2015-10-05 19:48:16 +02:00
public static function formatTime($t)
{
// http://stackoverflow.com/a/3172665
$f = ':';
return sprintf("%02d%s%02d%s%02d", floor($t/3600), $f, ($t/60)%60, $f, $t%60);
}
2015-03-16 22:45:25 +01:00
public static function today($formatResult = true)
{
2015-03-30 21:45:10 +02:00
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
2015-03-16 22:45:25 +01:00
$date = date_create(null, new DateTimeZone($timezone));
if ($formatResult) {
return $date->format($format);
} else {
return $date;
}
}
public static function trackViewed($name, $type, $url = false)
{
if (!$url) {
$url = Request::url();
}
$viewed = Session::get(RECENTLY_VIEWED);
if (!$viewed) {
$viewed = [];
}
$object = new stdClass();
$object->accountId = Auth::user()->account_id;
2015-03-16 22:45:25 +01:00
$object->url = $url;
$object->name = ucwords($type).': '.$name;
$data = [];
$counts = [];
2015-03-16 22:45:25 +01:00
for ($i = 0; $i<count($viewed); $i++) {
$item = $viewed[$i];
if ($object->url == $item->url || $object->name == $item->name) {
continue;
}
2015-11-11 17:24:48 +01:00
array_push($data, $item);
if (isset($counts[$item->accountId])) {
$counts[$item->accountId]++;
} else {
$counts[$item->accountId] = 1;
}
2015-03-16 22:45:25 +01:00
}
array_unshift($data, $object);
2016-01-19 14:01:19 +01:00
if (isset($counts[Auth::user()->account_id]) && $counts[Auth::user()->account_id] > RECENTLY_VIEWED_LIMIT) {
2015-03-16 22:45:25 +01:00
array_pop($data);
}
Session::put(RECENTLY_VIEWED, $data);
}
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;
}
2015-05-27 18:52:10 +02:00
usort($matches, function($a, $b) {
return strlen($b) - strlen($a);
});
2015-03-16 22:45:25 +01:00
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 = Utils::getDatePart($variable, $offset);
$str = str_replace($match, $val, $str);
}
}
return $str;
}
private static function getDatePart($part, $offset)
{
$offset = intval($offset);
if ($part == 'MONTH') {
return Utils::getMonth($offset);
} elseif ($part == 'QUARTER') {
return Utils::getQuarter($offset);
} elseif ($part == 'YEAR') {
return Utils::getYear($offset);
}
}
private static function getMonth($offset)
{
2016-03-22 15:37:14 +01:00
$months = [ "january", "february", "march", "april", "may", "june",
"july", "august", "september", "october", "november", "december", ];
2015-03-16 22:45:25 +01:00
$month = intval(date('n')) - 1;
$month += $offset;
$month = $month % 12;
if ($month < 0) {
$month += 12;
}
2016-03-22 15:37:14 +01:00
return trans('texts.' . $months[$month]);
2015-03-16 22:45:25 +01:00
}
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;
}
private static function getYear($offset)
{
$year = intval(date('Y'));
return $year + $offset;
}
2016-04-28 14:16:33 +02:00
public static function getEntityClass($entityType)
{
return 'App\\Models\\' . static::getEntityName($entityType);
}
2015-03-16 22:45:25 +01:00
public static function getEntityName($entityType)
{
2016-05-02 15:12:37 +02:00
return ucwords(Utils::toCamelCase($entityType));
2015-03-16 22:45:25 +01:00
}
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;
}
}
2016-01-06 15:23:58 +01:00
public static function getVendorDisplayName($model)
{
2016-01-19 14:01:19 +01:00
if(is_null($model))
return '';
if($model->vendor_name)
return $model->vendor_name;
return 'No vendor name';
2016-01-06 15:23:58 +01:00
}
2016-01-19 14:01:19 +01:00
2015-10-28 20:22:07 +01:00
public static function getPersonDisplayName($firstName, $lastName, $email)
2015-03-16 22:45:25 +01:00
{
2015-10-28 20:22:07 +01:00
if ($firstName || $lastName) {
return $firstName.' '.$lastName;
} elseif ($email) {
return $email;
} else {
return trans('texts.guest');
2015-03-16 22:45:25 +01:00
}
}
public static function generateLicense()
{
$parts = [];
for ($i = 0; $i<5; $i++) {
$parts[] = strtoupper(str_random(4));
}
return implode('-', $parts);
}
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;
2016-01-06 15:23:58 +01:00
} elseif ($eventName == 'create_vendor') {
return EVENT_CREATE_VENDOR;
2016-02-16 16:30:09 +01:00
} else {
2015-03-16 22:45:25 +01:00
return false;
}
}
public static function notifyZapier($subscription, $data)
{
$curl = curl_init();
2016-02-16 16:30:09 +01:00
$jsonEncodedData = json_encode($data);
2015-10-02 10:32:13 +02:00
2015-03-16 22:45:25 +01:00
$opts = [
2015-07-02 22:21:29 +02:00
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)],
2015-03-16 22:45:25 +01:00
];
curl_setopt_array($curl, $opts);
$result = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($status == 410) {
$subscription->delete();
}
}
public static function getApiHeaders($count = 0)
{
return [
'Content-Type' => 'application/json',
//'Access-Control-Allow-Origin' => '*',
//'Access-Control-Allow-Methods' => 'GET',
//'Access-Control-Allow-Headers' => 'Origin, Content-Type, Accept, Authorization, X-Requested-With',
//'Access-Control-Allow-Credentials' => 'true',
'X-Total-Count' => $count,
2015-11-19 12:37:30 +01:00
'X-Ninja-Version' => NINJA_VERSION,
2015-03-16 22:45:25 +01:00
//'X-Rate-Limit-Limit' - The number of allowed requests in the current period
//'X-Rate-Limit-Remaining' - The number of remaining requests in the current period
//'X-Rate-Limit-Reset' - The number of seconds left in the current period,
];
}
2015-10-13 09:11:44 +02:00
public static function isEmpty($value)
{
2015-12-17 09:19:56 +01:00
return !$value || $value == '0' || $value == '0.00' || $value == '0,00';
2015-10-13 09:11:44 +02:00
}
2015-03-16 22:45:25 +01:00
public static function startsWith($haystack, $needle)
{
return $needle === "" || strpos($haystack, $needle) === 0;
}
public static function endsWith($haystack, $needle)
{
return $needle === "" || substr($haystack, -strlen($needle)) === $needle;
}
public static function getEntityRowClass($model)
{
2015-11-05 23:37:04 +01:00
$str = '';
2015-03-16 22:45:25 +01:00
2015-11-05 23:37:04 +01:00
if (property_exists($model, 'is_deleted')) {
$str = $model->is_deleted || ($model->deleted_at && $model->deleted_at != '0000-00-00') ? 'DISABLED ' : '';
2015-03-16 22:45:25 +01:00
2015-11-05 23:37:04 +01:00
if ($model->is_deleted) {
$str .= 'ENTITY_DELETED ';
}
}
2016-01-19 14:01:19 +01:00
2015-03-16 22:45:25 +01:00
if ($model->deleted_at && $model->deleted_at != '0000-00-00') {
$str .= 'ENTITY_ARCHIVED ';
}
return $str;
}
2015-04-30 19:54:19 +02:00
2016-02-23 22:32:39 +01:00
public static function exportData($output, $data, $headers = false)
2015-04-30 19:54:19 +02:00
{
2016-02-23 22:32:39 +01:00
if ($headers) {
fputcsv($output, $headers);
} elseif (count($data) > 0) {
2015-04-30 19:54:19 +02:00
fputcsv($output, array_keys($data[0]));
}
foreach ($data as $record) {
fputcsv($output, $record);
}
fwrite($output, "\n");
}
2016-01-19 14:01:19 +01:00
2015-10-13 09:11:44 +02:00
public static function getFirst($values)
{
2015-08-20 10:02:03 +02:00
if (is_array($values)) {
return count($values) ? $values[0] : false;
} else {
return $values;
}
}
2015-09-03 09:32:39 +02:00
// nouns in German and French should be uppercase
2015-10-13 09:11:44 +02:00
public static function transFlowText($key)
{
2015-09-03 09:32:39 +02:00
$str = trans("texts.$key");
if (!in_array(App::getLocale(), ['de', 'fr'])) {
$str = strtolower($str);
}
return $str;
}
2015-10-13 09:11:44 +02:00
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;
}
2015-10-13 09:11:44 +02:00
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;
}
2015-09-17 21:01:06 +02:00
2015-10-13 09:11:44 +02:00
public static function replaceSubdomain($domain, $subdomain)
{
2015-09-17 21:01:06 +02:00
$parsedUrl = parse_url($domain);
$host = explode('.', $parsedUrl['host']);
if (count($host) > 0) {
$oldSubdomain = $host[0];
$domain = str_replace("://{$oldSubdomain}.", "://{$subdomain}.", $domain);
}
return $domain;
}
2015-10-11 16:41:09 +02:00
2015-10-13 09:11:44 +02:00
public static function splitName($name)
{
2015-10-11 16:41:09 +02:00
$name = trim($name);
$lastName = (strpos($name, ' ') === false) ? '' : preg_replace('#.*\s([\w-]*)$#', '$1', $name);
2015-10-13 09:11:44 +02:00
$firstName = trim(preg_replace('#'.$lastName.'#', '', $name));
2015-10-11 16:41:09 +02:00
return array($firstName, $lastName);
}
2015-10-13 09:11:44 +02:00
public static function decodePDF($string)
{
$string = str_replace('data:application/pdf;base64,', '', $string);
2015-12-13 21:12:54 +01:00
return base64_decode($string);
2015-10-13 09:11:44 +02:00
}
2015-10-13 17:44:01 +02:00
public static function cityStateZip($city, $state, $postalCode, $swap)
{
$str = $city;
if ($state) {
if ($str) {
$str .= ', ';
}
$str .= $state;
}
if ($swap) {
return $postalCode . ' ' . $str;
} else {
return $str . ' ' . $postalCode;
}
}
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, array('target' => '_blank'));
}
public static function wrapAdjustment($adjustment, $currencyId, $countryId)
2015-10-13 17:44:01 +02:00
{
$class = $adjustment <= 0 ? 'success' : 'default';
$adjustment = Utils::formatMoney($adjustment, $currencyId, $countryId);
2015-10-13 17:44:01 +02:00
return "<h4><div class=\"label label-{$class}\">$adjustment</div></h4>";
}
2015-10-28 20:22:07 +01:00
public static function copyContext($entity1, $entity2)
{
if (!$entity2) {
return $entity1;
}
$fields = [
'contact_id',
'payment_id',
'invoice_id',
'credit_id',
'invitation_id'
];
$fields1 = $entity1->getAttributes();
$fields2 = $entity2->getAttributes();
foreach ($fields as $field) {
if (isset($fields2[$field]) && $fields2[$field]) {
$entity1->$field = $entity2->$field;
}
}
return $entity1;
}
2015-11-01 19:21:11 +01:00
2015-12-14 22:05:17 +01:00
public static function addHttp($url)
{
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
2016-01-19 14:01:19 +01:00
2015-12-14 22:05:17 +01:00
return $url;
}
2015-03-16 22:45:25 +01:00
}