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

288 lines
7.3 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)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2019. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
2019-03-28 22:34:58 +01:00
namespace App\Http\Controllers;
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;
2019-09-11 02:37:53 +02:00
use App\Utils\Statics;
2019-03-28 22:34:58 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
2019-09-27 06:31:13 +02:00
use Illuminate\Support\Facades\Request as Input;
2019-03-28 22:34:58 +01:00
use League\Fractal\Manager;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use League\Fractal\Serializer\JsonApiSerializer;
/**
2019-09-11 02:37:53 +02:00
* Class BaseController
*/
2019-03-28 22:34:58 +01:00
class BaseController extends Controller
{
2019-09-11 02:37:53 +02:00
/**
* 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
* @var string
*/
public $forced_index;
/**
* Fractal manager
* @var object
*/
protected $manager;
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()->input('include') !== null)
{
$request_include = explode(",", request()->input('include'));
$include = array_merge($this->forced_includes, $request_include);
$include = implode(",", $include);
}
2019-06-24 13:05:47 +02:00
else if(count($this->forced_includes) >= 1)
{
2019-06-24 13:05:47 +02:00
$include = implode(",", $this->forced_includes);
2019-03-28 22:34:58 +01:00
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());
2019-06-24 13:05:47 +02:00
}
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()
{
2019-09-11 02:37:53 +02:00
return response()->json(['message' => '404 | Nothing to see here!'], 404)
2019-09-11 07:32:47 +02:00
->header('X-Api-Version', config('ninja.api_version'))
->header('X-App-Version', config('ninja.app_version'));
2019-09-11 02:37:53 +02:00
}
public function notFoundClient()
{
2019-09-11 02:37:53 +02:00
return abort(404);
2019-09-11 02:37:53 +02:00
}
2019-03-28 22:34:58 +01:00
protected function errorResponse($response, $httpErrorCode = 400)
{
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
$error['error'] = $response;
$error = json_encode($error, JSON_PRETTY_PRINT);
$headers = self::getApiHeaders();
return response()->make($error, $httpErrorCode, $headers);
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
}
protected function listResponse($query)
{
$this->buildManager();
2019-04-03 04:34:28 +02:00
$transformer = new $this->entity_transformer(Input::get('serializer'));
2019-03-28 22:34:58 +01:00
$includes = $transformer->getDefaultIncludes();
$includes = $this->getRequestIncludes($includes);
$query->with($includes);
2019-09-27 06:31:13 +02:00
if (auth()->user()->cannot('view_'.$this->entity_type))
{
if ($this->entity_type == User::class) {
$query->where('id', '=', auth()->user()->id);
} else {
$query->where('user_id', '=', auth()->user()->id);
}
}
2019-04-03 04:34:28 +02:00
$data = $this->createCollection($query, $transformer, $this->entity_type);
2019-03-28 22:34:58 +01:00
return $this->response($data);
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
}
2019-04-03 04:34:28 +02:00
protected function createCollection($query, $transformer, $entity_type)
2019-03-28 22:34:58 +01:00
{
$this->buildManager();
2019-03-28 22:34:58 +01:00
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
2019-04-03 04:34:28 +02:00
$entity_type = null;
2019-03-28 22:34:58 +01:00
}
if (is_a($query, "Illuminate\Database\Eloquent\Builder")) {
$limit = Input::get('per_page', 20);
$paginator = $query->paginate($limit);
$query = $paginator->getCollection();
2019-04-03 04:34:28 +02:00
$resource = new Collection($query, $transformer, $entity_type);
2019-03-28 22:34:58 +01:00
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
} else {
2019-04-03 04:34:28 +02:00
$resource = new Collection($query, $transformer, $entity_type);
2019-03-28 22:34:58 +01:00
}
return $this->manager->createData($resource)->toArray();
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
}
protected function response($response)
{
2019-09-11 02:37:53 +02:00
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)
$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);
$headers = self::getApiHeaders();
2019-03-28 22:34:58 +01:00
return response()->make($response, 200, $headers);
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
}
protected function itemResponse($item)
{
2019-09-11 02:37:53 +02:00
$this->buildManager();
2019-03-28 22:34:58 +01:00
2019-04-03 04:34:28 +02:00
$transformer = new $this->entity_transformer(Input::get('serializer'));
2019-03-28 22:34:58 +01:00
2019-04-03 04:34:28 +02:00
$data = $this->createItem($item, $transformer, $this->entity_type);
2019-03-28 22:34:58 +01:00
if(request()->include_static)
2019-09-11 02:37:53 +02:00
$data['static'] = Statics::company(auth()->user()->getCompany()->getLocale());
2019-09-11 01:31:55 +02:00
2019-03-28 22:34:58 +01:00
return $this->response($data);
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
}
2019-04-03 04:34:28 +02:00
protected function createItem($data, $transformer, $entity_type)
2019-03-28 22:34:58 +01:00
{
2019-09-11 02:37:53 +02:00
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON)
2019-04-03 04:34:28 +02:00
$entity_type = null;
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
2019-04-03 04:34:28 +02:00
$resource = new Item($data, $transformer, $entity_type);
2019-03-28 22:34:58 +01:00
return $this->manager->createData($resource)->toArray();
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
}
public static function getApiHeaders($count = 0)
{
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
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',
2019-09-11 02:37:53 +02:00
//'X-Total-Count' => $count,
2019-09-11 07:32:47 +02:00
'X-Api-Version' => config('ninja.api_version'),
'X-App-Version' => config('ninja.app_version'),
2019-03-28 22:34:58 +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,
];
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
}
protected function getRequestIncludes($data)
{
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
$included = request()->input('include');
$included = explode(',', $included);
foreach ($included as $include) {
if ($include == 'clients') {
$data[] = 'clients.contacts';
} elseif ($include) {
$data[] = $include;
}
}
return $data;
}
2019-09-11 02:37:53 +02:00
2019-03-28 22:34:58 +01:00
}