<?php
require_once ('Pager/Pager.php');
require_once ('CLbaseform.php');

class CLinqlist extends baseForm
{
	function CLinqlist()
	{
		$this->__construct();
	}

	function __construct()
	{
		baseForm::__construct();
		$this->_form = new HTML_QuickForm('form', 'post', $_SERVER['PHP_SELF'], '', null, true);
		$this->db	 = new DataBase();
	}

	/**
	 * Smarty Element 設定
	 * @param
	 * @return
	 */
	function setElements()
	{
		//登録フォーム作成
		$this->_form = new HTML_QuickForm('form', 'post', $_SERVER['PHP_SELF'], '', null, true);

		//検索条件を作成
		$this->_form->addElement('text', 'ihea_request_date_to', null, array('size' => 10, 'maxlength' => '10', 'id' => 'ihea_request_date_to'));
		$this->_form->addElement('text', 'ihea_request_date_from', null, array('size' => 10, 'maxlength' => '10', 'id' => 'ihea_request_date_from'));
		$this->_form->addElement('text', 'ihea_mail_address', null, array('size' => 30, 'maxlength' => '50', 'id' => 'ihea_mail_address'));
		$pref_ar = array(""=>"対応状況を選択してください",
						"0"=>"未対応", "1"=>"対応済");
		$this->_form->addElement('select','ihea_responseflg', null, $pref_ar,array('id' => 'ihea_responseflg'));
	}

