basic funictionality with published videos
[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                         $this->load->helper('url');
20                 }
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          * @return              array   a list of videos, each one being an assoc array with:
29          *   * id, name, title, duration, thumbs_count, default_thumb, views => from DB
30          *   * video_url => P2P-Tube video URl
31          *   * TODO: user_id, user_name
32          *   * thumbs => thumbnail images' URLs
33          */
34         public function get_videos_summary()
35         {
36                 $query = $this->db->query(
37                         'SELECT id, name, title, duration, user_id, views, thumbs_count,
38                                 default_thumb
39                         FROM `videos`
40                         ORDER BY name'); // TODO summary order
41                 $videos = $query->result_array();
42                 
43                 foreach ($videos as & $video)
44                 {
45                         // P2P-Tube Video URL
46                         $video['video_url'] = site_url(sprintf("video/watch/%d/%s",
47                                 $video['id'], $video['name']));
48                         
49                         // Thumbnails
50                         $video['thumbs'] = $this->get_thumbs($video['name'], 
51                                 $video['thumbs_count']);
52                 }
53                 
54                 return $videos;
55         }
56         
57         /**
58          * Retrieves information about a video.
59          *
60          * If $name does not match with the video's `name` from the DB an error is
61          * marked in the key 'err'. If it's NULL it is ignored.
62          *
63          * @access              public
64          * @param               string $id      video's `id` column from `videos` DB table
65          * @param               string $name    video's `name` column from `videos` DB
66          * table. NULL means there is no name provided.
67          * @return              array   an associative list with information about a video
68          * with the following keys:
69          *   * all columns form DB with some exceptions that are overwritten or new
70          *   * torrents => list of torrent file names formated as
71          * {name}_{format}.{default_video_ext}.{default_torrent_ext}
72          *   * user_name => TODO: user name from `users` table
73          *   * formats => list of formats like 1080p
74          *   * category_name => TODO: human-friendly category name
75          *   * tags => associative list of "tag => score"
76          *   * date => date and time when the video was created
77          *   * thumbs => thumbnail images' URLs
78          */
79         public function get_video($id, $name = NULL)
80         {
81                 $query = $this->db->query('SELECT * 
82                                                                 FROM `videos` 
83                                                                 WHERE id = ?', $id);
84                 $video = array();
85                 
86                 if ($query->num_rows() > 0)
87                 {
88                         $video = $query->row_array();
89                         if ($name !== NULL && $video['name'] != $name)
90                                 $video['err'] = 'INVALID_NAME';
91                 }
92                 else
93                 {
94                         $video['err'] = 'INVALID_ID';
95                         return $video;
96                 }
97                 
98                 // Convert JSON encoded string to arrays.
99                 $video['formats'] = json_decode($video['formats'], TRUE);
100                 $video['tags'] = json_decode($video['tags'], TRUE);
101                 asort($video['tags']);
102                 $video['tags'] = array_reverse($video['tags'], true);
103                 
104                 // Torrents
105                 $video['torrents'] = array();
106                 foreach ($video['formats'] as $format)
107                 {
108                         $ext = isset($format['ext']) ? 
109                                 $format['ext'] : $this->config->item('default_video_ext');
110                         $video['torrents'][] = site_url('data/torrents/'. $video['name'] . '_'
111                                 . $format['def'] . '.'. $ext
112                                 . '.'. $this->config->item('default_torrent_ext'));
113                 }
114                 
115                 // Thumbnails
116                 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
117                 
118                 return $video;
119         }
120         
121         public function get_thumbs($name, $count)
122         {
123                 $thumbs = array();
124                 
125                 for ($i=0; $i < $count; $i++)
126                         $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
127                 
128                 return $thumbs;
129         }
130 }
131
132 /* End of file videos_model.php */
133 /* Location: ./application/models/videos_model.php */