Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 8c6871be

Von Moritz Bunkus vor fast 12 Jahren hinzugefügt

  • ID 8c6871bed016edac152019db5a7f88136bf9457b
  • Vorgänger 5f68c975
  • Nachfolger 4f70967e

$::request: Funktionen "is_ajax()" und "type()"

Unterschiede anzeigen:

SL/Dispatcher.pm
35 35
use SL::Helper::DateTime;
36 36
use SL::InstanceConfiguration;
37 37
use SL::Template::Plugin::HTMLFixes;
38
use SL::Layout::None;
39 38

  
40 39
# Trailing new line is added so that Perl will not add the line
41 40
# number 'die' was called in.
......
69 68
      show_error('login_screen/auth_db_unreachable');
70 69
    }
71 70
  }
71

  
72
  if ($::request->type !~ m/^ (?: html | js | json ) $/x) {
73
    die $::locale->text("Invalid request type '#1'", $::request->type);
74
  }
72 75
}
73 76

  
74 77
sub show_error {
......
182 185
  $::locale        = Locale->new($::lx_office_conf{system}->{language});
183 186
  $::form          = Form->new;
184 187
  $::instance_conf = SL::InstanceConfiguration->new;
185
  $::request       = {
188
  $::request       = SL::Request->new(
186 189
    cgi => CGI->new({}),
187 190
    layout => SL::Layout::None->new,
188
  };
191
  );
189 192

  
190 193
  my $session_result = $::auth->restore_session;
191 194
  $::auth->create_or_refresh_session;
192 195

  
193 196
  $::form->read_cgi_input;
194 197

  
195
  eval { ($routing_type, $script_name, $action) = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
198
  my %routing;
199
  eval { %routing = _route_request($ENV{SCRIPT_NAME}); 1; } or return;
200
  ($routing_type, $script_name, $action) = @routing{qw(type controller action)};
201

  
202
  $::request->type(lc($routing{request_type} || 'html'));
196 203

  
197 204
  if ($routing_type eq 'old') {
198 205
    $::form->{action}  =  lc $::form->{action};
......
312 319
sub _route_request {
313 320
  my $script_name = shift;
314 321

  
315
  return $script_name =~ m/dispatcher\.pl$/ ? ('old',        _route_dispatcher_request())
316
       : $script_name =~ m/controller\.pl/  ? ('controller', _route_controller_request())
317
       :                                      ('old',        $script_name, $::form->{action});
322
  return $script_name =~ m/dispatcher\.pl$/ ? (type => 'old',        _route_dispatcher_request())
323
       : $script_name =~ m/controller\.pl/  ? (type => 'controller', _route_controller_request())
324
       :                                      (type => 'old',        controller => $script_name, action => $::form->{action});
318 325
}
319 326

  
320 327
sub _route_dispatcher_request {
......
344 351
    show_error('generic/error');
345 352
  };
346 353

  
347
  return ($script_name, $action);
354
  return (controller => $script_name, action => $action);
348 355
}
349 356

  
350 357
sub _route_controller_request {
351
  my ($controller, $action);
358
  my ($controller, $action, $request_type);
352 359

  
353 360
  eval {
354
    $::form->{action}      =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) $|x || die "Unroutable request -- inavlid controller/action.\n";
361
    $::form->{action}      =~ m|^ ( [A-Z] [A-Za-z0-9_]* ) / ( [a-z] [a-z0-9_]* ) ( \. [a-zA-Z]+ )? $|x || die "Unroutable request -- inavlid controller/action.\n";
355 362
    ($controller, $action) =  ($1, $2);
356 363
    delete $::form->{action};
357 364

  
365
    $request_type = $3 ? lc(substr($3, 1)) : 'html';
366

  
358 367
    1;
359 368
  } or do {
360 369
    $::form->{label_error} = $::request->{cgi}->pre($EVAL_ERROR);
361 370
    show_error('generic/error');
362 371
  };
363 372

  
364
  return ($controller, $action);
373
  return (controller => $controller, action => $action, request_type => $request_type);
365 374
}
366 375

  
367 376
sub _cache_file_modification_times {
SL/Presenter.pm
16 16
use SL::Presenter::Record;
17 17

  
18 18
sub get {
19
  $::request->{presenter} ||= SL::Presenter->new;
20
  return $::request->{presenter};
19
  return $::request->presenter;
21 20
}
22 21

  
23 22
sub render {
SL/Request.pm
2 2

  
3 3
use strict;
4 4

  
5
use SL::Common;
6
use SL::MoreCommon qw(uri_encode uri_decode);
5
use parent qw(Rose::Object);
6

  
7
use CGI qw(-no_xhtml);
7 8
use List::Util qw(first max min sum);
8 9
use List::MoreUtils qw(all any apply);
9 10
use Exporter qw(import);
10 11

  
12
use SL::Common;
13
use SL::MoreCommon qw(uri_encode uri_decode);
14
use SL::Layout::None;
15
use SL::Presenter;
16

  
11 17
our @EXPORT_OK = qw(flatten unflatten read_cgi_input);
12 18

  
19
use Rose::Object::MakeMethods::Generic
20
(
21
  'scalar --get_set_init' => [ qw(cgi layout presenter is_ajax type) ],
22
);
23

  
24
sub init_cgi {
25
  return CGI->new({});
26
}
27

  
28
sub init_layout {
29
  return SL::Layout::None->new;
30
}
31

  
32
sub init_presenter {
33
  return SL::Presenter->new;
34
}
35

  
36
sub init_is_ajax {
37
  return ($ENV{HTTP_X_REQUESTED_WITH} || '') eq 'XMLHttpRequest' ? 1 : 0;
38
}
39

  
40
sub init_type {
41
  return 'html';
42
}
43

  
13 44
sub _store_value {
14 45
  my ($target, $key, $value) = @_;
15 46
  my @tokens = split /((?:\[\+?\])?(?:\.)|(?:\[\+?\]))/, $key;
......
322 353

  
323 354
=head1 NAME
324 355

  
325
SL::Request.pm - request parsing and data serialization
356
SL::Request.pm - request parsing, data serialization, request information
326 357

  
327 358
=head1 SYNOPSIS
328 359

  
329
This module handles unpacking of cgi parameters. usually you don't want to call
330
anything in here directly.
360
This module handles unpacking of CGI parameters. It also gives
361
information about the request like whether or not it was done via AJAX
362
or the requested content type.
331 363

  
332 364
  use SL::Request qw(read_cgi_input);
333 365

  
......
338 370
  my $new_arrayref = flatten($hashref);
339 371
  my $new_hashref  = unflatten($new_arrayref);
340 372

  
373
  # Handle AJAX requests differently than normal requests:
374
  if ($::request->is_ajax) {
375
    $controller->render('json-mask', { type => 'json' });
376
  } else {
377
    $controller->render('full-mask');
378
  }
341 379

  
342 380
=head1 DESCRIPTION
343 381

  
344
This module handles flattening and unflattening of data for request
382
This module provides information about the request made by the
383
browser.
384

  
385
It also handles flattening and unflattening of data for request
345 386
roundtrip purposes. kivitendo uses the format as described below:
346 387

  
347 388
=over 4
......
472 513

  
473 514
This function will parse the array ref, and will store the contents into the hash ref. The hash ref may be non empty, in this case any new keys will override the old ones only on leafs with same type. Type changes on a node will die.
474 515

  
516
=item C<is_ajax>
517

  
518
Returns trueish if the request is an XML HTTP request, also known as
519
an 'AJAX' request.
520

  
521
=item C<type>
522

  
523
Returns the requested content type (either C<html>, C<js> or C<json>).
524

  
475 525
=back
476 526

  
477 527
=head1 SPECIAL FUNCTIONS
locale/de/all
1026 1026
  'Invalid'                     => 'Ungültig',
1027 1027
  'Invalid follow-up ID.'       => 'Ung&uuml;ltige Wiedervorlage-ID.',
1028 1028
  'Invalid quantity.'           => 'Die Mengenangabe ist ung&uuml;ltig.',
1029
  'Invalid request type \'#1\'' => '',
1029 1030
  'Invdate'                     => 'Rechnungsdatum',
1030 1031
  'Invdate from'                => 'Rechnungen von',
1031 1032
  'Inventory'                   => 'Inventar',

Auch abrufbar als: Unified diff