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;
15 public function __construct()
17 parent::__construct();
19 $this->lang->load('video');
22 public function index()
27 public function test()
29 $this->load->model('videos_model');
31 $videos = $this->videos_model->get_videos_summary(1, NULL, 0, 10,
32 'alphabetically', TRUE);
38 * The page used for watching a video
40 * @param string $id DB id of the video
41 * @param string $name `name` of the video from DB
42 * @param string $plugin video plugin ('ns-vlc', 'ns-html5'). If it's set
43 * to NULL or 'auto', the plugin is automatically selected.
45 public function watch($id, $name = NULL, $plugin = NULL)
50 $this->load->model('videos_model');
52 $data['user_id'] = $this->session->userdata('user_id');
53 if ($data['user_id'] === FALSE)
54 $data['user_id'] = '';
56 $data['user_id'] = intval($data['user_id']);
57 $user_roles = intval($this->session->userdata('roles'));
58 // echo USER_ROLE_ADMIN . ' / ';
59 // var_dump($user_roles);
60 // var_dump($user_roles | USER_ROLE_ADMIN);
63 // Retrieve video information.
64 $data['video'] = $this->videos_model->get_video($id, $name);
65 if ($data['video'] === FALSE)
67 $this->load->helper('message');
68 show_error_msg_page($this,
69 $this->lang->line('video_msg_no_video'));
73 // Video is being processed by CIS.
74 if ($data['video']['activation_code']
75 && !$data['video']['content_ingested'])
77 $this->load->helper('message');
78 show_error_msg_page($this,
79 $this->lang->line('video_msg_video_not_ready'));
83 // Unlogged in user can't see unactivated videos.
84 if (empty($data['user_id']))
85 $allow_unactivated = FALSE;
88 if (($user_roles & USER_ROLE_ADMIN) == 0
89 && $data['user_id'] != $data['video']['user_id'])
90 $allow_unactivated = FALSE;
92 $allow_unactivated = TRUE;
95 // Video is not activated; can be seen by owner and admin.
96 if ($data['video']['activation_code'] && !$allow_unactivated)
98 $this->load->helper('message');
99 show_error_msg_page($this,
100 $this->lang->line('video_msg_video_unactivated'));
104 $categories = $this->config->item('categories');
105 $data['video']['category_name'] =
106 $categories[ $data['video']['category_id'] ];
107 $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
109 // Increment the number of views for the video.
110 $this->videos_model->inc_views($id);
113 $params = array( 'title' => $data['video']['title'] . ' – '
114 . $this->config->item('site_name'),
116 'jquery.ui.nsvideo.css',
120 'jquery.ui.nsvideo.js',
121 'jquery.ui.ajax_links_maker.js'
123 //'metas' => array('description'=>'','keywords'=>'')
125 $this->load->library('html_head_params', $params);
127 // Preloading video plugin.
128 // TODO plugin auto: type and format
129 if ($data['plugin_type'] == 'auto')
130 $data['plugin_type'] = 'ns-html5';
131 $data['asset_index'] = 0;
133 // TODO remove old AJAX plugin content
134 // $data['plugin_content'] = $this->_plugin('ns-html5',
135 // $data['video']['url'][0], TRUE);
138 $data['comments'] = $this->_ajax_comment(TRUE, $id);
143 $this->load->view('html_begin', $this->html_head_params);
144 $this->load->view('header');
146 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
147 $this->load->view('video/watch_view', $data);
149 $this->load->view('footer');
150 $this->load->view('html_end');
153 public function upload()
155 $user_id = $this->session->userdata('user_id');
157 // Action not possible if an user is not logged in.
160 $this->load->helper('message');
161 show_error_msg_page($this,
162 $this->lang->line('ui_msg_login_restriction'));
166 $this->load->library('form_validation');
168 $this->form_validation->set_error_delimiters('<span class="error">',
171 if ($this->form_validation->run('upload') === FALSE)
173 $params = array('title' =>
174 $this->lang->line('ui_nav_menu_upload')
176 . $this->config->item('site_name'),
177 //'metas' => array('description'=>'')
179 $this->load->library('html_head_params', $params);
184 $this->load->view('html_begin', $this->html_head_params);
185 $this->load->view('header',
186 array('selected_menu' => 'upload'));
188 $main_params['content'] = $this->load->view(
189 'video/upload_view', array(), TRUE);
190 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
191 $this->load->view('main', $main_params);
193 $this->load->view('footer');
194 $this->load->view('html_end');
198 $this->load->model('videos_model');
199 $this->load->helper('video');
200 $this->config->load('content_ingestion');
202 $name = urlencode(str_replace(' ', '-',
203 $this->input->post('video-title')));
204 $category_id = $this->input->post('video-category');
207 $formats = $this->config->item('formats');
208 $prepared_formats = prepare_formats($formats, $this->av_info,
209 $this->config->item('elim_dupl_res'));
212 $activation_code = $this->videos_model->add_video($name,
213 $this->input->post('video-title'),
214 $this->input->post('video-description'),
215 $this->input->post('video-tags'),
216 $this->av_info['duration'],
217 $prepared_formats['db_formats'], $category_id, $user_id,
218 $this->uploaded_file);
220 // Send a content ingestion request to
221 // CIS (Content Ingestion Server).
222 $this->_send_content_ingestion($activation_code,
223 $this->uploaded_file,
224 $name, $this->av_info['size'],
225 $prepared_formats['transcode_configs']);
227 $this->load->helper('message');
228 show_info_msg_page($this,
229 $this->lang->line('video_msg_video_uploaded'));
233 public function cis_completion($activation_code)
235 $this->load->model('videos_model');
237 if ($this->config->item('require_moderation'))
238 $this->videos_model->set_content_ingested($activation_code);
240 $this->videos_model->activate_video($activation_code);
242 // log_message('info', "cis_completion $activation_code");
246 * Increments (dis)likes count for video with the specified id and returns to
247 * the client as plain text the number if likes.
249 * @param string $action 'like' or 'dislike'
250 * @param string $video_id
251 * @param string $user_id
253 public function ajax_vote($action, $video_id)
255 $video_id = intval($video_id);
256 $user_id = $this->session->userdata('user_id');
257 $this->load->model('videos_model');
259 $res = $this->videos_model->vote($video_id, $user_id,
260 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
266 * Increments (dis)likes count for a comment with a specified id and returns
267 * to the client as plain text the number if likes.
269 * @param string $action 'like' or 'dislike'
270 * @param string $comment_id
272 public function ajax_vote_comment($action, $comment_id)
274 $comment_id = intval($comment_id);
275 $user_id = $this->session->userdata('user_id');
276 $this->load->model('videos_model');
278 $res = $this->videos_model->vote_comment($comment_id, $user_id,
279 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
285 public function ajax_comment($video_id,
286 $ordering = 'newest', $offset = '0')
288 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
291 public function _ajax_comment($return_output, $video_id,
292 $ordering = 'newest', $offset = '0')
294 $video_id = intval($video_id);
296 $this->load->library('form_validation');
297 $this->form_validation->set_error_delimiters('<span class="error">',
300 if ($this->form_validation->run('comment_video'))
302 $this->load->model('videos_model');
303 $user_id = intval($this->session->userdata('user_id'));
304 $comment = $this->input->post('comment');
306 $this->videos_model->comment_video($video_id, $user_id, $comment);
312 $this->load->model('videos_model');
313 $data['comments'] = $this->videos_model->get_video_comments($video_id,
314 $offset, $this->config->item('video_comments_per_page'), $ordering);
315 $data['comments_count'] =
316 $this->videos_model->get_video_comments_count($video_id);
317 $data['hottest_comments'] = $this->videos_model->get_video_comments(
318 $video_id, 0, 2, 'hottest');
319 $data['video_id'] = $video_id;
320 $data['user_id'] = $this->session->userdata('user_id');
323 $this->load->library('pagination');
324 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
325 $pg_config['uri_segment'] = 5;
326 $pg_config['total_rows'] = $data['comments_count'];
327 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
328 $this->pagination->initialize($pg_config);
329 $data['comments_pagination'] = $this->pagination->create_links();
334 $output = $this->load->view('video/comments_view',
335 $data, $return_output);
342 * Request content_ingest to the CIS in order to start the content
345 * @param string $activation_code
346 * @param string $raw_video_fn uploaded video file name
347 * @param string $name
348 * @param int $raw_video_size uploaded video file size in bytes
349 * @param array $transcode_configs dictionary which must be included in
350 * the JSON data that needs to be sent to CIS
351 * @return mixed return the HTTP content (body) on success and FALSE
354 protected function _send_content_ingestion($activation_code, $raw_video_fn,
355 $name, $raw_video_size, $transcode_configs)
357 $this->config->load('content_ingestion');
359 $url = $this->config->item('cis_url') . 'ingest_content';
361 'code'=>$activation_code,
362 'raw_video'=>$raw_video_fn,
364 'weight'=>$raw_video_size,
365 'transcode_configs'=>$transcode_configs,
366 'thumbs'=>$this->config->item('thumbs_count')
368 $json_data = json_encode($data);
370 // Send request to CIS.
371 $r = new HttpRequest($url, HttpRequest::METH_POST);
372 $r->setBody($json_data);
375 $response = $r->send()->getBody();
377 catch (HttpException $ex)
385 public function _is_user_loggedin($param)
387 if (! $this->session->userdata('user_id'))
393 public function _do_comment($comment)
395 // Note: Videos_model must be already loaded.
396 $this->load->model('videos_model');
398 $video_id = intval($this->input->post('video-id'));
399 $user_id = intval($this->session->userdata('user_id'));
401 $this->videos_model->comment_video($video_id, $user_id, $comment);
404 public function _valid_tags($tags)
406 $tok = strtok($tags, ',');
407 while ($tok != FALSE)
410 if (!ctype_alnum($tok))
419 public function _valid_upload($file)
421 if ($_FILES['video-upload-file']['tmp_name'])
424 $config_upload = array();
425 $config_upload['upload_path'] = './data/upload';
426 $config_upload['allowed_types'] = '*';
427 $this->load->library('upload', $config_upload);
429 if ($this->upload->do_upload('video-upload-file'))
431 $upload_data = $this->upload->data();
432 $this->uploaded_file = $upload_data['file_name'];
434 $this->load->helper('video');
435 $this->av_info = get_av_info($upload_data['full_path']);
443 $this->form_validation->set_message('_valid_upload',
444 $this->upload->display_errors('<span class="error">',
450 $this->form_validation->set_message('_valid_upload',
451 $this->lang->line('_required_upload'));
456 * OBSOLETE: AJAX page which retrieves a video plugin.
458 * The view associated with this controller should be parameter type
459 * concatenated with '_plugin_view' and must be located in
460 * 'application/views/video'.
462 * @param string $type 'ns-vlc', 'ns-html5'
464 public function plugin($type)
466 $url = $this->input->post('url', TRUE);
468 $this->_plugin($type, $url);
472 * OBSOLETE: Video plugin controller
474 * See plugin function for details. If the second parameter is TRUE
475 * the output is return instead of being displayed (used in preloading).
477 public function _plugin($type, $url, $return_output=FALSE)
479 if ($type == 'ns-html5')
480 $data['url'] = 'tribe://' . $url;
481 else if ($type == 'ns-vlc')
484 $output = $this->load->view('video/'. $type . '_plugin_view', $data,
493 /* End of file video.php */
494 /* Location: ./application/controllers/video.php */