Bug 459251 - Implement new donor list page

Change-Id: I4111843ff03dbc30131f34ee61823fa703259d04
Signed-off-by: Christopher Guindon <chris.guindon@eclipse.org>
Also-by: Denis Roy <denis.roy@eclipse.org>
diff --git a/content/en_donorlist.php b/content/en_donorlist.php
new file mode 100644
index 0000000..be31eaa
--- /dev/null
+++ b/content/en_donorlist.php
@@ -0,0 +1,42 @@
+<?php
+/*******************************************************************************
+ * Copyright (c) 2014, 2015 Eclipse Foundation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *    Nathan Gervais (Eclipse Foundation) - Initial implementation (2007-11-07)
+ *    Edouard Poitras (Eclipse Foundation) - Further modifications
+ *    Christopher Guindon (Eclipse Foundation)
+ *******************************************************************************/
+?>
+<div>
+    <h1><?=$pageTitle;?></h1>
+    <div role="tabpanel">
+      <ul class="nav nav-tabs tabs" role="tablist">
+        <li role="presentation" class="active"><a href="#best-friends" aria-controls="best-friends" role="tab" data-toggle="tab">Best Friends</a></li>
+        <li role="presentation"><a href="#friends" aria-controls="friends" role="tab" data-toggle="tab">Friends</a></li>
+        <li role="presentation"><a href="#all" aria-controls="all" role="tab" data-toggle="tab">All</a></li>
+        <li role="presentation"><a href="/donate">Join Us</a></li>
+      </ul>
+    </div>
+    <div class="tab-content">
+      <div role="tabpanel" class="tab-pane fade in active" id="best-friends">
+        <?=displayPager($bfstart, $pageValue, $totalBestFriends, 'bfstart', '#best-friends');?>
+        <?=displayFriends($bestFriends);?>
+        <?=displayPager($bfstart, $pageValue, $totalBestFriends, 'bfstart', '#best-friends');?>
+      </div>
+      <div role="tabpanel" class="tab-pane fade" id="friends">
+        <?=displayPager($fstart, $pageValue, $totalFriends, 'fstart', '#friends');?>
+        <?=displayFriends($friends);?>
+        <?=displayPager($fstart, $pageValue, $totalFriends, 'fstart', '#friends');?>
+      </div>
+      <div role="tabpanel" class="tab-pane fade" id="all">
+        <?=displayPager($start, $pageValue, $totalContributionsCount, 'start', '#all');?>
+        <?=displayTable($contributions);?>
+        <?=displayPager($start, $pageValue, $totalContributionsCount, 'start', '#all');?>
+      </div>
+    </div>
+</div>
\ No newline at end of file
diff --git a/donorlist.php b/donorlist.php
index d40b74a..7883737 100644
--- a/donorlist.php
+++ b/donorlist.php
@@ -1,6 +1,6 @@
-<?php  																														require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/app.class.php");	require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/nav.class.php"); 	require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/menu.class.php"); 	$App 	= new App();	$Nav	= new Nav();	$Menu 	= new Menu();		include($App->getProjectCommon());    # All on the same line to unclutter the user's desktop'
+<?php
 /*******************************************************************************
- * Copyright (c) 2014 Eclipse Foundation and others.
+ * Copyright (c) 2014, 2015 Eclipse Foundation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -9,140 +9,68 @@
  * Contributors:
  *    Nathan Gervais (Eclipse Foundation) - Initial implementation (2007-11-07)
  *    Edouard Poitras (Eclipse Foundation) - Further modifications
+ *    Christopher Guindon (Eclipse Foundation)
  *******************************************************************************/
-	#
-	# Begin: page-specific settings.  Change these.
-	$pageTitle 		= "Donor List";
-	$pageKeywords	= "friends of eclipse, donation, contribution";
-	$pageAuthor		= "Nathan Gervais";
-	header("Cache-control: no-cache");
-	require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/smartconnection.class.php");
-	require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/classes/friends/friendsContributionsList.class.php");
-	require_once("functions.php");
-	ob_start();
+  require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/app.class.php");
+  require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/nav.class.php");
+  require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/menu.class.php");
+  require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/system/smartconnection.class.php");
+  require_once($_SERVER['DOCUMENT_ROOT'] . "/eclipse.org-common/classes/friends/friendsContributionsList.class.php");
+  require_once("functions.php");
 
-	$showAll = $App->getHTTPParameter('showAll');
-	if ($showAll == NULL) {
-		$showAll = 1;
-	}
-	if ($showAll == 0)
-	{
-		$showAll = 0;
-		$where = 'WHERE F.is_benefit = 1';
-	}
-	else {
-		$showAll = 1;
-		$where = NULL;
-	}
-	$start = $App->getHTTPParameter('start');
+  $App = new App();
+  $Menu = new Menu();
+  include($App->getProjectCommon());
+  $App->preventCaching();
+
+  # Begin: page-specific settings.  Change these.
+  $pageTitle     = "Hall of Friends";
+  $pageKeywords  = "friends of eclipse, donation, contribution";
+  $pageAuthor    = "Christopher Guindon";
+  ob_start();
+
+  $start = $App->getHTTPParameter('start');
+  $fstart = $App->getHTTPParameter('fstart');
+  $bfstart = $App->getHTTPParameter('bfstart');
+
+  $pageValue = 20;
+  if ( (!$start) || (!preg_match('/^[0-9]+$/', $start))) {
+      $start = 0;
+  }
+  if ( (!$fstart) || (!preg_match('/^[0-9]+$/', $fstart))) {
+      $fstart = 0;
+  }
+  if ( (!$bfstart) || (!preg_match('/^[0-9]+$/', $bfstart))) {
+      $bfstart = 0;
+  }
+
+  $totalContributionsCount = getFriendsCount(TRUE, TRUE, 0);
+  $contributionsList = new FriendsContributionsList();
+  $contributionsList->selectFriendsContributionsList($start, $pageValue);
+  $contributions = $contributionsList->getList();
+
+  $totalFriends = getFriendsCount(FALSE, TRUE, 35);
+  $friendsContributions = new FriendsContributionsList();
+  $friendsContributions->selectFriendsContributionsList($fstart, $pageValue, 'WHERE FC.amount >= 35 and F.is_anonymous = 0');
+  $friends = $friendsContributions->getList();
+
+  $totalBestFriends = getFriendsCount(FALSE, TRUE, 100);
+  $bestFriendsContributions = new FriendsContributionsList();
+  $bestFriendsContributions->selectFriendsContributionsList($bfstart, $pageValue, 'WHERE FC.amount >= 100 and F.is_anonymous = 0');
+  $bestFriends = $bestFriendsContributions->getList();
 
 
-	$pageValue = 25;
-	if ( (!$start) || (!preg_match('/^[0-9]+$/', $start))) {
-		$start = 0;
-	}
+  // Place your html content in a file called content/en_pagename.php
+  ob_start();
+  include("content/en_" . $App->getScriptName());
+  $html = ob_get_clean();
 
-	$totalContributionCount = new FriendsContributionsList();
-	$totalContributionCount->selectFriendsContributionsList(-1, -1 /*, 'WHERE F.is_benefit = 1'*/);
-	$pageCount = $totalContributionCount->getCount();
-	$totalContributionCount = NULL;
-	?>
-	<link rel="stylesheet" type="text/css" href="style.css" media="screen" />
-	<div id="midcolumn">
-		<h1><?=$pageTitle;?></h1>
-		<? if (!$showAll){ ?>
-		<a href="<?=$SERVER['PHP_SELF'];?>?showAll=1">Show All</a>
-		<? } else { ?>
-		<a href="donorlist.php?showAll=0">Show Friends Only</a>
-		<? } ?>
-		<?=displayPager($start, $pageValue, $pageCount, $showAll);?>
-		<table class="donorList" cellspacing=0>
-			<tr class="donorHeader">
-				<td colspan="2" width="60%">Name and Message</td>
-				<td width="20%">Date</td>
-				<td width="20%" align="right">Amount</td>
-			</tr>
-			<?
-				// Get total number of items so we can know whether to page or not.
+  // Custom theme variables
+  $variables = array();
+  $App->setThemeVariables($variables);
 
+  header('Content-Type: text/html; charset=utf-8;');
 
