Completed
Push — development ( 47d06a...03fb4f )
by Ashutosh
13:29
created

User::manager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
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', '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 ($lastActivity->description == 'Logged In') {
57
        //     $this->disableLogging();
58
        // }
59
        if ($eventName == 'updated') {
60
            $this->enableLogging();
61
62
            return 'User  <strong> '.$this->first_name.' '.$this->last_name.'</strong> was updated';
63
        }
64
65
        if ($eventName == 'deleted') {
66
            return 'User <strong> '.$this->first_name.' '.$this->last_name.' </strong> was deleted';
67
        }
68
69
        return '';
70
71
        // return "Product  has been {$eventName}";
72
         // \Auth::user()->activity;
73
    }
74
75
    /**
76
     * The attributes excluded from the model's JSON form.
77
     *
78
     * @var array
79
     */
80
    protected $hidden = ['password', 'remember_token'];
81
82
    public function order()
83
    {
84
        return $this->hasMany('App\Model\Order\Order', 'client');
85
    }
86
87
    public function subscription()
88
    {
89
        // Return an Eloquent relationship.
90
        return $this->hasMany('App\Model\Product\Subscription');
91
    }
92
93
    public function invoiceItem()
94
    {
95
        return $this->hasManyThrough('App\Model\Order\InvoiceItem', 'App\Model\Order\Invoice');
96
    }
97
98
    public function orderRelation()
99
    {
100
        return $this->hasManyThrough('App\Model\Order\OrderInvoiceRelation', 'App\Model\Order\Invoice');
101
    }
102
103
    public function invoice()
104
    {
105
        return $this->hasMany('App\Model\Order\Invoice');
106
    }
107
108
    public function timezone()
109
    {
110
        return $this->belongsTo('App\Model\Common\Timezone');
111
    }
112
113
    public function getCreatedAtAttribute($value)
114
    {
115
        if (\Auth::user()) {
116
            $tz = \Auth::user()->timezone()->first()->name;
117
            $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value, 'UTC');
118
119
            return $date->setTimezone($tz);
120
        }
121
122
        return $value;
123
    }
124
125
    public function getProfilePicAttribute($value)
126
    {
127
        $image = \Gravatar::src($this->attributes['email']);
128
        if ($value) {
129
            $file = public_path('dist/app/users/'.$value);
130
            if (is_file($file)) {
131
                $mime = \File::mimeType($file);
132
                $extension = \File::extension($file);
133
                if ($mime == 'image' && $extension == 'image') {
134
                    $image = asset('dist/app/users/'.$value);
135
                } else {
136
                    unlink($file);
137
                }
138
            }
139
        }
140
141
        return $image;
142
    }
143
144
    public function payment()
145
    {
146
        return $this->hasMany('App\Model\Order\Payment');
147
    }
148
149
    public function setCountryAttribute($value)
150
    {
151
        $value = strtoupper($value);
152
        $this->attributes['country'] = $value;
153
    }
154
155
    public function bussiness()
156
    {
157
        $short = $this->attributes['bussiness'];
158
        $name = '--';
159
        $bussiness = \App\Model\Common\Bussiness::where('short', $short)->first();
160
        if ($bussiness) {
161
            $name = $bussiness->name;
162
        }
163
164
        return $name;
165
    }
166
167
    public function delete()
168
    {
169
        $this->invoiceItem()->delete();
170
        $this->orderRelation()->delete();
171
        $this->invoice()->delete();
172
        $this->order()->delete();
173
        $this->subscription()->delete();
174
175
        return parent::delete();
176
    }
177
178
    public function manager()
179
    {
180
        return $this->belongsTo('App\User', 'manager');
181
    }
182
183
    public function save(array $options = [])
184
    {
185
        $changed = $this->isDirty() ? $this->getDirty() : false;
186
        parent::save($options);
187
        $role = $this->role;
188
        // if ($changed && checkArray('manager', $changed) && $role == 'user') {
189
        //     $auth = new Http\Controllers\Auth\AuthController();
190
        //     $auth->accountManagerMail($this);
191
        // }
192
    }
193
}
194