示例#1
0
 public function checkIsOwner(User $user, Entity $entity)
 {
     if ($user->id === $entity->get('createdById')) {
         return true;
     }
     return false;
 }
示例#2
0
 public function sendInvitations(Entity $entity)
 {
     $invitationManager = $this->getInvitationManager();
     $emailHash = array();
     $users = $entity->get('users');
     foreach ($users as $user) {
         if ($user->get('emailAddress') && !array_key_exists($user->get('emailAddress'), $emailHash)) {
             $invitationManager->sendInvitation($entity, $user, 'users');
             $emailHash[$user->get('emailAddress')] = true;
         }
     }
     $contacts = $entity->get('contacts');
     foreach ($contacts as $contact) {
         if ($contact->get('emailAddress') && !array_key_exists($contact->get('emailAddress'), $emailHash)) {
             $invitationManager->sendInvitation($entity, $contact, 'contacts');
             $emailHash[$user->get('emailAddress')] = true;
         }
     }
     $leads = $entity->get('leads');
     foreach ($leads as $lead) {
         if ($lead->get('emailAddress') && !array_key_exists($lead->get('emailAddress'), $emailHash)) {
             $invitationManager->sendInvitation($entity, $lead, 'leads');
             $emailHash[$user->get('emailAddress')] = true;
         }
     }
     return true;
 }
示例#3
0
文件: Lead.php 项目: sanduhrs/espocrm
 protected function getDuplicateWhereClause(Entity $entity)
 {
     $data = array('OR' => array(array('firstName' => $entity->get('firstName'), 'lastName' => $entity->get('lastName'))));
     if ($entity->get('emailAddress')) {
         $data['OR'][] = array('emailAddress' => $entity->get('emailAddress'));
     }
     return $data;
 }
示例#4
0
 protected function getDataFromEntity(Entity $entity)
 {
     $data = $entity->toArray();
     $fieldDefs = $entity->getFields();
     $fieldList = array_keys($fieldDefs);
     foreach ($fieldList as $field) {
         $type = null;
         if (!empty($fieldDefs[$field]['type'])) {
             $type = $fieldDefs[$field]['type'];
         }
         if ($type == Entity::DATETIME) {
             if (!empty($data[$field])) {
                 $data[$field] = $this->dateTime->convertSystemDateTime($data[$field]);
             }
         } else {
             if ($type == Entity::DATE) {
                 if (!empty($data[$field])) {
                     $data[$field] = $this->dateTime->convertSystemDate($data[$field]);
                 }
             } else {
                 if ($type == Entity::JSON_ARRAY) {
                     if (!empty($data[$field])) {
                         $list = $data[$field];
                         $newList = [];
                         foreach ($list as $item) {
                             $v = $item;
                             if ($item instanceof \StdClass) {
                                 $v = get_object_vars($v);
                             }
                             foreach ($v as $k => $w) {
                                 $v[$k] = $this->format($v[$k]);
                             }
                             $newList[] = $v;
                         }
                         $data[$field] = $newList;
                     }
                 } else {
                     if ($type == Entity::JSON_OBJECT) {
                         if (!empty($data[$field])) {
                             $value = $data[$field];
                             if ($value instanceof \StdClass) {
                                 $data[$field] = get_object_vars($value);
                             }
                             foreach ($data[$field] as $k => $w) {
                                 $data[$field][$k] = $this->format($data[$field][$k]);
                             }
                         }
                     }
                 }
             }
         }
         if (array_key_exists($field, $data)) {
             $data[$field] = $this->format($data[$field]);
         }
     }
     return $data;
 }
示例#5
0
 public function save(Entity $entity)
 {
     if ($entity->id) {
         $this->data[$entity->id] = $entity->toArray();
         $fileName = $this->getFilePath($entity->id);
         $this->getFileManager()->putContents($fileName, json_encode($this->data[$entity->id]));
         return $entity;
     }
 }
示例#6
0
 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);
     }
 }
