search improved but not finished
[living-lab-site.git] / application / models / videos_model.php
1 <?php
2
3 /**
4  * Class Videos_model models videos information from the DB
5  *
6  * @category    Model
7  * @author              Călin-Andrei Burloiu
8  */
9 class Videos_model extends CI_Model {
10         public $db = NULL;
11         
12         public function __construct()
13         {
14                 if ($this->db === NULL)
15                 {
16                         $this->load->library('singleton_db');
17                         $this->db = $this->singleton_db->connect();
18                 }
19         }
20         
21         /**
22          * Retrieves information about a set of videos which are going to be
23          * displayed in the catalog.
24          *
25          * TODO: filter, limit, ordering parameters
26          * @param               int $category_id        DB category ID
27          * @param               int $offset
28          * @param               int $count
29          * @return              array   a list of videos, each one being an assoc array with:
30          * <ul>
31          *   <li>id, name, title, duration, thumbs_count, default_thumb, views => from DB</li>
32          *   <li>shorted_title => ellipsized title</li>
33          *   <li>video_url => P2P-Tube video URl</li>
34          *   <li>TODO: user_id, user_name</li>
35          *   <li>thumbs => thumbnail images' URLs</li>
36          * </ul>
37          */
38         public function get_videos_summary($category_id, $offset, $count)
39         {
40                 $this->load->helper('text');
41                 
42                 $query = $this->db->query(
43                         'SELECT id, name, title, duration, user_id, views, thumbs_count,
44                                 default_thumb
45                         FROM `videos`
46                         WHERE category_id = ?
47                         ORDER BY name
48                         LIMIT ?, ?', // TODO summary order 
49                         array(intval($category_id), $offset, $count)); 
50                 
51                 if ($query->num_rows() > 0)
52                         $videos = $query->result_array();
53                 else
54                         return NULL;
55                 
56                 foreach ($videos as & $video)
57                 {
58                         // P2P-Tube Video URL
59                         $video['video_url'] = site_url(sprintf("watch/%d/%s",
60                                 $video['id'], $video['name']));
61                         
62                         // Thumbnails
63                         $video['thumbs'] = $this->get_thumbs($video['name'], 
64                                 $video['thumbs_count']);
65                                 
66                         // Ellipsized title
67                         //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
68                         $video['shorted_title'] = character_limiter($video['title'], 50);
69                         
70                         // TODO: user information
71                         $video['user_name'] = 'TODO';
72                 }
73                 
74                 return $videos;
75         }
76         
77         public function get_videos_count($category_id)
78         {
79                 $query = $this->db->query(
80                         'SELECT COUNT(*) count
81                         FROM `videos`
82                         WHERE category_id = ?',
83                         $category_id);
84                 
85                 if ($query->num_rows() > 0)
86                         return $row = $query->row()->count;
87                 
88                 // Error
89                 return NULL;
90         }
91         
92         /**
93          * Retrieves information about a video.
94          *
95          * If $name does not match with the video's `name` from the DB an error is
96          * marked in the key 'err'. If it's NULL it is ignored.
97          *
98          * @access              public
99          * @param               string $id      video's `id` column from `videos` DB table
100          * @param               string $name    video's `name` column from `videos` DB
101          * table. NULL means there is no name provided.
102          * @return              array   an associative list with information about a video
103          * with the following keys:
104          * <ul>
105          *   <li>all columns form DB with some exceptions that are overwritten or new</li>
106          *   <li>content is moved in assets</li>
107          *   <li>assets => list of associative lists where each one represents a</li>
108          * video asset having keys: "src", "res", "par" and "ext". Value of key
109          * "src" is the video torrent formated as
110          * {name}_{format}.{video_ext}.{default_torrent_ext}</li>
111          *   <li>user_name => TODO: user name from `users` table</li>
112          *   <li>category_title => a human-friendly category name</li>
113          *   <li>tags => associative list of "tag => score"</li>
114          *   <li>date => date and time when the video was created</li>
115          *   <li>thumbs => thumbnail images' URLs</li>
116          * </ul>
117          */
118         public function get_video($id, $name = NULL)
119         {
120                 $this->load->helper('video');
121                 
122                 $query = $this->db->query('SELECT * 
123                                                                 FROM `videos` 
124                                                                 WHERE id = ?', $id);
125                 $video = array();
126                 
127                 if ($query->num_rows() > 0)
128                 {
129                         $video = $query->row_array();
130                         if ($name !== NULL && $video['name'] != $name)
131                                 $video['err'] = 'INVALID_NAME';
132                 }
133                 else
134                 {
135                         $video['err'] = 'INVALID_ID';
136                         return $video;
137                 }
138                 
139                 // Convert JSON encoded string to arrays.
140                 $video['assets'] = json_decode($video['formats'], TRUE);
141                 unset($video['formats']);
142                 $video['tags'] = json_decode($video['tags'], TRUE);
143                 asort($video['tags']);
144                 $video['tags'] = array_reverse($video['tags'], TRUE);
145                 
146                 // Sort assets by their megapixels number.
147                 function access_function($a) { return $a['res']; }
148                 function assets_cmp($a, $b) 
149                         { return megapixels_cmp($a, $b, "access_function"); }
150                 usort($video['assets'], "assets_cmp");
151                 
152                 // Torrents
153                 $video['url'] = array();
154                 foreach ($video['assets'] as & $asset)
155                 {
156                         $def = substr($asset['res'], strpos($asset['res'], 'x') + 1) . 'p';
157                         $asset['src'] = site_url('data/torrents/'. $video['name'] . '_'
158                                 . $def . '.'. $asset['ext']
159                                 . '.'. $this->config->item('default_torrent_ext'));
160                 }
161                 
162                 // Category title
163                 $categories = $this->config->item('categories');
164                 $category_name = $categories[ intval($video['category_id']) ];
165                 $video['category_title'] = $category_name ?
166                         $this->lang->line("ui_categ_$category_name") : $category_name;
167                 
168                 // Thumbnails
169                 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
170                 
171                 // TODO: user information
172                 $video['user_name'] = 'TODO';
173                 
174                 return $video;
175         }
176         
177         /**
178          * Increment a video parameter from DB: `views`, `likes` or `dislikes`.
179          * 
180          * @param int $id       DB video id
181          * @param string $param DB parameter column name.
182          * @return void
183          */
184         public function inc_video_var($id, $var)
185         {
186                 // TODO error report if query returns FALSE
187                 $this->db->query('UPDATE `videos` '
188                                                 . 'SET `'. $var. '`=`'. $var. '`+1 '
189                                                 . 'WHERE id='. $id); 
190         }
191         
192         public function get_thumbs($name, $count)
193         {
194                 $thumbs = array();
195                 
196                 for ($i=0; $i < $count; $i++)
197                         $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
198                 
199                 return $thumbs;
200         }
201
202         public function search_videos($str_search)
203         {
204                 // TODO offset, count for search
205                 $offset = 0;
206                 $count = 100;
207
208                 $str_search = trim($str_search);
209                 
210                 $query = $this->db->query(
211                         "SELECT id, name, title, duration, user_id, views, thumbs_count,
212                                 default_thumb
213                         FROM `videos`
214                         WHERE MATCH (title, description, tags) AGAINST (?)
215                         ORDER BY name
216                         LIMIT ?, ?",
217                         array($str_search, $offset, $count)); 
218                 
219                 if ($query->num_rows() > 0)
220                         $videos = $query->result_array();
221                 else
222                         return NULL;
223                 
224                 $this->load->helper('text');
225                 
226                 foreach ($videos as & $video)
227                 {
228                         // P2P-Tube Video URL
229                         $video['video_url'] = site_url(sprintf("watch/%d/%s",
230                                 $video['id'], $video['name']));
231                         
232                         // Thumbnails
233                         $video['thumbs'] = $this->get_thumbs($video['name'], 
234                                 $video['thumbs_count']);
235                                 
236                         // Ellipsized title
237                         //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
238                         $video['shorted_title'] = character_limiter($video['title'], 50);
239                         
240                         // TODO: user information
241                         $video['user_name'] = 'TODO';
242                 }
243                 
244                 return $videos;
245         }
246 }
247
248 /* End of file videos_model.php */
249 /* Location: ./application/models/videos_model.php */