Skip to content

Commit

Permalink
初期インポート
Browse files Browse the repository at this point in the history
  • Loading branch information
stz2012 committed Aug 1, 2012
0 parents commit e96a0bf
Show file tree
Hide file tree
Showing 218 changed files with 32,319 additions and 0 deletions.
193 changes: 193 additions & 0 deletions DBRecord.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php
include_once( 'config.php' );
include_once( 'Settings.class.php' );

class DBRecord {
protected $__table;

protected $__record_data;
protected $__f_dirty;

public $__id;

protected static $__dbh = false;
protected static $__settings = false;

function __construct( $table, $property = null, $value = null ) {

$this->__f_dirty = false;
$this->__record_data = false;

if( self::$__dbh === false ) {
self::$__settings = Settings::factory();
self::$__dbh = @mysql_connect( self::$__settings->db_host , self::$__settings->db_user, self::$__settings->db_pass );
if( self::$__dbh === false ) throw new exception( "construct:データベースに接続できない" );
$sqlstr = "use ".self::$__settings->db_name;
$res = $this->__query($sqlstr);
if( $res === false ) throw new exception("construct: " . $sqlstr );
$sqlstr = "set NAMES utf8";
$res = $this->__query($sqlstr);
}
$this->__table = self::$__settings->tbl_prefix.$table;

if( ($property == null) || ($value == null) ) {
// レコードを特定する要素が指定されない場合はid=0として空のオブジェクトを作成する
$this->__id = 0;
}
else {
$sqlstr = "SELECT * FROM ".$this->__table.
" WHERE ".mysql_real_escape_string( $property ).
"='".mysql_real_escape_string( $value )."'";

$res = $this->__query( $sqlstr );
$this->__record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
if( $this->__record_data === false ) throw new exception( "construct:".$this->__table."".$property."=".$value."はありません" );
// 最初にヒットした行のidを使用する
$this->__id = $this->__record_data['id'];
}

return;
}

function createTable( $tblstring ) {
$sqlstr = "use ".self::$__settings->db_name;
$res = $this->__query($sqlstr);
if( $res === false ) throw new exception("createTable: " . $sqlstr );
$sqlstr = "CREATE TABLE IF NOT EXISTS ".$this->__table." (" .$tblstring.") DEFAULT CHARACTER SET 'utf8'";
$result = $this->__query( $sqlstr );
if( $result === false ) throw new exception( "createTable:テーブル作成失敗" );
}

protected function __query( $sqlstr ) {
if( self::$__dbh === false ) throw new exception( "__query:DBに接続されていない" );

$res = @mysql_query( $sqlstr, self::$__dbh );
if( $res === false ) throw new exception( "__query:DBクエリ失敗:".$sqlstr );
return $res;
}

function fetch_array( $property , $value, $options = null ) {
$retval = array();

$sqlstr = "SELECT * FROM ".$this->__table.
" WHERE ".mysql_real_escape_string( $property ).
"='".mysql_real_escape_string( $value )."'";

if( $options != null ) {
$sqlstr .= "AND ".$options;
}
$res = $this->__query( $sqlstr );
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
array_push( $retval, $row );
}

return $retval;
}

function __set( $property, $value ) {
if( $property === "id" ) throw new exception( "set:idの変更は不可" );
// id = 0なら空の新規レコード作成
if( $this->__id == 0 ) {
$sqlstr = "INSERT INTO ".$this->__table." VALUES ( )";
$res = $this->__query( $sqlstr );
$this->__id = mysql_insert_id();

// $this->__record_data読み出し
$sqlstr = "SELECT * FROM ".$this->__table.
" WHERE id = '".$this->__id."'";

$res = $this->__query( $sqlstr );
$this->__record_data = mysql_fetch_array( $res , MYSQL_ASSOC );
}
if( $this->__record_data === false ) throw new exception("set: DBの異常?" );

if( array_key_exists( $property, $this->__record_data ) ) {
$this->__record_data[$property] = mysql_real_escape_string($value);
$this->__f_dirty = true;
}
else {
throw new exception("set:$property はありません" );
}
}

function __get( $property ) {
if( $this->__id == 0 ) throw new exception( "get:無効なid" );
if( $property === "id" ) return $this->__id;
if( $this->__record_data === false ) throw new exception( "get: 無効なレコード" );
if( ! array_key_exists( $property, $this->__record_data ) ) throw new exception( "get: $propertyは存在しません" );

return stripslashes($this->__record_data[$property]);
}

function delete() {
if( $this->__id == 0 ) throw new exception( "delete:無効なid" );

$sqlstr = "DELETE FROM ".$this->__table." WHERE id='".$this->__id."'";
$this->__query( $sqlstr );
$this->__id = 0;
$this->__record_data = false;
$this->__f_dirty = false;
}

