%
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
require_once("lib/ob.php");
require_once("lib/common.php");
require_once("lib/stringcheck.php");
require_once('DB.php');
require_once('Smarty.class.php');
$DB = DB::connect( $Config['db.type'] . '://' . $Config['db.user'] .':' . $Config['db.password'] . '@' . $Config['db.host'] . '/' . $Config['db.name'] );
if( DB::isError( $DB ) ){
die( $DB->getMessage() );
}
$result = $DB->setFetchMode( DB_FETCHMODE_ASSOC );
if( DB::isError( $result ) ){
die( $result->getMessage() );
}
$result = $DB->simpleQuery("SET CLIENT_ENCODING TO '" . $Config['db.charcode'] . "'");
$result = $DB->simpleQuery("SET TIME ZONE 'Asia/Tokyo'");
$result = $DB->simpleQuery("SET DATESTYLE TO 'ISO'");
if( DB::isError( $result ) ){
die( $result->toString() );
}
// 地区の選択データ
$region = "";
$region_list = array(
'' => '(全て)',
'hokkaido' => '北海道地区',
'tohoku' => '東北地区',
'kanto' => '関東地区',
'chubu' => '中部地区',
'kinki' => '近畿地区',
'chugoku' => '中国地区',
'shikoku' => '四国地区',
'kyushu' => '九州地区',
'okinawa' => '沖縄地区');
$region_to_pref = array(
'hokkaido' => array('北海道'),
'tohoku' => array('青森県','岩手県','宮城県','秋田県','山形県','福島県'),
'kanto' => array('茨城県','栃木県','群馬県','埼玉県','千葉県','東京都','神奈川県'),
'chubu' => array('新潟県','富山県','石川県','福井県','山梨県','長野県','岐阜県','静岡県','愛知県','三重県'),
'kinki' => array('滋賀県','京都府','大阪府','兵庫県','奈良県','和歌山県'),
'chugoku' => array('鳥取県','島根県','岡山県','広島県','山口県'),
'shikoku' => array('徳島県','香川県','愛媛県','高知県'),
'kyushu' => array('福岡県','佐賀県','長崎県','熊本県','大分県','宮崎県','鹿児島県'),
'okinawa' => array('沖縄県')
);
// 都道府県の選択データ
$prefecture = "";
$prefecture_list = array(
'' => '(全て)',
'北海道' => '北海道',
'青森県' => '青森県',
'岩手県' => '岩手県',
'宮城県' => '宮城県',
'秋田県' => '秋田県',
'山形県' => '山形県',
'福島県' => '福島県',
'茨城県' => '茨城県',
'栃木県' => '栃木県',
'群馬県' => '群馬県',
'埼玉県' => '埼玉県',
'千葉県' => '千葉県',
'東京都' => '東京都',
'神奈川県' => '神奈川県',
'新潟県' => '新潟県',
'富山県' => '富山県',
'石川県' => '石川県',
'福井県' => '福井県',
'山梨県' => '山梨県',
'長野県' => '長野県',
'岐阜県' => '岐阜県',
'静岡県' => '静岡県',
'愛知県' => '愛知県',
'三重県' => '三重県',
'滋賀県' => '滋賀県',
'京都府' => '京都府',
'大阪府' => '大阪府',
'兵庫県' => '兵庫県',
'奈良県' => '奈良県',
'和歌山県' => '和歌山県',
'鳥取県' => '鳥取県',
'島根県' => '島根県',
'岡山県' => '岡山県',
'広島県' => '広島県',
'山口県' => '山口県',
'徳島県' => '徳島県',
'香川県' => '香川県',
'愛媛県' => '愛媛県',
'高知県' => '高知県',
'福岡県' => '福岡県',
'佐賀県' => '佐賀県',
'長崎県' => '長崎県',
'熊本県' => '熊本県',
'大分県' => '大分県',
'宮崎県' => '宮崎県',
'鹿児島県' => '鹿児島県',
'沖縄県' => '沖縄県');
Normalize($_REQUEST);
function Normalize(&$ary) {
foreach( $ary as $key => $val ) {
if ( is_array($val) ) {
Normalize($val);
$ary[$key] = $val;
} else {
$ary[$key] = Normalize_Japanese($val);
}
}
}
// 地区での検索
if ( $_REQUEST['region'] ) {
$search_type = $region_list[$_REQUEST['region']];
$region = $_REQUEST['region'];
$prefs = "'" . join("','",$region_to_pref[$region]) . "'";
if ( !$prefs ) {
die("使い方が誤っています。");
}
$where = "prefecture in ($prefs)";
}
// 都道府県での検索
if ( $_REQUEST['prefecture'] ) {
$search_type = $prefecture_list[$_REQUEST['prefecture']];
$prefecture = $_REQUEST[prefecture];
$where = "prefecture = '" . pg_escape_string($_REQUEST[prefecture]) . "'";
}
// キーワード検索
if ( $_REQUEST['keyword'] ) {
// 日本語半角カナ -> 全角
// 英数字(21h - 7Eh)を全角から半角に変換
$_REQUEST[keyword] = mb_convert_kana($_REQUEST[keyword] ,"KVa","EUC");
// 英小文字 -> 大文字
$keyword = mb_convert_case($_REQUEST[keyword],MB_CASE_UPPER);
// 全角空白を半角空白に変換
$keyword = mb_ereg_replace('/ /',' ',$keyword);
if ( $search_type ) { $search_type .= " 且つ "; }
$search_type .= "キーワード[" . htmlspecialchars($_REQUEST[keyword]) . "]";
$keywords = preg_split('/\s+/',$keyword);
foreach( $keywords as $kwd ) {
$wheres[] = "searchstr like '%" . pg_escape_string($kwd) . "%'";
}
if ( $where ) { $where .= " AND "; }
$where .= join(' AND ',$wheres);
}
// 郵便番号での検索
if ( $_REQUEST['zip1'] || $_REQUEST['zip2'] ) {
if ( $search_type ) { $search_type .= " 且つ "; }
$search_type .= "郵便番号[" . htmlspecialchars($_REQUEST[zip1]) ."-". htmlspecialchars($_REQUEST[zip2]) . "]";
if ( $where ) { $where .= " AND "; }
if ( $_REQUEST[zip1] ) {
if ( $_REQUEST[zip2] ) {
$where .= "zip = '" . pg_escape_string($_REQUEST[zip1]) . "-" . pg_escape_string($_REQUEST[zip2]) . "'";
} else {
$where .= "zip like '" . pg_escape_string($_REQUEST[zip1]) . "-%'";
}
} else {
$where .= "zip like '%-" . pg_escape_string($_REQUEST[zip2]) . "'";
}
}
if ( !$where ) {
$where = "true";
//die("使い方が誤っています。");
}
// ページング処理
$sql = "select count(*) from shopdata where $where";
$result = $DB->query( $sql );
if( DB::isError( $result ) ){
die( $result->getMessage() );
}
$row = $result->fetchRow();
$total_count = $row[count];
$count_per_page = 10;
$total_pages = ceil($total_count / $count_per_page);
if ( $_REQUEST[page] == '' ) {
$page = 1;
} else {
$page = $_REQUEST[page];
}
if ( $page < 0 ) { $page = 0; }
if ( $page > $total_pages ) { $page = $total_pages; }
$page_offset = ($page-1) * $count_per_page;
$start_count = $page_offset + 1;
if ( $start_count > $total_count ) {
$start_count = $total_count;
}
$end_count = $start_count + $count_per_page - 1;
if ( $end_count > $total_count ) {
$end_count = $total_count;
}
if ( $page >= 1 ) {
$limit = "limit $count_per_page offset $page_offset";
}
if ( $page > 1 ) {
$p = $page-1;
$page_bar .= "<<前の{$count_per_page}件 ";
} else if ( $total_pages > 1 ) {
$page_bar .= " ";
}
if ( $total_pages > 1 ) {
for($i=1;$i<=$total_pages;$i++) {
if ( $i == $page ) {
$page_bar .= "$i ";
} else if ( $page-10 <= $i && $i < $page+10 ) {
// 現在ページの前後10ページを最大として表示
$page_bar .= "$i ";
}
}
}
if ( $total_pages > 1 && $page < $total_pages ) {
$p = $page+1;
$page_bar .= "次の{$count_per_page}件>>";
} else if ( $total_pages > 1 ) {
$page_bar .= " ";
}
# ページ出力
$Tpl = new Smarty();
$Tpl->template_dir = './template/';
$Tpl->compile_dir = './template_c/';
$Tpl->config_dir = './tconfig/';
$Tpl->cache_dir = './cache/';
$Tpl->left_delimiter = "{{";
$Tpl->right_delimiter = "}}";
$Tpl->caching = false;
$Tpl->assign( 'search_type', $search_type );
$Tpl->assign( 'page_bar', $page_bar );
//$Tpl->register_modifier("array_join", "smarty_modifier_array_join");
// 都道府県の選択
if ( !$prefecture ) {
$sql = "select prefecture from shopdata where $where group by prefecture";
$result = $DB->query( $sql );
if( DB::isError( $result ) ){
die( $result->getMessage() );
}
if ( $result->numRows() == 1 ) {
$row = $result->fetchRow();
$prefecture = $row[prefecture];
}
}
if ( $prefecture ) {
// 都道府県が指定されたら地区は自動的に決められる。
foreach( $region_to_pref as $region => $prefs ) {
foreach( $prefs as $pref ) {
if ( $prefecture == $pref ) {
break(2);
}
}
}
}
// データ取得
$sql = "select * from shopdata where $where $orderby $limit";
//print "$sql
\n";;
$result = $DB->query( $sql );
if( DB::isError( $result ) ){
die( $result->getMessage() );
}
$Tpl->assign( 'total_count', $total_count );
$Tpl->assign( 'start_count', $start_count );
$Tpl->assign( 'end_count', $end_count );
$Tpl->assign( 'region_list', $region_list );
$Tpl->assign( 'region', $region );
$Tpl->assign( 'prefecture_list', $prefecture_list );
$Tpl->assign( 'prefecture', $prefecture );
$Tpl->assign( 'keyword', $_REQUEST[keyword] );
$detail_count = 0;
while($row = $result->fetchRow()) {
$shop = array();
$shop[shopid] = $row[shop_id];
$shop[prefecture] = $row[prefecture];
$shop[address] = $row[address];
$shop[shopname] = $row[shopname];
$shop[zip] = $row[zip];
$shop[tel] = $row[tel];
$shop[shoptype] = $row[shoptype];
if ( preg_match('/レンタル/',$shop[shoptype]) ) {
$shop[rental] = 'yes';
}
if ( preg_match('/販売/',$shop[shoptype]) ) {
$shop[sell] = 'yes';
}
$shop[homepage] = $row[homepage];
$shop[open_hours] = $row[open_hours];
$shop[holidays] = $row[holidays];
$shop[shop_image] = $row[shop_image];
$shop[map_image] = $row[map_image];
$shop[pr] = $row[pr];
if ( $shop[shop_image] || $shop[map_image] || $shop[pr] ) {
$shop[detail] = 'yes';
$detail_count++;
}
$shoplist[] = $shop;
}
$Tpl->assign( 'shoplist', $shoplist );
$Tpl->assign( 'detail_count', $detail_count );
$tpl_html = 'result.html';
if ( $Tpl->template_exists( $tpl_html ) ){
$Tpl->display( $tpl_html );
} else {
$Tpl->display( 'error.html' );
}
%>