| 1 | package OpenInteract::Template::Plugin; | ||
| 2 | |||
| 3 | # $Id: Plugin.pm,v 1.32 2003/08/13 12:28:43 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use base qw( Template::Plugin ); | ||
| 7 | use Class::Date qw( -DateParse ); | ||
| 8 | use Data::Dumper qw( Dumper ); | ||
| 9 | use HTML::Entities qw(); | ||
| 10 | use SPOPS::Secure qw( :level :scope ); | ||
| 11 | use SPOPS::Utility; | ||
| 12 | use Text::Sentence; | ||
| 13 | |||
| 14 | $OpenInteract::Template::Plugin::VERSION = sprintf("%d.%02d", q$Revision: 1.32 $ =~ /(\d+)\.(\d+)/); | ||
| 15 | |||
| 16 | my %SECURITY_CONSTANTS = ( | ||
| 17 | level => { | ||
| 18 | none => SEC_LEVEL_NONE, read => SEC_LEVEL_READ, write => SEC_LEVEL_WRITE | ||
| 19 | }, | ||
| 20 | scope => { | ||
| 21 | user => SEC_SCOPE_USER, group => SEC_SCOPE_GROUP, world => SEC_SCOPE_WORLD | ||
| 22 | }, | ||
| 23 | ); | ||
| 24 | |||
| 25 | use constant QUERY_ARG_SEPARATOR => '&'; | ||
| 26 | |||
| 27 | ######################################## | ||
| 28 | # PLUGIN IMPLEMENTATION | ||
| 29 | ######################################## | ||
| 30 | |||
| 31 | # Simple stub to load/create the plugin object. Since it's really just | ||
| 32 | # a way to call subroutines and doesn't maintain any state within the | ||
| 33 | # request, we can just return the same one again and again | ||
| 34 | |||
| 35 | sub load { | ||
| 36 | my ( $class, $context ) = @_; | ||
| 37 | return bless( { _CONTEXT => $context }, $class ); | ||
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | sub new { | ||
| 42 | my ( $self, $context, @params ) = @_; | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | ######################################## | ||
| 47 | # METADATA | ||
| 48 | ######################################## | ||
| 49 | |||
| 50 | sub show_all_actions { | ||
| 51 | my ( $self ) = @_; | ||
| 52 | my $class = ref $self || $self; | ||
| 53 | no strict 'refs'; | ||
| 54 | my %skip_actions = map { $_ => 1 } qw( load new Dumper ); | ||
| 55 | my @skip_initial = qw( SEC _ ); | ||
| 56 | my $src = \%{ $class . '::' }; | ||
| 57 | my @methods = (); | ||
| 58 | SYMBOL: | ||
| 59 | foreach my $symbol_name ( keys %{ $src } ) { | ||
| 60 | next SYMBOL if ( $skip_actions{ $symbol_name } ); | ||
| 61 | for ( @skip_initial ) { next SYMBOL if ( $symbol_name =~ /^$_/ ) } | ||
| 62 | push @methods, $symbol_name if ( defined *{ $src->{ $symbol_name } }{CODE} ); | ||
| 63 | } | ||
| 64 | return [ sort @methods ]; | ||
| 65 | } | ||
| 66 | |||
| 67 | |||
| 68 | ######################################## | ||
| 69 | # PLUGIN ACTIONS | ||
| 70 | ######################################## | ||
| 71 | |||
| 72 | # Stub to call the component processor | ||
| 73 | |||
| 74 | sub comp { | ||
| 75 | my ( $self, $name, @params ) = @_; | ||
| 76 | |||
| 77 | # Put the parameters in a consistent format: all unnamed | ||
| 78 | # parameters go into the key '_unnamed_' in the hashref, which | ||
| 79 | # is what is passed to the actual component. Note that Template Toolkit | ||
| 80 | # always passes the hashref of named parameters as the LAST parameter | ||
| 81 | |||
| 82 | my $p = pop @params; | ||
| 83 | $p->{_unnamed_} = \@params; | ||
| 84 | |||
| 85 | # Put the name of the component into the parameters (note: you cannot | ||
| 86 | # use 'name' as a parameter) | ||
| 87 | |||
| 88 | $p->{name} = $name; | ||
| 89 | |||
| 90 | # Pass the information to the component processor | ||
| 91 | |||
| 92 | my $R = OpenInteract::Request->instance; | ||
| 93 | $R->DEBUG && $R->scrib( 1, "Calling template component ($name)" ); | ||
| 94 | return $R->component->handler( $p ); | ||
| 95 | } | ||
| 96 | |||
| 97 | |||
| 98 | # Add the box named $box with $params | ||
| 99 | |||
| 100 | sub box_add { | ||
| 101 | my ( $self, $box, $params ) = @_; | ||
| 102 | $params ||= {}; | ||
| 103 | my $R = OpenInteract::Request->instance; | ||
| 104 | $R->DEBUG && $R->scrib( 1, "Trying to add $box with ", Dumper( $params ) ); | ||
| 105 | my %box_info = ( name => $box ); | ||
| 106 | if ( $params->{remove} ) { | ||
| 107 | $box_info{remove} = 'yes'; | ||
| 108 | delete $params->{remove}; | ||
| 109 | } | ||
| 110 | for ( qw( weight title template ) ) { | ||
| 111 | next unless ( $params->{ $_ } ); | ||
| 112 | $box_info{ $_ } = $params->{ $_ }; | ||
| 113 | delete $params->{ $_ }; | ||
| 114 | } | ||
| 115 | $box_info{params} = $params; | ||
| 116 | push @{ $R->{boxes} }, \%box_info; | ||
| 117 | return undef; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | ######################################## | ||
| 122 | # SPOPS/OBJECT INFORMATION | ||
| 123 | ######################################## | ||
| 124 | |||
| 125 | |||
| 126 | # Return a hashref of information about $obj | ||
| 127 | |||
| 128 | sub object_description { | ||
| 129 | my ( $self, $obj ) = @_; | ||
| 130 | return {} unless ( ref $obj and $obj->isa( 'SPOPS' ) ); | ||
| 131 | return $obj->object_description; | ||
| 132 | } | ||
| 133 | |||
| 134 | |||
| 135 | # Backward compatibility | ||
| 136 | |||
| 137 | sub object_info { return object_description( @_ ); } | ||
| 138 | |||
| 139 | |||
| 140 | # Wrap the call in an eval{} just in case people pass us bad data. | ||
| 141 | |||
| 142 | sub class_isa { | ||
| 143 | my ( $self, $item, $class ) = @_; | ||
| 144 | return eval { $item->isa( $class ) }; | ||
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | |||
| 149 | ######################################## | ||
| 150 | # DATES | ||
| 151 | ######################################## | ||
| 152 | |||
| 153 | sub _create_date_object { | ||
| 154 | my ( $date_string ) = @_; | ||
| 155 | return ( $date_string =~ /^(today|now)$/ ) | ||
| 156 | ? Class::Date->now() | ||
| 157 | : Class::Date->new( $date_string ); | ||
| 158 | } | ||
| 159 | |||
| 160 | |||
| 161 | # Format a date with a strftime format | ||
| 162 | |||
| 163 | sub date_format { | ||
| 164 | my ( $self, $date_string, $format, $params ) = @_; | ||
| 165 | return undef unless ( $date_string ); | ||
| 166 | $params ||= {}; | ||
| 167 | my $date = _create_date_object( $date_string ); | ||
| 168 | unless ( $date ) { | ||
| 169 | OpenInteract::Request->instance->scrib( 0, "Cannot parse ($date_string) into valid date" ); | ||
| 170 | return undef; | ||
| 171 | } | ||
| 172 | $format ||= '%Y-%m-%d %l:%M %p'; | ||
| 173 | my $formatted = $date->strftime( $format ); | ||
| 174 | if ( $params->{fill_nbsp} ) { | ||
| 175 | $formatted =~ s/\s/\ /g; | ||
| 176 | } | ||
| 177 | return $formatted; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | # Put a date into a hash with year, month, day, hour and second as | ||
| 182 | # keys. If the date is 'today' or 'now' you get back the current time. | ||
| 183 | |||
| 184 | sub date_into_object { | ||
| 185 | my ( $self, $date_string ) = @_; | ||
| 186 | return {} unless ( $date_string ); | ||
| 187 | return _create_date_object( $date_string ); | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | ######################################## | ||
| 192 | # STRING FORMATTING | ||
| 193 | ######################################## | ||
| 194 | |||
| 195 | # Limit $str to $len characters | ||
| 196 | |||
| 197 | sub limit_string { | ||
| 198 | my ( $self, $str, $len ) = @_; | ||
| 199 | my $R = OpenInteract::Request->instance; | ||
| 200 | $R->DEBUG && $R->scrib( 2, "limiting $str to $len characters" ); | ||
| 201 | return $str if ( length $str <= $len ); | ||
| 202 | return substr( $str, 0, $len ) . '...'; | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | # Quote something for use in generated Javascript code | ||
| 207 | |||
| 208 | sub javascript_quote { | ||
| 209 | my ( $self, $string ) = @_; | ||
| 210 | $string =~ s/\'/\\\'/g; | ||
| 211 | return $string; | ||
| 212 | } | ||
| 213 | |||
| 214 | # Match $match from $str, which should have parentheses in it | ||
| 215 | # somewhere so that the match will be passed out (works?) | ||
| 216 | |||
| 217 | sub regex_chunk { | ||
| 218 | my ( $self, $str, $match ) = @_; | ||
| 219 | my $R = OpenInteract::Request->instance; | ||
| 220 | $R->DEBUG && $R->scrib( 2, "Grabbing the match ($match) from string (($str))" ); | ||
| 221 | my ( $item ) = $str =~ /$match/; | ||
| 222 | $R->DEBUG && $R->scrib( 2, "Matched (($item)) from string." ); | ||
| 223 | return $item; | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | # Limit $text to $num_sentences sentences (works?) | ||
| 228 | |||
| 229 | sub limit_sentences { | ||
| 230 | my ( $self, $text, $num_sentences ) = @_; | ||
| 231 | return undef if ( ! $text ); | ||
| 232 | $num_sentences ||= 3; | ||
| 233 | my @sentences = Text::Sentence::split_sentences( $text ); | ||
| 234 | my $orig_num_sentences = scalar @sentences; | ||
| 235 | $sentences[ $num_sentences - 1 ] .= ' ...' if ( $orig_num_sentences > $num_sentences ); | ||
| 236 | return join ' ', @sentences[ 0 .. ( $num_sentences - 1 ) ]; | ||
| 237 | } | ||
| 238 | |||
| 239 | |||
| 240 | # Format $num as a percent to $places decimal places | ||
| 241 | |||
| 242 | sub percent_format { | ||
| 243 | my ( $self, $num, $places ) = @_; | ||
| 244 | $places = 2 unless ( defined $places ); | ||
| 245 | return sprintf( "%5.${places}f%%", $num * 100 ); | ||
| 246 | } | ||
| 247 | |||
| 248 | |||
| 249 | # Format $num as US currency | ||
| 250 | |||
| 251 | sub money_format { | ||
| 252 | my ( $self, $num, $places ) = @_; | ||
| 253 | $places = 2 unless ( defined $places ); | ||
| 254 | return sprintf( "\$%5.${places}f", $num ); | ||
| 255 | } | ||
| 256 | |||
| 257 | |||
| 258 | sub byte_format { | ||
| 259 | my ( $self, $num ) = @_; | ||
| 260 | my @formats = ( '%s bytes', '%5.1f KB', '%5.1f MB', '%5.1f GB' ); | ||
| 261 | my $idx = 0; | ||
| 262 | my $max = scalar @formats - 1; | ||
| 263 | while ( $num > 1024 and $idx < $max ) { | ||
| 264 | $num /= 1024; | ||
| 265 | $idx++; | ||
| 266 | } | ||
| 267 | my $fmt = sprintf( $formats[ $idx ], $num ); | ||
| 268 | $fmt =~ s/^\s+//; | ||
| 269 | return $fmt; | ||
| 270 | } | ||
| 271 | |||
| 272 | |||
| 273 | # Return the arg sent to ucfirst | ||
| 274 | |||
| 275 | sub uc_first { return ucfirst $_[1] } | ||
| 276 | |||
| 277 | |||
| 278 | # Return an HTML-encoded first argument | ||
| 279 | |||
| 280 | sub html_encode { | ||
| 281 | return HTML::Entities::encode( $_[1] ) | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | # Return an HTML-decoded first argument | ||
| 286 | |||
| 287 | sub html_decode { | ||
| 288 | return HTML::Entities::decode( $_[1] ); | ||
| 289 | } | ||
| 290 | |||
| 291 | |||
| 292 | # Create a URL, smartly. (The smart part was taken from | ||
| 293 | # Template::Plugin::URL) | ||
| 294 | |||
| 295 | sub make_url { | ||
| 296 | my ( $self, $p ) = @_; | ||
| 297 | my $url_base = $p->{base} || '/'; | ||
| 298 | delete $p->{base}; | ||
| 299 | my $R = OpenInteract::Request->instance; | ||
| 300 | if ( $R->{path}{location} ) { | ||
| 301 | $url_base = "$R->{path}{location}$url_base"; | ||
| 302 | } | ||
| 303 | my $query = join( QUERY_ARG_SEPARATOR, | ||
| 304 | map { "$_=" . _url_escape( $p->{ $_ } ) } | ||
| 305 | grep { defined $p->{ $_ } } | ||
| 306 | keys %{ $p } ); | ||
| 307 | |||
| 308 | return "$url_base?$query"; | ||
| 309 | } | ||
| 310 | |||
| 311 | sub _url_escape { | ||
| 312 | my ( $to_encode ) = shift; | ||
| 313 | return undef unless defined( $to_encode ); | ||
| 314 | $to_encode =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg; | ||
| 315 | return $to_encode; | ||
| 316 | } | ||
| 317 | |||
| 318 | |||
| 319 | ######################################## | ||
| 320 | # DATA RETRIEVAL | ||
| 321 | ######################################## | ||
| 322 | |||
| 323 | # TODO: Figure out how configure this to use a nice API to select only | ||
| 324 | # certain users (e.g., pass in a group name, group API, beginning of a | ||
| 325 | # last name, etc.. | ||
| 326 | |||
| 327 | sub get_users { | ||
| 328 | my ( $self ) = @_; | ||
| 329 | my $R = OpenInteract::Request->instance; | ||
| 330 | return eval { $R->user->fetch_iterator({ order => 'login_name' }) }; | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | ######################################## | ||
| 335 | # OI DISPLAY | ||
| 336 | ######################################## | ||
| 337 | |||
| 338 | # Tell OI (from a page) about the page title | ||
| 339 | |||
| 340 | sub page_title { | ||
| 341 | my ( $self, $title ) = @_; | ||
| 342 | my $R = OpenInteract::Request->instance; | ||
| 343 | $R->{page}{title} = $title; | ||
| 344 | $R->DEBUG && $R->scrib( 2, "Set page title to [$title]" ); | ||
| 345 | return undef; | ||
| 346 | } | ||
| 347 | |||
| 348 | # Tell OI (from a page) you want to use a different 'main' template | ||
| 349 | |||
| 350 | sub use_main_template { | ||
| 351 | my ( $self, $template_name ) = @_; | ||
| 352 | my $R = OpenInteract::Request->instance; | ||
| 353 | $R->{page}{_template_name_} = $template_name; | ||
| 354 | $R->DEBUG && $R->scrib( 2, "Set main template to [$template_name]" ); | ||
| 355 | return undef; | ||
| 356 | } | ||
| 357 | |||
| 358 | |||
| 359 | # This should return the main content template used. This isn't | ||
| 360 | # documented yet because while it probably works 90% of the time, I'm | ||
| 361 | # not sure about the other 10%. | ||
| 362 | |||
| 363 | sub content_template { | ||
| 364 | my ( $self ) = @_; | ||
| 365 | my $R = OpenInteract::Request->instance; | ||
| 366 | return $R->{templates_used}->[0]; | ||
| 367 | } | ||
| 368 | |||
| 369 | |||
| 370 | ######################################## | ||
| 371 | # PLUGIN PROPERTIES | ||
| 372 | ######################################## | ||
| 373 | |||
| 374 | sub security_level { | ||
| 375 | return $SECURITY_CONSTANTS{level}; | ||
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | sub security_scope { | ||
| 380 | return $SECURITY_CONSTANTS{scope}; | ||
| 381 | } | ||
| 382 | |||
| 383 | |||
| 384 | sub login { | ||
| 385 | return OpenInteract::Request->instance->{auth}{user}; | ||
| 386 | } | ||
| 387 | |||
| 388 | |||
| 389 | sub logged_in { | ||
| 390 | return OpenInteract::Request->instance->{auth}{logged_in} | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | sub login_group { | ||
| 395 | return OpenInteract::Request->instance->{auth}{group}; | ||
| 396 | } | ||
| 397 | |||
| 398 | |||
| 399 | sub is_admin { | ||
| 400 | return OpenInteract::Request->instance->{auth}{is_admin}; | ||
| 401 | } | ||
| 402 | |||
| 403 | |||
| 404 | sub return_url { | ||
| 405 | my $R = OpenInteract::Request->instance; | ||
| 406 | return $R->{page}{return_url} | ||
| 407 | || $R->{path}{login_fail} | ||
| 408 | || $R->{path}{original}; | ||
| 409 | } | ||
| 410 | |||
| 411 | |||
| 412 | sub theme_properties { | ||
| 413 | my $R = OpenInteract::Request->instance; | ||
| 414 | my $prop = $R->{theme_values} || $R->{theme}->all_values; | ||
| 415 | $R->{theme_values} ||= $prop; | ||
| 416 | return $prop; | ||
| 417 | } | ||
| 418 | |||
| 419 | sub theme { | ||
| 420 | return theme_properties( @_ ); | ||
| 421 | } | ||
| 422 | |||
| 423 | sub theme_fetch { | ||
| 424 | my ( $self, $theme_spec, $params ) = @_; | ||
| 425 | my ( $theme_id ); | ||
| 426 | my $R = OpenInteract::Request->instance; | ||
| 427 | if ( $theme_spec =~ /^[\d\s]+$/ ) { | ||
| 428 | $theme_id = $theme_spec; | ||
| 429 | } | ||
| 430 | else { | ||
| 431 | $theme_id = $R->CONFIG->{default_objects}{ $theme_spec }; | ||
| 432 | } | ||
| 433 | unless ( $theme_id ) { | ||
| 434 | $R->scrib( 0, "Could not fetch theme given spec of '$theme_spec': ", | ||
| 435 | "it doesn't look like an object ID and no matching ", | ||
| 436 | "no matching name found in 'default_objects' server ", | ||
| 437 | "configuration" ); | ||
| 438 | |||
| 439 | # Don't return undef or the caller will get a nasty surprise | ||
| 440 | # when he tries to use it | ||
| 441 | |||
| 442 | return $self->theme_properties; | ||
| 443 | } | ||
| 444 | my $new_theme = eval { $R->theme->fetch( $theme_id ) }; | ||
| 445 | if ( $@ ) { | ||
| 446 | $R->scrib( 0, "Error fetching theme with ID '$theme_id': $@" ); | ||
| 447 | return $self->theme_properties; | ||
| 448 | } | ||
| 449 | unless ( $new_theme ) { | ||
| 450 | $R->scrib( 0, "No theme with ID '$theme_id' exists" ); | ||
| 451 | return $self->theme_properties; | ||
| 452 | } | ||
| 453 | |||
| 454 | # New theme looks good, fill it up... | ||
| 455 | my $new_props = $new_theme->all_values; | ||
| 456 | |||
| 457 | # ... and set for rest of request if specified | ||
| 458 | if ( $params->{set_for_request} eq 'yes' ) { | ||
| 459 | $R->{theme} = $new_theme; | ||
| 460 | $R->{theme_values} = $new_props; | ||
| 461 | } | ||
| 462 | |||
| 463 | return $new_props; | ||
| 464 | } | ||
| 465 | |||
| 466 | sub error_hold { | ||
| 467 | return OpenInteract::Request->instance->{error_hold}; | ||
| 468 | } | ||
| 469 | |||
| 470 | |||
| 471 | sub session { | ||
| 472 | return \%{ OpenInteract::Request->instance->{session} }; | ||
| 473 | } | ||
| 474 | |||
| 475 | sub server_config { | ||
| 476 | return OpenInteract::Request->instance->CONFIG; | ||
| 477 | } | ||
| 478 | |||
| 479 | |||
| 480 | 1; | ||
| 481 | |||
| 482 | __END__ |