Location: PHPKode > scripts > AIRProjectXML > airprojectxml/GUI.php
<?php

/**
 * Graphic User Interface for AIRProjectXML library.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * @version  0.1 beta
 * @author   Vagharshak Tozalakyan <hide@address.com>
 * @license  http://www.opensource.org/licenses/mit-license.php
 */

require_once 'AIRProjectXML.php';

class AIRProjectXMLGUI
{
    var $_application = null;
    var $_fileType = null;
    var $_errors = array();
    var $_anchor = '';

    function AIRProjectXMLGUI()
    {
        $this->_initSession();
        $this->_application = new AIRProjectXML_application(APX_FOO, APX_FOO, APX_FOO, new AIRProjectXML_initialWindow(APX_FOO));
        $this->_application->icon = new AIRProjectXML_icon(APX_FOO, APX_ICON_32);
        $this->_fileType = new AIRProjectXML_fileType(APX_FOO, APX_FOO);
        $this->_application->id = $this->_application->filename = $this->_application->version = $this->_application->initialWindow->content = $this->_application->icon->image32x32 = $this->_fileType->name = $this->_fileType->extension = '';
        $this->_process();
        $this->_print();
    }

    function _initSession()
    {
        session_start();
        if (isset($_GET['clear'])) {
            unset($_SESSION['fileTypes']);
            unset($_SESSION['xmlFile']);
        }
        if (!isset($_SESSION['xmlFile'])) {
            $_SESSION['xmlFile'] = 'http://';
        }
        if (!isset($_SESSION['fileTypes'])) {
            $_SESSION['fileTypes'] = array();
        }
    }

    function _printHeader()
    {
        echo '<?xml version="1.0" encoding="utf-8"?>' . "\n";
        echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' . "\n";
        echo '<html xmlns="http://www.w3.org/1999/xhtml">' . "\n";
        echo '<head>' . "\n";
        echo '<meta http-equiv="content-type" content="text/html; charset=utf-8" />' . "\n";
        echo '<meta http-equiv="content-style-type" content="text/css" />' . "\n";
        echo '<meta http-equiv="content-script-type" content="text/javascript" />' . "\n";
        echo '<meta http-equiv="pragma" content="no-cache" />' . "\n";
        echo '<meta http-equiv="expires" content="-1" />' . "\n";
        echo '<meta http-equiv="imagetoolbar" content="no" />' . "\n";
        echo '<meta name="distribution" content="global" />' . "\n";
        echo '<title>AIRProjectXML GUI</title>' . "\n";
        echo '<style type="text/css">' . "\n";
        echo 'body { background-color: #ffffff; color: #333333; text-align: center; margin: 0px; }' . "\n";
        echo 'body, input, select, td { font-family: Tahoma, sans-serif; font-size: 9pt; }' . "\n";
        echo '#header { background-color: #980707; color: #f2f2f2; height: 90px; text-align: left; margin: 0px 0px 20px 0px; }' . "\n";
        echo '#header h1 { font-size: 20pt; font-weight: bold; padding: 20px 0px 0px 10px; margin: 0px; }' . "\n";
        echo '.txtInp { width: 200px; }' . "\n";
        echo '#main { width: 760px; text-align: center; line-height: 18px; padding: 0px 0px 20px 0px; margin: auto; }' . "\n";
        echo '#main a { background-color: inherit; color: #980707; text-decoration: none; }' . "\n";
        echo '#main a:hover { background-color: inherit; color: #980707; text-decoration: underline; }' . "\n";
        echo '#browse { background-color: #f2f2f2; color: #333333; height: 70px; border: 1px solid #777777; margin: 20px 0px 20px 0px; }' . "\n";
        echo '.errMsg { background-color: inherit; color: #980707; }' . "\n";
        echo '</style>' . "\n";
        echo '</head>' . "\n";
        echo '<body' . (!empty($this->_anchor) ? ' onload="location.href=\'#' . $this->_anchor . '\'"' : '') . '>' . "\n";
        echo '<div id="header"><h1>AIRProjectXML GUI 0.1 beta</h1></div>' . "\n";
        echo '<div id="main">' . "\n";
    }

