users can add pictures to their profiles; users can like and dislike videos
authorCalin Burloiu <calin.burloiu@gmail.com>
Tue, 27 Sep 2011 15:30:45 +0000 (18:30 +0300)
committerCalin Burloiu <calin.burloiu@gmail.com>
Tue, 27 Sep 2011 15:30:45 +0000 (18:30 +0300)
14 files changed:
application/controllers/user.php
application/controllers/video.php
application/language/english/ui_lang.php
application/language/english/user_lang.php
application/language/english/video_lang.php
application/libraries/Image.php
application/models/videos_model.php
application/views/user/profile_view.php
application/views/user/register_view.php
application/views/video/watch_view.php
css/default.css
css/video.css
data/index.html [new file with mode: 0644]
data/user_pictures/index.html [new file with mode: 0644]

index 96454be..169fa46 100644 (file)
@@ -26,7 +26,7 @@ class User extends CI_Controller {
        
        public function test($user_id = 1)
        {
-               echo sha1('hQwCUEPQZcN8c4Es');
+
        }
 
        /**
@@ -112,8 +112,32 @@ class User extends CI_Controller {
                        
                $this->form_validation->set_error_delimiters('<span class="error">',
                                        '</span>');
+               $error_upload = '';
+
+               if ($this->form_validation->run('register'))
+               {
+                       $b_validation = TRUE;
+                       
+                       if ($_FILES['picture']['tmp_name'])
+                       {
+                               // Upload library
+                               $config_upload['upload_path'] = './data/user_pictures';
+                               $config_upload['file_name'] = 
+                                       str_replace('.', '-', $this->input->post('username')) .'-';
+                               $config_upload['allowed_types'] = 'gif|jpg|png';
+                               $config_upload['max_size'] = '10240';
+                               $this->load->library('upload', $config_upload);
+                               
+                               $b_validation = $this->upload->do_upload('picture');
+                               $error_upload = 
+                                       $this->upload->display_errors('<span class="error">',
+                                                       '</span>');
+                       }
+               }
+               else
+                       $b_validation = FALSE;
                
-               if ($this->form_validation->run('register') === FALSE)
+               if (! $b_validation)
                {
                        // Edit account data if logged in, otherwise register.
                        if ($user_id = $this->session->userdata('user_id'))
@@ -143,7 +167,8 @@ class User extends CI_Controller {
                                array('selected_menu' => $selected_menu));
                        
                        $main_params['content'] = $this->load->view('user/register_view', 
-                               array('userdata'=> $userdata, 'redirect'=> $redirect),
+                               array('userdata'=> $userdata, 'redirect'=> $redirect,
+                                       'error_upload'=> $error_upload),
                                TRUE);
                        $main_params['side'] = $this->load->view('side_default', NULL, TRUE);
                        $this->load->view('main', $main_params);
@@ -163,6 +188,26 @@ class User extends CI_Controller {
                        $data['ui_lang'] = $this->input->post('ui-lang');
                        $data['time_zone'] = $this->input->post('time-zone');
                        
+                       // Handle picture if one was uploaded.
+                       if ($_FILES['picture']['tmp_name'])
+                       {
+                               $upload_data = $this->upload->data();
+                               $this->load->library('image');
+                               $this->image->load($upload_data['full_path']);
+                               // Resize original to a maximum size.
+                               if ($this->image->get_width() * $this->image->get_height()
+                                               > 640*480)
+                               {
+                                       $this->image->save_thumbnail(
+                                               $upload_data['full_path'],
+                                               640, 480, IMAGETYPE_AUTO);
+                               }
+                               // Create thumbnail.
+                               $data['picture'] = $upload_data['file_name']. '-thumb.jpg';
+                               $this->image->save_thumbnail($upload_data['file_path']
+                                               . $upload_data['file_name']. '-thumb.jpg', 120, 90);
+                       }
+                       
                        // Update session user data.
                        $this->_update_session_userdata($data);
                        
index 755de1b..5f5ab9f 100644 (file)
@@ -13,7 +13,7 @@ class Video extends CI_Controller {
        {
                parent::__construct();
                
-               //$this->lang->load('video');
+               $this->lang->load('video');
        }
        
        public function index()
@@ -36,12 +36,15 @@ class Video extends CI_Controller {
                // **
                // Retrieve video information.
                $this->load->model('videos_model');
-               $this->videos_model->inc_video_var($id, 'views');
+               $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'] . ' &ndash; '
@@ -80,6 +83,27 @@ class Video extends CI_Controller {
                $this->load->view('html_end');
        }
        
+       /**
+        * 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;
+       }
+       
        /**
         * AJAX page which retrieves a video plugin.
         *
index 1c0c555..fcbebc7 100644 (file)
@@ -59,5 +59,8 @@ $lang['ui_license'] = 'license';
 $lang['ui_uploaded_by'] = 'Uploaded by';
 $lang['ui_on_date'] = 'on';
 
+$lang['ui_msg_login_restriction'] = 'You must log in to perform this action.';
+$lang['ui_msg_repeated_action_restriction'] = 'You can only perform this action once a day.';
+
 /* End of file ui_lang.php */
 /* Location: ./application/language/english/ui_lang.php */
\ No newline at end of file
index cfb4939..31285a6 100644 (file)
@@ -17,7 +17,8 @@ $lang['user_birth_date'] = 'Birth Date';
 $lang['user_date_format_hint'] = 'use format YEAR-MONTH-DAY';
 $lang['user_country'] = 'Country';
 $lang['user_locality'] = 'City / Town';
-$lang['user_picture'] = 'Profile Picture';
+$lang['user_picture'] = 'Picture';
+$lang['user_change_picture'] = 'Change Picture';
 $lang['user_ui_lang'] = 'Site Interface Language';
 $lang['user_time_zone'] = 'Time Zone';
 $lang['user_roles'] = 'Roles';
index a0e0623..288cf34 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 
-$lang['video_'] = '';
+$lang['video_like'] = 'Like';
+$lang['video_dislike'] = 'Dislike';
 
 /* End of file video_lang.php */
 /* Location: ./application/language/english/video_lang.php */
\ No newline at end of file
index c4cd5fe..dda9368 100644 (file)
@@ -8,6 +8,8 @@
  * @copyright  2006 Simon Jarvis, 2011 Călin-Andrei Burloiu
  */
 
+define('IMAGETYPE_AUTO', 0);
+
 class Image {
         
        var $image;
@@ -25,6 +27,16 @@ class Image {
                }
        }
        function save($filename, $image_type=IMAGETYPE_JPEG, $compression=60, $permissions=null) {
+               if( $image_type == IMAGETYPE_AUTO) {
+                       if (preg_match('/\.jpg$/', $filename) 
+                                       || preg_match('/\.jpeg$/', $filename))
+                               $image_type = IMAGETYPE_JPEG;
+                       elseif (preg_match('/\.gif$/', $filename))
+                               $image_type = IMAGETYPE_GIF;
+                       elseif (preg_match('/\.png$/', $filename))
+                               $image_type = IMAGETYPE_PNG;
+               }
+               
                if( $image_type == IMAGETYPE_JPEG ) {
                        imagejpeg($this->image,$filename,$compression);
                } elseif( $image_type == IMAGETYPE_GIF ) {
@@ -32,6 +44,7 @@ class Image {
                } elseif( $image_type == IMAGETYPE_PNG ) {
                        imagepng($this->image,$filename);
                }
+               
                if( $permissions != null) {
                        chmod($filename,$permissions);
                }
@@ -45,42 +58,43 @@ class Image {
                        imagepng($this->image);
                }
        }
-       function saveThumbnail($filename, $width, $height)
+       function save_thumbnail($filename, $width, $height,
+                       $image_type=IMAGETYPE_JPEG)
        {
-               $ratio = $this->getWidth() / $this->getHeight();
+               $ratio = $this->get_width() / $this->get_height();
                $thumbRatio = $width / $height;
 
                if($ratio < $thumbRatio)
-               $this->resizeToHeight($height);
+                       $this->resize_to_height($height);
                else
-               $this->resizeToWidth($width);
+                       $this->resize_to_width($width);
 
-               $this->save($filename);
+               $this->save($filename, $image_type);
        }
-       function getWidth() {
+       function get_width() {
                return imagesx($this->image);
        }
-       function getHeight() {
+       function get_height() {
                return imagesy($this->image);
        }
-       function resizeToHeight($height) {
-               $ratio = $height / $this->getHeight();
-               $width = $this->getWidth() * $ratio;
+       function resize_to_height($height) {
+               $ratio = $height / $this->get_height();
+               $width = $this->get_width() * $ratio;
                $this->resize($width,$height);
        }
-       function resizeToWidth($width) {
-               $ratio = $width / $this->getWidth();
-               $height = $this->getheight() * $ratio;
+       function resize_to_width($width) {
+               $ratio = $width / $this->get_width();
+               $height = $this->get_height() * $ratio;
                $this->resize($width,$height);
        }
        function scale($scale) {
-               $width = $this->getWidth() * $scale/100;
-               $height = $this->getheight() * $scale/100;
+               $width = $this->get_width() * $scale/100;
+               $height = $this->get_height() * $scale/100;
                $this->resize($width,$height);
        }
        function resize($width,$height) {
                $new_image = imagecreatetruecolor($width, $height);
-               imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
+               imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->get_width(), $this->get_height());
                $this->image = $new_image;
        }
 }
index 0250038..02aef22 100644 (file)
@@ -243,20 +243,63 @@ class Videos_model extends CI_Model {
        }
        
        /**
-        * Increment a video parameter from DB: `views`, `likes` or `dislikes`.
+        * Increments views count for a video.
         * 
         * @param int $id       DB video id
-        * @param string $param DB parameter column name.
         * @return void
         */
-       public function inc_video_var($id, $var)
+       public function inc_views($id)
        {
-               // TODO error report if query returns FALSE
-               $this->db->query('UPDATE `videos` '
-                                               . 'SET `'. $var. '`=`'. $var. '`+1 '
+               return $this->db->query('UPDATE `videos` '
+                                               . 'SET `views`=`views`+1 '
                                                . 'WHERE id='. $id); 
        }
        
+       public function vote($video_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 = $video_id
+                               AND target_type = 'video'
+                               AND action = '$action'
+                               AND date = CURDATE()");
+               // User already voted today
+               if ($query->num_rows() > 0)
+                       return -1;
+               
+               $this->db->query("UPDATE `videos`
+                       SET $col = $col + 1
+                       WHERE id = $video_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', 'video', $video_id, CURDATE() )");
+               
+               $query = $this->db->query("SELECT $col FROM `videos`
+                       WHERE id = $video_id");
+               
+               if ($query->num_rows() === 1)
+               {
+                       $row = $query->row_array();
+                       return $row[ $col ];
+               }
+               
+               // Error
+               return FALSE;
+       }
+       
        public function get_thumbs($name, $count)
        {
                $thumbs = array();
index 3491ec4..d8db9ac 100644 (file)
@@ -2,8 +2,12 @@
        if (! isset($tab))
                $tab = 0;
 ?>
-<h1><?php echo $this->lang->line('user_appelation')
-       . ' <em>'. $userdata['username']. '</em>' ?></h1>
+
+<h1>
+       <?php echo $this->lang->line('user_appelation')
+       . ' <em>'. $userdata['username']. '</em>' ?>
+</h1>
+
 <div id="profile-tabs">
        <ul>
                <li><a href="#tab-profile">Profile</a></li>
        </ul>
        <div id="tab-profile">
                <table class="form">
+                       <tr>
+                               <td>
+                                 <?php if ($userdata['picture']): ?>
+                                       <img src="<?php echo site_url("data/user_pictures/{$userdata['picture']}") ?>" style="float: left" />
+                                 <?php endif ?>
+                               </td>
+                               <td></td>
+                       </tr>
+                       
                        <tr>
                                <th><?php echo $this->lang->line('user_username'). ': ' ?></th>
                                <td><?php echo $userdata['username'] ?></td>
index 438c410..419353a 100644 (file)
@@ -10,20 +10,16 @@ function _set_value($userdata, $field, $default = '')
                ? $userdata[ str_replace('-','_',$field) ]
                : $post_value);
 }
-?>
-<?php 
-if (!$userdata):
-       echo form_open("user/register/$redirect");
-else:
-       echo form_open("user/account/$redirect");
-endif;
+
+if (!$userdata)
+       echo form_open_multipart("user/register/$redirect");
+else
+       echo form_open_multipart("user/account/$redirect");
 ?>
 
 <?php if ($userdata): ?>
 <input type="hidden" name="user-id" value="<?php echo $userdata['id'] ?>" />
 <input type="hidden" name="username" value="<?php echo $userdata['username'] ?>" />
-<!--<input type="hidden" name="password" value="12345" />
-<input type="hidden" name="password-confirmation" value="12345" />-->
 <?php endif ?>
 
 <table class="form">
@@ -143,13 +139,29 @@ endif;
        </tr>
        <tr><td></td><td><?php echo form_error('locality') ?></td></tr>
        
+  <?php if ($userdata && $userdata['picture']): ?>
        <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'] ?>" />
+               </td>
+       </tr>
+       <tr><td></td><td></td></tr>
+  <?php endif ?>
+       
+       <tr>
+               <th>
+                 <?php if (! $userdata || ($userdata && ! $userdata['picture'])): ?>
+                       <?php echo $this->lang->line('user_picture'). ' : ' ?>
+                 <?php else: ?>
+                       <?php echo $this->lang->line('user_change_picture'). ' : ' ?>
+                 <?php endif ?>
+               </th>
                <td>
                        <input type="file" name="picture" size="16" />
                </td>
        </tr>
-       <tr><td></td><td><?php echo form_error('locality') ?></td></tr>
+       <tr><td></td><td><?php echo $error_upload ?></td></tr>
        
        <tr><td></td><td>&nbsp;</td></tr>
        
index 7486a78..a775419 100644 (file)
                        ?>
                </div>
                
-               <div><span id="video-likes">
-                       <?php echo $video['likes'] . ' '
-                               . ($video['likes'] == 1 ? 
-                                       $this->lang->line('ui_like') : 
-                                       $this->lang->line('ui_likes') );
-                       ?></span>,
-               <span id="video-dislikes">
-                       <?php echo $video['dislikes'] . ' '
-                               . ($video['dislikes'] == 1 ? 
-                                       $this->lang->line('ui_dislike') : 
-                                       $this->lang->line('ui_dislikes') );
-                       ?>
-               </span></div>
+               <div><!--<a id="link-like" href="#"><?php echo $this->lang->line('video_like') ?></a>
+                       <a id="link-dislike" href="#"><?php echo $this->lang->line('video_dislike') ?></a>-->
+                       <a class="link-vote" data-action="like" href="#"><?php echo $this->lang->line('video_like') ?></a>
+                       <a class="link-vote" data-action="dislike" href="#"><?php echo $this->lang->line('video_dislike') ?></a>
+                       <span id="video-likes"><?php echo $video['likes'] ?></span> <?php
+                               echo $this->lang->line('ui_likes') ?>,
+                       <span id="video-dislikes"><?php echo $video['dislikes'] ?></span> <?php
+                               echo $this->lang->line('ui_dislikes'); ?>
+               </div>
        </div>
        
        <div id="video-description"><?php echo $video['description'] ?></div>
                                                .css('width', $('#video-widget').css('width'));
                                }
                        });
+               
+               $('.link-vote')
+                       .click(function() {
+                               var user_id = "<?php echo $user_id ?>";
+                               var action, idOutput;
+                               if ($(this).data('action') == 'like')
+                               {
+                                       var action = 'like';
+                                       var idOutput = '#video-likes';
+                               }
+                               else
+                               {
+                                       var action = 'dislike';
+                                       var idOutput = '#video-dislikes';
+                               }
+                               //alert(action + " " + user_id);
+                               
+                               if (user_id.length != 0)
+                               {
+                                       $.ajax({
+                                               type: "GET",
+                                               url: "<?php echo site_url("video/ajax_vote") ?>/"
+                                                       + action
+                                                       + "<?php echo "/{$video['id']}/$user_id" ?>",
+                                               data: {t: ""+Math.random()},
+                                               dataType: "text",
+                                               success: function(text) {
+                                                       if (text)
+                                                               $(idOutput).html(text);
+                                                       else
+                                                               alert('<?php echo $this->lang->line('ui_msg_repeated_action_restriction') ?>');
+                                               }
+                                       });
+                               }
+                               else
+                                       alert('<?php echo $this->lang->line('ui_msg_login_restriction') ?>');
+                       })
+                       .button();
+               
+               $('#link-like')
+                       .click(function() {
+                               user_id = "<?php echo $user_id ?>";
+                               
+                               if (user_id)
+                               {
+                                       $.ajax({
+                                               type: "GET",
+                                               url: "<?php echo site_url("video/ajax_vote/like/{$video['id']}/$user_id") ?>",
+                                               dataType: "text",
+                                               success: function(text) {
+                                                       if (text)
+                                                               $('#video-likes').html(text);
+                                                       else
+                                                               alert('<?php echo $this->lang->line('ui_msg_repeated_action_restriction') ?>');
+                                               }
+                                       });
+                               }
+                               else
+                                       alert('<?php echo $this->lang->line('ui_msg_login_restriction') ?>');
+                       })
+                       .button();
+               $('#link-dislike')
+                       .click(function() {
+                               user_id = "<?php echo $user_id ?>";
+                               
+                               if (user_id)
+                               {
+                                       $.ajax({
+                                               type: "GET",
+                                               url: "<?php echo site_url("video/ajax_vote/dislike/{$video['id']}/$user_id") ?>",
+                                               data: {t: ""+Math.random()},
+                                               dataType: "text",
+                                               success: function(text) {
+                                                       $('#video-dislikes').html(text);
+                                               }
+                                       });
+                               }
+                               else
+                                       alert('<?php echo $this->lang->line('ui_msg_login_restriction') ?>');
+                       })
+                       .button();
        });
 </script>
\ No newline at end of file
index 223ef1c..ff8a424 100644 (file)
@@ -205,7 +205,7 @@ label.strong
        text-align: left;
        padding: 0px 0px 0px 8px;
        margin: 8px 0px 8px 0px;
-       background: url(/img/header.png) top repeat-x;
+       background: url(/img/header.png) center repeat-x;
        outline: 1px outset rgb(108,162,222);
        clear: both;
 }
index daeb9b2..fe76565 100644 (file)
@@ -21,6 +21,7 @@ dd
 #video-upload-info, #video-popularity
 {
        font-size: 0.8em;
+       line-height: 150%;
 }
 #video-description
 {
diff --git a/data/index.html b/data/index.html
new file mode 100644 (file)
index 0000000..c942a79
--- /dev/null
@@ -0,0 +1,10 @@
+<html>
+<head>
+       <title>403 Forbidden</title>
+</head>
+<body>
+
+<p>Directory access is forbidden.</p>
+
+</body>
+</html>
\ No newline at end of file
diff --git a/data/user_pictures/index.html b/data/user_pictures/index.html
new file mode 100644 (file)
index 0000000..c942a79
--- /dev/null
@@ -0,0 +1,10 @@
+<html>
+<head>
+       <title>403 Forbidden</title>
+</head>
+<body>
+
+<p>Directory access is forbidden.</p>
+
+</body>
+</html>
\ No newline at end of file