invoiceninja/app/Models/Contact.php

86 lines
2.1 KiB
PHP
Raw Normal View History

2015-03-18 09:39:03 +10:00
<?php namespace App\Models;
2015-03-17 07:45:25 +10:00
2015-03-31 21:50:58 +03:00
use HTML;
2015-03-31 12:38:24 +03:00
use Illuminate\Database\Eloquent\SoftDeletes;
2016-02-29 16:46:27 -05:00
use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
2015-03-31 12:38:24 +03:00
2016-02-29 16:46:27 -05:00
class Contact extends EntityModel implements AuthenticatableContract, CanResetPasswordContract
2015-03-17 07:45:25 +10:00
{
2016-02-29 16:46:27 -05:00
use SoftDeletes, Authenticatable, CanResetPassword;
2015-03-31 12:38:24 +03:00
protected $dates = ['deleted_at'];
2015-10-28 21:22:07 +02:00
protected $fillable = [
'first_name',
'last_name',
'email',
'phone',
'send_invoice',
];
2015-11-24 21:45:38 +02:00
public static $fieldFirstName = 'first_name';
public static $fieldLastName = 'last_name';
public static $fieldEmail = 'email';
public static $fieldPhone = 'phone';
2015-03-17 07:45:25 +10:00
2015-11-07 23:15:37 +11:00
public function account()
{
return $this->belongsTo('App\Models\Account');
}
2015-11-12 22:36:28 +02:00
public function user()
{
return $this->belongsTo('App\Models\User')->withTrashed();
2015-11-12 22:36:28 +02:00
}
2015-03-17 07:45:25 +10:00
public function client()
{
2015-11-15 19:23:45 +02:00
return $this->belongsTo('App\Models\Client')->withTrashed();
2015-03-17 07:45:25 +10:00
}
public function getPersonType()
{
return PERSON_CONTACT;
}
2015-07-02 23:21:29 +03:00
public function getName()
{
return $this->getDisplayName();
}
2015-03-17 07:45:25 +10:00
public function getDisplayName()
{
if ($this->getFullName()) {
return $this->getFullName();
} else {
return $this->email;
}
}
2016-05-24 14:16:42 -04:00
public function getContactKeyAttribute($contact_key)
{
if (empty($contact_key) && $this->id) {
$this->contact_key = $contact_key = str_random(RANDOM_KEY_LENGTH);
static::where('id', $this->id)->update(array('contact_key' => $contact_key));
}
return $contact_key;
}
2015-03-17 07:45:25 +10:00
public function getFullName()
{
if ($this->first_name || $this->last_name) {
return $this->first_name.' '.$this->last_name;
} else {
return '';
}
}
2016-05-24 17:02:28 -04:00
public function getLinkAttribute()
{
return \URL::to('client/dashboard/' . $this->contact_key);
}
2015-03-17 07:45:25 +10:00
}