| 1 | # | ||
| 2 | # $Id: TableIO.pm,v 1.3 2005/01/27 21:33:26 rsandberg Exp $ | ||
| 3 | # | ||
| 4 | |||
| 5 | |||
| 6 | package CGI::CRUD::TableIO; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use DBIx::IO::Search; | ||
| 10 | use DBIx::IO::Table; | ||
| 11 | use CGI::CRUD::Table; | ||
| 12 | use CGI::AutoForm; | ||
| 13 | use CGI::Enurl (); | ||
| 14 | |||
| 15 | use constant OK => 0; | ||
| 16 | |||
| 17 | =pod | ||
| 18 | |||
| 19 | =head1 NAME | ||
| 20 | |||
| 21 | CGI::CRUD::TableIO - Virtual base class for a basic web front-end to an RDBMS | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | Virtual base class provides skeletal CRUD routines for a web front-end. Subclass/override/customize | ||
| 26 | to your heart's content. One popular method to override is C<verify_input>. | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | # ctor | ||
| 31 | sub new | ||
| 32 | { | ||
| 33 | my ($caller,$dbh) = @_; | ||
| 34 | |||
| 35 | my $class = ref($caller) || $caller; | ||
| 36 | my $obj = { dbh => $dbh }; | ||
| 37 | |||
| 38 | $obj = bless($obj,$class); | ||
| 39 | |||
| 40 | $obj->{verify_input} = sub { $obj->verify_input(@_); }; | ||
| 41 | return $obj; | ||
| 42 | } | ||
| 43 | |||
| 44 | # Build SQL for the search form submission | ||
| 45 | sub where_sql | ||
| 46 | { | ||
| 47 | my ($self,$table_dat,$table_name) = @_; | ||
| 48 | |||
| 49 | my $searcher = $self->build_search($table_dat,$table_name) or return undef; | ||
| 50 | $searcher->_build_sql(); | ||
| 51 | return $searcher->{where} || 0; | ||
| 52 | } | ||
| 53 | |||
| 54 | # Build the DBIx::IO::Search object to assist with where_sql() | ||
| 55 | sub build_search | ||
| 56 | { | ||
| 57 | my ($self,$table_dat,$table_name) = @_; | ||
| 58 | |||
| 59 | my $searcher = new DBIx::IO::Search($self->{dbh},$table_name) or return undef; | ||
| 60 | my ($field,$val); | ||
| 61 | while (($field,$val) = each(%$table_dat)) | ||
| 62 | { | ||
| 63 | next if !length($val); | ||
| 64 | if (ref($val) eq 'ARRAY') | ||
| 65 | { | ||
| 66 | $searcher->build_list_crit($field,$val); | ||
| 67 | } | ||
| 68 | elsif (ref($val) eq 'HASH') | ||
| 69 | { | ||
| 70 | if (exists($val->{_WM})) | ||
| 71 | { | ||
| 72 | $searcher->build_scalar_crit($field,'LIKE',$val->{_WM}); | ||
| 73 | } | ||
| 74 | elsif (exists($val->{_CL})) | ||
| 75 | { | ||
| 76 | $searcher->build_list_crit($field,[split(/,/,$val->{_CL})]); | ||
| 77 | } | ||
| 78 | else | ||
| 79 | { | ||
| 80 | # Expect 2 keys from this hash for date range values | ||
| 81 | $searcher->build_range_crit($field,$val->{_RS},$val->{_RE}) | ||
| 82 | if (($searcher->{io}->is_date($field) || $searcher->{io}->is_datetime($field)) ? $val->{_UR} : 1); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | else | ||
| 86 | { | ||
| 87 | $searcher->build_scalar_crit($field,'=',$val); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | |||
| 91 | return $searcher; | ||
| 92 | } | ||
| 93 | |||
| 94 | # Perform record deletion operation | ||
| 95 | sub delete_req | ||
| 96 | { | ||
| 97 | my ($self,$r) = @_; | ||
| 98 | my $table_name = $r->param('__SDAT_TAB_ACTION.TABLE_NAME'); | ||
| 99 | my $q = $r->query(); | ||
| 100 | my ($table,$rec); | ||
| 101 | my $sq = CGI::AutoForm->format_query($q); | ||
| 102 | unless (($table = new DBIx::IO::Table($r->dbh(),undef,undef,$table_name)) && ($rec = $table->fetch($sq->{__SDAT}{KEYS}))) | ||
| 103 | { | ||
| 104 | defined($rec) && ($r->output("Record no longer exists"),return OK); | ||
| 105 | $r->server_error(); | ||
| 106 | return undef; | ||
| 107 | } | ||
| 108 | unless ($table->delete()) | ||
| 109 | { | ||
| 110 | $r->server_error(); | ||
| 111 | return undef; | ||
| 112 | } | ||
| 113 | my $msg = qq[<P>Record Deleted</P>]; | ||
| 114 | $msg .= $self->return_results($q); | ||
| 115 | $r->output($msg); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub return_results | ||
| 119 | { | ||
| 120 | my ($self,$fq) = @_; | ||
| 121 | my $msg = qq[<P><TABLE WIDTH="100%"><TR>]; | ||
| 122 | my $q = CGI::AutoForm->extract_query_group($fq,'__SDAT_TAB_ACTION'); | ||
| 123 | $q->{'__SDAT_TAB_ACTION.ACTION'} = 'SD'; | ||
| 124 | my $stq = CGI::AutoForm->extract_cut_query_group($fq,'__SDAT.SC'); | ||
| 125 | my $sq = stringify_query({ %$q, %$stq }); | ||
| 126 | $msg .= qq[<TD><A HREF="$self->{action}?$sq">Return to search results<A></TD>]; | ||
| 127 | $msg .= $self->sreturn($q); | ||
| 128 | $msg .= qq[</TR></TABLE></P>]; | ||
| 129 | return $msg; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub sreturn | ||
| 133 | { | ||
| 134 | my ($self,$q) = @_; | ||
| 135 | my $msg; | ||
| 136 | my $eq = CGI::AutoForm->extract_query_group($q,'__SDAT_TAB_ACTION'); | ||
| 137 | $eq->{'__SDAT_TAB_ACTION.ACTION'} = 'SR'; | ||
| 138 | my $sq = stringify_query($eq); | ||
| 139 | $msg .= qq[<TD><A HREF="$self->{action}?$sq">New Search with $q->{'__SDAT_TAB_ACTION.TABLE_NAME'}<A></TD>]; | ||
| 140 | $eq->{'__SDAT_TAB_ACTION.ACTION'} = 'IR'; | ||
| 141 | $sq = stringify_query($eq); | ||
| 142 | $msg .= qq[<TD><A HREF="$self->{action}?$sq">Add to $q->{'__SDAT_TAB_ACTION.TABLE_NAME'}<A></TD>]; | ||
| 143 | $eq->{'__SDAT_TAB_ACTION.RESTART'} = 1; | ||
| 144 | $sq = stringify_query($eq); | ||
| 145 | $msg .= qq[<TD><A HREF="$self->{action}?$sq">New DB Operation<A></TD>]; | ||
| 146 | return $msg; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub new_start | ||
| 150 | { | ||
| 151 | my ($self,$q) = @_; | ||
| 152 | my $msg = qq[<P><TABLE WIDTH="100%"><TR>]; | ||
| 153 | $msg .= $self->sreturn($q); | ||
| 154 | $msg .= qq[</TR></TABLE></P>]; | ||
| 155 | return $msg; | ||
| 156 | } | ||
| 157 | |||
| 158 | # Perform update operation | ||
| 159 | |||
| 160 | # special value of NULL still recognized, however its sufficient to have an empty new value | ||
| 161 | # where the existing value is not empty, this will update the value to NULL a little more risky but much more | ||
| 162 | # convenient because values of length < 4 (e.g. YORN and date elements) will have to be expanded to 4 | ||
| 163 | # losing some ability to constrain the values | ||
| 164 | # THIS MEANS IT IS UP TO YOU TO REPRESENT ALL VALUES IN AN UPDATE, OTHERWISE THEY **WILL BE SET TO NULL** | ||
| 165 | # e.g. submit a full record to form->add_record and make sure field_template has *all* fields, either by | ||
| 166 | # completely relying on the data dictionary or inserting a record for all fields in UI_TABLE_COLUMN | ||
| 167 | sub update_data | ||
| 168 | { | ||
| 169 | my ($self,$r) = @_; | ||
| 170 | my $form = $self->update_form($r) || return undef; | ||
| 171 | my $q = $r->query(); | ||
| 172 | my %vq = %$q; | ||
| 173 | map { $vq{$_} =~ s/^NULL$// } keys(%vq); | ||
| 174 | unless ($form->validate_query(\%vq,$self->{verify_input})) | ||
| 175 | { | ||
| 176 | $r->output($form->prepare($q)); | ||
| 177 | return OK; | ||
| 178 | } | ||
| 179 | |||
| 180 | my $table_name = $r->param('__SDAT_TAB_ACTION.TABLE_NAME'); | ||
| 181 | my ($table,$rec); | ||
| 182 | my $sq = $form->format_query($q); | ||
| 183 | unless (($table = new CGI::CRUD::Table($r->dbh(),$r->user,undef,undef,$table_name)) && ($rec = $table->fetch($sq->{__SDAT}{KEYS}))) | ||
| 184 | { | ||
| 185 | defined($rec) && ($r->output("Record no longer exists"),return OK); | ||
| 186 | $r->server_error(); | ||
| 187 | return undef; | ||
| 188 | } | ||
| 189 | my $table_dat = $sq->{uc($table_name)}; | ||
| 190 | |||
| 191 | # a special value of 'NULL' updates a value to NULL | ||
| 192 | map { $table_dat->{$_} =~ s/^NULL$// } keys(%$table_dat); | ||
| 193 | map { $table_dat->{$_} = '' unless exists($table_dat->{$_}) } keys(%{$table->column_types()}); | ||
| 194 | unless ($table->update($table_dat)) | ||
| 195 | { | ||
| 196 | $r->server_error(); | ||
| 197 | return undef; | ||
| 198 | } | ||
| 199 | my $msg = qq[<P>Record Updated</P>]; | ||
| 200 | $msg .= $self->return_results($q); | ||
| 201 | $r->output($msg); | ||
| 202 | } | ||
| 203 | |||
| 204 | # Build update form | ||
| 205 | sub update_form | ||
| 206 | { | ||
| 207 | my ($self,$r) = @_; | ||
| 208 | my $form = $r->form($r->dbh()); | ||
| 209 | my $table_name = $r->param('__SDAT_TAB_ACTION.TABLE_NAME'); | ||
| 210 | $form->heading("Update $table_name"); | ||
| 211 | $form->action($self->{action}); | ||
| 212 | $form->submit_value('Update'); | ||
| 213 | $r->graceful_add_form_group($form,'DISPLAY EDIT',$table_name,'Edit fields and submit when done') || return undef; | ||
| 214 | return $form; | ||
| 215 | } | ||
| 216 | |||
| 217 | # Build/present update form | ||
| 218 | sub update_req | ||
| 219 | { | ||
| 220 | my ($self,$r) = @_; | ||
| 221 | my $form = $self->update_form($r) || return undef; | ||
| 222 | my $q = $r->query(); | ||
| 223 | $q->{'__SDAT_TAB_ACTION.ACTION'} = 'UD'; | ||
| 224 | my $sq = $form->format_query($q); | ||
| 225 | my ($table,$rec); | ||
| 226 | my $table_name = $r->param('__SDAT_TAB_ACTION.TABLE_NAME'); | ||
| 227 | unless (($table = new DBIx::IO::Table($r->dbh(),undef,undef,$table_name)) && ($rec = $table->fetch($sq->{__SDAT}{KEYS}))) | ||
| 228 | { | ||
| 229 | defined($rec) && ($r->output("Record no longer exists"),return OK); | ||
| 230 | $r->server_error(); | ||
| 231 | return undef; | ||
| 232 | } | ||
| 233 | $form->add_record($rec); | ||
| 234 | $r->output($form->prepare($q)); | ||
| 235 | } | ||
| 236 | |||
| 237 | # Perform search operation and return results | ||
| 238 | sub search_results | ||
| 239 | { | ||
| 240 | my ($self,$r) = @_; | ||
| 241 | |||
| 242 | # keep in mind this is NOT normalized or unescaped | ||
| 243 | my $q = $r->query(); | ||
| 244 | my $form = $self->search_form($r) || return undef; | ||
| 245 | unless ($form->validate_query($q)) | ||
| 246 | { | ||
| 247 | $r->output($form->prepare($q)); | ||
| 248 | return OK; | ||
| 249 | } | ||
| 250 | |||
| 251 | my $table_name = $r->param('__SDAT_TAB_ACTION.TABLE_NAME'); | ||
| 252 | $form = new CGI::AutoForm($r->dbh()); | ||
| 253 | $r->graceful_add_form_group($form,'DISPLAY ONLY',$table_name,"Searching...",undef,1) || return undef; | ||
| 254 | $q = $form->format_query($r->query()); | ||
| 255 | my $table_dat = $q->{uc($table_name)}; | ||
| 256 | my $searcher = $self->build_search($table_dat,$table_name) or ($r->server_error(),return undef); | ||
| 257 | my $field_list = $form->field_list(); | ||
| 258 | my $ffield; | ||
| 259 | foreach my $f (@$field_list) | ||
| 260 | { | ||
| 261 | if (length($f->{BRIEF_HEADING})) | ||
| 262 | { | ||
| 263 | $ffield = $f->{FIELD_NAME}; | ||
| 264 | last; | ||
| 265 | } | ||
| 266 | } | ||
| 267 | $searcher->sortlist([ $ffield ]); | ||
| 268 | |||
| 269 | my $results = $searcher->search(); | ||
| 270 | unless ($results) | ||
| 271 | { | ||
| 272 | $r->server_error(); | ||
| 273 | return $results; | ||
| 274 | } | ||
| 275 | unless (@$results) | ||
| 276 | { | ||
| 277 | $r->output("No results found"); | ||
| 278 | return 1; | ||
| 279 | } | ||
| 280 | my $recno = scalar(@$results); | ||
| 281 | my $recapp = ($recno > 1 ? 's' : ''); | ||
| 282 | |||
| 283 | my $keys = $searcher->{pk}; | ||
| 284 | my $warnk = ''; | ||
| 285 | unless (@$keys) | ||
| 286 | { | ||
| 287 | ##at could workaround this for Oracle by using ROWID | ||
| 288 | #warn("search requested for a table with no primary key"); | ||
| 289 | $warnk = qq[<P><FONT COLOR="RED">View/Edit/Delete of individual records disabled because there is no primary key defined for this table.</FONT></P>]; | ||
| 290 | } | ||
| 291 | |||
| 292 | $form->current_group()->{heading} = "Found $recno record$recapp.$warnk"; | ||
| 293 | $form->add_record($results); | ||
| 294 | |||
| 295 | # requires that $table_name has a primary key (via DBIx::IO) | ||
| 296 | |||
| 297 | my $tq = $form->extract_query_group($r->query(),uc($table_name)); | ||
| 298 | my $search_cache = stringify_query($tq,"__SDAT.SC"); | ||
| 299 | |||
| 300 | # make sure there are no closure issues with, for example, $searcher shouldn't be any with an anonymous sub | ||
| 301 | my $rec_callback = sub | ||
| 302 | { | ||
| 303 | my ($rec_html,$rec,$group) = @_; | ||
| 304 | my $add; | ||
| 305 | my $etab = CGI::Enurl::enurl($table_name); | ||
| 306 | my $qs = CGI::Enurl::enURL(qq[__SDAT_TAB_ACTION.TABLE_NAME=]) . CGI::Enurl::enurl($table_name) . '&'; | ||
| 307 | my $ur = CGI::Enurl::enURL(qq[__SDAT_TAB_ACTION.ACTION=UR&]); | ||
| 308 | my $dr = CGI::Enurl::enURL(qq[__SDAT_TAB_ACTION.ACTION=DR&]); | ||
| 309 | foreach my $key (@$keys) | ||
| 310 | { | ||
| 311 | $qs .= CGI::Enurl::enurl("__SDAT.KEYS.$key") . '=' . CGI::Enurl::enurl($rec->{$key}) . "&"; | ||
| 312 | } | ||
| 313 | chop($qs); | ||
| 314 | $add .= qq[<TD><A HREF="$self->{action}?$ur$qs&$search_cache">View/Edit</A></TD>]; | ||
| 315 | $add .= qq[<TD style="width: 10px; text-align: center;">|</TD><TD><A HREF="$self->{action}?$dr$qs&$search_cache">Delete</A></TD>]; | ||
| 316 | return $rec_html . $add; | ||
| 317 | }; | ||
| 318 | $form->{head_html} = qq[<H2>Search Results for $table_name</H2>]; | ||
| 319 | |||
| 320 | $form->{tail_html} = ' '; | ||
| 321 | my $html = $form->prepare(undef,undef,(@$keys ? $rec_callback : undef())); | ||
| 322 | $$html .= $self->new_start($r->query()); | ||
| 323 | $r->output($html); | ||
| 324 | } | ||
| 325 | |||
| 326 | # Build search form | ||
| 327 | sub search_form | ||
| 328 | { | ||
| 329 | my ($self,$r) = @_; | ||
| 330 | my $form = $r->form($r->dbh()); | ||
| 331 | $form->heading('Search Criteria'); | ||
| 332 | $form->action($self->{action}); | ||
| 333 | $form->submit_value('Search'); | ||
| 334 | my $table_name = $r->param('__SDAT_TAB_ACTION.TABLE_NAME'); | ||
| 335 | $r->graceful_add_form_group($form,'SEARCHABLE',$table_name,"Build criteria to report on $table_name") || return undef; | ||
| 336 | return $form; | ||
| 337 | } | ||
| 338 | |||
| 339 | # Build/present search form | ||
| 340 | sub search_req | ||
| 341 | { | ||
| 342 | my ($self,$r) = @_; | ||
| 343 | my $form = $self->search_form($r) || return undef; | ||
| 344 | my $q = $r->query(); | ||
| 345 | $q->{'__SDAT_TAB_ACTION.ACTION'} = 'SD'; | ||
| 346 | $r->output($form->prepare($q)); | ||
| 347 | } | ||
| 348 | |||
| 349 | # Override in subclass to perform custom checks on input | ||
| 350 | # Will be passed as a callback to CGI::AutoForm::validate_query() | ||
| 351 | # refer to those docs for parameters and expected return values | ||
| 352 | sub verify_input | ||
| 353 | { | ||
| 354 | return 1; | ||
| 355 | } | ||
| 356 | |||
| 357 | # Perform insert operation | ||
| 358 | sub insert_data | ||
| 359 | { | ||
| 360 | my ($self,$r) = @_; | ||
| 361 | my $table_name = $r->param('__SDAT_TAB_ACTION.TABLE_NAME'); | ||
| 362 | my $q = $r->query(); | ||
| 363 | $q->{'__SDAT_TAB_ACTION.ACTION'} = 'ID'; | ||
| 364 | my $form = $self->insert_form($r->dbh(),$table_name,$r) || return undef; | ||
| 365 | unless ($form->validate_query($q,$self->{verify_input})) | ||
| 366 | { | ||
| 367 | $r->output($form->prepare($q)); | ||
| 368 | return OK; | ||
| 369 | } | ||
| 370 | |||
| 371 | my $table; | ||
| 372 | my $sq = $form->format_query($q); | ||
| 373 | my $rv; | ||
| 374 | unless (($table = new CGI::CRUD::Table($r->dbh(),$r->user,undef,undef,$table_name)) && ($rv = $table->insert($sq->{uc($table_name)}))) | ||
| 375 | { | ||
| 376 | $r->server_error(); | ||
| 377 | return undef; | ||
| 378 | } | ||
| 379 | if ($rv == -1.1) | ||
| 380 | { | ||
| 381 | $r->perror("No data to insert"); | ||
| 382 | return undef; | ||
| 383 | } | ||
| 384 | elsif ($rv == -1.4) | ||
| 385 | { | ||
| 386 | $r->perror("Duplicate key violation on insert."); | ||
| 387 | return undef; | ||
| 388 | } | ||
| 389 | my $aq = $form->extract_query_group($q,'__SDAT_TAB_ACTION'); | ||
| 390 | if ($q->{'__SDAT.CONTINUE'}) | ||
| 391 | { | ||
| 392 | $form->{top_message} = qq[<DIV>Data saved.</DIV>]; | ||
| 393 | $r->output($form->prepare($aq)); | ||
| 394 | } | ||
| 395 | else | ||
| 396 | { | ||
| 397 | $r->output($self->insert_or_return($aq)); | ||
| 398 | } | ||
| 399 | } | ||
| 400 | |||
| 401 | # Build insert form | ||
| 402 | sub insert_form | ||
| 403 | { | ||
| 404 | my ($self,$dbh,$table_name,$r) = @_; | ||
| 405 | my $form = $r->form($dbh); | ||
| 406 | $form->heading("Input for $table_name"); | ||
| 407 | $form->action($self->{action}); | ||
| 408 | |||
| 409 | $form->{tail_html} = qq[<P><TABLE WIDTH="100%"><TR ALIGN="CENTER"><TD><INPUT TYPE="RESET"> ] . | ||
| 410 | qq[<INPUT TYPE="SUBMIT" NAME="__SDAT.CONTINUE" VALUE="Save/Continue">] . | ||
| 411 | qq[ <INPUT TYPE="SUBMIT" VALUE="Save"></TD></TR></TABLE></P></FORM>]; | ||
| 412 | $r->graceful_add_form_group($form,'INSERTABLE',$table_name,"Insert data for \U$table_name") || return undef; | ||
| 413 | return $form; | ||
| 414 | } | ||
| 415 | |||
| 416 | sub insert_or_return | ||
| 417 | { | ||
| 418 | my ($self,$q,$top_message) = @_; | ||
| 419 | $q->{'__SDAT_TAB_ACTION.ACTION'} = 'IR'; | ||
| 420 | my $msg; | ||
| 421 | $msg .= 'Data saved.'; | ||
| 422 | $msg .= "<BR>$top_message" if $top_message; | ||
| 423 | $msg .= qq[<P><TABLE WIDTH="100%"><TR>]; | ||
| 424 | my $sq = stringify_query($q); | ||
| 425 | $msg .= qq[<TD><A HREF="$self->{action}?$sq">Insert new record<A></TD>]; | ||
| 426 | $q->{'__SDAT_TAB_ACTION.RESTART'} = 1; | ||
| 427 | $sq = stringify_query($q); | ||
| 428 | $msg .= qq[<TD><A HREF="$self->{action}?$sq">Admin panel<A></TD>]; | ||
| 429 | $msg .= qq[</TR></TABLE></P>]; | ||
| 430 | return $msg; | ||
| 431 | } | ||
| 432 | |||
| 433 | # Build/present insert form | ||
| 434 | sub insert_req | ||
| 435 | { | ||
| 436 | my ($self,$r) = @_; | ||
| 437 | my $table_name = $r->param('__SDAT_TAB_ACTION.TABLE_NAME'); | ||
| 438 | my $q = $r->query(); | ||
| 439 | $q->{'__SDAT_TAB_ACTION.ACTION'} = 'ID'; | ||
| 440 | my $form = $self->insert_form($r->dbh(),$table_name,$r) || return undef; | ||
| 441 | $r->output($form->prepare($q)); | ||
| 442 | } | ||
| 443 | |||
| 444 | sub stringify_query | ||
| 445 | { | ||
| 446 | my ($q,$prefix) = @_; | ||
| 447 | if (length($prefix)) | ||
| 448 | { | ||
| 449 | $prefix .= '.' unless substr($prefix,-1) eq '.'; | ||
| 450 | } | ||
| 451 | my ($str,$field,$val); | ||
| 452 | my %dates_expand; | ||
| 453 | foreach my $fq (@{flatten_query($q)}) | ||
| 454 | { | ||
| 455 | ($field,$val) = each(%$fq); | ||
| 456 | if ($field =~ /(.*)\._(RE\.|RS\.|UR$)/) | ||
| 457 | { | ||
| 458 | push(@{$dates_expand{$1}{params}},{$field,$val}); | ||
| 459 | $dates_expand{$1}{ur} = 1 if $2 eq 'UR' && length($val); | ||
| 460 | } | ||
| 461 | else | ||
| 462 | { | ||
| 463 | $str .= CGI::Enurl::enurl("$prefix$field") . '=' . CGI::Enurl::enurl($val) . '&' if length($val); | ||
| 464 | } | ||
| 465 | } | ||
| 466 | |||
| 467 | my ($f,$v); | ||
| 468 | while (($f,$v) = each(%dates_expand)) | ||
| 469 | { | ||
| 470 | if ($dates_expand{$f}{ur}) | ||
| 471 | { | ||
| 472 | foreach my $fq (@{$dates_expand{$f}{params}}) | ||
| 473 | { | ||
| 474 | ($field,$val) = each(%$fq); | ||
| 475 | $str .= CGI::Enurl::enurl("$prefix$field") . '=' . CGI::Enurl::enurl($val) . '&' if length($val); | ||
| 476 | } | ||
| 477 | } | ||
| 478 | } | ||
| 479 | chop($str); | ||
| 480 | return $str; | ||
| 481 | } | ||
| 482 | |||
| 483 | # Do not pass in a structured query | ||
| 484 | sub flatten_query | ||
| 485 | { | ||
| 486 | my ($q) = @_; | ||
| 487 | my (@flat,$name,$val); | ||
| 488 | while (($name,$val) = each(%$q)) | ||
| 489 | { | ||
| 490 | $val = [ $val ] unless ref($val); | ||
| 491 | foreach my $v (@$val) | ||
| 492 | { | ||
| 493 | push(@flat,{ $name => $v }); | ||
| 494 | } | ||
| 495 | } | ||
| 496 | return \@flat; | ||
| 497 | } | ||
| 498 | |||
| 499 | 1; | ||
| 500 | |||
| 501 | __END__ |