Example #1
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);
         }
     }
 }
Example #2
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;
 }
Example #3
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);
         }
     }
 }
Example #4
0
 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;
 }
Example #5
0
 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;
 }
Example #6
0
 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()->getFoxMetadata()->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);
             }
         }
     }
 }
Example #7
0
 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                            DELETE FROM entity_email_address\n                            WHERE\n                                entity_id = " . $pdo->quote($entity->id) . " AND\n                                entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n                                email_address_id = " . $pdo->quote($emailAddress->id) . "\n                        ";
                 $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                        INSERT entity_email_address\n                            (entity_id, entity_type, email_address_id, `primary`)\n                            VALUES\n                            (\n                                " . $pdo->quote($entity->id) . ",\n                                " . $pdo->quote($entity->getEntityName()) . ",\n                                " . $pdo->quote($emailAddress->id) . ",\n                                " . $pdo->quote((int) ($address === $primary)) . "\n                            )\n                    ";
             $sth = $pdo->prepare($query);
             $sth->execute();
         }
         if ($primary) {
             $emailAddress = $this->getByAddress($primary);
             if ($emailAddress) {
                 $query = "\n                            UPDATE entity_email_address\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_email_address\n                            SET `primary` = 1\n                            WHERE\n                                entity_id = " . $pdo->quote($entity->id) . " AND\n                                entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n                                email_address_id = " . $pdo->quote($emailAddress->id) . " AND\n                                deleted = 0\n                        ";
                 $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                            UPDATE entity_email_address\n                            SET `primary` = 1\n                            WHERE\n                                entity_id = " . $pdo->quote($entity->id) . " AND\n                                entity_type = " . $pdo->quote($entity->getEntityName()) . " AND\n                                email_address_id = " . $pdo->quote($emailAddressNew->id) . "\n                        ";
                 $sth = $pdo->prepare($query);
                 $sth->execute();
             }
         } else {
             $emailOld = $entity->getFetched('emailAddress');
             if (!empty($emailOld)) {
                 $emailAddressOld = $this->getByAddress($emailOld);
                 $entityRepository->unrelate($entity, 'emailAddresses', $emailAddressOld);
             }
         }
     }
 }
Example #8
0
 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;
     }
 }