<?php

/**
 * Sample web GUI to use FreemoteApi PHP class
 * 
 * @author DUVERGIER Claude <claude@duvergier.fr>
 * @link http://c.duvergier.free.fr/portfolio/developpements/freebox/freemote/
 * 
 * @version $Id: webGui.php 11 2011-09-22 22:34:32Z claude-duvergier $
 */
include 'FreemoteApi.php';

$api null;

/*** Gestion des paramètres ***/
$formSubmitted = !empty($_GET);
$error false;
if (
$formSubmitted) {
    if (
array_key_exists('freemoteCode'$_GET)) {
        
$tvBox 1;
        if (
array_key_exists('tvBox'$_GET)) {
            
$tvBox $_GET['tvBox'];
        }
        try {
            
$api = new FreemoteApi((int) $_GET['freemoteCode'], (int) $tvBox);
            if (!
$api->testConnection()) { // Connection error
                
$error 'credentials';
                
$api null;
            }
            else {
                echo 
'<p>La liaison est prête (<a href="' $_SERVER['SCRIPT_NAME'] . '">se déconnecter</a>)</p>';
            }
        }
        catch (
Exception $e) {
            
$error 'credentials';
        }
    }
    
    if (
$api instanceof FreemoteApi) {
        if ( 
            !
array_key_exists('action'$_GET) || // No action provided
            
// OR No value provided
                
!array_key_exists('value-command'$_GET) && 
                !
array_key_exists('value-text'$_GET)
            )
        ) {
            
//$error = 'noAction';
        
// Actions and values are present
        
else {
            
$sendCommandReturn null;
            switch (
$_GET['action']) {
                case 
'sendCommand':
                    if (
                        !
array_key_exists('value-command'$_GET)
                        || !
in_array($_GET['value-command'], $api->getKeys(), true)
                    ) {
                        
$error 'badCommand';
                    }
                    else {
                        
$longPression = (array_key_exists('value-command-long'$_GET) && $_GET['value-command-long'] == '1');
                        
$repetition array_key_exists('value-command-repetition'$_GET) ? $_GET['value-command-repetition'] : 1;
                        
$sendCommandReturn $api->sendCommand($_GET['value-command'], $longPression, (int) $repetition);
                    }
                    break;
                    
                case 
'sendInteger':
                case 
'sendText':
                    if (!
array_key_exists('value-text'$_GET)) {
                        
$error 'noValue';
                    }
                    else {
                        if (
$_GET['action'] === 'sendInteger') {
                            
$sendCommandReturn $api->sendInteger((int) $_GET['value-text']);
                        }
                        else {
                            
$sendCommandReturn $api->sendString((string) $_GET['value-text']);
                        }
                    }
                    break;
            }
            
            if (
$sendCommandReturn != null) {
                if (!empty(
$sendCommandReturn['nok'])) {
                    
$error 'sendCommandReturnedNok';
                }
            }
        }
        
        
    }
    
    if (
$error) {
        echo 
'<div style="color: red; border: 1px dashed red;">';
        switch (
$error) {
            case 
'credentials':
                echo 
'<p>Erreur de liaison (code et/ou de numéro de Freebox HD erroné ?) !</p>';
                break;
            case 
'noAction':
                echo 
'<p>Pas d\'action à effectuer !</p>';
                break;
            case 
'badCommand':
                echo 
'<p>Commande incorrecte !</p>';
                break;
            case 
'noValue':
                echo 
'<p>Pas de saisie à envoyer !</p>';
                break;
            case 
'sendCommandReturnedNok':
                echo 
'<p>Une ou plusieurs commande envoyée est revenue en erreur !</p>';
                break;
        }
        echo 
'</div>';
    }
    
    
/* Form refill */
    
$formCheckedInputs = array();
    if (
array_key_exists('action'$_GET)) {
        
$formCheckedInputs['action-sendCommand'] = ($_GET['action'] == 'sendCommand' ' checked="checked"' '');
        
$formCheckedInputs['action-sendInteger'] = ($_GET['action'] == 'sendInteger' ' checked="checked"' '');
        
$formCheckedInputs['action-sendText'] = ($_GET['action'] == 'sendText' ' checked="checked"' '');
    }
    else {
        
$formCheckedInputs['action-sendCommand'] = ' checked="checked"';
        
$formCheckedInputs['action-sendInteger'] = '';
        
$formCheckedInputs['action-sendText'] = '';
    }
    
/* /Form refill */
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="main.css" media="screen" />
    <title>Freemote - WebGUI</title>
</head>
<body>

<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" method="get">
<?php
if ($api instanceof FreemoteApi):
?>
    <input type="hidden" id="freemoteCode" name="freemoteCode" value="<?php echo $api->getVirtualRemoteCode(); ?>" />
    <input type="hidden" id="tvBox" name="tvBox" value="<?php echo $api->getTvBoxNumber(); ?>" />
    
    <p>Votre action&nbsp;:</p>
    <input type="radio" id="action-sendCommand" name="action" value="sendCommand" <?php echo $formCheckedInputs['action-sendCommand']; ?> />
    <label for="action-sendCommand">Appuyer sur un bouton</label>
    <div style="margin-left: 30px;">
        <select id="value-command" name="value-command">
        <?php
        $keys 
$api->getKeys();
        
ksort($keys);
        foreach (
$keys as $key => $value) {
            echo 
'    <option value="' $value '">' $key '</option>' PHP_EOL;
        }
        
?>
        </select><br />
        <input type="hidden" id="value-command-long-0" name="value-command-long" value="0" />
        <input type="checkbox" id="value-command-long-1" name="value-command-long" value="1" /> <label for="value-command-long-1">Longue pression</label><br />
        <label for="value-command-repetition">Répéter</label> <input type="text" id="value-command-repetition" name="value-command-repetition" value="1" /><br />
    </div>
    <br />
    <input type="radio" id="action-sendInteger" name="action" value="sendInteger" <?php echo $formCheckedInputs['action-sendInteger']; ?> /> <label for="action-sendInteger">Saisir un nombre (choix de chaîne, durée, etc.)</label><br />
    <input type="radio" id="action-sendText" name="action" value="sendText" <?php echo $formCheckedInputs['action-sendText']; ?> /> <label for="action-sendCommand">Saisir un texte (nom de l'enregistrement)</label><br />
    <div style="margin-left: 30px;">
        <label for="value-text">Votre saisie&nbsp;:</label> <input type="text" id="value-text" name="value-text" style="width: 250px;" /><br />
    </div>

<?php
else:
?>
    <p>Non connecté : veuillez vous connecter</p>
    <label for="freemoteCode">Code télécommande Freebox HD&nbsp;:</label> <input type="text" id="freemoteCode" name="freemoteCode" value="" /><br />
    <label for="tvBox">Numéro de Freebox HD&nbsp;:</label> <select id="tvBox" name="tvBox"><option value="1" selected="selected">1</option><option value="2">2</option></select>
<?php
endif;
?>
    <br />
    <input type="submit" value="Envoyer" />

</form>


</body>
</html>