Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: include institution information on OS book list #160

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PressbooksMultiInstitution\Services\PermissionsManager;
use PressbooksMultiInstitution\Views\BookList;
use PressbooksMultiInstitution\Views\UserList;
use PressbooksMultiInstitution\Views\WpBookList;

/**
* Class Bootstrap
Expand Down Expand Up @@ -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')));
}

Expand Down
3 changes: 3 additions & 0 deletions src/Services/PermissionsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
104 changes: 104 additions & 0 deletions src/Views/WpBookList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace PressbooksMultiInstitution\Views;

use PressbooksMultiInstitution\Models\Institution;

use WP_Site_Query;

use function PressbooksMultiInstitution\Support\get_institution_by_manager;

class WpBookList
{
public function init(): void
{
add_filter('wpmu_blogs_columns', [$this, 'addInstitutionColumn']);
add_action('manage_sites_custom_column', [$this, 'renderInstitutionColumn'], 10, 2);
add_filter('manage_sites-network_sortable_columns', [$this, 'addInstitutionAsSortableColumn']);

// Modify WP_Site_Query clauses
add_filter('sites_clauses', function (array $clauses): array {
if (get_current_screen()?->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',
];
}
}
Loading