1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-09 20:52:56 +01:00

Add ability to hook into Product view

Adds ability to define dynamic relations on entities
This commit is contained in:
Christopher Di Carlo 2018-05-03 09:46:55 -04:00
parent 44ee4e61c5
commit 1b2de482d0
7 changed files with 135 additions and 0 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace App\Events;
use App\Models\Product;
use Illuminate\Queue\SerializesModels;
class ProductWasCreated extends Event
{
use SerializesModels;
/**
* @var Product
*/
public $product;
/**
* @var array
**/
public $input;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Product $product, $input = null)
{
$this->product = $product;
$this->input = $input;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Events;
use App\Models\Product;
use Illuminate\Queue\SerializesModels;
class ProductWasDeleted extends Event
{
use SerializesModels;
/**
* @var Product
*/
public $product;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Product $product)
{
$this->product = $product;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Events;
use App\Models\Product;
use Illuminate\Queue\SerializesModels;
class ProductWasUpdated extends Event
{
use SerializesModels;
/**
* @var Product
*/
public $product;
/**
* @var array
**/
public $input;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Product $product, $input = null)
{
$this->product = $product;
$this->input = $input;
}
}

View File

@ -449,4 +449,20 @@ class EntityModel extends Eloquent
return $this->id == $obj->id && $this->getEntityType() == $obj->entityType;
}
/**
* @param $method
* @param $params
*/
public function __call($method, $params)
{
$entityType = $this->getEntityType();
if ($entityType) {
$config = implode('.', ['modules.relations.' . $entityType, $method]);
if (config()->has($config)) {
$function = config()->get($config);
return $function($this);
}
}
return parent::__call($method, $params);
}
}

View File

@ -3,6 +3,8 @@
namespace App\Ninja\Repositories;
use App\Models\Product;
use App\Events\ProductWasCreated;
use App\Events\ProductWasUpdated;
use Utils;
use DB;
@ -72,6 +74,11 @@ class ProductRepository extends BaseRepository
$product->qty = isset($data['qty']) ? Utils::parseFloat($data['qty']) : 1;
$product->save();
if ($publicId) {
event(new ProductWasUpdated($product, $data));
} else {
event(new ProductWasCreated($product, $data));
}
return $product;
}

View File

@ -173,4 +173,7 @@ return [
'register' => [
'translations' => true,
],
'relations' => [
// all dynamic relations registered from modules are added here
],
];

View File

@ -41,6 +41,25 @@
</div>
</div>
@foreach(Module::getOrdered() as $module)
@if(View::exists($module->alias . '::accounts.product'))
<div class="row">
<div class="col-lg-10 col-lg-offset-1">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title in-white">
<i class="fa fa-{{ $module->icon }}"></i>
{{ $module->name }}
</h3>
</div>
<div class="panel-body form-padding-right">
@includeIf($module->alias . '::accounts.product')
</div>
</div>
</div>
</div>
@endif
@endforeach
<center class="buttons">
{!! Button::normal(trans('texts.cancel'))->large()->asLinkTo(HTMLUtils::previousUrl('/products'))->appendIcon(Icon::create('remove-circle')) !!}
{!! Button::success(trans('texts.save'))->submit()->large()->appendIcon(Icon::create('floppy-disk')) !!}