<?php

class XmlScreen
{

    public $xw;

    public function __construct($type)
    {
        $this->xw = new xmlWriter();
        $this->xw->openMemory();
        $this->xw->setIndent(TRUE);
        $this->xw->startDocument('1.0', 'UTF-8');
        $this->xw->startElement($type);
    }

    public function setTitle($title)
    {
        $this->xw->writeElement('Title', $title);
    }

    /**
     * The <SoftKeyItem> tag allows to customize keys on the keypad to fetch the given page in the <URL> tag. <p>
     * @param string $index <p>
     * The Key which will used. <br>
     * Useable Keys: <br>
     * XmlIndexButtons::F1 <br>
     * XmlIndexButtons::F2 <br>
     * XmlIndexButtons::F3 <br>
     * XmlIndexButtons::F4 <br>
     * XmlIndexButtons::CANCEL <br>
     * XmlIndexButtons::ENTER <br>
     * XmlIndexButtons::ONHOOK <br>
     * XmlIndexButtons::RIGHT <br>
     * XmlIndexButtons::F_RIGHT (only snom 370)<br>
     * XmlIndexButtons::LEFT <br>
     * XmlIndexButtons::F_LEFT (only snom 370)<br>
     * XmlIndexButtons::REDIAL <br>
     * XmlIndexButtons::SPEAKER <br>
     * XmlIndexButtons::TRANSFER <br>
     * XmlIndexButtons::CONFERENCE <br>
     * XmlIndexButtons::HELP <br>
     * XmlIndexButtons::F_MUTE <br>
     * XmlIndexButtons::F_UP <br>
     * XmlIndexButtons::F_DOWN <br>
     * XmlIndexButtons::REC <br>
     * XmlIndexButtons::SNOM <br>
     * XmlIndexButtons::DND <br>
     * XmlIndexButtons::RECALL <br>
     * XmlIndexButtons::PHONE_BOOK <br>
     * </p>
     * @param string $label <p>
     * You can label the 4 softkeys F1..F4 by $label with your own name and functionality.
     * </p>
     * @param string $url [optional] <p>
     * If $url is set, the given URL will be loaded by pressing the button. <br>
     * Useable Keys: <br>
     * XmlInputFlags::ASCII <br>
     * XmlInputFlags::PHONE_NUMBER <br>
     * XmlInputFlags::NUMBERS <br>
     * XmlInputFlags::EQUATIONS <br>
     * XmlInputFlags::UPPERCASE <br>
     * XmlInputFlags::LOWERCASE <br>
     * XmlInputFlags::PASSWORD <br>
     * XmlInputFlags::PASSWORD_NUMERIC_MODE <br>
     * </p>
     * @param string $softkey [optional] <p>
     * The Key-Function which will used. 
     * So you can simulate the function of an other key.<br>
     * Useable Keys: <br>
     * XmlSoftkeyButtons::F_ABORT <br>
     * XmlSoftkeyButtons::F_REDIRECT <br>
     * XmlSoftkeyButtons::F_DND <br>
     * XmlSoftkeyButtons::F_SNOM <br>
     * XmlSoftkeyButtons::F_HELP <br>
     * XmlSoftkeyButtons::F_CONFERENCE <br>
     * XmlSoftkeyButtons::F_REC <br>
     * XmlSoftkeyButtons::F_RETRIEVE <br>
     * XmlSoftkeyButtons::F_TRANSFER <br>
     * XmlSoftkeyButtons::F_REDIAL <br>
     * XmlSoftkeyButtons::F_LOGOFF_ALL <br>
     * XmlSoftkeyButtons::F_HOLD <br>
     * XmlSoftkeyButtons::F_DENYALL <br>
     * XmlSoftkeyButtons::F_CC <br>
     * XmlSoftkeyButtons::F_MESSAGE <br>
     * XmlSoftkeyButtons::F_REGS <br>
     * XmlSoftkeyButtons::F_DIALOG <br>
     * XmlSoftkeyButtons::F_CONTACTS <br>
     * XmlSoftkeyButtons::F_CALL_LIST <br>
     * XmlSoftkeyButtons::F_ADR_BOOK <br>
     * XmlSoftkeyButtons::F_PRESENCE <br>
     * XmlSoftkeyButtons::F_SPEED_DIAL <br>
     * XmlSoftkeyButtons::F_DIRECTORY_SEARCH <br>
     * XmlSoftkeyButtons::F_AUTO_ANSWER <br>
     * XmlSoftkeyButtons::CANCEL <br>
     * XmlSoftkeyButtons::ENTER <br>
     * XmlSoftkeyButtons::ONHOOK <br>
     * XmlSoftkeyButtons::RIGHT <br>
     * XmlSoftkeyButtons::F_RIGHT (only snom 370)<br>
     * XmlSoftkeyButtons::LEFT <br>
     * XmlSoftkeyButtons::F_LEFT (only snom 370)<br>
     * XmlSoftkeyButtons::REDIAL <br>
     * XmlSoftkeyButtons::SPEAKER <br>
     * XmlSoftkeyButtons::TRANSFER <br>
     * XmlSoftkeyButtons::CONFERENCE <br>
     * XmlSoftkeyButtons::HELP <br>
     * XmlSoftkeyButtons::F_MUTE <br>
     * XmlSoftkeyButtons::F_UP <br>
     * XmlSoftkeyButtons::F_DOWN <br>
     * XmlSoftkeyButtons::REC <br>
     * XmlSoftkeyButtons::SNOM <br>
     * XmlSoftkeyButtons::DND <br>
     * XmlSoftkeyButtons::RECALL <br>
     * XmlSoftkeyButtons::PHONE_BOOK <br>
     */
    public function addButton($index, $label, $url = '', $softkey = '')
    {
        $this->xw->startElement("SoftKeyItem");
        $this->xw->writeElement("Name", $index);
        $this->xw->writeElement("Label", $label);
        if ($url != '') {
            $this->xw->writeElement("URL", $url);
        }
        if ($softkey != '') {
            $this->xw->writeElement("softkey", $softkey);
        }
        $this->xw->endElement();
    }

    /**
     * The <fetch> tag exists to load an xml object from an http server automatically after
     * assigned milliseconds (mil attribute).
     * @param type $url The URL which will be fetch after x milliseconds
     * @param type $milSec The timeout in milliseconds
     */
    public function fetchUrlAfterMilSeconds($url, $milSec = 30000)
    {
        $this->xw->startElement('fetch');
        $this->xw->writeAttribute("mil", $milSec);
        $this->xw->text($url);
        $this->xw->endElement();
    }

    public function printScreen()
    {
        $this->finishDocument();
        echo $this->xw->outputMemory();
    }

    private function finishDocument()
    {
        $this->xw->endElement();
        $this->xw->endDocument();
    }
}
