예제 #1
0
파일: Email.php 프로젝트: blackmode/espocrm
 public function loadBccField(Entity $entity)
 {
     $entity->loadLinkMultipleField('bccEmailAddresses');
     $names = $entity->get('bccEmailAddressesNames');
     if (!empty($names)) {
         $arr = array();
         foreach ($names as $id => $address) {
             $arr[] = $address;
         }
         $entity->set('bcc', implode(';', $arr));
     }
 }
예제 #2
0
 protected function loadLinkMultipleFields(Entity $entity)
 {
     $fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityType() . '.fields', array());
     foreach ($fieldDefs as $field => $defs) {
         if (isset($defs['type']) && in_array($defs['type'], ['linkMultiple', 'attachmentMultiple']) && empty($defs['noLoad'])) {
             $columns = null;
             if (!empty($defs['columns'])) {
                 $columns = $defs['columns'];
             }
             $entity->loadLinkMultipleField($field, $columns);
         }
     }
 }
예제 #3
0
 public function createQueue(Entity $massEmail, $isTest = false, $additionalTargetList = [])
 {
     if (!$isTest && $massEmail->get('status') !== 'Pending') {
         throw new Error("Mass Email '" . $massEmail->id . "' should be 'Pending'.");
     }
     if (!$isTest) {
         $existingQueueItemList = $this->getEntityManager()->getRepository('EmailQueueItem')->where(array('status' => ['Pending', 'Failed'], 'massEmailId' => $massEmail->id))->find();
         foreach ($existingQueueItemList as $existingQueueItem) {
             $this->getEntityManager()->getMapper('RDB')->deleteFromDb('EmailQueueItem', $existingQueueItem->id);
         }
     }
     $targetHash = array();
     $entityList = [];
     $pdo = $this->getEntityManager()->getPDO();
     if (!$isTest) {
         if (!$massEmail->has('excludingTargetListsIds')) {
             $massEmail->loadLinkMultipleField('excludingTargetLists');
         }
         $excludingTargetListIdList = $massEmail->get('excludingTargetListsIds');
         $targetListCollection = $massEmail->get('targetLists');
         foreach ($targetListCollection as $targetList) {
             $accountList = $targetList->get('accounts', array('additionalColumnsConditions' => array('optedOut' => false)));
             foreach ($accountList as $account) {
                 $hashId = $account->getEntityType() . '-' . $account->id;
                 if (!empty($targetHash[$hashId])) {
                     continue;
                 }
                 $toExclude = false;
                 foreach ($excludingTargetListIdList as $excludingTargetListId) {
                     $sql = "\n                            SELECT id FROM account_target_list\n                            WHERE\n                                account_id = " . $pdo->quote($account->id) . " AND\n                                target_list_id = " . $pdo->quote($excludingTargetListId) . " AND\n                                deleted = 0\n                        ";
                     $sth = $pdo->prepare($sql);
                     $sth->execute();
                     if ($sth->fetch()) {
                         $toExclude = true;
                         break;
                     }
                 }
                 if ($toExclude) {
                     continue;
                 }
                 $entityList[] = $account;
                 $targetHash[$hashId] = true;
             }
             $contactList = $targetList->get('contacts', array('additionalColumnsConditions' => array('optedOut' => false)));
             foreach ($contactList as $contact) {
                 $hashId = $contact->getEntityType() . '-' . $contact->id;
                 if (!empty($targetHash[$hashId])) {
                     continue;
                 }
                 $toExclude = false;
                 foreach ($excludingTargetListIdList as $excludingTargetListId) {
                     $sql = "\n                            SELECT id FROM contact_target_list\n                            WHERE\n                                contact_id = " . $pdo->quote($contact->id) . " AND\n                                target_list_id = " . $pdo->quote($excludingTargetListId) . " AND\n                                deleted = 0\n                        ";
                     $sth = $pdo->prepare($sql);
                     $sth->execute();
                     if ($sth->fetch()) {
                         $toExclude = true;
                         break;
                     }
                 }
                 if ($toExclude) {
                     continue;
                 }
                 $entityList[] = $contact;
                 $targetHash[$hashId] = true;
             }
             $leadList = $targetList->get('leads', array('additionalColumnsConditions' => array('optedOut' => false)));
             foreach ($leadList as $lead) {
                 $hashId = $lead->getEntityType() . '-' . $lead->id;
                 if (!empty($targetHash[$hashId])) {
                     continue;
                 }
                 $toExclude = false;
                 foreach ($excludingTargetListIdList as $excludingTargetListId) {
                     $sql = "\n                            SELECT id FROM lead_target_list\n                            WHERE\n                                lead_id = " . $pdo->quote($lead->id) . " AND\n                                target_list_id = " . $pdo->quote($excludingTargetListId) . " AND\n                                deleted = 0\n                        ";
                     $sth = $pdo->prepare($sql);
                     $sth->execute();
                     if ($sth->fetch()) {
                         $toExclude = true;
                         break;
                     }
                 }
                 if ($toExclude) {
                     continue;
                 }
                 $entityList[] = $lead;
                 $targetHash[$hashId] = true;
             }
             $userList = $targetList->get('users', array('additionalColumnsConditions' => array('optedOut' => false)));
             foreach ($userList as $user) {
                 $hashId = $user->getEntityType() . '-' . $user->id;
                 if (!empty($targetHash[$hashId])) {
                     continue;
                 }
                 $toExclude = false;
                 foreach ($excludingTargetListIdList as $excludingTargetListId) {
                     $sql = "\n                            SELECT id FROM target_list_user\n                            WHERE\n                                user_id = " . $pdo->quote($user->id) . " AND\n                                target_list_id = " . $pdo->quote($excludingTargetListId) . " AND\n                                deleted = 0\n                        ";
                     $sth = $pdo->prepare($sql);
                     $sth->execute();
                     if ($sth->fetch()) {
                         $toExclude = true;
                         break;
                     }
                 }
                 if ($toExclude) {
                     continue;
                 }
                 $entityList[] = $user;
                 $targetHash[$hashId] = true;
             }
         }
     }
     foreach ($additionalTargetList as $target) {
         $entityList[] = $target;
     }
     foreach ($entityList as $target) {
         $emailAddress = $target->get('emailAddress');
         if (!$target->get('emailAddress')) {
             continue;
         }
         $emailAddressRecord = $this->getEntityManager()->getRepository('EmailAddress')->getByAddress($emailAddress);
         if ($emailAddressRecord) {
             if ($emailAddressRecord->get('invalid') || $emailAddressRecord->get('optOut')) {
                 continue;
             }
         }
         $queueItem = $this->getEntityManager()->getEntity('EmailQueueItem');
         $queueItem->set(array('massEmailId' => $massEmail->id, 'status' => 'Pending', 'targetId' => $target->id, 'targetType' => $target->getEntityType(), 'isTest' => $isTest));
         $this->getEntityManager()->saveEntity($queueItem);
     }
     if (!$isTest) {
         $massEmail->set('status', 'In Process');
         if (empty($entityList)) {
             $massEmail->set('status', 'Complete');
         }
         $this->getEntityManager()->saveEntity($massEmail);
     }
 }
예제 #4
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'));
                 }
             }
         }
     }
 }
예제 #5
0
파일: Record.php 프로젝트: lucasmattos/crm
 protected function loadLinkMultipleFields(Entity $entity)
 {
     $fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityName() . '.fields', array());
     foreach ($fieldDefs as $field => $defs) {
         if ($defs['type'] == 'linkMultiple') {
             $columns = null;
             if (!empty($defs['columns'])) {
                 $columns = $defs['columns'];
             }
             $entity->loadLinkMultipleField($field, $columns);
         }
     }
 }
예제 #6
0
파일: Base.php 프로젝트: blackmode/espocrm
 public function checkInTeam(User $user, Entity $entity)
 {
     $userTeamIds = $user->get('teamsIds');
     if (!$entity->hasRelation('teams') || !$entity->hasField('teamsIds')) {
         return false;
     }
     if (!$entity->has('teamsIds')) {
         $entity->loadLinkMultipleField('teams');
     }
     $teamIds = $entity->get('teamsIds');
     if (empty($teamIds)) {
         return false;
     }
     foreach ($userTeamIds as $id) {
         if (in_array($id, $teamIds)) {
             return true;
         }
     }
     return false;
 }