From 6db375a0706b5328fbd53be4dd2e517c96560ea6 Mon Sep 17 00:00:00 2001 From: Calin Burloiu Date: Tue, 27 Sep 2011 18:30:45 +0300 Subject: [PATCH] users can add pictures to their profiles; users can like and dislike videos --- application/controllers/user.php | 51 +++++++++- application/controllers/video.php | 28 +++++- application/language/english/ui_lang.php | 3 + application/language/english/user_lang.php | 3 +- application/language/english/video_lang.php | 3 +- application/libraries/Image.php | 46 ++++++--- application/models/videos_model.php | 55 +++++++++-- application/views/user/profile_view.php | 17 +++- application/views/user/register_view.php | 32 ++++-- application/views/video/watch_view.php | 103 +++++++++++++++++--- css/default.css | 2 +- css/video.css | 1 + data/index.html | 10 ++ data/user_pictures/index.html | 10 ++ 14 files changed, 309 insertions(+), 55 deletions(-) create mode 100644 data/index.html create mode 100644 data/user_pictures/index.html diff --git a/application/controllers/user.php b/application/controllers/user.php index 96454be..169fa46 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -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('', ''); + $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('', + ''); + } + } + 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); diff --git a/application/controllers/video.php b/application/controllers/video.php index 755de1b..5f5ab9f 100644 --- a/application/controllers/video.php +++ b/application/controllers/video.php @@ -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'] . ' – ' @@ -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. * diff --git a/application/language/english/ui_lang.php b/application/language/english/ui_lang.php index 1c0c555..fcbebc7 100644 --- a/application/language/english/ui_lang.php +++ b/application/language/english/ui_lang.php @@ -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 diff --git a/application/language/english/user_lang.php b/application/language/english/user_lang.php index cfb4939..31285a6 100644 --- a/application/language/english/user_lang.php +++ b/application/language/english/user_lang.php @@ -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'; diff --git a/application/language/english/video_lang.php b/application/language/english/video_lang.php index a0e0623..288cf34 100644 --- a/application/language/english/video_lang.php +++ b/application/language/english/video_lang.php @@ -1,6 +1,7 @@ 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; } } diff --git a/application/models/videos_model.php b/application/models/videos_model.php index 0250038..02aef22 100644 --- a/application/models/videos_model.php +++ b/application/models/videos_model.php @@ -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(); diff --git a/application/views/user/profile_view.php b/application/views/user/profile_view.php index 3491ec4..d8db9ac 100644 --- a/application/views/user/profile_view.php +++ b/application/views/user/profile_view.php @@ -2,8 +2,12 @@ if (! isset($tab)) $tab = 0; ?> -

lang->line('user_appelation') - . ' '. $userdata['username']. '' ?>

+ +

+ lang->line('user_appelation') + . ' '. $userdata['username']. '' ?> +

+
+ + + + + diff --git a/application/views/user/register_view.php b/application/views/user/register_view.php index 438c410..419353a 100644 --- a/application/views/user/register_view.php +++ b/application/views/user/register_view.php @@ -10,20 +10,16 @@ function _set_value($userdata, $field, $default = '') ? $userdata[ str_replace('-','_',$field) ] : $post_value); } -?> - -
+ + " style="float: left" /> + +
lang->line('user_username'). ': ' ?>
@@ -143,13 +139,29 @@ endif; + + + + + + + + - + diff --git a/application/views/video/watch_view.php b/application/views/video/watch_view.php index 7486a78..a775419 100644 --- a/application/views/video/watch_view.php +++ b/application/views/video/watch_view.php @@ -51,19 +51,15 @@ ?> -
- lang->line('ui_like') : - $this->lang->line('ui_likes') ); - ?>, - - lang->line('ui_dislike') : - $this->lang->line('ui_dislikes') ); - ?> -
+
+ lang->line('video_like') ?> + lang->line('video_dislike') ?> + lang->line('ui_likes') ?>, + lang->line('ui_dislikes'); ?> +
@@ -136,5 +132,86 @@ .css('width', $('#video-widget').css('width')); } }); + + $('.link-vote') + .click(function() { + var 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: "/" + + action + + "", + data: {t: ""+Math.random()}, + dataType: "text", + success: function(text) { + if (text) + $(idOutput).html(text); + else + alert('lang->line('ui_msg_repeated_action_restriction') ?>'); + } + }); + } + else + alert('lang->line('ui_msg_login_restriction') ?>'); + }) + .button(); + + $('#link-like') + .click(function() { + user_id = ""; + + if (user_id) + { + $.ajax({ + type: "GET", + url: "", + dataType: "text", + success: function(text) { + if (text) + $('#video-likes').html(text); + else + alert('lang->line('ui_msg_repeated_action_restriction') ?>'); + } + }); + } + else + alert('lang->line('ui_msg_login_restriction') ?>'); + }) + .button(); + $('#link-dislike') + .click(function() { + user_id = ""; + + if (user_id) + { + $.ajax({ + type: "GET", + url: "", + data: {t: ""+Math.random()}, + dataType: "text", + success: function(text) { + $('#video-dislikes').html(text); + } + }); + } + else + alert('lang->line('ui_msg_login_restriction') ?>'); + }) + .button(); }); \ No newline at end of file diff --git a/css/default.css b/css/default.css index 223ef1c..ff8a424 100644 --- a/css/default.css +++ b/css/default.css @@ -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; } diff --git a/css/video.css b/css/video.css index daeb9b2..fe76565 100644 --- a/css/video.css +++ b/css/video.css @@ -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 index 0000000..c942a79 --- /dev/null +++ b/data/index.html @@ -0,0 +1,10 @@ + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + + \ No newline at end of file diff --git a/data/user_pictures/index.html b/data/user_pictures/index.html new file mode 100644 index 0000000..c942a79 --- /dev/null +++ b/data/user_pictures/index.html @@ -0,0 +1,10 @@ + + + 403 Forbidden + + + +

Directory access is forbidden.

+ + + \ No newline at end of file -- 2.20.1
lang->line('user_picture'). ' : ' ?> + " alt="" /> +
+ + lang->line('user_picture'). ' : ' ?> + + lang->line('user_change_picture'). ' : ' ?> + +