Version: 1.0

Type: Class

Category: HTML

License: GNU General Public License

Description: PHP5 class to take an array and create a TagCloud.



<?php
/**
 * creates a tag cloud from an array of terms
 *
 * Given a single dimensional array of terms, this class will output a piece of
 * HTML with each term wrapped in a span tag with an ID that you can define in
 * your CSS. It can be called either as an object or as a static class.
 *
 * Project page is :
 * http://www.calevans.com/view.php/page/tagcloud
 * 
 * @author Cal Evans <cal@calevans.com>
 * @version 1.0
 * @copyright (c) 2006 Evans Internet Construction Company
 * @license GPL 2.0
 * @access public
 *
 */
class TagCloud {

    /**
     * Initial array handed into the consturctor
     * @var array
     */
    public $input_array;
    /**
     * Final array built with weighted terms and sorted. This is what the
     * final output string is built from.
     * @var array
     */
    public $output_array;
    /**
     * The final output string.
     * @var string
     */
    public $output_cloud;
    /**
     * The CSS tag name prefix. i.e. tag_cloud_ would get you tag_cloud_1
     * @var string
     */
    protected $css_prefix;
    /**
     * URL to wrap around each phrase. a sing %s will be substutited for
     * the urlencoded phrase.
     * @var string
     */
    protected $link;
    /**
     * the number of buckets. Usually 10. Make sure you have the proper number
     * of CSS tags defined. (ie.e if you say 12 buckets make sure you have
     * tag_cloud_1 - tag_cloud_12 defined)
     * @var integer
     */
    protected $buckets;
    /**
     * Sort to apply to the array. valid values are alpha and weight.
     * @var string
     */
    protected $sort;
    /**
     * If >1 then only terms >= this number will be included in the final
     * output. The weighting will be adjusted before style sheets are applied.
     * This allows you to filter out low scoring terms.
     * @var integer
     */
    protected $min_count;
    /**
     * The highest weight
     * @var integer
     */    
    protected $ceiling;
    /**
     * the lowest weight
     * @var integer
     */
    protected $floor;
    /**
     * Intermediary array. This is in the form of word=>weight.
     * @var array
     */    
    protected $colsolidated_array;
    /**
     * character(s) to place between span tags.
     * @var array
     */    
    protected $spacer;    

    
	/**
	* constructor
	*/
    public function __construct($input_array=array(), $spacer='&nbsp;', $css_prefix='tagcloud_', $link='', $buckets=10, $sort='alpha', $min_count=1) {
        $this->consolidated_array = array();
        $this->input_array        = $input_array;
        $this->set_css_prefix($css_prefix);
        $this->set_link($link);
        $this->set_buckets($buckets);
        $this->set_sort($sort);
        $this->set_min_count($min_count);
        $this->set_spacer($spacer);
    } // public function __construct($input_array=array(), $css_prefix='tagcloud_',$link='', $buckets=10, $sort='alpha',$min_count=1)
    
    
	/**
	* set the css_prefix
	*
	* @public
	* @param string $value the new value
	*/
    public function set_css_prefix($value) {
        if($value) $this->css_prefix = $value;
    } // public function set_css_prefix($value)
    
	/**
	* get the css_prefix
	*
	* @public
	* @return string the value stored in $this->css_prefix
	*/
    public function get_css_prefix() {
        return $this->css_prefix;
    } // public function get_css_prefix()
    
    
	/**
	* set the link value
	*
	* @public
	* @param string $value the new value
	*/
    public function set_link($value) {
        if($value) $this->link = $value;
    } // public function set_link($value)
    
	/**
	* get the link value
	*
	* @public
	* @return string the value stored in $this->link
	*/
    public function get_link() {
        return $this->link;
    } // public function get_link()
    
    
	/**
	* set the buckets value
	*
	* @public
	* @param integer $value the new value
	*/
    public function set_buckets($value) {
        if($value) $this->buckets = intval($value);
    } // public function set_buckets($value)
    
