public function afterSave(Entity $entity)
 {
     if ($this->getConfig()->get('assignmentEmailNotifications') && in_array($entity->getEntityName(), $this->getConfig()->get('assignmentEmailNotificationsEntityList', array()))) {
         $userId = $entity->get('assignedUserId');
         if (!empty($userId) && $userId != $this->getUser()->id && $entity->isFieldChanged('assignedUserId')) {
             $job = $this->getEntityManager()->getEntity('Job');
             $job->set(array('serviceName' => 'EmailNotification', 'method' => 'notifyAboutAssignmentJob', 'data' => json_encode(array('userId' => $userId, 'assignerUserId' => $this->getUser()->id, 'entityId' => $entity->id, 'entityType' => $entity->getEntityName())), 'executeTime' => date('Y-m-d H:i:s')));
             $this->getEntityManager()->saveEntity($job);
         }
     }
 }
示例#2
0
 public function sendInvitation(Entity $entity, Entity $invitee, $link)
 {
     $uid = $this->getEntityManager()->getEntity('UniqueId');
     $uid->set('data', json_encode(array('eventType' => $entity->getEntityName(), 'eventId' => $entity->id, 'inviteeId' => $invitee->id, 'inviteeType' => $invitee->getEntityName(), 'link' => $link)));
     $this->getEntityManager()->saveEntity($uid);
     $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, $uid);
     $body = $this->parseInvitationTemplate($bodyTpl, $entity, $invitee, $uid);
     $email->set('subject', $subject);
     $email->set('body', $body);
     $email->set('isHtml', true);
     $this->getEntityManager()->saveEntity($email);
     $attachment = $this->getEntityManager()->getEntity('Attachment');
     $attachment->set(array('name' => 'event.ics', 'type' => 'text/calendar', 'contents' => $this->getIscContents($entity)));
     $email->addAttachment($attachment);
     $emailSender = $this->mailSender;
     $emailSender->send($email);
     $this->getEntityManager()->removeEntity($email);
 }
示例#3
0
 protected function handleCreateRelated(Entity $entity)
 {
     $linkDefs = $this->getMetadata()->get("entityDefs." . $entity->getEntityName() . ".links", array());
     $scopeNotifiedList = array();
     foreach ($linkDefs as $link => $defs) {
         if ($defs['type'] == 'belongsTo') {
             if (empty($defs['foreign']) || empty($defs['entity'])) {
                 continue;
             }
             $foreign = $defs['foreign'];
             $scope = $defs['entity'];
             $entityId = $entity->get($link . 'Id');
             if (!empty($scope) && !empty($entityId)) {
                 if (in_array($scope, $scopeNotifiedList) || !$this->isLinkObservableInStream($scope, $foreign)) {
                     continue;
                 }
                 $this->getStreamService()->noteCreateRelated($entity, $scope, $entityId);
                 $scopeNotifiedList[] = $scope;
             }
         } else {
             if ($defs['type'] == 'belongsToParent') {
                 $foreign = $defs['foreign'];
                 if (empty($defs['foreign'])) {
                     continue;
                 }
                 $scope = $entity->get($link . 'Type');
                 $entityId = $entity->get($link . 'Id');
                 if (!empty($scope) && !empty($entityId)) {
                     if (in_array($scope, $scopeNotifiedList) || !$this->isLinkObservableInStream($scope, $foreign)) {
                         continue;
                     }
                     $this->getStreamService()->noteCreateRelated($entity, $scope, $entityId);
                     $scopeNotifiedList[] = $scope;
                 }
             } else {
                 if ($defs['type'] == 'hasMany') {
                     if (empty($defs['foreign']) || empty($defs['entity'])) {
                         continue;
                     }
                     $foreign = $defs['foreign'];
                     $scope = $defs['entity'];
                     $entityIds = $entity->get($link . 'Ids');
                     if (!empty($scope) && is_array($entityIds) && !empty($entityIds)) {
                         if (in_array($scope, $scopeNotifiedList) || !$this->isLinkObservableInStream($scope, $foreign)) {
                             continue;
                         }
                         $entityId = $entityIds[0];
                         $this->getStreamService()->noteCreateRelated($entity, $scope, $entityId);
                         $scopeNotifiedList[] = $scope;
                     }
                 }
             }
         }
     }
 }
示例#4
0
 public function getPhoneNumberData(Entity $entity)
 {
     $data = array();
     $pdo = $this->getEntityManager()->getPDO();
     $sql = "\n            SELECT phone_number.name, phone_number.type, entity_phone_number.primary\n            FROM entity_phone_number\n            JOIN phone_number ON phone_number.id = entity_phone_number.phone_number_id AND phone_number.deleted = 0\n            WHERE\n            entity_phone_number.entity_id = " . $pdo->quote($entity->id) . " AND\n            entity_phone_number.entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n            entity_phone_number.deleted = 0\n            ORDER BY entity_phone_number.primary DESC\n        ";
     $sth = $pdo->prepare($sql);
     $sth->execute();
     if ($rows = $sth->fetchAll()) {
         foreach ($rows as $row) {
             $obj = new \StdClass();
             $obj->phoneNumber = $row['name'];
             $obj->primary = $row['primary'] == '1' ? true : false;
             $obj->type = $row['type'];
             $data[] = $obj;
         }
     }
     return $data;
 }
