예제 #1
0
파일: Job.php 프로젝트: mehulsbhatt/espocrm
 public function beforeSave(Entity $entity)
 {
     if (!$entity->has('executeTime')) {
         $entity->set('executeTime', date('Y-m-d H:i:s'));
     }
     if (!$entity->has('attempts')) {
         $attempts = $this->getConfig()->get('cron.attempts', 0);
         $entity->set('attempts', $attempts);
     }
 }
예제 #2
0
 protected function loadUrlField(Entity $entity)
 {
     $siteUrl = $this->getConfig()->get('siteUrl');
     $url = $siteUrl . '?entryPoint=portal';
     if ($entity->id === $this->getConfig()->get('defaultPortalId')) {
         $entity->set('isDefault', true);
     } else {
         $url .= '&id=' . $entity->id;
     }
     $entity->set('url', $url);
 }
예제 #3
0
 protected function beforeSave(Entity $entity)
 {
     $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress');
     $from = trim($entity->get('from'));
     if (!empty($from)) {
         $ids = $eaRepositoty->getIds(array($from));
         if (!empty($ids)) {
             $entity->set('fromEmailAddressId', $ids[0]);
         }
     } else {
         $entity->set('fromEmailAddressId', null);
     }
     $this->prepareAddressess($entity, 'to');
     $this->prepareAddressess($entity, 'cc');
     $this->prepareAddressess($entity, 'bcc');
     parent::beforeSave($entity);
     $parentId = $entity->get('parentId');
     $parentType = $entity->get('parentType');
     if (!empty($parentId) || !empty($parentType)) {
         $parent = $this->getEntityManager()->getEntity($parentType, $parentId);
         if (!empty($parent)) {
             if ($parent->getEntityName() == 'Account') {
                 $accountId = $parent->id;
             } else {
                 if ($parent->has('accountId')) {
                     $accountId = $parent->get('accountId');
                 }
             }
             if (!empty($accountId)) {
                 $entity->set('accountId', $accountId);
             }
         }
     } else {
         // TODO find account by from address
     }
 }
예제 #4
0
 protected function storeEntity(Entity $entity)
 {
     $assignedUserId = $entity->get('assignedUserId');
     if ($assignedUserId && $entity->has('usersIds')) {
         $usersIds = $entity->get('usersIds');
         if (!is_array($usersIds)) {
             $usersIds = array();
         }
         if (!in_array($assignedUserId, $usersIds)) {
             $usersIds[] = $assignedUserId;
             $entity->set('usersIds', $usersIds);
             $hash = $entity->get('usersNames');
             if ($hash instanceof \stdClass) {
                 $hash->assignedUserId = $entity->get('assignedUserName');
                 $entity->set('usersNames', $hash);
             }
         }
     }
     return parent::storeEntity($entity);
 }
예제 #5
0
파일: Email.php 프로젝트: blackmode/espocrm
 public function loadUserColumnFields(Entity $entity)
 {
     $pdo = $this->getEntityManager()->getPDO();
     $sql = "\n            SELECT is_read AS 'isRead', is_important AS 'isImportant' FROM email_user\n            WHERE\n                deleted = 0 AND\n                user_id = " . $pdo->quote($this->getUser()->id) . " AND\n                email_id = " . $pdo->quote($entity->id) . "\n        ";
     $sth = $pdo->prepare($sql);
     $sth->execute();
     if ($row = $sth->fetch(\PDO::FETCH_ASSOC)) {
         $isRead = !empty($row['isRead']) ? true : false;
         $isImportant = !empty($row['isImportant']) ? true : false;
         $entity->set('isRead', $isRead);
         $entity->set('isImportant', $isImportant);
     } else {
         $entity->set('isRead', null);
         $entity->clear('isImportant');
     }
 }
예제 #6
0
 protected function loadPhoneNumberField(Entity $entity)
 {
     $fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.fields', array());
     if (!empty($fieldDefs['phoneNumber']) && $fieldDefs['phoneNumber']['type'] == 'phone') {
         $dataFieldName = 'phoneNumberData';
         $entity->set($dataFieldName, $this->getEntityManager()->getRepository('PhoneNumber')->getPhoneNumberData($entity));
     }
 }
