<?
/**
 * VoiceOne Settings/System/IO
 *
 * Frontend to provide access to the IO system in voiceone. It allows to
 * turn on/off pbx service and to turn off or reboot the pc.  
 * @package voiceone
 * @subpackage  settings_system_io
 */
 
  include ("../../../admin/config/cfg.php");
  global $cwlang, $cwLanguage;
  require_once("$absBasePath/lib/xajax/xajax.inc.php");
  
  //================ XAJAX ================
  function rollingAndDo($target, $action){
    global $relBasePath, $cwlang;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $html="<table><tr>";
    $html.=" <td><img src=\"".$relBasePath."public/img/ico-progress.gif"."\" alt=\"running\" /></td>";
    //PBX ACTION
    if($target=="pbx"){
      if($action=="start") $html.=" <td><p>".T_("Starting PBX...")."</p></td>";
      else if ($action=="stop") $html.=" <td><p>".T_("Stopping PBX...")."</p></td>";
      $html.=" </tr></table>";
      $objResponse->addAssign("pbxrolling","innerHTML", $html);
      $newimg=" document.getElementById('tlpbx').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
      $objResponse->addScript($newimg);
    }
    //SYSTEM ACTION
    else if($target=="system"){
      if($action=="halt") $html.=" <td><p>".T_("Shutting down System...")."</p></td>";
      else if ($action=="reboot") $html.=" <td><p>".T_("Rebooting System...")."</p></td>";
      $html.=" </tr></table>";
      $objResponse->addAssign("systemrolling","innerHTML", $html);
      $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
      $objResponse->addScript($newimg);
    }
    $script=" xajax_action('".$target."','".$action."') ";
    $objResponse->addScript($script);
    // Return the xajaxResponse object
    return $objResponse->getXML();
  }
  function action($target, $action){
    global $relBasePath, $asterisk;
    // Instantiate the xajaxResponse object
    $objResponse = new xajaxResponse();
    // Build response
    $ret="";
    if($target=="pbx"){
      if ($action=="start") $ret=$asterisk->pbxSwitch("start");  
      else $ret=$asterisk->pbxSwitch("stop"); 
      if($ret=="ON"){
        //modify I/O page
        $newimg=" document.getElementById('tlpbx').src=\"".$relBasePath."/public/img/tl-green.gif\"; ";
        $objResponse->addScript($newimg);
        $objResponse->addAssign("send","disabled","");
        //modify template
        $tplimg=" document.getElementById('tplpbxsytatus').src=\"".$relBasePath."/public/img/tl-green-small.gif\"; ";
        $objResponse->addScript($tplimg);
        //reset rolling
        $objResponse->addAssign("pbxrolling","innerHTML", ""); 
      }else if($ret=="OFF"){
        //modify I/O page
        $newimg=" document.getElementById('tlpbx').src=\"".$relBasePath."/public/img/tl-red.gif\"; ";
        $objResponse->addScript($newimg);
        $objResponse->addAssign("send","disabled","disabled");
        //modify template
        $tplimg=" document.getElementById('tplpbxsytatus').src=\"".$relBasePath."/public/img/tl-red-small.gif\"; ";
        $objResponse->addScript($tplimg);
        //reset rolling
        $objResponse->addAssign("pbxrolling","innerHTML", ""); 
      }else{
        $newimg=" document.getElementById('tlpbx').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
        $objResponse->addScript($newimg);
        $objResponse->addAssign("pbxrolling","innerHTML", "<p color=\"#FF0000\"> ERROR </p>");
      }
    }
    else if($target=="system"){
      if($action=="halt"){
        $ret=$asterisk->shutdown("halt");
        if($ret>0){
          $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-red.gif\"; ";
          $objResponse->addScript($newimg);
          $objResponse->addAssign("systemrolling","innerHTML", "");
        }else{
          $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
          $objResponse->addScript($newimg);
          $objResponse->addAssign("systemrolling","innerHTML", "<p color=\"#FF0000\"> ERROR </p>");
        }
      }else if($action=="reboot"){
        $ret=$asterisk->shutdown("reboot");
        if($ret>0){
          $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-red.gif\"; ";
          $objResponse->addScript($newimg);
          $objResponse->addAssign("systemrolling","innerHTML", "");
        }else{
          $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
          $objResponse->addScript($newimg);
          $objResponse->addAssign("systemrolling","innerHTML", "<p color=\"#FF0000\"> ERROR </p>");
        }
      }else{
        $newimg=" document.getElementById('tlsystem').src=\"".$relBasePath."/public/img/tl-yellow.gif\"; ";
        $objResponse->addScript($newimg);
        $objResponse->addAssign("systemrolling","innerHTML", "<p color=\"#FF0000\"> ERROR </p>");
      }
    }
    // Return the xajaxResponse object
    return $objResponse->getXML();  
  }
  $xajax = new xajax();
  $xajax->registerFunction("rollingAndDo");
  $xajax->registerFunction("action");
  $xajax->processRequests();
  //================= POST & GET ==================
  $alert=getAlert();
  if((count($_POST)>0)&&(_isModuleLoaded('network'))&&(isset($_POST["save"]))&&($_POST["save"]==1)){
    $ret=$asterisk->saveConfPerm();
    if($ret>0) $alert="<div class=\"AlertGood\">".T_("Configuration successfully saved")."</div>";
    else $alert="<div class=\"AlertGood\">".T_("An error has occurred during save procedure")."</div>";
    //go to main page
    goToMainPage($alert,"./");
  }
  //================= PAGE LOGIC ==================
  // set pbx traffic light
  $status=$asterisk->pbxSwitch("status");
  if($status=="ON") $pbximg="<img id=\"tlpbx\" src=\"".$relBasePath."/public/img/tl-green.gif\">";
  else if($status=="OFF") $pbximg="<img id=\"tlpbx\" src=\"".$relBasePath."/public/img/tl-red.gif\">";
  else $pbximg="<img id=\"tlpbx\" src=\"".$relBasePath."/public/img/tl-yellow.gif\">";
  // set system traffic light
  $systemimg="<img id=\"tlsystem\" src=\"".$relBasePath."/public/img/tl-green.gif\">";
  //get the public key
  $key=nl2br($asterisk->getPublicKey());
  //================ MAIN CONTENTS ================
  ob_start();
  $xajax->printJavascript("../../../admin/lib/xajax/"); ?>  
  <?$sel="io";include("../navigation.php");?> 
  <div class="SectionHeader">
    <?=$alert?>
    <h1><?=T_("Server Management")?></h1>
  </div>
  <div id="basic_form">
    <? if(_isModuleLoaded('network')){ ?>
      <form id="mainform" action="<?=$PHP_SELF?>" method="post" onsubmit="document.getElementById('saveroll').style.display='inline';">
        <p class="blockintro"><?=nl2br(T_(
        "A scheduled procedure saves all system configuration chenges in the permanent memory every night. \n". 
        "Reboot or shutdown the system cause the saving procedure to be executed but, ".
        "if system crash or power supply is turned off, every changes since last permanent save will be lost. \n".
        "To execute now the saving procedure press the botton below (operation may takes few minutes)."))?></p>
        <div class="block">
          <dl>
            <dt class="required" style="width:200px;"><label><?=T_("Force save procedure")?>:</label></dt>
            <dd style="text-align:center;">
              <input type="hidden" id="save" name="save" value="1"/>
              <input type="submit" style="cursor:pointer;vertical-align:bottom;" value="Save Configuration Permanently"/>
              <div id="saveroll" style="display:none;vertical-align:middle;line-height:20px;margin-left:20px;">
                <img src="<?=$relBasePath?>public/img/ico-progress.gif" alt="running"/>
              </div>
            </dd>
          </dl>
        </div>
      </form>
    <? } ?>
    <table>
      <tr>
        <!-- PBX -->
        <td style="width: 50%">
          <h2><?=T_("PBX")?></h2>
          <p class="blockintro"><?=T_("You can Start or Stop pbx service")?></p>
          <div class="block">
            <table class="block">
              <tr>
                <td>
                  <table style="width: 10%">
                    <tr>
                      <td rowspan="2"><?=$pbximg?></td>
                      <td><a href="#"><img src="<?=$relBasePath?>/public/img/shutdown.png" onclick="if (confirm('<?=T_("Are you sure to STOP PBX?")?>')) xajax_rollingAndDo('pbx','stop');" ></a></td><td><a href="#" onclick="if (confirm('<?=T_("Are you sure to STOP PBX?")?>')) xajax_rollingAndDo('pbx','stop');"><?=T_("Stop")?></a></td>
                    </tr>
                    <tr>
                      <td><a href="#"><img src="<?=$relBasePath?>/public/img/ok.gif" onclick="if (confirm('<?=T_("Are you sure to START PBX?")?>')) xajax_rollingAndDo('pbx','start'); else return false;" ></a></td><td><a href="#" onclick="if (confirm('<?=T_("Are you sure to START PBX?")?>')) xajax_rollingAndDo('pbx','start');"><?=T_("Start")?></a></td>
                    </tr>
                  </table>
                </td>
                <td id="pbxrolling"></td>
              </tr>
            </table>
          </div>
        </td>
        <!-- System -->
        <td style="width: 50%">
          <h2><?=T_("System")?></h2>
          <p class="blockintro"><?=T_("You can Reboot or Shutdown the server")?></p>
          <div class="block">
            <table class="block">
              <tr>
                <td>
                  <table style="width: 10%">
                    <tr>
                      <td rowspan="2"><?=$systemimg?></td>
                      <td><a href="#"><img src="<?=$relBasePath?>/public/img/shutdown.png" onclick="if (confirm('<?=T_("Are you sure to TURN OFF the ystem?")?>')) xajax_rollingAndDo('system','halt');" ></a></td><td><a href="#" onclick="if (confirm('<?=T_("Are you sure to TURN OFF the system?")?>')) xajax_rollingAndDo('system','halt');"><?=T_("Shutdown")?></a></td>
                    </tr>
                    <tr>
                      <td><a href="#"><img src="<?=$relBasePath?>/public/img/reboot.png" onclick="if (confirm('<?=T_("Are you sure to REBOOT the system?")?>')) xajax_rollingAndDo('system','reboot');" ></a></td><td><a href="#" onclick="if (confirm('<?=T_("Are you sure to REBOOT the system?")?>')) xajax_rollingAndDo('system','reboot');"><?=T_("Reboot")?></a></td>
                    </tr>
                  </table>
                </td>
                <td id="systemrolling"></td>
              </tr>
            </table>
          </div>
        </td>        
      </tr>
    </table>
    <!-- Public key -->
    <h2><?=T_("Public key")?></h2>
    <p><?=$key?></p>  
  </div>
  <?
  $contents=ob_get_contents();
  ob_end_clean();

  //================ SIDEBAR ================
  ob_start(); 
  ?>
    <div class="Sidebar">
      <h1><?=T_("Informations")?></h1>
          <p><?=T_("This interface allow you to manage the server and the PBX service. The states are represented with traffic lights:")?></p>
      <table id="trafficlight" cellspacing="1">
        <tr>
          <td><img align="middle" src="<?=$relBasePath?>/public/img/tl-red.gif"></td>
          <td><img src="<?=$relBasePath?>/public/img/tl-yellow.gif"></td>
          <td><img src="<?=$relBasePath?>/public/img/tl-green.gif"></td>
        </tr>
        <tr>
          <th><?=T_("OFF")?></th>
          <th><?=T_("Changing")?></th>
          <th><?=T_("ON")?></th>
        </tr>
      </table>
    </div>    
           
  <?  
  $sidebar=ob_get_contents();
  ob_end_clean();

  //================ SETTINGS ================

  $smarty->assign("section", "system");
  $smarty->assign("subsection", "");
  $smarty->assign("templateType", "TwoColLeft");  //OneCol, TwoColLeft, TwoColRight
  $smarty->assign("contents", $contents);
  $smarty->assign("sidebar", $sidebar);
  $smarty->display('settings.tpl');
?>

