Skip to content

Instantly share code, notes, and snippets.

@xserna
Created January 11, 2012 11:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xserna/1594258 to your computer and use it in GitHub Desktop.
Save xserna/1594258 to your computer and use it in GitHub Desktop.
Class extension to have tagID indexed on Solr
<?php
/**
* File containing the ezfSolrDocumentFieldTagID class.
*
* @author Xavier Serna (xserna@microblau.net)
* @see ezfSolrDocumentFieldBase
*/
class ezfSolrDocumentFieldTagID extends ezfSolrDocumentFieldBase
{
/**
* Contains the definition of subattributes for this given datatype.
* This associative array takes as key the name of the field, and as value
* the type. The type must be picked amongst the value present as keys in the
* following array :
* ezfSolrDocumentFieldName::$FieldTypeMap
*
* WARNING : this definition *must* contain the default attribute's one as well.
*
* @see ezfSolrDocumentFieldName::$FieldTypeMap
* @var array
*/
public static $subattributesDefinition = array( 'tagid' => 'int',
self::DEFAULT_SUBATTRIBUTE => 'lckeyword' );
/**
* The name of the default subattribute. It will be used when
* this field is requested with no subfield refinement.
*
* @see ezfSolrDocumentFieldDummyExample::$subattributesDefinition
* @var string
*/
const DEFAULT_SUBATTRIBUTE = 'tag';
/**
* @see ezfSolrDocumentFieldBase::__construct()
*/
function __construct( eZContentObjectAttribute $attribute )
{
parent::__construct( $attribute );
}
/**
* @see ezfSolrDocumentFieldBase::getData()
*/
public function getData()
{
$data = array();
$contentClassAttribute = $this->ContentObjectAttribute->attribute( 'contentclass_attribute' );
// Get attribute content
$eztags = new eZTags();
$eztags->createFromAttribute( $this->ContentObjectAttribute );
$ids = $eztags->idArray();
if (!count($ids))
{
// prevent indexing empty attr values
return NULL;
}
$keywords = explode('|#',$eztags->keywordString());
// Then hanlde all subattributes
foreach ( self::$subattributesDefinition as $name => $type )
{
switch ($name)
{
case 'tag':
{
$data[self::getFieldName($contentClassAttribute)] = $keywords;
} break;
case 'tagid':
{
$data[self::getFieldName($contentClassAttribute , 'tagid')] = $ids;
} break;
}
}
return $data;
}
/**
* @see ezfSolrDocumentFieldBase::getFieldName()
*/
public static function getFieldName( eZContentClassAttribute $classAttribute, $subAttribute = null )
{
if ( $subAttribute and
$subAttribute !== '' and
array_key_exists( $subAttribute, self::$subattributesDefinition ) and
$subAttribute != self::DEFAULT_SUBATTRIBUTE )
{
// A subattribute was passed
return parent::generateSubattributeFieldName( $classAttribute,
$subAttribute,
self::$subattributesDefinition[$subAttribute] );
}
else
{
// return the default field name here.
return parent::generateAttributeFieldName( $classAttribute,
self::$subattributesDefinition[self::DEFAULT_SUBATTRIBUTE] );
}
}
/**
* @see ezfSolrDocumentFieldBase::getFieldNameList()
*/
public static function getFieldNameList( eZContentClassAttribute $classAttribute, $exclusiveTypeFilter = array() )
{
// Generate the list of subfield names.
$subfields = array();
// Handle first the default subattribute
$subattributesDefinition = self::$subattributesDefinition;
if ( !in_array( $subattributesDefinition[self::DEFAULT_SUBATTRIBUTE], $exclusiveTypeFilter ) )
{
$subfields[] = parent::generateAttributeFieldName( $classAttribute, $subattributesDefinition[self::DEFAULT_SUBATTRIBUTE] );
}
unset( $subattributesDefinition[self::DEFAULT_SUBATTRIBUTE] );
// Then hanlde all other subattributes
foreach ( $subattributesDefinition as $name => $type )
{
if ( empty( $exclusiveTypeFilter ) or !in_array( $type, $exclusiveTypeFilter ) )
{
$subfields[] = parent::generateSubattributeFieldName( $classAttribute, $name, $type );
}
}
return $subfields;
}
/**
* @see ezfSolrDocumentFieldBase::getClassAttributeType()
*/
static function getClassAttributeType( eZContentClassAttribute $classAttribute, $subAttribute = null )
{
if ( $subAttribute and
$subAttribute !== '' and
array_key_exists( $subAttribute, self::$subattributesDefinition ) )
{
// If a subattribute's type is being explicitly requested :
return self::$subattributesDefinition[$subAttribute];
}
else
{
// If no subattribute is passed, return the default subattribute's type :
return self::$subattributesDefinition[self::DEFAULT_SUBATTRIBUTE];
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment