Annotation of loncom/publisher/lonpubdir.pm, revision 1.166

1.1       www         1: # The LearningOnline Network with CAPA
1.145     raeburn     2: # Authoring Space Directory Lister
1.16      albertel    3: #
1.166   ! damieng     4: # $Id: lonpubdir.pm,v 1.165 2015/12/03 20:40:46 damieng Exp $
1.16      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
1.1       www        27: #
1.17      harris41   28: ###
1.1       www        29: 
                     30: package Apache::lonpubdir;
                     31: 
                     32: use strict;
                     33: use Apache::File;
                     34: use File::Copy;
                     35: use Apache::Constants qw(:common :http :methods);
1.17      harris41   36: use Apache::loncommon();
1.48      www        37: use Apache::lonhtmlcommon();
1.91      www        38: use Apache::londiff();
1.39      www        39: use Apache::lonlocal;
1.50      www        40: use Apache::lonmsg;
1.64      raeburn    41: use Apache::lonmenu;
                     42: use Apache::lonnet;
1.154     raeburn    43: use LONCAPA qw(:DEFAULT :match);
1.1       www        44: 
                     45: sub handler {
                     46: 
1.152     musolffc   47:     my $r=shift;
1.1       www        48: 
1.152     musolffc   49:     # Validate access to the construction space and get username:domain.
1.6       www        50: 
1.153     raeburn    51:     my ($uname,$udom)=&Apache::lonnet::constructaccess($r->uri); 
1.152     musolffc   52:     unless (($uname) && ($udom)) {
                     53:         return HTTP_NOT_ACCEPTABLE;
                     54:     }
1.23      foxr       55: 
1.127     www        56: # ----------------------------------------------------------- Start page output
1.23      foxr       57: 
1.152     musolffc   58:     my $fn=$r->filename;
                     59:     $fn=~s/\/$//;
                     60:     my $thisdisfn=$fn;
1.1       www        61: 
1.152     musolffc   62:     my $docroot=$r->dir_config('lonDocRoot');     # Apache  londocument root.
1.155     raeburn    63:     if ($thisdisfn eq "$docroot/priv/$udom") {
                     64:         if ((-d "/home/$uname/public_html/") && (!-e "$docroot/priv/$udom/$uname")) {
                     65:             my ($version) = ($r->dir_config('lonVersion') =~ /^\'?(\d+\.\d+)\./);
                     66:             &Apache::loncommon::content_type($r,'text/html');
                     67:             $r->send_http_header;
                     68: 
                     69:             &Apache::lonhtmlcommon::clear_breadcrumbs();
1.164     musolffc   70:             my $js = '<script type="text/javascript" 
                     71:                         src="/res/adm/includes/file_upload.js"></script>';
                     72:             $r->print(&Apache::loncommon::start_page('Authoring Space', $js).
1.155     raeburn    73:                       '<div class="LC_error">'.
                     74:                       '<br /><p>'.
                     75:                       &mt('Your Authoring Space is currently in the location used by LON-CAPA version 2.10 and older, but your domain is using a newer LON-CAPA version ([_1]).',$version).'</p>'.
                     76:                       '<p>'.
                     77:                       &mt('Please ask your Domain Coordinator to move your Authoring Space to the new location.').
                     78:                       '</p>'.
                     79:                       '</div>'.
                     80:                       &Apache::loncommon::end_page());
                     81:             return OK;
                     82:         }
                     83:     }
1.152     musolffc   84:     $thisdisfn=~s/^\Q$docroot\E\/priv//;
                     85:     
                     86:     my $resdir=$docroot.'/res'.$thisdisfn; # Resource directory
                     87:     my $targetdir='/res'.$thisdisfn; # Publication target directory.
                     88:     my $linkdir='/priv'.$thisdisfn;      # Full URL name of constr space.
                     89: 
                     90:     my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
                     91: 
1.164     musolffc   92:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
                     93:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,
                     94:                                 "$londocroot/priv/$udom/$uname"); # expressed in kB
                     95:     my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,
                     96:                                 'author'); # expressed in MB
                     97: 
                     98:     # Put out the start of page.
                     99:     &startpage($r, $uname, $udom, $thisdisfn, $current_disk_usage, $disk_quota); 
1.157     raeburn   100: 
                    101:     if (!-d $fn) {
                    102:         if (-e $fn) {
                    103:             $r->print('<p class="LC_info">'.&mt('Requested item is a file not a directory.').'</p>');
                    104:         } else {
                    105:             $r->print('<p class="LC_info">'.&mt('The requested subdirectory does not exist.').'</p>');
                    106:         }
                    107:         $r->print(&Apache::loncommon::end_page());
                    108:         return OK;
                    109:     }
                    110:     my @files;
                    111:     if (opendir(DIR,$fn)) {
1.158     raeburn   112:         @files = grep(!/^\.+$/,readdir(DIR));
1.157     raeburn   113:         closedir(DIR);
                    114:     } else {
                    115:         $r->print('<p class="LC_error">'.&mt('Could not open directory.').'</p>');
                    116:         $r->print(&Apache::loncommon::end_page());
                    117:         return OK;
                    118:     }
                    119: 
1.164     musolffc  120:     # Put out actions for directory, browse/upload + new file page.
                    121:     &dircontrols($r,$uname,$udom,$thisdisfn, $current_disk_usage, $disk_quota);
1.152     musolffc  122:     &resourceactions($r,$uname,$udom,$thisdisfn); # Put out form used for printing/deletion etc.
1.64      raeburn   123: 
1.152     musolffc  124:     my $numdir = 0;
                    125:     my $numres = 0;
1.6       www       126:   
1.160     musolffc  127:     if ((@files == 0) && ($thisdisfn =~ m{^/$match_domain/$match_username})) {
                    128:         if ($thisdisfn =~ m{^/$match_domain/$match_username$}) {
                    129:             $r->print('<p class="LC_info">'.&mt('This Authoring Space is currently empty.').'</p>');
                    130:         } else {
                    131:             $r->print('<p class="LC_info">'.&mt('This subdirectory is currently empty.').'</p>');
                    132:         }
                    133:         $r->print(&Apache::loncommon::end_page());
                    134:         return OK;
                    135:     }
                    136: 
1.152     musolffc  137:     # Retrieving value for "sortby" and "sortorder" from QUERY_STRING
                    138:     &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
                    139:         ['sortby','sortorder']);
                    140: 
                    141:     # Sort by name as default, not reversed
                    142:     if (! exists($env{'form.sortby'})) { $env{'form.sortby'} = 'filename' }
                    143:     if (! exists($env{'form.sortorder'})) { $env{'form.sortorder'} = '' }
                    144:     my $sortby = $env{'form.sortby'};
                    145:     my $sortorder = $env{'form.sortorder'};
                    146: 
