[vhffs-dev] [1353] User search is back.

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 1353
Author:   beuss
Date:     2009-03-09 13:04:30 +0100 (Mon, 09 Mar 2009)

Log Message:
-----------
User search is back.
Removed useless webarea search.

Modified Paths:
--------------
    branches/vhffs-design/vhffs-api/src/Vhffs/Panel/User.pm
    branches/vhffs-design/vhffs-panel/js/public.js
    branches/vhffs-design/vhffs-panel/js/vhffs/Common.js
    branches/vhffs-design/vhffs-public/Makefile.am
    branches/vhffs-design/vhffs-public/templates/Makefile.am
    branches/vhffs-design/vhffs-public/templates/parts/left-menu.tt
    branches/vhffs-design/vhffs-public/usersearch.pl

Added Paths:
-----------
    branches/vhffs-design/vhffs-public/templates/content/usersearch-form.tt
    branches/vhffs-design/vhffs-public/templates/content/usersearch-results.tt
    branches/vhffs-design/vhffs-public/templates/parts/user-general.tt
    branches/vhffs-design/vhffs-public/usersearch_form.pl


Modified: branches/vhffs-design/vhffs-api/src/Vhffs/Panel/User.pm
===================================================================
--- branches/vhffs-design/vhffs-api/src/Vhffs/Panel/User.pm	2009-03-03 00:37:39 UTC (rev 1352)
+++ branches/vhffs-design/vhffs-api/src/Vhffs/Panel/User.pm	2009-03-09 12:04:30 UTC (rev 1353)
@@ -120,34 +120,72 @@
     return $users;
 }
 
+=head2 public_search
+
+    $users = Vhffs::Panel::User::public_search($vhffs, $username, $start);
+
+Returns all users whose username contains C<$username>.
+
+=cut
+
 sub public_search {
-    my ($main, $username, $firstname, $lastname, $start, $count) = @_;
+    my ($main, $username, $start) = @_;
     my $result = {};
 
-    my $select_clause = 'SELECT u.uid, u.username, u.firstname, u.lastname';
-    my $restriction = ' FROM vhffs_users u INNER JOIN vhffs_object o ON o.object_id = u.object_id WHERE o.state = ?';
+    my $select_clause = 'SELECT u.uid, u.username, u.firstname, u.lastname ';
+    my $sql = 
+    	'FROM vhffs_users u '.
+    	'INNER JOIN vhffs_object o ON o.object_id = u.object_id '.
+    	'WHERE o.state = ? ';
     my @params;
     push @params, Vhffs::Constants::ACTIVATED;
 
     if(defined $username) {
         # usernames are enforced in lowercase
-        $restriction .= ' AND username LIKE ?';
+        $sql .= ' AND username LIKE ?';
         push @params, '%'.lc($username).'%';
     }
 
-    if(defined $firstname) {
-        $restriction .= ' AND UPPER(firstname) LIKE UPPER(?)';
-        push @params, '%'.$firstname.'%';
-    }
-
-    if(defined $lastname) {
-        $restriction .= ' AND UPPER(lastname) LIKE UPPER(?)';
-        push @params, '%'.$lastname.'%';
-    }
-
-    my $order = ' ORDER BY u.username';
-
-    return Vhffs::Panel::Commons::fetch_slice_and_count($main, $select_clause, $restriction, $order, $start, $count, \@params, \&fetch_users_and_groups);
+    my $limit = ' LIMIT 10';
+    $limit .= ' OFFSET '.($start * 10) if(defined $start); 
+    
+    my $dbh = $main->get_db_object();
+    
+    my $users = $dbh->selectall_hashref($select_clause.$sql.' ORDER BY u.username '.$limit, 'uid', undef, @params);
+	
+	my ($count) = $dbh->selectrow_array('SELECT COUNT(*) '.$sql, undef, @params);
+	
+	my @uids = ();
+	
+	foreach my $uid(keys(%$users)) {
+		push @uids, $uid;
+	}
+	
+	# OK, now fetch all groups in one shot
+	# we can't do this in the first query since
+	# we've a limit clause
+	$sql = 'SELECT g.groupname, u.uid FROM vhffs_groups g '.
+		'INNER JOIN vhffs_user_group ug ON ug.gid = g.gid '.
+		'INNER JOIN vhffs_users u ON u.uid = ug.uid '.
+		'WHERE g.groupname != u.username AND u.uid IN ( '.join(', ', @uids).') '.
+		'ORDER BY g.groupname';
+	
+	my $groups = $dbh->selectall_arrayref($sql, { Slice => {}});
+	my $i = 0;
+	
+	foreach my $g(@$groups) {
+		if(!exists $users->{$g->{uid}}{groups}) {
+			$users->{$g->{uid}}{groups} = [];
+		}
+		push(@{$users->{$g->{uid}}{groups}}, $g->{groupname});
+	}
+	
+	my @val = values(%$users);
+	
+	# We've to sort manualy since we use a hash
+	@val = sort { $a->{username} cmp $b->{username}} @val;
+	
+	return (\@val, $count);
 }
 
 sub fetch_users_and_groups {

Modified: branches/vhffs-design/vhffs-panel/js/public.js
===================================================================
--- branches/vhffs-design/vhffs-panel/js/public.js	2009-03-03 00:37:39 UTC (rev 1352)
+++ branches/vhffs-design/vhffs-panel/js/public.js	2009-03-09 12:04:30 UTC (rev 1353)
@@ -37,8 +37,6 @@
 dojo.require('vhffs.Menu');
 dojo.require('vhffs.Common');
 
-
-
 dojo.addOnLoad(function() {
 	new vhffs.Menu(dojo.byId('left-menu'));
 	vhffs.Common.ajaxizeLinks(dojo.byId('public-content'));
@@ -176,3 +174,23 @@
 	dojo.style(tagContainer, 'display', 'none');
 }
 
+Public.SearchUser = {};
+
+Public.SearchUser.onLoad = function() {
+	Public.SearchUser.ajaxizeForm();
+}
+
+Public.SearchUser.ajaxizeForm = function() {
+	var form = dojo.byId('SearchUserForm');
+	dojo.connect(form, 'onsubmit', function(e) {
+		dojo.stopEvent(e);
+		dojo.xhrPost({
+			url: dojo.attr(form, 'action'),
+			'form': form,
+			load: function(response) {
+				vhffs.Common.loadContent(dojo.byId('public-content'), response);
+				vhffs.Common.ajaxizeLinks(dojo.byId('public-content'));
+			}
+		});
+	});
+}

Modified: branches/vhffs-design/vhffs-panel/js/vhffs/Common.js
===================================================================
--- branches/vhffs-design/vhffs-panel/js/vhffs/Common.js	2009-03-03 00:37:39 UTC (rev 1352)
+++ branches/vhffs-design/vhffs-panel/js/vhffs/Common.js	2009-03-09 12:04:30 UTC (rev 1353)
@@ -49,6 +49,8 @@
 					});
 				}
 			});
