Update version numbers to 4.4 and lint php
[civicrm-core.git] / CRM / Contact / BAO / Individual.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 function __construct() {}
45
46 /**
47 * Function is used to format the individual contact values
48 *
49 * @param array $params (reference ) an assoc array of name/value pairs
50 * @param array $contact contact object
51 *
52 * @return object CRM_Contact_BAO_Contact object
53 * @access public
54 * @static
55 */
56 static function format(&$params, &$contact) {
57 if (!self::dataExists($params)) {
58 return;
59 }
60
61 // "null" value for example is passed by dedupe merge in order to empty.
62 // Display name computation shouldn't consider such values.
63 foreach (array(
64 'first_name', 'middle_name', 'last_name', 'nick_name') 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
78 // get prefix and suffix names
79 $prefixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
80 $suffixes = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
81
82 $prefix = $suffix = NULL;
83 if ($prefix_id) {
84 $prefix = $prefixes[$prefix_id];
85 $params['individual_prefix'] = $prefix;
86 }
87 if ($suffix_id) {
88 $suffix = $suffixes[$suffix_id];
89 $params['individual_suffix'] = $suffix;
90 }
91
92 $params['is_deceased'] = CRM_Utils_Array::value('is_deceased', $params, FALSE);
93
94 $individual = NULL;
95 if ($contact->id) {
96 $individual = new CRM_Contact_BAO_Contact();
97 $individual->id = $contact->id;
98 if ($individual->find(TRUE)) {
99
100 //lets allow to update single name field though preserveDBName
101 //but if db having null value and params contain value, CRM-4330.
102 $useDBNames = array();
103
104 foreach (array(
105 'last', 'middle', 'first', 'nick') as $name) {
106 $dbName = "{$name}_name";
107 $value = $individual->$dbName;
108
109 // the db has name values
110 if ($value && CRM_Utils_Array::value('preserveDBName', $params)) {
111 $useDBNames[] = $name;
112 }
113 }
114
115 foreach (array(
116 'prefix', 'suffix') as $name) {
117 $dbName = "{$name}_id";
118 $value = $individual->$dbName;
119 if ($value && CRM_Utils_Array::value('preserveDBName', $params)) {
120 $useDBNames[] = $name;
121 }
122 }
123
124 // CRM-4430
125 //1. preserve db name if want
126 //2. lets get value from param if exists.
127 //3. if not in params, lets get from db.
128
129 foreach (array(
130 'last', 'middle', 'first', 'nick') as $name) {
131 $phpName = "{$name}Name";
132 $dbName = "{$name}_name";
133 $value = $individual->$dbName;
134 if (in_array($name, $useDBNames)) {
135 $params[$dbName] = $value;
136 $contact->$dbName = $value;
137 $$phpName = $value;
138 }
139 elseif (array_key_exists($dbName, $params)) {
140 $$phpName = $params[$dbName];
141 }
142 elseif ($value) {
143 $$phpName = $value;
144 }
145 }
146
147 foreach (array(
148 'prefix', 'suffix') as $name) {
149 $phpName = $name;
150 $dbName = "{$name}_id";
151 $vals = "{$name}es";
152
153 $value = $individual->$dbName;
154 if (in_array($name, $useDBNames)) {
155 $params[$dbName] = $value;
156 $contact->$dbName = $value;
157 if ($value) {
158 $temp = $$vals;
159 $$phpName = $temp[$value];
160 }
161 else {
162 $$phpName = NULL;
163 }
164 }
165 elseif (array_key_exists($dbName, $params)) {
166 $temp = $$vals;
167 // CRM-5278
168 if (!empty($params[$dbName])) {
169 $$phpName = CRM_Utils_Array::value($params[$dbName], $temp);
170 }
171 }
172 elseif ($value) {
173 $temp = $$vals;
174 $$phpName = $temp[$value];
175 }
176 }
177 }
178 }
179
180 //first trim before further processing.
181 foreach (array(
182 'lastName', 'firstName', 'middleName') as $fld) {
183 $$fld = trim($$fld);
184 }
185
186 if ($lastName || $firstName || $middleName) {
187 // make sure we have values for all the name fields.
188 $formatted = $params;
189 $nameParams = array(
190 'first_name' => $firstName,
191 'middle_name' => $middleName,
192 'last_name' => $lastName,
193 'nick_name' => $nickName,
194 'individual_suffix' => $suffix,
195 'individual_prefix' => $prefix,
196 'prefix_id' => $prefix_id,
197 'suffix_id' => $suffix_id,
198 );
199 // make sure we have all the name fields.
200 foreach ($nameParams as $name => $value) {
201 if (!CRM_Utils_Array::value($name, $formatted) && $value) {
202 $formatted[$name] = $value;
203 }
204 }
205
206 $tokens = array();
207 CRM_Utils_Hook::tokens($tokens);
208 $tokenFields = array();
209 foreach ($tokens as $category => $catTokens) {
210 foreach ($catTokens as $token => $label) {
211 $tokenFields[] = $token;
212 }
213 }
214
215 //build the sort name.
216 $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
217 'sort_name_format'
218 );
219 $sortName = CRM_Utils_Address::format($formatted, $format,
220 FALSE, FALSE, TRUE, $tokenFields
221 );
222 $sortName = trim($sortName);
223
224 //build the display name.
225 $format = CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
226 'display_name_format'
227 );
228 $displayName = CRM_Utils_Address::format($formatted, $format,
229 FALSE, FALSE, TRUE, $tokenFields
230 );
231 $displayName = trim($displayName);
232 }
233
234 //start further check for email.
235 if (empty($sortName) || empty($displayName)) {
236 $email = NULL;
237 if (CRM_Utils_Array::value('email', $params) &&
238 is_array($params['email'])
239 ) {
240 foreach ($params['email'] as $emailBlock) {
241 if (isset($emailBlock['is_primary'])) {
242 $email = $emailBlock['email'];
243 break;
244 }
245 }
246 }
247 $uniqId = CRM_Utils_Array::value('user_unique_id', $params);
248 if (!$email && $contact->id) {
249 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contact->id);
250 }
251 }
252
253 //now set the names.
254 $names = array('sortName' => 'sort_name', 'displayName' => 'display_name');
255 foreach ($names as $value => $name) {
256 if (empty($$value)) {
257 if ($email) {
258 $$value = $email;
259 }
260 elseif ($uniqId) {
261 $$value = $uniqId;
262 }
263 }
264 //finally if we could not pass anything lets keep db.
265 if (!empty($$value)) {
266 $contact->$name = $$value;
267 }
268 }
269
270 $format = CRM_Utils_Date::getDateFormat('birth');
271 if ($date = CRM_Utils_Array::value('birth_date', $params)) {
272 if (in_array($format, array(
273 'dd-mm', 'mm/dd'))) {
274 $separator = '/';
275 if ($format == 'dd-mm') {
276 $separator = '-';
277 }
278 $date = $date . $separator . '1902';
279 }
280 elseif (in_array($format, array(
281 'yy-mm'))) {
282 $date = $date . '-01';
283 }
284 elseif (in_array($format, array(
285 'M yy'))) {
286 $date = $date . '-01';
287 }
288 elseif (in_array($format, array(
289 'yy'))) {
290 $date = $date . '-01-01';
291 }
292 $contact->birth_date = CRM_Utils_Date::processDate($date);
293 }
294 elseif ($contact->birth_date) {
295 $contact->birth_date = CRM_Utils_Date::isoToMysql($contact->birth_date);
296 }
297
298 if ($date = CRM_Utils_Array::value('deceased_date', $params)) {
299 if (in_array($format, array(
300 'dd-mm', 'mm/dd'))) {
301 $separator = '/';
302 if ($format == 'dd-mm') {
303 $separator = '-';
304 }
305 $date = $date . $separator . '1902';
306 }
307 elseif (in_array($format, array(
308 'yy-mm'))) {
309 $date = $date . '-01';
310 }
311 elseif (in_array($format, array(
312 'M yy'))) {
313 $date = $date . '-01';
314 }
315 elseif (in_array($format, array(
316 'yy'))) {
317 $date = $date . '-01-01';
318 }
319
320 $contact->deceased_date = CRM_Utils_Date::processDate($date);
321 }
322 elseif ($contact->deceased_date) {
323 $contact->deceased_date = CRM_Utils_Date::isoToMysql($contact->deceased_date);
324 }
325
326 if ($middle_name = CRM_Utils_Array::value('middle_name', $params)) {
327 $contact->middle_name = $middle_name;
328 }
329
330 return $contact;
331 }
332
333 /**
334 * regenerates display_name for contacts with given prefixes/suffixes
335 *
336 * @param array $ids the array with the prefix/suffix id governing which contacts to regenerate
337 * @param int $action the action describing whether prefix/suffix was UPDATED or DELETED
338 *
339 * @return void
340 */
341 static function updateDisplayNames(&$ids, $action) {
342 // get the proper field name (prefix_id or suffix_id) and its value
343 $fieldName = '';
344 foreach ($ids as $key => $value) {
345 switch ($key) {
346 case 'individualPrefix':
347 $fieldName = 'prefix_id';
348 $fieldValue = $value;
349 break 2;
350
351 case 'individualSuffix':
352 $fieldName = 'suffix_id';
353 $fieldValue = $value;
354 break 2;
355 }
356 }
357 if ($fieldName == '') {
358 return;
359 }
360
361 // query for the affected individuals
362 $fieldValue = CRM_Utils_Type::escape($fieldValue, 'Integer');
363 $contact = new CRM_Contact_BAO_Contact();
364 $contact->$fieldName = $fieldValue;
365 $contact->find();
366
367 // iterate through the affected individuals and rebuild their display_names
368 while ($contact->fetch()) {
369 $contact = new CRM_Contact_BAO_Contact();
370 $contact->id = $contact->contact_id;
371 if ($action == CRM_Core_Action::DELETE) {
372 $contact->$fieldName = 'NULL';
373 $contact->save();
374 }
375 $contact->display_name = $contact->displayName();
376 $contact->save();
377 }
378 }
379
380 /**
381 * creates display name
382 *
383 * @return string the constructed display name
384 */
385 function displayName() {
386 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
387 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
388 return str_replace(' ', ' ', trim($prefix[$this->prefix_id] . ' ' . $this->first_name . ' ' . $this->middle_name . ' ' . $this->last_name . ' ' . $suffix[$this->suffix_id]));
389 }
390
391 /**
392 * Check if there is data to create the object
393 *
394 * @param array $params (reference ) an assoc array of name/value pairs
395 *
396 * @return boolean
397 * @access public
398 * @static
399 */
400 static function dataExists(&$params) {
401 if ($params['contact_type'] == 'Individual') {
402 return TRUE;
403 }
404
405 return FALSE;
406 }
407 }
408