From 7dfc17d0009dffef00a9fe428c648a16e841d7d0 Mon Sep 17 00:00:00 2001 From: Laetitia Fesselier Date: Mon, 16 Aug 2021 16:53:15 -0400 Subject: [PATCH] Migrate helpers in NDB_Menu_Filter --- .../php/electrophysiology_browser.class.inc | 6 +- .../genomic_browser/php/cnvbrowser.class.inc | 2 +- .../genomic_browser/php/methylation.class.inc | 2 +- .../genomic_browser/php/profiles.class.inc | 2 +- .../genomic_browser/php/snpbrowser.class.inc | 2 +- php/libraries/DataFrameworkMenu.class.inc | 91 ------------------ php/libraries/NDB_Menu_Filter.class.inc | 95 +++++++++++++++++++ 7 files changed, 104 insertions(+), 96 deletions(-) diff --git a/modules/electrophysiology_browser/php/electrophysiology_browser.class.inc b/modules/electrophysiology_browser/php/electrophysiology_browser.class.inc index 7a0b523c8d1..f82d455d3ca 100644 --- a/modules/electrophysiology_browser/php/electrophysiology_browser.class.inc +++ b/modules/electrophysiology_browser/php/electrophysiology_browser.class.inc @@ -81,7 +81,11 @@ class Electrophysiology_Browser extends \DataFrameworkMenu } return [ - 'sites' => $this->getSiteOptions($user, false), + 'sites' => $this->getSiteOptions( + $user, + false, + $this->allSitePermissionNames() + ), 'projects' => $this->getProjectOptions($user, false), 'types' => $list_of_types ]; diff --git a/modules/genomic_browser/php/cnvbrowser.class.inc b/modules/genomic_browser/php/cnvbrowser.class.inc index 5b5f3618b1c..3d9220dde53 100644 --- a/modules/genomic_browser/php/cnvbrowser.class.inc +++ b/modules/genomic_browser/php/cnvbrowser.class.inc @@ -96,7 +96,7 @@ class CnvBrowser extends \DataFrameworkMenu implements ETagCalculator return (new \LORIS\genomic_browser\Views\CNV( $platform_options, - $this->getSiteOptions($user, false), + $this->getSiteOptions($user, false, $this->allSitePermissionNames()), $this->getSubprojectOptions($user), ))->toArray(); } diff --git a/modules/genomic_browser/php/methylation.class.inc b/modules/genomic_browser/php/methylation.class.inc index 74378a8bf5d..adfe557fd3e 100644 --- a/modules/genomic_browser/php/methylation.class.inc +++ b/modules/genomic_browser/php/methylation.class.inc @@ -84,7 +84,7 @@ class Methylation extends \DataFrameworkMenu implements ETagCalculator $user = \NDB_Factory::singleton()->user(); return (new \LORIS\genomic_browser\Views\Methylation( - $this->getSiteOptions($user, false), + $this->getSiteOptions($user, false, $this->allSitePermissionNames()), $this->getSubprojectOptions($user), ))->toArray(); } diff --git a/modules/genomic_browser/php/profiles.class.inc b/modules/genomic_browser/php/profiles.class.inc index e4c3c97b2b5..d18b47d6ac8 100644 --- a/modules/genomic_browser/php/profiles.class.inc +++ b/modules/genomic_browser/php/profiles.class.inc @@ -83,7 +83,7 @@ class Profiles extends \DataFrameworkMenu implements ETagCalculator $user = \NDB_Factory::singleton()->user(); return (new \LORIS\genomic_browser\Views\Profiles( - $this->getSiteOptions($user, false), + $this->getSiteOptions($user, false, $this->allSitePermissionNames()), $this->getSubprojectOptions($user, false), ))->toArray(); } diff --git a/modules/genomic_browser/php/snpbrowser.class.inc b/modules/genomic_browser/php/snpbrowser.class.inc index dc90e9fffa2..879c91239a3 100644 --- a/modules/genomic_browser/php/snpbrowser.class.inc +++ b/modules/genomic_browser/php/snpbrowser.class.inc @@ -92,7 +92,7 @@ class SnpBrowser extends \DataFrameworkMenu implements ETagCalculator $platform_options[$name] = $name; } return (new \LORIS\genomic_browser\Views\SNP( - $this->getSiteOptions($user, false), + $this->getSiteOptions($user, false, $this->allSitePermissionNames()), $this->getSubprojectOptions($user, false), $platform_options, ))->toArray(); diff --git a/php/libraries/DataFrameworkMenu.class.inc b/php/libraries/DataFrameworkMenu.class.inc index a9cd70ca1ff..2934da6caaf 100644 --- a/php/libraries/DataFrameworkMenu.class.inc +++ b/php/libraries/DataFrameworkMenu.class.inc @@ -187,96 +187,5 @@ abstract class DataFrameworkMenu extends NDB_Menu_Filter return new \LORIS\Http\DataIteratorBinaryStream($rows); } - - /** - * Format the user's projects to display the module filter options - * - * @param \User $user User - * @param bool $numericKeys If the returned projects keys - * should be numeric (ProjectID) or project name - * - * @return array - an associative array of the form (ID|mame => name) - */ - public function getProjectOptions( - \User $user, - bool $numericKeys = true - ): array { - $projects = $user->getProjects(); - $projectNames = array_map( - fn($project) => $project->getName(), - $projects - ); - - if ($numericKeys) { - $projectIDs = array_map( - fn($project) => strval($project->getId()), - $projects - ); - return array_combine($projectIDs, $projectNames); - } - - return array_combine($projectNames, $projectNames); - } - - /** - * Format the user's subprojects to display the module filter options - * - * @param \User $user User - * @param bool $numericKeys If the returned subprojects keys - * should be numeric (SubprojectID) - * or strings (subproject name) - * - * @return array - an associative array of the form (ID => name) - */ - public function getSubprojectOptions( - \User $user, - bool $numericKeys = true - ): array { - $subprojectOptions = array_reduce( - $user->getProjects(), - fn($subprojects, $project) => - $subprojects - + array_column($project->getSubprojects(), 'title', 'subprojectId'), - [] - ); - - if (!$numericKeys) { - $subprojectNames = array_values($subprojectOptions); - return array_combine($subprojectNames, $subprojectNames); - } - - return $subprojectOptions; - } - - /** - * Format the user's sites to display the module filter options - * - * @param \User $user User - * @param bool $numericKeys If the returned sites keys should be - numeric (CenterID) or strings (site name) - * @param bool $studySite If the returned sites should be study sites only - * - * @return array An associative array of the form (ID|name => name) - */ - public function getSiteOptions( - \User $user, - bool $numericKeys = true, - bool $studySite = true - ): array { - // If the user has any of the All Site permissions, - // get the list of all study sites - if ($user->hasAnyPermission($this->allSitePermissionNames())) { - $siteOptions = \Utility::getSiteList($studySite); - } else { - $siteOptions = $user->getStudySites(); - } - - if (!$numericKeys) { - $siteNames = array_values($siteOptions); - return array_combine($siteNames, $siteNames); - } - - return $siteOptions; - } } diff --git a/php/libraries/NDB_Menu_Filter.class.inc b/php/libraries/NDB_Menu_Filter.class.inc index a0fa596e336..c14fae479c3 100644 --- a/php/libraries/NDB_Menu_Filter.class.inc +++ b/php/libraries/NDB_Menu_Filter.class.inc @@ -849,5 +849,100 @@ class NDB_Menu_Filter extends NDB_Menu ] ); } + + /** + * Format the user's projects to display the module filter options + * + * @param \User $user User + * @param bool $numericKeys If the returned projects keys + * should be numeric (ProjectID) or project name + * + * @return array - an associative array of the form (ID|mame => name) + */ + public function getProjectOptions( + \User $user, + bool $numericKeys = true + ): array { + $projects = $user->getProjects(); + $projectNames = array_map( + fn($project) => $project->getName(), + $projects + ); + + if ($numericKeys) { + $projectIDs = array_map( + fn($project) => strval($project->getId()), + $projects + ); + return array_combine($projectIDs, $projectNames); + } + + return array_combine($projectNames, $projectNames); + } + + /** + * Format the user's subprojects to display the module filter options + * + * @param \User $user User + * @param bool $numericKeys If the returned subprojects keys + * should be numeric (SubprojectID) + * or strings (subproject name) + * + * @return array - an associative array of the form (ID => name) + */ + public function getSubprojectOptions( + \User $user, + bool $numericKeys = true + ): array { + $subprojectOptions = array_reduce( + $user->getProjects(), + fn($subprojects, $project) => + $subprojects + + array_column($project->getSubprojects(), 'title', 'subprojectId'), + [] + ); + + if (!$numericKeys) { + $subprojectNames = array_values($subprojectOptions); + return array_combine($subprojectNames, $subprojectNames); + } + + return $subprojectOptions; + } + + /** + * Format the user's sites to display the module filter options + * + * @param \User $user User + * @param bool $numericKeys If the returned sites keys should be + numeric (CenterID) or strings (site name) + * @param array $allsitesPermission A list of permission + * which gives access to all sites + * @param bool $studySite If the returned sites should be + * study sites only + * + * @return array An associative array of the form (ID|name => name) + */ + public function getSiteOptions( + \User $user, + bool $numericKeys = true, + array $allsitesPermission = ['access_all_profiles'], + bool $studySite = true + ): array { + // If the user has any of the All Site permissions, + // get the list of all study sites + if ($user->hasAnyPermission($allsitesPermission)) { + $siteOptions = \Utility::getSiteList($studySite); + } else { + $siteOptions = $user->getStudySites(); + } + + if (!$numericKeys) { + $siteNames = array_values($siteOptions); + return array_combine($siteNames, $siteNames); + } + + return $siteOptions; + } }