File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.151: download - view: text, annotated - select for diffs
Sun Apr 6 19:39:04 2014 UTC (10 years, 2 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Re-instate &mt() for column headings.

    1: # The LearningOnline Network with CAPA
    2: # Authoring Space Directory Lister
    3: #
    4: # $Id: lonpubdir.pm,v 1.151 2014/04/06 19:39:04 raeburn Exp $
    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/
   27: #
   28: ###
   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);
   36: use Apache::loncommon();
   37: use Apache::lonhtmlcommon();
   38: use Apache::londiff();
   39: use Apache::lonlocal;
   40: use Apache::lonmsg;
   41: use Apache::lonmenu;
   42: use Apache::lonnet;
   43: use LONCAPA;
   44: 
   45: sub handler {
   46: 
   47:   my $r=shift;
   48: 
   49:   # Validate access to the construction space and get username:domain.
   50: 
   51:   my $uname;
   52:   my $udom;
   53: 
   54:   ($uname,$udom)=&Apache::lonnet::constructaccess($r->uri); 
   55:   unless (($uname) && ($udom)) {
   56:      return HTTP_NOT_ACCEPTABLE;
   57:   }
   58: 
   59: # ----------------------------------------------------------- Start page output
   60: 
   61:   my $fn=$r->filename;
   62:   $fn=~s/\/$//;
   63: 
   64:   my $thisdisfn=$fn;
   65:   my $docroot=$r->dir_config('lonDocRoot');     # Apache  londocument root.
   66:   $thisdisfn=~s/^\Q$docroot\E\/priv//;
   67: 
   68:   my $resdir=$docroot.'/res'.$thisdisfn; # Resource directory
   69:   my $targetdir='/res'.$thisdisfn; # Publication target directory.
   70:   my $linkdir='/priv'.$thisdisfn;      # Full URL name of constr space.
   71: 
   72:   my %bombs=&Apache::lonmsg::all_url_author_res_msg($uname,$udom);
   73: 
   74:   &startpage($r, $uname, $udom, $thisdisfn);   # Put out the start of page.
   75:   &dircontrols($r,$uname,$udom,$thisdisfn);    # Put out actions for directory, 
   76:                                                # browse/upload + new file page.
   77:   &resourceactions($r,$uname,$udom,$thisdisfn); #Put out form used for printing/deletion etc.
   78: 
   79:   my $numdir = 0;
   80:   my $numres = 0;
   81:   
   82:   # Retrieving value for sortby from QUERY_STRING
   83:   &Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},['sortby']);
   84: 
   85:   # Default sort by 'Name'
   86:   if (! exists($env{'form.sortby'})) {
   87:     $env{'form.sortby'} = 'filename';
   88:   }
   89: 
   90:   # Start off the directory table.
   91:   $r->print(&Apache::loncommon::start_data_table()
   92:            .&Apache::loncommon::start_data_table_header_row()
   93:            .'<th>'.&mt('Type').'</th>'
   94:            .'<th>'.&mt('Actions').'</th>'
   95:            .'<th><a href="'.$linkdir.'/?sortby=filename">'.&mt('Name').'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
   96:            .'<th>'.&mt('Title').'</th>'
   97:            .'<th colspan="2">'.&mt('Status').'</th>'
   98:            .'<th><a href="'.$linkdir.'/?sortby=cmtime">'.&mt('Last Modified').'<span class="LC_fontsize_small"> &#9660;</span></a></th>'
   99:            .&Apache::loncommon::end_data_table_header_row()
  100:   );
  101: 
  102:   my $filename;
  103:   my $dirptr=16384;		# Mask indicating a directory in stat.cmode.
  104: 
  105:   opendir(DIR,$fn);
  106:   my $filehash = {};
  107:   my $sortby = $env{'form.sortby'};
  108:   my @files=sort {uc($a) cmp uc($b)} (readdir(DIR));
  109:   foreach my $filename (@files) {
  110:      next if ($filename eq '.DS_Store');
  111:      my ($cmode,$cmtime)=(stat($fn.'/'.$filename))[2,9];
  112:      # If you want to sort by "last modified", we need to make this hash.
  113:      if ($sortby eq 'cmtime') {
  114:        $filehash->{ $filename } = {"cmtime" => $cmtime,};
  115:      }
  116:      # Otherwise sort by name.  Don't bother with filehash.  Continue printing contents.
  117:      else {
  118:          my $extension='';
  119:          if ($filename=~/\.(\w+)$/) { $extension=$1; }
  120:          if ($cmode&$dirptr) {
  121:              &putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
  122:          } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
  123:              &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir, 
  124:                          $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
  125:          } else {
  126:             # "hidden" extension and not a directory, so hide it away.
  127:          }
  128:      }
  129:   };
  130: 
  131:   # Sorting files by "last modified" if that's what you selected   
  132:   if ($sortby eq 'cmtime') {
  133:       my @sorted_files = sort {
  134:         $filehash->{$b}->{$sortby} <=> $filehash->{$a}->{$sortby}
  135:         } (keys(%{$filehash}));
  136:       foreach my $filename (@sorted_files) {
  137:          my ($cmode,$cmtime)=(stat($fn.'/'.$filename))[2,9];
  138:          my $extension='';
  139:          if ($filename=~/\.(\w+)$/) { $extension=$1; }
  140:          if ($cmode&$dirptr) {
  141:              &putdirectory($r, $thisdisfn, $linkdir, $filename, $cmtime,$targetdir,\%bombs,\$numdir);
  142:          } elsif (&Apache::loncommon::fileembstyle($extension) ne 'hdn') {
  143:              &putresource($r, $udom, $uname, $filename, $thisdisfn, $resdir, 
  144:                          $targetdir, $linkdir, $cmtime,\%bombs,\$numres);
  145:          } else {
  146:             # "hidden" extension and not a directory, so hide it away.
  147:          };
  148:       };
  149:   };
  150:   
  151:   closedir(DIR);
  152: 
  153:   $r->print(&Apache::loncommon::end_data_table()
  154:            .&Apache::loncommon::end_page()
  155:   );
  156:   return OK;  
  157: }
  158: 
  159: #
  160: #   Output the header of the page.  This includes:
  161: #   - The HTML header 
  162: #   - The H1/H3  stuff which includes the directory.
  163: #
  164: #     startpage($r, $uame, $udom, $thisdisfn);
  165: #      $r     - The apache request object.
  166: #      $uname - User name.
  167: #      $udom  - Domain name the user is logged in under.
  168: #      $thisdisfn - Displayable version of the filename.
  169: 
  170: sub startpage {
  171:     my ($r, $uname, $udom, $thisdisfn) = @_;
  172:     &Apache::loncommon::content_type($r,'text/html');
  173:     $r->send_http_header;
  174: 
  175:     my $formaction='/priv'.$thisdisfn.'/';
  176:     $formaction=~s|/+|/|g;
  177:     &Apache::lonhtmlcommon::store_recent('construct',$formaction,$formaction);
  178: 
  179:     &Apache::lonhtmlcommon::clear_breadcrumbs();
  180:     &Apache::lonhtmlcommon::add_breadcrumb({
  181:         'text'  => 'Authoring Space',
  182:         'href'  => &Apache::loncommon::authorspace($formaction),
  183:     });
  184:     # breadcrumbs (and tools) will be created 
  185:     # in start_page->bodytag->innerregister
  186: 
  187:     $env{'request.noversionuri'}=$formaction;
  188:     $r->print(&Apache::loncommon::start_page('Authoring Space',undef));
  189: 
  190:     my $londocroot = $Apache::lonnet::perlvar{'lonDocRoot'};
  191:     my $current_disk_usage = &Apache::lonnet::diskusage($udom,$uname,"$londocroot/priv/$udom/$uname");
  192:     my $disk_quota = &Apache::loncommon::get_user_quota($uname,$udom,'author'); #expressed in Mb
  193:     $disk_quota = 1000 * $disk_quota; # convert from Mb to kb
  194: 
  195:     $r->print(&Apache::loncommon::head_subbox(
  196:                      '<div style="float:right;padding-top:0;margin-top;0">'
  197:                     .&Apache::lonhtmlcommon::display_usage($current_disk_usage,$disk_quota)
  198:                     .'</div>'
  199:                     .&Apache::loncommon::CSTR_pageheader()));
  200: 
  201:     my $esc_thisdisfn = &Apache::loncommon::escape_single($thisdisfn);
  202:     my $doctitle = 'LON-CAPA '.&mt('Authoring Space');
  203:     my $newname = &mt('New Name');
  204:     my $pubdirscript=(<<ENDPUBDIRSCRIPT);
  205: <script type="text/javascript">
  206: top.document.title = '$esc_thisdisfn/ - $doctitle';
  207: // Store directory location for menu bar to find
  208: 
  209: parent.lastknownpriv='/priv$esc_thisdisfn/';
  210: 
  211: // Confirmation dialogues
  212: 
  213:     function currdiract(theform) {
  214:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'publish') {
  215:             document.publishdir.filename.value = theform.filename.value;
  216: 	    document.publishdir.submit();
  217:         }
  218:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'editmeta') {
  219:             top.location=theform.filename.value+'default.meta'
  220:         }
  221:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == 'printdir' ) {
  222:             document.printdir.postdata.value=theform.filename.value
  223:             document.printdir.submit();
  224:         }
  225:         if (theform.dirtask.options[theform.dirtask.selectedIndex].value == "delete") {
  226:               var delform = document.delresource
  227:               delform.filename.value = theform.filename.value
  228:               delform.submit()
  229:         }
  230:     }
  231:   
  232:     function checkUpload(theform) {
  233:         if (theform.file == '') {
  234:             alert("Please use 'Browse..' to choose a file first, before uploading")
  235:             return 
  236:         }
  237:         theform.submit()  
  238:     }
  239: 
  240:     function SetPubDir(theform,printForm) {
  241:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "open") {
  242:             top.location = theform.openname.value
  243:             return
  244:         }
  245:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "publish") {
  246:             theform.submit();
  247:         }
  248:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "editmeta") {
  249:             top.location=theform.filename.value+'default.meta'
  250:         }
  251:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "printdir") {
  252:             theform.action = '/adm/printout'
  253:             theform.postdata.value = theform.filename.value
  254:             theform.submit()
  255:         }
  256:         if (theform.diraction.options[theform.diraction.selectedIndex].value == "delete") {
  257:               var delform = document.delresource
  258:               delform.filename.value = theform.filename.value
  259:               delform.submit()
  260:         }
  261:         return
  262:     }
  263:     function SetResChoice(theform) {
  264:       var activity = theform.reschoice.options[theform.reschoice.selectedIndex].value
  265:       if ((activity == 'rename') || (activity == 'copy') || (activity == 'move')) {
  266:           changename(theform,activity)
  267:       }
  268:       if (activity == 'publish') {
  269:           var pubform = document.pubresource
  270:           pubform.filename.value = theform.filename.value
  271:           pubform.submit()
  272:       }
  273:       if (activity == 'delete') {
  274:           var delform = document.delresource
  275:           delform.filename.value = theform.filename.value
  276:           delform.submit()
  277:       }
  278:       if (activity == 'obsolete') {
  279:           var pubform = document.pubresource
  280:           pubform.filename.value = theform.filename.value
  281:           pubform.makeobsolete.value=1;
  282:           pubform.submit()
  283:       }
  284:       if (activity == 'print') {
  285:           document.printresource.postdata.value = theform.filename.value
  286:           document.printresource.submit()
  287:       }
  288:       if (activity == 'retrieve') {
  289:           document.retrieveres.filename.value = theform.filename.value
  290:           document.retrieveres.submit()
  291:       }
  292:       if (activity == 'cleanup') {
  293:           document.cleanup.filename.value = theform.filename.value
  294:           document.cleanup.submit()
  295:       }
  296:       return
  297:     }
  298:     function changename(theform,activity) {
  299:         var oldname=theform.dispfilename.value;
  300:         var newname=prompt('$newname',oldname);
  301:         if (newname == "" || !newname || newname == oldname)  {
  302:             return
  303:         }
  304:         document.moveresource.newfilename.value = newname
  305:         document.moveresource.filename.value = theform.filename.value
  306:         document.moveresource.action.value = activity
  307:         document.moveresource.submit();
  308:     }
  309: </script>
  310: ENDPUBDIRSCRIPT
  311:     $r->print($pubdirscript);
  312: }
  313: 
  314: sub dircontrols {
  315:     my ($r,$uname,$udom,$thisdisfn) = @_;
  316:     my %lt=&Apache::lonlocal::texthash(
  317:                                        cnpd => 'Cannot publish directory',
  318:                                        cnrd => 'Cannot retrieve directory',
  319:                                        mcdi => 'Must create new subdirectory inside a directory',
  320:                                        pubr => 'Publish this Resource',
  321:                                        pubd => 'Publish this Directory',
  322:                                        dedr => 'Delete Directory',
  323:                                        rtrv => 'Retrieve Old Version',
  324:                                        list => 'List Directory',
  325:                                        uplo => 'Upload file',  
  326:                                        dele => 'Delete',
  327:                                        edit => 'Edit Metadata', 
  328:                                        sela => 'Select Action',
  329:                                        nfil => 'New file',
  330:                                        nhtm => 'New HTML file',
  331:                                        nprb => 'New problem',
  332:                                        npag => 'New assembled page',
  333:                                        nseq => 'New assembled sequence',
  334:                                        ncrf => 'New custom rights file',
  335:                                        nsty => 'New style file',
  336:                                        nlib => 'New library file',
  337:                                        nbt  => 'New bridgetask file',
  338:                                        nsub => 'New subdirectory',
  339:                                        renm => 'Rename current file to',
  340:                                        move => 'Move current file to',
  341:                                        copy => 'Copy current file to',
  342:                                        type => 'Type Name Here',
  343:                                        go   => 'Go',
  344:                                        prnt => 'Print contents of directory',
  345:                                        crea => 'Create a new directory or LON-CAPA document',
  346: 				       acti => 'Actions for current directory',
  347: 				       updc => 'Upload a new document',
  348: 				       pick => 'Please select an action to perform using the new filename',
  349:                                       );
  350:     my $mytype = $lt{'type'}; # avoid conflict with " and ' in javascript
  351:     $r->print(<<END);
  352: <div class="LC_columnSection">
  353:   <div>
  354:     <form name="curractions" method="post" action="">
  355:       <fieldset>
  356:         <legend>$lt{'acti'}</legend>
  357:         <select name="dirtask" onchange="currdiract(this.form)">
  358:             <option>$lt{'sela'}</option>
  359:             <option value="publish">$lt{'pubd'}</option>
  360:             <option value="editmeta">$lt{'edit'}</option>
  361:             <option value="printdir">$lt{'prnt'}</option>
  362:             <option value="delete">$lt{'dedr'}</option>
  363:         </select>
  364:         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
  365:       </fieldset>
  366:     </form>
  367:     <form name="publishdir" method="post" action="/adm/publish" target="_parent">
  368:       <input type="hidden" name="pubrec" value="" />
  369:       <input type="hidden" name="filename" value="" />
  370:     </form>
  371:     <form name="printdir" method="post" action="/adm/printout" target="_parent">
  372:       <input type="hidden" name="postdata" value="" />
  373:     </form>
  374:   </div>
  375: 
  376:   <div>
  377:     <form name="upublisher" enctype="multipart/form-data" method="post" action="/adm/upload" target="_parent">
  378:       <fieldset>
  379:         <legend>$lt{'updc'}</legend>
  380:         <input type="hidden" name="filename" value="/priv$thisdisfn/" />
  381:         <input type="file" name="upfile" size="20" />
  382:         <input type="button" value="$lt{'uplo'}"  onclick="checkUpload(this.form)" />
  383:       </fieldset>
  384:     </form>
  385:   </div>
  386: 
  387:   <div>
  388:     <form name="fileaction" method="post" action="/adm/cfile" target="_parent">
  389:       <fieldset>
  390:               <legend>$lt{'crea'}</legend>
  391: 	      <span class="LC_nobreak">
  392: 		<input type="hidden" name="filename" value="/priv$thisdisfn/" />
  393:                   <script type="text/javascript">
  394:                     function validate_go() {
  395:                         var selected = document.fileaction.action.selectedIndex;
  396:                         if (selected == 0) {
  397:                             alert('$lt{'pick'}');
  398:                         } else {
  399:                             document.fileaction.submit();
  400:                         }
  401:                     }
  402:                   </script>
  403: 		  <select name="action">
  404: 		    <option value="none">$lt{'sela'}</option>
  405: 		    <option value="newfile">$lt{'nfil'}:</option>
  406: 		    <option value="newhtmlfile">$lt{'nhtm'}:</option>
  407: 		    <option value="newproblemfile">$lt{'nprb'}:</option>
  408:                     <option value="newpagefile">$lt{'npag'}:</option>
  409:                     <option value="newsequencefile">$lt{'nseq'}:</option>
  410:                     <option value="newrightsfile">$lt{'ncrf'}:</option>
  411:                     <option value="newstyfile">$lt{'nsty'}:</option>
  412:                     <option value="newtaskfile">$lt{'nbt'}:</option>
  413:                     <option value="newlibraryfile">$lt{'nlib'}:</option>
  414: 	            <option value="newdir">$lt{'nsub'}:</option>
  415: 		  </select>&nbsp;<input type="text" name="newfilename" value="$lt{'type'}" onfocus="if (this.value == '$mytype') this.value=''" />&nbsp;<input type="button" value="Go" onclick="validate_go();" />
  416: 		 </span>
  417:       </fieldset>
  418:     </form>
  419:   </div>
  420: </div>
  421: END
  422: }
  423: 
  424: sub resourceactions {
  425:     my ($r,$uname,$udom,$thisdisfn) = @_;
  426:     $r->print(<<END);
  427:        <form name="moveresource" action="/adm/cfile" target="_parent" method="post">
  428:          <input type="hidden" name="filename" value="" />
  429:          <input type="hidden" name="newfilename" value="" />
  430:          <input type="hidden" name="action" value="" />
  431:        </form>
  432:        <form name="delresource" action="/adm/cfile" target="_parent" method="post">
  433:          <input type="hidden" name="filename" value="" />
  434:          <input type="hidden" name="action" value="delete" />
  435:        </form>
  436:        <form name="pubresource" action="/adm/publish" target="_parent" method="post">
  437:          <input type="hidden" name="filename" value="" />
  438:          <input type="hidden" name="makeobsolete" value="0" />
  439:        </form>
  440:        <form name="printresource" action="/adm/printout" target="_parent" method="post">
  441:            <input type="hidden" name="postdata" value="" />
  442:        </form>
  443:        <form name="retrieveres" action="/adm/retrieve" target="_parent" method="post">
  444:            <input type="hidden" name="filename" value="" />
  445:        </form>
  446:        <form name="cleanup" action="/adm/cleanup" target="_parent" method="post">
  447:            <input type="hidden" name="filename" value="" />
  448:        </form>
  449: END
  450: }
  451: 
  452: #
  453: #   Get the title string or "[untitled]" if the file has no title metadata:
  454: #   Without the latter substitution, it's impossible to examine metadata for
  455: #   untitled resources.  Resources may be legitimately untitled, to prevent
  456: #   searches from locating them.
  457: #
  458: #   $str = getTitleString($fullname);
  459: #       $fullname - Fully qualified filename to check.
  460: #
  461: sub getTitleString {
  462:     my $fullname = shift;
  463:     my $title    = &Apache::lonnet::metadata($fullname, 'title');
  464: 
  465:     unless ($title) {
  466: 	$title = "[".&mt('untitled')."]";
  467:     }
  468:     return $title;
  469: }
  470: 
  471: sub getCopyRightString {
  472:     my $fullname = shift;
  473:     return &Apache::lonnet::metadata($fullname, 'copyright');
  474: }
  475: 
  476: sub getSourceRightString {
  477:     my $fullname = shift;
  478:     return &Apache::lonnet::metadata($fullname, 'sourceavail');
  479: }
  480: #
  481: #  Put out a directory table row:
  482: #    putdirectory(r, base, here, dirname, modtime, targetdir, bombs, numdir)
  483: #      r         - Apache request object.
  484: #      reqfile   - File in request.
  485: #      here      - Where we are in directory tree.
  486: #      dirname   - Name of directory special file.
  487: #      modtime   - Encoded modification time.
  488: #      targetdir - Publication target directory.
  489: #      bombs     - Reference to hash of URLs with runtime error messages.
  490: #      numdir    - Reference to scalar used to track number of sub-directories
  491: #                  in directory (used in form name for each "actions" dropdown).
  492: #
  493: sub putdirectory {
  494:     my ($r, $reqfile, $here, $dirname, $modtime, $targetdir, $bombs, $numdir) = @_;
  495: 
  496: # construct the display filename: the directory name unless ..:
  497:    
  498:     my $actionitem;
  499:  
  500:     my $disfilename = $dirname;
  501: # Don't display directory itself, and there is no way up from root directory
  502:     unless ((($dirname eq '..') && ($reqfile=~/^\/[^\/]+\/[^\/]+$/)) || ($dirname eq '.')) {
  503:         my $kaputt=0;
  504:         if (ref($bombs) eq 'HASH') {
  505:             foreach my $key (keys(%{$bombs})) {
  506:                 my $currentdir = &Apache::lonnet::declutter("$targetdir/$disfilename");
  507:                 if (($key) =~ m{^\Q$currentdir\E/}) { $kaputt=1; last; }
  508:             }
  509:         }
  510: #
  511: # Get the metadata from that directory's default.meta to display titles
  512: #
  513: 	%Apache::lonpublisher::metadatafields=();
  514: 	%Apache::lonpublisher::metadatakeys=();
  515: 	&Apache::lonpublisher::metaeval(
  516:                  &Apache::lonnet::getfile($r->dir_config('lonDocRoot').$here.'/'.$dirname.'/default.meta')
  517:                                        );
  518:         if ($dirname eq '..') {
  519:             $actionitem = &mt('Go to ...');
  520:             $disfilename = '<i>'.&mt('Parent Directory').'</i>';
  521:         } else {
  522:             $actionitem = 
  523:                     '<form name="dirselect_'.$$numdir.
  524:                     '" action="/adm/publish" target="_parent">'.
  525:                     '<select name="diraction" onchange="SetPubDir(this.form,document)">'.
  526:                       '<option selected="selected">'.&mt('Select action').'</option>'.
  527:                       '<option value="open">'.&mt('Open').'</option>'.
  528:                       '<option value="publish">'.&mt('Publish').'</option>'.
  529:                       '<option value="editmeta">'.&mt('Edit Metadata').'</option>'.
  530:                       '<option value="printdir">'.&mt('Print directory').'</option>'.
  531:                       '<option value="delete">'.&mt('Delete directory').'</option>'.
  532:                     '</select>'.
  533:                      '<input type="hidden" name="filename" value="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" />'.
  534:                      '<input type="hidden" name="openname" value="'.$here.'/'.$dirname.'/" />'.
  535:                      '<input type="hidden" name="postdata" value="" />'.
  536:                    '</form>';
  537:             $$numdir ++;
  538:         }
  539: 	$r->print('<tr class="LC_browser_folder">'.
  540: 		  '<td><img src="'.
  541: 		  $Apache::lonnet::perlvar{'lonIconsURL'}.'/navmap.folder.closed.gif" alt="folder" /></td>'.
  542: 		  '<td>'.$actionitem.'</td>'.
  543: 		  '<td><span class="LC_filename"><a href="'.&HTML::Entities::encode($here.'/'.$dirname,'<>&"').'/" target="_parent">'.
  544: 		  $disfilename.'</a></span></td>'.
  545: 		        '<td colspan="3">'.($kaputt?&Apache::lonhtmlcommon::authorbombs($targetdir.'/'.$disfilename.'/'):'').$Apache::lonpublisher::metadatafields{'title'});
  546: 	if ($Apache::lonpublisher::metadatafields{'subject'} ne '') {
  547: 	    $r->print(' <i>'.
  548: 		      $Apache::lonpublisher::metadatafields{'subject'}.
  549: 		      '</i> ');
  550: 	}
  551: 	$r->print($Apache::lonpublisher::metadatafields{'keywords'}.'</td>'.
  552: 		  '<td>'.&Apache::lonlocal::locallocaltime($modtime).'</td>'.
  553: 		  "</tr>\n");
  554:     }
  555:     return OK;
  556: }
  557: #
  558: #   Put a table row for a file resource.
  559: #
  560: sub putresource {
  561:     my ($r, $udom, $uname, $filename, $thisdisfn, 
  562: 	$resdir, $targetdir, $linkdir,
  563: 	$cmtime,$bombs,$numres) = @_;
  564:     &Apache::lonnet::devalidate_cache_new('meta',$targetdir.'/'.$filename);
  565:     my $pubstatus = 'unpublished';
  566:     my $status=&mt('Unpublished');
  567:     my $css_class='LC_browser_file';
  568:     my $title='';
  569:     my $publish_button=&mt('Publish');
  570:     my $cstr_dir = $r->dir_config('lonDocRoot').'/priv'.$thisdisfn;
  571:     my $linkfilename=&HTML::Entities::encode('/priv'.$thisdisfn.'/'.$filename,'<>&"');
  572: 
  573:     if (-e $resdir.'/'.$filename) {
  574:         my $same=0;
  575: 	my ($rdev,$rino,$rmode,$rnlink,
  576: 	    $ruid,$rgid,$rrdev,$rsize,
  577: 	    $ratime,$rmtime,$rctime,
  578: 	    $rblksize,$rblocks)=stat($resdir.'/'.$filename);
  579:         if ($rmtime>=$cmtime) {
  580:            $same=1;
  581:         } else {
  582:            if (&Apache::londiff::are_different_files($resdir.'/'.$filename,
  583: 						     $cstr_dir.'/'.$filename)) {
  584:               $same=0;
  585:            } else {
  586:               $same=1;
  587:            }
  588:         }
  589: 	my $meta_cmtime = (stat($cstr_dir.'/'.$filename.'.meta'))[9];
  590: 	my $meta_rmtime = (stat($resdir.'/'.$filename.'.meta'))[9];
  591: 	my $meta_same = 1;
  592: 	if ($meta_rmtime < $meta_cmtime
  593: 	    && &Apache::londiff::are_different_files($resdir.'/'.$filename.'.meta',
  594: 						     $cstr_dir.'/'.$filename.'.meta')) {
  595: 	    $meta_same = 0;
  596: 	}
  597: 	$publish_button=&mt('Re-publish');
  598: 
  599:         my $rights_status =
  600:             &mt(&getCopyRightString($targetdir.'/'.$filename)).', ';
  601: 
  602:         my %lt_SourceRight = &Apache::lonlocal::texthash(
  603:                'open'   => 'Source: open',
  604:                'closed' => 'Source: closed',
  605:         );
  606:         $rights_status .=
  607:             $lt_SourceRight{&getSourceRightString($targetdir.'/'.$filename)};
  608: 
  609: 	$title = '<a href="'.$targetdir.'/'.$filename.
  610: 	    '.meta" target="cat">'.
  611: 	    &getTitleString($targetdir.'/'.$filename).'</a>';
  612: 	if ($same) {
  613: 	    if (&Apache::lonnet::metadata($targetdir.'/'.$filename,'obsolete')) {
  614:                 $pubstatus = 'obsolete';
  615: 		$status=&mt('Obsolete');
  616:             } else {
  617: 		if (!$meta_same) {
  618: 		    $pubstatus = 'metamodified';
  619: 		} else {
  620: 		    $pubstatus = 'published';
  621: 		}
  622: 		$status=&mt('Published').
  623: 		    '<br />'. $rights_status;
  624: 	    }
  625: 	} else {
  626:             $pubstatus = 'modified';
  627: 	    $status=&mt('Modified').
  628: 		'<br />'. $rights_status;
  629: 	    if (&Apache::loncommon::fileembstyle(($filename=~/\.(\w+)$/)) eq 'ssi') {
  630: 		$status.='<br />'.
  631:                          &Apache::loncommon::modal_link(
  632:                              '/adm/diff?filename='.$linkfilename.'&amp;versiontwo=priv',
  633:                              &mt('Diffs'),600,500);
  634: 	    }
  635: 	} 
  636: 	if (!$meta_same) {
  637: 	    $title = &mt('Metadata Modified').'<br />'.$title.
  638: 		'<br />'.
  639:                 &Apache::loncommon::modal_link(
  640:                     '/adm/diff?filename='.$linkfilename.'.meta'.'&amp;versiontwo=priv',
  641:                     &mt('Metadata Diffs'),600,500);
  642: 	    $title.="\n".'<br />'.
  643:                 &Apache::loncommon::modal_link(
  644:                     '/adm/retrieve?filename='.$linkfilename.'.meta&amp;inhibitmenu=yes&amp;add_modal=yes',
  645:                     &mt('Retrieve Metadata'),600,500);
  646: 	}
  647: 	$status.="\n".'<br />'.
  648:              &Apache::loncommon::modal_link(
  649:                  '/adm/retrieve?filename='.$linkfilename.'&amp;inhibitmenu=yes&amp;add_modal=yes',&mt('Retrieve'),600,500);
  650:     }
  651:     # Allow editing metadata of published and unpublished resources
  652:     $title .= "\n".'<br />' if ($title);
  653:     $title .= '<a href="'.$linkfilename.'.meta">'.
  654:               ($$bombs{&Apache::lonnet::declutter($targetdir.'/'.$filename)}?
  655:                   '<img src="/adm/lonMisc/bomb.gif" border="0" alt="'.&mt('bomb').'" />':
  656:                   &mt('Edit Metadata')).
  657:               '</a>';
  658: 
  659:     my $editlink='';
  660:     my $editlink2='';
  661:     if ($filename=~/\.(xml|html|htm|xhtml|xhtm|sty)$/) {
  662: 	$editlink=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
  663:     }
  664:     if ($filename=~/$LONCAPA::assess_re/) {
  665: 	$editlink=' (<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=editxml">'.&mt('EditXML').'</a>)';
  666: 	$editlink2=' <br />(<a href="'.$linkdir.'/'.$filename.'?editmode=Edit&amp;problemmode=edit">'.&mt('Edit').'</a>)';
  667:     }
  668:     if ($filename=~/\.(problem|exam|quiz|assess|survey|form|library|xml|html|htm|xhtml|xhtm|sty)$/) {
  669: 	$editlink.=' (<a href="/adm/cleanup?filename='.$linkfilename.'" target="_parent">'.&mt('Clean Up').')</a>';
  670:     }
  671:     if ($filename=~/\.(zip|tar|bz2|gz|tar.gz|tar.bz2|tgz)$/) {
  672: 	$editlink=' (<a target="_parent" href="/adm/cfile?decompress='.$linkfilename.'">'.&mt('Decompress').'</a>)';
  673:     }
  674:     my $pub_select = '';
  675:     &create_pubselect($r,\$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres);
  676:     $r->print(&Apache::loncommon::start_data_table_row().
  677: 	      '<td>'.($filename=~/[\#\~]$/?'&nbsp;':
  678: 		      '<img src="'.&Apache::loncommon::icon($filename).'" alt="" />').'</td>'.
  679:               '<td>'.$pub_select.'</td>'.
  680: 	      '<td><span class="LC_filename">'.
  681: 	      '<a href="'.$linkdir.'/'.$filename.'" target="_parent">'.
  682:                $filename.'</a></span>'.$editlink2.$editlink.
  683: 	      '</td>'.
  684: 	      '<td>'.$title.'</td>'.
  685:               '<td class="LC_browser_file_'.$pubstatus.'">&nbsp;&nbsp;</td>'. # Display publication status
  686:               '<td>'.$status.'</td>'.
  687: 	      '<td>'.&Apache::lonlocal::locallocaltime($cmtime).'</td>'.
  688: 	      &Apache::loncommon::end_data_table_row()
  689:     );
  690:     return OK;
  691: }
  692: 
  693: sub create_pubselect {
  694:     my ($r,$pub_select,$udom,$uname,$thisdisfn,$filename,$resdir,$pubstatus,$publish_button,$numres) = @_;
  695:     $$pub_select = '
  696: <form name="resselect_'.$$numres.'" action="">
  697: <select name="reschoice"  onchange="SetResChoice(this.form)">
  698: <option>'.&mt('Select action').'</option>'.
  699: '<option value="copy">'.&mt('Copy').'</option>';
  700:     if ($pubstatus eq 'obsolete' || $pubstatus eq 'unpublished') {
  701:         $$pub_select .= 
  702: '<option value="rename">'.&mt('Rename').'</option>'.
  703: '<option value="move">'.&mt('Move').'</option>'.
  704: '<option value="delete">'.&mt('Delete').'</option>';
  705:     } else {
  706:         $$pub_select .= '
  707: <option value="obsolete">'.&mt('Mark obsolete').'</option>';
  708:     }
  709: # check for versions
  710:     my $versions = &check_for_versions($r,'/'.$filename,$udom,$uname);
  711:     if ($versions > 0) {
  712:         $$pub_select .='
  713: <option value="retrieve">'.&mt('Retrieve old version').'</option>';
  714:     }
  715:     $$pub_select .= '
  716: <option value="publish">'.$publish_button.'</option>'.
  717: '<option value="cleanup">'.&mt('Clean up').'</option>'.
  718: '<option value="print">'.&mt('Print').'</option>'.
  719: '</select>
  720: <input type="hidden" name="filename" value="/priv'.
  721:  &HTML::Entities::encode($thisdisfn.'/'.$filename,'<>&"').'" />
  722:  <input type="hidden" name="dispfilename" value="'.
  723:  &HTML::Entities::encode($filename).'" /></form>';
  724:     $$numres ++;
  725: }
  726: 
  727: sub check_for_versions {
  728:     my ($r,$fn,$udom,$uname) = @_;
  729:     my $versions = 0;
  730:     my $docroot=$r->dir_config('lonDocRoot');
  731:     my $resfn=$docroot.'/res/'.$udom.'/'.$uname.$fn;
  732:     my $resdir=$resfn;
  733:     $resdir=~s/\/[^\/]+$/\//;
  734:     $fn=~/\/([^\/]+)\.(\w+)$/;
  735:     my $main=$1;
  736:     my $suffix=$2;
  737:     opendir(DIR,$resdir);
  738:     while (my $filename=readdir(DIR)) {
  739:         if ($filename=~/^\Q$main\E\.(\d+)\.\Q$suffix\E$/) {
  740:             $versions ++;        
  741:         }
  742:     }
  743:     return $versions;
  744: }
  745: 
  746: 1;
  747: __END__
  748: 
  749: 
  750: =head1 NAME
  751: 
  752: Apache::lonpubdir - Authoring space directory lister
  753: 
  754: =head1 SYNOPSIS
  755: 
  756: Invoked (for various locations) by /etc/httpd/conf/srm.conf:
  757: 
  758:  <LocationMatch "^/+priv.*/$">
  759:  PerlAccessHandler       Apache::loncacc
  760:  SetHandler perl-script
  761:  PerlHandler Apache::lonpubdir
  762:  ErrorDocument     403 /adm/login
  763:  ErrorDocument     404 /adm/notfound.html
  764:  ErrorDocument     406 /adm/unauthorized.html
  765:  ErrorDocument	  500 /adm/errorhandler
  766:  </LocationMatch>
  767: 
  768:  <Location /adm/pubdir>
  769:  PerlAccessHandler       Apache::lonacc
  770:  SetHandler perl-script
  771:  PerlHandler Apache::lonpubdir
  772:  ErrorDocument     403 /adm/login
  773:  ErrorDocument     404 /adm/notfound.html
  774:  ErrorDocument     406 /adm/unauthorized.html
  775:  ErrorDocument	  500 /adm/errorhandler
  776:  </Location>
  777: 
  778: =head1 INTRODUCTION
  779: 
  780: This module publishes a directory of files.
  781: 
  782: This is part of the LearningOnline Network with CAPA project
  783: described at http://www.lon-capa.org.
  784: 
  785: =head1 HANDLER SUBROUTINE
  786: 
  787: This routine is called by Apache and mod_perl.
  788: 
  789: =over 4
  790: 
  791: =item *
  792: 
  793: read in information
  794: 
  795: =item *
  796: 
  797: start page output
  798: 
  799: =item *
  800: 
  801: run through list of files and attempt to publish unhidden files
  802: 
  803: =back
  804: 
  805: =head1 SUBROUTINES:
  806: 
  807: =over
  808: 
  809: =item startpage($r, $uame, $udom, $thisdisfn)
  810: 
  811: Output the header of the page.  This includes:
  812:  - The HTML header 
  813:  - The H1/H3  stuff which includes the directory.
  814:  
  815:     startpage($r, $uame, $udom, $thisdisfn);
  816:         $r     - The apache request object.
  817:         $uname - User name.
  818:         $udom  - Domain name the user is logged in under.
  819:         $thisdisfn - Displayable version of the filename.
  820: 
  821: =item getTitleString($fullname)
  822: 
  823:     Get the title string or "[untitled]" if the file has no title metadata:
  824:     Without the latter substitution, it's impossible to examine metadata for
  825:     untitled resources.  Resources may be legitimately untitled, to prevent
  826:     searches from locating them.
  827:     
  828:     $str = getTitleString($fullname);
  829:         $fullname - Fully qualified filename to check.
  830: 
  831: =item putdirectory($r, $base, $here, $dirname, $modtime, $targetdir, $bombs,
  832:                    $numdir)
  833: 
  834:     Put out a directory table row:
  835:     
  836:         $r        - Apache request object.
  837:         $reqfile  - File in request.
  838:         $here     - Where we are in directory tree.
  839:         $dirname  - Name of directory special file.
  840:         $modtime  - Encoded modification time.
  841:         targetdir - Publication target directory.
  842:         bombs     - Reference to hash of URLs with runtime error messages.
  843:         numdir    - Reference to scalar used to track number of sub-directories
  844:                     in directory (used in form name for each "actions" dropdown).
  845: 
  846: =back
  847: 
  848: =cut
  849: 

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