2015-11-03 20:03:24 +01:00
|
|
|
<?php namespace App\Http\Controllers;
|
|
|
|
|
2015-11-27 13:55:28 +01:00
|
|
|
use Session;
|
2015-11-03 20:03:24 +01:00
|
|
|
use Utils;
|
2016-05-01 22:55:13 +02:00
|
|
|
use Auth;
|
2016-05-03 08:46:24 +02:00
|
|
|
use Log;
|
2016-05-01 22:55:13 +02:00
|
|
|
use Input;
|
2015-11-03 20:03:24 +01:00
|
|
|
use Response;
|
2015-11-27 13:55:28 +01:00
|
|
|
use Request;
|
2015-11-03 20:03:24 +01:00
|
|
|
use League\Fractal;
|
|
|
|
use League\Fractal\Manager;
|
2015-11-08 10:43:32 +01:00
|
|
|
use League\Fractal\Resource\Item;
|
|
|
|
use League\Fractal\Resource\Collection;
|
2015-11-27 13:55:28 +01:00
|
|
|
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
|
2016-05-01 22:55:13 +02:00
|
|
|
use App\Models\EntityModel;
|
2015-11-03 20:03:24 +01:00
|
|
|
use App\Ninja\Serializers\ArraySerializer;
|
2015-11-27 13:55:28 +01:00
|
|
|
use League\Fractal\Serializer\JsonApiSerializer;
|
2016-05-01 22:55:13 +02:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
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",
|
|
|
|
* @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"
|
|
|
|
* ),
|
|
|
|
* @SWG\SecurityScheme(
|
|
|
|
* securityDefinition="api_key",
|
|
|
|
* type="apiKey",
|
|
|
|
* in="header",
|
|
|
|
* name="TOKEN"
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*/
|
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
|
|
|
|
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-05-04 23:26:39 +02:00
|
|
|
if ($updatedAt = Input::get('updated_at')) {
|
|
|
|
$updatedAt = date('Y-m-d H:i:s', $updatedAt);
|
|
|
|
$query->where(function($query) use ($includes, $updatedAt) {
|
|
|
|
$query->where('updated_at', '>=', $updatedAt);
|
|
|
|
foreach ($includes as $include) {
|
|
|
|
$query->orWhereHas($include, function($query) use ($updatedAt) {
|
|
|
|
$query->where('updated_at', '>=', $updatedAt);
|
|
|
|
});
|
2016-05-04 22:49:20 +02:00
|
|
|
}
|
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')) {
|
|
|
|
$filter = function($query) use ($clientPublicId) {
|
|
|
|
$query->where('public_id', '=', $clientPublicId);
|
|
|
|
};
|
|
|
|
$query->whereHas('client', $filter);
|
|
|
|
}
|
2016-05-13 11:08:41 +02:00
|
|
|
|
2016-05-02 08:33:48 +02:00
|
|
|
if ( ! Utils::hasPermission('view_all')){
|
|
|
|
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);
|
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-05-01 22:55:13 +02:00
|
|
|
$resource = new Collection($query->get(), $transformer, $entityType);
|
2016-05-02 22:06:36 +02:00
|
|
|
$resource->setPaginator(new IlluminatePaginatorAdapter($query->paginate($limit)));
|
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 = [
|
|
|
|
$index => $response
|
|
|
|
];
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-02-15 11:24:06 +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;
|
|
|
|
}
|
2015-11-03 20:03:24 +01:00
|
|
|
}
|