working at video comments
[living-lab-site.git] / application / controllers / video.php
index 321dc80..4ffa089 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 
 /**
- * Class Video controlls video items handling: watching, commenting, rating,
+ * Class Video controls video items handling: watching, commenting, rating,
  * adding etc.
  *
  * @category   Controller
@@ -13,7 +13,7 @@ class Video extends CI_Controller {
        {
                parent::__construct();
                
-               //$this->lang->load('video');
+               $this->lang->load('video');
        }
        
        public function index()
@@ -21,6 +21,33 @@ class Video extends CI_Controller {
                
        }
        
+       public function test($video_id)
+       {
+               // Display page.
+               $params = array(        'title' => $this->config->item('site_name'),
+                                                                       'css' => array(
+                                                                               'video.css'
+               ),
+                                                                       'js' => array(
+               ),
+               //'metas' => array('description'=>'','keywords'=>'')
+               );
+               $this->load->library('html_head_params', $params);
+               
+               // **
+               // ** LOADING VIEWS
+               // **
+               $this->load->view('html_begin', $this->html_head_params);
+               $this->load->view('header');
+               
+               //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
+               $this->load->view('echo', array('output'=> 
+                       $this->_ajax_comment(TRUE, $video_id)));
+               
+               $this->load->view('footer');
+               $this->load->view('html_end');
+       }
+       
        /**
         * The page used for watching a video
         *
@@ -31,31 +58,34 @@ class Video extends CI_Controller {
         */
        public function watch($id, $name = NULL, $plugin = NULL)
        {
-               $this->load->helper('url');
-               
+               // **
+               // ** LOADING MODEL
+               // **
                // Retrieve video information.
                $this->load->model('videos_model');
+               $this->videos_model->inc_views($id);
                $data['video'] = $this->videos_model->get_video($id, $name);
+               $categories = $this->config->item('categories');
+               $data['video']['category_name'] = 
+                       $categories[ $data['video']['category_id'] ];
                $data['plugin_type'] = ($plugin === NULL ? 'auto' : $plugin);
+               $data['user_id'] = $this->session->userdata('user_id');
+               if ($data['user_id'] === FALSE)
+                       $data['user_id'] = '';
                
                // Display page.
-               $params = array(        'title' => $data['video']['title'] . ' -- '
+               $params = array(        'title' => $data['video']['title'] . ' &ndash; '
                                                                . $this->config->item('site_name'),
                                                        'css' => array(
-                                                               'jquery-ui-1.8.14.custom.css',
                                                                'jquery.ui.nsvideo.css',
                                                                'video.css'
                                                        ),
                                                        'js' => array(
-                                                               'jquery-1.6.2.min.js',
-                                                               'jquery-ui-1.8.14.custom.min.js',
                                                                'jquery.ui.nsvideo.js'
                                                        ),
                                                        //'metas' => array('description'=>'','keywords'=>'')
                                                        );
                $this->load->library('html_head_params', $params);
-               $this->load->view('html_begin', $this->html_head_params);
-               $this->load->view('header');
                
                // Preloading video plugin.
                // TODO plugin auto: type and format
@@ -67,6 +97,13 @@ class Video extends CI_Controller {
 //             $data['plugin_content'] = $this->_plugin('ns-html5', 
 //                     $data['video']['url'][0], TRUE);
                
+               // **
+               // ** LOADING VIEWS
+               // **
+               $this->load->view('html_begin', $this->html_head_params);
+               $this->load->view('header');
+               
+               //$main_params['content'] = $this->load->view('video/watch_view', $data, TRUE);
                $this->load->view('video/watch_view', $data);
                
                $this->load->view('footer');
@@ -74,7 +111,92 @@ class Video extends CI_Controller {
        }
        
        /**
-        * AJAX page which retrieves a video plugin.
+        * Increments likes count for video with the specified id and returns to 
+        * the client as plain text the number if likes. 
+        * 
+        * @param string $action        'like' or 'dislike'
+        * @param string $video_id
+        * @param string $user_id
+        */
+       public function ajax_vote($action, $video_id, $user_id = NULL)
+       {
+               $video_id = intval($video_id);
+               $user_id = intval($user_id);
+               $this->load->model('videos_model');
+               
+               $res = $this->videos_model->vote($video_id, $user_id, 
+                       (strcmp($action, 'like') == 0 ? TRUE : FALSE));
+               
+               if ($res !== -1)
+                       echo $res;
+       }
+       
+       public function ajax_comment($video_id,
+                       $ordering = 'newest', $offset = '0')
+       {
+               $this->_ajax_comment(FALSE, $video_id, $ordering, $offset);
+       }
+       
+       public function _ajax_comment($return_output, $video_id,
+                       $ordering = 'newest', $offset = '0')
+       {
+               $video_id = intval($video_id);
+               
+               $this->load->library('form_validation');
+               $this->form_validation->set_error_delimiters('<span class="error">',
+                                       '</span>');
+               $this->form_validation->run('comment_video');
+               
+               // **
+               // ** MODEL **
+               // **           
+               $this->load->model('videos_model');
+               $data['comments'] = $this->videos_model->get_video_comments($video_id,
+                       $offset, $this->config->item('video_comments_per_page'), $ordering);
+               $data['comments_count'] =
+                       $this->videos_model->get_video_comments_count($video_id);
+               $data['video_id'] = $video_id;
+               
+               // Pagination
+               $this->load->library('pagination');
+               $pg_config['base_url'] = site_url("video/ajax_comment/$video_id/$ordering/");
+               $pg_config['uri_segment'] = 5;
+               $pg_config['total_rows'] = $data['comments_count'];
+               $pg_config['per_page'] = $this->config->item('video_comments_per_page');
+               $this->pagination->initialize($pg_config);
+               $data['comments_pagination'] = $this->pagination->create_links();
+               
+               // **
+               // ** VIEWS **
+               // **
+               $output = $this->load->view('video/comments_view',
+                       $data, $return_output);
+               
+               if ($return_output)
+                       return $output;
+       }
+       
+       public function _is_user_loggedin($param)
+       {
+               if (! $this->session->userdata('user_id'))
+                       return FALSE;
+               
+               return TRUE;
+       }
+       
+       public function _do_comment($comment)
+       {
+               // Note: Videos_model must be already loaded.
+               $this->load->model('videos_model');
+               
+               $video_id = intval($this->input->post('video-id'));
+               $user_id = intval($this->session->userdata('user_id'));
+               
+               $this->videos_model->comment_video($video_id, $user_id, $comment);
+       }
+       
+       /**
+        * OBSOLETE: AJAX page which retrieves a video plugin.
         *
         * The view associated with this controller should be parameter type
         * concatenated with '_plugin_view' and must be located in
@@ -90,7 +212,7 @@ class Video extends CI_Controller {
        }
        
        /**
-        * Video plugin controller
+        * OBSOLETE: Video plugin controller
         *
         * See plugin function for details. If the second parameter is TRUE
         * the output is return instead of being displayed (used in preloading).