+			// Avoid double ajaxization
+			dojo.removeClass(link, 'ajax');
 		});
 	},
 	

Modified: branches/vhffs-design/vhffs-public/Makefile.am
===================================================================
--- branches/vhffs-design/vhffs-public/Makefile.am	2009-03-03 00:37:39 UTC (rev 1352)
+++ branches/vhffs-design/vhffs-public/Makefile.am	2009-03-09 12:04:30 UTC (rev 1353)
@@ -15,6 +15,7 @@
 	tagsearch.pl \
 	user.pl \
 	usersearch.pl \
+	usersearch_form.pl \
 	websitesearch.pl \
 	extern/newgroupsrss.pl \
 	extern/newusersrss.pl \

Modified: branches/vhffs-design/vhffs-public/templates/Makefile.am
===================================================================
--- branches/vhffs-design/vhffs-public/templates/Makefile.am	2009-03-03 00:37:39 UTC (rev 1352)
+++ branches/vhffs-design/vhffs-public/templates/Makefile.am	2009-03-09 12:04:30 UTC (rev 1353)
@@ -8,6 +8,8 @@
 	content/group-details.tt \
 	content/groupsearch-form.tt \
 	content/groupsearch-results.tt \
+	content/usersearch-form.tt \
+	content/usersearch-results.tt \
 	group_part.tmpl \
 	group.tmpl \
 	groupslist.tmpl \
@@ -27,6 +29,7 @@
 	parts/left-menu.tt \
 	parts/tags-cloud.tt \
 	parts/top-menu.tt \
+	parts/user-general.tt \
 	simplemsg.tmpl \
 	user_part.tmpl \
 	user.tmpl \