1.160     musolffc  147:     # Order in which columns are displayed from left to right
                    148:     my @order = ('filetype','actions','filename','title',
                    149:                     'pubstatus','cmtime','size');
                    150: 
                    151:     # Up and down arrows to indicate sort order
                    152:     my @arrows = ('&nbsp;&#9650;','&nbsp;&#9660;','');
                    153: 
                    154:     # Default sort order and column title
                    155:     my %columns = (
                    156:         filetype =>     {
                    157:                             order => 'ascending',
                    158:                             text  => &mt('Type'),
                    159:                         },
                    160:         actions =>      {
                    161:                             # Not sortable
                    162:                             text  => &mt('Actions'),
                    163:                         },
                    164:         filename =>     {
                    165:                             order => 'ascending',
                    166:                             text  => &mt('Name'),
                    167:                         },
                    168:         title =>        {
                    169:                             order => 'ascending',
                    170:                             text  => &mt('Title'),
                    171:                         },
                    172:         pubstatus =>    {
                    173:                             order => 'ascending',
                    174:                             text  => &mt('Status'),
                    175:                             colspan => '2',
                    176:                         },
                    177:         cmtime =>       {
                    178:                             order => 'descending',
                    179:                             text  => &mt('Last Modified'),
                    180:                         },
                    181:         size =>         {
                    182:                             order => 'ascending',
                    183:                             text  => &mt('Size').' (kB)',
                    184:                         },
                    185:     ); 
                    186: 
                    187:     # Print column headers
                    188:     my $output = '';
                    189:     foreach my $key (@order) {
                    190:         my $idx;
                    191:         # Append an up or down arrow to sorted column
                    192:         if ($sortby eq $key) {
                    193:             $idx = ($columns{$key}{order} eq 'ascending') ? 0:1;
                    194:             if ($sortorder eq 'rev') { $idx ++; }
                    195:             $idx = $idx%2;
                    196:         } else { $idx = 2; } # No arrow if column is not sorted
                    197:         $output .= (($columns{$key}{order}) ?
                    198:             '<th'.($columns{$key}{colspan} ? ' colspan="'.$columns{$key}{colspan}.'"' : '')
1.162     golterma  199:             .'><a href="'.$linkdir.'/?sortby='.$key.'&amp;sortorder='
1.160     musolffc  200:             .((($sortby eq $key) && ($sortorder ne 'rev')) ? 'rev' : '').'">'
                    201:             .$columns{$key}{text}.$arrows[$idx].'</a></th>' :
                    202:             '<th>'.$columns{$key}{text}.'</th>');
1.154     raeburn   203:     }
1.161     golterma  204: 
1.162     golterma  205: my $result = "<script type=\"text/javascript\">
1.161     golterma  206:     sessionStorage.setItem('CSTRcache','".&prepareJsonData($uname,$udom,$thisdisfn)."');
                    207:     localStorage.setItem('CSTRtrans', '".&prepareJsonTranslations()."');
                    208: </script>";
                    209:     $r->print($result);
                    210: 
                    211:     $r->print('<div id="currentFolder">'.&Apache::loncommon::start_data_table()
1.162     golterma  212:         .'<tr><th colspan="8" id="searchtitle" style="display:none"></th></tr>'
1.160     musolffc  213:         .&Apache::loncommon::start_data_table_header_row() . $output
1.152     musolffc  214:         .&Apache::loncommon::end_data_table_header_row()
                    215:     );
                    216: 
                    217:     my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
                    218:     my $filehash = {};
                    219:     foreach my $filename (@files) {
1.159     musolffc  220:         # Skip .DS_Store, .DAV and hidden files
1.152     musolffc  221:         my ($extension) = ($filename=~/\.(\w+)$/);
1.156     raeburn   222:         next if (($filename eq '.DS_Store')
1.159     musolffc  223:                 || ($filename eq '.DAV')
1.156     raeburn   224:                 || (&Apache::loncommon::fileembstyle($extension) eq 'hdn')
                    225:                 || ($filename =~ /^\._/));
1.152     musolffc  226: 
                    227:         my ($cmode,$csize,$cmtime)=(stat($fn.'/'.$filename))[2,7,9];
                    228:         my $linkfilename = &HTML::Entities::encode('/priv'.$thisdisfn.'/'.$filename,'<>&"');
                    229:         # Identify type of file according to icon used
                    230:         my ($filetype) = (&Apache::loncommon::icon($filename) =~ m{/(\w+).gif$}); 
                    231:         my $cstr_dir = $r->dir_config('lonDocRoot').'/priv'.$thisdisfn;
                    232:         my $meta_same = &isMetaSame($cstr_dir, $resdir, $filename);
                    233:         
                    234:         # Store size, title, and status for files but not directories
                    235:         my $size = (!($cmode&$dirptr)) ? $csize/1024. : 0;
                    236:         my ($status, $pubstatus, $title, $fulltitle);
                    237:         if (!($cmode&$dirptr)) {
                    238:             ($status, $pubstatus) = &getStatus($resdir, $targetdir, $cstr_dir, 
                    239:                 $filename, $linkfilename, $cmtime, $meta_same);
                    240:             ($fulltitle, $title) = &getTitle($resdir, $targetdir, $filename, 
                    241:                                         $linkfilename, $meta_same, \%bombs);
                    242:         } else {
                    243:             ($status, $pubstatus) = ('','');
                    244:             ($fulltitle, $title) = ('','');
                    245:         }
1.150     musolffc  246: 
1.152     musolffc  247:         # This hash will allow sorting
                    248:         $filehash->{ $filename } = {
                    249:             "cmtime"            => $cmtime,
                    250:             "size"              => $size,
                    251:             "cmode"             => $cmode,
                    252:             "filetype"          => $filetype,
                    253:             "title"             => $title,
                    254:             "fulltitle"         => $fulltitle,
                    255:             "status"            => $status,
                    256:             "pubstatus"         => $pubstatus,
                    257:             "linkfilename"      => $linkfilename,
                    258:         }
                    259:     }
                    260:    
                    261:     my @sorted_files;
                    262:     # Sorting by something other than "Name".  Name is the secondary key.
                    263:     if ($sortby =~ m{cmtime|size}) {    # Numeric fields
                    264:         # First check if order should be reversed
                    265:         if ($sortorder eq "rev") {
                    266:             @sorted_files = sort {
                    267:                 $filehash->{$a}->{$sortby} <=> $filehash->{$b}->{$sortby}
                    268:                     or
                    269:                 uc($a) cmp uc($b)
                    270:             } (keys(%{$filehash}));
                    271:         } else {
                    272:             @sorted_files = sort {
                    273:                 $filehash->{$b}->{$sortby} <=> $filehash->{$a}->{$sortby}
                    274:                     or
                    275:                 uc($a) cmp uc($b)
                    276:             } (keys(%{$filehash}));
                    277:         }
                    278:     } elsif ($sortby =~ m{filetype|title|status}) {     # String fields
                    279:         if ($sortorder eq "rev") {
                    280:             @sorted_files = sort {
                    281:                 $filehash->{$b}->{$sortby} cmp $filehash->{$a}->{$sortby}
                    282:                     or
                    283:                 uc($a) cmp uc($b)
                    284:             } (keys(%{$filehash}));
                    285:         } else {
                    286:             @sorted_files = sort {
                    287:                 $filehash->{$a}->{$sortby} cmp $filehash->{$b}->{$sortby}
                    288:                     or
                    289:                 uc($a) cmp uc($b)
                    290:             } (keys(%{$filehash}));
                    291:         }
                    292: 
                    293:     # Sort by "Name" is the default
                    294:     } else { 
                    295:         if ($sortorder eq "rev") {
                    296:             @sorted_files = sort {uc($b) cmp uc($a)} (keys(%{$filehash}));
                    297:         } else {
                    298:             @sorted_files = sort {uc($a) cmp uc($b)} (keys(%{$filehash}));
                    299:         }
                    300:     }
                    301: 
                    302:     # Print the sorted resources
                    303:     foreach my $filename (@sorted_files) {
                    304:         if ($filehash->{$filename}->{"cmode"}&$dirptr) {        # Directories
                    305:             &putdirectory($r, $thisdisfn, $linkdir, $filename, 
                    306:                 $filehash->{$filename}->{"cmtime"}, 
                    307:                 $targetdir, \%bombs, \$numdir);
                    308:         } else {                                                # Files
                    309:             &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir,
                    310:                 $targetdir, $linkdir, $filehash->{$filename}->{"cmtime"}, 
                    311:                 $filehash->{$filename}->{"size"}, \$numres, 
                    312:                 $filehash->{$filename}->{"linkfilename"},
                    313:                 $filehash->{$filename}->{"fulltitle"},
                    314:                 $filehash->{$filename}->{"status"},
                    315:                 $filehash->{$filename}->{"pubstatus"});
                    316:         }
                    317:     }
                    318: 
