"MDL-13766, repository settings page"
[moodle.git] / repository / ajax.php
1 <?php
2 require_once('../config.php');
3 require_once('lib.php');
4 if(!empty($_GET['create'])) {
5     $result = true;
6     $entry = new stdclass;
7     $entry->repositoryname = 'Box.net';
8     $entry->repositorytype = 'boxnet';
9     $entry->contextid = SITEID;
10     $entry->userid = $USER->id;
11     $entry->timecreated = time();
12     $entry->timemodified = time();
13     $result = $result && $DB->insert_record('repository', $entry);
14     $entry->repositoryname = 'Flickr!';
15     $entry->repositorytype = 'flickr';
16     $entry->contextid = SITEID;
17     $entry->userid = $USER->id;
18     $entry->timecreated = time();
19     $entry->timemodified = time();
20     $result = $result && $DB->insert_record('repository', $entry);
21     if($result){
22         die('200');
23     } else {
24         die('403');
25     }
26 }
27 ?>
28 <html>
29 <head>
30 <title> Ajax picker demo page </title>
31 <?php
32 /*******************************************************\
34   This file is a demo page for ajax repository file
35   picker.
37 \*******************************************************/
38 $meta = <<<EOD
39 <link rel="stylesheet" type="text/css" href="../lib/yui/reset-fonts-grids/reset-fonts-grids.css" />
40 <link rel="stylesheet" type="text/css" href="../lib/yui/reset/reset-min.css" />
41 <link rel="stylesheet" type="text/css" href="../lib/yui/resize/assets/skins/sam/resize.css" />
42 <link rel="stylesheet" type="text/css" href="../lib/yui/container/assets/skins/sam/container.css" />
43 <link rel="stylesheet" type="text/css" href="../lib/yui/layout/assets/skins/sam/layout.css" />
44 <link rel="stylesheet" type="text/css" href="../lib/yui/button/assets/skins/sam/button.css" />
45 <link rel="stylesheet" type="text/css" href="../lib/yui/menu/assets/skins/sam/menu.css" />
46 <style type="text/css">
47 body {margin:0; padding:0; background: #FFF7C6;}
48 img{margin:0;padding:0;border:0}
49 h1{font-size: 36px}
50 #list{line-height: 1.5em}
51 #list a{ padding: 3px }
52 #list li a:hover{ background: gray; color:white; }
53 #paging{margin:10px 5px; clear:both}
54 #paging a{padding: 4px; border: 1px solid gray}
55 #panel{padding:0;margin:0; text-align:left;}
56 .file_name{color:green;}
57 .file_date{color:blue}
58 .file_size{color:gray}
59 .grid{width:80px; float:left;text-align:center;}
60 .grid div{width: 80px; height: 36px; overflow: hidden}
61 .repo-opt{font-size: 10px;color:red}
62 </style>
63 <script type="text/javascript" src="../lib/yui/yahoo/yahoo.js"></script>
64 <script type="text/javascript" src="../lib/yui/event/event.js"></script>
65 <script type="text/javascript" src="../lib/yui/dom/dom.js"></script>
66 <script type="text/javascript" src="../lib/yui/element/element-beta.js"></script>
67 <script type="text/javascript" src="../lib/yui/dragdrop/dragdrop.js"></script>
68 <script type="text/javascript" src="../lib/yui/container/container.js"></script>
69 <script type="text/javascript" src="../lib/yui/resize/resize-beta.js"></script>
70 <script type="text/javascript" src="../lib/yui/animation/animation.js"></script>
71 <script type="text/javascript" src="../lib/yui/layout/layout-beta.js"></script>
72 <script type="text/javascript" src="../lib/yui/connection/connection.js"></script>
73 <script type="text/javascript" src="../lib/yui/json/json.js"></script>
74 <script type="text/javascript" src="../lib/yui/menu/menu.js"></script>
75 <script type="text/javascript" src="../lib/yui/button/button-debug.js"></script>
76 <script type="text/javascript" src="../lib/yui/selector/selector-beta.js"></script>
77 <script type="text/javascript" src="../lib/yui/logger/logger.js"></script>
78 EOD;
79 echo $meta;
80 ?>
81 </head>
82 <body class=" yui-skin-sam">
83 <div id='control'>
84     <h1>Open the picker</h1>
85     <input type="button" id="con1" onclick='openpicker()' value="Open File Picker" style="font-size: 24px;padding: 1em" /> <br/>
86     <input type='hidden' id="result">
87 </div>
88 <div>
89     <div id="file-picker"></div>
90 </div>
91 <hr />
93 <div>
94     <h1>Create Repository Instance</h1>
95     <input type='button' id="create-repo" value="Create!" style="font-size: 24px;padding: 1em" />
96 <script type="text/javascript">
97 btn = document.getElementById('create-repo');
98 var create_cb = {
99     success: function(o) {
100         try{
101             var ret = o.responseText;
102         } catch(e) {
103             alert(e);
104         }
105         if(ret == 200) {
106             alert('Create Repository Instances successfully!');
107             btn.value='Done';
108         } else {
109             alert('Failed to create repository instances.');
110             btn.value='Created';
111             btn.disabled = false;
112         }
113     }
115 if(btn){
116     btn.onclick = function(){
117         btn.value = 'waiting...';
118         btn.disabled = true;
119         var trans = YAHOO.util.Connect.asyncRequest('GET', 'ajax.php?create=true', create_cb);
120     }
122 </script>
123 </div>
124 <script type="text/javascript">
125 var repository_client = (function() {
126     // private static field
127     var dver = '1.0';
128     // private static methods
129     function alert_version(){
130         alert(dver);
131     }
132     function _client(){
133         // public varible
134         this.name = 'repository_client';
135         // private varible
136         var Dom = YAHOO.util.Dom, Event = YAHOO.util.Event, layout = null, resize = null;
137         var IE_QUIRKS = (YAHOO.env.ua.ie && document.compatMode == "BackCompat");
138         var IE_SYNC = (YAHOO.env.ua.ie == 6 || (YAHOO.env.ua.ie == 7 && IE_QUIRKS));
139         var PANEL_BODY_PADDING = (10*2);
140         var btn_list = {label: 'List', value: 'l', checked: true, onclick: {fn: _client.viewlist}};
141         var btn_thumb = {label: 'Thumbnail', value: 't', onclick: {fn: _client.viewthumb}};
142         var select = new YAHOO.util.Element('select');
143         var list = null;
144         var resize = null;
145         var panel = new YAHOO.widget.Panel('file-picker', {
146             draggable: true,
147             close: false,
148             underlay: 'none',
149             width: '510px',
150             xy: [100, 100]
151         });
152         // construct code section
153         {
154             panel.setHeader('Moodle Repository Picker');
155             panel.setBody('<div id="layout"></div>');
156             panel.beforeRenderEvent.subscribe(function() {
157                 Event.onAvailable('layout', function() {
158                     layout = new YAHOO.widget.Layout('layout', {
159                         height: 400, width: 490,
160                         units: [
161                             {position: 'top', height: 32, resize: false, 
162                             body:'<div class="yui-buttongroup" id="repo-viewbar"></div>', gutter: '2'},
163                             {position: 'left', width: 150, resize: true, 
164                             body:'<ul id="repo-list"></ul>', gutter: '0 5 0 2', minWidth: 150, maxWidth: 300 },
165                             {position: 'center', body: '<div id="panel"></div>', 
166                             scroll: true, gutter: '0 2 0 0' }
167                         ]
168                     });
169                     layout.render();
170                 });
171             });
172             resize = new YAHOO.util.Resize('file-picker', {
173                 handles: ['br'],
174                 autoRatio: true,
175                 status: true,
176                 minWidth: 380,
177                 minHeight: 400
178             });
179             resize.on('resize', function(args) {
180                 var panelHeight = args.height;
181                 var headerHeight = this.header.offsetHeight; // Content + Padding + Border
182                 var bodyHeight = (panelHeight - headerHeight);
183                 var bodyContentHeight = (IE_QUIRKS) ? bodyHeight : bodyHeight - PANEL_BODY_PADDING;
184                 YAHOO.util.Dom.setStyle(this.body, 'height', bodyContentHeight + 'px');
185                 if (IE_SYNC) {
186                     this.sizeUnderlay();
187                     this.syncIframe();
188                 }
189                 layout.set('height', bodyContentHeight);
190                 layout.set('width', (args.width - PANEL_BODY_PADDING));
191                 layout.resize();
193             }, panel, true);
194             _client.viewbar = new YAHOO.widget.ButtonGroup({
195                 id: 'btngroup',
196                 name: 'buttons',
197                 disabled: true,
198                 container: 'repo-viewbar'
199                 });
200         }
201         // public method
202         this.create_picker = function(){
203             // display UI
204             panel.render();
205             _client.viewbar.addButtons([btn_list, btn_thumb]);
206             // init repository list
207             list = new YAHOO.util.Element('repo-list');
208             list.on('contentReady', function(e){
209                 for(var i=0; i<_client.repos.length; i++) {
210                     var repo = _client.repos[i];
211                     li = document.createElement('ul');
212                     li.innerHTML = '<a href="###" id="repo-call-'+repo.id+'">'+
213                         repo.repositoryname+'</a><br/>';
214                     li.innerHTML += '<a href="###" class="repo-opt" onclick="repository_client.search('+repo.id+')">Search</a>';
215                     li.innerHTML += '<a href="###" class="repo-opt" id="repo-logout-'+repo.id+'">Logout</a>';
216                     li.id = 'repo-'+repo.id;
217                     this.appendChild(li);
218                     var e = new YAHOO.util.Element('repo-call-'+repo.id);
219                     e.on('click', function(e){
220                         var re = /repo-call-(\d+)/i;
221                         var id = this.get('id').match(re);
222                         repository_client.req(id[1], 1, 0);
223                         });
224                     e = new YAHOO.util.Element('repo-logout-'+repo.id);
225                     e.on('click', function(e){
226                         var re = /repo-logout-(\d+)/i;
227                         var id = this.get('id').match(re);
228                         repository_client.req(id[1], 1, 1);
229                         });
230                     repo = null;
231                 }
232                 });
233         }
234     }
235     // public static varible
236     _client.repos = [];
237     _client.repositoryid = 0;
238     _client.datasource, 
239     _client.viewmode = 0;
240     _client.viewbar =null;
241     // public static mehtod
242     _client.postdata = function(obj) {
243         var str = '';
244         for(k in obj) {
245             if(obj[k] instanceof Array) {
246                 for(i in obj[k]) {
247                     str += (encodeURIComponent(k) +'[]='+encodeURIComponent(obj[k][i]));
248                     str += '&';
249                 }
250             } else {
251                 str += encodeURIComponent(k) +'='+encodeURIComponent(obj[k]);
252                 str += '&';
253             }
254         }
255         return str;
256     }
257     _client.loading = function(){
258         var panel = new YAHOO.util.Element('panel');
259         panel.get('element').innerHTML = '<img src="<?php echo $CFG->pixpath.'/i/loading.gif'?>" alt="loading..." />';
260     }
261     _client.rename = function(oldname, url){
262         var panel = new YAHOO.util.Element('panel');
263         var html = '<div>';
264         html += '<label for="newname">Name:</label>';
265         html += '<input type="text" id="newname" value="'+oldname+'" /><br/>';
266         html += '<label for="syncfile">Sync?</label>';
267         html += '<input type="checkbox" id="syncfile" /><br/>';
268         html += '<input type="hidden" id="fileurl" value="'+url+'" />';
269         html += '<input type="button" onclick="repository_client.download()" value="Download" />';
270         html += '<a href="###" onclick="repository_client.viewfiles()">Back</a>';
271         html += '</div>';
272         panel.get('element').innerHTML = html;
273     }
274     _client.print_login = function(){
275         var panel = new YAHOO.util.Element('panel');
276         var data = _client.datasource.l;
277         panel.get('element').innerHTML = data;
278     }
280     _client.viewfiles = function(){
281         if(_client.viewmode) {
282             _client.viewthumb();
283         } else {
284             _client.viewlist();
285         }
286     }
288     _client.viewthumb = function(){
289         var panel = new YAHOO.util.Element('panel');
290         _client.viewbar.check(1);
291         obj = _client.datasource.list;
292         var str = '';
293         str += _client.makepage();
294         for(k in obj){
295             str += '<div class="grid">';
296             str += '<img title="'+obj[k].title+'" src="'+obj[k].thumbnail+'" />';
297             str += '<div style="text-align:center">';
298             str += ('<input type="radio" title="'+obj[k].title
299                     +'" name="selected-files" value="'+obj[k].source
300                     +'" onclick=\'repository_client.rename("'+obj[k].title+'", "'
301                     +obj[k].source+'")\' />');
302             str += obj[k].title+'</div>';
303             str += '</div>';
304         }
305         panel.get('element').innerHTML = str;
306         _client.viewmode = 1;
307         return str;
308     }
310     _client.viewlist = function(){
311         var panel = new YAHOO.util.Element('panel');
312         var str = '';
313         _client.viewbar.check(0);
314         obj = _client.datasource.list;
315         str += _client.makepage();
316         var re = new RegExp();
317         re.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
318         for(k in obj){
319             str += ('<input type="radio" title="'+obj[k].title+'" name="selected-files" value="'+obj[k].source+'" onclick=\'repository_client.rename("'+obj[k].title+'", "'+obj[k].source+'")\' /> ');
320             if(re.test(obj[k].source)) {
321                 str += '<a class="file_name" href="'+obj[k].source+'">'+obj[k].title+'</a>';
322             } else {
323                 str += '<span class="file_name" >'+obj[k].title+'</span>';
324             }
325             str += '<br/>';
326             str += '<label>Date: </label><span class="file_date">'+obj[k].date+'</span><br/>';
327             str += '<label>Size: </label><span class="file_size">'+obj[k].size+'</span>';
328             str += '<br/>';
329         }
330         panel.get('element').innerHTML = str;
331         _client.viewmode = 0;
332         return str;
333     }
334     // XXX: A ugly hack to show paging for flickr
335     _client.makepage = function(){
336         var str = '';
337         if(_client.datasource.pages){
338             str += '<div id="paging">';
339             for(var i = 1; i <= _client.datasource.pages; i++) {
340                 str += '<a onclick="repository_client.req('+_client.repositoryid+', '+i+', 0)" href="###">';
341                 str += String(i);
342                 str += '</a> ';
343             }
344             str += '</div>';
345         }
346         return str;
347     }
348     _client.download = function(){
349         var title = document.getElementById('newname').value;
350         var file = document.getElementById('fileurl').value;
351         _client.loading();
352         var trans = YAHOO.util.Connect.asyncRequest('POST', 
353             'ws.php?id='+_client.repositoryid+'&action=download', _client.dlfile, 
354             _client.postdata({'file':file, 'title':title}));
355     }
356     _client.login = function(){
357         YAHOO.util.Connect.setForm('moodle-repo-login');
358         _client.loading();
359         var trans = YAHOO.util.Connect.asyncRequest('POST', 'ws.php', _client.callback);
360     }
361     _client.callback = {
362     success: function(o) {
363         var panel = new YAHOO.util.Element('panel');
364         try {
365             var ret = YAHOO.lang.JSON.parse(o.responseText);
366         } catch(e) {
367             alert('Callback: Invalid JSON String\n'+o.responseText);
368         }
369         if(ret.e){
370             panel.get('element').innerHTML = ret.e;
371             return;
372         }
373         _client.datasource = ret;
374         if(_client.datasource.l){
375             _client.print_login();
376         } else if(_client.datasource.list) {
377             if(_client.viewmode) {
378                 _client.viewthumb();
379             } else {
380                 _client.viewlist();
381             }
382         }
383       }
384     }
385     _client.dlfile = {
386     success: function(o) {
387         var panel = new YAHOO.util.Element('panel');
388         try {
389             var ret = YAHOO.lang.JSON.parse(o.responseText);
390         } catch(e) {
391             alert('Invalid JSON String\n'+o.responseText);
392         }
393         if(ret.e){
394             panel.get('element').innerHTML = ret.e;
395             return;
396         }
397         var html = '<h1>Download Successfully!</h1>';
398         html += '<a href="###" onclick="repository_client.viewfiles()">Back</a>';
399         panel.get('element').innerHTML = html;
400       }
401     }
402     // request file list or login
403     _client.req = function(id, path, reset) {
404         _client.viewbar.set('disabled', false);
405         _client.loading();
406         _client.repositoryid = id;
407         var trans = YAHOO.util.Connect.asyncRequest('GET', 'ws.php?id='+id+'&p='+path+'&reset='+reset, _client.callback);
408     }
409     _client.search = function(id){
410         var data = window.prompt("What are you searching for?");
411         if(data == null || data == '') {
412             alert('nothing entered');
413             return;
414         }
415         _client.viewbar.set('disabled', false);
416         _client.loading();
417         var trans = YAHOO.util.Connect.asyncRequest('GET', 'ws.php?id='+id+'&s='+data, _client.callback);
418     }
419     return _client;
420 })();
421 <?php
422 $repos = repository_instances();
423 foreach($repos as $repo) {
424     echo 'repository_client.repos.push('.json_encode($repo).');'."\n";
425     echo "\n";
427 ?>
428 function openpicker(){
429     var r = new repository_client();
430     r.create_picker();
432 </script>
433 </body>
434 </html>