Location: PHPKode > projects > Blandware AtLeap Lite - CMS on PHP > atleaplite/include/form/formelements.php
<?php
/*
 *  Copyright 2008 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * Classes representing custom form elements.
 *
 * @package     AtleapLite
 * @author      Roman Puchkovskiy
 * @license     http://www.apache.org/licenses/LICENSE-2.0  Apache License, Version 2.0
 */

/**
 */
require_once 'HTML/QuickForm/element.php';
require_once 'HTML/QuickForm/text.php';
require_once 'fckeditor/fckeditor.php';

/**
 * Form element that uses FCKEditor to edit HTML.
 *
 * @package     AtleapLite
 */
class RichEdit extends HTML_QuickForm_element {
    var $_name;
    var $_value;
    var $_fckBase;
    var $_width;
    var $_height;
    var $_toolbarSet;

    /**
     * Constructor.
     *
     * @param string $elementName optional  name
     * @param string $elementLabel optional label
     * @param array $attributes optional    attributes
     */
    function RichEdit($elementName=null, $elementLabel=null, $attributes=null)
    {
        $this->HTML_QuickForm_element($elementName, $elementLabel, $attributes);
        $this->_persistantFreeze = true;
        $this->_type = 'richedit';

        $this->_fckBase = '';
        $this->_width = '';
        $this->_height = '';
        $this->_toolbarSet = '';

        if ($attributes) {
            $this->_fckBase = $attributes['fckBase'];
            $this->_width = $attributes['width'];
            $this->_height = $attributes['height'];
            $this->_toolbarSet = $attributes['toolbarSet'];
        }
    }

    /**
     * Sets name.
     *
     * @param string $name  name to set
     */
    function setName($name) {
        $this->_name = $name;
    }

    /**
     * Returns name.
     *
     * @return string name
     */
    function getName() {
        return $this->_name;
    }

    /**
     * Sets value.
     *
     * @param string $value     value to set
     */
    function setValue($value) {
        $this->_value = $value;
    }

    /**
     * Returns value.
     *
     * @return string value
     */
    function getValue() {
        return $this->_value;
    }

    /**
     * Renders HTML for this element.
     *
     * @return string HTML which will be used to render this element
     */
    function toHtml() {
        if ($this->_flagFrozen) {
            $result = $this->getFrozenHtml();
        } else {
            $fck = new FCKEditor($this->getName());
            $fck->BasePath = $this->_fckBase;
            if (!empty($this->_width)) {
                $fck->Width = $this->_width;
            }
            if (!empty($this->_height)) {
                $fck->Height = $this->_height;
            }
            if (!empty($this->_toolbarSet)) {
                $fck->ToolbarSet = $this->_toolbarSet;
            }
            $fck->Config['BaseHref'] = defaultBaseHref();
            $fck->Value = $this->getValue();
            ob_start();
            $fck->Create();
            $result = ob_get_contents();
            ob_end_clean();
        }
        return $result;
    }

    /**
     * Returns HTML which will be used to render this element in frozed state.
     *
     * @return string frozed HTML
     */
    function getFrozenHtml() {
        return $this->getValue();
    }
}


/**
 * Form element that renders a text element with link attached; link calls a
 * javascript calendar used to select a date. After date is selected, it will
 * be injected as text element value (normally this text element is read-only).
 *
 * @package     AtleapLite
 */
class CalendarDate extends HTML_QuickForm_text {
    var $_formName;
    var $_anchorName;
    var $_calendarDatePattern;
    var $_phpDatePattern;
    var $_startDate;
    var $_title;
    var $_imgSrc;

