Merge pull request #6177 from xurizaemon/CRM-16806
[civicrm-core.git] / CRM / Contact / BAO / Individual.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2015
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 */
44 public function __construct() {
45 }
46
47 /**
48 * Function is used to format the individual contact values.
49 *
50 * @param array $params
51 * (reference ) an assoc array of name/value pairs.
52 * @param CRM $contact
53 * Contact object.
54 *
55 * @return CRM_Contact_BAO_Contact
56 */
57 public static function format(&$params, &$contact) {
58 if (!self::dataExists($params)) {
59 return NULL;
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.
64 foreach (array('first_name', 'middle_name', 'last_name', 'nick_name', 'formal_title') as $displayField) {
65 if (CRM_Utils_Array::value($displayField, $params) == "null") {
66 $params[$displayField] = '';
67 }
68 }
69
70 $sortName = $displayName = '';
71 $firstName = CRM_Utils_Array::value('first_name', $params, '');
72 $middleName = CRM_Utils_Array::value('middle_name', $params, '');
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, '');
77 $formalTitle = CRM_Utils_Array::value('formal_title', $params, '');
78
79 // get prefix and suffix names
80 $prefix = $suffix = NULL;
81 if ($prefix_id) {
82 $params['individual_prefix'] = $prefix = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'prefix_id', $prefix_id);
83 }
84 if ($suffix_id) {
85 $params['individual_suffix'] = $suffix = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', 'suffix_id', $suffix_id);
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
100 foreach (array('last', 'middle', 'first', 'nick') as $name) {
101 $dbName = "{$name}_name";
102 $value = $individual->$dbName;
103
104 // the db has name values
105 if ($value && !empty($params['preserveDBName'])) {
106 $useDBNames[] = $name;
107 }
108 }
109
110 foreach (array('prefix', 'suffix') as $name) {
111 $dbName = "{$name}_id";
112 $value = $individual->$dbName;
113 if ($value && !empty($params['preserveDBName'])) {
114 $useDBNames[] = $name;
115 }
116 }
117
118 if ($individual->formal_title && !empty($params['preserveDBName'])) {
119 $useDBNames[] = 'formal_title';
120 }
121
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
127 foreach (array('last', 'middle', 'first', 'nick') as $name) {
128 $phpName = "{$name}Name";
129 $dbName = "{$name}_name";
130 $value = $individual->$dbName;
131 if (in_array($name, $useDBNames)) {
132 $params[$dbName] = $value;
133 $contact->$dbName = $value;
134 $$phpName = $value;
135 }
136 elseif (array_key_exists($dbName, $params)) {
137 $$phpName = $params[$dbName];
138 }
139 elseif ($value) {
140 $$phpName = $value;
141 }
142 }
143
144 foreach (array('prefix', 'suffix') as $name) {
145 $dbName = "{$name}_id";
146
147 $value = $individual->$dbName;
148 if (in_array($name, $useDBNames)) {
149 $params[$dbName] = $value;
150 $contact->$dbName = $value;
151 if ($value) {
152 $$name = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', $dbName, $value);
153 }
154 else {
155 $$name = NULL;
156 }
157 }
158 elseif (array_key_exists($dbName, $params)) {
159 // CRM-5278
160 if (!empty($params[$dbName])) {
161 $$name = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', $dbName, $params[$dbName]);
162 }
163 }
164 elseif ($value) {
165 $$name = CRM_Core_PseudoConstant::getLabel('CRM_Contact_DAO_Contact', $dbName, $value);
166 }
167 }
168
169 if (in_array('formal_title', $useDBNames)) {
170 $params['formal_title'] = $individual->formal_title;
171 $contact->formal_title = $individual->formal_title;
172 $formalTitle = $individual->formal_title;
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 }
180 }
181 }
182
183 //first trim before further processing.
184 foreach (array('lastName', 'firstName', 'middleName') as $fld) {
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,
200 'formal_title' => $formalTitle,
201 );
202 // make sure we have all the name fields.
203 foreach ($nameParams as $name => $value) {
204 if (empty($formatted[$name]) && $value) {
205 $formatted[$name] = $value;
206 }
207 }
208
209 $tokens = array();
210 CRM_Utils_Hook::tokens($tokens);
211 $tokenFields = array();
212 foreach ($tokens as $catTokens) {
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;
240 if (!empty($params['email']) &&
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('displayName' => 'display_name', 'sortName' => 'sort_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 elseif (!empty($params[$name])) {
267 $$value = $params[$name];
268 }
269 // If we have nothing else going on set sort_name to display_name.
270 elseif ($displayName) {
271 $$value = $displayName;
272 }
273 }
274 //finally if we could not pass anything lets keep db.
275 if (!empty($$value)) {
276 $contact->$name = $$value;
277 }
278 }
279
280 $format = CRM_Utils_Date::getDateFormat('birth');
281 if ($date = CRM_Utils_Array::value('birth_date', $params)) {
282 if (in_array($format, array(
283 'dd-mm',
284 'mm/dd',
285 ))) {
286 $separator = '/';
287 if ($format == 'dd-mm') {
288 $separator = '-';
289 }
290 $date = $date . $separator . '1902';
291 }
292 elseif (in_array($format, array(
293 'yy-mm',
294 ))) {
295 $date = $date . '-01';
296 }
297 elseif (in_array($format, array(
298 'M yy',
299 ))) {
300 $date = $date . '-01';
301 }
302 elseif (in_array($format, array(
303 'yy',
304 ))) {
305 $date = $date . '-01-01';
306 }
307 $contact->birth_date = CRM_Utils_Date::processDate($date);
308 }
309 elseif ($contact->birth_date) {
310 $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date);
311 }
312
313 if ($date = CRM_Utils_Array::value('deceased_date', $params)) {
314 if (in_array($format, array(
315 'dd-mm',
316 'mm/dd',
317 ))) {
318 $separator = '/';
319 if ($format == 'dd-mm') {
320 $separator = '-';
321 }
322 $date = $date . $separator . '1902';
323 }
324 elseif (in_array($format, array(
325 'yy-mm',
326 ))) {
327 $date = $date . '-01';
328 }
329 elseif (in_array($format, array(
330 'M yy',
331 ))) {
332 $date = $date . '-01';
333 }
334 elseif (in_array($format, array(
335 'yy',
336 ))) {
337 $date = $date . '-01-01';
338 }
339
340 $contact->deceased_date = CRM_Utils_Date::processDate($date);
341 }
342 elseif ($contact->deceased_date) {
343 $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date);
344 }
345
346 if ($middle_name = CRM_Utils_Array::value('middle_name', $params)) {
347 $contact->middle_name = $middle_name;
348 }
349
350 return $contact;
351 }
352
353 /**
354 * Regenerates display_name for contacts with given prefixes/suffixes
355 *
356 * @param array $ids
357 * The array with the prefix/suffix id governing which contacts to regenerate.
358 * @param int $action
359 * The action describing whether prefix/suffix was UPDATED or DELETED.
360 *
361 * @return void
362 */
363 public static function updateDisplayNames(&$ids, $action) {
364 // get the proper field name (prefix_id or suffix_id) and its value
365 $fieldName = '';
366 foreach ($ids as $key => $value) {
367 switch ($key) {
368 case 'individualPrefix':
369 $fieldName = 'prefix_id';
370 $fieldValue = $value;
371 break 2;
372
373 case 'individualSuffix':
374 $fieldName = 'suffix_id';
375 $fieldValue = $value;
376 break 2;
377 }
378 }
379 if ($fieldName == '') {
380 return;
381 }
382
383 // query for the affected individuals
384 $fieldValue = CRM_Utils_Type::escape($fieldValue, 'Integer');
385 $contact = new CRM_Contact_BAO_Contact();
386 $contact->$fieldName = $fieldValue;
387 $contact->find();
388
389 // iterate through the affected individuals and rebuild their display_names
390 while ($contact->fetch()) {
391 $contact = new CRM_Contact_BAO_Contact();
392 $contact->id = $contact->contact_id;
393 if ($action == CRM_Core_Action::DELETE) {
394 $contact->$fieldName = 'NULL';
395 $contact->save();
396 }
397 $contact->display_name = $contact->displayName();
398 $contact->save();
399 }
400 }
401
402 /**
403 * Creates display name.
404 *
405 * @return string
406 * the constructed display name
407 */
408 public function displayName() {
409 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
410 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
411 return str_replace(' ', ' ', trim($prefix[$this->prefix_id] . ' ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name . ' ' . $suffix[$this->suffix_id]));
412 }
413
414 /**
415 * Check if there is data to create the object.
416 *
417 * @param array $params
418 *
419 * @return bool
420 */
421 public static function dataExists($params) {
422 if ($params['contact_type'] == 'Individual') {
423 return TRUE;
424 }
425
426 return FALSE;
427 }
428
429 }