<?php

/**
 * Snippet to generate a navigation.
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * @package clearFusionCMS\snippet\navigator
 * @copyright Copyright (c) 2012 - 2014 clearFusionCMS. All rights reserved.
 * @link http://clearfusioncms.com
 */

$_params['start'] = isset($start) ? $start : $tplEngine->controller->pageData['id'];
$_params['depth'] = $depth = isset($depth) ? $depth : 1;
$_params['descendAll'] = isset($descendAll) ? $descendAll : 1;
$_params['include'] = $include = isset($include) ? $include : null;
$_params['exclude'] = $exclude = isset($exclude) ? $exclude : null;
$_params['sortBy'] = isset($sortBy) && preg_match('/^[a-z]+$/', $sortBy) ? $sortBy : 'lft';
$_params['sortOrder'] = isset($sortOrder) && $sortOrder == 'DESC' ? 'DESC' : 'ASC';
$_params['limit'] = isset($limit) && preg_match('/^\d+$/', $limit) ? $limit : 0;
$_params['ignoreState'] = isset($ignoreState) ? $ignoreState : 0;
$_params['customFields'] = isset($customFields) ? $customFields : '';
$_params['abs'] = isset($abs) ? $abs: false;
$_params['fileSizes'] = isset($fileSizes) ? $fileSizes : 0;
if(isset($onEach)) $_params['onEach'] = $onEach;
$_params['navigator'] = isset($navigator) ? $navigator : 'navigator';
$wrapperTpl = isset($wrapperTpl) ? $tplEngine->getChunk($wrapperTpl) : '<ul[[+class]]>[[+child]]</ul>';
$itemTpl = isset($itemTpl) ? $tplEngine->getChunk($itemTpl) : '<li [[+classes]]><a href="[[+link]]">[[+menutitle]]</a>[[+child]]</li>';
$class = isset($class) ? " class=\"$class\"" : '';
unset($_params['class']);
$_params['depth'] = $_params['depth'] > 1 ? $_params['depth'] - 1 : 0;
$_params['currentDepth'] = isset($currentDepth) ? $currentDepth + 1 : 0;
$controller = $tplEngine->controller;
$content = $controller->content;

// Get root to path
if(!isset($_params['_toRoot'])) {
	$_params['_toRoot'] = array();
	if(!$_params['descendAll'] || (isset($trackPath) && $trackPath)) {
		$toRoot = $content->getPath($controller->pageData['id']);
		foreach($toRoot as $p)
			$_params['_toRoot'][$p['id']] = $p['id'];
	}
}

$children = $content->getMenuItems($_params['start'], $include === null ? null : explode(',', str_replace(' ', '', $include)), $exclude === null ? null : explode(',', str_replace(' ', '', $exclude)), $_params['sortBy'], $_params['sortOrder'], $_params['limit'], $_params['ignoreState'], $_params['customFields']);
if(count($children)) {
	$html = '';
	foreach($children as $page) {
		$page['classes'] = $page['id'] == $controller->pageData['id'] ? 'class="active"' : (isset($_params['_toRoot'][$page['id']]) ? 'class="activeChild"' : '');
		if($page['restype'] == 'doc')
			$page['link'] = $content->getPageURI($page['id'], $_params['abs']);
		else if($page['restype'] == 'static') {
			$page['link'] = $content->getPageURI($page['id'], $_params['abs']);
			$c = $content->getNodeData($page['id'], array('content'));
			if($_params['fileSizes']) {
				$s = filesize($c['content']);
				if($s >= 1073741824)
					$page['size'] = round($s / 1073741824) . 'GB';
				else if($s >= 1048576)
					$page['size'] = round($s / 1048576) . 'MB';
				else
					$page['size'] = round($s / 1024) . 'KB';
			}
		}
		else if($page['restype'] == 'folder') {
			$page['link'] = '';
		}
		else {
			$c = $content->getNodeData($page['id'], array('content'));
			$page['link'] = $c['content'];
		}
		if((!$depth || $depth > 1) && ($_params['descendAll'] || isset($_params['_toRoot'][$page['id']]))) {
			$_params['start'] = $page['id'];
			$page['child'] = (isset($onEach) ? $tplEngine->runSnippet($onEach, array('id' => $page['id'])) : '') . $tplEngine->runSnippet($_params['navigator'], $_params);
		}
		$html .= $tplEngine->parseString($itemTpl, $page);
	}
	echo $tplEngine->parseString($wrapperTpl, array('child' => $html, 'class' => $class, 'depth' => $_params['currentDepth']));
}
