steffen: server/kolab-horde-framework/kolab-horde-framework/Mobile/Mobile/Renderer html.php, NONE, 1.1 wml.php, NONE, 1.1

cvs at intevation.de cvs at intevation.de
Fri Oct 14 16:33:10 CEST 2005


Author: steffen

Update of /kolabrepository/server/kolab-horde-framework/kolab-horde-framework/Mobile/Mobile/Renderer
In directory doto:/tmp/cvs-serv28903/kolab-horde-framework/kolab-horde-framework/Mobile/Mobile/Renderer

Added Files:
	html.php wml.php 
Log Message:
Separated Horde Framework from kolab-resource-handlers

--- NEW FILE: html.php ---
<?php
/**
 * Horde_Mobile_Renderer:: output module for simple HTML and
 * Imode/Avantgo/similar devices.
 *
 * $Horde: framework/Mobile/Mobile/Renderer/html.php,v 1.27 2004/01/01 15:16:08 jan Exp $
 *
 * Copyright 2002-2004 Chuck Hagenbuch <chuck at horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Chuck Hagenbuch <chuck at horde.org>
 * @version $Revision: 1.1 $
 * @since   Horde 3.0
 * @package Horde_Mobile
 */
class Horde_Mobile_Renderer_html extends Horde_Mobile_Renderer {

    /**
     * Properly encode characters for output to an HTML browser.
     *
     * @param string $input  Characters to encode.
     *
     * @return string  The encoded text.
     */
    function escape($input)
    {
        return @htmlspecialchars($input, ENT_COMPAT, NLS::getCharset());
    }

    /**
     * Creates the page in the appropriate markup. Depending on the
     * clients browser type pure HTML, handheldfriendly AvantGo HTML,
     * i-mode cHTML, or MML is created.
     *
     * @param object Horde_Mobile $deck  The deck to render.
     */
    function render(&$deck)
    {
        if ($deck->_debug) {
            header('Content-Type: text/plain; charset=' . NLS::getCharset());
        } else {
            header('Content-Type: text/html; charset=' . NLS::getCharset());
        }
        header('Vary: Accept-Language');

        if (!$this->isBrowser('mml')) {
            echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
        }

        echo !empty($GLOBALS['language']) ? '<html lang="' . strtr($GLOBALS['language'], '_', '-') . '">' : '<html>';
        echo '<head>';

        if ($this->isBrowser('avantgo')) {
            echo '<meta name="HandheldFriendly" content="True">';
        }

        printf("<title>%s</title>\n", $this->escape($deck->get('title')));

        if ($deck->_simulator) {
            // Use simulator (mobile theme) stylesheet.
            echo Horde::stylesheetLink('horde', 'mobile');
        }

        echo '</head><body>';

        if ($deck->_simulator) {
            echo "<center><br />\n";
            // Create default device simulator table layout with
            // central CSS layout.
            echo "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
            echo "<tr><td colspan=\"3\" class=\"top\"> </td></tr>\n";
            echo "<tr><td valign=\"top\" class=\"left\"> </td>\n";
            echo "<td valign=\"top\" class=\"display\">\n";
        }

        $divstyle = '';
        if ($this->hasQuirk('scroll_tds') && $deck->_simulator) {
            // Make content of table element scrollable (Horde_Mobile
            // simulator).
            $divstyle = ' class="simdev"';
        }
        echo '<div' . $divstyle . '>';

        if (($cnt = count($deck->_cards)) !== 0) {
            $i = 0;
            foreach ($deck->_cards as $card) {
                if ($i != 0) {
                    echo '<hr />';
                }
                $this->_renderCard($card);
                $i++;
            }
        } else {
            foreach ($deck->_elements as $page_element) {
                $this->renderElement($page_element);
            }
        }

        echo '</div>';

        if ($deck->_simulator) {
            // Display lower part of Horde_Mobile default device
            // simulator.
            echo '</td><td valign="top" class="right"> </td></tr><tr><td colspan="3" class="bottom"> </td></tr></table></center>';
        }

        echo '</body></html>';
    }

    function _renderCard(&$card)
    {
        $name = $card->get('name') ? ' name="' . $this->escape($card->get('name')) . '"' : '';
        printf('<a%s>%s</a>', $name, $card->get('title'));

        if (count($card->_softkeys)) {
            foreach ($card->_softkeys as $key) {
                echo ' | <a href="' . $key['url'] . '">' .  $this->escape($key['label']) . '</a>';
            }
        }

        // Render all tags.
        foreach ($card->_elements as $page_element) {
            $this->renderElement($page_element);
        }
    }

