From faf92fa039c2be353c94d0d0e8e488e56eaa5058 Mon Sep 17 00:00:00 2001 From: Calin Burloiu Date: Wed, 28 Sep 2011 17:30:15 +0300 Subject: [PATCH] working at video comments --- application/config/form_validation.php | 9 +- application/config/p2p-tube.php | 10 +++ application/config/pagination.php | 16 +++- application/controllers/user.php | 12 ++- application/controllers/video.php | 95 ++++++++++++++++++++- application/language/english/video_lang.php | 5 ++ application/models/users_model.php | 27 +++++- application/models/videos_model.php | 77 ++++++++++++++++- application/views/user/profile_view.php | 2 +- application/views/user/register_view.php | 2 +- application/views/video/comments_view.php | 36 ++++++++ application/views/video/watch_view.php | 2 +- 12 files changed, 278 insertions(+), 15 deletions(-) create mode 100644 application/views/video/comments_view.php diff --git a/application/config/form_validation.php b/application/config/form_validation.php index ff6bd0d..a3f9df3 100644 --- a/application/config/form_validation.php +++ b/application/config/form_validation.php @@ -65,7 +65,7 @@ $config = array( array( 'field'=>'birth-date', 'label'=>'lang:user_birth_date', - 'rules'=>'trim|callback__valid_date' + 'rules'=>'trim|callback__valid_date|callback__postprocess_birth_date' ), array( 'field'=>'locality', @@ -100,6 +100,13 @@ $config = array( 'label'=>'lang:user_email', 'rules'=>'trim|required|xss_clean|valid_email' ) + ), + 'comment_video'=> array( + array( + 'field'=>'comment', + 'label'=>'lang:video_comment', + 'rules'=>'trim|required|xss_clean|callback__is_user_loggedin|callback__do_comment' + ) ) ); diff --git a/application/config/p2p-tube.php b/application/config/p2p-tube.php index e924e69..a35cddc 100644 --- a/application/config/p2p-tube.php +++ b/application/config/p2p-tube.php @@ -122,6 +122,16 @@ $config['videos_per_row'] = 5; */ $config['search_results_per_page'] = 20; +/* +|-------------------------------------------------------------------------- +| Video Comments per Page +|-------------------------------------------------------------------------- +| +| The number of video comments shown per page (as in video/watch). +| +*/ +$config['video_comments_per_page'] = 20; + /* |-------------------------------------------------------------------------- | A list with all available languages in which the site is translated diff --git a/application/config/pagination.php b/application/config/pagination.php index cd18203..e373ed9 100644 --- a/application/config/pagination.php +++ b/application/config/pagination.php @@ -8,7 +8,21 @@ $config['num_links'] = 2; $config['full_tag_open'] = ''; +$config['first_tag_open'] = ' '; +$config['first_tag_close'] = ' '; $config['first_link'] = $CI->lang->line('ui_page_first'); + +$config['prev_tag_open'] = ' '; +$config['prev_tag_close'] = ' '; $config['prev_link'] = $CI->lang->line('ui_page_previous'); + +$config['next_tag_open'] = ' '; +$config['next_tag_close'] = ' '; $config['next_link'] = $CI->lang->line('ui_page_next'); -$config['last_link'] = $CI->lang->line('ui_page_last'); \ No newline at end of file + +$config['last_tag_open'] = ' '; +$config['last_tag_close'] = ' '; +$config['last_link'] = $CI->lang->line('ui_page_last'); + +$config['num_tag_open'] = ' '; +$config['num_tag_close'] = ' '; \ No newline at end of file diff --git a/application/controllers/user.php b/application/controllers/user.php index 169fa46..43b26a3 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -39,7 +39,6 @@ class User extends CI_Controller { public function login($redirect = '') { $this->load->library('form_validation'); - $this->form_validation->set_error_delimiters('', ''); @@ -203,7 +202,7 @@ class User extends CI_Controller { 640, 480, IMAGETYPE_AUTO); } // Create thumbnail. - $data['picture'] = $upload_data['file_name']. '-thumb.jpg'; + $data['picture'] = $upload_data['file_name']; $this->image->save_thumbnail($upload_data['file_path'] . $upload_data['file_name']. '-thumb.jpg', 120, 90); } @@ -506,6 +505,15 @@ class User extends CI_Controller { return (preg_match('/[\d]{4}-[\d]{2}-[\d]{2}/', $date) === 1); } + public function _postprocess_birth_date($date) + { + // If the user entered no birth date NULL needs to be inserted into DB. + if (! $date) + return NULL; + + return $date; + } + public function _valid_old_password($old_password, $field_username) { if (! $old_password) diff --git a/application/controllers/video.php b/application/controllers/video.php index 5f5ab9f..4ffa089 100644 --- a/application/controllers/video.php +++ b/application/controllers/video.php @@ -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 * @@ -104,8 +131,72 @@ class Video extends CI_Controller { 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('', + ''); + $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); + } + /** - * AJAX page which retrieves a video plugin. + * 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 @@ -121,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). diff --git a/application/language/english/video_lang.php b/application/language/english/video_lang.php index 288cf34..0f667e0 100644 --- a/application/language/english/video_lang.php +++ b/application/language/english/video_lang.php @@ -3,5 +3,10 @@ $lang['video_like'] = 'Like'; $lang['video_dislike'] = 'Dislike'; +$lang['video_title_comment'] = 'Comment'; +$lang['video_title_all_comments'] = 'All Comments'; +$lang['video_title_no_comments'] = 'No comments yet'; +$lang['video_submit_post_comment'] = 'Post'; + /* End of file video_lang.php */ /* Location: ./application/language/english/video_lang.php */ \ No newline at end of file diff --git a/application/models/users_model.php b/application/models/users_model.php index d59987c..76f378d 100644 --- a/application/models/users_model.php +++ b/application/models/users_model.php @@ -205,10 +205,17 @@ class Users_model extends CI_Model { $vals = ''; foreach ($data as $col=> $val) { + if ($val === NULL) + { + $cols .= "$col, "; + $vals .= "NULL, "; + continue; + } + $cols .= "$col, "; if (is_int($val)) $vals .= "$val, "; - else + else if (is_string($val)) $vals .= "'$val', "; } $cols = substr($cols, 0, -2); @@ -217,7 +224,6 @@ class Users_model extends CI_Model { $query = $this->db->query("INSERT INTO `users` ($cols, registration_date, last_login) VALUES ($vals, utc_timestamp(), utc_timestamp())"); - if ($query === FALSE) return FALSE; @@ -358,7 +364,18 @@ class Users_model extends CI_Model { if ($query->num_rows() === 0) return FALSE; - return $query->row_array(); + $userdata = $query->row_array(); + + // Post process userdata. + if (isset($userdata['picture'])) + { + $userdata['picture_thumb'] = site_url( + "data/user_pictures/{$userdata['picture']}-thumb.jpg"); + $userdata['picture'] = site_url( + "data/user_pictures/{$userdata['picture']}"); + } + + return $userdata; } /** @@ -383,8 +400,10 @@ class Users_model extends CI_Model { { if (is_int($val)) $set .= "$col = $val, "; - else + else if (is_string($val)) $set .= "$col = '$val', "; + else if (is_null($var)) + $set .= "$col = NULL, "; } $set = substr($set, 0, -2); diff --git a/application/models/videos_model.php b/application/models/videos_model.php index 02aef22..da7cdfe 100644 --- a/application/models/videos_model.php +++ b/application/models/videos_model.php @@ -129,7 +129,7 @@ class Videos_model extends CI_Model { * * @param int $category_id * @param mixed $user an user_id (as int) or an username (as string) - * @return int number of videos or NULL if an error occured + * @return int number of videos or FALSE if an error occured */ public function get_videos_count($category_id = NULL, $user = NULL) { @@ -157,7 +157,7 @@ class Videos_model extends CI_Model { return $query->row()->count; // Error - return NULL; + return FALSE; } /** @@ -242,6 +242,79 @@ class Videos_model extends CI_Model { return $video; } + /** + * Retrieves comments for a video. + * + * @param int $video_id + * @param int $offset + * @param int $count + * @param string $ordering control comments ording by these possibilities: + * + * @return array an array with comments + */ + public function get_video_comments($video_id, $offset, $count, + $ordering = 'newest') + { + // Ordering + switch ($ordering) + { + case 'newest': + $order_statement = "ORDER BY time DESC"; + break; + case 'hottest': + $order_statement = "ORDER BY time DESC, score DESC"; + break; + + default: + $order_statement = ""; + } + + $query = $this->db->query( + "SELECT c.*, u.username, (c.likes + c.dislikes) AS score + FROM `videos_comments` c, `users` u + WHERE c.user_id = u.id AND video_id = $video_id + $order_statement"); + + if ($query->num_rows() == 0) + return array(); + + $comments = $query->result_array(); + + return $comments; + } + + public function get_video_comments_count($video_id) + { + $query = $this->db->query( + "SELECT COUNT(*) count + FROM `videos_comments` + WHERE video_id = $video_id"); + + if ($query->num_rows() == 0) + return FALSE; + + return $query->row()->count; + } + + /** + * Insert in DB a comment for a video. + * + * @param int $video_id + * @param int $user_id + * @param string $content + */ + public function comment_video($video_id, $user_id, $content) + { + return $query = $this->db->query( + "INSERT INTO `videos_comments` (video_id, user_id, content, time) + VALUES ($video_id, $user_id, '$content', UTC_TIMESTAMP())"); + } + /** * Increments views count for a video. * diff --git a/application/views/user/profile_view.php b/application/views/user/profile_view.php index d8db9ac..d831658 100644 --- a/application/views/user/profile_view.php +++ b/application/views/user/profile_view.php @@ -18,7 +18,7 @@ - " style="float: left" /> + diff --git a/application/views/user/register_view.php b/application/views/user/register_view.php index 419353a..59fee7e 100644 --- a/application/views/user/register_view.php +++ b/application/views/user/register_view.php @@ -143,7 +143,7 @@ else lang->line('user_picture'). ' : ' ?> - " alt="" /> + <?php echo $userdata['username'] ?> diff --git a/application/views/video/comments_view.php b/application/views/video/comments_view.php new file mode 100644 index 0000000..d4ebc3f --- /dev/null +++ b/application/views/video/comments_view.php @@ -0,0 +1,36 @@ +
+

lang->line('video_title_comment') ?>:

+ + + + +
+ + + +

lang->line('video_title_no_comments') ?>

+ +

lang->line('video_title_all_comments'). " ($comments_count): " ?>

+ + +
"> + () +
+
+ + +
+ + \ No newline at end of file diff --git a/application/views/video/watch_view.php b/application/views/video/watch_view.php index a775419..f102c4e 100644 --- a/application/views/video/watch_view.php +++ b/application/views/video/watch_view.php @@ -37,7 +37,7 @@
lang->line('ui_uploaded_by') ?> - "> + "> lang->line('ui_on_date') ?>
-- 2.20.1