<?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()
    {
        
        $this->tpl->define(array('plugin_table' => '../../plugins/dashboard/servicestatus/PluginServicestatus.tpl'));
		$info = unserialize($this->settings->get('services_meta_info'));

		$noneEnabled = false;
		if ( !$info )
			$noneEnabled = true;
		else {
			foreach ( $info as $name => $row )
			{
				// Ensure we only show plugins that are enabled.
				if (is_null($this->settings->get("plugin_{$name}_Enabled",true)) || $this->settings->get("plugin_{$name}_Enabled",true) == '0') {
					continue;
				}

				//create default warning which is actualy status good            
				$errors = false;
				$statusPopup = "";

				$this->tpl->assign('SERVICE_SETTINGID', $name);
				$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 ( 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/>";
				}

				include_once ("modules/admin/models/PluginGateway.php");
				$gateway = new PluginGateway($this->user);
				$plugin = $gateway->getServicePluginByName($name);
				if ( $plugin ) {
					if ($output = $plugin->dashboard())  {
						$statusPopup .= $output;
					}
				}

				if ($errors) {
					$statusImg = '<img style="cursor:pointer;" qtip="'.$statusPopup.'" src="../templates/admin/images/icon_alert.gif" width="16" height="15" border="0" />';
				}else{
					$statusImg = '<img style="cursor:pointer;" qtip="'.$statusPopup.'" 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 style="cursor:pointer;" qtip="'.$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');
	}
}
?>