Skip to content

Commit cd98976

Browse files
committed
Added HasUploads trait and remove uploads method for models
Signed-off-by: snipe <[email protected]>
1 parent 6cbdefe commit cd98976

File tree

9 files changed

+35
-128
lines changed

9 files changed

+35
-128
lines changed

app/Models/Accessory.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Helpers\Helper;
66
use App\Models\Traits\Acceptable;
7+
use App\Models\Traits\HasUploads;
78
use App\Models\Traits\Searchable;
89
use App\Presenters\Presentable;
910
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -22,6 +23,7 @@ class Accessory extends SnipeModel
2223

2324
protected $presenter = \App\Presenters\AccessoryPresenter::class;
2425
use CompanyableTrait;
26+
use HasUploads;
2527
use Loggable, Presentable;
2628
use SoftDeletes;
2729

@@ -102,24 +104,6 @@ class Accessory extends SnipeModel
102104
];
103105

104106

105-
106-
/**
107-
* Establishes the accessories -> action logs -> uploads relationship
108-
*
109-
* @author A. Gianotto <[email protected]>
110-
* @since [v6.1.13]
111-
* @return \Illuminate\Database\Eloquent\Relations\Relation
112-
*/
113-
public function uploads()
114-
{
115-
return $this->hasMany(\App\Models\Actionlog::class, 'item_id')
116-
->where('item_type', '=', self::class)
117-
->where('action_type', '=', 'uploaded')
118-
->whereNotNull('filename')
119-
->orderBy('created_at', 'desc');
120-
}
121-
122-
123107
/**
124108
* Establishes the accessory -> supplier relationship
125109
*

app/Models/Asset.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,17 @@
77
use App\Helpers\Helper;
88
use App\Http\Traits\UniqueUndeletedTrait;
99
use App\Models\Traits\Acceptable;
10+
use App\Models\Traits\HasUploads;
1011
use App\Models\Traits\Searchable;
1112
use App\Presenters\Presentable;
1213
use App\Presenters\AssetPresenter;
13-
use Illuminate\Support\Facades\Auth;
1414
use Carbon\Carbon;
15-
use Illuminate\Support\Facades\DB;
1615
use Illuminate\Database\Eloquent\Builder;
1716
use Illuminate\Database\Eloquent\Factories\HasFactory;
1817
use Illuminate\Database\Eloquent\SoftDeletes;
1918
use Illuminate\Support\Facades\Storage;
2019
use Watson\Validating\ValidatingTrait;
2120
use Illuminate\Database\Eloquent\Casts\Attribute;
22-
use Illuminate\Database\Eloquent\Model;
2321

2422
/**
2523
* Model for Assets.
@@ -33,6 +31,7 @@ class Asset extends Depreciable
3331
protected $with = ['model', 'adminuser'];
3432

3533
use CompanyableTrait;
34+
use HasUploads;
3635
use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait;
3736

3837
public const LOCATION = 'location';
@@ -472,22 +471,6 @@ public function get_depreciation()
472471
}
473472

474473

475-
/**
476-
* Get uploads for this asset
477-
*
478-
* @author [A. Gianotto] [<[email protected]>]
479-
* @since [v4.0]
480-
* @return \Illuminate\Database\Eloquent\Relations\Relation
481-
*/
482-
public function uploads()
483-
{
484-
return $this->hasMany('\App\Models\Actionlog', 'item_id')
485-
->where('item_type', '=', Asset::class)
486-
->where('action_type', '=', 'uploaded')
487-
->whereNotNull('filename')
488-
->orderBy('created_at', 'desc');
489-
}
490-
491474
/**
492475
* Determines whether the asset is checked out to a user
493476
*

app/Models/AssetModel.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use App\Models\Traits\HasUploads;
56
use App\Models\Traits\Searchable;
67
use App\Presenters\Presentable;
78
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -24,6 +25,7 @@ class AssetModel extends SnipeModel
2425
use SoftDeletes;
2526
use Loggable, Requestable, Presentable;
2627
use TwoColumnUniqueUndeletedTrait;
28+
use HasUploads;
2729

2830
/**
2931
* Whether the model should inject its identifier to the unique
@@ -209,21 +211,6 @@ public function isDeletable()
209211
&& ($this->deleted_at == '');
210212
}
211213

212-
/**
213-
* Get uploads for this model
214-
*
215-
* @author [A. Gianotto] [<[email protected]>]
216-
* @since [v4.0]
217-
* @return \Illuminate\Database\Eloquent\Relations\Relation
218-
*/
219-
public function uploads()
220-
{
221-
return $this->hasMany('\App\Models\Actionlog', 'item_id')
222-
->where('item_type', '=', AssetModel::class)
223-
->where('action_type', '=', 'uploaded')
224-
->whereNotNull('filename')
225-
->orderBy('created_at', 'desc');
226-
}
227214

