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();
19 $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 * @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
34 public function get_videos_summary()
36 $query = $this->db->query(
37 'SELECT id, name, title, duration, user_id, views, thumbs_count,
40 ORDER BY name'); // TODO summary order
41 $videos = $query->result_array();
43 foreach ($videos as & $video)
46 $video['video_url'] = site_url(sprintf("video/watch/%d/%s",
47 $video['id'], $video['name']));
50 $video['thumbs'] = $this->get_thumbs($video['name'],
51 $video['thumbs_count']);
58 * Retrieves information about a video.
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.
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
79 public function get_video($id, $name = NULL)
81 $query = $this->db->query('SELECT *
86 if ($query->num_rows() > 0)
88 $video = $query->row_array();
89 if ($name !== NULL && $video['name'] != $name)
90 $video['err'] = 'INVALID_NAME';
94 $video['err'] = 'INVALID_ID';
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);
105 $video['torrents'] = array();
106 foreach ($video['formats'] as $format)
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'));
116 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
121 public function get_thumbs($name, $count)
125 for ($i=0; $i < $count; $i++)
126 $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
132 /* End of file videos_model.php */
133 /* Location: ./application/models/videos_model.php */