    function _printFooter()
    {
        echo '</div>' . "\n";
        echo '</body>' . "\n";
        echo '</html>' . "\n";
    }

    function _printBrowseForm()
    {
        echo '<div id="browse">' . "\n";
        echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '" style="margin: 20px">' . "\n";
        echo '<b>Load existed application descriptor:</b>' . "\n";
        echo '<input type="text" name="xmlFile" value="' . htmlspecialchars($_SESSION['xmlFile']) . '" class="txtInp" style="width: 600px" />' . "\n";
        echo '<input type="submit" value="Read XML" />' . "\n";
        echo '</form>' . "\n";
        echo '</div>' . "\n";
    }

    function _printMainForm()
    {
        echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">' . "\n";
        echo '<a name="parameters"></a>' . "\n";
        echo '<h2>Application and Initial Window Parameters</h2>' . "\n";
        $this->_printMainElements();
        $this->_printFileTypes();
        echo '<br /><br />' . "\n";
        echo '<input type="submit" name="addFileType" value="Add File Type" />' . "\n";
        echo '<br /><br />' . "\n";
        echo '<input type="submit" name="download" value="Download XML" />' . "\n";
        echo '<input type="button" value="Clear Fields" onclick="location.href=\'' . $_SERVER['PHP_SELF'] . '?clear=1\'" />' . "\n";
        echo '</form>' . "\n";
    }

    function _printFormElement($title, $name, $type, $value, $values = array())
    {
        if ($type == 'text') {
            echo '<tr>' . "\n";
            echo '<td align="left">' . $title . '</td>' . "\n";
            echo '<td align="left">';
            echo '<input type="text" name="' . $name . '" value="' . ($value ? htmlspecialchars($value) : '') . '" class="txtInp" />';
            echo '</td>' . "\n";
            echo '</tr>' . "\n";
        } elseif ($type == 'checkbox') {
            echo '<tr>' . "\n";
            echo '<td align="left" colspan="2">';
            echo '<input type="checkbox" name="' . $name . '"' . ($value ? ' checked="checked"' : '') . ' />';
            echo $title;
            echo '</td>' . "\n";
            echo '</tr>' . "\n";
        } elseif ($type == 'select') {
            echo '<tr>' . "\n";
            echo '<td align="left">' . $title . '</td>' . "\n";
            echo '<td align="left">' . "\n";
            echo '<select name="' . $name . '" class="txtInp">' . "\n";
            foreach ($values as $k => $v) {
                echo '<option value="' . htmlspecialchars($k) . '"';
                if ($k == $value) {
                    echo ' selected="selected"';
                }
                echo '>' . htmlspecialchars($v) . '</option>' . "\n";
            }
            echo '</select>' . "\n";
            echo '</td>' . "\n";
            echo '</tr>' . "\n";
        }
        if (isset($this->_errors[$name])) {
            echo '<tr><td>&nbsp;</td><td align="left"><span class="errMsg">^ ' . $this->_errors[$name] . '</span></td></tr>' . "\n";
        }
    }

