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

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

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