228215
/**
229216
* Get user who created the item

app/Models/Component.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use App\Models\Traits\HasUploads;
56
use App\Models\Traits\Searchable;
67
use App\Presenters\Presentable;
78
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -20,6 +21,7 @@ class Component extends SnipeModel
2021

2122
protected $presenter = \App\Presenters\ComponentPresenter::class;
2223
use CompanyableTrait;
24+
use HasUploads;
2325
use Loggable, Presentable;
2426
use SoftDeletes;
2527
protected $casts = [
@@ -113,21 +115,6 @@ public function isDeletable()
113115
&& ($this->deleted_at == '');
114116
}
115117

116-
/**
117-
* Establishes the components -> action logs -> uploads relationship
118-
*
119-
* @author A. Gianotto <[email protected]>
120-
* @since [v6.1.13]
121-
* @return \Illuminate\Database\Eloquent\Relations\Relation
122-
*/
123-
public function uploads()
124-
{
125-
return $this->hasMany(\App\Models\Actionlog::class, 'item_id')
126-
->where('item_type', '=', self::class)
127-
->where('action_type', '=', 'uploaded')
128-
->whereNotNull('filename')
129-
->orderBy('created_at', 'desc');
130-
}
131118

132119

133120
/**

app/Models/Consumable.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Helpers\Helper;
66
use App\Models\Traits\Acceptable;
7+
use App\Models\Traits\HasUploads;
78
use App\Models\Traits\Searchable;
89
use App\Presenters\Presentable;
910
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -29,6 +30,7 @@ class Consumable extends SnipeModel
2930
use Loggable, Presentable;
3031
use SoftDeletes;
3132
use Acceptable;
33+
use HasUploads;
3234

3335
protected $table = 'consumables';
3436
protected $casts = [
@@ -111,21 +113,6 @@ class Consumable extends SnipeModel
111113
];
112114

113115

114-
/**
115-
* Establishes the components -> action logs -> uploads relationship
116-
*
117-
* @author A. Gianotto <[email protected]>
118-
* @since [v6.1.13]
119-
* @return \Illuminate\Database\Eloquent\Relations\Relation
120-
*/
121-
public function uploads()
122-
{
123-
return $this->hasMany(Actionlog::class, 'item_id')
124-
->where('item_type', '=', self::class)
125-
->where('action_type', '=', 'uploaded')
126-
->whereNotNull('filename')
127-
->orderBy('created_at', 'desc');
128-
}
129116

130117

131118
/**

app/Models/License.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Models;
44

55
use App\Helpers\Helper;
6+
use App\Models\Traits\HasUploads;
67
use App\Models\Traits\Searchable;
78
use App\Presenters\Presentable;
89
use Carbon\Carbon;
@@ -21,6 +22,7 @@ class License extends Depreciable
2122

2223
use SoftDeletes;
2324
use CompanyableTrait;
25+
use HasUploads;
2426
use Loggable, Presentable;
2527
protected $injectUniqueIdentifier = true;
2628
use ValidatingTrait;
@@ -411,21 +413,6 @@ public function assetlog()
411413
->orderBy('created_at', 'desc');
412414
}
413415

414-
/**
415-
* Establishes the license -> action logs -> uploads relationship
416-
*
417-
* @author A. Gianotto <[email protected]>
418-
* @since [v2.0]
419-
* @return \Illuminate\Database\Eloquent\Relations\Relation
420-
*/
421-
public function uploads()
422-
{
423-
return $this->hasMany(\App\Models\Actionlog::class, 'item_id')
424-
->where('item_type', '=', self::class)
425-
->where('action_type', '=', 'uploaded')
426-
->whereNotNull('filename')
427-
->orderBy('created_at', 'desc');
428-
}
429416

