1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/Http/Controllers/BaseController.php

761 lines
28 KiB
PHP
Raw Normal View History

2019-03-28 22:34:58 +01:00
<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2019-05-11 05:32:07 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2019-05-11 05:32:07 +02:00
*/
2019-03-28 22:34:58 +01:00
namespace App\Http\Controllers;
use App\Models\Account;
2019-09-29 10:46:53 +02:00
use App\Models\Company;
2019-09-27 06:31:13 +02:00
use App\Models\User;
2019-03-28 22:34:58 +01:00
use App\Transformers\ArraySerializer;
use App\Transformers\EntityTransformer;
use App\Utils\Ninja;
2019-09-11 02:37:53 +02:00
use App\Utils\Statics;
use App\Utils\Traits\AppSetup;
2020-10-28 11:10:49 +01:00
use Illuminate\Contracts\Container\BindingResolutionException;
2020-11-13 10:09:20 +01:00
use Illuminate\Database\Eloquent\Builder;
2019-03-28 22:34:58 +01:00
use Illuminate\Http\Request;
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\JsonApiSerializer;
/**
* Class BaseController.
*/
2019-03-28 22:34:58 +01:00
class BaseController extends Controller
{
use AppSetup;
/**
* Passed from the parent when we need to force
* includes internally rather than externally via
2019-09-11 02:37:53 +02:00
* the $_REQUEST 'include' variable.
*
* @var array
*/
public $forced_includes;
2019-06-25 07:08:07 +02:00
/**
* Passed from the parent when we need to force
* the key of the response object.
2019-06-25 07:08:07 +02:00
* @var string
*/
public $forced_index;
/**
* Fractal manager.
* @var object
*/
protected $manager;
2020-07-26 10:30:55 +02:00
private $first_load = [
'account',
2020-10-26 01:58:08 +01:00
'user.company_user',
'token.company_user',
2020-07-26 10:30:55 +02:00
'company.activities',
'company.designs.company',
2020-10-25 21:56:02 +01:00
'company.task_statuses',
'company.expense_categories',
'company.documents',
2020-12-26 09:03:24 +01:00
'company.users.company_user',
2020-10-26 01:58:08 +01:00
'company.clients.contacts.company',
'company.clients.gateway_tokens',
2020-09-19 04:05:54 +02:00
'company.clients.documents',
'company.company_gateways.gateway',
'company.credits.invitations.contact',
'company.credits.invitations.company',
'company.credits.documents',
'company.expenses.documents',
2021-01-19 21:22:14 +01:00
'company.groups.documents',
2020-07-26 10:30:55 +02:00
'company.invoices.invitations.contact',
'company.invoices.invitations.company',
'company.invoices.documents',
'company.products',
'company.products.documents',
'company.payments.paymentables',
'company.payments.documents',
'company.payment_terms.company',
'company.projects.documents',
2020-09-19 04:05:54 +02:00
'company.recurring_invoices',
'company.recurring_invoices.invitations.contact',
'company.recurring_invoices.invitations.company',
'company.recurring_invoices.documents',
2020-07-26 10:30:55 +02:00
'company.quotes.invitations.contact',
'company.quotes.invitations.company',
'company.quotes.documents',
2020-10-18 09:46:10 +02:00
'company.tasks.documents',
'company.subscriptions',
'company.tax_rates',
'company.tokens_hashed',
2020-10-26 01:58:08 +01:00
'company.vendors.contacts.company',
'company.vendors.documents',
'company.webhooks',
2021-03-30 00:32:33 +02:00
'company.system_logs',
2020-07-26 10:30:55 +02:00
];
private $mini_load = [
'account',
'user.company_user',
'token',
'company.activities',
2021-01-27 11:56:50 +01:00
'company.documents',
2021-05-06 05:29:59 +02:00
'company.users.company_user',
2020-07-26 10:30:55 +02:00
'company.tax_rates',
'company.groups',
'company.payment_terms',
2021-05-06 05:29:59 +02:00
'company.designs.company',
2020-07-26 10:30:55 +02:00
];
2019-03-28 22:34:58 +01:00
public function __construct()
2019-03-28 22:34:58 +01:00
{
$this->manager = new Manager();
$this->forced_includes = [];
2019-06-25 07:08:07 +02:00
$this->forced_index = 'data';
}
private function buildManager()
{
2019-06-24 13:05:47 +02:00
$include = '';
if (request()->has('first_load') && request()->input('first_load') == 'true') {
$include = implode(',', array_merge($this->forced_includes, $this->getRequestIncludes([])));
} elseif (request()->input('include') !== null) {
$include = array_merge($this->forced_includes, explode(',', request()->input('include')));
$include = implode(',', $include);
} elseif (count($this->forced_includes) >= 1) {
$include = implode(',', $this->forced_includes);
2019-06-24 13:05:47 +02:00
}
$this->manager->parseIncludes($include);
2019-03-28 22:34:58 +01:00
$this->serializer = request()->input('serializer') ?: EntityTransformer::API_SERIALIZER_ARRAY;
if ($this->serializer === EntityTransformer::API_SERIALIZER_JSON) {
2019-03-28 22:34:58 +01:00
$this->manager->setSerializer(new JsonApiSerializer());
} else {
2019-03-28 22:34:58 +01:00
$this->manager->setSerializer(new ArraySerializer());
}
2019-03-28 22:34:58 +01:00
}
/**
* Catch all fallback route
* for non-existant route.
*/
public function notFound()
{
2021-01-24 23:24:13 +01:00
return response()->json(['message' => ctrans('texts.api_404')], 404)
->header('X-API-VERSION', config('ninja.minimum_client_version'))
->header('X-APP-VERSION', config('ninja.app_version'));
}
/**
* 404 for the client portal.
* @return Response 404 response
*/
public function notFoundClient()
{
2021-05-24 11:39:21 +02:00
abort(404, 'Page not found in client portal.');
}
/**
* API Error response.
2020-10-28 11:10:49 +01:00
* @param string $message The return error message
* @param int $httpErrorCode 404/401/403 etc
* @return Response The JSON response
2020-10-28 11:10:49 +01:00
* @throws BindingResolutionException
*/
protected function errorResponse($message, $httpErrorCode = 400)
2019-03-28 22:34:58 +01:00
{
$error['error'] = $message;
2019-03-28 22:34:58 +01:00
$error = json_encode($error, JSON_PRETTY_PRINT);
2019-03-28 22:34:58 +01:00
$headers = self::getApiHeaders();
return response()->make($error, $httpErrorCode, $headers);
}
protected function refreshResponse($query)
{
$user = auth()->user();
$this->manager->parseIncludes($this->first_load);
2020-07-24 11:39:43 +02:00
$this->serializer = request()->input('serializer') ?: EntityTransformer::API_SERIALIZER_ARRAY;
if ($this->serializer === EntityTransformer::API_SERIALIZER_JSON) {
$this->manager->setSerializer(new JsonApiSerializer());
} else {
$this->manager->setSerializer(new ArraySerializer());
}
2020-07-26 07:12:40 +02:00
$transformer = new $this->entity_transformer($this->serializer);
$updated_at = request()->has('updated_at') ? request()->input('updated_at') : 0;
$updated_at = date('Y-m-d H:i:s', $updated_at);
$query->with(
2020-11-25 15:19:52 +01:00
[
'company' => function ($query) use ($updated_at, $user) {
2020-09-19 12:05:29 +02:00
$query->whereNotNull('updated_at')->with('documents');
2020-07-26 12:21:55 +02:00
},
'company.clients' => function ($query) use ($updated_at, $user) {
2020-11-25 15:19:52 +01:00
$query->where('clients.updated_at', '>=', $updated_at)->with('contacts.company', 'gateway_tokens', 'documents');
if(!$user->hasPermission('view_client'))
$query->where('clients.user_id', $user->id)->orWhere('clients.assigned_user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.company_gateways' => function ($query) use ($user) {
2021-04-29 03:31:04 +02:00
$query->whereNotNull('updated_at')->with('gateway');
if(!$user->isAdmin())
$query->where('company_gateways.user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.credits'=> function ($query) use ($updated_at, $user) {
2020-11-11 09:55:53 +01:00
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents');
if(!$user->hasPermission('view_credit'))
$query->where('credits.user_id', $user->id)->orWhere('credits.assigned_user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.designs'=> function ($query) use ($updated_at, $user) {
2020-09-10 03:05:42 +02:00
$query->where('updated_at', '>=', $updated_at)->with('company');
if(!$user->isAdmin())
$query->where('designs.user_id', $user->id);
2020-09-10 03:05:42 +02:00
},
'company.documents'=> function ($query) use ($updated_at, $user) {
2020-10-19 12:59:58 +02:00
$query->where('updated_at', '>=', $updated_at);
2020-07-26 12:21:55 +02:00
},
'company.expenses'=> function ($query) use ($updated_at, $user) {
2020-11-11 09:55:53 +01:00
$query->where('updated_at', '>=', $updated_at)->with('documents');
if(!$user->hasPermission('view_expense'))
$query->where('expenses.user_id', $user->id)->orWhere('expenses.assigned_user_id', $user->id);
2020-09-19 04:05:54 +02:00
},
'company.groups' => function ($query) use ($updated_at, $user) {
2020-10-19 12:59:58 +02:00
$query->where('updated_at', '>=', $updated_at);
if(!$user->isAdmin())
$query->where('group_settings.user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.invoices'=> function ($query) use ($updated_at, $user) {
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents');
if(!$user->hasPermission('view_invoice'))
$query->where('invoices.user_id', $user->id)->orWhere('invoices.assigned_user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.payments'=> function ($query) use ($updated_at, $user) {
2020-11-25 15:19:52 +01:00
$query->where('updated_at', '>=', $updated_at)->with('paymentables', 'documents');
if(!$user->hasPermission('view_payment'))
$query->where('payments.user_id', $user->id)->orWhere('payments.assigned_user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.payment_terms'=> function ($query) use ($updated_at, $user) {
$query->where('updated_at', '>=', $updated_at);
if(!$user->isAdmin())
$query->where('payment_terms.user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.products' => function ($query) use ($updated_at, $user) {
2020-10-19 12:59:58 +02:00
$query->where('updated_at', '>=', $updated_at)->with('documents');
if(!$user->hasPermission('view_product'))
$query->where('products.user_id', $user->id)->orWhere('products.assigned_user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.projects'=> function ($query) use ($updated_at, $user) {
2020-11-25 15:19:52 +01:00
$query->where('updated_at', '>=', $updated_at)->with('documents');
if(!$user->hasPermission('view_project'))
$query->where('projects.user_id', $user->id)->orWhere('projects.assigned_user_id', $user->id);
2020-10-19 12:59:58 +02:00
},
'company.quotes'=> function ($query) use ($updated_at, $user) {
2020-11-11 09:55:53 +01:00
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents');
if(!$user->hasPermission('view_quote'))
$query->where('quotes.user_id', $user->id)->orWhere('quotes.assigned_user_id', $user->id);
2020-10-19 12:59:58 +02:00
},
'company.recurring_invoices'=> function ($query) use ($updated_at, $user) {
2020-10-19 12:59:58 +02:00
$query->where('updated_at', '>=', $updated_at)->with('invitations', 'documents');
if(!$user->hasPermission('view_recurring_invoice'))
$query->where('recurring_invoices.user_id', $user->id)->orWhere('recurring_invoices.assigned_user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.tasks'=> function ($query) use ($updated_at, $user) {
2020-11-25 15:19:52 +01:00
$query->where('updated_at', '>=', $updated_at)->with('documents');
if(!$user->hasPermission('view_task'))
$query->where('tasks.user_id', $user->id)->orWhere('tasks.assigned_user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.tax_rates' => function ($query) use ($updated_at, $user) {
$query->where('updated_at', '>=', $updated_at);
2020-07-26 12:21:55 +02:00
},
'company.vendors'=> function ($query) use ($updated_at, $user) {
2020-11-25 15:19:52 +01:00
$query->where('updated_at', '>=', $updated_at)->with('contacts', 'documents');
if(!$user->hasPermission('view_vendor'))
$query->where('vendors.user_id', $user->id)->orWhere('vendors.assigned_user_id', $user->id);
2020-07-26 12:21:55 +02:00
},
'company.expense_categories'=> function ($query) use ($updated_at, $user) {
2020-10-25 21:56:02 +01:00
$query->where('updated_at', '>=', $updated_at);
},
'company.task_statuses'=> function ($query) use ($updated_at, $user) {
2020-10-25 21:56:02 +01:00
$query->where('updated_at', '>=', $updated_at);
2020-10-28 11:10:49 +01:00
},
'company.activities'=> function ($query) use($user) {
if(!$user->isAdmin())
$query->where('activities.user_id', $user->id);
2021-03-29 04:14:55 +02:00
},
2021-03-29 05:49:29 +02:00
'company.subscriptions'=> function ($query) use($updated_at, $user) {
2021-03-29 04:14:55 +02:00
$query->where('updated_at', '>=', $updated_at);
if(!$user->isAdmin())
$query->where('subscriptions.user_id', $user->id);
}
2020-07-26 12:21:55 +02:00
]
);
2020-11-13 10:09:20 +01:00
if ($query instanceof Builder) {
$limit = request()->input('per_page', 20);
2020-07-24 11:39:43 +02:00
$paginator = $query->paginate($limit);
$query = $paginator->getCollection();
$resource = new Collection($query, $transformer, $this->entity_type);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
} else {
$resource = new Collection($query, $transformer, $this->entity_type);
}
return $this->response($this->manager->createData($resource)->toArray());
}
2021-05-14 09:38:16 +02:00
protected function miniLoadResponse($query)
{
$user = auth()->user();
$this->serializer = request()->input('serializer') ?: EntityTransformer::API_SERIALIZER_ARRAY;
if ($this->serializer === EntityTransformer::API_SERIALIZER_JSON) {
$this->manager->setSerializer(new JsonApiSerializer());
} else {
$this->manager->setSerializer(new ArraySerializer());
}
$transformer = new $this->entity_transformer($this->serializer);
$created_at = request()->has('created_at') ? request()->input('created_at') : 0;
$created_at = date('Y-m-d H:i:s', $created_at);
$query->with(
[
'company' => function ($query) use ($created_at, $user) {
$query->whereNotNull('created_at')->with('documents');
},
'company.designs'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('company');
},
'company.documents'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.groups' => function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.payment_terms'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.tax_rates' => function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.activities'=> function ($query) use($user) {
if(!$user->isAdmin())
$query->where('activities.user_id', $user->id);
}
]
);
if ($query instanceof Builder) {
$limit = request()->input('per_page', 20);
$paginator = $query->paginate($limit);
$query = $paginator->getCollection();
$resource = new Collection($query, $transformer, $this->entity_type);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
} else {
$resource = new Collection($query, $transformer, $this->entity_type);
}
return $this->response($this->manager->createData($resource)->toArray());
}
2021-04-29 00:44:40 +02:00
protected function timeConstrainedResponse($query)
{
$user = auth()->user();
2021-05-14 09:38:16 +02:00
if ($user->getCompany()->is_large){
2021-04-29 00:44:40 +02:00
$this->manager->parseIncludes($this->mini_load);
2021-05-14 09:38:16 +02:00
return $this->miniLoadResponse($query);
}
2021-04-29 00:44:40 +02:00
else
$this->manager->parseIncludes($this->first_load);
$this->serializer = request()->input('serializer') ?: EntityTransformer::API_SERIALIZER_ARRAY;
if ($this->serializer === EntityTransformer::API_SERIALIZER_JSON) {
$this->manager->setSerializer(new JsonApiSerializer());
} else {
$this->manager->setSerializer(new ArraySerializer());
}
$transformer = new $this->entity_transformer($this->serializer);
$created_at = request()->has('created_at') ? request()->input('created_at') : 0;
$created_at = date('Y-m-d H:i:s', $created_at);
$query->with(
[
'company' => function ($query) use ($created_at, $user) {
$query->whereNotNull('created_at')->with('documents');
},
2021-05-03 13:51:00 +02:00
'company.clients' => function ($query) use ($created_at, $user) {
$query->where('clients.created_at', '>=', $created_at)->with('contacts.company', 'gateway_tokens', 'documents');
2021-04-29 00:44:40 +02:00
if(!$user->hasPermission('view_client'))
$query->where('clients.user_id', $user->id)->orWhere('clients.assigned_user_id', $user->id);
},
'company.company_gateways' => function ($query) use ($user) {
2021-04-29 03:31:04 +02:00
$query->whereNotNull('created_at')->with('gateway');
2021-04-29 00:44:40 +02:00
if(!$user->isAdmin())
$query->where('company_gateways.user_id', $user->id);
},
'company.credits'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('invitations', 'documents');
if(!$user->hasPermission('view_credit'))
$query->where('credits.user_id', $user->id)->orWhere('credits.assigned_user_id', $user->id);
},
2021-05-06 00:42:18 +02:00
// 'company.designs'=> function ($query) use ($created_at, $user) {
// $query->where('created_at', '>=', $created_at)->with('company');
2021-04-29 00:44:40 +02:00
2021-05-06 00:42:18 +02:00
// if(!$user->isAdmin())
// $query->where('designs.user_id', $user->id);
// },
2021-04-29 00:44:40 +02:00
'company.documents'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.expenses'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('documents');
if(!$user->hasPermission('view_expense'))
$query->where('expenses.user_id', $user->id)->orWhere('expenses.assigned_user_id', $user->id);
},
'company.groups' => function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
if(!$user->isAdmin())
$query->where('group_settings.user_id', $user->id);
},
'company.invoices'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('invitations', 'documents');
if(!$user->hasPermission('view_invoice'))
$query->where('invoices.user_id', $user->id)->orWhere('invoices.assigned_user_id', $user->id);
},
'company.payments'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('paymentables', 'documents');
if(!$user->hasPermission('view_payment'))
$query->where('payments.user_id', $user->id)->orWhere('payments.assigned_user_id', $user->id);
},
'company.payment_terms'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.products' => function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('documents');
if(!$user->hasPermission('view_product'))
$query->where('products.user_id', $user->id)->orWhere('products.assigned_user_id', $user->id);
},
'company.projects'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('documents');
if(!$user->hasPermission('view_project'))
$query->where('projects.user_id', $user->id)->orWhere('projects.assigned_user_id', $user->id);
},
'company.quotes'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('invitations', 'documents');
if(!$user->hasPermission('view_quote'))
$query->where('quotes.user_id', $user->id)->orWhere('quotes.assigned_user_id', $user->id);
},
'company.recurring_invoices'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('invitations', 'documents');
if(!$user->hasPermission('view_recurring_invoice'))
$query->where('recurring_invoices.user_id', $user->id)->orWhere('recurring_invoices.assigned_user_id', $user->id);
},
'company.tasks'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('documents');
if(!$user->hasPermission('view_task'))
$query->where('tasks.user_id', $user->id)->orWhere('tasks.assigned_user_id', $user->id);
},
'company.tax_rates' => function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.vendors'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at)->with('contacts', 'documents');
if(!$user->hasPermission('view_vendor'))
$query->where('vendors.user_id', $user->id)->orWhere('vendors.assigned_user_id', $user->id);
},
'company.expense_categories'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.task_statuses'=> function ($query) use ($created_at, $user) {
$query->where('created_at', '>=', $created_at);
},
'company.activities'=> function ($query) use($user) {
if(!$user->isAdmin())
$query->where('activities.user_id', $user->id);
},
'company.subscriptions'=> function ($query) use($created_at, $user) {
$query->where('created_at', '>=', $created_at);
if(!$user->isAdmin())
$query->where('subscriptions.user_id', $user->id);
}
]
);
if ($query instanceof Builder) {
$limit = request()->input('per_page', 20);
$paginator = $query->paginate($limit);
$query = $paginator->getCollection();
$resource = new Collection($query, $transformer, $this->entity_type);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
} else {
$resource = new Collection($query, $transformer, $this->entity_type);
}
return $this->response($this->manager->createData($resource)->toArray());
}
protected function listResponse($query)
2019-03-28 22:34:58 +01:00
{
$this->buildManager();
2020-11-13 10:09:20 +01:00
$transformer = new $this->entity_transformer(request()->input('serializer'));
2019-03-28 22:34:58 +01:00
$includes = $transformer->getDefaultIncludes();
2019-03-28 22:34:58 +01:00
$includes = $this->getRequestIncludes($includes);
$query->with($includes);
2020-11-25 15:19:52 +01:00
if (auth()->user() && ! auth()->user()->hasPermission('view_'.lcfirst(class_basename($this->entity_type)))) {
$query->where('user_id', '=', auth()->user()->id);
2020-11-25 15:19:52 +01:00
}
2019-09-27 06:31:13 +02:00
2020-11-25 15:19:52 +01:00
if (request()->has('updated_at') && request()->input('updated_at') > 0) {
2020-11-13 10:09:20 +01:00
$query->where('updated_at', '>=', date('Y-m-d H:i:s', intval(request()->input('updated_at'))));
2020-11-25 15:19:52 +01:00
}
2020-11-25 15:19:52 +01:00
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
2020-11-13 10:09:20 +01:00
$this->entity_type = null;
2020-11-25 15:19:52 +01:00
}
2019-03-28 22:34:58 +01:00
2020-11-13 10:09:20 +01:00
if ($query instanceof Builder) {
$limit = request()->input('per_page', 20);
2019-03-28 22:34:58 +01:00
$paginator = $query->paginate($limit);
$query = $paginator->getCollection();
2020-11-13 10:09:20 +01:00
$resource = new Collection($query, $transformer, $this->entity_type);
2019-03-28 22:34:58 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
} else {
2020-11-13 10:09:20 +01:00
$resource = new Collection($query, $transformer, $this->entity_type);
2019-03-28 22:34:58 +01:00
}
2020-11-13 10:09:20 +01:00
return $this->response($this->manager->createData($resource)->toArray());
2019-03-28 22:34:58 +01:00
}
protected function response($response)
{
2019-06-25 07:08:07 +02:00
$index = request()->input('index') ?: $this->forced_index;
2019-03-28 22:34:58 +01:00
if ($index == 'none') {
unset($response['meta']);
} else {
$meta = isset($response['meta']) ? $response['meta'] : null;
$response = [
$index => $response,
];
if ($meta) {
$response['meta'] = $meta;
unset($response[$index]['meta']);
}
2019-09-18 08:02:05 +02:00
if (request()->include_static) {
2019-09-18 08:02:05 +02:00
$response['static'] = Statics::company(auth()->user()->getCompany()->getLocale());
}
2019-03-28 22:34:58 +01:00
}
ksort($response);
2019-03-28 22:34:58 +01:00
$response = json_encode($response, JSON_PRETTY_PRINT);
2019-03-28 22:34:58 +01:00
$headers = self::getApiHeaders();
2019-03-28 22:34:58 +01:00
return response()->make($response, 200, $headers);
}
protected function itemResponse($item)
{
$this->buildManager();
2019-03-28 22:34:58 +01:00
2020-11-13 10:09:20 +01:00
$transformer = new $this->entity_transformer(request()->input('serializer'));
2019-03-28 22:34:58 +01:00
2020-11-25 15:19:52 +01:00
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
2020-11-13 10:09:20 +01:00
$this->entity_type = null;
2020-11-25 15:19:52 +01:00
}
$resource = new Item($item, $transformer, $this->entity_type);
2019-03-28 22:34:58 +01:00
2020-11-25 15:19:52 +01:00
if (auth()->user() && request()->include_static) {
2019-09-11 02:37:53 +02:00
$data['static'] = Statics::company(auth()->user()->getCompany()->getLocale());
2020-11-25 15:19:52 +01:00
}
2020-11-13 10:09:20 +01:00
return $this->response($this->manager->createData($resource)->toArray());
2019-03-28 22:34:58 +01:00
}
public static function getApiHeaders($count = 0)
{
return [
'Content-Type' => 'application/json',
'X-Api-Version' => config('ninja.minimum_client_version'),
2019-09-11 07:32:47 +02:00
'X-App-Version' => config('ninja.app_version'),
2019-03-28 22:34:58 +01:00
];
}
protected function getRequestIncludes($data)
{
/*
* Thresholds for displaying large account on first load
*/
if (request()->has('first_load') && request()->input('first_load') == 'true') {
if (auth()->user()->getCompany()->is_large && request()->missing('updated_at')) {
2020-07-26 10:30:55 +02:00
$data = $this->mini_load;
} else {
2020-07-26 10:30:55 +02:00
$data = $this->first_load;
}
} else {
$included = request()->input('include');
$included = explode(',', $included);
foreach ($included as $include) {
if ($include == 'clients') {
$data[] = 'clients.contacts';
} elseif ($include) {
$data[] = $include;
}
}
2019-03-28 22:34:58 +01:00
}
return $data;
}
public function flutterRoute()
{
2020-10-26 01:58:08 +01:00
if ((bool) $this->checkAppSetup() !== false && $account = Account::first()) {
if (config('ninja.require_https') && ! request()->isSecure()) {
return redirect()->secure(request()->getRequestUri());
}
$data = [];
2020-05-28 10:54:13 +02:00
if (Ninja::isSelfHost()) {
$data['report_errors'] = $account->report_errors;
} else {
$data['report_errors'] = true;
}
$this->buildCache();
return view('index.index', $data);
}
return redirect('/setup');
}
2021-03-07 11:14:53 +01:00
public function checkFeature($feature)
{
if(auth()->user()->account->hasFeature($feature))
return true;
return false;
}
public function featureFailure()
{
return response()->json(['message' => 'Upgrade to a paid plan for this feature.'], 403);
}
}