Completed
Pull Request — development (#635)
by Ashutosh
09:42
created

User::comments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 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', '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 ($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 comments()
88
    {
89
        return $this->hasMany('App\Comment');
90
    }
91
92
    public function subscription()
93
    {
94
        // Return an Eloquent relationship.
95
        return $this->hasMany('App\Model\Product\Subscription');
96
    }
97
98
    public function invoiceItem()
99
    {
100
        return $this->hasManyThrough('App\Model\Order\InvoiceItem', 'App\Model\Order\Invoice');
101
    }
102
103
    public function orderRelation()
104
    {
105
        return $this->hasManyThrough('App\Model\Order\OrderInvoiceRelation', 'App\Model\Order\Invoice');
106
    }
107
108
    public function invoice()
109
    {
110
        return $this->hasMany('App\Model\Order\Invoice');
111
    }
112
113
    public function timezone()
114
    {
115
        return $this->belongsTo('App\Model\Common\Timezone');
116
    }
117
118
    // public function getCreatedAtAttribute($value)
119
    // {
120
    //     if (\Auth::user()) {
121
    //         $tz = \Auth::user()->timezone()->first()->name;
122
    //         $date = \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $value, 'UTC');
123
124
    //         return $date->setTimezone($tz);
125
    //     }
126
127
    //     return $value;
128
    // }
129
130
    public function getProfilePicAttribute($value)
131
    {
132
        $image = \Gravatar::src($this->attributes['email']);
133
        if ($value) {
134
            $file = public_path('dist/app/users/'.$value);
135
            if (is_file($file)) {
136
                $mime = \File::mimeType($file);
137
                $extension = \File::extension($file);
138
                if ($mime == 'image' && $extension == 'image') {
139
                    $image = asset('dist/app/users/'.$value);
140
                } else {
141
                    unlink($file);
142
                }
143
            }
144
        }
145
146
        return $image;
147
    }
148
149
    public function payment()
150
    {
151
        return $this->hasMany('App\Model\Order\Payment');
152
    }
153
154
    public function setCountryAttribute($value)
155
    {
156
        $value = strtoupper($value);
157
        $this->attributes['country'] = $value;
158
    }
159
160
    public function bussiness()
161
    {
162
        $short = $this->attributes['bussiness'];
163
        $name = '--';
164
        $bussiness = \App\Model\Common\Bussiness::where('short', $short)->first();
165
        if ($bussiness) {
166
            $name = $bussiness->name;
167
        }
168
169
        return $name;
170
    }
171
172
    public function delete()
173
    {
174
        $this->invoiceItem()->delete();
175
        $this->orderRelation()->delete();
176
        $this->invoice()->delete();
177
        $this->order()->delete();
178
        $this->subscription()->delete();
179
180
        return parent::delete();
181
    }
182
183
    public function manager()
184
    {
185
        return $this->belongsTo('App\User', 'manager');
186
    }
187
188
    // public function save(array $options = [])
189
    // {
190
191
    //     $changed = $this->isDirty() ? $this->getDirty() : false;
192
    //     parent::save($options);
193
    //     $role = $this->role;
194
    //     // if ($changed && checkArray('manager', $changed) && $role == 'user') {
195
    //     //     $auth = new Http\Controllers\Auth\AuthController();
196
    //     //     $auth->accountManagerMail($this);
197
    //     // }
198
    // }
199
}
200