|
/** |
|
* Returns the tag cloud for specified parameters using eZ Publish database |
|
* |
|
* @param array $params |
|
* |
|
* @return array |
|
*/ |
|
private function tagCloud( $params ) |
|
{ |
|
$parentNodeID = 0; |
|
$classIdentifier = ''; |
|
$classIdentifierSQL = ''; |
|
$pathString = ''; |
|
$parentNodeIDSQL = ''; |
|
$dbParams = array(); |
|
$orderBySql = 'ORDER BY eztags.keyword ASC'; |
|
// q&d fix for language problem |
|
$fetchLanguage = false; |
|
|
|
if ( isset( $params['class_identifier'] ) ) |
|
$classIdentifier = $params['class_identifier']; |
|
|
|
if ( isset( $params['parent_node_id'] ) ) |
|
$parentNodeID = $params['parent_node_id']; |
|
|
|
if ( isset( $params['limit'] ) ) |
|
$dbParams['limit'] = $params['limit']; |
|
|
|
if ( isset( $params['offset'] ) ) |
|
$dbParams['offset'] = $params['offset']; |
|
|
|
if ( isset( $params['sort_by'] ) && is_array( $params['sort_by'] ) && !empty( $params['sort_by'] ) ) |
|
{ |
|
$orderBySql = 'ORDER BY '; |
|
$orderArr = is_string( $params['sort_by'][0] ) ? array( $params['sort_by'] ) : $params['sort_by']; |
|
|
|
foreach ( $orderArr as $key => $order ) |
|
{ |
|
if ( $key !== 0 ) $orderBySql .= ', '; |
|
$direction = isset( $order[1] ) ? $order[1] : false; |
|
switch( $order[0] ) |
|
{ |
|
case 'keyword': |
|
{ |
|
$orderBySql .= 'eztags.keyword ' . ( $direction ? 'ASC' : 'DESC' ); |
|
}break; |
|
case 'count': |
|
{ |
|
$orderBySql .= 'keyword_count ' . ( $direction ? 'ASC' : 'DESC' ); |
|
}break; |
|
} |
|
} |
|
} |
|
|
|
// q&d fix for language problem |
|
if ( isset( $params['language'] ) ) |
|
$fetchLanguage = $params['language']; |
|
|
|
$db = eZDB::instance(); |
|
|
|
if ( $classIdentifier ) |
|
{ |
|
$classID = eZContentObjectTreeNode::classIDByIdentifier( $classIdentifier ); |
|
$classIdentifierSQL = "AND ezcontentobject.contentclass_id = '" . $classID . "'"; |
|
} |
|
|
|
if ( $parentNodeID ) |
|
{ |
|
$node = eZContentObjectTreeNode::fetch( $parentNodeID ); |
|
if ( $node ) |
|
$pathString = "AND ezcontentobject_tree.path_string like '" . $node->attribute( 'path_string' ) . "%'"; |
|
$parentNodeIDSQL = "AND ezcontentobject_tree.node_id != " . (int) $parentNodeID; |
|
} |
|
|
|
$showInvisibleNodesCond = eZContentObjectTreeNode::createShowInvisibleSQLString( true, false ); |
|
$limitation = false; |
|
$limitationList = eZContentObjectTreeNode::getLimitationList( $limitation ); |
|
$sqlPermissionChecking = eZContentObjectTreeNode::createPermissionCheckingSQL( $limitationList ); |
|
|
|
$languageFilter = 'AND ' . eZContentLanguage::languagesSQLFilter( 'ezcontentobject' ); |
|
$languageFilter .= 'AND ' . eZContentLanguage::languagesSQLFilter( 'ezcontentobject_attribute', 'language_id' ); |
|
|
|
$rs = $db->arrayQuery( "SELECT eztags.id, eztags.keyword, COUNT(DISTINCT ezcontentobject.id) AS keyword_count |
|
FROM eztags_attribute_link |
|
LEFT JOIN ezcontentobject_attribute |
|
ON eztags_attribute_link.objectattribute_id = ezcontentobject_attribute.id |
|
AND eztags_attribute_link.objectattribute_version = ezcontentobject_attribute.version |
|
LEFT JOIN ezcontentobject |
|
ON ezcontentobject_attribute.contentobject_id = ezcontentobject.id |
|
LEFT JOIN ezcontentobject_tree |
|
ON ezcontentobject_attribute.contentobject_id = ezcontentobject_tree.contentobject_id |
|
LEFT JOIN eztags |
|
ON eztags.id = eztags_attribute_link.keyword_id |
|
LEFT JOIN eztags_keyword |
|
ON eztags.id = eztags_keyword.keyword_id |
|
$sqlPermissionChecking[from] |
|
WHERE " . eZContentLanguage::languagesSQLFilter( 'eztags' ) . " |
|
AND " . eZContentLanguage::sqlFilter( 'eztags_keyword', 'eztags' ) . " |
|
AND ezcontentobject.status = " . eZContentObject::STATUS_PUBLISHED . " |
|
AND ezcontentobject_attribute.version = ezcontentobject.current_version |
|
AND ezcontentobject_tree.main_node_id = ezcontentobject_tree.node_id |
|
$pathString |
|
$parentNodeIDSQL |
|
$classIdentifierSQL |
|
$showInvisibleNodesCond |
|
$sqlPermissionChecking[where] |
|
$languageFilter |
|
GROUP BY eztags.id, eztags.keyword |
|
$orderBySql", $dbParams ); |
|
|
|
$tagsCountList = array(); |
|
foreach( $rs as $row ) |
|
{ |
|
$tagsCountList[$row['id']] = $row['keyword_count']; |
|
} |
|
if ( empty( $tagsCountList ) ) return array(); |
|
|
|
/** @var eZTagsObject[] $tagObjects */ |
|
//$tagObjects = eZTagsObject::fetchList( array( 'id' => array( array_keys( $tagsCountList ) ) ) ); |
|
// q&d fix for language problem |
|
$tagObjects = eZTagsObject::fetchList( array( 'id' => array( array_keys( $tagsCountList ) ) ), null, null, false, $fetchLanguage ); |
|
if ( !is_array( $tagObjects ) || empty( $tagObjects ) ) |
|
return array(); |
|
|
|
$tagSortArray = array(); |
|
$tagKeywords = array(); |
|
$tagCounts = array(); |
|
foreach ( $tagObjects as $tag ) |
|
{ |
|
$tagKeyword = $tag->attribute( 'keyword' ); |
|
$tagCount = $tagsCountList[$tag->attribute( 'id' )]; |
|
|
|
$tagSortArray[] = array( |
|
'keyword' => $tagKeyword, |
|
'count' => $tagCount, |
|
'tag' => $tag |
|
); |
|
|
|
$tagKeywords[] = $tagKeyword; |
|
$tagCounts[] = $tagCount; |
|
} |
|
|
|
if ( isset( $params['post_sort_by'] ) ) |
|
{ |
|
if ( $params['post_sort_by'] === 'keyword' ) |
|
array_multisort( $tagKeywords, SORT_ASC, SORT_LOCALE_STRING, $tagSortArray ); |
|
else if ( $params['post_sort_by'] === 'keyword_reverse' ) |
|
array_multisort( $tagKeywords, SORT_DESC, SORT_LOCALE_STRING, $tagSortArray ); |
|
else if ( $params['post_sort_by'] === 'count' ) |
|
array_multisort( $tagCounts, SORT_ASC, SORT_NUMERIC, $tagSortArray ); |
|
else if ( $params['post_sort_by'] === 'count_reverse' ) |
|
array_multisort( $tagCounts, SORT_DESC, SORT_NUMERIC, $tagSortArray ); |
|
} |
|
|
|
$this->normalizeTagCounts( $tagSortArray, $tagCounts ); |
|
|
|
return $tagSortArray; |
|
} |