Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First iteration of updates

  • Loading branch information...
commit 1d323be14e20bb842a0ce325a9b1cba3407f84ed 1 parent 80384ef
@lotsofcode authored
View
408 classes/wordcloud.class.php → classes/tagcloud.php
@@ -2,17 +2,17 @@
/************************************************************\
*
- * wordCloud Copyright 2011 Derek Harvey
- * www.lotsofcode.com
+ * tagcloud Copyright 2012 Derek Harvey
+ * lotsofcode.com
*
* This file is part of wordCloud.
*
- * wordCloud v2 is free software; you can redistribute it and/or modify
+ * tag cloud is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
- * wordCloud v2 is distributed in the hope that it will be useful,
+ * tag cloud is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
@@ -23,34 +23,48 @@
*
\************************************************************/
- class wordCloud {
- var $version = '2.1';
- var $wordsArray = array();
+ class tagcloud
+ {
+ /**
+ * Tag cloud version
+ */
+ public $version = '2.1';
/*
+ * Word array container
+ */
+ protected $_wordsArray = array();
+
+ /**
+ * List of words to remove from final output
+ */
+ protected $_removeWords = array();
+
+ /**
+ * Cached attributes
+ */
+ protected $_attributes = array();
+
+ /*
* Custom format ourput of words
*
- * @transformation
- * upper and lower for change of case
- *
- * @trim
- * bool, applies trimming to word
+ * transformation: upper and lower for change of case
+ * trim: bool, applies trimming to word
*/
-
- var $formatting = array(
+ protected $_formatting = array(
'transformation' => 'lower',
'trim' => true
);
/*
- * PHP 5 Constructor
+ * Constructor
*
* @param array $words
*
* @return void
*/
-
- function __construct($words = false) {
+ public function __construct($words = false)
+ {
// If we are trying to parse some works, in any format / type
if ($words !== false) {
// If we have a string
@@ -66,25 +80,13 @@ function __construct($words = false) {
}
/*
- * PHP 4 Constructor
- *
- * @param array $words
- *
- * @return void
- */
-
- function wordCloud($words = false) {
- return $this->__construct($words);
- }
-
- /*
* Convert a string into a cloud
*
* @param string $string
* @return void
*/
-
- function addString($string, $seperator = ' ') {
+ public function addString($string, $seperator = ' ')
+ {
$inputArray = explode($seperator, $string);
$wordArray = array();
foreach ($inputArray as $inputWord) {
@@ -100,10 +102,10 @@ function addString($string, $seperator = ' ') {
*
* @return string
*/
-
- function formatWord($string) {
- if($this->formatting['transformation']){
- switch($this->formatting['transformation']){
+ public function formatWord($string)
+ {
+ if ($this->_formatting['transformation']) {
+ switch ($this->_formatting['transformation']) {
case 'upper':
$string = strtoupper($string);
break;
@@ -111,39 +113,22 @@ function formatWord($string) {
$string = strtolower($string);
}
}
- if($this->formatting['trim']){
+ if ($this->_formatting['trim']) {
$string = trim($string);
}
return preg_replace('/[^\w ]/u', '', strip_tags($string));
}
-
- /*
- * Display user friendly message, for debugging mainly
- *
- * @param string $string
- * @param array $value
- *
- * @return bool
- */
-
- function notify($string, $value)
- {
- echo '<pre>';
- print_r($string);
- print_r($value);
- echo '</pre>';
- return false;
- }
+
/*
* Assign word to array
*
- * @param string $word
+ * @param array $word
*
- * @return string
+ * @return array
*/
-
- function addWord($wordAttributes = array()) {
+ public function addWord($wordAttributes = array())
+ {
if (is_string($wordAttributes)) {
$wordAttributes = array('word' => $wordAttributes);
}
@@ -152,30 +137,45 @@ function addWord($wordAttributes = array()) {
$wordAttributes = array_merge($wordAttributes, array('size' => 1));
}
if (!array_key_exists('word', $wordAttributes)) {
- return $this->notify('no word attribute', print_r($wordAttributes, true));
+ return false;
}
$word = $wordAttributes['word'];
- if (empty($this->wordsArray[$word])) {
- $this->wordsArray[$word] = array();
+ if (empty($this->_wordsArray[$word])) {
+ $this->_wordsArray[$word] = array();
}
- if (!empty($this->wordsArray[$word]['size']) && !empty($wordAttributes['size'])) {
- $wordAttributes['size'] = ($this->wordsArray[$word]['size'] + $wordAttributes['size']);
- } elseif (!empty($this->wordsArray[$word]['size'])) {
- $wordAttributes['size'] = $this->wordsArray[$word]['size'];
+ if (!empty($this->_wordsArray[$word]['size']) && !empty($wordAttributes['size'])) {
+ $wordAttributes['size'] = ($this->_wordsArray[$word]['size'] + $wordAttributes['size']);
+ } elseif (!empty($this->_wordsArray[$word]['size'])) {
+ $wordAttributes['size'] = $this->_wordsArray[$word]['size'];
}
- $this->wordsArray[$word] = $wordAttributes;
- return $this->wordsArray[$word];
+ $this->_wordsArray[$word] = $wordAttributes;
+ $this->addAttributes($wordAttributes);
+ return $this->_wordsArray[$word];
}
-
+
+ public function addAttributes($attributes)
+ {
+ $this->_attributes = array_unique(
+ array_merge(
+ $this->_attributes,
+ array_keys($attributes)
+ )
+ );
+ }
+ public function getAttributes()
+ {
+ return $this->_attributes;
+ }
+
/*
* Assign multiple words to array
*
- * @param string $word
+ * @param array $word
*
- * @return string
+ * @return void
*/
-
- function addWords($words = array()) {
+ public function addWords($words = array())
+ {
if (!is_array($words)) {
$words = func_get_args();
}
@@ -185,54 +185,18 @@ function addWords($words = array()) {
}
/*
- * Shuffle associated names in array
- *
- * @return array $this->wordsArray
- */
-
- function shuffleCloud() {
- $keys = array_keys($this->wordsArray);
- shuffle($keys);
- if (count($keys) && is_array($keys)) {
- $tmpArray = $this->wordsArray;
- $this->wordsArray = array();
- foreach ($keys as $key => $value)
- $this->wordsArray[$value] = $tmpArray[$value];
- }
- return $this->wordsArray;
- }
-
- /*
- * Get the class range using a percentage
+ * Sets a limit for the amount of clouds
*
- * @returns int $class
+ * @param string $limit
+ *
+ * @returns string $this->limitAmount
*/
-
- function getClassFromPercent($percent) {
- $percent = floor($percent);
- if ($percent >= 99)
- $class = 9;
- elseif ($percent >= 70)
- $class = 8;
- elseif ($percent >= 60)
- $class = 7;
- elseif ($percent >= 50)
- $class = 6;
- elseif ($percent >= 40)
- $class = 5;
- elseif ($percent >= 30)
- $class = 4;
- elseif ($percent >= 20)
- $class = 3;
- elseif ($percent >= 10)
- $class = 2;
- elseif ($percent >= 5)
- $class = 1;
- else
- $class = 0;
- return $class;
+ public function setLimit($limit)
+ {
+ $this->limitAmount = $limit;
+ return $this;
}
-
+
/*
* Sets a limit for the amount of clouds
*
@@ -240,63 +204,49 @@ function getClassFromPercent($percent) {
*
* @returns string $this->limitAmount
*/
-
- function setLimit($limit) {
- if (!empty($limit)) {
- $this->limitAmount = $limit;
- }
+ public function getLimit()
+ {
return $this->limitAmount;
}
-
+
/*
- * Gets the limited amount of clouds
+ * Remove a word from the array
*
- * @returns string $wordCloud
+ * @param string $word
+ *
+ * @returns voidgetAttributes
*/
-
- function limitCloud() {
- $i = 0;
- $wordsArray = array();
- foreach ($this->wordsArray as $key => $value) {
- if ($i < $this->limitAmount) {
- $wordsArray[$value['word']] = $value;
- }
- $i++;
- }
- $this->wordsArray = array();
- $this->wordsArray = $wordsArray;
- return $this->wordsArray;
+ public function setRemoveWord($word)
+ {
+ $this->_removeWords[] = $this->formatWord($word);
}
-
+
/*
- * Finds the maximum value of an array
+ * Remove multiple words from the array
*
- * @param string $word
+ * @param array $words
*
* @returns void
*/
-
- function removeWord($word) {
- $this->removeWords[] = $this->formatWord($word);
+ public function setRemoveWords($words)
+ {
+ foreach ($words as $word) {
+ $this->removeWord($word);
+ }
}
-
+
/*
- * Removes tags from the whole array
+ * Remove multiple words from the array
*
- * @returns array $this->wordsArray
+ * @param array $words
+ *
+ * @returns void
*/
-
- function removeWords() {
- foreach ($this->wordsArray as $key => $value) {
- if (!in_array($value['word'], $this->removeWords)) {
- $wordsArray[$value['word']] = $value;
- }
- }
- $this->wordsArray = array();
- $this->wordsArray = $wordsArray;
- return $this->wordsArray;
+ public function getRemoveWords()
+ {
+ return $this->_removeWords;
}
-
+
/*
* Assign the order field and order direction of the cloud
*
@@ -307,12 +257,32 @@ function removeWords() {
*
* @returns void
*/
-
- function orderBy($field, $direction = 'ASC') {
- return $this->orderBy = array('field' => $field, 'direction' => $direction);
+ public function setOrder($field, $direction = 'ASC')
+ {
+ return $this->orderBy = array(
+ 'field' => $field,
+ 'direction' => $direction
+ );
}
/*
+ * Removes tags from the whole array
+ *
+ * @returns array $this->_wordsArray
+ */
+ protected function _remove()
+ {
+ foreach ($this->_wordsArray as $key => $value) {
+ if (!in_array($value['word'], $this->getRemoveWords())) {
+ $_wordsArray[$value['word']] = $value;
+ }
+ }
+ $this->_wordsArray = array();
+ $this->_wordsArray = $_wordsArray;
+ return $this->_wordsArray;
+ }
+
+ /*
* Orders the cloud by a specific field
*
* @param array $unsortedArray
@@ -321,12 +291,16 @@ function orderBy($field, $direction = 'ASC') {
*
* @returns array $unsortedArray
*/
-
- function orderCloud($unsortedArray, $sortField, $sortWay = 'SORT_ASC') {
+ protected function _order($unsortedArray, $sortField, $sortWay = 'SORT_ASC')
+ {
$sortedArray = array();
foreach ($unsortedArray as $uniqid => $row) {
- foreach ($row as $key => $value) {
- $sortedArray[$key][$uniqid] = strtolower($value);
+ foreach ($this->getAttributes() as $attr) {
+ if (isset($row[$attr])) {
+ $sortedArray[$attr][$uniqid] = $unsortedArray[$uniqid][$attr];
+ } else {
+ $sortedArray[$attr][$uniqid] = null;
+ }
}
}
if ($sortWay) {
@@ -336,16 +310,36 @@ function orderCloud($unsortedArray, $sortField, $sortWay = 'SORT_ASC') {
}
/*
+ * Gets the limited amount of clouds
+ *
+ * @returns array $wordsArray
+ */
+ protected function _limit()
+ {
+ $i = 0;
+ $_wordsArray = array();
+ foreach ($this->_wordsArray as $key => $value) {
+ if ($i < $this->getLimit()) {
+ $_wordsArray[$value['word']] = $value;
+ }
+ $i++;
+ }
+ $this->_wordsArray = array();
+ $this->_wordsArray = $_wordsArray;
+ return $this->_wordsArray;
+ }
+
+ /*
* Finds the maximum value of an array
*
* @returns string $max
*/
-
- function getMax() {
+ protected function _getMax()
+ {
$max = 0;
- if (!empty($this->wordsArray)) {
+ if (!empty($this->_wordsArray)) {
$p_size = 0;
- foreach ($this->wordsArray as $cKey => $cVal) {
+ foreach ($this->_wordsArray as $cKey => $cVal) {
$c_size = $cVal['size'];
if ($c_size > $p_size) {
$max = $c_size;
@@ -354,32 +348,88 @@ function getMax() {
}
}
return $max;
+ }
+
+ /*
+ * Shuffle associated names in array
+ *
+ * @return array $this->_wordsArray The shuffled array
+ */
+ private function _shuffle()
+ {
+ $keys = array_keys($this->_wordsArray);
+ shuffle($keys);
+ if (count($keys) && is_array($keys)) {
+ $tmpArray = $this->_wordsArray;
+ $this->_wordsArray = array();
+ foreach ($keys as $key => $value)
+ $this->_wordsArray[$value] = $tmpArray[$value];
+ }
+ return $this->_wordsArray;
}
/*
+ * Get the class range using a percentage
+ *
+ * @returns int $class The respective class
+ * name based on the percentage value
+ */
+ private function _getClassFromPercent($percent)
+ {
+ $percent = floor($percent);
+ if ($percent >= 99)
+ $class = 9;
+ elseif ($percent >= 70)
+ $class = 8;
+ elseif ($percent >= 60)
+ $class = 7;
+ elseif ($percent >= 50)
+ $class = 6;
+ elseif ($percent >= 40)
+ $class = 5;
+ elseif ($percent >= 30)
+ $class = 4;
+ elseif ($percent >= 20)
+ $class = 3;
+ elseif ($percent >= 10)
+ $class = 2;
+ elseif ($percent >= 5)
+ $class = 1;
+ else
+ $class = 0;
+ return $class;
+ }
+
+ /*
* Create the HTML code for each word and apply font size.
*
* @returns string/array $return
*/
function showCloud($returnType = 'html') {
- if (!empty($this->removeWords)) {
- $this->removeWords();
- }
+
+
+ //echo '<pre>'; print_r($this->getAttributes());
+
+ $this->_remove();
if (empty($this->orderBy)) {
- $this->shuffleCloud();
+ $this->_shuffle();
} else {
$orderDirection = strtolower($this->orderBy['direction']) == 'desc' ? 'SORT_DESC' : 'SORT_ASC';
- $this->wordsArray = $this->orderCloud($this->wordsArray, $this->orderBy['field'], $orderDirection);
+ $this->_wordsArray = $this->_order(
+ $this->_wordsArray,
+ $this->orderBy['field'],
+ $orderDirection
+ );
}
if (!empty($this->limitAmount)) {
- $this->limitCloud();
+ $this->_limit();
}
- $this->max = $this->getMax();
- if (is_array($this->wordsArray)) {
+ $max = $this->_getMax();
+ if (is_array($this->_wordsArray)) {
$return = ($returnType == 'html' ? '' : ($returnType == 'array' ? array() : ''));
- foreach ($this->wordsArray as $word => $arrayInfo) {
- $sizeRange = $this->getClassFromPercent(($arrayInfo['size'] / $this->max) * 100);
+ foreach ($this->_wordsArray as $word => $arrayInfo) {
+ $sizeRange = $this->_getClassFromPercent(($arrayInfo['size'] / $max) * 100);
$arrayInfo['range'] = $sizeRange;
if ($returnType == 'array') {
$return [$word] = $arrayInfo;
View
0  css/wordcloud.css → css/tagcloud.css
File renamed without changes
View
37 playing.php
@@ -0,0 +1,37 @@
+<?
+ ini_set('display_errors','On');
+ error_reporting(E_ALL);
+ include 'classes/tagcloud.php';
+?>
+
+<link rel="stylesheet" type="text/css" href="./css/tagcloud.css" />
+
+<!-- <div style="xwidth: 200px;,'DESC'"> -->
+
+ <?
+ $cloud = new tagcloud();
+
+ /* adding a string */
+ $cloud->addString("This is a tag-cloud script, written by Del Harvey. I wrote this tag-cloud class because I just love writing code.");
+ /* adding a word */
+ $cloud->addWord("programming");
+ $cloud->addWord("tag-cloud");
+ /* adding words */
+ $cloud->addWords(array('tag-cloud','php','github',''));
+ /* removing a word */
+ $cloud->setRemoveWord('github');
+ /* removing a word */
+ $cloud->setRemoveWord('github');
+ /* more complex adding */
+ $cloud->addWord(array('word' => 'php', 'url' => 'http://www.php.net', 'colour' => 1));
+ $cloud->addWord(array('word' => 'ajax', 'url' => 'http://www.php.net', 'colour' => 2));
+ $cloud->addWord(array('word' => 'css', 'url' => 'http://www.php.net', 'colour' => 3));
+ /* limiting the output */
+// $cloud->setLimit(10);
+ /* set the order */
+ // $cloud->setOrder('colour','DESC');
+
+ echo $cloud->showCloud();
+ ?>
+
+</div>
Please sign in to comment.
Something went wrong with that request. Please try again.