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