<?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_ValidatorFiles.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 * -----------------------------------------------------------------------------
 */

/**
 * ファイルアップロード検証クラス
 * 
 * 例）
 * $SyL_Validator =& SyL_Validator::create( 'files', '{name}は必須です' );
 *
 * ○エラーメッセージ変換文字列
 *  なし
 *
 * @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_ValidatorFiles.php,v 1.7 2006/12/23 11:13:09 seasonstream Exp $
 * @link      http://www.syl.jp/
 */
class SyL_ValidatorFiles extends SyL_Validator
{
    /**
     * コンストラクタ
     *
     * @access public
     * @param string エラーメッセージのフォーマット
     * @param array 検証パラメータ
     */
    function SyL_ValidatorFiles($error_message, $parameters=array())
    {
        // デフォルトパラメータ
        $init_parameters = array(
          'name_format'          => null,
          'name_format_error'    => null,
          'min_size'             => 1, // サイズ
          'min_size_error'       => null,
          'max_size'             => null, // checkbox, select, fileの配列の最大チェック数
          'max_size_error'       => null,
          'mime_format'          => null, // mime typeを正規表現で検証
          'mime_format_error'    => null,
          'is_upload_func'       => null, // is_uploaded_file関数で検証
          'is_upload_func_error' => null
        );
        $parameters = array_merge($init_parameters, $parameters);
        parent::SyL_Validator($error_message, $parameters);
    }

    /**
     * 必須検証処理を実行する
     *
     * @access public
     * @param array 検証対象値
     * @return bool true: エラー無し, false: エラーあり
     */
    function validate($value)
    {
        sort($value['size'], SORT_NUMERIC);
        // サイズ最小値チェック
        if ($this->parameters['min_size'] !== null) {
            if ($value['size'][0] < $this->parameters['min']) {
                if ($this->parameters['min_size_error'] !== null) {
                    $this->error_message = $this->parameters['min_size_error'];
                }
                return false;
            }
        }
        // サイズ最大値チェック
        if ($this->parameters['max_size'] !== null) {
            if ($value['size'][count($value['size'])-1] > $this->parameters['max']) {
                if ($this->parameters['max_size_error'] !== null) {
                    $this->error_message = $this->parameters['max_size_error'];
                }
                return false;
            }
        }
        // ファイル名チェック
        if ($this->parameters['name_format'] !== null) {
            foreach ($value['name'] as $tmp) {
                if (!preg_match($this->parameters['name_format'], $tmp)) {
                    if ($this->parameters['name_format_error'] !== null) {
                        $this->error_message = $this->parameters['name_format_error'];
                    }
                    return false;
                }
            }
        }
        // MIMEタイプチェック
        if ($this->parameters['mime_format'] !== null) {
            foreach ($value['type'] as $tmp) {
                if (!preg_match($this->parameters['mime_format'], $tmp)) {
                    if ($this->parameters['mime_format_error'] !== null) {
                        $this->error_message = $this->parameters['mime_format_error'];
                    }
                    return false;
                }
            }
        }
        // POST送信チェック
        if ($this->parameters['is_upload_func'] !== null) {
            foreach ($value['tmp_name'] as $tmp) {
                if (!is_uploaded_file($tmp)) {
                    if ($this->parameters['is_upload_func_error'] !== null) {
                        $this->error_message = $this->parameters['is_upload_func_error'];
                    }
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * エラーメッセージを取得する
     * ※オーバーライド
     *
     * @access public
     * @param string 要素名
     * @return string エラーメッセージ
     */
    function getErrorMessage($name='')
    {
        if ($this->parameters['min_size'] !== null) {
            $this->error_message = str_replace('{min_size}', $this->parameters['min_size'], $this->error_message);
        }
        if ($this->parameters['max_size'] !== null) {
            $this->error_message = str_replace('{max_size}', $this->parameters['max_size'], $this->error_message);
        }
        return parent::getErrorMessage($name);
    }

    /**
     * 必須検証処理のJavaScriptを取得する
     *
     * @access public
     * @param string フォーム要素表示名
     * @param array フォーム要素の部品配列（radio, select, checkboxの場合のみ）
     * @return string JavaScript処理ロジック
     */
    function getJavaScript($display_name, $parts)
    {
        $js  = '';
        if ($this->parameters['name_format'] !== null) {
            $js .= 'if (!error_flag && !validator.isRegex(element_name, "' . $this->getErrorMessage($display_name) . '", ' . $this->parameters['name_format'] . ')) {' . "\r\n";
            $js .= '  error_flag = true;' . "\r\n";
            $js .= '}' . "\r\n";
        }
        return $js;
    }

}

?>
