| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Jifty::View::Declare::CRUD; | ||
| 5 | use Jifty::View::Declare -base; | ||
| 6 | use Scalar::Defer 'force'; | ||
| 7 | |||
| 8 | # XXX: should register 'template type' handler, so the | ||
| 9 | # client_cache_content & the TD sub here agrees with the arguments. | ||
| 10 | use Attribute::Handlers; | ||
| 11 | my %VIEW; | ||
| 12 | sub CRUDView :ATTR(CODE,BEGIN) { | ||
| 13 | $VIEW{$_[2]}++; | ||
| 14 | } | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Jifty::View::Declare::CRUD - Provides typical CRUD views to a model | ||
| 19 | |||
| 20 | =head1 SYNOPSIS | ||
| 21 | |||
| 22 | package App::View::User; | ||
| 23 | use Jifty::View::Declare -base; | ||
| 24 | use base qw/ Jifty::View::Declare::CRUD /; | ||
| 25 | |||
| 26 | template 'view' => sub { | ||
| 27 | # customize the view | ||
| 28 | }; | ||
| 29 | |||
| 30 | 1; | ||
| 31 | |||
| 32 | package App::View::Tag; | ||
| 33 | use Jifty::View::Declare -base; | ||
| 34 | use base qw/ Jifty::View::Declare::CRUD /; | ||
| 35 | |||
| 36 | template 'view' => sub { | ||
| 37 | # customize the view | ||
| 38 | }; | ||
| 39 | |||
| 40 | 1; | ||
| 41 | |||
| 42 | package App::View; | ||
| 43 | use Jifty::View::Declare -base; | ||
| 44 | |||
| 45 | use Jifty::View::Declare::CRUD; | ||
| 46 | |||
| 47 | # If you have customizations, this is a good way... | ||
| 48 | Jifty::View::Declare::CRUD->mount_view('User'); | ||
| 49 | Jifty::View::Declare::CRUD->mount_view('Category', 'App::View::Tag', '/tag'); | ||
| 50 | |||
| 51 | # Another way to do the above, good for quick and dirty | ||
| 52 | alias Jifty::View::Declare::CRUD under '/admin/blog', { | ||
| 53 | object_type => 'BlogPost', | ||
| 54 | }; | ||
| 55 | |||
| 56 | =head1 DESCRIPTION | ||
| 57 | |||
| 58 | This class provides a set of views that may be used by a model to display | ||
| 59 | Create/Read/Update/Delete views using the L<Template::Declare> templating | ||
| 60 | language. | ||
| 61 | |||
| 62 | Basically, you can use this class to do most (and maybe all) of the work you need to manipulate and view your records. | ||
| 63 | |||
| 64 | =head1 METHODS | ||
| 65 | |||
| 66 | =begin pod_coverage | ||
| 67 | |||
| 68 | =head2 CRUDView | ||
| 69 | |||
| 70 | =end pod_coverage | ||
| 71 | |||
| 72 | =head2 mount_view MODELCLASS VIEWCLASS /path | ||
| 73 | |||
| 74 | Call this method in your application's view class to add the CRUD views you're looking for. Only the first argument is required. | ||
| 75 | |||
| 76 | Arguments: | ||
| 77 | |||
| 78 | =over | ||
| 79 | |||
| 80 | =item MODELCLASS | ||
| 81 | |||
| 82 | This is the name of the model that you want to generate the CRUD views for. This is the only required parameter. Leave off the parts of the class name prior to and including the "Model" part. (I.e., C<App::Model::User> should be passed as just C<User>). | ||
| 83 | |||
| 84 | =item VIEWCLASS | ||
| 85 | |||
| 86 | This is the name of the class that will be generated to hold the CRUD views of your model. If not given, it will be set to: C<App::View::I<MODELCLASS>>. If given, it should be the full name of the view class. | ||
| 87 | |||
| 88 | =item /path | ||
| 89 | |||
| 90 | This is the path where you can reach the CRUD views for this model in your browser. If not given, this will be set to the model class name in lowercase letters. (I.e., C<User> would be found at C</user> if not passed explicitly). | ||
| 91 | |||
| 92 | =back | ||
| 93 | |||
| 94 | =cut | ||
| 95 | |||
| 96 | sub mount_view { | ||
| 97 | my ($class, $model, $vclass, $path) = @_; | ||
| 98 | my $caller = caller(0); | ||
| 99 | |||
| 100 | # Sanitize the arguments | ||
| 101 | $model = ucfirst($model); | ||
| 102 | $vclass ||= $caller.'::'.$model; | ||
| 103 | $path ||= '/'.lc($model); | ||
| 104 | |||
| 105 | # Load the view class, alias it, and define its object_type method | ||
| 106 | Jifty::Util->require($vclass); | ||
| 107 | eval qq{package $caller; | ||
| 108 | alias $vclass under '$path'; 1} or die $@; | ||
| 109 | |||
| 110 | # Override object_type | ||
| 111 | no strict 'refs'; | ||
| 112 | my $object_type = $vclass."::object_type"; | ||
| 113 | |||
| 114 | # Avoid the override if object_type() is already defined | ||
| 115 | *{$object_type} = sub { $model } unless defined *{$object_type}; | ||
| 116 | } | ||
| 117 | |||
| 118 | # XXX TODO FIXME This is related to the trimclient branch and performs some | ||
| 119 | # magic related to that or that was once related to that. This is also related | ||
| 120 | # to the CRUDView attribute above. This is a little unfinished, but I'll leave | ||
| 121 | # it up to clkao to figure out what needs to happen here. | ||
| 122 | sub _dispatch_template { | ||
| 123 | my $class = shift; | ||
| 124 | my $code = shift; | ||
| 125 | if ($VIEW{$code} && !UNIVERSAL::isa($_[0], 'Evil')) { | ||
| 126 | my ( $object_type, $id ) = ( $class->object_type, get('id') ); | ||
| 127 | @_ = ($class, $class->_get_record($id), @_); | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | unshift @_, $class; | ||
| 131 | } | ||
| 132 | goto $code; | ||
| 133 | } | ||
| 134 | |||
| 135 | =head2 object_type | ||
| 136 | |||
| 137 | This method returns the type of object this CRUD view has been generated for. This is normally the model class parameter that was passed to L</mount_view>. | ||
| 138 | |||
| 139 | =cut | ||
| 140 | |||
| 141 | sub object_type { | ||
| 142 | my $self = shift; | ||
| 143 | my $object_type = $self->package_variable('object_type') || get('object_type'); | ||
| 144 | |||
| 145 | warn "No object type found for $self" | ||
| 146 | if !$object_type; | ||
| 147 | |||
| 148 | return $object_type; | ||
| 149 | } | ||
| 150 | |||
| 151 | =head2 record_class | ||
| 152 | |||
| 153 | This is the full name of the model class these CRUD views are for. The default implementation returns: | ||
| 154 | |||
| 155 | Jifty->app_class('Model', $self->object_type); | ||
| 156 | |||
| 157 | You will want to override this (in addition to L</object_type>) if you want to provide CRUD views in a plugin, or from an external model class, or for one of the Jifty built-in models. | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | # NB: We don't just create the record class here and return it. Why? Because | ||
| 162 | # the mount_view() method is generally called very early in the Jifty | ||
| 163 | # lifecycle. As such, Jifty->app_class() might not work yet since it requires | ||
| 164 | # the Jifty singleton to be built and the configuration to be loaded. So, this | ||
| 165 | # implementation caches the record class after the first calculation, which | ||
| 166 | # should happen during the request dispatch process, which always happens after | ||
| 167 | # Jifty is completely initialized. | ||
| 168 | sub record_class { | ||
| 169 | my $self = shift; | ||
| 170 | |||
| 171 | # If object_type is set via set, don't cache | ||
| 172 | if (!$self->package_variable('object_type') && get('object_type')) { | ||
| 173 | return Jifty->app_class('Model', $self->object_type); | ||
| 174 | } | ||
| 175 | |||
| 176 | # Otherwise, assume object_type is permanent | ||
| 177 | else { | ||
| 178 | return ($self->package_variable('record_class') | ||
| 179 | or ($self->package_variable( record_class => | ||
| 180 | Jifty->app_class('Model', $self->object_type)))); | ||
| 181 | } | ||
| 182 | |||
| 183 | } | ||
| 184 | |||
| 185 | =head2 fragment_for | ||
| 186 | |||
| 187 | This is a helper that returns the path to a given fragment. The only argument is the name of the fragment. It returns a absolute base path to the fragment page. | ||
| 188 | |||
| 189 | This will attempt to lookup a method named C<fragment_for_I<FRAGMENT>>, where I<FRAGMENT> is the argument passed. If that method exists, it's result is used as the returned path. | ||
| 190 | |||
| 191 | Otherwise, the L</fragment_base_path> is joined to the passed fragment name to create the return value. | ||
| 192 | |||
| 193 | If you really want to mess with this, you may need to read the source code of this class. | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub fragment_for { | ||
| 198 | my $self = shift; | ||
| 199 | my $fragment = shift; | ||
| 200 | |||
| 201 | # Check for fragment_for_$fragment and use that if it exists | ||
| 202 | if ( my $coderef = $self->can( 'fragment_for_' . $fragment ) ) { | ||
| 203 | return $coderef->($self); | ||
| 204 | } | ||
| 205 | |||
| 206 | # Otherwise return the fragment_base_path/$fragment | ||
| 207 | return $self->package_variable( 'fragment_for_' . $fragment ) | ||
| 208 | || $self->fragment_base_path . "/" . $fragment; | ||
| 209 | } | ||
| 210 | |||
| 211 | =head2 fragment_base_path | ||
| 212 | |||
| 213 | This is a helper for L</fragment_for>. It looks up the current template using L<Template::Declare::Tags/current_template>, finds it's parent path and then returns that. | ||
| 214 | |||
| 215 | If you really want to mess with this, you may need to read the source code of this class. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | sub fragment_base_path { | ||
| 220 | my $self = shift; | ||
| 221 | |||
| 222 | # Rip it apart | ||
| 223 | my @parts = split('/', current_template()); | ||
| 224 | |||
| 225 | # Remove the last element | ||
| 226 | pop @parts; | ||
| 227 | |||
| 228 | # Put it back together again | ||
| 229 | my $path = join('/', @parts); | ||
| 230 | |||
| 231 | # And serve | ||
| 232 | return $path; | ||
| 233 | } | ||
| 234 | |||
| 235 | =head2 _get_record $id | ||
| 236 | |||
| 237 | Given an C<$id>, returns a record object for the CRUD view's model class. | ||
| 238 | |||
| 239 | =cut | ||
| 240 | |||
| 241 | sub _get_record { | ||
| 242 | my ( $self, $id ) = @_; | ||
| 243 | |||
| 244 | # Load the model, create an empty object, load the object by ID | ||
| 245 | my $record_class = $self->record_class; | ||
| 246 | my $record = $record_class->new(); | ||
| 247 | $record->load($id); | ||
| 248 | |||
| 249 | return $record; | ||
| 250 | } | ||
| 251 | |||
| 252 | =head2 display_columns [ACTION] | ||
| 253 | |||
| 254 | Returns a list of all the column names that this REST view should | ||
| 255 | display. Defaults to all argument names for the provided C<ACTION>. | ||
| 256 | If there is no action provided, returns the C<record_class>'s | ||
| 257 | C<readable_attributes>. | ||
| 258 | |||
| 259 | =cut | ||
| 260 | |||
| 261 | sub display_columns { | ||
| 262 | my $self = shift; | ||
| 263 | my $action = shift; | ||
| 264 | |||
| 265 | return $action->argument_names if $action; | ||
| 266 | |||
| 267 | return $self->record_class->readable_attributes; | ||
| 268 | } | ||
| 269 | |||
| 270 | =head2 edit_columns ACTION | ||
| 271 | |||
| 272 | Returns a list of all the columns that this REST view should display | ||
| 273 | for update. Defaults to the L</display_columns>, without C<id>. | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | sub edit_columns { | ||
| 278 | my $self = shift; | ||
| 279 | return grep { $_ ne 'id' } $self->display_columns(@_); | ||
| 280 | } | ||
| 281 | |||
| 282 | =head2 create_columns ACTION | ||
| 283 | |||
| 284 | Returns a list of all of the columns that this REST view should | ||
| 285 | display for create. Defaults to L</edit_columns>. | ||
| 286 | |||
| 287 | =cut | ||
| 288 | |||
| 289 | sub create_columns { | ||
| 290 | my $self = shift; | ||
| 291 | return $self->edit_columns(@_); | ||
| 292 | } | ||
| 293 | |||
| 294 | =head2 render_field mode => $mode, field => $field, action => $action | ||
| 295 | |||
| 296 | Renders a particular field in a given mode (read, create, edit). This attempts | ||
| 297 | to dispatch directly to a method with the given field name. For example, if the | ||
| 298 | subclass has, say, an C<edit_field_post> method, then it will be preferred over | ||
| 299 | the generic C<edit_field> method. | ||
| 300 | |||
| 301 | =cut | ||
| 302 | |||
| 303 | sub render_field { | ||
| 304 | my $self = shift; | ||
| 305 | my %args = @_; | ||
| 306 | |||
| 307 | my $mode = $args{mode}; | ||
| 308 | my $field = $args{field}; | ||
| 309 | |||
| 310 | my $render_method = "${mode}_field"; | ||
| 311 | |||
| 312 | $render_method = "${mode}_field_${field}" | ||
| 313 | if $self->can("${mode}_field_${field}"); | ||
| 314 | |||
| 315 | $self->$render_method(%args); | ||
| 316 | } | ||
| 317 | |||
| 318 | =head2 view_field action => $action_object, field => $field_name | ||
| 319 | |||
| 320 | Displays the column as read-only. | ||
| 321 | |||
| 322 | =cut | ||
| 323 | |||
| 324 | sub view_field { | ||
| 325 | my $self = shift; | ||
| 326 | my %args = @_; | ||
| 327 | |||
| 328 | my $action = delete $args{action}; | ||
| 329 | my $field = delete $args{field}; | ||
| 330 | |||
| 331 | render_param($action => $field, render_mode => 'read', %args); | ||
| 332 | } | ||
| 333 | |||
| 334 | =head2 create_field action => $action_object, field => $field_name | ||
| 335 | |||
| 336 | Displays the column for a create form. | ||
| 337 | |||
| 338 | =cut | ||
| 339 | |||
| 340 | sub create_field { | ||
| 341 | my $self = shift; | ||
| 342 | my %args = @_; | ||
| 343 | |||
| 344 | my $action = delete $args{action}; | ||
| 345 | my $field = delete $args{field}; | ||
| 346 | |||
| 347 | render_param($action => $field, %args); | ||
| 348 | } | ||
| 349 | |||
| 350 | =head2 edit_field action => $action_object, field => $field_name | ||
| 351 | |||
| 352 | Displays the column for an edit form. | ||
| 353 | |||
| 354 | =cut | ||
| 355 | |||
| 356 | sub edit_field { | ||
| 357 | my $self = shift; | ||
| 358 | my %args = @_; | ||
| 359 | |||
| 360 | my $action = delete $args{action}; | ||
| 361 | my $field = delete $args{field}; | ||
| 362 | |||
| 363 | render_param($action => $field, %args); | ||
| 364 | } | ||
| 365 | |||
| 366 | =head2 page_title | ||
| 367 | |||
| 368 | The title for the CRUD page | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | sub page_title { | ||
| 373 | my $self = shift; | ||
| 374 | $self->object_type; | ||
| 375 | } | ||
| 376 | |||
| 377 | =head1 TEMPLATES | ||
| 378 | |||
| 379 | =head2 index.html | ||
| 380 | |||
| 381 | Contains the master form and page region containing the list of items. This is mainly a wrapper for the L</list> fragment. | ||
| 382 | |||
| 383 | =cut | ||
| 384 | |||
| 385 | template 'index.html' => page { title => shift->page_title } content { | ||
| 386 | my $self = shift; | ||
| 387 | form { | ||
| 388 | render_region( | ||
| 389 | name => $self->object_type.'-list', | ||
| 390 | path => $self->fragment_base_path.'/list' | ||
| 391 | ); | ||
| 392 | } | ||
| 393 | |||
| 394 | }; | ||
| 395 | |||
| 396 | =head2 search | ||
| 397 | |||
| 398 | The search fragment displays a search screen connected to the search action of | ||
| 399 | the module. If your subclass can C<search_fields>, then that method will be | ||
| 400 | called and the return value (which should be a list) will be used as a list of | ||
| 401 | fields to render. Otherwise, all the fields of the Search action are | ||
| 402 | displayed. | ||
| 403 | |||
| 404 | See L<Jifty::Action::Record::Search>. | ||
| 405 | |||
| 406 | =cut | ||
| 407 | |||
| 408 | template 'search' => sub { | ||
| 409 | my $self = shift; | ||
| 410 | my ($object_type) = ( $self->object_type ); | ||
| 411 | my $search = $self->record_class->as_search_action( | ||
| 412 | moniker => 'search', | ||
| 413 | sticky_on_success => 1, | ||
| 414 | ); | ||
| 415 | |||
| 416 | div { | ||
| 417 | { class is "jifty_admin crud-search-form" }; | ||
| 418 | |||
| 419 | if ( $self->can('search_fields') ) { | ||
| 420 | render_param( $search => $_ ) | ||
| 421 | for $self->search_fields; | ||
| 422 | } | ||
| 423 | else { | ||
| 424 | render_action($search); | ||
| 425 | } | ||
| 426 | |||
| 427 | $search->button( | ||
| 428 | label => _('Search'), | ||
| 429 | onclick => [ | ||
| 430 | { submit => $search }, | ||
| 431 | { | ||
| 432 | refresh => Jifty->web->current_region->parent, | ||
| 433 | args => { page => 1 }, | ||
| 434 | }, | ||
| 435 | ], | ||
| 436 | ); | ||
| 437 | } | ||
| 438 | }; | ||
| 439 | |||
| 440 | =head2 view | ||
| 441 | |||
| 442 | This fragment displays the data held by a single model record. | ||
| 443 | |||
| 444 | =cut | ||
| 445 | |||
| 446 | template 'view' => sub :CRUDView { | ||
| 447 | my $self = shift; | ||
| 448 | my $record = $self->_get_record( get('id') ); | ||
| 449 | |||
| 450 | return unless $record->id; | ||
| 451 | my $update = $record->as_update_action( | ||
| 452 | moniker => "update-" . Jifty->web->serial, | ||
| 453 | ); | ||
| 454 | |||
| 455 | my @fields = $self->display_columns($update); | ||
| 456 | for my $field (@fields) { | ||
| 457 | div { { class is 'crud-field view-argument-'.$field}; | ||
| 458 | $self->render_field( | ||
| 459 | mode => 'view', | ||
| 460 | action => $update, | ||
| 461 | field => $field, | ||
| 462 | label => '', | ||
| 463 | ); | ||
| 464 | }; | ||
| 465 | } | ||
| 466 | div { { class is 'crud-field view-item-controls'}; | ||
| 467 | show ('./view_item_controls', $record, $update); | ||
| 468 | }; | ||
| 469 | }; | ||
| 470 | |||
| 471 | =head2 private template view_item_controls | ||
| 472 | |||
| 473 | Used by the view fragment to show the edit link for each record. | ||
| 474 | |||
| 475 | =cut | ||
| 476 | |||
| 477 | private template view_item_controls => sub { | ||
| 478 | my $self = shift; | ||
| 479 | my $record = shift; | ||
| 480 | |||
| 481 | if ( $record->current_user_can('update') ) { | ||
| 482 | hyperlink( | ||
| 483 | label => _("Edit"), | ||
| 484 | class => "editlink", | ||
| 485 | onclick => { | ||
| 486 | popout => $self->fragment_for('update'), | ||
| 487 | args => { id => $record->id }, | ||
| 488 | }, | ||
| 489 | ); | ||
| 490 | } | ||
| 491 | }; | ||
| 492 | |||
| 493 | =head2 update | ||
| 494 | |||
| 495 | The update fragment displays a form for editing the data held within a single model record. | ||
| 496 | |||
| 497 | See L<Jifty::Action::Record::Update>. | ||
| 498 | |||
| 499 | =cut | ||
| 500 | |||
| 501 | template 'update' => sub { | ||
| 502 | my $self = shift; | ||
| 503 | my ( $object_type, $id ) = ( $self->object_type, get('id') ); | ||
| 504 | |||
| 505 | my $record_class = $self->record_class; | ||
| 506 | my $record = $record_class->new(); | ||
| 507 | $record->load($id); | ||
| 508 | my $update = $record->as_update_action( | ||
| 509 | moniker => "update-" . Jifty->web->serial, | ||
| 510 | ); | ||
| 511 | |||
| 512 | div { | ||
| 513 | { class is "crud-field update item " . $object_type } | ||
| 514 | |||
| 515 | show('./edit_item', $update); | ||
| 516 | show('./edit_item_controls', $record, $update); | ||
| 517 | } | ||
| 518 | }; | ||
| 519 | |||
| 520 | =head2 edit_item_controls $record $action | ||
| 521 | |||
| 522 | The controls we should be rendering in the 'edit' region for a given fragment | ||
| 523 | |||
| 524 | =cut | ||
| 525 | |||
| 526 | private template edit_item_controls => sub { | ||
| 527 | my $self = shift; | ||
| 528 | my $record = shift; | ||
| 529 | my $update = shift; | ||
| 530 | |||
| 531 | my $object_type = $self->object_type; | ||
| 532 | my $id = $record->id; | ||
| 533 | |||
| 534 | my $delete = $record->as_delete_action( | ||
| 535 | moniker => 'delete-' . Jifty->web->serial, | ||
| 536 | ); | ||
| 537 | my $view_region = Jifty->web->qualified_parent_region; | ||
| 538 | |||
| 539 | div { | ||
| 540 | { class is 'crud editlink' }; | ||
| 541 | hyperlink( | ||
| 542 | label => _("Save"), | ||
| 543 | onclick => [ | ||
| 544 | { submit => $update }, | ||
| 545 | { refresh => $view_region }, | ||
| 546 | ], | ||
| 547 | ); | ||
| 548 | if ( $record->current_user_can('delete') ) { | ||
| 549 | $delete->button( | ||
| 550 | label => _('Delete'), | ||
| 551 | onclick => [ | ||
| 552 | { | ||
| 553 | submit => $delete, | ||
| 554 | confirm => _('Really delete?'), | ||
| 555 | }, | ||
| 556 | { | ||
| 557 | region => $view_region, | ||
| 558 | replace_with => '/__jifty/empty', | ||
| 559 | }, | ||
| 560 | ], | ||
| 561 | class => 'delete', | ||
| 562 | ); | ||
| 563 | } | ||
| 564 | }; | ||
| 565 | }; | ||
| 566 | |||
| 567 | =head2 list | ||
| 568 | |||
| 569 | The list template provides an interactive list for showing a list of records in the record collection, adding new records, deleting records, and updating records. | ||
| 570 | |||
| 571 | =cut | ||
| 572 | |||
| 573 | template 'list' => sub { | ||
| 574 | my $self = shift; | ||
| 575 | |||
| 576 | my ( $page ) = get('page'); | ||
| 577 | my $item_path = get('item_path') || $self->fragment_for("view"); | ||
| 578 | my $sort_by = get ('sort_by') || ''; | ||
| 579 | my $order = get ('order') || ''; | ||
| 580 | my $collection = $self->_current_collection(); | ||
| 581 | |||
| 582 | div { | ||
| 583 | {class is 'crud-ui crud-'.$self->object_type }; | ||
| 584 | show( './search_region'); | ||
| 585 | show( './paging_top', $collection, $page ); | ||
| 586 | |||
| 587 | div { | ||
| 588 | { class is 'crud-table' }; | ||
| 589 | show( './sort_header', $item_path, $sort_by, $order ); | ||
| 590 | show( './list_items', $collection, $item_path ); | ||
| 591 | }; | ||
| 592 | |||
| 593 | show( './paging_bottom', $collection, $page ); | ||
| 594 | show( './new_item_region'); | ||
| 595 | }; | ||
| 596 | }; | ||
| 597 | |||
| 598 | =head2 per_page | ||
| 599 | |||
| 600 | This routine returns how many items should be shown on each page of a listing. | ||
| 601 | The default is 25. | ||
| 602 | |||
| 603 | =cut | ||
| 604 | |||
| 605 | sub per_page { 25 } | ||
| 606 | |||
| 607 | # This method just does a whole lot of sanitizing to try and get a valid | ||
| 608 | # collection out the other end based upon either the current search or an | ||
| 609 | # unlimited collection if there is no current search. | ||
| 610 | sub _current_collection { | ||
| 611 | my $self = shift; | ||
| 612 | my ( $page ) = get('page') || 1; | ||
| 613 | my ( $sort_by ) = get('sort_by'); | ||
| 614 | my ( $order ) = get('order'); | ||
| 615 | my $collection_class = $self->record_class->collection_class; | ||
| 616 | my $search = ( Jifty->web->response->result('search') ? Jifty->web->response->result('search')->content('search') : undef ); | ||
| 617 | my $collection; | ||
| 618 | if ( $search ) { | ||
| 619 | $collection = $search; | ||
| 620 | } elsif (my $predefined = get('predefined')) { | ||
| 621 | my ($entry) = grep { $_->{name} eq $predefined } $self->predefined_search; | ||
| 622 | $collection = force $entry->{collection} || $collection_class->new(); | ||
| 623 | for (@{$entry->{condition} || []}) { | ||
| 624 | $collection->limit(%$_); | ||
| 625 | } | ||
| 626 | } else { | ||
| 627 | $collection = $collection_class->new(); | ||
| 628 | $collection->find_all_rows(); | ||
| 629 | $collection->order_by(column => $sort_by, order=>'ASC') if ($sort_by && !$order); | ||
| 630 | $collection->order_by(column => $sort_by, order=>'DESC') if ($sort_by && $order); | ||
| 631 | } | ||
| 632 | |||
| 633 | $collection->set_page_info( current_page => $page, per_page => $self->per_page ); | ||
| 634 | |||
| 635 | return $collection; | ||
| 636 | } | ||
| 637 | |||
| 638 | =head2 sort_header | ||
| 639 | |||
| 640 | Sort by field toolbar | ||
| 641 | |||
| 642 | =cut | ||
| 643 | |||
| 644 | template 'sort_header' => sub { | ||
| 645 | my $self = shift; | ||
| 646 | my $item_path = shift; | ||
| 647 | my $sort_by = shift; | ||
| 648 | my $order = shift; | ||
| 649 | my $record_class = $self->record_class; | ||
| 650 | my $update = $record_class->as_update_action(); | ||
| 651 | |||
| 652 | div { | ||
| 653 | { class is "crud-column-headers" }; | ||
| 654 | for my $argument ($self->display_columns($update)) { | ||
| 655 | my $column = $record_class->column($argument); | ||
| 656 | unless ($column) { | ||
| 657 | |||
| 658 | # in case we want to show a field but it's not a real column | ||
| 659 | div { | ||
| 660 | { class is 'crud-column-header' }; | ||
| 661 | $argument; | ||
| 662 | }; | ||
| 663 | next; | ||
| 664 | } | ||
| 665 | |||
| 666 | div { | ||
| 667 | { class is 'crud-column-header' }; | ||
| 668 | ul { attr { class => 'crud-sort-menu', style => 'display:none;' }; | ||
| 669 | li { | ||
| 670 | my $imgdown ="<img height='16' width='16' src='/images/silk/bullet_arrow_down.png' alt='down' name='down'>"; | ||
| 671 | hyperlink( | ||
| 672 | label => $imgdown, | ||
| 673 | escape_label => 0, | ||
| 674 | onclick => | ||
| 675 | { args => { sort_by => $argument, order => undef } }, | ||
| 676 | ); | ||
| 677 | } if (!($sort_by && !$order && $argument eq $sort_by)); | ||
| 678 | li { | ||
| 679 | my $imgup ="<img height='16' width='16' src='/images/silk/bullet_arrow_up.png' alt='up' name='up'>"; | ||
| 680 | hyperlink( | ||
| 681 | label => $imgup, | ||
| 682 | escape_label => 0, | ||
| 683 | onclick => | ||
| 684 | { args => { sort_by => $argument, order => 'D' } }, | ||
| 685 | ); | ||
| 686 | } if (!($sort_by && $order && $argument eq $sort_by)); | ||
| 687 | li { | ||
| 688 | my $imgup ="<img height='16' width='16' rc='/images/silk/cancel_grey.png' alt='del' name='del'>"; | ||
| 689 | hyperlink( | ||
| 690 | label => $imgup, | ||
| 691 | escape_label => 0, | ||
| 692 | onclick => | ||
| 693 | { args => { sort_by =>'', order => '' } }, | ||
| 694 | ); | ||
| 695 | } if ($sort_by && $argument eq $sort_by); | ||
| 696 | }; | ||
| 697 | span{ | ||
| 698 | {class is "field"}; | ||
| 699 | my $label = $record_class->column($argument)->label || $argument; | ||
| 700 | if ( $sort_by && $argument eq $sort_by ) { | ||
| 701 | div { class is 'crud-sort-selected'; | ||
| 702 | hyperlink ( label =>$label ); | ||
| 703 | my $img = ($order eq 'D')?'up':'down'; | ||
| 704 | img { attr { | ||
| 705 | height => 16, | ||
| 706 | width => 16, | ||
| 707 | src => '/images/silk/bullet_arrow_'.$img.'.png' }; }; | ||
| 708 | }; | ||
| 709 | } | ||
| 710 | else { | ||
| 711 | hyperlink(label => $label); | ||
| 712 | }; | ||
| 713 | }; | ||
| 714 | } | ||
| 715 | } | ||
| 716 | }; | ||
| 717 | outs_raw("<script type=\"text/javascript\"> | ||
| 718 | jQuery(document).ready(function() { | ||
| 719 | jQuery('.crud-sort-menu').each(function(){ | ||
| 720 | jQuery(this).parent().hover( | ||
| 721 | function(){ | ||
| 722 | jQuery(this).children('.crud-sort-menu').show(); | ||
| 723 | }, | ||
| 724 | function(){ | ||
| 725 | jQuery(this).children('.crud-sort-menu').hide(); | ||
| 726 | }); | ||
| 727 | }); | ||
| 728 | }); | ||
| 729 | </script>"); | ||
| 730 | |||
| 731 | }; | ||
| 732 | |||
| 733 | |||
| 734 | use constant predefined_search => (); | ||
| 735 | |||
| 736 | =head2 predefined_search | ||
| 737 | |||
| 738 | The I<private> template makes use of the C<predefined_search> constant, which contains a list of hashref, each defines a collection in the format: | ||
| 739 | |||
| 740 | { name => 'my_list', | ||
| 741 | label => "My List", | ||
| 742 | collection => defer { | ||
| 743 | # ... construct and return the collection | ||
| 744 | } | ||
| 745 | }, | ||
| 746 | { name => 'my_list2', | ||
| 747 | label => "My List2", | ||
| 748 | condition => [ | ||
| 749 | { column => 'foo' value => 'bar' }, | ||
| 750 | # ... and your other Jifty::DBI::Collection limit args | ||
| 751 | ] | ||
| 752 | } | ||
| 753 | |||
| 754 | |||
| 755 | =cut | ||
| 756 | |||
| 757 | private template 'predefined_search' => sub { | ||
| 758 | my $self = shift; | ||
| 759 | my @predefined = $self->predefined_search or return; | ||
| 760 | |||
| 761 | ul { { class is 'predefined-search' }; | ||
| 762 | li { hyperlink( label => _("Default"), | ||
| 763 | onclick => [ { | ||
| 764 | refresh => Jifty->web->current_region, | ||
| 765 | args => { predefined => undef } | ||
| 766 | } ] ) }; | ||
| 767 | |||
| 768 | for (@predefined) { | ||
| 769 | li { | ||
| 770 | hyperlink( label => $_->{label}, | ||
| 771 | onclick => [ { | ||
| 772 | refresh => Jifty->web->current_region, | ||
| 773 | args => { predefined => $_->{name} } | ||
| 774 | } ] | ||
| 775 | ); | ||
| 776 | } | ||
| 777 | } | ||
| 778 | }; | ||
| 779 | div { { class is 'clear' } }; | ||
| 780 | }; | ||
| 781 | |||
| 782 | =head2 search_region | ||
| 783 | |||
| 784 | This I<private> template renders a region to show an expandable region for a search widget. | ||
| 785 | |||
| 786 | =cut | ||
| 787 | |||
| 788 | private template 'search_region' => sub { | ||
| 789 | my $self = shift; | ||
| 790 | my $object_type = $self->object_type; | ||
| 791 | |||
| 792 | div { | ||
| 793 | attr { class is 'crud-search' }; | ||
| 794 | |||
| 795 | show('predefined_search'); | ||
| 796 | |||
| 797 | my $search_region = Jifty::Web::PageRegion->new( | ||
| 798 | name => 'search', | ||
| 799 | path => '/__jifty/empty' | ||
| 800 | ); | ||
| 801 | |||
| 802 | hyperlink( | ||
| 803 | onclick => { | ||
| 804 | popout => $self->fragment_for('search'), | ||
| 805 | args => { object_type => $object_type } | ||
| 806 | }, | ||
| 807 | label => _('Search'), | ||
| 808 | ); | ||
| 809 | |||
| 810 | outs( $search_region->render ); | ||
| 811 | } | ||
| 812 | }; | ||
| 813 | |||
| 814 | =head2 new_item_region | ||
| 815 | |||
| 816 | This I<private> template renders a region to show a the C<new_item> template. | ||
| 817 | |||
| 818 | =cut | ||
| 819 | |||
| 820 | private template 'new_item_region' => sub { | ||
| 821 | my $self = shift; | ||
| 822 | my $fragment_for_new_item = get('fragment_for_new_item') || $self->fragment_for('new_item'); | ||
| 823 | my $object_type = $self->object_type; | ||
| 824 | |||
| 825 | return unless $self->record_class->new->current_user_can('create'); | ||
| 826 | |||
| 827 | if ($fragment_for_new_item) { | ||
| 828 | render_region( | ||
| 829 | name => 'new_item', | ||
| 830 | path => $fragment_for_new_item, | ||
| 831 | defaults => { | ||
| 832 | object_type => $object_type }, | ||
| 833 | ); | ||
| 834 | } | ||
| 835 | }; | ||
| 836 | |||
| 837 | =head2 no_items_found | ||
| 838 | |||
| 839 | Prints "No items found." | ||
| 840 | |||
| 841 | =cut | ||
| 842 | |||
| 843 | template 'no_items_found' => sub { | ||
| 844 | div { | ||
| 845 | { class is 'no_items' }; | ||
| 846 | outs( _("No items found.") ); | ||
| 847 | } | ||
| 848 | }; | ||
| 849 | |||
| 850 | =head2 list_items $collection $item_path | ||
| 851 | |||
| 852 | Renders a div of class list with a region per item. | ||
| 853 | |||
| 854 | =cut | ||
| 855 | |||
| 856 | private template 'list_items' => sub { | ||
| 857 | my $self = shift; | ||
| 858 | my $collection = shift; | ||
| 859 | my $item_path = shift; | ||
| 860 | my $callback = shift; | ||
| 861 | my $object_type = $self->object_type; | ||
| 862 | $collection->_do_search(); # we're going to need the results. | ||
| 863 | # XXX TODO, should use a real API to force the search | ||
| 864 | |||
| 865 | div { | ||
| 866 | { class is 'crud-list list' }; | ||
| 867 | if ( $collection->count == 0 ) { | ||
| 868 | render_region( | ||
| 869 | name => 'no_items_found', | ||
| 870 | path => $self->fragment_for('no_items_found'), | ||
| 871 | ); | ||
| 872 | } | ||
| 873 | |||
| 874 | my $i = 0; | ||
| 875 | while ( my $item = $collection->next ) { | ||
| 876 | |||
| 877 | render_region( | ||
| 878 | name => 'item-' . $item->id, | ||
| 879 | path => $item_path, | ||
| 880 | class => 'crud-item ' . ($i++ % 2 ? 'odd' : 'even'), | ||
| 881 | defaults => { | ||
| 882 | id => $item->id, | ||
| 883 | object_type => $object_type, | ||
| 884 | } | ||
| 885 | ); | ||
| 886 | |||
| 887 | $callback->($i) if $callback; | ||
| 888 | } | ||
| 889 | }; | ||
| 890 | }; | ||
| 891 | |||
| 892 | =head2 paging_top $collection $page_number | ||
| 893 | |||
| 894 | Paging for your list, rendered at the top of the list | ||
| 895 | |||
| 896 | =cut | ||
| 897 | |||
| 898 | private template 'paging_top' => sub { | ||
| 899 | my $self = shift; | ||
| 900 | my $collection = shift; | ||
| 901 | my $page = shift; | ||
| 902 | |||
| 903 | render_mason '/_elements/paging' => { | ||
| 904 | collection => $collection, | ||
| 905 | page => $page, | ||
| 906 | allow_all => 1, | ||
| 907 | }; | ||
| 908 | }; | ||
| 909 | |||
| 910 | =head2 paging_bottom $collection $page_number | ||
| 911 | |||
| 912 | Paging for your list, rendered at the bottom of the list | ||
| 913 | |||
| 914 | =cut | ||
| 915 | |||
| 916 | private template paging_bottom => sub { | ||
| 917 | my $self = shift; | ||
| 918 | my $collection = shift; | ||
| 919 | my $page = shift; | ||
| 920 | |||
| 921 | render_mason '/_elements/paging' => { | ||
| 922 | collection => $collection, | ||
| 923 | page => $page, | ||
| 924 | allow_all => 1, | ||
| 925 | }; | ||
| 926 | }; | ||
| 927 | |||
| 928 | |||
| 929 | =head2 create_item $action | ||
| 930 | |||
| 931 | Renders the action $Action, handing it the array ref returned by L</display_columns>. | ||
| 932 | |||
| 933 | =cut | ||
| 934 | |||
| 935 | private template 'create_item' => sub { | ||
| 936 | my $self = shift; | ||
| 937 | my $action = shift; | ||
| 938 | |||
| 939 | for my $field ($self->create_columns($action)) { | ||
| 940 | div { | ||
| 941 | { class is 'create-argument-'.$field}; | ||
| 942 | $self->render_field( | ||
| 943 | mode => 'create', | ||
| 944 | action => $action, | ||
| 945 | field => $field, | ||
| 946 | ); | ||
| 947 | } | ||
| 948 | } | ||
| 949 | }; | ||
| 950 | |||
| 951 | =head2 edit_item $action | ||
| 952 | |||
| 953 | Renders the action $Action, handing it the array ref returned by L</display_columns>. | ||
| 954 | |||
| 955 | =cut | ||
| 956 | |||
| 957 | private template 'edit_item' => sub { | ||
| 958 | my $self = shift; | ||
| 959 | my $action = shift; | ||
| 960 | for my $field ($self->edit_columns($action)) { | ||
| 961 | div { | ||
| 962 | { class is 'update-argument-'.$field}; | ||
| 963 | $self->render_field( | ||
| 964 | mode => 'edit', | ||
| 965 | action => $action, | ||
| 966 | field => $field, | ||
| 967 | ); | ||
| 968 | } | ||
| 969 | } | ||
| 970 | }; | ||
| 971 | |||
| 972 | =head1 new_item | ||
| 973 | |||
| 974 | The new_item template provides a form for creating new model records. See L<Jifty::Action::Record::Create>. | ||
| 975 | |||
| 976 | =cut | ||
| 977 | |||
| 978 | template 'new_item' => sub { | ||
| 979 | my $self = shift; | ||
| 980 | my ( $object_type ) = ( $self->object_type ); | ||
| 981 | |||
| 982 | my $record_class = $self->record_class; | ||
| 983 | my $create = $record_class->as_create_action; | ||
| 984 | |||
| 985 | div { | ||
| 986 | { class is 'crud-create crud create item inline' }; | ||
| 987 | show('./create_item', $create); | ||
| 988 | show('./new_item_controls', $create); | ||
| 989 | } | ||
| 990 | }; | ||
| 991 | |||
| 992 | private template 'new_item_controls' => sub { | ||
| 993 | my $self = shift; | ||
| 994 | my $create = shift; | ||
| 995 | my ( $object_type ) = ( $self->object_type); | ||
| 996 | |||
| 997 | outs( | ||
| 998 | Jifty->web->form->submit( | ||
| 999 | label => _('Create'), | ||
| 1000 | onclick => [ | ||
| 1001 | { submit => $create }, | ||
| 1002 | { refresh_self => 1 }, | ||
| 1003 | { delete => Jifty->web->qualified_parent_region('no_items_found') }, | ||
| 1004 | { element => Jifty->web->current_region->parent->get_element( 'div.crud-list'), | ||
| 1005 | append => $self->fragment_for('view'), | ||
| 1006 | args => { | ||
| 1007 | object_type => $object_type, | ||
| 1008 | id => { result_of => $create, name => 'id' }, | ||
| 1009 | }, | ||
| 1010 | }, | ||
| 1011 | ] | ||
| 1012 | ) | ||
| 1013 | ) | ||
| 1014 | }; | ||
| 1015 | |||
| 1016 | |||
| 1017 | |||
| 1018 | |||
| 1019 | =head1 SEE ALSO | ||
| 1020 | |||
| 1021 | L<Jifty::Action::Record::Create>, L<Jifty::Action::Record::Search>, L<Jifty::Action::Record::Update>, L<Jifty::Action::Record::Delete>, L<Template::Declare>, L<Jifty::View::Declare::Helpers>, L<Jifty::View::Declare> | ||
| 1022 | |||
| 1023 | =head1 LICENSE | ||
| 1024 | |||
| 1025 | Jifty is Copyright 2005-2010 Best Practical Solutions, LLC. | ||
| 1026 | Jifty is distributed under the same terms as Perl itself. | ||
| 1027 | |||
| 1028 | =cut | ||
| 1029 | |||
| 1030 | 1; | ||
| 1031 |