From 4222d51e39e1a3ed0d6fdb689705da3dff78b506 Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Wed, 29 Oct 2014 14:37:13 +0000 Subject: [PATCH] * Add ability to rename current distribution list from context menu, as well as more consistent behaviour for adding lists --- addressbook/inc/class.addressbook_ui.inc.php | 58 +++++++++++++- addressbook/js/app.js | 79 ++++++++++++++++++-- 2 files changed, 129 insertions(+), 8 deletions(-) diff --git a/addressbook/inc/class.addressbook_ui.inc.php b/addressbook/inc/class.addressbook_ui.inc.php index aa9f6437d8..ec3bb80900 100644 --- a/addressbook/inc/class.addressbook_ui.inc.php +++ b/addressbook/inc/class.addressbook_ui.inc.php @@ -250,9 +250,10 @@ class addressbook_ui extends addressbook_bo } $sel_options['cat_id'] = array('' => lang('all'), '0' => lang('None')); - // Delete list action depends on permissions + // Edit and delete list actions depends on permissions if($this->get_lists(EGW_ACL_EDIT)) { + $content['nm']['placeholder_actions'][] = 'rename_list'; $content['nm']['placeholder_actions'][] = 'delete_list'; } @@ -482,6 +483,14 @@ class addressbook_ui extends addressbook_bo 'fieldId' => 'exec[nm][filter2]', 'fieldValue' => '!', // enable if list != '' ), + 'rename_list' => array( + 'caption' => 'Rename selected distribution list', + 'icon' => 'edit', + 'enabled' => 'javaScript:app.addressbook.nm_compare_field', + 'fieldId' => 'exec[nm][filter2]', + 'fieldValue' => '!', // enable if list != '' + 'onExecute' => 'javaScript:app.addressbook.rename_list' + ), 'delete_list' => array( 'caption' => 'Delete selected distribution list!', 'confirm' => 'Delete selected distribution list!', @@ -494,6 +503,7 @@ class addressbook_ui extends addressbook_bo if(is_subclass_of('etemplate', 'etemplate_new')) { $actions['lists']['children']['remove_from_list']['fieldId'] = 'filter2'; + $actions['lists']['children']['rename_list']['fieldId'] = 'filter2'; $actions['lists']['children']['delete_list']['fieldId'] = 'filter2'; } } @@ -879,6 +889,50 @@ window.egw_LAB.wait(function() { } + /** + * Create or rename an existing email list + * + * @param int $list_id ID of existing list, or 0 for a new one + * @param string $new_name List name + * @param int $owner List owner, or empty for current user + * @return boolean|string + */ + function ajax_set_list($list_id, $new_name, $owner = false) + { + // Set owner to current user, if not set + $owner = $owner ? $owner : $GLOBALS['egw_info']['user']['account_id']; + + // Check for valid list & permissions + if(!(int)$list_id && !$this->check_list(null,EGW_ACL_ADD|EGW_ACL_EDIT,$owner)) + { + egw_json_response::get()->apply('egw.message', array( lang('List creation failed, no rights!'),'error')); + return; + } + if ((int)$list_id && !$this->check_list((int)$list_id, EGW_ACL_EDIT, $owner)) + { + egw_json_response::get()->apply('egw.message', array( lang('Insufficent rights to edit this list!'),'error')); + return; + } + + $list = array('list_owner' => $owner); + + // Rename + if($list_id) + { + $list = $this->read_list((int)$list_id); + } + $list['list_name'] = $new_name; + + $new_id = $this->add_list(array('list_id' => (int)$list_id), $list['list_owner'],array(),$list); + + egw_json_response::get()->apply('egw.message', array( + $new_id == $list_id ? lang('Distribution list renamed') : lang('List created'), + 'success' + )); + // Success, just update selectbox to new value + egw_json_response::get()->data($new_id == $list_id ? "true" : $new_id); + } + /** * apply an action to multiple contacts * @@ -989,7 +1043,7 @@ window.egw_LAB.wait(function() { egw_session::appsession($session_name,'addressbook',$query); } return false; - + case 'document': if (!$document) $document = $this->prefs['default_document']; $document_merge = new addressbook_merge(); diff --git a/addressbook/js/app.js b/addressbook/js/app.js index b30dfa4b49..d1b36dec14 100644 --- a/addressbook/js/app.js +++ b/addressbook/js/app.js @@ -438,14 +438,81 @@ app.classes.addressbook = AppJS.extend( add_new_list: function(owner) { - var name = window.prompt(this.egw.lang('Name for the distribution list')); - if (name) + if(!owner || typeof owner == 'object') { - egw.open('','addressbook', 'list', { - 'add_list': name, - 'owner': owner - },'_self'); + var filter = this.et2.getWidgetById('filter'); + owner = filter.getValue()||egw.preference('add_default','addressbook'); } + var lists = this.et2.getWidgetById('filter2'); + et2_dialog.show_prompt( + function(button, name) { + if(button == et2_dialog.OK_BUTTON) + { + egw.json('addressbook.addressbook_ui.ajax_set_list',[0, name, owner], + function(result) + { + if(typeof result == 'object') return; // This response not for us + // Update list + if(result) + { + lists.options.select_options.unshift({value:result,label:name}); + lists.set_select_options(lists.options.select_options); + + // Set to new list so they can see it easily + lists.set_value(result); + } + } + ).sendRequest(true); + } + }, + this.egw.lang('Name for the distribution list'), + this.egw.lang('Add a new list...') + ); + }, + + /** + * Rename the current distribution list selected in the nextmatch filter2 + * + * Differences from add_new_list are in the dialog, parameters sent, and how the + * response is dealt with + * + * @param {egwAction} action Action selected in context menu (rename) + * @param {egwActionObject[]} selected The selected row(s). Not used for this. + */ + rename_list: function(action, selected) + { + var lists = this.et2.getWidgetById('filter2'); + var list = lists.getValue() || 0; + var value = null; + for(var i = 0; i < lists.options.select_options.length; i++) + { + if(lists.options.select_options[i].value == list) + { + value = lists.options.select_options[i]; + } + } + et2_dialog.show_prompt( + function(button, name) { + if(button == et2_dialog.OK_BUTTON) + { + egw.json('addressbook.addressbook_ui.ajax_set_list',[list, name], + function(result) + { + if(typeof result == 'object') return; // This response not for us + // Update list + if(result) + { + value.label = name; + lists.set_select_options(lists.options.select_options); + } + } + ).sendRequest(true); + } + }, + this.egw.lang('Name for the distribution list'), + this.egw.lang('Rename list'), + value.label + ); }, filter2_onchange: function()