admin/js/app.ts   F
last analyzed

Complexity

Total Complexity 189
Complexity/F 4.61

Size

Lines of Code 1477
Function Count 41

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 917
dl 0
loc 1477
rs 1.683
c 0
b 0
f 0
wmc 189
mnd 148
bc 148
fnc 41
bpm 3.6097
cpm 4.6097
noi 0

40 Functions

Rating   Name   Duplication   Size   Complexity  
C AdminApp.group 0 43 9
A AdminApp.change_icon 0 13 2
A AdminApp.acl 0 32 4
C AdminApp.load 0 67 9
F AdminApp._acl_dialog 0 199 37
A AdminApp.iframe_location 0 13 1
B AdminApp.run 0 36 8
B AdminApp.et2_ready 0 63 6
F AdminApp.observer 0 96 17
A AdminApp.acl_reopen_dialog 0 21 2
A AdminApp._hide_navbar 0 21 4
A AdminApp._acl_callback 0 13 1
A AdminApp._ajax_load_callback 0 12 3
D AdminApp.check_owner 0 66 12
A AdminApp.linkHandler 0 21 3
A AdminApp._acl_delete 0 43 4
A AdminApp.group_list 0 8 1
A AdminApp.destroy 0 13 1
A AdminApp.wizard_sieve_ssl_onchange 0 13 2
A AdminApp.wizard_manual 0 7 1
A AdminApp.wizard_smtp_ssl_onchange 0 12 3
A AdminApp.account_edit_action 0 12 2
A AdminApp.change_account 0 10 1
A AdminApp.cmds_onselect 0 38 4
A AdminApp.smime_exportCert 0 15 1
A AdminApp.wizard_imap_ssl_onchange 0 12 2
A AdminApp.submit_statistic 0 54 4
A AdminApp.wizard_popup_resize 0 21 2
A AdminApp.cf_type_change 0 15 1
C AdminApp.account_hide_not_applying 0 51 9
A AdminApp.change_folders 0 11 2
A AdminApp.account 0 28 2
B AdminApp.cf_type_delete 0 64 6
A AdminApp.wizard_sieve_onchange 0 10 1
A AdminApp.edit_multiple 0 15 1
A AdminApp.clear_cache 0 15 1
B AdminApp.emailadminActiveAccounts 0 36 5
A AdminApp.wizard_detect 0 21 3
A AdminApp.login_background_update 0 14 1
C AdminApp.smime_genCertificate 0 73 11

How to fix   Complexity   

Complexity

Complex classes like admin/js/app.ts often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
/**
2
 * EGroupware - Admin - Javascript UI
3
 *
4
 * @link: https://www.egroupware.org
5
 * @package filemanager
6
 * @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
7
 * @copyright (c) 2013-20 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
8
 * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
9
 */
10
11
/*egw:uses
12
	/api/js/jsapi/egw_app.js
13
 */
14
15
import 'jquery';
16
import 'jqueryui';
17
import '../jsapi/egw_global';
18
import '../etemplate/et2_types';
19
20
import {EgwApp} from '../../api/js/jsapi/egw_app';
21
22
/**
23
 * UI for Admin
24
 *
25
 * @augments AppJS
26
 */
27
class AdminApp extends EgwApp
28
/**
29
 * @lends app.classes.admin
30
 */
