From 854f6777ab11a5d9a9ede061eb483b2301fd7cb0 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Thu, 26 Feb 2015 21:24:49 +0200 Subject: [PATCH] Bug fixes --- app/controllers/InvoiceApiController.php | 25 +- app/libraries/utils.php | 1190 +++++++++--------- app/ninja/repositories/InvoiceRepository.php | 2 +- app/start/global.php | 12 +- app/views/emails/confirm_html.blade.php | 2 +- app/views/public/terms.blade.php | 26 + 6 files changed, 634 insertions(+), 623 deletions(-) diff --git a/app/controllers/InvoiceApiController.php b/app/controllers/InvoiceApiController.php index 10081da8ca37..bd488fd1ed7e 100644 --- a/app/controllers/InvoiceApiController.php +++ b/app/controllers/InvoiceApiController.php @@ -26,15 +26,20 @@ class InvoiceApiController extends Controller return Response::make($response, 200, $headers); } - /* - public function store() - { - $data = Input::all(); - $invoice = $this->invoiceRepo->save(false, $data, false); + /* + public function store() + { + if (!Utils::isPro()) { + return Redirect::to('/'); + } - $response = json_encode($invoice, JSON_PRETTY_PRINT); - $headers = Utils::getApiHeaders(); - return Response::make($response, 200, $headers); - } - */ + $data = Input::all(); + $invoice = $this->invoiceRepo->save(false, $data, false); + + $response = json_encode($invoice, JSON_PRETTY_PRINT); + $headers = Utils::getApiHeaders(); + + return Response::make($response, 200, $headers); + } + */ } diff --git a/app/libraries/utils.php b/app/libraries/utils.php index 1884713ee1ff..89f0427e921f 100755 --- a/app/libraries/utils.php +++ b/app/libraries/utils.php @@ -1,618 +1,600 @@ registered; - } - - public static function isConfirmed() - { - return Auth::check() && Auth::user()->confirmed; - } - - public static function isDatabaseSetup() - { - try - { - if (Schema::hasTable('accounts')) - { - return true; - } - } - catch (Exception $e) - { - return false; - } - } - - public static function isProd() - { - return App::environment() == ENV_PRODUCTION; - } - - public static function isNinja() - { - return self::isNinjaProd() || self::isNinjaDev(); - } - - public static function isNinjaProd() - { - return isset($_ENV['NINJA_PROD']) && $_ENV['NINJA_PROD']; - } - - public static function isNinjaDev() - { - return isset($_ENV['NINJA_DEV']) && $_ENV['NINJA_DEV']; - } - - public static function isPro() - { - return Auth::check() && Auth::user()->isPro(); - } - - 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 isDemo() - { - return Auth::check() && Auth::user()->isDemo(); - } - - 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; - } - - public static function getProLabel($feature) - { - if (Auth::check() - && !Auth::user()->isPro() - && $feature == ACCOUNT_ADVANCED_SETTINGS) - { - return ' PRO'; - } - 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; - } - else - { - $data[] = trans("texts.$field"); - } - } - - 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) - { - return "{$exception->getFile()} [Line {$exception->getLine()}] => {$exception->getMessage()}"; - } - - public static function logError($error, $context = 'PHP') - { - $count = Session::get('error_count', 0); - Session::put('error_count', ++$count); - if ($count > 100) return 'logged'; - - $data = [ - 'context' => $context, - 'user_id' => Auth::check() ? Auth::user()->id : 0, - 'user_name' => Auth::check() ? Auth::user()->getDisplayName() : '', - '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) - ]; - - Log::error($error."\n", $data); - - /* - 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); - } - - public static function formatPhoneNumber($phoneNumber) - { - $phoneNumber = preg_replace('/[^0-9a-zA-Z]/','',$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 && in_array(substr($phoneNumber, 0, 3), array(653, 656, 658, 659))) { - /** - * SG country code are 653, 656, 658, 659 - * US area code consist of 650, 651 and 657 - * @see http://en.wikipedia.org/wiki/Telephone_numbers_in_Singapore#Numbering_plan - * @see http://www.bennetyee.org/ucsd-pages/area.html - */ - $countryCode = substr($phoneNumber, 0, 2); - $nextFour = substr($phoneNumber, 2, 4); - $lastFour = substr($phoneNumber, 6, 4); - - $phoneNumber = '+'.$countryCode.' '.$nextFour.' '.$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; - } - - public static function formatMoney($value, $currencyId = false) - { - if (!$currencyId) - { - $currencyId = Session::get(SESSION_CURRENCY); - } - - $currency = Currency::remember(DEFAULT_QUERY_CACHE)->find($currencyId); - - if (!$currency) - { - $currency = Currency::remember(DEFAULT_QUERY_CACHE)->find(1); - } - - return $currency->symbol . number_format($value, $currency->precision, $currency->decimal_separator, $currency->thousand_separator); - } - - public static function pluralize($string, $count) - { - $field = $count == 1 ? $string : $string . 's'; - $string = trans("texts.$field", ['count' => $count]); - return $string; - } - - public static function toArray($data) - { - return json_decode(json_encode((array) $data), true); - } - - public static function toSpaceCase($camelStr) - { - return preg_replace('/([a-z])([A-Z])/s','$1 $2', $camelStr); - } - - 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) { - $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) - { - 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 null; - } - - $timezone = Session::get(SESSION_TIMEZONE); - $format = Session::get(SESSION_DATE_FORMAT); - - - $dateTime = DateTime::createFromFormat($format, $date, new DateTimeZone($timezone)); - return $formatResult ? $dateTime->format('Y-m-d') : $dateTime; - } - - public static function fromSqlDate($date, $formatResult = true) - { - if (!$date || $date == '0000-00-00') - { - return ''; - } - - $timezone = Session::get(SESSION_TIMEZONE); - $format = Session::get(SESSION_DATE_FORMAT); - - $dateTime = DateTime::createFromFormat('Y-m-d', $date, new DateTimeZone($timezone)); - return $formatResult ? $dateTime->format($format) : $dateTime; - } - - 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; - } - } - - 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->url = $url; - $object->name = ucwords($type) . ': ' . $name; - - $data = []; - - for ($i=0; $iurl == $item->url || $object->name == $item->name) - { - continue; - } - - array_unshift($data, $item); - } - - array_unshift($data, $object); - - if (count($data) > RECENTLY_VIEWED_LIMIT) - { - array_pop($data); - } - - Session::put(RECENTLY_VIEWED, $data); - } - - public static function processVariables($str) - { - if (!$str) { - return ''; - } - - $variables = ['MONTH', 'QUARTER', 'YEAR']; - for ($i=0; $i 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; - } - - 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; - - if ($month < 0) - { - $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; - } - - public static function getEntityName($entityType) - { - return ucwords(str_replace('_', ' ', $entityType)); - } - - 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; - } - } - - public static function encodeActivity($person = null, $action, $entity = null, $otherPerson = null) - { - $person = $person ? $person->getDisplayName() : 'System'; - $entity = $entity ? '[' . $entity->getActivityKey() . ']' : ''; - $otherPerson = $otherPerson ? 'to ' . $otherPerson->getDisplayName() : ''; - - 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; - } - - public static function generateLicense() { - $parts = []; - for ($i=0; $i<5; $i++) { - $parts[] = strtoupper(str_random(4)); - } - return join('-', $parts); - } - - public static function lookupEventId($eventName) - { - if ($eventName == 'create_client') { - return EVENT_CREATE_CLIENT; - } else if ($eventName == 'create_invoice') { - return EVENT_CREATE_INVOICE; - } else if ($eventName == 'create_quote') { - return EVENT_CREATE_QUOTE; - } else if ($eventName == 'create_payment') { - return EVENT_CREATE_PAYMENT; - } else { - return false; - } - } - - public static function notifyZapier($subscription, $data) { - $curl = curl_init(); - - $jsonEncodedData = json_encode($data->toJson()); - $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); - - $result = curl_exec($curl); - $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); - - curl_close($curl); - - if ($status == 410) + public static function isRegistered() { - $subscription->delete(); + return Auth::check() && Auth::user()->registered; } - } - public static function remapPublicIds($data) { - foreach ($data as $index => $record) { - if (!isset($data[$index]['public_id'])) { - continue; - } - $data[$index]['id'] = $data[$index]['public_id']; - unset($data[$index]['public_id']); - - foreach ($record as $key => $val) { - if (is_array($val)) { - $data[$index][$key] = Utils::remapPublicIds($val); - } - } + public static function isConfirmed() + { + return Auth::check() && Auth::user()->confirmed; } - return $data; - } - 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, - //'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, - ]; - } + public static function isDatabaseSetup() + { + try { + if (Schema::hasTable('accounts')) { + return true; + } + } catch (Exception $e) { + return false; + } + } - public static function startsWith($haystack, $needle) - { - return $needle === "" || strpos($haystack, $needle) === 0; - } + public static function isProd() + { + return App::environment() == ENV_PRODUCTION; + } - public static function endsWith($haystack, $needle) - { - return $needle === "" || substr($haystack, -strlen($needle)) === $needle; - } + public static function isNinja() + { + return self::isNinjaProd() || self::isNinjaDev(); + } - public static function getEntityRowClass($model) - { - $str = $model->is_deleted || ($model->deleted_at && $model->deleted_at != '0000-00-00') ? 'DISABLED ' : ''; + public static function isNinjaProd() + { + return isset($_ENV['NINJA_PROD']) && $_ENV['NINJA_PROD']; + } - if ($model->is_deleted) - { - $str .= 'ENTITY_DELETED '; - } + public static function isNinjaDev() + { + return isset($_ENV['NINJA_DEV']) && $_ENV['NINJA_DEV']; + } - if ($model->deleted_at && $model->deleted_at != '0000-00-00') - { - $str .= 'ENTITY_ARCHIVED '; - } + public static function isPro() + { + return Auth::check() && Auth::user()->isPro(); + } - return $str; - } -} \ No newline at end of file + 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 isDemo() + { + return Auth::check() && Auth::user()->isDemo(); + } + + 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; + } + + public static function getProLabel($feature) + { + if (Auth::check() + && !Auth::user()->isPro() + && $feature == ACCOUNT_ADVANCED_SETTINGS) { + return ' PRO'; + } 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; + } else { + $data[] = trans("texts.$field"); + } + } + + 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) + { + return "{$exception->getFile()} [Line {$exception->getLine()}] => {$exception->getMessage()}"; + } + + public static function logError($error, $context = 'PHP') + { + $count = Session::get('error_count', 0); + Session::put('error_count', ++$count); + if ($count > 100) { + return 'logged'; + } + + $data = [ + 'context' => $context, + 'user_id' => Auth::check() ? Auth::user()->id : 0, + 'user_name' => Auth::check() ? Auth::user()->getDisplayName() : '', + '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), + ]; + + Log::error($error."\n", $data); + + /* + 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); + } + + public static function formatPhoneNumber($phoneNumber) + { + $phoneNumber = preg_replace('/[^0-9a-zA-Z]/', '', $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; + } elseif (strlen($phoneNumber) == 10 && in_array(substr($phoneNumber, 0, 3), array(653, 656, 658, 659))) { + /** + * SG country code are 653, 656, 658, 659 + * US area code consist of 650, 651 and 657 + * @see http://en.wikipedia.org/wiki/Telephone_numbers_in_Singapore#Numbering_plan + * @see http://www.bennetyee.org/ucsd-pages/area.html + */ + $countryCode = substr($phoneNumber, 0, 2); + $nextFour = substr($phoneNumber, 2, 4); + $lastFour = substr($phoneNumber, 6, 4); + + $phoneNumber = '+'.$countryCode.' '.$nextFour.' '.$lastFour; + } elseif (strlen($phoneNumber) == 10) { + $areaCode = substr($phoneNumber, 0, 3); + $nextThree = substr($phoneNumber, 3, 3); + $lastFour = substr($phoneNumber, 6, 4); + + $phoneNumber = '('.$areaCode.') '.$nextThree.'-'.$lastFour; + } elseif (strlen($phoneNumber) == 7) { + $nextThree = substr($phoneNumber, 0, 3); + $lastFour = substr($phoneNumber, 3, 4); + + $phoneNumber = $nextThree.'-'.$lastFour; + } + + return $phoneNumber; + } + + public static function formatMoney($value, $currencyId = false) + { + if (!$currencyId) { + $currencyId = Session::get(SESSION_CURRENCY); + } + + $currency = Currency::remember(DEFAULT_QUERY_CACHE)->find($currencyId); + + if (!$currency) { + $currency = Currency::remember(DEFAULT_QUERY_CACHE)->find(1); + } + + return $currency->symbol.number_format($value, $currency->precision, $currency->decimal_separator, $currency->thousand_separator); + } + + public static function pluralize($string, $count) + { + $field = $count == 1 ? $string : $string.'s'; + $string = trans("texts.$field", ['count' => $count]); + + return $string; + } + + public static function toArray($data) + { + return json_decode(json_encode((array) $data), true); + } + + public static function toSpaceCase($camelStr) + { + return preg_replace('/([a-z])([A-Z])/s', '$1 $2', $camelStr); + } + + 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) + { + $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) + { + 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; + } + + $timezone = Session::get(SESSION_TIMEZONE); + $format = Session::get(SESSION_DATE_FORMAT); + + $dateTime = DateTime::createFromFormat($format, $date, new DateTimeZone($timezone)); + + return $formatResult ? $dateTime->format('Y-m-d') : $dateTime; + } + + public static function fromSqlDate($date, $formatResult = true) + { + if (!$date || $date == '0000-00-00') { + return ''; + } + + $timezone = Session::get(SESSION_TIMEZONE); + $format = Session::get(SESSION_DATE_FORMAT); + + $dateTime = DateTime::createFromFormat('Y-m-d', $date, new DateTimeZone($timezone)); + + return $formatResult ? $dateTime->format($format) : $dateTime; + } + + 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; + } + } + + 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->url = $url; + $object->name = ucwords($type).': '.$name; + + $data = []; + + for ($i = 0; $iurl == $item->url || $object->name == $item->name) { + continue; + } + + array_unshift($data, $item); + } + + array_unshift($data, $object); + + if (count($data) > RECENTLY_VIEWED_LIMIT) { + array_pop($data); + } + + Session::put(RECENTLY_VIEWED, $data); + } + + public static function processVariables($str) + { + if (!$str) { + return ''; + } + + $variables = ['MONTH', 'QUARTER', 'YEAR']; + for ($i = 0; $i 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) + { + $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 $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; + } + + public static function getEntityName($entityType) + { + return ucwords(str_replace('_', ' ', $entityType)); + } + + 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; + } + } + + public static function encodeActivity($person = null, $action, $entity = null, $otherPerson = null) + { + $person = $person ? $person->getDisplayName() : 'System'; + $entity = $entity ? '['.$entity->getActivityKey().']' : ''; + $otherPerson = $otherPerson ? 'to '.$otherPerson->getDisplayName() : ''; + + 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; + } + + 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; + } else { + return false; + } + } + + public static function notifyZapier($subscription, $data) + { + $curl = curl_init(); + + $jsonEncodedData = json_encode($data->toJson()); + $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); + + $result = curl_exec($curl); + $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); + + curl_close($curl); + + if ($status == 410) { + $subscription->delete(); + } + } + + public static function remapPublicIds($data) + { + foreach ($data as $index => $record) { + if (!isset($data[$index]['public_id'])) { + continue; + } + $data[$index]['id'] = $data[$index]['public_id']; + unset($data[$index]['public_id']); + + foreach ($record as $key => $val) { + if (is_array($val)) { + $data[$index][$key] = Utils::remapPublicIds($val); + } + } + } + + return $data; + } + + 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, + //'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, + ]; + } + + 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) + { + $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; + } +} diff --git a/app/ninja/repositories/InvoiceRepository.php b/app/ninja/repositories/InvoiceRepository.php index c099706fc717..c964d558a907 100755 --- a/app/ninja/repositories/InvoiceRepository.php +++ b/app/ninja/repositories/InvoiceRepository.php @@ -220,7 +220,7 @@ class InvoiceRepository $invoice->is_quote = true; } } - + $invoice->client_id = $data['client_id']; $invoice->discount = round(Utils::parseFloat($data['discount']), 2); $invoice->is_amount_discount = $data['is_amount_discount'] ? true : false; diff --git a/app/start/global.php b/app/start/global.php index c4a93c78d2e2..616e8fcebb8f 100755 --- a/app/start/global.php +++ b/app/start/global.php @@ -58,13 +58,11 @@ $monolog->pushHandler(new Monolog\Handler\SyslogHandler('intranet', 'user', Logg App::error(function(Exception $exception, $code) { - if (Utils::isNinjaProd()) - { - Utils::logError($code . ' ' . Utils::getErrorString($exception)); - return Response::view('error', ['hideHeader' => true, 'error' => "A {$code} error occurred."], $code); - } - else - { + Utils::logError($code . ' ' . Utils::getErrorString($exception)); + + if (Utils::isNinjaProd()) { + return Response::view('error', ['hideHeader' => true, 'error' => "A {$code} error occurred."], $code); + } else { return null; } }); diff --git a/app/views/emails/confirm_html.blade.php b/app/views/emails/confirm_html.blade.php index c7ad995255ba..c482b0fa841a 100755 --- a/app/views/emails/confirm_html.blade.php +++ b/app/views/emails/confirm_html.blade.php @@ -20,7 +20,7 @@

{{ trans('texts.confirmation_header') }}

- {{ $invitationMessage . trans('texts.confirmation_message') }} + {{ $invitationMessage . trans('texts.confirmation_message') }}
{{{ URL::to("user/confirm/{$user->confirmation_code}") }}} diff --git a/app/views/public/terms.blade.php b/app/views/public/terms.blade.php index 4f99598cada6..c509ddde5716 100644 --- a/app/views/public/terms.blade.php +++ b/app/views/public/terms.blade.php @@ -2,6 +2,28 @@ @section('content') + + + +

+

 

+ +
+
+ +
@@ -148,4 +170,8 @@

 

 

+
+
+
+ @stop \ No newline at end of file