<?php
/**
 * ウィジェット
 */
require_once 'Platy/Widget.php';

class Platy_Widget_ManageNodeNav extends Platy_Widget
{
    public function install() { return true; }
    public function uninstall() { return true; }
    
    public function getSummary()
    {
        return 'NodeNav(Manage->Node用)';
    }
    
    public function show()
    {
        global $db, $Me, $session;
        
        // 権限チェック
        if ($Me->isAllowed('Manage_Contents') == false && $Me->isAllowed('Approve_Contents') == false) {
            return;
        }
        
        echo '<div class="widget">' . "\n";
        echo '<div class="title">' . "\n";
        echo 'ManageNodeNav';
        echo '</div>' . "\n";
        echo '<div class="main">' . "\n";
        
        
        $parentId = 1;
        $id = parentId;
        
        //echo 'sess_cn=' . $session->currentNode . "<br />\n";
        
        echo '<li>';
        echo '<ul>';
        
        // ホーム
        echo '<a href="' . BASE_URL . 'Manage/node/cd/path/_"';
        if ($session->currentNode == '/') {
            echo ' class="selected"';
        }
        echo '>';
        //if ($config['W_NODENAV_ICON'] == 1) {
            echo '<img src="' . BASE_URL . 'media/system/house.png' . '" class="icon_small">';
        //}
        echo 'root';
        //if ($config['W_NODENAV_CNUM'] == 1) {
            $sql = "SELECT COUNT(*) FROM Platy_Content_Node n, Platy_Content_Base b WHERE parentId=0 AND b.type!='Folder' AND n.id=b.id";
            $row = $db->fetchRow($sql);
            echo '(' . $row['COUNT(*)'] . ')';
        //}
        echo '</a>';
        
        echo '</ul>' . "\n";

        
        if ($this->currentPath == '/' && $this->currentName != '') {
            $sql = "SELECT b.name, b.title FROM Platy_Content_Base b, Platy_Content_Node n WHERE b.type != 'Folder' AND n.parentId=0 AND b.id=n.id ORDER BY n.ordering";
            $rows = $db->fetchAll($sql);
            foreach ($rows as $row) {
                echo '<ul>';
                echo '<a href="' . BASE_URL . $row['name'] . '"';
                if ($this->currentName == $row['name']) {
                    echo ' class="selected"';
                }
                echo '>';
                echo '&nbsp;&nbsp;';
                echo $row['title'];
                echo '</a>';
                echo '</ul>';
            }
        }

        $path_array = array();
        $id = $parentId;
        $depth = 0;
        $this->getChildFolder($path_array, $id, $depth);
        
        echo '</li>';
        
        echo '</div>' . "\n";
        echo '</div>' . "\n";
    }
    
    /**
     * 
     */
    private function getChildFolder(&$path_array, $id, &$depth)
    {
        global $db, $config, $session;
        
        //echo "getChildNode";
        //var_dump($path_array);
        //echo "id=" . $id . ',';
        //echo "depth=" . $depth;
        //echo "<br />\n";
        
        $sql = "SELECT n.id, b.type, b.name, b.title, b.modified, b.status FROM Platy_Content_Node n, Platy_Content_Base b WHERE parentId={$id} AND b.type='Folder' AND n.id=b.id ORDER BY ordering";
        //echo 'SQL1=' . $sql . "<br />\n";
        $rows = $db->fetchAll($sql);
        
        for ($i = 0; $i < count($rows); $i++) {
            $row = $rows[$i];
            //var_dump($row);
            $id = $row['id'];
            $type = $row['type'];
            $name = $row['name'];
            $title = $row['title'];
            $modified = $row['modified'];
            $status = $row['status'];
            
            echo '<ul>';
            
            $path = '';
            foreach ($path_array as $p) { $path .= $p . '/'; }
            
            $path_and_name = '/' . $path . $name . '/';
            
            $tmp = str_replace('/', '_', $path_and_name);
            echo '<a href="' . BASE_URL . 'Manage/node/cd/path/' . $tmp . '"';
            
            if ($session->currentNode == $path_and_name && $this->currentName == '') {
                echo ' class="selected"';
            }
            echo '>';
            
            for ($k = 0; $k < $depth; $k++) { echo '&nbsp;&nbsp;'; }
            //if ($config['W_NODENAV_ICON'] == 1) {
                echo '<img src="' . BASE_URL . 'media/system/Folder.png' . '" class="icon_small">';
            //}
            echo $title;
            //if ($config['W_NODENAV_CNUM'] == 1) {
                $sql2 = "SELECT COUNT(*) FROM Platy_Content_Node n, Platy_Content_Base b WHERE parentId={$id} AND b.type!='Folder' AND n.id=b.id";
                //echo 'SQL2=' . $sql2 . "<br />\n";
                $row2 = $db->fetchRow($sql2);
                echo '(' . $row2['COUNT(*)'] . ')';
            //}
            echo '</a>';
            echo '</ul>' . "\n";
            
            if ($this->currentPath == $path_and_name && $this->currentName != '') {
                $sql3 = "SELECT b.name, b.title FROM Platy_Content_Base b, Platy_Content_Node n WHERE b.type != 'Folder' AND n.parentId={$id} AND b.id=n.id ORDER BY n.ordering";
                //echo 'SQL3=' . $sql3 . "<br />\n";
                //echo 'path_and_name=' . $path_and_name . "<br />\n";
                $rows3 = $db->fetchAll($sql3);
                foreach ($rows3 as $row3) {
                    echo '<ul>';
                    echo '<a href="' . $path_and_name . $row3['name'] . '"';
                    if ($this->currentName == $row3['name']) {
                        echo ' class="selected"';
                    }
                    echo '>';
                    for ($k = 0; $k < $depth + 1; $k++) { echo '&nbsp;&nbsp;'; }
                    echo $row3['title'];
                    echo '</a>';
                    echo '</ul>';
                }
            }

            $parentId = $id;
            $depth++;
            
            //echo "push<br />\n";
            array_push($path_array, $name);
            $this->getChildFolder($path_array, $parentId, $depth);
        }
        //echo "pop<br />\n";
        $depth--;
        array_pop($path_array);
    }
}
?>