    function _renderLink(&$link)
    {
        if ($link->get('title') &&
            !$this->isBrowser('avantgo') &&
            !$this->isBrowser('imode') &&
            !$this->isBrowser('mml')) {
            $title_option = sprintf(' onmouseover="self.status=\'%s\';return true;"',
                                    $this->escape($link->get('title')));
        } else {
            $title_option = '';
        }

        $accesskey_option = '';
        if ($link->get('accesskey')) {
            if ($this->isBrowser('imode')) {
                $accesskey_option = sprintf(' accesskey="%d"', $link->get('accesskey'));
            } elseif ($this->isBrowser('mml')) {
                $accesskey_option = sprintf(' directkey="%d"', $link->get('accesskey'));
            }
        }

        printf('<a href="%s"%s%s>%s</a>',
               $this->escape($link->get('url')), $title_option, $accesskey_option,
               $this->escape($link->get('label')));
    }

    function _renderLinkset(&$linkset)
    {
        if (count($linkset->_elements)) {
            echo '<ol>';
            foreach ($linkset->_elements as $val) {
                echo '<li>';
                $this->_renderLink($val);
                echo '</li>';
            }
            echo '</ol>';
        }
    }

    function _renderText(&$element)
    {
        foreach ($element->_attributes as $attribute) {
            echo '<' . $attribute . '>';
        }

        if ($element->get('linebreaks')) {
            echo nl2br($this->escape($element->get('text')));
        } else {
            echo $this->escape($element->get('text'));
        }

        $attributes = array_reverse($element->_attributes);
        foreach ($attributes as $attribute) {
            echo '</' . $attribute . '>';
        }
    }

    function _renderForm(&$form)
    {
        printf('<form action="%s" method="%s">', $form->get('url'), $form->get('method'));
        parent::_renderForm($form);
        echo '</form>';
    }

    function _renderInput(&$input)
    {
        $type = 'type="' . $input->get('type') . '"';
        $size = $input->get('size') ? sprintf('size="%d"', $input->get('size')) : '';
        $maxlength = $input->get('maxlength') ? sprintf('maxlength="%d"', $input->get('maxlength')) : '';

        if ($this->isBrowser('imode')) {
            $mode = sprintf(' istyle="%d"', $input->get('mode'));
        } elseif ($this->isBrowser('mml')) {
            $mode = $this->_getMode($input->get('mode'));
        } else {
            $mode = '';
        }

        // Create HTML input.
        printf('%s <input %s name="%s" value="%s"%s%s%s/>',
               $this->escape($input->get('label')), $type,
               $this->escape($input->get('name')), $this->escape($input->get('value')), $size, $maxlength, $mode);
    }

    function _renderTextarea(&$textarea)
    {
        if ($this->isBrowser('imode')) {
            $mode = sprintf(' istyle="%d"', $this->mode);
        } elseif ($this->isBrowser('mml')) {
            $mode = $this->_getMode($this->mode);
        } else {
            $mode = '';
        }

        $wrap = '';
        if (!$this->isBrowser('imode') && !$this->isBrowser('mml')) {
            $wrap = ' wrap="virtual"';
        }

        printf('%s<br /><textarea name="%s" rows="%s" cols="%s"%s%s>%s</textarea>',
               $this->escape($textarea->get('label')), $textarea->get('name'), $textarea->get('rows'),
               $textarea->get('cols'), $mode, $wrap, $textarea->get('value'));
    }

    function _renderSelect(&$select)
    {
        echo '<select name="' . $select->get('name') . '" size="1">';
        foreach ($select->_options as $val) {
            if ($val['value'] == $select->_value) {
                $sel = ' selected="selected"';
            } else {
                $sel = '';
            }
            echo '<option' . $sel . ' value="' . $val['value'] . '">' . $this->escape($val['label']) . '</option>';
        }
        echo '</select>';
    }

    function _renderRadio(&$radio)
    {
        foreach ($radio->_buttons as $val) {
            $sel = ($val['value'] == $radio->_value) ? ' checked="checked"' : '';
            printf('<input type="radio" name="%s"%s value="%s" /> %s<br />',
                   $radio->get('name'), $sel, $val['value'],
                   $this->escape($val['label']));
        }
    }