    function _printApplicationElements()
    {
        echo '<table border="0" cellpsacing="0" cellpadding="2">' . "\n";
        $this->_printFormElement('Namespace:', 'xmlns', 'text', $this->_application->xmlns);
        $this->_printFormElement('Minimum patch level:', 'minimumPatchLevel', 'text', $this->_application->minimumPatchLevel);
        $this->_printFormElement('ID:*', 'id', 'text', $this->_application->id);
        $this->_printFormElement('File name:*', 'filename', 'text', $this->_application->filename);
        $this->_printFormElement('Version:*', 'version', 'text', $this->_application->version);
        $this->_printFormElement('Name:', 'name', 'text', $this->_application->name);
        $this->_printFormElement('Description:', 'description', 'text', $this->_application->description);
        $this->_printFormElement('Copyright:', 'copyright', 'text', $this->_application->copyright);
        $this->_printFormElement('Install folder:', 'installFolder', 'text', $this->_application->installFolder);
        $this->_printFormElement('Program menu folder:', 'programMenuFolder', 'text', $this->_application->programMenuFolder);
        $this->_printFormElement('Icon 16x16:', 'image16x16', 'text', is_object($this->_application->icon) ? $this->_application->icon->image16x16 : '');
        $this->_printFormElement('Icon 32x32:', 'image32x32', 'text', is_object($this->_application->icon) ? $this->_application->icon->image32x32 : '');
        $this->_printFormElement('Icon 48x48:', 'image48x48', 'text', is_object($this->_application->icon) ? $this->_application->icon->image48x48 : '');
        $this->_printFormElement('Icon 128x128:', 'image128x128', 'text', is_object($this->_application->icon) ? $this->_application->icon->image128x128 : '');
        $this->_printFormElement('Custom update UI:', 'customUpdateUI', 'checkbox', $this->_application->customUpdateUI);
        $this->_printFormElement('Allow browser invocation:', 'allowBrowserInvocation', 'checkbox', $this->_application->allowBrowserInvocation);
        echo '</table>' . "\n";
    }

    function _printInitialWindowElements()
    {
        echo '<table border="0" cellspacing="0" cellpadding="2">' . "\n";
        $this->_printFormElement('Content:*', 'content', 'text', $this->_application->initialWindow->content);
        $this->_printFormElement('Title:', 'title', 'text', $this->_application->initialWindow->title);
        $this->_printFormElement('System chrome:', 'systemChrome', 'select', $this->_application->initialWindow->systemChrome, array('none' => 'none', 'standard' => 'standard'));
        $this->_printFormElement('Transparent:', 'transparent', 'checkbox', $this->_application->initialWindow->transparent);
        $this->_printFormElement('Visible:', 'visible', 'checkbox', $this->_application->initialWindow->visible);
        $this->_printFormElement('Minimizable:', 'minimizable', 'checkbox', $this->_application->initialWindow->minimizable);
        $this->_printFormElement('Maximizable:', 'maximizable', 'checkbox', $this->_application->initialWindow->maximizable);
        $this->_printFormElement('Resizable:', 'resizable', 'checkbox', $this->_application->initialWindow->resizable);
        $this->_printFormElement('Width:', 'width', 'text', $this->_application->initialWindow->width);
        $this->_printFormElement('Height:', 'height', 'text', $this->_application->initialWindow->height);
        $this->_printFormElement('Left:', 'x', 'text', $this->_application->initialWindow->x);
        $this->_printFormElement('Top:', 'y', 'text', $this->_application->initialWindow->y);
        $minSize = explode(' ', $this->_application->initialWindow->minSize);
        $maxSize = explode(' ', $this->_application->initialWindow->maxSize);
        $this->_printFormElement('Minimum width:', 'minWidth', 'text', isset($minSize[0]) ? $minSize[0] : '');
        $this->_printFormElement('Minimum height:', 'minHeight', 'text', isset($minSize[1]) ? $minSize[1] : '');
        $this->_printFormElement('Maximum width:', 'maxWidth', 'text', isset($maxSize[0]) ? $maxSize[0] : '');
        $this->_printFormElement('Maximum height:', 'maxHeight', 'text', isset($maxSize[1]) ? $maxSize[1] : '');
        echo '</table>' . "\n";
    }

    function _printFileTypesLeft()
    {
        echo '<table border="0" cellspacing="0" cellpadding="2">' . "\n";
        $this->_printFormElement('Name:*', 'ftName', 'text', $this->_fileType->name);
        $this->_printFormElement('Extension:*', 'ftExtension', 'text', $this->_fileType->extension);
        $this->_printFormElement('Description:', 'ftDescription', 'text', $this->_fileType->description);
        $this->_printFormElement('Content type:', 'ftContentType', 'text', $this->_fileType->contentType);
        echo '</table>' . "\n";
    }