31
{
32
	readonly appname = 'admin';
33
34
	/**
35
	 * reference to iframe
36
	 *
37
	 * {et2_iframe}
38
	 */
39
	iframe : any = null;
40
41
	/**
42
	 * reference to nextmatch
43
	 *
44
	 * {et2_extension_nextmatch}
45
	 */
46
	nm : any = null;
47
48
	/**
49
	 * Reference to div to hold AJAX loadable pages
50
	 *
51
	 * {et2_box}
52
	 */
53
	ajax_target : any = null;
54
55
	/**
56
	 * Reference to ACL edit dialog (not the list)
57
	 */
58
	acl_dialog : any = null;
59
	tree : any = null;
60
	groups : any;
61
62
	/**
63
	 * Constructor
64
	 *
65
	 * @memberOf app.classes.admin
66
	 */
67
	constructor()
68
	{
69
		// call parent
70
		super();
71
	}
72
73
	/**
74
	 * Destructor
75
	 */
76
	destroy(_app)
77
	{
78
		this.iframe = null;
79
		this.nm = null;
80
		this.acl_dialog = null;
81
		this.tree = null;
82
83
		// call parent
84
		super.destroy(_app);
85
	}
86
87
	/**
88
	 * This function is called when the etemplate2 object is loaded
89
	 * and ready.  If you must store a reference to the et2 object,
90
	 * make sure to clean it up in destroy().
91
	 *
92
	 * @param {etemplate2} _et2
93
	 * @param {string} _name name of template loaded
94
	 */
95
	et2_ready(_et2, _name)
96
	{
97
		// call parent
98
		super.et2_ready(_et2, _name);
99
100
		switch(_name)
101
		{
102
			case 'admin.index':
103
				var iframe = this.iframe = this.et2.getWidgetById('iframe');
104
				this.nm = this.et2.getWidgetById('nm');
105
				this.groups = this.et2.getWidgetById('groups');
106
				this.groups.set_disabled(true);
107
				this.ajax_target = this.et2.getWidgetById('ajax_target');
108
				this.tree = this.et2.getWidgetById('tree');
109
				if (iframe)
110
				{
111
					var self = this;
112
					jQuery(iframe.getDOMNode()).off('load.admin')
113
						.bind('load.admin', function(){
114
							if (this.contentDocument.location.href.match(/(\/admin\/|\/admin\/index.php|menuaction=admin.admin_ui.index)/))
115
							{
116
								this.contentDocument.location.href = 'about:blank';	// stops redirect from admin/index.php
117
								self.load();	// load own top-level index aka user-list
118
							}
119
							self._hide_navbar.call(self);
120
						}
121
					);
122
				}
123
				if( this.ajax_target && this.et2.getArrayMgr('content').getEntry('ajax_target'))
124
				{
125
					this.load(this.et2.getArrayMgr('content').getEntry('ajax_target'));
126
				}
127
				break;
128
129
			case 'admin.customfield_edit':
130
				// Load settings appropriate to currently set type
131
				var widget = _et2.widgetContainer.getWidgetById('cf_type');
132
				this.cf_type_change(null,widget);
133
				break;
134
135
			case 'admin.mailaccount':
136
				this.account_hide_not_applying();
137
				break;
138
			case 'admin.cmds':
139
				var selected = this.et2.getWidgetById('nm').getSelection();
140
				if (selected && selected.ids.length == 1)
141
				{
142
					this.cmds_onselect(selected.ids);
143
				}
144
				else
145
				{
146
					this.et2.getWidgetById('splitter').dock();
147
				}
148
				break;
149
		}
150
	}
151
152
	/**
153
	 * Show given url in (visible) iframe or nextmatch with accounts (!_url)
154
	 *
155
	 * @param {string} [_url=] url to show in iframe or nothing for showing
156
	 */
157
	load(_url? : string)
158
	{
159
		if (this.iframe && this.iframe.getDOMNode().contentDocument.location.href
160
			.match(/menuaction=admin.admin_statistics.submit.+required=true/) && ( !_url ||
161
			!_url.match(/statistics=(postpone|canceled|submitted)/)))
162
		{
163
			this.egw.message(this.egw.lang('Please submit (or postpone) statistic first'), 'info');
164
			return;	// do not allow to leave statistics submit
165
		}
166
		// url outside EGroupware eg. eSyncPro linking to wikipedia
167
		if (_url && _url.indexOf(this.egw.webserverUrl) == -1)
168
		{
169
			window.open(_url, '_blank');
170
			return;
171
		}
172
		var ajax : any = false;
173
		if (_url)
174
		{
175
			// Try to load it without the iframe
176
			ajax = _url.match(/ajax=true/) && _url.match(/menuaction=/);
177
			if(ajax)
178
			{
179
180
				if(this.ajax_target.node.children.length)
181
				{
182
					// Node has children already?  Check for loading over an
183
					// existing etemplate, and remove it first
184
					jQuery(this.ajax_target.node.children).each(function() {
185
						var old = etemplate2.getById(this.id);
186
						if(old) old.clear();
187
					});
188
					jQuery(this.ajax_target.node).empty();
189
				}
190
				this.egw.json(
191
					framework.activeApp.getMenuaction('ajax_exec', _url),
192
					// It's important that the context is null, or etemplate2
193
					// won't load the template properly
194
					[_url], this._ajax_load_callback,null, true, this
195
				).sendRequest();
196
			}
197
			else
198
			{
199
				this.iframe.set_src(_url);
200
			}
201
			var m = _url.match(/menuaction=([^&]+)(?:.*appname=(\w+))?/);
202
			if(m && m.length >= 2)
203
			{
204
				var app = m[2] ? m[2] : m[1].split('.')[0];
205
				this.tree.set_value('/apps/'+app+'/'+m[1]);
206
			}
207
		}
208
		else
209
		{
210
			this.egw.app_header('');
211
			// blank iframe, to not keep something running there
212
			this.iframe.getDOMNode().contentDocument.location.href = 'about:blank';
213
		}
214
		this.iframe.set_disabled(!_url || ajax);
215
		this.nm.set_disabled(!!_url || ajax);
216
		this.groups.set_disabled(true);
217
		this.ajax_target.set_disabled(!ajax);
218
	}
219
220
	/**
221
	 * Observer method receives update notifications from all applications
222
	 *
223
	 * App is responsible for only reacting to "messages" it is interested in!
224
	 *
225
	 * @param {string} _msg message (already translated) to show, eg. 'Entry deleted'
226
	 * @param {string} _app application name
227
	 * @param {(string|number)} _id id of entry to refresh or null
228
	 * @param {string} _type either 'update', 'edit', 'delete', 'add' or null
229
	 * - update: request just modified data from given rows.  Sorting is not considered,
230
	 *		so if the sort field is changed, the row will not be moved.
231
	 * - edit: rows changed, but sorting may be affected.  Requires full reload.
232
	 * - delete: just delete the given rows clientside (no server interaction neccessary)
233
	 * - add: requires full reload for proper sorting
234
	 * @param {string} _msg_type 'error', 'warning' or 'success' (default)
235
	 * @param {string} _targetapp which app's window should be refreshed, default current
236
	 * @return {false|*} false to stop regular refresh, thought all observers are run
237
	 */
238
	observer(_msg, _app, _id, _type, _msg_type, _targetapp)
239
	{
240
		switch(_app)
241
		{
242
			case 'admin':
243
				// if iframe is used --> refresh it
244
				var iframe_node = this.iframe ? this.iframe.getDOMNode() : undefined;
245
				var iframe_url = iframe_node ? iframe_node.contentDocument.location.href : undefined;
246
				if (_id && iframe_url != 'about:blank')
247
				{
248
					var refresh_done = false;
249
					// Try for intelligent et2 refresh inside iframe
250
					if(iframe_node && iframe_node.contentWindow && iframe_node.contentWindow.etemplate2)
251
					{
252
						var templates = iframe_node.contentWindow.etemplate2.getByApplication('admin');
253
						for(var i = 0; i < templates.length; i++)
254
						{
255
							templates[i].refresh(_msg,_app,_id,_type);
256
							refresh_done = true;
257
						}
258
					}
259
					if (!refresh_done)	// --> reload iframe
260
					{
261
						this.load(iframe_url);
262
					}
263
					return false;	// --> no regular refresh
264
				}
265
				// invalidate client-side account-cache
266
				this.egw.invalidate_account(_id, _type);
267
				// group deleted, added or updated
268
				if (_id < 0)
269
				{
270
					var tree = this.et2.getWidgetById('tree');
271
					var nm = this.et2.getWidgetById('nm');
272
					switch(_type)
273
					{
274
						case 'delete':
275
							tree.deleteItem('/groups/'+_id, false);
276
							if (nm) nm.getInstanceManager().submit();
277
							break;
278
279
						default:	// add, update, edit, null
280
							if (nm)
281
							{
282
								var activeFilters = nm.activeFilters;
283
								nm.getInstanceManager().submit();
284
								var nm = this.et2.getWidgetById('nm');
285
								nm.applyFilters(activeFilters);
286
							}
287
288
					}
289
					var refreshTree = this.et2.getWidgetById('tree');
290
					if (refreshTree) refreshTree.refreshItem('/groups');
291
					return false;	// --> no regular refresh
292
				}
293
				// not a user or group, eg. categories
294
				else if (!_id)
295
				{
296
					// Try just refreshing the nextmatch
297
					if(this.nm)
298
					{
299
						this.nm.refresh();
300
					}
301
					if(this.groups)
302
					{
303
						this.groups.refresh();
304
					}
305
					if(this.et2 && this.et2.getWidgetById('nm') && this.nm !== this.et2.getWidgetById('nm'))
306
					{
307
						this.et2.getWidgetById('nm').refresh();
308
					}
309
					else
310
					{
311
						// Load something else
312
						this.load();
313
					}
314
					return false;	// --> no regular refresh needed
315
				}
316
		}
317
	}
318
319
	/**
320
	 * Hide navbar for idots template
321
	 *
322
	 * Just a hack for old idots, not neccesary for jdots
323
	 */
324
	_hide_navbar()
325
	{
326
		var document = this.iframe.getDOMNode().contentDocument;
327
328
		if (!document) return;	// nothing we can do ...
329
330
		// set white background, as transparent one lets account-list show through
331
		document.getElementsByTagName('body')[0].style.backgroundColor = 'white';
332
333
		// hide navbar elements
334
		var ids2hide = ['divLogo', 'topmenu', 'divAppIconBar', 'divStatusBar', 'tdSidebox', 'divAppboxHeader'];
335
		for(var i=0; i < ids2hide.length; ++i)
336
		{
337
			var elem = document.getElementById(ids2hide[i]);
338
			if (elem) elem.style.display = 'none';
339
		}
340
	}
341
342
	/**
343
	 * Set location of iframe for given _action and _sender (row)
344
	 *
345
	 * @param _action
346
	 * @param _senders
347
	 */
348
	iframe_location(_action, _senders)
349
	{
350
		var id = _senders[0].id.split('::');
351
		var url = _action.data.url.replace(/(%24|\$)id/, id[1]);
352
353
		this.load(url);
354
	}
355
356
	/**
357
	 * Callback to load an etemplate
358
	 *
359
	 * @param {Object[]} _data
360
	 */
361
	_ajax_load_callback(_data)
362
	{
363
		if(!_data || _data.type != undefined) return;
364
365
		// Insert the content, etemplate will load into it
366
		jQuery(this.ajax_target.node).append(typeof _data === 'string' ? _data : _data[0]);
367
	}
368
369
	/**
370
	 * Link hander for jDots template to just reload our iframe, instead of reloading whole admin app
371
	 *
372
	 * @param _url
373
	 * @return boolean true, if linkHandler took care of link, false otherwise
374
	 */
375
	linkHandler(_url)
376
	{
377
		var matches = _url.match(/menuaction=admin.admin_ui.index.*&load=([^&]+)/);
378
		if (_url !='about:blank' && (this.iframe != null && !_url.match('menuaction=admin.admin_ui.index') || matches))
379
		{
380
			if (matches)
381
			{
382
				_url = _url.replace(/menuaction=admin.admin_ui.index/, 'menuaction='+matches[1]).replace(/&(load=[^&]+)/g, '');
383
			}
384
			this.load(_url);
385
			return true;
386
		}
387
		// can not load our own index page, has to be done by framework
388
		return false;
389
	}
390
391
	/**
392
	 * Run an admin module / onclick callback for tree
393
	 *
394
	 * @param {string} _id id of clicked node
395
	 * @param {et2_tree} _widget reference to tree widget
396
	 */
397
	run(_id, _widget)
398
	{
399
		var link = _widget.getUserData(_id, 'link');
400
401
		this.groups.set_disabled(true);
402
403
		if (_id == '/accounts' || _id.substr(0, 8) == '/groups/')
404
		{
405
			this.load();
406
			var parts = _id.split('/');
407
			this.nm.applyFilters({ filter: parts[2] ? parts[2] : '', search: ''});
408
		}
409
		else if (_id === '/groups')
410
		{
411
			this.load();
412
			this.group_list();
413
		}
414
		else if (typeof link == 'undefined')
415
		{
416
			_widget.openItem(_id, 'toggle');
417
		}
418
		else if (link[0] == '/' || link.substr(0,4) == 'http')
419
		{
420
			link += (link.indexOf('?') >= 0 ? '&' : '?')+'nonavbar=1';
421
			this.load(link);
422
		}
423
		else if (link.substr(0,11) == 'javascript:')
424
		{
425
			eval(link.substr(11));
426
		}
427
	}
428
429
	/**
430
	 * Show the group list in the main window
431
	 */
432
	group_list()
433
	{
434
		this.nm.set_disabled(true);
435
		this.groups.set_disabled(false);
436
	}
437
438
439
	/**
440
	 * View, edit or delete a group callback for tree
441
	 *
442
	 * @param {object} _action egwAction
443
	 * @param {array} _senders egwActionObject _senders[0].id holds id
444
	 */
445
	group(_action, _senders)
446
	{
447
		// Tree IDs look like /groups/ID, nm uses admin::ID
448
		var from_nm = _senders[0].id.indexOf('::') > 0;
449
		var account_id = _senders[0].id.split(from_nm ? '::' : '/')[from_nm ? 1 : 2];
450
451
		switch(_action.id)
452
		{
453
			case 'view':
454
				this.run(from_nm ? '/groups/'+account_id : _senders[0].id, this.et2.getWidgetById('tree'));
455
				break;
456
457
			case 'delete':
458
				this.egw.json('admin_account::ajax_delete_group', [account_id, _action.data, this.et2._inst.etemplate_exec_id]).sendRequest();
459
				break;
460
			default:
461
				if (!_action.data.url)
462
				{
463
					alert('Missing url in action '+_action.id+'!');
464
					break;
465
				}
466
				var url = unescape(_action.data.url).replace('$id', account_id);
467
				if (url[0] != '/' && url.substr(0, 4) != 'http')
468
				{
469
					url = this.egw.link('/index.php', url);
470
				}
471
				if (_action.data.popup || _action.data.width && _action.data.height)
472
				{
473
					this.egw.open_link(url, '_blank', _action.data.popup ? _action.data.popup : _action.data.width + 'x' + _action.data.height);
474
				}
475
				else
476
				{
477
					this.load(url);
478
				}
479
				break;
480
		}
481
	}
482
483
	/**
484
	 * Modify an ACL entry
485
	 *
486
	 * @param {object} _action egwAction
487
	 * @param {array} _senders egwActionObject _senders[0].id holds the id "admin::app:account:location"
488
	 */
489
	acl(_action, _senders)
490
	{
491
		var ids = [];
492
		for(var i=0; i < _senders.length; ++i)
493
		{
494
			ids.push(_senders[i].id.split('::').pop());	// remove "admin::" prefix
495
		}
496
497
		// For edit, set some data from the list since it's already there
498
		var content = _senders[0].id ? jQuery.extend({}, egw.dataGetUIDdata(_senders[0].id).data) : {};
499
500
		switch(_action.id)
501
		{
502
			case 'delete':
503
				this._acl_delete(ids);
504
				break;
505
506
			case 'add':
507
				// No content, even if they clicked on a row
508
				// Defaults set by _acl_content() based on nm values
509
				content = {};
510
				// Fall through
511
			case 'edit':
512
				this._acl_dialog(content);
513
				break;
514
		}
515
	}
516
517
	_acl_delete(ids)
518
	{
519
		var app = egw.app_name();	// can be either admin or preferences!
520
		if (app != 'admin') app = 'preferences';
521
		var className = app+'_acl';
522
		var callback = function(_button_id, _value) {
523
			if(_button_id != et2_dialog.OK_BUTTON) return;
524
525
			var request = egw.json(className+'::ajax_change_acl', [ids, null, _value, this.et2._inst.etemplate_exec_id], this._acl_callback,this,false,this)
526
				.sendRequest();
527
		}.bind(this);
528
529
		var modifications : any = {};
530
		var dialog_options = {
531
			callback: callback,
532
			title: this.egw.lang('Delete'),
533
			buttons: et2_dialog.BUTTONS_OK_CANCEL,
534
			value: {
535
				content: {},
536
				sel_options: {},
537
				modifications: modifications,
538
				readonlys: {}
539
			},
540
			template: egw.webserverUrl+'/admin/templates/default/acl.delete.xet'
541
		};
542
543
		// Handle policy documentation tab here
544
		if(this.egw.user('apps').policy)
545
		{
546
			dialog_options['width'] = 550;
547
			dialog_options['height'] = 350,
548
			modifications.tabs = {
549
				add_tabs:   true,
550
				tabs: [{
551
					label:    egw.lang('Documentation'),
552
					template: 'policy.admin_cmd',
553
					prepend:  false
554
				}]
555
			};
556
		}
557
		// Create the dialog
558
		this.acl_dialog = et2_createWidget("dialog", dialog_options, et2_dialog._create_parent(app));
559
	}
560
561
	/**
562
	 * Create the ACL edit dialog, including defaults & fetching what can be found
563
	 *
564
	 * @param content List of content for the dialog template
565
	 * @param sel_options optional select options
566
	 * @param {etemplate2} etemplate of etemplate that 'owns' the dialog
567
	 * @param {string} app Name of app
568
	 * @param {function} callback
569
	 */
570
	_acl_dialog(content, sel_options?, etemplate?, app?, callback? : Function)
571
	{
572
		if(typeof content == 'undefined') content = {};
573
574
		// Determine which application we're running as
575
		app = app ? app : egw.app_name();
576
		// can be either admin or preferences!
577
		if (app != 'admin') app = 'preferences';
578
		// Get by ID, since this.et2 isn't always the ACL list
579
		var et2 = etemplate ? etemplate : etemplate2.getById('admin-acl').widgetContainer;
580
		var className = app+'_acl';
581
		var acl_rights : any = {};
582
		var readonlys : any = {acl: {}};
583
		var modifications : any = {};
584
585
		// Select options are already here, just pull them and pass along
586
		sel_options = et2.getArrayMgr('sel_options').data||{};
587
588
		// Some defaults
589
		if(et2 && et2.getWidgetById('nm'))
590
		{
591
			// This is which checkboxes are available for each app
592
			acl_rights = et2.getWidgetById('nm').getArrayMgr('content').getEntry('acl_rights')||{};
593
594
			if(!content.acl_appname)
595
			{
596
				// Pre-set appname to currently selected
597
				content.acl_appname = et2.getWidgetById('filter2').getValue()||"";
598
			}
599
			if(!content.acl_account)
600
			{
601
				content.acl_account = et2.getWidgetById('nm').getArrayMgr('content').getEntry('account_id');
602
			}
603
			if(!content.acl_location)
604
			{
605
				content.acl_location = et2.getWidgetById('filter').getValue() == 'run' ? 'run' : null;
606
			}
607
			// If no admin rights, change UI to not allow adding access to apps
608
			if(content.acl_location == 'run' && !egw.user('apps')['admin'])
609
			{
610
				content.acl_location = null;
611
			}
612
			if(content.acl_location == 'run')
613
			{
614
				// These are the apps the account has access to
615
				// Fetch current values from server
616
				this.egw.json(className+'::ajax_get_app_list', [content.acl_account], function(data) {content.apps = data;},this,false,this)
617
					.sendRequest();
618
			}
619
			else
620
			{
621
				// Restrict application selectbox to only apps that support ACL
622
				sel_options.acl_appname = [];
623
				for(let app in acl_rights)
624
				{
625
					sel_options.acl_appname.push({value: app, label: this.egw.lang(app)});
626
				}
627
				// Sort list
628
				sel_options.acl_appname.sort(function(a,b) {
629
					if(a.label > b.label) return 1;
630
					if(a.label < b.label) return -1;
631
					return 0;
632
				});
633
			}
634
635
		}
636
		if(content.acl_appname)
637
		{
638
			// Load checkboxes & their values
639
			content.acl_rights = content.acl_rights ? parseInt(content.acl_rights) : null;
640
			jQuery.extend(content, {acl:[],right:[],label:[]});
641
			for( var right in acl_rights[content.acl_appname])
642
			{
643
				// only user himself is allowed to grant private (16) rights
644
				if(right == '16' && content['acl_account'] != egw.user('account_id'))
645
				{
646
					readonlys.acl[content.acl.length] = true;
647
				}
648
				content.acl.push(content.acl_rights & parseInt(right));
649
				content.right.push(right);
650
				content.label.push(egw.lang(acl_rights[content.acl_appname][right]));
651
			}
652
		}
653
654
		if(content.acl_account && !egw.user('apps')['admin'])
655
		{
656
			readonlys.acl_account = true;
657
		}
658
		if(content.acl_location)
659
		{
660
			sel_options.acl_location = jQuery.extend({},sel_options.acl_location);
661
			this.egw.link_title('api-accounts', content.acl_location, function(title) {sel_options.acl_location[content.acl_location] = title;});
662
		}
663
664
		var dialog_options = {
665
			callback: jQuery.proxy(function(_button_id, _value) {
666
				this.acl_dialog = null;
667
				if(_button_id != et2_dialog.OK_BUTTON) return;
668
669
				// Restore account if it's readonly in dialog
670
				if(!_value.acl_account) _value.acl_account = content.acl_account;
671
672
				// Handle no applications selected
673
				if(typeof _value.apps == 'undefined' && content.acl_location == 'run')
674
				{
675
					_value.apps = [];
676
				}
677
678
				// Only send the request if they entered everything (or selected no apps)
679
				if(_value.acl_account && (_value.acl_appname && _value.acl_location || typeof _value.apps != 'undefined'))
680
				{
681
					var id : any = [];
682
					if(_value.acl_appname && _value.acl_account && _value.acl_location)
683
					{
684
						id = _value.acl_appname+':'+_value.acl_account+':'+_value.acl_location;
685
						if(content && content.id && id != content.id)
686
						{
687
							// Changed the account or location, remove previous or we
688
							// get a new line instead of an edit
689
							this.egw.json(className+'::ajax_change_acl', [content.id, 0, [], this.et2._inst.etemplate_exec_id], null,this,false,this)
690
								.sendRequest();
691
						}
692
						id = [id];
693
					}
694
					var rights = 0;
695
					for(var i in _value.acl)
696
					{
697
						rights += parseInt(_value.acl[i]);
698
					}
699
					if(typeof _value.apps != 'undefined' && !_value.acl_appname)
700
					{
701
						rights = 1;
702
						var removed = [];
703
704
						// Loop through all apps, remove the ones with no permission
705
						for(var idx in sel_options.filter2)
706
						{
707
							var app = sel_options.filter2[idx].value || false;
708
							if (!app) continue;
709
							var run_id = app+":"+_value.acl_account+":run";
710
							if(_value.apps.indexOf(app) < 0 && (content.apps.indexOf(app) >= 0 || content.apps.length == 0))
711
							{
712
								removed.push(run_id);
713
							}
714
							else if (_value.apps.indexOf(app) >= 0 && content.apps.indexOf(app) < 0)
715
							{
716
								id.push(run_id);
717
							}
718
						}
719
720
						// Remove any removed
721
						if(removed.length > 0)
722
						{
723
							this.egw.json(className+'::ajax_change_acl', [removed, 0, [], this.et2._inst.etemplate_exec_id], callback ? callback : this._acl_callback,this,false,this)
724
								.sendRequest();
725
						}
726
					}
727
					this.egw.json(className+'::ajax_change_acl', [id, rights, _value, this.et2._inst.etemplate_exec_id], callback ? callback : this._acl_callback,this,false,this)
728
						.sendRequest();
729
				}
730
			},this),
731
			title: this.egw.lang('Access control'),
732
			buttons: et2_dialog.BUTTONS_OK_CANCEL,
733
			value: {
734
				content: content,
735
				sel_options: sel_options,
736
				modifications: modifications,
737
				readonlys: readonlys
738
			},
739
			template: egw.webserverUrl+'/admin/templates/default/acl.edit.xet'
740
		};
741
742
		// Handle policy documentation tab here
743
		if(this.egw.user('apps').policy)
744
		{
745
			dialog_options['width'] = 550;
746
			dialog_options['height'] = 450,
747
			modifications.tabs = {
748
				add_tabs:   true,
749
				tabs: [{
750
					label:    egw.lang('Documentation'),
751
					template: 'policy.admin_cmd',
752
					prepend:  false
753
				}]
754
			};
755
		}
756
757
		// Create the dialog
758
		this.acl_dialog = et2_createWidget("dialog", dialog_options, et2_dialog._create_parent(app));
759
	}
760
761
	/**
762
	 * Change handler for ACL edit dialog application selectbox.
763
	 * Re-creates the dialog with the current values
764
	 *
765
	 * @param input
766
	 * @param widget
767
	 */
768
	acl_reopen_dialog(input, widget)
769
	{
770
		var content = {};
771
		if(this.acl_dialog != null)
772
		{
773
			content = this.acl_dialog.get_value() || {};
774
775
			// Destroy the dialog
776
			this.acl_dialog.free();
777
			this.acl_dialog = null;
778
		}
779
		// Re-open the dialog
780
		this._acl_dialog(content);
781
	}
782
783
	/**
784
	 * Callback called on successfull call of serverside ACL handling
785
	 *
786
	 * @param {object} _data returned from server
787
	 */
788
	_acl_callback(_data)
789
	{
790
		// Avoid the window / framework / app and just refresh the etemplate
791
		// Framework will try to refresh the opener
792
		// Get by ID, since this.et2 isn't always the ACL list
793
		var et2 = etemplate2.getById('admin-acl').widgetContainer;
794
		et2.getInstanceManager().refresh(_data.msg, this.appname,_data.ids,_data.type);
795
	}
796
797
	/**
798
	 * Check to see if admin has taken away access to a category
799
	 *
800
	 * @@param {widget} button add/apply pressed button
801
	 */
802
	check_owner(button) {
803
		var select_owner = this.et2.getWidgetById('owner');
804
		var diff = [];
805
806
		if (typeof select_owner != 'undefined')
807
		{
808
			var owner = select_owner.get_value();
809
		}
810
811
		if(typeof owner != 'object')
812
		{
813
			owner = [owner];
814
		}
815
		// No owner probably means selectbox is read-only, so no need to check
816
		if(owner == null) return true;
817
818
		var all_users = owner.indexOf('0') >= 0;
819
820
		// If they checked all users, uncheck the others
821
		if(all_users) {
822
			select_owner.set_value(['0']);
823
			return true;
824
		}
825
826
		// Find out what changed
827
		var cat_original_owner = this.et2.getArrayMgr('content').getEntry('owner');
828
		if (cat_original_owner)
829
		{
830
			var selected_groups = select_owner.get_value().toString();
831
832
			for(var i =0;i < cat_original_owner.length;i++)
833
			{
834
				if (selected_groups.search(cat_original_owner[i]) < 0)
835
				{
836
					diff.push(cat_original_owner[i]);
837
				}
838
			}
839
840
			if (diff.length > 0)
841
			{
842
				var removed_cat_label = jQuery.map(select_owner.options.select_options, function (val, i)
843
				{
844
					for (var j=0; j <= diff.length;j++)
845
					{
846
						if (diff[j] == val.value)
847
						{
848
							return val.label;
849
						}
850
					}
851
				});
852
853
				// Somebody will lose permission, give warning.
854
				if(removed_cat_label)
855
				{
856
					var msg = this.egw.lang('Removing access for groups may cause problems for data in this category.  Are you sure?  Users in these groups may no longer have access:');
857
					return et2_dialog.confirm(button,msg + removed_cat_label.join(','));
858
				}
859
			}
860
		}
861
		return true;
862
	}
863
864
	/**
865
	 * Show icon based on icon-selectbox, hide placeholder (broken image), if no icon selected
866
	 *
867
	 * @param {widget} widget select box widget
868
	 */
869
	change_icon(widget)
870
	{
871
		var img = widget.getRoot().getWidgetById('icon_url');
872
873
		if (img)
874
		{
875
			img.set_src(widget.getValue());
876
		}
877
	}
878
879
880
	/**
881
	 * Add / edit an account
882
	 *
883
	 * @param {object} _action egwAction
884
	 * @param {array} _senders egwActionObject _senders[0].id holds account_id
885
	 */
886
	account(_action, _senders)
887
	{
888
		var params = jQuery.extend({}, this.egw.link_get_registry('addressbook', 'edit'));
889
		var popup = <string>this.egw.link_get_registry('addressbook', 'edit_popup');
890
891
		switch(_action.id)
892
		{
893
			case 'add':
894
				params.owner = '0';
895
				break;
896
			case 'copy':
897
				params.owner = '0';
898
				params.copy = true;
899
				// Fall through
900
			default:
901
				params.account_id = _senders[0].id.split('::').pop();	// get last :: separated part
902
				break;
903
		}
904
905
		this.egw.open_link(this.egw.link('/index.php', params), 'admin', popup);
906
	}
907
908
	/**
909
	 * Submit statistic
910
	 *
911
	 * Webkit browsers (Chrome, Safari, ...) do NOT allow to call form.submit() from within onclick of a submit button.
912
	 * Therefor we first store our own form action, replace it with egroupware.org submit url and set a timeout calling
913
	 * submit_statistic again with just the form, to do the second submit to our own webserver
914
	 *
915
	 * @param {DOM} form
916
	 * @param {string} submit_url
917
	 * @return {boolean}
918
	 */
919
	submit_statistic(form, submit_url)
920
	{
921
		var that = this;
922
		var submit = function()
923
		{
924
			// submit to egroupware.org
925
			var method=form.method;
926
			form.method='POST';
927
			var action = form.action;
928
			form.action=submit_url;
929
			var target = form.target;
930
			form.target='_blank';
931
			form.submit();
932
933
			// submit to own webserver
934
			form.method=method;
935
			form.action=action;
936
			form.target=target;
937
			that.et2.getInstanceManager().submit('submit');
938
		};
939
940
		// Safari does NOT allow to call form.submit() outside of onclick callback
941
		// so we have to use browsers ugly synchron confirm
942
		if (navigator.userAgent.match(/Safari/) && !navigator.userAgent.match(/Chrome/))
943
		{
944
			if (confirm(this.egw.lang('Submit displayed information?')))
945
			{
946
				submit();
947
			}
948
		}
949
		else
950
		{
951
			et2_dialog.show_dialog(function(_button)
952
			{
953
				if (_button == et2_dialog.YES_BUTTON)
954
				{
955
					submit();
956
				}
957
			}, this.egw.lang('Submit displayed information?'), '', {},
958
				et2_dialog.BUTTON_YES_NO, et2_dialog.QUESTION_MESSAGE, undefined, egw);
959
		}
960
		return false;
961
	}
962
963
	/**
964
	 * Change handler for when you change the type of a custom field.
965
	 * It toggles options / attributes as appropriate.
966
	 * @param {event object} e
967
	 * @param {widget object} widget
968
	 */
969
	cf_type_change(e,widget)
970
	{
971
		var root = widget.getRoot();
972
		var attributes = widget.getArrayMgr('content').getEntry('attributes['+widget.getValue()+']')||{};
973
		root.getWidgetById('cf_values').set_statustext(widget.egw().lang(widget.getArrayMgr('content').getEntry('options['+widget.getValue()+']')||''));
974
		jQuery(root.getWidgetById('cf_len').getDOMNode()).toggle(attributes.cf_len && true);
975
		jQuery(root.getWidgetById('cf_rows').getDOMNode()).toggle(attributes.cf_rows && true);
976
		jQuery(root.getWidgetById('cf_values').getParentDOMNode()).toggle(attributes.cf_values && true);
977
	}
978
979
	/**
980
	 * Change handler for when you delete a custom app type
981
	 * If Policy app is available, it asks for documentation
982
	 */
983
	cf_type_delete(e, widget)
984
	{
985
		var callback = function(button, value)
986
		{
987
			if(button === et2_dialog.YES_BUTTON)
988
			{
989
				var values = jQuery.extend(
990
					{},
991
					this.getInstanceManager().getValues(this.getRoot()),
992
					value,
993
					{appname: this.getRoot().getArrayMgr('content').getEntry('content_types[appname]')}
994
				);
995
				egw.json('admin.admin_customfields.ajax_delete_type', [values, this.getInstanceManager().etemplate_exec_id]).sendRequest();
996
997
				// Immediately remove the type
998
				var types = this.getRoot().getWidgetById('types');
999
				var options = types.options.select_options;
1000
				var key;
1001
				for(key in options)
1002
				{
1003
					if(options.hasOwnProperty(key) && key === types.getValue())
1004
					{
1005
						delete options[key];
1006
						break;
1007
					}
1008
				}
1009
				types.set_select_options(options);
1010
				this.egw().message('');
1011
1012
				// Trigger load of status for existing type
1013
				types.set_value(Object.keys(options)[0]);
1014
			}
1015
		}.bind(widget);
1016
1017
		if(egw.app('policy'))
1018
		{
1019
			egw.includeJS(egw.link('/policy/js/app.js'), function() {
1020
				if(typeof app.policy === 'undefined' || typeof app.policy.confirm === 'undefined')
1021
				{
1022
					app.policy = new app.classes.policy();
1023
				}
1024
1025
				var parent = et2_dialog._create_parent(widget.egw());
1026
				var dialog = et2_createWidget("dialog", {
1027
					callback: callback,
1028
					template: egw.link('/policy/templates/default/admin_cmd_narrow.xet'),
1029
					title: egw.lang('Delete'),
1030
					buttons: et2_dialog.BUTTONS_YES_NO,
1031
					value: {content:{}},
1032
					width: 'auto'
1033
				}, parent);
1034
				dialog.egw().message("Entries with a deleted type can cause problems.\nCheck for entries with this type before deleting.", 'warning');
1035
			});
1036
		}
1037
		else
1038
		{
1039
			callback(et2_dialog.YES_BUTTON);
1040
		}
1041
		return false;
1042
	}
1043
1044
	/**
1045
	 * Activate none standard SMTP mail accounts for selected users
1046
	 *
1047
	 * @param {egw_action} _action
1048
	 * @param {array} _selected selected users
1049
	 */
1050
	emailadminActiveAccounts(_action, _selected)
1051
	{
1052
		var menuaction = 'admin.admin_mail.ajax_activeAccounts';
1053
		var accounts = [];
1054
		var msg1 = egw.lang('%1 accounts being activated', ""+Object.keys(_selected).length);
1055
1056
		for (var i=0;i< Object.keys(_selected).length;i++)
1057
		{
1058
			accounts[i] = [{id:_selected[i]['id'].split('::')[1],qouta:"", domain:"", status:_action.id == 'active'?_action.id:''}, this.et2._inst.etemplate_exec_id];
1059
		}
1060
		var callbackDialog = function (btn){
1061
			if (btn === et2_dialog.YES_BUTTON)
1062
			{
1063
				// long task dialog for de/activation accounts
1064
				et2_dialog.long_task(function(_val, _resp){
1065
					if (_val && _resp.type !== 'error')
1066
					{
1067
						console.log(_val,_resp);
1068
					}
1069
					else
1070
					{
1071
1072
					}
1073
				}, msg1, 'Mail Acounts Activation', menuaction, accounts, 'admin');
1074
			}
1075
		};
1076
		// confirmation dialog
1077
		et2_dialog.show_dialog(callbackDialog, egw.lang('Are you sure you want to %1 mail for selected accounts?', egw.lang(_action.id)), egw.lang('Active Mail Accounts'), {},
1078
			et2_dialog.BUTTON_YES_NO, et2_dialog.WARNING_MESSAGE, undefined, egw);
1079
	}
1080
1081
	/**
1082
	 * No SSL
1083
	 */
1084
	SSL_NONE = 0;
1085
	/**
1086
	 * STARTTLS on regular tcp connection/port
1087
	 */
1088
	SSL_STARTTLS = 1;
1089
	/**
1090
	 * SSL (inferior to TLS!)
1091
	 */
1092
	SSL_SSL = 3;
1093
	/**
1094
	 * require TLS version 1+, no SSL version 2 or 3
1095
	 */
1096
	SSL_TLS = 2;
1097
	/**
1098
	 * if set, verify certifcate (currently not implemented in Horde_Imap_Client!)
1099
	 */
1100
	SSL_VERIFY = 8;
1101
1102
	/**
1103
	 * Resize window methode
1104
	 *
1105
	 * @returns {undefined}
1106
	 */
1107
	wizard_popup_resize()
1108
	{
1109
		var $main_div = jQuery('#popupMainDiv');
1110
		var $et2 = jQuery('.et2_container');
1111
		var w = {
1112
			width: egw_getWindowInnerWidth(),
1113
			height: egw_getWindowInnerHeight()
1114
		};
1115
		// Use et2_container for width since #popupMainDiv is full width, but we still need
1116
		// to take padding/margin into account
1117
		var delta_width = w.width - ($et2.outerWidth(true) + ($main_div.outerWidth(true) - $main_div.width()));
1118
		var delta_height = w.height - ($et2.outerHeight(true) + ($main_div.outerHeight(true) - $main_div.height()));
1119
		if(delta_width != 0 || delta_height != 0)
1120
		{
1121
			window.resizeTo(egw_getWindowOuterWidth() - delta_width,egw_getWindowOuterHeight() - delta_height);
1122
		}
1123
	}
1124
1125
	/**
1126
	 * Switch account wizard to manual entry
1127
	 */
1128
	wizard_manual()
1129
	{
1130
		jQuery('.emailadmin_manual').fadeToggle();// not sure how to to this et2-isch
1131
		this.wizard_popup_resize(); // popup needs to be resized after toggling
1132
	}
1133
1134
	/**
1135
	 * onclick for continue button to show progress animation
1136
	 *
1137
	 * @param {object} _event event-object or information about event
1138
	 * @param {et2_baseWidget} _widget widget causing the event
1139
	 */
1140
	wizard_detect(_event, _widget)
1141
	{
1142
		// we need to do a manual asynchronious submit to show progress animation
1143
		// default synchronious submit stops animation!
1144
		if (this.et2._inst.submit('button[continue]', true))	// true = async submit
1145
		{
1146
			var sieve_enabled = this.et2.getWidgetById('acc_sieve_enabled');
1147
			if (!sieve_enabled || sieve_enabled.get_value())
1148
			{
1149
				jQuery('#admin-mailwizard_output').hide();
1150
				jQuery('td.emailadmin_progress').show();
1151
			}
1152
		}
1153
		return false;
1154
	}
1155
1156
	/**
1157
	 * Set default port, if imap ssl-type changes
1158
	 *
1159
	 * @param {object} _event event-object or information about event
1160
	 * @param {et2_baseWidget} _widget widget causing the event
1161
	 */
1162
	wizard_imap_ssl_onchange(_event, _widget)
1163
	{
1164
		var ssl_type = _widget.get_value();
1165
		this.et2.getWidgetById('acc_imap_port').set_value(
1166
			ssl_type == this.SSL_SSL || ssl_type == this.SSL_TLS ? 993 : 143);
1167
	}
1168
1169
	/**
1170
	 * Set default port, if imap ssl-type changes
1171
	 *
1172
	 * @param {object} _event event-object or information about event
1173
	 * @param {et2_baseWidget} _widget widget causing the event
1174
	 */
1175
	wizard_smtp_ssl_onchange(_event, _widget)
1176
	{
1177
		var ssl_type = _widget.get_value();
1178
		this.et2.getWidgetById('acc_smtp_port').set_value(
1179
			ssl_type == 'no' ? 25 : (ssl_type == this.SSL_SSL || ssl_type == this.SSL_TLS ? 465 : 587));
1180
	}
1181
1182
	/**
1183
	 * Set default port, if imap ssl-type changes
1184
	 *
1185
	 * @param {object} _event event-object or information about event
1186
	 * @param {et2_baseWidget} _widget widget causing the event
1187
	 */
1188
	wizard_sieve_ssl_onchange(_event, _widget)
1189
	{
1190
		var ssl_type = _widget.get_value();
1191
		this.et2.getWidgetById('acc_sieve_port').set_value(
1192
			ssl_type == this.SSL_SSL || ssl_type == this.SSL_TLS ? 5190 : 4190);
1193
		this.wizard_sieve_onchange(_event, _widget);
1194
	}
1195
1196
	/**
1197
	 * Enable sieve, if user changes some setting
1198
	 *
1199
	 * @param {object} _event event-object or information about event
1200
	 * @param {et2_baseWidget} _widget widget causing the event
1201
	 */
1202
	wizard_sieve_onchange(_event, _widget)
1203
	{
1204
		this.et2.getWidgetById('acc_sieve_enabled').set_value(1);
1205
	}
1206
1207
	/**
1208
	 * Switch to select multiple accounts
1209
	 *
1210
	 * @param {object} _event event-object or information about event
1211
	 * @param {et2_baseWidget} _widget widget causing the event
1212
	 */
1213
	edit_multiple(_event, _widget)
1214
	{
1215
		// hide multiple button
1216
		_widget.set_disabled(true);
1217
1218
		// switch account-selection to multiple
1219
		var account_id = this.et2.getWidgetById('account_id');
1220
		account_id.set_multiple(true);
1221
	}
1222
1223
	/**
1224
	 * Hide not applying fields, used as:
1225
	 * - onchange handler on account_id
1226
	 * - called from et2_ready for emailadmin.account template
1227
	 *
1228
	 * @param {object} _event event-object or information about event
1229
	 * @param {et2_baseWidget} _widget widget causing the event
1230
	 */
1231
	account_hide_not_applying(_event?, _widget?)
1232
	{
1233
		var account_id = this.et2.getWidgetById('account_id');
1234
		var ids = account_id && account_id.get_value ? account_id.get_value() : [];
1235
		if (typeof ids == 'string') ids = ids.split(',');
1236
1237
		var multiple = ids.length >= 2 || ids[0] === '' || ids[0] < 0;
1238
		//alert('multiple='+(multiple?'true':'false')+': '+ids.join(','));
1239
1240
		// initial call
1241
		if (typeof _widget == 'undefined')
1242
		{
1243
			if (!multiple)
1244
			{
1245
				jQuery('.emailadmin_no_single').hide();
1246
			}
1247
			if (!this.egw.user('apps').admin)
1248
			{
1249
				jQuery('.emailadmin_no_user,#button\\[multiple\\]').hide();
1250
			}
1251
			if (ids.length == 1)
1252
			{
1253
				// switch back to single selectbox
1254
				account_id.set_multiple(false);
1255
				this.et2.getWidgetById('button[multiple]').set_disabled(false);
1256
			}
1257
		}
1258
		// switched to single user
1259
		else if (!multiple)
1260
		{
1261
			jQuery('.emailadmin_no_single').fadeOut();
1262
			// switch back to single selectbox
1263
			account_id.set_multiple(false);
1264
			this.et2.getWidgetById('button[multiple]').set_disabled(false);
1265
		}
1266
		// switched to multiple user
1267
		else
1268
		{
1269
			jQuery('.emailadmin_no_single').fadeIn();
1270
		}
1271
		if (_event && _event.stopPropagation) _event.stopPropagation();
1272
		return false;
1273
	}
1274
1275
	/**
1276
	 * Callback if user changed account selction
1277
	 *
1278
	 * @param {object} _event event-object or information about event
1279
	 * @param {et2_baseWidget} _widget widget causing the event
1280
	 */
1281
	change_account(_event, _widget)
1282
	{
1283
		// todo check dirty and query user to a) save changes, b) discard changes, c) cancel selection
1284
		_widget.getInstanceManager().submit();
1285
	}
1286
1287
	/**
1288
	 * Callback if user changes notification folders: unset use-default checkbox
1289
	 *
1290
	 * @param {object} _event
1291
	 * @param {et2_widget} _widget
1292
	 */
1293
	change_folders(_event, _widget)
1294
	{
1295
		var use_default = this.et2.getWidgetById('notify_use_default');
1296
		if (use_default) use_default.set_value(false);
1297
	}
1298
1299
	/**
1300
	 * default onExecute for admin actions
1301
	 *
1302
	 * @param {object} _action
1303
	 * @param {object} _senders
1304
	 */
1305
	account_edit_action(_action, _senders)
1306
	{
1307
		if (_action.data.url)
1308
		{
1309
			this.egw.open_link(_action.data.url, _action.data.target || '_blank', _action.data.popup);
1310
		}
1311
	}
1312
1313
	/**
1314
	 * Clear instance cache
1315
	 *
1316
	 * If there is an error on server-side, resend request with an parameter allowing
1317
	 * cache to use different method not requiring eg. so much memory
1318
	 */
1319
	clear_cache()
1320
	{
1321
		this.egw.message(this.egw.lang('Clear cache and register hooks')+"\n"+this.egw.lang('Please wait...'),'info');
1322
1323
		this.egw.json('admin.admin_hooks.ajax_clear_cache').sendRequest(true, undefined, jQuery.proxy(function(_xmlhttp, _err)
1324
		{
1325
			this.egw.json('admin.admin_hooks.ajax_clear_cache&errored=1').sendRequest(true);
1326
		}, this));
1327
	}
1328
1329
	/**
1330
	 * Export content of given field into relevant file
1331
	 */
1332
	smime_exportCert()
1333
	{
1334
		var $a = jQuery(document.createElement('a')).appendTo('body').hide();
1335
		var acc_id = this.et2.getArrayMgr("content").getEntry('acc_id');
1336
		var url = window.egw.webserverUrl+'/index.php?';
1337
			url += 'menuaction=mail.mail_ui.smimeExportCert';
1338
			url += '&acc_id='+acc_id;
1339
		$a.prop('href',url);
1340
		$a.prop('download',"");
1341
		$a[0].click();
1342
		$a.remove();
1343
	}
1344
1345
	/**
1346
	 * Create certificate generator dialog
1347
	 */
1348
	smime_genCertificate()
1349
	{
1350
		var self = this;
1351
		et2_createWidget("dialog",
1352
		{
1353
			callback(_button_id, _value)
1354
			{
1355
				if (_button_id == 'create' && _value)
1356
				{
1357
					var isValid = true;
1358
					var required = ['countryName', 'emailAddress'];
1359
					var widget;
1360
					// check the required fields
1361
					for (var i=0;i<required.length;i++)
1362
					{
1363
						if (_value[required[i]]) continue;
1364
						widget = this.template.widgetContainer.getWidgetById(required[i]);
1365
						widget.set_validation_error('This field is required!');
1366
						isValid = false;
1367
					}
1368
					// check mismatch passphrase
1369
					if (_value.passphrase && _value.passphrase !== _value.passphraseConf)
1370
					{
1371
						var passphraseConf = this.template.widgetContainer.getWidgetById('passphrase');
1372
						passphraseConf.set_validation_error('Confirm passphrase is not match!');
1373
						isValid = false;
1374
					}
1375
1376
					if (isValid)
1377
					{
1378
						egw.json('mail.mail_ui.ajax_smimeGenCertificate', _value, function(_cert){
1379
							if (_cert)
1380
							{
1381
								for (var key in _cert)
1382
								{
1383
									if (!_cert[key]) continue;
1384
									switch (key)
1385
									{
1386
										case 'cert':
1387
											self.et2.getWidgetById('smime_cert').set_value(_cert[key]);
1388
											break;
1389
										case 'privkey':
1390
											self.et2.getWidgetById('acc_smime_password').set_value(_cert[key]);
1391
											break;
1392
									}
1393
								}
1394
								self.egw.message('New certificate information has been generated, please save your account if you want to store it.');
1395
							}
1396
						}).sendRequest(true);
1397
					}
1398
					else
1399
					{
1400
						return false;
1401
					}
1402
				}
1403
			},
1404
			title: egw.lang('Generate Certificate'),
1405
			buttons: [
1406
				{text: this.egw.lang("Create"), id: "create", "class": "ui-priority-primary", "default": true},
1407
				{text: this.egw.lang("Cancel"), id:"cancel"}
1408
			],
1409
			value:{
1410
				content:{
1411
					value: ''
1412
			}},
1413
			template: egw.webserverUrl+'/mail/templates/default/smimeCertGen.xet?'+Date.now(),
1414
			resizable: false,
1415
			position: 'left top'
1416
		}, et2_dialog._create_parent('mail'));
1417
	}
1418
1419
	/**
1420
	 * Triggers upload for background image and updates its taglist
1421
	 *
1422
	 * @param {type} node
1423
	 * @param {type} widget
1424
	 */
1425
	login_background_update(node, widget)
1426
	{
1427
		var taglist = widget._parent._children[0];
1428
		egw.json('admin.admin_config.ajax_upload_anon_images',
1429
			[widget.get_value(), taglist.get_value()],
1430
			function(_data){
1431
				taglist.set_value(_data);
1432
		}).sendRequest();
1433
	}
1434
1435
	/**
1436
	 * Set content of selected row
1437
	 *
1438
	 * @param {array} node
1439
	 * @returns
1440
	 */
1441
	cmds_onselect(node)
1442
	{
1443
		var splitter = this.et2.getWidgetById('splitter');
1444
		var cmds_preview = this.et2.getWidgetById('cmds_preview');
1445
		if (node.length != 1)
1446
		{
1447
			splitter.dock();
1448
			return;
1449
		}
1450
1451
		if (splitter.isDocked())
1452
		{
1453
			splitter.undock();
1454
		}
1455
		var data = egw.dataGetUIDdata(node[0]);
1456
		var policy_preview = this.et2.getWidgetById('policy_preview');
1457
		var id = node[0].replace('admin::', '');
1458
1459
		if (app.policy)
1460
		{
1461
			cmds_preview.set_disabled(true);
1462
			policy_preview.set_src(egw.link('/index.php', {
1463
				menuaction:'policy.EGroupware\\Policy\\History.view',
1464
				'cmd_id':id,
1465
				'cmd_template': "policy.admin_cmd_history"
1466
			}));
1467
		}
1468
		else
1469
		{
1470
			policy_preview.set_disabled(true);
1471
			cmds_preview.set_value({content:[data.data]});
1472
		}
1473
	}
1474
}
1475
1476
app.classes.admin = AdminApp;
1477