Added: branches/vhffs-design/vhffs-public/templates/content/usersearch-form.tt
===================================================================
--- branches/vhffs-design/vhffs-public/templates/content/usersearch-form.tt	                        (rev 0)
+++ branches/vhffs-design/vhffs-public/templates/content/usersearch-form.tt	2009-03-09 12:04:30 UTC (rev 1353)
@@ -0,0 +1,12 @@
+<h1>[% 'User search' | i18n%]</h1>
+<form action="usersearch.pl" method="post" id="SearchUserForm">
+<p><label for="username">[% 'Username' | i18n %]:</label>
+<input type="text" maxlength="50" id="username" name="username"/></p>
+<p class="submit"><input type="submit" value="[% 'OK' | i18n %]" name="searchSubmit" id="SearchUserFormSubmit"/></p>
+</form>
+<script type="text/javascript">
+<!--
+dojo.addOnLoad(Public.SearchUser.onLoad);
+//-->
+</script>
+

Added: branches/vhffs-design/vhffs-public/templates/content/usersearch-results.tt
===================================================================
--- branches/vhffs-design/vhffs-public/templates/content/usersearch-results.tt	                        (rev 0)
+++ branches/vhffs-design/vhffs-public/templates/content/usersearch-results.tt	2009-03-09 12:04:30 UTC (rev 1353)
@@ -0,0 +1,8 @@
+<h1>[% 'Search results' | i18n %]</h1>
+[% INCLUDE common/pager.tt pager=u_pager %]
+
+[% FOREACH u = users %]
+[% INCLUDE 'parts/user-general.tt' %]
+[% END %]
+
+[% INCLUDE common/pager.tt  pager=u_pager%]
\ No newline at end of file

Modified: branches/vhffs-design/vhffs-public/templates/parts/left-menu.tt
===================================================================
--- branches/vhffs-design/vhffs-public/templates/parts/left-menu.tt	2009-03-03 00:37:39 UTC (rev 1352)
+++ branches/vhffs-design/vhffs-public/templates/parts/left-menu.tt	2009-03-09 12:04:30 UTC (rev 1353)
@@ -4,11 +4,10 @@
 	<div class="menu">
 		<a href="/groupsearch_form.pl" class="ajax">[% 'Search' | i18n %]</a>
 		<a href="/" class="ajax">[% 'Last projects' | i18n %]</a>
-		<a href="/allgroups.pl">[% 'All' | i18n %]</a>
+		<a href="/allgroups.pl" class="ajax">[% 'All' | i18n %]</a>
 	</div>
-	<h2>[% 'Webareas' | i18n %]</h2>
+	<h2>[% 'Users' | i18n %]</h2>
 	<div class="menu">
-		<a href="#">[% 'Search' | i18n %]</a>
-		<a href="#">[% 'Last webareas' | i18n %]</a>
-		<a href="#">[% 'All' | i18n %]</a>
+		<a href="/usersearch_form.pl" class="ajax">[% 'Search' | i18n %]</a>
+		<a href="/lastusers.pl">[% 'Last users' | i18n %]</a>
 	</div>

Added: branches/vhffs-design/vhffs-public/templates/parts/user-general.tt
===================================================================
--- branches/vhffs-design/vhffs-public/templates/parts/user-general.tt	                        (rev 0)
+++ branches/vhffs-design/vhffs-public/templates/parts/user-general.tt	2009-03-09 12:04:30 UTC (rev 1353)
@@ -0,0 +1,13 @@
+<h2 class="username">[% u.username | html%]</h2>
+<div class="user-info">
+<img class="user-avatar" src="/avatar.pl?oid=[% u.object_id %]"/>
+<ul>
+<li>[% 'First name' | i18n %]: [% u.firstname | html %]</li>
+<li>[% 'Last name' | i18n %]: [% u.lastname | html %]</li>
+<li>[% 'Groups' | i18n %]: 
+[% FOREACH ug = u.groups %]
+<a href="/group.pl?name=[% ug | html %]" class="ajax">[% ug | html %]</a>[% ', ' UNLESS loop.last() %]
+[% END %]
+</li>
+</ul>
+</div>
\ No newline at end of file

Modified: branches/vhffs-design/vhffs-public/usersearch.pl
===================================================================
--- branches/vhffs-design/vhffs-public/usersearch.pl	2009-03-03 00:37:39 UTC (rev 1352)
+++ branches/vhffs-design/vhffs-public/usersearch.pl	2009-03-09 12:04:30 UTC (rev 1353)
@@ -35,52 +35,55 @@
 use POSIX qw(locale_h);
 use locale;
 use Locale::gettext;
-use CGI;
-use Encode;
 
 use lib '%VHFFS_LIB_DIR%';
-use Vhffs::Panel::Commons;
-use Vhffs::Panel::Main;
-use Vhffs::Panel::Template;
 
-my $panel = new Vhffs::Panel::Main();
-exit 0 unless $panel;
+use Vhffs::Panel::Public;
+use Vhffs::Panel::User;
 
