| @@ -35,8 +35,28 @@ | ||
| 35 | 35 | ->where('key', Constant::FLUENT_BOARD_ADMIN) |
| 36 | 36 | ->first(); |
| 37 | 37 | } |
| 38 | 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 | + | |
| 39 | 59 | public function updateGlobalNotificationSettings($newSettings) |
| 40 | 60 | { |
| 41 | 61 | $userId = get_current_user_id(); |
| 42 | 62 | |
| @@ -44,13 +64,17 @@ | ||
| 44 | 64 | ->where('object_type', Constant::OBJECT_TYPE_USER) |
| 45 | 65 | ->where('key', Constant::USER_GLOBAL_NOTIFICATIONS) |
| 46 | 66 | ->first(); |
| 47 | 67 | |
| 48 | - foreach ($newSettings as $index => $setting) | |
| 49 | - { | |
| 50 | - $newSettings[$index] = $setting == 'true' ? true : false; | |
| 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); | |
| 51 | 73 | } |
| 52 | 74 | |
| 75 | + $newSettings = array_merge($allowedSettings, $filteredSettings); | |
| 76 | + | |
| 53 | 77 | $globalNotification->value = $newSettings; |
| 54 | 78 | $globalNotification->save(); |
| 55 | 79 | |
| 56 | 80 | //set this settings to all board |
| @@ -70,19 +94,9 @@ | ||
| 70 | 94 | ->where('key', Constant::USER_GLOBAL_NOTIFICATIONS) |
| 71 | 95 | ->first(); |
| 72 | 96 | |
| 73 | 97 | //default notification settings |
| 74 | - $newSettingsArray = [ | |
| 75 | - Constant::GLOBAL_EMAIL_NOTIFICATION_COMMENT => true, | |
| 76 | - Constant::GLOBAL_EMAIL_NOTIFICATION_STAGE_CHANGE => true, | |
| 77 | - Constant::GLOBAL_EMAIL_NOTIFICATION_TASK_ASSIGN => true, | |
| 78 | - Constant::GLOBAL_EMAIL_NOTIFICATION_DUE_DATE => true, | |
| 79 | - Constant::GLOBAL_EMAIL_NOTIFICATION_REMOVE_FROM_TASK => true, | |
| 80 | - Constant::GLOBAL_EMAIL_NOTIFICATION_TASK_ARCHIVE => true, | |
| 81 | - Constant::GLOBAL_EMAIL_NOTIFICATION_CREATING_TASK => true, | |
| 82 | - Constant::GLOBAL_EMAIL_NOTIFICATION_COMMENTING => true, | |
| 83 | - Constant::GLOBAL_EMAIL_NOTIFICATION_ASSIGNING => true | |
| 84 | - ]; | |
| 98 | + $newSettingsArray = $this->getDefaultGlobalNotificationSettings(); | |
| 85 | 99 | |
| 86 | 100 | //if no settings found of this user then store default |
| 87 | 101 | if(!$globalNotification) { |
| 88 | 102 | $meta = new Meta(); |
| @@ -92,8 +106,23 @@ | ||
| 92 | 106 | $meta->value = $newSettingsArray; |
| 93 | 107 | $meta->save(); |
| 94 | 108 | |
| 95 | 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 | + } | |
| 96 | 125 | } |
| 97 | 126 | |
| 98 | 127 | return $globalNotification; |
| 99 | 128 | } |