| @@ -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,37 @@ | ||
| 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, resolved once per request. Reporting filters on this list | |
| 32 | + * rather than whereHas('person', ...), which drove those queries off | |
| 33 | + * fs_persons (no person_type index) and cost them their date range. | |
| 34 | + * | |
| 35 | + * @return array | |
| 36 | + */ | |
| 37 | + public static function getAgentIds() | |
| 38 | + { | |
| 39 | + static $agentIds = null; | |
| 40 | + | |
| 41 | + if (is_null($agentIds)) { | |
| 42 | + $agentIds = array_map('intval', static::query()->pluck('id')->all()); | |
| 43 | + } | |
| 44 | + | |
| 45 | + return $agentIds; | |
| 46 | + } | |
| 47 | + | |
| 20 | 48 | public static function boot() |
| 21 | 49 | { |
| 50 | + parent::boot(); | |
| 51 | + | |
| 22 | 52 | static::creating(function ($model) { |
| 23 | 53 | $model->person_type = static::$type; |
| 24 | 54 | $model->hash = md5(time().wp_generate_uuid4()); |
| 25 | 55 | }); |
| @@ -42,7 +72,27 @@ | ||
| 42 | 72 | |
| 43 | 73 | return $this->hasMany( |
| 44 | 74 | $class, $foreign_key, 'id' |
| 45 | 75 | ); |
| 76 | + } | |
| 77 | + | |
| 78 | + public function groups() | |
| 79 | + { | |
| 80 | + return $this->belongsToMany( | |
| 81 | + AgentGroup::class, 'fs_tag_pivot', 'source_id', 'tag_id' | |
| 82 | + )->wherePivot('source_type', 'agent_group'); | |
| 83 | + } | |
| 84 | + | |
| 85 | + public function scopeMentionBy($query, $search) | |
| 86 | + { | |
| 87 | + if (!$search) { | |
| 88 | + return $query; | |
| 89 | + } | |
| 90 | + | |
| 91 | + return $query->where(function ($q) use ($search) { | |
| 92 | + $q->where('first_name', 'LIKE', '%' . $search . '%') | |
| 93 | + ->orWhere('last_name', 'LIKE', '%' . $search . '%') | |
| 94 | + ->orWhere('email', 'LIKE', '%' . $search . '%'); | |
| 95 | + }); | |
| 46 | 96 | } |
| 47 | 97 | |
| 48 | 98 | } |