class.fare.phpの例

<?php
  //BusMapクラスインクルード
include_once("class.busmap.php");

/**
 * 料金管理用クラス
 *
 */
class Fare extends BusMap {

  private $db;
  private $user;
  /**
   * コンストラクタ
   *
   * @return unknown_type
   */
  private $fareTable,$majorStopsTable;

  function __construct() {
    parent::__construct();
    $this->db = parent::getXoopsDb();
    $this->user = parent::getXoopsUser();
    $this->fareTable = $this->db->prefix("BusMap2_Fare");
    $this->majorStopsTable = $this->db->prefix("DevBusMap2_MajorStops");
  }

  function writeFareDB($origin,$destination,$RouteID,$f) {
    global $xoopsUser;
    $uid = $xoopsUser->uid();
    $ctime = date("Y-m-d H:i:s");

    //乗車バス停,下車バス停,料金をdbb0fd_feeテーブルに登録
    $sql1 = sprintf("select * from %s where DepartureID=%d and ArrivalID=%d and RouteID=%d", $this->fareTable, $origin, $destination, $RouteID);
    $res = $this->db->query($sql1) or die($xoopsDB->error());
    $temp =  $this->db->getRowsNum($res);
    if($this->db->getRowsNum($res)==0){
      $sql = sprintf("INSERT INTO %s (DepartureID, ArrivalID, RouteID, Fare, UID,Ctime) VALUES(%d, %d, %d, %d, %d, '%s')", $this->fareTable, $origin, $destination, $RouteID, $f, $uid, $ctime);
    }else{
      $sql = sprintf("update %s set Fare = %d, UID= %d, Ctime='%s' where DepartureID=%d and ArrivalID=%d and RouteID=%d",$this->fareTable ,$f, $uid, $ctime, $origin, $destination, $RouteID);
    }
    $res = $this->db->query($sql) or die($this->db->error() . $sql1 . $sql . $temp);
    return $temp;
  }

  /*
    路線IDを与えて,2つのバス停に対する料金の一覧を返す
    @param  $RuteID
    @return  Fare as 2d array of departure and arrival stopID
    $f[$i][$j]
  */
  function getFareByRouteID($RouteID) {

    $sql = "select * from " . $this->fareTable ." where RouteID=${RouteID}";
    $res = $this->db->query($sql) or die($xoopsDB->error());
    while($row = $this->db->fetchArray($res)) {
      $i = $row['DepartureID']; $j = $row['ArrivalID'];
      if(!isset($f[$i])) $f[$i] = array();
      $f[$i][$j] = $row['Fare'];
    }
    return $f;
  }

  /*
    路線IDを与えて,バス停IDの列と名称,あるいはIDのみ,
  あるいは主要バス停かどうかの情報も加えて返す.
    @param $RouteID:int,   $IDonly:bool,   $withMajorFlag:bool
    @return array of ('ID','Name') or array of 'ID'
  */
  function getStopNamesByRouteID($RouteID,$IDonly=false,$withMajorFlag=false) {
    global $xoopsDB,$xoopsUser;
    $ConnTable = parent::getBusStopConnectionsTable();
    $PlatTable = parent::getPlatformTable();
    $BusStopTable = parent::getBusStopTable();

    $majorStops = array('0');
    $stops = array();
    if($withMajorFlag) $majorStops = $this->getMajorStops($RouteID,true);
    $sql = "select c.OrderNum, s.ID, s.Name
         from $ConnTable c, $PlatTable p, $BusStopTable s
         where c.PlatformID = p.ID and p.BusStopID = s.ID and
         c.RouteID = ${RouteID} order by c.OrderNum";
    $res = $this->db->query($sql) or die($this->db->error());
    while($row = $this->db->fetchArray($res)) {
      if($IDonly) {
        $stops[$row['OrderNum']] = $row['ID'];
      } elseif($withMajorFlag) {
        $flag = in_array($row['ID'],$majorStops)?1:0;
        $stops[$row['OrderNum']] =
          array('ID'=>$row['ID'],'Name'=>$row['Name'],'major'=>$flag);
      } else {
        $stops[$row['OrderNum']] =
          array('ID'=>$row['ID'],'Name'=>$row['Name']);
      }
    }
    return $stops;
  }


  /**
   * プラットフォーム取得
   *
   * @param $route_id
   * @return unknown_type
   */
  function getPlatforms($route_id) {
    $result = array();
    $sql = "";
    //$sql = $sql."SELECT bscT.ID ConnectionID, pT.ID PlatformID, bsT.Name PlatformName, '' NullTime, '' IntervalTime "; //DEL 20091118 naitou
    $sql = $sql."SELECT bscT.ID ConnectionID, pT.ID PlatformID, bsT.Name PlatformName, bscT.DestinationID, bscT.OrderNum, '' NullTime, '' IntervalTime ";
    $sql = $sql."FROM ".parent::getBusStopConnectionsTable()." AS bscT ";
    $sql = $sql."INNER JOIN ".parent::getPlatformTable()." AS pT ON bscT.PlatformID = pT.ID ";
    $sql = $sql."INNER JOIN ".parent::getBusStopTable()." AS bsT ON pT.BusStopID = bsT.ID ";
    $sql = $sql."WHERE bscT.RouteID = '".$route_id."' ";
    $sql = $sql."ORDER BY bscT.OrderNum ";

    $stmt = $this->db->query($sql) or die($this->db->error());
    while($row = $this->db->fetchArray($stmt)) {
      //array_push($result,array('connection' => $row['ConnectionID'], 'platformId' => $row['PlatformID'], 'platformName' => $row['PlatformName'], 'time' => $row['NullTime'], 'interval' => $row['IntervalTime'])); //DEL 20091118 naitou
      array_push($result,array('connection' => $row['ConnectionID'], 'platformId' => $row['PlatformID'], 'platformName' => $row['PlatformName'], 'time' => $row['NullTime'], 'interval' => $row['IntervalTime'], 'destinationId' => $row['DestinationID'], 'orderNum' => $row['OrderNum'])); //ADD 20091118 naitou
    }
    $this->db->freeRecordSet($stmt);
    return $result;
  }

  /**
   * 主要停留所取得
   *
   * @param $route_id
   * @return unknown_type
   */
  function getMajorStops($route_id,$IDonly=false) {
    if(!is_numeric($route_id)) return array();
    $stops = $this->getStopNamesByRouteID($route_id,true);
    $result = array();
    $sql = "select * from " . $this->majorStopsTable . " where RouteID=" .
      intval($route_id) . " order by OrderNum ";
    $res = $this->db->query($sql);
    while($row = $this->db->fetchArray($res)) {
      if($IDonly){
        $result[] = $row['StopID'];
      } else {
        if(in_array($row['StopID'],$stops))
          $result[] = array("OrderNum" => $row("OrderNum"), "StopID"=>$row["StopID"]);
      }
    }
    return $result;
  }

  /**
   * 主要停留所登録
   *
   * @param $route_id, $stops: key="orderNum", value="stop_id"
   * @return bool
   */
  function putMajorStops($route_id, $stops) {
    $routeID = intval($route_id);
    if(!$this->isRoute($routeID)) return;
    $this->db->query('delete from ' . $this->majorStopsTable . ' where RouteID=' . intval($routeID));
    foreach($stops as $k => $val) {
      if(is_numeric($k) && is_numeric($val)) {
      $this->db->query('insert into ' . $this->majorStopsTable . " (RouteID,OrderNum,StopID) values (${routeID},${k},${val})") or die($this->db->error());
      }
    }
  }
}

?>