0012950: More attachment methods for mail
[tine20] / tine20 / Felamimail / js / AttachmentUploadGrid.js
1 /*
2  * Tine 2.0
3  *
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Michael Spahn <m.spahn@metaways.de>
6  * @copyright   Copyright (c) 2017 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8
9 Ext.ns('Tine.Felamimail');
10
11 /**
12  * @namespace   Tine.Felamimail
13  * @class       Tine.Felamimail.AttachmentUploadGrid
14  * @extends     Ext.grid.GridPanel
15  *
16  * @author      Michael Spahn <m.spahn@metaways.de>
17  * @copyright   Copyright (c) 2017 Metaways Infosystems GmbH (http://www.metaways.de)
18  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
19  */
20 Tine.Felamimail.AttachmentUploadGrid = Ext.extend(Tine.widgets.grid.FileUploadGrid, {
21     /**
22      * Store with all valid attachment types
23      */
24     attachmentTypeStore: null,
25
26     currentRecord: null,
27
28     initComponent: function () {
29         this.attachmentTypeStore = new Ext.data.JsonStore({
30             fields: ['id', 'name'],
31             data: this.getAttachmentMethods()
32         });
33
34         Tine.Felamimail.AttachmentUploadGrid.superclass.initComponent.call(this);
35
36         this.on('beforeedit', this.onBeforeEdit.createDelegate(this));
37     },
38
39     onBeforeEdit: function (e) {
40         var record = e.record;
41         this.currentRecord = record;
42     },
43
44     getAttachmentMethods: function () {
45         var methods = [{
46             id: 'attachment',
47             name: i18n._('Attachment')
48         }];
49
50         if (!Tine.Tinebase.appMgr.isEnabled('Filemanager')) {
51             return methods;
52         }
53
54         methods = methods.concat([{
55                 id: 'download_public_fm',
56                 name: i18n._('Filemanager (inline)')
57             }, {
58                 id: 'download_protected_fm',
59                 name: i18n._('Filemanager (inline, password)')
60             }, {
61                 id: 'systemlink_fm',
62                 name: i18n._('Filemanager (Systemlink)')
63             }]
64         );
65
66         return methods;
67     },
68
69     /**
70      * Override columns
71      */
72     getColumns: function () {
73         var me = this;
74
75         var combo = new Ext.form.ComboBox({
76             blurOnSelect: true,
77             expandOnFocus: true,
78             listWidth: 150,
79             mode: 'local',
80             value: 'attachment',
81             displayField: 'name',
82             valueField: 'id',
83             store: me.attachmentTypeStore,
84             disableKeyFilter: true,
85             queryMode: 'local'
86         });
87
88         combo.doQuery = function (q, forceAll, uploadGrid) {
89             this.store.clearFilter();
90
91             this.store.filterBy(function (record, id) {
92                 var _ = window.lodash;
93
94                 if (_.get(uploadGrid.currentRecord, 'data.type') === 'file' && !_.get(uploadGrid.currentRecord, 'data.account_grants.downloadGrant', true) && id === 'attachment') {
95                     return false;
96                 }
97
98                 // only fm files can be system links
99                 if (_.get(uploadGrid.currentRecord, 'data.type') !== 'file' && id === 'systemlink_fm') {
100                     return false
101                 }
102
103                 // if no grants, then its not from fm
104                 if (!_.get(uploadGrid.currentRecord, 'data.account_grants.publishGrant', true) && id.startsWith('download_')) {
105                     return false;
106                 }
107
108                 return true;
109             }.createDelegate(this, [uploadGrid.currentRecord], true));
110
111             this.onLoad();
112         }.createDelegate(combo, [this], true);
113
114         return [{
115             id: 'attachment_type',
116             dataIndex: 'attachment_type',
117             sortable: true,
118             width: 150,
119             header: i18n._('Attachment Type'),
120             tooltip: i18n._('Click icon to change'),
121             listeners: {},
122             value: 'attachment',
123             renderer: function (value) {
124                 if (!value) {
125                     return null;
126                 }
127
128                 var record = me.attachmentTypeStore.getById(value);
129
130                 if (!record) {
131                     return null;
132                 }
133
134                 return record.get('name');
135             },
136             editor: combo
137         }, {
138             resizable: true,
139             id: 'name',
140             dataIndex: 'name',
141             flex: 1,
142             header: i18n._('name'),
143             renderer: Ext.ux.PercentRendererWithName
144         }, {
145             resizable: true,
146             id: 'size',
147             dataIndex: 'size',
148             width: 70,
149             header: i18n._('size'),
150             renderer: Ext.util.Format.fileSize
151         }, {
152             resizable: true,
153             id: 'type',
154             dataIndex: 'type',
155             width: 70,
156             header: i18n._('type')
157         }]
158     },
159
160     onFilesSelect: function (fileSelector, e) {
161         var files = fileSelector.getFileList();
162         Ext.each(files, function (file) {
163
164             var upload = new Ext.ux.file.Upload({
165                 file: file,
166                 fileSelector: fileSelector
167             });
168
169             var uploadKey = Tine.Tinebase.uploadManager.queueUpload(upload);
170             var fileRecord = Tine.Tinebase.uploadManager.upload(uploadKey);
171
172             upload.on('uploadfailure', this.onUploadFail, this);
173             upload.on('uploadcomplete', this.onUploadComplete, fileRecord);
174             upload.on('uploadstart', Tine.Tinebase.uploadManager.onUploadStart, this);
175
176             if (fileRecord.get('status') !== 'failure') {
177                 // overriden because of this
178                 fileRecord.data.attachment_type = 'attachment';
179                 this.store.add(fileRecord);
180             }
181
182
183         }, this);
184
185     },
186
187     onFileSelectFromFilemanager: function (nodes) {
188         var me = this;
189
190         Ext.each(nodes, function (node) {
191             var record = new Tine.Filemanager.Model.Node(node);
192
193             if (me.store.find('name', record.get('name')) === -1) {
194                 // Overriden because of this
195                 record.data.attachment_type = 'systemlink_fm';
196                 me.store.add(record);
197             } else {
198                 Ext.MessageBox.show({
199                     title: i18n._('Failure'),
200                     msg: i18n._('This file is already attached to this record.'),
201                     buttons: Ext.MessageBox.OK,
202                     icon: Ext.MessageBox.ERROR
203                 });
204             }
205         });
206     },
207
208 });