article engine for static pages was integrated
[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         private $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                 $this->load->helper('url');
21         }
22         
23         /**
24          * Retrieves information about a set of videos which are going to be
25          * displayed in the catalog.
26          *
27          * TODO: filter, limit, ordering parameters
28          * @param               int $category_id        DB category ID
29          * @param               int $offset
30          * @param               int $count
31          * @return              array   a list of videos, each one being an assoc array with:
32          * <ul>
33          *   <li>id, name, title, duration, thumbs_count, default_thumb, views => from DB</li>
34          *   <li>shorted_title => ellipsized title</li>
35          *   <li>video_url => P2P-Tube video URl</li>
36          *   <li>TODO: user_id, user_name</li>
37          *   <li>thumbs => thumbnail images' URLs</li>
38          * </ul>
39          */
40         public function get_videos_summary($category_id, $offset, $count)
41         {
42                 $this->load->helper('text');
43                 
44                 $query = $this->db->query(
45                         'SELECT id, name, title, duration, user_id, views, thumbs_count,
46                                 default_thumb
47                         FROM `videos`
48                         WHERE category_id = ?
49                         ORDER BY name
50                         LIMIT ?, ?', // TODO summary order 
51                         array(intval($category_id), $offset, $count)); 
52                 
53                 if ($query->num_rows() > 0)
54                         $videos = $query->result_array();
55                 else
56                         return NULL;
57                 
58                 foreach ($videos as & $video)
59                 {
60                         // P2P-Tube Video URL
61                         $video['video_url'] = site_url(sprintf("watch/%d/%s",
62                                 $video['id'], $video['name']));
63                         
64                         // Thumbnails
65                         $video['thumbs'] = $this->get_thumbs($video['name'], 
66                                 $video['thumbs_count']);
67                                 
68                         // Ellipsized title
69                         //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
70                         $video['shorted_title'] = character_limiter($video['title'], 50);
71                         
72                         // TODO: user information
73                         $video['user_name'] = 'TODO';
74                 }
75                 
76                 return $videos;
77         }
78         
79         public function get_videos_count($category_id)
80         {
81                 $query = $this->db->query(
82                         'SELECT COUNT(*) count
83                         FROM `videos`
84                         WHERE category_id = ?',
85                         $category_id);
86                 
87                 if ($query->num_rows() > 0)
88                         return $row = $query->row()->count;
89                 
90                 // Error
91                 return NULL;
92         }
93         
94         /**
95          * Retrieves information about a video.
96          *
97          * If $name does not match with the video's `name` from the DB an error is
98          * marked in the key 'err'. If it's NULL it is ignored.
99          *
100          * @access              public
101          * @param               string $id      video's `id` column from `videos` DB table
102          * @param               string $name    video's `name` column from `videos` DB
103          * table. NULL means there is no name provided.
104          * @return              array   an associative list with information about a video
105          * with the following keys:
106          * <ul>
107          *   <li>all columns form DB with some exceptions that are overwritten or new</li>
108          *   <li>content is moved in assets</li>
109          *   <li>assets => list of associative lists where each one represents a</li>
110          * video asset having keys: "src", "res", "par" and "ext". Value of key
111          * "src" is the video torrent formated as
112          * {name}_{format}.{video_ext}.{default_torrent_ext}</li>
113          *   <li>user_name => TODO: user name from `users` table</li>
114          *   <li>category_title => a human-friendly category name</li>
115          *   <li>tags => associative list of "tag => score"</li>
116          *   <li>date => date and time when the video was created</li>
117          *   <li>thumbs => thumbnail images' URLs</li>
118          * </ul>
119          */
120         public function get_video($id, $name = NULL)
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                 // Torrents
147                 $video['url'] = array();
148                 foreach ($video['assets'] as & $asset)
149                 {
150                         $def = substr($asset['res'], strpos($asset['res'], 'x') + 1) . 'p';
151                         $asset['src'] = site_url('data/torrents/'. $video['name'] . '_'
152                                 . $def . '.'. $asset['ext']
153                                 . '.'. $this->config->item('default_torrent_ext'));
154                 }
155                 
156                 // Category title
157                 $categories = $this->config->item('categories');
158                 $category_name = $categories[ intval($video['category_id']) ];
159                 $video['category_title'] = $category_name ?
160                         $this->lang->line("ui_categ_$category_name") : $category_name;
161                 
162                 // Thumbnails
163                 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
164                 
165                 // TODO: user information
166                 $video['user_name'] = 'TODO';
167                 
168                 return $video;
169         }
170         
171         public function get_thumbs($name, $count)
172         {
173                 $thumbs = array();
174                 
175                 for ($i=0; $i < $count; $i++)
176                         $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
177                 
178                 return $thumbs;
179         }
180 }
181
182 /* End of file videos_model.php */
183 /* Location: ./application/models/videos_model.php */