function update() {
if( $this->__id != 0 ) {
if( $this->__f_dirty ) {
$sqlstr = "UPDATE ".$this->__table." SET";
foreach( $this->__record_data as $property => $value ) {
if( $property === "id" ) continue;
$sqlstr .= " ".$property." = '".$value."',";
}
$sqlstr = rtrim($sqlstr, "," );
$sqlstr .= " WHERE id = '".$this->__id."'";
$res = $this->__query($sqlstr);
if( $res === false ) throw new exception( "close: アップデート失敗" );
}
$this->__f_dirty = false;
}
}

// countを実行する
static function countRecords( $table, $options = "" ) {
try{
$tbl = new self( $table );
$sqlstr = "SELECT COUNT(*) FROM " . $tbl->__table ." " . $options;
$result = $tbl->__query( $sqlstr );
}
catch( Exception $e ) {
throw $e;
}
if( $result === false ) throw new exception("COUNT失敗");
$retval = mysql_fetch_row( $result );
return $retval[0];
}

// DBRecordオブジェクトを返すstaticなメソッド
static function createRecords( $table, $options = "" ) {
$retval = array();
$arr = array();
try{
$tbl = new self( $table );
$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( $table, 'id', $row['id'] ) );
}
return $retval;
}

// デストラクタ
function __destruct() {
// 呼び忘れに対応
if( $this->__id != 0 ) {
$this->update();
}
$this->__id = 0;
$this->__record_data = false;
}
}
?>
168 changes: 168 additions & 0 deletions Keyword.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?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' );
include_once( INSTALL_PATH . '/recLog.inc.php' );

class Keyword extends DBRecord {

public function __construct($property = null, $value = null ) {
try {
parent::__construct(KEYWORD_TBL, $property, $value );
}
catch( Exception $e ) {
throw $e;
}
}

static public function search( $keyword = "",
$use_regexp = false,
$type = "*",
$category_id = 0,
$channel_id = 0,
$weekofday = 7,
$prgtime = 24,
$limit = 300 ) {
$sts = Settings::factory();

$dbh = @mysql_connect($sts->db_host, $sts->db_user, $sts->db_pass );

// ちょっと先を検索する
$options = " WHERE starttime > '".date("Y-m-d H:i:s", time() + $sts->padding_time + 60 )."'";

if( $keyword != "" ) {
if( $use_regexp ) {
$options .= " AND CONCAT(title,description) REGEXP '".mysql_real_escape_string($keyword)."'";
}
else {
$options .= " AND CONCAT(title,description) like _utf8'%".mysql_real_escape_string($keyword)."%' collate utf8_unicode_ci";
}
}

if( $type != "*" ) {
$options .= " AND type = '".$type."'";
}

if( $category_id != 0 ) {
$options .= " AND category_id = '".$category_id."'";
}

if( $channel_id != 0 ) {
$options .= " AND channel_id = '".$channel_id."'";
}

if( $weekofday != 7 ) {
$options .= " AND WEEKDAY(starttime) = '".$weekofday."'";
}

if( $prgtime != 24 ) {
$options .= " AND time(starttime) BETWEEN cast('".sprintf( "%02d:00:00", $prgtime)."' as time) AND cast('".sprintf("%02d:59:59", $prgtime)."' as time)";
}

$options .= " ORDER BY starttime ASC LIMIT ".$limit ;

$recs = array();
try {
$recs = DBRecord::createRecords( PROGRAM_TBL, $options );
}
catch( Exception $e ) {
throw $e;
}
return $recs;
}

private function getPrograms() {
if( $this->__id == 0 ) return false;
$recs = array();
try {
$recs = self::search( trim($this->keyword), $this->use_regexp, $this->type, $this->category_id, $this->channel_id, $this->weekofday, $this->prgtime );
}
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;
}
// 一気に録画予約
foreach( $precs as $rec ) {
try {
if( $rec->autorec ) {
Reservation::simple( $rec->id, $this->__id, $this->autorec_mode );
reclog( "Keyword.class::キーワードID".$this->id."の録画が予約された");
usleep( 100 ); // あんまり時間を空けないのもどう?
}
}
catch( Exception $e ) {
// 無視
}
}
}

public function delete() {
if( $this->id == 0 ) return;

$precs = array();
try {
$precs = $this->getPrograms();
}
catch( Exception $e ) {
throw $e;
}
// 一気にキャンセル
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();
}
}
?>
Loading

0 comments on commit e96a0bf

Please sign in to comment.