1
0
mirror of https://github.com/e107inc/e107.git synced 2025-08-01 20:30:39 +02:00

Tagcloud class upgraded to v4.0.1

This commit is contained in:
Cameron
2017-03-03 12:13:14 -08:00
parent bfaf50ab25
commit be765d214d

View File

@@ -6,54 +6,72 @@ class TagCloud
{
/**
* Tag cloud version
*
* @var string
*/
public $version = '4.0.0';
public $version = '4.0.1';
/*
/**
* Tag array container
*
* @var array
*/
protected $_tagsArray = array();
protected $tagsArray = array();
/**
* List of tags to remove from final output
*
* @var array
*/
protected $_removeTags = array();
protected $removeTags = array();
/**
* Cached attributes for order comparison
*
* @var array
*/
protected $_attributes = array();
protected $attributes = array();
/*
/**
* Amount to limit cloud by
*
* @var null
*/
protected $_limit = null;
protected $limit = null;
/*
/**
* Minimum length of string to filtered in string
*
* @var null
*/
protected $_minLength = null;
protected $minLength = null;
/*
/**
* Custom format output of tags
*
* transformation: upper and lower for change of case
* transliterate: true\false
* trim: bool, applies trimming to tag
*
* @var array
*/
protected $_formatting = array(
protected $options = array(
'transformation' => 'lower',
'transliterate' => true,
'trim' => true
);
/**
* Custom function to create the tag-output
*
* @var null
*/
protected $_htmlizeTagFunction = null;
protected $htmlizeTagFunction = null;
/**
* @var array Conversion map
*/
protected $_transliterationTable = array(
protected $transliterationTable = array(
'á' => 'a', 'Á' => 'A', 'à' => 'a', 'À' => 'A', 'ă' => 'a', 'Ă' => 'A', 'â' => 'a', 'Â' => 'A',
'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A',
'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C',
@@ -90,12 +108,13 @@ class TagCloud
'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja'
);
/*
* Constructor
/**
* Takes the tags and calls the correct
* setter based on the type of input
*
* @param array $tags
* @constructor
*
* @return void
* @param mixed $tags String or Collection of tags
*/
public function __construct($tags = false)
{
@@ -103,62 +122,96 @@ class TagCloud
if (is_string($tags)) {
$this->addString($tags);
} else if (count($tags)) {
foreach ($tags as $key => $value) {
$this->addTag($value);
foreach ($tags as $tag) {
$this->addTag($tag);
}
}
}
}
/*
/**
* Convert a string into a array
*
* @param string $string The string to use
* @param string $seperator The seperator to extract the tags
* @param string $separator The separator to extract the tags
*
* @return void
* @return $this
*/
public function addString($string, $seperator = ' ')
public function addString($string, $separator = ' ')
{
$inputArray = explode($seperator, $string);
$inputArray = explode($separator, $string);
$tagArray = array();
foreach ($inputArray as $inputTag) {
$tagArray[] = $this->formatTag($inputTag);
}
$this->addTags($tagArray);
return $this;
}
/*
/**
* Set option value
*
* @param string $option Option property name
* @param string $value New property value
*
* @return $this
*/
public function setOption($option, $value)
{
$this->options[$option] = $value;
return $this;
}
/**
* Get option by name otherwise return all options
*
* @param string $option Option property name
*
* @return array
*/
public function getOption($option = null)
{
if ($option !== null) {
return $this->options[$option];
}
return $this->options;
}
/**
* Parse tag into safe format
*
* @param string $string
* @param string $string Tag to be formatted
*
* @return string
* @return mixed
*/
public function formatTag($string)
{
$string = $this->_convertCharacters($string);
if ($this->_formatting['transformation']) {
switch ($this->_formatting['transformation']) {
if ($this->options['transliterate']) {
$string = $this->transliterate($string);
}
if ($this->options['transformation']) {
switch ($this->options['transformation']) {
case 'upper':
$string = strtoupper($string);
$string = $this->options['transliterate'] ? strtoupper($string) : mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
break;
default:
$string = strtolower($string);
$string = $this->options['transliterate'] ? strtolower($string) : mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
}
}
if ($this->_formatting['trim']) {
if ($this->options['trim']) {
$string = trim($string);
}
return preg_replace('/[^\w ]/u', '', strip_tags($string));
}
/*
/**
* Assign tag to array
*
* @param array $tagAttributes Tags or tag attributes array
*
* @return array $this->tagsArray
* @return bool
*/
public function addTag($tagAttributes = array())
{
@@ -173,50 +226,53 @@ class TagCloud
return false;
}
$tag = $tagAttributes['tag'];
if (empty($this->_tagsArray[$tag])) {
$this->_tagsArray[$tag] = array();
if (empty($this->tagsArray[$tag])) {
$this->tagsArray[$tag] = array();
}
if (!empty($this->_tagsArray[$tag]['size']) && !empty($tagAttributes['size'])) {
$tagAttributes['size'] = ($this->_tagsArray[$tag]['size'] + $tagAttributes['size']);
} elseif (!empty($this->_tagsArray[$tag]['size'])) {
$tagAttributes['size'] = $this->_tagsArray[$tag]['size'];
if (!empty($this->tagsArray[$tag]['size']) && !empty($tagAttributes['size'])) {
$tagAttributes['size'] = ($this->tagsArray[$tag]['size'] + $tagAttributes['size']);
} elseif (!empty($this->tagsArray[$tag]['size'])) {
$tagAttributes['size'] = $this->tagsArray[$tag]['size'];
}
$this->_tagsArray[$tag] = $tagAttributes;
$this->tagsArray[$tag] = $tagAttributes;
$this->addAttributes($tagAttributes);
return $this->_tagsArray[$tag];
return $this->tagsArray[$tag];
}
/*
/**
* Add all attributes to cached array
*
* @return void
* @param $attributes
*
* @return $this
*/
public function addAttributes($attributes)
{
$this->_attributes = array_unique(
$this->attributes = array_unique(
array_merge(
$this->_attributes,
$this->attributes,
array_keys($attributes)
)
);
return $this;
}
/*
/**
* Get attributes from cache
*
* @return array $this->_attibutes
* @return array Collection of Attributes
*/
public function getAttributes()
{
return $this->_attributes;
return $this->attributes;
}
/*
/**
* Assign multiple tags to array
*
* @param array $tags
* @param array $tags A collection of multiple tabs
*
* @return void
* @return $this
*/
public function addTags($tags = array())
{
@@ -226,79 +282,75 @@ class TagCloud
foreach ($tags as $tagAttributes) {
$this->addTag($tagAttributes);
}
return $this;
}
/*
* Sets a minimum string length for the
* tags to display
/**
* Sets a minimum string length for the tags to display
*
* @param int $minLength
* @param int $minLength The minimum string length of a tag
*
* @returns obj $this
* @return $this
*/
public function setMinLength($minLength)
{
$this->_minLength = $minLength;
$this->minLength = $minLength;
return $this;
}
/*
/**
* Gets the minimum length value
*
* @returns void
* @return int
*/
public function getMinLength()
{
return $this->_minLength;
return $this->minLength;
}
/*
/**
* Sets a limit for the amount of clouds
*
* @param int $limit
* @param int $limit The maximum number to display
*
* @returns obj $this
* @return $this
*/
public function setLimit($limit)
{
$this->_limit = $limit;
$this->limit = $limit;
return $this;
}
/*
* Get the limit for the amount tags
* to display
/**
* Get the limit for the amount tags to display
*
* @param int $limit
*
* @returns int $this->_limit
* @return int The maximum number
*/
public function getLimit()
{
return $this->_limit;
return $this->limit;
}
/*
* Remove a tag from the array
/**
* Assign a tag to be removed from the array
*
* @param string $tag
* @param string $tag The tag value
*
* @returns obj $this
* @return $this
*/
public function setRemoveTag($tag)
{
$this->_removeTags[] = $this->formatTag($tag);
$this->removeTags[] = $this->formatTag($tag);
return $this;
}
/*
/**
* Remove multiple tags from the array
*
* @param array $tags
* @param $tags A collection of removable tags
*
* @returns obj $this
* @return $this
*/
public function setRemoveTags($tags)
{
@@ -308,72 +360,76 @@ class TagCloud
return $this;
}
/*
/**
* Get the list of remove tags
*
* @returns array $this->_removeTags
* @return array A collection of tags to remove
*/
public function getRemoveTags()
{
return $this->_removeTags;
return $this->removeTags;
}
/*
/**
* Assign the order field and order direction of the array
*
* Order by tag or size / defaults to random
*
* @param array $field
* @param string $direction
* @param string $field The name of the field to sort by
* @param string $direction The sort direction ASC|DESC
*
* @returns $this->orderBy
* @return $this
*/
public function setOrder($field, $direction = 'ASC')
{
return $this->orderBy = array(
$this->orderBy = array(
'field' => $field,
'direction' => $direction
);
return $this;
}
/*
* Inject a custom function for generatinng the rendered HTML
/**
* Inject a custom function/closure for generating the rendered HTML
*
* @param function $htmlizer
* @param callable $htmlizeTagFunction The function/closure
*
* @return $this->_htmlizeTagFunction
* @return mixed
*/
public function setHtmlizeTagFunction($htmlizer)
public function setHtmlizeTagFunction($htmlizeTagFunction)
{
return $this->_htmlizeTagFunction = $htmlizer;
$this->htmlizeTagFunction = $htmlizeTagFunction;
return $this;
}
/*
/**
* Generate the output for each tag.
*
* @returns string/array $return
* @param string $returnType The type of data to return [html|array]
*
* @return array|null|string
*/
public function render($returnType = 'html')
{
$this->_remove();
$this->_minLength();
$this->remove();
$this->minLength();
if (empty($this->orderBy)) {
$this->_shuffle();
$this->shuffle();
} else {
$orderDirection = strtolower($this->orderBy['direction']) == 'desc' ? 'SORT_DESC' : 'SORT_ASC';
$this->_tagsArray = $this->_order(
$this->_tagsArray,
$this->tagsArray = $this->order(
$this->tagsArray,
$this->orderBy['field'],
$orderDirection
);
}
$this->_limit();
$max = $this->_getMax();
if (count($this->_tagsArray)) {
$this->limit();
$max = $this->getMax();
if (count($this->tagsArray)) {
$return = ($returnType == 'html' ? '' : ($returnType == 'array' ? array() : ''));
foreach ($this->_tagsArray as $tag => $arrayInfo) {
$sizeRange = $this->_getClassFromPercent(($arrayInfo['size'] / $max) * 100);
foreach ($this->tagsArray as $tag => $arrayInfo) {
$sizeRange = $this->getClassFromPercent(($arrayInfo['size'] / $max) * 100);
$arrayInfo['range'] = $sizeRange;
if ($returnType == 'array') {
$return [$tag] = $arrayInfo;
@@ -394,14 +450,14 @@ class TagCloud
*
* This will most likely only work in PHP >= 5.3
*
* @param array $arrayInfo
* @param string $sizeRange
* @param array $arrayInfo The data to pass into the closure
* @param string $sizeRange The size to pass into the closure
*
* @return string
*/
public function htmlizeTag($arrayInfo, $sizeRange)
{
$htmlizeTagFunction = $this->_htmlizeTagFunction;
$htmlizeTagFunction = $this->htmlizeTagFunction;
if (isset($htmlizeTagFunction) &&
is_callable($htmlizeTagFunction)
) {
@@ -414,34 +470,34 @@ class TagCloud
}
}
/*
/**
* Removes tags from the whole array
*
* @returns array $this->_tagsArray
* @return array The tag array excluding the removed tags
*/
protected function _remove()
protected function remove()
{
$_tagsArray = array();
foreach ($this->_tagsArray as $key => $value) {
foreach ($this->tagsArray as $key => $value) {
if (!in_array($value['tag'], $this->getRemoveTags())) {
$_tagsArray[$value['tag']] = $value;
}
}
$this->_tagsArray = array();
$this->_tagsArray = $_tagsArray;
return $this->_tagsArray;
$this->tagsArray = array();
$this->tagsArray = $_tagsArray;
return $this->tagsArray;
}
/*
/**
* Orders the cloud by a specific field
*
* @param array $unsortedArray
* @param string $sortField
* @param string $sortWay
* @param array $unsortedArray Collection of unsorted data
* @param string $sortField The field that should be sorted
* @param string $sortWay The direction to sort the data [SORT_ASC|SORT_DESC]
*
* @returns array $unsortedArray
* @return mixed
*/
protected function _order($unsortedArray, $sortField, $sortWay = 'SORT_ASC')
protected function order($unsortedArray, $sortField, $sortWay = 'SORT_ASC')
{
$sortedArray = array();
foreach ($unsortedArray as $uniqid => $row) {
@@ -459,65 +515,65 @@ class TagCloud
return $unsortedArray;
}
/*
* Parses the array and retuns
* limited amount of items
/**
* Parses the array and returns limited amount of items
*
* @returns array $this->_tagsArray
* @return array The collection limited to the amount defined
*/
protected function _limit()
protected function limit()
{
$limit = $this->getLimit();
if ($limit !== null) {
$i = 0;
$_tagsArray = array();
foreach ($this->_tagsArray as $key => $value) {
foreach ($this->tagsArray as $key => $value) {
if ($i < $limit) {
$_tagsArray[$value['tag']] = $value;
}
$i++;
}
$this->_tagsArray = array();
$this->_tagsArray = $_tagsArray;
$this->tagsArray = array();
$this->tagsArray = $_tagsArray;
}
return $this->_tagsArray;
return $this->tagsArray;
}
/*
* Reduces the array by removing strings
* with a length shorter than the minLength
/**
* Reduces the array by removing strings with a
* length shorter than the minLength
*
* @returns array $this->_tagsArray
* @return array The collection of items within
* the string length boundaries
*/
protected function _minLength()
protected function minLength()
{
$limit = $this->getMinLength();
if ($limit !== null) {
$i = 0;
$_tagsArray = array();
foreach ($this->_tagsArray as $key => $value) {
foreach ($this->tagsArray as $key => $value) {
if (strlen($value['tag']) >= $limit) {
$_tagsArray[$value['tag']] = $value;
}
$i++;
}
$this->_tagsArray = array();
$this->_tagsArray = $_tagsArray;
$this->tagsArray = array();
$this->tagsArray = $_tagsArray;
}
return $this->_tagsArray;
return $this->tagsArray;
}
/*
/**
* Finds the maximum 'size' value of an array
*
* @returns string $max
* @return int The maximum size value in the entire collection
*/
protected function _getMax()
protected function getMax()
{
$max = 0;
if (!empty($this->_tagsArray)) {
if (!empty($this->tagsArray)) {
$p_size = 0;
foreach ($this->_tagsArray as $cKey => $cVal) {
foreach ($this->tagsArray as $cKey => $cVal) {
$c_size = $cVal['size'];
if ($c_size > $p_size) {
$max = $c_size;
@@ -528,31 +584,32 @@ class TagCloud
return $max;
}
/*
/**
* Shuffle associated names in array
*
* @return array $this->_tagsArray The shuffled array
* @return array The shuffled collection
*/
protected function _shuffle()
protected function shuffle()
{
$keys = array_keys($this->_tagsArray);
$keys = array_keys($this->tagsArray);
shuffle($keys);
if (count($keys) && is_array($keys)) {
$tmpArray = $this->_tagsArray;
$this->_tagsArray = array();
$tmpArray = $this->tagsArray;
$this->tagsArray = array();
foreach ($keys as $key => $value)
$this->_tagsArray[$value] = $tmpArray[$value];
$this->tagsArray[$value] = $tmpArray[$value];
}
return $this->_tagsArray;
return $this->tagsArray;
}
/*
/**
* Get the class range using a percentage
*
* @returns int $class The respective class
* name based on the percentage value
* @param $percent
*
* @return float|int The respective class name based on the percentage value
*/
protected function _getClassFromPercent($percent)
protected function getClassFromPercent($percent)
{
$class = floor(($percent / 10));
@@ -572,18 +629,26 @@ class TagCloud
/**
* Calculate the class given to a tag from the
* weight percentage of the given tag.
*
* @param int $percent The percentage value
*
* @return float|int
*/
public function calculateClassFromPercent($percent)
{
return $this->_getClassFromPercent($percent);
return $this->getClassFromPercent($percent);
}
/**
* Convert accented chars into basic latin chars
* @see http://stackoverflow.com/questions/6837148/change-foreign-characters-to-normal-equivalent
*
* Taken from http://stackoverflow.com/questions/6837148/change-foreign-characters-to-normal-equivalent
* @param string $string Non transliterated string
*
* @return mixed Transliterated string
*/
function _convertCharacters($string) {
return str_replace(array_keys($this->_transliterationTable), array_values($this->_transliterationTable), $string);
protected function transliterate($string)
{
return str_replace(array_keys($this->transliterationTable), array_values($this->transliterationTable), $string);
}
}