Skip to content

Instantly share code, notes, and snippets.

@dfritschy
Last active May 30, 2016 09:22
Show Gist options
  • Save dfritschy/5afaad4e925a409e6b5b3166069dbf44 to your computer and use it in GitHub Desktop.
Save dfritschy/5afaad4e925a409e6b5b3166069dbf44 to your computer and use it in GitHub Desktop.
q&d fix for eztagscloud returning tags in main language instead of translation
/**
* 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;
}
{% include 'file:extension/site_stepping-stone/design/standard/parts/tagcloud.tpl' with { 'locationId': location.id, 'includeContentType': ['sst_news','sst_blog'], 'language': cjw_lang_get_default_code() } %}
<span class="hidden">
{eztagscloud(
hash(
'class_identifier', $includeContentTypes,
'sort_by', array('keyword', true()),
'language', $language
)
)}
</span>
{*{$tag_cloud|attribute(show,3)}*}
<div class="tags-cloud">
<h3>Tags</h3>
<ul>
{def $url = ''}
{foreach $tag_cloud as $tag}
{* q&d fix for url encoding mismatch bewteen legacy and symfony bundle *}
{* urldecode removes plus sign + used for spaces in url *}
{set $url = $tag['tag'].url|ezurl|urldecode}
<li>
<a href={$url}>{$tag['keyword']|wash} ({$tag['count']})</a>
</li>
{/foreach}
</ul>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment