<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
/*********************************************************************************
 * The contents of this file are subject to the SugarCRM Public License Version
 * 1.1.3 ("License"); You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.sugarcrm.com/SPL
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * All copies of the Covered Code must include on each user interface screen:
 *    (i) the "Powered by SugarCRM" logo and
 *    (ii) the SugarCRM copyright notice
 * in the same form as they appear in the distribution.  See full license for
 * requirements.
 *
 * The Original Code is: SugarCRM Open Source
 * The Initial Developer of the Original Code is SugarCRM, Inc.
 * Portions created by SugarCRM are Copyright (C) 2004-2006 SugarCRM, Inc.;
 * All Rights Reserved.
 * Contributor(s): ______________________________________.
 ********************************************************************************/
/*********************************************************************************

 * Description:  TODO: To be written.
 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
 * All Rights Reserved.
 * Contributor(s): ______________________________________..
 ********************************************************************************/

/************** general UI Stuff *************/
require_once('include/Sugar_Smarty.php');
require_once('data/Tracker.php');
require_once('modules/Administration/Administration.php');

global $mod_strings;
global $app_list_strings;
global $app_strings;
global $current_user;

//if (!is_admin($current_user)) sugar_die("Unauthorized access to administration.");
//account for use within wizards
if(!isset($_REQUEST['inline']) || $_REQUEST['inline'] != 'inline'){
    echo "\n<p>\n";
    echo "<h2>".get_module_title($mod_strings['LBL_MODULE_NAME'],$mod_strings['LBL_CAMPAIGN_DIAGNOSTICS'],true)."</h2>";
    echo "\n</p>\n";
}

global $theme;
global $currentModule;
$theme_path = "themes/".$theme."/";
$image_path = $theme_path."images/";
require_once($theme_path.'layout_utils.php');


if(isset($_REQUEST['inline']) && $_REQUEST['inline'] == 'inline'){
    {

}
}else{
    //use html if not inline
    $ss = new Sugar_Smarty();
    $ss->assign("MOD", $mod_strings);
    $ss->assign("APP", $app_strings);
    if (isset($_REQUEST['return_module'])) $ss->assign("RETURN_MODULE", $_REQUEST['return_module']);
    if (isset($_REQUEST['return_action'])) $ss->assign("RETURN_ACTION", $_REQUEST['return_action']);
    if (isset($_REQUEST['return_id'])) $ss->assign("RETURN_ID", $_REQUEST['return_id']);
    // handle Create $module then Cancel
    if (empty($_REQUEST['return_id'])) {
        $ss->assign("RETURN_ACTION", 'index');
    }
}

/************  EMAIL COMPONENTS *************/
//monitored mailbox section
$focus = new Administration();
$focus->retrieveSettings(); //retrieve all admin settings.


//run query for mail boxes of type 'bounce' 
$email_health = 0;
$email_components = 2;
$mbox_qry = "select * from inbound_email where deleted ='0' and mailbox_type = 'bounce'";
$mbox_res = $focus->db->query($mbox_qry);
$mboxTable = "<table border ='0' width='100%'  class='tabDetailView' cellpadding='0' cellspacing='0'>";
//put all rows returned into an array
$mbox = array();
while ($mbox_row = $focus->db->fetchByAssoc($mbox_res)){$mbox[] = $mbox_row;}
    $mbox_msg = ' ';
//if the array is not empty, then set "good" message
if(isset($mbox) && count($mbox)>0){
    $mboxTable .= "<tr><td colspan='5' style='text-align: left;'><b>" .count($mbox) ." ". $mod_strings['LBL_MAILBOX_CHECK1_GOOD']." </b>.</td></tr>";
        $mboxTable .= "<tr><td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_MAILBOX_NAME']."</b></td>"
                   .  " <td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_LOGIN']."</b></td>"
                   .  " <td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_MAILBOX']."</b></td>" 
                   .  " <td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_SERVER_URL']."</b></td>"
                   .  " <td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_LIST_STATUS']."</b></td></tr>";

    foreach($mbox as $details){
        $mboxTable .= "<tr><td>".$details['name']."</td>";
        $mboxTable .= "<td class='tabDetailViewDL2'>".$details['email_user']."</td>";
        $mboxTable .= "<td class='tabDetailViewDL2'>".$details['mailbox']."</td>";
        $mboxTable .= "<td class='tabDetailViewDL2'>".$details['server_url']."</td>";
        $mboxTable .= "<td class='tabDetailViewDL2'>".$details['status']."</td></tr>";
    }

}else{
    //if array is empty, then set "bad" message and increment health counter
    $mboxTable .=  "<tr><td colspan='5' class='tabDetailViewDF'><b class='error'>". $mod_strings['LBL_MAILBOX_CHECK1_BAD']."</b></td></tr>";
    $email_health =$email_health +1;
}

$mboxTable.= '</table>' ;


    
$ss->assign("MAILBOXES_DETECTED_MESSAGE", $mboxTable);

//email settings configured 
$conf_msg="<table border='0' width='100%' class='tabDetailView' cellpadding='0' cellspacing='0'>";
if (strstr($focus->settings['notify_fromaddress'], 'example.com')){
    //if from address is the default, then set "bad" message and increment health counter
    $conf_msg .= "<tr><td colspan = '5' class='tabDetailViewDF'><b class='error'> ".$mod_strings['LBL_MAILBOX_CHECK2_BAD']." </b></td></td>";
    $email_health =$email_health +1;
}else{
    $conf_msg .= "<tr><td colspan = '5' class='tabDetailViewDF'><b> ".$mod_strings['LBL_MAILBOX_CHECK2_GOOD']."</b></td></tr>";
    $conf_msg .= "<tr><td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_WIZ_FROM_NAME']."</b></td>"
               .  " <td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_WIZ_FROM_ADDRESS']."</b></td>" 
               .  " <td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_MAIL_SENDTYPE']."</b></td>";
    if($focus->settings['mail_sendtype']=='SMTP'){
     $conf_msg .= " <td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_MAIL_SMTPSERVER']."</b></td>" 
               .  " <td width='20%' class='tabDetailViewDF'><b>".$mod_strings['LBL_MAIL_SMTPUSER']."</b></td></tr>";

    }else{$conf_msg .= "</tr>";}
                   
    

        $conf_msg .= "<tr class='tabDetailViewDL2'><td>".$focus->settings['notify_fromname']."</td>";
        $conf_msg .= "<td>".$focus->settings['notify_fromaddress']."</td>";
        $conf_msg .= "<td>".$focus->settings['mail_sendtype']."</td>";
     if($focus->settings['mail_sendtype']=='SMTP'){
        $conf_msg .= "<td>".$focus->settings['mail_smtpserver']."</td>";
        $conf_msg .= "<td>".$focus->settings['mail_smtpuser']."</td></tr>";

    }else{$conf_msg .= "</tr>";}       

}
          
$conf_msg .= '</table>'; 
$ss->assign("EMAIL_SETTINGS_CONFIGURED_MESSAGE", $conf_msg);
$email_setup_wiz_link='';
if ($email_health>0){
    if (is_admin($current_user)){
        $email_setup_wiz_link="<a href='index.php?module=Campaigns&action=WizardEmailSetup'>".$mod_strings['LBL_EMAIL_SETUP_WIZ']."</a>";
    }else{
        $email_setup_wiz_link=$mod_strings['LBL_NON_ADMIN_ERROR_MSG'];
    }    
}

$ss->assign("EMAIL_SETUP_WIZ_LINK", $email_setup_wiz_link);
$ss->assign( 'EMAIL_IMAGE', define_image($email_health, 2,$image_path));

/************* SCHEDULER COMPONENTS ************/

//create and run the scheduler queries 
$sched_qry = "select job, name, status from schedulers where deleted = 0 and status = 'Active'";
$sched_res = $focus->db->query($sched_qry);
$sched_health = 0;
$sched = array();
$check_sched1 = 'function::runMassEmailCampaign';
$check_sched2 = 'function::pollMonitoredInboxesForBouncedCampaignEmails';
$sched_mes = '';
$sched_mes_body = '';

$scheds = array();
//build the table rows for scheduler display
while ($sched_row = $focus->db->fetchByAssoc($sched_res)){$scheds[] = $sched_row;}
foreach ($scheds as $funct){
  if( ($funct['job']==$check_sched1)  ||   ($funct['job']==$check_sched2)){
        $sched_mes = 'use';
        $sched_mes_body .= "<tr><td class='tabDetailViewDL2' style='text-align: left;'>".$funct['name']."</td>";
        $sched_mes_body .= "<td class='tabDetailViewDL2' style='text-align: left;'>".$funct['status']."</td></tr>";
        if($funct['job']==$check_sched1){
            $check_sched1 ="found";
        }else{
            $check_sched2 ="found";
        }  
        
  }
}

//determine which table header to use, based on whether or not schedulers were found
$show_admin_link = false;
if($sched_mes == 'use'){
    $sched_mes = "<h5>".$mod_strings['LBL_SCHEDULER_CHECK_GOOD']."</h5><br><table class='tabDetailView2' border='0' width='100%' cellpadding='0' cellspacing='1'>";
    $sched_mes .= "<tr><td width='40%' class='tabDetailViewDF2'><b>".$mod_strings['LBL_SCHEDULER_NAME']."</b></td>"
               .  " <td width='60%' class='tabDetailViewDF2'><b>".$mod_strings['LBL_SCHEDULER_STATUS']."</b></td></tr>";
            
}else{
    $sched_mes = "<table class='tabDetailView2' border='0' width='100%' cellpadding='0' cellspacing='1'>";
    $sched_mes  .= "<tr><td colspan ='3' class='tabDetailViewDL2'><font color='red'><b> ".$mod_strings['LBL_SCHEDULER_CHECK_BAD']."</b></font></td></tr>";
    $show_admin_link = true;
}

//determine if error messages need to be displayed for schedulers
if($check_sched1 != 'found'){
    $sched_health =$sched_health +1;
    $sched_mes_body  .= "<tr><td colspan ='3' class='tabDetailViewDL2'><font color='red'> ".$mod_strings['LBL_SCHEDULER_CHECK1_BAD']."</font></td></tr>";
}
if($check_sched2 != 'found'){
    $sched_health =$sched_health +1;
    $sched_mes_body  .= "<tr><td colspan ='3' class='tabDetailViewDL2'><font color='red'>".$mod_strings['LBL_SCHEDULER_CHECK2_BAD']."</font></td></tr>";
}
$admin_sched_link='';
if ($sched_health>0){
    if (is_admin($current_user)){
        $admin_sched_link="<a href='index.php?module=Schedulers&action=index'>".$mod_strings['LBL_SCHEDULER_LINK']."</a>";
    }else{
     $admin_sched_link=$mod_strings['LBL_NON_ADMIN_ERROR_MSG'];   
    }    
}    

//put table html together and display
    $final_sched_msg = $sched_mes . $sched_mes_body . '</table>' . $admin_sched_link;        
    $ss->assign("SCHEDULER_EMAILS_MESSAGE", $final_sched_msg);
    $ss->assign( 'SCHEDULE_IMAGE', define_image($sched_health, 2,$image_path));


/********** FINAL END OF PAGE UI Stuff ********/
if(!isset($_REQUEST['inline']) || $_REQUEST['inline'] != 'inline'){

      $ss->display('modules/Campaigns/CampaignDiagnostic.html');
}

/*
 * This function takes in 3 parameters and determines the appropriate image source.  
 * $total Parameter is the total number things being checked.
 * $num parameter is the "health" parameter being tracked whenever there is something wrong.  (higher number =bad)
 * $image_path is simply the image path
 * */

function define_image($num, $total, $image_path){
    //if health number is equal to total number then all checks failed, set red image
    if($num == $total){
        //red
        return "<img src='".$image_path."red_camp.gif' align='absmiddle'> ";
    }elseif($num == 0){
        //if health number is zero, then all checks passed, set green image
        //green
       return "<img src='".$image_path."green_camp.gif' align='absmiddle' > ";
    }else{
        //if health number is between total and num params, then some checks failed but not all, set yellow image
        //yellow
        return "<img src='".$image_path."yellow_camp.gif' align='absmiddle' > ";
    }
}
    
?>