    /**
     * Constructor.
     *
     * @param string $elementName optional  name
     * @param string $elementLabel optional label
     * @param array $attributes optional    attributes
     * @param array $options optional       options
     */
    function CalendarDate($elementName=null, $elementLabel=null,
            $attributes=null, $options=null) {
        if (!$attributes) {
            $attributes['readonly'] = 'true';
        }
        $this->HTML_QuickForm_text($elementName, $elementLabel, $attributes);
        $this->_persistantFreeze = true;
        $this->_type = 'datecalendar';

        $this->_anchorName = null;
        $this->_calendarDatePattern = 'dd/MM/yyyy';
        $this->_phpDatePattern = 'd/m/Y';
        $this->_startDate = null;
        $this->_title = null;
        $this->_imgSrc = null;

        if ($options) {
            if (isset($options['anchorName'])) {
                $this->_anchorName = $options['anchorName'];
            }
            if (isset($options['calendarDatePattern'])) {
                $this->_calendarDatePattern = $options['calendarDatePattern'];
            }
            if (isset($options['phpDatePattern'])) {
                $this->_phpDatePattern = $options['phpDatePattern'];
            }
            if (isset($options['startDate'])) {
                $this->_startDate = $options['startDate'];
            }
            if (isset($options['title'])) {
                $this->_title = $options['title'];
            }
            if (isset($options['imgSrc'])) {
                $this->_imgSrc = $options['imgSrc'];
            }
        }

        $startDate = $this->_startDate;
        if (empty($startDate)) {
            $startDate = time();
        }
        $this->_startDate = date($this->_phpDatePattern, $startDate);
        $this->setValue($this->_startDate);
    }

    /**
     * Renders HTML for this element.
     *
     * @return string HTML which will be used to render this element
     */
    function toHtml() {
        parent::toHtml();
        return parent::toHtml() . $this->_getCalendarHtml();
    }

    /**
     * Used to get form name...
     *
     * @param string $event     event
     * @param mixed $arg        ignored
     * @param object $caller    form
     */
    function onQuickFormEvent($event, $arg, &$caller) {
        // hack to get form name 
        if ($event == 'createElement') {
            $this->_formName = $caller->getAttribute('name');
        }
        return parent::onQuickFormEvent($event, $arg, $caller);
    }

    /**
     * Returns full element name (to make it possible to address element
     * from JS).
     *
     * @return string full name
     */
    function getFullName() {
        $formName = $this->_formName;
        $name = $this->getName();
        return "document.forms['$formName'].elements['$name']";
    }

    /**
     * Returns link HTML.
     *
     * @return string link HTML
     * @access private
     */
    function _getButtonHtml() {
        $title = $this->_title;
        $imgSrc = $this->_imgSrc;
        if (!empty($imgSrc)) {
            $result = "<img src=\"$imgSrc\">";
        } else {
            if (empty($title)) {
                $title = getMessage('calendar.link');
            }
            $result = $title;
        }
        return $result;
    }

    /**
     * Returns HTML used to create a JS calendar.
     *
     * @return string calendar HTML
     * @access private
     */
    function _getCalendarHtml() {
        $name = $this->getName();
        $fullName = $this->getFullName();
        $anchorName = $this->_anchorName;
        if (empty($anchorName)) {
            $anchorName = $name . '_calendar';
        }
        $var = 'calendar_' . $name;
        $calendarDatePattern = $this->_calendarDatePattern;
        $startDate = $this->_startDate;
        $button = $this->_getButtonHtml();

        $monthNames = getMessage('calendar.monthNames');
        $dayHeaders = getMessage('calendar.dayHeaders');
        $weekStart = getMessage('calendar.weekStart');
        $today = getMessage('calendar.today');
        $today = str_replace("'", "\\'", $today);

        $result = "<script type=\"text/javascript\">\n";
        $result .= "var $var = new CalendarPopup();\n";
        $result .= "$var.showYearNavigation();\n";
        $result .= "$var.showYearNavigationInput();\n";
        $result .= "$var.setMonthNames($monthNames);\n";
        $result .= "$var.setDayHeaders($dayHeaders);\n";
        $result .= "$var.setWeekStartDay($weekStart);\n";
        $result .= "$var.setTodayText('$today');\n";
        $result .= "</script>\n";

        $result .= "<a href=\"#\" id=\"$anchorName\" name=\"$anchorName\" ";
        $result .= "onclick=\"$var.select($fullName, '$anchorName', '$calendarDatePattern', '$startDate'); return false;\">";
        $result .= "$button</a>";
        return $result;
    }
}


?>
Return current item: Blandware AtLeap Lite - CMS on PHP