<?php
/**
 * -----------------------------------------------------------------------------
 *
 * SyL - Web Application Framework for PHP
 *
 * PHP version 4 (>= 4.3.x) or 5
 *
 * Copyright (C) 2006 k.watanabe
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * -----------------------------------------------------------------------------
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_FormElementSelect.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

/**
 * プルダウンフォーム要素クラス
 *
 * 名前 : 要素部品名 : 要素名(name) : 値(value)
 *   1  :      n     :    1         :  n
 *
 * ○インスタンス作成方法
 * // 基底クラスからスタティックメソッドで取得
 * $SyL_FormElementSelect = SyL_FormElement::createElement( 'select', 'prefecture', array( '01' => '北海道', '13' => '東京' ) );
 *
 * @package   SyL
 * @author    k.watanabe <k.watanabe@syl.jp>
 * @copyright 2006 k.watanabe
 * @license   http://www.opensource.org/licenses/lgpl-license.php
 * @version   CVS: $Id: SyL_FormElementSelect.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_FormElementSelect extends SyL_FormElement
{
    /**
     * 要素間の区切り文字
     *
     * @access private
     * @var string
     */
    var $element_separator = "&nbsp;\r\n";

    /**
     * コンストラクタ
     *
     * @access public
     * @param string フォーム要素HTMLタグ名
     * @param string フォーム要素表示名
     * @param string フォーム要素の値
     * @param array フォーム要素の部品配列（radio, select, checkboxの場合のみ）
     * @param string フォーム要素の追加属性
     */
    function SyL_FormElementSelect($name, $display_name, $options=array(), $value=null, $attributes=array())
    {
        $this->type         = 'select';
        $this->name         = $name;
        $this->display_name = $display_name;
        $this->options      = $options;
        $this->attributes   = $attributes;
  
        // デフォルト値の取得
        $value = ($value === null) ? $this->getParameter() : $value;
        if (!is_array($value)) {
            $value = array($value);
        }

        $this->value = array();
        foreach ($options as $k => $v) {
            if (($k !== '') && in_array((string)$k, $value, true)) {
                $this->value[] = (string)$k;
            }
        }
    }

    /**
     * 要素間の区切り文字をセット
     *
     * @access public
     * @param string 要素間の区切り文字
     */
    function setSeparator( $separetor )
    {
        $this->element_separator = $separetor;
    }

    /**
     * フォーム要素HTML出力（入力項目）
     *
     * @access public
     * @return string フォーム要素のHTML
     */
    function getHtmlTag()
    {
        $options = array();
        foreach ($this->options as $key => $value) {
            $html_element  = '';
            $html_element .= '<option ';
            $html_element .=   'value="' . $key . '" ';
            if (in_array((string)$key, $this->value, true)) {
                $html_element .= ' selected';
            }
            $html_element .= '>' . $this->encodeHtml($value);
            $options[] = $html_element;
        }

        $html_element  = '';
        $html_element .= '<select ';
        $html_element .=   'name="'  . $this->name . '[]" ';
        $html_element .= $this->getAttribute();
        $html_element .= '>';
        $html_element .= implode("\r\n", $options);
        $html_element .= '</select>';

        return $html_element;
    }

    /**
     * フォーム要素HTML出力（表示）
     *
     * @access public
     * @return string フォーム要素のHTML
     */
    function getHtmlView()
    {
        $options = array();
        foreach ($this->options as $key => $value) {
            if (in_array((string)$key, $this->value, true)) {
                $options[] = $this->encodeHtml($value) . $this->getHtmlHidden($this->name . '[]', $key);
            }
        }
        return implode($this->element_separator, $options);
    }

    /**
     * JavaScript入力チェックタグを取得
     * ※オーバーライド
     *
     * @access public
     * @return string JavaScript入力チェックタグ
     */
    function getJavaScript()
    {
        if (is_object($this->validators)) {
            $js  = '';
            $js .= 'var error_flag = false;' . "\r\n";
            $js .= 'var element_name = "' . $this->getName() . '[]";' . "\r\n";
            $js .= $this->validators->getJavaScript($this->getDisplayName(), $this->options);
            $js .= "\r\n";
            return $js;
        } else {
            return '';
        }
    }
}


?>
