Skip to content

Commit

Permalink
Migrate helpers in NDB_Menu_Filter
Browse files Browse the repository at this point in the history
  • Loading branch information
laemtl committed Aug 18, 2021
1 parent 1a067ea commit 27821bc
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
Expand Down
2 changes: 1 addition & 1 deletion modules/genomic_browser/php/cnvbrowser.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion modules/genomic_browser/php/methylation.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion modules/genomic_browser/php/profiles.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion modules/genomic_browser/php/snpbrowser.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
91 changes: 0 additions & 91 deletions php/libraries/DataFrameworkMenu.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

95 changes: 95 additions & 0 deletions php/libraries/NDB_Menu_Filter.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ($allsitesPermission && $user->hasAnyPermission($allsitesPermission)) {
$siteOptions = \Utility::getSiteList($studySite);
} else {
$siteOptions = $user->getStudySites();
}

if (!$numericKeys) {
$siteNames = array_values($siteOptions);
return array_combine($siteNames, $siteNames);
}

return $siteOptions;
}
}

0 comments on commit 27821bc

Please sign in to comment.