<?php echo '<?xml version="1.0" encoding="utf-8"?>' ."\n" ?>
<?php mb_http_output('utf-8'); ?>

<!--
## (c)2007 Takuya NISHIMOTO (nishi@hil.t.u-tokyo.ac.jp)
## PostgreSQL + PHP + VoiceXML
## $Id: goods.php,v 1.2 2007/08/20 02:22:23 nishi Exp $
-->

<vxml version="2.0" xml:lang="ja">

<?php
$db_user = "galatea";
$db_pass = "galatea";
$db_host = "localhost";
$db_name = "galatea_db";
$db = pg_connect("host=$db_host port=5432 dbname=$db_name user=$db_user password=$db_pass");

function to_euc(&$src) {
  mb_convert_variables("euc-jp", mb_detect_encoding($src), $src);
}

function to_utf8(&$src) {
  mb_convert_variables("utf-8", mb_detect_encoding($src), $src);
}

if (isset($_REQUEST['item']) && $_REQUEST['item'] != '') {
   $item = $_REQUEST['item'];
   to_euc($item);
   $rs = pg_query($db, "SELECT name,price FROM goods WHERE code = $item");
   if ($row = pg_fetch_assoc($rs)) {
      $name  = $row['name'];
      $price = $row['price'];
      to_utf8($name);
   }
?>

<form id="answer">
  <block>
    <log>name: <?php echo $name ?>，price: <?php echo $price ?></log>
    <prompt>
      <?php echo $name ?>は<?php echo $price ?>円です。
      <break/>
    </prompt>
    <goto next="#ask"/>
  </block>
</form>

<?php } ?>

<form id="ask">
  <field name="item">
   <prompt timeout="20s"> 値段を聞きたい果物は何ですか？ </prompt>
   <grammar version="1.0" root="#item_rule">
    <rule id="item_rule">
     <one-of>

      <item> <token sym="まいくてすと">マイクテスト</token> </item>
<?php
$rs = pg_query($db, "SELECT code,name,yomi FROM goods");
while ($row = pg_fetch_assoc($rs)) {
  $code = $row['code'];
  $name = $row['name']; to_utf8($name);
  $yomi = $row['yomi']; to_utf8($yomi);
  echo "      <item> <token sym=\"$yomi\" slot=\"item\" value=\"$code\">$name</token> </item>\n";
}
?>      
     </one-of>
    </rule>
   </grammar>
  </field>
  <block>
    <submit method="post" next="<?php echo basename(__FILE__) ?>"/>
  </block>
</form>

</vxml> 
