<?php
require_once 'modules/admin/models/DashboardPlugin.php';

class PluginTodo extends DashboardPlugin
{
    var $hasTab = false;
    var $hasPanel = true;
    var $default = true;
    var $name = /*T*/"Todo List"/*/T*/;
    var $description = /*T*/"This shows a todo list on your dashboard."/*/T*/;
    
    var $jsLibs         = array('plugins/dashboard/todo/PluginTodo.js');
    var $cssPages       = array('plugins/dashboard/todo/PluginTodo.css');
    
    var $tasks = array();

    function getTab()
    {
        return '';
    }
    
    function getPanel()
    {
        return '<div id="todo-list"></div><script>PluginTodo();</script>';
    }
    
    function doAction()
    {
        $this->_loadTasks();
        if (!isset($_REQUEST['pluginAction'])) $_REQUEST['pluginAction'] = 'listTasks';
        
        switch($_REQUEST['pluginAction']) {
            case 'listTasks':
                return $this->listTasks($this->tasks);
                break;
            case 'addTask':
                if ($_POST['type'] == 'task')
                    $this->addTask();
                else 
                    $this->addGroup();
                break;
            case 'completeTask':
                if (!isset($_POST['id'])) break;
                $this->tasks = $this->completeTask($this->tasks);
                $this->_storeTasks();
                break;
            case 'uncompleteTask':
                if (!isset($_POST['id'])) break;
                $this->tasks = $this->uncompleteTask($this->tasks);
                $this->_storeTasks();
                break;
            case 'removeTask':
                if (!isset($_POST['id'])) break;
                $this->tasks = $this->deleteTask($this->tasks);
                $this->_storeTasks();
                break;
            case 'moveNode':
                if (!isset($_POST['id'])) break;
                $added = false;
                // make a copy of the task before deleting
                $movedTask = $this->getItem($this->tasks);
                // delete the stored task
                $this->tasks = $this->deleteTask($this->tasks);
                // now readd it to the new position
                $this->tasks = $this->addTaskToPosition($this->tasks, $movedTask, $_POST['parentId'], $_POST['index'], ($_POST['parentId'] == -1), $added);
                
                $this->_storeTasks();
                break;
            case 'editTask':
                if (!isset($_POST['id'])) break;
                $this->tasks = $this->editTask($this->tasks);
                $this->_storeTasks();
        }
    }
    
    function _storeTasks()
    {
        $this->storeData('tasks', serialize($this->tasks));
    }
    
    function _loadTasks()
    {
        if ($this->storedDataExists('tasks')) {
            $this->tasks = unserialize($this->retrieveData('tasks'));
        } else {
            /* Setup sample tasks */
            $this->tasks = array();
            $group = new PluginTodoGroup();
            $group->setId($this->_nextId());
            $group->setName('Sample Tasks');
            
            $taskOne = new PluginTodoTask();
            $taskOne->setId($this->_nextId());
            $taskOne->setName('Run Batch Payments');
            $taskOne->setChecked(false);
            
            $taskTwo = new PluginTodoTask();
            $taskTwo->setId($this->_nextId());
            $taskTwo->setName('Update Support Tickets');
            $taskTwo->setChecked(false);
            
            $taskThree = new PluginTodoTask();
            $taskThree->setId($this->_nextId());
            $taskThree->setName('Setup todo list');
            $taskThree->setChecked(true);
            
            $group->addChild($taskOne);
            $group->addChild($taskTwo);
            $group->addChild($taskThree);
            
            $this->tasks[] = $group;
            $this->_storeTasks();
        }
    }
    
    function _nextId()
    {
        $nextId = 1;
        if ($this->storedDataExists('lastId')) {
            $nextId = $this->retrieveData('lastId') + 1;
        }
        $this->storeData('lastId', $nextId);
        return $nextId;
    }
    
    function getItem($tasks)
    {
        foreach ($tasks as $task)
        {
            if ($task instanceof PluginTodoTask) {
                if ($task->getId() == $_POST['id']) {
                    return $task;
                }
            } else {
                if ($task->getId() == $_POST['id']) {
                    return $task;
                }
                $ret = $this->getItem($task->getChildren());
                if ($ret != null) return $ret;
            }
        }
        return null;
    }
    
    function listTasks($tasks)
    {
        $data = "[";
        foreach ($tasks as $task)
        {
            if ($task instanceof PluginTodoTask) {
                $data .= "{";                
                $data .= "id: '".$task->getId()."',";
                $data .= "text: '".addslashes($task->getName())."',";
                $data .= "leaf: true,";
                $data .= "checked: ".$task->getChecked().",";
                $data .= "cls: '".$task->getClass()."'";
                $data .= "},";
            } else {
                $data .= "{";
                $data .= "id: '".$task->getId()."',";
                $data .= "text: '".addslashes($task->getName())."',";
                $data .= "leaf: false,";
                $data .= "cls: 'folder',";
                $data .= "children: ".$this->listTasks($task->getChildren()).",";
                $data .= "expanded: ".$task->getExpanded();
                $data .= "},";
            }
        }
        if ($data[strlen($data) - 1] == ',')
            $data = mb_substr($data, 0, strlen($data) - 1);
        
        $data .= "]";
        
        return $data;
    }
    
    function deleteTask($tasks)
    {
        if ($_POST['id'] == -1) return $tasks;
        $updatedTasks = array();
        foreach ($tasks as $task)
        {
            if ($task instanceof PluginTodoTask) {
                if ($task->getId() != $_POST['id']) {
                    $updatedTasks[] = $task;
                }
            } else {
                if ($task->getId() != $_POST['id']) {
                    $task->setChildren($this->deleteTask($task->getChildren()));
                    $updatedTasks[] = $task;
                }
            }
        }
        return $updatedTasks;
    }
    
