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 * @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
35 public function get_videos_summary($category_id)
37 $this->load->helper('text');
39 $query = $this->db->query(
40 'SELECT id, name, title, duration, user_id, views, thumbs_count,
44 ORDER BY name', // TODO summary order
46 $videos = $query->result_array();
48 foreach ($videos as & $video)
51 $video['video_url'] = site_url(sprintf("watch/%d/%s",
52 $video['id'], $video['name']));
55 $video['thumbs'] = $this->get_thumbs($video['name'],
56 $video['thumbs_count']);
59 //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
60 $video['shorted_title'] = character_limiter($video['title'], 45);
67 * Retrieves information about a video.
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.
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
88 public function get_video($id, $name = NULL)
90 $query = $this->db->query('SELECT *
95 if ($query->num_rows() > 0)
97 $video = $query->row_array();
98 if ($name !== NULL && $video['name'] != $name)
99 $video['err'] = 'INVALID_NAME';
103 $video['err'] = 'INVALID_ID';
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);
114 $video['torrents'] = array();
115 foreach ($video['formats'] as $format)
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'));
125 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
130 public function get_thumbs($name, $count)
134 for ($i=0; $i < $count; $i++)
135 $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
141 /* End of file videos_model.php */
142 /* Location: ./application/models/videos_model.php */