1.161     golterma  319:   $r->print(&Apache::loncommon::end_data_table()
                    320:            .'</div><div id="otherplaces" style="display:none">'
                    321:            .&Apache::loncommon::start_data_table()
1.162     golterma  322:            .'<tr><th colspan="7">'.&mt('Results in other directories:').'</th></tr>'
1.161     golterma  323:            .'<tr class="LC_header_row" id="otherplacestable">'
                    324:            .'<th>'.&mt('Type').'</th>'
                    325:            .'<th>'.&mt('Directory').'</th>'
                    326:            .'<th>'.&mt('Name').'</th>'
                    327:            .'<th>'.&mt('Title').'</th>'
                    328:            .'<th colspan="2">'.&mt('Status').'</th>'
                    329:            .'<th>'.&mt('Last Modified').'</th>'
                    330:            .'</tr>'
1.162     golterma  331:            .&Apache::loncommon::end_data_table()
1.161     golterma  332:            .'</div>'
                    333:            .&Apache::loncommon::end_page()
                    334:   );
                    335:   return OK;  
1.1       www       336: }
1.127     www       337: 
1.152     musolffc  338: 
                    339: 
1.23      foxr      340: #   Output the header of the page.  This includes:
                    341: #   - The HTML header 
                    342: #   - The H1/H3  stuff which includes the directory.
                    343: #
1.164     musolffc  344: #     startpage($r, $uame, $udom, $thisdisfn, $current_disk_usage, $disk_quota);
1.23      foxr      345: #      $r     - The apache request object.
                    346: #      $uname - User name.
                    347: #      $udom  - Domain name the user is logged in under.
                    348: #      $thisdisfn - Displayable version of the filename.
1.164     musolffc  349: #      $current_disk_usage - User's current disk usage (in kB).
                    350: #      $disk_quota - Disk quota for user's authoring space (in MB).
1.26      www       351: 
1.23      foxr      352: sub startpage {
1.164     musolffc  353:     my ($r, $uname, $udom, $thisdisfn, $current_disk_usage, $disk_quota) = @_;
1.39      www       354:     &Apache::loncommon::content_type($r,'text/html');
1.23      foxr      355:     $r->send_http_header;
1.64      raeburn   356: 
1.132     www       357:     my $formaction='/priv'.$thisdisfn.'/';
1.89      albertel  358:     $formaction=~s|/+|/|g;
1.66      raeburn   359:     &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
1.121     bisitz    360: 
1.125     droeschl  361:     &Apache::lonhtmlcommon::clear_breadcrumbs();
                    362:     &Apache::lonhtmlcommon::add_breadcrumb({
1.145     raeburn   363:         'text'  => 'Authoring Space',
1.137     raeburn   364:         'href'  => &Apache::loncommon::authorspace($formaction),
1.125     droeschl  365:     });
                    366:     # breadcrumbs (and tools) will be created 
                    367:     # in start_page->bodytag->innerregister
                    368: 
1.132     www       369:     $env{'request.noversionuri'}=$formaction;
1.164     musolffc  370:     my $js = '<script type="text/javascript" 
                    371:                 src="/res/adm/includes/file_upload.js"></script>';
                    372:     $r->print(&Apache::loncommon::start_page('Authoring Space', $js));
1.90      albertel  373: 
1.164     musolffc  374:     $disk_quota = 1024 * $disk_quota; # convert from MB to kB
1.147     raeburn   375: 
1.121     bisitz    376:     $r->print(&Apache::loncommon::head_subbox(
1.147     raeburn   377:                      '<div style="float:right;padding-top:0;margin-top;0">'
                    378:                     .&Apache::lonhtmlcommon::display_usage($current_disk_usage,$disk_quota)
                    379:                     .'</div>'
                    380:                     .&Apache::loncommon::CSTR_pageheader()));
1.121     bisitz    381: 
1.101     albertel  382:     my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
1.145     raeburn   383:     my $doctitle = 'LON-CAPA '.&mt('Authoring Space');
1.109     bisitz    384:     my $newname = &mt('New Name');
1.29      www       385:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
1.77      albertel  386: <script type="text/javascript">
1.107     bisitz    387: top.document.title = '$esc_thisdisfn/ - $doctitle';
1.37      www       388: // Store directory location for menu bar to find
                    389: 
1.132     www       390: parent.lastknownpriv='/priv$esc_thisdisfn/';
1.37      www       391: 
                    392: // Confirmation dialogues
                    393: 
1.64      raeburn   394:     function currdiract(theform) {
                    395:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
1.79      www       396:             document.publishdir.filename.value = theform.filename.value;
                    397: 	    document.publishdir.submit();
1.64      raeburn   398:         }
1.113     schafran  399:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editmeta') {
1.66      raeburn   400:             top.location=theform.filename.value+'default.meta'
1.64      raeburn   401:         }
                    402:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
                    403:             document.printdir.postdata.value=theform.filename.value
                    404:             document.printdir.submit();
                    405:         }
1.88      raeburn   406:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == "delete") {
                    407:               var delform = document.delresource
                    408:               delform.filename.value = theform.filename.value
                    409:               delform.submit()
                    410:         }
1.64      raeburn   411:     }
                    412:   
                    413:     function checkUpload(theform) {
                    414:         if (theform.file == '') {
                    415:             alert("Please use 'Browse..' to choose a file first, before uploading")
                    416:             return 
                    417:         }
                    418:         theform.submit()  
                    419:     }
                    420: 
                    421:     function SetPubDir(theform,printForm) {
                    422:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
1.75      albertel  423:             top.location = theform.openname.value
1.64      raeburn   424:             return
                    425:         }
                    426:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
1.79      www       427:             theform.submit();
1.64      raeburn   428:         }
1.113     schafran  429:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editmeta") {
1.66      raeburn   430:             top.location=theform.filename.value+'default.meta'
1.64      raeburn   431:         }
1.68      raeburn   432:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
1.64      raeburn   433:             theform.action = '/adm/printout'
                    434:             theform.postdata.value = theform.filename.value
                    435:             theform.submit()
                    436:         }
1.88      raeburn   437:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "delete") {
                    438:               var delform = document.delresource
                    439:               delform.filename.value = theform.filename.value
                    440:               delform.submit()
                    441:         }
1.64      raeburn   442:         return
                    443:     }
                    444:     function SetResChoice(theform) {
                    445:       var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
                    446:       if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
                    447:           changename(theform,activity)
                    448:       }
                    449:       if (activity == 'publish') {
                    450:           var pubform = document.pubresource
                    451:           pubform.filename.value = theform.filename.value
                    452:           pubform.submit()
                    453:       }
                    454:       if (activity == 'delete') {
                    455:           var delform = document.delresource
                    456:           delform.filename.value = theform.filename.value
1.71      raeburn   457:           delform.submit()
1.64      raeburn   458:       }
                    459:       if (activity == 'obsolete') {
1.68      raeburn   460:           var pubform = document.pubresource
                    461:           pubform.filename.value = theform.filename.value
1.80      www       462:           pubform.makeobsolete.value=1;
1.68      raeburn   463:           pubform.submit()
1.64      raeburn   464:       }
                    465:       if (activity == 'print') {
1.68      raeburn   466:           document.printresource.postdata.value = theform.filename.value
1.64      raeburn   467:           document.printresource.submit()
                    468:       }
                    469:       if (activity == 'retrieve') {
1.68      raeburn   470:           document.retrieveres.filename.value = theform.filename.value
                    471:           document.retrieveres.submit()
1.64      raeburn   472:       }
1.82      www       473:       if (activity == 'cleanup') {
                    474:           document.cleanup.filename.value = theform.filename.value
                    475:           document.cleanup.submit()
                    476:       }
