/**
  * @return string
  */
 public function publishersFilter()
 {
     $applicationLocalized = CacheApplicationTool::buildLocalizedApplicationByIdentifier( MerckManualShowcase::mainApplicationIdentifier() );
     if ( $applicationLocalized instanceof ApplicationLocalized )
     {
         return $applicationLocalized->getPublishersFilter();
     }
     else
     {
         return null;
     }
 }
    /**
     * @param string $path
     * @param MerckManualSection $app
     * @return eZContentObjectTreeNode
     */
    public static function findNodeFromPath( $path, $app )
    {
        if( preg_match('#^/\d+/(?P<section>.*?)[/\?]?#', $path, $match) )
        {
            foreach ( array('url', 'remote', 'name') as $attribute )
            {
                $section = MerckManualFunctionCollection::fetchSection( $attribute, $match['section'] );
                if ( $section )
                {
                    $app->section = $section;
                    break;
                }
            }

            return eZContentObjectTreeNode::fetch(MerckManualShowcase::rootNodeId());
        }
        else
            return false;
    }
    /**
     * @see ezfSolrDocumentFieldBase::getData()
     * @return array
     */
    public function getData()
    {
        $data           = parent::getData();
        $content        = $this->ContentObjectAttribute->content();
        
        /* @var $content array */
        foreach ( array_merge( self::taxonomyAttribute(), array_keys($content) ) as $taxonomyIdentifier )
        {
            $taxonomyValues              = isset( $content[$taxonomyIdentifier] ) ? $content[$taxonomyIdentifier] : array(); 
            $subattrSourceIdValues       = array();
            $subattrSourceIdFieldName    = self::getCustomSubattributeFieldName(
                                                $taxonomyIdentifier,
                                                'source_id');
            
            foreach ( $taxonomyValues as $taxonomyValue )
            {
                if( preg_match( '#^symptom_.*$#', $taxonomyValue ) )
                {
                    $sourceKey                  = $taxonomyValue;
                    $subattrSourceIdValues[]    = $sourceKey;
                    
                    // we need a few more things for the symptoms
                    /* @type $node eZContentObjectTreeNode */
                    $contentObject = $this->ContentObjectAttribute->object();
                    $node          = $contentObject->mainNode();
                    $clusters      = NodeTool::getArticleClusters($node);

                    foreach( $clusters as $cluster )
                    {
                        ClusterTool::setCurrentCluster($cluster);

                        $ini = eZINI::fetchFromFile('extension/'.$cluster.'/settings/site.ini');
                        $node->setCurrentLanguage( $ini->variable('RegionalSettings', 'ContentObjectLocale') );

                        $applicationLocalized = CacheApplicationTool::buildLocalizedApplicationByIdentifier( MerckManualShowcase::mainApplicationIdentifier() );
                        if ( !$applicationLocalized )
                            continue;

                        $customSymptomAttributeKey = 'attr_custom_symptom_'.$cluster.'_s';
                        $labelTranslations         = FacetFilteringTool::getTaxonomyTranslation( 'symptom' );
                        $label                     = $labelTranslations[$sourceKey];
                        $url                       = preg_replace( '#^[^/]+#', '', MerckManualFunctionCollection::getMerckManualNodeUrl( 'meck-manual-showcase', $node, $ini->variable('RegionalSettings', 'ContentObjectLocale') ) );

                        ClusterTool::resetCurrentCluster();

                        $customSymptomAttributeValueParts = array(
                            mb_strtolower(mb_substr(StringTool::removeAccents( StringTool::CJK2Pinyin($label) ), 0, 1)),
                            $label,
                            $url
                        );

                        $data[$customSymptomAttributeKey] = implode( '##', $customSymptomAttributeValueParts );
                    }
                }
                else if ( preg_match('#^222\.[0-9]+$#', $taxonomyValue ) )
                {
                    $sourceKey                  = $taxonomyValue;
                    $subattrSourceIdValues[]    = $sourceKey;

                    /* @type $node eZContentObjectTreeNode */
                    /* @type $dataMap eZContentObjectAttribute[] */
                    $contentObject                    = $this->ContentObjectAttribute->object();
                    $node                             = $contentObject->mainNode();
                    $clusters                         = NodeTool::getArticleClusters($node);
                    $customSymptomAttributeValueParts = array(
                        $content['layer_natom'][0],
                        $content['sub_layer_natom'][0],
                    );

                    foreach( $clusters as $cluster )
                    {
                        ClusterTool::setCurrentCluster($cluster);

                        $customSymptomAttributeKey        = 'attr_custom_layer_sublayer_natom_'.$cluster.'_s';
                        $data[$customSymptomAttributeKey] = implode( '##', $customSymptomAttributeValueParts );

                        ClusterTool::resetCurrentCluster();
                    }
                }
                else
                    $subattrSourceIdValues[] = trim( $taxonomyValue );
            }

            $data[$subattrSourceIdFieldName] = empty($subattrSourceIdValues) ? '' : $subattrSourceIdValues;
        }
        
        return $data;
    }
    /**
     * @param array $params
     * @return array
     */
    public static function rawSearch($params)
    {
        $localApplication = CacheApplicationTool::buildLocalizedApplicationByIdentifier(MerckManualShowcase::mainApplicationIdentifier());
        $publishersFilters = $localApplication->getPublishersFilter();
        
        if ( isset($params['fq']) && trim($params['fq']) != '' )
        {
            $params['fq'] .= ' AND ' . $publishersFilters;
        }
        else
        {
            $params['fq'] = $publishersFilters;
        }

        return SolrTool::rawSearch($params);
    }
    /**
     * @param string $path
     * @param MerckManualShowcase $app
     * @return bool|eZContentObjectTreeNode
     */
    public static function findNodeFromPath( $path, &$app )
    {
        /* We remove the publisher folder id at the beginning of the url as we already have it */
        $path                  = preg_replace('#^/\d+/#', '/', $path );

        $manualAppNode         = eZContentObjectTreeNode::fetch( self::rootNodeId() );
        $manualAppNodePathName = $manualAppNode->pathWithNames();
        $tabNodePath           = explode('/', substr($path, 1));
        $remoteId              = end($tabNodePath);

        if(preg_match('/^[a-f0-9]{32}$/', $remoteId))
        {
            $nodeRemote = eZContentObjectTreeNode::fetchByRemoteID($remoteId);

            if($nodeRemote instanceof eZContentObjectTreeNode)
            {
                $app->fullContext = 'topic';
                return $nodeRemote;
            }
        }

        $pathParts = explode('/', substr($path, 1));

        if ( $pathParts[0] == 'table' )
        {
            $app->fullContext = 'table';
            return eZContentObjectTreeNode::fetch($pathParts[1]);
        }

        // Url with topic
        list($sectionName, $chapterName, $topicName, $mediaType, $mediaName, $other) = $pathParts;

        // Url without topic
        $matches = array();

        if ( preg_match('/media-([a-z]+)/', $topicName, $matches) )
        {
            list($sectionName, $chapterName, $mediaType, $mediaName, $other) = $pathParts;
            $topicName = null;
        }

        // Full section
        if ( $sectionName != null && is_null( $chapterName ) && is_null( $topicName ) )
        {
            $app->fullContext = 'section';
            $app->section = MerckManualFunctionCollection::fetchSection('url', $sectionName);
            return eZContentObjectTreeNode::fetch(MerckManualShowcase::rootNodeId());
        }
        if ( $sectionName == 'about-us' )
        {
            list($chapterName, $topicName, $mediaType, $mediaName, $other) = $pathParts;
        }

        $path = $manualAppNodePathName;
        // Full of chapter
        if ( $chapterName )
        {
            $app->fullContext = 'chapter';
            $path .= '/' . $chapterName;
        }

        // Full of topic
        if ( $topicName )
        {
            $app->fullContext = 'topic';
            $path .= '/' . $topicName;
        }

        $nodeId = eZURLAliasML::fetchNodeIDByPath($path);

        if ( $nodeId )
        {
            if ( $mediaName )
            {
                $mediaType = str_replace('media-', '', $mediaType);

                // Get media node
                // Full of html table image
                // /topic_name/(media-image-file)/oscar2/filename
                if ( $mediaType == 'image-file' )
                {
                    $app->fullContext = $mediaType;
                    $app->oscarFolder = $mediaName;
                    $app->mediaName = $other;
                }

                // Full of inline audio, video, image
                elseif ( $mediaType )
                {
                    // Construct the remote_id of the media folder
                    /* @type $dataMap eZContentObjectAttribute[] */
                    $contentObject        = eZContentObject::fetchByNodeID($nodeId);
                    $dataMap              = $contentObject->dataMap();
                    $publisherAttrContent = $dataMap['publisher']->content();

                    if ( isset($publisherAttrContent['relation_list'][0]['contentobject_remote_id']) )
                    {
                        // Get media folder
                        $mediaFolderRemoteId = sprintf('%s_media_%s', $publisherAttrContent['relation_list'][0]['contentobject_remote_id'], $mediaType);
                        $mediaFolder = eZContentObject::fetchByRemoteID($mediaFolderRemoteId);

                        if ( $mediaFolder )
                        {
                            // Get media
                            $mediaFolderPathName = $mediaFolder->mainNode()->pathWithNames();
                            $nodeId = eZURLAliasML::fetchNodeIDByPath($mediaFolderPathName . '/' . $mediaName);
                            $app->fullContext = $mediaType;
                        }
                    }
                }
            }
        }

        if ( $nodeId )
        {
            // Full of chapter must not be accessible, show first topic
            if ( $app->fullContext == 'chapter' )
            {
                $params = array(
                    'Depth'         => 1,
                    'DepthOperator' => 'eq',
                    'SortBy'        => array(array('priority', 'asc')),
                    'Limit'         => 1
                );
                $children = eZContentObjectTreeNode::subTreeByNodeID($params, $nodeId);

                if ( isset($children[0]) && $children[0] instanceof eZContentObjectTreeNode )
                {
                    $node = $children[0];
                    $app->fullContext = 'topic';
                }
                else
                    $node = null;
            }
            else
            {
                $node = eZContentObjectTreeNode::fetch($nodeId);
            }

            return $node;
        }

        return false;
    }
    /**
     * @return int
     */
    public static function rootNodeId()
    {
        if (self::$rootNodeID == null)
        {
            $aboutChapterPublisherInternalID = self::iniMerck()->variable( 'MerckManualAboutSettings', 'PublisherInternalID' );

            $params = array(
                'ClassFilterType'  => 'include',
                'ClassFilterArray' => array('article'),
                'Limit'            => 1,
                'Offset'           => 0,
                'Depth'            => 1,
                'AttributeFilter'  => array(
                    array(
                        'article/publisher_internal_id',
                        'like',
                        strtolower($aboutChapterPublisherInternalID) . '*'
                    )
                ),
            );

            /* @type $nodes eZContentObjectTreeNode[] */
            $nodes = eZContentObjectTreeNode::subTreeByNodeID( $params, MerckManualShowcase::rootNodeId() );
            $node  = $nodes[0];

            if ( !empty( $nodes ) )
                self::$rootNodeID = $node->attribute('node_id');
        }

        return self::$rootNodeID;
    }
    /**
     * @param eZSolrDoc[] $docList
     * @param eZContentObjectTreeNode $node
     * @param $publisherFolderRelations
     */
    protected function processSpecificMerckManual(&$docList, $node, $dataMap, $publisherFolderRelations)
    {
        /* @var eZContentObjectAttribute[] $datamapDefault */
        $datamapDefault             = $dataMap;
        $taxonomiesAttribute        = $datamapDefault['serialized_taxonomies'];
        $merckManualClusters        = array();

        foreach ( $publisherFolderRelations['clusters'] as $cluster => $applications )
            foreach ( $applications as $application )
                if( $application == MerckManualShowcase::mainApplicationIdentifier() )
                    $merckManualClusters[] = $cluster;

        if( $taxonomiesAttribute instanceof eZContentObjectAttribute && $taxonomiesAttribute->hasContent() )
        {
            $taxonomies = $taxonomiesAttribute->content();

            if( isset( $taxonomies['section']) )
            {
                $sections = $taxonomies['section'];

                foreach( $docList as $languageCode => $doc )
                {
                    $datamap = $node->object()->fetchDataMap(false, $languageCode);

                    if( !$datamap )
                        $datamap = $datamapDefault;

                    $customSectionChaptersParts = array(
                        $sections[0],
                        $node->attribute( 'node_id' ),
                        $datamap['headline']->content()
                    );

                    foreach( $merckManualClusters as $cluster )
                    {
                        $doc->addField('attr_custom_section_chapter_' . $cluster . '_s', implode('##', $customSectionChaptersParts));
                    }
                }
                $this->addValueToDoc( $docList, 'subattr_section___source_id____s', $sections, true, true );
            }
        }
        elseif ( !($taxonomiesAttribute instanceof eZContentObjectAttribute) )
        {
            eZLog::write( "Indexation : Article with nodeId {$node->NodeID} doesn't have serialized_taxonomies attribute", 'error.log' );
        }
    }