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->helper('video');
31 var_dump(get_av_info('data/upload/test.ogv'));
35 * The page used for watching a video
37 * @param string $id DB id of the video
38 * @param string $name `name` of the video from DB
39 * @param string $plugin video plugin ('ns-vlc', 'ns-html5'). If it's set
40 * to NULL or 'auto', the plugin is automatically selected.
42 public function watch($id, $name = NULL, $plugin = NULL)
47 $this->load->model('videos_model');
49 $data['user_id'] = $this->session->userdata('user_id');
50 if ($data['user_id'] === FALSE)
51 $data['user_id'] = '';
53 $data['user_id'] = intval($data['user_id']);
54 $user_roles = intval($this->session->userdata('roles'));
55 // echo USER_ROLE_ADMIN . ' / ';
56 // var_dump($user_roles);
57 // var_dump($user_roles | USER_ROLE_ADMIN);
60 // Retrieve video information.
61 $data['video'] = $this->videos_model->get_video($id, $name);
62 if ($data['video'] === FALSE)
64 $this->load->helper('message');
65 show_error_msg_page($this,
66 $this->lang->line('video_msg_no_video'));
70 // Video is being processed by CIS.
71 if ($data['video']['activation_code']
72 && !$data['video']['content_ingested'])
74 $this->load->helper('message');
75 show_error_msg_page($this,
76 $this->lang->line('video_msg_video_not_ready'));
80 // Unlogged in user can't see unactivated videos.
81 if (empty($data['user_id']))
82 $allow_unactivated = FALSE;
85 if (($user_roles & USER_ROLE_ADMIN) == 0
86 && $data['user_id'] != $data['video']['user_id'])
87 $allow_unactivated = FALSE;
89 $allow_unactivated = TRUE;
92 // Video is not activated; can be seen by owner and admin.
93 if ($data['video']['activation_code'] && !$allow_unactivated)
95 $this->load->helper('message');
96 show_error_msg_page($this,
97 $this->lang->line('video_msg_video_unactivated'));
101 $categories = $this->config->item('categories');
102 $data['video']['category_name'] =
103 $categories[ $data['video']['category_id'] ];
104 $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
106 // Increment the number of views for the video.
107 $this->videos_model->inc_views($id);
110 $params = array( 'title' => $data['video']['title'] . ' – '
111 . $this->config->item('site_name'),
113 'jquery.ui.nsvideo.css',
117 'jquery.ui.nsvideo.js',
118 'jquery.ui.ajax_links_maker.js'
120 //'metas' => array('description'=>'','keywords'=>'')
122 $this->load->library('html_head_params', $params);
124 // Preloading video plugin.
125 // TODO plugin auto: type and format
126 if ($data['plugin_type'] == 'auto')
127 $data['plugin_type'] = 'ns-html5';
128 $data['asset_index'] = 0;
130 // TODO remove old AJAX plugin content
131 // $data['plugin_content'] = $this->_plugin('ns-html5',
132 // $data['video']['url'][0], TRUE);
135 $data['comments'] = $this->_ajax_comment(TRUE, $id);
140 $this->load->view('html_begin', $this->html_head_params);
141 $this->load->view('header');
143 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
144 $this->load->view('video/watch_view', $data);
146 $this->load->view('footer');
147 $this->load->view('html_end');
150 public function upload()
152 $user_id = $this->session->userdata('user_id');
154 // Action not possible if an user is not logged in.
157 $this->load->helper('message');
158 show_error_msg_page($this,
159 $this->lang->line('ui_msg_login_restriction'));
163 $this->load->library('form_validation');
165 $this->form_validation->set_error_delimiters('<span class="error">',
168 if ($this->form_validation->run('upload') === FALSE)
170 $params = array('title' =>
171 $this->lang->line('ui_nav_menu_upload')
173 . $this->config->item('site_name'),
174 //'metas' => array('description'=>'')
176 $this->load->library('html_head_params', $params);
181 $this->load->view('html_begin', $this->html_head_params);
182 $this->load->view('header',
183 array('selected_menu' => 'upload'));
185 $main_params['content'] = $this->load->view(
186 'video/upload_view', array(), TRUE);
187 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
188 $this->load->view('main', $main_params);
190 $this->load->view('footer');
191 $this->load->view('html_end');
195 $this->load->model('videos_model');
196 $this->load->helper('video');
197 $this->config->load('content_ingestion');
199 $name = urlencode(str_replace(' ', '-',
200 $this->input->post('video-title')));
201 $category_id = $this->input->post('video-category');
204 $formats = $this->config->item('formats');
205 $prepared_formats = prepare_formats($formats, $this->av_info,
206 $this->config->item('elim_dupl_res'));
209 $activation_code = $this->videos_model->add_video($name,
210 $this->input->post('video-title'),
211 $this->input->post('video-description'),
212 $this->input->post('video-tags'),
213 $this->av_info['duration'],
214 $prepared_formats['db_formats'], $category_id, $user_id,
215 $this->uploaded_file);
217 // Send a content ingestion request to
218 // CIS (Content Ingestion Server).
219 $this->_send_content_ingestion($activation_code,
220 $this->uploaded_file,
221 $name, $this->av_info['size'],
222 $prepared_formats['transcode_configs']);
224 $this->load->helper('message');
225 show_info_msg_page($this,
226 $this->lang->line('video_msg_video_uploaded'));
230 public function cis_completion($activation_code)
232 $this->load->model('videos_model');
234 if ($this->config->item('require_moderation'))
235 $this->videos_model->set_content_ingested($activation_code);
237 $this->videos_model->activate_video($activation_code);
239 // log_message('info', "cis_completion $activation_code");
243 * Increments (dis)likes count for video with the specified id and returns to
244 * the client as plain text the number if likes.
246 * @param string $action 'like' or 'dislike'
247 * @param string $video_id
248 * @param string $user_id
250 public function ajax_vote($action, $video_id)
252 $video_id = intval($video_id);
253 $user_id = $this->session->userdata('user_id');
254 $this->load->model('videos_model');
256 $res = $this->videos_model->vote($video_id, $user_id,
257 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
263 * Increments (dis)likes count for a comment with a specified id and returns
264 * to the client as plain text the number if likes.
266 * @param string $action 'like' or 'dislike'
267 * @param string $comment_id
269 public function ajax_vote_comment($action, $comment_id)
271 $comment_id = intval($comment_id);
272 $user_id = $this->session->userdata('user_id');
273 $this->load->model('videos_model');
275 $res = $this->videos_model->vote_comment($comment_id, $user_id,
276 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
282 public function ajax_comment($video_id,
283 $ordering = 'newest', $offset = '0')
285 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
288 public function _ajax_comment($return_output, $video_id,
289 $ordering = 'newest', $offset = '0')
291 $video_id = intval($video_id);
293 $this->load->library('form_validation');
294 $this->form_validation->set_error_delimiters('<span class="error">',
297 if ($this->form_validation->run('comment_video'))
299 $this->load->model('videos_model');
300 $user_id = intval($this->session->userdata('user_id'));
301 $comment = $this->input->post('comment');
303 $this->videos_model->comment_video($video_id, $user_id, $comment);
309 $this->load->model('videos_model');
310 $data['comments'] = $this->videos_model->get_video_comments($video_id,
311 $offset, $this->config->item('video_comments_per_page'), $ordering);
312 $data['comments_count'] =
313 $this->videos_model->get_video_comments_count($video_id);
314 $data['hottest_comments'] = $this->videos_model->get_video_comments(
315 $video_id, 0, 2, 'hottest');
316 $data['video_id'] = $video_id;
317 $data['user_id'] = $this->session->userdata('user_id');
320 $this->load->library('pagination');
321 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
322 $pg_config['uri_segment'] = 5;
323 $pg_config['total_rows'] = $data['comments_count'];
324 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
325 $this->pagination->initialize($pg_config);
326 $data['comments_pagination'] = $this->pagination->create_links();
331 $output = $this->load->view('video/comments_view',
332 $data, $return_output);
339 * Request content_ingest to the CIS in order to start the content
342 * @param string $activation_code
343 * @param string $raw_video_fn uploaded video file name
344 * @param string $name
345 * @param int $raw_video_size uploaded video file size in bytes
346 * @param array $transcode_configs dictionary which must be included in
347 * the JSON data that needs to be sent to CIS
348 * @return mixed return the HTTP content (body) on success and FALSE
351 protected function _send_content_ingestion($activation_code, $raw_video_fn,
352 $name, $raw_video_size, $transcode_configs)
354 $this->config->load('content_ingestion');
356 $url = $this->config->item('cis_url') . 'ingest_content';
358 'code'=>$activation_code,
359 'raw_video'=>$raw_video_fn,
361 'weight'=>$raw_video_size,
362 'transcode_configs'=>$transcode_configs,
363 'thumbs'=>$this->config->item('thumbs_count')
365 $json_data = json_encode($data);
367 // Send request to CIS.
368 $r = new HttpRequest($url, HttpRequest::METH_POST);
369 $r->setBody($json_data);
372 $response = $r->send()->getBody();
374 catch (HttpException $ex)
382 public function _is_user_loggedin($param)
384 if (! $this->session->userdata('user_id'))
390 public function _do_comment($comment)
392 // Note: Videos_model must be already loaded.
393 $this->load->model('videos_model');
395 $video_id = intval($this->input->post('video-id'));
396 $user_id = intval($this->session->userdata('user_id'));
398 $this->videos_model->comment_video($video_id, $user_id, $comment);
401 public function _valid_tags($tags)
403 $tok = strtok($tags, ',');
404 while ($tok != FALSE)
407 if (!ctype_alnum($tok))
416 public function _valid_upload($file)
418 if ($_FILES['video-upload-file']['tmp_name'])
421 $config_upload = array();
422 $config_upload['upload_path'] = './data/upload';
423 $config_upload['allowed_types'] = '*';
424 $this->load->library('upload', $config_upload);
426 if ($this->upload->do_upload('video-upload-file'))
428 $upload_data = $this->upload->data();
429 $this->uploaded_file = $upload_data['file_name'];
431 $this->load->helper('video');
432 $this->av_info = get_av_info($upload_data['full_path']);
440 $this->form_validation->set_message('_valid_upload',
441 $this->upload->display_errors('<span class="error">',
447 $this->form_validation->set_message('_valid_upload',
448 $this->lang->line('_required_upload'));
453 * OBSOLETE: AJAX page which retrieves a video plugin.
455 * The view associated with this controller should be parameter type
456 * concatenated with '_plugin_view' and must be located in
457 * 'application/views/video'.
459 * @param string $type 'ns-vlc', 'ns-html5'
461 public function plugin($type)
463 $url = $this->input->post('url', TRUE);
465 $this->_plugin($type, $url);
469 * OBSOLETE: Video plugin controller
471 * See plugin function for details. If the second parameter is TRUE
472 * the output is return instead of being displayed (used in preloading).
474 public function _plugin($type, $url, $return_output=FALSE)
476 if ($type == 'ns-html5')
477 $data['url'] = 'tribe://' . $url;
478 else if ($type == 'ns-vlc')
481 $output = $this->load->view('video/'. $type . '_plugin_view', $data,
490 /* End of file video.php */
491 /* Location: ./application/controllers/video.php */