1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 13:12:50 +01:00
invoiceninja/app/libraries/utils.php

313 lines
7.3 KiB
PHP
Raw Normal View History

2013-12-07 21:33:07 +01:00
<?php
class Utils
{
2014-01-02 00:12:33 +01:00
public static function fatalError($error = false)
2013-12-31 20:49:54 +01:00
{
2014-01-02 00:12:33 +01:00
if (!$error)
{
$error = "An error occurred, please try again later";
}
2013-12-31 20:49:54 +01:00
Log::error($error);
2014-01-02 00:12:33 +01:00
2013-12-31 20:49:54 +01:00
return View::make('error')->with('error', $error);
}
2013-12-07 21:33:07 +01:00
public static function formatPhoneNumber($phoneNumber)
{
$phoneNumber = preg_replace('/[^0-9]/','',$phoneNumber);
if (!$phoneNumber) {
return '';
}
if(strlen($phoneNumber) > 10) {
$countryCode = substr($phoneNumber, 0, strlen($phoneNumber)-10);
$areaCode = substr($phoneNumber, -10, 3);
$nextThree = substr($phoneNumber, -7, 3);
$lastFour = substr($phoneNumber, -4, 4);
$phoneNumber = '+'.$countryCode.' ('.$areaCode.') '.$nextThree.'-'.$lastFour;
}
else if(strlen($phoneNumber) == 10) {
$areaCode = substr($phoneNumber, 0, 3);
$nextThree = substr($phoneNumber, 3, 3);
$lastFour = substr($phoneNumber, 6, 4);
$phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour;
}
else if(strlen($phoneNumber) == 7) {
$nextThree = substr($phoneNumber, 0, 3);
$lastFour = substr($phoneNumber, 3, 4);
$phoneNumber = $nextThree.'-'.$lastFour;
}
return $phoneNumber;
}
2013-12-29 18:40:11 +01:00
public static function formatMoney($value, $currencyId)
{
$currency = Currency::find($currencyId);
if (!$currency) {
$currency = Currency::find(1);
}
return $currency->symbol . number_format($value, $currency->precision, $currency->decimal_separator, $currency->thousand_separator);
}
2013-12-08 14:32:49 +01:00
public static function pluralize($string, $count)
2013-12-07 21:33:07 +01:00
{
$string = str_replace('?', $count, $string);
return $count == 1 ? $string : $string . 's';
}
2013-12-08 14:32:49 +01:00
public static function toArray($data)
2013-12-07 21:33:07 +01:00
{
return json_decode(json_encode((array) $data), true);
}
2013-12-08 14:32:49 +01:00
public static function toSpaceCase($camelStr)
2013-12-07 21:33:07 +01:00
{
return preg_replace('/([a-z])([A-Z])/s','$1 $2', $camelStr);
}
2013-12-08 14:32:49 +01:00
public static function timestampToDateTimeString($timestamp) {
2013-12-15 13:55:50 +01:00
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
$format = Session::get(SESSION_DATETIME_FORMAT, DEFAULT_DATETIME_FORMAT);
return Utils::timestampToString($timestamp, $timezone, $format);
2013-12-07 21:33:07 +01:00
}
2013-12-08 14:32:49 +01:00
public static function timestampToDateString($timestamp) {
2013-12-15 13:55:50 +01:00
$timezone = Session::get(SESSION_TIMEZONE, DEFAULT_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT, DEFAULT_DATE_FORMAT);
return Utils::timestampToString($timestamp, $timezone, $format);
}
2013-12-16 22:27:32 +01:00
public static function dateToString($date) {
$dateTime = new DateTime($date);
$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)
2013-12-15 13:55:50 +01:00
{
2013-12-24 07:22:16 +01:00
if (!$timestamp) {
return '';
}
2013-12-16 22:27:32 +01:00
$date = Carbon::createFromTimeStamp($timestamp);
if ($timezone) {
$date->tz = $timezone;
}
2013-12-07 21:33:07 +01:00
if ($date->year < 1900) {
return '';
}
2013-12-15 13:55:50 +01:00
return $date->format($format);
2013-12-16 22:27:32 +01:00
}
2013-12-07 21:33:07 +01:00
2013-12-08 14:32:49 +01:00
public static function toSqlDate($date)
2013-12-07 21:33:07 +01:00
{
if (!$date)
{
2013-12-09 10:38:49 +01:00
return null;
2013-12-07 21:33:07 +01:00
}
2014-01-01 00:50:13 +01:00
$timezone = Session::get(SESSION_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT);
2013-12-25 22:34:42 +01:00
2014-01-01 00:50:13 +01:00
return DateTime::createFromFormat($format, $date, new DateTimeZone($timezone));
2013-12-07 21:33:07 +01:00
}
2013-12-08 14:32:49 +01:00
public static function fromSqlDate($date)
2013-12-07 21:33:07 +01:00
{
if (!$date || $date == '0000-00-00')
{
return '';
}
2014-01-01 00:50:13 +01:00
$timezone = Session::get(SESSION_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT);
2013-12-25 22:34:42 +01:00
2014-01-01 00:50:13 +01:00
return DateTime::createFromFormat('Y-m-d', $date, new DateTimeZone($timezone))->format($format);
}
public static function today($formatResult = true)
{
$timezone = Session::get(SESSION_TIMEZONE);
$format = Session::get(SESSION_DATE_FORMAT);
$date = date_create(null, new DateTimeZone($timezone));
if ($formatResult)
{
return $date->format($format);
}
else
{
return $date;
}
2013-12-07 21:33:07 +01:00
}
2013-12-25 22:34:42 +01:00
public static function trackViewed($name, $type, $url = false)
2013-12-07 21:33:07 +01:00
{
2013-12-25 22:34:42 +01:00
if (!$url)
{
$url = Request::url();
}
2013-12-07 21:33:07 +01:00
$viewed = Session::get(RECENTLY_VIEWED);
if (!$viewed)
{
$viewed = [];
}
$object = new stdClass;
$object->url = $url;
$object->name = ucwords($type) . ': ' . $name;
for ($i=0; $i<count($viewed); $i++)
{
$item = $viewed[$i];
if ($object->url == $item->url)
{
array_splice($viewed, $i, 1);
break;
}
}
array_unshift($viewed, $object);
if (count($viewed) > RECENTLY_VIEWED_LIMIT)
{
array_pop($viewed);
}
Session::put(RECENTLY_VIEWED, $viewed);
}
2013-12-10 18:18:35 +01:00
public static function processVariables($str)
{
if (!$str) {
return '';
}
$variables = ['MONTH', 'QUARTER', 'YEAR'];
for ($i=0; $i<count($variables); $i++)
{
$variable = $variables[$i];
2013-12-11 12:11:59 +01:00
$regExp = '/:' . $variable . '[+-]?[\d]*/';
2013-12-10 18:18:35 +01:00
preg_match_all($regExp, $str, $matches);
$matches = $matches[0];
if (count($matches) == 0) {
continue;
}
foreach ($matches as $match) {
$offset = 0;
$addArray = explode('+', $match);
$minArray = explode('-', $match);
if (count($addArray) > 1) {
$offset = intval($addArray[1]);
} else if (count($minArray) > 1) {
$offset = intval($minArray[1]) * -1;
}
$val = Utils::getDatePart($variable, $offset);
$str = str_replace($match, $val, $str);
}
}
return $str;
}
2013-12-07 21:33:07 +01:00
2013-12-10 18:18:35 +01:00
private static function getDatePart($part, $offset)
{
$offset = intval($offset);
if ($part == 'MONTH') {
return Utils::getMonth($offset);
} else if ($part == 'QUARTER') {
return Utils::getQuarter($offset);
} else if ($part == 'YEAR') {
return Utils::getYear($offset);
}
}
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;
return $months[$month];
}
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;
}
2013-12-10 23:10:43 +01:00
public static function getEntityName($entityType)
{
return ucwords(str_replace('_', ' ', $entityType));
}
2013-12-11 21:33:44 +01:00
2013-12-30 21:17:45 +01:00
public static function getClientDisplayName($model)
{
if ($model->client_name)
{
return $model->client_name;
}
else if ($model->first_name || $model->last_name)
{
return $model->first_name . ' ' . $model->last_name;
}
else
{
return $model->email;
}
}
2014-01-08 21:09:47 +01:00
public static function encodeActivity($person = null, $action, $entity = null, $otherPerson = null)
{
$person = $person ? $person->getFullName() : '<i>System</i>';
$entity = $entity ? '[' . $entity->getKey() . ']' : '';
$otherPerson = $otherPerson ? 'to ' . $otherPerson->getFullName() : '';
return trim("$person $action $entity $otherPerson");
}
public static function decodeActivity($message)
{
$pattern = '/\[([\w]*):([\d]*):(.*)\]/i';
preg_match($pattern, $message, $matches);
if (count($matches) > 0)
{
$match = $matches[0];
$type = $matches[1];
$publicId = $matches[2];
$name = $matches[3];
$link = link_to($type . 's/' . $publicId, $name);
$message = str_replace($match, "$type $link", $message);
}
return $message;
}
2013-12-07 21:33:07 +01:00
}