Skip to content

Commit

Permalink
Merge pull request #473 from Sae126V/GT-141-Add-Support-to-Pre-select…
Browse files Browse the repository at this point in the history
…-site,-services-and-endpoints

[GT-141] Add support for pre selecting endpoints
  • Loading branch information
gregcorbett authored Nov 29, 2023
2 parents c065546 + 7d2f569 commit bc68c0b
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 23 deletions.
68 changes: 66 additions & 2 deletions htdocs/web_portal/controllers/downtime/add_downtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
require_once __DIR__.'/../utils.php';
require_once __DIR__.'/../../../web_portal/components/Get_User_Principle.php';

use Exception;

/**
* Controller for a new_downtime request.
*
Expand Down Expand Up @@ -158,6 +160,18 @@ function draw(\User $user = null) {
die(json_encode(array('UTC', 0)));
}

/**
* URL Mapping for `site` and `se` (Service Endpoint).
*
* If a user wants to add downtime to a specific
* `site` and `se` (Service Endpoint), the portal will
* pre-select the service endpoint corresponding to the `se` parameter
* and the endpoints of that service.
*/
elseif (isset($_GET['site']) && isset($_GET['se'])) {
displaySiteAndSeEndpoints($user);
}

// URL Mapping
// If the user wants to add a downtime to a specific site, show only that site's SEs
else if(isset($_GET['site'])) {
Expand Down Expand Up @@ -210,10 +224,60 @@ function draw(\User $user = null) {
throw new Exception("You don't hold a role over a NGI "
. "or site with child services.");
}
$params = array('ses' => $ses, 'nowUtc' => $nowUtcDateTime->format('H:i T'));

$params = array(
'ses' => $ses,
'userCannotPreSelect' => true
);

show_view("downtime/add_downtime.php", $params);
die();
}
}

?>
/**
* Helper method to fetch service endpoint detail(s) associated
* with `site` provided.
* This will help portal to pre-select endpoints corresponding
* to the `se` (Service Endpoint) provided.
*/
function displaySiteAndSeEndpoints($user)
{
$site = \Factory::getSiteService()->getSite($_GET['site']);
$ses = $site->getServices();

if (!hasEditPermission($site, $user)) {
throwPermissionException($site, true);
}

$params = [
'ses' => $ses
];

show_view("downtime/add_downtime.php", $params);
die();
}

// Validates if the user has edit permission for the given site.
function hasEditPermission($site, $user)
{
return \Factory::getRoleActionAuthorisationService()
->authoriseAction(\Action::EDIT_OBJECT, $site, $user)
->getGrantAction();
}

/**
* Handles exceptions for permission-related issues.
*
* @throws Exception
*/
function throwPermissionException($resource, $isGeneric)
{
if ($isGeneric) {
$errorMsg = "You do not have permission over $resource";
} else {
$errorMsg = "$resource";
}

throw new Exception($errorMsg);
}
5 changes: 5 additions & 0 deletions htdocs/web_portal/controllers/downtime/view_endpoint_tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ function getServiceandEndpointList() {
require_once __DIR__ . '/../utils.php';
require_once __DIR__ . '/../../../web_portal/components/Get_User_Principle.php';

$params = [];
$dn = Get_User_Principle();
$user = \Factory::getUserService()->getUserByPrinciple($dn);
$params['portalIsReadOnly'] = portalIsReadOnlyAndUserIsNotAdmin($user);

if (!isset($_REQUEST['site_id']) || !is_numeric($_REQUEST['site_id']) ){
throw new Exception("An id must be specified");
}
if (isset($_REQUEST['se'])) {
$params['se'] = $_REQUEST['se'];
}

$site = \Factory::getSiteService()->getSite($_REQUEST['site_id']);
$services = $site->getServices();
$params['services'] = $services;
Expand Down
59 changes: 50 additions & 9 deletions htdocs/web_portal/views/downtime/add_downtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,14 @@ class="form-control" id="Select_Sites" name="select_sites" size="10"
$siteName = $site->getName();
$ngiName = $site->getNgi()->getName();
$label = xssafe($site." (".$ngiName.")");
echo "<option value=\"{$site->getId()}\">$label</option>";
echo "<option value=\"{$site->getId()}\"";
if ($params['userCannotPreSelect']) {
echo ">";
} else {
echo " SELECTED>";
}
echo "$label";
echo "</option>";
}
?>
</select> <br /> <br />
Expand Down Expand Up @@ -231,7 +238,11 @@ class="form-control" id="Select_Sites" name="select_sites" size="10"
validate();
});


