<?php

/**
 * Static functions that assist in generating HTML go in this class.
 *
 */
class HTMLHelper {
	
    /**
     * Generate the HTML for a rating <img/> tag. If you change the
     * filenames of the rating images in images/, change this function
     *
     * @param integer $avgRating The rating
     * @return string
     */
    public static function ratingImage($avgRating) {
	if ($avgRating) {
	    return '<img src="http://'.$_SERVER['HTTP_HOST'].'/images/stars' . $avgRating .
		'.gif" alt="' . $avgRating . 
		' stars" width="66" height="15" />';
	} else {
	    return 'n/a';
	}
    }

    /**
     * Generate an HTML list of linked tag words
     *
     * @param array $items Array of tag words
     * @param string $link sprintf()-style link format string
     * @param string optional $delimiter Link delimiter
     * @return string
     */
    public static function tagLinkList($items, $link, $delimiter = ', ') {
        $html = array();
        if (count($items) == 0) return "None";
        foreach($items as $item => $count) {
            $urlItem = urlencode($item);
            $htmlItem = htmlentities($item, ENT_QUOTES, 'UTF-8');
            $html[] = '<a class="tag" href="http://'.$_SERVER['HTTP_HOST'].'/' . sprintf($link,$urlItem) . '">' . $htmlItem . '</a>';
        }
        return implode($html, $delimiter);
    }
    
    /**
     * Simple English word pluralizer. No irregulars handled.
     *
     * @param string $word Word to possible pluralize
     * @param string $count Number to determine plural form
     * @return string
     */
    function pluralize($word, $count) {
        return ($count == 1) ? $word : $word.'s';
    }
    
    /**
     * Build the HTML for the index view of the user axis in _map.php
     *
     * @param array $users Array of users and review counts
     * @param array $userTagCounts Array of users and tag counts
     * @param string $link sprintf()-style format string for linking user names
     * @return string
     */
    function buildUserIndex($users, $userTagCounts, $link) {
        $html = '<p>';
        foreach ($users as $user => $reviewCount) {
            $userLink = sprintf($link,urlencode($user));
            $safeUser = htmlentities($user, ENT_QUOTES, 'UTF-8');
            $tagWord = self::pluralize('Tag',$userTagCounts[$user]);
            $reviewWord = self::pluralize('Review',$reviewCount);
            $html .=<<<_HTML_
<a class="title user" href="$userLink">$safeUser</a>
 <br />
$reviewCount $reviewWord | {$userTagCounts[$user]} $tagWord<br/><br/>
_HTML_;
        }       
        return $html.'</p>';
    }

}
