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