[MOD] Sort group members by hostname - 0.32, Ubuntu 10.04 LTS
-
I got really annoyed with the membership page for groups. The list was in seemingly random order and it make finding a particular member a bit of a pain. Plus I like things sorted.
I basically copied a an existing function and modified it to do what I wanted, then made the calling page use the new function. This way I didn’t step on anything FOG was doing elsewhere. I’m pretty sure now that I could have just modified the original function, but I haven’t had the time to verify.
First, you’ll want to make a copy of the original file before modifying it.
[CODE]
sudo cp /var/www/fog/commons/functions.include.php /var/www/fog/commons/functions.include.BACKUP.php
[/CODE]Then open up the /var/www/fog/commons/functions.include.php and go to line 1425. This should be the end of the function getImageMembersByGroupID.
Make a few blank lines below the end of the function and paste in the following.
[CODE]
function getImageMembersByGroupIDSorted( $conn, $groupID )
{
$arM = array();
if ( $conn != null && $groupID != null )
{
$sql = “SELECT
groupMembers.gmHostID
FROM
groups inner join groupMembers
on (groups.groupID = groupMembers.gmGroupID)
inner join hosts
on (hosts.hostID = gmHostID)
WHERE groups.groupID = $groupID
ORDER BY hosts.hostName”;$res = mysql_query( $sql, $conn ) or criticalError( mysql_error(), _("FOG :: Database error!") ); $arHosts = array(); while( $ar = mysql_fetch_array( $res ) ) { $arHosts[] = $ar["gmHostID"]; } for( $i = 0; $i < count( $arHosts ); $i++ ) { $tmpMember = getImageMemberFromHostID( $conn, $arHosts[$i] ); if( $tmpMember != null ) $arM[] = $tmpMember; } } return $arM;
}
[/CODE]Save the file.
Now to edit the file that uses this function.
Make a backup of the file, just in case.
[CODE] sudo cp /var/www/fog/management/includes/groups.edit.include.php /var/www/fog/management/includes/groups.edit.include.BACKUP.php[/CODE]Now open the file for editing.
[CODE]
sudo nano -w /var/www/fog/management/includes/groups.edit.include.php
[/CODE]On or near line 641, you’ll be inside the if ( $_GET[“tab”] == “member” ) block. Change the function call to your new function name.
Original line:
[CODE]$members = getImageMembersByGroupID( $conn, $ar[“groupID”] );[/CODE]
Modified line:
[CODE]$members = getImageMembersByGroupIDSorted( $conn, $ar[“groupID”] );[/CODE]Save the file. Now membership listing should display sorted by hostname.
-
I’m going to try implementing this within 0.33b
I hope you don’t mind.
-
I don’t mind at all. I haven’t been keen on trying to mod 0.33 code because I don’t yet understand the class changes and what code might be abandoned or moved into methods as opposed to being in include files.
-
From what I can tell, all include elements have a class equivalent, it’s just a matter of moving the code from the include into the proper class.
I only just started getting into the bowels of FOG 0.33b, so it’s kind of fascinating that things were missing. My only issue so far is figuring out how to allow the css to maintain display: none rather than display: hidden for the group element.
EDIT: just to add more info.
The same commons/functions.include.php contains the original element you changed. So I’ll just make the mods to that so it’s built directly into the original function.
-
If it’s any help to anybody:
Locate the function [FONT=Consolas]getImageMembersByGroupID [/FONT]in:
{fogwebdir}/commons/functions.include.phpThe only change within this function is the select statement from lines 1405 to 1409:
[CODE]$sql = “select
*
from groups
inner join groupMembers on ( groups.groupID = groupMembers.gmGroupID )
where groupID = $groupID”;[/CODE]
[FONT=Courier New][COLOR=#000000]to (add lines 1409 thru 1413)[/COLOR][/FONT]
[CODE]$sql = “SELECT
groupMembers.gmHostID
FROM
groups inner join groupMembers
on (groups.groupID = groupMembers.gmGroupID)
inner join hosts
on (hosts.hostID = gmHostID)
WHERE groups.groupID = $groupID
ORDER BY hosts.hostName”;[/CODE][FONT=Consolas]Really it’s quite beautiful.[/FONT]
-
Thanks. I could not find a reason to query for all fields in the table (*) when only the gmHostID was ever being used. In all the calls to the function I could never see where other fields were being used out of the result set. Since I didn’t want to mess something up I didn’t know about, I made a new function and changed the call in the specific page.