1.64      raeburn   477:       return
                    478:     }
                    479:     function changename(theform,activity) {
1.96      banghart  480:         var oldname=theform.dispfilename.value;
1.109     bisitz    481:         var newname=prompt('$newname',oldname);
1.97      banghart  482:         if (newname == "" || !newname || newname == oldname)  {
1.64      raeburn   483:             return
                    484:         }
                    485:         document.moveresource.newfilename.value = newname
                    486:         document.moveresource.filename.value = theform.filename.value
                    487:         document.moveresource.action.value = activity
                    488:         document.moveresource.submit();
                    489:     }
1.29      www       490: </script>
                    491: ENDPUBDIRSCRIPT
1.64      raeburn   492:     $r->print($pubdirscript);
                    493: }
                    494: 
                    495: sub dircontrols {
1.164     musolffc  496:     my ($r,$uname,$udom,$thisdisfn, $current_disk_usage, $disk_quota) = @_;
1.84      www       497:     my %lt=&Apache::lonlocal::texthash(
                    498:                                        cnpd => 'Cannot publish directory',
                    499:                                        cnrd => 'Cannot retrieve directory',
                    500:                                        mcdi => 'Must create new subdirectory inside a directory',
                    501:                                        pubr => 'Publish this Resource',
                    502:                                        pubd => 'Publish this Directory',
1.88      raeburn   503:                                        dedr => 'Delete Directory',
1.84      www       504:                                        rtrv => 'Retrieve Old Version',
                    505:                                        list => 'List Directory',
                    506:                                        uplo => 'Upload file',  
                    507:                                        dele => 'Delete',
1.113     schafran  508:                                        edit => 'Edit Metadata', 
1.84      www       509:                                        sela => 'Select Action',
                    510:                                        nfil => 'New file',
                    511:                                        nhtm => 'New HTML file',
                    512:                                        nprb => 'New problem',
                    513:                                        npag => 'New assembled page',
                    514:                                        nseq => 'New assembled sequence',
                    515:                                        ncrf => 'New custom rights file',
                    516:                                        nsty => 'New style file',
                    517:                                        nlib => 'New library file',
1.103     albertel  518:                                        nbt  => 'New bridgetask file',
1.84      www       519:                                        nsub => 'New subdirectory',
                    520:                                        renm => 'Rename current file to',
                    521:                                        move => 'Move current file to',
                    522:                                        copy => 'Copy current file to',
                    523:                                        type => 'Type Name Here',
1.105     albertel  524:                                        go   => 'Go',
1.84      www       525:                                        prnt => 'Print contents of directory',
                    526:                                        crea => 'Create a new directory or LON-CAPA document',
1.161     golterma  527:                                        qs   => 'Quick Search',
                    528:                                        cs   => 'Case Sensitive',
                    529:                                        re   => 'Regular Expression',
1.84      www       530: 				       acti => 'Actions for current directory',
1.105     albertel  531: 				       updc => 'Upload a new document',
                    532: 				       pick => 'Please select an action to perform using the new filename',
1.84      www       533:                                       );
1.106     bisitz    534:     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
1.164     musolffc  535:     # Calculate free space in bytes.
                    536:     # $disk_quota is in MB and $current_disk_usage is in kB
                    537:     my $free_space = 1024 * ((1024 * $disk_quota) - $current_disk_usage);
1.161     golterma  538:     $r->printf(<<END,&Apache::loncommon::help_open_topic('Quicksearch'));
1.117     harmsja   539: <div class="LC_columnSection">
1.116     bisitz    540:   <div>
                    541:     <form name="curractions" method="post" action="">
                    542:       <fieldset>
                    543:         <legend>$lt{'acti'}</legend>
                    544:         <select name="dirtask" onchange="currdiract(this.form)">
1.84      www       545:             <option>$lt{'sela'}</option>
                    546:             <option value="publish">$lt{'pubd'}</option>
1.113     schafran  547:             <option value="editmeta">$lt{'edit'}</option>
1.84      www       548:             <option value="printdir">$lt{'prnt'}</option>
1.88      raeburn   549:             <option value="delete">$lt{'dedr'}</option>
1.116     bisitz    550:         </select>
1.132     www       551:         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
1.116     bisitz    552:       </fieldset>
                    553:     </form>
                    554:     <form name="publishdir" method="post" action="/adm/publish" target="_parent">
                    555:       <input type="hidden" name="pubrec" value="" />
                    556:       <input type="hidden" name="filename" value="" />
                    557:     </form>
                    558:     <form name="printdir" method="post" action="/adm/printout" target="_parent">
                    559:       <input type="hidden" name="postdata" value="" />
                    560:     </form>
                    561:   </div>
                    562: 
                    563:   <div>
                    564:     <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
                    565:       <fieldset>
                    566:         <legend>$lt{'updc'}</legend>
1.132     www       567:         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
1.164     musolffc  568:         <input type="file" name="upfile" class="flUpload testclass" size="20" />
                    569:         <input type="hidden" id="free_space" value=$free_space />
1.116     bisitz    570:         <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
                    571:       </fieldset>
                    572:     </form>
                    573:   </div>
                    574: 
                    575:   <div>
                    576:     <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
                    577:       <fieldset>
                    578:               <legend>$lt{'crea'}</legend>
                    579: 	      <span class="LC_nobreak">
1.132     www       580: 		<input type="hidden" name="filename" value="/priv$thisdisfn/" />
1.105     albertel  581:                   <script type="text/javascript">
                    582:                     function validate_go() {
                    583:                         var selected = document.fileaction.action.selectedIndex;
                    584:                         if (selected == 0) {
                    585:                             alert('$lt{'pick'}');
                    586:                         } else {
                    587:                             document.fileaction.submit();
                    588:                         }
                    589:                     }
                    590:                   </script>
                    591: 		  <select name="action">
                    592: 		    <option value="none">$lt{'sela'}</option>
                    593: 		    <option value="newfile">$lt{'nfil'}:</option>
                    594: 		    <option value="newhtmlfile">$lt{'nhtm'}:</option>
                    595: 		    <option value="newproblemfile">$lt{'nprb'}:</option>
                    596:                     <option value="newpagefile">$lt{'npag'}:</option>
                    597:                     <option value="newsequencefile">$lt{'nseq'}:</option>
                    598:                     <option value="newrightsfile">$lt{'ncrf'}:</option>
                    599:                     <option value="newstyfile">$lt{'nsty'}:</option>
                    600:                     <option value="newtaskfile">$lt{'nbt'}:</option>
                    601:                     <option value="newlibraryfile">$lt{'nlib'}:</option>
                    602: 	            <option value="newdir">$lt{'nsub'}:</option>
1.163     golterma  603: 		  </select>&nbsp;<input type="text" name="newfilename" placeholder="$lt{'type'}" value="" onfocus="if (this.value == is.empty()) this.value=''" />&nbsp;<input type="button" value="Go" onclick="validate_go();" />
                    604: 		<br />
                    605:                 <span>Quickactions:
                    606:                  <input type="hidden" name="mode"/>
                    607:                  <a href="javascript:void(0)" onclick="javascript:validate_action('blank')">
                    608: 		    <img src="/adm/lonIcons/unknown.gif" title="Create blank problem file"></a>
                    609:                  <a href="javascript:void(0)" onclick="javascript:validate_action('problemtempl')"> 
                    610:                     <img src="/adm/lonIcons/problem.gif" title="Create new problem from template"></a>
                    611:                  <a href="javascript:void(0)" onclick="javascript:validate_action('blankhtml')"> 
                    612:                     <img src="/adm/lonIcons/html.gif" title="Create new blank HTML file"></a>
                    613:                  <a href="javascript:void(0)" onclick="javascript:validate_action('folder')"> 
                    614: 		    <img src="/adm/lonIcons/navmap.folder.closed.gif" title="Create new subdirectory"></a>
                    615:                 </span>
                    616:                  <script type="text/javascript">
                    617:                      function validate_action(action){
                    618: 
                    619:                          if (document.getElementsByName(\'newfilename\')[0].value != \'\'){
                    620:                              if (action == "blank") {
                    621: 								 document.fileaction.action.value=\'newproblemfile\';
                    622: 								 document.fileaction.mode.value=\'blank\';
                    623: 							 } else if (action == "problemtempl") {
                    624: 								 document.fileaction.action.value=\'newproblemfile\';
                    625:                                  validate_go();
                    626:                              } else if (action == "blankhtml") {
                    627:                                  document.fileaction.action.value=\'newhtmlfile\';
                    628:                                  validate_go();
                    629:                              } else if (action == "folder") {
                    630:                                  document.fileaction.action.value=\'newdir\';
                    631:                                  document.fileaction.mode.value=\'folder\';
                    632:                              }
                    633:                              fileaction.submit();
                    634:                          } else {
                    635:                              alert(\'Please specify file name.\');
                    636:                              // TODO: ask for filename? if so, do some refactoring
                    637: 
                    638:                          }
                    639:                      }
                    640:                  </script>
1.92      albertel  641: 		 </span>
1.116     bisitz    642:       </fieldset>
                    643:     </form>
1.161     golterma  644:     </div>
                    645:     <div>
1.162     golterma  646:       <fieldset style="display:inline">
                    647:             <legend>$lt{'qs'}</legend>
                    648:                 <script type="text/javascript" src="/adm/quicksearch/quicksearch.js"></script>
                    649:                 <input type="text" id="quickfilter" placeholder="Enter search term" onkeyup="applyFilter()"/>
                    650:                 <input type="button" value="Clear" onclick="document.getElementById(\'quickfilter\').value=\'\'; applyFilter()" />
                    651:                 %s
                    652:                 <br />
                    653:                 <label><input type="checkbox" id="casesens" onchange="applyFilter()"/>$lt{'cs'}&nbsp;&nbsp;</label>
                    654:                 <label><input type="checkbox" id="regex" onchange="applyFilter()"/>$lt{'re'}&nbsp;&nbsp;</label>
                    655:         </fieldset>
1.116     bisitz    656:   </div>
                    657: </div>
1.64      raeburn   658: END
                    659: }
                    660: 
                    661: sub resourceactions {
                    662:     my ($r,$uname,$udom,$thisdisfn) = @_;
                    663:     $r->print(<<END);
                    664:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
                    665:          <input type="hidden" name="filename" value="" />
                    666:          <input type="hidden" name="newfilename" value="" />
                    667:          <input type="hidden" name="action" value="" />
                    668:        </form>
                    669:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
                    670:          <input type="hidden" name="filename" value="" />
                    671:          <input type="hidden" name="action" value="delete" />
                    672:        </form>
                    673:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
                    674:          <input type="hidden" name="filename" value="" />
1.80      www       675:          <input type="hidden" name="makeobsolete" value="0" />
1.64      raeburn   676:        </form>
                    677:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
                    678:            <input type="hidden" name="postdata" value="" />
                    679:        </form>
                    680:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
                    681:            <input type="hidden" name="filename" value="" />
                    682:        </form>
1.82      www       683:        <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
                    684:            <input type="hidden" name="filename" value="" />
                    685:        </form>
1.64      raeburn   686: END
1.23      foxr      687: }
                    688: 
                    689: #
                    690: #   Get the title string or "[untitled]" if the file has no title metadata:
                    691: #   Without the latter substitution, it's impossible to examine metadata for
                    692: #   untitled resources.  Resources may be legitimately untitled, to prevent
                    693: #   searches from locating them.
                    694: #
                    695: #   $str = getTitleString($fullname);
                    696: #       $fullname - Fully qualified filename to check.
                    697: #
                    698: sub getTitleString {
                    699:     my $fullname = shift;
                    700:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
                    701: 
                    702:     unless ($title) {
1.40      www       703: 	$title = "[".&mt('untitled')."]";
1.23      foxr      704:     }
                    705:     return $title;
                    706: }
                    707: 
1.55      www       708: sub getCopyRightString {
                    709:     my $fullname = shift;
                    710:     return &Apache::lonnet::metadata($fullname, 'copyright');
                    711: }
1.61      www       712: 
                    713: sub getSourceRightString {
                    714:     my $fullname = shift;
                    715:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
                    716: }
1.23      foxr      717: #
1.21      foxr      718: #  Put out a directory table row:
1.134     raeburn   719: #    putdirectory(r, base, here, dirname, modtime, targetdir, bombs, numdir)
                    720: #      r         - Apache request object.
                    721: #      reqfile   - File in request.
                    722: #      here      - Where we are in directory tree.
                    723: #      dirname   - Name of directory special file.
                    724: #      modtime   - Encoded modification time.
                    725: #      targetdir - Publication target directory.
                    726: #      bombs     - Reference to hash of URLs with runtime error messages.
                    727: #      numdir    - Reference to scalar used to track number of sub-directories
                    728: #                  in directory (used in form name for each "actions" dropdown).
                    729: #
1.21      foxr      730: sub putdirectory {
1.134     raeburn   731:     my ($r, $reqfile, $here, $dirname, $modtime, $targetdir, $bombs, $numdir) = @_;
1.129     www       732: 
                    733: # construct the display filename: the directory name unless ..:
1.133     www       734:    
                    735:     my $actionitem;
                    736:  
1.21      foxr      737:     my $disfilename = $dirname;
1.130     www       738: # Don't display directory itself, and there is no way up from root directory
1.133     www       739:     unless ((($dirname eq '..') && ($reqfile=~/^\/[^\/]+\/[^\/]+$/)) || ($dirname eq '.')) {
1.143     bisitz    740:         my $kaputt=0;
1.134     raeburn   741:         if (ref($bombs) eq 'HASH') {
1.143     bisitz    742:             foreach my $key (keys(%{$bombs})) {
                    743:                 my $currentdir = &Apache::lonnet::declutter("$targetdir/$disfilename");
                    744:                 if (($key) =~ m{^\Q$currentdir\E/}) { $kaputt=1; last; }
                    745:             }
1.134     raeburn   746:         }
1.129     www       747: #
                    748: # Get the metadata from that directory's default.meta to display titles
                    749: #
1.52      www       750: 	%Apache::lonpublisher::metadatafields=();
                    751: 	%Apache::lonpublisher::metadatakeys=();
1.129     www       752: 	&Apache::lonpublisher::metaeval(
                    753:                  &Apache::lonnet::getfile($r->dir_config('lonDocRoot').$here.'/'.$dirname.'/default.meta')
                    754:                                        );
1.133     www       755:         if ($dirname eq '..') {
1.109     bisitz    756:             $actionitem = &mt('Go to ...');
1.133     www       757:             $disfilename = '<i>'.&mt('Parent Directory').'</i>';
1.64      raeburn   758:         } else {
                    759:             $actionitem = 
                    760:                     '<form name="dirselect_'.$$numdir.
                    761:                     '" action="/adm/publish" target="_parent">'.
1.85      albertel  762:                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
1.64      raeburn   763:                       '<option selected="selected">'.&mt('Select action').'</option>'.
                    764:                       '<option value="open">'.&mt('Open').'</option>'.
                    765:                       '<option value="publish">'.&mt('Publish').'</option>'.
1.113     schafran  766:                       '<option value="editmeta">'.&mt('Edit Metadata').'</option>'.
1.77      albertel  767:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
1.88      raeburn   768:                       '<option value="delete">'.&mt('Delete directory').'</option>'.
1.64      raeburn   769:                     '</select>'.
1.129     www       770:                      '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" />'.
1.75      albertel  771:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
1.64      raeburn   772:                      '<input type="hidden" name="postdata" value="" />'.
                    773:                    '</form>';
                    774:             $$numdir ++;
                    775:         }
1.92      albertel  776: 	$r->print('<tr class="LC_browser_folder">'.
1.53      www       777: 		  '<td><img src="'.
1.123     bisitz    778: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/navmap.folder.closed.gif" alt="folder" /></td>'.
1.64      raeburn   779: 		  '<td>'.$actionitem.'</td>'.
1.92      albertel  780: 		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
                    781: 		  $disfilename.'</a></span></td>'.
1.134     raeburn   782: 		        '<td colspan="3">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($targetdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
1.92      albertel  783: 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
                    784: 	    $r->print(' <i>'.
                    785: 		      $Apache::lonpublisher::metadatafields{'subject'}.
                    786: 		      '</i> ');
                    787: 	}
                    788: 	$r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
1.42      www       789: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
1.152     musolffc  790: 	          '<td></td>'.
1.25      www       791: 		  "</tr>\n");
1.64      raeburn   792:     }
1.154     raeburn   793:     return;
1.21      foxr      794: }
1.152     musolffc  795: 
                    796: sub getTitle {
                    797:     my ($resdir, $targetdir, $filename, $linkfilename, $meta_same, $bombs) = @_;
                    798:     my $title='';
                    799:     my $titleString = &getTitleString($targetdir.'/'.$filename);
                    800:     if (-e $resdir.'/'.$filename) {
                    801: 	$title = '<a href="'.$targetdir.'/'.$filename.
                    802: 	    '.meta" target="cat">'.$titleString.'</a>';
                    803:         if (!$meta_same) {
                    804: 	    $title = &mt('Metadata Modified').'<br />'.$title.
                    805: 		'<br />'.
                    806:                 &Apache::loncommon::modal_link(
                    807:                     '/adm/diff?filename='.$linkfilename.'.meta'.'&amp;versiontwo=priv',
                    808:                     &mt('Metadata Diffs'),600,500);
                    809: 	    $title.="\n".'<br />'.
                    810:                 &Apache::loncommon::modal_link(
                    811:                     '/adm/retrieve?filename='.$linkfilename.'.meta&amp;inhibitmenu=yes&amp;add_modal=yes',
                    812:                     &mt('Retrieve Metadata'),600,500);
                    813:         } 
                    814:     }
                    815:     # Allow editing metadata of published and unpublished resources
                    816:     $title .= "\n".'<br />' if ($title);
                    817:     $title .= '<a href="'.$linkfilename.'.meta">'.
                    818:               ($$bombs{&Apache::lonnet::declutter($targetdir.'/'.$filename)}?
                    819:                   '<img src="/adm/lonMisc/bomb.gif" border="0" alt="'.&mt('bomb').'" />':
                    820:                   &mt('Edit Metadata')).
                    821:               '</a>';
                    822: 
                    823:     return ($title, $titleString);
                    824: }
                    825: 
                    826: 
                    827: sub isMetaSame {
                    828:     my ($cstr_dir, $resdir, $filename) = @_;
                    829:     my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
                    830:     my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
                    831:     return (&Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
                    832:             $cstr_dir.'/'.$filename.'.meta') && $meta_rmtime < $meta_cmtime) 
                    833:         ? 0 : 1;
                    834: }
                    835:     
                    836: 
                    837: sub getStatus {    
                    838:     my ($resdir, $targetdir, $cstr_dir, $filename,  
                    839:             $linkfilename, $cmtime, $meta_same) = @_;
1.64      raeburn   840:     my $pubstatus = 'unpublished';
1.152     musolffc  841:     my $status = &mt('Unpublished');
1.129     www       842: 
1.22      foxr      843:     if (-e $resdir.'/'.$filename) {
1.152     musolffc  844:         my $same = 0;
                    845:         if ((stat($resdir.'/'.$filename))[9] >= $cmtime) {
                    846:             $same = 1;
1.91      www       847:         } else {
                    848:            if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
1.95      albertel  849: 						     $cstr_dir.'/'.$filename)) {
1.152     musolffc  850:               $same = 0;
1.91      www       851:            } else {
1.152     musolffc  852:               $same = 1;
1.91      www       853:            }
                    854:         }