예제 #7
0
파일: RDB.php 프로젝트: naushrambo/espocrm
 public function save(Entity $entity, array $options = array())
 {
     $nowString = date('Y-m-d H:i:s', time());
     $restoreData = array();
     if ($entity->isNew()) {
         if (!$entity->has('id')) {
             $entity->set('id', Util::generateId());
         }
         if ($entity->hasField('createdAt')) {
             $entity->set('createdAt', $nowString);
         }
         if ($entity->hasField('modifiedAt')) {
             $entity->set('modifiedAt', $nowString);
         }
         if ($entity->hasField('createdById')) {
             $entity->set('createdById', $this->entityManager->getUser()->id);
         }
         if ($entity->has('modifiedById')) {
             $restoreData['modifiedById'] = $entity->get('modifiedById');
         }
         if ($entity->has('modifiedAt')) {
             $restoreData['modifiedAt'] = $entity->get('modifiedAt');
         }
         $entity->clear('modifiedById');
     } else {
         if (empty($options['silent'])) {
             if ($entity->hasField('modifiedAt')) {
                 $entity->set('modifiedAt', $nowString);
             }
             if ($entity->hasField('modifiedById')) {
                 $entity->set('modifiedById', $this->entityManager->getUser()->id);
             }
         }
         if ($entity->has('createdById')) {
             $restoreData['createdById'] = $entity->get('createdById');
         }
         if ($entity->has('createdAt')) {
             $restoreData['createdAt'] = $entity->get('createdAt');
         }
         $entity->clear('createdById');
         $entity->clear('createdAt');
     }
     $this->restoreData = $restoreData;
     $result = parent::save($entity, $options);
     return $result;
 }
예제 #8
0
 protected function parseValue(Entity $entity, $field, $value, $params = array())
 {
     $decimalMark = '.';
     if (!empty($params['decimalMark'])) {
         $decimalMark = $params['decimalMark'];
     }
     $defaultCurrency = 'USD';
     if (!empty($params['defaultCurrency'])) {
         $defaultCurrency = $params['defaultCurrency'];
     }
     $dateFormat = 'Y-m-d';
     if (!empty($params['dateFormat'])) {
         if (!empty($this->dateFormatsMap[$params['dateFormat']])) {
             $dateFormat = $this->dateFormatsMap[$params['dateFormat']];
         }
     }
     $timeFormat = 'H:i';
     if (!empty($params['timeFormat'])) {
         if (!empty($this->timeFormatsMap[$params['timeFormat']])) {
             $timeFormat = $this->timeFormatsMap[$params['timeFormat']];
         }
     }
     $fieldDefs = $entity->getFields();
     if (!empty($fieldDefs[$field])) {
         $type = $fieldDefs[$field]['type'];
         switch ($type) {
             case Entity::DATE:
                 $dt = \DateTime::createFromFormat($dateFormat, $value);
                 if ($dt) {
                     return $dt->format('Y-m-d');
                 }
                 break;
             case Entity::DATETIME:
                 $dt = \DateTime::createFromFormat($dateFormat . ' ' . $timeFormat, $value);
                 if ($dt) {
                     return $dt->format('Y-m-d H:i');
                 }
                 break;
             case Entity::FLOAT:
                 $currencyField = $field . 'Currency';
                 if ($entity->hasField($currencyField)) {
                     if (!$entity->has($currencyField)) {
                         $entity->set($currencyField, $defaultCurrency);
                     }
                 }
                 $a = explode($decimalMark, $value);
                 $a[0] = preg_replace('/[^A-Za-z0-9\\-]/', '', $a[0]);
                 if (count($a) > 1) {
                     return $a[0] . '.' . $a[1];
                 } else {
                     return $a[0];
                 }
                 break;
         }
     }
     return $value;
 }
예제 #9
0
 protected function fetchAutoFollowEntityTypeList(Entity $entity)
 {
     $id = $entity->id;
     $autoFollowEntityTypeList = [];
     $pdo = $this->getEntityManger()->getPDO();
     $sql = "\n            SELECT `entity_type` AS 'entityType' FROM `autofollow`\n            WHERE `user_id` = " . $pdo->quote($id) . "\n            ORDER BY `entity_type`\n        ";
     $sth = $pdo->prepare($sql);
     $sth->execute();
     $rows = $sth->fetchAll();
     foreach ($rows as $row) {
         $autoFollowEntityTypeList[] = $row['entityType'];
     }
     $this->data[$id]['autoFollowEntityTypeList'] = $autoFollowEntityTypeList;
     $entity->set('autoFollowEntityTypeList', $autoFollowEntityTypeList);
 }
