diff --git a/application/Espo/Modules/Crm/Controllers/InboundEmail.php b/application/Espo/Modules/Crm/Controllers/InboundEmail.php index 7a9f47672e..08872d2868 100644 --- a/application/Espo/Modules/Crm/Controllers/InboundEmail.php +++ b/application/Espo/Modules/Crm/Controllers/InboundEmail.php @@ -24,17 +24,23 @@ namespace Espo\Modules\Crm\Controllers; class InboundEmail extends \Espo\Core\Controllers\Record { - - public function actionFetch($params, $data, $request) + protected function checkGlobalAccess() { - $id = $request->get('id'); - try { - $this->getRecordService()->fetchFromMailServer($id); - } catch (\Exception $e) { - echo $e->getMessage(); + if (!$this->getUser()->isAdmin()) { + throw new Forbidden(); } - echo "--"; - die; + } + + public function actionGetFolders($params, $data, $request) + { + return $this->getRecordService()->getFolders(array( + 'host' => $request->get('host'), + 'port' => $request->get('port'), + 'ssl' => $request->get('ssl'), + 'username' => $request->get('username'), + 'password' => $request->get('password'), + )); + } } diff --git a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/InboundEmail.json b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/InboundEmail.json index 7eac387d78..c84aefc84c 100644 --- a/application/Espo/Modules/Crm/Resources/metadata/entityDefs/InboundEmail.json +++ b/application/Espo/Modules/Crm/Resources/metadata/entityDefs/InboundEmail.json @@ -30,12 +30,13 @@ "monitoredFolders": { "type": "varchar", "required": true, - "default": "INBOX" + "default": "INBOX", + "view": "Crm:InboundEmail.Fields.Folders" }, "trashFolder": { "type": "varchar", "required": true, - "default": "INBOX.Trash" + "view": "Crm:InboundEmail.Fields.Folder" }, "assignToUser": { "type": "link" diff --git a/application/Espo/Modules/Crm/Services/InboundEmail.php b/application/Espo/Modules/Crm/Services/InboundEmail.php index 4c05e360fa..79395c83c7 100644 --- a/application/Espo/Modules/Crm/Services/InboundEmail.php +++ b/application/Espo/Modules/Crm/Services/InboundEmail.php @@ -46,7 +46,7 @@ class InboundEmail extends \Espo\Services\Record protected function findFolder($storage, $path) { - $arr = explode('.', $path); + $arr = explode('/', $path); $pointer = $storage->getFolders(); foreach ($arr as $folderName) { $pointer = $pointer->$folderName; @@ -54,6 +54,31 @@ class InboundEmail extends \Espo\Services\Record return $pointer; } + public function getFolders($params) + { + + $imapParams = array( + 'host' => $params['host'], + 'port' => $params['port'], + 'user' => $params['username'], + 'password' => $params['password'], + ); + + if (!empty($params['ssl'])) { + $imapParams['ssl'] = 'SSL'; + } + + $foldersArr = array(); + + $storage = new \Zend\Mail\Storage\Imap($imapParams); + + $folders = new \RecursiveIteratorIterator($storage->getFolders(), \RecursiveIteratorIterator::SELF_FIRST); + foreach ($folders as $name => $folder) { + $foldersArr[] = $folder->getGlobalName(); + } + return $foldersArr; + } + public function fetchFromMailServer($id) { $inboundEmail = $this->getEntity($id); @@ -75,10 +100,6 @@ class InboundEmail extends \Espo\Services\Record $storage = new \Zend\Mail\Storage\Imap($imapParams); - if (empty($storage)) { - throw new Error("Could not connect to IMAP of Inbound Email {$inboundEmail->id}."); - } - $trash = null; $trashFolder = $inboundEmail->get('trashFolder'); if (empty($trashFolder)) { diff --git a/frontend/client/modules/crm/res/templates/inbound-email/fields/folder/edit.tpl b/frontend/client/modules/crm/res/templates/inbound-email/fields/folder/edit.tpl new file mode 100644 index 0000000000..bb60aadcb5 --- /dev/null +++ b/frontend/client/modules/crm/res/templates/inbound-email/fields/folder/edit.tpl @@ -0,0 +1,9 @@ +
+ + + + +
+ + + diff --git a/frontend/client/modules/crm/res/templates/inbound-email/modals/select-folder.tpl b/frontend/client/modules/crm/res/templates/inbound-email/modals/select-folder.tpl new file mode 100644 index 0000000000..9c08cb0bf6 --- /dev/null +++ b/frontend/client/modules/crm/res/templates/inbound-email/modals/select-folder.tpl @@ -0,0 +1,11 @@ +{{#unless folders}} + {{translate 'No Data'}} +{{/unless}} + diff --git a/frontend/client/modules/crm/src/views/inbound-email/fields/folder.js b/frontend/client/modules/crm/src/views/inbound-email/fields/folder.js new file mode 100644 index 0000000000..3087b4716e --- /dev/null +++ b/frontend/client/modules/crm/src/views/inbound-email/fields/folder.js @@ -0,0 +1,67 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + ************************************************************************/ + +Espo.define('Crm:Views.InboundEmail.Fields.Folder', 'Views.Fields.Base', function (Dep) { + + return Dep.extend({ + + editTemplate: 'crm:inbound-email.fields.folder.edit', + + events: { + 'click [data-action="selectFolder"]': function () { + var self = this; + + this.notify('Please wait...'); + + $.ajax({ + type: 'GET', + url: 'InboundEmail/action/getFolders', + data: { + host: this.model.get('host'), + port: this.model.get('port'), + ssl: this.model.get('ssl'), + username: this.model.get('username'), + password: this.model.get('password'), + }, + error: function () { + Espo.Ui.error(self.translate('couldNotConnectToImap', 'messages', 'InboundEmail')); + }, + }).done(function (folders) { + this.createView('modal', 'Crm:InboundEmail.Modals.SelectFolder', { + folders: folders + }, function (view) { + self.notify(false); + view.render(); + + self.listenToOnce(view, 'select', function (folder) { + view.close(); + self.addFolder(folder); + }); + }); + }.bind(this)); + } + }, + + addFolder: function (folder) { + this.$element.val(folder); + }, + }); +}); diff --git a/frontend/client/modules/crm/src/views/inbound-email/fields/folders.js b/frontend/client/modules/crm/src/views/inbound-email/fields/folders.js new file mode 100644 index 0000000000..8e9de5b89d --- /dev/null +++ b/frontend/client/modules/crm/src/views/inbound-email/fields/folders.js @@ -0,0 +1,40 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + ************************************************************************/ + +Espo.define('Crm:Views.InboundEmail.Fields.Folders', 'Crm:Views.InboundEmail.Fields.Folder', function (Dep) { + + return Dep.extend({ + + addFolder: function (folder) { + var value = this.$element.val(); + + var folders = []; + if (value != '') { + folders = value.split(','); + } + + if (!~folders.indexOf(folder)) { + folders.push(folder); + } + this.$element.val(folders.join(',')); + }, + }); +}); diff --git a/frontend/client/modules/crm/src/views/inbound-email/modals/select-folder.js b/frontend/client/modules/crm/src/views/inbound-email/modals/select-folder.js new file mode 100644 index 0000000000..95a1c9eac7 --- /dev/null +++ b/frontend/client/modules/crm/src/views/inbound-email/modals/select-folder.js @@ -0,0 +1,59 @@ +/************************************************************************ + * This file is part of EspoCRM. + * + * EspoCRM - Open Source CRM application. + * Copyright (C) 2014 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko + * Website: http://www.espocrm.com + * + * EspoCRM is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * EspoCRM is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with EspoCRM. If not, see http://www.gnu.org/licenses/. + ************************************************************************/ + +Espo.define('Crm:Views.InboundEmail.Modals.SelectFolder', 'Views.Modal', function (Dep) { + + return Dep.extend({ + + cssName: 'select-folder-modal', + + template: 'crm:inbound-email.modals.select-folder', + + data: function () { + return { + folders: this.options.folders, + }; + }, + + events: { + 'click button[data-action="select"]': function (e) { + var value = $(e.currentTarget).data('value'); + this.trigger('select', value); + }, + }, + + setup: function () { + + this.buttons = [ + { + name: 'cancel', + label: 'Cancel', + onClick: function (dialog) { + dialog.close(); + } + } + ]; + + }, + + }); +}); +