    function _renderCheckbox(&$checkbox)
    {
        $state = $checkbox->isChecked() ? ' checked="checked"' : '';
        printf('<input type="checkbox" name="%s"%s value="%s" /> %s<br />',
               $checkbox->get('name'), $state, $checkbox->get('value'),
               $this->escape($checkbox->get('label')));
    }

    function _renderSubmit(&$submit)
    {
        $name = !empty($submit->_name) ? ' name="' . $submit->_name . '"' : '';
        printf('<input type="submit"%s value="%s" /><br />',
               $name, $this->escape($submit->_label));
    }

    function _renderHidden(&$hidden)
    {
        printf('<input type="hidden" name="%s" value="%s" />',
               $hidden->get('name'), $hidden->get('value'));
    }

    function _renderTable(&$table)
    {
        $border = $table->get('border');
        $padding = $table->get('padding');
        $spacing = $table->get('spacing');

        echo '<table';
        if (!is_null($border)) {
            echo ' border="' . $border . '"';
        }
        if (!is_null($padding)) {
            echo ' cellpadding="' . $padding . '"';
        }
        if (!is_null($spacing)) {
            echo ' cellspacing="' . $spacing . '"';
        }
        echo '>';

        parent::_renderTable($table);

        // Terminate table.
        if ($this->isBrowser('mml')) {
            echo '</table><br />';
        } else {
            // MML has problems with the clear attribute.
            echo '</table><br clear="all">';
        }
    }

    function _renderPhone(&$phone)
    {
        if ($this->isBrowser('imode')) {
            // Create phoneto: link for i-Mode.
            printf('<p><a href="phoneto:%s">%s</a></p>',
                   $phone->get('number'), $phone->get('label'));
        } elseif ($this->isBrowser('mml')) {
            // Create tel: link for MML.
            printf('<p><a href="tel:%s">%s</a></p>',
                   $phone->get('number'), $phone->get('label'));
        } else {
            // Display phone number as plain text.
            printf('<p><big>%s</big></p>', $phone->get('label'));
        }
    }

    function _renderRule(&$rule)
    {
        $width = $rule->get('width');
        $size = $rule->get('size');

        echo '<hr' . ($width ? ' width="' . $width . '"' : '') . ($size ? ' size="' . $size . '"' : '') . "/>\n";
    }

    function _getMode($mode)
    {
        switch ($mode) {
        case 'katakana':
            return ' mode="katakana"';

        case 'hiragana':
            return ' mode="hiragana"';

        case 'numeric':
            return ' mode="numeric"';

        case 'alpha':
        default:
            return ' mode="alphabet"';
        }
    }

}

--- NEW FILE: wml.php ---
<?php
/**
 * Horde_Mobile_Renderer:: output module for WML (Wireless Markup
 * Language).
 *
 * $Horde: framework/Mobile/Mobile/Renderer/wml.php,v 1.31 2004/01/07 22:28:50 chuck Exp $
 *
 * Copyright 2002-2004 Chuck Hagenbuch <chuck at horde.org>
 *
 * See the enclosed file COPYING for license information (LGPL). If you
 * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
 *
 * @author  Chuck Hagenbuch <chuck at horde.org>
 * @version $Revision: 1.1 $
 * @since   Horde 3.0
 * @package Horde_Mobile
 */
class Horde_Mobile_Renderer_wml extends Horde_Mobile_Renderer {

    /**
     * Properly encode characters for output to a WML device.
     *
     * @param string $input  Characters to encode.
     *
     * @return string  The encoded text.
     */
    function escape($input)
    {
        // Encode entities.
        $output = @htmlspecialchars($input, ENT_COMPAT, NLS::getCharset());

        // Escape $ character in WML.
        $output = str_replace('$', '$$', $output);

        // Generate UTF-8.
        $output = String::convertCharset($output, NLS::getCharset(), 'utf-8');

        return $output;
    }

