/**
     * @param eZContentObjectTreeNode $node
     * @return eZContentObjectTreeNode
     */
    public static function getPublisherNodeFromNode( $node )
    {
        if ( !$node instanceof eZContentObjectTreeNode )
            return null;

        $publisherFolderNodeId = PublisherFolderTool::getPublisherNodeIdFromArticleNode($node);

        return eZContentObjectTreeNode::fetch( $publisherFolderNodeId );
    }
    /**
     * @see eZContentObjectEditHandler::publish()
     * @param int $contentObjectID
     * @param eZContentObjectVersion $contentObjectVersion
     */
    function publish( $contentObjectID, $contentObjectVersion )
    {
        $contentObject = eZContentObject::fetch( $contentObjectID );

        if( $contentObject instanceof eZContentObject )
        {
            switch($contentObject->ClassIdentifier)
            {
                case 'article':
                {
                    ObjectVisibilityManager::updateGlobalLimitation($contentObject);

                    /* @type $mainNode eZContentObjectTreeNode */
                    /* @type $clustersToHide array */
                    $db                 = eZDB::instance();
                    $mainNode           = $contentObject->mainNode();
                    $clustersToHide     = eZINI::instance( 'merck.ini' )->variable( 'PublishSettings', 'clustersToHide' );
                    $publisherNodeId    = PublisherFolderTool::getPublisherNodeIdFromArticleNode($mainNode);
                    $publisherInfo      = PublisherFolderTool::getNodeIdToPathMapping($publisherNodeId);
                    $publisherClusters  = PublisherFolderTool::getClustersFromPath($publisherInfo['path']);
                    $inserted           = false;
                    
                    foreach( $clustersToHide as $clusterToHide )
                    {
                        if( in_array($clusterToHide, $publisherClusters) )
                        {
                            if ( !$inserted )
                            {
                                $db->query( sprintf(
                                    "INSERT INTO ezpending_actions (action, created, param)
                                        VALUES ('check_node_visibility', %d, '%s' )",
                                    time(),
                                    $contentObject->mainNodeID()
                                ));
                                $inserted = true;
                            }
                            
                            ObjectVisibilityManager::updateClusterLimitation($contentObjectID, $clusterToHide, true);
                        }
                    }
                }
                break;
            }
        }
    }
    public function contentBuildResult()
    {
        $oldSkipPage = SolrSafeOperatorHelper::getCustomParameter($this->applicationObject->identifier, 'SkipExitPage', 'application');
        $skipPage = $oldSkipPage || !$this->exitStrategy();

        $this->pushResult('user' , self::user());
        $this->pushResult('country', LocaleTool::countryISO3166Code());

        if ( $skipPage )
        {
            $_REQUEST['proceed'] = 'proceed';
            $_COOKIE[$this->cookieKey()] = '1';
            if ( ContextTool::instance()->isMobile() )
                $_REQUEST['r'] = 1;
        }

        if( isset($_REQUEST['proceed']) && isset($_COOKIE[$this->cookieKey()]) && filter_var($_COOKIE[$this->cookieKey()], FILTER_VALIDATE_INT))
        {
            // proceed to iframe view
            $this->iframeBuildResult( );
        }
        elseif( isset($_REQUEST['proceed']) )
        {
            // proceed requested but no cookie, we ask again and force redirect to make sure we the proper layout
            header('Location: '.ContextTool::instance()->domain().$this->applicationLocalized()->applicationUrl());
            eZExecution::cleanExit();
        }
        else
        {
            // normal exit page vie
            $isMobile   = ContextTool::instance()->isMobile();
            $urlToRedirect = isset($_REQUEST['r']) ? trim($_REQUEST['r']) : null;
            if ($this->isExitApplication() && $this->displayIframe())
            {
                $serverUrl  = ContextTool::instance()->domain();
                $params = array(
                    'r' => urlencode($urlToRedirect),
                    'exit_strategy' => 0,
                    'display_iframe' => $this->displayIframe(),
                    'new_window' => $this->newWindow()
                );

                $urlToRedirect = $serverUrl . '/external/exit/?' . http_build_query($params);
                $this->pushResult('decode_url', false);
                $this->pushResult('server_url', $serverUrl);
            }
            if($isMobile){
                if($this->applicationObject()->externalLinkHandler())
                {
                    $externalUrl = $this->applicationObject()->externalLinkHandler()->getNodeUrl( $this->node );
                    $urlToRedirect = isset($externalUrl) ? $externalUrl : $urlToRedirect ;

                    if(SolrSafeOperatorHelper::getCustomParameter($this->applicationObject->identifier, 'usePostMethod', 'application' )){

                        $url = $this->applicationObject()->externalLinkHandler()->getNodeUrl( $this->node );
                        $url = $this->getDeeplink( $url );

                        if ($url == null && isset($_REQUEST['r']))
                        {
                            $url = urldecode($_REQUEST['r']);
                        }

                        $url = in_array('arg', array_keys($this->_params['UserParameters'])) ? $url . $this->_params['UserParameters']['arg'] : $url;

                        $this->pushResult('postParams', $url);
                    }
                }
                else
                {
                    $urlToRedirect = '/bad_config';
                }
            }
            $this->pushResult('back_url', ContextTool::instance()->backUrl());
            $this->pushResult('url', $urlToRedirect);
            $this->pushResult('deeplink', urlencode( $this->getDeeplink() ));
            // Because we are passing boolean parameters to final URL, we need to make isset check.
            if ( $this->isDeeplinkApplication() || $this->isExitApplication() )
            {
                if ( isset( $_GET['display_iframe'] ) )
                {
                    $this->pushResult('display_iframe', $this->displayIframe());
                }
                if ( isset( $_GET['new_window'] ) )
                {
                    $this->pushResult('new_window', $this->newWindow());
                }
                if ( isset( $_GET['exit_strategy'] ) )
                {
                    $this->pushResult('exit_strategy', $this->exitStrategy());
                }
            }
        }

        if($this->node && $this->node->attribute('url_alias') != "")
        {
            $this->pushResult('article_url', end(explode("/", $this->node->attribute('url_alias'))));
            /* @type $pfDM eZContentObjectAttribute[] */
            $pf   = NodeTool::getPublisherNodeFromNode( $this->node );
            $pfDM = $pf->DataMap();
            $pfID = PublisherFolderTool::getPathToIDMapping( $pfDM['path']->content() );
            $this->pushResult( 'pfid', $pfID['pfid'] . '/' );
        }

        $oldOpenInNewWindow = SolrSafeOperatorHelper::getCustomParameter($this->applicationObject()->identifier, 'openInNewWindow', 'application');
        $openInNewWindow = $this->newWindow() || $oldOpenInNewWindow;
        $this->pushResult('application', $this->applicationObject);
        $this->pushResult('open_in_new_window', $openInNewWindow);
        $this->pushResult('cookie_key', $this->cookieKey());
        $this->pushResult('node', $this->node);
        
        if( SolrSafeOperatorHelper::featureIsActive( 'GoogleAnalytics' ) )
        {
            $this->pushResult( 'gtm_variables', $this->getGTMTags( $this->node ) );
            if( $this->node )
            {
                $publisherNodeId = PublisherFolderTool::getPublisherNodeIdFromArticleNode($this->node);

                if ($publisherNodeId)
                {
                    $publisherInfos  = PublisherFolderTool::getNodeIdToPathMapping( $publisherNodeId );
                    $publisherPath   = $publisherInfos['path'];

                    $this->pushResult( 'publisher', $this->applicationLocalized()->getPublisherFolderFromPath( $publisherPath ) );
                }
            }
        }
    }
 /**
  * @param eZContentObjectTreeNode $articleNode
  * @return integer
  */
 public static function getPublisherNodeIdFromArticleNode ( $articleNode )
 {
     return PublisherFolderTool::getPublisherNodeIdFromArticleNode($articleNode);
 }
    /**
     * The modify method gets the current content object AND the list of
     * Solr Docs (for each available language version).
     *
     * @param eZContentObject $contentObject
     * @param eZSolrDoc[] $docList
     */
    public function modify( eZContentObject $contentObject, &$docList )
    {
        /* @var eZContentObjectTreeNode $contentMainNode */
        $contentMainNode        = $contentObject->mainNode();
        $contentObjectLanguages = $contentObject->allLanguages();
        $articleLanguageArray   = array_keys($contentObjectLanguages);
        $publisherFolderNodeId  = PublisherFolderTool::getPublisherNodeIdFromArticleNode($contentMainNode);
        $isMerckManualShowcase  = false;
        // Depth : 0 => father article, 1+ => child article
        $articleRelativeDepth   = $contentMainNode->attribute('depth') - 4;

        if ( is_null($publisherFolderNodeId) )
            return;

        $publisherFolderInfos = PublisherFolderTool::getPublisherFolderInfosFromNodeId($publisherFolderNodeId);
        
        if ( !$publisherFolderInfos )
            return;

        $publisherFolderRelations = PublisherFolderTool::getPublisherFolderRelations($publisherFolderInfos['path']);

        if ( !$publisherFolderRelations || count($publisherFolderRelations['clusters']) === 0 )
            return;

        // node remote
        $remote = $contentMainNode->attribute("remote_id");
        $this->addValueToDoc( $docList, 'attr_node_remote_s', $remote);
        $this->addValueToDoc( $docList, 'attr_relative_depth_i', $articleRelativeDepth);

        foreach ( $publisherFolderRelations['clusters'] as $cluster => $applications )
        {
            ClusterTool::setCurrentCluster( $cluster );
            
            $applicationIdentifier = $applications[0];

            /* @var $applicationLocalized ApplicationLocalized */
            $applicationLocalized = CacheApplicationTool::buildLocalizedApplicationByIdentifier( $applicationIdentifier );

            if ( !($applicationLocalized instanceof ApplicationLocalized) )
            {
                if (count($applications) == 1)
                {
                    unset($publisherFolderRelations['clusters'][$cluster]);
                    continue;
                }

                $applicationIdentifier = $applications[1];
                $applicationLocalized = CacheApplicationTool::buildLocalizedApplicationByIdentifier( $applicationIdentifier );

                if ( !($applicationLocalized instanceof ApplicationLocalized) )
                {
                    unset($publisherFolderRelations['clusters'][$cluster]);
                    continue;
                }
                
                $publisherFolderRelations['clusters'][$cluster] = array ($applicationIdentifier);
            }

            // visibility per cluster
            $isVisible = ObjectVisibilityManager::isVisible($contentMainNode->attribute('contentobject_id'), $cluster);

            $this->addValueToDoc( $docList, 'attr_is_invisible_' . $cluster . '_b', !$isVisible );

            if (!isset(self::$_applicationIdentifierToId[$applicationIdentifier]))
                self::$_applicationIdentifierToId[$applicationIdentifier] = $applicationLocalized->applicationObject()->attribute ('id');

            $publisherFolder              = $applicationLocalized->getPublisherFolderFromPath($publisherFolderInfos['path']);
            $publisherLanguages           = $publisherFolder->getLanguages();
            $publisherAndArticleLanguages = array_intersect($publisherLanguages, $articleLanguageArray);

            if ( count($publisherAndArticleLanguages) == 0 )
                continue;

            $primaryLanguage = reset($publisherAndArticleLanguages);
            $contentMainNode->setCurrentLanguage($primaryLanguage);

            // publisher folder languages
            $languageKey       = 'subattr_language_' . $cluster . '____s';
            $formatedLanguages = array_map(
                array('kezfSolrArticleIndex', 'languageCode') ,
                $publisherAndArticleLanguages
            );

            $indexLanguages = array_unique( $formatedLanguages );

            $this->addValueToDoc( $docList, $languageKey, $indexLanguages, true, false );
            
            // Meck manual sections
            if( $applicationIdentifier == MerckManualShowcase::mainApplicationIdentifier() )
                $isMerckManualShowcase = true;

            // url computation
            /* @var eZContentObjectAttribute[] $dataMap */
            $dataMap = $contentMainNode->dataMap();

            ClusterTool::setCurrentCluster( $cluster );

            if( $applicationIdentifier == MerckManualShowcase::mainApplicationIdentifier() )
            {
                $urlContent = preg_replace(
                    '#^([^/]+/){2}#',
                    '',
                    MerckManualFunctionCollection::getMerckManualNodeUrl(
                        $applicationIdentifier,
                        $contentMainNode,
                        $primaryLanguage
                    )
                );
            }
            else
                $urlContent = preg_replace( '#^([^/]+/){2}#', '', $contentMainNode->urlAlias() );

            $this->addValueToDoc( $docList, 'attr_' . $cluster . '_url_s', $urlContent);

            $hasImage = array();

            for ( $mediaCase = 0; $mediaCase<=2; $mediaCase++ )
            {
                $hasImage[$mediaCase] = NodeOperatorHelper::hasImageArticle($contentMainNode, $mediaCase);
            }

            self::addValueToDoc( $docList, 'attr_has_image_' . $cluster . '_bst', base64_encode(json_encode($hasImage)) );

            $db = MMDB::instance();

            // index rating
            $row = $db->arrayQuery(sprintf("SELECT * FROM mm_rating_remote WHERE cluster_identifier = '%s' AND remote_id = '%s'", $cluster, $contentObject->remoteID()));
            if(count($row) && $row[0]["counter"] > 5 )
            {
                self::addValueToDoc( $docList, 'attr_content_rating_'.$cluster.'_f', $row[0]["total"]/$row[0]["counter"] );
                //update to_reindex
                $db->query(sprintf("UPDATE mm_rating_remote SET to_reindex=0 WHERE cluster_identifier = '%s' AND remote_id = '%s'", $cluster, $contentObject->remoteID()));
            }

            // index views
            $row = $db->arrayQuery(sprintf("SELECT * FROM mm_readcount_remote WHERE cluster_identifier = '%s' AND remote_id = '%s'", $cluster, $contentObject->remoteID()));
            if(count($row) && $row[0]["count"] > 0 )
            {
                self::addValueToDoc( $docList, 'attr_view_counter_'.$cluster.'_i', $row[0]["count"] );
                //update to_reindex
                $db->query(sprintf("UPDATE mm_readcount_remote SET to_reindex=0 WHERE cluster_identifier = '%s' AND remote_id = '%s'", $cluster, $contentObject->remoteID()));
            }

            if( $dataMap && $dataMap['media_content']->hasContent() )
            {
                $mediaContent      = $dataMap['media_content']->content();
                $mediaRelationList = $mediaContent['relation_list'];
                $entries           = array();
                $firstLink         = true;
                $totalQuizzes      = 0;

                foreach ( $mediaRelationList as $mediaRelation )
                {
                    $mediaObjectId = $mediaRelation['contentobject_id'];
                    $mediaClass = $mediaRelation['contentclass_identifier'];

                    /*******************************************************************************
                     * TODO : Adapt after PF Refactor
                     *******************************************************************************/
                    if ( !in_array($mediaClass, array('image', 'quiz', 'link')) )
                        continue;

                    
                    if ( $mediaClass === 'image' )
                    {
                        if ( count( $entries ) >= 3 )
                            continue;

                        // Language is DEPRECATED, Fake Language instead
                        $entries[] = $mediaObjectId . ";dep-DP";
                        /*******************************************************************************
                         * TODO : End : Adapt after PF Refactor
                         *******************************************************************************/
                    }
                    elseif ( ( $mediaClass === 'link' && $firstLink ) || $mediaClass === 'quiz' )
                    {
                        $mediaObject = eZContentObject::fetch( $mediaObjectId );

                        if ( !$mediaObject )
                            continue;

                        /* @var eZContentObjectAttribute[] $mediaDatamap */
                        $mediaDatamap = $mediaObject->dataMap();

                        if ( !is_array($mediaDatamap) )
                            continue;

                        if ( $mediaClass === 'link' )
                        {
                            if (!$mediaDatamap['url']->hasContent())
                                continue;

                            $firstLink = false;

                            self::addValueToDoc( $docList, 'attr_media_content_link_' . $cluster . '____ms', $mediaDatamap['url']->content(), false );
                        }
                        elseif ( $mediaClass === 'quiz' )
                        {
                            if ( $mediaDatamap['replies']->hasContent() )
                            {
                                $quizReplies = $mediaDatamap['replies']->content();
                                $quizReplies = $quizReplies->attribute('columns');
                                $quizReplies = $quizReplies['sequential'][1]['rows'];

                                if ( count($quizReplies) > 0 )
                                    self::addValueToDoc( $docList, 'attr_media_content_quiz_replies_' . $cluster . '____ms', $quizReplies, true);
                            }

                            if ( !empty($mediaDatamap['question']->DataText) )
                            {
                                self::addValueToDoc( $docList, 'attr_media_content_quiz_question_' . $cluster . '_ms', trim(strip_tags($mediaDatamap['question']->DataText)), false );
                            }

                            if ( !$mediaDatamap['points']->hasContent() )
                                continue;

                            $totalQuizzes+= $mediaDatamap['points']->content();
                        }
                    }
                }

                $withMediaDuration    = true;
                $typeMedias           = NodeOperatorHelper::getPictosMedia( $contentMainNode, $withMediaDuration );
                $typeMediasSerialized = base64_encode( json_encode($typeMedias) );

                // getPictosMedia as solr field
                self::addValueToDoc( $docList, 'attr_media_content_types_' . $cluster . '_bst', $typeMediasSerialized, false );

                // getMediaCount as solr field
                $mediaCount = NodeOperatorHelper::getMediaCount($contentMainNode);
                self::addValueToDoc( $docList, 'attr_media_content_count_' . $cluster . '_i', $mediaCount, false );

                // Used only for Quizz
                if ( $totalQuizzes > 0 )
                    self::addValueToDoc( $docList, 'attr_media_content_quiz_points_' . $cluster . '_i', $totalQuizzes, false );
            }
            
            unset($isHidden);
        }

        ClusterTool::resetCurrentCluster();

        $concernedNode = $contentMainNode;
        $concernedNode->setCurrentLanguage($articleLanguageArray[0]);

        if ( $contentMainNode->attribute('depth') > 4 )
        {
            $concernedNode = $contentMainNode->fetchParent();
            $concernedNode->setCurrentLanguage($articleLanguageArray[0]);
        }

        /* @type $concernedDM eZContentObjectAttribute[] */
        $concernedDM = $concernedNode->dataMap();
        $tags        = $concernedDM['tags'];

        $MerckManualAboutSectionId = eZIni::instance('merck.ini')->variable('MerckManualAboutSettings', 'SectionSourceId');
        $taxonomiesAttribute       = $concernedDM['serialized_taxonomies'];

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

            foreach( array_keys($publisherFolderRelations['clusters']) as $cluster )
            {
                self::addValueToDoc(
                    $docList,
                    "attr_exclude_from_search_{$cluster}_b",
                    $MerckManualAboutSectionId && in_array($MerckManualAboutSectionId, $taxonomies['section']),
                    false
                );
            }
        }

        if( $tags instanceof eZContentObjectAttribute && $tags->hasContent() )
            self::addValueToDoc( $docList, 'subattr_tags____ms', explode("|", $tags->content()) );

        if ( $isMerckManualShowcase )
            $this->processSpecificMerckManual($docList, $concernedNode, $concernedDM, $publisherFolderRelations);

        foreach ( $publisherFolderRelations['clusters'] as $cluster => $applications )
        {
            ClusterTool::setCurrentCluster( $cluster );

            foreach( $applications as $key => $applicationIdentifier )
            {
                if ( !isset(self::$_applicationIdentifierToId[$applicationIdentifier]) )
                {
                    $applicationLocalized = CacheApplicationTool::buildLocalizedApplicationByIdentifier( $applicationIdentifier );

                    if( !($applicationLocalized instanceof ApplicationLocalized) )
                    {
                        eZDebug::writeError(
                            sprintf('Cluster: %s; Identifier: %s; ObjectId: %s', $cluster, $applicationIdentifier, $contentObject->attribute('id') ),
                            'Error getting application localized' );
                        unset( $publisherFolderRelations['clusters'][$cluster][$key] );
                    }
                    else
                        self::$_applicationIdentifierToId[$applicationIdentifier] = $applicationLocalized->applicationObject()->attribute('id');
                }
            }
        }

        ClusterTool::resetCurrentCluster();
        
        $applicationIds = array();

        foreach ( $publisherFolderRelations['clusters'] as $cluster => $applications )
            foreach( $applications as $applicationIdentifier )
                $applicationIds[] = self::$_applicationIdentifierToId[$applicationIdentifier];

        $uniqueApplicationIds = array_unique($applicationIds);

        foreach( $docList as $languageCode => $doc )
        {
            $dataMap               = $contentObject->fetchDataMap(false, $languageCode);
            $headline              = $dataMap['headline']->content();
            $headlineLowerCase     = mb_strtolower( StringTool::removeAccents($headline, 'utf-8', false), 'utf-8' );
            $unaccentedFirstLetter = mb_substr($headlineLowerCase, 0, 1, 'utf-8');

            $doc->addField( 'attr_headline_first_letter_lc_s', $unaccentedFirstLetter );
            $doc->addField( 'attr_headline_lc_s', $headlineLowerCase );

            foreach( $uniqueApplicationIds as $id )
            {
                $doc->addField( 'subattr_local_application___source_id____s', $id );
            }

            foreach( $publisherFolderRelations['clusters'] as $cluster => $applications )
            {
                foreach( $applications as $applicationIdentifier )
                {
                    $doc->addField('subattr_local_application___source_mixed____s', $cluster . '##unused##' . self::$_applicationIdentifierToId[$applicationIdentifier]);
                }
            }
        }
    }