Skip to content

Commit

Permalink
View presenters (#3099)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
dmeltzer authored and snipe committed Dec 24, 2016
1 parent 40615f9 commit 02c1a45
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 78 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/StatuslabelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public function getDatatable()
$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
Expand Down
14 changes: 11 additions & 3 deletions app/Presenters/AssetModelPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function forDataTable()
$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;
Expand All @@ -51,8 +51,8 @@ public function forDataTable()
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;
Expand All @@ -74,6 +74,14 @@ public function note()

}

public function eolText()
{
if($this->eol) {
return $this->eol.' '.trans('general.months');
}
return '';
}

/**
* Pretty name for this model
* @return string
Expand Down
38 changes: 16 additions & 22 deletions app/Presenters/AssetPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,24 +71,10 @@ public function forDataTable($all_custom_fields)
$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'] = '';
Expand All @@ -108,7 +94,7 @@ public function forDataTable($all_custom_fields)
$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]);
Expand All @@ -120,9 +106,9 @@ public function forDataTable($all_custom_fields)
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
Expand Down Expand Up @@ -172,6 +158,14 @@ public function nameUrl()
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
Expand Down
12 changes: 6 additions & 6 deletions app/Presenters/ComponentPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@ public function forDataTable()
'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;
Expand Down
22 changes: 11 additions & 11 deletions app/Presenters/ConsumablePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public function forDataTable()
$actions .='</nobr>';

$results = [
'actions' => $actions,
'category' => $this->categoryUrl(),
'companyName' => $this->companyUrl(),
'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',
'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_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() : '',
'purchase_date' => $this->purchase_date,
'qty' => $this->qty,
];
return $results;
}
Expand Down
24 changes: 12 additions & 12 deletions app/Presenters/LicensePresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ public function forDataTable()
$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;
Expand Down
24 changes: 12 additions & 12 deletions app/Presenters/LocationPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ public function forDataTable()
$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;
Expand Down
10 changes: 5 additions & 5 deletions app/Presenters/ManufacturerPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ public function forDataTable()
$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;
Expand Down
46 changes: 46 additions & 0 deletions app/Presenters/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

abstract class Presenter
{

/**
* @var SnipeModel
*/
Expand All @@ -21,6 +22,50 @@ public function __construct(SnipeModel $model)
$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)) {
Expand All @@ -34,4 +79,5 @@ public function __call($method, $args)
{
return $this->model->$method($args);
}

}
18 changes: 12 additions & 6 deletions app/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@ public function forDataTable($status = null)
$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() : '',
Expand All @@ -86,12 +83,21 @@ public function forDataTable($status = null)
'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.
Expand Down

0 comments on commit 02c1a45

Please sign in to comment.