| 1 | package OpenInteract2::TT2::Plugin; | ||
| 2 | |||
| 3 | # $Id: Plugin.pm,v 1.19 2005/03/17 14:58:05 sjn Exp $ | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use base qw( Template::Plugin ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Data::Dumper qw( Dumper ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use DateTime; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use DateTime::Format::Strptime qw( strptime ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use HTML::Entities qw(); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use Log::Log4perl qw( get_logger ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use OpenInteract2::Constants qw( :log :template ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use OpenInteract2::Context qw( CTX ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 1 | ▶ | use OpenInteract2::URL; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 1 | ▶ | use SPOPS::Secure qw( :level :scope ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 0 | ▶ | use SPOPS::Utility; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 17 | |||
| 18 | $OpenInteract2::TT2::Plugin::VERSION = sprintf("%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/); | ||
| 19 | |||
| 20 | my ( $log ); | ||
| 21 | |||
| 22 | my %SECURITY_CONSTANTS = ( | ||
| 23 | level => { | ||
| 24 | none => SEC_LEVEL_NONE, read => SEC_LEVEL_READ, write => SEC_LEVEL_WRITE | ||
| 25 | }, | ||
| 26 | scope => { | ||
| 27 | user => SEC_SCOPE_USER, group => SEC_SCOPE_GROUP, world => SEC_SCOPE_WORLD | ||
| 28 | }, | ||
| 29 | ); | ||
| 30 | |||
| 31 | |||
| 32 | ######################################## | ||
| 33 | # PLUGIN IMPLEMENTATION | ||
| 34 | ######################################## | ||
| 35 | |||
| 36 | # Simple stub to load/create the plugin object. Since it's really just | ||
| 37 | # a way to call subroutines and doesn't maintain any state within the | ||
| 38 | # request, we can just return the same one again and again | ||
| 39 | |||
| 40 | sub load { | ||
| 41 | my ( $class, $context ) = @_; | ||
| 42 | return bless( { _CONTEXT => $context }, $class ); | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | sub new { | ||
| 47 | my ( $self, $context, @params ) = @_; | ||
| 48 | return $self; | ||
| 49 | } | ||
| 50 | |||
| 51 | ######################################## | ||
| 52 | # METADATA | ||
| 53 | ######################################## | ||
| 54 | |||
| 55 | sub show_all_actions { | ||
| 56 | my ( $self ) = @_; | ||
| 57 | my $class = ref $self || $self; | ||
| 58 | no strict 'refs'; | ||
| 59 | my %skip_actions = map { $_ => 1 } qw( load new Dumper ); | ||
| 60 | my @skip_initial = qw( SEC _ ); | ||
| 61 | my $src = \%{ $class . '::' }; | ||
| 62 | my @methods = (); | ||
| 63 | SYMBOL: | ||
| 64 | foreach my $symbol_name ( keys %{ $src } ) { | ||
| 65 | next SYMBOL if ( $skip_actions{ $symbol_name } ); | ||
| 66 | for ( @skip_initial ) { next SYMBOL if ( $symbol_name =~ /^$_/ ) } | ||
| 67 | push @methods, $symbol_name if ( defined *{ $src->{ $symbol_name } }{CODE} ); | ||
| 68 | } | ||
| 69 | return [ sort @methods ]; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub show_all_plugins { | ||
| 73 | my ( $self ) = @_; | ||
| 74 | my $generator = CTX->content_generator( 'TT' ); | ||
| 75 | |||
| 76 | # Yes, this is relying on a private data structure... | ||
| 77 | my %plugin_info = %{ $generator->{_plugin_class} }; | ||
| 78 | return \%plugin_info; | ||
| 79 | } | ||
| 80 | |||
| 81 | |||
| 82 | ######################################## | ||
| 83 | # PLUGIN ACTIONS | ||
| 84 | ######################################## | ||
| 85 | |||
| 86 | sub action_execute { | ||
| 87 | my ( $self, $name, $params ) = @_; | ||
| 88 | my $action = eval { CTX->lookup_action( $name ) }; | ||
| 89 | if ( $@ ) { | ||
| 90 | return "No action defined for '$name'"; | ||
| 91 | } | ||
| 92 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 93 | $params ||= {}; | ||
| 94 | $log->is_debug && | ||
| 95 | $log->debug( "Trying to assign properties/params to execute ", | ||
| 96 | "action '$name': ", join( ', ', keys %{ $params } ) ); | ||
| 97 | $action->property_assign( $params ); | ||
| 98 | $action->param_assign( $params ); | ||
| 99 | return $action->execute; | ||
| 100 | } | ||
| 101 | |||
| 102 | # L10N stuff | ||
| 103 | |||
| 104 | sub msg { | ||
| 105 | my ( $self, $key, @params ) = @_; | ||
| 106 | my $h = $self->msg_handle(); | ||
| 107 | unless ( $h ) { | ||
| 108 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 109 | $log->error( "No language handle available from the request object!" ); | ||
| 110 | return "msg $key n/a"; | ||
| 111 | } | ||
| 112 | return $h->maketext( $key, @params ); | ||
| 113 | } | ||
| 114 | |||
| 115 | sub msg_handle { | ||
| 116 | my ( $self ) = @_; | ||
| 117 | return CTX->request->language_handle; | ||
| 118 | } | ||
| 119 | |||
| 120 | # BOXES | ||
| 121 | |||
| 122 | sub box_add { | ||
| 123 | my ( $self, $box, $params ) = @_; | ||
| 124 | $params ||= {}; | ||
| 125 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 126 | $log->is_debug && $log->debug( "Template box add '$box' " ); | ||
| 127 | my %box_info = ( name => $box ); | ||
| 128 | if ( $params->{remove} ) { | ||
| 129 | return $self->box_remove( $box ); | ||
| 130 | } | ||
| 131 | |||
| 132 | # First assign all the known stuff... | ||
| 133 | |||
| 134 | for ( qw( weight title template is_template ) ) { | ||
| 135 | next unless ( $params->{ $_ } ); | ||
| 136 | $log->is_debug && | ||
| 137 | $log->debug( "Box '$box' param '$_' '$params->{$_}'" ); | ||
| 138 | $box_info{ $_ } = $params->{ $_ }; | ||
| 139 | delete $params->{ $_ }; | ||
| 140 | } | ||
| 141 | |||
| 142 | # Everything else is passed to the box as params... | ||
| 143 | |||
| 144 | $box_info{params} = $params; | ||
| 145 | eval { CTX->controller->add_box( \%box_info ) }; | ||
| 146 | if ( $@ ) { | ||
| 147 | $log->error( "Failed to add box '$box_info{name}': $@" ); | ||
| 148 | } | ||
| 149 | return undef; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub box_remove { | ||
| 153 | my ( $self, $box_name ) = @_; | ||
| 154 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 155 | $box_name =~ s/^\-//; | ||
| 156 | eval { CTX->controller->remove_box( $box_name ) }; | ||
| 157 | if ( $@ ) { | ||
| 158 | $log->error( "Failed to remove box '$box_name': $@" ); | ||
| 159 | } | ||
| 160 | return undef; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | ######################################## | ||
| 165 | # SPOPS/OBJECT INFORMATION | ||
| 166 | |||
| 167 | # Return a hashref of information about $obj | ||
| 168 | |||
| 169 | sub object_description { | ||
| 170 | my ( $self, $obj ) = @_; | ||
| 171 | return {} unless ( ref $obj and $obj->can( 'object_description' ) ); | ||
| 172 | return $obj->object_description; | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | # Backward compatibility | ||
| 177 | |||
| 178 | sub object_info { return object_description( @_ ); } | ||
| 179 | |||
| 180 | |||
| 181 | # Wrap the call in an eval{} just in case people pass us bad data. | ||
| 182 | |||
| 183 | sub class_isa { | ||
| 184 | my ( $self, $item, $class ) = @_; | ||
| 185 | return eval { $item->isa( $class ) }; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub can_read { | ||
| 190 | my ( $self, $spops_object ) = @_; | ||
| 191 | return undef unless ( $spops_object ); | ||
| 192 | return ( $spops_object->{tmp_security_level} >= SEC_LEVEL_READ ); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub can_write { | ||
| 196 | my ( $self, $spops_object ) = @_; | ||
| 197 | return undef unless ( $spops_object ); | ||
| 198 | return ( $spops_object->{tmp_security_level} >= SEC_LEVEL_WRITE ); | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | |||
| 203 | |||
| 204 | |||
| 205 | ######################################## | ||
| 206 | # DATES | ||
| 207 | |||
| 208 | sub _create_date_object { | ||
| 209 | my ( $date_string, $date_format ) = @_; | ||
| 210 | return $date_string if ( ref $date_string ); | ||
| 211 | $date_format ||= '%Y-%m-%d %H:%M'; | ||
| 212 | return ( $date_string =~ /^(today|now)$/ ) | ||
| 213 | ? DateTime->now() | ||
| 214 | : strptime( $date_format, $date_string ); | ||
| 215 | } | ||
| 216 | |||
| 217 | |||
| 218 | # Format a date with a strftime format | ||
| 219 | |||
| 220 | sub date_format { | ||
| 221 | my ( $self, $date_string, $format, $params ) = @_; | ||
| 222 | return undef unless ( $date_string ); | ||
| 223 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 224 | my $date = _create_date_object( $date_string, $format ); | ||
| 225 | unless ( $date ) { | ||
| 226 | $log->error( "Cannot parse '$date_string' into valid date" ); | ||
| 227 | return undef; | ||
| 228 | } | ||
| 229 | $format ||= '%Y-%m-%d %l:%M %p'; | ||
| 230 | my $formatted = $date->strftime( $format ); | ||
| 231 | if ( $params->{fill_nbsp} ) { | ||
| 232 | $formatted =~ s|\s|\ |g; | ||
| 233 | } | ||
| 234 | return $formatted; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | # Put a date into a hash with year, month, day, hour and second as | ||
| 239 | # keys. If the date is 'today' or 'now' you get back the current time. | ||
| 240 | |||
| 241 | sub date_into_object { | ||
| 242 | my ( $self, $date_string, $date_format ) = @_; | ||
| 243 | return {} unless ( $date_string ); | ||
| 244 | return _create_date_object( $date_string, $date_format ); | ||
| 245 | } | ||
| 246 | |||
| 247 | |||
| 248 | ######################################## | ||
| 249 | # STRING FORMATTING | ||
| 250 | |||
| 251 | sub as_boolean { | ||
| 252 | my ( $self, $value ) = @_; | ||
| 253 | return $value =~ /^(t|true|y|yes|1)$/i | ||
| 254 | } | ||
| 255 | |||
| 256 | sub as_boolean_label { | ||
| 257 | my ( $self, $value, $positive, $negative ) = @_; | ||
| 258 | my $result = $self->as_boolean( $value ); | ||
| 259 | if ( $result ) { | ||
| 260 | $positive ||= $self->msg( 'global.label.yes' ); | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | $negative ||= $self->msg( 'global.label.no' ); | ||
| 264 | } | ||
| 265 | return $result ? $positive : $negative; | ||
| 266 | } | ||
| 267 | |||
| 268 | # Limit $str to $len characters | ||
| 269 | |||
| 270 | sub limit_string { | ||
| 271 | my ( $self, $str, $len ) = @_; | ||
| 272 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 273 | $log->is_debug && | ||
| 274 | $log->debug( "limiting '$str' to '$len' characters" ); | ||
| 275 | return $str if ( length $str <= $len ); | ||
| 276 | return substr( $str, 0, $len ) . '...'; | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 280 | # Quote something for use in generated Javascript code | ||
| 281 | |||
| 282 | sub javascript_quote { | ||
| 283 | my ( $self, $string ) = @_; | ||
| 284 | $string =~ s/\'/\\\'/g; | ||
| 285 | return $string; | ||
| 286 | } | ||
| 287 | |||
| 288 | # Limit $text to $num_sentences sentences (works?) | ||
| 289 | |||
| 290 | sub limit_sentences { | ||
| 291 | my ( $self, $text, $num_sentences ) = @_; | ||
| 292 | return undef if ( ! $text ); | ||
| 293 | require Text::Sentence; | ||
| 294 | $num_sentences ||= 3; | ||
| 295 | my @sentences = Text::Sentence::split_sentences( $text ); | ||
| 296 | my $orig_num_sentences = scalar @sentences; | ||
| 297 | $sentences[ $num_sentences - 1 ] .= ' ...' if ( $orig_num_sentences > $num_sentences ); | ||
| 298 | return join ' ', @sentences[ 0 .. ( $num_sentences - 1 ) ]; | ||
| 299 | } | ||
| 300 | |||
| 301 | |||
| 302 | # Format $num as a percent to $places decimal places | ||
| 303 | |||
| 304 | sub percent_format { | ||
| 305 | my ( $self, $num, $places ) = @_; | ||
| 306 | $places = 2 unless ( defined $places ); | ||
| 307 | return sprintf( "%5.${places}f%%", $num * 100 ); | ||
| 308 | } | ||
| 309 | |||
| 310 | |||
| 311 | # Format $num as US currency -- egads, who wants to mess with Locales?! | ||
| 312 | |||
| 313 | sub money_format { | ||
| 314 | my ( $self, $num, $places ) = @_; | ||
| 315 | $places = 2 unless ( defined $places ); | ||
| 316 | return sprintf( "\$%5.${places}f", $num ); | ||
| 317 | } | ||
| 318 | |||
| 319 | |||
| 320 | sub byte_format { | ||
| 321 | my ( $self, $num ) = @_; | ||
| 322 | my @formats = ( '%s bytes', '%5.1f KB', '%5.1f MB', '%5.1f GB' ); | ||
| 323 | my $idx = 0; | ||
| 324 | my $max = scalar @formats - 1; | ||
| 325 | while ( $num > 1024 and $idx < $max ) { | ||
| 326 | $num /= 1024; | ||
| 327 | $idx++; | ||
| 328 | } | ||
| 329 | my $fmt = sprintf( $formats[ $idx ], $num ); | ||
| 330 | $fmt =~ s/^\s+//; | ||
| 331 | return $fmt; | ||
| 332 | } | ||
| 333 | |||
| 334 | |||
| 335 | # Return the arg sent to ucfirst | ||
| 336 | |||
| 337 | sub uc_first { return ucfirst $_[1] } | ||
| 338 | |||
| 339 | sub uc { return uc $_[1] } | ||
| 340 | |||
| 341 | |||
| 342 | # Return an HTML-encoded first argument | ||
| 343 | |||
| 344 | sub html_encode { | ||
| 345 | return HTML::Entities::encode( $_[1] ) | ||
| 346 | } | ||
| 347 | |||
| 348 | |||
| 349 | # Return an HTML-decoded first argument | ||
| 350 | |||
| 351 | sub html_decode { | ||
| 352 | return HTML::Entities::decode( $_[1] ); | ||
| 353 | } | ||
| 354 | |||
| 355 | |||
| 356 | # Create a URL, smartly. (The smart part was taken from | ||
| 357 | # Template::Plugin::URL, and then moved to OI::URL) | ||
| 358 | |||
| 359 | my $U = OpenInteract2::URL->new(); | ||
| 360 | |||
| 361 | sub add_params_to_url { | ||
| 362 | my ( $self, $url, $p ) = @_; | ||
| 363 | return $U->add_params_to_url( $url, $p ); | ||
| 364 | } | ||
| 365 | |||
| 366 | sub make_url { | ||
| 367 | my ( $self, $p ) = @_; | ||
| 368 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 369 | $log->is_debug && | ||
| 370 | $log->debug( "Plugin trying to create URL with: ", | ||
| 371 | join( '; ', map { "$_ = $p->{$_}" } keys %{ $p } ) ); | ||
| 372 | my ( $url_base ); | ||
| 373 | my $no_escape = $p->{do_not_escape}; | ||
| 374 | delete $p->{do_not_escape}; | ||
| 375 | if ( $p->{BASE} ) { | ||
| 376 | $url_base = $p->{BASE}; | ||
| 377 | delete $p->{ $_ } for ( qw( ACTION TASK BASE IMAGE STATIC ) ); | ||
| 378 | return $U->create( $url_base, $p, $no_escape ); | ||
| 379 | } | ||
| 380 | elsif ( $p->{ACTION} ) { | ||
| 381 | my ( $action, $task ) = ( $p->{ACTION}, $p->{TASK} ); | ||
| 382 | delete $p->{ $_ } for ( qw( ACTION TASK BASE IMAGE STATIC ) ); | ||
| 383 | return $U->create_from_action( $action, $task, $p, $no_escape ); | ||
| 384 | } | ||
| 385 | elsif ( $p->{IMAGE} ) { | ||
| 386 | my $image_url = $p->{IMAGE}; | ||
| 387 | delete $p->{ $_ } for ( qw( ACTION TASK BASE IMAGE STATIC ) ); | ||
| 388 | return $U->create_image( $image_url, $p, $no_escape ); | ||
| 389 | } | ||
| 390 | elsif ( $p->{STATIC} ) { | ||
| 391 | my $static_url = $p->{STATIC}; | ||
| 392 | delete $p->{ $_ } for ( qw( ACTION TASK BASE IMAGE STATIC ) ); | ||
| 393 | return $U->create_static( $static_url, $p, $no_escape ); | ||
| 394 | } | ||
| 395 | else { | ||
| 396 | return q{javascript:alert('Incorrect parameters passed to make_url()')}; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | |||
| 400 | |||
| 401 | ######################################## | ||
| 402 | # DATA RETRIEVAL | ||
| 403 | |||
| 404 | # TODO: Figure out how configure this to use a nice API to select only | ||
| 405 | # certain users (e.g., pass in a group name, group API, beginning of a | ||
| 406 | # last name, etc.. | ||
| 407 | |||
| 408 | sub get_users { | ||
| 409 | my ( $self ) = @_; | ||
| 410 | return eval { CTX->lookup_object( 'user' ) | ||
| 411 | ->fetch_iterator({ order => 'login_name' }) }; | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | ######################################## | ||
| 416 | # OI DISPLAY | ||
| 417 | |||
| 418 | # Tell OI (from a page) about the page title | ||
| 419 | |||
| 420 | sub page_title { | ||
| 421 | my ( $self, $title ) = @_; | ||
| 422 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 423 | eval { CTX->controller->add_content_param( title => $title ) }; | ||
| 424 | if ( $@ ) { | ||
| 425 | $log->error( "Failed to set page title: $@" ); | ||
| 426 | } | ||
| 427 | return undef; | ||
| 428 | } | ||
| 429 | |||
| 430 | # Tell OI (from a page) you want to use a different 'main' template | ||
| 431 | |||
| 432 | sub use_main_template { | ||
| 433 | my ( $self, $template_name ) = @_; | ||
| 434 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 435 | eval { CTX->controller->main_template( $template_name ) }; | ||
| 436 | if ( $@ ) { | ||
| 437 | $log->error( "Cannot set main template '$template_name' in ", | ||
| 438 | "controller: $@" ); | ||
| 439 | } | ||
| 440 | else { | ||
| 441 | $log->is_info && | ||
| 442 | $log->info( "Set main template to '$template_name'" ); | ||
| 443 | } | ||
| 444 | return undef; | ||
| 445 | } | ||
| 446 | |||
| 447 | |||
| 448 | # This should return the main content template used. This isn't | ||
| 449 | # documented yet because while it probably works 90% of the time, I'm | ||
| 450 | # not sure about the other 10%. | ||
| 451 | |||
| 452 | sub content_template { | ||
| 453 | my ( $self ) = @_; | ||
| 454 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 455 | my $template = eval { CTX->controller->main_template }; | ||
| 456 | if ( $@ ) { | ||
| 457 | $log->error( "Cannot get main_template from controller: $@" ); | ||
| 458 | } | ||
| 459 | return $template; | ||
| 460 | } | ||
| 461 | |||
| 462 | |||
| 463 | ######################################## | ||
| 464 | # PLUGIN PROPERTIES | ||
| 465 | |||
| 466 | sub security_level { | ||
| 467 | return $SECURITY_CONSTANTS{level}; | ||
| 468 | } | ||
| 469 | |||
| 470 | |||
| 471 | sub security_scope { | ||
| 472 | return $SECURITY_CONSTANTS{scope}; | ||
| 473 | } | ||
| 474 | |||
| 475 | |||
| 476 | sub action { | ||
| 477 | my ( $self ) = @_; | ||
| 478 | return $self->{_CONTEXT}->stash()->get( 'ACTION' ); | ||
| 479 | } | ||
| 480 | |||
| 481 | # Needed so we put lists in proper form for TT | ||
| 482 | |||
| 483 | sub action_param { | ||
| 484 | my ( $self, $param_name ) = @_; | ||
| 485 | my $action = $self->action; | ||
| 486 | return undef unless ( $action ); | ||
| 487 | my @params = $action->param( $param_name ); | ||
| 488 | my $num_params = scalar @params; | ||
| 489 | if ( $num_params < 2 ) { | ||
| 490 | return $params[0]; | ||
| 491 | } | ||
| 492 | return [ @params ]; | ||
| 493 | } | ||
| 494 | |||
| 495 | sub param { | ||
| 496 | my ( $self, $param_name ) = @_; | ||
| 497 | return CTX->request->param( $param_name ); | ||
| 498 | } | ||
| 499 | |||
| 500 | sub request { | ||
| 501 | return CTX->request; | ||
| 502 | } | ||
| 503 | |||
| 504 | sub response { | ||
| 505 | return CTX->response; | ||
| 506 | } | ||
| 507 | |||
| 508 | sub controller { | ||
| 509 | return CTX->controller; | ||
| 510 | } | ||
| 511 | |||
| 512 | sub login { | ||
| 513 | my ( $self ) = @_; | ||
| 514 | return CTX->request->auth_user(); | ||
| 515 | } | ||
| 516 | |||
| 517 | |||
| 518 | sub logged_in { | ||
| 519 | my ( $self ) = @_; | ||
| 520 | return CTX->request->auth_is_logged_in(); | ||
| 521 | } | ||
| 522 | |||
| 523 | |||
| 524 | sub login_group { | ||
| 525 | my ( $self ) = @_; | ||
| 526 | return CTX->request->auth_group(); | ||
| 527 | } | ||
| 528 | |||
| 529 | |||
| 530 | sub is_admin { | ||
| 531 | my ( $self ) = @_; | ||
| 532 | return CTX->request->auth_is_admin(); | ||
| 533 | } | ||
| 534 | |||
| 535 | |||
| 536 | sub return_url { | ||
| 537 | my ( $self ) = @_; | ||
| 538 | return CTX->response->return_url() || CTX->request->url_absolute(); | ||
| 539 | } | ||
| 540 | |||
| 541 | |||
| 542 | sub theme_properties { | ||
| 543 | my ( $self ) = @_; | ||
| 544 | my $request = CTX->request; | ||
| 545 | my $values = $request->theme_values; | ||
| 546 | unless ( $values ) { | ||
| 547 | $values = $request->theme_values( $request->theme->all_values ); | ||
| 548 | } | ||
| 549 | return $values; | ||
| 550 | } | ||
| 551 | |||
| 552 | |||
| 553 | # Don't return undef when things go sour or the caller will get a | ||
| 554 | # nasty surprise when he tries to use it | ||
| 555 | |||
| 556 | sub theme_fetch { | ||
| 557 | my ( $self, $theme_spec, $params ) = @_; | ||
| 558 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 559 | unless ( $theme_spec ) { | ||
| 560 | $log->warn( "No theme spec given to fetch" ); | ||
| 561 | return $self->theme_properties; | ||
| 562 | } | ||
| 563 | my ( $theme_id ); | ||
| 564 | if ( $theme_spec =~ /^[\d\s]+$/ ) { | ||
| 565 | $theme_id = $theme_spec; | ||
| 566 | } | ||
| 567 | else { | ||
| 568 | $theme_id = CTX->lookup_default_object_id( $theme_spec ); | ||
| 569 | } | ||
| 570 | unless ( $theme_id ) { | ||
| 571 | $log->error( "Could not fetch theme given spec of '$theme_spec': ", | ||
| 572 | "it doesn't look like an object ID and no matching ", | ||
| 573 | "no matching name found in 'default_objects' server ", | ||
| 574 | "configuration" ); | ||
| 575 | return $self->theme_properties; | ||
| 576 | } | ||
| 577 | my $new_theme = eval { | ||
| 578 | CTX->lookup_object( 'theme' )->fetch( $theme_id ) | ||
| 579 | }; | ||
| 580 | if ( $@ ) { | ||
| 581 | $log->error( "Error fetching theme with ID '$theme_id': $@" ); | ||
| 582 | return $self->theme_properties; | ||
| 583 | } | ||
| 584 | unless ( $new_theme ) { | ||
| 585 | $log->warn( "No theme with ID '$theme_id' exists" ); | ||
| 586 | return $self->theme_properties; | ||
| 587 | } | ||
| 588 | |||
| 589 | # New theme looks good, fill it up... | ||
| 590 | my $new_props = $new_theme->all_values; | ||
| 591 | |||
| 592 | # ... and set for rest of request if specified | ||
| 593 | if ( $params->{set_for_request} eq 'yes' ) { | ||
| 594 | CTX->request->theme( $new_theme ); | ||
| 595 | } | ||
| 596 | |||
| 597 | return $new_props; | ||
| 598 | } | ||
| 599 | |||
| 600 | |||
| 601 | sub theme { | ||
| 602 | return theme_properties( @_ ); | ||
| 603 | } | ||
| 604 | |||
| 605 | |||
| 606 | sub session { | ||
| 607 | my ( $self ) = @_; | ||
| 608 | CTX->request->session(); | ||
| 609 | } | ||
| 610 | |||
| 611 | # TODO: Should we make this available? | ||
| 612 | |||
| 613 | sub server_config { | ||
| 614 | return CTX->server_config; | ||
| 615 | } | ||
| 616 | |||
| 617 | |||
| 618 | ######################################## | ||
| 619 | # DEPRECATED | ||
| 620 | |||
| 621 | # use action_execute instead | ||
| 622 | |||
| 623 | sub comp { | ||
| 624 | my ( $self, $name, @params ) = @_; | ||
| 625 | $log ||= get_logger( LOG_TEMPLATE ); | ||
| 626 | $log->warn( "Deprecated plugin method comp() called: use ", | ||
| 627 | "action_execute() instead" ); | ||
| 628 | return $self->action_execute( $name, @params ); | ||
| 629 | } | ||
| 630 | |||
| 631 | |||
| 632 | 1; | ||
| 633 | |||
| 634 | __END__ | ||