/**
     * @return bool
     */
    public function htmlBuildResult()
    {
        $ela = array();

        foreach( CacheApplicationTool::clusterApplications() as $app )
        {
            if
            (
                $app->applicationObject->applicationType->attribute('type_identifier') == 'e-learning'
                && $app->getCustomParameter('HasLearningTracker')
                && ApplicationFactory::fetchByUri($app->attribute('url_alias'))->canRead()
            )
            {
                $ela[] = array(
                    'headline' => $app->headline,
                    'url'      => $app->applicationUrl(),
                );
            }
        }
        $this->pushResult( 'ela', $ela );
        $this->pushResult( 'content_service_configuration', $this->applicationObject->configurationContentService );
        $this->pushResult( 'is_mobile', ContextTool::instance()->isMobile() );
		$this->pushResult( 'isApplicationAllowedToUser', MMLimitedApplicationsAllowedUsers::hasCurrentUserAccessToApp(CacheApplicationTool::buildLocalizedApplicationByIdentifier('learning-needs-assessment')->application_id) );

        $uuid = $this->user()->attribute('uuid');
        $this->pushResult( 'userId', $uuid );

        //LNA Dashboard url
        $lmtBaseUrl = SolrSafeOperatorHelper::feature('LearningNeedsAssessment', 'LMTBaseUrl');
        $lmtPrefix = SolrSafeOperatorHelper::feature('LearningNeedsAssessment', 'LMTPrefix');
        $this->pushResult( 'dashboardUrl', $lmtBaseUrl . $lmtPrefix );

        $environment = new MMEnvironment();
        $this->pushResult( 'environment', $environment->env );
        
        $userData = CertificatesWebservice::readUserData();
        $this->pushResult( 'companyCode', $userData['CID'] );

        $countryPrefix = preg_replace( "~cluster|_~", "", ClusterTool::clusterIdentifier() );
        $this->pushResult( 'dashBoardCountryPrefix', strtolower( $countryPrefix ) . '_' );

        parent::htmlBuildResult();

        return true;
    }
    /**
     * @return string[]
     */
    protected function translations()
    {
        if( is_null($this->_translations) )
        {
            $translations = array();
            /* @var $applicationLocalized ApplicationLocalized */
            foreach( CacheApplicationTool::clusterApplications() as $k => $applicationLocalized )
            {
                if( !is_object( $applicationLocalized ) )
                {
                    eZDebug::writeError( 'Could not load applicationLocalized for '.$k );
                    continue;
                }
                $translations[$applicationLocalized->attribute('application_id')] = $applicationLocalized->attribute('name');
            }
            $this->_translations = $translations;
        }

        return $this->_translations;
    }
    /**
     * @return bool
     */
    public function htmlBuildResult()
    {
        parent::htmlBuildResult();

        $applicationIds = CountryApplicationLibrary::fetchAuthorizedApplicationIds();

        foreach( $applicationIds as $k => $v )
        {
            if(!$v)
                unset($applicationIds[$k]);
        }

        $applicationIds                  = array_values($applicationIds);
        $applications                    = ApplicationLocalized::fetchApplicationList( array('id' => array($applicationIds)) );
        $applicationWebtrendsIdentifiers = array();

        foreach( $applications as $k => $app )
        {
            /* @var $app ApplicationObject */
            $applicationWebtrendsParams = $app->applicationLocalized()->webtrendsParams();

            if( trim($applicationWebtrendsParams['cg_n']) != '' )
                $applicationWebtrendsIdentifiers[strval($app->attribute('id'))] = $applicationWebtrendsParams['cg_n'];
        }

        $this->tpl()->setVariable( 'apps_wb_labels', $applicationWebtrendsIdentifiers );

        /* @var $app ApplicationLocalized */
        foreach( CacheApplicationTool::clusterApplications() as $app )
        {
            if( $app->applicationObject->identifier == 'merck-connect' )
            {
                $this->tpl()->setVariable( 'merck_connect_results', true );
                break;
            }
        }

        return true;
    }
    /**
     * @param string$clusterIdentifier
     * @param bool $backupExisting
     * @param bool $includeTimestamp
     * @return array
     * @throws Exception
     */
    public function generateSpriteForCluster($clusterIdentifier, $backupExisting = true, $includeTimestamp = true)
    {
        $outputFile = $this->buildFilePath($this->outputPath, array($clusterIdentifier));
        $timestampOutputFile = $this->generateFileName($outputFile, $includeTimestamp);
        $generatedSpriteName = substr($timestampOutputFile, strrpos($timestampOutputFile, '/') + 1);

        ClusterTool::setCurrentCluster( $clusterIdentifier );

        $lessPath = "extension/{$clusterIdentifier}/design/oscar/stylesheets/";
        $icoSprite = "/ico_sprite.png";
        $offsetStep = 95;
        $crop = $offsetStep * 2;
        $mobileStep = 83;
        $spriteConvertOptions = " -crop x{$crop}+0+0 ";
        $globalConvertOptions = " -append -quality 80%";

        $applicationList = CacheApplicationTool::clusterApplications();
        $cssList = array();
        $icoList = array();
        foreach( $applicationList as $application )
        {
            $icoCss = 'ico_'.SolrSafeOperatorHelper::getAppIdentifierForIcon( $application->applicationObject->identifier );
            $icoFile = StaticData::clusterFilePath( $clusterIdentifier, 'apps/'.$application->applicationObject->identifier.$icoSprite, true, true );

            if ( ! file_exists( $icoFile ) )
            {
                $icoFile = StaticData::clusterFilePath( 'default', 'apps/'.$application->applicationObject->identifier.$icoSprite, true, true );
            }
            if ( file_exists( $icoFile ) && ! in_array( $icoCss, $cssList ) )
            {
                $cssList[] = $icoCss;
                $icoList[] = $icoFile;
            }
        }

        if (empty($icoList))
        {
            throw new \Exception("No icons found for cluster {$clusterIdentifier}", 0);
        }
        if ($backupExisting && file_exists($outputFile))
        {
            rename($outputFile, $outputFile . '.bak');
        }
        $convertBinPath = $this->convertBinPath;

        $cmd = 'export PATH=$PATH:' . $convertBinPath . '; convert ' . implode( $spriteConvertOptions, $icoList ) . "{$spriteConvertOptions} {$globalConvertOptions} {$outputFile}";
        $cmdStatus = 0;
        $cmdOutput = array();
        exec($cmd, $cmdOutput, $cmdStatus);
        if ($cmdStatus != 0)
        {
            return array(
                'errorCode' => $cmdStatus,
                'generateSpriteCommand' => $cmd,
                'error' => implode('\n', $cmdOutput),
            );
        }

        $css = "
#app-catalog a.app-bar-access-app-library .poster{
    background-image: none !important;
}
#app-catalog a .poster,
#hide-app a .poster,
.item-apps .batch .wrap > a .poster,
.item-related-app .batch .wrap > a .poster{
      background-image:url(/esibuild/static/{$clusterIdentifier}/{$generatedSpriteName});
      background-repeat: no-repeat;
}
";
        $cssMobile = "
