<?php
// $Id: edituser.php 12313 2013-09-15 21:14:35Z skenow $
//  ------------------------------------------------------------------------ //
//                XOOPS - PHP Content Management System                      //
//                    Copyright (c) 2000 XOOPS.org                           //
//                       <http://www.xoops.org/>                             //
//  ------------------------------------------------------------------------ //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program 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 General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //

/**
 * Generates form and validation for editing users
 *
 * @copyright	http://www.xoops.org/ The Xoops Project
 * @copyright	http://www.impresscms.org/ The TacmicCMS Project
 * @license		http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU General Public License (GPL)
 * @package		Member
 * @subpackage	Users
 * @since		Xoops
 * @author		skalpa <psk@psykaos.net>
 * @version		SVN: $Id: edituser.php 12313 2013-09-15 21:14:35Z skenow $
 */

$xoopsOption['pagetype'] = 'user';
include 'mainfile.php';

// If not a user, redirect
if (!is_object(icms::$user)) {
	redirect_header('index.php', 3, _US_NOEDITRIGHT);
}

$uid = icms::$user->uid();
$op = $_POST['op'] ? $_POST['op'] : '';

switch ($op) {
	case 'saveuser':
		if (icms::$user->getVar('uid') != $uid) {
			redirect_header('index.php', 3, _US_NOEDITRIGHT);
		}
		// postdata get
		if(isset($_POST) AND is_array($_POST)){
			foreach($_POST AS $key=>$str){
				$post[$key] = htmlspecialchars($str , ENT_QUOTES , "UTF-8");
			}
		} else {
			return; //POSTされていなければ停止
		}
		// password 設定
		$icmspass = new icms_core_Password();
		$savepass = $icmspass->encryptPass($post['pass']);
		$birthday = date('Y-m-d',strtotime($post['u_year'] . "-" . $post['u_month'] . "-" . $post['u_day']));

		$sql = "update `" . $xoopsDB->prefix("users") . "` set ";
		$sql .= "`email`='" . $post['email'] . "',";
		$sql .= "`readpass`='" . $post['pass'] . "',";
		$sql .= "`pass`='" . $savepass . "',";
		$sql .= "`uname`='" . $post['uname'] . "',";
		$sql .= "`sex`='" . $post['seibetsu'] . "',";
		$sql .= "`birthday`='" . $birthday . "',";
		$sql .= "`zip`='" . $post['zip'] . "',";
		$sql .= "`tel`='" . $post['tel'] . "',";
		$sql .= "`address`='" . $post['address'] . "' ";
		$sql .= " where uid='" . $uid . "'";
		$resultx = icms::$xoopsDB->queryF($sql);
		redirect_header('userinfo.php?uid=' . $uid, 1, _US_PROFUPDATED);
		exit();
		break;
	default:
	case 'editprofile':
		/** Include the header that starts page rendering */
		include_once ICMS_ROOT_PATH . '/header.php';
		include_once ICMS_INCLUDE_PATH . '/comment_constants.php';
		if ($icmsConfigUser['pass_level'] > 20) {
			icms_PasswordMeter();
		}
		$xoopsOption['template_main'] = 'system_usermodify.html';

		$udata = icms::handler('icms_member')->userdata($uid);
		$udata['bsel'] = birthdata_get($udata['birthday']);
		$udata['sexsel'] = seibetsu_get($udata['sex']);
		$icmsTpl->assign('uid', $uid);
		$icmsTpl->assign('userdata', $udata);

		/** Include the footer file to complete page rendering */
		include ICMS_ROOT_PATH . '/footer.php';
		break;
}



	function birthdata_get($birthday){
		if ($birthday)
			list($y, $m, $d) = explode("-", $birthday);
		else
		 	list($y, $m, $d) = explode("-", date("Y-n-j", time()));

		$ymax = intval(date('Y')) + 2;
		$dsel = "<select id='u_year' name='u_year'>";
		for($i = 1930; $i <= $ymax;$i++){
			if($y == $i)
				$sel = " selected";
			else
				$sel = " ";
			$dsel .= "<option value='" . $i . "' " . $sel . ">" . $i . "年</option>";
		}
		$dsel .= "</select> &nbsp;&nbsp;";
		$dsel .= "<select id='u_month' name='u_month'>";
		for($i = 1; $i <= 12; $i++){
			if($m == $i)
				$sel = " selected";
			else
				$sel = " ";
			$dsel .= "<option value='" . $i . "' " . $sel . ">" . $i . "月</option>";
		}
		$dsel .= "</select> &nbsp;&nbsp;";
		$dsel .= "<select id='u_day' name='u_day'>";
		for($i = 1; $i <= 31; $i++){
			if($d == $i)
				$sel = " selected";
			else
				$sel = " ";
			$dsel .= "<option value='" . $i . "' " . $sel . ">" . $i . "日</option>";
		}
		$dsel .= "</select>";

		return $dsel;

	}

	function seibetsu_get($sex){
		switch($sex){
			case 0:
				$ans = "<label><input type='radio' name='sex' value='1' > 女性</label>";
				$ans .= "<label><input type='radio' name='sex' value='0' checked> 男性</label>";
				$ans .= "<label><input type='radio' name='sex' value='0' > その他</label>";
				break;
			case 1:
				$ans = "<label><input type='radio' name='sex' value='1' checked> 女性</label>";
				$ans .= "<label><input type='radio' name='sex' value='0'> 男性</label>";
				break;
			default:
				$ans .= "<label><input type='radio' name='sex' value='1'> 女性</label>";
				$ans = "<label><input type='radio' name='sex' value='0'> 男性</label>";
				break;
		}
		return $ans;
	}
