| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Sorting; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-05 04:11:35 +08:00
										 |  |  | use BookStack\Activity\Models\Loggable; | 
					
						
							| 
									
										
										
										
											2025-02-05 22:33:46 +08:00
										 |  |  | use BookStack\Entities\Models\Book; | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  | use Carbon\Carbon; | 
					
						
							| 
									
										
										
										
											2025-02-09 23:16:18 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Collection; | 
					
						
							| 
									
										
										
										
											2025-02-10 07:24:36 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Factories\HasFactory; | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Model; | 
					
						
							| 
									
										
										
										
											2025-02-05 22:33:46 +08:00
										 |  |  | use Illuminate\Database\Eloquent\Relations\HasMany; | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @property int $id | 
					
						
							|  |  |  |  * @property string $name | 
					
						
							|  |  |  |  * @property string $sequence | 
					
						
							|  |  |  |  * @property Carbon $created_at | 
					
						
							|  |  |  |  * @property Carbon $updated_at | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  | class SortRule extends Model implements Loggable | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2025-02-10 07:24:36 +08:00
										 |  |  |     use HasFactory; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |      * @return SortRuleOperation[] | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-02-04 23:14:22 +08:00
										 |  |  |     public function getOperations(): array | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |         return SortRuleOperation::fromSequence($this->sequence); | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |      * @param SortRuleOperation[] $options | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  |      */ | 
					
						
							| 
									
										
										
										
											2025-02-04 23:14:22 +08:00
										 |  |  |     public function setOperations(array $options): void | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |         $values = array_map(fn (SortRuleOperation $opt) => $opt->value, $options); | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  |         $this->sequence = implode(',', $values); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-05 04:11:35 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function logDescriptor(): string | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return "({$this->id}) {$this->name}"; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function getUrl(): string | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-02-11 22:36:25 +08:00
										 |  |  |         return url("/settings/sorting/rules/{$this->id}"); | 
					
						
							| 
									
										
										
										
											2025-02-05 04:11:35 +08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-05 22:33:46 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function books(): HasMany | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return $this->hasMany(Book::class); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-02-09 23:16:18 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public static function allByName(): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return static::query() | 
					
						
							|  |  |  |             ->withCount('books') | 
					
						
							|  |  |  |             ->orderBy('name', 'asc') | 
					
						
							|  |  |  |             ->get(); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-01-31 01:49:19 +08:00
										 |  |  | } |