<?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 $configFunction = "RedirectServicePlugin";
    var $description = /*T*/"This plugin adds a dashboard panel which summarizes the latest status of your automated services."/*/T*/;
    var $default = false;
    var $jsLibs         = array('plugins/dashboard/servicestatus/PluginServicestatusConfig.js');

    function getTab()
    {
        return '';
    }
    
    function getPanel()
    {
        
        $plugins = new NE_PluginCollection('services', $this->user);
        $this->tpl->define(array('plugin_table' => '../../plugins/dashboard/servicestatus/PluginServicestatus.tpl'));
        
        $noneEnabled = true;
        while ($plugin = $plugins->getNext()) {
            if (is_null($this->settings->get("plugin_".$plugin->getInternalName()."_Enabled")) || $this->settings->get("plugin_".$plugin->getInternalName()."_Enabled") == '0') {
                continue;
            }
            $noneEnabled = false;
            //create default warning which is actualy status good            
            $errors = false;
            $statusPopup = "";

            $this->tpl->assign('SERVICE_SETTINGID', $plugin->getInternalName());
            $this->tpl->assign('SERVICE_NAME', $this->settings->get("plugin_".$plugin->getInternalName()."_Plugin Name"));
            if ($this->user->getGroupID() == ROLE_SUPERADMIN) {
                $this->tpl->parse('trash', 'linkToService');
            } else {
                $this->tpl->parse('trash', 'serviceName');
            }

            $query = "SELECT UNIX_TIMESTAMP(time) as time, manually_executed, results, errors "
                    ."FROM services_log "
                    ."WHERE plugin=? AND (results != '' OR errors != '') "
                    ."ORDER BY time DESC LIMIT 1";
       
            $dbResult = $this->db->query($query, $plugin->getInternalName());
            if ($row = $dbResult->fetch()) {
                $stringTime = str_replace(" ", "&nbsp;", date($this->settings->get('Date Format').' H:i', $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/>";
            }

            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" />';
            }

            if ($output = $plugin->dashboard()) {
                $statusPopup .=$output;
            }

            $this->tpl->assign('SERVICE_WARNING', NE_Lib::returnPopUpString($statusPopup, $this->settings, "",$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_".$plugin->getInternalName()."_Run schedule - Minute"),
                                               $this->settings->get("plugin_".$plugin->getInternalName()."_Run schedule - Hour"),
                                               $this->settings->get("plugin_".$plugin->getInternalName()."_Run schedule - Day"),
                                               $this->settings->get("plugin_".$plugin->getInternalName()."_Run schedule - Month"),
                                               $this->settings->get("plugin_".$plugin->getInternalName()."_Run schedule - Day of the week"));

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

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


            if ($this->user->hasPermission('admin_manage_plugins')) {
                $this->tpl->parse('trash', 'serviceActionLinks');
            }


            $this->tpl->parse('serviceblock_out','.serviceblock');
        }

        //If has permission to add more services
        //$this->tpl->parse('addmoreservices_out','addmoreservices');

        if ($noneEnabled) {
            $this->tpl->parse('noplugins_out', 'noplugins');
        }
        
        $this->tpl->parse('allPlugins', '.plugin_table');   
        return $this->tpl->fetch('allPlugins');
    }
}

?>