1.124     bisitz    855: 
                    856:         my $rights_status =
                    857:             &mt(&getCopyRightString($targetdir.'/'.$filename)).', ';
                    858: 
                    859:         my %lt_SourceRight = &Apache::lonlocal::texthash(
                    860:                'open'   => 'Source: open',
                    861:                'closed' => 'Source: closed',
                    862:         );
                    863:         $rights_status .=
                    864:             $lt_SourceRight{&getSourceRightString($targetdir.'/'.$filename)};
                    865: 
1.91      www       866: 	if ($same) {
1.40      www       867: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
1.64      raeburn   868:                 $pubstatus = 'obsolete';
1.41      www       869: 		$status=&mt('Obsolete');
1.95      albertel  870:             } else {
                    871: 		if (!$meta_same) {
                    872: 		    $pubstatus = 'metamodified';
                    873: 		} else {
                    874: 		    $pubstatus = 'published';
                    875: 		}
                    876: 		$status=&mt('Published').
                    877: 		    '<br />'. $rights_status;
                    878: 	    }
1.22      foxr      879: 	} else {
1.64      raeburn   880:             $pubstatus = 'modified';
1.95      albertel  881: 	    $status=&mt('Modified').
                    882: 		'<br />'. $rights_status;
1.22      foxr      883: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
1.139     www       884: 		$status.='<br />'.
                    885:                          &Apache::loncommon::modal_link(
                    886:                              '/adm/diff?filename='.$linkfilename.'&amp;versiontwo=priv',
                    887:                              &mt('Diffs'),600,500);
1.22      foxr      888: 	    }
1.95      albertel  889: 	} 
1.152     musolffc  890: 
1.140     www       891: 	$status.="\n".'<br />'.
                    892:              &Apache::loncommon::modal_link(
                    893:                  '/adm/retrieve?filename='.$linkfilename.'&amp;inhibitmenu=yes&amp;add_modal=yes',&mt('Retrieve'),600,500);
