| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Models; |
| 4 |
|
| 5 |
use FluentBoards\App\Services\Constant; |
| 6 |
|
| 7 |
class User extends Model |
| 8 |
{ |
| 9 |
protected $table = 'users'; |
| 10 |
|
| 11 |
protected $primaryKey = 'ID'; |
| 12 |
|
| 13 |
protected $hidden = ['user_pass', 'user_activation_key']; |
| 14 |
|
| 15 |
/** |
| 16 |
* wp_users columns that never belong in an API response. Nothing in the frontend reads them. |
| 17 |
*/ |
| 18 |
const ALWAYS_HIDDEN = [ |
| 19 |
'user_pass', |
| 20 |
'user_activation_key', |
| 21 |
'user_nicename', |
| 22 |
'user_url', |
| 23 |
'user_registered', |
| 24 |
'user_status', |
| 25 |
]; |
| 26 |
|
| 27 |
/** |
| 28 |
* Columns only serialized for users holding the `list_users` capability, matching the |
| 29 |
* policy Helper::sanitizeUserCollections() applies on the board and task routes. |
| 30 |
*/ |
| 31 |
const PRIVILEGED_ONLY = ['user_email']; |
| 32 |
|
| 33 |
/** @var bool Set by the board export so the importer can match members by email. */ |
| 34 |
protected static $serializePrivileged = false; |
| 35 |
|
| 36 |
protected $appends = ['photo']; |
| 37 |
|
| 38 |
/** |
| 39 |
* Allow (or stop allowing) privileged columns in serialized output for the rest of the |
| 40 |
* request. Only for trusted, permission-checked flows such as the board JSON export. |
| 41 |
*/ |
| 42 |
public static function serializePrivilegedFields($enabled = true) |
| 43 |
{ |
| 44 |
static::$serializePrivileged = (bool)$enabled; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Hidden attributes are resolved per request so that every response path serializing a |
| 49 |
* User model (relations, eager loads, toArray) applies the same disclosure policy. |
| 50 |
*/ |
| 51 |
public function getHidden() |
| 52 |
{ |
| 53 |
$hidden = array_merge(self::ALWAYS_HIDDEN, parent::getHidden()); |
| 54 |
|
| 55 |
if (!static::$serializePrivileged && !current_user_can('list_users')) { |
| 56 |
$hidden = array_merge($hidden, self::PRIVILEGED_ONLY); |
| 57 |
} |
| 58 |
|
| 59 |
return array_values(array_unique($hidden)); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Accessor to get dynamic photo attribute |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function getPhotoAttribute() |
| 67 |
{ |
| 68 |
return fluent_boards_user_avatar($this->attributes['user_email'], $this->attributes['display_name']); |
| 69 |
} |
| 70 |
|
| 71 |
public function tasks() |
| 72 |
{ |
| 73 |
return $this->belongsToMany( |
| 74 |
Task::class, |
| 75 |
'fbs_relations', |
| 76 |
'foreign_id', |
| 77 |
'object_id' |
| 78 |
)->withPivot('settings') |
| 79 |
->wherePivot('object_type', Constant::OBJECT_TYPE_USER_TASK_WATCH) |
| 80 |
->withTimestamps(); |
| 81 |
} |
| 82 |
|
| 83 |
public function watchingTasks() |
| 84 |
{ |
| 85 |
return $this->belongsToMany( |
| 86 |
Task::class, |
| 87 |
'fbs_relations', |
| 88 |
'foreign_id', |
| 89 |
'object_id' |
| 90 |
)->withPivot('settings') |
| 91 |
->wherePivot('object_type', Constant::OBJECT_TYPE_USER_TASK_WATCH) |
| 92 |
->withTimestamps(); |
| 93 |
} |
| 94 |
|
| 95 |
public function highPriorityTasks() |
| 96 |
{ |
| 97 |
return $this->tasks() |
| 98 |
->where('is_archived', false) |
| 99 |
->where('parent_id', null) |
| 100 |
->where('priority', 'high') |
| 101 |
->take(3); |
| 102 |
} |
| 103 |
|
| 104 |
public function overDueTasks() |
| 105 |
{ |
| 106 |
return $this->tasks() |
| 107 |
->where('is_archived', false) |
| 108 |
->where('parent_id', null) |
| 109 |
->overdue() |
| 110 |
->orderBy('due_date', 'ASC') |
| 111 |
->take(5); |
| 112 |
} |
| 113 |
|
| 114 |
public function upcomingTasks() |
| 115 |
{ |
| 116 |
return $this->tasks() |
| 117 |
->where('is_archived', false) |
| 118 |
->where('parent_id', null) |
| 119 |
->upcoming() |
| 120 |
->orderBy('due_date', 'ASC') |
| 121 |
->take(3); |
| 122 |
} |
| 123 |
|
| 124 |
public function upcomingWithoutDuedate() |
| 125 |
{ |
| 126 |
return $this->tasks() |
| 127 |
->where('is_archived', false) |
| 128 |
->where('parent_id', null) |
| 129 |
->whereNull('due_date') |
| 130 |
->orderBy('due_date', 'ASC') |
| 131 |
->take(3); |
| 132 |
} |
| 133 |
|
| 134 |
public function boards() |
| 135 |
{ |
| 136 |
return $this->belongsToMany( |
| 137 |
Board::class, |
| 138 |
'fbs_relations', |
| 139 |
'object_id', |
| 140 |
'foreign_id' |
| 141 |
) |
| 142 |
->wherePivot('object_type', 'board_user') |
| 143 |
->withTimestamps() |
| 144 |
->withPivot('settings', 'preferences'); |
| 145 |
} |
| 146 |
|
| 147 |
public function whichBoards() |
| 148 |
{ |
| 149 |
return $this->belongsToMany( |
| 150 |
Board::class, |
| 151 |
'fbs_relations', |
| 152 |
'foreign_id', |
| 153 |
'object_id' |
| 154 |
) |
| 155 |
->wherePivot('object_type', Constant::OBJECT_TYPE_BOARD_USER) |
| 156 |
->withPivot('settings', 'preferences') |
| 157 |
->withTimestamps(); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
public function boardUser() |
| 162 |
{ |
| 163 |
// return $this->hasMany(BoardUser::class); |
| 164 |
return null; |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
public function notifications() |
| 169 |
{ |
| 170 |
return $this->belongsToMany( |
| 171 |
Notification::class, |
| 172 |
'fbs_notification_users', |
| 173 |
'user_id', |
| 174 |
'notification_id' |
| 175 |
)->withTimestamps() |
| 176 |
->withPivot('marked_read_at'); |
| 177 |
} |
| 178 |
public function assignedTasks() |
| 179 |
{ |
| 180 |
return $this->belongsToMany( |
| 181 |
Task::class, |
| 182 |
'fbs_relations', |
| 183 |
'foreign_id', |
| 184 |
'object_id' |
| 185 |
)->withPivot('settings') |
| 186 |
->wherePivot('object_type', Constant::OBJECT_TYPE_TASK_ASSIGNEE) |
| 187 |
->withTimestamps(); |
| 188 |
} |
| 189 |
|
| 190 |
public function mentionedTasks() |
| 191 |
{ |
| 192 |
return Task::whereHas('notifications', function ($query) { |
| 193 |
$query->where('action', 'task_comment_mentioned') |
| 194 |
->whereHas('users', function ($q) { |
| 195 |
$q->where('user_id', $this->ID); |
| 196 |
}); |
| 197 |
}); |
| 198 |
} |
| 199 |
} |
| 200 |
|