- //get existsing block ids.
- $blockIds = self::getBlockIds($blockName, $contactId, $entityElements, $updateBlankLocInfo);
-
- if (!$updateBlankLocInfo) {
- $resetPrimaryId = NULL;
- $primaryId = FALSE;
- foreach ($params[$blockName] as $count => $value) {
- $blockId = CRM_Utils_Array::value('id', $value);
- if ($blockId) {
- if (is_array($blockIds)
- && array_key_exists($blockId, $blockIds)
- ) {
- unset($blockIds[$blockId]);
- }
- else {
- unset($value['id']);
- }
- }
- //lets allow to update primary w/ more cleanly.
- if (!$resetPrimaryId && !empty($value['is_primary'])) {
- $primaryId = TRUE;
- if (is_array($blockIds)) {
- foreach ($blockIds as $blockId => $blockValue) {
- if (!empty($blockValue['is_primary'])) {
- $resetPrimaryId = $blockId;
- break;
- }
- }
- }
- if ($resetPrimaryId) {
- $baoString = 'CRM_Core_BAO_' . $blockName;
- $block = new $baoString( );
+ // For each submitted value
+ foreach ($submittedValues as $count => $submittedValue) {
+
+ // Set the contact ID
+ $submittedValue['contact_id'] = $contactId;
+
+ // If this is a primary value, and we haven't unset a primary value yet, and there are values on the contact
+ // Then unset any primary value currently on the Contact
+ if (!empty($submittedValue['is_primary']) && !$resetPrimary && is_array($existingValues)) {
+ foreach ($existingValues as $existingValueId => $existingValue) {
+ if (!empty($existingValue['is_primary'])) {
+
+ // @todo Can we refactor this?
+ $block = new $baoString();