示例#5
0
 public function getEmailAddressData(Entity $entity)
 {
     $data = array();
     $pdo = $this->getEntityManager()->getPDO();
     $sql = "\n\t\t\tSELECT email_address.name, email_address.invalid, email_address.opt_out AS optOut, entity_email_address.primary \n\t\t\tFROM entity_email_address\n\t\t\tJOIN email_address ON email_address.id = entity_email_address.email_address_id AND email_address.deleted = 0\n\t\t\tWHERE \n\t\t\tentity_email_address.entity_id = " . $pdo->quote($entity->id) . " AND \n\t\t\tentity_email_address.entity_type = " . $pdo->quote($entity->getEntityName()) . " AND \n\t\t\tentity_email_address.deleted = 0\n\t\t\tORDER BY entity_email_address.primary DESC\n\t\t";
     $sth = $pdo->prepare($sql);
     $sth->execute();
     if ($rows = $sth->fetchAll()) {
         foreach ($rows as $row) {
             $obj = new \StdClass();
             $obj->emailAddress = $row['name'];
             $obj->primary = $row['primary'] == '1' ? true : false;
             $obj->optOut = $row['optOut'] == '1' ? true : false;
             $obj->invalid = $row['invalid'] == '1' ? true : false;
             $data[] = $obj;
         }
     }
     return $data;
 }
示例#6
0
 protected function handleSpecifiedRelations(Entity $entity)
 {
     $relationTypes = array($entity::HAS_MANY, $entity::MANY_MANY, $entity::HAS_CHILDREN);
     foreach ($entity->getRelations() as $name => $defs) {
         if (in_array($defs['type'], $relationTypes)) {
             $fieldName = $name . 'Ids';
             if ($entity->has($fieldName)) {
                 $specifiedIds = $entity->get($fieldName);
                 if (is_array($specifiedIds)) {
                     $toRemoveIds = array();
                     $existingIds = array();
                     $toUpdateIds = array();
                     $existingColumnsData = new \stdClass();
                     $defs = array();
                     $columns = $this->getMetadata()->get("entityDefs." . $entity->getEntityName() . ".fields.{$name}.columns");
                     if (!empty($columns)) {
                         $columnData = $entity->get($name . 'Columns');
                         $defs['additionalColumns'] = $columns;
                     }
                     foreach ($entity->get($name, $defs) as $foreignEntity) {
                         $existingIds[] = $foreignEntity->id;
                         if (!empty($columns)) {
                             $data = new \stdClass();
                             foreach ($columns as $columnName => $columnField) {
                                 $foreignId = $foreignEntity->id;
                                 $data->{$columnName} = $foreignEntity->get($columnField);
                             }
                             $existingColumnsData->{$foreignId} = $data;
                         }
                     }
                     foreach ($existingIds as $id) {
                         if (!in_array($id, $specifiedIds)) {
                             $toRemoveIds[] = $id;
                         } else {
                             if (!empty($columns)) {
                                 foreach ($columns as $columnName => $columnField) {
                                     if ($columnData->{$id}->{$columnName} != $existingColumnsData->{$id}->{$columnName}) {
                                         $toUpdateIds[] = $id;
                                     }
                                 }
                             }
                         }
                     }
                     foreach ($specifiedIds as $id) {
                         if (!in_array($id, $existingIds)) {
                             $data = null;
                             if (!empty($columns)) {
                                 $data = $columnData->{$id};
                             }
                             $this->relate($entity, $name, $id, $data);
                         }
                     }
                     foreach ($toRemoveIds as $id) {
                         $this->unrelate($entity, $name, $id);
                     }
                     if (!empty($columns)) {
                         foreach ($toUpdateIds as $id) {
                             $data = $columnData->{$id};
                             $this->updateRelation($entity, $name, $id, $data);
                         }
                     }
                 }
             }
         }
     }
 }
