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