<?php

/**
 * Tagcloud Class
 * 
 * @package Vframe
 * @subpackage Vframe Utils
 * @version 1.0
 * @license GNU
 * 
 */

class Vframe_Tagcloud extends Vframe_Attribute
{
  function 
add($sTag$iTag)
  {
    
$this->__set($sTag$iTag);
  }
  
  
/**
   * Render a Tagcloud in extended or simple mode
   * 
   * Render a tag cloud with assigned weights (levels) between
   * $iWeightMin and $iWeightMax with precision $iRoundPrecision.
   * 
   * @param int $iWeightMin minimal level (assigned to min %)
   * @param int $iWeightMax maximal level (assigned to 100%)
   * @param int $iRoundPrecision precision in rounding levels
   * @param bool $bExtended extended or simple mode
   * @return array
   * @access public
   * 
   */
  
function Render($iWeightMin 1$iWeightMax 10$iRoundPrecision 3$bExtended false)
  {
    if(!
count($this->_aAttributes))
      throw new 
Vframe_Exception('You have to add at least one tag to render tag cloud.');
    
    
// get minimum and maximum counts of tags
    
$iWeightMinRelative min($this->_aAttributes);
    
$iWeightMaxRelative max($this->_aAttributes);
    
    if(!
$iWeightMaxRelative// cannot divide by 0
        
$iWeightMaxRelative 1;
    
    
// count minimal relative weight, if it does not exists, assign maximum weight.
    
$iWeightRelative = (($iWeightRelative abs($iWeightMin $iWeightMax))) ? $iWeightRelative $iWeightMax;
    
    
$aRender = array();
    
    foreach(
$this->_aAttributes as $sTag => $iTag)
    {
      
// count tag weight in percent related to maximum count
      
$iTagWeight ceil(($iTag 100) / $iWeightMaxRelative);
      
// count tag level in float related to $iWeightRelative with $iRoundPrecision.
      
$iTagLevels = (float)round($iWeightMin + ($iWeightRelative * ($iTagWeight 100)), (int)$iRoundPrecision);
      
      if(
$bExtended)
        
$aRender[$sTag] = array
        (
          
'level' => $iTagLevels,
          
'count' => $iTag,
          
'count_percentage' => $iTagWeight,
        );
      else
        
$aRender[$sTag] = $iTagLevels;
    }
    
    return 
$aRender;
  }
}

?>