1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00
invoiceninja/app/Http/Controllers/BaseAPIController.php

251 lines
7.8 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
2015-11-03 20:03:24 +01:00
2017-01-30 20:40:43 +01:00
namespace App\Http\Controllers;
use App\Models\EntityModel;
use App\Ninja\Serializers\ArraySerializer;
2016-05-01 22:55:13 +02:00
use Auth;
use Input;
2015-11-03 20:03:24 +01:00
use League\Fractal\Manager;
2015-11-27 13:55:28 +01:00
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
2017-01-30 20:40:43 +01:00
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
2015-11-27 13:55:28 +01:00
use League\Fractal\Serializer\JsonApiSerializer;
2017-01-30 20:40:43 +01:00
use Request;
use Response;
use Utils;
2015-11-03 20:03:24 +01:00
2015-11-08 21:34:26 +01:00
/**
* @SWG\Swagger(
* schemes={"http","https"},
2017-12-31 10:08:46 +01:00
* host="ninja.test",
2015-11-08 21:34:26 +01:00
* basePath="/api/v1",
* produces={"application/json"},
2015-11-08 21:34:26 +01:00
* @SWG\Info(
* version="1.0.0",
* title="Invoice Ninja API",
* description="An open-source invoicing and time-tracking app built with Laravel",
* termsOfService="",
* @SWG\Contact(
* email="contact@invoiceninja.com"
* ),
* @SWG\License(
* name="Attribution Assurance License",
* url="https://raw.githubusercontent.com/invoiceninja/invoiceninja/master/LICENSE"
* )
* ),
* @SWG\ExternalDocumentation(
* description="Find out more about Invoice Ninja",
* url="https://www.invoiceninja.com"
* ),
2017-03-17 10:28:46 +01:00
* security={"api_key": {}},
2015-11-08 21:34:26 +01:00
* @SWG\SecurityScheme(
* securityDefinition="api_key",
* type="apiKey",
* in="header",
2017-03-17 10:28:46 +01:00
* name="X-Ninja-Token"
2015-11-08 21:34:26 +01:00
* )
* )
*/
2015-11-03 20:03:24 +01:00
class BaseAPIController extends Controller
{
protected $manager;
2015-11-27 13:55:28 +01:00
protected $serializer;
2015-11-03 20:03:24 +01:00
public function __construct()
{
$this->manager = new Manager();
2015-11-27 13:55:28 +01:00
if ($include = Request::get('include')) {
$this->manager->parseIncludes($include);
}
$this->serializer = Request::get('serializer') ?: API_SERIALIZER_ARRAY;
2015-11-27 13:55:28 +01:00
if ($this->serializer === API_SERIALIZER_JSON) {
$this->manager->setSerializer(new JsonApiSerializer());
} else {
$this->manager->setSerializer(new ArraySerializer());
}
2015-11-03 20:03:24 +01:00
}
2016-05-02 10:38:01 +02:00
protected function handleAction($request)
{
2016-05-02 10:38:01 +02:00
$entity = $request->entity();
$action = $request->action;
2018-07-07 21:13:02 +02:00
if (! in_array($action, ['archive', 'delete', 'restore', 'mark_sent', 'markSent', 'emailInvoice', 'markPaid'])) {
2017-05-30 12:56:51 +02:00
return $this->errorResponse("Action [$action] is not supported");
2017-05-22 11:16:27 +02:00
}
2017-10-01 20:25:59 +02:00
if ($action == 'mark_sent') {
$action = 'markSent';
}
2016-05-02 10:38:01 +02:00
$repo = Utils::toCamelCase($this->entityType) . 'Repo';
2016-05-02 10:38:01 +02:00
$this->$repo->$action($entity);
2016-05-02 10:38:01 +02:00
return $this->itemResponse($entity);
}
protected function listResponse($query)
2016-05-01 22:55:13 +02:00
{
2016-05-03 10:39:10 +02:00
$transformerClass = EntityModel::getTransformerName($this->entityType);
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
2016-05-03 10:39:10 +02:00
$includes = $transformer->getDefaultIncludes();
$includes = $this->getRequestIncludes($includes);
$query->with($includes);
2018-06-17 12:01:43 +02:00
if (Input::get('filter_active')) {
$query->whereNull('deleted_at');
}
if (Input::get('updated_at') > 0) {
$updatedAt = intval(Input::get('updated_at'));
$query->where('updated_at', '>=', date('Y-m-d H:i:s', $updatedAt));
}
2018-02-26 11:04:25 +01:00
if (Input::get('client_id') > 0) {
$clientPublicId = Input::get('client_id');
$filter = function ($query) use ($clientPublicId) {
2016-05-01 22:55:13 +02:00
$query->where('public_id', '=', $clientPublicId);
};
$query->whereHas('client', $filter);
2016-05-01 22:55:13 +02:00
}
if (! Utils::hasPermission('admin')) {
2016-05-02 08:33:48 +02:00
if ($this->entityType == ENTITY_USER) {
$query->where('id', '=', Auth::user()->id);
} else {
$query->where('user_id', '=', Auth::user()->id);
}
}
2016-05-01 22:55:13 +02:00
$data = $this->createCollection($query, $transformer, $this->entityType);
return $this->response($data);
}
2016-05-02 10:38:01 +02:00
protected function itemResponse($item)
{
2017-07-12 22:56:31 +02:00
if (! $item) {
return $this->errorResponse('Record not found', 404);
}
2016-05-02 10:38:01 +02:00
$transformerClass = EntityModel::getTransformerName($this->entityType);
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
2016-05-02 10:38:01 +02:00
$data = $this->createItem($item, $transformer, $this->entityType);
2016-05-02 10:38:01 +02:00
return $this->response($data);
}
2015-11-27 13:55:28 +01:00
protected function createItem($data, $transformer, $entityType)
2015-11-03 20:03:24 +01:00
{
2015-11-27 13:55:28 +01:00
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
$entityType = null;
}
$resource = new Item($data, $transformer, $entityType);
2017-01-30 20:40:43 +01:00
2015-11-08 10:43:32 +01:00
return $this->manager->createData($resource)->toArray();
}
2015-11-05 09:44:48 +01:00
2016-05-01 22:55:13 +02:00
protected function createCollection($query, $transformer, $entityType)
2015-11-08 10:43:32 +01:00
{
2015-11-27 13:55:28 +01:00
if ($this->serializer && $this->serializer != API_SERIALIZER_JSON) {
$entityType = null;
}
2016-05-02 08:33:48 +02:00
if (is_a($query, "Illuminate\Database\Eloquent\Builder")) {
$limit = Input::get('per_page', DEFAULT_API_PAGE_SIZE);
if (Utils::isNinja()) {
$limit = min(MAX_API_PAGE_SIZE, $limit);
}
2016-07-17 12:31:31 +02:00
$paginator = $query->paginate($limit);
$query = $paginator->getCollection();
$resource = new Collection($query, $transformer, $entityType);
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
2016-05-02 08:33:48 +02:00
} else {
$resource = new Collection($query, $transformer, $entityType);
2015-11-27 13:55:28 +01:00
}
2015-11-08 10:43:32 +01:00
return $this->manager->createData($resource)->toArray();
}
protected function response($response)
{
2015-11-27 13:55:28 +01:00
$index = Request::get('index') ?: 'data';
2016-02-16 16:30:09 +01:00
if ($index == 'none') {
unset($response['meta']);
} else {
$meta = isset($response['meta']) ? $response['meta'] : null;
$response = [
2017-01-30 20:40:43 +01:00
$index => $response,
2016-02-16 16:30:09 +01:00
];
if ($meta) {
$response['meta'] = $meta;
unset($response[$index]['meta']);
}
2015-11-27 13:55:28 +01:00
}
2015-11-03 20:03:24 +01:00
$response = json_encode($response, JSON_PRETTY_PRINT);
$headers = Utils::getApiHeaders();
return Response::make($response, 200, $headers);
}
2017-01-30 17:05:31 +01:00
protected function errorResponse($response, $httpErrorCode = 400)
2016-02-01 04:42:05 +01:00
{
$error['error'] = $response;
$error = json_encode($error, JSON_PRETTY_PRINT);
$headers = Utils::getApiHeaders();
2016-02-15 11:24:06 +01:00
return Response::make($error, $httpErrorCode, $headers);
2016-02-01 04:42:05 +01:00
}
2016-05-03 10:39:10 +02:00
protected function getRequestIncludes($data)
2015-11-27 13:55:28 +01:00
{
$included = Request::get('include');
$included = explode(',', $included);
foreach ($included as $include) {
if ($include == 'invoices') {
$data[] = 'invoices.invoice_items';
2018-02-26 11:04:25 +01:00
$data[] = 'invoices.client.contacts';
} elseif ($include == 'invoice') {
$data[] = 'invoice.invoice_items';
$data[] = 'invoice.client.contacts';
2016-05-03 10:53:00 +02:00
} elseif ($include == 'client') {
$data[] = 'client.contacts';
2015-11-27 13:55:28 +01:00
} elseif ($include == 'clients') {
$data[] = 'clients.contacts';
2016-01-06 15:23:58 +01:00
} elseif ($include == 'vendors') {
$data[] = 'vendors.vendor_contacts';
2017-07-04 11:04:21 +02:00
} elseif ($include == 'documents' && $this->entityType == ENTITY_INVOICE) {
$data[] = 'documents.expense';
2016-05-03 22:02:29 +02:00
} elseif ($include) {
2015-11-27 13:55:28 +01:00
$data[] = $include;
}
}
2015-11-27 13:55:28 +01:00
return $data;
}
2016-06-23 11:38:06 +02:00
protected function fileReponse($name, $data)
{
header('Content-Type: application/pdf');
header('Content-Length: ' . strlen($data));
header('Content-disposition: attachment; filename="' . $name . '"');
header('Cache-Control: public, must-revalidate, max-age=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
return $data;
}
2015-11-03 20:03:24 +01:00
}