/**
 * Prints the nested list for pages and categories
 *
 * @param string $listtype 'cats-checkboxlist' for a fake nested checkbock list of categories for the news article edit/add page
 * 												'cats-sortablelist' for a sortable nested list of categories for the admin categories page
 * 												'pages-sortablelist' for a sortable nested list of pages for the admin pages page
 * @param int $articleid Only for $listtype = 'cats-checkboxlist': For ID of the news article if the categories an existing articles is assigned to shall be shown, empty if this is a new article to be added.
 * @param string $option Only for $listtype = 'cats-checkboxlist': "all" to show all categories if creating a new article without categories assigned, empty if editing an existing article that already has categories assigned.
 * @return string | bool
 */
function printNestedItemsList($listtype = 'cats-sortablelist', $articleid = '', $option = '')
{
    global $_zp_zenpage;
    switch ($listtype) {
        case 'cats-checkboxlist':
        default:
            $ulclass = "";
            break;
        case 'cats-sortablelist':
        case 'pages-sortablelist':
            $ulclass = " class=\"page-list\"";
            break;
    }
    switch ($listtype) {
        case 'cats-checkboxlist':
        case 'cats-sortablelist':
            $items = $_zp_zenpage->getAllCategories(false);
            break;
        case 'pages-sortablelist':
            $items = $_zp_zenpage->getPages(false);
            break;
        default:
            $items = array();
            break;
    }
    $indent = 1;
    $open = array(1 => 0);
    $rslt = false;
    foreach ($items as $item) {
        switch ($listtype) {
            case 'cats-checkboxlist':
            case 'cats-sortablelist':
                $itemobj = new ZenpageCategory($item['titlelink']);
                $ismypage = $itemobj->isMyItem(ZENPAGE_NEWS_RIGHTS);
                break;
            case 'pages-sortablelist':
                $itemobj = new ZenpagePage($item['titlelink']);
                $ismypage = $itemobj->isMyItem(ZENPAGE_PAGES_RIGHTS);
                break;
        }
        $itemsortorder = $itemobj->getSortOrder();
        $itemid = $itemobj->getID();
        if ($ismypage) {
            $order = explode('-', $itemsortorder);
            $level = max(1, count($order));
            if ($toodeep = $level > 1 && $order[$level - 1] === '') {
                $rslt = true;
            }
            if ($level > $indent) {
                echo "\n" . str_pad("\t", $indent, "\t") . "<ul" . $ulclass . ">\n";
                $indent++;
                $open[$indent] = 0;
            } else {
                if ($level < $indent) {
                    while ($indent > $level) {
                        $open[$indent]--;
                        $indent--;
                        echo "</li>\n" . str_pad("\t", $indent, "\t") . "</ul>\n";
                    }
                } else {
                    // indent == level
                    if ($open[$indent]) {
                        echo str_pad("\t", $indent, "\t") . "</li>\n";
                        $open[$indent]--;
                    } else {
                        echo "\n";
                    }
                }
            }
            if ($open[$indent]) {
                echo str_pad("\t", $indent, "\t") . "</li>\n";
                $open[$indent]--;
            }
            switch ($listtype) {
                case 'cats-checkboxlist':
                    echo "<li>\n";
                    printCategoryCheckboxListEntry($itemobj, $articleid, $option);
                    break;
                case 'cats-sortablelist':
                    echo str_pad("\t", $indent - 1, "\t") . "<li id=\"id_" . $itemid . "\" class=\"clear-element page-item1 left\">";
                    printCategoryListSortableTable($itemobj, $toodeep);
                    break;
                case 'pages-sortablelist':
                    echo str_pad("\t", $indent - 1, "\t") . "<li id=\"id_" . $itemid . "\">";
                    printPagesListTable($itemobj, $toodeep);
                    break;
            }
            $open[$indent]++;
        }
    }
    while ($indent > 1) {
        echo "</li>\n";
        $open[$indent]--;
        $indent--;
        echo str_pad("\t", $indent, "\t") . "</ul>";
    }
    if ($open[$indent]) {
        echo "</li>\n";
    } else {
        echo "\n";
    }
    return $rslt;
}
/**
 * Prints the nested list for pages and categories
 *
 * @param string $listtype 'cats-checkboxlist' for a fake nested checkbock list of categories for the news article edit/add page
 * 												'cats-sortablelist' for a sortable nested list of categories for the admin categories page
 * 												'pages-sortablelist' for a sortable nested list of pages for the admin pages page
 * @param int $articleid Only for $listtype = 'cats-checkboxlist': For ID of the news article if the categories an existing articles is assigned to shall be shown, empty if this is a new article to be added.
 * @param string $option Only for $listtype = 'cats-checkboxlist': "all" to show all categories if creating a new article without categories assigned, empty if editing an existing article that already has categories assigned.
 * @return string | bool
 */
