eb8900596596a135c04a6884c05e465f4cbe2112
[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         public function test()
24         {
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');
29                 try
30                 {
31                         echo $r->send()->getBody();
32                 }
33                 catch (HttpException $ex) 
34                 {
35                         echo $ex;
36                 }
37         }
38         
39         /**
40          * The page used for watching a video
41          *
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.
46          */
47         public function watch($id, $name = NULL, $plugin = NULL)
48         {
49                 // **
50                 // ** LOADING MODEL
51                 // **
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'] = '';
63                 
64                 // Display page.
65                 $params = array(        'title' => $data['video']['title'] . ' &ndash; '
66                                                                 . $this->config->item('site_name'),
67                                                         'css' => array(
68                                                                 'jquery.ui.nsvideo.css',
69                                                                 'video.css'
70                                                         ),
71                                                         'js' => array(
72                                                                 'jquery.ui.nsvideo.js',
73                                                                 'jquery.ui.ajax_links_maker.js'
74                                                         ),
75                                                         //'metas' => array('description'=>'','keywords'=>'')
76                                                         );
77                 $this->load->library('html_head_params', $params);
78                 
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;
84                 
85                 // TODO remove old AJAX plugin content
86 //              $data['plugin_content'] = $this->_plugin('ns-html5', 
87 //                      $data['video']['url'][0], TRUE);
88
89                 // Comments
90                 $data['comments'] = $this->_ajax_comment(TRUE, $id);
91                 
92                 // **
93                 // ** LOADING VIEWS
94                 // **
95                 $this->load->view('html_begin', $this->html_head_params);
96                 $this->load->view('header');
97                 
98                 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
99                 $this->load->view('video/watch_view', $data);
100                 
101                 $this->load->view('footer');
102                 $this->load->view('html_end');
103         }
104                 
105         public function upload()
106         {
107                 $this->load->library('form_validation');
108
109                 $this->form_validation->set_error_delimiters('<span class="error">',
110                                 '</span>');
111                 
112                 // TODO check if user is logged in
113
114                 if ($this->form_validation->run('upload') === FALSE)
115                 {
116                         $params = array('title' =>
117                                                                 $this->lang->line('ui_nav_menu_upload')
118                                                                         .' &ndash; '
119                                                                         . $this->config->item('site_name'),
120                                                         //'metas' => array('description'=>'')
121                         );
122                         $this->load->library('html_head_params', $params);
123
124                         // **
125                         // ** LOADING VIEWS
126                         // **
127                         $this->load->view('html_begin', $this->html_head_params);
128                         $this->load->view('header',
129                                         array('selected_menu' => 'upload'));
130
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);
135
136                         $this->load->view('footer');
137                         $this->load->view('html_end');
138                 }
139                 else
140                 {
141                         $this->load->model('videos_model');
142                         $this->load->helper('video');
143                         
144                         $file_name = './data/upload/'. $_FILES['video-upload-file']['name'];
145                         $av_info = get_av_info($file_name);
146                         
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'),
152 //                                      $av_info, 0, 1);
153                         
154                         // TODO call CIS
155                 }
156         }
157         
158         /**
159         * Increments (dis)likes count for video with the specified id and returns to
160         * the client as plain text the number if likes.
161         *
162         * @param string $action 'like' or 'dislike'
163         * @param string $video_id
164         * @param string $user_id
165         */
166         public function ajax_vote($action, $video_id)
167         {
168                 $video_id = intval($video_id);
169                 $user_id = $this->session->userdata('user_id');
170                 $this->load->model('videos_model');
171         
172                 $res = $this->videos_model->vote($video_id, $user_id,
173                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
174         
175                 if ($res !== -1)
176                         echo $res;
177         }
178         /**
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. 
181          * 
182          * @param string $action        'like' or 'dislike'
183          * @param string $comment_id
184          */
185         public function ajax_vote_comment($action, $comment_id)
186         {
187                 $comment_id = intval($comment_id);
188                 $user_id = $this->session->userdata('user_id');
189                 $this->load->model('videos_model');
190                 
191                 $res = $this->videos_model->vote_comment($comment_id, $user_id,
192                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
193                 
194                 if ($res !== -1)
195                         echo $res;
196         }
197         
198         public function ajax_comment($video_id,
199                         $ordering = 'newest', $offset = '0')
200         {
201                 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
202         }
203         
204         public function _ajax_comment($return_output, $video_id,
205                         $ordering = 'newest', $offset = '0')
206         {
207                 $video_id = intval($video_id);
208                 
209                 $this->load->library('form_validation');
210                 $this->form_validation->set_error_delimiters('<span class="error">',
211                                         '</span>');
212                 
213                 if ($this->form_validation->run('comment_video'))
214                 {
215                         $this->load->model('videos_model');
216                         $user_id = intval($this->session->userdata('user_id'));
217                         $comment = $this->input->post('comment');
218                         
219                         $this->videos_model->comment_video($video_id, $user_id, $comment);
220                 }
221                 
222                 // **
223                 // ** MODEL **
224                 // **           
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');
234                 
235                 // Pagination
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();
243                 
244                 // **
245                 // ** VIEWS **
246                 // **
247                 $output = $this->load->view('video/comments_view',
248                         $data, $return_output);
249                 
250                 if ($return_output)
251                         return $output;
252         }
253         
254         public function _is_user_loggedin($param)
255         {
256                 if (! $this->session->userdata('user_id'))
257                         return FALSE;
258                 
259                 return TRUE;
260         }
261         
262         public function _do_comment($comment)
263         {
264                 // Note: Videos_model must be already loaded.
265                 $this->load->model('videos_model');
266                 
267                 $video_id = intval($this->input->post('video-id'));
268                 $user_id = intval($this->session->userdata('user_id'));
269                 
270                 $this->videos_model->comment_video($video_id, $user_id, $comment);
271         }
272         
273         public function _valid_tags($tags)
274         {
275                 $tok = strtok($tags, ',');
276                 while ($tok != FALSE)
277                 {
278                         $tok = trim($tok);
279                         if (!ctype_alnum($tok))
280                                 return FALSE;
281                         
282                         $tok = strtok(',');
283                 }
284                 
285                 return TRUE;
286         }
287         
288         public function _valid_upload($file)
289         {
290                 if ($_FILES['video-upload-file']['tmp_name'])
291                 {
292                         // Upload library
293                         $config_upload = array();
294                         $config_upload['upload_path'] = './data/upload';
295                         $config_upload['allowed_types'] = '*';
296                         $this->load->library('upload', $config_upload);
297
298                         if ($this->upload->do_upload('video-upload-file'))
299                         {
300                                 return TRUE;
301                         }
302                         else
303                         {
304                                 $this->form_validation->set_message('_valid_upload',
305                                                 $this->upload->display_errors('<span class="error">',
306                                                                 '</span>'));
307                                 return FALSE;
308                         }
309                 }
310                 
311                 $this->form_validation->set_message('_valid_upload',
312                                 $this->lang->line('_required_upload'));
313                 return FALSE;
314         }
315         
316         /**
317          * OBSOLETE: AJAX page which retrieves a video plugin.
318          *
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'.
322          *
323          * @param       string $type    'ns-vlc', 'ns-html5'
324          */
325         public function plugin($type)
326         {
327                 $url = $this->input->post('url', TRUE);
328                 
329                 $this->_plugin($type, $url);
330         }
331         
332         /**
333          * OBSOLETE: Video plugin controller
334          *
335          * See plugin function for details. If the second parameter is TRUE
336          * the output is return instead of being displayed (used in preloading).
337          */
338         public function _plugin($type, $url, $return_output=FALSE)
339         {       
340                 if ($type == 'ns-html5')
341                         $data['url'] = 'tribe://' . $url;
342                 else if ($type == 'ns-vlc')
343                         $data['url'] = $url;
344                 
345                 $output = $this->load->view('video/'. $type . '_plugin_view', $data, 
346                         $return_output);
347                 
348                 if ($return_output)
349                         return $output;
350         }
351         
352 }
353
354 /* End of file video.php */
355 /* Location: ./application/controllers/video.php */