パブリックタグ登録申請サンプルサイト ソース解説

場所情報コード検索・選択ページ

概要

このファイルでは、申請者IDなどの条件を指定して修正申請の対象となるものを検索し、一覧表示する。また、一覧画面から対象を選択し、編集画面へと遷移する。その際、 パラメータとしてpubtag=yes又はpubtag=noを引き継ぐ。検索には場所情報コードAPI(APIのURLはhttp://ucopendb.gsi.go.jp/ucode/api/search.json?、使用法などはhttp://ucopendb.gsi.go.jp/ucode/help_with_API.htmlを参照)を使用する。

解説

検索用フォームは次のようになる。
    <form method="POST">
    <input type="hidden" name="pubtag" value="<?php h($_REQUEST['pubtag']); ?>"">
        <dl>
            <dt>申請者ID<span style="color:red;">*必須</span></dt>
            <dd><input type="text" name="search_param[applicant_id]" value="<?php h(_d($params,'applicant_id'));?>""></dd>
            <dt>場所情報コード</dt>
            <dd><input type="text" name="search_param[ucode]" value="<?php h(_d($params,'ucode'));?>""></dd>
            <dt>名称</dt>
            <dd><input type="text" name="search_param[name]" value="<?php h(_d($params,'name'));?>""></dd>
            <dt>住所</dt>
            <dd><input type="text" name="search_param[address]" value="<?php h(_d($params,'address'));?>""></dd>
            <dt>キーワード</dt>
            <dd><input type="text" name="search_param[feature]" value="<?php h(_d($params,'feature'));?>""></dd>
        </dl>
        <input type="submit" value="送信">
    </form>
ここで、value="<?php h(_d($params,'applicant_id'));?>"などとしているのは、指定された検索条件が予め入力された状態とするためである。検索条件は、 パラメータにsearch_param[applicant_id]、search_param[ucode]などとして指定されるが、PHPでは、これは$_REQUEST['search_param']に配列として格納され、$_REQUEST['search_param']['applicant_id']などとして取得できる。
    $params = array();
    if(isset($_REQUEST['search_param'])){
        foreach($_REQUEST['search_param'] as $pn => $pv){
            if($pv){
                $params[$pn] = $pv;
            }
        }
        $url = UCODE_SEARCH_API.http_build_query($params);
        $result = json_decode(file_get_contents($url));
    }
値が指定されていれば、場所情報コードAPIに検索条件として引き渡し、結果を得る。結果の表示は以下の部分で行われる。
</form>
    <?php endif;?>
<form action="input.php">
    <input type="hidden" name="pubtag" value="<?php h($_REQUEST['pubtag']); ?>"">
    <dl id="result_list">
    <?php foreach($result->items as $item):?>
        <dt><?php h($item->ucode)?><input type="submit" name="ucode[<?php h($item->"ucode)?>]" value="修正"></dt>
        <dd><?php if(isset($item->name))h($item->name)?><br>
            <?php if(isset($item->address))h($item->address)?><br>
        </dd>
    <?php endforeach;?>
    </dl>
</form>
「修正」ボタンを押すと、input.phpに遷移し、前ページから引き継いだパラメータaction、pubtag、およびucode[選択したコード]=修正を引き渡す。

ソースコード

<?php
include 'lib.php';
deny_path_info();
if(empty($_SESSION['token'])){
    header("Location: token.php");
    exit();
}

    $params = array();
    if(isset($_REQUEST['search_param'])){
        foreach($_REQUEST['search_param'] as $pn => $pv){
            if($pv){
                $params[$pn] = $pv;
            }
        }
        $url = UCODE_SEARCH_API.http_build_query($params);
        $result = json_decode(file_get_contents($url));
    }
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style=Type" content="text/css" />
<meta name="viewport" content="width=device-width" >
<title><?php h(SITE_NAME);?></title>
<link rel="stylesheet" href="base.css" />
</head>
<style>
th{
    min-width: 4em;
}
#result_list dt{
    font-size: 0.9em;
    border-top: 1px dotted #888;
    background-color: #eff;
}
</style>
<body>

<div id="contents">
<h2><?php h(SITE_NAME);?></h2>

<?php if(isset($result) && !$result):?>
検索に失敗しました。
<?php elseif(isset($result) && $result && $result->status == 200): ?>
<div>
<h3>検索結果</h3>
    <?php if(false):?>
<form action="input.php">
    <input type="hidden" name="pubtag" value="<?php h($_REQUEST['pubtag']); ?>"">
    <table>
    <thead>
    <tr></tr><th>場所情報コード</th><th>名称</th><th>住所</th></tr>
    </thead>
    <tbody>
    <?php foreach($result->items as $item):?>
    <tr>
        <td><?php h($item->ucode)?></td>
        <td><?php if(isset($item->name))h($item->name)?></td>
        <td><?php if(isset($item->address))h($item->address)?></td>
        <td><input type="submit" name="ucode[<?php h($item->"ucode)?>]" value="修正"></td>
    </tr>
    <?php endforeach;?>
    </tbody>
    </table>
</form>
    <?php endif;?>
<form action="input.php">
    <input type="hidden" name="pubtag" value="<?php h($_REQUEST['pubtag']); ?>"">
    <dl id="result_list">
    <?php foreach($result->items as $item):?>
        <dt><?php h($item->ucode)?><input type="submit" name="ucode[<?php h($item->"ucode)?>]" value="修正"></dt>
        <dd><?php if(isset($item->name))h($item->name)?><br>
            <?php if(isset($item->address))h($item->address)?><br>
        </dd>
    <?php endforeach;?>
    </dl>
</form>
</div>
<?php elseif(isset($result) && $result && $result->status == 404): ?>
検索条件に一致するものがありませんでした。
<?php elseif(isset($result) && $result && $result->status != 200): ?>
<?php h(_d($result->message)); ?>
<?php endif;?>
<?php if(isset($result)):?>
<hr>
<?php endif;?>
修正するコードを検索します。
<div style="background-color: #ffe">
<h3>検索条件</h3>
    <form method="POST">
    <input type="hidden" name="pubtag" value="<?php h($_REQUEST['pubtag']); ?>"">
        <dl>
            <dt>申請者ID<span style="color:red;">*必須</span></dt>
            <dd><input type="text" name="search_param[applicant_id]" value="<?php h(_d($params,'applicant_id'));?>""></dd>
            <dt>場所情報コード</dt>
            <dd><input type="text" name="search_param[ucode]" value="<?php h(_d($params,'ucode'));?>""></dd>
            <dt>名称</dt>
            <dd><input type="text" name="search_param[name]" value="<?php h(_d($params,'name'));?>""></dd>
            <dt>住所</dt>
            <dd><input type="text" name="search_param[address]" value="<?php h(_d($params,'address'));?>""></dd>
            <dt>キーワード</dt>
            <dd><input type="text" name="search_param[feature]" value="<?php h(_d($params,'feature'));?>""></dd>
        </dl>
        <input type="submit" value="送信">
    </form>
</div>
</div>
</body>
</html>