	/**
	 * IDからフォーム情報取得
	 * @param	ihea_frm_id
	 * @param	ihea_mail_address
	 * @return	array
	 */
	function getData($ihea_frm_id = null, $ihea_mail_address = null,$ihea_responseflg = null, $ihea_request_date = null) {
		$data  = array();
		$where = array();
 	 	$sql = "select ihea_id
 	 				  ,ihea_frm_id
 	 				  ,ihea_responseflg
 	 				  ,ihea_response_date
 	 				  ,ihea_response_comment
 	 				  ,ihea_mail_address
 	 				  ,ihea_request_date
 	 		    from   mdl_extMdlFormGnrtr_tbl_inquiry_header
 	 		    where  ihea_active = 1 ";
		if ($ihea_frm_id) {
			$where[] = $ihea_frm_id;
			$sql	.= " and ihea_frm_id = ?";
		}
		if ($ihea_mail_address) {
			$where[] = "%" . trim($ihea_mail_address) . "%";
			$sql	.= " and ihea_mail_address Like ?";
		}

		if (isset($ihea_responseflg) && $ihea_responseflg !=null) {
			$where[] = "%" . trim($ihea_responseflg) . "%";
				$sql	.= " and ihea_responseflg like ?";
		}

		if ($ihea_request_date['to'] && $ihea_request_date['from']) {
			$where[] = trim($ihea_request_date['to']);
			$where[] = trim($ihea_request_date['from']).' 23:59:59';
			$sql	.= " and ihea_request_date between ? and ?";
		}else if($ihea_request_date['to'] && !$ihea_request_date['from']){
			$where[] = trim($ihea_request_date['to']);
			$where[] = date('Y-m-d H:i:s');
			$sql	.= " and ihea_request_date between ? and ?";
		}else if(!$ihea_request_date['to'] && $ihea_request_date['from']){
			$where[] = trim($ihea_request_date['from'].' 23:59:59');
			$sql	.= " and ihea_request_date <= ?";
		}

 	 	$sql .= " order by ihea_responseflg
 	 					  ,ihea_request_date desc
 	 					  ,ihea_id desc ";
		$result = $this->db->prepare($sql, $where);
		if ($result) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				if(isset($row) && is_array($row) && count($row) > 0) {
					if ($row['ihea_mail_address'] != "") {
						$row["ihea_mail_address"] = ereg_replace(UNITS_REPLACE_FLG, "　", $row['ihea_mail_address']);
					}
					$data[$row["ihea_id"]] = $row;
				}
			}
		}
		return $data;
	}

	/**
	 * CSV出力用なタイトルを取得
	 * @param	ihea_frm_id
	 * @param	ihea_mail_address
	 * @return	array
	 */
	function gettitleData($ihea_frm_id = null, $ihea_mail_address = null) {
		$data  = array();
		$where = array();
 	 	$sql = "select distinct tp.prt_part_label
 	 				  ,tp.prt_part_order
 	 				  ,tp.prt_part_id
 	 		    from   mdl_extMdlFormGnrtr_tbl_part as tp
 	 		    	  ,mdl_extMdlFormGnrtr_tbl_inquiry_detail as td
 	 		    	  ,mdl_extMdlFormGnrtr_tbl_inquiry_header as th
 	 		    where  1 = 1
 	 		    and	   tp.prt_frm_id  = th.ihea_frm_id
 	 		    and	   tp.prt_frm_id  = td.idet_frm_id
 	 		    and	   tp.prt_part_id = td.idet_part_id
 	 		    and	   tp.prt_part_type not in('fileupload', 'line', 'guidetext') ";
		if ($ihea_frm_id) {
			$where[] = $ihea_frm_id;
			$sql	.= " and th.ihea_frm_id = ?";
		}
		if ($ihea_mail_address) {
			$where[] = "%" . trim($ihea_mail_address) . "%";
			$sql	.= " and ihea_mail_address Like ?";
		}
 	 	$sql .= " order by tp.prt_part_order ";

		$result = $this->db->prepare($sql, $where);
		if ($result) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$data[] = array('prt_part_label' => $row['prt_part_label'],
								'prt_part_id' => $row['prt_part_id']
				  				);
			}
		}
		return $data;
	}

	/**
	 * CSV出力用な明細データを取得
	 * @param	ihea_frm_id
	 * @param	ihea_mail_address
	 * @return	array
	 */
	function getcsvData($ihea_frm_id = null, $ihea_mail_address = null)	{
		$data  = array();
		$where = array();

 	 	$sql = "select ihea_id
 	 				  ,ihea_responseflg
 	 				  ,ihea_response_date
 	 				  ,ihea_response_comment
 	 				  ,ihea_mail_address
 	 				  ,ihea_request_date
 	 		    from mdl_extMdlFormGnrtr_tbl_inquiry_header
 	 		    where 1 = 1 ";
		if ($ihea_frm_id) {
			$where[] = $ihea_frm_id;
			$sql	.= " and ihea_frm_id = ?";
		}
		if ($ihea_mail_address) {
			$where[] = "%" . trim($ihea_mail_address) . "%";
			$sql	.= " and ihea_mail_address Like ?";
		}
 	 	$sql .= " order by ihea_responseflg
 	 					  ,ihea_request_date desc
 	 					  ,ihea_id desc ";

		$result = $this->db->prepare($sql, $where);
		if ($result) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$ihea_response_date = "";
				$ihea_request_date = "";
				$ihea_responseflg = "";

				if($row['ihea_responseflg'] == "0"){
					$ihea_responseflg = "未対応";
				}else{
					$ihea_responseflg = "対応済み";
				}

				if($row['ihea_response_date'] != ""){
                    $ihea_response_date = date('Y-m-d H:i', strtotime($row['ihea_response_date']));
				}

				if($row['ihea_request_date'] != ""){
					$ihea_request_date = date('Y-m-d H:i', strtotime($row['ihea_request_date']));
				}

				$data[] = array('0' => $row['ihea_id'],
								'1' => $ihea_responseflg,
								'2' => $ihea_response_date,
								'3' => $row['ihea_response_comment'],
								'4' => ereg_replace(UNITS_REPLACE_FLG, "　", $row['ihea_mail_address']),
								'5' => $ihea_request_date,
								);
			}
		}
		return $data;
	}

	/**
	 * パーツに対する値取得
	 * @param	ihea_frm_id
	 * @param	ihea_mail_address
	 * @return	array
	 */
	function getvalueData($ihea_frm_id = null, $ihea_mail_address = null)	{
		$data  = array();
		$where = array();
 	 	$sql = "select th.ihea_id
 	 				  ,td.idet_part_id
					  ,tp.prt_part_label
					  ,td.idet_part_value
					  ,tp.prt_part_type
					  ,tp.prt_part_sub_type
					  ,tp.prt_part_fullname_kanaflg
					  ,tp.prt_part_datetime_fromtoflg
				from   mdl_extMdlFormGnrtr_tbl_inquiry_header as th
					  ,mdl_extMdlFormGnrtr_tbl_inquiry_detail as td
					  ,mdl_extMdlFormGnrtr_tbl_part as tp
 	 		    where  td.idet_ihea_id = th.ihea_id
 	 		    and	   td.idet_part_id = tp.prt_part_id
 	 		    and	   td.idet_frm_id  = tp.prt_frm_id
 	 		    and	   tp.prt_part_type not in('fileupload', 'line', 'guidetext') ";
		if ($ihea_frm_id) {
			$where[] = $ihea_frm_id;
			$sql	.= " and td.idet_frm_id = ?";
		}
		if ($ihea_mail_address) {
			$where[] = "%" . trim($ihea_mail_address) . "%";
			$sql	.= " and ihea_mail_address Like ?";
		}
 	 	$sql .= " order by td.idet_ihea_id
 	 					  ,tp.prt_part_order ";

		$result = $this->db->prepare($sql, $where);
		if ($result) {
			$data = array('' => '');
			//戻りデータを作成
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$key	 = $row['ihea_id'] . $row['idet_part_id'];
				$data[$key] = array('ihea_id' 						=> $row['ihea_id'],
				                    'idet_part_id' 					=> $row['idet_part_id'],
				                    'prt_part_label' 				=> $row['prt_part_label'],
									'idet_part_value' 				=> $row['idet_part_value'],
									'prt_part_type' 				=> $row['prt_part_type'],
									'prt_part_sub_type' 			=> $row['prt_part_sub_type'],
									'prt_part_fullname_kanaflg' 	=>$row['prt_part_fullname_kanaflg'],
									'prt_part_datetime_fromtoflg' 	=> $row['prt_part_datetime_fromtoflg']
									);
			}
		}
		return $data;
	}

	//フォーム名前の取得
	function getFormName($form_id)
	{
		$frm_name = "";
		/*
		$sql = 'SELECT frm_name FROM mdl_extMdlFormGnrtr_tbl_form Where frm_id =' . $form_id;
		$result = $this->db->query($sql);
		*/
		$sql = 'SELECT frm_name FROM mdl_extMdlFormGnrtr_tbl_form Where frm_id = ?';
		$result = $this->db->prepare($sql, array($form_id));
		if ($result) {
			while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC)) {
				$frm_name = $row['frm_name'];
			}
		}
		return $frm_name;
	}

	/**
	 *	ページ付け
	 *	@author	Konno emi
	 *  @param	 	$pg_limit		int　	1ページの表示件数
	 *  @param	 	$current_page　	int　	表示ページ
	 *  @param	 	$data　			array　	データ
	 *  @param	 	$method　		string　送信メソッド（'POST'/'GET'、デフォルト='GET'）
	 *  @param	 	$urlvar　		string　パラメータ名（任意、デフォルト= 'FROM'）
	 * 	@return	array			表示データ＆ページ情報
	 *  							$rtn['pagedata'] array	ページ表示用データ
	 *  							$rtn['pageNavi'] string	ページナビゲーションテキスト
	*/
	function formPager($pg_limit, $current_page, $data, $method='GET',$urlvar='pager_num')
	{
		$params = array('mode' => 'sliding',
					'perPage' => $pg_limit,
					'delta' => 2,
					'prevImg' => '&lt;&lt;&nbsp;&nbsp;',
					'nextImg' => '&nbsp;&gt;&gt; ',
					'urlVar' => $urlvar,
					'currentPage' => $current_page,
					"curPageSpanPre" => "<b>[",
					"curPageSpanPost" => "]</b>",
					'separator' => '&nbsp;',
					'clearIfVoid'=>true,
					'spacesBeforeSeparator' => 1,
					'spacesAfterSeparator' => 1,
					'itemData' => $data,
					'httpMethod' => $method,
					"firstPagePre" => "[",
					"firstPageText" => "先頭",
					"firstPagePost" => "]",
					"lastPagePre" => "[",
					"lastPageText" => "最後",
					"lastPagePost" => "]"
					);

		$rtn = array();
		$pager = & Pager::factory($params);
		//表示データ
		$rtn['pagedata'] = $pager->getPageData();

		//ページナビ
		$links = $pager->getLinks();
		$rtn['pageNavi'] = $links;

		return $rtn;

	}


	/****************************************************/
	/*    選択項目 集計結果対応                         */
	/****************************************************/
	/**
	 * フォームパーツに"select"タイプが存在するかチェック
	 *
	 * return true or false
	*/
	function existPartSelect($prt_frm_id){
		if(!is_numeric($prt_frm_id)){
			return false;
		}

		$sql = "SELECT COUNT(*) FROM mdl_extMdlFormGnrtr_tbl_part
				WHERE prt_part_active = 1
				AND prt_frm_id = ?
				AND prt_part_type LIKE 'select'";
		$result = $this->db->prepare($sql, array($prt_frm_id));
		$data = $result->fetchRow(DB_FETCHMODE_ASSOC);

		if($data['count'] > 0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * selectタイプのパーツ情報取得
	 */
	function getSelectPart($frm_id){
		if(!is_numeric($frm_id)){
			return false;
		}

		$sql = "SELECT  prt_part_id,    prt_frm_id,    prt_part_sub_type,    prt_part_label,    prt_part_select_value
				FROM    mdl_extMdlFormGnrtr_tbl_part
				WHERE   prt_part_active = 1 AND prt_frm_id = ?
				  AND   (prt_part_sub_type LIKE 'radiobutton' OR prt_part_sub_type LIKE 'dropdown' OR prt_part_sub_type LIKE 'checkbox')";

		$result = $this->db->prepare($sql, array($frm_id));
		if($result != null) {
			$rtn = null;
			while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
				$rtn[] = $row;
			}
			return $rtn;
		}else{
			return false;
		}
	}

	/**
	 * selectタイプ回答取得
	 */
	function getSelectAnswer($frm_id, $part_id){
		if(!is_numeric($part_id)){
			return false;
		}

		$sql = "SELECT  idet_part_value
				FROM    mdl_extMdlFormGnrtr_tbl_inquiry_detail
				WHERE   idet_frm_id = ? AND idet_part_id = ?";
		$prm[] = $frm_id;
		$prm[] = $part_id;

		$result = $this->db->prepare($sql, $prm);
		if($result != null) {
			$rtn = null;
			while($row = $result->fetchRow(DB_FETCHMODE_ASSOC)){
				$rtn[] = $row;
			}
			return $rtn;
		}else{
			return false;
		}
	}

}
?>