Completed
Push — development ( 877883...280d38 )
by Ashutosh
10:04
created

User::getBussinessAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 10
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App;
4
5
use Illuminate\Auth\Authenticatable;
6
use Illuminate\Auth\Passwords\CanResetPassword;
7
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
8
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
9
use Illuminate\Database\Eloquent\Model;
10
use Spatie\Activitylog\Models\Activity;
11
use Spatie\Activitylog\Traits\LogsActivity;
12
13
//use Laravel\Cashier\Billable;
14
//use LinkThrow\Billing\CustomerBillableTrait;
15
//use App\Model\Common\Website;
16
17
class User extends Model implements AuthenticatableContract, CanResetPasswordContract
18
{
19
    use Authenticatable,
20
    CanResetPassword;
21
    use LogsActivity;
22
23
    // use Billable;
24
    // use CustomerBillableTrait;
25
26
    /**
27
     * The database table used by the model.
28
     *
29
     * @var string
30
     */
31
    protected $table = 'users';
32
33
    /**
34
     * The attributes that are mass assignable.
35
     *
36
     * @var array
37
     */
38
    protected $fillable = ['first_name', 'last_name', 'user_name', 'company', 'zip',
39
        'state', 'town', 'mobile',
40
        'email', 'password', 'role', 'active', 'profile_pic',
41
        'address', 'country', 'currency', 'currency_symbol', 'timezone_id', 'mobile_code', 'bussiness',
42
        'company_type', 'company_size', 'ip', 'mobile_verified', 'position', 'skype', 'manager', ];
43
44
    protected static $logName = 'User';
45
    protected static $logAttributes = ['first_name', 'last_name', 'user_name', 'company', 'zip',
46
        'state', 'town', 'mobile',
47
        'email', 'password', 'role', 'active', 'profile_pic',
48
        'address', 'country', 'currency', 'timezone_id', 'mobile_code', 'bussiness',
49
        'company_type', 'company_size', 'ip', 'mobile_verified', 'position', 'skype', 'manager', ];
50
51
    protected static $logOnlyDirty = true;
52
53
    public function getDescriptionForEvent(string $eventName): string
54
    {
55
        $lastActivity = Activity::all()->last(); //returns the last logged activity
56
        if ($eventName == 'updated') {
57
            $this->enableLogging();
58
59
            return 'User  <strong> '.$this->first_name.' '.$this->last_name.'</strong> was updated';
60
        }
61
62
        if ($eventName == 'deleted') {
63
            return 'User <strong> '.$this->first_name.' '.$this->last_name.' </strong> was deleted';
64
        }
65
66
        return '';
67
    }
68
69
    /**
70
     * The attributes excluded from the model's JSON form.
71
     *
72
     * @var array
73
     */
74
    protected $hidden = ['password', 'remember_token'];
75
76
    public function order()
77
    {
78
        return $this->hasMany('App\Model\Order\Order', 'client');
79
    }
80
81
    public function comments()
82
    {
83
        return $this->hasMany('App\Comment');
84
    }
85
86
    public function subscription()
87
    {
88
        // Return an Eloquent relationship.
89
        return $this->hasMany('App\Model\Product\Subscription');
90
    }
91
92
    public function invoiceItem()
93
    {
94
        return $this->hasManyThrough('App\Model\Order\InvoiceItem', 'App\Model\Order\Invoice');
95
    }
96
97
    public function orderRelation()
98
    {
99
        return $this->hasManyThrough('App\Model\Order\OrderInvoiceRelation', 'App\Model\Order\Invoice');
100
    }
101
102
    public function invoice()
103
    {
104
        return $this->hasMany('App\Model\Order\Invoice');
105
    }
106
107
    public function timezone()
108
    {
109
        return $this->belongsTo('App\Model\Common\Timezone');
110
    }
111
112
    // public function getCreatedAtAttribute($value)
113
    // {
114
    //     if (\Auth::user()) {
115
    //         $tz = \Auth::user()->timezone()->first()->name;
116
    //         $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value, 'UTC');
117
118
    //         return $date->setTimezone($tz);
119
    //     }
120
121
    //     return $value;
122
    // }
123
124
    public function getProfilePicAttribute($value)
125
    {
126
        $image = \Gravatar::src($this->attributes['email']);
127
        if ($value) {
128
            $file = public_path('common/images/users/'.$value);
129
            if (is_file($file)) {
130
                $mime = \File::mimeType($file);
131
                $extension = \File::extension($file);
132
                if (mime($mime) == 'image' && mime($extension) == 'image') {
133
                    $image = asset('common/images/users/'.$value);
134
                } else {
135
                    unlink($file);
136
                }
137
            }
138
        }
139
140
        return $image;
141
    }
142
143
    public function payment()
144
    {
145
        return $this->hasMany('App\Model\Order\Payment');
146
    }
147
148
    public function setCountryAttribute($value)
149
    {
150
        $value = strtoupper($value);
151
        $this->attributes['country'] = $value;
152
    }
153
154
    public function getBussinessAttribute()
155
    {
156
        $short = $this->attributes['bussiness'];
157
        $name = '--';
158
        $bussiness = \App\Model\Common\Bussiness::where('short', $short)->first();
159
        if ($bussiness) {
160
            $name = $bussiness->name;
161
        }
162
163
        return $name;
164
    }
165
166
    public function getCompanyTypeAttribute()
167
    {
168
        $short = $this->attributes['company_type'];
169
        $name = '--';
170
        $company = \DB::table('company_types')->where('short', $short)->first();
171
        if ($company) {
172
            $name = $company->name;
173
        }
174
175
        return $name;
176
    }
177
178
    public function delete()
179
    {
180
        $this->invoiceItem()->delete();
181
        $this->orderRelation()->delete();
182
        $this->invoice()->delete();
183
        $this->order()->delete();
184
        $this->subscription()->delete();
185
186
        return parent::delete();
187
    }
188
189
    public function manager()
190
    {
191
        return $this->belongsTo('App\User', 'manager');
192
    }
193
194
    // public function save(array $options = [])
195
    // {
196
197
    //     $changed = $this->isDirty() ? $this->getDirty() : false;
198
    //     parent::save($options);
199
    //     $role = $this->role;
200
    //     // if ($changed && checkArray('manager', $changed) && $role == 'user') {
201
    //     //     $auth = new Http\Controllers\Auth\AuthController();
202
    //     //     $auth->accountManagerMail($this);
203
    //     // }
204
    // }
205
}
206