| 1 | package QWizard::Plugins::Bookmarks; | ||
| 2 | |||
| 3 | our $VERSION = '3.15'; | ||
| 4 | require Exporter; | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use QWizard; | ||
| 8 | use QWizard::API; | ||
| 9 | use QWizard::Storage::File; | ||
| 10 | |||
| 11 | our @ISA = qw(Exporter); | ||
| 12 | our @EXPORT_OK = qw(init_bookmarks); | ||
| 13 | |||
| 14 | our $marks; | ||
| 15 | |||
| 16 | our $memory_mark = '__memory_mark'; | ||
| 17 | our $memory_mark_modified = '__memory_mark_modified'; | ||
| 18 | |||
| 19 | sub get_bookmark_list { | ||
| 20 | my $qw = shift; | ||
| 21 | my @list = ($qw->{'__bookmarks_params'}{'title'}, | ||
| 22 | $qw->{'__bookmarks_params'}{'saveopt'}); | ||
| 23 | my $all = $marks->get_all(); | ||
| 24 | push @list, (grep { $_ !~ /^__/ } keys(%$all)); | ||
| 25 | return \@list; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | # | ||
| 30 | # this function is called at the top of a page and if the user had hit | ||
| 31 | # the 'save' equivelent menu item then it jumps to the primary to | ||
| 32 | # request a name. | ||
| 33 | # | ||
| 34 | sub bookmarks_start_page_begin { | ||
| 35 | my ($qw) = @_; | ||
| 36 | return if ($qw->qwpref('qw_nobookmarks')); | ||
| 37 | if ($qw->qwparam('qwbookmarks') ne $qw->{'__bookmarks_params'}{'title'}) { | ||
| 38 | if ($qw->qwparam('qwbookmarks') eq | ||
| 39 | $qw->{'__bookmarks_params'}{'saveopt'}) { | ||
| 40 | if (!$qw->qwparam('bookmark_name')) { | ||
| 41 | $qw->add_todos('get_bookmark_name'); | ||
| 42 | } | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | # | ||
| 48 | # this function is called right at the beginning of the keep_working | ||
| 49 | # qwizard loop. | ||
| 50 | # | ||
| 51 | # 1) if we got here after entering a save name save the data under | ||
| 52 | # the bookmark_name parameter setting and then restore the screen | ||
| 53 | # to just before they hit the 'save' button. | ||
| 54 | # | ||
| 55 | # 2) If they hit the menu to load a bookmark, load that data | ||
| 56 | # | ||
| 57 | # 3) else (!2) save the current spot into the special $memory_mark slot. | ||
| 58 | # | ||
| 59 | sub bookmarks_keep_working_begin { | ||
| 60 | my ($qw) = @_; | ||
| 61 | |||
| 62 | return if ($qw->qwpref('qw_nobookmarks')); | ||
| 63 | if ($qw->qwparam('bookmark_name')) { | ||
| 64 | # | ||
| 65 | # memorize the page the user was looking at | ||
| 66 | # Note: which is different than where we are now | ||
| 67 | # | ||
| 68 | my $usemodified = $qw->qwparam('qwbm_use_modified'); | ||
| 69 | my $str = $marks->get($usemodified ? | ||
| 70 | $memory_mark_modified : $memory_mark); | ||
| 71 | $marks->set($qw->qwparam('bookmark_name'), $str); | ||
| 72 | |||
| 73 | # after saving it, reset to that point to continue on | ||
| 74 | bookmarks_jump_to_string($qw, $str); | ||
| 75 | } | ||
| 76 | if ($qw->qwparam('qwbookmarks') ne $qw->{'__bookmarks_params'}{'title'}) { | ||
| 77 | if ($qw->qwparam('qwbookmarks') ne | ||
| 78 | $qw->{'__bookmarks_params'}{'saveopt'}) { | ||
| 79 | |||
| 80 | # Get the requested book mark data | ||
| 81 | my $str = $marks->get($qw->qwparam('qwbookmarks')); | ||
| 82 | |||
| 83 | # Jump to it | ||
| 84 | bookmarks_jump_to_string($qw, $str); | ||
| 85 | } | ||
| 86 | } else { | ||
| 87 | # save the current QWizard history spot for use later if they | ||
| 88 | # do bookmark this page. | ||
| 89 | my $str = $qw->{'generator'}{'datastore'}->to_string(); | ||
| 90 | $marks->set($memory_mark, $str); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | sub bookmarks_jump_to_string { | ||
| 95 | my ($qw, $str) = @_; | ||
| 96 | |||
| 97 | my $store = $qw->{'generator'}{'datastore'}; | ||
| 98 | |||
| 99 | $store->from_string($str); | ||
| 100 | |||
| 101 | if ($store->get('qwbookmarks') ne $qw->{'__bookmarks_params'}{'title'}) { | ||
| 102 | # we need to refresh the current screen instead of | ||
| 103 | # 'submitting' to the next one. | ||
| 104 | $store->set('redo_screen',1); | ||
| 105 | |||
| 106 | # We need to change the value of the bookmark menu back to | ||
| 107 | # its default so we don't trigger any other code. | ||
| 108 | $store->set('qwbookmarks', | ||
| 109 | $qw->{'__bookmarks_params'}{'title'}); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | |||
| 114 | # | ||
| 115 | # call for end_sections_hook | ||
| 116 | # | ||
| 117 | sub bookmarks_end_section_hook { | ||
| 118 | my ($qw, $p) = @_; | ||
| 119 | $p = $p->[0]; | ||
| 120 | return if ($p->{'title'} eq 'Remember This Spot'); | ||
| 121 | my $str = $qw->{'generator'}{'datastore'}->to_string(); | ||
| 122 | $marks->set($memory_mark_modified, $str); | ||
| 123 | } | ||
| 124 | |||
| 125 | sub init_bookmarks { | ||
| 126 | my ($qw, $storage, %params) = @_; | ||
| 127 | |||
| 128 | our @questions = | ||
| 129 | ( | ||
| 130 | { type => 'menu', name => 'qwbookmarks', | ||
| 131 | values => \&get_bookmark_list, | ||
| 132 | submit => 1, | ||
| 133 | default => $params{'title'} || 'Bookmarks', override => 1}, | ||
| 134 | ); | ||
| 135 | |||
| 136 | our %bookmark_primaries = | ||
| 137 | ( | ||
| 138 | get_bookmark_name => | ||
| 139 | { title => 'Remember This Spot', | ||
| 140 | questions => | ||
| 141 | [qw_text('bookmark_name', "Name to remember it by:", | ||
| 142 | check_value => \&QWizard::qw_required_field), | ||
| 143 | qw_checkbox('qwbm_use_modified', | ||
| 144 | "Use Page Settings Including Changes", | ||
| 145 | 1, 0, | ||
| 146 | default => 1, | ||
| 147 | helpdesc => '(otherwise save the page before modifications were made to it)')], | ||
| 148 | } | ||
| 149 | ); | ||
| 150 | |||
| 151 | |||
| 152 | $qw->{'__bookmarks_params'} = \%params; | ||
| 153 | $qw->{'__bookmarks_params'}{'title'} = 'Bookmarks' | ||
| 154 | unless (exists($qw->{'__bookmarks_params'}{'title'})); | ||
| 155 | $qw->{'__bookmarks_params'}{'saveopt'} = 'Remember This Page' | ||
| 156 | unless (exists($qw->{'__bookmarks_params'}{'saveopt'})); | ||
| 157 | $marks = $storage; | ||
| 158 | push @{$qw->{'topbar'}},@questions; | ||
| 159 | $qw->add_hook('start_page_begin', \&bookmarks_start_page_begin); | ||
| 160 | $qw->add_hook('keep_working_begin', \&bookmarks_keep_working_begin); | ||
| 161 | $qw->add_hook('end_section', \&bookmarks_end_section_hook); | ||
| 162 | $qw->merge_primaries(\%bookmark_primaries); | ||
| 163 | } | ||
| 164 | |||
| 165 | =pod | ||
| 166 | |||
| 167 | =head1 NAME | ||
| 168 | |||
| 169 | QWizard::Plugins::Bookmarks - Adds a bookmark menu to QWizard based applications | ||
| 170 | answers. | ||
| 171 | |||
| 172 | =head1 SYNOPSIS | ||
| 173 | |||
| 174 | use QWizard; | ||
| 175 | use QWizard::Storage::File; | ||
| 176 | use QWizard::Plugins::Bookmarks; | ||
| 177 | |||
| 178 | my $qw = new QWizard( ... ); | ||
| 179 | my $storage = new QWizard::Stoarge::File(file => "/path/to/file"); | ||
| 180 | init_bookmarks($qw, $storage, OPTIONS); | ||
| 181 | |||
| 182 | =head1 DESCRIPTION | ||
| 183 | |||
| 184 | This module simply adds in a menu at the top of all QWizard screens to | ||
| 185 | create and, display and jump to bookmarks. The bookmarks_init | ||
| 186 | function needs access to the already created qwizard object and a | ||
| 187 | QWizard storage container (SQL or File based ones, for example, work | ||
| 188 | well). | ||
| 189 | |||
| 190 | The bookmarks will not be shown if the qw_nobookmarks preference is | ||
| 191 | set to a true value. | ||
| 192 | |||
| 193 | =head1 OPTIONS | ||
| 194 | |||
| 195 | The options parameter of the init_bookmarks() function allows you to | ||
| 196 | set I<name> => I<value> value pairs of options. The following are | ||
| 197 | currently supported: | ||
| 198 | |||
| 199 | =over | ||
| 200 | |||
| 201 | =item I<title> => I<"Bookmarks"> | ||
| 202 | |||
| 203 | The title parameter lets you override the default menu name from | ||
| 204 | "Bookmarks" to a title of your choice. | ||
| 205 | |||
| 206 | =item I<saveopt> => I<"Remember This Page"> | ||
| 207 | |||
| 208 | The option name that used used as the "save this spot" kind of value. | ||
| 209 | |||
| 210 | =back | ||
| 211 | |||
| 212 | =head1 AUTHOR | ||
| 213 | |||
| 214 | Wes Hardaker, hardaker@users.sourceforge.net | ||
| 215 | |||
| 216 | =head1 SEE ALSO | ||
| 217 | |||
| 218 | perl(1) | ||
| 219 | |||
| 220 | Net-Policy: http://net-policy.sourceforge.net/ | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | 1; |