View presenters (#3099)

* Add presenters for models.  Move bootstrap table JSON generation to these presenters, which cleans up controllers a lot.  Move view specific modifications from the models to the presenters as well.

* Fix some issues found by travis and codacy

* Fix a few more issues found while testing.

* Attempt another acceptance test fix

* Try something else

* Maybe..

* Move conditionals out of the datatable method and into dedicated url methods.
This commit is contained in:
Daniel Meltzer 2016-12-23 22:23:07 -05:00 committed by snipe
parent 40615f9504
commit 02c1a45025
10 changed files with 135 additions and 81 deletions

View file

@ -281,7 +281,7 @@ class StatuslabelsController extends Controller
$actions .= Helper::generateDatatableButton('edit', route('statuslabels.edit', $statuslabel->id));
$actions .= Helper::generateDatatableButton(
'delete',
route('statuslabels.destroy', ['statuslabel' => $statuslabel->id]),
route('statuslabels.destroy', $statuslabel->id),
true, /*enabled*/
trans('admin/statuslabels/message.delete.confirm'),
$statuslabel->name

View file

@ -42,7 +42,7 @@ class AssetModelPresenter extends Presenter
$results = [];
$results['id'] = $this->id;
$results['manufacturer'] = $this->model->manufacturer->present()->nameUrl();
$results['manufacturer'] = $this->manufacturerUrl();
$results['name'] = $this->nameUrl();
$results['image'] = $this->imageUrl();
$results['model_number'] = $this->model_number;
@ -51,8 +51,8 @@ class AssetModelPresenter extends Presenter
if(($depreciation = $this->model->depreciation) and $depreciation->id > 0) {
$results['depreciation'] = $depreciation->name.' ('.$depreciation->months.')';
}
$results['category'] = $this->model->category ? $this->model->category->present()->nameUrl() : '';
$results['eol'] = $this->eol ? $this->eol.' '.trans('general.months') : '';
$results['category'] = $this->categoryUrl();
$results['eol'] = $this->eolText();
$results['note'] = $this->note();
$results['fieldset'] = $this->model->fieldset ? link_to_route('custom_fields/model', $this->model->fieldset->name, $this->model->fieldset->id) : '';
$results['actions'] = $actions;
@ -74,6 +74,14 @@ class AssetModelPresenter extends Presenter
}
public function eolText()
{
if($this->eol) {
return $this->eol.' '.trans('general.months');
}
return '';
}
/**
* Pretty name for this model
* @return string

View file

@ -71,24 +71,10 @@ class AssetPresenter extends Presenter
$results['serial'] = $this->serial;
$results['image'] = $this->imageUrl();
// Presets for when conditionals fail.
$results['model'] = 'No Model';
$results['model_number'] = '';
$results['category'] = '';
$results['manufacturer'] = '';
if($model = $this->model->model) {
$results['model'] = $model->present()->nameUrl();
if(!empty($model->model_number)) {
$results['model_number'] = $model->model_number;
}
if ($model->category) {
$results['category'] = $model->category->present()->nameUrl();
}
if($model->manufacturer) {
$results['manufacturer'] = $model->manufacturer->present()->nameUrl();
}
}
$results['model'] = $this->modelUrl();
$results['model_number'] = $this->model->model_number;
$results['category'] = $this->categoryUrl();
$results['manufacturer'] = $this->manufacturerUrl();
$results['status_label'] = '';
$results['assigned_to'] = '';
@ -108,7 +94,7 @@ class AssetPresenter extends Presenter
$results['eol'] = $this->eol_date() ?: '';
$results['purchase_cost'] = Helper::formatCurrencyOutput($this->purchase_cost);
$results['purchase_date'] = $this->purchase_date ?: '';
$results['notes'] = e($this->notes);
$results['notes'] = $this->notes;
$results['order_number'] = '';
if(!empty($this->order_number)) {
$results['order_number'] = link_to_route('hardware.index', $this->order_number, ['order_number' => $this->order_number]);
@ -120,9 +106,9 @@ class AssetPresenter extends Presenter
if(!empty($this->created_at)) {
$results['created_at'] = $this->created_at->format('F j, Y h:iA');
}
$results['companyName'] = $this->model->company ? $this->model->company->name : '';
$results['actions'] = $actions ?: '';
$results['change'] = $inout ?: '';
$results['companyName'] = $this->companyUrl();
$results['actions'] = $actions;
$results['change'] = $inout;
// Custom Field bits
@ -172,6 +158,14 @@ class AssetPresenter extends Presenter
return (string) link_to_route('hardware.show', e($this->name), $this->id);
}
public function modelUrl()
{
if($this->model->model) {
return $this->model->model->present()->nameUrl();
}
return '';
}
/**
* Generate img tag to this items image.
* @return mixed|string

View file

@ -45,16 +45,16 @@ class ComponentPresenter extends Presenter
'id' => $this->id,
'name' => $this->nameUrl(),
'serial_number' => $this->serial,
'location' => ($this->model->location) ? $this->model->location->present()->nameUrl() : '',
'location' => $this->locationUrl(),
'qty' => number_format($this->qty),
'min_amt' => e($this->min_amt),
'category' => ($this->model->category) ? $this->model->category->present()->nameUrl() : 'Missing category',
'min_amt' => e($this->min_amt),
'category' => $this->categoryUrl(),
'order_number' => $this->order_number,
'purchase_date' => $this->purchase_date,
'purchase_cost' => Helper::formatCurrencyOutput($this->purchase_cost),
'purchase_date' => $this->purchase_date,
'purchase_cost' => Helper::formatCurrencyOutput($this->purchase_cost),
'numRemaining' => $this->numRemaining(),
'actions' => $actions,
'companyName' => $this->model->company ? $this->model->company->present()->nameUrl() : '',
'companyName' => $this->companyUrl(),
];
return $results;

View file

@ -39,21 +39,21 @@ class ConsumablePresenter extends Presenter
$actions .='</nobr>';
$results = [
'id' => $this->id,
'name' => $this->nameUrl(),
'location' => ($this->model->location) ? $this->model->location->present()->nameUrl() : '',
'min_amt' => $this->min_amt,
'qty' => $this->qty,
'manufacturer' => ($this->model->manufacturer) ? $this->model->manufacturer->present()->nameUrl() : '',
'model_number' => $this->model_number,
'item_no' => $this->item_no,
'category' => ($this->model->category) ? $this->model->category->present()->nameUrl() : 'Missing category',
'order_number' => $this->order_number,
'purchase_date' => $this->purchase_date,
'purchase_cost' => Helper::formatCurrencyOutput($this->purchase_cost),
'numRemaining' => $this->numRemaining(),
'actions' => $actions,
'companyName' => $this->model->company ? $this->model->company->present()->nameUrl() : '',
'category' => $this->categoryUrl(),
'companyName' => $this->companyUrl(),
'id' => $this->id,
'item_no' => $this->item_no,
'location' => $this->locationUrl(),
'manufacturer' => $this->manufacturerUrl(),
'min_amt' => $this->min_amt,
'model_number' => $this->model_number,
'name' => $this->nameUrl(),
'numRemaining' => $this->numRemaining(),
'order_number' => $this->order_number,
'purchase_cost' => Helper::formatCurrencyOutput($this->purchase_cost),
'purchase_date' => $this->purchase_date,
'qty' => $this->qty,
];
return $results;
}

View file

@ -46,22 +46,22 @@ class LicensePresenter extends Presenter
$actions .='</span>';
$results = [
'actions' => $actions,
'company' => $this->companyUrl(),
'expiration_date' => $this->expiration_date,
'id' => $this->id,
'license_email' => $this->license_email,
'license_name' => $this->license_name,
'manufacturer' => $this->manufacturerUrl(),
'name' => $this->nameUrl(),
'notes' => $this->notes,
'order_number' => $this->order_number,
'purchase_cost' => Helper::formatCurrencyOutput($this->purchase_cost),
'purchase_date' => $this->purchase_date,
'purchase_order' => $this->purchase_order,
'remaining' => $this->remaincount(),
'serial' => $this->serialUrl(),
'totalSeats' => $this->model->licenseSeatsCount,
'remaining' => $this->remaincount(),
'license_name' => $this->license_name,
'license_email' => $this->license_email,
'purchase_date' => ($this->purchase_date) ?: '',
'expiration_date' => ($this->expiration_date) ?: '',
'purchase_cost' => Helper::formatCurrencyOutput($this->purchase_cost),
'purchase_order' => ($this->purchase_order) ?: '',
'order_number' => ($this->order_number) ?: '',
'notes' => ($this->notes) ?: '',
'actions' => $actions,
'company' => $this->model->company ? e($this->model->company->present()->nameUrl()) : '',
'manufacturer' => $this->model->manufacturer ? $this->model->manufacturer->present()->nameUrl() : ''
];
return $results;

View file

@ -29,19 +29,19 @@ class LocationPresenter extends Presenter
$actions .= '</nobr>';
$results = [
'id' => $this->id,
'name' => $this->nameUrl(),
'parent' => ($this->model->parent) ? $this->model->parent->present()->nameUrl() : '',
// 'assets' => ($this->assets->count() + $this->assignedassets->count()),
'assets_default' => $this->model->assignedassets()->count(),
'actions' => $actions,
'address' => $this->address,
'assets_checkedout' => $this->model->assets()->count(),
'address' => $this->address,
'city' => $this->city,
'state' => $this->state,
'zip' => $this->zip,
'country' => $this->country,
'currency' => $this->currency,
'actions' => $actions
'assets_default' => $this->model->assignedassets()->count(),
'city' => $this->city,
'country' => $this->country,
'currency' => $this->currency,
'id' => $this->id,
'name' => $this->nameUrl(),
'parent' => ($this->model->parent) ? $this->model->parent->present()->nameUrl() : '',
'state' => $this->state,
'zip' => $this->zip,
// 'assets' => ($this->assets->count() + $this->assignedassets->count()),
];
return $results;

View file

@ -30,13 +30,13 @@ class ManufacturerPresenter extends Presenter
$actions .= '</nobr>';
$results = [
'id' => $this->id,
'name' => $this->nameUrl(),
'assets' => $this->assets()->count(),
'licenses' => $this->licenses()->count(),
'accessories' => $this->accessories()->count(),
'actions' => $actions,
'assets' => $this->assets()->count(),
'consumables' => $this->consumables()->count(),
'actions' => $actions
'id' => $this->id,
'licenses' => $this->licenses()->count(),
'name' => $this->nameUrl(),
];
return $results;

View file

@ -7,6 +7,7 @@ use App\Models\SnipeModel;
abstract class Presenter
{
/**
* @var SnipeModel
*/
@ -21,6 +22,50 @@ abstract class Presenter
$this->model = $model;
}
// Convenience functions for datatables stuff
public function categoryUrl()
{
$model = $this->model;
// Category of Asset belongs to model.
if($model->model) {
$model = $this->model->model;
}
if($model->category) {
return $model->category->present()->nameUrl();
}
return '';
}
public function locationUrl()
{
if ($this->model->location) {
return $this->model->location->present()->nameUrl();
}
return '';
}
public function companyUrl()
{
if ($this->model->company) {
return $this->model->company->present()->nameUrl();
}
return '';
}
public function manufacturerUrl()
{
$model = $this->model;
// Category of Asset belongs to model.
if($model->model) {
$model = $this->model->model;
}
if ($model->manufacturer) {
return $model->manufacturer->present()->nameUrl();
}
return '';
}
public function __get($property)
{
if( method_exists($this, $property)) {
@ -34,4 +79,5 @@ abstract class Presenter
{
return $this->model->$method($args);
}
}

View file

@ -65,12 +65,9 @@ class UserPresenter extends Presenter
$result = [
'id' => $this->id,
'checkbox' => ($status!='deleted') ? '<div class="text-center hidden-xs hidden-sm"><input type="checkbox" name="edit_user['.e($this->id).']" class="one_required"></div>' : '',
'name' => $this->present()->fullName(),
'name' => $this->fullName(),
'jobtitle' => $this->jobtitle,
'email' => ($this->email!='') ?
'<a href="mailto:'.$this->email.'" class="hidden-md hidden-lg">'.$this->email.'</a>'
.'<a href="mailto:'.$this->email.'" class="hidden-xs hidden-sm"><i class="fa fa-envelope"></i></a>'
.'</span>' : '',
'email' => $this->emailLink(),
'username' => $this->username,
'location' => ($this->model->userloc) ? $this->model->userloc->present()->nameUrl() : '',
'manager' => ($this->model->manager) ? $this->manager->present()->nameUrl() : '',
@ -86,12 +83,21 @@ class UserPresenter extends Presenter
'created_at' => ($this->model->created_at!='') ? e($this->model->created_at->format('F j, Y h:iA')) : '',
'activated' => ($this->activated=='1') ? '<i class="fa fa-check text-success"></i>' : '<i class="fa fa-times text-danger"></i>',
'actions' => $actions ?: '',
'companyName' => $this->company ? $this->company->name : ''
'companyName' => $this->companyUrl()
];
return $result;
}
public function emailLink()
{
if ($this->email) {
return '<a href="mailto:'.$this->email.'">'.$this->email.'</a>'
.'<a href="mailto:'.$this->email.'" class="hidden-xs hidden-sm"><i class="fa fa-envelope"></i></a>';
}
return '';
}
/**
* Returns the user full name, it simply concatenates
* the user first and last name.