예제 #10
0
 protected function sendQueueItem(Entity $queueItem, Entity $massEmail, Entity $emailTemplate, $attachmetList = [], $campaign = null, $isTest = false)
 {
     $target = $this->getEntityManager()->getEntity($queueItem->get('targetType'), $queueItem->get('targetId'));
     if (!$target || !$target->id || !$target->get('emailAddress')) {
         $queueItem->set('status', 'Failed');
         $this->getEntityManager()->saveEntity($queueItem);
         return;
     }
     $emailAddress = $target->get('emailAddress');
     if (!$emailAddress) {
         $queueItem->set('status', 'Failed');
         $this->getEntityManager()->saveEntity($queueItem);
         return false;
     }
     $emailAddressRecord = $this->getEntityManager()->getRepository('EmailAddress')->getByAddress($emailAddress);
     if ($emailAddressRecord) {
         if ($emailAddressRecord->get('invalid') || $emailAddressRecord->get('optOut')) {
             $queueItem->set('status', 'Failed');
             $this->getEntityManager()->saveEntity($queueItem);
             return false;
         }
     }
     $trackingUrlList = [];
     if ($campaign) {
         $trackingUrlList = $campaign->get('trackingUrls');
     }
     $email = $this->getPreparedEmail($queueItem, $massEmail, $emailTemplate, $target, $trackingUrlList);
     $params = array();
     if ($massEmail->get('fromName')) {
         $params['fromName'] = $massEmail->get('fromName');
     }
     if ($massEmail->get('replyToName')) {
         $params['replyToName'] = $massEmail->get('replyToName');
     }
     try {
         $attemptCount = $queueItem->get('attemptCount');
         $attemptCount++;
         $queueItem->set('attemptCount', $attemptCount);
         $message = new \Zend\Mail\Message();
         $header = new \Espo\Core\Mail\Mail\Header\XQueueItemId();
         $header->setId($queueItem->id);
         $message->getHeaders()->addHeader($header);
         $this->getMailSender()->useGlobal()->send($email, $params, $message, $attachmetList);
         $emailObject = $emailTemplate;
         if ($massEmail->get('storeSentEmails')) {
             $this->getEntityManager()->saveEntity($email);
             $emailObject = $email;
         }
         $queueItem->set('emailAddress', $target->get('emailAddress'));
         $queueItem->set('status', 'Sent');
         $queueItem->set('sentAt', date('Y-m-d H:i:s'));
         $this->getEntityManager()->saveEntity($queueItem);
         if ($campaign) {
             $this->getCampaignService()->logSent($campaign->id, $queueItem->id, $target, $emailObject, $target->get('emailAddress'));
         }
     } catch (\Exception $e) {
         if ($queueItem->get('attemptCount') >= self::MAX_ATTEMPT_COUNT) {
             $queueItem->set('status', 'Failed');
         }
         $this->getEntityManager()->saveEntity($queueItem);
         $GLOBALS['log']->error('MassEmail#sendQueueItem: [' . $e->getCode() . '] ' . $e->getMessage());
         return false;
     }
     return true;
 }
예제 #11
0
 protected function findParent(Entity $email, $emailAddress)
 {
     $contact = $this->getEntityManager()->getRepository('Contact')->where(array('emailAddress' => $emailAddress))->findOne();
     if ($contact) {
         if (!$this->getConfig()->get('b2cMode')) {
             if ($contact->get('accountId')) {
                 $email->set('parentType', 'Account');
                 $email->set('parentId', $contact->get('accountId'));
                 return true;
             }
         } else {
             $email->set('parentType', 'Contact');
             $email->set('parentId', $contact->id);
             return true;
         }
     } else {
         $account = $this->getEntityManager()->getRepository('Account')->where(array('emailAddress' => $emailAddress))->findOne();
         if ($account) {
             $email->set('parentType', 'Account');
             $email->set('parentId', $account->id);
             return true;
         } else {
             $lead = $this->getEntityManager()->getRepository('Lead')->where(array('emailAddress' => $emailAddress))->findOne();
             if ($lead) {
                 $email->set('parentType', 'Lead');
                 $email->set('parentId', $lead->id);
                 return true;
             }
         }
     }
 }
