From 09e9b47c9d23ef6820ad55a2f4f3f9cab4ac04d1 Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Mon, 12 Jun 2017 15:25:21 +0200 Subject: [PATCH] Make spam actions available on multiple entries --- mail/inc/class.mail_ui.inc.php | 83 ++++++++++++++++++++++------------ mail/js/app.js | 49 +++++++++++++------- 2 files changed, 85 insertions(+), 47 deletions(-) diff --git a/mail/inc/class.mail_ui.inc.php b/mail/inc/class.mail_ui.inc.php index db3bb51abf..25c52bdbd3 100644 --- a/mail/inc/class.mail_ui.inc.php +++ b/mail/inc/class.mail_ui.inc.php @@ -881,19 +881,23 @@ class mail_ui * Method to execute spam actions * * @param type $_action action id - * @param type $_params + * @param type $_items */ - public function ajax_spamAction($_action, $_params) + public function ajax_spamAction($_action, $_items) { $msg = array(); $refresh = false; $response = Api\Json\Response::get(); - - $id_parts = self::splitRowID($_params['row_id']); - if ($id_parts['profileID'] && $id_parts['profileID'] != $this->mail_bo->profileID) + // Check active profile and change it if it's neccessary + if (is_array($_items[0])) { - $this->changeProfile($id_parts['profileID']); + $id_parts = self::splitRowID($_items[0]['row_id']); + if ($id_parts['profileID'] && $id_parts['profileID'] != $this->mail_bo->profileID) + { + $this->changeProfile($id_parts['profileID']); + } } + $delimiter = $this->mail_bo->getHierarchyDelimiter(); // Ham folder $ham = $this->mail_bo->profileID.self::$delimiter.$this->mail_bo->icServer->acc_folder_ham; @@ -901,49 +905,60 @@ class mail_ui $junk = $this->mail_bo->profileID.self::$delimiter.$this->mail_bo->getJunkFolder(); // Inbox folder $inbox = $this->mail_bo->profileID.self::$delimiter.'INBOX'; - // Current Mailbox - $mailbox = $id_parts['folder']; - if ($GLOBALS['egw_info']['apps']['stylite']) + $messages = array(); + + foreach ($_items as &$params) { - $_params['mailbody'] = $this->get_load_email_data($_params['uid'], null, $mailbox); - $msg[] = stylite_mail_spamtitan::execAction($_action, $_params, array( - 'userpwd' => $this->mail_bo->icServer->acc_imap_password, - 'user' => $this->mail_bo->icServer->acc_imap_username, - 'api_url' => $this->mail_bo->icServer->acc_spam_api - )); + $id_parts = self::splitRowID($params['row_id']); + // Current Mailbox + $mailbox = $id_parts['folder']; + $messages[] = $params['row_id']; + if ($GLOBALS['egw_info']['apps']['stylite']) + { + $params['mailbody'] = $this->get_load_email_data($params['uid'], null, $mailbox); + } } switch ($_action) { case 'spam': - $this->ajax_copyMessages($junk, array( + $msg[] = $this->ajax_copyMessages($junk, array( 'all' => false, - 'msg' => array($_params['row_id']) - ), 'move'); + 'msg' => $messages + ), 'move', null, true); $refresh = true; break; case 'ham': if (isset($this->mail_bo->icServer->acc_folder_ham) && !isset($this->mail_bo->icServer->acc_spam_api)) { - $this->ajax_copyMessages($ham, array( + $msg[] = $this->ajax_copyMessages($ham, array( 'all' => false, - 'msg' => array($_params['row_id']) - ), 'copy'); + 'msg' => $messages + ), 'copy', null, true); } // Move mails to Inbox if they are in Junk folder if ($junk == $this->mail_bo->profileID.self::$delimiter.$mailbox) { - $this->ajax_copyMessages($inbox, array( + $msg[] = $this->ajax_copyMessages($inbox, array( 'all' => false, - 'msg' => array($_params['row_id']) - ), 'move'); + 'msg' => $messages + ), 'move', null, true); $refresh = true; } break; } + if ($GLOBALS['egw_info']['apps']['stylite']) + { + stylite_mail_spamtitan::setActionItems($_action, $_items, array( + 'userpwd' => $this->mail_bo->icServer->acc_imap_password, + 'user' => $this->mail_bo->icServer->acc_imap_username, + 'api_url' => $this->mail_bo->icServer->acc_spam_api + )); + } + if ($refresh) { - $response->apply('egw.refresh',[implode('\n',$msg),'mail',$_params['row_id'],'delete']); + $response->data([implode('\n',$msg),$messages]); } else { @@ -962,20 +977,21 @@ class mail_ui 'spamfilter' => array ( 'caption' => 'Spam', 'icon' => 'dhtmlxtree/MailFolderJunk', + 'allowOnMultiple' => true, 'children' => array ( 'spam' => array ( 'caption' => 'Report as Spam', 'icon' => 'dhtmlxtree/MailFolderJunk', 'onExecute' => 'javaScript:app.mail.spam_actions', 'hint' => 'Report this email content as Spam - spam solutions like spamTitan will learn', - 'allowOnMultiple' => false + 'allowOnMultiple' => true ), 'ham' => array ( 'caption' => 'Report as Ham', 'icon' => 'dhtmlxtree/MailFolderHam', 'onExecute' => 'javaScript:app.mail.spam_actions', 'hint' => 'Report this email content as Ham (not spam) - spam solutions like spamTitan will learn', - 'allowOnMultiple' => false + 'allowOnMultiple' => true ) ) ) @@ -5122,10 +5138,12 @@ $filter['before']= date("d-M-Y", $cutoffdate2); * @param array _messageList list of UID's * @param string _copyOrMove method to use copy or move allowed * @param string _move2ArchiveMarker marker to indicate if a move 2 archive was triggered + * @param boolean _return if true the function will return the result instead of + * responding to client * * @return xajax response */ - function ajax_copyMessages($_folderName, $_messageList, $_copyOrMove='copy', $_move2ArchiveMarker='_') + function ajax_copyMessages($_folderName, $_messageList, $_copyOrMove='copy', $_move2ArchiveMarker='_', $_return = false) { if(Mail::$debug) error_log(__METHOD__."->".$_folderName.':'.print_r($_messageList,true).' Method:'.$_copyOrMove.' ArchiveMarker:'.$_move2ArchiveMarker); Api\Translation::add_app('mail'); @@ -5292,17 +5310,22 @@ $filter['before']= date("d-M-Y", $cutoffdate2); unset($lastFoldersUsedForMoveCont[$targetProfileID][$targetFolder]); $changeFolderActions = true; } + if ($_return) return $error; $response->call('egw.message',$error,"error"); } else { if ($_copyOrMove=='copy') { - $response->call('egw.message',lang('copied %1 message(s) from %2 to %3',($messageList=='all'||$_messageList['all']?($filtered?lang('all filtered'):lang('all')):count($messageList)),$folder,$targetFolder)); + $msg = lang('copied %1 message(s) from %2 to %3',($messageList=='all'||$_messageList['all']?($filtered?lang('all filtered'):lang('all')):count($messageList)),$folder,$targetFolder); + if ($_return) return $msg; + $response->call('egw.message',$msg); } else { - $response->call('egw.refresh',lang('moved %1 message(s) from %2 to %3',($messageList=='all'||$_messageList['all']?($filtered?lang('all filtered'):lang('all')):count($messageList)),$folder,$targetFolder),'mail',$messageListForRefresh,'delete'); + $msg = lang('moved %1 message(s) from %2 to %3',($messageList=='all'||$_messageList['all']?($filtered?lang('all filtered'):lang('all')):count($messageList)),$folder,$targetFolder); + if ($_return) return $msg; + $response->call('egw.refresh',$msg,'mail',$messageListForRefresh,'delete'); } } if ($changeFolderActions == true) diff --git a/mail/js/app.js b/mail/js/app.js index 62e7768d28..7f8756bdeb 100644 --- a/mail/js/app.js +++ b/mail/js/app.js @@ -5484,31 +5484,46 @@ app.classes.mail = AppJS.extend( * @param {object} _action egw action * @param {object} _sender nm row */ - spam_actions: function (_action, _sender) + spam_actions: function (_action, _senders) { - var id = ''; - if (_sender.length == 0) + var id,fromaddress,domain, email = ''; + var data = {}; + var items = []; + + // called action for a single row from toolbar + if (_senders.length == 0) { var nm = this.et2.getWidgetById(this.nm_index); - id = nm.getSelection().ids[0]; + _senders[0]['id'] = nm.getSelection().ids[0]; } - else + + for (var i in _senders) { - id = _sender[0].id; - } - var data = egw.dataGetUIDdata(id); - var fromaddress = data.data.fromaddress.match(/<([^\'\" <>]+)>$/); - var email = (fromaddress && fromaddress[1])?fromaddress[1]:data.data.fromaddress; - var domain = '@'+email.split('@')[1]; - this.egw.json('mail.mail_ui.ajax_spamAction', [ - _action.id, - { + id = _senders[i].id; + data = egw.dataGetUIDdata(id); + fromaddress = data.data.fromaddress.match(/<([^\'\" <>]+)>$/); + email = (fromaddress && fromaddress[1])?fromaddress[1]:data.data.fromaddress; + domain = '@'+email.split('@')[1]; + items[i] = { 'acc_id':id.split('::')[2], 'row_id':data.data.row_id, 'uid': data.data.uid, 'sender': _action.id.match(/domain/)? domain : email + }; + } + + this.egw.json('mail.mail_ui.ajax_spamAction', [ + _action.id,items + ], function(_data){ + if (_data[1] && _data[1].length > 0) + { + egw.refresh(_data[0],'mail',_data[1],'delete'); } - ]).sendRequest(true); + else + { + egw.message(_data[0]); + } + }).sendRequest(true); }, spamTitan_setActionTitle: function (_action, _sender) @@ -5516,8 +5531,8 @@ app.classes.mail = AppJS.extend( var id = _sender[0].id; var data = egw.dataGetUIDdata(id); var fromaddress = data.data.fromaddress.match(/<([^\'\" <>]+)>$/); - var email = (fromaddress && fromaddress[1]) ?fromaddress[1]:data.data.fromaddress; - var domain = email.split('@')[1]; + var email = _sender.length>1? this.egw.lang('emails'):(fromaddress && fromaddress[1]) ?fromaddress[1]:data.data.fromaddress; + var domain = _sender.length>1? this.egw.lang('domains'):email.split('@')[1]; switch (_action.id) { case 'whitelist_email_add':