    function _printFileTypesRight()
    {
        echo '<table border="0" cellspacing="0" cellpadding="2">' . "\n";
        $this->_printFormElement('Icon 16x16:', 'ftImage16x16', 'text', is_object($this->_fileType->icon) ? $this->_fileType->icon->image16x16 : '');
        $this->_printFormElement('Icon 32x32:', 'ftImage32x32', 'text', is_object($this->_fileType->icon) ? $this->_fileType->icon->image32x32 : '');
        $this->_printFormElement('Icon 48x48:', 'ftImage48x48', 'text', is_object($this->_fileType->icon) ? $this->_fileType->icon->image48x48 : '');
        $this->_printFormElement('Icon 128x128:', 'ftImage128x128', 'text', is_object($this->_fileType->icon) ? $this->_fileType->icon->image128x128 : '');
        echo '</table>' . "\n";
    }

    function _printMainElements()
    {
        echo '<table width="760" border="0" cellspacing="0" cellpadding="0">' . "\n";
        echo '<tr>' . "\n";
        echo '<td align="center" valign="top">' . "\n";
        $this->_printApplicationElements();
        echo '</td>' . "\n";
        echo '<td align="center" valign="top">' . "\n";
        $this->_printInitialWindowElements();
        echo '</td>' . "\n";
        echo '</tr>' . "\n";
        echo '</table>' . "\n";
    }

    function _printFileTypes()
    {
        echo '<a name="fileTypes"></a>' . "\n";
        echo '<h2>Associated File Types</h2>' . "\n";
        if (sizeof($_SESSION['fileTypes'])) {
            echo '<br />';
            echo '<table width="760" border="1" cellspacing="0" cellpadding="2">';
            echo '<tr>';
            echo '<td align="left"><b>Name</b></td>';
            echo '<td align="center"><b>Extension</b></td>';
            echo '<td align="left"><b>Description</b></td>';
            echo '<td align="left"><b>Content Type</b></td>';
            echo '<td align="left"><b>Icons</b></td>';
            echo '<td align="center">&nbsp;</td>';
            echo '</tr>';
            foreach ($_SESSION['fileTypes'] as $k => $ft) {
                $ft = unserialize($ft);
                echo '<tr>';
                echo '<td align="left">' . $ft->name . '</td>';
                echo '<td align="center">' . $ft->extension . '</td>';
                echo '<td align="left">' . (!empty($ft->description) ? $ft->description : '&nbsp;') . '</td>';
                echo '<td align="left">' . (!empty($ft->contentType) ? $ft->contentType : '&nbsp;') . '</td>';
                echo '<td align="left">';
                if (!empty($ft->icon->image16x16)) {
                    echo $ft->icon->image16x16 . '<br />';
                }
                if (!empty($ft->icon->image32x32)) {
                    echo $ft->icon->image32x32 . '<br />';
                }
                if (!empty($ft->icon->image48x48)) {
                    echo $ft->icon->image48x48 . '<br />';
                }
                if (!empty($ft->icon->image128x128)) {
                    echo $ft->icon->image128x128 . '<br />';
                }
                if (empty($ft->icon->image16x16) && empty($ft->icon->image32x32) && empty($ft->icon->image48x48) && empty($ft->icon->image128x128)) {
                    echo '&nbsp;';
                }
                echo '</td>';
                echo '<td align="center"><input type="submit" name="delFt' . $k . '" value="Remove" /></td>';
                echo '</tr>';
            }
            echo '</table>';
            echo '<br /><br />';
        }
        echo '<table width="760" border="0" cellspacing="0" cellpadding="0">' . "\n";
        echo '<tr>' . "\n";
        echo '<td align="center" valign="top">' . "\n";
        $this->_printFileTypesLeft();
        echo '</td>' . "\n";
        echo '<td align="center" valign="top">' . "\n";
        $this->_printFileTypesRight();
        echo '</td>' . "\n";
        echo '</tr>' . "\n";
        echo '</table>' . "\n";
    }

