3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class contains functions for individual contact type.
21 class CRM_Contact_BAO_Individual
extends CRM_Contact_DAO_Contact
{
26 public function __construct() {
30 * Function is used to format the individual contact values.
32 * @param array $params
33 * (reference ) an assoc array of name/value pairs.
34 * @param CRM_Contact_BAO_Contact $contact
37 * @return CRM_Contact_BAO_Contact
39 public static function format(&$params, &$contact) {
40 if (!self
::dataExists($params)) {
44 // "null" value for example is passed by dedupe merge in order to empty.
45 // Display name computation shouldn't consider such values.
46 foreach (['first_name', 'middle_name', 'last_name', 'nick_name', 'formal_title', 'birth_date', 'deceased_date'] as $displayField) {
47 if (CRM_Utils_Array
::value($displayField, $params) == "null") {
48 $params[$displayField] = '';
52 $sortName = $displayName = '';
53 $firstName = CRM_Utils_Array
::value('first_name', $params, '');
54 $middleName = CRM_Utils_Array
::value('middle_name', $params, '');
55 $lastName = CRM_Utils_Array
::value('last_name', $params, '');
56 $nickName = CRM_Utils_Array
::value('nick_name', $params, '');
57 $prefix_id = CRM_Utils_Array
::value('prefix_id', $params, '');
58 $suffix_id = CRM_Utils_Array
::value('suffix_id', $params, '');
59 $formalTitle = CRM_Utils_Array
::value('formal_title', $params, '');
61 // get prefix and suffix names
62 $prefix = $suffix = NULL;
64 $params['individual_prefix'] = $prefix = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'prefix_id', $prefix_id);
67 $params['individual_suffix'] = $suffix = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'suffix_id', $suffix_id);
70 $params['is_deceased'] = CRM_Utils_Array
::value('is_deceased', $params, FALSE);
74 $individual = new CRM_Contact_BAO_Contact();
75 $individual->id
= $contact->id
;
76 if ($individual->find(TRUE)) {
78 //lets allow to update single name field though preserveDBName
79 //but if db having null value and params contain value, CRM-4330.
82 foreach (['last', 'middle', 'first', 'nick'] as $name) {
83 $dbName = "{$name}_name";
84 $value = $individual->$dbName;
86 // the db has name values
87 if ($value && !empty($params['preserveDBName'])) {
88 $useDBNames[] = $name;
92 foreach (['prefix', 'suffix'] as $name) {
93 $dbName = "{$name}_id";
94 $value = $individual->$dbName;
95 if ($value && !empty($params['preserveDBName'])) {
96 $useDBNames[] = $name;
100 if ($individual->formal_title
&& !empty($params['preserveDBName'])) {
101 $useDBNames[] = 'formal_title';
105 //1. preserve db name if want
106 //2. lets get value from param if exists.
107 //3. if not in params, lets get from db.
109 foreach (['last', 'middle', 'first', 'nick'] as $name) {
110 $phpName = "{$name}Name";
111 $dbName = "{$name}_name";
112 $value = $individual->$dbName;
113 if (in_array($name, $useDBNames)) {
114 $params[$dbName] = $value;
115 $contact->$dbName = $value;
118 elseif (array_key_exists($dbName, $params)) {
119 $
$phpName = $params[$dbName];
126 foreach (['prefix', 'suffix'] as $name) {
127 $dbName = "{$name}_id";
129 $value = $individual->$dbName;
130 if (in_array($name, $useDBNames)) {
131 $params[$dbName] = $value;
132 $contact->$dbName = $value;
134 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $dbName, $value);
140 elseif (array_key_exists($dbName, $params)) {
142 if (!empty($params[$dbName])) {
143 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $dbName, $params[$dbName]);
147 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $dbName, $value);
151 if (in_array('formal_title', $useDBNames)) {
152 $params['formal_title'] = $individual->formal_title
;
153 $contact->formal_title
= $individual->formal_title
;
154 $formalTitle = $individual->formal_title
;
156 elseif (array_key_exists('formal_title', $params)) {
157 $formalTitle = $params['formal_title'];
159 elseif ($individual->formal_title
) {
160 $formalTitle = $individual->formal_title
;
165 //first trim before further processing.
166 foreach (['lastName', 'firstName', 'middleName'] as $fld) {
170 if ($lastName ||
$firstName ||
$middleName) {
171 // make sure we have values for all the name fields.
172 $formatted = $params;
174 'first_name' => $firstName,
175 'middle_name' => $middleName,
176 'last_name' => $lastName,
177 'nick_name' => $nickName,
178 'individual_suffix' => $suffix,
179 'individual_prefix' => $prefix,
180 'prefix_id' => $prefix_id,
181 'suffix_id' => $suffix_id,
182 'formal_title' => $formalTitle,
184 // make sure we have all the name fields.
185 foreach ($nameParams as $name => $value) {
186 if (empty($formatted[$name]) && $value) {
187 $formatted[$name] = $value;
192 CRM_Utils_Hook
::tokens($tokens);
194 foreach ($tokens as $catTokens) {
195 foreach ($catTokens as $token => $label) {
196 $tokenFields[] = $token;
200 //build the sort name.
201 $format = Civi
::settings()->get('sort_name_format');
202 $sortName = CRM_Utils_Address
::format($formatted, $format,
203 FALSE, FALSE, $tokenFields
205 $sortName = trim($sortName);
207 //build the display name.
208 $format = Civi
::settings()->get('display_name_format');
209 $displayName = CRM_Utils_Address
::format($formatted, $format,
210 FALSE, FALSE, $tokenFields
212 $displayName = trim($displayName);
215 //start further check for email.
216 if (empty($sortName) ||
empty($displayName)) {
218 if (!empty($params['email']) &&
219 is_array($params['email'])
221 foreach ($params['email'] as $emailBlock) {
222 if (isset($emailBlock['is_primary'])) {
223 $email = $emailBlock['email'];
228 $uniqId = CRM_Utils_Array
::value('user_unique_id', $params);
229 if (!$email && $contact->id
) {
230 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contact->id
);
235 $names = ['displayName' => 'display_name', 'sortName' => 'sort_name'];
236 foreach ($names as $value => $name) {
237 if (empty($
$value)) {
244 elseif (!empty($params[$name])) {
245 $
$value = $params[$name];
247 // If we have nothing else going on set sort_name to display_name.
248 elseif ($displayName) {
249 $
$value = $displayName;
252 //finally if we could not pass anything lets keep db.
253 if (!empty($
$value)) {
254 $contact->$name = $
$value;
258 $format = CRM_Utils_Date
::getDateFormat('birth');
259 if ($date = CRM_Utils_Array
::value('birth_date', $params)) {
260 if (in_array($format, [
265 if ($format == 'dd-mm') {
268 $date = $date . $separator . '1902';
270 elseif (in_array($format, [
273 $date = $date . '-01';
275 elseif (in_array($format, [
278 $date = $date . '-01';
280 elseif (in_array($format, [
283 $date = $date . '-01-01';
285 $contact->birth_date
= CRM_Utils_Date
::processDate($date);
287 elseif ($contact->birth_date
) {
288 $contact->birth_date
= CRM_Utils_Date
::isoToMysql($contact->birth_date
);
291 if ($date = CRM_Utils_Array
::value('deceased_date', $params)) {
292 if (in_array($format, [
297 if ($format == 'dd-mm') {
300 $date = $date . $separator . '1902';
302 elseif (in_array($format, [
305 $date = $date . '-01';
307 elseif (in_array($format, [
310 $date = $date . '-01';
312 elseif (in_array($format, [
315 $date = $date . '-01-01';
318 $contact->deceased_date
= CRM_Utils_Date
::processDate($date);
320 elseif ($contact->deceased_date
) {
321 $contact->deceased_date
= CRM_Utils_Date
::isoToMysql($contact->deceased_date
);
324 if ($middle_name = CRM_Utils_Array
::value('middle_name', $params)) {
325 $contact->middle_name
= $middle_name;
332 * Regenerates display_name for contacts with given prefixes/suffixes.
335 * The array with the prefix/suffix id governing which contacts to regenerate.
337 * The action describing whether prefix/suffix was UPDATED or DELETED.
339 public static function updateDisplayNames(&$ids, $action) {
340 // get the proper field name (prefix_id or suffix_id) and its value
342 foreach ($ids as $key => $value) {
344 case 'individualPrefix':
345 $fieldName = 'prefix_id';
346 $fieldValue = $value;
349 case 'individualSuffix':
350 $fieldName = 'suffix_id';
351 $fieldValue = $value;
355 if ($fieldName == '') {
359 // query for the affected individuals
360 $fieldValue = CRM_Utils_Type
::escape($fieldValue, 'Integer');
361 $contact = new CRM_Contact_BAO_Contact();
362 $contact->$fieldName = $fieldValue;
365 // iterate through the affected individuals and rebuild their display_names
366 while ($contact->fetch()) {
367 $contact = new CRM_Contact_BAO_Contact();
368 $contact->id
= $contact->contact_id
;
369 if ($action == CRM_Core_Action
::DELETE
) {
370 $contact->$fieldName = 'NULL';
373 $contact->display_name
= $contact->displayName();
379 * Creates display name.
382 * the constructed display name
384 public function displayName() {
385 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
386 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
387 return str_replace(' ', ' ', trim($prefix[$this->prefix_id
] . ' ' . $this->first_name
. ' ' . $this->middle_name
. ' ' . $this->last_name
. ' ' . $suffix[$this->suffix_id
]));
391 * Check if there is data to create the object.
393 * @param array $params
397 public static function dataExists($params) {
398 if ($params['contact_type'] == 'Individual') {