function printNestedItemsList($listtype = 'cats-sortablelist', $articleid = '', $option = '', $class = 'nestedItem')
{
    global $_zp_CMS;
    switch ($listtype) {
        case 'cats-checkboxlist':
            $items = $_zp_CMS->getAllCategories(false);
            $classInstantiator = 'newCategory';
            $rights = LIST_RIGHTS;
            $ulclass = "";
            break;
        case 'cats-sortablelist':
            $items = $_zp_CMS->getAllCategories(false);
            $classInstantiator = 'newCategory';
            $rights = ZENPAGE_NEWS_RIGHTS;
            $ulclass = " class=\"page-list\"";
            break;
        case 'pages-sortablelist':
            $items = $_zp_CMS->getPages(false);
            $classInstantiator = 'newPage';
            $rights = ZENPAGE_PAGES_RIGHTS;
            $ulclass = " class=\"page-list\"";
            break;
        default:
            $items = array();
            $ulclass = "";
            break;
    }
    $indent = 1;
    $open = array(1 => 0);
    $rslt = false;
    foreach ($items as $item) {
        $itemobj = $classInstantiator($item['titlelink']);
        if ($rights == LIST_RIGHTS) {
            //	list the catagory if the user has it as a maanaged object
            $ismine = $itemobj->subRights();
        } else {
            $ismine = $itemobj->isMyItem($rights);
        }
        if ($ismine) {
            $itemsortorder = $itemobj->getSortOrder();
            $itemid = $itemobj->getID();
            $order = explode('-', $itemsortorder);
            $level = max(1, count($order));
            if ($toodeep = $level > 1 && $order[$level - 1] === '') {
                $rslt = true;
            }
            if ($level > $indent) {
                echo "\n" . str_pad("\t", $indent, "\t") . "<ul" . $ulclass . ">\n";
                $indent++;
                $open[$indent] = 0;
            } else {
                if ($level < $indent) {
                    while ($indent > $level) {
                        $open[$indent]--;
                        $indent--;
                        echo "</li>\n" . str_pad("\t", $indent, "\t") . "</ul>\n";
                    }
                } else {
                    // indent == level
                    if ($open[$indent]) {
                        echo str_pad("\t", $indent, "\t") . "</li>\n";
                        $open[$indent]--;
                    } else {
                        echo "\n";
                    }
                }
            }
            if ($open[$indent]) {
                echo str_pad("\t", $indent, "\t") . "</li>\n";
                $open[$indent]--;
            }
            switch ($listtype) {
                case 'cats-checkboxlist':
                    echo "<li>\n";
                    printCategoryCheckboxListEntry($itemobj, $articleid, $option, $class);
                    break;
                case 'cats-sortablelist':
                    echo str_pad("\t", $indent - 1, "\t") . "<li id=\"id_" . $itemid . "\">";
                    printCategoryListSortableTable($itemobj, $toodeep);
                    break;
                case 'pages-sortablelist':
                    echo str_pad("\t", $indent - 1, "\t") . "<li id=\"id_" . $itemid . "\">";
                    printPagesListTable($itemobj, $toodeep);
                    break;
            }
            $open[$indent]++;
        }
    }
    while ($indent > 1) {
        echo "</li>\n";
        $open[$indent]--;
        $indent--;
        echo str_pad("\t", $indent, "\t") . "</ul>";
    }
    if ($open[$indent]) {
        echo "</li>\n";
    } else {
        echo "\n";
    }
    return $rslt;
}