| 
									
										
										
										
											2023-08-04 19:27:29 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Activity; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  | use BookStack\Entities\Models\Bookshelf; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Entity; | 
					
						
							|  |  |  | use BookStack\Entities\Models\Page; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-04 19:27:29 +08:00
										 |  |  | class WatchLevels | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Default level, No specific option set | 
					
						
							|  |  |  |      * Typically not a stored status | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     const DEFAULT = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Ignore all notifications. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     const IGNORE = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Watch for new content. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     const NEW = 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Watch for updates and new content | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     const UPDATES = 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Watch for comments, updates and new content. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     const COMMENTS = 3; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Get all the possible values as an option_name => value array. | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  |      * @returns array<string, int> | 
					
						
							| 
									
										
										
										
											2023-08-04 19:27:29 +08:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function all(): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $options = []; | 
					
						
							|  |  |  |         foreach ((new \ReflectionClass(static::class))->getConstants() as $name => $value) { | 
					
						
							|  |  |  |             $options[strtolower($name)] = $value; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $options; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Get the watch options suited for the given entity. | 
					
						
							|  |  |  |      * @returns array<string, int> | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function allSuitedFor(Entity $entity): array | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $options = static::all(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($entity instanceof Page) { | 
					
						
							|  |  |  |             unset($options['new']); | 
					
						
							|  |  |  |         } elseif ($entity instanceof Bookshelf) { | 
					
						
							|  |  |  |             return []; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $options; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Convert the given name to a level value. | 
					
						
							|  |  |  |      * Defaults to default value if the level does not exist. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-08-04 19:27:29 +08:00
										 |  |  |     public static function levelNameToValue(string $level): int | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  |         return static::all()[$level] ?? static::DEFAULT; | 
					
						
							| 
									
										
										
										
											2023-08-04 19:27:29 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-14 20:11:18 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Convert the given int level value to a level name. | 
					
						
							|  |  |  |      * Defaults to 'default' level name if not existing. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2023-08-04 19:27:29 +08:00
										 |  |  |     public static function levelValueToName(int $level): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         foreach (static::all() as $name => $value) { | 
					
						
							|  |  |  |             if ($level === $value) { | 
					
						
							|  |  |  |                 return $name; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return 'default'; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |