From a69349a99360f6cb64ae3d87f6572f1d8a98400f Mon Sep 17 00:00:00 2001 From: Calin Burloiu Date: Thu, 6 Oct 2011 17:11:17 +0300 Subject: [PATCH] users can comment videos and can like and dislike comments => a new production version --- application/config/config.php | 3 +- application/config/form_validation.php | 2 +- application/config/pagination.php | 23 +-- application/controllers/user.php | 9 +- application/controllers/video.php | 64 ++++++-- application/helpers/MY_date_helper.php | 16 ++ .../language/english/form_validation_lang.php | 3 +- application/language/english/ui_lang.php | 3 + application/language/english/video_lang.php | 2 + application/models/videos_model.php | 70 ++++++++- application/views/video/comments_view.php | 139 +++++++++++++++--- application/views/video/watch_view.php | 114 +++++++------- css/video.css | 26 +++- js/jquery.ui.ajax_links_maker.js | 56 +++++++ 14 files changed, 418 insertions(+), 112 deletions(-) create mode 100644 application/helpers/MY_date_helper.php create mode 100644 js/jquery.ui.ajax_links_maker.js diff --git a/application/config/config.php b/application/config/config.php index 81ea72d..f5a488d 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -248,7 +248,7 @@ $config['sess_cookie_name'] = 'p2ptube_session'; $config['sess_expiration'] = 7200; $config['sess_expire_on_close'] = FALSE; $config['sess_encrypt_cookie'] = FALSE; -$config['sess_use_database'] = FALSE; +$config['sess_use_database'] = TRUE; $config['sess_table_name'] = 'sessions'; $config['sess_match_ip'] = TRUE; $config['sess_match_useragent'] = TRUE; @@ -384,4 +384,3 @@ function __autoload($class) } } } - diff --git a/application/config/form_validation.php b/application/config/form_validation.php index a3f9df3..a52115c 100644 --- a/application/config/form_validation.php +++ b/application/config/form_validation.php @@ -105,7 +105,7 @@ $config = array( array( 'field'=>'comment', 'label'=>'lang:video_comment', - 'rules'=>'trim|required|xss_clean|callback__is_user_loggedin|callback__do_comment' + 'rules'=>'trim|required|xss_clean|callback__is_user_loggedin' ) ) ); diff --git a/application/config/pagination.php b/application/config/pagination.php index e373ed9..82aea73 100644 --- a/application/config/pagination.php +++ b/application/config/pagination.php @@ -8,21 +8,24 @@ $config['num_links'] = 2; $config['full_tag_open'] = ''; -$config['first_tag_open'] = ' '; -$config['first_tag_close'] = ' '; +$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_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_tag_open'] = '  '; +$config['next_tag_close'] = '  '; $config['next_link'] = $CI->lang->line('ui_page_next'); -$config['last_tag_open'] = ' '; -$config['last_tag_close'] = ' '; +$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 +$config['num_tag_open'] = '  '; +$config['num_tag_close'] = ''; + +$config['cur_tag_open'] = '  '; +$config['cur_tag_close'] = ''; \ No newline at end of file diff --git a/application/controllers/user.php b/application/controllers/user.php index 43b26a3..8ebfdb3 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -257,19 +257,14 @@ class User extends CI_Controller { // ** // Logged in user time zone $time_zone = $this->session->userdata('time_zone'); - if (! $time_zone) - $time_zone = 'UTC'; // User data $userdata = $this->users_model->get_userdata($username); $userdata['roles'] = Users_model::roles_to_string($userdata['roles']); $country_list = $this->config->item('country_list'); $userdata['country_name'] = $country_list[ $userdata['country'] ]; - $userdata['last_login'] = date('Y-m-d H:i:s', - gmt_to_local( - strtotime($userdata['last_login']), - $time_zone, - TRUE)) . ($time_zone == 'UTC' ? ' (UTC)' : ''); + $userdata['last_login'] = human_gmt_to_human_local( + $userdata['last_login'], $time_zone); $userdata['time_zone'] = $this->lang->line($userdata['time_zone']); // User's videos diff --git a/application/controllers/video.php b/application/controllers/video.php index 4ffa089..4e31540 100644 --- a/application/controllers/video.php +++ b/application/controllers/video.php @@ -29,6 +29,7 @@ class Video extends CI_Controller { 'video.css' ), 'js' => array( + 'jquery.ui.ajax_links_maker.js' ), //'metas' => array('description'=>'','keywords'=>'') ); @@ -40,9 +41,12 @@ class Video extends CI_Controller { $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))); + $main_params['content'] = + $this->load->view('echo', array('output'=> + $this->_ajax_comment(TRUE, $video_id)), + TRUE); + $main_params['side'] = $this->load->view('side_default', NULL, TRUE); + $this->load->view('main', $main_params); $this->load->view('footer'); $this->load->view('html_end'); @@ -81,7 +85,8 @@ class Video extends CI_Controller { 'video.css' ), 'js' => array( - 'jquery.ui.nsvideo.js' + 'jquery.ui.nsvideo.js', + 'jquery.ui.ajax_links_maker.js' ), //'metas' => array('description'=>'','keywords'=>'') ); @@ -96,6 +101,9 @@ class Video extends CI_Controller { // TODO remove old AJAX plugin content // $data['plugin_content'] = $this->_plugin('ns-html5', // $data['video']['url'][0], TRUE); + + // Comments + $data['comments'] = $this->_ajax_comment(TRUE, $id); // ** // ** LOADING VIEWS @@ -111,20 +119,39 @@ class Video extends CI_Controller { } /** - * Increments likes count for video with the specified id and returns to - * the client as plain text the number if likes. + * Increments (dis)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) + { + $video_id = intval($video_id); + $user_id = $this->session->userdata('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; + } + /** + * Increments (dis)likes count for a comment with a 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 + * @param string $comment_id */ - public function ajax_vote($action, $video_id, $user_id = NULL) + public function ajax_vote_comment($action, $comment_id) { - $video_id = intval($video_id); - $user_id = intval($user_id); + $comment_id = intval($comment_id); + $user_id = $this->session->userdata('user_id'); $this->load->model('videos_model'); - $res = $this->videos_model->vote($video_id, $user_id, + $res = $this->videos_model->vote_comment($comment_id, $user_id, (strcmp($action, 'like') == 0 ? TRUE : FALSE)); if ($res !== -1) @@ -145,7 +172,15 @@ class Video extends CI_Controller { $this->load->library('form_validation'); $this->form_validation->set_error_delimiters('', ''); - $this->form_validation->run('comment_video'); + + if ($this->form_validation->run('comment_video')) + { + $this->load->model('videos_model'); + $user_id = intval($this->session->userdata('user_id')); + $comment = $this->input->post('comment'); + + $this->videos_model->comment_video($video_id, $user_id, $comment); + } // ** // ** MODEL ** @@ -155,7 +190,10 @@ class Video extends CI_Controller { $offset, $this->config->item('video_comments_per_page'), $ordering); $data['comments_count'] = $this->videos_model->get_video_comments_count($video_id); + $data['hottest_comments'] = $this->videos_model->get_video_comments( + $video_id, 0, 2, 'hottest'); $data['video_id'] = $video_id; + $data['user_id'] = $this->session->userdata('user_id'); // Pagination $this->load->library('pagination'); diff --git a/application/helpers/MY_date_helper.php b/application/helpers/MY_date_helper.php new file mode 100644 index 0000000..8703577 --- /dev/null +++ b/application/helpers/MY_date_helper.php @@ -0,0 +1,16 @@ +config->item('default_torrent_ext')); @@ -260,6 +261,9 @@ class Videos_model extends CI_Model { public function get_video_comments($video_id, $offset, $count, $ordering = 'newest') { + $this->load->helper('date'); + $cond_hottest = ''; + // Ordering switch ($ordering) { @@ -267,7 +271,8 @@ class Videos_model extends CI_Model { $order_statement = "ORDER BY time DESC"; break; case 'hottest': - $order_statement = "ORDER BY time DESC, score DESC"; + $order_statement = "ORDER BY score DESC, time DESC"; + $cond_hottest = "AND c.likes + c.dislikes > 0"; break; default: @@ -275,16 +280,23 @@ class Videos_model extends CI_Model { } $query = $this->db->query( - "SELECT c.*, u.username, (c.likes + c.dislikes) AS score + "SELECT c.*, u.username, u.time_zone, (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"); + WHERE c.user_id = u.id AND video_id = $video_id $cond_hottest + $order_statement + LIMIT $offset, $count"); if ($query->num_rows() == 0) return array(); $comments = $query->result_array(); + foreach ($comments as & $comment) + { + $comment['local_time'] = human_gmt_to_human_local($comment['time'], + $comment['time_zone']); + } + return $comments; } @@ -310,6 +322,11 @@ class Videos_model extends CI_Model { */ public function comment_video($video_id, $user_id, $content) { + // Prepping content. + $content = substr($content, 0, 512); + $content = htmlspecialchars($content); + $content = nl2br($content); + return $query = $this->db->query( "INSERT INTO `videos_comments` (video_id, user_id, content, time) VALUES ($video_id, $user_id, '$content', UTC_TIMESTAMP())"); @@ -373,6 +390,51 @@ class Videos_model extends CI_Model { return FALSE; } + public function vote_comment($comment_id, $user_id, $like = TRUE) + { + if ($like) + { + $col = 'likes'; + $action = 'like'; + } + else + { + $col = 'dislikes'; + $action = 'dislike'; + } + + $query = $this->db->query("SELECT * FROM `users_actions` + WHERE user_id = $user_id + AND target_id = $comment_id + AND target_type = 'vcomment' + AND action = '$action' + AND date = CURDATE()"); + // User already voted today + if ($query->num_rows() > 0) + return -1; + + $this->db->query("UPDATE `videos_comments` + SET $col = $col + 1 + WHERE id = $comment_id"); + + // Mark this action so that the user cannot repeat it today. + $this->db->query("INSERT INTO `users_actions` + (user_id, action, target_type, target_id, date) + VALUES ( $user_id, '$action', 'vcomment', $comment_id, CURDATE() )"); + + $query = $this->db->query("SELECT $col FROM `videos_comments` + WHERE id = $comment_id"); + + if ($query->num_rows() === 1) + { + $row = $query->row_array(); + return $row[ $col ]; + } + + // Error + return FALSE; + } + public function get_thumbs($name, $count) { $thumbs = array(); diff --git a/application/views/video/comments_view.php b/application/views/video/comments_view.php index d4ebc3f..80f56fd 100644 --- a/application/views/video/comments_view.php +++ b/application/views/video/comments_view.php @@ -1,36 +1,139 @@ -
-

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

+

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

- - - -
- + + + +
+ 512 lang->line('ui_chars_left') ?> +
+
+ + + + +

lang->line('video_title_no_comments') ?>

- -

lang->line('video_title_no_comments') ?>

- -

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

+ - -
"> - () + +

lang->line('video_title_hottest_comments'). ": " ?>

+ + +
"> + () +
+ +
+ +
lang->line('video_like') ?> +  lang->line('video_dislike') ?> +     "> lang->line('ui_likes') ?>, + "> lang->line('ui_dislikes'); ?>
-
-
+ +

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

+ + +
"> + () +
+ +
+ +
lang->line('video_like') ?> +  lang->line('video_dislike') ?> +     "> lang->line('ui_likes') ?>, + "> lang->line('ui_dislikes'); ?> +
+ + + + \ No newline at end of file diff --git a/application/views/video/watch_view.php b/application/views/video/watch_view.php index f102c4e..2da1ba0 100644 --- a/application/views/video/watch_view.php +++ b/application/views/video/watch_view.php @@ -34,58 +34,68 @@
-
-
- lang->line('ui_uploaded_by') ?> - "> - lang->line('ui_on_date') ?> - -
+
-
-
- lang->line('ui_view') : - $this->lang->line('ui_views') ); - ?> -
- -
- lang->line('video_like') ?> - lang->line('video_dislike') ?> - lang->line('ui_likes') ?>, - lang->line('ui_dislikes'); ?> + - -
-
-
lang->line('ui_category')) - . ': ' ?>
-
-
- -
-
lang->line('ui_tags')). ': ' ?>
-
$score): ?> - - - -
-
- -
-
lang->line('ui_license')).': ' ?>
-
-
+
-
@@ -134,7 +144,7 @@ }); $('.link-vote') - .click(function() { + .click(function(event) { var user_id = ""; var action, idOutput; if ($(this).data('action') == 'like') @@ -149,13 +159,15 @@ } //alert(action + " " + user_id); + event.preventDefault(); + if (user_id.length != 0) { $.ajax({ type: "GET", url: "/" + action - + "", + + "", data: {t: ""+Math.random()}, dataType: "text", success: function(text) { @@ -179,7 +191,7 @@ { $.ajax({ type: "GET", - url: "", + url: "", dataType: "text", success: function(text) { if (text) diff --git a/css/video.css b/css/video.css index fe76565..babcef9 100644 --- a/css/video.css +++ b/css/video.css @@ -1,12 +1,14 @@ -#watch-main +.ui-tabs-panel { - width: 1056px; - margin: 0 auto; + padding: 0 !important; } -.ui-tabs-panel +#video-footer { - padding: 0 !important; + float: left; + width: 752px; + margin: 8px; + /* outline: 2px outset gray; */ } dt @@ -31,4 +33,18 @@ dd .video-tag { margin-right: 1.6em; +} + +.comment-info +{ + margin-bottom: 0.33em; +} +.comment-content +{ + margin-bottom: 0.33em; +} +.comment-popularity +{ + font-size: 0.8em; + margin-bottom: 2em; } \ No newline at end of file diff --git a/js/jquery.ui.ajax_links_maker.js b/js/jquery.ui.ajax_links_maker.js new file mode 100644 index 0000000..2895e70 --- /dev/null +++ b/js/jquery.ui.ajax_links_maker.js @@ -0,0 +1,56 @@ +/* + * jQuery UI AJAX Links Maker 1.0.0 beta + * + * Transforms normal anchors into AJAX anchors. The AJAX URL is took from + * href HTML attribute. Anchors to be transformed are found within elements + * listed in option linkSelectors. The AJAX content retrieved from the server + * is placed in the option target. + * + * Copyright 2011, Călin-Andrei Burloiu + * Dual licensed under the MIT or GPL Version 2 licenses. + * http://jquery.org/license + * + * Depends: + * jquery.ui.core.js + * jquery.ui.widget.js + */ +(function( $, undefined ) { + +$.widget( "ui.ajaxLinksMaker", { + version: "1.0.0 beta", + options: { + + }, + + _create: function() { + var widget = this; + + for (i in widget.options.linkSelectors) + { + var selector = widget.options.linkSelectors[i]; + + $(selector + ' a', widget[0]) + .each(function(index) { + var url = $(this).attr('href'); + + if (typeof(url) == 'undefined') + return; + + $(this) + .click(function(event) { + event.preventDefault(); + + $.post( + url, + function(data) { + $(widget.options.target).html(data); + }); + }); + }); + + } + } + +}); + +})( jQuery ); \ No newline at end of file -- 2.20.1