-				$friendsContributionsList = new FriendsContributionsList();
-				$friendsContributionsList->selectFriendsContributionsList($start, $pageValue, $where);
-
-				$friend = new Friend();
-				$contribution = new Contribution();
-				$fcObject = new FriendsContributions();
-				$count = $friendsContributionsList->getCount();
-				for ($i=0; $i < $count; $i++)
-				{
-					$fcObject = $friendsContributionsList->getItemAt($i);
-					$friend = $fcObject->getFriendObject();
-					$contribution = $fcObject->getContributionObject();
-					$anonymous = $friend->getIsAnonymous();
-					if ($anonymous != 1 && ($friend->getFirstName() != '' || $friend->getLastName() != ''))
-						$name = $friend->getFirstName() . " " . $friend->getLastName();
-					else
-						$name = "Anonymous";
-					$benefit = $friend->getIsBenefit();
-					if ($benefit != 0)
-						$benefit = " <img width='25' src=\"images/stars.png\">";
-					else
-						$benefit = "";
-					$amount = $contribution->getAmount();
-					if (strpos($amount, ".") == 0)
-					{
-						$amount = $amount . ".00";
-					}
-					$comment =  stripslashes(strip_tags($contribution->getMessage()));
-					if (strlen($comment) > 80)
-						if (strpos($comment, ' ') == 0 )
-						{
-							$commentArray = str_split($comment, 80);
-							$comment = 0;
-							foreach ($commentArray as $value)
-							{
-								$comment .= $value . " ";
-							}
-						}
-					$date = strtotime("-1 year", strtotime($contribution->getDateExpired()));
-					$now = strtotime("now");
-					if ($date <= $now) {
-						$date = date("Y-m-d", $date);
-						//$date = $friend->getDateJoined();
-						if ($showAll == 1 || $benefit != "") {
-				?>
-				<tr class="donorRecord">
-					<td width="25"><?=$benefit;?></td>
-					<td width="59%"><b><?=$name;?></b><br/><?=$comment;?></td>
-					<td><?=$date;?></td>
-					<td align="right">$<?=$amount;?> USD</td>
-				</tr>
-				<?		}
-					}
-				}?>
-		</table>
-		<?=displayPager($start, $pageValue, $pageCount, $showAll);?>
-		<br/><br/>
-	</div>
-	<div id="rightcolumn">
-		<div class="sideitem">
-			<h6>Total Donations</h6>
-			<div style="text-align:center;font-size:24px;padding:5px 0px;"><?=$pageCount;?></div>
-		</div>
-		<div class="sideitem">
-		<h6>Legend</h6>
-			<p align="center">
-				<img width='25' src="images/stars.png" align="absbottom">Friend of Eclipse
-			</p>
-		</div>
-	</div>
-	<?
-	header('Content-Type: text/html; charset=utf-8;');
-	$html = ob_get_clean();
-//	$html = mb_convert_encoding($html, "HTML-ENTITIES", "UTF-8");
-	$Nav->addCustomNav("Donate to Eclipse", 		"index.php", 			"_self", 1);
-	$Nav->addCustomNav("Friends Login", 		"http://dev.eclipse.org/site_login", 			"_self", 1);
-	$Nav->addCustomNav("Donation FAQ", 		"faq.php", 			"_self", 1);
-	# Generate the web page
-	$App->generatePage("solstice", $Menu, $Nav, $pageAuthor, $pageKeywords, $pageTitle, $html);
-?>
+  # Generate the web page
+  $App->AddExtraHtmlHeader('<link rel="stylesheet" type="text/css" href="style.css" media="screen" />');
+  $App->generatePage("solstice", $Menu, NULL, $pageAuthor, $pageKeywords, $pageTitle, $html);
diff --git a/functions.php b/functions.php
index a85395b..88a1b0c 100644
--- a/functions.php
+++ b/functions.php
@@ -90,9 +90,10 @@
   return $return_html;
 }
 
