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