1# Copyright (C) 2007-2017 Michael Daum http://michaeldaumconsulting.com
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version. For
7# more details read LICENSE in the root of this distribution.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13package Foswiki::Plugins::NatEditPlugin::FormButton;
14
15use strict;
16use warnings;
17
18use Foswiki::Func                  ();
19use Foswiki::Plugins::JQueryPlugin ();
20
21=begin TML
22
23---+ package Foswiki::Plugins::NatEditPlugin::FormButton
24
25render a button to add/change the form while editing
26returns
27   * the empty string if there's no WEBFORM
28   * or "Add form" if there is no form attached to a topic yet
29   * or "Change form" otherwise
30
31there are no native means besides the "addform" template being used
32to render the FORMFIELDS. but this is not what we need here at all. infact
33we need an empty addform.nat.tmp to switch off this feature of FORMFIELDS
34
35=cut
36
37sub handle {
38    my ( $session, $params, $theTopic, $theWeb ) = @_;
39
40    Foswiki::Plugins::JQueryPlugin::createPlugin("natedit");
41
42    my $saveCmd = '';
43    my $request = Foswiki::Func::getCgiQuery();
44    $saveCmd = $request->param('cmd') || '' if $request;
45    return '' if $saveCmd eq 'repRev';
46
47    my $form = $request->param('formtemplate') || '';
48
49    unless ($form) {
50        my ( $meta, $dumy ) = Foswiki::Func::readTopic( $theWeb, $theTopic );
51        my $formMeta = $meta->get('FORM');
52        $form = $formMeta->{"name"} if $formMeta;
53    }
54
55    $form = '' if $form eq 'none';
56
57    my $action;
58    my $actionTitle;
59    my $actionText;
60
61    if ($form) {
62        $action = 'replaceform';
63    }
64    else {
65        $action = 'addform';
66    }
67
68    if ($form) {
69        $actionText = $session->{i18n}->maketext("Change form");
70        $actionTitle =
71          $session->{i18n}->maketext( "Change the current form of <nop>[_1]",
72            "$theWeb.$theTopic" );
73    }
74    elsif ( Foswiki::Func::getPreferencesValue( 'WEBFORMS', $theWeb ) ) {
75        $actionText = $session->{i18n}->maketext("Add form");
76        $actionTitle =
77          $session->{i18n}
78          ->maketext( "Add a new form to <nop>[_1]", "$theWeb.$theTopic" );
79    }
80    else {
81        return '';
82    }
83    $actionText  =~ s/&&/\&/g;
84    $actionTitle =~ s/&&/\&/g;
85
86    my $theFormat = $params->{_DEFAULT} || $params->{format} || '$link';
87    $theFormat =~
88s/\$link/<a href='\$url' accesskey='f' title='\$title'><span>\$acton<\/span><\/a>/g;
89    $theFormat =~ s/\$url/javascript:\$script/g;
90    $theFormat =~ s/\$script/submitEditForm('save', '$action');/g;
91    $theFormat =~ s/\$title/$actionTitle/g;
92    $theFormat =~ s/\$action/$actionText/g;
93    $theFormat =~ s/\$id/$action/g;
94
95    return Foswiki::Func::decodeFormatTokens($theFormat);
96}
97
981;
99