<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/

    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 3 of the License, or
    (at your option) any later version.

    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, see <http://www.gnu.org/licenses/>.
 */

/**
 * ユーザー情報のDBアクセスクラス
 * @author 	yamamoto
 * @version	
 */

//ユーザー情報をまとめて取得する
//特定のユーザー情報を更新・削除する
//ユーザー登録を行う
class sectionuserDB extends Model
{
	var $_form = null;
	var $erchk = 'server';//quickformエラーチェックのフラグ'client'or'server;
	var $db=null;
	//KVa => 半カナを全カナ,濁点つきの文字を１文字に変換する (K、H と共に利用する）,全角英数字を半角英数字に変換する
	//KVC => 半カナを全カナ,濁点つきの文字を１文字に変換する (K、H と共に利用する）,全角ひらがなを全角カタカナに変換する
	//n   => 全角数字を半角数字に変換する
/*
CREATE TABLE tbl_section_user (
	secusr_sidx_code		integer	NOT NULL,			--セクションコード
	secusr_usr_code			integer,					--ユーザーコード
	primary key (secusr_sidx_code, secusr_usr_code)
);
*/
	var $columns = array(
						array("column"=>"secusr_sidx_code","conv"=>"n","tags"=>false),		//セクションコード
						array("column"=>"secusr_usr_code","conv"=>"n","tags"=>false)		//ユーザコード
						);
	
	/**
	 * コンストラクタ php4対応・・・
	 */
	function sectionuserDB()
	{
		$this->__construct();
	}
	function __construct()
	{
		Model::__construct();
		$this->db = new DataBase();
	}
	
	function delete($secusr_sidx_code) {
		if(!is_numeric($secusr_sidx_code)) {
			return false;
		}
		return $this->db->delete("tbl_section_user", "secusr_sidx_code = ".$secusr_sidx_code);
	}
	
	function insert($secusr_sidx_code,$secusr_usr_code) {
		if(!is_numeric($secusr_sidx_code) || !is_numeric($secusr_usr_code)) {
			return false;
		}
		return $this->db->insert("tbl_section_user", array("secusr_sidx_code"=>$secusr_sidx_code,"secusr_usr_code"=>$secusr_usr_code));
	}
	
	/* セクションコードから担当ユーザ一覧を取得
	 * @param secusr_sidx_code;
	 */
	function getusrlist($secusr_sidx_code=null) {
		$sql = "select * from tbl_section_user 
				inner join tbl_user on usr_code = secusr_usr_code and usr_status = 1 ";
		if(is_numeric($secusr_sidx_code)){
			 $sql .= " where secusr_sidx_code = ?";
			 $whrdata[] = $secusr_sidx_code;
		}
		if(count($whrdata) > 0) {
			$result = $this->db->prepare($sql,$whrdata);
		} else {
			$result = $this->db->query($sql);
		}
		$rtn=null;
		if($result != null) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
				$rtn[] = $row;
			}
		}
		return $rtn;
	}
	
	function getPersonofCharge($sidx_code) {
		$rtn = null;
		if(is_numeric($sidx_code)) {
			//このセクションの編集者を取得
			$result0 = $this->db->prepare("select secusr_usr_code from tbl_section_user where secusr_sidx_code = ?", $sidx_code);
			if($result0 != null) {
				while($row0 = $result0->fetchRow(DB_FETCHMODE_ASSOC)) {
					$usrwhr[] = $row0["secusr_usr_code"];
				}
			}

			$sql = "select * from tbl_user 
					where (usr_kanri = 1 and usr_status = 1) or
					(usr_code in (".implode(',',$usrwhr)."))";
			$result = $this->db->query($sql);
			if($result != null) {
				while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
					$rtn[] = $row;
				}
			}
		}
		return $rtn;
	}
	
	//sidx_codeのメニューがusr_codeのユーザが編集できるかチェック
	function EditCheck($usr_code, $sidx_code) {
		$rtn = null;
		if(is_numeric($usr_code)&&is_numeric($sidx_code)) {
			$sql = "select * from tbl_section_user 
					where secusr_usr_code = ? and secusr_sidx_code = ?";
			$result = $this->db->prepare($sql,array($usr_code,$sidx_code));
			if($result != null) {
				while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
					$rtn[] = $row;
				}
				if(count($rtn)>0) {
					return true;
				}
			}
		}
		return false;
	}
	
}
?>