4 * Class Video controls video items handling: watching, commenting, rating,
8 * @author Călin-Andrei Burloiu
10 class Video extends CI_Controller {
12 public function __construct()
14 parent::__construct();
16 $this->lang->load('video');
19 public function index()
23 public function test()
25 $r = new HttpRequest('http://example.com/form.php', HttpRequest::METH_POST);
26 $r->setOptions(array('cookies' => array('lang' => 'de')));
27 $r->addPostFields(array('user' => 'mike', 'pass' => 's3c|r3t'));
28 $r->addPostFile('image', 'profile.jpg', 'image/jpeg');
31 echo $r->send()->getBody();
33 catch (HttpException $ex)
40 * The page used for watching a video
42 * @param string $id DB id of the video
43 * @param string $name `name` of the video from DB
44 * @param string $plugin video plugin ('ns-vlc', 'ns-html5'). If it's set
45 * to NULL or 'auto', the plugin is automatically selected.
47 public function watch($id, $name = NULL, $plugin = NULL)
52 // Retrieve video information.
53 $this->load->model('videos_model');
54 $this->videos_model->inc_views($id);
55 $data['video'] = $this->videos_model->get_video($id, $name);
56 $categories = $this->config->item('categories');
57 $data['video']['category_name'] =
58 $categories[ $data['video']['category_id'] ];
59 $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
60 $data['user_id'] = $this->session->userdata('user_id');
61 if ($data['user_id'] === FALSE)
62 $data['user_id'] = '';
65 $params = array( 'title' => $data['video']['title'] . ' – '
66 . $this->config->item('site_name'),
68 'jquery.ui.nsvideo.css',
72 'jquery.ui.nsvideo.js',
73 'jquery.ui.ajax_links_maker.js'
75 //'metas' => array('description'=>'','keywords'=>'')
77 $this->load->library('html_head_params', $params);
79 // Preloading video plugin.
80 // TODO plugin auto: type and format
81 if ($data['plugin_type'] == 'auto')
82 $data['plugin_type'] = 'ns-html5';
83 $data['asset_index'] = 0;
85 // TODO remove old AJAX plugin content
86 // $data['plugin_content'] = $this->_plugin('ns-html5',
87 // $data['video']['url'][0], TRUE);
90 $data['comments'] = $this->_ajax_comment(TRUE, $id);
95 $this->load->view('html_begin', $this->html_head_params);
96 $this->load->view('header');
98 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
99 $this->load->view('video/watch_view', $data);
101 $this->load->view('footer');
102 $this->load->view('html_end');
105 public function upload()
107 $this->load->library('form_validation');
109 $this->form_validation->set_error_delimiters('<span class="error">',
112 // TODO check if user is logged in
114 if ($this->form_validation->run('upload') === FALSE)
116 $params = array('title' =>
117 $this->lang->line('ui_nav_menu_upload')
119 . $this->config->item('site_name'),
120 //'metas' => array('description'=>'')
122 $this->load->library('html_head_params', $params);
127 $this->load->view('html_begin', $this->html_head_params);
128 $this->load->view('header',
129 array('selected_menu' => 'upload'));
131 $main_params['content'] = $this->load->view(
132 'video/upload_view', array(), TRUE);
133 $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
134 $this->load->view('main', $main_params);
136 $this->load->view('footer');
137 $this->load->view('html_end');
141 $this->load->model('videos_model');
142 $this->load->helper('video');
144 $file_name = './data/upload/'. $_FILES['video-upload-file']['name'];
145 $av_info = get_av_info($file_name);
147 // TODO category_id, user_id
148 // $this->videos_model->add_video(
149 // $this->input->post('video-title'),
150 // $this->input->post('video-description'),
151 // $this->input->post('video-tags'),
159 * Increments (dis)likes count for video with the specified id and returns to
160 * the client as plain text the number if likes.
162 * @param string $action 'like' or 'dislike'
163 * @param string $video_id
164 * @param string $user_id
166 public function ajax_vote($action, $video_id)
168 $video_id = intval($video_id);
169 $user_id = $this->session->userdata('user_id');
170 $this->load->model('videos_model');
172 $res = $this->videos_model->vote($video_id, $user_id,
173 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
179 * Increments (dis)likes count for a comment with a specified id and returns
180 * to the client as plain text the number if likes.
182 * @param string $action 'like' or 'dislike'
183 * @param string $comment_id
185 public function ajax_vote_comment($action, $comment_id)
187 $comment_id = intval($comment_id);
188 $user_id = $this->session->userdata('user_id');
189 $this->load->model('videos_model');
191 $res = $this->videos_model->vote_comment($comment_id, $user_id,
192 (strcmp($action, 'like') == 0 ? TRUE : FALSE));
198 public function ajax_comment($video_id,
199 $ordering = 'newest', $offset = '0')
201 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
204 public function _ajax_comment($return_output, $video_id,
205 $ordering = 'newest', $offset = '0')
207 $video_id = intval($video_id);
209 $this->load->library('form_validation');
210 $this->form_validation->set_error_delimiters('<span class="error">',
213 if ($this->form_validation->run('comment_video'))
215 $this->load->model('videos_model');
216 $user_id = intval($this->session->userdata('user_id'));
217 $comment = $this->input->post('comment');
219 $this->videos_model->comment_video($video_id, $user_id, $comment);
225 $this->load->model('videos_model');
226 $data['comments'] = $this->videos_model->get_video_comments($video_id,
227 $offset, $this->config->item('video_comments_per_page'), $ordering);
228 $data['comments_count'] =
229 $this->videos_model->get_video_comments_count($video_id);
230 $data['hottest_comments'] = $this->videos_model->get_video_comments(
231 $video_id, 0, 2, 'hottest');
232 $data['video_id'] = $video_id;
233 $data['user_id'] = $this->session->userdata('user_id');
236 $this->load->library('pagination');
237 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
238 $pg_config['uri_segment'] = 5;
239 $pg_config['total_rows'] = $data['comments_count'];
240 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
241 $this->pagination->initialize($pg_config);
242 $data['comments_pagination'] = $this->pagination->create_links();
247 $output = $this->load->view('video/comments_view',
248 $data, $return_output);
254 public function _is_user_loggedin($param)
256 if (! $this->session->userdata('user_id'))
262 public function _do_comment($comment)
264 // Note: Videos_model must be already loaded.
265 $this->load->model('videos_model');
267 $video_id = intval($this->input->post('video-id'));
268 $user_id = intval($this->session->userdata('user_id'));
270 $this->videos_model->comment_video($video_id, $user_id, $comment);
273 public function _valid_tags($tags)
275 $tok = strtok($tags, ',');
276 while ($tok != FALSE)
279 if (!ctype_alnum($tok))
288 public function _valid_upload($file)
290 if ($_FILES['video-upload-file']['tmp_name'])
293 $config_upload = array();
294 $config_upload['upload_path'] = './data/upload';
295 $config_upload['allowed_types'] = '*';
296 $this->load->library('upload', $config_upload);
298 if ($this->upload->do_upload('video-upload-file'))
304 $this->form_validation->set_message('_valid_upload',
305 $this->upload->display_errors('<span class="error">',
311 $this->form_validation->set_message('_valid_upload',
312 $this->lang->line('_required_upload'));
317 * OBSOLETE: AJAX page which retrieves a video plugin.
319 * The view associated with this controller should be parameter type
320 * concatenated with '_plugin_view' and must be located in
321 * 'application/views/video'.
323 * @param string $type 'ns-vlc', 'ns-html5'
325 public function plugin($type)
327 $url = $this->input->post('url', TRUE);
329 $this->_plugin($type, $url);
333 * OBSOLETE: Video plugin controller
335 * See plugin function for details. If the second parameter is TRUE
336 * the output is return instead of being displayed (used in preloading).
338 public function _plugin($type, $url, $return_output=FALSE)
340 if ($type == 'ns-html5')
341 $data['url'] = 'tribe://' . $url;
342 else if ($type == 'ns-vlc')
345 $output = $this->load->view('video/'. $type . '_plugin_view', $data,
354 /* End of file video.php */
355 /* Location: ./application/controllers/video.php */