OSDN Git Service

modified: epgwakealarm.php 省電力少し修正
[epgrec/epgrec.git] / Keyword.class.php
1 <?php
2 include_once('config.php');
3 include_once( INSTALL_PATH . "/DBRecord.class.php" );
4 include_once( INSTALL_PATH . "/reclib.php" );
5 include_once( INSTALL_PATH . "/Reservation.class.php" );
6 include_once( INSTALL_PATH . '/Settings.class.php' );
7 include_once( INSTALL_PATH . '/recLog.inc.php' );
8
9 class Keyword extends DBRecord {
10         
11         public function __construct($property = null, $value = null ) {
12                 try {
13                         parent::__construct(KEYWORD_TBL, $property, $value );
14                 }
15                 catch( Exception $e ) {
16                         throw $e;
17                 }
18         }
19         
20         static public function search(  $keyword = "", 
21                                                                         $use_regexp = false,
22                                                                         $type = "*", 
23                                                                         $category_id = 0,
24                                                                         $channel_id = 0,
25                                                                         $weekofday = 7,
26                                                                         $prgtime = 24,
27                                                                         $limit = 300 ) {
28                 $sts = Settings::factory();
29                 
30                 $dbh = @mysql_connect($sts->db_host, $sts->db_user, $sts->db_pass );
31                 
32                 // ちょっと先を検索する
33                 $options = " WHERE starttime > '".date("Y-m-d H:i:s", time() + $sts->padding_time + 60 )."'";
34                 
35                 if( $keyword != "" ) {
36                         if( $use_regexp ) {
37                                 $options .= " AND CONCAT(title,description) REGEXP '".mysql_real_escape_string($keyword)."'";
38                         }
39                         else {
40                                 $options .= " AND CONCAT(title,description) like _utf8'%".mysql_real_escape_string($keyword)."%' collate utf8_unicode_ci";
41                         }
42                 }
43                 
44                 if( $type != "*" ) {
45                         $options .= " AND type = '".$type."'";
46                 }
47                 
48                 if( $category_id != 0 ) {
49                         $options .= " AND category_id = '".$category_id."'";
50                 }
51                 
52                 if( $channel_id != 0 ) {
53                         $options .= " AND channel_id = '".$channel_id."'";
54                 }
55                 
56                 if( $weekofday != 7 ) {
57                         $options .= " AND WEEKDAY(starttime) = '".$weekofday."'";
58                 }
59                 
60                 if( $prgtime != 24 ) {
61                         $options .= " AND time(starttime) BETWEEN cast('".sprintf( "%02d:00:00", $prgtime)."' as time) AND cast('".sprintf("%02d:59:59", $prgtime)."' as time)";
62                 }
63                 
64                 $options .= " ORDER BY starttime ASC  LIMIT ".$limit ;
65                 
66                 $recs = array();
67                 try {
68                         $recs = DBRecord::createRecords( PROGRAM_TBL, $options );
69                 }
70                 catch( Exception $e ) {
71                         throw $e;
72                 }
73                 return $recs;
74         }
75         
76         private function getPrograms() {
77                 if( $this->__id == 0 ) return false;
78                 $recs = array();
79                 try {
80                          $recs = self::search( trim($this->keyword), $this->use_regexp, $this->type, $this->category_id, $this->channel_id, $this->weekofday, $this->prgtime );
81                 }
82                 catch( Exception $e ) {
83                         throw $e;
84                 }
85                 return $recs;
86         }
87         
88         public function reservation() {
89                 if( $this->__id == 0 ) return;
90                 
91                 $precs = array();
92                 try {
93                         $precs = $this->getPrograms();
94                 }
95                 catch( Exception $e ) {
96                         throw $e;
97                 }
98                 // 一気に録画予約
99                 foreach( $precs as $rec ) {
100                         try {
101                                 if( $rec->autorec ) {
102                                         Reservation::simple( $rec->id, $this->__id, $this->autorec_mode );
103                                         reclog( "Keyword.class::キーワードID".$this->id."の録画が予約された");
104                                         usleep( 100 );          // あんまり時間を空けないのもどう?
105                                 }
106                         }
107                         catch( Exception $e ) {
108                                 // 無視
109                         }
110                 }
111         }
112         
113         public function delete() {
114                 if( $this->id == 0 ) return;
115                 
116                 $precs = array();
117                 try {
118                         $precs = $this->getPrograms();
119                 }
120                 catch( Exception $e ) {
121                         throw $e;
122                 }
123                 // 一気にキャンセル
124                 foreach( $precs as $rec ) {
125                         try {
126                                 $reserve = new DBRecord( RESERVE_TBL, "program_id", $rec->id );
127                                 // 自動予約されたもののみ削除
128                                 if( $reserve->autorec ) {
129                                         Reservation::cancel( $reserve->id );
130                                         usleep( 100 );          // あんまり時間を空けないのもどう?
131                                 }
132                         }
133                         catch( Exception $e ) {
134                                 // 無視
135                         }
136                 }
137                 try {
138                         parent::delete();
139                 }
140                 catch( Exception $e ) {
141                         throw $e;
142                 }
143         }
144
145         // staticなファンクションはオーバーライドできない
146         static function createKeywords( $options = "" ) {
147                 $retval = array();
148                 $arr = array();
149                 try{
150                         $tbl = new self();
151                         $sqlstr = "SELECT * FROM ".$tbl->__table." " .$options;
152                         $result = $tbl->__query( $sqlstr );
153                 }
154                 catch( Exception $e ) {
155                         throw $e;
156                 }
157                 if( $result === false ) throw new exception("レコードが存在しません");
158                 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
159                         array_push( $retval, new self('id', $row['id']) );
160                 }
161                 return $retval;
162         }
163         
164         public function __destruct() {
165                 parent::__destruct();
166         }
167 }
168 ?>