working at video comments
authorCalin Burloiu <calin.burloiu@gmail.com>
Wed, 28 Sep 2011 14:30:15 +0000 (17:30 +0300)
committerCalin Burloiu <calin.burloiu@gmail.com>
Wed, 28 Sep 2011 14:30:15 +0000 (17:30 +0300)
12 files changed:
application/config/form_validation.php
application/config/p2p-tube.php
application/config/pagination.php
application/controllers/user.php
application/controllers/video.php
application/language/english/video_lang.php
application/models/users_model.php
application/models/videos_model.php
application/views/user/profile_view.php
application/views/user/register_view.php
application/views/video/comments_view.php [new file with mode: 0644]
application/views/video/watch_view.php

index ff6bd0d..a3f9df3 100644 (file)
@@ -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'
+               )
        )
 );
 
index e924e69..a35cddc 100644 (file)
@@ -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
index cd18203..e373ed9 100644 (file)
@@ -8,7 +8,21 @@ $config['num_links'] = 2;
 $config['full_tag_open'] = '<div class="pagination">';
 $config['full_tag_close'] = '</div>';
 
+$config['first_tag_open'] = ' <span class="pg-first">';
+$config['first_tag_close'] = '</span> ';
 $config['first_link'] = $CI->lang->line('ui_page_first');
+
+$config['prev_tag_open'] = ' <span class="pg-prev">';
+$config['prev_tag_close'] = '</span> ';
 $config['prev_link'] = $CI->lang->line('ui_page_previous');
+
+$config['next_tag_open'] = ' <span class="pg-next">';
+$config['next_tag_close'] = '</span> ';
 $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'] = ' <span class="pg-last">';
+$config['last_tag_close'] = '</span> ';
+$config['last_link'] = $CI->lang->line('ui_page_last');
+
+$config['num_tag_open'] = ' <span class="pg-num">';
+$config['num_tag_close'] = '</span> ';
\ No newline at end of file
index 169fa46..43b26a3 100644 (file)
@@ -39,7 +39,6 @@ class User extends CI_Controller {
        public function login($redirect = '')
        {
                $this->load->library('form_validation');
-                       
                $this->form_validation->set_error_delimiters('<span class="error">',
                        '</span>');
 
@@ -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)
index 5f5ab9f..4ffa089 100644 (file)
@@ -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('<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);
+       }
+       
        /**
-        * 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).
index 288cf34..0f667e0 100644 (file)
@@ -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
index d59987c..76f378d 100644 (file)
@@ -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);
                
index 02aef22..da7cdfe 100644 (file)
@@ -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:
+        * <ul>
+        *   <li><strong>'hottest':</strong> newest most appreciated first. An
+        *   appreciated comment is one which has a bigger
+        *   score = likes - dislikes.</li>
+        *   <li><strong>'newest':</strong> newest first.</li>
+        * </ul>
+        * @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.
         * 
index d8db9ac..d831658 100644 (file)
@@ -18,7 +18,7 @@
                        <tr>
                                <td>
                                  <?php if ($userdata['picture']): ?>
-                                       <img src="<?php echo site_url("data/user_pictures/{$userdata['picture']}") ?>" style="float: left" />
+                                       <a href="<?php echo $userdata['picture'] ?>"><img src="<?php echo $userdata['picture_thumb'] ?>" style="float: left" /></a>
                                  <?php endif ?>
                                </td>
                                <td></td>
index 419353a..59fee7e 100644 (file)
@@ -143,7 +143,7 @@ else
        <tr>
                <th><?php echo $this->lang->line('user_picture'). ' : ' ?></th>
                <td>
-                       <img src="<?php echo site_url("data/user_pictures/{$userdata['picture']}") ?>" alt="<?php echo $userdata['username'] ?>" />
+                       <a href="<?php echo $userdata['picture'] ?>"><img src="<?php echo $userdata['picture_thumb'] ?>" alt="<?php echo $userdata['username'] ?>" /></a>
                </td>
        </tr>
        <tr><td></td><td></td></tr>
diff --git a/application/views/video/comments_view.php b/application/views/video/comments_view.php
new file mode 100644 (file)
index 0000000..d4ebc3f
--- /dev/null
@@ -0,0 +1,36 @@
+<div id="video-comments">
+       <h4><?php echo $this->lang->line('video_title_comment') ?>: </h4>
+
+       <?php echo form_open("video/comment/$video_id") ?>
+               <textarea name="comment" id="comment" rows="2" cols="56"></textarea>
+               
+               <div><input type="button" id="button-post" value="<?php echo $this->lang->line('video_submit_post_comment') ?>" /></div>
+       </form>
+
+  <?php if ($comments_count == 0): ?>
+       <h4><?php echo $this->lang->line('video_title_no_comments') ?></h4>
+  <?php else: ?>
+       <h4><?php echo $this->lang->line('video_title_all_comments'). " ($comments_count): " ?></h4>
+
+       <?php foreach ($comments as $comment): ?>
+               <div class="comment-info"><span class="comment-user"><a href="<?php echo site_url("user/profile/{$comment['username']}") ?>"><?php echo $comment['username'] ?></a></span>
+                       (<span class="comment-time"><?php echo $comment['time'] ?></span>)
+               </div>
+               <div class="comment-content"><?php echo $comment['content'] ?></div>
+       <?php endforeach ?>
+  <?php endif ?>
+</div>
+
+<script type="text/javascript">
+       $(function() {
+               $('#button-post')
+                       .click(function() {
+                               $.post('<?php echo site_url("video/ajax_comment/$video_id") ?>',
+                                       {comment: $('#comment').val(), 'video-id': <?php echo $video_id ?>},
+                                       function(data) {
+                                               $('#video-comments').html(data);
+                                       });
+                       });
+       });
+
+</script>
\ No newline at end of file
index a775419..f102c4e 100644 (file)
@@ -37,7 +37,7 @@
        <div id="video-info" style="clear: both">
        <div id="video-upload-info">
                <?php echo $this->lang->line('ui_uploaded_by') ?>
-                       <span id="video-date"><a href="<?php echo site_url("user/profile/{$video['username']}") ?>"><?php echo $video['username'] ?></a></span>
+                       <span id="video-user"><a href="<?php echo site_url("user/profile/{$video['username']}") ?>"><?php echo $video['username'] ?></a></span>
                        <?php echo $this->lang->line('ui_on_date') ?>
                        <span id="video-date"><?php echo $video['date'] ?></span>
        </div>