OSDN Git Service

modified: viewer.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
8 class Keyword extends DBRecord {
9         
10         public function __construct($property = null, $value = null ) {
11                 try {
12                         parent::__construct(KEYWORD_TBL, $property, $value );
13                 }
14                 catch( Exception $e ) {
15                         throw $e;
16                 }
17         }
18         
19         private function getPrograms() {
20                 if( $this->id == 0 ) return false;
21                 
22                 // ちょっと先を検索する
23                 $options = " WHERE starttime > '".date("Y-m-d H:i:s", time() + $this->settings->padding_time + 120 )."'";
24                 
25                 if( $this->keyword != "" ) {
26                         if( $this->use_regexp ) {
27                                 $options .= " AND CONCAT(title,description) REGEXP '".mysql_real_escape_string($this->keyword)."'";
28                         }
29                         else {
30                                 $options .= " AND CONCAT(title,description) like '%".mysql_real_escape_string($this->keyword)."%'";
31                         }
32                 }
33                 
34                 if( $this->type != "*" ) {
35                         $options .= " AND type = '".$this->type."'";
36                 }
37                 
38                 if( $this->category_id != 0 ) {
39                         $options .= " AND category_id = '".$this->category_id."'";
40                 }
41                 
42                 if( $this->channel_id != 0 ) {
43                         $options .= " AND channel_id = '".$this->channel_id."'";
44                 }
45                 
46                 $options .= " ORDER BY starttime ASC";
47                 
48                 $recs = array();
49                 try {
50                         $recs = DBRecord::createRecords( PROGRAM_TBL, $options );
51                 }
52                 catch( Exception $e ) {
53                         throw $e;
54                 }
55                 
56                 return $recs;
57         }
58         
59         
60         public function reservation() {
61                 if( $this->id == 0 ) return;
62                 
63                 $precs = array();
64                 try {
65                         $precs = $this->getPrograms();
66                 }
67                 catch( Exception $e ) {
68                         throw $e;
69                 }
70                 if( count($precs) < 300 ) {
71                         // 一気に録画予約
72                         foreach( $precs as $rec ) {
73                                 try {
74                                         if( $rec->autorec ) {
75                                                 Reservation::simple( $rec->id, $this->id );
76                                                 usleep( 100 );          // あんまり時間を空けないのもどう?
77                                         }
78                                 }
79                                 catch( Exception $e ) {
80                                         // 無視
81                                 }
82                         }
83                 }
84                 else {
85                         throw new Exception( "300件以上の自動録画は実行できません" );
86                 }
87         }
88         
89         public function delete() {
90                 if( $this->id == 0 ) return;
91                 
92                 $precs = array();
93                 try {
94                         $precs = $this->getPrograms();
95                 }
96                 catch( Exception $e ) {
97                         throw $e;
98                 }
99                 // 一気にキャンセル
100                 foreach( $precs as $rec ) {
101                         try {
102                                 $reserve = new DBRecord( RESERVE_TBL, "program_id", $rec->id );
103                                 // 自動予約されたもののみ削除
104                                 if( $reserve->autorec ) {
105                                         Reservation::cancel( $reserve->id );
106                                         usleep( 100 );          // あんまり時間を空けないのもどう?
107                                 }
108                         }
109                         catch( Exception $e ) {
110                                 // 無視
111                         }
112                 }
113                 try {
114                         parent::delete();
115                 }
116                 catch( Exception $e ) {
117                         throw $e;
118                 }
119         }
120
121         // staticなファンクションはオーバーライドできない
122         static function createKeywords( $options = "" ) {
123                 $retval = array();
124                 $arr = array();
125                 try{
126                         $tbl = new self();
127                         $sqlstr = "SELECT * FROM ".$tbl->table." " .$options;
128                         $result = $tbl->__query( $sqlstr );
129                 }
130                 catch( Exception $e ) {
131                         throw $e;
132                 }
133                 if( $result === false ) throw new exception("レコードが存在しません");
134                 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
135                         array_push( $retval, new self('id', $row['id']) );
136                 }
137                 return $retval;
138         }
139         
140         public function __destruct() {
141                 parent::__destruct();
142         }
143 }
144 ?>