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:
32 * * id, name, title, duration, thumbs_count, default_thumb, views => from DB
33 * * shorted_title => ellipsized title
34 * * video_url => P2P-Tube video URl
35 * * TODO: user_id, user_name
36 * * thumbs => thumbnail images' URLs
38 public function get_videos_summary($category_id, $offset, $count)
40 $this->load->helper('text');
42 $query = $this->db->query(
43 'SELECT id, name, title, duration, user_id, views, thumbs_count,
48 LIMIT ?, ?', // TODO summary order
49 array(intval($category_id), $offset, $count));
51 if ($query->num_rows() > 0)
52 $videos = $query->result_array();
56 foreach ($videos as & $video)
59 $video['video_url'] = site_url(sprintf("watch/%d/%s",
60 $video['id'], $video['name']));
63 $video['thumbs'] = $this->get_thumbs($video['name'],
64 $video['thumbs_count']);
67 //$video['shorted_title'] = ellipsize($video['title'], 45, 0.75);
68 $video['shorted_title'] = character_limiter($video['title'], 50);
74 public function get_videos_count($category_id)
76 $query = $this->db->query(
77 'SELECT COUNT(*) count
79 WHERE category_id = ?',
82 if ($query->num_rows() > 0)
83 return $row = $query->row()->count;
90 * Retrieves information about a video.
92 * If $name does not match with the video's `name` from the DB an error is
93 * marked in the key 'err'. If it's NULL it is ignored.
96 * @param string $id video's `id` column from `videos` DB table
97 * @param string $name video's `name` column from `videos` DB
98 * table. NULL means there is no name provided.
99 * @return array an associative list with information about a video
100 * with the following keys:
101 * * all columns form DB with some exceptions that are overwritten or new
102 * * formats content is moved in assets
103 * * assets => list of associative lists where each one represents a
104 * video asset having keys: "src", "res", "par" and "ext". Value of key
105 * "src" is the video torrent formated as
106 * {name}_{format}.{video_ext}.{default_torrent_ext}
107 * * user_name => TODO: user name from `users` table
108 * * category_title => a human-friendly category name
109 * * tags => associative list of "tag => score"
110 * * date => date and time when the video was created
111 * * thumbs => thumbnail images' URLs
113 public function get_video($id, $name = NULL)
115 $query = $this->db->query('SELECT *
120 if ($query->num_rows() > 0)
122 $video = $query->row_array();
123 if ($name !== NULL && $video['name'] != $name)
124 $video['err'] = 'INVALID_NAME';
128 $video['err'] = 'INVALID_ID';
132 // Convert JSON encoded string to arrays.
133 $video['assets'] = json_decode($video['formats'], TRUE);
134 unset($video['formats']);
135 $video['tags'] = json_decode($video['tags'], TRUE);
136 asort($video['tags']);
137 $video['tags'] = array_reverse($video['tags'], TRUE);
140 $video['url'] = array();
141 foreach ($video['assets'] as & $asset)
143 $def = substr($asset['res'], strpos($asset['res'], 'x') + 1) . 'p';
144 $asset['src'] = site_url('data/torrents/'. $video['name'] . '_'
145 . $def . '.'. $asset['ext']
146 . '.'. $this->config->item('default_torrent_ext'));
150 $categories = $this->config->item('categories');
151 $category_name = $categories[ intval($video['category_id']) ];
152 $video['category_title'] = $category_name ?
153 $this->lang->line("ui_categ_$category_name") : $category_name;
156 $video['thumbs'] = $this->get_thumbs($video['name'], $video['thumbs_count']);
161 public function get_thumbs($name, $count)
165 for ($i=0; $i < $count; $i++)
166 $thumbs[] = site_url(sprintf("data/thumbs/%s_t%02d.jpg", $name, $i));
172 /* End of file videos_model.php */
173 /* Location: ./application/models/videos_model.php */