<?php
$start = microtime(true);
$instances = $this->instances;

$graph = $this->instances->getGraph();
$store = $this->instances->getStore();

// prepare namespaces
try{
    $namespacePrefixes = $graph->getNamespacePrefixes();
} catch (Exception $e){
    $namespacePrefixes = array();
}
$graphBase  = $graph->getBaseUri();
if (!array_key_exists(OntoWiki_Utils::DEFAULT_BASE, $namespacePrefixes)) {
    $namespacePrefixes[OntoWiki_Utils::DEFAULT_BASE] = $graphBase;
}

//data for filter module
$filter = $instances->getFilter();
$filter_js = json_encode(is_array($filter) ? $filter : array());

if (OntoWiki::getInstance()->extensionManager->isExtensionRegistered('filter')) {
    $this->headScript()->prependFile(
        OntoWiki::getInstance()->extensionManager->getComponentUrl('filter') .'resources/FilterAPI.js'
    );
}

if ($instances->hasData()) {
    $instanceInfo = $instances->getResources();
    if(!isset($this->other->disableValueQuery) || !$this->other->disableValueQuery){
        $instanceData = $instances->getValues();
    } else {
         $instanceData = array();
    }

    if (!isset($this->other->statusBar) || $this->other->statusBar) {
        $statusBar = $this->placeholder('main.window.statusbar');
    } else {
        $statusBar = null;
    }

    $itemsOnPage = count($instanceData);

    $propertyInfo = $instances->getShownProperties();

    $time = (microtime(true) - $start) * 1000;

    $start     = $instances->getOffset() + 1;
    $translate = OntoWiki::getInstance()->translate;

    $limit = $instances->getLimit();
    $offset = $instances->getOffset();
    if($limit != 0) {
        $page = ($offset / $limit) +1;
    } else {
        $page = 1;
    }
    $config = Erfurt_App::getInstance()->getConfig();

    if($graph->getOption($config->sysont->properties->isLarge)) {
        if ($statusBar !== null) {
            $statusBar->append(OntoWiki_Pager::get( Erfurt_Store::COUNT_NOT_SUPPORTED, $limit, $itemsOnPage, $page, $this->listName));
        }
    } else {
        $query = clone $instances->getResourceQuery();

        $where = 'WHERE '.$query->getWhere();

        try {
            $count = $store->countWhereMatches($graph->getModelIri(), $where, '?resourceUri', true);
        } catch (Erfurt_Store_Exception $e) {
            $count = Erfurt_Store::COUNT_NOT_SUPPORTED;
        }
        if ($statusBar !== null) {
            $statusBar->append(OntoWiki_Pager::get($count, $limit, $itemsOnPage, $page, $this->listName));
            if ($count != Erfurt_Store::COUNT_NOT_SUPPORTED) {
                $results = $count > 1 ? $translate->translate('results') : $translate->translate('result');
                $statusBar->append(sprintf($translate->translate('Search returned %1$d %2$s.'), $count, $results));
            }
        }
    }
    if(!isset($count) || $count > 10 ){
        if ($statusBar !== null) {
            $thisActionUrl = new OntoWiki_Url().'/';
            $listLimit = $limit;
            
            $limit = '<ul>';
            for($i = 10; $i <= 50; $i+=10){
            	$additionalClass = ($i == $listLimit)? 'selected ': '';
            	if($i == 10){
            		$limit .= '<li> Show me: ';
            	}else{
            		$limit .= '<li>';
            	}
            	$limit .= '<a class="'.$additionalClass.'minibutton" href="'. $thisActionUrl.'list/'.$this->listName.'/limit/'.$i.'">'.$i.'</a></li>';
            
            }
            $limit .= '</ul>';
            $statusBar->append($limit);
        }
    }

    if (defined('_OWDEBUG')) {
        if ($statusBar !== null) {
            $statusBar->append(sprintf($translate->translate('Query execution took %1$d ms.'), $time));
        }
    }
} else {
    $instanceData = array();
    $instanceInfo = array();
    $propertyInfo = array();
}

$this->headScript()->prependScript(
        'var reloadUrl = "'.
        new OntoWiki_Url(array()). // url to reload -> without config params
        '";
            var filtersFromSession = ' . $filter_js.';
            var listName = "'.$this->listName.'";'
        );
//other contains template specific data
$other = $this->other;
$other->namespacePrefixes = $namespacePrefixes;
$other->graphbase = $graphBase;

//call the requested template
echo $this->partial('partials/'.$this->mainTemplate.'.phtml',
    array(
        'instances'     => $instances,
        'instanceData'  => $instanceData,
        'instanceInfo'  => $instanceInfo,
        'propertyInfo'  => $propertyInfo,
        'other'         => $other,
        'listName'      => $this->listName,
        'start'         => $start
    )
);
