[vhffs-dev] [649] Mailinglist creation now uses only one page.

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


Revision: 649
Author:   beuss
Date:     2007-06-26 19:12:59 +0000 (Tue, 26 Jun 2007)

Log Message:
-----------
Mailinglist creation now uses only one page.

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Panel/Mail.pm
    trunk/vhffs-api/src/Vhffs/Panel/Mailinglist.pm
    trunk/vhffs-backend/src/pgsql/initdb.sql.in
    trunk/vhffs-panel/Makefile.am
    trunk/vhffs-panel/mailinglist/create.pl
    trunk/vhffs-panel/templates/mailinglist/create.tmpl

Removed Paths:
-------------
    trunk/vhffs-panel/mailinglist/submit.pl


Modified: trunk/vhffs-api/src/Vhffs/Panel/Mail.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/Mail.pm	2007-06-16 07:53:40 UTC (rev 648)
+++ trunk/vhffs-api/src/Vhffs/Panel/Mail.pm	2007-06-26 19:12:59 UTC (rev 649)
@@ -114,6 +114,28 @@
     return $mails;
 }
 
+=pod
+
+=head2 is_owned_by_group
+
+    print "Group #$gid owns mail domain $domain\n" if(Vhffs::Panel::Mail::is_owned_by_group( $vhffs, $domain, $gid );
+
+Returns true if a given mail domain is owned by a given group (GID), false otherwise.
+C<$domain> is a string containing the mail domain name.
+
+=cut
+
+sub is_owned_by_group($$$) {
+    my ($main, $domain, $gid) = @_;
+
+    my $dbh = $main->get_db_object;
+    my $sql = 'SELECT COUNT(*) FROM vhffs_mxdomain WHERE domain = ? AND owner_gid = ?';
+    my $sth = $dbh->prepare( $sql );
+    $sth->execute( $domain, $gid );
+    my ($count) = $sth->fetchrow_array();
+    return $count > 0;
+}
+
 sub getall_mail_per_group
 {
 	my $group  = shift;

Modified: trunk/vhffs-api/src/Vhffs/Panel/Mailinglist.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Panel/Mailinglist.pm	2007-06-16 07:53:40 UTC (rev 648)
+++ trunk/vhffs-api/src/Vhffs/Panel/Mailinglist.pm	2007-06-26 19:12:59 UTC (rev 649)
@@ -88,9 +88,9 @@
 
 =head2 getall_per_group
 
-    $dns = Vhffs::Panel::Mail::getall_per_group($vhffs, $gid);
+    $ml = Vhffs::Panel::Mailinglist::getall_per_group($vhffs, $gid);
 
-Returns an array of hashrefs (oid, displayname, active, state (localized string)) of all mail domains by
+Returns an array of hashrefs (oid, displayname, active, state (localized string)) of all mailing lists owned by
 a given group.
 
 =cut
@@ -113,18 +113,37 @@
     return $mls;
 }
 
+=pod
+
+=head2 getall_mdomains_per_group
+
+    $domains = Vhffs::Panel::Mailinglist::getall_mdomains_per_group($vhffs, $gid);
+
+Returns an array of hashref (domain) of all active mail domains for
+a given group.
+
+=cut
+
+sub getall_mdomains_per_group($$) {
+    my ($main, $gid) = @_;
+
+    my $dbh = $main->get_db_object;
+    my $sql = q{ SELECT m.domain FROM vhffs_mxdomain m INNER JOIN vhffs_object o ON o.object_id = m.object_id WHERE m.owner_gid = ? AND o.state = ? ORDER BY m.domain };
+    return ($dbh->selectall_arrayref($sql, { Slice => {} }, $gid, Vhffs::Constants::ACTIVATED) );
+}
+
 sub create_list
 {
     my ($main, $lpart, $domain, $description, $user, $group) = @_;
     
-    return -5 if( ! defined $user );
-    return -5 if( ! defined $group ); 
+    return undef unless( defined $user );
+    return undef unless( defined $group ); 
     
     my $list = Vhffs::Services::Mailing::create( $main , $lpart , $domain, $user->get_mail, $description, $user, $group );
-    return -1 if( ! defined $list );
+    return undef unless( defined $list );
     
-    return -2 if ( Vhffs::Acl::add_acl( $user , $list , Vhffs::Constants::ACL_DELETE , $main ) < 0 );
-    return -3 if( Vhffs::Acl::add_acl( $group , $list , Vhffs::Constants::ACL_VIEW , $main ) < 0 ); 
+    return undef if ( Vhffs::Acl::add_acl( $user , $list , Vhffs::Constants::ACL_DELETE , $main ) < 0 );
+    return undef if( Vhffs::Acl::add_acl( $group , $list , Vhffs::Constants::ACL_VIEW , $main ) < 0 ); 
     
     return $list;
 }

Modified: trunk/vhffs-backend/src/pgsql/initdb.sql.in
===================================================================
--- trunk/vhffs-backend/src/pgsql/initdb.sql.in	2007-06-16 07:53:40 UTC (rev 648)
+++ trunk/vhffs-backend/src/pgsql/initdb.sql.in	2007-06-26 19:12:59 UTC (rev 649)
@@ -308,6 +308,8 @@
 
 -- This index drastically improves performances on get_used_letters
 CREATE INDEX idx_vhffs_httpd_servername_firstletter ON vhffs_httpd(substr(servername, 1, 1));
+-- domain is massively used when manipulating mailing lists (yes we should use IDs)
+CREATE INDEX idx_vhffs_ml_domain ON vhffs_ml(domain);
 -- state is massively used in WHERE clause, this index improves overall performances
 CREATE INDEX idx_vhffs_object_state ON vhffs_object(state);
 

Modified: trunk/vhffs-panel/Makefile.am
===================================================================
--- trunk/vhffs-panel/Makefile.am	2007-06-16 07:53:40 UTC (rev 648)
+++ trunk/vhffs-panel/Makefile.am	2007-06-26 19:12:59 UTC (rev 649)
@@ -132,7 +132,6 @@
 	mailinglist/prefs.pl \
 	mailinglist/save_options.pl \
 	mailinglist/save_sig.pl \
-	mailinglist/submit.pl \
 	mysql/create.pl \
 	mysql/delete.pl \
 	mysql/index.pl \

Modified: trunk/vhffs-panel/mailinglist/create.pl
===================================================================
--- trunk/vhffs-panel/mailinglist/create.pl	2007-06-16 07:53:40 UTC (rev 648)
+++ trunk/vhffs-panel/mailinglist/create.pl	2007-06-26 19:12:59 UTC (rev 649)
@@ -31,11 +31,9 @@
 
 
 use POSIX qw(locale_h);
-use HTML::Template;
+use HTML::Template::Expr;
 use locale;
 use Locale::gettext;
-use CGI;
-use CGI::Session;
 use strict;
 
 use lib '%VHFFS_LIB_DIR%';
@@ -51,55 +49,70 @@
     exit 0;
 }
 
