Skip to content

Commit

Permalink
Item13324: restructure code to reduce redundancy
Browse files Browse the repository at this point in the history
Item13302: working around CGI>=4.11 breaking utf8 byte strings in data forms
  • Loading branch information
MichaelDaum committed Mar 24, 2015
1 parent 59f74bd commit 8b20e71
Show file tree
Hide file tree
Showing 8 changed files with 962 additions and 801 deletions.
4 changes: 3 additions & 1 deletion data/System/FlexFormPlugin.txt
Expand Up @@ -182,11 +182,13 @@ Note that the actual results may vary depending on the formfield type of the !Da
-->

| Author(s): | Foswiki:MichaelDaum|
| Copyright: | © 2009-2014 Michael Daum http://michaeldaumconsulting.com |
| Copyright: | © 2009-2015 Michael Daum http://michaeldaumconsulting.com |
| License: | [[http://www.gnu.org/licenses/gpl.html][GPL (Gnu General Public License)]] |
| Release: | %$RELEASE% |
| Version: | %$VERSION% |
| Change History: | <!-- versions below in reverse order -->&nbsp; |
| 24 Mar 2015: | working around encoding problems with CGI >= 4.11 |
| 12 Mar 2015: | restructured code to offer a proper perl api |
| 16 Dec 2014: | added =$defaultvalue= variable; \
catch exceptions reading a form definition not to propagate up to the page level; \
by default hide rows marked as hidden; \
Expand Down
84 changes: 49 additions & 35 deletions lib/Foswiki/Plugins/FlexFormPlugin.pm
@@ -1,6 +1,6 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2009-2014 Michael Daum http://michaeldaumconsulting.com
# Copyright (C) 2009-2015 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
Expand All @@ -18,57 +18,71 @@ package Foswiki::Plugins::FlexFormPlugin;
use strict;
use warnings;

our $VERSION = '4.00';
our $RELEASE = '4.00';
our $VERSION = '5.00';
our $RELEASE = '24 Mar 2015';
our $SHORTDESCRIPTION = 'Flexible way to render <nop>DataForms';
our $NO_PREFS_IN_TOPIC = 1;
our $doneInit;
our $baseWeb;
our $baseTopic;
our $renderForEditInstance;
our $renderForDisplayInstance;
our $renderFormDefInstance;

##############################################################################
sub initPlugin {
($baseTopic, $baseWeb) = @_;

Foswiki::Func::registerTagHandler('RENDERFOREDIT', sub {
init();
return Foswiki::Plugins::FlexFormPlugin::Core::handleRENDERFOREDIT(@_);
});
Foswiki::Func::registerTagHandler('RENDERFOREDIT', \&renderForEdit);
Foswiki::Func::registerTagHandler('RENDERFORDISPLAY', \&renderForDisplay);
Foswiki::Func::registerTagHandler('RENDERFORMDEF', \&renderFormDef);

Foswiki::Func::registerTagHandler('RENDERFORDISPLAY', sub {
init();
return Foswiki::Plugins::FlexFormPlugin::Core::handleRENDERFORDISPLAY(@_);
});
return 1;
}

Foswiki::Func::registerTagHandler('RENDERFORMDEF', sub {
init();
return Foswiki::Plugins::FlexFormPlugin::Core::handleRENDERFORMDEF(@_);
});
sub renderForEdit {
my $session = shift;

$doneInit = 0;
return 1;
unless ($renderForEditInstance) {
require Foswiki::Plugins::FlexFormPlugin::RenderForEdit;
$renderForEditInstance = Foswiki::Plugins::FlexFormPlugin::RenderForEdit->new($session);
}

return $renderForEditInstance->handle(@_);
}

sub renderForDisplay {
my $session = shift;

unless ($renderForDisplayInstance) {
require Foswiki::Plugins::FlexFormPlugin::RenderForDisplay;
$renderForDisplayInstance = Foswiki::Plugins::FlexFormPlugin::RenderForDisplay->new($session);
}

return $renderForDisplayInstance->handle(@_);
}

###############################################################################
sub init {
return if $doneInit;
$doneInit = 1;
require Foswiki::Plugins::FlexFormPlugin::Core;
Foswiki::Plugins::FlexFormPlugin::Core::init($baseWeb, $baseTopic);
sub renderFormDef {
my $session = shift;

unless ($renderFormDefInstance) {
require Foswiki::Plugins::FlexFormPlugin::RenderFormDef;
$renderFormDefInstance = new Foswiki::Plugins::FlexFormPlugin::RenderFormDef($session)
}

return $renderFormDefInstance->handle(@_);
}

###############################################################################
# deprecated to be used as a finish handler
sub modifyHeaderHandler {
init();
return Foswiki::Plugins::FlexFormPlugin::Core::finish(@_);
sub finishPlugin {

$renderForEditInstance->finish() if $renderForEditInstance;
$renderForDisplayInstance->finish() if $renderForDisplayInstance;
$renderFormDefInstance->finish() if $renderFormDefInstance;

$renderForEditInstance = undef;
$renderForDisplayInstance = undef;
$renderFormDefInstance = undef;

}

###############################################################################
sub completePageHandler {
$_[0] =~ s/<\/?literal>//g;
}


1;

143 changes: 143 additions & 0 deletions lib/Foswiki/Plugins/FlexFormPlugin/Base.pm
@@ -0,0 +1,143 @@
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/
#
# Copyright (C) 2009-2015 Michael Daum http://michaeldaumconsulting.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details, published at
# http://www.gnu.org/copyleft/gpl.html

package Foswiki::Plugins::FlexFormPlugin::Base;

use strict;
use warnings;

use Foswiki::Func();
use Foswiki::Form();
use Data::Dump qw(dump);

use constant TRACE => 0; # toggle me
our %topicObjs = (); # shared among all classes

sub new {
my $class = shift;
my $session = shift;

my $this = bless({
session => $session,
@_
}, $class);

return $this;
}

sub writeDebug {
return unless TRACE;

my ($this, $msg) = @_;
print STDERR __PACKAGE__ . " - ". $msg . "\n";
}

sub getTopicObject {
my ($this, $web, $topic, $rev) = @_;

$web ||= '';
$topic ||= '';
$rev ||= '';

$web =~ s/\//\./go;
my $key = $web . '.' . $topic . '@' . $rev;
my $topicObj = $topicObjs{$key};

unless ($topicObj) {
($topicObj) = Foswiki::Func::readTopic($web, $topic, $rev);
$topicObjs{$key} = $topicObj;
}

return $topicObj;
}

sub translate {
my ($this, $text, $web, $topic) = @_;

return "" unless defined $text && $text ne "";

return $text unless Foswiki::Func::getContext()->{MultiLingualPluginEnabled};

unless ($this->{translator}) {
require Foswiki::Plugins::MultiLingualPlugin;
$this->{translator} = Foswiki::Plugins::MultiLingualPlugin::getCore();
}

my $params = {
_DEFAULT => $text,
};

return $this->{translator}->TRANSLATE($this->{session}, $params, $topic, $web);
}

sub translateField {
my ($this, $field, $web, $topic) = @_;

return unless $field;
return unless $field->{type} =~ /\+values/;
return unless Foswiki::Func::getContext()->{MultiLingualPluginEnabled};

# populate valueMap
return unless $field->{valueMap};

while (my ($key, $val) = each %{$field->{valueMap}}) {
$field->{valueMap}{$key} = $this->translate($val, $web, $topic);
}
}

sub sortValues {
my ($this, $values, $sort) = @_;

my @values = split(/\s*,\s*/, $values);
my $isNumeric = 1;
foreach my $item (@values) {
$item =~ s/\s*$//;
$item =~ s/^\s*//;
unless ($item =~ /^(\s*[+-]?\d+(\.?\d+)?\s*)$/) {
$isNumeric = 0;
last;
}
}

if ($isNumeric) {
@values = sort { $a <=> $b } @values;
} else {
@values = sort { lc($a) cmp lc($b) } @values;
}

@values = reverse @values if $sort =~ /(rev(erse)?)|desc(end(ing)?)?/;

return join(', ', @values);
}

sub finish {
my $this = shift;

$this->{session} = undef;
$this->{translator} = undef;
%topicObjs = ();
}

sub inlineError {
my ($this, $msg) = @_;

return "<div class='foswikiAlert'>ERROR: ".$msg."</div>";
}

sub handle {
die "not implemented in ".__PACKAGE__;
}

1;

0 comments on commit 8b20e71

Please sign in to comment.