/** * takes an associative array and creates a contact object * * the function extract all the params it needs to initialize the create a * contact object. the params array could contain additional unused name/value * pairs * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * @param array $locationId * * @return object CRM_Core_BAO_Location object on success, null otherwise * @access public * @static */ function add(&$params, &$ids, $locationId) { if (!CRM_Core_BAO_Location::dataExists($params, $locationId, $ids)) { return null; } $location =& new CRM_Core_BAO_Location(); if (!isset($params['contact_id'])) { require_once 'CRM/Core/BAO/Domain.php'; $location->entity_table = CRM_Core_BAO_Domain::getTableName(); $location->entity_id = $params['domain_id']; } else { $location->entity_table = CRM_Contact_BAO_Contact::getTableName(); $location->entity_id = $params['contact_id']; } $location->location_type_id = CRM_Utils_Array::value('location_type_id', $params['location'][$locationId]); $location->name = CRM_Utils_Array::value('name', $params['location'][$locationId]); $location->is_primary = CRM_Utils_Array::value('is_primary', $params['location'][$locationId], false); // check if there exists another location has is_primary set, and if so reset that // if no location has is_primary, make this one is_primart if ($location->is_primary) { // reset all other locations with the same entity table entity id $sql = "UPDATE " . CRM_Core_BAO_Location::getTableName() . "\n SET is_primary = 0 WHERE \n entity_table = '{$location->entity_table}' AND\n entity_id = '{$location->entity_id}' "; CRM_Core_DAO::executeQuery($sql); } else { // make sure there is at once location with is_primary set $sql = "SELECT count( " . CRM_Core_BAO_Location::getTableName() . ".id )\n FROM " . CRM_Core_BAO_Location::getTableName() . " WHERE\n entity_table = '{$location->entity_table}' AND\n entity_id = '{$location->entity_id}' AND\n is_primary = 1"; $count = CRM_Core_DAO::singleValueQuery($sql); if ($count == 0) { $location->is_primary = true; } } $location->id = CRM_Utils_Array::value('id', $ids['location'][$locationId]); $location->save(); $params['location'][$locationId]['id'] = $location->id; $address_object = CRM_Core_BAO_Address::add($params, $ids, $locationId); $location->address = $address_object; // set this to true if this has been made the primary IM. // the rule is the first entered value is the primary object $isPrimaryPhone = $isPrimaryEmail = $isPrimaryIM = true; $location->phone = array(); $location->email = array(); $location->im = array(); for ($i = 1; $i <= CRM_CONTACT_FORM_LOCATION_BLOCKS; $i++) { $location->phone[$i] = CRM_Core_BAO_Phone::add($params, $ids, $locationId, $i, $isPrimaryPhone); $location->email[$i] = CRM_Core_BAO_Email::add($params, $ids, $locationId, $i, $isPrimaryEmail); $location->im[$i] = CRM_Core_BAO_IM::add($params, $ids, $locationId, $i, $isPrimaryIM); } return $location; }
/** * global validation rules for the form * * @param array $fields posted values of the form * @param array $errors list of errors to be posted back to the form * * @return void * @static * @access public */ function formRule(&$fields, &$errors) { $primaryEmail = null; // make sure that at least one field is marked is_primary if (array_key_exists('location', $fields) && is_array($fields['location'])) { $locationKeys = array_keys($fields['location']); $isPrimary = false; $dataExists = false; $locTypeId = false; foreach ($locationKeys as $locationId) { if (array_key_exists('is_primary', $fields['location'][$locationId])) { if ($fields['location'][$locationId]['is_primary']) { if ($isPrimary) { $errors["location[{$locationId}][is_primary]"] = ts('Only one location can be marked as primary.'); } $isPrimary = true; } // only harvest email from the primary locations if (array_key_exists('email', $fields['location'][$locationId]) && is_array($fields['location'][$locationId]['email']) && empty($primaryEmail)) { foreach ($fields['location'][$locationId]['email'] as $idx => $email) { if (array_key_exists('email', $email)) { $primaryEmail = $email['email']; break; } } } } if (CRM_Contact_Form_Edit::locationDataExists($fields['location'][$locationId])) { $dataExists = true; if (!CRM_Utils_Array::value('location_type_id', $fields['location'][$locationId])) { $errors["location[{$locationId}][location_type_id]"] = ts('The Location Type should be set if there is any location information'); } } // for checking duplicate location type. if (CRM_Core_BAO_Location::dataExists($fields, $locationId, $ids)) { if ($locTypeId == $fields['location'][$locationId]['location_type_id']) { $errors["location[{$locationId}][location_type_id]"] = ts('Two locations cannot have same location type'); } $locTypeId = $fields['location'][$locationId]['location_type_id']; } } if ($dataExists && !$isPrimary) { $errors["location[1][is_primary]"] = ts('One location should be marked as primary.'); } } return $primaryEmail; }