PHP Classes
elePHPant
Icontem

File: class.Clouds.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of zyablik  >  Tag Clouds  >  class.Clouds.php  >  Download  
File: class.Clouds.php
Role: Class source
Content type: text/plain
Description: class.Clouds.php
Class: Tag Clouds
Manage tag clouds from tags stored in files
Author: By
Last change: new comments
Date: 7 years ago
Size: 1,983 bytes
 

Contents

Class file image Download
<?
class Clouds {
   
/**
     * constructor
     */
   
function Clouds($filename = 'clouds.dat')
    {
        if(!
file_exists($filename))
        {
           
$f = fopen($filename,"w");
           
fwrite($f,serialize(array()));
           
fclose($f);
        }
    }
   
   
/**
     * prepare words
     */
   
   
function prepareWord($entry)
    {
       
$entry = preg_replace("/\d+/","",$entry);
       
$entry = preg_replace("/\W+/","-",$entry);
       
$entry = preg_replace("/(^\W)|(\W$)/","",$entry);
       
$entry = preg_replace("/[\-]+/","-",$entry);
        return (
$entry);
    }
   
   
/**
     * write
     */
   
function write($clouds,$filename = 'clouds.dat')
    {
       
$f = fopen($filename,"w");
       
fwrite($f,serialize($clouds));
       
fclose($f);
    }
   
   
/**
     * read
     */
   
function read($filename = 'clouds.dat')
    {
        if(!
file_exists($filename))
       
$clouds = new Clouds;
       
$file = file($filename);
       
$clouds = unserialize($file[0]);
        return
$clouds;
    }
   
   
/**
     * update
     */
   
function update($word)
    {
       
$clouds = Clouds::read();
       
$word = Clouds::prepareWord($word);
        if(
array_key_exists($word,$clouds))
           
$clouds[$word]++;
        else
           
$clouds[$word] = 1;
       
Clouds::write($clouds);
    }
   
   
/**
     * render clouds
     */
   
function render($format,$min = 9,$max = 28)
    {
       
$clouds = Clouds::read();
        if(
sizeOf($clouds) == 0)
        {
           
$result = 'No Data';
           
$clouds = new Clouds;
        } else {
           
$minimum_count = min(array_values($clouds));
           
$maximum_count = max(array_values($clouds));
           
$spread = $maximum_count - $minimum_count;
           
$result = '';
            foreach(
$clouds AS $cloud=>$cnt)
            {
               
$size = $min + ($cnt - $minimum_count) * ($max - $min) / $spread;
               
$tag = @str_replace('###WORD###', $cloud, $format);
               
$tag = @str_replace('###SIZE###', floor($size), $tag);
               
$result .= $tag;
            }
        }
        return
$result;
    }
}

// usage of class
// 1. add words. Clouds::update("php");
// 2. show tag cloud
//echo Clouds::render('<a href="?###WORD###" style="font-size:###SIZE###px">###WORD###</a> ');
?>