SVN tag for release should only contain scripts in "docs" folder without "mods",...
[atutor.git] / docs / mods / _core / file_manager / filemanager_display.inc.php
1 <?php
2 /****************************************************************/
3 /* ATutor                                                                                                               */
4 /****************************************************************/
5 /* Copyright (c) 2002-2010                                      */
6 /* Inclusive Design Institute                                   */
7 /* http://atutor.ca                                                                                             */
8 /*                                                              */
9 /* This program is free software. You can redistribute it and/or*/
10 /* modify it under the terms of the GNU General Public License  */
11 /* as published by the Free Software Foundation.                                */
12 /****************************************************************/
13 // $Id$
14
15 if (!defined('AT_INCLUDE_PATH')) { exit; }
16
17 function get_file_extension($file_name) {
18         $ext = pathinfo($file_name);
19         return $ext['extension'];
20 }
21
22 function get_file_type_icon($file_name) {
23         static $mime;
24
25         $ext = get_file_extension($file_name);
26
27         if (!isset($mime)) {
28                 require(AT_INCLUDE_PATH .'lib/mime.inc.php');
29         }
30
31         if (isset($mime[$ext]) && $mime[$ext][1]) {
32                 return $mime[$ext][1];
33         }
34         return 'generic';
35 }
36
37 function get_relative_path($src, $dest) {
38         if ($src == '') {
39                 $path = $dest;
40         } else if (substr($dest, 0, strlen($src)) == $src) {
41                 $path = substr($dest, strlen($src) + 1);
42         } else {
43                 $depth = substr_count($src, '/');
44                 for ($i = 0; $i < $depth + 1; $i++)  // $depth+1 because the last '/' is not recorded in content.content_path
45                         $path .= '../';
46                 $path .= $dest;
47         }
48
49         return $path;
50 }
51
52 // get the course total in Bytes 
53 $course_total = dirsize($current_path);
54
55 $framed = intval($_GET['framed']);
56 $popup = intval($_GET['popup']);
57 $cp = $_GET['cp'];
58 $cid = intval($_GET['cid']);        // content id, used at "adapted content" page, => add/edit alternatives
59 $pid = intval($_GET['pid']);        // primary resource id, used at "adapted content" page, => add/edit alternatives
60 $a_type = intval($_GET['a_type']);  // alternative_type, used at "adapted content" page, => add/edit alternatives
61
62 if (defined('AT_FORCE_GET_FILE') && AT_FORCE_GET_FILE) {
63         $get_file = 'get.php/';
64 } else {
65         $get_file = 'content/' . $_SESSION['course_id'] . '/';
66 }
67
68 function fm_path(){
69         global $pathext, $framed, $popup, $cp, $cid, $pid, $a_type;
70 echo '<p>'._AT('current_path').' ';
71
72 if (isset($pathext) && $pathext != '') {
73         echo '<a href="'.$_SERVER['PHP_SELF'].'?popup=' . $popup . SEP . 'framed=' . $framed.SEP . 'cp=' . $cp.SEP . 'cid=' . $cid.SEP . 'pid=' . $pid.SEP . 'a_type=' . $a_type.'">'._AT('home').'</a> ';
74 }
75 else {
76         $pathext = '';
77         echo _AT('home');
78 }
79
80
81 if ($pathext == '' && isset($_POST['pathext'])) {
82
83         $pathext = urlencode($_POST['pathext']);
84 }
85
86 if ($pathext != '') {
87         $bits = explode('/', $pathext);
88
89         foreach ($bits as $bit) {
90                 if ($bit != '') {
91                         $bit_path .= $bit . '/';
92                         echo ' / ';
93
94                         if ($bit_path == $pathext) {
95                                 echo $bit;
96                         } else {
97                                 echo '<a href="'.$_SERVER['PHP_SELF'].'?pathext=' . urlencode($bit_path) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed . SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'">' . $bit . '</a>';
98                         }
99                 }
100         }
101         $bit_path = "";
102         $bit = "";
103 }
104 echo '</p>';
105
106 }
107
108 if ($popup == TRUE) {
109         $totalcol = 6;
110 } else {
111         $totalcol = 5;
112 }
113 $labelcol = 3;
114
115 if (TRUE || $framed != TRUE) {
116
117         if ($_GET['overwrite'] != '') {
118                 // get file name, out of the full path
119                 $path_parts = pathinfo($current_path.$_GET['overwrite']);
120
121                 if (!file_exists($path_parts['dirname'].'/'.$pathext.$path_parts['basename'])
122                         || !file_exists($path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5))) {
123                         /* source and/or destination does not exist */
124                         $msg->addErrors('CANNOT_OVERWRITE_FILE');
125                 } else {
126                         @unlink($path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5));
127                         $result = @rename($path_parts['dirname'].'/'.$pathext.$path_parts['basename'], $path_parts['dirname'].'/'.$pathext.substr($path_parts['basename'], 5));
128
129                         if ($result) {
130                                 $msg->addFeedback('FILE_OVERWRITE');
131                         } else {
132                                 $msg->addErrors('CANNOT_OVERWRITE_FILE');
133                         }
134                 }
135         }
136         
137         // filemanager listing table
138         // make new directory 
139         echo '<div class="input-form"><fieldset class="group_form"><legend class="group_form">'._AT('add_file_folder').'</legend>'."\n";
140         echo '  <div class="row">'."\n";
141         echo '          <form name="form1" method="post" action="'.$_SERVER['PHP_SELF'].'?'.(($pathext != '') ? 'pathext='.urlencode($pathext).SEP : ''). 'popup='.$popup.SEP.'cp='.SEP.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'">'."\n";
142         if( $MakeDirOn ) {
143                 if ($depth < $MaxDirDepth) {
144                         echo '          <label for="dirname">'._AT('create_folder_here').'</label><br />'."\n";
145                         echo '          &nbsp;<small class="spacer">'._AT('keep_it_short').'</small><br />'."\n";
146                         echo '          <input type="text" name="dirname" id="dirname" size="20" /> '."\n";
147                         echo '          <input type="hidden" name="mkdir_value" value="true" /> '."\n";
148                         echo '          <input type="submit" name="mkdir" value="'._AT('create_folder').'" class="button" />'."\n";
149                 } else {
150                         echo _AT('depth_reached')."\n";
151                 }
152         }
153         echo '          <input type="hidden" name="pathext" value="'.$pathext.'" />'."\n";
154         echo '          </form>'."\n";
155         echo '  </div>'."\n";
156
157         echo '  <div class="row"><hr /></div>'."\n";
158
159
160         // If flash is available, provide the option of using Fluid's uploader or the basic uploader
161         if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") {
162                 echo '<div class="row">'."\n";
163                 if (isset($_COOKIE["fluid_on"]) && $_COOKIE["fluid_on"]=="yes")
164                         $fluid_on = 'checked="checked"';
165                 echo '(<input type="checkbox" id="fluid_on" name="fluid_on" onclick="toggleform(\'simple-container\', \'fluid-container\'); setCheckboxCookie(this, \'fluid_on=yes\', \'fluid_on=no\',\'December 31, 2099\');" value="yes" '.$fluid_on.' /> '."\n";
166                 echo '<label for="fluid_on" >'._AT('enable_uploader').'</label>)'."\n";
167                 echo '</div>'."\n";
168         }
169
170
171         // Create a new file
172         echo '  <div class="row" style="float: left;"><input type="button" class="button" name="new_file" value="' . _AT('file_manager_new') . '" onclick="window.location.href=\''.AT_BASE_HREF.'mods/_core/file_manager/new.php?pathext=' . urlencode($pathext) . SEP . 'framed=' . $framed . SEP . 'popup=' . $popup . '\'"/></div>'."\n";
173
174         $my_MaxCourseSize = $system_courses[$_SESSION['course_id']]['max_quota'];
175
176         // upload file 
177         if (($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED) 
178                 || (($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) && ($course_total < $MaxCourseSize))
179                 || ($my_MaxCourseSize-$course_total > 0)) {
180                 echo '  <div class="row" style="float: left;">'._AT('OR').'</div>'."\n".'       <div class="row" style="float: left;">'."\n";
181                 if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") {
182                 ?>
183                         <div id="fluid-container" <?php echo (isset($_COOKIE["fluid_on"]) && $_COOKIE["fluid_on"]=="yes") ? '' : 'style="display:none;"'; ?>>
184                                 <input type="button" id="uploader_link" class="button" name="upload_file" value="<?php echo _AT('upload_files'); ?>" onclick="toggleform('uploader', 'uploader_link');" />
185                                 <div id="uploader" style="display: none;">
186                                         <form id="flc-uploader" class="flc-uploader fl-uploader fl-progEnhance-enhanced" method="get" enctype="multipart/form-data" action="" style="margin: 0px; padding: 0px;">
187                                                 <div class="start">
188                                                         <div class="fl-uploader-queue-wrapper">
189                                                                 <div class="fl-uploader-queue-header">
190                                                                         <table cellspacing="0" cellpadding="0">
191                                                                                         <tr>
192                                                                                                 <th scope="col" class="fileName"><?php echo _AT('file_name'); ?></th>
193                                                                                                 <th scope="col" class="fileSize"><?php echo _AT('size'); ?>&nbsp;&nbsp;</th>
194                                                                                                 <th scope="col" class="fileRemove">&nbsp;</th>
195                                                                                         </tr>
196                                                                         </table>
197                                                                 </div>
198
199                                        <div class="flc-scroller fl-scroller">
200                                                 <div class="fl-scroller-inner">
201                                                     <table cellspacing="0" class="flc-uploader-queue fl-uploader-queue" summary="Queue of files to upload.">
202                                                         <tbody>
203                                                             <!-- Rows will be rendered in here. -->
204                                                             
205                                                             <!-- Template markup for the file queue rows -->
206                                                             <tr class="flc-uploader-file-tmplt flc-uploader-file fl-uploader-hidden-templates">
207                                                                 <th class="flc-uploader-file-name fl-uploader-file-name" scope="row"><?php echo _AT('file_placeholder'); ?></th>
208                                                                 <td class="flc-uploader-file-size fl-uploader-file-size">0 <?php echo _AT('kb'); ?></td>
209                                                                 <td class="fl-uploader-file-actions">
210                                                                     <button type="button" class="flc-uploader-file-action fl-uploader-file-action" title="<?php echo _AT('remove_queued_file'); ?>" tabindex="-1">
211                                                                         <span class="fl-uploader-button-text fl-uploader-hidden"><?php echo _AT('remove_queued_file'); ?></span>
212                                                                     </button>
213                                                                 </td>
214                                                             </tr>
215                                                             
216                                                             <!-- Template for the file error info rows -->
217                                                             <tr class="flc-uploader-file-error-tmplt fl-uploader-file-error fl-uploader-hidden-templates">
218                                                                 <td colspan="3" class="flc-uploader-file-error"></td>
219                                                             </tr>
220                                                         </tbody>
221                                                     </table>
222                                                     <div class="flc-uploader-file-progressor-tmplt fl-uploader-file-progress"><span class="fl-uploader-file-progress-text fl-uploader-hidden">76%</span></div>
223                                                 </div>
224                                             </div>
225
226                                                                 <div class="flc-uploader-browse-instructions fl-uploader-browse-instructions"> <?php echo _AT('click_browse_files'); ?> </div>
227
228                                                                 <div class="flc-uploader-queue-footer fl-uploader-queue-footer">
229                                                                         <table cellspacing="0" cellpadding="0">
230                                                                                         <tr>
231                                                                                                 <td class="flc-uploader-total-progress-text"><?php echo _AT('total'); ?>: <span class="fluid-uploader-totalFiles">0</span> <?php echo _AT('files'); ?> (<span class="fluid-uploader-totalBytes">0 <?php echo _AT('kb'); ?></span>)</td>
232                                                                                                 <td class="fl-uploader-footer-buttons" align="right" ><a class="flc-uploader-button-browse fl-uploader-browse" tabindex="0" ><?php echo _AT('browse_files'); ?></a></td>
233                                                                                         </tr>
234                                                                         </table>
235                                                                         <div class="flc-uploader-total-progress fl-uploader-total-progress-okay">&nbsp;</div>
236                                                                 </div>
237                                                         </div>
238                                                         
239                                                         <div class="fl-uploader-btns">
240                                                                 <button type="button" class="flc-uploader-button-pause fl-uploader-pause fl-uploader-hidden" onclick="toggleform('uploader', 'uploader_link');"><?php echo _AT('cancel'); ?></button>
241                                                                 <button type="button" class="flc-uploader-button-upload fl-uploader-upload fl-uploader-button-default fl-uploader-dim" ><?php echo _AT('upload'); ?></button>
242                                                         </div>
243                                                         
244                                                 </div>
245                                         </form>
246
247                                 </div>
248                         </div>
249                 <?php
250                         if (isset($_COOKIE["fluid_on"]) && $_COOKIE["fluid_on"]=="yes")
251                                 echo '<div id="simple-container" style="display: none;">';
252                         else
253                                 echo '<div id="simple-container">';
254                 } else {
255                         // Display as regular if there's no Flash detected
256                         echo '<div id="simple-container">'."\n";
257                 }
258
259                 // Simple single file uploader
260                 echo '<form onsubmit="openWindow(\''.AT_BASE_HREF.'tools/prog.php\');" class="fl-ProgEnhance-basic" name="form1" method="post" action="mods/_core/file_manager/upload.php?popup='.$popup.SEP. 'framed='.$framed.SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'" enctype="multipart/form-data">';
261                 echo '<input type="hidden" name="MAX_FILE_SIZE" value="'.$my_MaxFileSize.'" />';
262                 echo '<label for="uploadedfile">'._AT('upload_files').'</label><br />'."\n";
263                 echo '<input type="file" name="uploadedfile" id="uploadedfile" class="formfield" size="20" /> ';
264                 echo '<input type="submit" name="submit" value="'._AT('upload').'" class="button" />';
265                 echo '<input type="hidden" name="pathext" value="'.$pathext.'" />  ';
266
267                 if ($popup == TRUE) {
268                         echo '<input type="hidden" name="popup" value="1" />';
269                 }
270                 echo '</form>';
271                 echo '</div>';
272
273                 echo '          </div>'."\n".'  </fieldset></div>';
274
275         } else {
276                 echo '  </fieldset></div>'."\n";
277                 $msg->printInfos('OVER_QUOTA');
278         }
279 }
280
281 // Directory and File listing 
282 echo '<form name="checkform" action="'.$_SERVER['PHP_SELF'].'?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : '').'popup='.$popup .SEP. 'framed='.$framed.SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'" method="post">';
283 echo '<input type="hidden" name="pathext" value ="'.$pathext.'" />';
284
285 // display the section to use a remote URL as an alternative
286 if ($a_type > 0) {
287 ?>
288 <div class="input-form" style="min-height:10px">
289 <fieldset class="group_form" style="min-height: 0px;"><legend class="group_form"><?php echo _AT('use_url_as_alternative'); ?></legend>
290         <div class="row">
291           <input name="remote_alternative" id="remote_alternative" value="http://" size="60" />
292           <input class="button" type="button" name="alternative" value="<?php echo _AT('use_as_alternative'); ?>" onclick="javascript: setURLAlternative();" />
293         </div>
294 </fieldset>
295 </div>
296 <?php }?>
297
298 <table class="data static" summary="" border="0" rules="groups" style="width: 90%">
299 <thead>
300 <tr>
301 <td colspan="5">
302 <?php fm_path(); ?>
303 </td>
304 </tr>
305 <tr>
306         <th scope="col"><input type="checkbox" name="checkall" onclick="Checkall(checkform);" id="selectall" title="<?php echo _AT('select_all'); ?>" /></th>
307         <th>&nbsp;</th>
308         <th scope="col"><?php echo _AT('name');   ?></th>
309         <th scope="col"><?php echo _AT('date');   ?></th>
310         <th scope="col"><?php echo _AT('size');   ?></th>
311 </tr>
312 </thead>
313 <tfoot>
314 <tr>
315         <td colspan="5"><input type="submit" name="rename" value="<?php echo _AT('rename'); ?>" /> 
316                 <input type="submit" name="delete" value="<?php echo _AT('delete'); ?>" /> 
317                 <input type="submit" name="move"   value="<?php echo _AT('move'); ?>" /></td>
318 </tr>
319
320 <tr>
321         <td colspan="4" align="right"><strong><?php echo _AT('directory_total'); ?>:</strong></td>
322         <td align="right">&nbsp;<strong><?php echo get_human_size(dirsize($current_path.$pathext.$file.'/')); ?></strong>&nbsp;</td>
323 </tr>
324
325 <tr>
326         <td colspan="4" align="right"><strong><?php echo _AT('course_total'); ?>:</strong></td>
327         <td align="right">&nbsp;<strong><?php echo get_human_size($course_total); ?></strong>&nbsp;</td>
328 </tr>
329 <tr>
330         <td colspan="4" align="right"><strong><?php echo _AT('course_available'); ?>:</strong></td>
331         <td align="right"><strong><?php
332                 if ($my_MaxCourseSize == AT_COURSESIZE_UNLIMITED) {
333                         echo _AT('unlimited');
334                 } else if ($my_MaxCourseSize == AT_COURSESIZE_DEFAULT) {
335                         echo get_human_size($MaxCourseSize-$course_total);
336                 } else {
337                         echo get_human_size($my_MaxCourseSize-$course_total);
338                 } ?></strong>&nbsp;</td>
339 </tr>
340 </tfoot>
341 <?php
342
343
344 if($pathext) : ?>
345         <tr>
346                 <td colspan="5"><a href="<?php echo $_SERVER['PHP_SELF'].'?back=1'.SEP.'pathext='.$pathext.SEP. 'popup=' . $popup .SEP. 'framed=' . $framed .SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type; ?>"><img src="images/arrowicon.gif" border="0" height="11" width="10" alt="" /> <?php echo _AT('back'); ?></a></td>
347         </tr>
348 <?php endif; ?>
349 <?php
350 $totalBytes = 0;
351
352 if ($dir == '')
353         $dir=opendir($current_path);
354         
355 // loop through folder to get files and directory listing
356 while (false !== ($file = readdir($dir)) ) {
357
358         // if the name is not a directory 
359         if( ($file == '.') || ($file == '..') ) {
360                 continue;
361         }
362
363         // get some info about the file
364         $filedata = stat($current_path.$pathext.$file);
365         $path_parts = pathinfo($file);
366         $ext = strtolower($path_parts['extension']);
367
368         $is_dir = false;
369
370         // if it is a directory change the file name to a directory link 
371         if(is_dir($current_path.$pathext.$file)) {
372                 $size = dirsize($current_path.$pathext.$file.'/');
373                 $totalBytes += $size;
374                 $filename = '<a href="'.$_SERVER['PHP_SELF'].'?pathext='.urlencode($pathext.$file.'/'). SEP . 'popup=' . $popup . SEP . 'framed='. $framed . SEP.'cp='.$_GET['cp'].SEP.'pid='.$_GET['pid'].SEP.'cid='.$cid.SEP.'a_type='.$a_type.'">'.$file.'</a>';
375                 $fileicon = '&nbsp;';
376                 $fileicon .= '<img src="images/folder.gif" alt="'._AT('folder').':'.$file.'" height="18" width="20" class="img-size-fm1" />';
377                 $fileicon .= '&nbsp;';
378                 if(!$MakeDirOn) {
379                         $deletelink = '';
380                 }
381
382                 $is_dir = true;
383         } else if ($ext == 'zip') {
384
385                 $totalBytes += $filedata[7];
386                 $filename = $file;
387                 $fileicon = '&nbsp;<img src="images/icon-zip.gif" alt="'._AT('zip_archive').':'.$file.'" height="16" width="16" border="0" class="img-size-fm2" />&nbsp;';
388
389         } else {
390                 $totalBytes += $filedata[7];
391                 $filename = $file;
392                 $fileicon = '&nbsp;<img src="images/file_types/'.get_file_type_icon($filename).'.gif" height="16" width="16" alt="" title="" class="img-size-fm2" />&nbsp;';
393         } 
394         $file1 = strtolower($file);
395         // create listing for dirctor or file
396         if ($is_dir) {
397                 
398                 $dirs[$file1] .= '<tr><td  align="center" width="0%">';
399                 $dirs[$file1] .= '<input type="checkbox" id="'.$file.'" value="'.$file.'" name="check[]"/></td>';
400                 $dirs[$file1] .= '<td  align="center"><label for="'.$file.'" >'.$fileicon.'</label></td>';
401                 $dirs[$file1] .= '<td >&nbsp;';
402                 $dirs[$file1] .= $filename.'</td>';
403                 $dirs[$file1] .= '<td  align="right">&nbsp;';
404                 $dirs[$file1] .= AT_date(_AT('filemanager_date_format'), $filedata[10], AT_DATE_UNIX_TIMESTAMP);
405                 $dirs[$file1] .= '&nbsp;</td>';
406                 $dirs[$file1] .= '<td  align="right">';
407                 $dirs[$file1] .= get_human_size($size).'</td></tr>';
408
409                 
410         } else {
411                 $files[$file1] .= '<tr> <td  align="center">';
412                 $files[$file1] .= '<input type="checkbox" id="'.$file.'" value="'.$file.'" name="check[]"/> </td>';
413                 $files[$file1] .= '<td  align="center"><label for="'.$file.'">'.$fileicon.'</label></td>';
414                 $files[$file1] .= '<td >&nbsp;';
415
416                 if ($framed) {
417                         $files[$file1] .= '<a href="'.$get_file.$pathext.urlencode($filename).'">'.$filename.'</a>';
418                 } else {
419                         $files[$file1] .= '<a href="mods/_core/file_manager/preview.php?file='.$pathext.$filename.SEP.'pathext='.urlencode($pathext).SEP.'popup='.$popup.'">'.$filename.'</a>';
420                 }
421
422                 if ($ext == 'zip') {
423                         $files[$file1] .= ' <a href="mods/_core/file_manager/zip.php?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : ''). 'file=' . urlencode($file) . SEP . 'popup=' . $popup . SEP . 'framed=' . $framed .'">';
424                         $files[$file1] .= '<img src="images/archive.gif" border="0" alt="'._AT('extract_archive').'" title="'._AT('extract_archive').'"height="16" width="11" class="img-size-fm3" />';
425                         $files[$file1] .= '</a>';
426                 }
427
428                 if (in_array($ext, $editable_file_types)) {
429                         $files[$file1] .= ' <a href="mods/_core/file_manager/edit.php?'.(($pathext!='') ? 'pathext='.urlencode($pathext).SEP : ''). 'popup=' . $popup . SEP . 'framed=' . $framed . SEP . 'file=' . $file . '">';
430                         $files[$file1] .= '<img src="images/edit.gif" border="0" alt="'._AT('extract_archive').'" title="'._AT('edit').'" height="15" width="18" class="img-size-fm4" />';
431                         $files[$file1] .= '</a>';
432                 }
433
434                 $files[$file1] .= '&nbsp;</td>';
435
436                 $files[$file1] .= '<td  align="right" style="white-space:nowrap">';
437
438                 if ($popup == TRUE) {
439                         if ($a_type > 0)  // define content alternative
440                         {
441                                 $files[$file1] .= '<input class="button" type="button" name="alternative" value="' ._AT('use_as_alternative') . '" onclick="javascript: setAlternative(\''.get_relative_path($_GET['cp'], $pathext).$file.'\', \''.AT_BASE_HREF.$get_file.$pathext.urlencode($file).'\', \''.$cid.'\', \''.$pid.'\', \''.$a_type.'\');" />&nbsp;';
442                         }
443                         else
444                                 $files[$file1] .= '<input class="button" type="button" name="insert" value="' ._AT('insert') . '" onclick="javascript:insertFile(\'' . $file . '\', \'' . get_relative_path($_GET['cp'], $pathext) . '\', \'' . $ext . '\', \'' .$_SESSION['prefs']['PREF_CONTENT_EDITOR']. '\');" />&nbsp;';
445                 }
446
447                 $files[$file1] .= AT_date(_AT('filemanager_date_format'), $filedata[10], AT_DATE_UNIX_TIMESTAMP);
448                 $files[$file1] .= '&nbsp;</td>';
449                 
450                 $files[$file1] .= '<td  align="right" style="white-space:nowrap">';
451                 $files[$file1] .= get_human_size($filedata[7]).'</td></tr>';
452         }
453 } // end while
454
455 // sort listing and output directories
456 if (is_array($dirs)) {
457         ksort($dirs, SORT_STRING);
458         foreach($dirs as $x => $y) {
459                 echo $y;
460         }
461 }
462
463 //sort listing and output files
464 if (is_array($files)) {
465         ksort($files, SORT_STRING);
466         foreach($files as $x => $y) {
467                 echo $y;
468         }
469 }
470
471
472 echo '</table></form>';
473
474
475
476 ?>
477
478 <script type="text/javascript">
479 //<!--
480 function insertFile(fileName, pathTo, ext, ed_pref) { 
481         // pathTo + fileName should be relative to current path (specified by the Content Package Path)
482
483         if (ext == "gif" || ext == "jpg" || ext == "jpeg" || ext == "png") {
484                 var info = "<?php echo _AT('alternate_text'); ?>";
485                 var html = '<img src="' + pathTo+fileName + '" border="0" alt="' + info + '" />';
486
487                 insertLink(html, ed_pref);
488         } else if (ext == "mpg" || ext == "avi" || ext == "wmv" || ext == "mov" || ext == "swf" || ext == "mp3" || ext == "wav" || ext == "ogg" || ext == "mid" ||ext == "flv"|| ext == "mp4") {
489                 var html = '[media]'+ pathTo + fileName + '[/media]';
490
491                 insertLink(html, ed_pref);
492         } else {
493                 var info = "<?php echo _AT('put_link'); ?>";
494                 var html = '<a href="' + pathTo+fileName + '">' + info + '</a>';
495                 
496                 insertLink(html, ed_pref);
497         }
498 }
499
500 function insertLink(html, ed_pref)
501 {
502         var isVisual = false;
503         
504         if (window.opener) {
505                 if (typeof window.opener.jQuery("#html_visual_editor:checked").val() !== "undefined") {
506                         isVisual = true;
507                 }
508         }
509
510         if (!window.opener || isVisual) {
511                 if (!window.opener && window.parent.tinyMCE)
512                         window.parent.tinyMCE.execCommand('mceInsertContent', false, html);
513                 else
514                         if (window.opener && window.opener.tinyMCE)
515                                 window.opener.tinyMCE.execCommand('mceInsertContent', false, html);
516         } else {
517                 insertAtCursor(window.opener.document.form.body_text, html);
518         }
519 }
520
521 function insertAtCursor(myField, myValue) {
522         //IE support
523         if (window.opener.document.selection) {
524                 myField.focus();
525                 sel = window.opener.document.selection.createRange();
526                 sel.text = myValue;
527         }
528         //MOZILLA/NETSCAPE support
529         else if (myField.selectionStart || myField.selectionStart == '0') {
530                 var startPos = myField.selectionStart;
531                 var endPos = myField.selectionEnd;
532                 myField.value = myField.value.substring(0, startPos)
533                 + myValue
534                 + myField.value.substring(endPos, myField.value.length);
535                 myField.focus();
536         } else {
537                 myField.value += myValue;
538                 myField.focus();
539         }
540 }
541
542 // This function does:
543 // 1. save into db via ajax
544 // 2. set the according field in opener window to the selected file
545 // 3. close file manager itself
546 function setAlternative(file, file_preview_link, cid, pid, a_type) {
547         // HTML encode the name of the secondary resource
548         file = jQuery('<div/>').text(file).html();
549
550         // save the selected secondary resource into db
551         jQuery.post("<?php echo AT_BASE_HREF; ?>mods/_core/editor/save_alternative.php", 
552                         {"pid":pid, "a_type":a_type, "alternative":file}, 
553                         function(data) {});
554
555         link_html = '\
556       <a href="'+file_preview_link+'" title="<?php echo _AT('new_window'); ?>" target="_new">'+file+'</a><br /> \
557       <a href="#" onclick="ATutor.poptastic(\\\'<?php echo AT_BASE_HREF; ?>mods/_core/file_manager/index.php?framed=1<?php echo SEP; ?>popup=1<?php echo SEP; ?>cp=<?php echo $cp.SEP; ?>cid='+cid+'<?php echo SEP; ?>pid='+pid+'<?php echo SEP; ?>a_type='+a_type+'\\\');return false;" title="<?php echo _AT('new_window'); ?>"> \
558         <img src="<?php echo AT_BASE_HREF; ?>images/home-tests_sm.png" border="0" title="<?php echo _AT('alter'); ?>" alt="<?php echo _AT('alter'); ?>" /> \
559       </a> \
560       <a href="#" onclick="removeAlternative(\\\'<?php echo $cp; ?>\\\', '+cid+','+pid+','+a_type+');return false;"> \
561         <img src="<?php echo AT_BASE_HREF; ?>images/icon_delete.gif" border="0" title="<?php echo _AT('remove'); ?>" alt="<?php echo _AT('remove'); ?>" /> \
562       </a> \
563     </div> \
564 ';
565         eval("window.opener.document.getElementById(\""+pid+"_"+a_type+"\").innerHTML = '"+link_html+"'");
566         
567         window.close();
568 }
569
570 // This function validates the url then call setAlternative()
571 function setURLAlternative() {
572         remote_url = jQuery('#remote_alternative').val();
573         if (remote_url == '' || remote_url == 'http://') {
574                 alert("<?php echo _AT('empty_url'); ?>");
575                 return false;
576         }
577         setAlternative(remote_url, remote_url, '<?php echo $cid; ?>.', '<?php echo $pid; ?>', '<?php echo $a_type; ?>');
578 }
579
580 <?php  if (isset($_SESSION['flash']) && $_SESSION['flash'] == "yes") { ?>
581 // toggle the view between div object and button
582 function toggleform(id, link) {
583         var obj = document.getElementById(id);
584         var btn = document.getElementById(link);
585
586         if (obj.style.display == "none") {
587                 //show
588                 obj.style.display='';
589                 obj.focus();
590
591                 btn.style.display = 'none';
592
593
594         } else {
595                 //hide
596                 obj.style.display='none';
597                 btn.style.display = '';
598         }
599 }
600
601 // set a cookie
602 function setCheckboxCookie(obj, value1, value2, date)
603 {
604         var today = new Date();
605         var the_date = new Date(date);
606         var the_cookie_date = the_date.toGMTString();
607         if (obj.checked==true)
608                 var the_cookie = value1 + ";expires=" + the_cookie_date;
609         else
610                 var the_cookie = value2 + ";expires=" + the_cookie_date;
611         document.cookie = the_cookie;
612 }
613 <?php } ?>
614
615 <?php 
616 // When uploading a file as an alternative content, set the alternative field in the opener window 
617 // and close "file manager" once the upload is successful
618 if ($a_type > 0 && isset($_GET['uploadfile']) && $_GET['uploadfile'] <> '') { ?>
619 function setAlternativeAndClose() {
620         setAlternative('<?php echo get_relative_path($_GET['cp'], $pathext).$_GET['uploadfile']; ?>', '<?php echo AT_BASE_HREF.$get_file.$pathext.urlencode($_GET['uploadfile']); ?>', '<?php echo $cid; ?>', '<?php echo $pid; ?>', '<?php echo $a_type; ?>');
621         window.close();
622 }
623
624 window.onload=setAlternativeAndClose;
625 <?php } ?>
626
627 //-->
628 </script>