예제 #12
0
 public function save(Entity $entity)
 {
     $nowString = date('Y-m-d H:i:s', time());
     $restoreData = array();
     if ($entity->isNew()) {
         if (!$entity->has('id')) {
             $entity->set('id', uniqid());
         }
         if ($entity->hasField('createdAt')) {
             $entity->set('createdAt', $nowString);
         }
         if ($entity->hasField('createdById')) {
             $entity->set('createdById', $this->entityManager->getUser()->id);
         }
         if ($entity->has('modifiedById')) {
             $restoreData['modifiedById'] = $entity->get('modifiedById');
         }
         if ($entity->has('modifiedAt')) {
             $restoreData['modifiedAt'] = $entity->get('modifiedAt');
         }
         $entity->clear('modifiedById');
         $entity->clear('modifiedAt');
     } else {
         if ($entity->hasField('modifiedAt')) {
             $entity->set('modifiedAt', $nowString);
         }
         if ($entity->hasField('modifiedById')) {
             $entity->set('modifiedById', $this->entityManager->getUser()->id);
         }
         if ($entity->has('createdById')) {
             $restoreData['createdById'] = $entity->get('createdById');
         }
         if ($entity->has('createdAt')) {
             $restoreData['createdAt'] = $entity->get('createdAt');
         }
         $entity->clear('createdById');
         $entity->clear('createdAt');
     }
     $result = parent::save($entity);
     $entity->set($restoreData);
     $this->handleEmailAddressSave($entity);
     $this->handlePhoneNumberSave($entity);
     $this->handleSpecifiedRelations($entity);
     return $result;
 }
예제 #13
0
파일: Email.php 프로젝트: lucasmattos/crm
 public function loadNameHash(Entity $entity)
 {
     $addressList = array();
     if ($entity->get('from')) {
         $addressList[] = $entity->get('from');
     }
     $arr = explode(';', $entity->get('to'));
     foreach ($arr as $address) {
         if (!in_array($address, $addressList)) {
             $addressList[] = $address;
         }
     }
     $arr = explode(';', $entity->get('cc'));
     foreach ($arr as $address) {
         if (!in_array($address, $addressList)) {
             $addressList[] = $address;
         }
     }
     $nameHash = array();
     $typeHash = array();
     $idHash = array();
     foreach ($addressList as $address) {
         $p = $this->getEntityManager()->getRepository('EmailAddress')->getEntityByAddress($address);
         if ($p) {
             $nameHash[$address] = $p->get('name');
             $typeHash[$address] = $p->getEntityName();
             $idHash[$address] = $p->id;
         }
     }
     $entity->set('nameHash', $nameHash);
     $entity->set('typeHash', $typeHash);
     $entity->set('idHash', $idHash);
 }
예제 #14
0
 protected function beforeSave(Entity $entity, array $options)
 {
     $eaRepositoty = $this->getEntityManager()->getRepository('EmailAddress');
     if ($entity->has('attachmentsIds')) {
         $attachmentsIds = $entity->get('attachmentsIds');
         if (!empty($attachmentsIds)) {
             $entity->set('hasAttachment', true);
         }
     }
     if ($entity->has('from') || $entity->has('to') || $entity->has('cc') || $entity->has('bcc') || $entity->has('replyTo')) {
         if (!$entity->has('usersIds')) {
             $entity->loadLinkMultipleField('users');
         }
         if ($entity->has('from')) {
             $from = trim($entity->get('from'));
             if (!empty($from)) {
                 $ids = $eaRepositoty->getIds(array($from));
                 if (!empty($ids)) {
                     $entity->set('fromEmailAddressId', $ids[0]);
                     $this->setUsersIdsByEmailAddressId($entity, $ids[0]);
                 }
             } else {
                 $entity->set('fromEmailAddressId', null);
             }
         }
         if ($entity->has('to')) {
             $this->prepareAddressess($entity, 'to');
         }
         if ($entity->has('cc')) {
             $this->prepareAddressess($entity, 'cc');
         }
         if ($entity->has('bcc')) {
             $this->prepareAddressess($entity, 'bcc');
         }
         if ($entity->has('replyTo')) {
             $this->prepareAddressess($entity, 'replyTo');
         }
         $usersIds = $entity->get('usersIds');
         $assignedUserId = $entity->get('assignedUserId');
         if (!empty($assignedUserId) && !in_array($assignedUserId, $usersIds)) {
             $usersIds[] = $assignedUserId;
         }
         $entity->set('usersIds', $usersIds);
     }
     parent::beforeSave($entity, $options);
     $parentId = $entity->get('parentId');
     $parentType = $entity->get('parentType');
     if (!empty($parentId) || !empty($parentType)) {
         $parent = $this->getEntityManager()->getEntity($parentType, $parentId);
         if (!empty($parent)) {
             if ($parent->getEntityType() == 'Account') {
                 $accountId = $parent->id;
             } else {
                 if ($parent->has('accountId')) {
                     $accountId = $parent->get('accountId');
                 }
             }
             if (!empty($accountId)) {
                 $account = $this->getEntityManager()->getEntity('Account', $accountId);
                 if ($account) {
                     $entity->set('accountId', $accountId);
                     $entity->set('accountName', $account->get('name'));
                 }
             }
         }
     }
 }