示例#7
0
 protected function handleAfterSaveAccounts(Entity $entity, array $options)
 {
     $accountIdChanged = $entity->has('accountId') && $entity->get('accountId') != $entity->getFetched('accountId');
     $titleChanged = $entity->has('title') && $entity->get('title') != $entity->getFetched('title');
     if ($accountIdChanged) {
         $accountId = $entity->get('accountId');
         if (empty($accountId)) {
             $this->unrelate($entity, 'accounts', $entity->getFetched('accountId'));
             return;
         }
     }
     if ($titleChanged) {
         if (empty($accountId)) {
             $accountId = $entity->getFetched('accountId');
             if (empty($accountId)) {
                 return;
             }
         }
     }
     if ($accountIdChanged || $titleChanged) {
         $pdo = $this->getEntityManager()->getPDO();
         $sql = "\n                SELECT id, role FROM account_contact\n                WHERE\n                    account_id = " . $pdo->quote($accountId) . " AND\n                    contact_id = " . $pdo->quote($entity->id) . " AND\n                    deleted = 0\n            ";
         $sth = $pdo->prepare($sql);
         $sth->execute();
         if ($row = $sth->fetch()) {
             if ($titleChanged && $entity->get('title') != $row['role']) {
                 $this->updateRelation($entity, 'accounts', $accountId, array('role' => $entity->get('title')));
             }
         } else {
             if ($accountIdChanged) {
                 $this->relate($entity, 'accounts', $accountId, array('role' => $entity->get('title')));
             }
         }
     }
 }
 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);
         }
     }
 }
示例#9
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);
 }
示例#10
0
 public function save(Entity $entity, array $options = array())
 {
     $isNew = $entity->isNew();
     $result = parent::save($entity, $options);
     if ($isNew) {
         if (!empty($entity->id) && $entity->has('contents')) {
             $contents = $entity->get('contents');
             $this->getFileManager()->putContents('data/upload/' . $entity->id, $contents);
         }
     }
     return $result;
 }
示例#11
0
 public function checkIsOwner(User $user, Entity $entity)
 {
     if ($user->id === $entity->get('assignedUserId')) {
         return true;
     }
     if ($user->id === $entity->get('createdById')) {
         return true;
     }
     if ($entity->hasLinkMultipleId('assignedUsers', $user->id)) {
         return true;
     }
     return false;
 }
示例#12
0
 public function checkInTeam(User $user, Entity $entity)
 {
     if ($entity->has('campaignId')) {
         $campaignId = $entity->get('campaignId');
         if (!$campaignId) {
             return;
         }
         $campaign = $this->getEntityManager()->getEntity('Campaign', $campaignId);
         if ($campaign && $this->getAclManager()->getImplementation('Campaign')->checkInTeam($user, $campaign)) {
             return true;
         }
     }
     return;
 }
示例#13
0
 protected function afterRemove(Entity $entity, array $options = array())
 {
     if ($entity->get('fileId')) {
         $attachment = $this->getEntityManager()->getEntity('Attachment', $entity->get('fileId'));
         if ($attachment) {
             $this->getEntityManager()->removeEntity($attachment);
         }
     }
     $pdo = $this->getEntityManager()->getPDO();
     $sql = "DELETE FROM import_entity WHERE import_id = :importId";
     $sth = $pdo->prepare($sql);
     $sth->bindValue(':importId', $entity->id);
     $sth->execute();
     parent::afterRemove($entity, $options);
 }
示例#14
0
 public function checkIsOwner(User $user, Entity $entity)
 {
     if ($entity->has('parentId') && $entity->has('parentType')) {
         $parentType = $entity->get('parentType');
         $parentId = $entity->get('parentId');
         if (!$parentType || !$parentId) {
             return;
         }
         $parent = $this->getEntityManager()->getEntity($parentType, $parentId);
         if ($parent && $parent->has('assignedUserId') && $parent->get('assignedUserId') === $user->id) {
             return true;
         }
     }
     return;
 }
示例#15
0
 public function getEntityReminders(Entity $entity)
 {
     $pdo = $this->getEntityManager()->getPDO();
     $reminders = array();
     $sql = "\n            SELECT id, `seconds`, `type`\n            FROM `reminder`\n            WHERE\n                `entity_type` = " . $pdo->quote($entity->getEntityType()) . " AND\n                `entity_id` = " . $pdo->quote($entity->id) . " AND\n                `deleted` = 0\n                ORDER BY `seconds` ASC\n        ";
     $sth = $pdo->prepare($sql);
     $sth->execute();
     $rows = $sth->fetchAll(\PDO::FETCH_ASSOC);
     foreach ($rows as $row) {
         $o = new \StdClass();
         $o->seconds = intval($row['seconds']);
         $o->type = $row['type'];
         $reminders[] = $o;
     }
     return $reminders;
 }