-my $vhffs = $panel->{'vhffs'};
-my $templatedir = $panel->{'templatedir'};
-my $cgi = $panel->{'cgi'};
+my $panel = new Vhffs::Panel::Public();
+my $cgi = $panel->{cgi};
 
-$panel->check_public();
+my $username = Encode::decode_utf8($cgi->param('username'));
+my $page = defined($cgi->param('page')) ? int($cgi->param('page')) : 1;
 
-my $template = new Vhffs::Panel::Template( filename => $templatedir.'/public/userslist.tmpl', die_on_bad_params => 0 );
-my $username = $cgi->param('username') || '';
-my $firstname = Encode::decode_utf8( $cgi->param('firstname') ) || '';
-my $lastname = Encode::decode_utf8( $cgi->param('lastname') ) || '';
-my $page = $cgi->param('page');
-my $per_page_count = 5;
-$page = 1 unless(defined $page && int($page) > 0);
+if($username =~ /^\s*$/) {
+	$panel->render('common/error.tt', {
+		message => gettext('You have to enter an username')
+	});
+	exit(0);
+}
 
-my $result = Vhffs::Panel::User::public_search(
-    $vhffs,
-    ( $username =~ /^\s*$/ ? undef : $username ),
-    ( $firstname =~ /^\s*$/ ? undef : $firstname ),
-    ( $lastname =~ /^\s*$/ ? undef : $lastname ),
-    ($page -1) * $per_page_count,
-    $per_page_count
-);
+my ($users, $total) = Vhffs::Panel::User::public_search($panel->{vhffs}, $username, ($page - 1));
 
-$template->param( URL_PANEL => $vhffs->get_config->get_panel->{'url'} );
-
-if($result->{total_count} == 0) {
-    $template->param( TEXT_TITLE => gettext('No user found') );
-} else {
-    Vhffs::Panel::Commons::paginate($template, $page, $result->{total_count}, $per_page_count,
-        { username => $username, firstname => $firstname, lastname => $lastname});
-    $template->param( USE_AVATAR => $panel->use_users_avatars );
-    $template->param( TEXT_TITLE => sprintf( gettext('%d user(s) found'), $result->{total_count} ) );
-    $template->param( USERS => $result->{data} );
+if($total == 0) {
+	$panel->render('common/error.tt', {
+		message => gettext('No user found')
+	});
+	exit(0);
 }
 
-$panel->light( $template );
-$panel->display;
+# Let's format users (what's the best ?
+# this or have multiple requests ?
 
+#my $users = [];
+#my $user;
+#
+#foreach my $u(@$tmp) {
+#	if(!defined($user) || $user->{username} ne $u->{username}) {
+#		push(@$users, $user) if(defined($user));
+#		$user = $u;
+#		
+#		$user->{groups} = ();
+#	}
+#	push(@{$user->{groups}}, $u->{groupname});
+#}
+#
+#push(@$users, $user) if(defined $user);
+
+my $pager = Vhffs::Panel::Commons::get_pager($page, $total, 10, 5, $panel->{url}, { username => $username });
+
+$panel->render('content/usersearch-results.tt', {
+	users => $users,
+	u_pager => $pager
+});

Added: branches/vhffs-design/vhffs-public/usersearch_form.pl
===================================================================
--- branches/vhffs-design/vhffs-public/usersearch_form.pl	                        (rev 0)
+++ branches/vhffs-design/vhffs-public/usersearch_form.pl	2009-03-09 12:04:30 UTC (rev 1353)
@@ -0,0 +1,45 @@
+#!%PERL%
+# Copyright (c) vhffs project and its contributors
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without 
+# modification, are permitted provided that the following conditions 
+# are met:
+#
+# 1. Redistributions of source code must retain the above copyright 
+#   notice, this list of conditions and the following disclaimer.
+#2. Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in 
+#   the documentation and/or other materials provided with the 
+#   distribution.
+#3. Neither the name of vhffs nor the names of its contributors 
+#   may be used to endorse or promote products derived from this 
+#   software without specific prior written permission.
+#
+#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
+#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
+#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
+#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
+#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
+#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
+#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
+#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
+#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
+# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
+# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
+# POSSIBILITY OF SUCH DAMAGE.
+
+use strict;
+use utf8;
+
+use POSIX qw(locale_h);
+use locale;
+use Locale::gettext;
+
+use lib '%VHFFS_LIB_DIR%';
+
+use Vhffs::Panel::Public;
+
+my $panel = new Vhffs::Panel::Public();
+
+$panel->render('content/usersearch-form.tt');


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/