예제 #15
0
 protected function loadAssignedUserName(Entity $entity)
 {
     $user = $this->getEntityManager()->getEntity('User', $entity->get('assignedUserId'));
     if ($user) {
         $entity->set('assignedUserName', $user->get('name'));
     }
 }
예제 #16
0
 public function fetchFromMailServer(Entity $emailAccount)
 {
     if ($emailAccount->get('status') != 'Active') {
         throw new Error();
     }
     $importer = new \Espo\Core\Mail\Importer($this->getEntityManager(), $this->getFileManager());
     $maxSize = $this->getConfig()->get('emailMessageMaxSize');
     $user = $this->getEntityManager()->getEntity('User', $emailAccount->get('assignedUserId'));
     if (!$user) {
         throw new Error();
     }
     $userId = $user->id;
     $teamId = $user->get('defaultTeam');
     $fetchData = json_decode($emailAccount->get('fetchData'), true);
     if (empty($fetchData)) {
         $fetchData = array();
     }
     if (!array_key_exists('lastUID', $fetchData)) {
         $fetchData['lastUID'] = array();
     }
     if (!array_key_exists('lastUID', $fetchData)) {
         $fetchData['lastDate'] = array();
     }
     $imapParams = array('host' => $emailAccount->get('host'), 'port' => $emailAccount->get('port'), 'user' => $emailAccount->get('username'), 'password' => $emailAccount->get('password'));
     if ($emailAccount->get('ssl')) {
         $imapParams['ssl'] = 'SSL';
     }
     $storage = new \Espo\Core\Mail\Storage\Imap($imapParams);
     $monitoredFolders = $emailAccount->get('monitoredFolders');
     if (empty($monitoredFolders)) {
         throw new Error();
     }
     $monitoredFoldersArr = explode(',', $monitoredFolders);
     foreach ($monitoredFoldersArr as $folder) {
         $folder = trim($folder);
         $storage->selectFolder($folder);
         $lastUID = 0;
         $lastDate = 0;
         if (!empty($fetchData['lastUID'][$folder])) {
             $lastUID = $fetchData['lastUID'][$folder];
         }
         if (!empty($fetchData['lastDate'][$folder])) {
             $lastDate = $fetchData['lastDate'][$folder];
         }
         if (!empty($lastUID)) {
             $ids = $storage->getIdsFromUID($lastUID);
         } else {
             $dt = new \DateTime($emailAccount->get('fetchSince'));
             if ($dt) {
                 $ids = $storage->getIdsFromDate($dt->format('d-M-Y'));
             } else {
                 return false;
             }
         }
         if (count($ids) == 1 && !empty($lastUID)) {
             if ($storage->getUniqueId($ids[0]) == $lastUID) {
                 continue;
             }
         }
         $k = 0;
         foreach ($ids as $i => $id) {
             if ($k == count($ids) - 1) {
                 $lastUID = $storage->getUniqueId($id);
             }
             if ($maxSize) {
                 if ($storage->getSize($id) > $maxSize * 1024 * 1024) {
                     continue;
                 }
             }
             $message = $storage->getMessage($id);
             $email = $importer->importMessage($message, $userId, array($teamId));
             if ($k == count($ids) - 1) {
                 $lastUID = $storage->getUniqueId($id);
                 if ($message) {
                     $dt = new \DateTime($message->date);
                     if ($dt) {
                         $dateSent = $dt->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s');
                         $lastDate = $dateSent;
                     }
                 }
             }
             if ($k == self::PORTION_LIMIT - 1) {
                 $lastUID = $storage->getUniqueId($id);
                 break;
             }
             $k++;
         }
         $fetchData['lastUID'][$folder] = $lastUID;
         $fetchData['lastDate'][$folder] = $lastDate;
         $emailAccount->set('fetchData', json_encode($fetchData));
         $this->getEntityManager()->saveEntity($emailAccount);
     }
     return true;
 }