	/**
	* get the buckets value
	*
	* @public
	* @return integer value stored in $this->buckets
	* 
	*/
    public function get_buckets() {
        return intval($this->buckets);
    } // public function get_buckets()
    
    
	/**
	* set the sort order
	*
	* @public
	* @param string $value the new value
	*/
    public function set_sort($value) {
        if($value) $this->sort = $value;
    } // public function set_sort($value)
    
	/**
	* get the sort order
	*
	* @public
	* @string the sort order to use.
	*/
    public function get_sort() {
        return $this->sort;
    } // public function get_sort()
        
    
	/**
	* set the min_coun
	*
	* @public
	* @param integer $value the new value
	*/
    public function set_min_count($value) {
        if($value) $this->min_count = intval($value);
    } // public function set_min_count($value)
    
	/**
	* set the min_count
	*
	* @public
	* @preturn integer the value stored in $this->min_count
	*/
    public function get_min_count() {
        return intval($this->min_count);
    } // public function get_min_count()
    
    
	/**
	* set the spacer
	*
	* @public
	* @param string $value the new value
	*/
    public function set_spacer($value) {
        if($value) $this->spacer = $value;
    } // public function set_spacer($value)
    
	/**
	* get the spacer
	*
	* @public
	* @return string the value stored in $this->spacer.
	*/
    public function get_spacer() {
        return $this->spacer;
    } // public function get_spacer()