    function _print()
    {
        $this->_printHeader();
        $this->_printBrowseForm();
        $this->_printMainForm();
        $this->_printFooter();
    }

    function _processFileRead()
    {
        if (isset($_POST['xmlFile'])) {
            $_SESSION['xmlFile'] = stripslashes(trim($_POST['xmlFile']));
            $parser = new AIRProjectXMLParser();
            if (@$parser->parseXMLFile($_SESSION['xmlFile'])) {
                $this->_application = $parser->application;
                $_SESSION['fileTypes'] = array();
                foreach ($this->_application->fileTypes as $ft) {
                    $_SESSION['fileTypes'][] = serialize($ft);
                }
            }
        }
    }

    function _processMainData()
    {
        if (isset($_POST['xmlns'])) {
            $this->_application->xmlns = stripslashes(trim($_POST['xmlns']));
            $this->_application->minimumPatchLevel = stripslashes(trim($_POST['minimumPatchLevel']));
            $this->_application->id = stripslashes(trim($_POST['id']));
            $this->_application->filename = stripslashes(trim($_POST['filename']));
            $this->_application->version = stripslashes(trim($_POST['version']));
            $this->_application->name = stripslashes(trim($_POST['name']));
            $this->_application->description = stripslashes(trim($_POST['description']));
            $this->_application->copyright = stripslashes(trim($_POST['copyright']));
            $this->_application->installFolder = stripslashes(trim($_POST['installFolder']));
            $this->_application->programMenuFolder = stripslashes(trim($_POST['programMenuFolder']));
            $this->_application->icon->image16x16 = stripslashes(trim($_POST['image16x16']));
            $this->_application->icon->image32x32 = stripslashes(trim($_POST['image32x32']));
            $this->_application->icon->image48x48 = stripslashes(trim($_POST['image48x48']));
            $this->_application->icon->image128x128 = stripslashes(trim($_POST['image128x128']));
            $this->_application->customUpdateUI = isset($_POST['customUpdateUI']);
            $this->_application->allowBrowserInvocation = isset($_POST['allowBrowserInvocation']);
            $this->_application->initialWindow->content = stripslashes(trim($_POST['content']));
            $this->_application->initialWindow->title = stripslashes(trim($_POST['title']));
            $this->_application->initialWindow->systemChrome = stripslashes(trim($_POST['systemChrome']));
            $this->_application->initialWindow->transparent = isset($_POST['transparent']);
            $this->_application->initialWindow->visible = isset($_POST['visible']);
            $this->_application->initialWindow->minimizable = isset($_POST['minimizable']);
            $this->_application->initialWindow->maximizable = isset($_POST['maximizable']);
            $this->_application->initialWindow->resizable = isset($_POST['resizable']);
            $this->_application->initialWindow->width = abs(intval($_POST['width']));
            $this->_application->initialWindow->height = abs(intval($_POST['height']));
            $this->_application->initialWindow->x = abs(intval($_POST['x']));
            $this->_application->initialWindow->y = abs(intval($_POST['y']));
            $this->_application->initialWindow->minSize = abs(intval($_POST['minWidth'])) . ' ' . abs(intval($_POST['minHeight']));
            $this->_application->initialWindow->maxSize = abs(intval($_POST['maxWidth'])) . ' ' . abs(intval($_POST['maxHeight']));
        }
    }

    function _processRemoveFileType()
    {
        foreach($_SESSION['fileTypes'] as $k => $ft) {
            if (isset($_POST['delFt' . $k])) {
                unset($_SESSION['fileTypes'][$k]);
                $this->_anchor = 'fileTypes';
                break;
            }
        }
    }

