<& /htmlheader &>

<h2>MOPS Channel Details</h2>

% # Fix channel name.
% $nodeID =~ s/$NODE_ROOT//;
% # Fix subscriber name
% my $owner = substr($oref->{owner}, 0, index($oref->{owner}, "@" . $ALERT_SERVER));
% # Fix date: we have here the number of millisecs since 1970.
% my $sec;
% my $min;
% my $hour;
% my $mday;
% my $mon;
% my $year;
% my $wday;
% my $yday;
% my $isdst;
% my $t;
% my $creationDate;
% my $modifDate;
% my $user;
% my $email;
<b>Channel name:</b> <% $nodeID %><br />
<b>Channel owner:</b> <% $owner %><br />
<b>Subscribers</b><br />
<table border=1 cellspacing=2>
<tr> 
    <td>
        <b>Username</b>
    </td>
    <td>
        <b>Name</b>
    </td>
    <td>
        <b>Email</b>
    </td>
    <td>
        <b>Creation Date (UT)</b>
    </td>
    <td>
        <b>Modification Date (UT)</b>
    </td>
</tr>
% foreach my $uref (@users) {
<tr> 
    <td>
        <% $uref->{username} %>
    </td>
    <td>
% if(! $uref->{name}) {
%     $user = "N/A";
% } else {
%     $user = $uref->{name};
% }
        <% $user %>
    </td>
    <td>
% if(! $uref->{email}) {
%     $email = "N/A";
% } else {
%     $email = $uref->{email};
% }
        <% $email %>
    </td>
    <td>
% $t = $uref->{creationDate} / 1000.0;
% ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime($t);
% $creationDate = sprintf("%4d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
        <% $creationDate %>
    </td>
    <td>
% $t = $uref->{creationDate} / 1000.0;
% ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = gmtime($t);
% $modifDate = sprintf("%4d-%02d-%02dT%02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
        <% $modifDate %>
    </td>
</tr>
% }
</table>

<& /htmlfooter &>

<%args>
$nodeID => undef
</%args>

<%init>
use DBI;


my $DBSERVER = "mops00.ifa.hawaii.edu";
my $ALERT_SERVER = "mopshq2.ifa.hawaii.edu";
my $USER = "mops";
my $PASSWD = "mops";
my $DB = "openfire";
my $NODE_ROOT = "home/" . $ALERT_SERVER . "/";

my $connectionStr = "dbi:mysql:host=" . $DBSERVER . ";database=" . $DB;
my $dbh = DBI->connect($connectionStr, $USER, $PASSWD);
my $dbh2 = DBI->connect($connectionStr, $USER, $PASSWD);

# Fetch the channel owner.
my $cur = $dbh->prepare("select owner from ofPubsubSubscription where nodeID=?");
$cur->{mysql_use_result} = 1;
$cur->execute($nodeID);
my $oref = $cur->fetchrow_hashref;

# Now fetch the subscribers.
$cur = $dbh->prepare("select jid from ofPubsubSubscription where nodeID=? and jid not like 'mops@%' and state = 'subscribed'");
$cur->{mysql_use_result} = 1;
$cur->execute($nodeID);

my $aref;
my $username;
my @users;
while($aref = $cur->fetchrow_hashref) {
    $username = substr($aref->{jid}, 0, index($aref->{jid}, "@" . $ALERT_SERVER));
    push @users, $dbh2->selectrow_hashref("select username, name, email, creationDate, modificationDate from ofUser where username=?", undef, $username);
}
</%init>