示例#7
0
 protected function getAuditedFields(Entity $entity)
 {
     $entityName = $entity->getEntityName();
     if (!array_key_exists($entityName, $this->auditedFieldsCache)) {
         $fields = $this->getMetadata()->get('entityDefs.' . $entityName . '.fields');
         $auditedFields = array();
         foreach ($fields as $field => $d) {
             if (!empty($d['audited'])) {
                 $attributes = array();
                 $fieldsDefs = $this->getMetadata()->get('fields.' . $d['type']);
                 if (empty($fieldsDefs['actualFields'])) {
                     $attributes[] = $field;
                 } else {
                     foreach ($fieldsDefs['actualFields'] as $part) {
                         if (!empty($fieldsDefs['naming']) && $fieldsDefs['naming'] == 'prefix') {
                             $attributes[] = $part . ucfirst($field);
                         } else {
                             $attributes[] = $field . ucfirst($part);
                         }
                     }
                 }
                 $auditedFields[$field] = $attributes;
             }
         }
         $this->auditedFieldsCache[$entityName] = $auditedFields;
     }
     return $this->auditedFieldsCache[$entityName];
 }
示例#8
0
 protected function loadPhoneNumberField(Entity $entity)
 {
     $fieldDefs = $this->getMetadata()->get('entityDefs.' . $entity->getEntityName() . '.fields', array());
     if (!empty($fieldDefs['phoneNumber']) && $fieldDefs['phoneNumber']['type'] == 'phone') {
         $dataFieldName = 'phoneNumberData';
         $entity->set($dataFieldName, $this->getEntityManager()->getRepository('PhoneNumber')->getPhoneNumberData($entity));
     }
 }
示例#9
0
 public function sendInvitation(Entity $entity, Entity $invitee, $link)
 {
     $uid = $this->getEntityManager()->getEntity('UniqueId');
     $uid->set('data', array('eventType' => $entity->getEntityName(), 'eventId' => $entity->id, 'inviteeId' => $invitee->id, 'inviteeType' => $invitee->getEntityName(), 'link' => $link));
     $this->getEntityManager()->saveEntity($uid);
     $emailAddress = $invitee->get('emailAddress');
     if (empty($emailAddress)) {
         return;
     }
     $email = $this->getEntityManager()->getEntity('Email');
     $email->set('to', $emailAddress);
     $subjectTpl = $this->getTemplate('InvitationSubject');
     $bodyTpl = $this->getTemplate('InvitationBody');
     $subject = $this->parseInvitationTemplate($subjectTpl, $entity, $invitee, $uid);
     $subject = str_replace(array("\n", "\r"), '', $subject);
     $body = $this->parseInvitationTemplate($bodyTpl, $entity, $invitee, $uid);
     $email->set('subject', $subject);
     $email->set('body', $body);
     $email->set('isHtml', true);
     $this->getEntityManager()->saveEntity($email);
     $attachmentName = ucwords($this->language->translate($entity->getEntityName(), 'scopeNames')) . '.ics';
     $attachment = $this->getEntityManager()->getEntity('Attachment');
     $attachment->set(array('name' => $attachmentName, 'type' => 'text/calendar', 'contents' => $this->getIscContents($entity)));
     $email->addAttachment($attachment);
     $emailSender = $this->mailSender;
     if ($this->smtpParams) {
         $emailSender->useSmtp($this->smtpParams);
     }
     $emailSender->send($email);
     $this->getEntityManager()->removeEntity($email);
 }
示例#10
0
 public function unfollowAllUsersFromEntity(Entity $entity)
 {
     if (empty($entity->id)) {
         return;
     }
     $pdo = $this->getEntityManager()->getPDO();
     $sql = "\n            DELETE FROM subscription\n            WHERE\n                entity_id = " . $pdo->quote($entity->id) . " AND entity_type = " . $pdo->quote($entity->getEntityName()) . "\n        ";
     $sth = $pdo->prepare($sql)->execute();
 }
示例#11
0
 public function afterSave(Entity $entity)
 {
     $entityName = $entity->getEntityName();
     if ($this->checkHasStream($entity)) {
         if (!$entity->isFetched()) {
             $assignedUserId = $entity->get('assignedUserId');
             $createdById = $entity->get('createdById');
             if (!empty($createdById)) {
                 $this->getStreamService()->followEntity($entity, $createdById);
             }
             if (!empty($assignedUserId) && $createdById != $assignedUserId) {
                 $this->getStreamService()->followEntity($entity, $assignedUserId);
             }
             $this->getStreamService()->noteCreate($entity);
         } else {
             if ($entity->isFieldChanged('assignedUserId')) {
                 $assignedUserId = $entity->get('assignedUserId');
                 if (!empty($assignedUserId)) {
                     $this->getStreamService()->followEntity($entity, $assignedUserId);
                     $this->getStreamService()->noteAssign($entity);
                 }
             }
             $this->getStreamService()->handleAudited($entity);
             if (array_key_exists($entityName, $this->statusDefs)) {
                 $field = $this->statusDefs[$entityName];
                 $value = $entity->get($field);
                 if (!empty($value) && $value != $entity->getFetched($field)) {
                     $this->getStreamService()->noteStatus($entity, $field);
                 }
             }
         }
     }
     if (!$entity->isFetched() && $this->getMetadata()->get("scopes.{$entityName}.tab")) {
         $this->handleCreateRelated($entity);
     }
 }