Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Class contains functions for individual contact type | |
38 | */ | |
39 | class CRM_Contact_BAO_Individual extends CRM_Contact_DAO_Contact { | |
40 | ||
41 | /** | |
42 | * This is a contructor of the class. | |
43 | */ | |
6ea503d4 TO |
44 | public function __construct() { |
45 | } | |
6a488035 TO |
46 | |
47 | /** | |
fe482240 | 48 | * Function is used to format the individual contact values. |
6a488035 | 49 | * |
77c5b619 TO |
50 | * @param array $params |
51 | * (reference ) an assoc array of name/value pairs. | |
52 | * @param array $contact | |
53 | * Contact object. | |
6a488035 | 54 | * |
16b10e64 | 55 | * @return CRM_Contact_BAO_Contact |
6a488035 | 56 | */ |
00be9182 | 57 | public static function format(&$params, &$contact) { |
6a488035 | 58 | if (!self::dataExists($params)) { |
5396af74 | 59 | return NULL; |
6a488035 TO |
60 | } |
61 | ||
62 | // "null" value for example is passed by dedupe merge in order to empty. | |
63 | // Display name computation shouldn't consider such values. | |
606c258c | 64 | foreach (array('first_name', 'middle_name', 'last_name', 'nick_name', 'formal_title') as $displayField) { |
6a488035 TO |
65 | if (CRM_Utils_Array::value($displayField, $params) == "null") { |
66 | $params[$displayField] = ''; | |
67 | } | |
68 | } | |
69 | ||
353ffa53 TO |
70 | $sortName = $displayName = ''; |
71 | $firstName = CRM_Utils_Array::value('first_name', $params, ''); | |
6a488035 | 72 | $middleName = CRM_Utils_Array::value('middle_name', $params, ''); |
353ffa53 TO |
73 | $lastName = CRM_Utils_Array::value('last_name', $params, ''); |
74 | $nickName = CRM_Utils_Array::value('nick_name', $params, ''); | |
75 | $prefix_id = CRM_Utils_Array::value('prefix_id', $params, ''); | |
76 | $suffix_id = CRM_Utils_Array::value('suffix_id', $params, ''); | |
e171748b | 77 | $formalTitle = CRM_Utils_Array::value('formal_title', $params, ''); |
6a488035 TO |
78 | |
79 | // get prefix and suffix names | |
6a488035 TO |
80 | $prefix = $suffix = NULL; |
81 | if ($prefix_id) { | |
874347bd | 82 | $params['individual_prefix'] = $prefix = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'prefix_id', $prefix_id); |
6a488035 TO |
83 | } |
84 | if ($suffix_id) { | |
874347bd | 85 | $params['individual_suffix'] = $suffix = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'suffix_id', $suffix_id); |
6a488035 TO |
86 | } |
87 | ||
88 | $params['is_deceased'] = CRM_Utils_Array::value('is_deceased', $params, FALSE); | |
89 | ||
90 | $individual = NULL; | |
91 | if ($contact->id) { | |
92 | $individual = new CRM_Contact_BAO_Contact(); | |
93 | $individual->id = $contact->id; | |
94 | if ($individual->find(TRUE)) { | |
95 | ||
96 | //lets allow to update single name field though preserveDBName | |
97 | //but if db having null value and params contain value, CRM-4330. | |
98 | $useDBNames = array(); | |
99 | ||
874347bd | 100 | foreach (array('last', 'middle', 'first', 'nick') as $name) { |
6a488035 TO |
101 | $dbName = "{$name}_name"; |
102 | $value = $individual->$dbName; | |
103 | ||
104 | // the db has name values | |
8cc574cf | 105 | if ($value && !empty($params['preserveDBName'])) { |
6a488035 TO |
106 | $useDBNames[] = $name; |
107 | } | |
108 | } | |
109 | ||
874347bd | 110 | foreach (array('prefix', 'suffix') as $name) { |
6a488035 TO |
111 | $dbName = "{$name}_id"; |
112 | $value = $individual->$dbName; | |
8cc574cf | 113 | if ($value && !empty($params['preserveDBName'])) { |
6a488035 TO |
114 | $useDBNames[] = $name; |
115 | } | |
116 | } | |
117 | ||
8cc574cf | 118 | if ($individual->formal_title && !empty($params['preserveDBName'])) { |
e171748b OB |
119 | $useDBNames[] = 'formal_title'; |
120 | } | |
121 | ||
6a488035 TO |
122 | // CRM-4430 |
123 | //1. preserve db name if want | |
124 | //2. lets get value from param if exists. | |
125 | //3. if not in params, lets get from db. | |
126 | ||
874347bd | 127 | foreach (array('last', 'middle', 'first', 'nick') as $name) { |
6a488035 | 128 | $phpName = "{$name}Name"; |
353ffa53 TO |
129 | $dbName = "{$name}_name"; |
130 | $value = $individual->$dbName; | |
6a488035 | 131 | if (in_array($name, $useDBNames)) { |
353ffa53 | 132 | $params[$dbName] = $value; |
6a488035 | 133 | $contact->$dbName = $value; |
353ffa53 | 134 | $$phpName = $value; |
6a488035 TO |
135 | } |
136 | elseif (array_key_exists($dbName, $params)) { | |
137 | $$phpName = $params[$dbName]; | |
138 | } | |
139 | elseif ($value) { | |
140 | $$phpName = $value; | |
141 | } | |
142 | } | |
143 | ||
874347bd | 144 | foreach (array('prefix', 'suffix') as $name) { |
353ffa53 | 145 | $dbName = "{$name}_id"; |
6a488035 TO |
146 | |
147 | $value = $individual->$dbName; | |
148 | if (in_array($name, $useDBNames)) { | |
149 | $params[$dbName] = $value; | |
150 | $contact->$dbName = $value; | |
151 | if ($value) { | |
ec5cc0a3 | 152 | $$name = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', $dbName, $value); |
6a488035 TO |
153 | } |
154 | else { | |
309a09df | 155 | $$name = NULL; |
6a488035 TO |
156 | } |
157 | } | |
158 | elseif (array_key_exists($dbName, $params)) { | |
6a488035 TO |
159 | // CRM-5278 |
160 | if (!empty($params[$dbName])) { | |
309a09df | 161 | $$name = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', $dbName, $params[$dbName]); |
6a488035 TO |
162 | } |
163 | } | |
164 | elseif ($value) { | |
ec5cc0a3 | 165 | $$name = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', $dbName, $value); |
6a488035 TO |
166 | } |
167 | } | |
e171748b OB |
168 | |
169 | if (in_array('formal_title', $useDBNames)) { | |
170 | $params['formal_title'] = $individual->formal_title; | |
353ffa53 TO |
171 | $contact->formal_title = $individual->formal_title; |
172 | $formalTitle = $individual->formal_title; | |
e171748b OB |
173 | } |
174 | elseif (array_key_exists('formal_title', $params)) { | |
175 | $formalTitle = $params['formal_title']; | |
176 | } | |
177 | elseif ($individual->formal_title) { | |
178 | $formalTitle = $individual->formal_title; | |
179 | } | |
6a488035 TO |
180 | } |
181 | } | |
182 | ||
183 | //first trim before further processing. | |
874347bd | 184 | foreach (array('lastName', 'firstName', 'middleName') as $fld) { |
6a488035 TO |
185 | $$fld = trim($$fld); |
186 | } | |
187 | ||
188 | if ($lastName || $firstName || $middleName) { | |
189 | // make sure we have values for all the name fields. | |
190 | $formatted = $params; | |
191 | $nameParams = array( | |
192 | 'first_name' => $firstName, | |
193 | 'middle_name' => $middleName, | |
194 | 'last_name' => $lastName, | |
195 | 'nick_name' => $nickName, | |
196 | 'individual_suffix' => $suffix, | |
197 | 'individual_prefix' => $prefix, | |
198 | 'prefix_id' => $prefix_id, | |
199 | 'suffix_id' => $suffix_id, | |
e171748b | 200 | 'formal_title' => $formalTitle, |
6a488035 TO |
201 | ); |
202 | // make sure we have all the name fields. | |
203 | foreach ($nameParams as $name => $value) { | |
a7488080 | 204 | if (empty($formatted[$name]) && $value) { |
6a488035 TO |
205 | $formatted[$name] = $value; |
206 | } | |
207 | } | |
208 | ||
209 | $tokens = array(); | |
210 | CRM_Utils_Hook::tokens($tokens); | |
211 | $tokenFields = array(); | |
874347bd | 212 | foreach ($tokens as $catTokens) { |
6a488035 TO |
213 | foreach ($catTokens as $token => $label) { |
214 | $tokenFields[] = $token; | |
215 | } | |
216 | } | |
217 | ||
218 | //build the sort name. | |
219 | $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
220 | 'sort_name_format' | |
221 | ); | |
222 | $sortName = CRM_Utils_Address::format($formatted, $format, | |
223 | FALSE, FALSE, TRUE, $tokenFields | |
224 | ); | |
225 | $sortName = trim($sortName); | |
226 | ||
227 | //build the display name. | |
228 | $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, | |
229 | 'display_name_format' | |
230 | ); | |
231 | $displayName = CRM_Utils_Address::format($formatted, $format, | |
232 | FALSE, FALSE, TRUE, $tokenFields | |
233 | ); | |
234 | $displayName = trim($displayName); | |
235 | } | |
236 | ||
237 | //start further check for email. | |
238 | if (empty($sortName) || empty($displayName)) { | |
239 | $email = NULL; | |
a7488080 | 240 | if (!empty($params['email']) && |
6a488035 TO |
241 | is_array($params['email']) |
242 | ) { | |
243 | foreach ($params['email'] as $emailBlock) { | |
244 | if (isset($emailBlock['is_primary'])) { | |
245 | $email = $emailBlock['email']; | |
246 | break; | |
247 | } | |
248 | } | |
249 | } | |
250 | $uniqId = CRM_Utils_Array::value('user_unique_id', $params); | |
251 | if (!$email && $contact->id) { | |
252 | $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contact->id); | |
253 | } | |
254 | } | |
255 | ||
256 | //now set the names. | |
257 | $names = array('sortName' => 'sort_name', 'displayName' => 'display_name'); | |
258 | foreach ($names as $value => $name) { | |
259 | if (empty($$value)) { | |
260 | if ($email) { | |
261 | $$value = $email; | |
262 | } | |
263 | elseif ($uniqId) { | |
264 | $$value = $uniqId; | |
265 | } | |
266 | } | |
267 | //finally if we could not pass anything lets keep db. | |
268 | if (!empty($$value)) { | |
269 | $contact->$name = $$value; | |
270 | } | |
271 | } | |
272 | ||
273 | $format = CRM_Utils_Date::getDateFormat('birth'); | |
274 | if ($date = CRM_Utils_Array::value('birth_date', $params)) { | |
275 | if (in_array($format, array( | |
353ffa53 | 276 | 'dd-mm', |
5396af74 | 277 | 'mm/dd', |
353ffa53 | 278 | ))) { |
6a488035 TO |
279 | $separator = '/'; |
280 | if ($format == 'dd-mm') { | |
281 | $separator = '-'; | |
282 | } | |
283 | $date = $date . $separator . '1902'; | |
284 | } | |
285 | elseif (in_array($format, array( | |
5396af74 | 286 | 'yy-mm', |
353ffa53 | 287 | ))) { |
6a488035 TO |
288 | $date = $date . '-01'; |
289 | } | |
290 | elseif (in_array($format, array( | |
5396af74 | 291 | 'M yy', |
353ffa53 | 292 | ))) { |
6a488035 TO |
293 | $date = $date . '-01'; |
294 | } | |
295 | elseif (in_array($format, array( | |
5396af74 | 296 | 'yy', |
353ffa53 | 297 | ))) { |
6a488035 TO |
298 | $date = $date . '-01-01'; |
299 | } | |
300 | $contact->birth_date = CRM_Utils_Date::processDate($date); | |
301 | } | |
302 | elseif ($contact->birth_date) { | |
303 | $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date); | |
304 | } | |
305 | ||
306 | if ($date = CRM_Utils_Array::value('deceased_date', $params)) { | |
307 | if (in_array($format, array( | |
353ffa53 | 308 | 'dd-mm', |
5396af74 | 309 | 'mm/dd', |
353ffa53 | 310 | ))) { |
6a488035 TO |
311 | $separator = '/'; |
312 | if ($format == 'dd-mm') { | |
313 | $separator = '-'; | |
314 | } | |
315 | $date = $date . $separator . '1902'; | |
316 | } | |
317 | elseif (in_array($format, array( | |
5396af74 | 318 | 'yy-mm', |
353ffa53 | 319 | ))) { |
6a488035 TO |
320 | $date = $date . '-01'; |
321 | } | |
322 | elseif (in_array($format, array( | |
5396af74 | 323 | 'M yy', |
353ffa53 | 324 | ))) { |
6a488035 TO |
325 | $date = $date . '-01'; |
326 | } | |
327 | elseif (in_array($format, array( | |
5396af74 | 328 | 'yy', |
353ffa53 | 329 | ))) { |
6a488035 TO |
330 | $date = $date . '-01-01'; |
331 | } | |
332 | ||
333 | $contact->deceased_date = CRM_Utils_Date::processDate($date); | |
334 | } | |
335 | elseif ($contact->deceased_date) { | |
336 | $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date); | |
337 | } | |
338 | ||
339 | if ($middle_name = CRM_Utils_Array::value('middle_name', $params)) { | |
340 | $contact->middle_name = $middle_name; | |
341 | } | |
342 | ||
343 | return $contact; | |
344 | } | |
345 | ||
346 | /** | |
100fef9d | 347 | * Regenerates display_name for contacts with given prefixes/suffixes |
6a488035 | 348 | * |
77c5b619 TO |
349 | * @param array $ids |
350 | * The array with the prefix/suffix id governing which contacts to regenerate. | |
351 | * @param int $action | |
352 | * The action describing whether prefix/suffix was UPDATED or DELETED. | |
6a488035 TO |
353 | * |
354 | * @return void | |
355 | */ | |
00be9182 | 356 | public static function updateDisplayNames(&$ids, $action) { |
6a488035 TO |
357 | // get the proper field name (prefix_id or suffix_id) and its value |
358 | $fieldName = ''; | |
359 | foreach ($ids as $key => $value) { | |
360 | switch ($key) { | |
361 | case 'individualPrefix': | |
362 | $fieldName = 'prefix_id'; | |
363 | $fieldValue = $value; | |
364 | break 2; | |
365 | ||
366 | case 'individualSuffix': | |
367 | $fieldName = 'suffix_id'; | |
368 | $fieldValue = $value; | |
369 | break 2; | |
370 | } | |
371 | } | |
372 | if ($fieldName == '') { | |
373 | return; | |
374 | } | |
375 | ||
376 | // query for the affected individuals | |
353ffa53 TO |
377 | $fieldValue = CRM_Utils_Type::escape($fieldValue, 'Integer'); |
378 | $contact = new CRM_Contact_BAO_Contact(); | |
6a488035 TO |
379 | $contact->$fieldName = $fieldValue; |
380 | $contact->find(); | |
381 | ||
382 | // iterate through the affected individuals and rebuild their display_names | |
383 | while ($contact->fetch()) { | |
384 | $contact = new CRM_Contact_BAO_Contact(); | |
385 | $contact->id = $contact->contact_id; | |
386 | if ($action == CRM_Core_Action::DELETE) { | |
387 | $contact->$fieldName = 'NULL'; | |
388 | $contact->save(); | |
389 | } | |
390 | $contact->display_name = $contact->displayName(); | |
391 | $contact->save(); | |
392 | } | |
393 | } | |
394 | ||
395 | /** | |
fe482240 | 396 | * Creates display name. |
6a488035 | 397 | * |
a6c01b45 CW |
398 | * @return string |
399 | * the constructed display name | |
6a488035 | 400 | */ |
00be9182 | 401 | public function displayName() { |
e6c4755b CW |
402 | $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
403 | $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); | |
6a488035 TO |
404 | return str_replace(' ', ' ', trim($prefix[$this->prefix_id] . ' ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name . ' ' . $suffix[$this->suffix_id])); |
405 | } | |
406 | ||
407 | /** | |
fe482240 | 408 | * Check if there is data to create the object. |
6a488035 | 409 | * |
77c5b619 TO |
410 | * @param array $params |
411 | * (reference ) an assoc array of name/value pairs. | |
6a488035 | 412 | * |
5396af74 | 413 | * @return bool |
6a488035 | 414 | */ |
00be9182 | 415 | public static function dataExists(&$params) { |
6a488035 TO |
416 | if ($params['contact_type'] == 'Individual') { |
417 | return TRUE; | |
418 | } | |
419 | ||
420 | return FALSE; | |
421 | } | |
96025800 | 422 | |
6a488035 | 423 | } |