| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Services; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Board; |
| 6 |
use FluentBoards\App\Models\Meta; |
| 7 |
use FluentBoards\App\Models\Relation; |
| 8 |
|
| 9 |
class OptionService |
| 10 |
{ |
| 11 |
public function createSuperAdmin($userId) |
| 12 |
{ |
| 13 |
$existUser = $this->searchUserMeta($userId); |
| 14 |
if (!$existUser) { |
| 15 |
$meta = new Meta(); |
| 16 |
$meta->object_id = $userId; |
| 17 |
$meta->object_type = Constant::OBJECT_TYPE_USER; |
| 18 |
$meta->key = Constant::FLUENT_BOARD_ADMIN; |
| 19 |
$meta->save(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
public function removeUserSuperAdmin($userId) |
| 24 |
{ |
| 25 |
$existUser = $this->searchUserMeta($userId); |
| 26 |
if ($existUser) { |
| 27 |
$existUser->delete(); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
private function searchUserMeta($userId) |
| 32 |
{ |
| 33 |
return Meta::query()->where('object_id', $userId) |
| 34 |
->where('object_type', Constant::OBJECT_TYPE_USER) |
| 35 |
->where('key', Constant::FLUENT_BOARD_ADMIN) |
| 36 |
->first(); |
| 37 |
} |
| 38 |
|
| 39 |
private function getDefaultGlobalNotificationSettings() |
| 40 |
{ |
| 41 |
return [ |
| 42 |
Constant::GLOBAL_EMAIL_NOTIFICATION_COMMENT => true, |
| 43 |
Constant::GLOBAL_EMAIL_NOTIFICATION_STAGE_CHANGE => true, |
| 44 |
Constant::GLOBAL_EMAIL_NOTIFICATION_TASK_ASSIGN => true, |
| 45 |
Constant::GLOBAL_EMAIL_NOTIFICATION_DUE_DATE => true, |
| 46 |
Constant::GLOBAL_EMAIL_NOTIFICATION_REMOVE_FROM_TASK => true, |
| 47 |
Constant::GLOBAL_EMAIL_NOTIFICATION_TASK_ARCHIVE => true, |
| 48 |
Constant::GLOBAL_EMAIL_NOTIFICATION_CREATING_TASK => true, |
| 49 |
Constant::GLOBAL_EMAIL_NOTIFICATION_COMMENTING => true, |
| 50 |
Constant::GLOBAL_EMAIL_NOTIFICATION_ASSIGNING => true |
| 51 |
]; |
| 52 |
} |
| 53 |
|
| 54 |
private function normalizeNotificationPreferenceValue($value) |
| 55 |
{ |
| 56 |
return true === $value || 1 === $value || '1' === $value || 'true' === $value; |
| 57 |
} |
| 58 |
|
| 59 |
public function updateGlobalNotificationSettings($newSettings) |
| 60 |
{ |
| 61 |
$userId = get_current_user_id(); |
| 62 |
|
| 63 |
$globalNotification = Meta::where('object_id', $userId) |
| 64 |
->where('object_type', Constant::OBJECT_TYPE_USER) |
| 65 |
->where('key', Constant::USER_GLOBAL_NOTIFICATIONS) |
| 66 |
->first(); |
| 67 |
|
| 68 |
$allowedSettings = $this->getDefaultGlobalNotificationSettings(); |
| 69 |
$newSettings = array_intersect_key($newSettings, $allowedSettings); |
| 70 |
$filteredSettings = []; |
| 71 |
foreach ($newSettings as $index => $setting) { |
| 72 |
$filteredSettings[$index] = $this->normalizeNotificationPreferenceValue($setting); |
| 73 |
} |
| 74 |
|
| 75 |
$newSettings = array_merge($allowedSettings, $filteredSettings); |
| 76 |
|
| 77 |
$globalNotification->value = $newSettings; |
| 78 |
$globalNotification->save(); |
| 79 |
|
| 80 |
//set this settings to all board |
| 81 |
$relatedBoardsQuery = Board::where('type', 'to-do')->byAccessUser($userId)->get(); |
| 82 |
$notificationService = new NotificationService(); |
| 83 |
foreach ($relatedBoardsQuery as $board) |
| 84 |
{ |
| 85 |
$notificationService->updateBoardNotificationSettings($newSettings, $board->id); |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
public function getGlobalNotificationSettings() |
| 90 |
{ |
| 91 |
$userId = get_current_user_id(); |
| 92 |
$globalNotification = Meta::where('object_id', $userId) |
| 93 |
->where('object_type', Constant::OBJECT_TYPE_USER) |
| 94 |
->where('key', Constant::USER_GLOBAL_NOTIFICATIONS) |
| 95 |
->first(); |
| 96 |
|
| 97 |
//default notification settings |
| 98 |
$newSettingsArray = $this->getDefaultGlobalNotificationSettings(); |
| 99 |
|
| 100 |
//if no settings found of this user then store default |
| 101 |
if(!$globalNotification) { |
| 102 |
$meta = new Meta(); |
| 103 |
$meta->object_id = $userId; |
| 104 |
$meta->object_type = Constant::OBJECT_TYPE_USER; |
| 105 |
$meta->key = Constant::USER_GLOBAL_NOTIFICATIONS; |
| 106 |
$meta->value = $newSettingsArray; |
| 107 |
$meta->save(); |
| 108 |
|
| 109 |
return $meta; |
| 110 |
} |
| 111 |
|
| 112 |
$storedSettings = maybe_unserialize($globalNotification->value); |
| 113 |
if (is_array($storedSettings)) { |
| 114 |
$filteredSettings = array_merge($newSettingsArray, array_intersect_key($storedSettings, $newSettingsArray)); |
| 115 |
foreach ($filteredSettings as $index => $setting) { |
| 116 |
$filteredSettings[$index] = $this->normalizeNotificationPreferenceValue($setting); |
| 117 |
} |
| 118 |
|
| 119 |
if ($filteredSettings !== $storedSettings) { |
| 120 |
$globalNotification->value = $filteredSettings; |
| 121 |
$globalNotification->save(); |
| 122 |
} else { |
| 123 |
$globalNotification->value = $filteredSettings; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $globalNotification; |
| 128 |
} |
| 129 |
|
| 130 |
public function getDashboardViewSettings() |
| 131 |
{ |
| 132 |
$userId = get_current_user_id(); |
| 133 |
$dshboardViewSettings = Meta::where('object_id', $userId) |
| 134 |
->where('object_type', Constant::OBJECT_TYPE_USER) |
| 135 |
->where('key', Constant::USER_DASHBOARD_VIEW) |
| 136 |
->first(); |
| 137 |
|
| 138 |
//if no settings found of this user then store default |
| 139 |
if(!$dshboardViewSettings) { |
| 140 |
$meta = new Meta(); |
| 141 |
$meta->object_id = $userId; |
| 142 |
$meta->object_type = Constant::OBJECT_TYPE_USER; |
| 143 |
$meta->key = Constant::USER_DASHBOARD_VIEW; |
| 144 |
$meta->value = Constant::DEFAULT_DASHBOARD_VIEW_PREFERENCES; |
| 145 |
$meta->save(); |
| 146 |
|
| 147 |
return $meta; |
| 148 |
} |
| 149 |
|
| 150 |
return $dshboardViewSettings; |
| 151 |
} |
| 152 |
|
| 153 |
public function getListViewPreferences() |
| 154 |
{ |
| 155 |
$userId = get_current_user_id(); |
| 156 |
$dshboardViewSettings = Meta::where('object_id', $userId) |
| 157 |
->where('object_type', Constant::OBJECT_TYPE_USER) |
| 158 |
->where('key', Constant::USER_LISTVIEW_PREFERENCES) |
| 159 |
->first(); |
| 160 |
//if no settings found of this user then store default |
| 161 |
if(!$dshboardViewSettings) { |
| 162 |
$meta = new Meta(); |
| 163 |
$meta->object_id = $userId; |
| 164 |
$meta->object_type = Constant::OBJECT_TYPE_USER; |
| 165 |
$meta->key = Constant::USER_LISTVIEW_PREFERENCES; |
| 166 |
$meta->value = Constant::DEFAULT_DASHBOARD_VIEW_PREFERENCES; |
| 167 |
$meta->save(); |
| 168 |
|
| 169 |
return $meta; |
| 170 |
} |
| 171 |
|
| 172 |
return $dshboardViewSettings; |
| 173 |
} |
| 174 |
public function updateDashboardViewSettings($newSettings, $view) |
| 175 |
{ |
| 176 |
$userId = get_current_user_id(); |
| 177 |
|
| 178 |
if ($view == 'listview') { |
| 179 |
$viewWiseKey = Constant::USER_LISTVIEW_PREFERENCES; |
| 180 |
} elseif ($view == 'tableview') { |
| 181 |
$viewWiseKey = Constant::USER_TABLEVIEW_PREFERENCES; |
| 182 |
} else { |
| 183 |
$viewWiseKey = Constant::USER_DASHBOARD_VIEW; |
| 184 |
} |
| 185 |
|
| 186 |
$dshboardViewSettings = Meta::where('object_id', $userId) |
| 187 |
->where('object_type', Constant::OBJECT_TYPE_USER) |
| 188 |
->where('key', $viewWiseKey) |
| 189 |
->first(); |
| 190 |
|
| 191 |
foreach ($newSettings as $index => $setting) |
| 192 |
{ |
| 193 |
$newSettings[$index] = $setting == 'true' ? true : false; |
| 194 |
} |
| 195 |
|
| 196 |
$dshboardViewSettings->value = $newSettings; |
| 197 |
$dshboardViewSettings->save(); |
| 198 |
} |
| 199 |
|
| 200 |
// Add this new method |
| 201 |
public function getTableViewPreferences() |
| 202 |
{ |
| 203 |
$userId = get_current_user_id(); |
| 204 |
$tableViewSettings = Meta::where('object_id', $userId) |
| 205 |
->where('object_type', Constant::OBJECT_TYPE_USER) |
| 206 |
->where('key', Constant::USER_TABLEVIEW_PREFERENCES) |
| 207 |
->first(); |
| 208 |
if (!$tableViewSettings) { |
| 209 |
$meta = new Meta(); |
| 210 |
$meta->object_id = $userId; |
| 211 |
$meta->object_type = Constant::OBJECT_TYPE_USER; |
| 212 |
$meta->key = Constant::USER_TABLEVIEW_PREFERENCES; |
| 213 |
$meta->value = Constant::DEFAULT_TABLEVIEW_VIEW_PREFERENCES; |
| 214 |
$meta->save(); |
| 215 |
|
| 216 |
return $meta; |
| 217 |
} |
| 218 |
|
| 219 |
return $tableViewSettings; |
| 220 |
} |
| 221 |
} |
| 222 |
|