-function displayPager($_start, $_pageValue, $_pageCount, $_showAll = NULL) {
-	$showAll = "";
-	if ($_showAll == 0) $showAll = "&showAll=0";
+function displayPager($_start, $_pageValue, $_pageCount, $_startParam='start', $_anchor='') {
+    // Build URL
+    $next = $_SERVER['PHP_SELF'] . '?' . $_startParam . '=' . ($_start + $_pageValue) . '' . $_anchor;
+    $previous = $_SERVER['PHP_SELF'] . '?' . $_startParam . '=' . ($_start - $_pageValue) . '' . $_anchor;
 	ob_start();
 	?>
 	<table class="pager">
@@ -100,13 +101,13 @@
 				<td style="text-align:left">
 			<?php
 				if ($_start >= $_pageValue) {
-					?><a href="<?=$_SERVER['PHP_SELF'];?>?start=<?=$_start-$_pageValue;?><?=$showAll;?>"><< Previous Page</a><?php
+                    ?><a href="<?=$previous;?>"><< Previous Page</a><?php
 				}
 			?>&nbsp;</td>
 				<td style="text-align:right">
 			<?php
 				if (($_start + $_pageValue) < $_pageCount) {
-					?><a href="<?=$_SERVER['PHP_SELF'];?>?start=<?=$_start+$_pageValue;?><?=$showAll;?>">Next Page >></a><?php
+                    ?><a href="<?=$next;?>">Next Page >></a><?php
 				}
 			?>
 				</td>
@@ -115,4 +116,92 @@
 	<?php
 	return ob_get_clean();
 }
-?>
+
+function displayTable($contributions) {
+    echo '<table class="table table-hover table-condensed" cellspacing=0>' . 
+           '<tr class="donorHeader">' .
+             '<td colspan="2" width="60%">Name and Message</td>' .
+             '<td width="20%">Date</td>' .
+             '<td width="20%" align="right">Amount</td>' .
+           '</tr>';
+    // Get total number of items so we can know whether to page or not.
+    $friend = new Friend();
+    $contribution = new Contribution();
+    $fcObject = new FriendsContributions();
+    foreach ($contributions as $contribution) {
+        $contrib = $contribution->getContributionObject();
+        $friend = $contribution->getFriendObject();
+        $anonymous = $friend->getIsAnonymous();
+        if ($anonymous != 1 && ($friend->getFirstName() != '' || $friend->getLastName() != ''))
+            $name = $friend->getFirstName() . " " . $friend->getLastName();
+        else $name = "Anonymous";
+        $benefit = $friend->getIsBenefit();
+        if ($benefit != 0) $benefit = " <img width='25' src=\"images/stars.png\">";
+        else $benefit = "";
+        $amount = $contrib->getAmount();
+        if (strpos($amount, ".") == 0) {
+            $amount = $amount . ".00";
+        }
+        $comment =  stripslashes(strip_tags($contrib->getMessage()));
+        if (strlen($comment) > 80) {
+            if (strpos($comment, ' ') == 0) {
+                $commentArray = str_split($comment, 80);
+                $comment = 0;
+                foreach ($commentArray as $value) {
+                    $comment .= $value . " ";
+                }
+            }
+        }
+        $date = strtotime("-1 year", strtotime($contrib->getDateExpired()));
+        $now = strtotime("now");
+        if ($date <= $now) {
+            $date = date("Y-m-d", $date);
+            echo '<tr class="donorRecord">' .
+	               '<td width="25">' . $benefit . '</td>' .
+	               '<td width="59%"><b>' . $name . '</b><br/>' . $comment . '</td>' .
+	               '<td>' . $date . '</td>' .
+	               '<td align="right">$' . $amount . ' USD</td>' .
+                 '</tr>';
+        }
+    }
+    echo '</table>';
+}
+
+function getGravatarURL($email, $size=80, $default='mm') {
+  return "https://secure.gravatar.com/avatar/" . md5(strtolower(trim($email))) . "?d=" . $default . "&s=" . $size;
+}
+
+function displayFriends($friendsContributions) {
+    require_once("/home/data/httpd/eclipse-php-classes/system/ldapconnection.class.php");
+    $ldap = new LDAPConnection();
+    $content = '<div class="row friend-images">';
+    foreach ($friendsContributions as $contributions) {
+        $f = $contributions->getFriendObject();
+        $name = ucfirst(strtolower($f->getFirstName())) . ' ' . ucfirst(strtolower($f->getLastName()));
+        $uid = $f->getLDAPUID();
+        $email = '';
+        if ($uid) {
+            $dn = $ldap->getDNFromUID($uid);
+            $email = $ldap->getLDAPAttribute($dn, 'mail');
+        }
+        $content .= '<div class="col-md-6 col-sm-8, col-xs-12">';
+        $content .= '  <div class="user-picture">';
+        $content .= '    <img class="col-xm-24" typeof="foaf:Image" src="' . getGravatarURL($email) . '" alt="' . $name . '" title="' . $name . '" />';
+        $content .= '    <span class="col-xs-24 donor-name">' . $name . '</span>';
+        $content .= '  </div>';
+        $content .= '</div>';
+    }
+    $content .= '</div>';
+    return $content;
+}
+
+function getFriendsCount($get_anonymous=TRUE, $get_expired=TRUE, $donation_minimum=35) {
+    $friends = array();
+    $App = new App();
+    $sql = "SELECT COUNT(*) FROM friends_contributions as FC
+            LEFT JOIN friends AS F ON FC.friend_id = F.friend_id WHERE FC.amount >= " . $App->sqlSanitize($donation_minimum);
+    if (!$get_anonymous) $sql .= " AND F.is_anonymous = 0";
+    if (!$get_expired) $sql .= " AND FC.date_expired > NOW()";
+    $result = $App->eclipse_sql($sql);
+    return mysql_fetch_row($result)[0];
+}
diff --git a/style.css b/style.css
index 1d264bb..1f9707f 100644
--- a/style.css
+++ b/style.css
@@ -6,6 +6,10 @@
     border:1px solid #888;
 }
 
+.pager {
+    width: 100%;
+}
+
 .donorList .donorHeader td {
     background-image:url(/images/bar.jpg);
     font-weight:bold;
@@ -201,3 +205,12 @@
     background-color: #FFBABA;
 
 }
+
+.friend-images > div {
+  height: 140px;
+}
+
+.friend-images .user-picture {
+  text-align: center;
+  padding: 20px;
+}