1.22      foxr      894:     }
1.152     musolffc  895: 
                    896:     return ($status, $pubstatus);
                    897: }
                    898: 
                    899: 
                    900: #
                    901: #   Put a table row for a file resource.
                    902: #
                    903: sub putresource {
                    904:     my ($r, $udom, $uname, $filename, $thisdisfn, $resdir, $targetdir, 
                    905:             $linkdir, $cmtime, $size, $numres, $linkfilename, $title, 
                    906:             $status, $pubstatus) = @_;
                    907:     &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
1.143     bisitz    908: 
1.33      www       909:     my $editlink='';
1.38      taceyjo1  910:     my $editlink2='';
1.36      www       911:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
1.146     raeburn   912: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
1.34      www       913:     }
1.136     www       914:     if ($filename=~/$LONCAPA::assess_re/) {
1.146     raeburn   915: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=editxml">'.&mt('EditXML').'</a>)';
                    916: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
1.165     damieng   917:         if ($env{'browser.type'} ne 'explorer' || $env{'browser.version'} > 9) {
1.166   ! damieng   918:             my $daxeurl = '/daxepage'.$linkdir.'/'.$filename;
1.165     damieng   919:             $editlink .= ' (<a href="'.$daxeurl.'" target="_blank">Daxe</a>)';
                    920:         }
1.43      taceyjo1  921:     }
1.82      www       922:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
1.129     www       923: 	$editlink.=' (<a href="/adm/cleanup?filename='.$linkfilename.'" target="_parent">'.&mt('Clean Up').')</a>';
1.82      www       924:     }
1.43      taceyjo1  925:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
1.129     www       926: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress='.$linkfilename.'">'.&mt('Decompress').'</a>)';
1.33      www       927:     }
1.152     musolffc  928:     my $publish_button = (-e $resdir.'/'.$filename) ? &mt('Re-publish') : &mt('Publish');
1.64      raeburn   929:     my $pub_select = '';
                    930:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