    /**
     * Creates the page in WML, allowing for different WML browser
     * quirks.
     *
     * @param object Horde_Mobile $deck  The deck to render.
     */
    function render(&$deck)
    {
        if ($deck->_debug) {
            header('Content-Type: text/plain; charset=utf-8');
        } else {
            header('Content-Type: text/vnd.wap.wml; charset=utf-8');
        }

        // Tricks to foil caching.
        header('Content-location: ' . md5(date('U')));

        echo "<?xml version=\"1.0\"?>\n";
        if ($this->hasQuirk('ow_gui_1.3')) {
            echo '<!DOCTYPE wml PUBLIC "-//PHONE.COM//DTD WML 1.3//EN" "http://www.openwave.com/dtd/wml13.dtd">';
        } else {
            echo '<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">';
        }
        echo '<wml>';

        if (count($deck->_cards)) {
            foreach ($deck->_cards as $card) {
                $this->_renderCard($card);
            }
        } else {
            $title = $deck->get('title') ? ' title="' . $this->escape($deck->get('title')) . '"' : '';
            printf('<card%s>', $title);

            // Render all tags.
            foreach ($deck->_elements as $page_element) {
                $this->renderElement($page_element);
            }

            echo '</card>';
        }

        // End the WML page.
        echo '</wml>';
    }

    function _renderCard(&$card)
    {
        $name = $card->get('name') ? ' id="' . $this->escape($card->get('name')) . '"' : '';
        $title = $card->get('title') ? ' title="' . $this->escape($card->get('title')) . '"' : '';
        printf('<card%s%s>', $name, $title);

        // Initialize WML variables with their default values.
        if (!is_null($card->_form)) {
            echo '<onevent type="onenterforward"><refresh>';
            $defaults = $card->_form->getDefaults();
            foreach ($defaults as $d) {
                printf('<setvar name="_%s" value="%s"/>', $d['name'], $this->escape($d['value']));
            }
            echo '</refresh></onevent>';
        }

        if (count($card->_softkeys)) {
            if (count($card->_softkeys) == 1) {
                // If there is only one softkey, make it of type
                // 'options' so that it always shows up on the right,
                // instead of having to share the left softkey with
                // active links, making it much harder to get to.
                $type = 'options';
            } else {
                $type = 'accept';
            }
            foreach ($card->_softkeys as $key) {
                echo '<do type="' . $type . '" label="' . $this->escape($key['label']) . '"><go href="' . $key['url'] . '"/></do>';
            }
        }

        // Render all tags.
        foreach ($card->_elements as $page_element) {
            $this->renderElement($page_element);
        }

        echo '</card>';
    }

    function _renderLink(&$link)
    {
        $title_option = $link->get('title') ? sprintf(' title="%s"', $this->escape($link->get('title'))) : '';

        printf('<a%s href="%s">%s</a>',
               $title_option, str_replace('&amp;', '&', $this->escape($link->get('url'))),
               $this->escape($link->get('label')));
    }

    function _renderLinkset(&$linkset)
    {
        if (count($linkset->_elements)) {
            if ($this->isBrowser('up')) {
                echo '<select>';
                foreach ($linkset->_elements as $val) {
                    $title = $val->get('title') ? ' title="' . $this->escape($val->get('title')) . '"' : '';
                    printf('<option onpick="%s"%s>%s</option>',
                           $this->escape($val->get('url')), $title,
                           $this->escape($val->get('label')));
                }
                echo '</select>';
            } else {
                foreach ($linkset->_elements as $val) {
                    $this->_renderLink($val);
                    echo '<br/>';
                }
            }
        }
    }

    function _renderText(&$element)
    {
        foreach ($element->_attributes as $attribute) {
            echo '<' . $attribute . '>';
        }

        if ($element->get('linebreaks')) {
            echo nl2br($this->escape($element->get('text')));
        } else {
            echo $this->escape($element->get('text'));
        }

        $attributes = array_reverse($element->_attributes);
        foreach ($attributes as $attribute) {
            echo '</' . $attribute . '>';
        }
    }

    function _renderInput(&$input)
    {
        $type = ' type="' . $input->get('type') . '"';
        $size = $input->get('size') ? sprintf(' size="%d"', $input->get('size')) : '';
        $maxlength = $input->get('maxlength') ? sprintf(' maxlength="%d"', $input->get('maxlength')) : '';

        printf('%s<input emptyok="true" format="%s"%s name="_%s" value="%s"%s%s/>',
               $this->escape($input->get('label')), $input->get('format'),
               $type, $this->escape($input->get('name')), $this->escape($input->get('value')), $size, $maxlength);
    }

    function _renderTextarea(&$textarea)
    {
        printf('%s<input emptyok="true" name="_%s" value="%s"/>',
               $this->escape($textarea->get('label')),
               $textarea->get('name'), $textarea->get('value'));
    }

