From 02c1a45025489a4f4e37d1813ff4c65ac8d5b257 Mon Sep 17 00:00:00 2001 From: Daniel Meltzer Date: Fri, 23 Dec 2016 22:23:07 -0500 Subject: [PATCH] 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. --- .../Controllers/StatuslabelsController.php | 2 +- app/Presenters/AssetModelPresenter.php | 14 ++++-- app/Presenters/AssetPresenter.php | 38 +++++++-------- app/Presenters/ComponentPresenter.php | 12 ++--- app/Presenters/ConsumablePresenter.php | 22 ++++----- app/Presenters/LicensePresenter.php | 24 +++++----- app/Presenters/LocationPresenter.php | 24 +++++----- app/Presenters/ManufacturerPresenter.php | 10 ++-- app/Presenters/Presenter.php | 46 +++++++++++++++++++ app/Presenters/UserPresenter.php | 18 +++++--- 10 files changed, 132 insertions(+), 78 deletions(-) diff --git a/app/Http/Controllers/StatuslabelsController.php b/app/Http/Controllers/StatuslabelsController.php index ca3be474ebe0..1f557bdf1526 100755 --- a/app/Http/Controllers/StatuslabelsController.php +++ b/app/Http/Controllers/StatuslabelsController.php @@ -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 diff --git a/app/Presenters/AssetModelPresenter.php b/app/Presenters/AssetModelPresenter.php index 6938fcf58398..23268a3d11fd 100644 --- a/app/Presenters/AssetModelPresenter.php +++ b/app/Presenters/AssetModelPresenter.php @@ -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; @@ -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; @@ -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 diff --git a/app/Presenters/AssetPresenter.php b/app/Presenters/AssetPresenter.php index 18d1a76067d8..56596c614402 100644 --- a/app/Presenters/AssetPresenter.php +++ b/app/Presenters/AssetPresenter.php @@ -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'] = ''; @@ -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]); @@ -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 @@ -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 diff --git a/app/Presenters/ComponentPresenter.php b/app/Presenters/ComponentPresenter.php index 6f965da90237..439f7bc78238 100644 --- a/app/Presenters/ComponentPresenter.php +++ b/app/Presenters/ComponentPresenter.php @@ -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; diff --git a/app/Presenters/ConsumablePresenter.php b/app/Presenters/ConsumablePresenter.php index fe558b785333..be8f09931c4a 100644 --- a/app/Presenters/ConsumablePresenter.php +++ b/app/Presenters/ConsumablePresenter.php @@ -39,21 +39,21 @@ public function forDataTable() $actions .=''; $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; } diff --git a/app/Presenters/LicensePresenter.php b/app/Presenters/LicensePresenter.php index 8adc48b23468..a761864558ab 100644 --- a/app/Presenters/LicensePresenter.php +++ b/app/Presenters/LicensePresenter.php @@ -46,22 +46,22 @@ public function forDataTable() $actions .=''; $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; diff --git a/app/Presenters/LocationPresenter.php b/app/Presenters/LocationPresenter.php index 9d1d40f11dee..214a301b9aef 100644 --- a/app/Presenters/LocationPresenter.php +++ b/app/Presenters/LocationPresenter.php @@ -29,19 +29,19 @@ public function forDataTable() $actions .= ''; $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; diff --git a/app/Presenters/ManufacturerPresenter.php b/app/Presenters/ManufacturerPresenter.php index b25c7a019358..e695b55cb930 100644 --- a/app/Presenters/ManufacturerPresenter.php +++ b/app/Presenters/ManufacturerPresenter.php @@ -30,13 +30,13 @@ public function forDataTable() $actions .= ''; $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; diff --git a/app/Presenters/Presenter.php b/app/Presenters/Presenter.php index ba8bbcfd938b..877568da1975 100644 --- a/app/Presenters/Presenter.php +++ b/app/Presenters/Presenter.php @@ -7,6 +7,7 @@ abstract class Presenter { + /** * @var SnipeModel */ @@ -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)) { @@ -34,4 +79,5 @@ public function __call($method, $args) { return $this->model->$method($args); } + } diff --git a/app/Presenters/UserPresenter.php b/app/Presenters/UserPresenter.php index c309cb6855fb..0d17aa2836d4 100644 --- a/app/Presenters/UserPresenter.php +++ b/app/Presenters/UserPresenter.php @@ -65,12 +65,9 @@ public function forDataTable($status = null) $result = [ 'id' => $this->id, 'checkbox' => ($status!='deleted') ? '' : '', - 'name' => $this->present()->fullName(), + 'name' => $this->fullName(), 'jobtitle' => $this->jobtitle, - 'email' => ($this->email!='') ? - '' - .'' - .'' : '', + '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 @@ 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') ? '' : '', 'actions' => $actions ?: '', - 'companyName' => $this->company ? $this->company->name : '' + 'companyName' => $this->companyUrl() ]; return $result; } + + public function emailLink() + { + if ($this->email) { + return ''.$this->email.'' + .''; + } + return ''; + } /** * Returns the user full name, it simply concatenates * the user first and last name.