/** * Try to find this person in the database. * If we cannot find them, create a new person record. * * @return Person */ public static function findPerson($post) { $search = array(); // Translates Open311 parameters into PersonList search parameters // open311 => personList $fields = array('first_name' => 'firstname', 'last_name' => 'lastname', 'email' => 'email', 'phone' => 'phoneNumber', 'device_id' => 'phoneDeviceId'); foreach ($fields as $open311Field => $crmField) { if (!empty($post[$open311Field])) { $search[$crmField] = $post[$open311Field]; } } // If the user provided any personal info, do a person search if (count($search)) { $table = new PersonTable(); $list = $table->find($search); // When we find one and only one record, use the record we found if (count($list) == 1) { $person = $list->current(); } else { $p = array(); foreach ($fields as $key => $field) { if (!empty($post[$key])) { $p[$field] = $post[$key]; } } if (count($p)) { $person = new Person(); try { $person->handleUpdate($p); $person->save(); if (!empty($post['email'])) { $email = new Email(); $email->setPerson($person); $email->setEmail($post['email']); $email->save(); } if (!empty($post['phone']) || !empty($post['device_id'])) { $phone = new Phone(); $phone->setPerson($person); if (!empty($post['phone'])) { $phone->setNumber($post['phone']); } if (!empty($post['device_id'])) { $phone->setDeviceId($post['device_id']); } $phone->save(); } } catch (\Exception $e) { unset($person); } } } } return isset($person) ? $person : null; }