/**
* Helps us to decide whether to fetch
* and `select` all services, endpoints or NOT.
*/
getSitesServices();

});

Expand Down Expand Up @@ -494,14 +505,44 @@ function validateUtcDates(){
}

function getSitesServices(){
var siteId=$('#Select_Sites').val();
if(siteId != null){ //If the user clicks on the box but not a specific row there will be no input, so catch that here
$('#chooseEndpoints').empty(); //Remove any previous content from the endpoints select list
$('#chooseServices').load('index.php?Page_Type=Downtime_view_endpoint_tree&site_id='+siteId,function( response, status, xhr ) {
if ( status == "success" ) {
validate();
let selectedSiteID = $('#Select_Sites').val();

/**
* If the user clicks on the box but not a specific row
* there will be no input, so catch that here.
*/
if (selectedSiteID != null) {
// Remove any previous content from the endpoints select list.
$('#chooseEndpoints').empty();

let urlpathAndSearchName;
const SE_FROM_QUERY_PARAMS = new URL(
this.location.href
).searchParams.get('se');

if (SE_FROM_QUERY_PARAMS) {
/**
* NOTE: If you use template literal for string interpolation,
* i.e., `${}`. It has to be in one line.
*/
urlpathAndSearchName = "index.php?"
+ "Page_Type=Downtime_view_endpoint_tree"
+ `&se=${SE_FROM_QUERY_PARAMS}`
+ `&site_id=${selectedSiteID}`;
} else {
urlpathAndSearchName = "index.php?"
+ "Page_Type=Downtime_view_endpoint_tree"
+ `&site_id=${selectedSiteID}`;
}

$('#chooseServices').load(
urlpathAndSearchName,
function(response, status, xhr) {
if ( status == "success" ) {
validate();
}
}
});
);
}
}

Expand Down
55 changes: 43 additions & 12 deletions htdocs/web_portal/views/downtime/view_nested_endpoints_list.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,57 @@
<?php
$services = $params['services'];
$specificServiceEndpoint = isset($params['se']) ? $params['se'] : '';
$configService = \Factory::getConfigService();

?>
<!-- Dynamically create a select list from a sites services -->
<label> Select Affected Services+Endpoints (Ctrl+click to select)</label>
<select name="IMPACTED_IDS[]" id="Select_Services" size="10" class="form-control" onclick="" style="width:99%; margin-left:1%" onChange="selectServicesEndpoint()" multiple>
<?php
foreach($services as $service){
$count=0;
echo "<option value=\"s" . $service->getId() . "\" id=\"" . $service->getId() . "\" SELECTED>" . '('.xssafe($service->getServiceType()->getName()).') '.xssafe($service->getHostName()) . "</option>";
foreach($service->getEndpointLocations() as $endpoint){
if($endpoint->getName() == ''){
$name = xssafe('myEndpoint');
}else{
$name = xssafe($endpoint->getName());
foreach ($services as $service) {
$count = 0;

if ($specificServiceEndpoint) {
if ($service->getId() == $specificServiceEndpoint) {
$selected = 'SELECTED';
} else {
$selected = '';
}
} else {
$selected = 'SELECTED';
}

echo "<option value=\"s" . $service->getId() . "\" id=\""
. $service->getId() . "\" " . $selected . ">" . '('
. xssafe($service->getServiceType()->getName()) . ') '
. xssafe($service->getHostName()) . "</option>";

foreach ($service->getEndpointLocations() as $endpoint) {
if ($specificServiceEndpoint) {
if ($service->getId() == $specificServiceEndpoint) {
$selected = 'SELECTED';
} else {
$selected = '';
}
//Option styling doesn't work well cross browser so just use 4 spaces to indent the branch
echo "<option id=\"".$service->getId()."\" value=\"e" . $endpoint->getId() . "\" SELECTED>&nbsp&nbsp&nbsp&nbsp-" . $name . "</option>";
$count++;
} else {
$selected = 'SELECTED';
}

if ($endpoint->getName() == '') {
$name = xssafe('myEndpoint');
} else {
$name = xssafe($endpoint->getName());
}

/**
* Option styling doesn't work well cross browser,
* so just use 4 spaces to indent the branch.
*/
echo "<option id=\"" . $service->getId() . "\" value=\"e"
. $endpoint->getId() . "\" " . $selected
. ">&nbsp&nbsp&nbsp&nbsp-" . $name . "</option>";

$count++;
}
}
?>
</select>

0 comments on commit bc68c0b

Please sign in to comment.