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($fn)
28 $fn = "data/upload/$fn";
29 $this->load->helper('video');
31 var_dump(get_av_info($fn));
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 // Retrieve video information.
48 $this->load->model('videos_model');
49 $this->videos_model->inc_views($id);
50 $data['video'] = $this->videos_model->get_video($id, $name);
51 $categories = $this->config->item('categories');
52 $data['video']['category_name'] =
53 $categories[ $data['video']['category_id'] ];
54 $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
55 $data['user_id'] = $this->session->userdata('user_id');
56 if ($data['user_id'] === FALSE)
57 $data['user_id'] = '';
60 $params = array( 'title' => $data['video']['title'] . ' – '
61 . $this->config->item('site_name'),
63 'jquery.ui.nsvideo.css',
67 'jquery.ui.nsvideo.js',
68 'jquery.ui.ajax_links_maker.js'
70 //'metas' => array('description'=>'','keywords'=>'')
72 $this->load->library('html_head_params', $params);
74 // Preloading video plugin.
75 // TODO plugin auto: type and format
76 if ($data['plugin_type'] == 'auto')
77 $data['plugin_type'] = 'ns-html5';
78 $data['asset_index'] = 0;
80 // TODO remove old AJAX plugin content
81 // $data['plugin_content'] = $this->_plugin('ns-html5',
82 // $data['video']['url'][0], TRUE);
85 $data['comments'] = $this->_ajax_comment(TRUE, $id);
90 $this->load->view('html_begin', $this->html_head_params);
91 $this->load->view('header');
93 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
94 $this->load->view('video/watch_view', $data);
96 $this->load->view('footer');
97 $this->load->view('html_end');
100 public function upload()
102 $user_id = $this->session->userdata('user_id');
104 // Action not possible if an user is not logged in.
107 $this->load->helper('message');
108 show_error_msg_page($this,
109 $this->lang->line('ui_msg_login_restriction'));
113 $this->load->library('form_validation');
115 $this->form_validation->set_error_delimiters('<span class="error">',
118 if ($this->form_validation->run('upload') === FALSE)
120 $params = array('title' =>
121 $this->lang->line('ui_nav_menu_upload')
123 . $this->config->item('site_name'),
124 //'metas' => array('description'=>'')
126 $this->load->library('html_head_params', $params);
131 $this->load->view('html_begin', $this->html_head_params);
132 $this->load->view('header',
133 array('selected_menu' => 'upload'));
135 $main_params['content'] = $this->load->view(
136 'video/upload_view', array(), TRUE);
137 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
138 $this->load->view('main', $main_params);
140 $this->load->view('footer');
141 $this->load->view('html_end');
145 $this->load->model('videos_model');
146 $this->load->helper('video');
147 $this->config->load('content_ingestion');
149 $name = urlencode(str_replace(' ', '-',
150 $this->input->post('video-title')));
151 $category_id = $this->input->post('video-category');
154 $formats = $this->config->item('formats');
155 $prepared_formats = prepare_formats($formats, $this->av_info,
156 $this->config->item('elim_dupl_res'));
159 $activation_code = $this->videos_model->add_video($name,
160 $this->input->post('video-title'),
161 $this->input->post('video-description'),
162 $this->input->post('video-tags'),
163 $this->av_info['duration'],
164 $prepared_formats['db_formats'], $category_id, $user_id);
166 // Send a content ingestion request to
167 // CIS (Content Ingestion Server).
168 $this->_send_content_ingestion($activation_code,
169 $this->uploaded_file,
170 $name, $this->av_info['size'],
171 $prepared_formats['transcode_configs']);
173 $this->load->helper('message');
174 show_info_msg_page($this,
175 $this->lang->line('video_msg_video_uploaded'));
180 * Increments (dis)likes count for video with the specified id and returns to
181 * the client as plain text the number if likes.
183 * @param string $action 'like' or 'dislike'
184 * @param string $video_id
185 * @param string $user_id
187 public function ajax_vote($action, $video_id)
189 $video_id = intval($video_id);
190 $user_id = $this->session->userdata('user_id');
191 $this->load->model('videos_model');
193 $res = $this->videos_model->vote($video_id, $user_id,
194 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
200 * Increments (dis)likes count for a comment with a specified id and returns
201 * to the client as plain text the number if likes.
203 * @param string $action 'like' or 'dislike'
204 * @param string $comment_id
206 public function ajax_vote_comment($action, $comment_id)
208 $comment_id = intval($comment_id);
209 $user_id = $this->session->userdata('user_id');
210 $this->load->model('videos_model');
212 $res = $this->videos_model->vote_comment($comment_id, $user_id,
213 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
219 public function ajax_comment($video_id,
220 $ordering = 'newest', $offset = '0')
222 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
225 public function _ajax_comment($return_output, $video_id,
226 $ordering = 'newest', $offset = '0')
228 $video_id = intval($video_id);
230 $this->load->library('form_validation');
231 $this->form_validation->set_error_delimiters('<span class="error">',
234 if ($this->form_validation->run('comment_video'))
236 $this->load->model('videos_model');
237 $user_id = intval($this->session->userdata('user_id'));
238 $comment = $this->input->post('comment');
240 $this->videos_model->comment_video($video_id, $user_id, $comment);
246 $this->load->model('videos_model');
247 $data['comments'] = $this->videos_model->get_video_comments($video_id,
248 $offset, $this->config->item('video_comments_per_page'), $ordering);
249 $data['comments_count'] =
250 $this->videos_model->get_video_comments_count($video_id);
251 $data['hottest_comments'] = $this->videos_model->get_video_comments(
252 $video_id, 0, 2, 'hottest');
253 $data['video_id'] = $video_id;
254 $data['user_id'] = $this->session->userdata('user_id');
257 $this->load->library('pagination');
258 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
259 $pg_config['uri_segment'] = 5;
260 $pg_config['total_rows'] = $data['comments_count'];
261 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
262 $this->pagination->initialize($pg_config);
263 $data['comments_pagination'] = $this->pagination->create_links();
268 $output = $this->load->view('video/comments_view',
269 $data, $return_output);
276 * Request content_ingest to the CIS in order to start the content
279 * @param string $activation_code
280 * @param string $raw_video_fn uploaded video file name
281 * @param string $name
282 * @param int $raw_video_size uploaded video file size in bytes
283 * @param array $transcode_configs dictionary which must be included in
284 * the JSON data that needs to be sent to CIS
285 * @return mixed return the HTTP content (body) on success and FALSE
288 protected function _send_content_ingestion($activation_code, $raw_video_fn,
289 $name, $raw_video_size, $transcode_configs)
291 $this->config->load('content_ingestion');
293 $url = $this->config->item('cis_url') . 'ingest_content';
295 'code'=>$activation_code,
296 'raw_video'=>$raw_video_fn,
298 'weight'=>$raw_video_size,
299 'transcode_configs'=>$transcode_configs,
300 'thumbs'=>$this->config->item('thumbs_count')
302 $json_data = json_encode($data);
304 // Send request to CIS.
305 $r = new HttpRequest($url, HttpRequest::METH_POST);
306 $r->setBody($json_data);
309 $response = $r->send()->getBody();
311 catch (HttpException $ex)
319 public function _is_user_loggedin($param)
321 if (! $this->session->userdata('user_id'))
327 public function _do_comment($comment)
329 // Note: Videos_model must be already loaded.
330 $this->load->model('videos_model');
332 $video_id = intval($this->input->post('video-id'));
333 $user_id = intval($this->session->userdata('user_id'));
335 $this->videos_model->comment_video($video_id, $user_id, $comment);
338 public function _valid_tags($tags)
340 $tok = strtok($tags, ',');
341 while ($tok != FALSE)
344 if (!ctype_alnum($tok))
353 public function _valid_upload($file)
355 if ($_FILES['video-upload-file']['tmp_name'])
358 $config_upload = array();
359 $config_upload['upload_path'] = './data/upload';
360 $config_upload['allowed_types'] = '*';
361 $this->load->library('upload', $config_upload);
363 if ($this->upload->do_upload('video-upload-file'))
365 $upload_data = $this->upload->data();
366 $this->uploaded_file = $upload_data['file_name'];
368 $this->load->helper('video');
369 $this->av_info = get_av_info($upload_data['full_path']);
377 $this->form_validation->set_message('_valid_upload',
378 $this->upload->display_errors('<span class="error">',
384 $this->form_validation->set_message('_valid_upload',
385 $this->lang->line('_required_upload'));
390 * OBSOLETE: AJAX page which retrieves a video plugin.
392 * The view associated with this controller should be parameter type
393 * concatenated with '_plugin_view' and must be located in
394 * 'application/views/video'.
396 * @param string $type 'ns-vlc', 'ns-html5'
398 public function plugin($type)
400 $url = $this->input->post('url', TRUE);
402 $this->_plugin($type, $url);
406 * OBSOLETE: Video plugin controller
408 * See plugin function for details. If the second parameter is TRUE
409 * the output is return instead of being displayed (used in preloading).
411 public function _plugin($type, $url, $return_output=FALSE)
413 if ($type == 'ns-html5')
414 $data['url'] = 'tribe://' . $url;
415 else if ($type == 'ns-vlc')
418 $output = $this->load->view('video/'. $type . '_plugin_view', $data,
427 /* End of file video.php */
428 /* Location: ./application/controllers/video.php */