working at video comments
[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                 ),
33                 //'metas' => array('description'=>'','keywords'=>'')
34                 );
35                 $this->load->library('html_head_params', $params);
36                 
37                 // **
38                 // ** LOADING VIEWS
39                 // **
40                 $this->load->view('html_begin', $this->html_head_params);
41                 $this->load->view('header');
42                 
43                 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
44                 $this->load->view('echo', array('output'=> 
45                         $this->_ajax_comment(TRUE, $video_id)));
46                 
47                 $this->load->view('footer');
48                 $this->load->view('html_end');
49         }
50         
51         /**
52          * The page used for watching a video
53          *
54          * @param       string $id      DB id of the video
55          * @param       string $name    `name` of the video from DB
56          * @param       string $plugin  video plugin ('ns-vlc', 'ns-html5'). If it's set 
57          * to NULL or 'auto', the plugin is automatically selected.
58          */
59         public function watch($id, $name = NULL, $plugin = NULL)
60         {
61                 // **
62                 // ** LOADING MODEL
63                 // **
64                 // Retrieve video information.
65                 $this->load->model('videos_model');
66                 $this->videos_model->inc_views($id);
67                 $data['video'] = $this->videos_model->get_video($id, $name);
68                 $categories = $this->config->item('categories');
69                 $data['video']['category_name'] = 
70                         $categories[ $data['video']['category_id'] ];
71                 $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
72                 $data['user_id'] = $this->session->userdata('user_id');
73                 if ($data['user_id'] === FALSE)
74                         $data['user_id'] = '';
75                 
76                 // Display page.
77                 $params = array(        'title' => $data['video']['title'] . ' &ndash; '
78                                                                 . $this->config->item('site_name'),
79                                                         'css' => array(
80                                                                 'jquery.ui.nsvideo.css',
81                                                                 'video.css'
82                                                         ),
83                                                         'js' => array(
84                                                                 'jquery.ui.nsvideo.js'
85                                                         ),
86                                                         //'metas' => array('description'=>'','keywords'=>'')
87                                                         );
88                 $this->load->library('html_head_params', $params);
89                 
90                 // Preloading video plugin.
91                 // TODO plugin auto: type and format
92                 if ($data['plugin_type'] == 'auto')
93                         $data['plugin_type'] = 'ns-html5';
94                 $data['asset_index'] = 0;
95                 
96                 // TODO remove old AJAX plugin content
97 //              $data['plugin_content'] = $this->_plugin('ns-html5', 
98 //                      $data['video']['url'][0], TRUE);
99                 
100                 // **
101                 // ** LOADING VIEWS
102                 // **
103                 $this->load->view('html_begin', $this->html_head_params);
104                 $this->load->view('header');
105                 
106                 //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
107                 $this->load->view('video/watch_view', $data);
108                 
109                 $this->load->view('footer');
110                 $this->load->view('html_end');
111         }
112         
113         /**
114          * Increments likes count for video with the specified id and returns to 
115          * the client as plain text the number if likes. 
116          * 
117          * @param string $action        'like' or 'dislike'
118          * @param string $video_id
119          * @param string $user_id
120          */
121         public function ajax_vote($action, $video_id, $user_id = NULL)
122         {
123                 $video_id = intval($video_id);
124                 $user_id = intval($user_id);
125                 $this->load->model('videos_model');
126                 
127                 $res = $this->videos_model->vote($video_id, $user_id, 
128                         (strcmp($action, 'like') == 0 ? TRUE : FALSE));
129                 
130                 if ($res !== -1)
131                         echo $res;
132         }
133         
134         public function ajax_comment($video_id,
135                         $ordering = 'newest', $offset = '0')
136         {
137                 $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
138         }
139         
140         public function _ajax_comment($return_output, $video_id,
141                         $ordering = 'newest', $offset = '0')
142         {
143                 $video_id = intval($video_id);
144                 
145                 $this->load->library('form_validation');
146                 $this->form_validation->set_error_delimiters('<span class="error">',
147                                         '</span>');
148                 $this->form_validation->run('comment_video');
149                 
150                 // **
151                 // ** MODEL **
152                 // **           
153                 $this->load->model('videos_model');
154                 $data['comments'] = $this->videos_model->get_video_comments($video_id,
155                         $offset, $this->config->item('video_comments_per_page'), $ordering);
156                 $data['comments_count'] =
157                         $this->videos_model->get_video_comments_count($video_id);
158                 $data['video_id'] = $video_id;
159                 
160                 // Pagination
161                 $this->load->library('pagination');
162                 $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
163                 $pg_config['uri_segment'] = 5;
164                 $pg_config['total_rows'] = $data['comments_count'];
165                 $pg_config['per_page'] = $this->config->item('video_comments_per_page');
166                 $this->pagination->initialize($pg_config);
167                 $data['comments_pagination'] = $this->pagination->create_links();
168                 
169                 // **
170                 // ** VIEWS **
171                 // **
172                 $output = $this->load->view('video/comments_view',
173                         $data, $return_output);
174                 
175                 if ($return_output)
176                         return $output;
177         }
178         
179         public function _is_user_loggedin($param)
180         {
181                 if (! $this->session->userdata('user_id'))
182                         return FALSE;
183                 
184                 return TRUE;
185         }
186         
187         public function _do_comment($comment)
188         {
189                 // Note: Videos_model must be already loaded.
190                 $this->load->model('videos_model');
191                 
192                 $video_id = intval($this->input->post('video-id'));
193                 $user_id = intval($this->session->userdata('user_id'));
194                 
195                 $this->videos_model->comment_video($video_id, $user_id, $comment);
196         }
197         
198         /**
199          * OBSOLETE: AJAX page which retrieves a video plugin.
200          *
201          * The view associated with this controller should be parameter type
202          * concatenated with '_plugin_view' and must be located in
203          * 'application/views/video'.
204          *
205          * @param       string $type    'ns-vlc', 'ns-html5'
206          */
207         public function plugin($type)
208         {
209                 $url = $this->input->post('url', TRUE);
210                 
211                 $this->_plugin($type, $url);
212         }
213         
214         /**
215          * OBSOLETE: Video plugin controller
216          *
217          * See plugin function for details. If the second parameter is TRUE
218          * the output is return instead of being displayed (used in preloading).
219          */
220         public function _plugin($type, $url, $return_output=FALSE)
221         {       
222                 if ($type == 'ns-html5')
223                         $data['url'] = 'tribe://' . $url;
224                 else if ($type == 'ns-vlc')
225                         $data['url'] = $url;
226                 
227                 $output = $this->load->view('video/'. $type . '_plugin_view', $data, 
228                         $return_output);
229                 
230                 if ($return_output)
231                         return $output;
232         }
233         
234 }
235
236 /* End of file video.php */
237 /* Location: ./application/controllers/video.php */