<?php

/* ========================================================================
 *
 * @license This source file is subject to version 3.01 of the PHP license,
 *              that is available at http://www.php.net/license/3_01.txt
 *              If you did not receive a copy of the PHP license and are unable
 *              to obtain it through the world-wide-web, please send a note to
 *              license@php.net so we can mail you a copy immediately.
 *
 * @category   Application of MyNETS
 * @project    OpenPNE UsagiProject 2006-2007
 * @package    MyNETS
 * @author     UsagiProject <info@usagi.mynets.jp>
 * @copyright  2006-2007 UsagiProject <author member ad http://usagi.mynets.jp/member.html>
 * @version    MyNETS,v 1.0.0
 * @since      File available since Release 1.0.0 Nighty
 * @chengelog  [2007/04/14] Ver1.0.1Nighty package
 * ========================================================================
 */

/**
 * OpenPNE
 * @copyright 2005-2006 OpenPNE Project
 * @link      http://www.tejimaya.com/openpne.shtml
 *
 */

class pc_page_h_diary_add_confirm extends OpenPNE_Action
{
    function handleError($errors)
    {
        $_REQUEST['msg1'] = $errors['subject'];
        $_REQUEST['msg2'] = $errors['body'];
        $_REQUEST['msg3'] = $errors['public_flag'];
        openpne_forward('pc', 'page', 'h_diary_add', $errors);
        exit;
    }

    function execute($requests)
    {
        $u = $GLOBALS['AUTH']->uid();

        // --- リクエスト変数
        $subject = $requests['subject'];
        $body = $requests['body'];
        $public_flag = $requests['public_flag'];
        $tagsname = trim($requests['tagsname']);
        // ----------

        //タグの全角スペースを半角スペースへ変換する
        $tagsname = mb_ereg_replace("　", " ", $tagsname);

        $sessid = session_id();
        t_image_clear_tmp($sessid);

        $upfiles = array(
            1 => $_FILES['upfile_1'],
            $_FILES['upfile_2'],
            $_FILES['upfile_3'],
        );
        $tmpfiles = array(
            1 => '',
            '',
            '',
        );
        
        
        //追加
        
        $is_set_file = false;
        foreach ($upfiles as $key => $upfile) {
            if($upfile['name']) {
                $is_set_file = true;
                break;
            }
        }
        if(!$is_set_file) {
            $_REQUEST['msg'] = '作品を指定してください';
            openpne_forward('pc', 'page', 'h_diary_add');
            exit;
        }
        
        
        //終わり

        foreach ($upfiles as $key => $upfile) {
            if ($upfile['error'] !== UPLOAD_ERR_NO_FILE) {
                if (empty($upfile)) continue;
                if (!($image = t_check_image($upfile))) {
                    $_REQUEST['msg'] = '画像は'.IMAGE_MAX_FILESIZE.'KB以内のGIF・JPEG・PNGにしてください';
                    openpne_forward('pc', 'page', 'h_diary_add');
                    exit;
                } else {
                    $tmpfiles[$key] = t_image_save2tmp($upfile, $sessid, "d_{$key}", $image['format']);
                }
            }
        }

        $tagsname_list = array_unique(explode(" ", rtrim($tagsname)));
        if (count($tagsname_list) > 5) {
            $_REQUEST['msg'] = 'タグの指定は5個以下にしてください';
            openpne_forward('pc', 'page', 'h_diary_add');
            exit;
        }
        foreach ($tagsname_list as $value) {
            if(mb_strlen($value, 'UTF-8') > 20) {
                $_REQUEST['msg'] = 'タグの文字数は20文字以内にしてください(半角全角ともに)';
                openpne_forward('pc', 'page', 'h_diary_add');
                exit;
            }
        }

        $this->set('inc_navi', fetch_inc_navi('h'));

        //プロフィール
        $this->set("member", db_common_c_member4c_member_id($u));

        $form_val = array(
            "subject" => $subject,
            "body" => $body,
            "public_flag" => $public_flag,
            "upfile_1" => $_FILES['upfile_1'],
            "upfile_2" => $_FILES['upfile_2'],
            "upfile_3" => $_FILES['upfile_3'],
            "tmpfile_1" => $tmpfiles[1],
            "tmpfile_2" => $tmpfiles[2],
            "tmpfile_3" => $tmpfiles[3],
            "tagsname" => join(" ", $tagsname_list),
        );
        $this->set("form_val", $form_val);

        return 'success';
    }
}

?>