1.115     bisitz    931:     $r->print(&Apache::loncommon::start_data_table_row().
1.53      www       932: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
1.86      albertel  933: 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
1.64      raeburn   934:               '<td>'.$pub_select.'</td>'.
1.104     albertel  935: 	      '<td><span class="LC_filename">'.
1.64      raeburn   936: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
1.92      albertel  937:                $filename.'</a></span>'.$editlink2.$editlink.
1.22      foxr      938: 	      '</td>'.
                    939: 	      '<td>'.$title.'</td>'.
1.115     bisitz    940:               '<td class="LC_browser_file_'.$pubstatus.'">&nbsp;&nbsp;</td>'. # Display publication status
                    941:               '<td>'.$status.'</td>'.
1.42      www       942: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
1.153     raeburn   943: 	      '<td>'.sprintf("%.1f",$size).'</td>'.
1.115     bisitz    944: 	      &Apache::loncommon::end_data_table_row()
                    945:     );
1.154     raeburn   946:     return;
1.23      foxr      947: }
1.64      raeburn   948: 
                    949: sub create_pubselect {
                    950:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
                    951:     $$pub_select = '
                    952: <form name="resselect_'.$$numres.'" action="">
1.85      albertel  953: <select name="reschoice"  onchange="SetResChoice(this.form)">
1.77      albertel  954: <option>'.&mt('Select action').'</option>'.
                    955: '<option value="copy">'.&mt('Copy').'</option>';
1.68      raeburn   956:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
                    957:         $$pub_select .= 
1.77      albertel  958: '<option value="rename">'.&mt('Rename').'</option>'.
                    959: '<option value="move">'.&mt('Move').'</option>'.
                    960: '<option value="delete">'.&mt('Delete').'</option>';
1.64      raeburn   961:     } else {
                    962:         $$pub_select .= '
1.77      albertel  963: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
1.64      raeburn   964:     }
                    965: # check for versions
                    966:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
                    967:     if ($versions > 0) {
                    968:         $$pub_select .='
1.77      albertel  969: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
1.64      raeburn   970:     }
                    971:     $$pub_select .= '
1.77      albertel  972: <option value="publish">'.$publish_button.'</option>'.
1.82      www       973: '<option value="cleanup">'.&mt('Clean up').'</option>'.
1.77      albertel  974: '<option value="print">'.&mt('Print').'</option>'.
1.68      raeburn   975: '</select>
1.131     www       976: <input type="hidden" name="filename" value="/priv'.
                    977:  &HTML::Entities::encode($thisdisfn.'/'.$filename,'<>&"').'" />
1.96      banghart  978:  <input type="hidden" name="dispfilename" value="'.
1.99      albertel  979:  &HTML::Entities::encode($filename).'" /></form>';
1.64      raeburn   980:     $$numres ++;
                    981: }
                    982: 
                    983: sub check_for_versions {
                    984:     my ($r,$fn,$udom,$uname) = @_;
                    985:     my $versions = 0;
                    986:     my $docroot=$r->dir_config('lonDocRoot');
                    987:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
                    988:     my $resdir=$resfn;
                    989:     $resdir=~s/\/[^\/]+$/\//;
                    990:     $fn=~/\/([^\/]+)\.(\w+)$/;
                    991:     my $main=$1;
                    992:     my $suffix=$2;
                    993:     opendir(DIR,$resdir);
                    994:     while (my $filename=readdir(DIR)) {
                    995:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
                    996:             $versions ++;        
                    997:         }
                    998:     }
1.154     raeburn   999:     closedir(DIR);
1.64      raeburn  1000:     return $versions;
                   1001: }
                   1002: 
