1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Models/ClientContact.php

185 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2019-05-11 05:32:07 +02:00
/**
* Invoice Ninja (https://invoiceninja.com)
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
2019-05-11 05:32:07 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\Models;
2019-07-08 02:08:57 +02:00
use App\Models\Company;
use App\Models\Language;
2019-07-08 02:08:57 +02:00
use App\Models\User;
use App\Notifications\ClientContactResetPassword as ResetPasswordNotification;
use App\Notifications\ClientContactResetPassword;
use App\Utils\Traits\MakesHash;
use Hashids\Hashids;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Facades\Cache;
2019-08-08 10:13:32 +02:00
use Illuminate\Support\Facades\Log;
use Laracasts\Presenter\PresentableTrait;
class ClientContact extends Authenticatable implements HasLocalePreference
{
use Notifiable;
use MakesHash;
use PresentableTrait;
use SoftDeletes;
/* Used to authenticate a contact */
protected $guard = 'contact';
/* Allow microtime timestamps */
protected $dateFormat = 'Y-m-d H:i:s.u';
2019-08-14 02:15:21 +02:00
protected $presenter = 'App\Models\Presenters\ClientContactPresenter';
protected $dates = [
'deleted_at'
];
2020-03-23 18:10:42 +01:00
2019-08-02 02:31:48 +02:00
protected $appends = [
'hashed_id'
];
2019-08-14 02:15:21 +02:00
protected $with = [
// 'client',
// 'company'
2019-08-14 02:15:21 +02:00
];
2019-10-02 11:04:27 +02:00
protected $casts = [
'updated_at' => 'timestamp',
'created_at' => 'timestamp',
'deleted_at' => 'timestamp',
];
protected $hidden = [
'password',
'remember_token',
2019-08-08 13:07:26 +02:00
'user_id',
'company_id',
'client_id',
'google_2fa_secret',
'id',
'oauth_provider_id',
'oauth_user_id',
'token',
];
protected $fillable = [
'first_name',
'last_name',
'phone',
'custom_value1',
'custom_value2',
'custom_value3',
'custom_value4',
'email',
'is_primary',
];
2020-03-23 18:10:42 +01:00
public function getEntityType()
{
return ClientContact::class;
}
2019-08-14 02:15:21 +02:00
public function getHashedIdAttribute()
{
return $this->encodePrimaryKey($this->id);
}
2020-03-23 18:10:42 +01:00
/**/
public function getRouteKeyName()
{
return 'contact_id';
}
public function getContactIdAttribute()
{
return $this->encodePrimaryKey($this->id);
}
2019-08-08 10:13:32 +02:00
public function setAvatarAttribute($value)
2019-08-08 02:22:54 +02:00
{
if (!filter_var($value, FILTER_VALIDATE_URL) && $value) {
2019-08-08 10:13:32 +02:00
$this->attributes['avatar'] = url('/') . $value;
} else {
2019-08-08 10:13:32 +02:00
$this->attributes['avatar'] = $value;
}
2019-08-08 02:22:54 +02:00
}
public function client()
{
return $this->belongsTo(Client::class)->withTrashed();
}
public function primary_contact()
{
2019-07-10 03:42:34 +02:00
return $this->where('is_primary', true);
}
2019-07-08 02:08:57 +02:00
public function company()
{
return $this->belongsTo(Company::class);
}
public function user()
{
return $this->belongsTo(User::class)->withTrashed();
2019-07-08 02:08:57 +02:00
}
public function sendPasswordResetNotification($token)
{
$this->notify(new ClientContactResetPassword($token));
}
public function preferredLocale()
{
$languages = Cache::get('languages');
2020-03-23 18:10:42 +01:00
return $languages->filter(function ($item) {
return $item->id == $this->client->getSetting('language_id');
})->first()->locale;
}
public function routeNotificationForMail($notification)
{
return $this->email;
}
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value)
{
return $this
->withTrashed()
->where('id', $this->decodePrimaryKey($value))->firstOrFail();
}
2020-03-23 18:10:42 +01:00
/**
* @return mixed|string
*/
public function avatar()
{
if ($this->avatar) {
2020-03-23 18:10:42 +01:00
return $this->avatar;
}
2020-03-23 18:10:42 +01:00
return asset('images/svg/user.svg');
}
}