invitations 1

This commit is contained in:
Yuri Kuznetsov
2014-03-26 17:38:14 +02:00
parent 6f76f7289d
commit 69f7373c8b
8 changed files with 175 additions and 39 deletions
+8 -3
View File
@@ -191,12 +191,17 @@ class Sender
$body = new MimeMessage;
$parts = array();
$bodyPart = new MimePart($email->get('body'));
if ($email->get('isHtml')) {
$bodyPart = new MimePart($email->get('body'));
$bodyPart->type = 'text/html';
} else {
} else {
if ($email->get('bodyPlain')) {
$bodyPart = new MimePart($email->get('bodyPlain'));
} else {
$bodyPart = new MimePart($email->get('body'));
}
$bodyPart->type = 'text/plain';
}
+4 -5
View File
@@ -18,7 +18,7 @@
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
************************************************************************/
namespace Espo\Entities;
@@ -27,11 +27,11 @@ class Preferences extends \Espo\Core\ORM\Entity
public function getSmtpParams()
{
$smtpParams = array();
$smtpParams = array();
$smtpParams['server'] = $this->get('smtpServer');
if ($smtpParams['server']) {
$smtpParams['port'] = $this->get('smtpPort');
$smtpParams['server'] = $this->get('smtpServer');
$smtpParams['server'] = $this->get('smtpServer');
$smtpParams['auth'] = $this->get('smtpAuth');
$smtpParams['security'] = $this->get('smtpSecurity');
$smtpParams['username'] = $this->get('smtpUsername');
@@ -40,7 +40,6 @@ class Preferences extends \Espo\Core\ORM\Entity
} else {
return false;
}
}
}
}
@@ -22,7 +22,30 @@
namespace Espo\Modules\Crm\Controllers;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\BadRequest;
use \Espo\Core\Exceptions\NotFound;
class Call extends \Espo\Core\Controllers\Record
{
public function actionSendInvitations($params, $data)
{
if (empty($data['id'])) {
throw new BadRequest();
}
$entity = $this->getRecordService()->getEntity($data['id']);
if (!$entity) {
throw new NotFound();
}
if (!$this->getAcl()->check($entity, 'edit')) {
throw new Forbidden();
}
return $this->getRecordService()->sendInvitations($entity);
}
}
@@ -22,7 +22,30 @@
namespace Espo\Modules\Crm\Controllers;
use \Espo\Core\Exceptions\Forbidden;
use \Espo\Core\Exceptions\BadRequest;
use \Espo\Core\Exceptions\NotFound;
class Meeting extends \Espo\Core\Controllers\Record
{
{
public function actionSendInvitations($params, $data)
{
if (empty($data['id'])) {
throw new BadRequest();
}
$entity = $this->getRecordService()->getEntity($data['id']);
if (!$entity) {
throw new NotFound();
}
if (!$this->getAcl()->check($entity, 'edit')) {
throw new Forbidden();
}
return $this->getRecordService()->sendInvitations($entity);
}
}
@@ -0,0 +1,5 @@
<p>Subject: {name}</p>
<p>Date & time: {dateStart}</p>
<p>
<a href="{{acceptLink}}">Accept</a>, <a href="{{declineLink}}">Decline</a>
</p>
@@ -0,0 +1 @@
Invitation to the {activityType} '{name}'
@@ -29,6 +29,86 @@ use \Espo\Core\Exceptions\Forbidden;
class Meeting extends \Espo\Services\Record
{
protected function init()
{
$this->dependencies[] = 'mailSender';
$this->dependencies[] = 'preferences';
$this->dependencies[] = 'i18n';
}
protected function getMailSender()
{
return $this->injections['mailSender'];
}
protected function getPreferences()
{
return $this->injections['preferences'];
}
protected function getI18n()
{
return $this->injections['i18n'];
}
protected function parseInvitationTemplate($contents, $entity, $invitee = null)
{
$contents = str_replace('{name}', $entity->get('name'), $contents);
$contents = str_replace('{activityType}', strtolower($this->getI18n()->translate($entity->getEntityName(), 'scopeNames')), $contents);
$contents = str_replace('{dateStart}', $entity->get('dateStart'), $contents);
return $contents;
}
protected function sendInvitation(Entity $entity, Entity $invitee)
{
$email = $this->getEntityManager()->getEntity('Email');
$email->set('to', $invitee->get('emailAddress'));
$subjectTplFileName = 'custom/Espo/Custom/Resources/templates/InvitationSubject.tpl';
if (!file_exists($subjectTplFileName)) {
$subjectTplFileName = 'application/Espo/Modules/Crm/Resources/templates/InvitationSubject.tpl';
}
$subjectTpl = file_get_contents($subjectTplFileName);
$bodyTplFileName = 'custom/Espo/Custom/Resources/templates/InvitationBody.tpl';
if (!file_exists($bodyTplFileName)) {
$bodyTplFileName = 'application/Espo/Modules/Crm/Resources/templates/InvitationBody.tpl';
}
$bodyTpl = file_get_contents($bodyTplFileName);
$subject = $this->parseInvitationTemplate($subjectTpl, $entity, $invitee);
$body = $this->parseInvitationTemplate($bodyTpl, $entity, $invitee);
$email->set('subject', $subject);
$email->set('body', $body);
$email->set('isHtml', true);
$emailSender = $this->getMailSender();
$emailSender->send($email);
}
public function sendInvitations(Entity $entity)
{
$users = $entity->get('users');
foreach ($users as $user) {
$this->sendInvitation($entity, $user);
}
$contacts = $entity->get('contacts');
foreach ($contacts as $contact) {
$this->sendInvitation($entity, $contact);
}
$leads = $entity->get('leads');
foreach ($leads as $lead) {
$this->sendInvitation($entity, $lead);
}
return true;
}
protected function storeEntity(Entity $entity)
{
$assignedUserId = $entity->get('assignedUserId');
+30 -30
View File
@@ -18,7 +18,7 @@
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
************************************************************************/
************************************************************************/
namespace Espo\Services;
@@ -28,90 +28,90 @@ use \Espo\Core\Exceptions\Error;
class Email extends Record
{
protected function init()
{
$this->dependencies[] = 'mailSender';
$this->dependencies[] = 'preferences';
}
protected function getMailSender()
{
return $this->injections['mailSender'];
}
protected function getPreferences()
{
return $this->injections['preferences'];
}
}
public function createEntity($data)
{
$entity = parent::createEntity($data);
if ($entity && $entity->get('status') == 'Sending') {
$emailSender = $this->getMailSender();
if (strtolower($this->getUser()->get('emailAddress')) == strtolower($entity->get('from'))) {
$smtpParams = $this->getPreferences()->getSmtpParams();
$smtpParams = $this->getPreferences()->getSmtpParams();
if ($smtpParams) {
$smtpParams['fromName'] = $this->getUser()->get('name');
$smtpParams['fromName'] = $this->getUser()->get('name');
$emailSender->useSmtp($smtpParams);
}
} else {
if (!$this->getConfig()->get('outboundEmailIsShared')) {
throw new Error('Can not use system smtp. outboundEmailIsShared is false.');
throw new Error('Can not use system smtp. outboundEmailIsShared is false.');
}
}
$emailSender->send($entity);
}
$emailSender->send($entity);
$this->getEntityManager()->saveEntity($entity);
}
return $entity;
}
public function getEntity($id = null)
{
$entity = parent::getEntity($id);
if (!empty($id)) {
if ($entity->get('fromEmailAddressName')) {
$entity->set('from', $entity->get('fromEmailAddressName'));
}
$entity->loadLinkMultipleField('toEmailAddresses');
$entity->loadLinkMultipleField('ccEmailAddresses');
$entity->loadLinkMultipleField('bccEmailAddresses');
$names = $entity->get('toEmailAddressesNames');
$names = $entity->get('toEmailAddressesNames');
if (!empty($names)) {
$arr = array();
foreach ($names as $id => $address) {
$arr[] = $address;
}
$entity->set('to', implode('; ', $arr));
$entity->set('to', implode('; ', $arr));
}
$names = $entity->get('ccEmailAddressesNames');
$names = $entity->get('ccEmailAddressesNames');
if (!empty($names)) {
$arr = array();
foreach ($names as $id => $address) {
$arr[] = $address;
}
$entity->set('cc', implode('; ', $arr));
$entity->set('cc', implode('; ', $arr));
}
$names = $entity->get('bccEmailAddressesNames');
$names = $entity->get('bccEmailAddressesNames');
if (!empty($names)) {
$arr = array();
foreach ($names as $id => $address) {
$arr[] = $address;
}
$entity->set('bcc', implode('; ', $arr));
}
$entity->set('bcc', implode('; ', $arr));
}
}
return $entity;
}