| +----------------------------------------------------------------------+ $Id:$ */ // {{{ class HTML_TagCloud /** * * HTML Tag Cloud * * build HTML with CSS base Tag Cloud. * * @category HTML * @author Shoma Suzuki * @version $Id$ * @package HTML_TagCloud * @see http://search.cpan.org/~lyokato/HTML-TagCloud-Extended-0.10/lib/HTML/TagCloud/Extended.pm */ class HTML_TagCloud { // {{{ class vars /** * @var array * @access private */ var $_elements = array(); /** * @var int * @access public */ var $basefontsize = 24; /** * @var int * @access public */ var $fontsizerange = 12; /** * @var int * @access private */ var $_max = 0; /** * @var int * @access private */ var $_min = 0; /** * @var int * @access private */ var $_max_epoc; /** * @var int * @access private */ var $_min_epoc; /** * @var string * @access public */ var $css_class = 'tagcloud'; /** * @var string * @access public * mm,cm,in,pt,pc,px,em */ var $size_suffix = 'px'; /** * @var int * @access public */ var $factor; /** * @var array * @access public */ var $epoc_level = array( array( 'earliest' => array( 'link' => 'cccccc', 'visited' => 'cccccc', 'hover' => 'cccccc', 'active' => 'cccccc', ), ), array( 'earlier' => array( 'link' => '9999cc', 'visited' => '9999cc', 'hover' => '9999cc', 'active' => '9999cc', ), ), array( 'later' => array( 'link' => '9999ff', 'visited' => '9999ff', 'hover' => '9999ff', 'active' => '9999ff', ), ), array( 'latest' => array( 'link' => '0000ff', 'visited' => '0000ff', 'hover' => '0000ff', 'active' => '0000ff', ), ), ); // }}} // {{{ HTML_Tagcloud($basefontsize) /** * * Class constructor * * @param int $basefontsize base font size of output tag (option) * @param int $fontsizerange font size range * @access public */ function HTML_TagCloud($basefontsize = 24, $fontsizerange = 12){ $this->basefontsize = $basefontsize; $this->minfontsize = ($this->basefontsize - $this->fontsizerange > 0) ? $this->basefontsize - $this->fontsizerange : 0; $this->maxfontsize = $this->basefontsize + $this->fontsizerange; } // }}} // {{{ addElement($tag, $url, $count, $timestamp = null) /** * * add a Tag Element to build Tag Cloud * * @return void * @param string $tag * @param string $url * @param int $count * @param int $timestamp unixtimestamp * @access public */ function addElement($name = '', $url ='', $count = 0, $timestamp = null){ $i = count($this->_elements); $this->_elements[$i]['name'] = $name; $this->_elements[$i]['url'] = $url; $this->_elements[$i]['count'] = $count; $this->_elements[$i]['timestamp'] = $timestamp == null ? time() : $timestamp; } // }}} // {{{ addElements($tags) /** * * add a Tag Element to build Tag Cloud * * @return void * @param array $tags Associative array to $this->_elements * @access public */ function addElements($tags){ $this->_elements = array_merge($this->_elements, $tags); } // }}} // {{{ clearElements() /** * * clear Tag Elements * * @access public */ function clearElements(){ $this->_elements = array(); } // }}} // {{{ buildAll($param = array()) /** * * build HTML and CSS at once. * * @return string HTML and CSS * @param array $param * @see _buidHTMLTags * @access public */ function buildAll($param = array()){ $html = "\n"; $html .= $this->buildHTML($param); return $html; } // }}} // {{{ html_and_css($param = array()) /** * * Alias to buildAll. Compatibilities for Perl Module. * * @return string HTML and CSS * @param string $hoge * @access public */ function html_and_css($param = array()){ return $this->buildAll($param); } // }}} // {{{ buildHTML($param = array()) /** * * build HTML part * * @return string HTML * @param array $hoge * @access public */ function buildHTML($param = array()){ $htmltags = $this->_buidHTMLTags($param); return $this->_wrapDiv($htmltags); } // }}} // {{{ buildCSS() /** * * build CSS part * * @return string base CSS * @access public */ function buildCSS(){ $css = ''; foreach($this->epoc_level as $item){ foreach($item as $epoc_name => $colors){ foreach($colors as $attr => $color){ $css .= ".{$this->css_class} .{$epoc_name} a:{$attr} {text-decoration: none; color: #{$color};}\n"; } } } return $css; } // }}} // {{{ _buidHTMLTags($param) /** * * calc Tag level and create whole HTML of each Tags * * @return string HTML * @param array $param limit of Tag Number * @access private */ function _buidHTMLTags($param){ $this->total = count($this->_elements); // no tags elements if($this->total == 0){ return array(); }elseif($this->total == 1){ $tag = $this->_elements[0]; return $this->_createHTMLTag($tag, 'latest', $this->basefontsize); } $limit = array_key_exists('limit', $param) ? $param['limit'] : 0; $this->_sortTags($limit); $this->_calcMumCount(); $this->_calcMumEpoc(); $range = $this->maxfontsize - $this->minfontsize; $this->factor = $this->_max == $this->_min ? 1 : $range / (sqrt($this->_max) - sqrt($this->_min)); $this->epoc_factor = $this->_max_epoc == $this->_min_epoc ? 1 : count($this->epoc_level) / (sqrt($this->_max_epoc) - sqrt($this->_min_epoc)); $rtn = array(); foreach($this->_elements as $tag){ $count_lv = $this->_getCountLevel($tag['count']); if(! isset($tag['timestamp']) || empty($tag['timestamp'])){ $epoc_lv = count($this->epoc_level) - 1; }else{ $epoc_lv = $this->_getEpocLevel($tag['timestamp']); } $color_type = $this->epoc_level[$epoc_lv]; $font_size = $this->minfontsize + $count_lv; $rtn[] = $this->_createHTMLTag($tag, key($color_type), $font_size); } return implode("", $rtn); } // }}} // {{{ _createHTMLTag($tagname, $epoc_level, $fontsize) /** * * create a Element of HTML part * * @return string a Element of Tag HTML * @param array $tag * @param string $type css class of time line param * @param int $fontsize * @access private */ function _createHTMLTag($tag, $type, $fontsize){ return sprintf("%s\n", $type, $fontsize, $this->size_suffix, $tag['url'], $tag['name']); } // }}} // {{{ _sortTags( /** * * sort tags by name * * @return array * @param int $limit limit element number of create TagCloud * @access private */ function _sortTags($limit = 0){ usort($this->_elements, array(get_class($this), "_cmpElementsName")); if($limit != 0){ $this->_elements = array_splice($this->_elements, 0, $limit); } } // }}} // {{{ _cmpElementsName($a, $b) /** * * using for usort() * * @return int * @access private */ function _cmpElementsName($a, $b){ if ($a['name'] == $b['name']) { return 0; } return ($a['name'] < $b['name']) ? -1 : 1; } // }}} // {{{ _calcMumCount() /** * * calc max and min tag count of use * * @access private */ function _calcMumCount(){ foreach($this->_elements as $item){ $array[] = $item['count']; } $this->_min = min($array); $this->_max = max($array); } // }}} // {{{ _calcMumEpoc() /** * * calc max and min timestamp * * @access private */ function _calcMumEpoc(){ foreach($this->_elements as $item){ $array[] = $item['timestamp']; } $this->_min_epoc = min($array); $this->_max_epoc = max($array); } // }}} // {{{ _getCountLevel($count) /** * * calc Tag Level of size * * @return int level * @param int $count * @access private */ function _getCountLevel($count = 0){ return (int)(sqrt($count) - sqrt($this->_min) ) * $this->factor; } // }}} // {{{ _getEpocLevel($timestamp) /** * * calc timeline level of Tag * * @return int level of timeline * @param int $timestamp * @access private */ function _getEpocLevel($timestamp = 0){ return (int) (sqrt($timestamp) - sqrt($this->_min_epoc)) * $this->epoc_factor; } // }}} // {{{ _wrapDiv($html) /** * * wrap div tag * * @return string * @param string $html * @access private */ function _wrapDiv($html){ return $html == "" ? "" : sprintf("
\n%s
\n", $this->css_class, $html); } // }}} } // }}} /* * vim: set expandtab tabstop=4 shiftwidth=4 * vim600: foldmethod=marker * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */ ?>