4 * Class Video controls video items handling: watching, commenting, rating,
8 * @author Călin-Andrei Burloiu
10 class Video extends CI_Controller {
12 protected $uploaded_file;
14 public function __construct()
16 parent::__construct();
18 $this->lang->load('video');
21 public function index()
26 public function test()
28 var_dump($this->session->userdata('user_id'));
32 * The page used for watching a video
34 * @param string $id DB id of the video
35 * @param string $name `name` of the video from DB
36 * @param string $plugin video plugin ('ns-vlc', 'ns-html5'). If it's set
37 * to NULL or 'auto', the plugin is automatically selected.
39 public function watch($id, $name = NULL, $plugin = NULL)
44 // Retrieve video information.
45 $this->load->model('videos_model');
46 $this->videos_model->inc_views($id);
47 $data['video'] = $this->videos_model->get_video($id, $name);
48 $categories = $this->config->item('categories');
49 $data['video']['category_name'] =
50 $categories[ $data['video']['category_id'] ];
51 $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
52 $data['user_id'] = $this->session->userdata('user_id');
53 if ($data['user_id'] === FALSE)
54 $data['user_id'] = '';
57 $params = array( 'title' => $data['video']['title'] . ' – '
58 . $this->config->item('site_name'),
60 'jquery.ui.nsvideo.css',
64 'jquery.ui.nsvideo.js',
65 'jquery.ui.ajax_links_maker.js'
67 //'metas' => array('description'=>'','keywords'=>'')
69 $this->load->library('html_head_params', $params);
71 // Preloading video plugin.
72 // TODO plugin auto: type and format
73 if ($data['plugin_type'] == 'auto')
74 $data['plugin_type'] = 'ns-html5';
75 $data['asset_index'] = 0;
77 // TODO remove old AJAX plugin content
78 // $data['plugin_content'] = $this->_plugin('ns-html5',
79 // $data['video']['url'][0], TRUE);
82 $data['comments'] = $this->_ajax_comment(TRUE, $id);
87 $this->load->view('html_begin', $this->html_head_params);
88 $this->load->view('header');
90 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
91 $this->load->view('video/watch_view', $data);
93 $this->load->view('footer');
94 $this->load->view('html_end');
97 public function upload()
99 $user_id = $this->session->userdata('user_id');
101 // Action not possible if an user is not logged in.
104 $this->load->helper('message');
105 show_error_msg_page($this,
106 $this->lang->line('ui_msg_login_restriction'));
110 $this->load->library('form_validation');
112 $this->form_validation->set_error_delimiters('<span class="error">',
115 if ($this->form_validation->run('upload') === FALSE)
117 $params = array('title' =>
118 $this->lang->line('ui_nav_menu_upload')
120 . $this->config->item('site_name'),
121 //'metas' => array('description'=>'')
123 $this->load->library('html_head_params', $params);
128 $this->load->view('html_begin', $this->html_head_params);
129 $this->load->view('header',
130 array('selected_menu' => 'upload'));
132 $main_params['content'] = $this->load->view(
133 'video/upload_view', array(), TRUE);
134 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
135 $this->load->view('main', $main_params);
137 $this->load->view('footer');
138 $this->load->view('html_end');
142 $this->load->model('videos_model');
143 $this->load->helper('video');
144 $this->config->load('content_ingestion');
146 $file_name = $this->uploaded_file;
147 $av_info = get_av_info($file_name);
148 $name = urlencode(str_replace(' ', '-',
149 $this->input->post('video-title')));
150 $category_id = $this->input->post('video-category');
153 $formats = $this->config->item('formats');
154 $prepared_formats = prepare_formats($formats, $av_info,
155 $this->config->item('elim_dupl_res'));
158 $activation_code = $this->videos_model->add_video($name,
159 $this->input->post('video-title'),
160 $this->input->post('video-description'),
161 $this->input->post('video-tags'),
162 $av_info['duration'],
163 $prepared_formats['db_formats'], $category_id, $user_id);
165 // Send a content ingestion request to
166 // CIS (Content Ingestion Server).
167 $this->_send_content_ingestion($activation_code,
169 $name, $av_info['size'],
170 $prepared_formats['transcode_configs']);
172 $this->load->helper('message');
173 show_info_msg_page($this,
174 $this->lang->line('video_msg_video_uploaded'));
179 * Increments (dis)likes count for video with the specified id and returns to
180 * the client as plain text the number if likes.
182 * @param string $action 'like' or 'dislike'
183 * @param string $video_id
184 * @param string $user_id
186 public function ajax_vote($action, $video_id)
188 $video_id = intval($video_id);
189 $user_id = $this->session->userdata('user_id');
190 $this->load->model('videos_model');
192 $res = $this->videos_model->vote($video_id, $user_id,
193 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
199 * Increments (dis)likes count for a comment with a specified id and returns
200 * to the client as plain text the number if likes.
202 * @param string $action 'like' or 'dislike'
203 * @param string $comment_id
205 public function ajax_vote_comment($action, $comment_id)
207 $comment_id = intval($comment_id);
208 $user_id = $this->session->userdata('user_id');
209 $this->load->model('videos_model');
211 $res = $this->videos_model->vote_comment($comment_id, $user_id,
212 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
218 public function ajax_comment($video_id,
219 $ordering = 'newest', $offset = '0')
221 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
224 public function _ajax_comment($return_output, $video_id,
225 $ordering = 'newest', $offset = '0')
227 $video_id = intval($video_id);
229 $this->load->library('form_validation');
230 $this->form_validation->set_error_delimiters('<span class="error">',
233 if ($this->form_validation->run('comment_video'))
235 $this->load->model('videos_model');
236 $user_id = intval($this->session->userdata('user_id'));
237 $comment = $this->input->post('comment');
239 $this->videos_model->comment_video($video_id, $user_id, $comment);
245 $this->load->model('videos_model');
246 $data['comments'] = $this->videos_model->get_video_comments($video_id,
247 $offset, $this->config->item('video_comments_per_page'), $ordering);
248 $data['comments_count'] =
249 $this->videos_model->get_video_comments_count($video_id);
250 $data['hottest_comments'] = $this->videos_model->get_video_comments(
251 $video_id, 0, 2, 'hottest');
252 $data['video_id'] = $video_id;
253 $data['user_id'] = $this->session->userdata('user_id');
256 $this->load->library('pagination');
257 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
258 $pg_config['uri_segment'] = 5;
259 $pg_config['total_rows'] = $data['comments_count'];
260 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
261 $this->pagination->initialize($pg_config);
262 $data['comments_pagination'] = $this->pagination->create_links();
267 $output = $this->load->view('video/comments_view',
268 $data, $return_output);
275 * Request content_ingest to the CIS in order to start the content
278 * @param string $activation_code
279 * @param string $raw_video_fn uploaded video file name
280 * @param string $name
281 * @param int $raw_video_size uploaded video file size in bytes
282 * @param array $transcode_configs dictionary which must be included in
283 * the JSON data that needs to be sent to CIS
284 * @return mixed return the HTTP content (body) on success and FALSE
287 protected function _send_content_ingestion($activation_code, $raw_video_fn,
288 $name, $raw_video_size, $transcode_configs)
290 $this->config->load('content_ingestion');
292 $url = $this->config->item('cis_url') . 'ingest_content';
294 'code'=>$activation_code,
295 'raw_video'=>$raw_video_fn,
297 'weight'=>$raw_video_size,
298 'transcode_configs'=>$transcode_configs,
299 'thumbs'=>$this->config->item('thumbs_count')
301 $json_data = json_encode($data);
303 // Send request to CIS.
304 $r = new HttpRequest($url, HttpRequest::METH_POST);
305 $r->setBody($json_data);
308 $response = $r->send()->getBody();
310 catch (HttpException $ex)
318 public function _is_user_loggedin($param)
320 if (! $this->session->userdata('user_id'))
326 public function _do_comment($comment)
328 // Note: Videos_model must be already loaded.
329 $this->load->model('videos_model');
331 $video_id = intval($this->input->post('video-id'));
332 $user_id = intval($this->session->userdata('user_id'));
334 $this->videos_model->comment_video($video_id, $user_id, $comment);
337 public function _valid_tags($tags)
339 $tok = strtok($tags, ',');
340 while ($tok != FALSE)
343 if (!ctype_alnum($tok))
352 public function _valid_upload($file)
354 if ($_FILES['video-upload-file']['tmp_name'])
357 $config_upload = array();
358 $config_upload['upload_path'] = './data/upload';
359 $config_upload['allowed_types'] = '*';
360 $this->load->library('upload', $config_upload);
362 if ($this->upload->do_upload('video-upload-file'))
364 $this->uploaded_file = $this->upload->data();
365 $this->uploaded_file = $this->uploaded_file['file_name'];
370 $this->form_validation->set_message('_valid_upload',
371 $this->upload->display_errors('<span class="error">',
377 $this->form_validation->set_message('_valid_upload',
378 $this->lang->line('_required_upload'));
383 * OBSOLETE: AJAX page which retrieves a video plugin.
385 * The view associated with this controller should be parameter type
386 * concatenated with '_plugin_view' and must be located in
387 * 'application/views/video'.
389 * @param string $type 'ns-vlc', 'ns-html5'
391 public function plugin($type)
393 $url = $this->input->post('url', TRUE);
395 $this->_plugin($type, $url);
399 * OBSOLETE: Video plugin controller
401 * See plugin function for details. If the second parameter is TRUE
402 * the output is return instead of being displayed (used in preloading).
404 public function _plugin($type, $url, $return_output=FALSE)
406 if ($type == 'ns-html5')
407 $data['url'] = 'tribe://' . $url;
408 else if ($type == 'ns-vlc')
411 $output = $this->load->view('video/'. $type . '_plugin_view', $data,
420 /* End of file video.php */
421 /* Location: ./application/controllers/video.php */