Files
luos/workflow/engine/src/ProcessMaker/BusinessModel/EmailEvent.php

582 lines
19 KiB
PHP
Raw Normal View History

2015-06-18 18:12:35 -04:00
<?php
2018-03-05 16:13:12 -04:00
2015-06-18 18:12:35 -04:00
namespace ProcessMaker\BusinessModel;
2018-03-05 16:13:12 -04:00
use BasePeer;
use Bootstrap;
use Criteria;
use EmailEvent as ModelEmailEvent;
use EmailEventPeer;
use EmailServerPeer;
use Exception;
use G;
2018-03-05 16:13:12 -04:00
use ProcessMaker\Util\Common;
use Propel;
use ResultSet;
use UsersPeer;
2015-06-18 18:12:35 -04:00
class EmailEvent
{
/**
* Get the email accounts of the current workspace
*
2018-03-05 16:13:12 -04:00
* @return array
* @throws Exception
2015-06-18 18:12:35 -04:00
*/
public function getEmailEventAccounts()
{
try {
2018-03-05 16:13:12 -04:00
$criteria = new Criteria('workflow');
2015-06-18 18:12:35 -04:00
$criteria->clearSelectColumns();
2018-03-05 16:13:12 -04:00
$criteria->addSelectColumn(UsersPeer::USR_UID);
$criteria->addSelectColumn(UsersPeer::USR_EMAIL);
$criteria->addAsColumn('UID', 'USR_UID');
$criteria->addAsColumn('EMAIL', 'USR_EMAIL');
2018-03-05 16:13:12 -04:00
$criteria->add(UsersPeer::USR_STATUS, 'ACTIVE');
$result = UsersPeer::doSelectRS($criteria);
$result->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$accounts = [];
while ($result->next()) {
$row = $result->getRow();
if (!empty($row['USR_EMAIL'])) {
$accounts[] = array_change_key_case($row, CASE_LOWER);
}
2015-06-18 18:12:35 -04:00
}
2018-03-05 16:13:12 -04:00
return $accounts;
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
throw $e;
}
}
/**
* Get the email server accounts of the current workspace
*
2018-03-05 16:13:12 -04:00
* @return array
* @throws Exception
*/
public function getEmailEventServerAccounts()
{
try {
2018-03-05 16:13:12 -04:00
$criteria = new Criteria('workflow');
$criteria->clearSelectColumns();
2018-03-05 16:13:12 -04:00
$criteria->addSelectColumn(EmailServerPeer::MESS_UID);
$criteria->addSelectColumn(EmailServerPeer::MESS_FROM_MAIL);
2018-06-26 13:09:00 -04:00
$criteria->addSelectColumn(EmailServerPeer::MESS_FROM_NAME);
2018-03-05 16:13:12 -04:00
$criteria->addSelectColumn(EmailServerPeer::MESS_ACCOUNT);
$criteria->addSelectColumn(EmailServerPeer::MESS_ENGINE);
$criteria->addAsColumn('UID', 'MESS_UID');
2018-03-05 16:13:12 -04:00
$result = EmailServerPeer::doSelectRS($criteria);
$result->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$accounts = [];
while ($result->next()) {
$row = $result->getRow();
if (!empty($row['MESS_UID'])) {
$row['EMAIL'] = $row['MESS_ACCOUNT'];
$accounts[] = array_change_key_case($row, CASE_LOWER);
}
}
2018-03-05 16:13:12 -04:00
return $accounts;
} catch (Exception $e) {
throw $e;
}
}
2015-06-18 18:12:35 -04:00
/**
* Get the Email-Event data
2018-03-05 16:13:12 -04:00
*
* @var string $evn_uid . uid for activity
* @var string $pro_uid . uid for process
*
* @return array
* @throws Exception
2015-06-18 18:12:35 -04:00
*/
public function getEmailEventData($pro_uid, $evn_uid)
2015-06-18 18:12:35 -04:00
{
try {
//Get data
$criteria = $this->getEmailEventCriteria();
2018-03-05 16:13:12 -04:00
$criteria->add(EmailEventPeer::EVN_UID, $evn_uid, Criteria::EQUAL);
$criteria->add(EmailEventPeer::PRJ_UID, $pro_uid, Criteria::EQUAL);
$rsCriteria = EmailEventPeer::doSelectRS($criteria);
$rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$rsCriteria->next();
$row = $rsCriteria->getRow();
2018-04-17 14:52:21 -04:00
$emailServer = new EmailServer();
2018-03-05 16:13:12 -04:00
if ($row) {
2018-06-26 13:09:00 -04:00
// We need to initialize these values in empty, in order to return always the same structure
$row['MESS_ENGINE'] = $row['MESS_ACCOUNT'] = $row['MESS_FROM_MAIL'] = '';
2018-06-01 14:59:45 -04:00
if (!empty($row['EMAIL_SERVER_UID'])) {
$emailServerData = $emailServer->getEmailServer($row['EMAIL_SERVER_UID'], true);
2018-06-26 13:09:00 -04:00
$row['MESS_ENGINE'] = $emailServerData['MESS_ENGINE'];
$row['MESS_ACCOUNT'] = $emailServerData['MESS_ACCOUNT'];
$row['MESS_FROM_MAIL'] = $emailServerData['MESS_FROM_MAIL'];
2018-06-01 14:59:45 -04:00
}
$row = array_change_key_case($row, CASE_LOWER);
}
return $row;
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
throw $e;
}
}
/**
* Get the Email-Event data
2018-03-05 16:13:12 -04:00
*
* @var string $emailEventUid . uid for email event
* @var string $pro_uid . uid for process
*
* @return array
* @throws Exception
*/
public function getEmailEventDataByUid($pro_uid, $emailEventUid)
{
try {
//Get data
$criteria = $this->getEmailEventCriteria();
2018-03-05 16:13:12 -04:00
$criteria->add(EmailEventPeer::EMAIL_EVENT_UID, $emailEventUid, Criteria::EQUAL);
$criteria->add(EmailEventPeer::PRJ_UID, $pro_uid, Criteria::EQUAL);
$rsCriteria = EmailEventPeer::doSelectRS($criteria);
$rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC);
2015-06-18 18:12:35 -04:00
$rsCriteria->next();
$row = $rsCriteria->getRow();
2018-03-05 16:13:12 -04:00
if ($row) {
2015-06-18 18:12:35 -04:00
$row = array_change_key_case($row, CASE_LOWER);
}
return $row;
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
throw $e;
}
}
2015-06-18 18:12:35 -04:00
/**
* Save Data for Email-Event
*
2018-03-05 16:13:12 -04:00
* @var string $prj_uid . Uid for Process
* @var string $arrayData . Data for Trigger
*
* @return array
* @throws Exception
2015-06-18 18:12:35 -04:00
*/
2018-03-05 16:13:12 -04:00
public function save($prj_uid = '', $arrayData = [])
2015-06-18 18:12:35 -04:00
{
try {
//Verify data
2018-03-05 16:13:12 -04:00
$process = new Process();
$validator = new Validator();
2015-06-18 18:12:35 -04:00
$validator->throwExceptionIfDataIsNotArray($arrayData, "\$arrayData");
$validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData");
//Set data
$arrayData = array_change_key_case($arrayData, CASE_UPPER);
//Verify data
2018-03-05 16:13:12 -04:00
$process->throwExceptionIfNotExistsProcess($prj_uid, 'projectUid');
2015-06-18 18:12:35 -04:00
//Create
2018-03-05 16:13:12 -04:00
$db = Propel::getConnection('workflow');
2015-06-18 18:12:35 -04:00
try {
2018-03-05 16:13:12 -04:00
$emailEvent = new ModelEmailEvent();
2018-03-05 16:13:12 -04:00
$emailEvent->fromArray($arrayData, BasePeer::TYPE_FIELDNAME);
2018-03-05 16:13:12 -04:00
$emailEventUid = Common::generateUID();
2015-06-18 18:12:35 -04:00
$emailEvent->setEmailEventUid($emailEventUid);
$emailEvent->setPrjUid($prj_uid);
2015-06-18 18:12:35 -04:00
$db->begin();
$result = $emailEvent->save();
$db->commit();
2015-06-18 18:12:35 -04:00
return $this->getEmailEvent($emailEventUid);
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
$db->rollback();
throw $e;
}
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
throw $e;
}
2015-06-18 18:12:35 -04:00
}
2015-06-18 18:12:35 -04:00
/**
* Update Email-Event
*
* @param string $emailEventUid Unique id of Email-Event
2018-03-05 16:13:12 -04:00
* @param array $arrayData Data
2015-06-18 18:12:35 -04:00
*
2018-03-05 16:13:12 -04:00
* @return array Return data of the Email-Event updated
* @throws Exception
2015-06-18 18:12:35 -04:00
*/
public function update($emailEventUid, array $arrayData)
{
try {
//Verify data
2018-03-05 16:13:12 -04:00
$validator = new Validator();
2015-06-18 18:12:35 -04:00
$validator->throwExceptionIfDataIsNotArray($arrayData, "\$arrayData");
$validator->throwExceptionIfDataIsEmpty($arrayData, "\$arrayData");
//Set data
$arrayData = array_change_key_case($arrayData, CASE_UPPER);
$arrayDataBackup = $arrayData;
//Set variables
$arrayEmailEventData = $this->getEmailEvent($emailEventUid);
//Verify data
$this->verifyIfEmailEventExists($emailEventUid);
2015-06-18 18:12:35 -04:00
//Update
2018-03-05 16:13:12 -04:00
$db = Propel::getConnection('workflow');
2015-06-18 18:12:35 -04:00
try {
2018-03-05 16:13:12 -04:00
$emailEvent = EmailEventPeer::retrieveByPK($emailEventUid);
$emailEvent->fromArray($arrayData, BasePeer::TYPE_FIELDNAME);
2015-06-18 18:12:35 -04:00
$db->begin();
$result = $emailEvent->save();
$db->commit();
$arrayData = $arrayDataBackup;
$arrayData = array_change_key_case($arrayData, CASE_LOWER);
return $arrayData;
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
$db->rollback();
2015-06-18 18:12:35 -04:00
throw $e;
}
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
throw $e;
}
}
2015-06-18 18:12:35 -04:00
/**
* Delete Email-Event
*
2018-03-05 16:13:12 -04:00
* @param string $pro_uid
2015-06-18 18:12:35 -04:00
* @param string $emailEventUid Unique id of Email-Event
2018-03-05 16:13:12 -04:00
* @param boolean $passValidation
* @param boolean $verifyRelation
2015-06-18 18:12:35 -04:00
*
2018-03-05 16:13:12 -04:00
* @throws Exception
2015-06-18 18:12:35 -04:00
*/
2018-03-05 16:13:12 -04:00
public function delete($pro_uid, $emailEventUid, $passValidation = true, $verifyRelation = true)
2015-06-18 18:12:35 -04:00
{
try {
//Verify data
if ($passValidation) {
$this->verifyIfEmailEventExists($emailEventUid);
//Delete file
2018-03-05 16:13:12 -04:00
$filesManager = new FilesManager();
$arrayData = $this->getEmailEventDataByUid($pro_uid, $emailEventUid);
2015-06-25 15:35:29 -04:00
$arrayData = array_change_key_case($arrayData, CASE_UPPER);
2018-03-05 16:13:12 -04:00
if ($arrayData) {
2015-06-25 15:35:29 -04:00
$prfUid = $arrayData['PRF_UID'];
2018-03-05 16:13:12 -04:00
$filesManager->deleteProcessFilesManager($pro_uid, $prfUid, $verifyRelation);
}
}
//Delete Email event
2018-03-05 16:13:12 -04:00
$criteria = new Criteria('workflow');
$criteria->add(EmailEventPeer::EMAIL_EVENT_UID, $emailEventUid, Criteria::EQUAL);
$result = EmailEventPeer::doDelete($criteria);
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
throw $e;
}
}
/**
* Delete Email-Event by event uid
*
2018-03-05 16:13:12 -04:00
* @param string $prj_uid Unique id of Process
* @param string $evn_uid Unique id of Email-Event
*
2018-03-05 16:13:12 -04:00
* @throws Exception
*/
public function deleteByEvent($prj_uid, $evn_uid)
{
try {
//Verify data
if (!$this->existsEvent($prj_uid, $evn_uid)) {
2018-03-05 16:13:12 -04:00
throw new Exception(G::LoadTranslation('ID_EMAIL_EVENT_DEFINITION_DOES_NOT_EXIST'));
}
$arrayData = $this->existsEvent($prj_uid, $evn_uid);
$this->delete($prj_uid, $arrayData[0]);
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
throw $e;
}
}
2015-06-18 18:12:35 -04:00
/**
* Get data of a Email-Event
*
* @param string $emailEventUid Unique id of Email-Event
*
2018-03-05 16:13:12 -04:00
* @return array Return an array with data of a Email-Event
* @throws Exception
2015-06-18 18:12:35 -04:00
*/
public function getEmailEvent($emailEventUid)
{
try {
//Verify data
2018-03-05 16:13:12 -04:00
$this->verifyIfEmailEventExists($emailEventUid);
2015-06-18 18:12:35 -04:00
//Get data
$criteria = $this->getEmailEventCriteria();
2018-03-05 16:13:12 -04:00
$criteria->add(EmailEventPeer::EMAIL_EVENT_UID, $emailEventUid, Criteria::EQUAL);
$rsCriteria = EmailEventPeer::doSelectRS($criteria);
$rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC);
2015-06-18 18:12:35 -04:00
$rsCriteria->next();
$row = $rsCriteria->getRow();
2018-03-05 16:13:12 -04:00
return $row ? $row : [];
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
throw $e;
}
}
2015-06-18 18:12:35 -04:00
/**
* Verify if exists the Email-Event
*
* @param string $emailEventUid Unique id of Email-Event
*
2018-03-05 16:13:12 -04:00
* @return bool Return true if exists the Email-Event, false otherwise
* @throws Exception
2015-06-18 18:12:35 -04:00
*/
public function exists($emailEventUid)
{
try {
2018-03-05 16:13:12 -04:00
$obj = EmailEventPeer::retrieveByPK($emailEventUid);
2015-06-18 18:12:35 -04:00
2018-03-05 16:13:12 -04:00
return $obj ? true : false;
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
throw $e;
}
}
2015-06-18 18:12:35 -04:00
/**
* Get criteria for Email-Event
*
2018-03-05 16:13:12 -04:00
* @return Criteria
* @throws Exception
2015-06-18 18:12:35 -04:00
*/
public function getEmailEventCriteria()
{
try {
2018-03-05 16:13:12 -04:00
$criteria = new Criteria('workflow');
2015-06-18 18:12:35 -04:00
2018-03-05 16:13:12 -04:00
$criteria->addSelectColumn(EmailEventPeer::EMAIL_EVENT_UID);
$criteria->addSelectColumn(EmailEventPeer::PRJ_UID);
$criteria->addSelectColumn(EmailEventPeer::EVN_UID);
$criteria->addSelectColumn(EmailEventPeer::EMAIL_EVENT_FROM);
$criteria->addSelectColumn(EmailEventPeer::EMAIL_EVENT_TO);
$criteria->addSelectColumn(EmailEventPeer::EMAIL_EVENT_SUBJECT);
$criteria->addSelectColumn(EmailEventPeer::PRF_UID);
$criteria->addSelectColumn(EmailEventPeer::EMAIL_SERVER_UID);
return $criteria;
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
throw $e;
}
}
/**
2018-03-05 16:13:12 -04:00
* Get Criteria for Email Event when Server Uid is empty
*
* @return Criteria
* @throws Exception
*/
public function getEmailEventCriteriaEmailServer()
{
try {
2018-03-05 16:13:12 -04:00
$criteria = new Criteria('workflow');
$criteria->addSelectColumn(EmailEventPeer::EMAIL_EVENT_UID);
$criteria->addSelectColumn(EmailEventPeer::PRJ_UID);
$criteria->addSelectColumn(EmailEventPeer::EVN_UID);
$criteria->addSelectColumn(EmailEventPeer::EMAIL_EVENT_FROM);
$criteria->addSelectColumn(EmailEventPeer::EMAIL_EVENT_TO);
$criteria->addSelectColumn(EmailEventPeer::EMAIL_EVENT_SUBJECT);
$criteria->addSelectColumn(EmailEventPeer::PRF_UID);
$criteria->addSelectColumn(EmailEventPeer::EMAIL_SERVER_UID);
$criteria->add(EmailEventPeer::EMAIL_SERVER_UID, '', Criteria::EQUAL);
2015-06-18 18:12:35 -04:00
return $criteria;
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
2015-06-18 18:12:35 -04:00
throw $e;
}
}
2018-03-05 16:13:12 -04:00
/**
* Verify if email Event exists
*
* @param $emailEventUid
* @throws Exception
*/
2015-06-18 18:12:35 -04:00
public function verifyIfEmailEventExists($emailEventUid)
{
if (!$this->exists($emailEventUid)) {
2018-03-05 16:13:12 -04:00
throw new Exception(G::LoadTranslation('ID_EMAIL_EVENT_DEFINITION_DOES_NOT_EXIST', ['Email Event Uid', $emailEventUid]));
2015-06-18 18:12:35 -04:00
}
}
/**
* Verify if exists the Event of a Message-Event-Definition
*
2018-03-05 16:13:12 -04:00
* @param string $projectUid Unique id of Project
* @param string $eventUid Unique id of Event
*
2018-03-05 16:13:12 -04:00
* @return bool Return true if exists the Event of a Message-Event-Definition, false otherwise
* @throws Exception
*/
public function existsEvent($projectUid, $eventUid)
{
try {
$criteria = $this->getEmailEventCriteria();
2018-03-05 16:13:12 -04:00
$criteria->add(EmailEventPeer::PRJ_UID, $projectUid, Criteria::EQUAL);
$criteria->add(EmailEventPeer::EVN_UID, $eventUid, Criteria::EQUAL);
$rsCriteria = EmailEventPeer::doSelectRS($criteria);
$rsCriteria->next();
$row = $rsCriteria->getRow();
2018-03-05 16:13:12 -04:00
if ($row) {
return array_change_key_case($row, CASE_LOWER);
}
2018-03-05 16:13:12 -04:00
return false;
} catch (Exception $e) {
throw $e;
}
}
/**
* Email-event do function
*
2018-03-05 16:13:12 -04:00
* @param string $appUID Unique id of application
* @param string $prj_uid Unique id of Project
* @param string $eventUid Unique id of event
* @param array $arrayApplicationData Case data
*
2018-03-05 16:13:12 -04:00
* @return void
* @throws Exception
*/
public function sendEmail($appUID, $prj_uid, $eventUid, $arrayApplicationData)
{
if (!$this->existsEvent($prj_uid, $eventUid)) {
2018-03-05 16:13:12 -04:00
throw new Exception(G::LoadTranslation('ID_EMAIL_EVENT_DEFINITION_DOES_NOT_EXIST'));
}
$arrayData = $this->existsEvent($prj_uid, $eventUid);
if (sizeof($arrayData)) {
$oEmailServer = new EmailServer();
$configEmailData = $oEmailServer->getEmailServer($arrayData[7]);
2018-03-05 16:13:12 -04:00
$emailGroupTo = [];
$emailTo = '';
$prfUid = $arrayData[6];
2018-03-05 16:13:12 -04:00
$filesManager = new FilesManager();
$contentFile = $filesManager->getProcessFileManager($prj_uid, $prfUid);
2018-03-05 16:13:12 -04:00
if (strpos($arrayData[4], ',')) {
$emailsArray = explode(',', $arrayData[4]);
foreach ($emailsArray as $email) {
2018-03-05 16:13:12 -04:00
if (substr($email, 0, 1) === '@') {
$email = substr($email, 2, strlen($email));
if (isset($arrayApplicationData['APP_DATA'])) {
if (is_array($arrayApplicationData['APP_DATA']) && isset($arrayApplicationData['APP_DATA'][$email])) {
$emailGroupTo[] = $arrayApplicationData['APP_DATA'][$email];
}
}
} else {
$emailGroupTo[] = $email;
}
}
2018-03-05 16:13:12 -04:00
$emailTo = implode(',', array_unique(array_filter($emailGroupTo)));
} else {
$email = $arrayData[4];
2018-03-05 16:13:12 -04:00
if (substr($email, 0, 1) === '@') {
$email = substr($email, 2, strlen($email));
if (isset($arrayApplicationData['APP_DATA'])) {
if (is_array($arrayApplicationData['APP_DATA']) && isset($arrayApplicationData['APP_DATA'][$email])) {
$emailTo = $arrayApplicationData['APP_DATA'][$email];
}
}
} else {
$emailTo = $email;
}
}
2016-08-17 16:27:32 -04:00
if (!empty($emailTo)) {
2018-03-05 16:13:12 -04:00
PMFSendMessage(
$appUID,
G::buildFrom($configEmailData),
$emailTo,
'',
'',
G::replaceDataField($arrayData[5], $arrayApplicationData['APP_DATA']),
$contentFile['prf_filename'],
[],
[],
true,
0,
$configEmailData
);
} else {
2018-03-05 16:13:12 -04:00
Bootstrap::registerMonolog(
'EmailEventMailError',
200,
G::LoadTranslation('ID_EMAIL_EVENT_CONFIGURATION_EMAIL', [$eventUid, $prj_uid]),
['eventUid' => $eventUid, 'prj_uid' => $prj_uid],
config('system.workspace'),
'processmaker.log');
}
}
}
/**
* Update process file Uid
*
2018-03-05 16:13:12 -04:00
* @param string $oldUid Unique id of old process file
* @param string $newUid Unique id of new process file
* @param string $projectUid Unique id of Project
*
2018-03-05 16:13:12 -04:00
* @throws Exception
*/
2018-03-05 16:13:12 -04:00
public function updatePrfUid($oldUid, $newUid, $projectUid)
{
try {
2018-03-05 16:13:12 -04:00
$newValues = [];
$rowData = $this->verifyIfEmailEventExistsByPrfUid($oldUid, $projectUid);
2018-03-05 16:13:12 -04:00
if ($rowData) {
2015-06-25 15:35:29 -04:00
$newValues['PRF_UID'] = $newUid;
$this->update($rowData['EMAIL_EVENT_UID'], $newValues);
}
2018-03-05 16:13:12 -04:00
} catch (Exception $e) {
throw $e;
}
}
/**
* Verify if exists the Email Event of
*
2018-03-05 16:13:12 -04:00
* @param string $oldUid Unique id of old process file
* @param string $projectUid Unique id of Project
*
2018-03-05 16:13:12 -04:00
* @return bool Return array if exists, false otherwise
* @throws Exception
*/
public function verifyIfEmailEventExistsByPrfUid($oldUid, $projectUid)
{
try {
$criteria = $this->getEmailEventCriteria();
2018-03-05 16:13:12 -04:00
$criteria->add(EmailEventPeer::PRJ_UID, $projectUid, Criteria::EQUAL);
$criteria->add(EmailEventPeer::PRF_UID, $oldUid, Criteria::EQUAL);
$rsCriteria = EmailEventPeer::doSelectRS($criteria);
$rsCriteria->setFetchmode(ResultSet::FETCHMODE_ASSOC);
$rsCriteria->next();
$row = $rsCriteria->getRow();
2018-03-05 16:13:12 -04:00
if ($row) {
return array_change_key_case($row, CASE_UPPER);
}
2018-03-05 16:13:12 -04:00
return false;
} catch (Exception $e) {
throw $e;
}
}
2015-06-18 18:12:35 -04:00
}