Changeset 937

Show
Ignore:
Timestamp:
12/16/06 02:56:46 (2 years ago)
Author:
bchoate
Message:

Updates to support asset search within selection dialog; MT::Asset now provides insert options.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/wheeljack/lib/MT/App/CMS.pm

    r936 r937  
    920920        my $blog; 
    921921        unless ($blog_id) { 
    922             $blog = $blogs{$obj->blog_id} ||= MT::Blog->load($obj->blog_id, { cached_ok => 1 }); 
    923         } else { 
    924             $blog = $app->blog; 
     922            $blog = $blogs{$obj->blog_id} ||= $obj->blog; 
    925923        } 
    926924        $row->{blog_name} = $blog ? $blog->name : '-'; 
     
    997995            can_delete_files => ($blog ? $app->{perms}->can_edit_assets : $app->user->is_superuser), 
    998996            nav_assets => 1, 
     997            panel_searchable => 1, 
     998            search_prompt => $app->translate("Search Assets") . ":", 
     999            #search_type => $app->translate("Assets"), 
    9991000        }, 
    10001001    }); 
     
    54905491 
    54915492sub asset_insert { 
    5492     my ($app, %args) = @_
     5493    my $app = shift
    54935494 
    54945495    # Just insert text if there are arguments, unless one is 
    54955496    # 'is_image' otherwise we are definitely uploading. 
    5496     my $text = !keys %args || $args{is_image} 
    5497         ? $app->_process_post_upload() 
    5498         : $app->asset_insert_text($args{asset_id}); 
     5497    my $text = $app->_process_post_upload(); 
    54995498    return unless defined $text; 
    55005499 
    55015500    $app->build_page('asset_insert.tmpl', { 
    5502             upload_html => $text
    5503             edit_field => $app->param('edit_field')
     5501            upload_html => $text || ''
     5502            edit_field => scalar $app->param('edit_field') || ''
    55045503        }, 
    55055504    ); 
     
    55125511    defined(my $text = $app->_process_post_upload) or return; 
    55135512    $q->param('text', $text); 
     5513    # strip any asset id 
     5514    $q->param('id', 0); 
    55145515    $app->edit_object; 
    55155516} 
     
    55175518sub _process_post_upload { 
    55185519    my $app = shift; 
     5520    my %param = $app->param_hash; 
     5521    my $asset; 
     5522    require MT::Asset; 
     5523    $param{id} && ($asset = MT::Asset->load($param{id})) 
     5524        or return $app->errtrans("Invalid request."); 
     5525    $asset->on_upload(\%param); 
     5526    return $app->asset_insert_text(\%param); 
     5527} 
     5528 
     5529sub asset_insert_text { 
     5530    my $app = shift; 
     5531    my ($param) = @_; 
    55195532    my $q = $app->param; 
    5520     my($url, $width, $height) = map $q->param($_), qw( url width height ); 
    5521     my ($base_url, $fname) = $url =~ m|(.*)/([^/]*)|; 
    5522     $url = $base_url . '/' . $fname; # no need to re-encode filename; url is already encoded 
    5523     my $blog_id = $q->param('blog_id'); 
    5524     require MT::Blog; 
    5525     my $blog = MT::Blog->load($blog_id, {cached_ok=>1}); 
    5526     my($thumb, $thumb_width, $thumb_height); 
    5527     # Save new defaults if requested. 
    5528     if($q->param('image_defaults')) { 
    5529         return $app->error($app->translate( 
    5530             'Permission denied setting image defaults for blog #[_2]', $blog_id 
    5531         )) unless $app->{perms}->can_save_image_defaults; 
    5532         $blog->image_default_set(1); 
    5533         $blog->image_default_wrap_text($q->param('wrap_text') ? 1 : 0); 
    5534         $blog->image_default_align($q->param('align') || MT::Blog::ALIGN()); 
    5535         $blog->image_default_thumb($q->param('thumb') ? 1 : 0); 
    5536         $blog->image_default_width($q->param('thumb_width') || MT::Blog::WIDTH()); 
    5537         $blog->image_default_wunits($q->param('thumb_width_type') || MT::Blog::UNITS()); 
    5538         $blog->image_default_height($q->param('thumb_height') || MT::Blog::WIDTH()); 
    5539         $blog->image_default_hunits($q->param('thumb_height_type') || MT::Blog::UNITS()); 
    5540         $blog->image_default_constrain($q->param('constrain') ? 1 : 0); 
    5541         $blog->image_default_popup($q->param('popup') ? 1 : 0); 
    5542     } 
    5543     else { 
    5544         $blog->image_default_set(0); 
    5545         $blog->image_default_wrap_text(0); 
    5546         $blog->image_default_align(MT::Blog::ALIGN()); 
    5547         $blog->image_default_thumb(0); 
    5548         $blog->image_default_width(0); 
    5549         $blog->image_default_wunits(MT::Blog::UNITS()); 
    5550         $blog->image_default_height(0); 
    5551         $blog->image_default_hunits(MT::Blog::UNITS()); 
    5552         $blog->image_default_constrain(1); 
    5553         $blog->image_default_popup(0); 
    5554     } 
    5555     $blog->save; 
    5556     if ($thumb = $q->param('thumb')) { 
    5557         require MT::Image; 
    5558         my $base_path = $q->param('site_path') ? 
    5559             $blog->site_path : $blog->archive_path; 
    5560         my $file = $q->param('fname'); 
    5561         if ($file =~ m!\.\.|\0|\|!) { 
    5562             return $app->error($app->translate("Invalid filename '[_1]'", $file)); 
    5563         } 
    5564         my $i_file = File::Spec->catfile($base_path, $file); 
    5565         ## Untaint. We checked $file for security holes above. 
    5566         ($i_file) = $i_file =~ /(.+)/s; 
    5567         my $fmgr = $blog->file_mgr; 
    5568         my $data = $fmgr->get_data($i_file, 'upload') 
    5569             or return $app->error($app->translate( 
    5570                 "Reading '[_1]' failed: [_2]", $i_file, $fmgr->errstr)); 
    5571         my $image_type = scalar $q->param('image_type'); 
    5572         my $img = MT::Image->new( Data => $data, 
    5573                                   Type => $image_type ) 
    5574             or return $app->error($app->translate( 
    5575                 "Thumbnail failed: [_1]", MT::Image->errstr)); 
    5576         my($w, $h) = map $q->param($_), qw( thumb_width thumb_height ); 
    5577         (my($blob), $thumb_width, $thumb_height) = 
    5578             $img->scale( Width => $w, Height => $h ) 
    5579             or return $app->error($app->translate("Thumbnail failed: [_1]", 
    5580                 $img->errstr)); 
    5581         require File::Basename; 
    5582         my($base, $path, $ext) = File::Basename::fileparse($i_file, '\.[^.]*'); 
    5583         my $t_file = $path . $base . '-thumb' . $ext; 
    5584         my $basename = $base . '-thumb' . $ext; 
    5585         $fmgr->put_data($blob, $t_file, 'upload') 
    5586             or return $app->error($app->translate( 
    5587                 "Error writing to '[_1]': [_2]", $t_file, $fmgr->errstr)); 
    5588  
    5589         $file =~ s/\Q$base$ext\E$//; 
    5590         my $url = $q->param('site_path') ? $blog->site_url : $blog->archive_url; 
    5591         $url .= '/' unless $url =~ m!/$!; 
    5592         $url .= $file; 
    5593         $thumb = $url . encode_url($base . '-thumb' . $ext); 
    5594  
    5595         require MT::Asset; 
    5596         my $img_pkg = MT::Asset->class_handler('image'); 
    5597         my $asset = new $img_pkg; 
    5598         my $original = $asset->clone; 
    5599         $asset->blog_id($blog_id); 
    5600         #$asset->url($thumb); 
    5601         $asset->file_path($t_file); 
    5602         $asset->file_name($basename); 
    5603         my $ext2 = $ext; 
    5604         $ext2 =~ s/^\.//; 
    5605         $asset->file_ext($ext2); 
    5606         $asset->image_width($thumb_width); 
    5607         $asset->image_height($thumb_height); 
    5608         $asset->created_by($app->user->id); 
    5609         $asset->save; 
    5610         MT->run_callbacks('CMSPostSave.asset', $app, $asset, $original); 
    5611  
    5612         $app->param('thumb_asset_id' => $asset->id); 
    5613  
    5614         MT->run_callbacks('CMSUploadFile', 
    5615                           File => $t_file, Url => $thumb, Size => length($blob), 
    5616                           Asset => $asset, 
    5617                           Type => 'thumbnail', 
    5618                           Blog => $blog); 
    5619  
    5620         MT->run_callbacks('CMSUploadImage', 
    5621                           File => $t_file, Url => $thumb, 
    5622                           Asset => $asset, 
    5623                           Width => $thumb_width, Height => $thumb_height, 
    5624                           ImageType => $image_type, 
    5625                           Size => length($blob), 
    5626                           Type => 'thumbnail', 
    5627                           Blog => $blog); 
    5628     } 
    5629     if ($q->param('popup')) { 
    5630         require MT::Template; 
    5631         if (my $tmpl = MT::Template->load({ blog_id => $blog_id, 
    5632                                             type => 'popup_image' })) { 
    5633             (my $rel_path = $q->param('fname')) =~ s!\.[^.]*$!!; 
    5634             if ($rel_path =~ m!\.\.|\0|\|!) { 
    5635                 return $app->error($app->translate( 
    5636                     "Invalid basename '[_1]'", $rel_path)); 
    5637             } 
    5638             my $ext = $blog->file_extension || ''; 
    5639             $ext = '.' . $ext if $ext ne ''; 
    5640             require MT::Template::Context; 
    5641             my $ctx = MT::Template::Context->new; 
    5642             $ctx->stash('image_url', $url); 
    5643             $ctx->stash('image_width', $width); 
    5644             $ctx->stash('image_height', $height); 
    5645             my $popup = $tmpl->build($ctx); 
    5646             my $fmgr = $blog->file_mgr; 
    5647             my $root_path = $q->param('site_path') ? 
    5648                 $blog->site_path : $blog->archive_path; 
    5649             my $abs_file_path = File::Spec->catfile($root_path, $rel_path . $ext); 
    5650  
    5651             ## If the popup filename already exists, we don't want to overwrite 
    5652             ## it, because it could contain valuable data; so we'll just make 
    5653             ## sure to generate the name uniquely. 
    5654             my($i, $rel_path_ext) = (0, $rel_path . $ext); 
    5655             while ($fmgr->exists($abs_file_path)) { 
    5656                 $rel_path_ext = $rel_path . ++$i . $ext; 
    5657                 $abs_file_path = File::Spec->catfile($root_path, $rel_path_ext); 
    5658             } 
    5659             my ($vol, $dirs, $basename) = File::Spec->splitpath($rel_path_ext); 
    5660             my $rel_url_ext = File::Spec->catpath($vol, $dirs, encode_url($basename)); 
    5661   
    5662             ## Untaint. We have checked for security holes above, so we 
    5663             ## should be safe. 
    5664             ($abs_file_path) = $abs_file_path =~ /(.+)/s; 
    5665             $fmgr->put_data($popup, $abs_file_path, 'upload') 
    5666                 or return $app->error($app->translate( 
    5667                    "Error writing to '[_1]': [_2]", $abs_file_path, 
    5668                                                      $fmgr->errstr)); 
    5669             $url = $q->param('site_path') ? 
    5670                 $blog->site_url : $blog->archive_url; 
    5671             $url .= '/' unless $url =~ m!/$!; 
    5672             $rel_url_ext =~ s!^/!!; 
    5673             $url .= $rel_url_ext; 
    5674  
    5675             my $img_pkg = MT::Asset->class_handler('image'); 
    5676             my $asset = new $img_pkg; 
    5677             my $original = $asset->clone; 
    5678             $asset->blog_id($blog_id); 
    5679             $asset->url($url); 
    5680             $asset->file_path($abs_file_path); 
    5681             $asset->file_name($basename); 
    5682             $asset->file_ext($blog->file_extension); 
    5683             $asset->created_by($app->user->id); 
    5684             $asset->save; 
    5685             MT->run_callbacks('CMSPostSave.asset', $app, $asset, $original); 
    5686  
    5687             MT->run_callbacks('CMSUploadFile', 
    5688                           File => $abs_file_path, Url => $url, 
    5689                           Asset => $asset, 
    5690                           Size => length($popup), 
    5691                           Type => 'popup', 
    5692                           Blog => $blog); 
    5693         } 
    5694     } 
    5695  
    5696     return $app->asset_insert_text(); 
    5697 
    5698  
    5699 sub asset_insert_text { 
    5700     my $app = shift; 
    5701     my $q = $app->param; 
    5702     my $id = shift || $q->param('id'); 
     5533    my $id = $app->param('id') 
     5534        or return $app->errtrans("Invalid request."); 
    57035535    require MT::Asset; 
    5704     my $asset = MT::Asset->load($id) || 
    5705         return $app->errtrans("Can't load asset, $id."); 
    5706     my $text = $asset->as_html($q); 
    5707     return $q->param('popup') || $q->param('link') 
    5708         ? $app->translate_templatized($text) 
    5709         : $text; 
     5536    my $asset = MT::Asset->load($id) 
     5537        or return $app->errtrans("Can't load asset #[_1].", $id); 
     5538    return $asset->as_html($param); 
    57105539} 
    57115540 
     
    92039032 
    92049033sub complete_insert { 
    9205     my ($app, %args) = @_; 
     9034    my $app = shift; 
     9035    my (%args) = @_; 
    92069036 
    92079037    my $asset = $args{asset}; 
     
    92099039        require MT::Asset; 
    92109040        $asset = MT::Asset->load($app->param('id')) || 
    9211             return $app->errtrans("Can't load asset, ". $app->param('id') .'.'); 
    9212     } 
    9213     return $app->errtrans('No asset to upload.') unless $asset; 
     9041            return $app->errtrans("Can't load asset #[_1].", $app->param('id')); 
     9042    } 
     9043    return $app->errtrans('Invalid request.') unless $asset; 
    92149044 
    92159045    $args{is_image} = $asset->isa('MT::Asset::Image') ? 1 : 0 
     
    92179047 
    92189048    require MT::Blog; 
    9219     my $blog = $args{blog} || MT::Blog->load($app->param('blog_id')) || 
    9220         return $app->errtrans("Can't load blog, ". $app->param('blog_id') .'.'); 
    9221     my $perms = $args{perms} || $app->{perms} || 
     9049    my $blog = $asset->blog or 
     9050        return $app->errtrans("Can't load blog #[_1].", $app->param('blog_id') ); 
     9051    my $perms = $app->{perms} or 
    92229052        return $app->errtrans('No permissions'); 
    92239053 
    9224     if ($args{is_image}) { 
    9225         my $param = { 
    9226             asset_id => $asset->id, 
    9227             bytes => $args{bytes}, 
    9228             direct_asset_insert => scalar $app->param('direct_asset_insert'), 
    9229             edit_field => scalar $app->param('edit_field'), 
    9230             entry_insert => scalar $app->param('entry_insert'), 
    9231             fname => $asset->file_name, 
    9232             height => $asset->image_height, 
    9233             is_image => $args{is_image}, 
    9234             site_path => scalar $app->param('site_path'), 
    9235             url => $asset->url, 
    9236             width => $asset->image_width, 
    9237         }; 
    9238  
    9239         eval { require MT::Image; MT::Image->new or die; }; 
    9240         $param->{do_thumb} = $@ ? 0 : 1; 
    9241  
    9242         $param->{can_save_image_defaults} = $perms->can_save_image_defaults ? 1 : 0; 
    9243         $param->{constrain} = $blog->image_default_constrain ? 1 : 0; 
    9244         $param->{popup} = $blog->image_default_popup ? 1 : 0; 
    9245         $param->{image_defaults} = $blog->image_default_set ? 1 : 0; 
    9246         $param->{wrap_text} = $blog->image_default_wrap_text ? 1 : 0; 
    9247         $param->{make_thumb} = $blog->image_default_thumb ? 1 : 0; 
    9248         $param->{'align_'.$_} = $blog->image_default_align eq $_ ? 1 : 0 for qw(left center right); 
    9249         $param->{'unit_w'.$_} = $blog->image_default_wunits eq $_ ? 1 : 0 for qw(percent pixels); 
    9250         $param->{'unit_h'.$_} = $blog->image_default_hunits eq $_ ? 1 : 0 for qw(percent pixels); 
    9251         $param->{thumb_width} = $blog->image_default_width || $asset->image_width || 0; 
    9252         $param->{thumb_height} = $blog->image_default_height || $asset->image_height || 0; 
    9253  
    9254         $app->build_page('upload_complete.tmpl', $param); 
    9255     } 
    9256     else { 
    9257         $app->asset_insert( 
    9258             asset_id => $asset->id, 
    9259             is_image => $args{is_image}, 
    9260         ); 
    9261     } 
     9054    my $param = { 
     9055        asset_id => $asset->id, 
     9056        bytes => $args{bytes}, 
     9057        direct_asset_insert => scalar $app->param('direct_asset_insert') || 0, 
     9058        edit_field => scalar $app->param('edit_field') || '', 
     9059        entry_insert => scalar $app->param('entry_insert') || 0, 
     9060        fname => $asset->file_name, 
     9061        height => $asset->image_height, 
     9062        is_image => $args{is_image} || 0, 
     9063        site_path => scalar $app->param('site_path') || '', 
     9064        url => $asset->url, 
     9065        width => $asset->image_width, 
     9066    }; 
     9067    my $html = $asset->insert_options($param); 
     9068    unless ($html) { 
     9069        return $app->asset_insert(); 
     9070    } 
     9071    $param->{options_snippet} = $html; 
     9072    $app->build_page('upload_complete.tmpl', $param); 
    92629073} 
    92639074 
     
    94829293 
    94839294    if ($is_image) { 
    9484         MT->run_callbacks('CMSUploadFile'
     9295        MT->run_callbacks('CMSUploadFile.' . $asset->class
    94859296                          File => $local_file, Url => $url, Size => $bytes, 
    94869297                          Asset => $asset, 
     
    94959306                          Blog => $blog); 
    94969307    } else { 
    9497         MT->run_callbacks('CMSUploadFile'
     9308        MT->run_callbacks('CMSUploadFile.' . $asset->class
    94989309                          File => $local_file, Url => $url, Size => $bytes, 
    94999310                          Asset => $asset, 
     
    95049315    $app->complete_insert( 
    95059316        asset => $asset, 
    9506         blog => $blog, 
    95079317        bytes => $bytes, 
    9508         perms => $perms, 
    95099318    ); 
    95109319} 
     
    95339342    my $res = $app->do_search_replace(@_); 
    95349343    $app->add_breadcrumb($app->translate('Search & Replace')); 
     9344    $res->{nav_search} = 1; 
    95359345    $app->build_page('search_replace.tmpl', $res); 
    95369346} 
     
    95539363 
    95549364    my $search_api = { 
     9365        'asset' => { 
     9366            'perm_check' => sub { 
     9367                1; 
     9368            }, 
     9369            'search_cols' => [ qw(file_name description) ], 
     9370            'replace_cols' => [], 
     9371            'can_replace' => 0, 
     9372            'can_search_by_date' => 1, 
     9373        }, 
    95559374        'entry' => { 
    95569375            'perm_check' => sub { 
     
    98809699    if (@data) { 
    98819700        my $meth = 'build_' . $type . '_table'; 
    9882         $app->$meth( items => \@data, param => \%param ); 
     9701        if ($app->can($meth)) { 
     9702            $app->$meth( items => \@data, param => \%param ); 
     9703        } else { 
     9704            my @objects; 
     9705            push @objects, { object => $_ } for @data; 
     9706            $param{object_loop} = \@objects; 
     9707        } 
    98839708    } 
    98849709    if ($is_dateranged) { 
     
    99229747    $res{'tab_junk'} = 1 if $is_junk; 
    99239748    $res{'search_cols_' . $_} = 1 foreach @cols; 
    9924     $res{nav_search} = 1; 
    99259749    \%res; 
    99269750} 
  • branches/wheeljack/lib/MT/Asset.pm

    r935 r937  
    189189        my $blog_id = $asset->blog_id or return undef; 
    190190        require MT::Blog; 
    191         MT::Blog->load($blog_id, { cached_ok => 1 }); 
     191        MT::Blog->load($blog_id, { cached_ok => 1 }) 
     192            or return $asset->error("Failed to load blog for asset"); 
    192193    }); 
    193194} 
     
    199200    # undef is returned from fileparse if the extension is not known. 
    200201    require File::Basename; 
    201     return (File::Basename::fileparse($filename, @{ $pkg->extensions }))[2] ? 1 : 0; 
     202    my $ext = $pkg->extensions || []; 
     203    return (File::Basename::fileparse($filename, @$ext))[2] ? 1 : 0; 
    202204} 
    203205 
     
    258260 
    259261sub as_html { 
    260     my ($self, $q) = @_; 
    261     (my $fname = $self->file_name) =~ s/'/\\'/g; 
    262     my $text = sprintf '<a href="%s">%s</a>', $self->url, $fname; 
     262    my $asset = shift; 
     263    my ($param) = @_; 
     264    my $fname = $asset->file_name; 
     265    require MT::Util; 
     266    my $text = sprintf '<a href="%s">%s</a>', 
     267        MT::Util::encode_html($asset->url), 
     268        MT::Util::encode_html($fname); 
    263269    return $text; 
     270} 
     271 
     272# Return a HTML snippet of form options for inserting this asset 
     273# into a web page. Default behavior is no options. 
     274sub insert_options { 
     275    my $asset = shift; 
     276    my ($param) = @_; 
     277    return undef; 
     278} 
     279 
     280sub on_upload { 
     281    my $asset = shift; 
     282    my ($param) = @_; 
     283    1; 
    264284} 
    265285 
  • branches/wheeljack/lib/MT/Asset/Image.pm

    r928 r937  
    77package MT::Asset::Image; 
    88 
     9use strict; 
    910use base 'MT::Asset'; 
    1011 
     
    3940    my $file = $asset->file_name; 
    4041    $file =~ s!\.[a-z]+$!!i; 
    41     $thumbnail = File::Spec->catfile($path, $file . '-thumb-' . $h . 'x' . $w . '.' . $asset->file_ext); 
     42    my $thumbnail = File::Spec->catfile($path, $file . '-thumb-' . $h . 'x' . $w . '.' . $asset->file_ext); 
    4243    my @thumbinfo = stat($thumbnail); 
    4344 
     
    4849 
    4950    # stale or non-existent thumbnail. let's create one! 
    50     my $blog = $param{Blog}; 
    51     $blog ||= MT::Blog->load($asset->blog_id, { cached_ok => 1 }); 
     51    my $blog = $param{Blog} || $asset->blog; 
    5252    return undef unless $blog; 
    5353    my $fmgr = $blog->file_mgr; 
     
    103103 
    104104sub as_html { 
    105     my ($self, $q) = @_; 
     105    my $asset = shift; 
     106    my ($param) = @_; 
    106107 
    107108    my $text = ''; 
    108109 
    109     (my $fname = $self->file_name) =~ s/'/\\'/g; 
     110    my $fname = $asset->file_name; 
     111    require MT::Util; 
    110112 
    111113    my $thumb = undef; 
    112     if ($q->param('thumb')) { 
    113         $thumb = MT::Asset->load($q->param('thumb_asset_id')) || 
    114             return MT::App->errtrans( 
    115                 "Can't load asset, ". $q->param('thumb_asset_id') .'.' 
     114    if ($param->{thumb}) { 
     115        $thumb = MT::Asset->load($param->{thumb_asset_id}) || 
     116            return $asset->error( 
     117                MT->translate("Can't load asset #[_1]", 
     118                    $param->{thumb_asset_id}) 
    116119            ); 
    117120    } 
    118121 
    119     if ($q->param('popup')) { 
     122    if ($param->{popup}) { 
     123        my $dimensions = ''; 
     124        if ($thumb->image_width && $thumb->image_height) { 
     125            $dimensions = sprintf('width="%s" height="%s"', 
     126                $thumb->image_width, $thumb->image_height); 
     127        } 
    120128        my $link = $thumb 
    121             ? sprintf('<img src="%s" width="%d" height="%d" alt="%s" />', 
    122                 $thumb->url, $thumb->image_width, $thumb->image_height, $fname, 
     129            ? sprintf('<img src="%s" %s alt="%s" />', 
     130                MT::Util::encode_html($thumb->url), 
     131                $dimensions, 
     132                MT::Util::encode_html($fname), 
    123133              ) 
    124             : '<MT_TRANS phrase="View image">'
     134            : MT->translate('View image')
    125135        $text = sprintf( 
    126136            q|<a href="%s" onclick="window.open('%s','popup','width=%d,height=%d,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false">%s</a>|, 
    127             $self->url, $self->url, $self->image_width, $self->image_height, $link, 
     137            MT::Util::encode_html($asset->url), 
     138            MT::Util::encode_html($asset->url), 
     139            $asset->image_width, $asset->image_height, $link, 
    128140        ); 
    129     } 
    130     elsif ($q->param('include')) { 
    131         my $wrap_style = $q->param('wrap_text') && $q->param('align') 
    132             ? 'class="display_img_' . $q->param('align') . '" ' : ''; 
    133         if ($q->param('thumb')) { 
     141    } elsif ($param->{include}) { 
     142        my $wrap_style = $param->{wrap_text} && $param->{align} 
     143            ? 'class="display_img_' . $param->{align} . '" ' : ''; 
     144        if ($param->{thumb}) { 
     145            my $dimensions = ''; 
     146            if ($thumb->image_width && $thumb->image_height) { 
     147                $dimensions = sprintf('width="%s" height="%s"', 
     148                    $thumb->image_width, $thumb->image_height); 
     149            } 
    134150            $text = sprintf( 
    135                 '<a href="%s"><img alt="%s" src="%s" width="%d" height="%d" %s/></a>', 
    136                 $self->url, $fname, $thumb->url, $thumb->image_width, $thumb->image_height, $wrap_style, 
     151                '<a href="%s"><img alt="%s" src="%s" %s %s/></a>', 
     152                MT::Util::encode_html($asset->url), 
     153                MT::Util::encode_html($fname), 
     154                MT::Util::encode_html($thumb->url), 
     155                $dimensions, $wrap_style, 
    137156            ); 
    138         } 
    139         else { 
     157        } else { 
     158            my $dimensions = ''; 
     159            if ($asset->image_width && $asset->image_height) { 
     160                $dimensions = sprintf('width="%s" height="%s"', 
     161                    $asset->image_width, $asset->image_height); 
     162            } 
    140163            $text = sprintf( 
    141                 '<img alt="%s" src="%s" width="%d" height="%d" %s/>', 
    142                 $fname, $self->url, $self->image_width, $self->image_height, $wrap_style, 
     164                '<img alt="%s" src="%s" %s %s/>', 
     165                MT::Util::encode_html($fname), 
     166                MT::Util::encode_html($asset->url), 
     167                $dimensions, $wrap_style, 
    143168            ); 
    144169        } 
    145     } 
    146     elsif ($q->param('link')) { 
     170    } else { 
    147171        $text = sprintf( 
    148             '<a href="%s"><MT_TRANS phrase="Download file"></a>', $self->url, 
     172            '<a href="%s">' . MT->translate('Download file') . '</a>', 
     173            MT::Util::encode_html($asset->url), 
    149174        ); 
    150175    } 
    151176 
    152177    return $text; 
     178} 
     179 
     180# Return a HTML snippet of form options for inserting this asset 
     181# into a web page. Default behavior is no options. 
     182sub insert_options { 
     183    my $asset = shift; 
     184    my ($param) = @_; 
     185 
     186    my $app = MT->instance; 
     187    my $perms = $app->{perms}; 
     188    my $blog = $asset->blog or return; 
     189 
     190    eval { require MT::Image; MT::Image->new or die; }; 
     191    $param->{do_thumb} = $@ ? 0 : 1; 
     192 
     193    $param->{can_save_image_defaults} = $perms->can_save_image_defaults ? 1 : 0; 
     194    $param->{constrain} = $blog->image_default_constrain ? 1 : 0; 
     195    $param->{popup} = $blog->image_default_popup ? 1 : 0; 
     196    $param->{image_defaults} = $blog->image_default_set ? 1 : 0; 
     197    $param->{wrap_text} = $blog->image_default_wrap_text ? 1 : 0; 
     198    $param->{make_thumb} = $blog->image_default_thumb ? 1 : 0; 
     199    $param->{'align_'.$_} = $blog->image_default_align eq $_ ? 1 : 0 
     200        for qw(left center right); 
     201    $param->{'unit_w'.$_} = $blog->image_default_wunits eq $_ ? 1 : 0 
     202        for qw(percent pixels); 
     203    $param->{'unit_h'.$_} = $blog->image_default_hunits eq $_ ? 1 : 0 
     204        for qw(percent pixels); 
     205    $param->{thumb_width} = $blog->image_default_width || $asset->image_width || 0; 
     206    $param->{thumb_height} = $blog->image_default_height || $asset->image_height || 0; 
     207 
     208    return $app->build_page('asset_image_options.tmpl', $param); 
     209} 
     210 
     211sub on_upload { 
     212    my $asset = shift; 
     213    my ($param) = @_; 
     214 
     215    my $app = MT->instance; 
     216    require MT::Util; 
     217 
     218    my $url = $asset->url; 
     219    my $width = $asset->image_width; 
     220    my $height = $asset->image_height; 
     221 
     222    my ($base_url, $fname) = $url =~ m|(.*)/([^/]*)|; 
     223    $url = $base_url . '/' . $fname; # no need to re-encode filename; url is already encoded 
     224    my $blog = $asset->blog or return; 
     225    my $blog_id = $blog->id; 
     226 
     227    my($thumb, $thumb_width, $thumb_height); 
     228    # Save new defaults if requested. 
     229    if($param->{image_defaults}) { 
     230        return $app->error($app->translate( 
     231            'Permission denied setting image defaults for blog #[_2]', $blog_id 
     232        )) unless $app->{perms}->can_save_image_defaults; 
     233        $blog->image_default_set(1); 
     234        $blog->image_default_wrap_text($param->{wrap_text} ? 1 : 0); 
     235        $blog->image_default_align($param->{align} || MT::Blog::ALIGN()); 
     236        $blog->image_default_thumb($param->{thumb} ? 1 : 0); 
     237        $blog->image_default_width($param->{thumb_width} || MT::Blog::WIDTH()); 
     238        $blog->image_default_wunits($param->{thumb_width_type} || MT::Blog::UNITS()); 
     239        $blog->image_default_height($param->{thumb_height} || MT::Blog::WIDTH()); 
     240        $blog->image_default_hunits($param->{thumb_height_type} || MT::Blog::UNITS()); 
     241        $blog->image_default_constrain($param->{constrain} ? 1 : 0); 
     242        $blog->image_default_popup($param->{popup} ? 1 : 0); 
     243        $blog->save; 
     244    } 
     245 
     246    # Thumbnail creation 
     247    if ($thumb = $param->{thumb}) { 
     248        require MT::Image; 
     249        my $base_path = $param->{site_path} ? 
     250            $blog->site_path : $blog->archive_path; 
     251        my $file = $param->{fname}; 
     252        if ($file =~ m!\.\.|\0|\|!) { 
     253            return $app->error($app->translate("Invalid filename '[_1]'", $file)); 
     254        } 
     255        my $i_file = File::Spec->catfile($base_path, $file); 
     256        ## Untaint. We checked $file for security holes above. 
     257        ($i_file) = $i_file =~ /(.+)/s; 
     258        my $fmgr = $blog->file_mgr; 
     259        my $data = $fmgr->get_data($i_file, 'upload') 
     260            or return $app->error($app->translate( 
     261                "Reading '[_1]' failed: [_2]", $i_file, $fmgr->errstr)); 
     262        my $image_type = scalar $param->{image_type}; 
     263        my $img = MT::Image->new( Data => $data, 
     264                                  Type => $image_type ) 
     265            or return $app->error($app->translate( 
     266                "Thumbnail failed: [_1]", MT::Image->errstr)); 
     267        my($w, $h) = map $param->{$_}, qw( thumb_width thumb_height ); 
     268        (my($blob), $thumb_width, $thumb_height) = 
     269            $img->scale( Width => $w, Height => $h ) 
     270            or return $app->error($app->translate("Thumbnail failed: [_1]", 
     271                $img->errstr)); 
     272        require File::Basename; 
     273        my($base, $path, $ext) = File::Basename::fileparse($i_file, '\.[^.]*'); 
     274        my $t_file = $path . $base . '-thumb' . $ext; 
     275        my $basename = $base . '-thumb' . $ext; 
     276        ## If the thumbnail filename already exists, we don't want to overwrite 
     277        ## it, because it could contain valuable data; so we'll just make 
     278        ## sure to generate the name uniquely. 
     279        my $i = 0; 
     280        while ($fmgr->exists($t_file)) { 
     281            $t_file = File::Spec->catfile($path . $base . '-thumb' . (++$i) . $ext); 
     282        } 
     283        $fmgr->put_data($blob, $t_file, 'upload') 
     284            or return $app->error($app->translate( 
     285                "Error writing to '[_1]': [_2]", $t_file, $fmgr->errstr)); 
     286 
     287        $file =~ s/\Q$base$ext\E$//; 
     288        my $url = $param->{site_path} ? $blog->site_url : $blog->archive_url; 
     289        $url .= '/' unless $url =~ m!/$!; 
     290        $url .= $file; 
     291        $thumb = $url . MT::Util::encode_url($base . '-thumb' . $ext); 
     292 
     293        my $img_pkg = MT::Asset->class_handler('image'); 
     294        my $asset_thumb = new $img_pkg; 
     295        my $original = $asset_thumb->clone; 
     296        $asset_thumb->blog_id($blog_id); 
     297        $asset_thumb->url($thumb); 
     298        $asset_thumb->file_path($t_file); 
     299        $asset_thumb->file_name($basename); 
     300        my $ext2 = $ext; 
     301        $ext2 =~ s/^\.//; 
     302        $asset_thumb->file_ext($ext2); 
     303        $asset_thumb->image_width($thumb_width); 
     304        $asset_thumb->image_height($thumb_height); 
     305        $asset_thumb->created_by($app->user->id); 
     306        $asset_thumb->save; 
     307        MT->run_callbacks('CMSPostSave.asset', $app, $asset_thumb, $original); 
     308 
     309        $param->{thumb_asset_id} = $asset_thumb->id; 
     310 
     311        MT->run_callbacks('CMSUploadFile.thumbnail', 
     312                          File => $t_file, Url => $thumb, Size => length($blob), 
     313                          Asset => $asset_thumb, 
     314                          Type => 'thumbnail', 
     315                          Blog => $blog); 
     316 
     317        MT->run_callbacks('CMSUploadImage.thumbnail', 
     318                          File => $t_file, Url => $thumb, 
     319                          Asset => $asset_thumb, 
     320                          Width => $thumb_width, Height => $thumb_height, 
     321                          ImageType => $image_type, 
     322                          Size => length($blob), 
     323                          Type => 'thumbnail', 
     324                          Blog => $blog); 
     325    } 
     326    if ($param->{popup}) { 
     327        require MT::Template; 
     328        if (my $tmpl = MT::Template->load({ blog_id => $blog_id, 
     329                                            type => 'popup_image' })) { 
     330            (my $rel_path = $param->{fname}) =~ s!\.[^.]*$!!; 
     331            if ($rel_path =~ m!\.\.|\0|\|!) { 
     332                return $app->error($app->translate( 
     333                    "Invalid basename '[_1]'", $rel_path)); 
     334            } 
     335            my $ext = $blog->file_extension || ''; 
     336            $ext = '.' . $ext if $ext ne ''; 
     337            require MT::Template::Context; 
     338            my $ctx = MT::Template::Context->new; 
     339            $ctx->stash('blog', $blog); 
     340            $ctx->stash('blog_id', $blog->id); 
     341            $ctx->stash('asset', $asset); 
     342            $ctx->stash('image_url', $url); 
     343            $ctx->stash('image_width', $width); 
     344            $ctx->stash('image_height', $height); 
     345            my $popup = $tmpl->build($ctx) or die $tmpl->errstr; 
     346            my $fmgr = $blog->file_mgr; 
     347            my $root_path = $param->{site_path} ? 
     348                $blog->site_path : $blog->archive_path; 
     349            my $abs_file_path = File::Spec->catfile($root_path, $rel_path . $ext); 
     350 
     351            ## If the popup filename already exists, we don't want to overwrite 
     352            ## it, because it could contain valuable data; so we'll just make 
     353            ## sure to generate the name uniquely. 
     354            my($i, $rel_path_ext) = (0, $rel_path . $ext); 
     355            while ($fmgr->exists($abs_file_path)) { 
     356                $rel_path_ext = $rel_path . ++$i . $ext; 
     357                $abs_file_path = File::Spec->catfile($root_path, $rel_path_ext); 
     358            } 
     359            my ($vol, $dirs, $basename) = File::Spec->splitpath($rel_path_ext); 
     360            my $rel_url_ext = File::Spec->catpath($vol, $dirs, MT::Util::encode_url($basename)); 
     361  
     362            ## Untaint. We have checked for security holes above, so we 
     363            ## should be safe. 
     364            ($abs_file_path) = $abs_file_path =~ /(.+)/s; 
     365            $fmgr->put_data($popup, $abs_file_path, 'upload') 
     366                or return $app->error($app->translate( 
     367                   "Error writing to '[_1]': [_2]", $abs_file_path, 
     368                                                     $fmgr->errstr)); 
     369            $url = $param->{site_path} ? 
     370                $blog->site_url : $blog->archive_url; 
     371            $url .= '/' unless $url =~ m!/$!; 
     372            $rel_url_ext =~ s!^/!!; 
     373            $url .= $rel_url_ext; 
     374 
     375            my $html_pkg = MT::Asset->handler_for_file($abs_file_path); 
     376            my $asset_html = new $html_pkg; 
     377            my $original = $asset_html->clone; 
     378            $asset_html->blog_id($blog_id); 
     379            $asset_html->url($url); 
     380            $asset_html->file_path($abs_file_path); 
     381            $asset_html->file_name($basename); 
     382            $asset_html->file_ext($blog->file_extension); 
     383            $asset_html->created_by($app->user->id); 
     384            $asset_html->save; 
     385            MT->run_callbacks('CMSPostSave.asset', $app, $asset_html, $original); 
     386 
     387            MT->run_callbacks('CMSUploadFile.popup', 
     388                          File => $abs_file_path, Url => $url, 
     389                          Asset => $asset_html, 
     390                          Size => length($popup), 
     391                          Type => 'popup', 
     392                          Blog => $blog); 
     393        } 
     394    } 
     395    1; 
    153396} 
    154397 
  • branches/wheeljack/tmpl/cms/dialog_list_assets.tmpl

    r935 r937  
    3333        // setup 
    3434        dlg = new Dialog.Simple("list-assets"); 
    35         var panel = new ListingPanel("assets"); 
     35        var panel = new ListingPanel("asset"); 
    3636        dlg.panel = panel; 
     37 
     38        // hook into view updates so we can close any opened detail panel 
    3739        var old_update = panel.datasource.onUpdate; 
    3840        panel.datasource.onUpdate = function(ds) { viewChange(ds, old_update) }; 
     41 
    3942        panel.pager.setState(<TMPL_VAR NAME=PAGER_JSON>); 
    4043        panel.parent = dlg; 
     
    4548</script> 
    4649 
    47 <div id="assets-panel" class="panel"> 
     50<div id="asset-panel" class="panel"> 
    4851 
    4952<h2><span class="weblog-title-highlight"><TMPL_IF NAME=EDIT_BLOG_ID><TMPL_VAR NAME=BLOG_NAME ESCAPE=HTML><TMPL_ELSE><MT_TRANS phrase="System-wide"></TMPL_IF>: </span> <MT_TRANS phrase="Assets"></h2> 
     
    5962</TMPL_IF> 
    6063</p> 
     64 
     65<div class="search-box"> 
     66    <TMPL_IF PANEL_SEARCHABLE> 
     67    <form action="" method="get"> 
     68        <p class="page-desc"><TMPL_VAR NAME=SEARCH_PROMPT> 
     69        <input type="text" class="search-input"<TMPL_UNLESS PANEL_SEARCHABLE> disabled="disabled"</TMPL_UNLESS> /> 
     70        <input type="image" class="search-command" src="<TMPL_VAR NAME=STATIC_URI>images/nav_icons/color/search.gif" title="<MT_TRANS phrase="Search">" /> 
     71        <input type="button" class="search-reset hidden" value="<MT_TRANS phrase="View All">" /> 
     72    </p></form> 
     73    <TMPL_ELSE> 
     74    <p class="page-desc">&nbsp;<img border="0" src="<TMPL_VAR NAME=STATIC_URI>images/spacer.gif" width="17" height="20"></p> 
     75    </TMPL_IF> 
     76</div> 
    6177 
    6278<div class="list-wrapper"> 
  • branches/wheeljack/tmpl/cms/upload_complete.tmpl

    r935 r937  
    44<!-- 
    55 
    6 function doClick (f, trigger) { 
     6function presubmit(f) { 
    77    var mode = 'list_assets'; 
    88<TMPL_IF NAME=ENTRY_INSERT> 
     
    1010<TMPL_ELSE> 
    1111<TMPL_IF NAME=CAN_POST> 
    12     if (f.new_entry.checked) mode = 'start_upload_entry'; 
     12    if (f.new_entry && f.new_entry.checked) mode = 'start_upload_entry'; 
    1313</TMPL_IF> 
    1414</TMPL_IF> 
    15 doHandleMode(f, trigger, mode); 
    16 
    17  
    18 function doHandleMode (f, trigger, mode) { 
    19     var url = '<TMPL_VAR NAME=SCRIPT_URL>?__mode=' + mode + trigger; 
    20     url += '&blog_id=<TMPL_VAR NAME=BLOG_ID>&site_path=<TMPL_VAR NAME=SITE_PATH>&fname=<TMPL_VAR NAME=FNAME ESCAPE=URL>&url=<TMPL_VAR NAME=URL ESCAPE=URL>'; 
    21 <TMPL_IF NAME=DO_THUMB> 
    22     if (f.thumb.checked) { 
    23         url += '&thumb=1' + calcGeometry(f); 
    24         url += '&thumb_width_type=pixels&thumb_height_type=pixels&constrain=1'; 
     15    if (mode != 'asset_insert') { 
     16        f.target = "_top"; 
    2517    } 
    26 </TMPL_IF> 
    27 <TMPL_IF NAME=IS_IMAGE> 
    28     url += '&width=<TMPL_VAR NAME=WIDTH>&height=<TMPL_VAR NAME=HEIGHT>&image_type=<TMPL_VAR NAME=IMAGE_TYPE>'; 
    29     if (f.include.checked) {  
    30         url += '&include=1'; 
    31     } else { 
    32         url += '&link=1'; 
    33     } 
    34     if (f.include.checked) { 
    35         url += '&wrap_text=1&align='; 
    36         for (i = 0; i < f.align.length; i++) { 
    37             if (f.align[i].checked) url += f.align[i].value; 
    38         } 
    39     } 
    40      
    41     if (f.popup.checked) url += '&popup=1'; 
    42     if (f.image_defaults.checked) url += '&image_defaults=1'; 
    43 </TMPL_IF> 
    44 <TMPL_IF NAME=ENTRY_INSERT> 
    45     url += '&id=<TMPL_VAR NAME=ASSET_ID>&edit_field=<TMPL_VAR NAME=EDIT_FIELD>'; 
    46     location.href = url; 
    47 <TMPL_ELSE> 
    48     closeDialog(url); 
    49 </TMPL_IF> 
    50 
    51  
    52 <TMPL_IF NAME=DO_THUMB> 
    53  
    54 function calcGeometry (f) { 
    55     var w = calcDim(f.thumb_width, f.thumb_width_type, f.full_width.value); 
    56     var h = calcDim(f.thumb_height, f.thumb_height_type, f.full_height.value); 
    57     return '&thumb_width=' + w + '&thumb_height=' + h; 
    58 
    59  
    60 function calcDim (val, type, full) { 
    61     return val.value; 
    62 
    63  
    64 function adjustPixelsPct (f, width) { 
    65 //    if (!f.constrain.checked) return; 
    66     var e, s, full; 
    67     if (width) { 
    68         e = f.thumb_width;