/**
  * @param $id
  * @return PersonEntity|null
  */
 public static function getValidPersonWithId($id)
 {
     $org = Apollo::getInstance()->getUser()->getOrganisation();
     $people = Person::getRepository();
     $person = $people->find($id);
     if (!empty($person) && !$person->isHidden() && $person->getOrganisation() == $org) {
         return $person;
     } else {
         return null;
     }
 }
 /**
  * Given a record, this returns the record as a "row"
  * @param RecordEntity $record
  * @return array|null
  */
 private function getRecordDataAsArray($record)
 {
     try {
         $name = Person::getNameAsArray($record->getPerson());
         $essential = Record::getFormattedFields($record, true);
         $non_essential = Record::getFormattedFields($record, false);
         $activityNames = Person::getActivityNames($record->getPerson());
         $formattedNames = Data::concatMultiple($activityNames);
         $strings = $name;
         $strings = array_merge($strings, Data::formattedDataArrayToString($essential));
         $strings = array_merge($strings, Data::formattedDataArrayToString($non_essential));
         $strings = array_merge($strings, [$formattedNames]);
         return $strings;
     } catch (Exception $e) {
         return null;
     }
 }
 /**
  * Returns an object containing the information about a record
  * @param RecordEntity $record
  * @return array
  * @since 0.0.9
  */
 public static function getFormattedData($record)
 {
     /**
      * @var RecordEntity[] $other_records
      */
     $person = $record->getPerson();
     $other_records = self::getValidRecordsOfPerson($person->getId());
     $id_array = [];
     $name_array = [];
     foreach ($other_records as $other_record) {
         if ($other_record->getId() != intval($_GET['id'])) {
             $id_array[] = $other_record->getId();
             $name_array[] = $other_record->findVarchar(FIELD_RECORD_NAME);
         }
     }
     return ["given_name" => $person->getGivenName(), "middle_name" => $person->getMiddleName(), "last_name" => $person->getLastName(), "email" => $record->findVarchar(FIELD_EMAIL), "address" => $record->findMultiple(FIELD_ADDRESS), "phone" => $record->findVarchar(FIELD_PHONE), "awards" => $record->findMultiple(FIELD_AWARDS), "publications" => $record->findMultiple(FIELD_PUBLICATIONS), "start_date" => $record->findDateTime(FIELD_START_DATE)->format('Y-m-d H:i:s'), "end_date" => $record->findDateTime(FIELD_END_DATE)->format('Y-m-d H:i:s'), "person_id" => $person->getId(), "record_id" => $record->getId(), "record_name" => $record->findVarchar(FIELD_RECORD_NAME), "record_ids" => $id_array, "record_names" => $name_array, "activities" => Person::getFormattedActivitiesOfPerson($person)];
 }
 /**
  * Formats an activity as a valid JSON object (with all the information about the activity)
  * @param ActivityEntity $activity
  * @return array
  */
 public static function getFormattedData(ActivityEntity $activity)
 {
     $people = Person::getFormattedPeopleShortWithRecords($activity->getPeople());
     $activityInfo = ['error' => null, 'id' => $activity->getId(), 'name' => $activity->getName(), 'target_groups' => TargetGroup::getFormattedTargetGroups($activity->getTargetGroup()), 'target_group_comment' => $activity->getTargetGroupComment(), 'start_date' => $activity->getStartDate()->format('Y-m-d H:i:s'), 'end_date' => $activity->getEndDate()->format('Y-m-d H:i:s'), 'participants' => $people];
     return $activityInfo;
 }
 /**
  * Just accepts new people, adds them/deletes them from the activity
  * @return mixed
  */
 public function actionActivitySavePeople()
 {
     $response['error'] = null;
     $data = $this->parseRequest(['activity_id' => null, 'toAdd' => null, 'toDelete' => null]);
     if (!$this->areFieldsEmpty($data)) {
         $activity = Activity::getValidActivityWithId($data['activity_id']);
         if ($activity) {
             foreach ($data['toAdd'] as $person_id) {
                 $person = Person::getValidPersonWithId($person_id);
                 if ($person) {
                     $activity->addPerson($person);
                 }
             }
             foreach ($data['toDelete'] as $person_id) {
                 $person = Person::getValidPersonWithId($person_id);
                 if ($person) {
                     $activity->removePerson($person);
                 }
             }
             try {
                 $this->writeActivityToDB($activity);
             } catch (Exception $e) {
                 $response['error'] = $this->getJSONError(4, 'Unexpected exception when saving the new data. Message: ' . $e->getMessage());
             }
         } else {
             $response['error'] = $this->getJSONError(3, 'Could not find activity with given id while saving people');
         }
     } else {
         $response['error'] = $this->getJSONError(1, 'Some of the fields are empty');
     }
     return $response;
 }
 /**
  * @return QueryBuilder
  */
 private function getQueryValidPeople()
 {
     $em = DB::getEntityManager();
     $peopleRepo = $em->getRepository(Person::getEntityNamespace());
     $organisation_id = Apollo::getInstance()->getUser()->getOrganisationId();
     $pqb = $peopleRepo->createQueryBuilder('p');
     $pqb->where($pqb->expr()->andX($pqb->expr()->eq('p.organisation', $organisation_id), $pqb->expr()->eq('p.is_hidden', '0')));
     return $pqb;
 }