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); } } }
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; }
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); } } }
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(); } } } }
public function isPermittedTeams(Entity $entity) { $assignmentPermission = $this->getAcl()->get('assignmentPermission'); if (empty($assignmentPermission) || $assignmentPermission === true || !in_array($assignmentPermission, ['team', 'no'])) { return true; } if (!$entity->hasField('teamsIds')) { return true; } $teamIds = $entity->get('teamsIds'); if (empty($teamIds)) { return true; } $newIds = []; if (!$entity->isNew()) { $existingIds = []; foreach ($entity->get('teams') as $team) { $existingIds[] = $team->id; } foreach ($teamIds as $id) { if (!in_array($id, $existingIds)) { $newIds[] = $id; } } } else { $newIds = $teamIds; } if (empty($newIds)) { return true; } $userTeamIds = $this->getUser()->get('teamsIds'); foreach ($newIds as $id) { if (!in_array($id, $userTeamIds)) { return false; } } return true; }
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; }
public function storeEntityEmailAddress(Entity $entity) { $email = trim($entity->get('emailAddress')); $emailAddressData = null; if ($entity->has('emailAddressData')) { $emailAddressData = $entity->get('emailAddressData'); } $pdo = $this->getEntityManager()->getPDO(); if ($emailAddressData !== null && is_array($emailAddressData)) { $previousEmailAddressData = array(); if (!$entity->isNew()) { $previousEmailAddressData = $this->getEmailAddressData($entity); } $hash = array(); foreach ($emailAddressData as $row) { $key = $row->emailAddress; if (!empty($key)) { $hash[$key] = array('primary' => $row->primary ? true : false, 'optOut' => $row->optOut ? true : false, 'invalid' => $row->invalid ? true : false); } } $hashPrev = array(); foreach ($previousEmailAddressData as $row) { $key = $row->emailAddress; if (!empty($key)) { $hashPrev[$key] = array('primary' => $row->primary ? true : false, 'optOut' => $row->optOut ? true : false, 'invalid' => $row->invalid ? true : false); } } $primary = false; $toCreate = array(); $toUpdate = array(); $toRemove = array(); foreach ($hash as $key => $data) { $new = true; $changed = false; if ($hash[$key]['primary']) { $primary = $key; } if (array_key_exists($key, $hashPrev)) { $new = false; $changed = $hash[$key]['optOut'] != $hashPrev[$key]['optOut'] || $hash[$key]['invalid'] != $hashPrev[$key]['invalid']; if ($hash[$key]['primary']) { if ($hash[$key]['primary'] == $hashPrev[$key]['primary']) { $primary = false; } } } if ($new) { $toCreate[] = $key; } if ($changed) { $toUpdate[] = $key; } } foreach ($hashPrev as $key => $data) { if (!array_key_exists($key, $hash)) { $toRemove[] = $key; } } foreach ($toRemove as $address) { $emailAddress = $this->getByAddress($address); if ($emailAddress) { $query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `deleted` = 1, `primary` = 0\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\temail_address_id = " . $pdo->quote($emailAddress->id) . "\n\t\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); } } foreach ($toUpdate as $address) { $emailAddress = $this->getByAddress($address); if ($emailAddress) { $emailAddress->set(array('optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid'])); $this->save($emailAddress); } } foreach ($toCreate as $address) { $emailAddress = $this->getByAddress($address); if (!$emailAddress) { $emailAddress = $this->get(); $emailAddress->set(array('name' => $address, 'optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid'])); $this->save($emailAddress); } else { if ($emailAddress->get('optOut') != $hash[$address]['optOut'] || $emailAddress->get('invalid') != $hash[$address]['invalid']) { $emailAddress->set(array('optOut' => $hash[$address]['optOut'], 'invalid' => $hash[$address]['invalid'])); $this->save($emailAddress); } } $query = "\n\t\t\t\t\t\tINSERT entity_email_address \n\t\t\t\t\t\t\t(entity_id, entity_type, email_address_id, `primary`)\n\t\t\t\t\t\t\tVALUES\n\t\t\t\t\t\t\t(\n\t\t\t\t\t\t\t\t" . $pdo->quote($entity->id) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($entity->getEntityName()) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($emailAddress->id) . ",\n\t\t\t\t\t\t\t\t" . $pdo->quote($address === $primary) . "\n\t\t\t\t\t\t\t)\n\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); } if ($primary) { $emailAddress = $this->getByAddress($primary); if ($emailAddress) { $query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `primary` = 0\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\t`primary` = 1 AND \n\t\t\t\t\t\t\t\tdeleted = 0\n\t\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); $query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `primary` = 1\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\temail_address_id = " . $pdo->quote($emailAddress->id) . " AND \n\t\t\t\t\t\t\t\tdeleted = 0\n\t\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); } } } else { $entityRepository = $this->getEntityManager()->getRepository($entity->getEntityName()); if (!empty($email)) { if ($email != $entity->getFetched('emailAddress')) { $emailAddressNew = $this->where(array('lower' => strtolower($email)))->findOne(); $isNewEmailAddress = false; if (!$emailAddressNew) { $emailAddressNew = $this->get(); $emailAddressNew->set('name', $email); $this->save($emailAddressNew); $isNewEmailAddress = true; } $emailOld = $entity->getFetched('emailAddress'); if (!empty($emailOld)) { $emailAddressOld = $this->getByAddress($emailOld); $entityRepository->unrelate($entity, 'emailAddresses', $emailAddressOld); } $entityRepository->relate($entity, 'emailAddresses', $emailAddressNew); $query = "\n\t\t\t\t\t\t\tUPDATE entity_email_address\n\t\t\t\t\t\t\tSET `primary` = 1\n\t\t\t\t\t\t\tWHERE\n\t\t\t\t\t\t\t\tentity_id = " . $pdo->quote($entity->id) . " AND\n\t\t\t\t\t\t\t\tentity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n\t\t\t\t\t\t\t\temail_address_id = " . $pdo->quote($emailAddressNew->id) . "\n\t\t\t\t\t\t"; $sth = $pdo->prepare($query); $sth->execute(); } } else { $emailOld = $entity->getFetched('emailAddress'); if (!empty($emailOld)) { $emailAddressOld = $this->getByAddress($emailOld); $entityRepository->unrelate($entity, 'emailAddresses', $emailAddressOld); } } } }
public function findRelated(Entity $entity, $relationName, array $params = array()) { if ($entity->isNew()) { return; } $entityName = $entity->relations[$relationName]['entity']; $this->getEntityManager()->getRepository($entityName)->handleSelectParams($params); $result = $this->getMapper()->selectRelated($entity, $relationName, $params); if (is_array($result)) { $collection = new EntityCollection($result, $entityName, $this->entityFactory); return $collection; } else { return $result; } }
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; }
public function checkAssignment(Entity $entity) { if ($entity->isNew()) { $targetType = $entity->get('targetType'); if ($targetType) { $assignmentPermission = $this->getAcl()->get('assignmentPermission'); if ($assignmentPermission === false || $assignmentPermission === 'no') { if ($targetType !== 'self') { throw new Forbidden('Not permitted to post to anybody except self.'); } } if ($targetType === 'teams') { $teamIdList = $entity->get('teamsIds'); if (empty($teamIdList) || !is_array($teamIdList)) { throw new BadRequest(); } } if ($targetType === 'users') { $userIdList = $entity->get('usersIds'); if (empty($userIdList) || !is_array($userIdList)) { throw new BadRequest(); } } if ($assignmentPermission === 'team') { if ($targetType === 'all') { throw new Forbidden('Not permitted to post to all.'); } $userTeamIdList = $this->getUser()->getTeamIdList(); if ($targetType === 'teams') { if (empty($userTeamIdList)) { throw new Forbidden('Not permitted to post to foreign teams.'); } foreach ($teamIdList as $teamId) { if (!in_array($teamId, $userTeamIdList)) { throw new Forbidden('Not permitted to post to foreign teams.'); } } } else { if ($targetType === 'users') { if (empty($userTeamIdList)) { throw new Forbidden('Not permitted to post to users from foreign teams.'); } foreach ($userIdList as $userId) { if ($userId === $this->getUser()->id) { continue; } if (!$this->getEntityManager()->getRepository('User')->checkBelongsToAnyOfTeams($userId, $userTeamIdList)) { throw new Forbidden('Not permitted to post to users from foreign teams.'); } } } } } } } return true; }
public function storeEntityPhoneNumber(Entity $entity) { $phone = trim($entity->get('phoneNumber')); $phoneNumberData = null; if ($entity->has('phoneNumberData')) { $phoneNumberData = $entity->get('phoneNumberData'); } $pdo = $this->getEntityManager()->getPDO(); if ($phoneNumberData !== null && is_array($phoneNumberData)) { $previousPhoneNumberData = array(); if (!$entity->isNew()) { $previousPhoneNumberData = $this->getPhoneNumberData($entity); } $hash = array(); foreach ($phoneNumberData as $row) { $key = $row->phoneNumber; if (!empty($key)) { $hash[$key] = array('primary' => $row->primary ? true : false, 'type' => $row->type); } } $hashPrev = array(); foreach ($previousPhoneNumberData as $row) { $key = $row->phoneNumber; if (!empty($key)) { $hashPrev[$key] = array('primary' => $row->primary ? true : false, 'type' => $row->type); } } $primary = false; $toCreate = array(); $toUpdate = array(); $toRemove = array(); foreach ($hash as $key => $data) { $new = true; $changed = false; if ($hash[$key]['primary']) { $primary = $key; } if (array_key_exists($key, $hashPrev)) { $new = false; $changed = $hash[$key]['type'] != $hashPrev[$key]['type']; if ($hash[$key]['primary']) { if ($hash[$key]['primary'] == $hashPrev[$key]['primary']) { $primary = false; } } } if ($new) { $toCreate[] = $key; } if ($changed) { $toUpdate[] = $key; } } foreach ($hashPrev as $key => $data) { if (!array_key_exists($key, $hash)) { $toRemove[] = $key; } } foreach ($toRemove as $number) { $phoneNumber = $this->getByNumber($number); if ($phoneNumber) { $query = "\n DELETE FROM entity_phone_number\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n phone_number_id = " . $pdo->quote($phoneNumber->id) . "\n "; $sth = $pdo->prepare($query); $sth->execute(); } } foreach ($toUpdate as $number) { $phoneNumber = $this->getByNumber($number); if ($phoneNumber) { $phoneNumber->set(array('type' => $hash[$number]['type'])); $this->save($phoneNumber); } } foreach ($toCreate as $number) { $phoneNumber = $this->getByNumber($number); if (!$phoneNumber) { $phoneNumber = $this->get(); $phoneNumber->set(array('name' => $number, 'type' => $hash[$number]['type'])); $this->save($phoneNumber); } else { if ($phoneNumber->get('type') != $hash[$number]['type']) { $phoneNumber->set(array('type' => $hash[$number]['type'])); $this->save($phoneNumber); } } $query = "\n INSERT entity_phone_number\n (entity_id, entity_type, phone_number_id, `primary`)\n VALUES\n (\n " . $pdo->quote($entity->id) . ",\n " . $pdo->quote($entity->getEntityName()) . ",\n " . $pdo->quote($phoneNumber->id) . ",\n " . $pdo->quote((int) ($number === $primary)) . "\n )\n "; $sth = $pdo->prepare($query); $sth->execute(); } if ($primary) { $phoneNumber = $this->getByNumber($primary); if ($phoneNumber) { $query = "\n UPDATE entity_phone_number\n SET `primary` = 0\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n `primary` = 1 AND\n deleted = 0\n "; $sth = $pdo->prepare($query); $sth->execute(); $query = "\n UPDATE entity_phone_number\n SET `primary` = 1\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n phone_number_id = " . $pdo->quote($phoneNumber->id) . " AND \n deleted = 0\n "; $sth = $pdo->prepare($query); $sth->execute(); } } } else { $entityRepository = $this->getEntityManager()->getRepository($entity->getEntityName()); if (!empty($phone)) { if ($phone != $entity->getFetched('phoneNumber')) { $phoneNumberNew = $this->where(array('name' => $phone))->findOne(); $isNewPhoneNumber = false; if (!$phoneNumberNew) { $phoneNumberNew = $this->get(); $phoneNumberNew->set('name', $phone); $defaultType = $this->getEntityManager()->getEspoMetadata()->get('entityDefs.' . $entity->getEntityName() . '.fields.phoneNumber.defaultType'); $phoneNumberNew->set('type', $defaultType); $this->save($phoneNumberNew); $isNewPhoneNumber = true; } $phoneOld = $entity->getFetched('phoneNumber'); if (!empty($phoneOld)) { $phoneNumberOld = $this->getByNumber($phoneOld); $entityRepository->unrelate($entity, 'phoneNumbers', $phoneNumberOld); } $entityRepository->relate($entity, 'phoneNumbers', $phoneNumberNew); $query = "\n UPDATE entity_phone_number\n SET `primary` = 1\n WHERE\n entity_id = " . $pdo->quote($entity->id) . " AND\n entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n phone_number_id = " . $pdo->quote($phoneNumberNew->id) . "\n "; $sth = $pdo->prepare($query); $sth->execute(); } } else { $phoneOld = $entity->getFetched('phoneNumber'); if (!empty($phoneOld)) { $phoneNumberOld = $this->getByNumber($phoneOld); $entityRepository->unrelate($entity, 'phoneNumbers', $phoneNumberOld); } } } }
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)); } } else { $targetType = $entity->get('targetType'); if ($targetType === 'users') { $targetUserIdList = $entity->get('usersIds'); foreach ($targetUserIdList as $userId) { if ($userId === $this->getUser()->id) { continue; } $userIdList[] = $userId; } } else { if ($targetType === 'teams') { $targetTeamIdList = $entity->get('teamsIds'); foreach ($targetTeamIdList as $teamId) { $team = $this->getEntityManager()->getEntity('Team', $teamId); if (!$team) { continue; } $targetUserList = $this->getEntityManager()->getRepository('Team')->findRelated($team, 'users', array('whereClause' => array('isActive' => true))); foreach ($targetUserList as $user) { if ($user->id === $this->getUser()->id) { continue; } $userIdList[] = $user->id; } } } else { if ($targetType === 'all') { $targetUserList = $this->getEntityManager()->getRepository('User')->find(array('whereClause' => array('isActive' => true))); foreach ($targetUserList as $user) { if ($user->id === $this->getUser()->id) { continue; } $userIdList[] = $user->id; } } } } } $userIdList = array_unique($userIdList); foreach ($userIdList as $i => $userId) { if ($entity->isUserIdNotified($userId)) { unset($userIdList[$i]); } } $userIdList = array_values($userIdList); if (!empty($userIdList)) { $this->getNotificationService()->notifyAboutNote($userIdList, $entity); } } }