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"},
|
|
|
|
* host="ninja.dev",
|
|
|
|
* basePath="/api/v1",
|
2017-03-09 17:22:37 +01:00
|
|
|
* 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;
|
2016-05-13 11:08:41 +02:00
|
|
|
|
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-13 11:08:41 +02:00
|
|
|
{
|
2016-05-02 10:38:01 +02:00
|
|
|
$entity = $request->entity();
|
|
|
|
$action = $request->action;
|
2016-05-13 11:08:41 +02:00
|
|
|
|
2017-05-22 11:16:27 +02:00
|
|
|
if (! in_array($action, ['archive', 'delete', 'restore'])) {
|
2017-05-30 12:56:51 +02:00
|
|
|
return $this->errorResponse("Action [$action] is not supported");
|
2017-05-22 11:16:27 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 10:38:01 +02:00
|
|
|
$repo = Utils::toCamelCase($this->entityType) . 'Repo';
|
2016-05-13 11:08:41 +02:00
|
|
|
|
2016-05-02 10:38:01 +02:00
|
|
|
$this->$repo->$action($entity);
|
2016-05-13 11:08:41 +02:00
|
|
|
|
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);
|
2016-05-13 11:08:41 +02:00
|
|
|
$transformer = new $transformerClass(Auth::user()->account, Input::get('serializer'));
|
2016-05-03 10:39:10 +02:00
|
|
|
|
2016-05-04 22:49:20 +02:00
|
|
|
$includes = $transformer->getDefaultIncludes();
|
|
|
|
$includes = $this->getRequestIncludes($includes);
|
|
|
|
|
2016-05-04 23:26:39 +02:00
|
|
|
$query->with($includes);
|
2016-05-13 11:08:41 +02:00
|
|
|
|
2016-07-15 15:47:18 +02:00
|
|
|
if ($updatedAt = intval(Input::get('updated_at'))) {
|
|
|
|
$query->where('updated_at', '>=', date('Y-m-d H:i:s', $updatedAt));
|
2016-05-04 23:26:39 +02:00
|
|
|
}
|
2016-05-13 11:08:41 +02:00
|
|
|
|
2016-05-01 22:55:13 +02:00
|
|
|
if ($clientPublicId = Input::get('client_id')) {
|
2017-01-30 17:05:31 +01:00
|
|
|
$filter = function ($query) use ($clientPublicId) {
|
2016-05-01 22:55:13 +02:00
|
|
|
$query->where('public_id', '=', $clientPublicId);
|
|
|
|
};
|
|
|
|
$query->whereHas('client', $filter);
|
|
|
|
}
|
2016-05-13 11:08:41 +02:00
|
|
|
|
2017-01-30 17:05:31 +01:00
|
|
|
if (! Utils::hasPermission('view_all')) {
|
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-13 11:08:41 +02:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$transformerClass = EntityModel::getTransformerName($this->entityType);
|
2016-05-13 11:08:41 +02:00
|
|
|
$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-13 11:08:41 +02:00
|
|
|
|
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")) {
|
2016-05-02 22:06:36 +02:00
|
|
|
$limit = min(MAX_API_PAGE_SIZE, Input::get('per_page', DEFAULT_API_PAGE_SIZE));
|
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
|
|
|
}
|
2016-05-13 11:08:41 +02: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';
|
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') {
|
2016-05-04 14:16:49 +02:00
|
|
|
$data[] = 'vendors.vendor_contacts';
|
2016-05-03 22:02:29 +02:00
|
|
|
} elseif ($include) {
|
2015-11-27 13:55:28 +01:00
|
|
|
$data[] = $include;
|
|
|
|
}
|
|
|
|
}
|
2016-05-13 11:08:41 +02:00
|
|
|
|
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
|
|
|
}
|