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

class PluginPhpsysinfoDashboard extends DashboardPlugin
{

    var $hasTab = true;
    var $hasPanel = true;
    var $name = "Server Info";
    var $description = "This shows your current server status based off the phpsysinfo plugin.";
    var $default = false;

    protected $featureSet = 'products';

    function getTab()
    {
        return $this->getPanel();
    }
    
    function getPanel()
    {
        // dependent on the snapin plugin
        @include_once 'plugins/snapins/phpsysinfo/PluginPhpsysinfo.php';
        
        if (!class_exists('PluginPhpsysinfo')) {
            return 'Phpsysinfo is not installed.';
        }
        
        $phpsysinfo = new PluginPhpsysinfo($this->user,1);

        $this->tpl->define(array('dashboard_table' => '../../plugins/snapins/phpsysinfo/dashboard_table.tpl'));

        $serverGateway = new ServerGateway();
        $result = $serverGateway->getStatsUrlDBIT($this->user);
        $results = '';
        $workingServerFound = false;
        while ($row = $result->fetch()) {
            $data = $phpsysinfo->getDashboardData($row['statsurl']);
            if (is_a($data, 'NE_Error')) continue;
            $workingServerFound = true;
            $this->tpl->assign(array(
                'SERVERSTATUS_FUSE'         => $this->user->isAdmin()? 'admin' : 'clients',
                'SERVERSTATUS_ACTION'       => $this->user->isAdmin()? 'Snapins' : 'ShowSnapinsCustomer',
                'SERVERSTATUS_SERVERID'     => $row['id'],
                'SERVERSTATUS_SERVERNAME'   => $row['name'],
                'SERVERSTATUS_LOADAVERAGES' => $data['loadAverages'],
                'SERVERSTATUS_UPTIME'       => $data['uptime'],
                'SERVERSTATUS_MEMORYUSED'   => $data['memoryUsedPercent'],
                'SERVERSTATUS_MEMORYCACHED' => $data['memoryCached'],
            ));

            $this->tpl->unsetkey('SERVERSTATUS_MOUNTSUSED');
            $numMounts = count($data['mounts']);
            for ($i = 0; $i < $numMounts; $i++) {
                if ($i == 0) {
                    $this->tpl->setKey('SERVERSTATUS_MOUNTSUSED',
                                 $phpsysinfo->_getMountPoint($data['mounts'][$i])
                                 .': '.$phpsysinfo->_getMountPercent($data['mounts'][$i]).'%');
                } else {
                    $this->tpl->append('SERVERSTATUS_MOUNTSUSED',
                                 $phpsysinfo->_getMountPoint($data['mounts'][$i])
                                 .': '.$phpsysinfo->_getMountPercent($data['mounts'][$i]).'%');
                }
                if ($i < ($numMounts - 1)) {
                    $this->tpl->append('SERVERSTATUS_MOUNTSUSED', ',  ');
                }
            }

            $this->tpl->parse('dashboard_table', 'dashboard_table');
            $results .= $this->tpl->fetch('dashboard_table');
        }
        
        if ($results) {
            return $results;
        }

        if (!$result->getNumRows() || !$workingServerFound) {
            $this->tpl->parse('noServers_out', 'noServers');
            return $this->tpl->fetch('noServers_out');
        }
    }
    
    function doAction()
    {
        
    }
}

?>
