<?php
// Charray's CMS (CCMS) Version 0.9.5
//
// Copyright (c) 2007, Kinson Chan
// charray[at]gmail.com / kchan[at]cs.hku.hk
// All rights reserved.
//
// Please refer to LICENSE.txt coming with this package for
// terms and conditions for redistribution and reuse.
if (!defined("CCMS_IN")) {
die();
}
function ccms_expanded_menu($path, $sel_path, $full = false) {
global $ccms_data_path;
$result = array();
// To make the return value of this function consistent,
// provide some primitive return values here.
$result['title'] = ccms_path_title($path);
$result['nested'] = array();
// If it is not a fully expanded menu, check if it worths expanding.
if (!$full && (strpos($sel_path, $path) !== 0)) {
$result = array($path => $result);
return $result;
}
// We now scan the directory with the given path.
// Separate the found directories and files.
$dir = "$ccms_data_path$path";
foreach (ccms_list_directory($dir) as $filename) {
if (is_dir($filename)) {
$sub_path = substr($filename, strlen($ccms_data_path));
$temp = ccms_expanded_menu($sub_path, $sel_path, $full);
$result['nested']["$sub_path/"]['title'] = $temp[$sub_path]['title'];
$result['nested']["$sub_path/"]['nested'] = $temp[$sub_path]['nested'];
} else {
$sub_path = ccms_obtain_path($filename);
if (!$sub_path) {
continue;
}
$title = ccms_path_title($sub_path);
$result['nested'][$sub_path]['title'] = $title;
}
}
// Sort the resultant array according to path.
uksort($result['nested'], 'ccms_menu_comp');
// Remember the index file inside directories?
// This file gets counted in the previous procedures.
// We have to remove it to avoid double counting.
unset($result['nested']["$path"]);
unset($result['nested']["$path/"]);
// Finally, make array of result.
// This make the representation in a consistent form of path => info.
$result = array($path => $result);
return $result;
}
function ccms_full_menu($path, $sel_path) {
return ccms_expanded_menu($path, $sel_path, true);
}
function ccms_ancestors_menu($path) {
global $ccms_data_path;
global $ccms_site_name;
$result = array();
// The document where we stand at.
$result[$path]['title'] = ccms_path_title($path);
// Special case: when we are in root already, stop now.
if ($path == '' || $path == '/') {
return $result;
}
// Keep searching / from the back and stip them off one by one.
// We move step by step towards the root.
$pos = strrpos($path, '/');
while (($path != '' || $path != '/') && $pos) {
$path = substr($path, 0, $pos);
$result["$path/"]['title'] = ccms_path_title($path);
$pos = strrpos($path, '/');
}
// We stopped when we arrived at /. However this is also a valid path.
$result['/']['title'] = $ccms_site_name;
// We are adding the paths towards the root direction.
// To make it logical, we have to reverse it.
return array_reverse($result);
}
function ccms_menu_comp($a, $b) {
global $ccms_data_path;
static $orders = false;
if (!is_array($orders)) {
$sorted = @file("$ccms_data_path/path_order");
if (!$sorted) {
$sorted = array();
}
foreach ($sorted as $line_number => $path) {
$path = trim($path);
if (!$path) {
continue;
}
$path = ccms_normalize_path($path);
$orders[$path] = $line_number;
}
}
if (isset($orders[$a]) && isset($orders[$b])) {
return ($orders[$a] > $orders[$b]);
} else {
return strnatcmp($a, $b);
}
}
?>