    function _processAddFileType()
    {
        if (isset($_POST['addFileType'])) {
            $this->_fileType->name = stripslashes(trim($_POST['ftName']));
            $this->_fileType->extension = stripslashes(trim($_POST['ftExtension']));
            $this->_fileType->description = stripslashes(trim($_POST['ftDescription']));
            $this->_fileType->contentType = stripslashes(trim($_POST['ftContentType']));
            $this->_fileTypei = new AIRProjectXML_icon(APX_FOO, APX_ICON_32);
            $this->_fileTypei->image16x16 = stripslashes(trim($_POST['ftImage16x16']));
            $this->_fileTypei->image32x32 = stripslashes(trim($_POST['ftImage32x32']));
            $this->_fileTypei->image48x48 = stripslashes(trim($_POST['ftImage48x48']));
            $this->_fileTypei->image128x128 = stripslashes(trim($_POST['ftImage128x128']));
            $this->_fileType->icon = $this->_fileTypei;
            if (empty($this->_fileType->name)) {
                $this->_errors['ftName'] = 'The name is required.';
            }
            if (empty($this->_fileType->extension)) {
                $this->_errors['ftExtension'] = 'The extension is required.';
            }
            if (!sizeof($this->_errors)) {
                $_SESSION['fileTypes'][] = serialize($this->_fileType);
            }
            $this->_anchor = 'fileTypes';
        }
    }

    function _processDownload()
    {
        if (isset($_POST['download'])) {
            if (empty($this->_application->id)) {
                $this->_errors['id'] = 'The ID is required.';
            }
            if (empty($this->_application->filename)) {
                $this->_errors['filename'] = 'The file name is required.';
            }
            if (empty($this->_application->version)) {
                $this->_errors['version'] = 'The version is required.';
            }
            if (empty($this->_application->initialWindow->content)) {
                $this->_errors['content'] = 'The content is required.';
            }
            list($w, $h) = explode(' ', $this->_application->initialWindow->minSize);
            if (!$w && $h) {
                $this->_errors['minWidth'] = 'Minimum width is required when minimum height is set.';
            }
            if ($w && !$h) {
                $this->_errors['minHeight'] = 'Minimum height is required when minimum width is set.' . "$w $h";
            }
            list($w, $h) = explode(' ', $this->_application->initialWindow->maxSize);
            if (!$w && $h) {
                $this->_errors['maxWidth'] = 'Maximum width is required when maximum height is set.';
            }
            if ($w && !$h) {
                $this->_errors['maxHeight'] = 'Maximum height is required when maximum width is set.';
            }
            if (!sizeof($this->_errors)) {
                if (empty($this->_application->icon->image16x16) && empty($this->_application->icon->image32x32) && empty($this->_application->icon->image48x48) && empty($this->_application->icon->image48x48)) {
                    $this->_application->icon = null;
                }
                if ($this->_application->initialWindow->minSize == '0 0') {
                    $this->_application->initialWindow->minSize = null;
                }
                if ($this->_application->initialWindow->maxSize == '0 0') {
                    $this->_application->initialWindow->maxSize = null;
                }
                foreach ($_SESSION['fileTypes'] as $ft) {
                    $this->_application->fileTypes[] = unserialize($ft);
                }
                $obj = new AIRProjectXML($this->_application);
                $fileName = 'application.xml';
                if (!empty($xmlFile)) {
                    $fileName = basename($xmlFile);
                }
                $obj->forceDownload((empty($_SESSION['xmlFile']) || $_SESSION['xmlFile'] == 'http://') ? 'application.xml' : basename($_SESSION['xmlFile']));
                exit;
            } else {
                $this->_anchor = 'parameters';
            }
        }
    }

    function _process()
    {
        $this->_processFileRead();
        $this->_processmainData();
        $this->_processRemoveFileType();
        $this->_processAddFileType();
        $this->_processDownload();
    }

}

new AIRProjectXMLGUI();

?>
Return current item: AIRProjectXML