| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Models; |
| 4 |
|
| 5 |
use FluentBoards\App\Services\Constant; |
| 6 |
|
| 7 |
class TaskImage extends Attachment |
| 8 |
{ |
| 9 |
public static function boot() |
| 10 |
{ |
| 11 |
parent::boot(); |
| 12 |
|
| 13 |
static::creating(function ($model) { |
| 14 |
$uid = wp_generate_uuid4(); |
| 15 |
$model->file_hash = md5($uid . wp_rand(0, 1000)); |
| 16 |
}); |
| 17 |
} |
| 18 |
|
| 19 |
public function scopeWithComment($query) |
| 20 |
{ |
| 21 |
return $query->where('object_type', 'COMMENT')->with('comment', function ($query) { |
| 22 |
$query->select('id', 'board_id'); |
| 23 |
}); |
| 24 |
} |
| 25 |
|
| 26 |
public function getSecureUrlAttribute() |
| 27 |
{ |
| 28 |
if ($this->attachment_type === 'url') { |
| 29 |
return $this->full_url; |
| 30 |
} |
| 31 |
return add_query_arg([ |
| 32 |
'fbs' => 1, |
| 33 |
'fbs_comment_image' => $this->file_hash, |
| 34 |
'secure_sign' => md5($this->id . gmdate('YmdH')) |
| 35 |
], site_url('/index.php')); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Scope a query to include tasks related to attachments and descriptions. |
| 40 |
* |
| 41 |
* This scope filters the query to include records where the `object_type` |
| 42 |
* is either `TASK_ATTACHMENT` or `TASK_DESCRIPTION`. Additionally, it |
| 43 |
* includes related task data by using the `with` method to eager load |
| 44 |
* the `task` relationship, but only selects specific columns (`id`, |
| 45 |
* `title`, `board_id`) for efficiency. |
| 46 |
* |
| 47 |
* @param \Illuminate\Database\Eloquent\Builder $query |
| 48 |
* @return \Illuminate\Database\Eloquent\Builder |
| 49 |
*/ |
| 50 |
public function scopeWithTask($query) |
| 51 |
{ |
| 52 |
return $query->where(function ($query) { |
| 53 |
$query->where('object_type', Constant::TASK_DESCRIPTION); |
| 54 |
}) |
| 55 |
->with(['task' => function ($query) { |
| 56 |
$query->select('id', 'title', 'board_id'); |
| 57 |
}]); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
} |
| 62 |
|
| 63 |
|