|
2 | 2 |
|
3 | 3 | namespace PressbooksMultiInstitution;
|
4 | 4 |
|
| 5 | +use Illuminate\Database\Eloquent\Builder as EloquentBuilder; |
| 6 | +use Illuminate\Database\Query\Builder; |
5 | 7 | use Kucrut\Vite;
|
6 | 8 | use Pressbooks\Container;
|
7 | 9 | use PressbooksMultiInstitution\Actions\AssignBookToInstitution;
|
8 | 10 | use PressbooksMultiInstitution\Actions\AssignUserToInstitution;
|
9 | 11 | use PressbooksMultiInstitution\Actions\InstitutionalManagerDashboard;
|
| 12 | +use PressbooksMultiInstitution\Models\Institution; |
10 | 13 | use PressbooksMultiInstitution\Services\InstitutionStatsService;
|
11 | 14 | use PressbooksMultiInstitution\Services\MenuManager;
|
12 | 15 | use PressbooksMultiInstitution\Services\PermissionsManager;
|
@@ -40,6 +43,8 @@ public function setUp(): void
|
40 | 43 |
|
41 | 44 | Container::getInstance()->singleton(BookList::class, fn () => new BookList(app('db')));
|
42 | 45 | Container::getInstance()->singleton(UserList::class, fn () => new UserList(app('db')));
|
| 46 | + |
| 47 | + $this->registerInstitutionHooks(); |
43 | 48 | }
|
44 | 49 |
|
45 | 50 | private function registerActions(): void
|
@@ -71,6 +76,57 @@ private function registerBlade(): void
|
71 | 76 | );
|
72 | 77 | }
|
73 | 78 |
|
| 79 | + private function registerInstitutionHooks(): void |
| 80 | + { |
| 81 | + add_filter( |
| 82 | + hook_name: 'pressbooks_get_institution_by_id', |
| 83 | + callback: function (bool $default, string|int $id) { |
| 84 | + $institution = Institution::query()->find($id); |
| 85 | + |
| 86 | + return $institution?->id ?? $default; |
| 87 | + }, |
| 88 | + accepted_args: 2 |
| 89 | + ); |
| 90 | + |
| 91 | + add_filter( |
| 92 | + hook_name: 'pressbooks_get_institution_dropdown', |
| 93 | + callback: function (array $default) { |
| 94 | + return Institution::query() |
| 95 | + ->orderBy('name') |
| 96 | + ->pluck('name', 'id') |
| 97 | + ->prepend(__('Unassigned', 'pressbooks-multi-institution'), 0) |
| 98 | + ->toArray(); |
| 99 | + }, |
| 100 | + ); |
| 101 | + |
| 102 | + add_filter( |
| 103 | + hook_name: 'pressbooks_append_institution_to_query', |
| 104 | + callback: function (EloquentBuilder $query, string $columnToCompare, string $search, string $order, string $direction) { |
| 105 | + $query |
| 106 | + ->addSelect([ |
| 107 | + 'institution' => Institution::query() |
| 108 | + ->select('name') |
| 109 | + ->whereColumn('id', '=', $columnToCompare) |
| 110 | + ]) |
| 111 | + ->when($search, function (EloquentBuilder $query, string $value) use ($columnToCompare) { |
| 112 | + $query->orWhereExists(function (Builder $query) use ($value, $columnToCompare) { |
| 113 | + $query |
| 114 | + ->selectRaw(1) |
| 115 | + ->from('institutions') |
| 116 | + ->whereColumn('id', '=', $columnToCompare) |
| 117 | + ->where('name', 'like', "%{$value}%"); |
| 118 | + }); |
| 119 | + }) |
| 120 | + ->when($order === 'institution', function (EloquentBuilder $query) use ($direction) { |
| 121 | + $query |
| 122 | + ->orderByRaw($direction === 'asc' ? 'institution IS NOT NULL' : 'institution IS NULL') |
| 123 | + ->orderBy('institution', $direction); |
| 124 | + }); |
| 125 | + }, |
| 126 | + accepted_args: 5 |
| 127 | + ); |
| 128 | + } |
| 129 | + |
74 | 130 | private function enqueueScripts(): void
|
75 | 131 | {
|
76 | 132 | add_action('admin_enqueue_scripts', function ($page) {
|
|
0 commit comments