示例#16
0
 public function sendInvitations(Entity $entity)
 {
     $invitationManager = $this->getInvitationManager();
     $users = $entity->get('users');
     foreach ($users as $user) {
         $invitationManager->sendInvitation($entity, $user, 'users');
     }
     $contacts = $entity->get('contacts');
     foreach ($contacts as $contact) {
         $invitationManager->sendInvitation($entity, $contact, 'contacts');
     }
     $leads = $entity->get('leads');
     foreach ($leads as $lead) {
         $invitationManager->sendInvitation($entity, $lead, 'leads');
     }
     return true;
 }
示例#17
0
 public function process(Entity $entity)
 {
     if ($entity->has('assignedUserId') && $entity->get('assignedUserId')) {
         $assignedUserId = $entity->get('assignedUserId');
         if ($assignedUserId != $this->getUser()->id && $entity->isFieldChanged('assignedUserId')) {
             $notification = $this->getEntityManager()->getEntity('Notification');
             $notification->set(array('type' => 'Assign', 'userId' => $assignedUserId, 'data' => array('entityType' => $entity->getEntityType(), 'entityId' => $entity->id, 'entityName' => $entity->get('name'), 'isNew' => $entity->isNew(), 'userId' => $this->getUser()->id, 'userName' => $this->getUser()->get('name'))));
             $this->getEntityManager()->saveEntity($notification);
         }
     }
 }
 protected function afterUnrelateCases(Entity $entity, $foreign)
 {
     $case = null;
     if ($foreign instanceof Entity) {
         $case = $foreign;
     } else {
         if (is_string($foreign)) {
             $case = $this->getEntityManager()->getEntity('Case', $foreign);
         }
     }
     if (!$case) {
         return;
     }
     $note = $this->getEntityManager()->getRepository('Note')->where(array('type' => 'Relate', 'parentId' => $case->id, 'parentType' => 'Case', 'relatedId' => $entity->id, 'relatedType' => $entity->getEntityType()))->findOne();
     if (!$note) {
         return;
     }
     $this->getEntityManager()->removeEntity($note);
 }
示例#19
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
     }
 }
示例#20
0
文件: User.php 项目: lucasmattos/crm
 protected function beforeSave(Entity $entity)
 {
     if ($entity->isNew()) {
         $userName = $entity->get('userName');
         if (empty($userName)) {
             throw new Error();
         }
         $user = $this->where(array('userName' => $userName))->findOne();
         if ($user) {
             throw new Error();
         }
     } else {
         if ($entity->isFieldChanged('userName')) {
             $userName = $entity->get('userName');
             if (empty($userName)) {
                 throw new Error();
             }
             $user = $this->where(array('userName' => $userName, 'id!=' => $entity->id))->findOne();
             if ($user) {
                 throw new Error();
             }
         }
     }
 }
示例#21
0
 public function afterSave(Entity $entity)
 {
     if (!$entity->isFetched()) {
         $parentType = $entity->get('parentType');
         $parentId = $entity->get('parentId');
         if ($parentType && $parentId) {
             $pdo = $this->getEntityManager()->getPDO();
             $sql = "\n\t\t\t\t\tSELECT user_id AS userId \n\t\t\t\t\tFROM subscription\n\t\t\t\t\tWHERE entity_id = " . $pdo->quote($parentId) . " AND entity_type = " . $pdo->quote($parentType);
             $sth = $pdo->prepare($sql);
             $sth->execute();
             $userIdList = array();
             while ($row = $sth->fetch(\PDO::FETCH_ASSOC)) {
                 if ($this->getUser()->id != $row['userId']) {
                     $userIdList[] = $row['userId'];
                 }
             }
             if (!empty($userIdList)) {
                 $job = $this->getEntityManager()->getEntity('Job');
                 $job->set(array('serviceName' => 'Notification', 'method' => 'notifyAboutNoteFromJob', 'data' => json_encode(array('userIdList' => $userIdList, 'noteId' => $entity->id)), 'executeTime' => date('Y-m-d H:i:s')));
                 $this->getEntityManager()->saveEntity($job);
             }
         }
     }
 }
