| 1 | package Jifty::View::Declare::CoreTemplates; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use Jifty::View::Declare -base; | ||
| 7 | |||
| 8 | use Scalar::Defer; | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | Jifty::View::Declare::CoreTemplates - Templates Jifty can't function without | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This library contains templates that Jifty can't function without: | ||
| 17 | |||
| 18 | =over | ||
| 19 | |||
| 20 | =item Validate | ||
| 21 | |||
| 22 | =item Autocomplete | ||
| 23 | |||
| 24 | =item Canonicalize | ||
| 25 | |||
| 26 | =item YAML and XML webservice endpoints for core jifty functionality | ||
| 27 | |||
| 28 | =back | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | =for later | ||
| 33 | |||
| 34 | These templates are still in Masonland. we're doing something wrong with escaping in them | ||
| 35 | |||
| 36 | template '__jifty/autocomplete.xml' => sub { | ||
| 37 | |||
| 38 | # Note: the only point to this file is to set the content_type; the actual | ||
| 39 | # behavior is accomplished inside the framework. It will go away once we | ||
| 40 | # have infrastructure for serving things of various content-types. | ||
| 41 | Jifty->web->response->content_type('text/xml; charset=utf-8'); | ||
| 42 | my $ref = Jifty->web->response->result('autocomplete')->content; | ||
| 43 | my @options = @{ $ref->{'completions'} || [] }; | ||
| 44 | body { | ||
| 45 | ul { | ||
| 46 | foreach my $item (@options) { | ||
| 47 | if ( !ref($item) ) { | ||
| 48 | li { $item }; | ||
| 49 | } | ||
| 50 | elsif ( exists $item->{label} ) { | ||
| 51 | li { | ||
| 52 | with( class => "informal" ), span { $item->{label} }; | ||
| 53 | with( class => "hidden_value" ), | ||
| 54 | span { $item->{value} }; | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | else { | ||
| 58 | li { $item->{value} }; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | }; | ||
| 63 | }; | ||
| 64 | |||
| 65 | |||
| 66 | template '__jifty/validator.xml' => sub { | ||
| 67 | Jifty->web->response->content_type('text/xml; charset=utf-8'); | ||
| 68 | my $output = ""; | ||
| 69 | use XML::Writer; | ||
| 70 | my $writer = XML::Writer->new( OUTPUT => \$output ); | ||
| 71 | $writer->xmlDecl( "UTF-8", "yes" ); | ||
| 72 | $writer->startTag("validation"); | ||
| 73 | for my $ra ( Jifty->web->request->actions ) { | ||
| 74 | my $action = Jifty->web->new_action_from_request($ra); | ||
| 75 | $writer->startTag( "validationaction", id => $action->register_name ); | ||
| 76 | for my $arg ( $action->argument_names ) { | ||
| 77 | if ( not $action->arguments->{$arg}->{ajax_validates} ) { | ||
| 78 | $writer->emptyTag( "ignored", | ||
| 79 | id => $action->error_div_id($arg) ); | ||
| 80 | $writer->emptyTag( "ignored", | ||
| 81 | id => $action->warning_div_id($arg) ); | ||
| 82 | } | ||
| 83 | elsif ( not defined $action->argument_value($arg) | ||
| 84 | or length $action->argument_value($arg) == 0 ) | ||
| 85 | { | ||
| 86 | $writer->emptyTag( "blank", id => $action->error_div_id($arg) ); | ||
| 87 | $writer->emptyTag( "blank", | ||
| 88 | id => $action->warning_div_id($arg) ); | ||
| 89 | } | ||
| 90 | elsif ( $action->result->field_error($arg) ) { | ||
| 91 | $writer->dataElement( | ||
| 92 | "error", | ||
| 93 | $action->result->field_error($arg), | ||
| 94 | id => $action->error_div_id($arg) | ||
| 95 | ); | ||
| 96 | $writer->emptyTag( "ok", id => $action->warning_div_id($arg) ); | ||
| 97 | } | ||
| 98 | elsif ( $action->result->field_warning($arg) ) { | ||
| 99 | $writer->dataElement( | ||
| 100 | "warning", | ||
| 101 | $action->result->field_warning($arg), | ||
| 102 | id => $action->warning_div_id($arg) | ||
| 103 | ); | ||
| 104 | $writer->emptyTag( "ok", id => $action->error_div_id($arg) ); | ||
| 105 | } | ||
| 106 | else { | ||
| 107 | $writer->emptyTag( "ok", id => $action->error_div_id($arg) ); | ||
| 108 | $writer->emptyTag( "ok", id => $action->warning_div_id($arg) ); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | $writer->endTag(); | ||
| 112 | $writer->startTag( "canonicalizeaction", id => $action->register_name ); | ||
| 113 | for my $arg ( $action->argument_names ) { | ||
| 114 | no warnings 'uninitialized'; | ||
| 115 | if ( $ra->arguments->{$arg} eq $action->argument_value($arg) ) { | ||
| 116 | |||
| 117 | # if the value doesn' t change, it can be ignored . | ||
| 118 | |||
| 119 | # canonicalizers can change other parts of the action, so we want to send all changes | ||
| 120 | $writer->emptyTag( "ignored", | ||
| 121 | name => $action->form_field_name($arg) ); | ||
| 122 | } | ||
| 123 | elsif ( not defined $action->argument_value($arg) | ||
| 124 | or length $action->argument_value($arg) == 0 ) | ||
| 125 | { | ||
| 126 | $writer->emptyTag( "blank", | ||
| 127 | name => $action->form_field_name($arg) ); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | if ( $action->result->field_canonicalization_note($arg) ) { | ||
| 131 | $writer->dataElement( | ||
| 132 | "canonicalization_note", | ||
| 133 | $action->result->field_canonicalization_note($arg), | ||
| 134 | id => $action->canonicalization_note_div_id($arg) | ||
| 135 | ); | ||
| 136 | } | ||
| 137 | $writer->dataElement( | ||
| 138 | "update", | ||
| 139 | $action->argument_value($arg), | ||
| 140 | name => $action->form_field_name($arg) | ||
| 141 | ); | ||
| 142 | } | ||
| 143 | } | ||
| 144 | $writer->endTag(); | ||
| 145 | } | ||
| 146 | $writer->endTag(); | ||
| 147 | Jifty->web->out($output); | ||
| 148 | }; | ||
| 149 | |||
| 150 | template '__jifty/webservices/xml' => sub { | ||
| 151 | my $output = ""; | ||
| 152 | my $writer = XML::Writer->new( | ||
| 153 | OUTPUT => \$output, | ||
| 154 | UNSAFE => 1 | ||
| 155 | ); | ||
| 156 | $writer->xmlDecl( "UTF-8", "yes" ); | ||
| 157 | $writer->startTag("response"); | ||
| 158 | for my $f ( Jifty->web->request->fragments ) { | ||
| 159 | |||
| 160 | # Set up the region stack | ||
| 161 | local Jifty->web->{'region_stack'} = []; | ||
| 162 | my @regions; | ||
| 163 | do { | ||
| 164 | push @regions, $f; | ||
| 165 | } while ( $f = $f->parent ); | ||
| 166 | |||
| 167 | for $f ( reverse @regions ) { | ||
| 168 | my $new = | ||
| 169 | Jifty->web->get_region( join '-', | ||
| 170 | grep { $_ } Jifty->web->qualified_region, $f->name ); | ||
| 171 | |||
| 172 | # Arguments can be complex mapped hash values. Get their | ||
| 173 | # real values by mapping. | ||
| 174 | my %defaults = %{ $f->arguments || {} }; | ||
| 175 | for ( keys %defaults ) { | ||
| 176 | my ( $key, $value ) = Jifty::Request::Mapper->map( | ||
| 177 | destination => $_, | ||
| 178 | source => $defaults{$_} | ||
| 179 | ); | ||
| 180 | delete $defaults{$_}; | ||
| 181 | $defaults{$key} = $value; | ||
| 182 | } | ||
| 183 | |||
| 184 | $new ||= Jifty::Web::PageRegion->new( | ||
| 185 | name => $f->name, | ||
| 186 | path => $f->path, | ||
| 187 | region_wrapper => $f->wrapper, | ||
| 188 | parent => Jifty->web->current_region, | ||
| 189 | defaults => \%defaults, | ||
| 190 | ); | ||
| 191 | $new->enter; | ||
| 192 | } | ||
| 193 | |||
| 194 | # Stuff the rendered region into the XML | ||
| 195 | $writer->startTag( "fragment", | ||
| 196 | id => Jifty->web->current_region->qualified_name ); | ||
| 197 | my %args = %{ Jifty->web->current_region->arguments }; | ||
| 198 | $writer->dataElement( "argument", $args{$_}, name => $_ ) | ||
| 199 | for sort keys %args; | ||
| 200 | $writer->cdataElement( "content", | ||
| 201 | Jifty->web->current_region->as_string ); | ||
| 202 | $writer->endTag(); | ||
| 203 | |||
| 204 | Jifty->web->current_region->exit while Jifty->web->current_region; | ||
| 205 | } | ||
| 206 | |||
| 207 | my %results = Jifty->web->response->results; | ||
| 208 | for ( keys %results ) { | ||
| 209 | $writer->startTag( | ||
| 210 | "result", | ||
| 211 | moniker => $_, | ||
| 212 | class => $results{$_}->action_class | ||
| 213 | ); | ||
| 214 | $writer->dataElement( "success", $results{$_}->success ); | ||
| 215 | |||
| 216 | $writer->dataElement( "message", $results{$_}->message ) | ||
| 217 | if $results{$_}->message; | ||
| 218 | $writer->dataElement( "error", $results{$_}->error ) | ||
| 219 | if $results{$_}->error; | ||
| 220 | |||
| 221 | my %warnings = $results{$_}->field_warnings; | ||
| 222 | my %errors = $results{$_}->field_errors; | ||
| 223 | my %fields; | ||
| 224 | $fields{$_}++ for keys(%warnings), keys(%errors); | ||
| 225 | for ( sort keys %fields ) { | ||
| 226 | next unless $warnings{$_} or $errors{$_}; | ||
| 227 | $writer->startTag( "field", name => $_ ); | ||
| 228 | $writer->dataElement( "warning", $warnings{$_} ) | ||
| 229 | if $warnings{$_}; | ||
| 230 | $writer->dataElement( "error", $errors{$_} ) | ||
| 231 | if $errors{$_}; | ||
| 232 | $writer->endTag(); | ||
| 233 | } | ||
| 234 | |||
| 235 | # XXX TODO: Hack because we don't have a good way to serialize | ||
| 236 | # Jifty::DBI::Record's yet, which are technically circular data | ||
| 237 | # structures at some level (current_user of a | ||
| 238 | # current_user->user_object is itself) | ||
| 239 | use Scalar::Util qw(blessed); | ||
| 240 | my $content = $results{$_}->content; | ||
| 241 | |||
| 242 | |||
| 243 | $content = _stripkids($content); | ||
| 244 | use XML::Simple; | ||
| 245 | $writer->raw( | ||
| 246 | XML::Simple::XMLout( | ||
| 247 | $content, | ||
| 248 | NoAttr => 1, | ||
| 249 | RootName => "content", | ||
| 250 | NoIndent => 1 | ||
| 251 | ) | ||
| 252 | ) if keys %{$content}; | ||
| 253 | |||
| 254 | $writer->endTag(); | ||
| 255 | } | ||
| 256 | |||
| 257 | $writer->endTag(); | ||
| 258 | Jifty->web->response->content_type('text/xml; charset=utf-8'); | ||
| 259 | |||
| 260 | # For some reason, this line is needed, lest we end up outputting ISO-8859-1 text | ||
| 261 | utf8::decode($output); | ||
| 262 | |||
| 263 | outs_raw($output); | ||
| 264 | }; | ||
| 265 | |||
| 266 | sub _stripkids { | ||
| 267 | my $top = shift; | ||
| 268 | if ( not ref $top ) { return $top } | ||
| 269 | elsif ( | ||
| 270 | blessed($top) | ||
| 271 | and ( $top->isa("Jifty::DBI::Record") | ||
| 272 | or $top->isa("Jifty::DBI::Collection") ) | ||
| 273 | ) | ||
| 274 | { | ||
| 275 | return undef; | ||
| 276 | } | ||
| 277 | elsif ( ref $top eq 'HASH' ) { | ||
| 278 | foreach my $item ( keys %$top ) { | ||
| 279 | $top->{$item} = _stripkids( $top->{$item} ); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | elsif ( ref $top eq 'ARRAY' ) { | ||
| 283 | for ( 0 .. $#{$top} ) { | ||
| 284 | $top->[$_] = _stripkids( $top->[$_] ); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | return $top; | ||
| 288 | } | ||
| 289 | |||
| 290 | |||
| 291 | template '__jifty/webservices/yaml' => sub { | ||
| 292 | Jifty->web->response->content_type("text/x-yaml"); | ||
| 293 | outs( Jifty::YAML::Dump( { Jifty->web->response->results } ) ); | ||
| 294 | }; | ||
| 295 | |||
| 296 | =cut | ||
| 297 | |||
| 298 | 1; |