| @@ -1,11 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentSupport\App\Models; |
| 4 | 4 | |
| 5 | +use FluentSupport\App\Models\Traits\AgentTrait; | |
| 5 | 6 | |
| 6 | 7 | class Agent extends Person |
| 7 | 8 | { |
| 9 | + use AgentTrait; | |
| 10 | + | |
| 8 | 11 | protected static $type = 'agent'; |
| 9 | 12 | |
| 10 | 13 | protected $searchable = [ |
| 11 | 14 | 'id', |
| @@ -16,10 +19,36 @@ | ||
| 16 | 19 | 'address_line_2', |
| 17 | 20 | 'country' |
| 18 | 21 | ]; |
| 19 | 22 | |
| 23 | + /** | |
| 24 | + * @return array | |
| 25 | + */ | |
| 26 | + public function getSearchableFields(){ | |
| 27 | + return $this->searchable; | |
| 28 | + } | |
| 29 | + | |
| 30 | + /** | |
| 31 | + * Agent ids, cached per request. Reporting filters on these ids because | |
| 32 | + * whereHas('person') hits fs_persons, which has no person_type index. | |
| 33 | + * | |
| 34 | + * @return array | |
| 35 | + */ | |
| 36 | + public static function getAgentIds() | |
| 37 | + { | |
| 38 | + static $agentIds = null; | |
| 39 | + | |
| 40 | + if (is_null($agentIds)) { | |
| 41 | + $agentIds = array_map('intval', static::query()->pluck('id')->all()); | |
| 42 | + } | |
| 43 | + | |
| 44 | + return $agentIds; | |
| 45 | + } | |
| 46 | + | |
| 20 | 47 | public static function boot() |
| 21 | 48 | { |
| 49 | + parent::boot(); | |
| 50 | + | |
| 22 | 51 | static::creating(function ($model) { |
| 23 | 52 | $model->person_type = static::$type; |
| 24 | 53 | $model->hash = md5(time().wp_generate_uuid4()); |
| 25 | 54 | }); |
| @@ -30,9 +59,8 @@ | ||
| 30 | 59 | } |
| 31 | 60 | |
| 32 | 61 | |
| 33 | 62 | /** |
| 34 | - * One2Many: Customer has to many Click Tickets | |
| 35 | 63 | * @return Model Collection |
| 36 | 64 | */ |
| 37 | 65 | public function tickets() |
| 38 | 66 | { |
| @@ -42,7 +70,27 @@ | ||
| 42 | 70 | |
| 43 | 71 | return $this->hasMany( |
| 44 | 72 | $class, $foreign_key, 'id' |
| 45 | 73 | ); |
| 74 | + } | |
| 75 | + | |
| 76 | + public function groups() | |
| 77 | + { | |
| 78 | + return $this->belongsToMany( | |
| 79 | + AgentGroup::class, 'fs_tag_pivot', 'source_id', 'tag_id' | |
| 80 | + )->wherePivot('source_type', 'agent_group'); | |
| 81 | + } | |
| 82 | + | |
| 83 | + public function scopeMentionBy($query, $search) | |
| 84 | + { | |
| 85 | + if (!$search) { | |
| 86 | + return $query; | |
| 87 | + } | |
| 88 | + | |
| 89 | + return $query->where(function ($q) use ($search) { | |
| 90 | + $q->where('first_name', 'LIKE', '%' . $search . '%') | |
| 91 | + ->orWhere('last_name', 'LIKE', '%' . $search . '%') | |
| 92 | + ->orWhere('email', 'LIKE', '%' . $search . '%'); | |
| 93 | + }); | |
| 46 | 94 | } |
| 47 | 95 | |
| 48 | 96 | } |