4 * Class Videos_model models videos information from the DB
7 * @author Călin-Andrei Burloiu
9 class Videos_model extends CI_Model {
12 public function __construct()
14 if ($this->db === NULL)
16 $this->load->library('singleton_db');
17 $this->db = $this->singleton_db->connect();
20 $this->load->helper('url');
24 * Retrieves information about a set of videos which are going to be
25 * displayed in the catalog.
27 * TODO: filter, limit, ordering parameters
28 * @param int $category_id DB category ID
31 * @return array a list of videos, each one being an assoc array with:
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>
40 public function get_videos_summary($category_id, $offset, $count)
42 $this->load->helper('text');
44 $query = $this->db->query(
45 'SELECT id, name, title, duration, user_id, views, thumbs_count,
50 LIMIT ?, ?', // TODO summary order
51 array(intval($category_id), $offset, $count));
53 if ($query->num_rows() > 0)
54 $videos = $query->result_array();
58 foreach ($videos as & $video)
61 $video['video_url'] = site_url(sprintf("watch/%d/%s",
62 $video['id'], $video['name']));
65 $video['thumbs'] = $this->get_thumbs($video['name'],
66 $video['thumbs_count']);
69 //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
70 $video['shorted_title'] = character_limiter($video['title'], 50);
72 // TODO: user information
73 $video['user_name'] = 'TODO';
79 public function get_videos_count($category_id)
81 $query = $this->db->query(
82 'SELECT COUNT(*) count
84 WHERE category_id = ?',
87 if ($query->num_rows() > 0)
88 return $row = $query->row()->count;
95 * Retrieves information about a video.
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.
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:
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>
120 public function get_video($id, $name = NULL)
122 $this->load->helper('video');
124 $query = $this->db->query('SELECT *
129 if ($query->num_rows() > 0)
131 $video = $query->row_array();
132 if ($name !== NULL && $video['name'] != $name)
133 $video['err'] = 'INVALID_NAME';
137 $video['err'] = 'INVALID_ID';
141 // Convert JSON encoded string to arrays.
142 $video['assets'] = json_decode($video['formats'], TRUE);
143 unset($video['formats']);
144 $video['tags'] = json_decode($video['tags'], TRUE);
145 asort($video['tags']);
146 $video['tags'] = array_reverse($video['tags'], TRUE);
148 // Sort assets by their megapixels number.
149 function access_function($a) { return $a['res']; }
150 function assets_cmp($a, $b)
151 { return megapixels_cmp($a, $b, "access_function"); }
152 usort($video['assets'], "assets_cmp");
155 $video['url'] = array();
156 foreach ($video['assets'] as & $asset)
158 $def = substr($asset['res'], strpos($asset['res'], 'x') + 1) . 'p';
159 $asset['src'] = site_url('data/torrents/'. $video['name'] . '_'
160 . $def . '.'. $asset['ext']
161 . '.'. $this->config->item('default_torrent_ext'));
165 $categories = $this->config->item('categories');
166 $category_name = $categories[ intval($video['category_id']) ];
167 $video['category_title'] = $category_name ?
168 $this->lang->line("ui_categ_$category_name") : $category_name;
171 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
173 // TODO: user information
174 $video['user_name'] = 'TODO';
180 * Increment a video parameter from DB: `views`, `likes` or `dislikes`.
182 * @param int $id DB video id
183 * @param string $param DB parameter column name.
186 public function inc_video_var($id, $var)
188 // TODO error report if query returns FALSE
189 $this->db->query('UPDATE `videos` '
190 . 'SET `'. $var. '`=`'. $var. '`+1 '
194 public function get_thumbs($name, $count)
198 for ($i=0; $i < $count; $i++)
199 $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
205 /* End of file videos_model.php */
206 /* Location: ./application/models/videos_model.php */