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