File:  [LON-CAPA] / loncom / publisher / lonpubdir.pm
Revision 1.121: download - view: text, annotated - select for diffs
Tue May 19 22:52:29 2009 UTC (15 years ago) by bisitz
Branches: MAIN
CVS tags: HEAD
To get rid of the title_bar, customized titles aren't allowed anymore.

Moved special title for Construction Space pages from &start_page() to new routine &CSTR_pageheader(). Since &start_page() isn't the only routine called to create the page header, the centralized creation of the CSTR page header code can't be currently called in &start_page(). This needs to be done at each place where &start_page() is called.

First changes to get rid of customized page titles:
- Moved CSTR page header code from start_page to new sub routine &CSTR_pageheader()
- Currently keep old header but use &CSTR_pageheader()
- Directly call &CSTR_pageheader() in CSTR related scripts (lonpubdir.pm, lonhomework.pm, structuretags.pm)
  (More scripts need to call this routine)
- Added new sub routine &head_subbox() to create head_subbox which contains page related links and functions, etc.
- Added styles for &head_subbox()
  (Currently implemented as LC_head_subbox2 to avoid conflicts with code created by lonhtmlcommon::breadcrumbs - will be changed)

Other but related changes:
- Added some breadcrumbs to CSTR
  (Only basic calls - links and structure has to be added/revisited)
- Adjusted styles used for breadcrumbs:
    - Replaced hardcoded border color by dynamical standard border color
    - Removed margin to allow direct concatination with head_subbox

- Added some FIXME comments to mark work in progress

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

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