示例#22
0
 protected function getIscContents(Entity $entity)
 {
     $user = $entity->get('assignedUser');
     $who = '';
     $email = '';
     if ($user) {
         $who = $user->get('name');
         $email = $user->get('emailAddress');
     }
     $ics = new Ics('//EspoCRM//EspoCRM Calendar//EN', array('startDate' => strtotime($entity->get('dateStart')), 'endDate' => strtotime($entity->get('dateEnd')), 'uid' => $entity->id, 'summary' => $entity->get('name'), 'who' => $who, 'email' => $email, 'description' => $entity->get('description')));
     return $ics->get();
 }
示例#23
0
 public function afterSave(Entity $entity)
 {
     if ($entity->isNew()) {
         $parentType = $entity->get('parentType');
         $parentId = $entity->get('parentId');
         $superParentType = $entity->get('superParentType');
         $superParentTypeId = $entity->get('superParentTypeId');
         $userIdList = [];
         if ($parentType && $parentId) {
             $userIdList = array_merge($userIdList, $this->getSubscriberIdList($parentType, $parentId));
         }
         if ($superParentType && $superParentTypeId) {
             $userIdList = array_merge($userIdList, $this->getSubscriberIdList($superParentType, $superParentTypeId));
         }
         //$userIdList = array_merge($userIdList, $this->getMentionedUserIdList($entity));
         $userIdList = array_unique($userIdList);
         if (!empty($userIdList)) {
             $this->getNotificationService()->notifyAboutNote($userIdList, $entity);
         }
     }
 }
示例#24
0
 protected function handleAfterSaveContacts(Entity $entity, array $options)
 {
     $contactIdChanged = $entity->has('contactId') && $entity->get('contactId') != $entity->getFetched('contactId');
     if ($contactIdChanged) {
         $contactId = $entity->get('contactId');
         if (empty($contactId)) {
             $this->unrelate($entity, 'contacts', $entity->getFetched('contactId'));
             return;
         }
     }
     if ($contactIdChanged) {
         $pdo = $this->getEntityManager()->getPDO();
         $sql = "\n                SELECT id FROM case_contact\n                WHERE\n                    contact_id = " . $pdo->quote($contactId) . " AND\n                    case_id = " . $pdo->quote($entity->id) . " AND\n                    deleted = 0\n            ";
         $sth = $pdo->prepare($sql);
         $sth->execute();
         if (!$sth->fetch()) {
             $this->relate($entity, 'contacts', $contactId);
         }
     }
 }
示例#25
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);
 }
示例#26
0
 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');
     }
 }
示例#27
0
 public function prepareEntityForOutput(Entity $entity)
 {
     foreach ($this->internalFields as $field) {
         $entity->clear($field);
     }
 }
示例#28
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';
             $columnsFieldsName = $name . 'Columns';
             if ($entity->has($fieldName) || $entity->has($columnsFieldsName)) {
                 if ($this->getMetadata()->get("entityDefs." . $entity->getEntityType() . ".fields.{$name}.noSave")) {
                     continue;
                 }
                 if ($entity->has($fieldName)) {
                     $specifiedIds = $entity->get($fieldName);
                 } else {
                     $specifiedIds = array();
                     foreach ($entity->get($columnsFieldsName) as $id => $d) {
                         $specifiedIds[] = $id;
                     }
                 }
                 if (is_array($specifiedIds)) {
                     $toRemoveIds = array();
                     $existingIds = array();
                     $toUpdateIds = array();
                     $existingColumnsData = new \stdClass();
                     $defs = array();
                     $columns = $this->getMetadata()->get("entityDefs." . $entity->getEntityType() . ".fields.{$name}.columns");
                     if (!empty($columns)) {
                         $columnData = $entity->get($columnsFieldsName);
                         $defs['additionalColumns'] = $columns;
                     }
                     $foreignCollection = $entity->get($name, $defs);
                     if ($foreignCollection) {
                         foreach ($foreignCollection 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;
                                 $entity->setFetched($columnsFieldsName, $existingColumnsData);
                             }
                         }
                     }
                     if ($entity->has($fieldName)) {
                         $entity->setFetched($fieldName, $existingIds);
                     }
                     if ($entity->has($columnsFieldsName) && !empty($columns)) {
                         $entity->setFetched($columnsFieldsName, $existingColumnsData);
                     }
                     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) && isset($columnData->{$id})) {
                                 $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);
                         }
                     }
                 }
             }
         }
     }
 }
示例#29
0
 protected function getDuplicateWhereClause(Entity $entity)
 {
     return array('name' => $entity->get('name'));
 }
示例#30
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;
 }