#app-catalog a.app-bar-access-app-library .poster{
    background-image: none !important;
}
#app-catalog a .poster,
#hide-app a .poster,
.item-apps .batch .wrap > a .poster,
.item-related-app .batch .wrap > a .poster{
      background-image:url(/esibuild/static/{$clusterIdentifier}/{$generatedSpriteName});
      background-repeat: no-repeat;
      background-size: {$mobileStep}px auto !important;
}
";
        $offset = 0;
        $offsetMobile = 0;
        foreach( $cssList as $key => $cssStyle )
        {
            $css .= "
#app-catalog a .poster.$cssStyle,
#hide-app a .poster.$cssStyle,
.item-apps .batch .wrap > a .poster.$cssStyle,
.item-related-app .batch .wrap > a .poster.$cssStyle{
      background-position: 0 -{$offset}px !important;
}
";
            $cssMobile .= "
#app-catalog a .poster.$cssStyle,
#hide-app a .poster.$cssStyle,
.item-apps .batch .wrap > a .poster.$cssStyle{
      background-position: 0 -{$offsetMobile}px !important;
}
";
            $offset += $offsetStep;
            $offsetMobile += $mobileStep;
            $css .= "
#app-catalog a:hover .poster.$cssStyle,
#app-catalog a:active .poster.$cssStyle,
#app-catalog a.active .poster.$cssStyle,
#hide-app a:active .poster.$cssStyle,
#hide-app a.active .poster.$cssStyle,
.item-apps .batch:hover .wrap > a .poster.$cssStyle,
.item-related-app .batch:hover .wrap > a .poster.$cssStyle{
      background-position: 0 -{$offset}px !important;
}
";
            $cssMobile .= "
#app-catalog a:hover .poster.$cssStyle,
#app-catalog a:active .poster.$cssStyle,
#app-catalog a.active .poster.$cssStyle,
#hide-app a:active .poster.$cssStyle,
#hide-app a.active .poster.$cssStyle,
.item-apps .batch:hover .wrap > a .poster.$cssStyle,
.item-related-app .batch:hover .wrap > a .poster.$cssStyle{
      background-position: 0 -{$offsetMobile}px !important;
}
";
            $offset += $offsetStep;
            $offsetMobile += $mobileStep;
        }

        if ( ! file_exists( $lessPath ) )
        {
            mkdir( $lessPath, 0755 );
        }
        if ( is_dir( $lessPath ) )
        {
            $lessFile = $lessPath."mod.icosprite.less";
            file_put_contents( $lessFile, $css );
            $lessFileMobile = $lessPath."mod.icosprite.mobile.less";
            file_put_contents( $lessFileMobile, $cssMobile );
            eZCache::clearByID( array('ezjscore-packer'), 'template-block' );
        }

        return array(
            'errorCode' => 0,
            'generateSpriteCommand' => $cmd
        );
    }
) );


$options = $script->getOptions( "[clusterIdentifier:]", "", array(
    'clusterIdentifier' => 'Cluster identifier',
) );

$script->startup();
$script->initialize();

$clusters = array();
if( $options['clusterIdentifier'] )
{
    $clusters = array( $options['clusterIdentifier'] );
}
else 
{
    foreach( glob( 'extension/cluster_*') as $f )
        $clusters[] = basename( $f );
}


foreach( $clusters as $clusterIdentifier )
{
    $cli->output("generating cache for $clusterIdentifier");
    ClusterTool::setCurrentCluster( $clusterIdentifier );
    CacheApplicationTool::clusterApplications( true );
    ClusterTool::resetCurrentCluster();
}

$script->shutdown();