diff --git a/src/Bootstrap.php b/src/Bootstrap.php index ab659da5..6437ec4c 100644 --- a/src/Bootstrap.php +++ b/src/Bootstrap.php @@ -12,6 +12,7 @@ use PressbooksMultiInstitution\Services\PermissionsManager; use PressbooksMultiInstitution\Views\BookList; use PressbooksMultiInstitution\Views\UserList; +use PressbooksMultiInstitution\Views\WpBookList; /** * Class Bootstrap @@ -39,6 +40,7 @@ public function setUp(): void $this->loadTranslations(); Container::getInstance()->singleton(BookList::class, fn () => new BookList(app('db'))); + Container::getInstance()->singleton(WpBookList::class, fn () => new WpBookList); Container::getInstance()->singleton(UserList::class, fn () => new UserList(app('db'))); } diff --git a/src/Services/PermissionsManager.php b/src/Services/PermissionsManager.php index 2b429e1a..38b37f0d 100644 --- a/src/Services/PermissionsManager.php +++ b/src/Services/PermissionsManager.php @@ -9,6 +9,8 @@ use PressbooksMultiInstitution\Views\BookList; use PressbooksMultiInstitution\Views\UserList; +use PressbooksMultiInstitution\Views\WpBookList; + use function Pressbooks\Admin\NetworkManagers\_restricted_users; use function PressbooksMultiInstitution\Support\get_allowed_book_pages; use function PressbooksMultiInstitution\Support\get_allowed_pages; @@ -38,6 +40,7 @@ public function setupFilters(): void Container::get(TableViews::class)->init(); Container::get(BookList::class)->init(); + Container::get(WpBookList::class)->init(); Container::get(UserList::class)->init(); do_action('pb_institutional_filters_created', $institution, $institutionalManagers, $institutionalUsers); diff --git a/src/Views/WpBookList.php b/src/Views/WpBookList.php new file mode 100644 index 00000000..a4a8e8ea --- /dev/null +++ b/src/Views/WpBookList.php @@ -0,0 +1,104 @@ +id !== 'sites-network') { + return $clauses; + } + + if (! is_main_site()) { + return $clauses; + } + + if (! is_super_admin()) { + return $clauses; + } + + // TODO: find a better way other than clearing the cache + wp_cache_flush_group('site-queries'); + + global $wpdb; + + $clauses['join'] .= " LEFT JOIN {$wpdb->base_prefix}institutions_blogs ON {$wpdb->blogs}.blog_id = {$wpdb->base_prefix}institutions_blogs.blog_id"; + $clauses['join'] .= " LEFT JOIN {$wpdb->base_prefix}institutions ON {$wpdb->base_prefix}institutions_blogs.institution_id = {$wpdb->base_prefix}institutions.id"; + + $clauses['fields'] .= ", {$wpdb->base_prefix}institutions.name AS institution"; + + $institutionId = get_institution_by_manager(); + + if ($institutionId > 0) { + $clauses['where'] .= $wpdb->prepare(" AND {$wpdb->base_prefix}institutions.id = %d", $institutionId); + } + + $orderBy = $_REQUEST['orderby'] ?? null; + $order = $_REQUEST['order'] ?? 'asc'; + + if ($orderBy === 'institution') { + $clauses['orderby'] = $order === 'asc' + ? "{$wpdb->base_prefix}institutions.name ASC" + : "{$wpdb->base_prefix}institutions.name DESC"; + } + + return $clauses; + }); + + // add_filter('sites_pre_query', function (array|null $site_data, WP_Site_Query $query): array|null { + // dump($site_data); + // + // dump($query); + // + // return $site_data; + // }, 10, 2); + } + + public function addInstitutionColumn(array $columns): array + { + return [ + ... array_splice($columns, 0, 4), + 'institution' => __('Institution', 'pressbooks-multi-institution'), + ...$columns, + ]; + } + + public function renderInstitutionColumn(string $columnId, int $blogId): void + { + if ($columnId !== 'institution') { + return; + } + + if (is_main_site($blogId)) { + return; + } + + /** @var Institution|null $institution */ + $institution = Institution::query() + ->whereHas('books', fn ($query) => $query->where('blog_id', $blogId)) + ->first(); + + echo $institution->name ?? __('Unassigned', 'pressbooks-multi-institution'); + } + + public function addInstitutionAsSortableColumn(array $columns): array + { + return [ + ...$columns, + 'institution' => 'institution', + ]; + } +}