1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/Providers/AppServiceProvider.php

215 lines
7.3 KiB
PHP
Raw Normal View History

2015-03-12 01:44:39 +01:00
<?php namespace App\Providers;
2015-06-03 19:55:48 +02:00
use Auth;
use Utils;
2016-03-02 14:36:42 +01:00
use Form;
2015-06-03 19:55:48 +02:00
use URL;
use Request;
use Validator;
2015-03-12 01:44:39 +01:00
use Illuminate\Support\ServiceProvider;
/**
* Class AppServiceProvider
*/
class AppServiceProvider extends ServiceProvider
{
2015-03-12 01:44:39 +01:00
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
Form::macro('image_data', function($image, $contents = false) {
if(!$contents){
$contents = file_get_contents($image);
}
else{
$contents = $image;
}
return 'data:image/jpeg;base64,' . base64_encode($contents);
2016-03-02 14:36:42 +01:00
});
2016-08-23 19:28:45 +02:00
Form::macro('nav_link', function($url, $text) {
2016-08-24 07:49:28 +02:00
//$class = ( Request::is($url) || Request::is($url.'/*') || Request::is($url2.'/*') ) ? ' class="active"' : '';
$class = ( Request::is($url) || Request::is($url.'/*') ) ? ' class="active"' : '';
2016-08-23 19:28:45 +02:00
$title = trans("texts.$text") . Utils::getProLabel($text);
return '<li'.$class.'><a href="'.URL::to($url).'">'.$title.'</a></li>';
2015-06-03 19:55:48 +02:00
});
2016-03-02 14:36:42 +01:00
Form::macro('tab_link', function($url, $text, $active = false) {
2015-06-03 19:55:48 +02:00
$class = $active ? ' class="active"' : '';
return '<li'.$class.'><a href="'.URL::to($url).'" data-toggle="tab">'.$text.'</a></li>';
});
2016-03-02 14:36:42 +01:00
Form::macro('menu_link', function($type) {
2015-06-03 19:55:48 +02:00
$types = $type.'s';
$Type = ucfirst($type);
$Types = ucfirst($types);
2015-11-11 17:24:48 +01:00
$class = ( Request::is($types) || Request::is('*'.$type.'*')) && !Request::is('*settings*') ? ' active' : '';
2015-06-03 19:55:48 +02:00
2016-08-23 19:28:45 +02:00
return '<li class="dropdown '.$class.'">
<a href="'.URL::to($types).'" class="dropdown-toggle">'.trans("texts.$types").'</a>
</li>';
2015-06-03 19:55:48 +02:00
});
2016-03-02 14:36:42 +01:00
Form::macro('flatButton', function($label, $color) {
2015-12-02 14:26:06 +01:00
return '<input type="button" value="' . trans("texts.{$label}") . '" style="background-color:' . $color . ';border:0 none;border-radius:5px;padding:12px 40px;margin:0 6px;cursor:hand;display:inline-block;font-size:14px;color:#fff;text-transform:none;font-weight:bold;"/>';
});
2016-03-02 14:36:42 +01:00
Form::macro('emailViewButton', function($link = '#', $entityType = ENTITY_INVOICE) {
2015-12-02 14:26:06 +01:00
return view('partials.email_button')
->with([
'link' => $link,
'field' => "view_{$entityType}",
'color' => '#0b4d78',
])
->render();
});
2016-03-02 14:36:42 +01:00
Form::macro('emailPaymentButton', function($link = '#') {
2015-12-02 14:26:06 +01:00
return view('partials.email_button')
->with([
'link' => $link,
'field' => 'pay_now',
'color' => '#36c157',
])
->render();
});
2015-06-03 19:55:48 +02:00
2016-03-02 14:36:42 +01:00
Form::macro('breadcrumbs', function($status = false) {
2015-06-03 19:55:48 +02:00
$str = '<ol class="breadcrumb">';
// Get the breadcrumbs by exploding the current path.
$basePath = Utils::basePath();
2015-08-20 17:09:04 +02:00
$parts = explode('?', isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '');
2015-06-03 19:55:48 +02:00
$path = $parts[0];
if ($basePath != '/') {
$path = str_replace($basePath, '', $path);
}
$crumbs = explode('/', $path);
foreach ($crumbs as $key => $val) {
if (is_numeric($val)) {
unset($crumbs[$key]);
}
}
$crumbs = array_values($crumbs);
for ($i=0; $i<count($crumbs); $i++) {
$crumb = trim($crumbs[$i]);
if (!$crumb) {
continue;
}
if ($crumb == 'company') {
return '';
}
$name = trans("texts.$crumb");
if ($i==count($crumbs)-1) {
$str .= "<li class='active'>$name</li>";
} else {
$str .= '<li>'.link_to($crumb, $name).'</li>';
}
}
2016-02-04 19:36:39 +01:00
if ($status) {
$str .= '&nbsp;&nbsp;&nbsp;&nbsp;' . $status;
}
2015-06-03 19:55:48 +02:00
return $str . '</ol>';
});
Form::macro('human_filesize', function($bytes, $decimals = 1) {
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
$factor = floor((strlen($bytes) - 1) / 3);
2016-03-24 16:58:30 +01:00
if($factor == 0)$decimals=0;// There aren't fractional bytes
return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
});
2015-06-03 19:55:48 +02:00
Validator::extend('positive', function($attribute, $value, $parameters) {
return Utils::parseFloat($value) >= 0;
});
Validator::extend('has_credit', function($attribute, $value, $parameters) {
$publicClientId = $parameters[0];
$amount = $parameters[1];
$client = \App\Models\Client::scope($publicClientId)->firstOrFail();
$credit = $client->getTotalCredit();
return $credit >= $amount;
});
2015-09-25 11:57:40 +02:00
// check that the time log elements don't overlap
Validator::extend('time_log', function($attribute, $value, $parameters) {
$lastTime = 0;
$value = json_decode($value);
array_multisort($value);
foreach ($value as $timeLog) {
list($startTime, $endTime) = $timeLog;
if (!$endTime) {
continue;
}
if ($startTime < $lastTime || $startTime > $endTime) {
return false;
}
if ($endTime < min($startTime, $lastTime)) {
return false;
}
$lastTime = max($lastTime, $endTime);
}
return true;
});
2015-06-03 19:55:48 +02:00
2015-10-25 12:08:11 +01:00
Validator::extend('has_counter', function($attribute, $value, $parameters) {
return !$value || strstr($value, '{$counter}');
});
2015-10-28 20:22:07 +01:00
Validator::extend('valid_contacts', function($attribute, $value, $parameters) {
foreach ($value as $contact) {
$validator = Validator::make($contact, [
'email' => 'email|required_without:first_name',
'first_name' => 'required_without:email',
]);
if ($validator->fails()) {
return false;
}
}
return true;
});
2015-11-04 08:48:47 +01:00
Validator::extend('valid_invoice_items', function($attribute, $value, $parameters) {
$total = 0;
foreach ($value as $item) {
$qty = isset($item['qty']) ? $item['qty'] : 1;
$cost = isset($item['cost']) ? $item['cost'] : 1;
$total += $qty * $cost;
2015-11-04 08:48:47 +01:00
}
return $total <= MAX_INVOICE_AMOUNT;
});
2015-03-12 01:44:39 +01:00
}
/**
* Register any application services.
*
* This service provider is a great spot to register your various container
* bindings with the application. As you can see, we are registering our
* "Registrar" implementation here. You can add your own bindings too!
*
* @return void
*/
public function register()
{
$this->app->bind(
'Illuminate\Contracts\Auth\Registrar',
'App\Services\Registrar'
);
}
}