uploading works, but AV info is not automatically detected and video activation featu...
[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()
27         {
28                 var_dump($this->session->userdata('user_id'));
29         }
30         
31         /**
32          * The page used for watching a video
33          *
34          * @param       string $id      DB id of the video
35          * @param       string $name    `name` of the video from DB
36          * @param       string $plugin  video plugin ('ns-vlc', 'ns-html5'). If it's set 
37          * to NULL or 'auto', the plugin is automatically selected.
38          */
39         public function watch($id, $name = NULL, $plugin = NULL)
40         {
41                 // **
42                 // ** LOADING MODEL
43                 // **
44                 // Retrieve video information.
45                 $this->load->model('videos_model');
46                 $this->videos_model->inc_views($id);
47                 $data['video'] = $this->videos_model->get_video($id, $name);
48                 $categories = $this->config->item('categories');
49                 $data['video']['category_name'] = 
50                         $categories[ $data['video']['category_id'] ];
51                 $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
52                 $data['user_id'] = $this->session->userdata('user_id');
53                 if ($data['user_id'] === FALSE)
54                         $data['user_id'] = '';
55                 
56                 // Display page.
57                 $params = array(        'title' => $data['video']['title'] . ' &ndash; '
58                                                                 . $this->config->item('site_name'),
59                                                         'css' => array(
60                                                                 'jquery.ui.nsvideo.css',
61                                                                 'video.css'
62                                                         ),
63                                                         'js' => array(
64                                                                 'jquery.ui.nsvideo.js',
65                                                                 'jquery.ui.ajax_links_maker.js'
66                                                         ),
67                                                         //'metas' => array('description'=>'','keywords'=>'')
68                                                         );
69                 $this->load->library('html_head_params', $params);
70                 
71                 // Preloading video plugin.
72                 // TODO plugin auto: type and format
73                 if ($data['plugin_type'] == 'auto')
74                         $data['plugin_type'] = 'ns-html5';
75                 $data['asset_index'] = 0;
76                 
77                 // TODO remove old AJAX plugin content
78 //              $data['plugin_content'] = $this->_plugin('ns-html5', 
79 //                      $data['video']['url'][0], TRUE);
80
81                 // Comments
82                 $data['comments'] = $this->_ajax_comment(TRUE, $id);
83                 
84                 // **
85                 // ** LOADING VIEWS
86                 // **
87                 $this->load->view('html_begin', $this->html_head_params);
88                 $this->load->view('header');
89                 
90                 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
91                 $this->load->view('video/watch_view', $data);
92                 
93                 $this->load->view('footer');
94                 $this->load->view('html_end');
95         }
96                 
97         public function upload()
98         {
99                 $user_id = $this->session->userdata('user_id');
100                 
101                 // Action not possible if an user is not logged in.
102                 if (!$user_id)
103                 {
104                         $this->load->helper('message');
105                         show_error_msg_page($this, 
106                                 $this->lang->line('ui_msg_login_restriction'));
107                         return;
108                 }
109                 
110                 $this->load->library('form_validation');
111
112                 $this->form_validation->set_error_delimiters('<span class="error">',
113                                 '</span>');
114                 
115                 if ($this->form_validation->run('upload') === FALSE)
116                 {
117                         $params = array('title' =>
118                                                                 $this->lang->line('ui_nav_menu_upload')
119                                                                         .' &ndash; '
120                                                                         . $this->config->item('site_name'),
121                                                         //'metas' => array('description'=>'')
122                         );
123                         $this->load->library('html_head_params', $params);
124
125                         // **
126                         // ** LOADING VIEWS
127                         // **
128                         $this->load->view('html_begin', $this->html_head_params);
129                         $this->load->view('header',
130                                         array('selected_menu' => 'upload'));
131
132                         $main_params['content'] = $this->load->view(
133                                         'video/upload_view', array(), TRUE);
134                         $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
135                         $this->load->view('main', $main_params);
136
137                         $this->load->view('footer');
138                         $this->load->view('html_end');
139                 }
140                 else
141                 {
142                         $this->load->model('videos_model');
143                         $this->load->helper('video');
144                         $this->config->load('content_ingestion');
145                         
146                         $file_name = $this->uploaded_file;
147                         $av_info = get_av_info($file_name);
148                         $name = urlencode(str_replace(' ', '-',
149                                         $this->input->post('video-title')));
150                         $category_id = $this->input->post('video-category');
151                         
152                         // Prepare formats
153                         $formats = $this->config->item('formats');
154                         $prepared_formats = prepare_formats($formats, $av_info,
155                                         $this->config->item('elim_dupl_res'));
156                         
157                         // Add video to DB.
158                         $activation_code = $this->videos_model->add_video($name,
159                                         $this->input->post('video-title'),
160                                         $this->input->post('video-description'),
161                                         $this->input->post('video-tags'),
162                                         $av_info['duration'],
163                                         $prepared_formats['db_formats'], $category_id, $user_id);
164                         
165                         // Send a content ingestion request to
166                         // CIS (Content Ingestion Server).
167                         $this->_send_content_ingestion($activation_code,
168                                         $file_name,
169                                         $name, $av_info['size'],
170                                         $prepared_formats['transcode_configs']);
171                         
172                         $this->load->helper('message');
173                         show_info_msg_page($this, 
174                                 $this->lang->line('video_msg_video_uploaded'));
175                 }
176         }
177         
178         /**
179         * Increments (dis)likes count for video with the specified id and returns to
180         * the client as plain text the number if likes.
181         *
182         * @param string $action 'like' or 'dislike'
183         * @param string $video_id
184         * @param string $user_id
185         */
186         public function ajax_vote($action, $video_id)
187         {
188                 $video_id = intval($video_id);
189                 $user_id = $this->session->userdata('user_id');
190                 $this->load->model('videos_model');
191         
192                 $res = $this->videos_model->vote($video_id, $user_id,
193                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
194         
195                 if ($res !== -1)
196                         echo $res;
197         }
198         /**
199          * Increments (dis)likes count for a comment with a specified id and returns
200          * to the client as plain text the number if likes. 
201          * 
202          * @param string $action        'like' or 'dislike'
203          * @param string $comment_id
204          */
205         public function ajax_vote_comment($action, $comment_id)
206         {
207                 $comment_id = intval($comment_id);
208                 $user_id = $this->session->userdata('user_id');
209                 $this->load->model('videos_model');
210                 
211                 $res = $this->videos_model->vote_comment($comment_id, $user_id,
212                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
213                 
214                 if ($res !== -1)
215                         echo $res;
216         }
217         
218         public function ajax_comment($video_id,
219                         $ordering = 'newest', $offset = '0')
220         {
221                 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
222         }
223         
224         public function _ajax_comment($return_output, $video_id,
225                         $ordering = 'newest', $offset = '0')
226         {
227                 $video_id = intval($video_id);
228                 
229                 $this->load->library('form_validation');
230                 $this->form_validation->set_error_delimiters('<span class="error">',
231                                         '</span>');
232                 
233                 if ($this->form_validation->run('comment_video'))
234                 {
235                         $this->load->model('videos_model');
236                         $user_id = intval($this->session->userdata('user_id'));
237                         $comment = $this->input->post('comment');
238                         
239                         $this->videos_model->comment_video($video_id, $user_id, $comment);
240                 }
241                 
242                 // **
243                 // ** MODEL **
244                 // **           
245                 $this->load->model('videos_model');
246                 $data['comments'] = $this->videos_model->get_video_comments($video_id,
247                         $offset, $this->config->item('video_comments_per_page'), $ordering);
248                 $data['comments_count'] =
249                         $this->videos_model->get_video_comments_count($video_id);
250                 $data['hottest_comments'] = $this->videos_model->get_video_comments(
251                         $video_id, 0, 2, 'hottest');
252                 $data['video_id'] = $video_id;
253                 $data['user_id'] = $this->session->userdata('user_id');
254                 
255                 // Pagination
256                 $this->load->library('pagination');
257                 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
258                 $pg_config['uri_segment'] = 5;
259                 $pg_config['total_rows'] = $data['comments_count'];
260                 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
261                 $this->pagination->initialize($pg_config);
262                 $data['comments_pagination'] = $this->pagination->create_links();
263                 
264                 // **
265                 // ** VIEWS **
266                 // **
267                 $output = $this->load->view('video/comments_view',
268                         $data, $return_output);
269                 
270                 if ($return_output)
271                         return $output;
272         }
273         
274         /**
275          * Request content_ingest to the CIS in order to start the content
276          * ingestion process.
277          * 
278          * @param string $activation_code
279          * @param string $raw_video_fn uploaded video file name
280          * @param string $name
281          * @param int $raw_video_size uploaded video file size in bytes
282          * @param array $transcode_configs dictionary which must be included in
283          * the JSON data that needs to be sent to CIS
284          * @return mixed return the HTTP content (body) on success and FALSE
285          * otherwise
286          */
287         protected function _send_content_ingestion($activation_code, $raw_video_fn,
288                         $name, $raw_video_size, $transcode_configs)
289         {
290                 $this->config->load('content_ingestion');
291                 
292                 $url = $this->config->item('cis_url') . 'ingest_content';
293                 $data = array(
294                         'code'=>$activation_code,
295                         'raw_video'=>$raw_video_fn,
296                         'name'=>$name,
297                         'weight'=>$raw_video_size,
298                         'transcode_configs'=>$transcode_configs,
299                         'thumbs'=>$this->config->item('thumbs_count')
300                 );
301                 $json_data = json_encode($data);
302                 
303                 // Send request to CIS.
304                 $r = new HttpRequest($url, HttpRequest::METH_POST);
305                 $r->setBody($json_data);
306                 try
307                 {
308                         $response = $r->send()->getBody();
309                 }
310                 catch (HttpException $ex) 
311                 {
312                         return FALSE;
313                 }
314                 
315                 return $response;
316         }
317         
318         public function _is_user_loggedin($param)
319         {
320                 if (! $this->session->userdata('user_id'))
321                         return FALSE;
322                 
323                 return TRUE;
324         }
325         
326         public function _do_comment($comment)
327         {
328                 // Note: Videos_model must be already loaded.
329                 $this->load->model('videos_model');
330                 
331                 $video_id = intval($this->input->post('video-id'));
332                 $user_id = intval($this->session->userdata('user_id'));
333                 
334                 $this->videos_model->comment_video($video_id, $user_id, $comment);
335         }
336         
337         public function _valid_tags($tags)
338         {
339                 $tok = strtok($tags, ',');
340                 while ($tok != FALSE)
341                 {
342                         $tok = trim($tok);
343                         if (!ctype_alnum($tok))
344                                 return FALSE;
345                         
346                         $tok = strtok(',');
347                 }
348                 
349                 return TRUE;
350         }
351         
352         public function _valid_upload($file)
353         {
354                 if ($_FILES['video-upload-file']['tmp_name'])
355                 {
356                         // Upload library
357                         $config_upload = array();
358                         $config_upload['upload_path'] = './data/upload';
359                         $config_upload['allowed_types'] = '*';
360                         $this->load->library('upload', $config_upload);
361
362                         if ($this->upload->do_upload('video-upload-file'))
363                         {
364                                 $this->uploaded_file = $this->upload->data();
365                                 $this->uploaded_file = $this->uploaded_file['file_name'];
366                                 return TRUE;
367                         }
368                         else
369                         {
370                                 $this->form_validation->set_message('_valid_upload',
371                                                 $this->upload->display_errors('<span class="error">',
372                                                                 '</span>'));
373                                 return FALSE;
374                         }
375                 }
376                 
377                 $this->form_validation->set_message('_valid_upload',
378                                 $this->lang->line('_required_upload'));
379                 return FALSE;
380         }
381         
382         /**
383          * OBSOLETE: AJAX page which retrieves a video plugin.
384          *
385          * The view associated with this controller should be parameter type
386          * concatenated with '_plugin_view' and must be located in
387          * 'application/views/video'.
388          *
389          * @param       string $type    'ns-vlc', 'ns-html5'
390          */
391         public function plugin($type)
392         {
393                 $url = $this->input->post('url', TRUE);
394                 
395                 $this->_plugin($type, $url);
396         }
397         
398         /**
399          * OBSOLETE: Video plugin controller
400          *
401          * See plugin function for details. If the second parameter is TRUE
402          * the output is return instead of being displayed (used in preloading).
403          */
404         public function _plugin($type, $url, $return_output=FALSE)
405         {       
406                 if ($type == 'ns-html5')
407                         $data['url'] = 'tribe://' . $url;
408                 else if ($type == 'ns-vlc')
409                         $data['url'] = $url;
410                 
411                 $output = $this->load->view('video/'. $type . '_plugin_view', $data, 
412                         $return_output);
413                 
414                 if ($return_output)
415                         return $output;
416         }
417         
418 }
419
420 /* End of file video.php */
421 /* Location: ./application/controllers/video.php */