<?php
//  ------------------------------------------------------------------------ //
//                XOOPS Cube Realty - Realty Management System               //
//                    Copyright (c) 2011 Bluemoon inc.                       //
//               auther Yoshi Sakai <http://www.bluemooninc.jp/>             //
//  ------------------------------------------------------------------------ //
//  This program is free software; you can redistribute it and/or modify     //
//  it under the terms of the GNU General Public License as published by     //
//  the Free Software Foundation; either version 2 of the License, or        //
//  (at your option) any later version.                                      //
//                                                                           //
//  You may not change or alter any portion of this comment or credits       //
//  of supporting developers from this source code or any supporting         //
//  source code which is considered copyrighted (c) material of the          //
//  original comment or credit authors.                                      //
//                                                                           //
//  This program is distributed in the hope that it will be useful,          //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
//  GNU General Public License for more details.                             //
//                                                                           //
//  You should have received a copy of the GNU General Public License        //
//  along with this program; if not, write to the Free Software              //
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
//  ------------------------------------------------------------------------ //
/**
 * Real Class
 */
class model_stations {
	var $tblname;
	var $id;
	var $sortname;
	var $sortorder;
	var $start=0;
	var $where_array_and = array();
	var $where_array_or = array();
	var $parPage=10;
	var $data_records=array();
	var $station_names;
	var $data_count=0;
	var $sql;
	
	function model_stations($tblname,$search_railline=NULL,$search_station_from=NULL,$search_station_to=NULL ) {
		global $xoopsDB;
		$this->tblname = $xoopsDB->prefix($tblname);
		if ($search_station_from && $search_station_to){
			$station_sort_from = $this->get_station_sort($search_railline,$search_station_from);
			$station_sort_to = $this->get_station_sort($search_railline,$search_station_to);
			if ( $station_sort_to < $station_sort_from ){
				$i = $station_sort_to;
				$station_sort_to = $station_sort_from;
				$station_sort_from = $i;
			} 
			if ($station_sort_from && $station_sort_to){
				$this->where_array_and[] = " station_sort>=$station_sort_from ";
				$this->where_array_and[] = " station_sort<=$station_sort_to ";  
			}
		}else{
			if ($search_railline) $this->where_array_and[] = " line_name=%$search_railline% ";  
			if ($search_station_from && !$search_station_to) $this->where_array_and[] = " station_name=$search_station_from ";  
			if (!$search_station_from && $search_station_to) $this->where_array_and[] = " station_name=$search_station_to ";  
		}
		$wstr = '';
		if ($this->where_array_and){
			$wstr .= " WHERE (" . implode(" AND ", $this->where_array_and) . " )";
		}
		$this->sql = "SELECT * FROM ".$this->tblname . $wstr . " ORDER BY station_sort ";
		if ($wstr=='') return NULL;
		$result = $xoopsDB->query($this->sql);
      	if (!$result) return NULL;
    	$this->data_records = array();
    	$this->station_names ='';
    	while($row = $xoopsDB->fetchArray($result)) {  // １レコードの情報を取得
     		$this->data_records[] = $row;
     		if ( $this->station_names!='' ) $this->station_names .= ",";
     		$this->station_names .= "'".$row['station_name']."'";
     	}
     	//echo $this->station_names; die;
	}
	function get_station_sort($search_railline=NULL,$search_station=NULL){
		global $xoopsDB;
		$sql = "SELECT station_sort FROM " . $this->tblname . " WHERE line_name LIKE '%$search_railline%' AND station_name='".$search_station."'";
		$result = $xoopsDB->query($sql);
      	if (!$result) return NULL;		
      	list($station_sort) = $xoopsDB->fetchRow($result);
      	return $station_sort;
	}
	
}
?>