3ab8592933957ade4e3e9daff0560a436ae5ab84
[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          *   * id, name, title, duration, thumbs_count, default_thumb, views => from DB
33          *   * shorted_title => ellipsized title
34          *   * video_url => P2P-Tube video URl
35          *   * TODO: user_id, user_name
36          *   * thumbs => thumbnail images' URLs
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'], 45);
69                 }
70                 
71                 return $videos;
72         }
73         
74         public function get_videos_count($category_id)
75         {
76                 $query = $this->db->query(
77                         'SELECT COUNT(*) count
78                         FROM `videos`
79                         WHERE category_id = ?',
80                         $category_id);
81                 
82                 if ($query->num_rows() > 0)
83                         return $row = $query->row()->count;
84                 
85                 // Error
86                 return NULL;
87         }
88         
89         /**
90          * Retrieves information about a video.
91          *
92          * If $name does not match with the video's `name` from the DB an error is
93          * marked in the key 'err'. If it's NULL it is ignored.
94          *
95          * @access              public
96          * @param               string $id      video's `id` column from `videos` DB table
97          * @param               string $name    video's `name` column from `videos` DB
98          * table. NULL means there is no name provided.
99          * @return              array   an associative list with information about a video
100          * with the following keys:
101          *   * all columns form DB with some exceptions that are overwritten or new
102          *   * torrents => list of torrent file names formated as
103          * {name}_{format}.{default_video_ext}.{default_torrent_ext}
104          *   * user_name => TODO: user name from `users` table
105          *   * formats => list of formats like 1080p
106          *   * category_name => TODO: human-friendly category name
107          *   * tags => associative list of "tag => score"
108          *   * date => date and time when the video was created
109          *   * thumbs => thumbnail images' URLs
110          */
111         public function get_video($id, $name = NULL)
112         {
113                 $query = $this->db->query('SELECT * 
114                                                                 FROM `videos` 
115                                                                 WHERE id = ?', $id);
116                 $video = array();
117                 
118                 if ($query->num_rows() > 0)
119                 {
120                         $video = $query->row_array();
121                         if ($name !== NULL && $video['name'] != $name)
122                                 $video['err'] = 'INVALID_NAME';
123                 }
124                 else
125                 {
126                         $video['err'] = 'INVALID_ID';
127                         return $video;
128                 }
129                 
130                 // Convert JSON encoded string to arrays.
131                 $video['formats'] = json_decode($video['formats'], TRUE);
132                 $video['tags'] = json_decode($video['tags'], TRUE);
133                 asort($video['tags']);
134                 $video['tags'] = array_reverse($video['tags'], true);
135                 
136                 // Torrents
137                 $video['torrents'] = array();
138                 foreach ($video['formats'] as $format)
139                 {
140                         $ext = isset($format['ext']) ? 
141                                 $format['ext'] : $this->config->item('default_video_ext');
142                         $video['torrents'][] = site_url('data/torrents/'. $video['name'] . '_'
143                                 . $format['def'] . '.'. $ext
144                                 . '.'. $this->config->item('default_torrent_ext'));
145                 }
146                 
147                 // Thumbnails
148                 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
149                 
150                 return $video;
151         }
152         
153         public function get_thumbs($name, $count)
154         {
155                 $thumbs = array();
156                 
157                 for ($i=0; $i < $count; $i++)
158                         $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
159                 
160                 return $thumbs;
161         }
162 }
163
164 /* End of file videos_model.php */
165 /* Location: ./application/models/videos_model.php */