| 1 | package JiftyX::Markapl::Helpers; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | BEGIN { | ||
| 6 | require Markapl; | ||
| 7 | *outs_raw = \&Markapl::outs_raw; | ||
| 8 | *outs = \&Markapl::outs | ||
| 9 | } | ||
| 10 | |||
| 11 | use base qw/Exporter/; | ||
| 12 | |||
| 13 | our @EXPORT = qw(hyperlink tangent redirect new_action form_submit | ||
| 14 | form_return form_next_page request render_param render_hidden | ||
| 15 | render_action render_region current_user | ||
| 16 | get set ); | ||
| 17 | |||
| 18 | sub current_user { Jifty->web->current_user } | ||
| 19 | |||
| 20 | sub request { Jifty->web->request } | ||
| 21 | |||
| 22 | sub _function_wrapper(@) { | ||
| 23 | my $function = shift; | ||
| 24 | Markapl->buffer->append(Jifty->web->$function(@_)->render || ''); | ||
| 25 | return ''; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub hyperlink(@) { _function_wrapper link => @_ } | ||
| 29 | sub tangent(@) { _function_wrapper tangent => @_ } | ||
| 30 | sub redirect(@) { _function_wrapper redirect => @_ } | ||
| 31 | |||
| 32 | sub new_action(@) { Jifty->web->new_action(@_);} | ||
| 33 | |||
| 34 | sub render_param { | ||
| 35 | my $action = shift; | ||
| 36 | outs_raw( $action->form_field(@_) ); | ||
| 37 | return ''; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub render_hidden { | ||
| 41 | my $action = shift; | ||
| 42 | outs_raw( $action->hidden(@_) ); | ||
| 43 | return '' | ||
| 44 | } | ||
| 45 | |||
| 46 | sub render_action(@) { | ||
| 47 | my ( $action, $fields, $field_args ) = @_; | ||
| 48 | |||
| 49 | my @f = ($fields && ref ($fields) eq 'ARRAY') ? @$fields : $action->argument_names; | ||
| 50 | foreach my $argument (@f) { | ||
| 51 | outs_raw( $action->form_field( $argument, %$field_args )->render ); | ||
| 52 | } | ||
| 53 | ''; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub form_submit(@) { | ||
| 57 | outs_raw( Jifty->web->form->submit(@_) ); | ||
| 58 | ''; | ||
| 59 | } | ||
| 60 | |||
| 61 | sub form_return(@) { | ||
| 62 | outs_raw( Jifty->web->form->return(@_) ); | ||
| 63 | ''; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub form_next_page(@) { | ||
| 67 | Jifty->web->form->next_page(@_); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub render_region(@) { | ||
| 71 | unshift @_, 'name' if @_ % 2; | ||
| 72 | my $args = {@_}; | ||
| 73 | my $path = $args->{path} ||= '/__jifty/empty'; | ||
| 74 | |||
| 75 | Jifty::Web::PageRegion->new(%$args)->render; | ||
| 76 | } | ||
| 77 | |||
| 78 | sub js_handlers(@) {} | ||
| 79 | |||
| 80 | { | ||
| 81 | no warnings qw/redefine/; | ||
| 82 | sub form (&) { | ||
| 83 | my $code = shift; | ||
| 84 | outs_raw( Jifty->web->form->start(@_) ); | ||
| 85 | $code->(); | ||
| 86 | outs_raw( Jifty->web->form->end ); | ||
| 87 | return ''; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub get { | ||
| 91 | if (wantarray) { | ||
| 92 | map { _get_single($_) } @_; | ||
| 93 | } else { | ||
| 94 | _get_single($_[0]); | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _get_single { | ||
| 99 | my $v = request->template_argument($_[0]) || request->argument( $_[0] ); | ||
| 100 | return $v if defined $v; | ||
| 101 | |||
| 102 | if (request->top_request ne request() and $v = request->top_request->template_argument($_[0])) { | ||
| 103 | if (ref $v) { | ||
| 104 | warn("The template argument '$_[0]' was not explicitly passed to the current region ('@{[request->path]}'), and thus will not work if the region is ever refreshed. Unfortunately, it is a reference, so it can't be passed explicitly either. You'll need to explicitly pass some stringification of what it is to the region.".Carp::longmess); | ||
| 105 | } else { | ||
| 106 | warn("The template argument '$_[0]' was not explicitly passed to the the current region ('@{[request->path]}'), and thus will not work if the region is ever refreshed. Try passing it explicitly?"); | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub set { | ||
| 112 | while ( my ( $arg, $val ) = splice(@_, 0, 2) ) { | ||
| 113 | request->template_argument( $arg => $val ); | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | 1; |