Dateimanagement: Controller zum Laden und Generierung der Dateien
[kivitendo-erp.git] / js / kivi.File.js
1 namespace('kivi.File', function(ns) {
2
3   ns.rename = function(id,type,file_type,checkbox_class,is_global) {
4     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
5
6     if (checkboxes.size() === 0) {
7                   alert(kivi.t8("No file selected, please set one checkbox!"));
8                   return false;
9           }
10     if (checkboxes.size() > 1) {
11             alert(kivi.t8("More than one file selected, please set only one checkbox!"));
12                   return false;
13           }
14     var file_id = checkboxes[0].value;
15     $('#newfilename_id').val($('#filename_'+file_id).text());
16     $('#next_ids_id').val('');
17     $('#is_global_id').val(is_global);
18     $('#rename_id_id').val(file_id);
19     $('#sessionfile_id').val('');
20         $('#rename_extra_text').html('');
21     kivi.popup_dialog({
22                       id:     'rename_dialog',
23                       dialog: { title: kivi.t8("Rename attachment")
24                                , width:  400
25                                , height: 200
26                                , modal:  true } });
27     return true;
28   }
29   
30   ns.renameclose = function() {
31     $("#rename_dialog").dialog('close');
32     return false;
33   }
34   
35   ns.renameaction = function() {
36           $("#rename_dialog").dialog('close');
37     var data = {
38       action:          'File/ajax_rename',
39       id:              $('#rename_id_id').val(),
40             to:              $('#newfilename_id').val(),
41       next_ids:        $('#next_ids_id').val(),
42       is_global:       $('#is_global_id').val(),
43       sessionfile:     $('#sessionfile_id').val(),
44     };
45     $.post("controller.pl", data, kivi.eval_json_result);
46     return true;
47   }
48   
49   ns.askForRename = function(file_id,file_name,sessionfile,next_ids,is_global) {
50     $('#newfilename_id').val(file_name);
51     $('#rename_id_id').val(file_id);
52     $('#is_global_id').val(is_global);
53     $('#next_ids_id').val(next_ids);
54     $('#sessionfile_id').val(sessionfile);
55     $('#rename_extra_text').html(kivi.t8("The uploaded filename still exists.<br>If you not modify the name this is a new version of the file"));
56     kivi.popup_dialog(
57       {
58         id:     'rename_dialog',
59         dialog: { title: kivi.t8("Rename attachment")
60                   , width:  400
61                   , height: 200
62                   , modal:  true }
63       });
64   }
65
66   ns.upload = function(id,type,filetype,upload_title,gl) {
67     kivi.popup_dialog({ url:     'controller.pl',
68                         data:    { action: 'File/ajax_upload',
69                                    file_type:   filetype,
70                                    object_type: type,
71                                    object_id:   id,
72                                    is_global:   gl
73                                  },
74                         id:     'files_upload',
75                         dialog: { title: upload_title, width: 650, height: 240 } });
76     return true;
77   }
78
79   ns.reset_upload_form = function() {
80       $('#attachment_updfile').val('');
81       $("#upload_result").html('');
82       ns.allow_upload_submit();
83   }
84
85   ns.allow_upload_submit = function() {
86       $('#upload_selected_button').prop('disabled',$('#upload_files').val() === '');
87   }
88
89   ns.upload_selected_files = function(id,type,filetype,maxsize,is_global) {
90       var myform = document.getElementById("upload_form");
91       var filesize  = 0;
92       var myfiles = document.getElementById("upload_files").files;
93       for ( i=0; i < myfiles.length; i++ ) {
94           var fname ='';
95           try {
96               filesize  += myfiles[i].size;
97               fname = encodeURIComponent(myfiles[i].name);
98           }
99           catch(err) {
100               fname ='';
101               try {
102                   fname = myfiles[i].name;
103               }
104               catch(err2) { fname ='';}
105               $("#upload_result").html(kivi.t8("filename has not uploadable characters ")+fname);
106               return;     
107           }
108       }
109       if ( filesize > maxsize ) {
110           $("#upload_result").html(kivi.t8("filesize too big: ")+
111                                    filesize+ kivi.t8(" bytes, max=") + maxsize );
112           return;
113       }
114
115       myform.action ="controller.pl?action=File/ajax_files_uploaded&json=1&object_type="+
116           type+'&object_id='+id+'&file_type='+filetype+'&is_global='+is_global;
117       var oReq = new XMLHttpRequest();
118       oReq.onload            = ns.attSuccess;
119       oReq.upload.onprogress = ns.attProgress;
120       oReq.upload.onerror    = ns.attFailed;
121       oReq.upload.onabort    = ns.attCanceled;
122       oReq.open("post",myform.action, true);
123       $("#upload_result").html(kivi.t8("start upload"));
124       oReq.send(new FormData(myform));
125   }
126
127   ns.attProgress = function(oEvent) {
128       if (oEvent.lengthComputable) {
129           var percentComplete = (oEvent.loaded / oEvent.total) * 100;
130           $("#upload_result").html(percentComplete+" % "+ kivi.t8("uploaded"));
131       }
132   }
133
134   ns.attFailed = function(evt) {
135       $('#files_upload').dialog('close');
136       $("#upload_result").html(kivi.t8("An error occurred while transferring the file."));
137   }
138
139   ns.attCanceled = function(evt) {
140       $('#files_upload').dialog('close');
141       $("#upload_result").html(kivi.t8("The transfer has been canceled by the user."));
142   }
143
144   ns.attSuccess = function() {
145       $('#files_upload').dialog('close');
146       kivi.eval_json_result(jQuery.parseJSON(this.response));
147   }
148
149   ns.delete = function(id,type,file_type,checkbox_class,is_global) {
150     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
151
152     if ((checkboxes.size() === 0) || 
153         !confirm(kivi.t8('Do you really want to delete the selected documents?')))
154       return false;
155     var data = {
156       action     :  'File/ajax_delete',
157       object_id  :  id,
158       object_type:  type,
159       file_type  :  file_type,
160       ids        :  checkbox_class,
161       is_global  :  is_global,
162     };
163     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
164     return false;
165   }
166
167   ns.unimport = function(id,type,file_type,checkbox_class) {
168     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
169
170     if ((checkboxes.size() === 0) || 
171         !confirm(kivi.t8('Do you really want to unimport the selected documents?')))
172       return false;
173     var data = {
174       action     :  'File/ajax_unimport',
175       object_id  :  id,
176       object_type:  type,
177       file_type  :  file_type,
178       ids        :  checkbox_class,
179     };
180     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
181     return false;
182   }
183
184   ns.update = function(id,type,file_type,is_global) {
185     var data = {
186       action:       'File/list',
187       json:         1,
188       object_type:  type,
189       object_id:    id,
190       file_type:    file_type,
191       is_global:    is_global
192     };
193
194     $.post("controller.pl", data, kivi.eval_json_result);
195     return false;
196   }
197
198   ns.import = function (id,type,file_type,fromwhere,frompath) {
199     kivi.popup_dialog({ url:     'controller.pl',
200                         data:    { action      : 'File/ajax_importdialog',
201                                    object_type : type,
202                                    source      : fromwhere,
203                                    path        : frompath,
204                                    file_type   : file_type,
205                                    object_id   : id
206                                  },
207                         id:     'import_dialog',
208                         dialog: { title: kivi.t8('Import documents from #1',[fromwhere]), width: 420, height: 540 }
209                       });
210     return true;
211   }
212     
213   ns.importclose = function() {
214     $("#import_dialog").dialog('close');
215     return false;
216   }
217   
218   ns.importaction = function(id,type,file_type,fromwhere,frompath,checkbox_class) {
219     var checkboxes = $('.'+checkbox_class).filter(function () { return  $(this).prop('checked'); });
220
221           $("#import_dialog").dialog('close');
222     if (checkboxes.size() === 0) {
223                   return false;
224           }
225         var data = {
226         action     : 'File/ajax_import',
227         object_id  : id,
228         object_type: type,
229         file_type  : file_type,
230         source     : fromwhere,
231         path       : frompath,
232         ids        : checkbox_class
233     };
234     $.post("controller.pl?" + checkboxes.serialize(), data, kivi.eval_json_result);
235     return true;
236   }
237
238
239   ns.init = function() {
240   }
241
242 });