/**
* Creates a new contact for the given data.
*
* @param array $data
* @param int|null $id
* @param bool $patch
* @param bool $flush
*
* @return Contact
*
* @throws EntityNotFoundException
*/
public function save($data, $id = null, $patch = false, $flush = true)
{
/*
* TODO: https://github.com/sulu-io/sulu/pull/1171
* This method needs to be refactored since in the first
* iteration the logic was just moved from the Controller to this class due
* to better reusability.
*/
$firstName = $this->getProperty($data, 'firstName');
$lastName = $this->getProperty($data, 'lastName');
$avatar = $this->getProperty($data, 'avatar');
if ($id) {
/** @var Contact $contact */
$contact = $this->contactRepository->findById($id);
if (!$contact) {
throw new EntityNotFoundException($this->contactRepository->getClassName(), $id);
}
if (!$patch || $this->getProperty($data, 'account')) {
$this->setMainAccount($contact, $data);
}
if (!$patch || $this->getProperty($data, 'emails')) {
$this->processEmails($contact, $this->getProperty($data, 'emails', []));
}
if (!$patch || $this->getProperty($data, 'phones')) {
$this->processPhones($contact, $this->getProperty($data, 'phones', []));
}
if (!$patch || $this->getProperty($data, 'addresses')) {
$this->processAddresses($contact, $this->getProperty($data, 'addresses', []));
}
if (!$patch || $this->getProperty($data, 'notes')) {
$this->processNotes($contact, $this->getProperty($data, 'notes', []));
}
if (!$patch || $this->getProperty($data, 'faxes')) {
$this->processFaxes($contact, $this->getProperty($data, 'faxes', []));
}
if (!$patch || $this->getProperty($data, 'tags')) {
$this->processTags($contact, $this->getProperty($data, 'tags', []));
}
if (!$patch || $this->getProperty($data, 'urls')) {
$this->processUrls($contact, $this->getProperty($data, 'urls', []));
}
if (!$patch || $this->getProperty($data, 'categories')) {
$this->processCategories($contact, $this->getProperty($data, 'categories', []));
}
if (!$patch || $this->getProperty($data, 'bankAccounts')) {
$this->processBankAccounts($contact, $this->getProperty($data, 'bankAccounts', []));
}
} else {
$contact = $this->contactRepository->createNew();
}
if (!$patch || $firstName !== null) {
$contact->setFirstName($firstName);
}
if (!$patch || $lastName !== null) {
$contact->setLastName($lastName);
}
if (!$patch || $avatar !== null) {
$this->setAvatar($contact, $avatar);
}
// Set title relation on contact
$this->setTitleOnContact($contact, $this->getProperty($data, 'title'));
$formOfAddress = $this->getProperty($data, 'formOfAddress');
if (!is_null($formOfAddress) && is_array($formOfAddress) && array_key_exists('id', $formOfAddress)) {
$contact->setFormOfAddress($formOfAddress['id']);
}
$salutation = $this->getProperty($data, 'salutation');
if (!empty($salutation)) {
$contact->setSalutation($salutation);
}
$birthday = $this->getProperty($data, 'birthday');
if (!empty($birthday)) {
$contact->setBirthday(new DateTime($birthday));
}
if (!$id) {
$parentData = $this->getProperty($data, 'account');
if ($parentData != null && $parentData['id'] != null && $parentData['id'] != 'null' && $parentData['id'] != '') {
/** @var AccountInterface $parent */
$parent = $this->accountRepository->findAccountById($parentData['id']);
if (!$parent) {
throw new EntityNotFoundException(self::$accountContactEntityName, $parentData['id']);
}
// Set position on contact
$position = $this->getPosition($this->getProperty($data, 'position'));
// create new account-contact relation
$this->createMainAccountContact($contact, $parent, $position);
}
// add urls, phones, emails, tags, bankAccounts, notes, addresses,..
$this->addNewContactRelations($contact, $data);
$this->processCategories($contact, $this->getProperty($data, 'categories', []));
}
$this->em->persist($contact);
if ($flush) {
$this->em->flush();
}
return $contact;
}