<?php
/*
 * member/download/Form.class.php
 * 
 * CopyRight(C) 2010 Shopformer Development Team. All Right Reserved
 * URL: http://sourceforge.jp/projects/shopformer/
 * 
 * 
 * Mail: m_nakashima@users.sourceforge.jp
 * Auther: Masanori Nakashima
 * Last Update: 2010-06-18
 */
require_once(dirname(dirname(dirname(__FILE__)))
.DIRECTORY_SEPARATOR.'system'
.DIRECTORY_SEPARATOR.'login'
.DIRECTORY_SEPARATOR.'ModuleBase.class.php');
require_once( dirname(dirname(__FILE__))
.DIRECTORY_SEPARATOR.'PackageConfig.class.php');
require_once( dirname(dirname(__FILE__))
.DIRECTORY_SEPARATOR.'DaoMember.class.php');
require_once(dirname(dirname(dirname(__FILE__)))
.DIRECTORY_SEPARATOR.'system'
.DIRECTORY_SEPARATOR.'DataFileWriter.class.php');
class member_download_Form extends system_login_ModuleBase {
	function member_download_Form(){
		array_push($this->require_module_array,'database2.Connect');
	}
	function execute( & $request ) {
		// 変数取得
		$dbo 						= $request->getAttribute( 'dbo' );
		$member_class				= stripslashes( $_GET['mc'] );
		$select_type				= trim(stripslashes( $_GET['stype'] ));
		$session_name_csv_column	= MEMBER_SESSION_NAME_DOWNLOAD_CSV_COLUMN_DEFINITION . $member_class;
		if( 'q' != $select_type && 's' != $select_type ) {
			$select_type		= 'q';
		}
		
		// カラム定義
		$download_column_definition			= $GLOBALS['MEMBER_UPLOAD_COLUMN_DEFINITION'];
		$download_column_name_definition	= $GLOBALS['MEMBER_UPLOAD_COLUMN_NAME_DEFINITION'];
		// ダウンロードで作成する一時ファイルパス
		$downloadUri		= $request->getSession(MEMBER_SESSION_NAME_DOWNLOAD_CSV_FILE_URI);
		if ( strlen( $downloadUri ) > 0 ) {
			if ( system_DataFileWriter::delete( $request, $downloadUri, DIR_NAME_TMP ) ) {
			}
		}
		// セッション登録済みのカラム定義を確認
		$column_definition	= $request->getSession($session_name_csv_column);
		if( !is_array($column_definition) ) {
			$column_definition	= $download_column_definition;
		}
		// ダウンロードする対象クエリの確認
		$total_count		= 0;
		if( 'q' == $select_type ) {
			$load_count_query	= $request->getSession(MEMBER_SESSION_NAME_LOADCOUNT_QUERY);
			$load_list_query	= $request->getSession(MEMBER_SESSION_NAME_LOADLIST_QUERY);
		} else if( 's' == $select_type ) {
			$sessionSelectedHash			= $request->getSession( MEMBER_SESSION_NAME_SELECTED_LIST );
			if( !is_array($sessionSelectedHash) ) {
				$sessionSelectedHash		= array();
			}
			$query_condition	= " WHERE member_id IN ('" . implode( "','", array_keys($sessionSelectedHash) ) . "')";
			$load_count_query	= "SELECT COUNT(DISTINCT(member_id)) FROM " . TABLE_NAME_MEMBER . $query_condition;
			$load_list_query	= "SELECT DISTINCT(member_id) FROM " . TABLE_NAME_MEMBER . $query_condition;
		}
		if( strlen(trim($load_count_query)) == 0 || strlen(trim($load_count_query)) == 0 ) {
			$request->addLocaledError('system.error.common.invalidaccess',SPIDER_LOG_LEVEL_FATAL,array(''));
		} else {
			$total_count	= $dbo->queryOne($load_count_query);
			if ( $total_count === false ) {
				$request->addLocaledError('database2.error.common',SPIDER_LOG_LEVEL_FATAL,array($load_count_query));
			}
		}

		$request->setAttribute(MEMBER_SESSION_NAME_DOWNLOAD_CSV_COLUMN_DEFINITION, $column_definition );
		$request->setAttribute('member.download.total_count', $total_count );
		$request->setAttribute('member.download.member_class', $member_class );
		$request->setAttribute('member.download.select_type', $select_type );
	}
}
?>