view Keyword.class.php @ 106:57676bb30f64

add: 時間帯で絞り込みを追加
author Sushi-k <epgrec@park.mda.or.jp>
date Mon, 08 Mar 2010 18:07:19 +0900
parents cb7da56c4198
children 7a64d5e3baee
line wrap: on
line source

<?php
include_once('config.php');
include_once( INSTALL_PATH . "/DBRecord.class.php" );
include_once( INSTALL_PATH . "/reclib.php" );
include_once( INSTALL_PATH . "/Reservation.class.php" );
include_once( INSTALL_PATH . '/Settings.class.php' );

class Keyword extends DBRecord {
	
	public function __construct($property = null, $value = null ) {
		try {
			parent::__construct(KEYWORD_TBL, $property, $value );
		}
		catch( Exception $e ) {
			throw $e;
		}
	}
	
	private function getPrograms() {
		if( $this->id == 0 ) return false;
		
		// <c罎膣≪
		$options = " WHERE starttime > '".date("Y-m-d H:i:s", time() + $this->settings->padding_time + 120 )."'";
		
		if( $this->keyword != "" ) {
			if( $this->use_regexp ) {
				$options .= " AND CONCAT(title,description) REGEXP '".mysql_real_escape_string($this->keyword)."'";
			}
			else {
				$options .= " AND CONCAT(title,description) like '%".mysql_real_escape_string($this->keyword)."%'";
			}
		}
		
		if( $this->type != "*" ) {
			$options .= " AND type = '".$this->type."'";
		}
		
		if( $this->category_id != 0 ) {
			$options .= " AND category_id = '".$this->category_id."'";
		}
		
		if( $this->channel_id != 0 ) {
			$options .= " AND channel_id = '".$this->channel_id."'";
		}
		
		if( $this->weekofday != 7 ) {
			$options .= " AND WEEKDAY(starttime) = '".$this->weekofday."'";
		}
		
		if( $this->prgtime != 24 ) {
			$options .= " AND time(starttime) BETWEEN cast('".sprintf( "%02d:00:00", $this->prgtime)."' as time) AND cast('".sprintf("%02d:59:59", $this->prgtime)."' as time)";
		}
		
		$options .= " ORDER BY starttime ASC";
		
		$recs = array();
		try {
			$recs = DBRecord::createRecords( PROGRAM_TBL, $options );
		}
		catch( Exception $e ) {
			throw $e;
		}
		return $recs;
	}
	
	
	public function reservation() {
		if( $this->id == 0 ) return;
		
		$precs = array();
		try {
			$precs = $this->getPrograms();
		}
		catch( Exception $e ) {
			throw $e;
		}
		if( count($precs) < 300 ) {
			// 筝羂牙私膣
			foreach( $precs as $rec ) {
				try {
					if( $rec->autorec ) {
						Reservation::simple( $rec->id, $this->id, $this->autorec_mode );
						usleep( 100 );		// 障腥冴?
					}
				}
				catch( Exception $e ) {
					// ∴
				}
			}
		}
		else {
			throw new Exception( "300篁銀札筝牙祉絎茵с障" );
		}
	}
	
	public function delete() {
		if( $this->id == 0 ) return;
		
		$precs = array();
		try {
			$precs = $this->getPrograms();
		}
		catch( Exception $e ) {
			throw $e;
		}
		// 筝羂c潟祉
		foreach( $precs as $rec ) {
			try {
				$reserve = new DBRecord( RESERVE_TBL, "program_id", $rec->id );
				// 篋膣水
				if( $reserve->autorec ) {
					Reservation::cancel( $reserve->id );
					usleep( 100 );		// 障腥冴?
				}
			}
			catch( Exception $e ) {
				// ∴
			}
		}
		try {
			parent::delete();
		}
		catch( Exception $e ) {
			throw $e;
		}
	}

	// static<潟激с潟若若ゃс
	static function createKeywords( $options = "" ) {
		$retval = array();
		$arr = array();
		try{
			$tbl = new self();
			$sqlstr = "SELECT * FROM ".$tbl->table." " .$options;
			$result = $tbl->__query( $sqlstr );
		}
		catch( Exception $e ) {
			throw $e;
		}
		if( $result === false ) throw new exception("潟若絖障");
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
			array_push( $retval, new self('id', $row['id']) );
		}
		return $retval;
	}
	
	public function __destruct() {
		parent::__destruct();
	}
}
?>