4e31540c65d9259ecccdfcda716606f06cb2c486
[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         /**
122         * Increments (dis)likes count for video with the specified id and returns to
123         * the client as plain text the number if likes.
124         *
125         * @param string $action 'like' or 'dislike'
126         * @param string $video_id
127         * @param string $user_id
128         */
129         public function ajax_vote($action, $video_id)
130         {
131                 $video_id = intval($video_id);
132                 $user_id = $this->session->userdata('user_id');
133                 $this->load->model('videos_model');
134         
135                 $res = $this->videos_model->vote($video_id, $user_id,
136                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
137         
138                 if ($res !== -1)
139                         echo $res;
140         }
141         /**
142          * Increments (dis)likes count for a comment with a specified id and returns
143          * to the client as plain text the number if likes. 
144          * 
145          * @param string $action        'like' or 'dislike'
146          * @param string $comment_id
147          */
148         public function ajax_vote_comment($action, $comment_id)
149         {
150                 $comment_id = intval($comment_id);
151                 $user_id = $this->session->userdata('user_id');
152                 $this->load->model('videos_model');
153                 
154                 $res = $this->videos_model->vote_comment($comment_id, $user_id,
155                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
156                 
157                 if ($res !== -1)
158                         echo $res;
159         }
160         
161         public function ajax_comment($video_id,
162                         $ordering = 'newest', $offset = '0')
163         {
164                 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
165         }
166         
167         public function _ajax_comment($return_output, $video_id,
168                         $ordering = 'newest', $offset = '0')
169         {
170                 $video_id = intval($video_id);
171                 
172                 $this->load->library('form_validation');
173                 $this->form_validation->set_error_delimiters('<span class="error">',
174                                         '</span>');
175                 
176                 if ($this->form_validation->run('comment_video'))
177                 {
178                         $this->load->model('videos_model');
179                         $user_id = intval($this->session->userdata('user_id'));
180                         $comment = $this->input->post('comment');
181                         
182                         $this->videos_model->comment_video($video_id, $user_id, $comment);
183                 }
184                 
185                 // **
186                 // ** MODEL **
187                 // **           
188                 $this->load->model('videos_model');
189                 $data['comments'] = $this->videos_model->get_video_comments($video_id,
190                         $offset, $this->config->item('video_comments_per_page'), $ordering);
191                 $data['comments_count'] =
192                         $this->videos_model->get_video_comments_count($video_id);
193                 $data['hottest_comments'] = $this->videos_model->get_video_comments(
194                         $video_id, 0, 2, 'hottest');
195                 $data['video_id'] = $video_id;
196                 $data['user_id'] = $this->session->userdata('user_id');
197                 
198                 // Pagination
199                 $this->load->library('pagination');
200                 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
201                 $pg_config['uri_segment'] = 5;
202                 $pg_config['total_rows'] = $data['comments_count'];
203                 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
204                 $this->pagination->initialize($pg_config);
205                 $data['comments_pagination'] = $this->pagination->create_links();
206                 
207                 // **
208                 // ** VIEWS **
209                 // **
210                 $output = $this->load->view('video/comments_view',
211                         $data, $return_output);
212                 
213                 if ($return_output)
214                         return $output;
215         }
216         
217         public function _is_user_loggedin($param)
218         {
219                 if (! $this->session->userdata('user_id'))
220                         return FALSE;
221                 
222                 return TRUE;
223         }
224         
225         public function _do_comment($comment)
226         {
227                 // Note: Videos_model must be already loaded.
228                 $this->load->model('videos_model');
229                 
230                 $video_id = intval($this->input->post('video-id'));
231                 $user_id = intval($this->session->userdata('user_id'));
232                 
233                 $this->videos_model->comment_video($video_id, $user_id, $comment);
234         }
235         
236         /**
237          * OBSOLETE: AJAX page which retrieves a video plugin.
238          *
239          * The view associated with this controller should be parameter type
240          * concatenated with '_plugin_view' and must be located in
241          * 'application/views/video'.
242          *
243          * @param       string $type    'ns-vlc', 'ns-html5'
244          */
245         public function plugin($type)
246         {
247                 $url = $this->input->post('url', TRUE);
248                 
249                 $this->_plugin($type, $url);
250         }
251         
252         /**
253          * OBSOLETE: Video plugin controller
254          *
255          * See plugin function for details. If the second parameter is TRUE
256          * the output is return instead of being displayed (used in preloading).
257          */
258         public function _plugin($type, $url, $return_output=FALSE)
259         {       
260                 if ($type == 'ns-html5')
261                         $data['url'] = 'tribe://' . $url;
262                 else if ($type == 'ns-vlc')
263                         $data['url'] = $url;
264                 
265                 $output = $this->load->view('video/'. $type . '_plugin_view', $data, 
266                         $return_output);
267                 
268                 if ($return_output)
269                         return $output;
270         }
271         
272 }
273
274 /* End of file video.php */
275 /* Location: ./application/controllers/video.php */