MDL-17457 migrated all modules to new db/install.php; added upgrade.txt file for...
[moodle.git] / repository / javascript.php
1 <?php // $Id$
2 ///////////////////////////////////////////////////////////////////////////
3 //                                                                       //
4 //       Don't modify this file unless you know how it works             //
5 //                                                                       //
6 ///////////////////////////////////////////////////////////////////////////
7 /**
8  * Return javascript to create file picker to browse repositories
9  * @global object $CFG
10  * @global object $USER
11  * @param object $context the context
12  * @return array
13  */
14 function repository_get_client($context, $accepted_filetypes = '*', $returnvalue = '*') {
15     global $CFG, $USER;
16     $suffix = uniqid();
17     $sesskey = sesskey();
18     // language string
19     $stradd       = get_string('add', 'repository');
20     $strback      = get_string('back', 'repository');
21     $strcancel    = get_string('cancel');
22     $strclose     = get_string('close', 'repository');
23     $strccache    = get_string('cleancache', 'repository');
24     $strcopying   = get_string('copying', 'repository');
25     $strdownbtn   = get_string('getfile', 'repository');
26     $strdownload  = get_string('downloadsucc', 'repository');
27     $strdate      = get_string('date', 'repository').': ';
28     $strerror     = get_string('error', 'repository');
29     $strfilenotnull = get_string('filenotnull', 'repository');
30     $strrefresh   = get_string('refresh', 'repository');
31     $strinvalidjson = get_string('invalidjson', 'repository');
32     $strlistview  = get_string('listview', 'repository');
33     $strlogin     = get_string('login', 'repository');
34     $strlogout    = get_string('logout', 'repository');
35     $strloading   = get_string('loading', 'repository');
36     $strthumbview = get_string('thumbview', 'repository');
37     $strtitle     = get_string('title', 'repository');
38     $strnoresult  = get_string('noresult', 'repository');
39     $strmgr       = get_string('manageurl', 'repository');
40     $strnoenter   = get_string('noenter', 'repository');
41     $strsave      = get_string('save', 'repository');
42     $strsaveas    = get_string('saveas', 'repository').': ';
43     $strsaved     = get_string('saved', 'repository');
44     $strsaving    = get_string('saving', 'repository');
45     $strsize      = get_string('size', 'repository').': ';
46     $strsync      = get_string('sync', 'repository');
47     $strsearch    = get_string('search', 'repository');
48     $strsearching = get_string('searching', 'repository');
49     $strsubmit    = get_string('submit', 'repository');
50     $strpreview   = get_string('preview', 'repository');
51     $strpopup     = get_string('popup', 'repository');
52     $strupload    = get_string('upload', 'repository').'...';
53     $struploading = get_string('uploading', 'repository');
54     $css = '';
55     if (!isset($CFG->repo_yui_loaded)) {
56         $css .= <<<EOD
57 <style type="text/css">
58 @import "$CFG->httpswwwroot/lib/yui/resize/assets/skins/sam/resize.css";
59 @import "$CFG->httpswwwroot/lib/yui/container/assets/skins/sam/container.css";
60 @import "$CFG->httpswwwroot/lib/yui/layout/assets/skins/sam/layout.css";
61 @import "$CFG->httpswwwroot/lib/yui/button/assets/skins/sam/button.css";
62 @import "$CFG->httpswwwroot/lib/yui/assets/skins/sam/treeview.css";
63 </style>
64 <style type="text/css">
65 .file-picker{font-size:12px;}
66 .file-picker strong{background:#FFFFCC}
67 .file-picker a{color: #336699}
68 .file-picker a:hover{background:#003366;color:white}
69 .fp-panel{padding:0;margin:0; text-align:left;}
70 .fp-searchbar{float:right}
71 .fp-viewbar{width:300px;float:left}
72 .fp-toolbar{padding: .8em;background: #FFFFCC;color:white;text-align:center}
73 .fp-toolbar a{padding: 0 .5em}
74 .fp-list{list-style-type:none;padding:0;float:left;width:100%;margin:0;}
75 .fp-list li{border-bottom:1px dotted gray;margin-bottom: 1em;}
76 .fp-repo-name{display:block;padding: .5em;margin-bottom: .5em}
77 .fp-pathbar{margin: .4em;border-bottom: 1px dotted gray;}
78 .fp-pathbar a{padding: .4em;}
79 .fp-rename-form{text-align:center}
80 .fp-rename-form p{margin: 1em;}
81 .fp-upload-form{margin: 2em 0;text-align:center}
82 .fp-upload-btn a{cursor: default;background: white;border:1px solid gray;color:black;padding: .5em}
83 .fp-upload-btn a:hover {background: grey;color:white}
84 .fp-paging{margin:1em .5em; clear:both;text-align:center;line-height: 2.5em;}
85 .fp-paging a{padding: .5em;border: 1px solid #CCC}
86 .fp-popup{text-align:center}
87 .fp-grid{float:left;text-align:center;}
88 .fp-grid div{overflow: hidden}
89 .fp-grid p{margin:0;padding:0;background: #FFFFCC}
90 .fp-grid .label{height:48px;text-align:center}
91 .fp-grid span{color:gray}
92 </style>
94 <!--[if IE 6]>
95     <style type="text/css">
96     /* Fix for IE6 */
97     .yui-skin-sam .yui-panel .hd{
99     }
100     </style>
101 <![endif]-->
102 EOD;
104         $js = <<<EOD
105 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/yahoo-dom-event/yahoo-dom-event.js"></script>
106 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/element/element-beta-min.js"></script>
107 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/treeview/treeview-min.js"></script>
108 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/dragdrop/dragdrop-min.js"></script>
109 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/container/container.js"></script>
110 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/resize/resize-min.js"></script>
111 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/layout/layout-min.js"></script>
112 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/connection/connection-min.js"></script>
113 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/json/json-min.js"></script>
114 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/button/button-min.js"></script>
115 <script type="text/javascript" src="$CFG->httpswwwroot/lib/yui/selector/selector-beta-min.js"></script>
116 EOD;
117         $CFG->repo_yui_loaded = true;
118     } else {
119         $js = '';
120     }
122     $js .= <<<EOD
123 <script type="text/javascript">
124 //<![CDATA[
125 //
126 var mdl_in_array = function(el, arr) {
127     for(var i = 0, l = arr.length; i < l; i++) {
128         if(arr[i] == el) {
129             return true;
130         }
131     }
132     return false;
135 var active_instance = null;
136 function repository_callback(id) {
137     active_instance.req(id, '', 0);
139 var repository_client_$suffix = (function() {
140 // private static field
141 var dver = '1.0';
142 // private static methods
143 function version() {
144     alert(dver);
146 function _client() {
147 // public varible
148 this.name = 'repository_client_$suffix';
149 // private varible
150 var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, layout = null, resize = null;
151 var IE_QUIRKS = (YAHOO.env.ua.ie && document.compatMode == "BackCompat");
152 var IE_SYNC = (YAHOO.env.ua.ie == 6 || (YAHOO.env.ua.ie == 7 && IE_QUIRKS));
153 var PANEL_BODY_PADDING = (10*2);
154 var btn_list = {label: '$strlistview', value: 'l', checked: true, onclick: {fn: _client.viewlist}};
155 var btn_thumb = {label: '$strthumbview', value: 't', onclick: {fn: _client.viewthumb}};
156 var repo_list = null;
157 var resize = null;
158 var filepicker = new YAHOO.widget.Panel('file-picker-$suffix', {
159     draggable: true,
160     close: true,
161     underlay: 'none',
162     zindex: 666666,
163     xy: [50, Dom.getDocumentScrollTop()+20]
164 });
165 // construct code section
167     filepicker.setHeader('$strtitle');
168     filepicker.setBody('<div id="layout-$suffix"></div>');
169     filepicker.beforeRenderEvent.subscribe(function() {
170         Event.onAvailable('layout-$suffix', function() {
171             layout = new YAHOO.widget.Layout('layout-$suffix', {
172                 height: 480, width: 700,
173                 units: [
174                 {position: 'top', height: 32, resize: false,
175                 body:'<div class="yui-buttongroup fp-viewbar" id="repo-viewbar-$suffix"></div><div class="fp-searchbar" id="search-div-$suffix"></div>', gutter: '2'},
176                 {position: 'left', width: 200, resize: true, scroll:true,
177                 body:'<ul class="fp-list" id="repo-list-$suffix"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
178                 {position: 'center', body: '<div class="fp-panel" id="panel-$suffix"></div>',
179                 scroll: true, gutter: '0 2 0 0' }
180                 ]
181             });
182             layout.render();
183         });
184     });
185     resize = new YAHOO.util.Resize('file-picker-$suffix', {
186         handles: ['br'],
187         autoRatio: true,
188         status: true,
189         minWidth: 680,
190         minHeight: 400
191     });
192     if(YAHOO.env.ua.ie == 6){
193         var fp_title = document.getElementById('file-picker-$suffix');
194         fp_title.style.width = '680px';
195     }
196     resize.on('resize', function(args) {
197         var panelHeight = args.height;
198         var headerHeight = this.header.offsetHeight; // Content + Padding + Border
199         var bodyHeight = (panelHeight - headerHeight);
200         var bodyContentHeight = (IE_QUIRKS) ? bodyHeight : bodyHeight - PANEL_BODY_PADDING;
201         Dom.setStyle(this.body, 'height', bodyContentHeight + 'px');
202         if(YAHOO.env.ua.ie == 6){
203             var fp_title = document.getElementById('file-picker-$suffix');
204             fp_title.style.width = args.width;
205         }
206         if (IE_SYNC) {
207             this.sizeUnderlay();
208             this.syncIframe();
209         }
210         layout.set('height', bodyContentHeight);
211         layout.set('width', (args.width - PANEL_BODY_PADDING));
212         layout.resize();
214     }, filepicker, true);
215     filepicker.update_instances = function(){
216         _client.print_instances();
217     }
218     _client.viewbar = new YAHOO.widget.ButtonGroup({
219         id: 'btngroup-$suffix',
220         name: 'buttons',
221         disabled: true,
222         container: 'repo-viewbar-$suffix'
223     });
225 // public method
226 this.show = function() {
227     filepicker.update_instances();
228     filepicker.show();
230 this.hide = function() {
231     filepicker.hide();
233 this.create_picker = function() {
234     // display UI
235     filepicker.render();
236     _client.viewbar.addButtons([btn_thumb, btn_list]);
237     // init repository list
238     repo_list = new YAHOO.util.Element('repo-list-$suffix');
239     repo_list.on('contentReady', function(e) {
240         var searchbar = new YAHOO.util.Element('search-div-$suffix');
241         searchbar.get('element').innerHTML = '<input id="search-input-$suffix" /><button id="search-btn-$suffix">$strsearch</button>';
242         var btn_search = new YAHOO.util.Element('search-btn-$suffix');
243         var input_keyword = new YAHOO.util.Element('search-input-$suffix');
244         btn_search.fnSearch = function(e) {
245             var el = new YAHOO.util.Element('search-input-$suffix')
246             var keyword = el.get('value');
247             var params = [];
248             params['s'] = keyword;
249             params['env']=_client.env;
250             params['action']='gsearch';
251             params['sesskey']='$sesskey';
252             params['ctx_id']=$context->id;
253             _client.loading('load');
254             var trans = YAHOO.util.Connect.asyncRequest('POST',
255                 '$CFG->httpswwwroot/repository/ws.php?action=gsearch', this.global_search_cb, _client.postdata(params));
256         }
257         btn_search.global_search_cb={
258             success: function(o) {
259                 var panel = new YAHOO.util.Element('panel-$suffix');
260                 if(!o.responseText) {
261                     panel.get('element').innerHTML = '$strnoresult';
262                     return;
263                 }
264                 try {
265                     var data = YAHOO.lang.JSON.parse(o.responseText);
266                 } catch(e) {
267                     alert('$strinvalidjson - |global_search_cb| -'+_client.stripHTML(o.responseText));
268                     return;
269                 }
270                 _client.ds={};
271                 if(!data.list || data.list.length<1){
272                     panel.get('element').innerHTML = '$strnoresult';
273                     return;
274                 }
275                 _client.ds.list = data.list;
276                 if(_client.viewmode) {
277                     _client.viewlist();
278                 } else {
279                     _client.viewthumb();
280                 }
281                 var el = new YAHOO.util.Element('search-input-$suffix')
282                 el.set('value', '');
283             }
284         }
285         btn_search.on('contentReady', function() {
286             btn_search.on('click', this.fnSearch, this.input_keyword);
287         });
288         input_keyword.on('contentReady', function() {
289             var scope = document.getElementById('search-input-$suffix');
290             var k1 = new YAHOO.util.KeyListener(scope, {keys:13}, {fn:function(){this.fnSearch()},scope:btn_search, correctScope: true});
291             k1.enable();
292         });
293         _client.print_instances();
294     });
298 // public static varible
299 _client.repos = [];
300 _client.repositoryid = 0;
301 // _client.ds save all data received from server side
302 _client.ds = null;
303 _client.viewmode = 0;
304 _client.viewbar = null;
305 _client.print_instances = function() {
306     var container = new YAHOO.util.Element('repo-list-$suffix');
307     container.set('innerHTML', '');
308     for(var i in _client.repos) {
309         var repo = _client.repos[i];
310         var support = false;
311         if(repository_client_$suffix.env=='editor' && _client.accepted_types != '*'){
312             if(repo.supported_types!='*'){
313                 for (var j in repo.supported_types){
314                     if(mdl_in_array(repo.supported_types[j], _client.accepted_types)){
315                         support = true;
316                     }
317                 }
318             }
319         }else{
320             support = true;
321         }
322         if(repo.supported_types == '*' || support){
323             var li = document.createElement('li');
324             li.id = 'repo-$suffix-'+repo.id;
325             var icon = document.createElement('img');
326             icon.src = repo.icon;
327             icon.width = '16';
328             icon.height = '16';
329             var link = document.createElement('a');
330             link.href = '###';
331             link.id = 'repo-call-$suffix-'+repo.id;
332             link.appendChild(icon);
333             link.className = 'fp-repo-name';
334             link.onclick = function() {
335                 var re = /repo-call-$suffix-(\d+)/i;
336                 var id = this.id.match(re);
337                 repository_client_$suffix.req(id[1], '', 0);
338             }
339             link.innerHTML += ' '+repo.name;
340             li.appendChild(link);
341             container.appendChild(li);
342             repo = null;
343         }
344     }
346 _client.stripHTML = function(str){
347     var re= /<\S[^><]*>/g
348     var ret = str.replace(re, "")
349     return ret;
352 // public static mehtod
353 _client.postdata = function(obj) {
354     var str = '';
355     for(k in obj) {
356         if(obj[k] instanceof Array) {
357             for(i in obj[k]) {
358                 str += (encodeURIComponent(k) +'[]='+encodeURIComponent(obj[k][i]));
359                 str += '&';
360             }
361         } else {
362             str += encodeURIComponent(k) +'='+encodeURIComponent(obj[k]);
363             str += '&';
364         }
365     }
366     return str;
368 _client.loading = function(type, name) {
369     var panel = new YAHOO.util.Element('panel-$suffix');
370     panel.get('element').innerHTML = '';
371     var content = document.createElement('div');
372     content.style.textAlign='center';
373     var para = document.createElement('P');
374     var img = document.createElement('IMG');
375     if(type=='load') {
376         img.src = '$CFG->pixpath/i/loading.gif';
377         para.innerHTML = '$strloading';
378     }else{
379         img.src = '$CFG->pixpath/i/progressbar.gif';
380         para.innerHTML = '$strcopying <strong>'+name+'</strong>';
381     }
382     content.appendChild(para);
383     content.appendChild(img);
384     //content.innerHTML = '';
385     panel.get('element').appendChild(content);
387 _client.rename = function(oldname, url, icon, repo_id) {
388     var thumbnail = document.getElementById('fp-grid-panel-$suffix');
389     if(thumbnail){
390         thumbnail.style.display = 'none';
391     }
392     var header = document.getElementById('fp-header-$suffix');
393     header.style.display = 'none';
394     var footer = document.getElementById('fp-footer-$suffix');
395     footer.style.display = 'none';
396     var panel = new YAHOO.util.Element('panel-$suffix');
397     var html = '<div class="fp-rename-form">';
398     _client.repositoryid=repo_id;
399     html += '<p><img src="'+icon+'" /></p>';
400     html += '<p><label for="newname-$suffix">$strsaveas</label>';
401     html += '<input type="text" id="newname-$suffix" value="'+oldname+'" /></p>';
402     /**
403     html += '<p><label for="syncfile-$suffix">$strsync</label> ';
404     html += '<input type="checkbox" id="syncfile-$suffix" /></p>';
405     */
406     html += '<p><input type="hidden" id="fileurl-$suffix" value="'+url+'" />';
407     html += '<input type="button" onclick="repository_client_$suffix.download()" value="$strdownbtn" />';
408     html += '<input type="button" onclick="repository_client_$suffix.viewfiles()" value="$strcancel" /></p>';
409     html += '</div>';
410     panel.get('element').innerHTML += html;
411     var tree = document.getElementById('treediv-$suffix');
412     if(tree){
413         tree.style.display = 'none';
414     }
416 _client.popup = function(url) {
417     active_instance = repository_client_$suffix;
418     _client.win = window.open(url,'repo_auth', 'location=0,status=0,scrollbars=0,width=500,height=300');
419     return false;
421 _client.print_login = function() {
422     var panel = new YAHOO.util.Element('panel-$suffix');
423     var data = _client.ds.login;
424     var str = '';
425     var has_pop = false;
426     for(var k in data) {
427         if(data[k].type=='popup') {
428             str += '<p class="fp-popup">$strpopup</p>';
429             str += '<p class="fp-popup"><button onclick="repository_client_$suffix.popup(\''+data[k].url+'\')">$strlogin</button>';
430             str += '</p>';
431             has_pop = true;
432         }else if(data[k].type=='textarea') {
433             str += '<p><textarea id="'+data[k].id+'" name="'+data[k].name+'"></textarea></p>';
434         }else{
435             str += '<p>';
436             var lable_id = '';
437             var field_id = '';
438             var field_value = '';
439             if(data[k].id) {
440                 lable_id = ' for="'+data[k].id+'"';
441                 field_id = ' id="'+data[k].id+'"';
442             }
443             if (data[k].label) {
444                 str += '<label'+lable_id+'>'+data[k].label+'</label><br/>';
445             }
446             if(data[k].value) {
447                 field_value = ' value="'+data[k].value+'"';
448             }
449             str += '<input type="'+data[k].type+'"'+' name="'+data[k].name+'"'+field_id+field_value+' />';
450             str += '</p>';
451         }
452     }
453     if(!has_pop) {
454         str += '<p><input type="button" onclick="repository_client_$suffix.login()" value="$strsubmit" /></p>';
455     }
456     panel.get('element').innerHTML = str;
459 _client.viewfiles = function() {
460     if(_client.viewmode) {
461         _client.viewlist();
462     } else {
463         _client.viewthumb();
464     }
466 _client.print_header = function() {
467     var panel = new YAHOO.util.Element('panel-$suffix');
468     var str = '<div id="fp-header-$suffix">';
469     str += '<div class="fp-toolbar" id="repo-tb-$suffix"></div>';
470     str += _client.makepage('header');
471     str += '</div>';
472     panel.set('innerHTML', str);
473     _client.makepath();
475 _client.print_footer = function() {
476     var panel = document.getElementById('panel-$suffix');
477     var footer = document.createElement('DIV');
478     footer.id = 'fp-footer-$suffix';
479     footer.innerHTML += _client.uploadcontrol();
480     footer.innerHTML += _client.makepage('footer');
481     panel.appendChild(footer);
482     // add repository manage buttons here
483     var oDiv = document.getElementById('repo-tb-$suffix');
484     if(!_client.ds.nosearch) {
485         var search = document.createElement('A');
486         search.href = '###';
487         search.innerHTML = '<img src="$CFG->pixpath/a/search.png" /> $strsearch';
488         oDiv.appendChild(search);
489         search.onclick = function() {
490             repository_client_$suffix.search_form(repository_client_$suffix.repositoryid);
491         }
492     }
493     // weather we use cache for this instance, this button will reload listing anyway
494     var ccache = document.createElement('A');
495     ccache.href = '###';
496     ccache.innerHTML = '<img src="$CFG->pixpath/a/refresh.png" /> $strrefresh';
497     oDiv.appendChild(ccache);
498     ccache.onclick = function() {
499         var params = [];
500         params['env']=_client.env;
501         params['sesskey']='$sesskey';
502         params['ctx_id']=$context->id;
503         params['repo_id']=repository_client_$suffix.repositoryid;
504         _client.loading('load');
505         var trans = YAHOO.util.Connect.asyncRequest('POST',
506                 '$CFG->httpswwwroot/repository/ws.php?action=ccache', repository_client_$suffix.req_cb, _client.postdata(params));
507     }
508     if(_client.ds.manage) {
509         var mgr = document.createElement('A');
510         mgr.innerHTML = '<img src="$CFG->pixpath/a/setting.png" /> $strmgr';
511         mgr.href = _client.ds.manage;
512         mgr.target = "_blank";
513         oDiv.appendChild(mgr);
514     }
515     if(!_client.ds.nologin) {
516         var logout = document.createElement('A');
517         logout.href = '###';
518         logout.innerHTML = '<img src="$CFG->pixpath/a/logout.png" /> $strlogout';
519         oDiv.appendChild(logout);
520         logout.onclick = function() {
521             repository_client_$suffix.req(repository_client_$suffix.repositoryid, 1, 1);
522         }
523     }
525 _client.viewthumb = function(ds) {
526     _client.viewmode = 0;
527     _client.viewbar.check(0);
528     var container = document.getElementById('panel-$suffix');
529     var panel = document.createElement('DIV');
530     panel.id = 'fp-grid-panel-$suffix';
531     var list = null;
532     if(arguments.length == 1) {
533         list = ds;
534     } else {
535         // from button
536         list = _client.ds.list;
537     }
538     _client.print_header();
539     var count = 0;
540     for(k in list) {
541         // the container
542         var el = document.createElement('div');
543         el.className='fp-grid';
544         // the file name
545         var title = document.createElement('div');
546         title.id = 'grid-title-'+String(count);
547         title.className = 'label';
548         if(list[k].children) {
549             title.innerHTML = '<i><u>'+list[k].title+'</i></u>';
550         } else {
551             title.innerHTML += '<a href="###"><span>'+list[k].title+"</span></a>";
552         }
553         if(list[k].thumbnail_width){
554             el.style.width = list[k].thumbnail_width+'px';
555             title.style.width = (list[k].thumbnail_width-20)+'px';
556         } else {
557             el.style.width = title.style.width = '80px';
558         }
559         var frame = document.createElement('DIV');
560         frame.style.textAlign='center';
561         if(list[k].thumbnail_height){
562             frame.style.height = list[k].thumbnail_height+'px';
563         }
564         var img = document.createElement('img');
565         img.src = list[k].thumbnail;
566         var link = document.createElement('A');
567         link.href='###';
568         link.id = 'img-id-'+String(count);
569         link.appendChild(img);
570         frame.appendChild(link);
571         el.appendChild(frame);
572         if(list[k].url) {
573             el.innerHTML += '<p><a target="_blank" href="'+list[k].url+'">$strpreview</a></p>';
574         }
575         el.appendChild(title);
576         
577         panel.appendChild(el);
578         if(list[k].children) {
579             var folder = new YAHOO.util.Element(link.id);
580             folder.ds = list[k].children;
581             folder.path = list[k].path;
582             folder.on('contentReady', function() {
583                 this.on('click', function() {
584                     if(_client.ds.dynload) {
585                         var params = [];
586                         params['p'] = this.path;
587                         params['env'] = _client.env;
588                         params['repo_id'] = _client.repositoryid;
589                         params['ctx_id'] = $context->id;
590                         params['sesskey']= '$sesskey';
591                         _client.loading('load');
592                         var trans = YAHOO.util.Connect.asyncRequest('POST',
593                                 '$CFG->httpswwwroot/repository/ws.php?action=list', _client.req_cb, _client.postdata(params));
594                     }else{
595                         _client.viewthumb(this.ds);
596                     }
597                 });
598             });
599         } else {
600             var el_title = new YAHOO.util.Element(title.id);
601             var file = new YAHOO.util.Element(link.id);
602             el_title.title = file.title = list[k].title;
603             el_title.value = file.value = list[k].source;
604             el_title.icon = file.icon  = list[k].thumbnail;
605             if(list[k].repo_id) {
606                 el_title.repo_id = file.repo_id = list[k].repo_id;
607             }else{
608                 el_title.repo_id = file.repo_id = _client.repositoryid;
609             }
610             file.on('contentReady', function() {
611                 this.on('click', function() {
612                     repository_client_$suffix.rename(this.title, this.value, this.icon, this.repo_id);
613                 });
614             });
615             el_title.on('contentReady', function() {
616                 this.on('click', function() {
617                     repository_client_$suffix.rename(this.title, this.value, this.icon, this.repo_id);
618                 });
619             });
620         }
621         count++;
622     }
623     container.appendChild(panel);
624     _client.print_footer();
626 _client.buildtree = function(node, level) {
627     if(node.children) {
628         node.title = '<i><u>'+node.title+'</u></i>';
629     }
630     var info = {
631         label:node.title,
632         title:"$strdate"+node.date+' $strsize'+node.size,
633         filename:node.title,
634         value:node.source,
635         icon:node.thumbnail,
636         path:node.path
637     };
638     var tmpNode = new YAHOO.widget.TextNode(info, level, false);
639     var tooltip = new YAHOO.widget.Tooltip(tmpNode.labelElId, {
640         context:tmpNode.labelElId, text:info.title});
641     if(node.repo_id) {
642         tmpNode.repo_id=node.repo_id;
643     }else{
644         tmpNode.repo_id=_client.repositoryid;
645     }
646     if(node.children) {
647         if(node.expanded) {
648             tmpNode.expand();
649         }
650         tmpNode.isLeaf = false;
651         if (node.path) {
652             tmpNode.path = node.path;
653         } else {
654             tmpNode.path = '';
655         }
656         for(var c in node.children) {
657             _client.buildtree(node.children[c], tmpNode);
658         }
659     } else {
660         tmpNode.isLeaf = true;
661     }
663 _client.dynload = function (node, fnLoadComplete) {
664     var callback = {
665         success: function(o) {
666              try {
667                  var json = YAHOO.lang.JSON.parse(o.responseText);
668              } catch(e) {
669                  alert('$strinvalidjson - |dynload| -'+_client.stripHTML(o.responseText));
670                  return;
671              }
672              for(k in json.list) {
673                  _client.buildtree(json.list[k], node);
674              }
675              o.argument.fnLoadComplete();
676         },
677         failure:function(oResponse) {
678             alert('$strerror - |dynload| -');
679             oResponse.argument.fnLoadComplete();
680         },
681         argument:{"node":node, "fnLoadComplete": fnLoadComplete}
682     }
683     var params = [];
684     params['p']=node.path;
685     params['env']=_client.env;
686     params['sesskey']='$sesskey';
687     params['ctx_id']=$context->id;
688     params['repo_id']=_client.repositoryid;
689     var trans = YAHOO.util.Connect.asyncRequest('POST',
690             '$CFG->httpswwwroot/repository/ws.php?action=list', callback, _client.postdata(params));
692 _client.viewiframe = function() {
693     var panel = new YAHOO.util.Element('panel-$suffix');
694     panel.get('element').innerHTML = "<iframe frameborder=\"0\" width=\"98%\" height=\"400px\" src=\""+_client.ds.iframe+"\" />";
696 _client.viewlist = function() {
697     _client.viewmode = 1;
698     var panel = new YAHOO.util.Element('panel-$suffix');
699     _client.viewbar.check(1);
700     list = _client.ds.list;
701     _client.print_header();
702     panel.get('element').innerHTML += '<div id="treediv-$suffix"></div>';
703     var tree = new YAHOO.widget.TreeView('treediv-$suffix');
704     if(_client.ds.dynload) {
705         tree.setDynamicLoad(_client.dynload, 1);
706     } else {
707     }
708     for(k in list) {
709         _client.buildtree(list[k], tree.getRoot());
710     }
711     tree.draw();
712     tree.subscribe('clickEvent', function(e){
713         if(e.node.isLeaf){
714             repository_client_$suffix.rename(e.node.data.filename, e.node.data.value, e.node.data.icon, e.node.repo_id);
715         }
716     });
717     _client.print_footer();
719 _client.upload = function() {
720     var u = _client.ds.upload;
721     var aform = document.getElementById(u.id);
722     var parent = document.getElementById(u.id+'_div');
723     var d = document.getElementById(_client.ds.upload.id+'-file');
724     if(d.value!='' && d.value!=null) {
725         var container = document.createElement('DIV');
726         container.id = u.id+'_loading';
727         container.style.textAlign='center';
728         var img = document.createElement('IMG');
729         img.src = '$CFG->pixpath/i/progressbar.gif';
730         var para = document.createElement('p');
731         para.innerHTML = '$struploading';
732         container.appendChild(para);
733         container.appendChild(img);
734         parent.appendChild(container);
735         YAHOO.util.Connect.setForm(aform, true, true);
736         var trans = YAHOO.util.Connect.asyncRequest('POST',
737                 '$CFG->httpswwwroot/repository/ws.php?action=upload&sesskey=$sesskey&ctx_id=$context->id&repo_id='
738                 +_client.repositoryid,
739                 _client.upload_cb);
740     }else{
741         alert('$strfilenotnull');
742     }
744 _client.upload_cb = {
745 upload: function(o) {
746         try {
747             var ret = YAHOO.lang.JSON.parse(o.responseText);
748         } catch(e) {
749             alert('$strinvalidjson - |upload| -'+_client.stripHTML(o.responseText));
750         }
751         if(ret && ret.e) {
752             var panel = new YAHOO.util.Element('panel-$suffix');
753             panel.get('element').innerHTML = ret.e;
754             return;
755         }
756         if(ret) {
757             alert('$strsaved');
758             repository_client_$suffix.end(ret);
759         }
760     }
762 _client.uploadcontrol = function() {
763     var str = '';
764     if(_client.ds.upload) {
765         str += '<div id="'+_client.ds.upload.id+'_div" class="fp-upload-form">';
766         str += '<form id="'+_client.ds.upload.id+'" onsubmit="return false">';
767         str += '<label for="'+_client.ds.upload.id+'-file">'+_client.ds.upload.label+': </label>';
768         str += '<input type="file" id="'+_client.ds.upload.id+'-file" name="repo_upload_file" />';
769         str += '<p class="fp-upload-btn"><a href="###" onclick="return repository_client_$suffix.upload();">$strupload</a></p>';
770         str += '</form>';
771         str += '</div>';
772     }
773     return str;
775 _client.makepage = function(id) {
776     var str = '';
777     if(_client.ds.pages) {
778         str += '<div class="fp-paging" id="paging-'+id+'-$suffix">';
779         for(var i = 1; i <= _client.ds.pages; i++) {
780             if(!_client.ds.search_result){
781                     str += '<a onclick="repository_client_$suffix.req('+_client.repositoryid+', '+i+', 0)" href="###">';
782             } else {
783                     str += '<a onclick="repository_client_$suffix.search_paging('+_client.repositoryid+', '+i+')" href="###">';
784             }
785             str += String(i);
786             str += '</a> ';
787         }
788         str += '</div>';
789     }
790     return str;
792 _client.search_paging = function(id, path) {
793     _client.viewbar.set('disabled', false);
794     _client.loading('load');
795     _client.repositoryid = id;
796     var params = [];
797     params['p'] = path;
798     params['env']=_client.env;
799     params['action']='search';
800     params['search_paging']='true';
801     params['sesskey']='$sesskey';
802     params['ctx_id']=$context->id;
803     params['repo_id']=id;
804     var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action='+action, _client.req_cb, _client.postdata(params));
806 _client.makepath = function() {
807     if(_client.viewmode == 1) {
808         return;
809     }
810     var panel = new YAHOO.util.Element('panel-$suffix');
811     var p = _client.ds.path;
812     if(p && p.length!=0) {
813         var oDiv = document.createElement('DIV');
814         oDiv.id = "path-$suffix";
815         oDiv.className = "fp-pathbar";
816         panel.get('element').appendChild(oDiv);
817         for(var i = 0; i < _client.ds.path.length; i++) {
818             var link = document.createElement('A');
819             link.href = "###";
820             link.innerHTML = _client.ds.path[i].name;
821             link.id = 'path-'+i+'-el';
822             var sep = document.createElement('SPAN');
823             sep.innerHTML = '/';
824             oDiv.appendChild(link);
825             oDiv.appendChild(sep);
826             var el = new YAHOO.util.Element(link.id);
827             el.id = _client.repositoryid;
828             el.path = _client.ds.path[i].path;
829             el.on('contentReady', function() {
830                 this.on('click', function() {
831                     repository_client_$suffix.req(this.id, this.path, 0);
832                 })
833             });
834         }
835     }
837 // send download request
838 _client.download = function() {
839     var title = document.getElementById('newname-$suffix').value;
840     var file = document.getElementById('fileurl-$suffix').value;
841     _client.loading('download', title);
842     var params = [];
843     if(_client.itemid){
844         params['itemid']=_client.itemid;
845     }
846     params['env']=_client.env;
847     params['file']=file;
848     params['title']=title;
849     params['sesskey']='$sesskey';
850     params['ctx_id']=$context->id;
851     params['repo_id']=_client.repositoryid;
852     var trans = YAHOO.util.Connect.asyncRequest('POST',
853             '$CFG->httpswwwroot/repository/ws.php?action=download', _client.download_cb, _client.postdata(params));
855 // send login request
856 _client.login = function() {
857     var params = [];
858     var data = _client.ds.login;
859     for (var k in data) {
860         if(data[k].type!='popup') {
861             var el = document.getElementsByName(data[k].name)[0];
862             params[data[k].name] = '';
863             if(el.type == 'checkbox') {
864                 params[data[k].name] = el.checked;
865             } else {
866                 params[data[k].name] = el.value;
867             }
868         }
869     }
870     params['env'] = _client.env;
871     params['repo_id'] = _client.repositoryid;
872     params['ctx_id'] = $context->id;
873     params['sesskey']= '$sesskey';
874     _client.loading('load');
875     var trans = YAHOO.util.Connect.asyncRequest('POST',
876             '$CFG->httpswwwroot/repository/ws.php?action=sign', _client.req_cb, _client.postdata(params));
878 _client.end = function(obj) {
879     if(_client.env=='filepicker') {
880         _client.target.value = obj['id'];
881     }else if(_client.env=='editor'){
882         _client.target.value = obj['url'];
883         _client.target.onchange();
884     }
885     _client.formcallback(obj);
886     _client.instance.hide();
887     _client.viewfiles();
889 _client.hide = function() {
890     _client.instance.hide();
891     _client.viewfiles();
893 // request file list or login
894 _client.req = function(id, path, logout) {
895     _client.viewbar.set('disabled', false);
896     _client.loading('load');
897     _client.repositoryid = id;
898     if (logout == 1) {
899         action = 'logout';
900     } else {
901         action = 'list';
902     }
903     var params = [];
904     params['p'] = path;
905     params['env']=_client.env;
906     params['action']=action;
907     params['sesskey']='$sesskey';
908     params['ctx_id']=$context->id;
909     params['repo_id']=id;
910     params['accepted_types'] = _client.accepted_types;
911     var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action='+action, _client.req_cb, _client.postdata(params));
913 _client.search_form_cb = {
914 success: function(o) {
915      var el = document.getElementById('fp-search-dlg');
916      var _r = repository_client_$suffix;
917      if(el) {
918          el.innerHTML = '';
919      } else {
920          var el = document.createElement('DIV');
921          el.id = 'fp-search-dlg';
922      }
923      var div1 = document.createElement('DIV');
924      div1.className = 'hd';
925      div1.innerHTML = "$strsearching\"" + _r.repos[_r.repositoryid].name + '"';
926      var div2 = document.createElement('DIV');
927      div2.className = 'bd';
928      var sform = document.createElement('FORM');
929      sform.method = 'POST';
930      sform.id = "fp-search-form";
931      sform.action = '$CFG->httpswwwroot/repository/ws.php?action=search';
932      sform.innerHTML = o.responseText;
933      div2.appendChild(sform);
934      el.appendChild(div1);
935      el.appendChild(div2);
936      document.body.appendChild(el);
937      var dlg = new YAHOO.widget.Dialog("fp-search-dlg",{
938         postmethod: 'async',
939         draggable: true,
940         width : "30em",
941         fixedcenter : true,
942         zindex: 766667,
943         visible : false,
944         constraintoviewport : true,
945         buttons : [
946             { text:"$strsubmit",handler: function() {
947                 _client.viewbar.set('disabled', false); _client.loading('load');
948                 YAHOO.util.Connect.setForm('fp-search-form', false, false);
949                 this.cancel();
950                 var trans = YAHOO.util.Connect.asyncRequest('POST',
951                     '$CFG->httpswwwroot/repository/ws.php?action=search&env='+_client.env, _client.req_cb);
952                 },isDefault:true},
953             {text:"$strcancel",handler:function() {this.cancel()}}
954         ]
955     });
956     dlg.render();
957     dlg.show();
960 _client.search_form = function(id) {
961     var params = [];
962     params['env']=_client.env;
963     params['sesskey']='$sesskey';
964     params['ctx_id']=$context->id;
965     params['repo_id']=id;
966     var trans = YAHOO.util.Connect.asyncRequest('POST', '$CFG->httpswwwroot/repository/ws.php?action=searchform', _client.search_form_cb, _client.postdata(params));
968 _client.req_cb = {
969 success: function(o) {
970      var panel = new YAHOO.util.Element('panel-$suffix');
971      try {
972          var data = YAHOO.lang.JSON.parse(o.responseText);
973      } catch(e) {
974          alert('$strinvalidjson - |req_cb| -'+_client.stripHTML(o.responseText));
975      };
976      if(data && data.e) {
977          panel.get('element').innerHTML = data.e;
978          return;
979      }
980      _client.ds = data;
981      if(!data) {
982          return;
983      }else if(data.iframe) {
984          _client.viewiframe();
985      }else if(data.login) {
986          _client.print_login();
987      }else if(data.list) {
988          if(_client.viewmode) {
989              _client.viewlist();
990          } else {
991              _client.viewthumb();
992          }
993      }
996 _client.download_cb = {
997 success: function(o) {
998      var panel = new YAHOO.util.Element('panel-$suffix');
999      try {
1000          var data = YAHOO.lang.JSON.parse(o.responseText);
1001      } catch(e) {
1002          alert('$strinvalidjson - |download_cb| -'+_client.stripHTML(o.responseText));
1003      }
1004      if(data && data.e) {
1005          panel.get('element').innerHTML = data.e;
1006          return;
1007      }
1008      if(data) {
1009          repository_client_$suffix.end(data);
1010      }
1014 return _client;
1015 })();
1016 EOD;
1018 $user_context = get_context_instance(CONTEXT_USER, $USER->id);
1019 if (is_array($accepted_filetypes) && in_array('*', $accepted_filetypes)) {
1020     $accepted_filetypes = '*';
1022 $repos = repository::get_instances(array($user_context, $context, get_system_context()), null, true, null, $accepted_filetypes, $returnvalue);
1023 $js .= "\r\n".'repository_client_'.$suffix.'.repos=[];'."\r\n";
1024 foreach ($repos as $repo) {
1025     $info = $repo->ajax_info();
1026     $js .= "\r\n";
1027     $js .= 'repository_client_'.$suffix.'.repos['.$info->id.']='.json_encode($repo->ajax_info()).';'."\n";
1029 $js .= "\r\n";
1031 $ft = new file_type_to_ext();
1032 $image_file_ext = json_encode($ft->get_file_ext(array('image')));
1033 $video_file_ext = json_encode($ft->get_file_ext(array('video')));
1034 $accpeted_file_ext = json_encode($ft->get_file_ext($accepted_filetypes));
1035 $js .= <<<EOD
1036 function openpicker_$suffix(params) {
1037     if(params.filetype) {
1038         if(params.filetype == 'image') {
1039             repository_client_$suffix.accepted_types = $image_file_ext;
1040         } else if(params.filetype == 'video' || params.filetype== 'media') {
1041             repository_client_$suffix.accepted_types = $video_file_ext;
1042         } else if(params.filetype == 'file') {
1043             repository_client_$suffix.accepted_types = '*';
1044         }
1045     } else {
1046         repository_client_$suffix.accepted_types = $accpeted_file_ext;
1047     }
1048     if(!repository_client_$suffix.instance) {
1049         repository_client_$suffix.env = params.env;
1050         repository_client_$suffix.target = params.target;
1051         if(params.itemid){
1052             repository_client_$suffix.itemid = params.itemid;
1053         }
1054         repository_client_$suffix.instance = new repository_client_$suffix();
1055         repository_client_$suffix.instance.create_picker();
1056         if(params.callback) {
1057             repository_client_$suffix.formcallback = params.callback;
1058         } else {
1059             repository_client_$suffix.formcallback = function() {};
1060         }
1061     } else {
1062         repository_client_$suffix.target = params.target;
1063         repository_client_$suffix.instance.show();
1064     }
1066 //]]>
1067 </script>
1068 EOD;
1069 return array('css'=>$css, 'js'=>$js, 'suffix'=>$suffix);