2017-01-30 20:40:43 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2017-02-19 21:47:16 +01:00
|
|
|
use Utils;
|
2016-02-29 22:46:27 +01:00
|
|
|
use Illuminate\Auth\Authenticatable;
|
|
|
|
use Illuminate\Auth\Passwords\CanResetPassword;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
|
|
|
|
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
|
2017-01-30 20:40:43 +01:00
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
2017-04-30 21:18:17 +02:00
|
|
|
use App\Models\LookupContact;
|
2015-03-31 11:38:24 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
2017-01-30 20:40:43 +01:00
|
|
|
* Class Contact.
|
2016-07-03 18:11:58 +02:00
|
|
|
*/
|
2016-02-29 22:46:27 +01:00
|
|
|
class Contact extends EntityModel implements AuthenticatableContract, CanResetPasswordContract
|
2015-03-16 22:45:25 +01:00
|
|
|
{
|
2016-02-29 22:46:27 +01:00
|
|
|
use SoftDeletes, Authenticatable, CanResetPassword;
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-03-31 11:38:24 +02:00
|
|
|
protected $dates = ['deleted_at'];
|
|
|
|
|
2016-08-08 22:47:09 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getEntityType()
|
|
|
|
{
|
|
|
|
return ENTITY_CONTACT;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2015-10-28 20:22:07 +01:00
|
|
|
protected $fillable = [
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'email',
|
|
|
|
'phone',
|
|
|
|
'send_invoice',
|
2017-04-16 13:31:14 +02:00
|
|
|
'custom_value1',
|
|
|
|
'custom_value2',
|
2015-10-28 20:22:07 +01:00
|
|
|
];
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-24 20:45:38 +01:00
|
|
|
public static $fieldFirstName = 'first_name';
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-24 20:45:38 +01:00
|
|
|
public static $fieldLastName = 'last_name';
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-24 20:45:38 +01:00
|
|
|
public static $fieldEmail = 'email';
|
2016-07-03 18:11:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2015-11-24 20:45:38 +01:00
|
|
|
public static $fieldPhone = 'phone';
|
2015-03-16 22:45:25 +01:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
*/
|
2015-11-07 13:15:37 +01:00
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Account');
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-11-12 21:36:28 +01:00
|
|
|
public function user()
|
|
|
|
{
|
2016-06-08 07:11:05 +02:00
|
|
|
return $this->belongsTo('App\Models\User')->withTrashed();
|
2015-11-12 21:36:28 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function client()
|
|
|
|
{
|
2015-11-15 18:23:45 +01:00
|
|
|
return $this->belongsTo('App\Models\Client')->withTrashed();
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getPersonType()
|
|
|
|
{
|
|
|
|
return PERSON_CONTACT;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
2015-07-02 22:21:29 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->getDisplayName();
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return mixed|string
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getDisplayName()
|
|
|
|
{
|
|
|
|
if ($this->getFullName()) {
|
|
|
|
return $this->getFullName();
|
|
|
|
} else {
|
|
|
|
return $this->email;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @param $contact_key
|
2017-01-30 20:40:43 +01:00
|
|
|
*
|
2016-07-03 18:11:58 +02:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2016-05-24 20:16:42 +02:00
|
|
|
public function getContactKeyAttribute($contact_key)
|
|
|
|
{
|
|
|
|
if (empty($contact_key) && $this->id) {
|
2017-04-02 19:46:01 +02:00
|
|
|
$this->contact_key = $contact_key = strtolower(str_random(RANDOM_KEY_LENGTH));
|
2016-07-03 18:11:58 +02:00
|
|
|
static::where('id', $this->id)->update(['contact_key' => $contact_key]);
|
2016-05-24 20:16:42 +02:00
|
|
|
}
|
2017-01-30 20:40:43 +01:00
|
|
|
|
2016-05-24 20:16:42 +02:00
|
|
|
return $contact_key;
|
|
|
|
}
|
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-03-16 22:45:25 +01:00
|
|
|
public function getFullName()
|
|
|
|
{
|
|
|
|
if ($this->first_name || $this->last_name) {
|
2017-01-23 16:00:44 +01:00
|
|
|
return trim($this->first_name.' '.$this->last_name);
|
2015-03-16 22:45:25 +01:00
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
2016-05-24 23:02:28 +02:00
|
|
|
|
2016-07-03 18:11:58 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-05-24 23:02:28 +02:00
|
|
|
public function getLinkAttribute()
|
|
|
|
{
|
2017-02-19 21:47:16 +01:00
|
|
|
if (! $this->account) {
|
|
|
|
$this->load('account');
|
|
|
|
}
|
|
|
|
|
|
|
|
$account = $this->account;
|
|
|
|
$url = trim(SITE_URL, '/');
|
|
|
|
|
|
|
|
if ($account->hasFeature(FEATURE_CUSTOM_URL)) {
|
2017-08-24 14:32:45 +02:00
|
|
|
if (Utils::isNinjaProd() && ! Utils::isReseller()) {
|
2017-02-19 21:47:16 +01:00
|
|
|
$url = $account->present()->clientPortalLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->account->subdomain) {
|
|
|
|
$url = Utils::replaceSubdomain($url, $account->subdomain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "{$url}/client/dashboard/{$this->contact_key}";
|
2016-05-24 23:02:28 +02:00
|
|
|
}
|
2015-03-16 22:45:25 +01:00
|
|
|
}
|
2017-04-30 21:18:17 +02:00
|
|
|
|
|
|
|
Contact::creating(function ($contact)
|
|
|
|
{
|
|
|
|
LookupContact::createNew($contact->account->account_key, [
|
|
|
|
'contact_key' => $contact->contact_key,
|
|
|
|
]);
|
|
|
|
});
|
2017-05-01 11:29:45 +02:00
|
|
|
|
|
|
|
Contact::deleted(function ($contact)
|
|
|
|
{
|
2017-05-10 12:02:20 +02:00
|
|
|
if ($contact->forceDeleting) {
|
|
|
|
LookupContact::deleteWhere([
|
|
|
|
'contact_key' => $contact->contact_key,
|
|
|
|
]);
|
|
|
|
}
|
2017-05-01 11:29:45 +02:00
|
|
|
});
|