mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Implement forced includes to allow forcing additional objects in the json response
This commit is contained in:
parent
9cb33f3e10
commit
67fa34fe82
@ -22,25 +22,76 @@ use League\Fractal\Resource\Collection;
|
||||
use League\Fractal\Resource\Item;
|
||||
use League\Fractal\Serializer\JsonApiSerializer;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class BaseController extends Controller
|
||||
{
|
||||
/**
|
||||
* Passed from the parent when we need to force
|
||||
* includes internally rather than externally via
|
||||
* the REQUEST 'include' variable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public $forced_includes;
|
||||
|
||||
/**
|
||||
* Fractal manager
|
||||
* @var object
|
||||
*/
|
||||
protected $manager;
|
||||
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
$this->manager = new Manager();
|
||||
|
||||
if ($include = request()->input('include')) {
|
||||
$this->manager->parseIncludes($include);
|
||||
$this->forced_includes = [];
|
||||
|
||||
}
|
||||
|
||||
private function buildManager()
|
||||
{
|
||||
|
||||
if(request()->input('include') !== null)
|
||||
{
|
||||
|
||||
$request_include = explode(",", request()->input('include'));
|
||||
|
||||
$include = array_merge($this->forced_includes, $request_include);
|
||||
|
||||
$include = implode(",", $include);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if(count($this->forced_includes)>=1)
|
||||
$include = implode(",", $this->forced_includes);
|
||||
else
|
||||
$include = '';
|
||||
}
|
||||
|
||||
Log::error('forced includes = ' . print_r($this->forced_includes,1));
|
||||
Log::error('includes = ' . $include);
|
||||
|
||||
$this->manager->parseIncludes($include);
|
||||
|
||||
$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());
|
||||
}
|
||||
if ($this->serializer === EntityTransformer::API_SERIALIZER_JSON)
|
||||
{
|
||||
|
||||
$this->manager->setSerializer(new JsonApiSerializer());
|
||||
|
||||
} else
|
||||
{
|
||||
|
||||
$this->manager->setSerializer(new ArraySerializer());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,6 +115,9 @@ class BaseController extends Controller
|
||||
|
||||
protected function listResponse($query)
|
||||
{
|
||||
|
||||
$this->buildManager();
|
||||
|
||||
$transformer = new $this->entity_transformer(Input::get('serializer'));
|
||||
|
||||
$includes = $transformer->getDefaultIncludes();
|
||||
@ -79,6 +133,8 @@ class BaseController extends Controller
|
||||
protected function createCollection($query, $transformer, $entity_type)
|
||||
{
|
||||
|
||||
$this->buildManager();
|
||||
|
||||
if ($this->serializer && $this->serializer != EntityTransformer::API_SERIALIZER_JSON) {
|
||||
$entity_type = null;
|
||||
}
|
||||
@ -123,6 +179,7 @@ class BaseController extends Controller
|
||||
|
||||
protected function itemResponse($item)
|
||||
{
|
||||
$this->buildManager();
|
||||
|
||||
$transformer = new $this->entity_transformer(Input::get('serializer'));
|
||||
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\DataMapper\DefaultSettings;
|
||||
use App\Http\Requests\Company\CreateCompanyRequest;
|
||||
use App\Http\Requests\Company\DestroyCompanyRequest;
|
||||
use App\Http\Requests\Company\EditCompanyRequest;
|
||||
@ -43,6 +44,9 @@ class CompanyController extends BaseController
|
||||
protected $entity_transformer = CompanyTransformer::class;
|
||||
|
||||
protected $company_repo;
|
||||
|
||||
public $forced_includes = [];
|
||||
|
||||
/**
|
||||
* CompanyController constructor.
|
||||
*/
|
||||
@ -90,9 +94,19 @@ class CompanyController extends BaseController
|
||||
*/
|
||||
public function store(StoreCompanyRequest $request)
|
||||
{
|
||||
// $this->forced_includes = ['company_user'];
|
||||
|
||||
$company = CreateCompany::dispatchNow($request->all(), auth()->user()->company()->account);
|
||||
|
||||
auth()->user()->companies()->attach($company->id, [
|
||||
'account_id' => $company->account->id,
|
||||
'is_owner' => 1,
|
||||
'is_admin' => 1,
|
||||
'is_locked' => 0,
|
||||
'permissions' => json_encode([]),
|
||||
'settings' => json_encode(DefaultSettings::userSettings()),
|
||||
]);
|
||||
|
||||
return $this->itemResponse($company);
|
||||
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ class QueryLogging
|
||||
$time = $timeEnd - $timeStart;
|
||||
Log::info($request->method() . ' - ' . $request->url() . ": $count queries - " . $time);
|
||||
|
||||
if($count > 20)
|
||||
if($count > 16)
|
||||
Log::info($queries);
|
||||
}
|
||||
|
||||
|
@ -51,7 +51,7 @@ class CreateCompany
|
||||
{
|
||||
|
||||
$company = new Company();
|
||||
$company->name = $this->request['name'] ?: $this->request['first_name'] . ' ' . $this->request['last_name'];
|
||||
$company->name = isset($this->request['name']) ?: $this->request['first_name'] . ' ' . $this->request['last_name'];
|
||||
$company->account_id = $this->account->id;
|
||||
$company->company_key = $this->createHash();
|
||||
$company->ip = request()->ip();
|
||||
|
@ -15,7 +15,9 @@ namespace App\Transformers;
|
||||
use App\Models\Account;
|
||||
use App\Models\Client;
|
||||
use App\Models\Company;
|
||||
use App\Models\CompanyUser;
|
||||
use App\Models\User;
|
||||
use App\Transformers\CompanyUserTransformer;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
|
||||
/**
|
||||
@ -51,6 +53,7 @@ class CompanyTransformer extends EntityTransformer
|
||||
'language',
|
||||
'expenses',
|
||||
'payments',
|
||||
'company_user'
|
||||
];
|
||||
|
||||
|
||||
@ -85,6 +88,13 @@ class CompanyTransformer extends EntityTransformer
|
||||
];
|
||||
}
|
||||
|
||||
public function includeCompanyUser(Company $company)
|
||||
{
|
||||
$transformer = new CompanyUserTransformer($this->serializer);
|
||||
|
||||
return $this->includeItem($company->company_users->where('user_id', auth()->user()->id)->first(), $transformer, CompanyUser::class);
|
||||
|
||||
}
|
||||
|
||||
public function includeUsers(Company $company)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user