430417

431418
/**

app/Models/Location.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Models\Asset;
77
use App\Models\Setting;
88
use App\Models\SnipeModel;
9+
use App\Models\Traits\HasUploads;
910
use App\Models\Traits\Searchable;
1011
use App\Models\User;
1112
use App\Presenters\Presentable;
@@ -25,6 +26,7 @@ class Location extends SnipeModel
2526
protected $presenter = \App\Presenters\LocationPresenter::class;
2627
use Presentable;
2728
use SoftDeletes;
29+
use HasUploads;
2830

2931
protected $table = 'locations';
3032
protected $rules = [
@@ -300,22 +302,6 @@ public function setLdapOuAttribute($ldap_ou)
300302
return $this->attributes['ldap_ou'] = empty($ldap_ou) ? null : $ldap_ou;
301303
}
302304

303-
/**
304-
* Get uploads for this location
305-
*
306-
* @author [A. Gianotto] [<[email protected]>]
307-
* @since [v4.0]
308-
* @return \Illuminate\Database\Eloquent\Relations\Relation
309-
*/
310-
public function uploads()
311-
{
312-
return $this->hasMany('\App\Models\Actionlog', 'item_id')
313-
->where('item_type', '=', Location::class)
314-
->where('action_type', '=', 'uploaded')
315-
->whereNotNull('filename')
316-
->orderBy('created_at', 'desc');
317-
}
318-
319305

320306
/**
321307
* Query builder scope to order on parent

app/Models/Traits/HasUploads.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Models\Traits;
4+
5+
use App\Models\Actionlog;
6+
7+
trait HasUploads
8+
{
9+
10+
public function uploads() {
11+
return $this->hasMany(Actionlog::class, 'item_id')
12+
->where('item_type', self::class)
13+
->where('action_type', '=', 'uploaded')
14+
->whereNotNull('filename');
15+
}
16+
17+
18+
}

app/Models/User.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Http\Traits\UniqueUndeletedTrait;
66
use App\Models\Traits\Searchable;
7+
use App\Models\Traits\HasUploads;
78
use App\Presenters\Presentable;
89
use Illuminate\Auth\Authenticatable;
910
use Illuminate\Auth\Passwords\CanResetPassword;
@@ -27,6 +28,7 @@ class User extends SnipeModel implements AuthenticatableContract, AuthorizableCo
2728
{
2829
use HasFactory;
2930
use CompanyableTrait;
31+
use HasUploads;
3032

3133
protected $presenter = \App\Presenters\UserPresenter::class;
3234
use SoftDeletes, ValidatingTrait, Loggable;
@@ -530,21 +532,7 @@ public function assetlog()
530532
return $this->hasMany(\App\Models\Asset::class, 'id')->withTrashed();
531533
}
532534

533-
/**
534-
* Establishes the user -> uploads relationship
535-
*
536-
* @author A. Gianotto <[email protected]>
537-
* @since [v3.0]
538-
* @return \Illuminate\Database\Eloquent\Relations\Relation
539-
*/
540-
public function uploads()
541-
{
542-
return $this->hasMany(\App\Models\Actionlog::class, 'item_id')
543-
->where('item_type', self::class)
544-
->where('action_type', '=', 'uploaded')
545-
->whereNotNull('filename')
546-
->orderBy('created_at', 'desc');
547-
}
535+
548536

549537
/**
550538
* Establishes the user -> acceptances relationship

0 commit comments

Comments
 (0)