	/**
	* builds the tag cloud.
	*
	* Use this method if you have instantiated the object. This is mainly calls
	* to the other methods. All the work is encapsulated in th emethods so that
	* any single piece can be overridden without haivng to overrise everything.
	* @public
	* @return bool true...always true
	*/
    public function get_cloud() {
        $ceiling = 1;
        $floor   = 99999999;
        $this->consolidated_array = $this->consolidate_array($this->input_array);
        $this->consolidated_array = $this->filter_min_count($this->consolidated_array,$this->min_count);
        $this->compute_limits($this->consolidated_array,$this->ceiling,$this->floor);
        $this->sort_array($this->consolidated_array,$this->sort);
        $this->output_cloud = $this->compute_weight($this->consolidated_array,$this->spacer,$this->css_prefix,$this->link,$this->buckets,$this->ceiling,$this->floor);
        return true;
    } // public function get_cloud()
    
    
	/**
	* build the consolidated array.
	*
	* This takes the input array and builds the consolidated array. 
	* @protected
	* @param array $input_array the initial array to work from.
	*/
    protected function consolidate_array($input_array) {
        $consolidated_array = array();
        for($lcvA=0;$lcvA<count($input_array);$lcvA++) {
            if (isset($consolidated_array[$input_array[$lcvA]])) {
                $consolidated_array[$input_array[$lcvA]]++;
            } else {
                $consolidated_array[$input_array[$lcvA]]=1;
            } // if (isset($consolidated_array[$inputArray[$lcvA]]))
        } // for($lcvA=0;$lcvA<count($input_array);$lcvA++)
        return $consolidated_array;
    } // protected function consolidate_array($input_array)
    
    
	/**
	* filters out low scores
	*
	* If min_count is >1 then this function will filter out the entries from a
	* consolidated array whose score is lower than $min_count. 
	* @protected
	* @param array $input_array the consolidated array to work from.
	* @param integer $min_count the minimum score to allow in the output
	* @return array the filtered array.
	*/
    protected function filter_min_count($input_array,$min_count) {
        if ($min_count>1) {
            $input_array;
            $output_array = array();
            
            foreach($input_array as $word=>$count) {
                if ($count>=$min_count) $output_array[$word]=$count;
            } // foreach($holding_array as $word=>$weight)
        } else {
            $output_array=$input_array;
        } //if ($min_count>1)        
        return $output_array;
    } // protected function filter_min_count()
    
    
	/**
	* computer the high and low score from the consolidated array.
	*
	* @protected
	* @param array $input_array the consolidated array to work from.
	* @param integer $ceiling the variable to store the ceiling in.
	* @param integer $floor the variable to store the floor in.
	* @return bool true...always true.
	*/
    protected function compute_limits($input_array,&$ceiling,&$floor) {
        foreach($input_array as $word=>$weight) {
            $ceiling = max($weight,$ceiling);
            $floor   = min($weight,$floor);

        } // foreach($work_array as $word=>$weight)
        return true;
    } // protected function compute_limits()
    
    
	/**
	* sort the array by the requested sort order.
	*
	* @protected
	* @param array $input_array the consolidated array to work from.
	* @return bool true...always true.
	*/
    protected function sort_array(&$input_array,$sort) {
        if ($sort=='weight') {
            arsort($input_array);
        } else if ($sort=='alpha') {
            ksort($input_array);
        } // if ($sort=='alpha')        
        return true;
    } // protected function sort_array()
    
    
	/**
	* compute the weights and build the output
	*
	* converts scores into weights and the build the output string
	* @protected
	* @param array $input_array the consolidated array to work from. Also, the array with the converted weights will be returned in this variable.
	* @param string $spacer the spacer to place between SPAN tags.
	* @param string $css_prefix The CSS_previx to add to the span tag ID.
	* @param string $link the value to put in the HREF of the anchor tags.
	* @param integer $buckets The number of buckets to use.
	* @param integer $ceiling the variable to store the ceiling in.
	* @param integer $floor the variable to store the floor in.
	* @return string the tag cloud
	*/
    protected function compute_weight(&$input_array, $spacer, $css_prefix, $link, $buckets, $ceiling, $floor) {
        $difference = ($ceiling-$floor);
        $increment = $difference/($buckets-1);
        $output_array = array();
        foreach($input_array as $word=>$weight) {
            $output_array[$word] = intval(($weight-$floor)/$increment)+1;
            if (!empty($link)) $output .= '<a href="'.sprintf($link,urlencode($word)).'">';
            $output .= '<span id="'.$css_prefix.$output_array[$word].'" >'.$word.'</span>';
            if (!empty($link)) $output .= '</a>';
            $output .= $spacer;
        } // foreach($work_array as $word=>$weight)
        if ($this) $this->output_array=$output_array;
        return $output;
    } // protected function compute_weight()
    
    
	/**
	* fetch the entire cloud.
	*
	* Use this method if you are calling the class statically.
	*
	* @public
	* @static
	* @param array $input_array the consolidated array to work from. Also, the array with the converted weights will be returned in this variable.
	* @param string $spacer the spacer to place between SPAN tags.
	* @param string $css_prefix The CSS_previx to add to the span tag ID.
	* @param string $link the value to put in the HREF of the anchor tags.
	* @param integer $buckets the number of weighting buckets to use.
	* @param stirng $sort the sort to apply to the array
	* @param integer $min_count the minimum score to allow into the array.
	* @return string the tag cloud.
	*/
    public static function fetch_cloud($input_array=array(), $spacer='&nbsp;', $css_prefix='tagcloud_',$link='', $buckets=10, $sort='alpha', $min_count=1) {
        /*
         * Start with boundries outside of reasonable norms.
         */
        $ceiling = 1;
        $floor  = 99999999;
        $consolidated_array = array();
        $consolidated_array = TagCloud::consolidate_array($input_array);
        $consolidated_array = TagCloud::filter_min_count($consolidated_array,$min_count);
        TagCloud::compute_limits($consolidated_array,$ceiling,$floor);
        TagCloud::sort_array($consolidated_array,$sort);
        $output = TagCloud::compute_weight($consolidated_array,$spacer,$css_prefix,$link,$buckets,$ceiling,$floor);
        return $output;        
    } // public static function fetch_cloud($input_array=array(), $spacer='&nbsp;', $css_prefix='tagcloud_',$link='', $buckets=10, $sort='alpha', $min_count=1) 
    
} // class TagCloud 
?>