3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2013
37 * Class contains functions for individual contact type
39 class CRM_Contact_BAO_Individual
extends CRM_Contact_DAO_Contact
{
42 * This is a contructor of the class.
44 function __construct() {}
47 * Function is used to format the individual contact values
49 * @param array $params (reference ) an assoc array of name/value pairs
50 * @param array $contact contact object
52 * @return object CRM_Contact_BAO_Contact object
56 static function format(&$params, &$contact) {
57 if (!self
::dataExists($params)) {
61 // "null" value for example is passed by dedupe merge in order to empty.
62 // Display name computation shouldn't consider such values.
64 'first_name', 'middle_name', 'last_name', 'nick_name') as $displayField) {
65 if (CRM_Utils_Array
::value($displayField, $params) == "null") {
66 $params[$displayField] = '';
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, '');
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');
82 $prefix = $suffix = NULL;
84 $prefix = $prefixes[$prefix_id];
85 $params['individual_prefix'] = $prefix;
88 $suffix = $suffixes[$suffix_id];
89 $params['individual_suffix'] = $suffix;
92 $params['is_deceased'] = CRM_Utils_Array
::value('is_deceased', $params, FALSE);
96 $individual = new CRM_Contact_BAO_Contact();
97 $individual->id
= $contact->id
;
98 if ($individual->find(TRUE)) {
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();
105 'last', 'middle', 'first', 'nick') as $name) {
106 $dbName = "{$name}_name";
107 $value = $individual->$dbName;
109 // the db has name values
110 if ($value && CRM_Utils_Array
::value('preserveDBName', $params)) {
111 $useDBNames[] = $name;
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;
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.
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;
139 elseif (array_key_exists($dbName, $params)) {
140 $
$phpName = $params[$dbName];
148 'prefix', 'suffix') as $name) {
150 $dbName = "{$name}_id";
153 $value = $individual->$dbName;
154 if (in_array($name, $useDBNames)) {
155 $params[$dbName] = $value;
156 $contact->$dbName = $value;
159 $
$phpName = $temp[$value];
165 elseif (array_key_exists($dbName, $params)) {
168 if (!empty($params[$dbName])) {
169 $
$phpName = CRM_Utils_Array
::value($params[$dbName], $temp);
174 $
$phpName = $temp[$value];
180 //first trim before further processing.
182 'lastName', 'firstName', 'middleName') as $fld) {
186 if ($lastName ||
$firstName ||
$middleName) {
187 // make sure we have values for all the name fields.
188 $formatted = $params;
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,
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;
207 CRM_Utils_Hook
::tokens($tokens);
208 $tokenFields = array();
209 foreach ($tokens as $category => $catTokens) {
210 foreach ($catTokens as $token => $label) {
211 $tokenFields[] = $token;
215 //build the sort name.
216 $format = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
219 $sortName = CRM_Utils_Address
::format($formatted, $format,
220 FALSE, FALSE, TRUE, $tokenFields
222 $sortName = trim($sortName);
224 //build the display name.
225 $format = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
226 'display_name_format'
228 $displayName = CRM_Utils_Address
::format($formatted, $format,
229 FALSE, FALSE, TRUE, $tokenFields
231 $displayName = trim($displayName);
234 //start further check for email.
235 if (empty($sortName) ||
empty($displayName)) {
237 if (CRM_Utils_Array
::value('email', $params) &&
238 is_array($params['email'])
240 foreach ($params['email'] as $emailBlock) {
241 if (isset($emailBlock['is_primary'])) {
242 $email = $emailBlock['email'];
247 $uniqId = CRM_Utils_Array
::value('user_unique_id', $params);
248 if (!$email && $contact->id
) {
249 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contact->id
);
254 $names = array('sortName' => 'sort_name', 'displayName' => 'display_name');
255 foreach ($names as $value => $name) {
256 if (empty($
$value)) {
264 //finally if we could not pass anything lets keep db.
265 if (!empty($
$value)) {
266 $contact->$name = $
$value;
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'))) {
275 if ($format == 'dd-mm') {
278 $date = $date . $separator . '1902';
280 elseif (in_array($format, array(
282 $date = $date . '-01';
284 elseif (in_array($format, array(
286 $date = $date . '-01';
288 elseif (in_array($format, array(
290 $date = $date . '-01-01';
292 $contact->birth_date
= CRM_Utils_Date
::processDate($date);
294 elseif ($contact->birth_date
) {
295 $contact->birth_date
= CRM_Utils_Date
::isoToMysql($contact->birth_date
);
298 if ($date = CRM_Utils_Array
::value('deceased_date', $params)) {
299 if (in_array($format, array(
300 'dd-mm', 'mm/dd'))) {
302 if ($format == 'dd-mm') {
305 $date = $date . $separator . '1902';
307 elseif (in_array($format, array(
309 $date = $date . '-01';
311 elseif (in_array($format, array(
313 $date = $date . '-01';
315 elseif (in_array($format, array(
317 $date = $date . '-01-01';
320 $contact->deceased_date
= CRM_Utils_Date
::processDate($date);
322 elseif ($contact->deceased_date
) {
323 $contact->deceased_date
= CRM_Utils_Date
::isoToMysql($contact->deceased_date
);
326 if ($middle_name = CRM_Utils_Array
::value('middle_name', $params)) {
327 $contact->middle_name
= $middle_name;
334 * regenerates display_name for contacts with given prefixes/suffixes
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
341 static function updateDisplayNames(&$ids, $action) {
342 // get the proper field name (prefix_id or suffix_id) and its value
344 foreach ($ids as $key => $value) {
346 case 'individualPrefix':
347 $fieldName = 'prefix_id';
348 $fieldValue = $value;
351 case 'individualSuffix':
352 $fieldName = 'suffix_id';
353 $fieldValue = $value;
357 if ($fieldName == '') {
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;
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';
375 $contact->display_name
= $contact->displayName();
381 * creates display name
383 * @return string the constructed display name
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
]));
392 * Check if there is data to create the object
394 * @param array $params (reference ) an assoc array of name/value pairs
400 static function dataExists(&$params) {
401 if ($params['contact_type'] == 'Individual') {