-my $vhffs           = $panel->{'vhffs'};
-my $session         = $panel->{'session'};
-my $maintemplate    = $panel->{'template'};
-my $user            = $panel->{'user'};
-my $group           = $panel->{'group'};
-my $groupname       = $panel->{'groupname'};
-my $default_domain;
-my $domains         = Vhffs::Panel::Mail::getall_mail_per_group( $group , $vhffs , Vhffs::Constants::ACTIVATED ); 
+my $vhffs           = $panel->{vhffs};
+my $user            = $panel->{user};
+my $group           = $panel->{group};
+my $groupname       = $panel->{groupname};
+my $cgi             = $panel->{cgi};
+my $submitted       = $cgi->param('mailing_submit');
 
 # Get default domain for list configured in the config file
 my $list_conf       = $vhffs->get_config->get_service("mailing");
-if( defined $list_conf->{'default_domain'} )
-{
-    $default_domain = $list_conf->{'default_domain'};
+my $default_domain = $list_conf->{'default_domain'};
+
+my $localpart;
+my $domain;
+my $description;
+
+if($submitted) {
+    $localpart = $cgi->param( 'localpart' );
+    $domain = $cgi->param( 'domain' );
+    $description = $cgi->param( 'description' );
+    if( !defined($localpart) || !defined($domain) || !defined($description) ) {
+        $panel->add_error( gettext('CGI error') );
+    } elsif( $description =~ /^\s*$/ ) {
+        $panel->add_error( gettext('You must enter a description') );
+    } elsif( $localpart !~ /^[a-z0-9\_\-]+$/ ) {
+        $panel->add_error( gettext('Invalid local part') );
+        $localpart = '';
+    } elsif( $domain ne $default_domain && (! Vhffs::Panel::Mail::is_owned_by_group( $vhffs, $domain, $group->get_gid ) ) ) {
+        $panel->add_error( gettext('You do not own this domain !') );
+    } elsif( !defined(Vhffs::Panel::Mailinglist::create_list( $vhffs, $localpart, $domain, $description, $user, $group )) ) {
+        $panel->add_error( gettext('An error occured while creating the object.It probably already exists') );
+    } else {
+        my $url = '/panel.pl?project='.$panel->{groupname}.'&msg='.gettext('The mailing list object was successfully created !');
+        $panel->redirect( $url );
+    }
 }
-else
-{
-    $default_domain = undef;
-}
 
-my $template;
+if(!$submitted || $panel->has_errors()) {
+    my $domains = Vhffs::Panel::Mailinglist::getall_mdomains_per_group( $vhffs, $group->get_gid );
+    push(@$domains, { domain => $default_domain }) if(defined $default_domain);
 
-my $templatedir = $vhffs->get_config->get_templatedir;
+    my $template;
 
-if( ( ! defined $domains ) && ( ! defined $default_domain ) )
-{
-	$template = new HTML::Template( filename => $templatedir."/misc/simplemsg.tmpl" );
-	$template->param( MESSAGE => gettext( "You need to manage at least a domain to host a mailing-list" ) );
-}
-else
-{
-	$template = new HTML::Template( filename => $templatedir."/mailinglist/create.tmpl" );
+    my $templatedir = $vhffs->get_config->get_templatedir;
 
-	$template->param( TITLE => gettext("Create a new mailing list") );
-	$template->param( LIST_TEXT => gettext("Mail for the list") );
-	$template->param( GROUP_NAME => gettext("Group owning this mailing list") );
+    $panel->set_title( gettext("Create a new mailing list") );
 
-	my $lol = "";
-	foreach( sort ( keys %{$domains} ) )
-	{
-		$lol .= "<OPTION VALUE=\"".$_."\">".$_."\n";
-	}
-	$lol .= "<OPTION VALUE=\"".$default_domain."\">".$default_domain."\n" if( defined $default_domain );
+    if( ( !@$domains ) && ( ! defined $default_domain ) ) {
+        $panel->add_error( gettext('There is no default mail domain on this platform, you have to create a mail domain before creating a mailing list') );
+        display Vhffs::Panel::Main($panel, '');
+    } else {
+	    $template = new HTML::Template::Expr( filename => $templatedir."/mailinglist/create.tmpl", global_vars => 1 );
 
-	
-	$template->param( DOMAINS => $lol );
-	$template->param( GROUP => $group->get_groupname );
-	$template->param( SEND => gettext("Send") );
-	$template->param( MOTIVATION => gettext("Tell us what the use of this mailing list will be") );
+    	$template->param( LIST_TEXT => gettext("Mail for the list") );
+    	$template->param( GROUP_NAME => gettext("Group owning this mailing list") );
+
+	    $template->param( DOMAINS => $domains );
+    	$template->param( GROUP => $group->get_groupname );
+    	$template->param( SEND => gettext("Send") );
+	    $template->param( MOTIVATION => gettext("Tell us what the use of this mailing list will be") );
+
+        $template->param( LOCALPART_VALUE => $localpart );
+        $template->param( DOMAIN_VALUE => $domain );
+        $template->param( DESCRIPTION_VALUE => $description );
+        display Vhffs::Panel::Main($panel, $template->output);
+    }
 }
 
-display Vhffs::Panel::Main($panel, $template->output);

Deleted: trunk/vhffs-panel/mailinglist/submit.pl
===================================================================
--- trunk/vhffs-panel/mailinglist/submit.pl	2007-06-16 07:53:40 UTC (rev 648)
+++ trunk/vhffs-panel/mailinglist/submit.pl	2007-06-26 19:12:59 UTC (rev 649)
@@ -1,125 +0,0 @@
-#!%PERL% -w
-# 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 POSIX qw(locale_h);
-use HTML::Template;
-use locale;
-use Locale::gettext;
-use CGI;
-use CGI::Session;
-use strict;
-
-use lib '%VHFFS_LIB_DIR%';
-use Vhffs::User;
-use Vhffs::Group;
-use Vhffs::Main;
-use Vhffs::Panel::Main;
-use Vhffs::Panel::Menu;
-use Vhffs::Panel::Group;
-use Vhffs::Panel::Mailinglist;
-use Vhffs::Services::Mailing;
-
-my $panel = new Vhffs::Panel::Main;
-if(!$panel)  {
-        exit 0;
-}
-
-my $vhffs = $panel->{'vhffs'};
-my $session = $panel->{'session'};
-my $maintemplate = $panel->{'template'};
-my $user = $panel->{'user'};
-my $group = $panel->{'group'};
-my $groupname = $panel->{'groupname'};
-my $cgi = $panel->{'cgi'};
-my $message;
-
-my $domain = $cgi->param("DOMAIN");
-my $lpart = $cgi->param("LOCAL_PART");
-
-my $description = $cgi->param("USAGE");
-
-
-my $list;
-
-my $templatedir = $vhffs->get_config->get_templatedir;
-my $default_domain;
-my $domains         = Vhffs::Panel::Mail::getall_mail_per_group( $group , $vhffs , Vhffs::Constants::ACTIVATED );
-my $valid = 0;
-if( defined $vhffs->get_config->get_service("mailing")->{'default_domain'} )
-{
-    $default_domain = $vhffs->get_config->get_service("mailing")->{'default_domain'};
-}
-else
-{
-    $default_domain = undef;
-}
-
-$valid = 1 if( $default_domain eq $domain );
-
-foreach( sort ( keys %{$domains} ) )
-{
-    $valid = 1 if( $_ eq $domain );
-}
-
-
-if( ( ! defined $description ) || ( ! defined $domain ) || ( ! defined $lpart ) )
-{
-	$message = gettext("CGI Error");
-}
-elsif( $valid == 0 )
-{
-    $message = gettext("You don't own this domain");
-}
-else
-{
-	my $lst;
-	$lst = Vhffs::Panel::Mailinglist::create_list( $vhffs, $lpart, $domain, $description, $user, $group );
-
-    if( ref($lst) )	{
-        $message = gettext("Mailing-list successfully created !");
-    } elsif( $lst == -1 ) {
-        $message = gettext("An error occured while creating the list");
-    } elsif( $lst == -2 ) {
-		$message = gettext("An error occured while adding yourself to the ACL");
-    } elsif( $lst == -3 ) {
-        $message = gettext("An error occured while adding an ACL for the group");
-    } else {
-        $message = gettext("Error while creating list (unknow problem)");
-    }
-}
-
-
-my $template = new HTML::Template( filename => $templatedir."/misc/simplemsg.tmpl" );
-$template->param( MESSAGE => $message );
-
-set_refresh_url Vhffs::Panel::Main($panel, "/panel.pl?project=$groupname", 0);
-display Vhffs::Panel::Main($panel, $template->output);

Modified: trunk/vhffs-panel/templates/mailinglist/create.tmpl
===================================================================
--- trunk/vhffs-panel/templates/mailinglist/create.tmpl	2007-06-16 07:53:40 UTC (rev 648)
+++ trunk/vhffs-panel/templates/mailinglist/create.tmpl	2007-06-26 19:12:59 UTC (rev 649)
@@ -1,25 +1,27 @@
-<h1><TMPL_VAR NAME="TITLE"></h1>
-
-<form method="post" action="submit.pl" class="largeLabel">
+<form method="post" action="#" class="largeLabel">
 	<p>
-		<label>		
-			<TMPL_VAR NAME="GROUP_NAME">: 
-		</label>
-		<TMPL_VAR NAME="GROUP">
+		<TMPL_VAR NAME="GROUP_NAME">:<TMPL_VAR NAME="GROUP">
 	</p>
 	<p>
-		<label for="LOCAL_PART">
+		<label for="localpart">
 			<TMPL_VAR NAME="LIST_TEXT">: 
-		</label>		
-		<input type="text" name="LOCAL_PART" id="LOCAL_PART"/>@<SELECT NAME="DOMAIN"><TMPL_VAR NAME="DOMAINS"></SELECT>
+		</label>
+    </p>
+    <div class="clear"></div>
+    <p>
+		<input type="text" name="localpart" id="localpart" value="<tmpl_var name="LOCALPART_VALUE">"/>@<select name="domain">
+<tmpl_loop name="DOMAINS">
+<option value="<tmpl_var name="DOMAIN">"<tmpl_if expr="DOMAIN eq DOMAIN_VALUE"> selected="selected"</tmpl_if>><tmpl_var NAME="DOMAIN"></option>
+</tmpl_loop>
+</select>
 	</p>
 	<p>
-		<label for="USAGE">
+		<label for="description">
 			<TMPL_VAR NAME="MOTIVATION">:
 		</label>
-		<textarea name="USAGE" id="USAGE" cols="45" rows="7"></textarea>
+		<textarea name="description" id="description" cols="45" rows="7"><tmpl_var name="DESCRIPTION_VALUE"></textarea>
 	</p>
 	<p class="button" id="buttonSend">
-		<input type="submit" value="<TMPL_VAR NAME="SEND">" />
+		<input type="submit" value="<TMPL_VAR NAME="SEND">" name="mailing_submit"/>
 	</p>
 </form>


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