<?php
require_once 'modules/admin/models/DashboardPlugin.php';
require_once 'newedge/classes/NE_PluginCollection.php';
require_once 'newedge/classes/NE_Crontab.php';

class PluginServicestatus extends DashboardPlugin
{
    var $hasTab = false;
    var $hasPanel = true;
    var $name = "Service Status";
    var $configJSFile = "PluginServicestatusConfig.js";
    var $configFunction = "RedirectServicePlugin";
    var $description = /*T*/"This plugin adds a dashboard panel which summarizes the latest status of your automated services."/*/T*/;
    var $default = false;

    function getTab()
    {
        return '';
    }
    
    function getPanel()
    {
        include_once ("modules/admin/models/PluginGateway.php");        
        
        $this->tpl->define(array('plugin_table' => '../../plugins/dashboard/servicestatus/PluginServicestatus.tpl'));
        $plugins = new NE_PluginCollection('services', $this->user);
        $noneEnabled = true;
        while ($plugin = $plugins->getNext()) {
            $name = $plugin->getInternalName();                   
            
            if (is_null($enabled=$this->settings->get("plugin_{$name}_Enabled",true)) || $enabled == '0') {
                continue;
            }
            // If we get this far, at least one plugin is enabled.
            $noneEnabled = false;
          
            // Get info on last service run            
            try {
            	$tInfo = $this->settings->get('service_' . $name . '_info',false,false,false);
            	if (trim($tInfo) == "") { $row = array(); }
            	else { $row = unserialize($tInfo); }
            } catch (Exception $ex) {
            	$row = array();
            }
            //create default warning which is actualy status good            
            $errors = false;
            $statusPopup = "";
        
            $this->tpl->assign('SERVICE_SETTINGID', $name);
            $this->tpl->assign('SESSIONHASH', md5(session_id()));
            $this->tpl->assign('SERVICE_NAME', $this->settings->get("plugin_{$name}_Plugin Name",true));
            if ($this->user->getGroupID() == ROLE_SUPERADMIN) {
                $this->tpl->parse('trash', 'linkToService');
            } else {
                $this->tpl->parse('trash', 'serviceName');
            }
          
            if ( isset($row['time']) && strlen($row['time']) > 0 ) {
                $stringTime = str_replace(" ", "&nbsp;", date($this->settings->get('Date Format').' H:i', strtotime($row['time'])));
                $stringLastRun = $stringTime;
                if($row['manually_executed'] == 1){
                    $stringLastRun .= ' '.$this->user->lang('ran manually');
                } elseif ($row['manually_executed'] == 2){
                    $stringLastRun .= ' '.$this->user->lang('ran from URL');
                }

                $this->tpl->assign('SERVICE_LASTRUN', $stringLastRun);
                //$statusPopup .= nl2br($row['results']);

                if (trim($row['errors'])) {
                    //$statusPopup .= "<b>{$stringTime}: </b> " . nl2br(trim($row['errors']));
                    $errors = true;
                }

            } else {
                $this->tpl->assign('SERVICE_LASTRUN', $this->user->lang('Never'));
                //$statusPopup .= $this->user->lang('This service has yet to run')."<br/>";
            }
			            
			//this should be done via ajax later
			/*          
            $gateway = new PluginGateway($this->user);
            $plugin = $gateway->getServicePluginByName($name);
            if ( $plugin ) {
                if ($output = $plugin->dashboard())  {
                    $statusPopup .= $output;
                }
            }*/

            if ($errors) {
                $statusImg = '<img src="../templates/admin/images/icon_alert.gif" width="16" height="15" border="0" />';
            }else{
                $statusImg = '<img src="../templates/admin/images/servicestatusgood.png" border="0" />';
            }

            $this->tpl->assign('SERVICE_WARNING', $statusImg);

            if (strtoupper(mb_substr(PHP_OS, 0, 3)) == 'WIN') {
                $nextRun = $this->user->lang('Manual Only');
            } else {
                $nextRun = NE_Crontab::getNextRun($this->settings->get("plugin_{$name}_Run schedule - Minute",true),
                $this->settings->get("plugin_{$name}_Run schedule - Hour",true),
                $this->settings->get("plugin_{$name}_Run schedule - Day",true),
                $this->settings->get("plugin_{$name}_Run schedule - Month",true),
                $this->settings->get("plugin_{$name}_Run schedule - Day of the week",true));

                if (!$nextRun) {
                    $nextRun = $this->user->lang('Incorrect format');
                } else {
                    $nextRun = date($this->settings->get('Date Format').' H:i', $nextRun);
                }
            }

            $nextrunImg = '<img title="'.$this->user->lang("Next Run").": ".$nextRun.'" src="../templates/admin/images/clock.png" border="0" />';
            $this->tpl->assign('POPUPNEXTRUN', $nextrunImg);

            if ($this->user->hasPermission('admin_manage_plugins')) {
                $this->tpl->parse('trash', 'serviceActionLinks');
            }
            $this->tpl->parse('serviceblock_out','.serviceblock');
            
        }
         
        if ($noneEnabled) {
            $this->tpl->parse('noplugins_out', 'noplugins');
        }
        
        $this->tpl->parse('allPlugins', '.plugin_table');           
        
        return $this->tpl->fetch('allPlugins');
        
    }
}
?>