[vhffs-dev] [1089] Started to factorize some code. |
[ Thread Index |
Date Index
| More vhffs.org/vhffs-dev Archives
]
Revision: 1089
Author: beuss
Date: 2007-11-18 11:20:40 +0000 (Sun, 18 Nov 2007)
Log Message:
-----------
Started to factorize some code.
Added missing file for group search (sorry).
Modified Paths:
--------------
trunk/vhffs-api/src/Vhffs/Panel/Commons.pm
trunk/vhffs-api/src/Vhffs/Panel/Group.pm
trunk/vhffs-api/src/Vhffs/Panel/Web.pm
trunk/vhffs-public/allgroups.pl
trunk/vhffs-public/allwebsites.pl
Added Paths:
-----------
trunk/vhffs-public/groupsearch.pl
Modified: trunk/vhffs-api/src/Vhffs/Panel/Commons.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/Commons.pm 2007-11-17 23:25:11 UTC (rev 1088)
+++ trunk/vhffs-api/src/Vhffs/Panel/Commons.pm 2007-11-18 11:20:40 UTC (rev 1089)
@@ -90,6 +90,15 @@
$panel->display;
}
+=head2 paginate
+
+ Vhffs::Panel::Commons::paginate($template, $current_page, $total_count, $items_per_page, $url_params);
+
+C<$url_param> is a reference to a hash containing parameters to append to the URL and their values. Empty
+parameters won't be appended.
+
+=cut
+
sub paginate($$$$$) {
my ($template, $current_page, $total_count, $items_per_page, $url_params) = @_;
die('Attempted to paginate without pagination template') unless($template->query(name => 'PAGINATION'));
@@ -124,4 +133,50 @@
}
}
+=head2
+
+ fetch_slice_and_count($main, $select, $conditions, $order, $start, $count, $params, $select_callback);
+
+Helper function for pagination. Fetches a slice of result (using C<"LIMIT"> and C<"OFFSET"> clauses)
+and the total count of items (if there was no slice).
+
+=over 4
+
+=item C<$main>: Vhffs::Main instance;
+
+=item C<$select>: C<"SELECT"> clause of the query, will be replaced with C<"SELECT COUNT(*)"> to
+ get the total count;
+
+=item C<$conditions>: conditions applied to the SELECT clausse. Includes all the query
+ begining at C<"FROM"> and ending right befor C<"ORDER BY">;
+
+=item C<$order>: C<"ORDER BY"> clause of the query;
+
+=item C<$start>: first item of the result set to fetch;
+
+=item C<$count>: number of items to fetch;
+
+=item C<$params>: array ref containing parameters of the query when using placeholders;
+
+=item C<$select_callback>: optional sub reference which will be used to fetch results instead
+ of C<DBI::selectall_arrayref>. Its protorype must be ($main, $sql, @params).
+=back
+
+=cut
+
+sub fetch_slice_and_count($$$$$$$;$) {
+ my ($main, $select, $conditions, $order, $start, $count, $params, $select_callback) = @_;
+
+ my $dbh = $main->get_db_object;
+ my $result = {};
+ my $sql = $select.$conditions.$order." LIMIT $count OFFSET $start";
+ if(defined $select_callback) {
+ $result->{data} = $select_callback->($main, $sql, @{$params});
+ } else {
+ $result->{data} = $dbh->selectall_arrayref($sql, { Slice => {} }, @{$params});
+ }
+ ($result->{total_count}) = @{$dbh->selectrow_arrayref('SELECT COUNT(*) '.$conditions, undef, @{$params})};
+ return $result;
+}
+
1;
Modified: trunk/vhffs-api/src/Vhffs/Panel/Group.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/Group.pm 2007-11-17 23:25:11 UTC (rev 1088)
+++ trunk/vhffs-api/src/Vhffs/Panel/Group.pm 2007-11-18 11:20:40 UTC (rev 1089)
@@ -119,6 +119,7 @@
my $select_clause = 'SELECT g.gid, g.groupname, g.realname, o.description';
my $restriction = ' FROM vhffs_groups g LEFT OUTER JOIN vhffs_users u ON u.username=g.groupname INNER JOIN vhffs_object o ON o.object_id=g.object_id WHERE o.state = ? AND u.username IS NULL';
my @params;
+ push @params, Vhffs::Constants::ACTIVATED;
if(defined $groupname) {
# groupnames are enforced in lowercase
@@ -131,16 +132,8 @@
push @params, '%'.$description.'%';
}
- my $order = " ORDER BY g.groupname LIMIT $count OFFSET $start";
-
- my $dbh = $main->get_db_object;
- my $sth = $dbh->prepare($select_clause.$restriction.$order);
- my @users;
- $sth->execute( Vhffs::Constants::ACTIVATED, @params );
- $result->{groups} = fetch_groups_and_users($main, $select_clause.$restriction.$order, Vhffs::Constants::ACTIVATED, @params);
- $select_clause = 'SELECT COUNT(*)';
- ($result->{total_count}) = @{$dbh->selectrow_arrayref($select_clause.$restriction, undef, Vhffs::Constants::ACTIVATED, @params)};
- return $result;
+ return Vhffs::Panel::Commons::fetch_slice_and_count($main, $select_clause, $restriction,
+ ' ORDER BY g.groupname', $start, $count, \@params, \&Vhffs::Panel::Group::fetch_groups_and_users);
}
sub getall_groups_per_user
@@ -207,18 +200,14 @@
}
my $order_clause = ' ORDER BY g.groupname LIMIT '.$count.' OFFSET '.$starting;
-
- my $result = {};
- $result->{groups} = fetch_groups_and_users($main, $select_clause.$sql.$order_clause, @params);
- my $dbh = $main->get_db_object;
- ($result->{total_count}) = @{$dbh->selectrow_arrayref('SELECT COUNT(*)'.$sql, undef, @params)};
- return $result;
+ return Vhffs::Panel::Commons::fetch_slice_and_count($main, $select_clause, $sql, ' ORDER BY groupname', $starting, $count, \@params, \&Vhffs::Panel::Group::fetch_groups_and_users);
}
sub fetch_groups_and_users {
my ($main, $sql, @params) = @_;
my @groups;
+
my $dbh = $main->get_db_object;
my $sth = $dbh->prepare($sql);
$sql = 'SELECT u.username FROM vhffs_users u INNER JOIN vhffs_user_group ug ON ug.uid = u.uid WHERE ug.gid = ?';
Modified: trunk/vhffs-api/src/Vhffs/Panel/Web.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/Web.pm 2007-11-17 23:25:11 UTC (rev 1088)
+++ trunk/vhffs-api/src/Vhffs/Panel/Web.pm 2007-11-18 11:20:40 UTC (rev 1089)
@@ -152,10 +152,7 @@
push @params, $letter;
}
my $dbh = $vhffs->get_db_object;
- my $result = {};
- $result->{websites} = $dbh->selectall_arrayref($select.$from." ORDER BY w.servername LIMIT $count OFFSET $start", { Slice => {} }, @params);
- ($result->{total_count}) = @{$dbh->selectrow_arrayref('SELECT COUNT(*)'.$from, undef, @params)};
- return $result;
+ return Vhffs::Panel::Commons::fetch_slice_and_count($vhffs, $select, $from, ' ORDER BY w.servername', $start, $count, \@params);
}
=head2 get_used_letters
Modified: trunk/vhffs-public/allgroups.pl
===================================================================
--- trunk/vhffs-public/allgroups.pl 2007-11-17 23:25:11 UTC (rev 1088)
+++ trunk/vhffs-public/allgroups.pl 2007-11-18 11:20:40 UTC (rev 1089)
@@ -72,7 +72,7 @@
$template->param( TEXT_TITLE => sprintf( gettext("All groups on %s") , $hostname ) );
$template->param( LETTERS => $used_letters );
$template->param( ALL => gettext('All') );
-$template->param( GROUPS => $result->{groups} );
+$template->param( GROUPS => $result->{data} );
$panel->light( $template );
$panel->display;
Modified: trunk/vhffs-public/allwebsites.pl
===================================================================
--- trunk/vhffs-public/allwebsites.pl 2007-11-17 23:25:11 UTC (rev 1088)
+++ trunk/vhffs-public/allwebsites.pl 2007-11-18 11:20:40 UTC (rev 1089)
@@ -61,17 +61,19 @@
my $per_page_count = 20;
my $res = Vhffs::Panel::Web::get_websites_starting_with( $vhffs, $letter, ($page - 1) * $per_page_count, $per_page_count);
+
my $hostname = $vhffs->get_config->get_host_name;
my $template = new Vhffs::Panel::Template( filename => $templatedir."/public/websiteslist.tmpl" );
Vhffs::Panel::Commons::paginate($template, $page, $res->{total_count}, $per_page_count, {'letter' => (defined($letter) ? $letter : 'all') });
+
$template->param( TEXT_TITLE => sprintf( gettext("All websites on %s") , $hostname ) );
$template->param( LETTERS => $letters );
$template->param( ALL => gettext('All') );
$template->param( URL_PANEL => $vhffs->get_config->get_panel->{'url'} );
-$template->param( WEBSITES => $res->{websites} );
+$template->param( WEBSITES => $res->{data} );
$panel->light( $template );
$panel->display;
Added: trunk/vhffs-public/groupsearch.pl
===================================================================
--- trunk/vhffs-public/groupsearch.pl (rev 0)
+++ trunk/vhffs-public/groupsearch.pl 2007-11-18 11:20:40 UTC (rev 1089)
@@ -0,0 +1,84 @@
+#!%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 CGI;
+
+use lib '%VHFFS_LIB_DIR%';
+use Vhffs::Panel::Commons;
+use Vhffs::Panel::Group;
+use Vhffs::Panel::Main;
+use Vhffs::Panel::Template;
+
+my $panel = new Vhffs::Panel::Main();
+exit 0 unless $panel;
+
+my $vhffs = $panel->{'vhffs'};
+my $templatedir = $panel->{'templatedir'};
+my $cgi = $panel->{'cgi'};
+
+$panel->check_public();
+
+my $template = new Vhffs::Panel::Template( filename => $templatedir.'/public/groupslist.tmpl', die_on_bad_params => 0 );
+my $groupname = $cgi->param('groupname') || '';
+my $description = $cgi->param('description') || '';
+my $page = $cgi->param('page');
+my $per_page_count = 5;
+$page = 1 unless(defined $page && int($page) > 0);
+
+my $result = Vhffs::Panel::Group::public_search(
+ $vhffs,
+ ( $groupname =~ /^\s*$/ ? undef : $groupname ),
+ ( $description =~ /^\s*$/ ? undef : $description ),
+ ($page -1) * $per_page_count,
+ $per_page_count
+);
+
+$template->param( URL_PANEL => $vhffs->get_config->get_panel->{'url'} );
+
+if($result->{total_count} == 0) {
+ $template->param( TEXT_TITLE => gettext('No group found') );
+} else {
+ Vhffs::Panel::Commons::paginate($template, $page, $result->{total_count}, $per_page_count,
+ { groupname => $groupname, description => $description });
+ $template->param( USE_AVATAR => $panel->use_groups_avatars );
+ $template->param( TEXT_TITLE => sprintf( gettext('%d group(s) found'), $result->{total_count} ) );
+ $template->param( GROUPS => $result->{data} );
+}
+
+$panel->light( $template );
+$panel->display;
+
Property changes on: trunk/vhffs-public/groupsearch.pl
___________________________________________________________________
Name: svn:executable
+ *