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,55 +6,73 @@ class TagCloud
{ {
/** /**
* Tag cloud version * Tag cloud version
*
* @var string
*/ */
public $version = '4.0.0'; public $version = '4.0.1';
/* /**
* Tag array container * Tag array container
*
* @var array
*/ */
protected $_tagsArray = array(); protected $tagsArray = array();
/** /**
* List of tags to remove from final output * List of tags to remove from final output
*
* @var array
*/ */
protected $_removeTags = array(); protected $removeTags = array();
/** /**
* Cached attributes for order comparison * Cached attributes for order comparison
*
* @var array
*/ */
protected $_attributes = array(); protected $attributes = array();
/* /**
* Amount to limit cloud by * Amount to limit cloud by
*
* @var null
*/ */
protected $_limit = null; protected $limit = null;
/* /**
* Minimum length of string to filtered in string * Minimum length of string to filtered in string
*
* @var null
*/ */
protected $_minLength = null; protected $minLength = null;
/* /**
* Custom format output of tags * Custom format output of tags
* *
* transformation: upper and lower for change of case * transformation: upper and lower for change of case
* transliterate: true\false
* trim: bool, applies trimming to tag * trim: bool, applies trimming to tag
*
* @var array
*/ */
protected $_formatting = array( protected $options = array(
'transformation' => 'lower', 'transformation' => 'lower',
'transliterate' => true,
'trim' => true 'trim' => true
); );
/** /**
* Custom function to create the tag-output * Custom function to create the tag-output
*
* @var null
*/ */
protected $_htmlizeTagFunction = null; protected $htmlizeTagFunction = null;
/** /**
* @var array Conversion map * @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',
'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A', 'å' => 'a', 'Å' => 'A', 'ã' => 'a', 'Ã' => 'A', 'ą' => 'a', 'Ą' => 'A', 'ā' => 'a', 'Ā' => 'A',
'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C', 'ä' => 'ae', 'Ä' => 'AE', 'æ' => 'ae', 'Æ' => 'AE', 'ḃ' => 'b', 'Ḃ' => 'B', 'ć' => 'c', 'Ć' => 'C',
'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C', 'ĉ' => 'c', 'Ĉ' => 'C', 'č' => 'c', 'Č' => 'C', 'ċ' => 'c', 'Ċ' => 'C', 'ç' => 'c', 'Ç' => 'C',
@@ -90,75 +108,110 @@ class TagCloud
'ь' => '', 'Ь' => '', 'э' => 'e', 'Э' => 'e', 'ю' => 'ju', 'Ю' => 'ju', 'я' => 'ja', 'Я' => 'ja' 'ь' => '', 'Ь' => '', 'э' => '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) public function __construct($tags = false)
{ {
if ($tags !== false) { if ($tags !== false) {
if (is_string($tags)) { if (is_string($tags)) {
$this->addString($tags); $this->addString($tags);
} elseif (count($tags)) { } else if (count($tags)) {
foreach ($tags as $key => $value) { foreach ($tags as $tag) {
$this->addTag($value); $this->addTag($tag);
} }
} }
} }
} }
/* /**
* Convert a string into a array * Convert a string into a array
* *
* @param string $string The string to use * @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(); $tagArray = array();
foreach ($inputArray as $inputTag) { foreach ($inputArray as $inputTag) {
$tagArray[]=$this->formatTag($inputTag); $tagArray[] = $this->formatTag($inputTag);
} }
$this->addTags($tagArray); $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 * Parse tag into safe format
* *
* @param string $string * @param string $string Tag to be formatted
* *
* @return string * @return mixed
*/ */
public function formatTag($string) public function formatTag($string)
{ {
$string = $this->_convertCharacters($string); if ($this->options['transliterate']) {
if ($this->_formatting['transformation']) { $string = $this->transliterate($string);
switch ($this->_formatting['transformation']) { }
if ($this->options['transformation']) {
switch ($this->options['transformation']) {
case 'upper': case 'upper':
$string = strtoupper($string); $string = $this->options['transliterate'] ? strtoupper($string) : mb_convert_case($string, MB_CASE_UPPER, "UTF-8");
break; break;
default: 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); $string = trim($string);
} }
return preg_replace('/[^\w ]/u', '', strip_tags($string)); return preg_replace('/[^\w ]/u', '', strip_tags($string));
} }
/* /**
* Assign tag to array * Assign tag to array
* *
* @param array $tagAttributes Tags or tag attributes array * @param array $tagAttributes Tags or tag attributes array
* *
* @return array $this->tagsArray * @return bool
*/ */
public function addTag($tagAttributes = array()) public function addTag($tagAttributes = array())
{ {
@@ -173,50 +226,53 @@ class TagCloud
return false; return false;
} }
$tag = $tagAttributes['tag']; $tag = $tagAttributes['tag'];
if (empty($this->_tagsArray[$tag])) { if (empty($this->tagsArray[$tag])) {
$this->_tagsArray[$tag] = array(); $this->tagsArray[$tag] = array();
} }
if (!empty($this->_tagsArray[$tag]['size']) && !empty($tagAttributes['size'])) { if (!empty($this->tagsArray[$tag]['size']) && !empty($tagAttributes['size'])) {
$tagAttributes['size'] = ($this->_tagsArray[$tag]['size'] + $tagAttributes['size']); $tagAttributes['size'] = ($this->tagsArray[$tag]['size'] + $tagAttributes['size']);
} elseif (!empty($this->_tagsArray[$tag]['size'])) { } elseif (!empty($this->tagsArray[$tag]['size'])) {
$tagAttributes['size'] = $this->_tagsArray[$tag]['size']; $tagAttributes['size'] = $this->tagsArray[$tag]['size'];
} }
$this->_tagsArray[$tag] = $tagAttributes; $this->tagsArray[$tag] = $tagAttributes;
$this->addAttributes($tagAttributes); $this->addAttributes($tagAttributes);
return $this->_tagsArray[$tag]; return $this->tagsArray[$tag];
} }
/* /**
* Add all attributes to cached array * Add all attributes to cached array
* *
* @return void * @param $attributes
*
* @return $this
*/ */
public function addAttributes($attributes) public function addAttributes($attributes)
{ {
$this->_attributes = array_unique( $this->attributes = array_unique(
array_merge( array_merge(
$this->_attributes, $this->attributes,
array_keys($attributes) array_keys($attributes)
) )
); );
return $this;
} }
/* /**
* Get attributes from cache * Get attributes from cache
* *
* @return array $this->_attibutes * @return array Collection of Attributes
*/ */
public function getAttributes() public function getAttributes()
{ {
return $this->_attributes; return $this->attributes;
} }
/* /**
* Assign multiple tags to array * 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()) public function addTags($tags = array())
{ {
@@ -226,79 +282,75 @@ class TagCloud
foreach ($tags as $tagAttributes) { foreach ($tags as $tagAttributes) {
$this->addTag($tagAttributes); $this->addTag($tagAttributes);
} }
return $this;
} }
/* /**
* Sets a minimum string length for the * Sets a minimum string length for the tags to display
* 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) public function setMinLength($minLength)
{ {
$this->_minLength = $minLength; $this->minLength = $minLength;
return $this; return $this;
} }
/**
/*
* Gets the minimum length value * Gets the minimum length value
* *
* @returns void * @return int
*/ */
public function getMinLength() public function getMinLength()
{ {
return $this->_minLength; return $this->minLength;
} }
/* /**
* Sets a limit for the amount of clouds * 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) public function setLimit($limit)
{ {
$this->_limit = $limit; $this->limit = $limit;
return $this; return $this;
} }
/* /**
* Get the limit for the amount tags * Get the limit for the amount tags to display
* to display
* *
* @param int $limit * @return int The maximum number
*
* @returns int $this->_limit
*/ */
public function getLimit() 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) public function setRemoveTag($tag)
{ {
$this->_removeTags[] = $this->formatTag($tag); $this->removeTags[] = $this->formatTag($tag);
return $this; return $this;
} }
/* /**
* Remove multiple tags from the array * 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) public function setRemoveTags($tags)
{ {
@@ -308,77 +360,81 @@ class TagCloud
return $this; return $this;
} }
/* /**
* Get the list of remove tags * Get the list of remove tags
* *
* @returns array $this->_removeTags * @return array A collection of tags to remove
*/ */
public function getRemoveTags() public function getRemoveTags()
{ {
return $this->_removeTags; return $this->removeTags;
} }
/* /**
* Assign the order field and order direction of the array * Assign the order field and order direction of the array
* *
* Order by tag or size / defaults to random * Order by tag or size / defaults to random
* *
* @param array $field * @param string $field The name of the field to sort by
* @param string $direction * @param string $direction The sort direction ASC|DESC
* *
* @returns $this->orderBy * @return $this
*/ */
public function setOrder($field, $direction = 'ASC') public function setOrder($field, $direction = 'ASC')
{ {
return $this->orderBy = array( $this->orderBy = array(
'field' => $field, 'field' => $field,
'direction' => $direction '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. * 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') public function render($returnType = 'html')
{ {
$this->_remove(); $this->remove();
$this->_minLength(); $this->minLength();
if (empty($this->orderBy)) { if (empty($this->orderBy)) {
$this->_shuffle(); $this->shuffle();
} else { } else {
$orderDirection = strtolower($this->orderBy['direction']) == 'desc' ? 'SORT_DESC' : 'SORT_ASC'; $orderDirection = strtolower($this->orderBy['direction']) == 'desc' ? 'SORT_DESC' : 'SORT_ASC';
$this->_tagsArray = $this->_order( $this->tagsArray = $this->order(
$this->_tagsArray, $this->tagsArray,
$this->orderBy['field'], $this->orderBy['field'],
$orderDirection $orderDirection
); );
} }
$this->_limit(); $this->limit();
$max = $this->_getMax(); $max = $this->getMax();
if (count($this->_tagsArray)) { if (count($this->tagsArray)) {
$return = ($returnType == 'html' ? '' : ($returnType == 'array' ? array() : '')); $return = ($returnType == 'html' ? '' : ($returnType == 'array' ? array() : ''));
foreach ($this->_tagsArray as $tag => $arrayInfo) { foreach ($this->tagsArray as $tag => $arrayInfo) {
$sizeRange = $this->_getClassFromPercent(($arrayInfo['size'] / $max) * 100); $sizeRange = $this->getClassFromPercent(($arrayInfo['size'] / $max) * 100);
$arrayInfo['range'] = $sizeRange; $arrayInfo['range'] = $sizeRange;
if ($returnType == 'array') { if ($returnType == 'array') {
$return [$tag] = $arrayInfo; $return [$tag] = $arrayInfo;
} elseif ($returnType == 'html') { } elseif ($returnType == 'html') {
$return .= $this->htmlizeTag( $arrayInfo, $sizeRange ); $return .= $this->htmlizeTag($arrayInfo, $sizeRange);
} }
} }
return $return; return $return;
@@ -394,14 +450,14 @@ class TagCloud
* *
* This will most likely only work in PHP >= 5.3 * This will most likely only work in PHP >= 5.3
* *
* @param array $arrayInfo * @param array $arrayInfo The data to pass into the closure
* @param string $sizeRange * @param string $sizeRange The size to pass into the closure
* *
* @return string * @return string
*/ */
public function htmlizeTag($arrayInfo, $sizeRange) public function htmlizeTag($arrayInfo, $sizeRange)
{ {
$htmlizeTagFunction = $this->_htmlizeTagFunction; $htmlizeTagFunction = $this->htmlizeTagFunction;
if (isset($htmlizeTagFunction) && if (isset($htmlizeTagFunction) &&
is_callable($htmlizeTagFunction) is_callable($htmlizeTagFunction)
) { ) {
@@ -414,34 +470,34 @@ class TagCloud
} }
} }
/* /**
* Removes tags from the whole array * 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(); $_tagsArray = array();
foreach ($this->_tagsArray as $key => $value) { foreach ($this->tagsArray as $key => $value) {
if (!in_array($value['tag'], $this->getRemoveTags())) { if (!in_array($value['tag'], $this->getRemoveTags())) {
$_tagsArray[$value['tag']] = $value; $_tagsArray[$value['tag']] = $value;
} }
} }
$this->_tagsArray = array(); $this->tagsArray = array();
$this->_tagsArray = $_tagsArray; $this->tagsArray = $_tagsArray;
return $this->_tagsArray; return $this->tagsArray;
} }
/* /**
* Orders the cloud by a specific field * Orders the cloud by a specific field
* *
* @param array $unsortedArray * @param array $unsortedArray Collection of unsorted data
* @param string $sortField * @param string $sortField The field that should be sorted
* @param string $sortWay * @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(); $sortedArray = array();
foreach ($unsortedArray as $uniqid => $row) { foreach ($unsortedArray as $uniqid => $row) {
@@ -459,65 +515,65 @@ class TagCloud
return $unsortedArray; return $unsortedArray;
} }
/* /**
* Parses the array and retuns * Parses the array and returns limited amount of items
* 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(); $limit = $this->getLimit();
if ($limit !== null) { if ($limit !== null) {
$i = 0; $i = 0;
$_tagsArray = array(); $_tagsArray = array();
foreach ($this->_tagsArray as $key => $value) { foreach ($this->tagsArray as $key => $value) {
if ($i < $limit) { if ($i < $limit) {
$_tagsArray[$value['tag']] = $value; $_tagsArray[$value['tag']] = $value;
} }
$i++; $i++;
} }
$this->_tagsArray = array(); $this->tagsArray = array();
$this->_tagsArray = $_tagsArray; $this->tagsArray = $_tagsArray;
} }
return $this->_tagsArray; return $this->tagsArray;
} }
/* /**
* Reduces the array by removing strings * Reduces the array by removing strings with a
* with a length shorter than the minLength * 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(); $limit = $this->getMinLength();
if ($limit !== null) { if ($limit !== null) {
$i = 0; $i = 0;
$_tagsArray = array(); $_tagsArray = array();
foreach ($this->_tagsArray as $key => $value) { foreach ($this->tagsArray as $key => $value) {
if (strlen($value['tag']) >= $limit) { if (strlen($value['tag']) >= $limit) {
$_tagsArray[$value['tag']] = $value; $_tagsArray[$value['tag']] = $value;
} }
$i++; $i++;
} }
$this->_tagsArray = array(); $this->tagsArray = array();
$this->_tagsArray = $_tagsArray; $this->tagsArray = $_tagsArray;
} }
return $this->_tagsArray; return $this->tagsArray;
} }
/* /**
* Finds the maximum 'size' value of an array * 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; $max = 0;
if (!empty($this->_tagsArray)) { if (!empty($this->tagsArray)) {
$p_size = 0; $p_size = 0;
foreach ($this->_tagsArray as $cKey => $cVal) { foreach ($this->tagsArray as $cKey => $cVal) {
$c_size = $cVal['size']; $c_size = $cVal['size'];
if ($c_size > $p_size) { if ($c_size > $p_size) {
$max = $c_size; $max = $c_size;
@@ -528,31 +584,32 @@ class TagCloud
return $max; return $max;
} }
/* /**
* Shuffle associated names in array * 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); shuffle($keys);
if (count($keys) && is_array($keys)) { if (count($keys) && is_array($keys)) {
$tmpArray = $this->_tagsArray; $tmpArray = $this->tagsArray;
$this->_tagsArray = array(); $this->tagsArray = array();
foreach ($keys as $key => $value) 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 * Get the class range using a percentage
* *
* @returns int $class The respective class * @param $percent
* name based on the percentage value *
* @return float|int The respective class name based on the percentage value
*/ */
protected function _getClassFromPercent($percent) protected function getClassFromPercent($percent)
{ {
$class = floor(($percent / 10)); $class = floor(($percent / 10));
@@ -572,18 +629,26 @@ class TagCloud
/** /**
* Calculate the class given to a tag from the * Calculate the class given to a tag from the
* weight percentage of the given tag. * weight percentage of the given tag.
*
* @param int $percent The percentage value
*
* @return float|int
*/ */
public function calculateClassFromPercent($percent) public function calculateClassFromPercent($percent)
{ {
return $this->_getClassFromPercent($percent); return $this->getClassFromPercent($percent);
} }
/** /**
* Convert accented chars into basic latin chars * 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) { protected function transliterate($string)
return str_replace(array_keys($this->_transliterationTable), array_values($this->_transliterationTable), $string); {
return str_replace(array_keys($this->transliterationTable), array_values($this->transliterationTable), $string);
} }
} }