1.161     golterma 1003: sub prepareJsonTranslations {
                   1004:     my $json = 
                   1005:         '{"translations":{'.
                   1006:             '"edit":"'.&mt('Edit').'",'.
                   1007:             '"editxml":"'.&mt('EditXML').'",'.
                   1008:             '"editmeta":"'.&mt('Edit Metadata').'",'.
                   1009:             '"obsolete":"'.&mt('Obsolete').'",'.
                   1010:             '"modified":"'.&mt('Modified').'",'.
                   1011:             '"published":"'.&mt('Published').'",'.
                   1012:             '"unpublished":"'.&mt('Unpublished').'",'.
                   1013:             '"diff":"'.&mt('Diff').'",'.
                   1014:             '"retrieve":"'.&mt('Retrieve').'",'.
                   1015:             '"directory":"'.&mt('Directory').'",'.
                   1016:             '"results":"'.&mt('Show results for keyword:').'"'.
                   1017:         '}}';
                   1018: }
                   1019: 
                   1020: # gathers all files in the working directory except the ones that are already on screen
                   1021: sub prepareJsonData {
                   1022:     my ($uname, $udom, $pathToSkip) = @_;
                   1023:     my $path = "/home/httpd/html/priv/$udom/$uname/";
                   1024: 
                   1025:     # maximum number of entries, to limit workload and required storage space
                   1026:     my $entries = 100;
                   1027:     my $firstfile = 1;
                   1028:     my $firstdir = 1;
                   1029: 
                   1030:     my $json = '{"resources":[';
                   1031:     $json .= &prepareJsonData_rec($path, \$entries, \$firstfile, \$firstdir, $pathToSkip);
                   1032:     $json .= ']}';
                   1033: 
                   1034:     # if the json string is invalid the whole search breaks.
                   1035:     # so we want to make sure that the string is valid in any case.
                   1036:     $json =~ s/,\s*,/,/g;
                   1037:     $json =~ s/\}\s*\{/\},\{/g;
                   1038:     $json =~ s/\}\s*,\s*\]/\}\]/g;
                   1039:     return $json;
                   1040: }
                   1041: 
                   1042: # recursive part of json file gathering
                   1043: sub prepareJsonData_rec {
                   1044:     my ($path, $entries, $firstfile, $firstdir, $pathToSkip) = @_;
                   1045:     my $json;
                   1046:     my $skipThisFolder = $path =~ m/$pathToSkip\/$/?1:0;
                   1047: 
                   1048:     my @dirs;
                   1049:     my @resources;
                   1050:     my @ignored = qw(bak log meta save . ..);
                   1051: 
                   1052: # Phase 1: Gathering
                   1053:     opendir(DIR,$path);
                   1054:     my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
                   1055:     foreach my $filename (@files) {
                   1056:         next if ($filename eq '.DS_Store');
                   1057: 
                   1058:         # gather all resources
                   1059:         if ($filename !~ /\./) {
                   1060:             # its a folder
                   1061:             push(@dirs, $filename);
                   1062:         } else {
                   1063:             # only push files we dont want to ignore
                   1064:             next if ($skipThisFolder);
                   1065: 
                   1066:             $filename =~ /\.(\w+?)$/;
                   1067:             unless (grep /$1/, @ignored) {
                   1068:                 push(@resources, $filename);
                   1069:             }
                   1070:         }
                   1071:     }
                   1072:     closedir(DIR);
                   1073:     # nothing to do here if both lists are empty
                   1074:     return unless ( @dirs || @resources );
                   1075:     
                   1076: # Phase 2: Working
                   1077:     $$firstfile = 1;
                   1078: 
                   1079:     foreach (@dirs) {
                   1080:         $json .= '{"name":"'.$_.'",'.
                   1081:                   '"path":"'.$path.$_.'",'.
                   1082:                   '"title":"",'.
                   1083:                   '"status":"",'.
                   1084:                   '"cmtime":""},';
                   1085:     }
                   1086: 
                   1087:     foreach (@resources) {
                   1088:         last if ($$entries < 1);
                   1089:         my $title = &getTitleString($path.$_);
                   1090: 
                   1091:         my $privpath = $path.$_;
                   1092:         my $respath = $privpath;
                   1093:         $respath =~ s/httpd\/html\/priv\//httpd\/html\/res\//;
                   1094: 
                   1095:         my $cmtime = (stat($privpath))[9];
                   1096:         my $rmtime = (stat($respath))[9];
                   1097: 
                   1098:         unless ($$firstfile) { $json .= ','; } else { $$firstfile = 0; }
                   1099: 
                   1100:         my $status = 'unpublished';
                   1101: 
                   1102:         # if a resource is published, the published version (/html/res/filepath) gets its own modification time
                   1103:         # this is newer or equal then the version in your authoring space (/html/priv/filepath)
                   1104:         if ($rmtime >= $cmtime) {
                   1105:             # obsolete
                   1106:             if (&Apache::lonnet::metadata($respath, 'obsolete')) {
                   1107:                 $status = 'obsolete';
                   1108:             }else{
                   1109:                 $status = 'published';
                   1110:             }
                   1111:         } else {
                   1112:             $status = 'modified';
                   1113:         }
                   1114: 
                   1115:         $json .= '{"name":"'.$_.'",'.
                   1116:                   '"path":"'.$path.'",'.
                   1117:                   '"title":"'.$title.'",'.
                   1118:                   '"status":"'.$status.'",'.
                   1119:                   '"cmtime":"'.&Apache::lonlocal::locallocaltime($cmtime).'"}';
                   1120:         $$entries--;
                   1121:     }
                   1122: 
                   1123:     foreach(@dirs) {
                   1124:         next if ($$entries < 1);
                   1125:         $json .= ',';
                   1126:         $json .= &prepareJsonData_rec
                   1127:                     ($path.$_.'/', $entries, $firstfile, $firstdir, $pathToSkip);
                   1128:     }
                   1129:     return $json;
                   1130: }
1.4       www      1131: 1;
                   1132: __END__
1.17      harris41 1133: 
                   1134: 
1.114     jms      1135: =head1 NAME
                   1136: 
1.145     raeburn  1137: Apache::lonpubdir - Authoring space directory lister
1.114     jms      1138: 
                   1139: =head1 SYNOPSIS
                   1140: 
                   1141: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
                   1142: 
1.134     raeburn  1143:  <LocationMatch "^/+priv.*/$">
1.114     jms      1144:  PerlAccessHandler       Apache::loncacc
                   1145:  SetHandler perl-script
                   1146:  PerlHandler Apache::lonpubdir
                   1147:  ErrorDocument     403 /adm/login
                   1148:  ErrorDocument     404 /adm/notfound.html
                   1149:  ErrorDocument     406 /adm/unauthorized.html
                   1150:  ErrorDocument	  500 /adm/errorhandler
                   1151:  </LocationMatch>
                   1152: 
                   1153:  <Location /adm/pubdir>
                   1154:  PerlAccessHandler       Apache::lonacc
                   1155:  SetHandler perl-script
                   1156:  PerlHandler Apache::lonpubdir
                   1157:  ErrorDocument     403 /adm/login
                   1158:  ErrorDocument     404 /adm/notfound.html
                   1159:  ErrorDocument     406 /adm/unauthorized.html
                   1160:  ErrorDocument	  500 /adm/errorhandler
                   1161:  </Location>
                   1162: 
                   1163: =head1 INTRODUCTION
                   1164: 
                   1165: This module publishes a directory of files.
                   1166: 
                   1167: This is part of the LearningOnline Network with CAPA project
                   1168: described at http://www.lon-capa.org.
                   1169: 
                   1170: =head1 HANDLER SUBROUTINE
                   1171: 
                   1172: This routine is called by Apache and mod_perl.
                   1173: 
                   1174: =over 4
                   1175: 
                   1176: =item *
                   1177: 
                   1178: read in information
                   1179: 
                   1180: =item *
                   1181: 
                   1182: start page output
                   1183: 
                   1184: =item *
                   1185: 
                   1186: run through list of files and attempt to publish unhidden files
                   1187: 
                   1188: =back
                   1189: 
                   1190: =head1 SUBROUTINES:
                   1191: 
                   1192: =over
                   1193: 
                   1194: =item startpage($r, $uame, $udom, $thisdisfn)
                   1195: 
                   1196: Output the header of the page.  This includes:
                   1197:  - The HTML header 
                   1198:  - The H1/H3  stuff which includes the directory.
                   1199:  
                   1200:     startpage($r, $uame, $udom, $thisdisfn);
                   1201:         $r     - The apache request object.
                   1202:         $uname - User name.
                   1203:         $udom  - Domain name the user is logged in under.
                   1204:         $thisdisfn - Displayable version of the filename.
                   1205: 
                   1206: =item getTitleString($fullname)
                   1207: 
                   1208:     Get the title string or "[untitled]" if the file has no title metadata:
                   1209:     Without the latter substitution, it's impossible to examine metadata for
                   1210:     untitled resources.  Resources may be legitimately untitled, to prevent
                   1211:     searches from locating them.
                   1212:     
                   1213:     $str = getTitleString($fullname);
                   1214:         $fullname - Fully qualified filename to check.
                   1215: 
1.134     raeburn  1216: =item putdirectory($r, $base, $here, $dirname, $modtime, $targetdir, $bombs,
                   1217:                    $numdir)
1.114     jms      1218: 
                   1219:     Put out a directory table row:
                   1220:     
1.134     raeburn  1221:         $r        - Apache request object.
                   1222:         $reqfile  - File in request.
                   1223:         $here     - Where we are in directory tree.
                   1224:         $dirname  - Name of directory special file.
                   1225:         $modtime  - Encoded modification time.
                   1226:         targetdir - Publication target directory.
                   1227:         bombs     - Reference to hash of URLs with runtime error messages.
                   1228:         numdir    - Reference to scalar used to track number of sub-directories
                   1229:                     in directory (used in form name for each "actions" dropdown).
1.114     jms      1230: 
                   1231: =back
                   1232: 
                   1233: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>