    function _renderSelect(&$select)
    {
        if ($this->hasQuirk('ow_gui_1.3')) {
            switch ($select->get('type')) {
            case 'spin':
                $type_option = 'type="spin"';
                break;

            case 'popup':
            default:
                $type_option = 'type="popup"';
                break;
            }

            echo '<select ' . $type_option . ' name="_' . $select->get('name') . '">';
        } else {
            echo '<select name="_' . $select->get('name') . '">';
        }

        foreach ($select->_options as $val) {
            echo '<option value="' . $val['value'] . '">' . $this->escape($val['label']) . '</option>';
        }
        echo '</select>';
    }

    function _renderRadio(&$radio)
    {
        if ($this->hasQuirk('ow_gui_1.3')) {
            // Openwave GUI extensions for WML 1.3
            printf('<select type="radio" name="_%s">', $radio->get('name'));
        } else {
            // Conventional WML (similar to Horde_Mobile_select).
            printf('<select name="_%s">', $radio->get('name'));
        }

        foreach ($radio->_buttons as $val) {
            printf('<option value="%s">%s</option>',
                   $val['value'], $this->escape($val['label']));
        }

        echo '</select>';
    }

    function _renderCheckbox(&$checkbox)
    {
        printf('<select name="_%s" multiple="true">', $checkbox->get('name'));
        printf('<option value="%s">%s</option></select>',
               $checkbox->get('value'), $this->escape($checkbox->get('label')));
    }

    function _renderSubmit(&$submit)
    {
        if ($this->hasQuirk('ow_gui_1.3')) {
            // Create <do type="button"> sequence for Openwave GUI
            // extensions WML 1.3.
            printf('<do type="button" label="%s">',
                   $this->escape($submit->get('label')));
            $tag = 'do';
        } else {
            // Create <anchor> sequence in normal WML.
            printf('<anchor title="%s">%s',
                   $this->escape($submit->get('label')),
                   $this->escape($submit->get('label')));
            $tag = 'anchor';
        }

        if ($submit->_form->get('method') == 'post') {
            printf('<go href="%s" method="post">', Horde::url($submit->_form->get('url')));

            // Value for this submit element, only if non-empty name.
            if ($submit->get('name')) {
                printf('<postfield name="%s" value="%s"/>', $submit->get('name'), $this->escape($submit->get('label')));
            }

            $defaults = $submit->_form->getDefaults();
            foreach ($defaults as $d) {
                if (array_key_exists('hidden', $d)) {
                    printf('<postfield name="%s" value="%s"/>', $d['name'], $this->escape($d['value']));
                } else {
                    printf('<postfield name="%s" value="$(_%s)"/>', $d['name'], $d['name']);
                }
            }
        } else {
            // Start with the value for this submit element.
            $query_string = $submit->get('name') . '=' . $this->escape($submit->get('label')) . '&';

            $getvars = $submit->_form->getGetVars();
            foreach ($getvars as $val) {
                $query_string .= $val . '=$(_' . $val . ')&';
            }

            if (substr($query_string, -5) == '&') {
                $query_string = substr($query_string, 0, strlen($query_string) - 5);
            }

            printf('<go href="%s?%s">', $submit->_form->get('url'), $query_string);
        }

        echo "</go></$tag>";
    }

    function _renderTable(&$table)
    {
        // Count maximum number of columns in table.
        $max = 0;
        foreach ($table->_rows as $row) {
            $max = max($max, $row->getColumnCount());
        }
        printf('<table columns="%d">', $max);

        parent::_renderTable($table);

        // Terminate table.
        echo '</table>';
    }

    function _renderPhone(&$phone)
    {
        $title = $phone->get('title');
        $title_option = ($title ? sprintf(' title="%s"', $this->escape($title)) : '');

        printf('<a%s href="wtai://wp/mc;%s">%s</a>', $title_option,
               str_replace('+', '%2B', $phone->get('number')), $phone->get('label'));
    }

    function _renderRule(&$rule)
    {
        if ($this->hasQuirk('ow_gui_1.3')) {
            // WAP device accepts Openwave GUI extensions for WML 1.3
            $width = $rule->get('width');
            $size = $rule->get('size');

            echo '<hr' . ($width ? ' width="' . $width . '"' : '') . ($size ? ' size="' . $size . '"' : '') . '/>';
        } else {
            // WAP device does not understand <hr/> tags.
            // ==> draw some number of hyphens to create a rule
            echo '----------<br/>';
        }
    }

}





More information about the commits mailing list