    function completeTask($tasks)
    {
        $updatedTasks = array();
        foreach ($tasks as $task)
        {
            if ($task instanceof PluginTodoTask) {
                if ($task->getId() == $_POST['id']) {
                    $task->setChecked(true);
                }
                $updatedTasks[] = $task;
            } else {
               $task->setChildren($this->completeTask($task->getChildren()));
               $updatedTasks[] = $task;
            }
        }
        return $updatedTasks;
    }
    
    function uncompleteTask($tasks)
    {
        $updatedTasks = array();
        foreach ($tasks as $task)
        {
            if ($task instanceof PluginTodoTask) {
                if ($task->getId() == $_POST['id']) {
                    $task->setChecked(false);
                }
                $updatedTasks[] = $task;
            } else {
               $task->setChildren($this->uncompleteTask($task->getChildren()));
               $updatedTasks[] = $task;
            }
        }
        return $updatedTasks;
    }
    
    function editTask($tasks)
    {
        $updatedTasks = array();
        foreach ($tasks as $task)
        {
            if ($task->getId() == $_POST['id']) {
                $task->setName($_POST['name']);
            }

            if ($task instanceof PluginTodoTask) {
                $updatedTasks[] = $task;
            } else {
                $task->setChildren($this->editTask($task->getChildren()));
                $updatedTasks[] = $task;
            }
        }
        return $updatedTasks;
    }
    
    function addGroup()
    {
        $newGroup = new PluginTodoGroup();
        $newGroup->setId($this->_nextId());

        //$newGroup->setName(htmlentities($_POST['name'], ENT_QUOTES));
        $newGroup->setName(htmlspecialchars($_POST['name']));

        $newGroup->setExpanded(true);
        
        if ($_POST['selectedId'] == -1) {
            $this->tasks[] = $newGroup;
        } else {
            $this->tasks = $this->addTaskAfterId($this->tasks, $newGroup, $_POST['selectedId']);
        }
        $this->_storeTasks();
    }
    
    function addTask()
    {
        $newTask = new PluginTodoTask();
        $newTask->setId($this->_nextId());

        //$newTask->setName(htmlentities($_POST['name'], ENT_QUOTES));
        $newTask->setName(htmlspecialchars($_POST['name']));

        $newTask->setChecked(false);
        
        if ($_POST['selectedId'] == -1) {
            $this->tasks[] = $newTask;
        } else {
            $this->tasks = $this->addTaskAfterId($this->tasks, $newTask, $_POST['selectedId']);
        }
        $this->_storeTasks();
    }
    
    function addTaskAfterId($tasks, $newTask, $id)
    {
        $updatedTasks = array();
        foreach ($tasks as $task)
        {

            if ($task instanceof PluginTodoTask) {
                $updatedTasks[] = $task;
                if ($task->getId() == $id) {
                    $updatedTasks[] = $newTask;
                }
            } else {
                if ($task->getId() == $id) {
                    $task->addChild($newTask);
                } else {
                    $task->setChildren($this->addTaskAfterId($task->getChildren(), $newTask, $id));
                }
                $updatedTasks[] = $task;
            }
        }
        return $updatedTasks;
    }
    
    function addTaskToPosition($tasks, $newTask, $groupId, $index, $properGroup, &$added)
    {
        $updatedTasks = array();
        $pos = 0;
        if ($properGroup && $index == 0) {
            $updatedTasks[] = $newTask;
            $added = true;
        }
        foreach ($tasks as $task)
        {
            if ($properGroup && $pos++ == $index && !$added) {
                $updatedTasks[] = $newTask;
                $added = true;
            }

            if ($task instanceof PluginTodoTask) {
                $updatedTasks[] = $task;
            } else {
                $newProperGroup = ($groupId == $task->getId());
                $task->setChildren($this->addTaskToPosition($task->getChildren(), $newTask, $groupId, $index, $newProperGroup, $added));
                $updatedTasks[] = $task;
            }
        }
        if ($properGroup && !$added) {
            $updatedTasks[] = $newTask;
            $added = true;
        }
        return $updatedTasks;
    }
}

class PluginTodoTask
{
    var $id;
    var $name;
    var $checked = false;
    
    function TodoTask()
    {
    }
    
    function getId()
    {
        return $this->id;
    }
    
    function setId($id)
    {
        $this->id = $id;
    }
    
    function getName()
    {
        return $this->name;
    }
    
    function setName($name)
    {
        $this->name = $name;
    }
    
    function getChecked()
    {
        return $this->checked? 'true' : 'false';
    }
    
    function setChecked($checked)
    {
        $this->checked = $checked;
    }
    
    function getClass()
    {
        return $this->checked? 'complete' : '';
    }
}

class PluginTodoGroup
{
    var $id;
    var $name;
    var $children = array();
    var $expanded = true;
    
    function getId()
    {
        return $this->id;
    }
    
    function setId($id)
    {
        $this->id = $id;
    }
    
    function getName()
    {
        return $this->name;
    }
    
    function setName($name)
    {
        $this->name = $name;
    }
    
    function getExpanded()
    {
        return $this->expanded? 'true' : 'false';
    }
    
    function setExpanded($expanded)
    {
        $this->expanded = $expanded;
    }
    
    function getChildren()
    {
        return $this->children;
    }
    
    function setChildren($children)
    {
        $this->children = $children;
    }
    
    function addChild($child)
    {
        $this->children[] = $child;
    }
    
    function removeChild($id)
    {
        for ($i = 0; $i < sizeof($this->children); $i++) {
            if ($this->children[$i]->getId() == $id) {
                unset($this->children[$i]);
            }
        }
    }
}

?>
