6a5c19e60682c42a7d662d4f419e269dd67bedaf
[living-lab-site.git] / application / controllers / video.php
1 <?php
2
3 /**
4  * Class Video controls video items handling: watching, commenting, rating,
5  * adding etc.
6  *
7  * @category    Controller
8  * @author              Călin-Andrei Burloiu
9  */
10 class Video extends CI_Controller {
11
12         protected $uploaded_file;
13         
14         public function __construct()
15         {
16                 parent::__construct();
17                 
18                 $this->lang->load('video');
19         }
20         
21         public function index()
22         {
23                 //phpinfo();
24         }
25         
26         public function test($fn)
27         {
28                 $fn = "data/upload/$fn";
29                 $this->load->helper('video');
30                 
31                 var_dump(get_av_info($fn));
32         }
33         
34         /**
35          * The page used for watching a video
36          *
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.
41          */
42         public function watch($id, $name = NULL, $plugin = NULL)
43         {
44                 // **
45                 // ** LOADING MODEL
46                 // **
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'] = '';
58                 
59                 // Display page.
60                 $params = array(        'title' => $data['video']['title'] . ' &ndash; '
61                                                                 . $this->config->item('site_name'),
62                                                         'css' => array(
63                                                                 'jquery.ui.nsvideo.css',
64                                                                 'video.css'
65                                                         ),
66                                                         'js' => array(
67                                                                 'jquery.ui.nsvideo.js',
68                                                                 'jquery.ui.ajax_links_maker.js'
69                                                         ),
70                                                         //'metas' => array('description'=>'','keywords'=>'')
71                                                         );
72                 $this->load->library('html_head_params', $params);
73                 
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;
79                 
80                 // TODO remove old AJAX plugin content
81 //              $data['plugin_content'] = $this->_plugin('ns-html5', 
82 //                      $data['video']['url'][0], TRUE);
83
84                 // Comments
85                 $data['comments'] = $this->_ajax_comment(TRUE, $id);
86                 
87                 // **
88                 // ** LOADING VIEWS
89                 // **
90                 $this->load->view('html_begin', $this->html_head_params);
91                 $this->load->view('header');
92                 
93                 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
94                 $this->load->view('video/watch_view', $data);
95                 
96                 $this->load->view('footer');
97                 $this->load->view('html_end');
98         }
99                 
100         public function upload()
101         {
102                 $user_id = $this->session->userdata('user_id');
103                 
104                 // Action not possible if an user is not logged in.
105                 if (!$user_id)
106                 {
107                         $this->load->helper('message');
108                         show_error_msg_page($this, 
109                                 $this->lang->line('ui_msg_login_restriction'));
110                         return;
111                 }
112                 
113                 $this->load->library('form_validation');
114
115                 $this->form_validation->set_error_delimiters('<span class="error">',
116                                 '</span>');
117                 
118                 if ($this->form_validation->run('upload') === FALSE)
119                 {
120                         $params = array('title' =>
121                                                                 $this->lang->line('ui_nav_menu_upload')
122                                                                         .' &ndash; '
123                                                                         . $this->config->item('site_name'),
124                                                         //'metas' => array('description'=>'')
125                         );
126                         $this->load->library('html_head_params', $params);
127
128                         // **
129                         // ** LOADING VIEWS
130                         // **
131                         $this->load->view('html_begin', $this->html_head_params);
132                         $this->load->view('header',
133                                         array('selected_menu' => 'upload'));
134
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);
139
140                         $this->load->view('footer');
141                         $this->load->view('html_end');
142                 }
143                 else
144                 {
145                         $this->load->model('videos_model');
146                         $this->load->helper('video');
147                         $this->config->load('content_ingestion');
148                         
149                         $name = urlencode(str_replace(' ', '-',
150                                         $this->input->post('video-title')));
151                         $category_id = $this->input->post('video-category');
152                         
153                         // Prepare formats
154                         $formats = $this->config->item('formats');
155                         $prepared_formats = prepare_formats($formats, $this->av_info,
156                                         $this->config->item('elim_dupl_res'));
157                         
158                         // Add video to DB.
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);
165                         
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']);
172                         
173                         $this->load->helper('message');
174                         show_info_msg_page($this, 
175                                 $this->lang->line('video_msg_video_uploaded'));
176                 }
177         }
178         
179         /**
180         * Increments (dis)likes count for video with the specified id and returns to
181         * the client as plain text the number if likes.
182         *
183         * @param string $action 'like' or 'dislike'
184         * @param string $video_id
185         * @param string $user_id
186         */
187         public function ajax_vote($action, $video_id)
188         {
189                 $video_id = intval($video_id);
190                 $user_id = $this->session->userdata('user_id');
191                 $this->load->model('videos_model');
192         
193                 $res = $this->videos_model->vote($video_id, $user_id,
194                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
195         
196                 if ($res !== -1)
197                         echo $res;
198         }
199         /**
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. 
202          * 
203          * @param string $action        'like' or 'dislike'
204          * @param string $comment_id
205          */
206         public function ajax_vote_comment($action, $comment_id)
207         {
208                 $comment_id = intval($comment_id);
209                 $user_id = $this->session->userdata('user_id');
210                 $this->load->model('videos_model');
211                 
212                 $res = $this->videos_model->vote_comment($comment_id, $user_id,
213                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
214                 
215                 if ($res !== -1)
216                         echo $res;
217         }
218         
219         public function ajax_comment($video_id,
220                         $ordering = 'newest', $offset = '0')
221         {
222                 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
223         }
224         
225         public function _ajax_comment($return_output, $video_id,
226                         $ordering = 'newest', $offset = '0')
227         {
228                 $video_id = intval($video_id);
229                 
230                 $this->load->library('form_validation');
231                 $this->form_validation->set_error_delimiters('<span class="error">',
232                                         '</span>');
233                 
234                 if ($this->form_validation->run('comment_video'))
235                 {
236                         $this->load->model('videos_model');
237                         $user_id = intval($this->session->userdata('user_id'));
238                         $comment = $this->input->post('comment');
239                         
240                         $this->videos_model->comment_video($video_id, $user_id, $comment);
241                 }
242                 
243                 // **
244                 // ** MODEL **
245                 // **           
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');
255                 
256                 // Pagination
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();
264                 
265                 // **
266                 // ** VIEWS **
267                 // **
268                 $output = $this->load->view('video/comments_view',
269                         $data, $return_output);
270                 
271                 if ($return_output)
272                         return $output;
273         }
274         
275         /**
276          * Request content_ingest to the CIS in order to start the content
277          * ingestion process.
278          * 
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
286          * otherwise
287          */
288         protected function _send_content_ingestion($activation_code, $raw_video_fn,
289                         $name, $raw_video_size, $transcode_configs)
290         {
291                 $this->config->load('content_ingestion');
292                 
293                 $url = $this->config->item('cis_url') . 'ingest_content';
294                 $data = array(
295                         'code'=>$activation_code,
296                         'raw_video'=>$raw_video_fn,
297                         'name'=>$name,
298                         'weight'=>$raw_video_size,
299                         'transcode_configs'=>$transcode_configs,
300                         'thumbs'=>$this->config->item('thumbs_count')
301                 );
302                 $json_data = json_encode($data);
303                 
304                 // Send request to CIS.
305                 $r = new HttpRequest($url, HttpRequest::METH_POST);
306                 $r->setBody($json_data);
307                 try
308                 {
309                         $response = $r->send()->getBody();
310                 }
311                 catch (HttpException $ex) 
312                 {
313                         return FALSE;
314                 }
315                 
316                 return $response;
317         }
318         
319         public function _is_user_loggedin($param)
320         {
321                 if (! $this->session->userdata('user_id'))
322                         return FALSE;
323                 
324                 return TRUE;
325         }
326         
327         public function _do_comment($comment)
328         {
329                 // Note: Videos_model must be already loaded.
330                 $this->load->model('videos_model');
331                 
332                 $video_id = intval($this->input->post('video-id'));
333                 $user_id = intval($this->session->userdata('user_id'));
334                 
335                 $this->videos_model->comment_video($video_id, $user_id, $comment);
336         }
337         
338         public function _valid_tags($tags)
339         {
340                 $tok = strtok($tags, ',');
341                 while ($tok != FALSE)
342                 {
343                         $tok = trim($tok);
344                         if (!ctype_alnum($tok))
345                                 return FALSE;
346                         
347                         $tok = strtok(',');
348                 }
349                 
350                 return TRUE;
351         }
352         
353         public function _valid_upload($file)
354         {
355                 if ($_FILES['video-upload-file']['tmp_name'])
356                 {
357                         // Upload library
358                         $config_upload = array();
359                         $config_upload['upload_path'] = './data/upload';
360                         $config_upload['allowed_types'] = '*';
361                         $this->load->library('upload', $config_upload);
362
363                         if ($this->upload->do_upload('video-upload-file'))
364                         {
365                                 $upload_data = $this->upload->data();
366                                 $this->uploaded_file = $upload_data['file_name'];
367                                 
368                                 $this->load->helper('video');
369                                 $this->av_info = get_av_info($upload_data['full_path']);
370                                 if (!$this->av_info)
371                                         return FALSE;
372                                 
373                                 return TRUE;
374                         }
375                         else
376                         {
377                                 $this->form_validation->set_message('_valid_upload',
378                                                 $this->upload->display_errors('<span class="error">',
379                                                                 '</span>'));
380                                 return FALSE;
381                         }
382                 }
383                 
384                 $this->form_validation->set_message('_valid_upload',
385                                 $this->lang->line('_required_upload'));
386                 return FALSE;
387         }
388         
389         /**
390          * OBSOLETE: AJAX page which retrieves a video plugin.
391          *
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'.
395          *
396          * @param       string $type    'ns-vlc', 'ns-html5'
397          */
398         public function plugin($type)
399         {
400                 $url = $this->input->post('url', TRUE);
401                 
402                 $this->_plugin($type, $url);
403         }
404         
405         /**
406          * OBSOLETE: Video plugin controller
407          *
408          * See plugin function for details. If the second parameter is TRUE
409          * the output is return instead of being displayed (used in preloading).
410          */
411         public function _plugin($type, $url, $return_output=FALSE)
412         {       
413                 if ($type == 'ns-html5')
414                         $data['url'] = 'tribe://' . $url;
415                 else if ($type == 'ns-vlc')
416                         $data['url'] = $url;
417                 
418                 $output = $this->load->view('video/'. $type . '_plugin_view', $data, 
419                         $return_output);
420                 
421                 if ($return_output)
422                         return $output;
423         }
424         
425 }
426
427 /* End of file video.php */
428 /* Location: ./application/controllers/video.php */