<?php
require_once 'modules/admin/models/DashboardPlugin.php';
include_once "modules/home/models/FollowGroupsGateway.php" ;

class PluginTeamstatus extends DashboardPlugin
{
    var $hasTab = false;
    var $hasPanel = true;
    var $configJSFile = "PluginTeamStatusConfig.js";
    var $configFunction = "teamstatus.createWindow";
    var $name = "Team Status";
    var $description = "This allows you to view the status of your team and update your own status.";
    var $default = true;
    var $jsLibs = array(
        'plugins/dashboard/teamstatus/PluginTeamStatus.js'
    );
    
    var $apiSettings = array();
    
    function getTab()
    {
        return '';
    }
    
    function doAction()
    {
        $this->_loadSettings();
        switch($_REQUEST['pluginAction']) {
            case 'SaveAPIProvider':
                return $this->saveAPIProvider();
                break;
            case 'SaveAPISettings':
                return $this->saveAPISettings();
                break;
            case 'GetAPISettings':
                return $this->getAPISettings();
                break;
        }
    }
    
    function _loadSettings()
    {
        $setting = $this->settings->get("TeamStatusAPIInfo");
        if ($setting == ""){
            //create default api settings
            $this->apiSettings = array();
            $this->apiSettings['apiprovider'] = 0;
            $this->apiSettings['vars'] = array(
                "url" => '',
                "token" => '',
                "room" => ''
            );
        } else {
            $this->apiSettings = unserialize($setting);
        }
    }
    
    function getAPISettings()
    {
        $dataArray["error"] = false;
        $dataArray["success"] = true;
        $dataArray["message"] = "";
        $dataArray["data"] = $this->apiSettings;
        echo NE_Lib::jsonencode($dataArray);
    }
    
    function saveAPISettings()
    {
        $savedProvider = $this->apiSettings['apiprovider'];
        $this->apiSettings = array(); //clear out anything we had before
        $this->apiSettings['apiprovider'] = $savedProvider;
        if (!isset($_REQUEST['url'])) $_REQUEST['url'] = "";
        $this->apiSettings['vars'] = array(
            "url" => $_REQUEST['url'],
            "token" => $_REQUEST['token'],
            "room" => $_REQUEST['room']
        );
        $this->settings->updateValue("TeamStatusAPIInfo",serialize($this->apiSettings));
        
        $dataArray["error"] = false;
        $dataArray["success"] = true;
        $dataArray["message"] = "";
        echo NE_Lib::jsonencode($dataArray);
    }
    
    function saveAPIProvider()
    {
        $this->apiSettings = array();
        $this->apiSettings['apiprovider'] = $_REQUEST['providerid'];
        $this->apiSettings['vars'] = array(
            "url" => '',
            "token" => '',
            "room" => ''
        );
        $this->settings->updateValue("TeamStatusAPIInfo",serialize($this->apiSettings));

        $dataArray["error"] = false;
        $dataArray["success"] = true;
        $dataArray["message"] = "";
        echo NE_Lib::jsonencode($dataArray);
    }
    
    function getPanel()
    {
        // Set the plugin template
        $this->tpl->define(array('teamstatus' => '../../plugins/dashboard/teamstatus/PluginTeamStatus.tpl'));
        
        // Get what groups the user is following
        $gateway = new FollowGroupsGateway();
        $followGroups = $gateway->getFollowGroups($this->user->getId());
        
        while(list($id, $group,$follow) = $followGroups->fetch()){
            if($follow){
                // Add to the array if the user is following the group
                $this->tpl->setKey(array(
                    'groupvalue'    =>  $id,
                    'groupselected' =>  '',
                    'groupname'     =>  $group,
                ));
                
                // Parse the dropdown
                $this->tpl->parse('teamstatusgroups_out', '.teamstatusgroups');
            }
        }
        
        // Parse and output the whole template
        $this->tpl->parse('teamstatus_out', 'teamstatus');
        return $this->tpl->fetch('teamstatus_out');
    }
}
?>
