checkbox.pl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # Copyright (C) 2006 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. AddModuleDescription('checkbox.pl', 'Checklist Extension');
  21. our ($q, $bol, @HtmlStack, %Action, %Page, $OpenPageName, @MyRules);
  22. # [[ : To do]]
  23. # [[X: Done]]
  24. # [[save: Save]]
  25. push(@MyRules, \&CheckBoxRule);
  26. sub CheckBoxRule {
  27. if ($bol and /\G\[\[( |x):([^]]+)\]\]/cgi) {
  28. my $html;
  29. if (not InElement('form')) {
  30. # We want to use GetFormStart so we have to trade-off using
  31. # AddHtmlEnvironment.
  32. $html = CloseHtmlEnvironments()
  33. . GetFormStart(undef, 'get', 'checkboxes');
  34. unshift(@HtmlStack, 'form');
  35. $html .= AddHtmlEnvironment('p');
  36. }
  37. $html .= $q->checkbox(-name => FreeToNormal($2),
  38. -checked => ($1 eq ' '? 0 : 1),
  39. -label => $2)
  40. . $q->br();
  41. return $html;
  42. } elsif (/\G\[\[save:([^]]+)\]\]/cgi) {
  43. if (InElement('form')) {
  44. return ($q->input({-type => 'hidden',
  45. -name => 'action',
  46. -value => 'checkbox'})
  47. . $q->input({-type => 'hidden',
  48. -name => 'id',
  49. -value => $OpenPageName})
  50. . $q->submit(-name => $1));
  51. } else {
  52. return $1;
  53. }
  54. }
  55. return;
  56. }
  57. $Action{checkbox} = \&DoCheckBox;
  58. sub DoCheckBox{
  59. my $id = shift;
  60. OpenPage($id);
  61. my $text = $Page{text};
  62. my %summary;
  63. $text =~ s{(\A|\n)\[\[( |x):([^]]+)\]\]}{
  64. # no search and replace in this loop
  65. if (GetParam(FreeToNormal($3))) {
  66. $summary{$3} = 1 if $2 eq ' ';
  67. "${1}[[x:${3}]]";
  68. } else {
  69. $summary{$3} = 0 if $2 eq 'x' or $2 eq 'X';
  70. "${1}[[ :${3}]]";
  71. }
  72. }egi;
  73. SetParam('text', $text);
  74. SetParam('summary', join(', ', map {
  75. if ($summary{$_}) {
  76. Ts('set %s', $_);
  77. } else {
  78. Ts('unset %s', $_);
  79. }
  80. } keys %summary));
  81. DoPost($id);
  82. }