3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
35 * Class contains functions for individual contact type.
37 class CRM_Contact_BAO_Individual
extends CRM_Contact_DAO_Contact
{
42 public function __construct() {
46 * Function is used to format the individual contact values.
48 * @param array $params
49 * (reference ) an assoc array of name/value pairs.
53 * @return CRM_Contact_BAO_Contact
55 public static function format(&$params, &$contact) {
56 if (!self
::dataExists($params)) {
60 // "null" value for example is passed by dedupe merge in order to empty.
61 // Display name computation shouldn't consider such values.
62 foreach (array('first_name', 'middle_name', 'last_name', 'nick_name', 'formal_title') as $displayField) {
63 if (CRM_Utils_Array
::value($displayField, $params) == "null") {
64 $params[$displayField] = '';
68 $sortName = $displayName = '';
69 $firstName = CRM_Utils_Array
::value('first_name', $params, '');
70 $middleName = CRM_Utils_Array
::value('middle_name', $params, '');
71 $lastName = CRM_Utils_Array
::value('last_name', $params, '');
72 $nickName = CRM_Utils_Array
::value('nick_name', $params, '');
73 $prefix_id = CRM_Utils_Array
::value('prefix_id', $params, '');
74 $suffix_id = CRM_Utils_Array
::value('suffix_id', $params, '');
75 $formalTitle = CRM_Utils_Array
::value('formal_title', $params, '');
77 // get prefix and suffix names
78 $prefix = $suffix = NULL;
80 $params['individual_prefix'] = $prefix = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'prefix_id', $prefix_id);
83 $params['individual_suffix'] = $suffix = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', 'suffix_id', $suffix_id);
86 $params['is_deceased'] = CRM_Utils_Array
::value('is_deceased', $params, FALSE);
90 $individual = new CRM_Contact_BAO_Contact();
91 $individual->id
= $contact->id
;
92 if ($individual->find(TRUE)) {
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();
98 foreach (array('last', 'middle', 'first', 'nick') as $name) {
99 $dbName = "{$name}_name";
100 $value = $individual->$dbName;
102 // the db has name values
103 if ($value && !empty($params['preserveDBName'])) {
104 $useDBNames[] = $name;
108 foreach (array('prefix', 'suffix') as $name) {
109 $dbName = "{$name}_id";
110 $value = $individual->$dbName;
111 if ($value && !empty($params['preserveDBName'])) {
112 $useDBNames[] = $name;
116 if ($individual->formal_title
&& !empty($params['preserveDBName'])) {
117 $useDBNames[] = 'formal_title';
121 //1. preserve db name if want
122 //2. lets get value from param if exists.
123 //3. if not in params, lets get from db.
125 foreach (array('last', 'middle', 'first', 'nick') as $name) {
126 $phpName = "{$name}Name";
127 $dbName = "{$name}_name";
128 $value = $individual->$dbName;
129 if (in_array($name, $useDBNames)) {
130 $params[$dbName] = $value;
131 $contact->$dbName = $value;
134 elseif (array_key_exists($dbName, $params)) {
135 $
$phpName = $params[$dbName];
142 foreach (array('prefix', 'suffix') as $name) {
143 $dbName = "{$name}_id";
145 $value = $individual->$dbName;
146 if (in_array($name, $useDBNames)) {
147 $params[$dbName] = $value;
148 $contact->$dbName = $value;
150 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $dbName, $value);
156 elseif (array_key_exists($dbName, $params)) {
158 if (!empty($params[$dbName])) {
159 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $dbName, $params[$dbName]);
163 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $dbName, $value);
167 if (in_array('formal_title', $useDBNames)) {
168 $params['formal_title'] = $individual->formal_title
;
169 $contact->formal_title
= $individual->formal_title
;
170 $formalTitle = $individual->formal_title
;
172 elseif (array_key_exists('formal_title', $params)) {
173 $formalTitle = $params['formal_title'];
175 elseif ($individual->formal_title
) {
176 $formalTitle = $individual->formal_title
;
181 //first trim before further processing.
182 foreach (array('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,
198 'formal_title' => $formalTitle,
200 // make sure we have all the name fields.
201 foreach ($nameParams as $name => $value) {
202 if (empty($formatted[$name]) && $value) {
203 $formatted[$name] = $value;
208 CRM_Utils_Hook
::tokens($tokens);
209 $tokenFields = array();
210 foreach ($tokens as $catTokens) {
211 foreach ($catTokens as $token => $label) {
212 $tokenFields[] = $token;
216 //build the sort name.
217 $format = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
220 $sortName = CRM_Utils_Address
::format($formatted, $format,
221 FALSE, FALSE, TRUE, $tokenFields
223 $sortName = trim($sortName);
225 //build the display name.
226 $format = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
227 'display_name_format'
229 $displayName = CRM_Utils_Address
::format($formatted, $format,
230 FALSE, FALSE, TRUE, $tokenFields
232 $displayName = trim($displayName);
235 //start further check for email.
236 if (empty($sortName) ||
empty($displayName)) {
238 if (!empty($params['email']) &&
239 is_array($params['email'])
241 foreach ($params['email'] as $emailBlock) {
242 if (isset($emailBlock['is_primary'])) {
243 $email = $emailBlock['email'];
248 $uniqId = CRM_Utils_Array
::value('user_unique_id', $params);
249 if (!$email && $contact->id
) {
250 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contact->id
);
255 $names = array('displayName' => 'display_name', 'sortName' => 'sort_name');
256 foreach ($names as $value => $name) {
257 if (empty($
$value)) {
264 elseif (!empty($params[$name])) {
265 $
$value = $params[$name];
267 // If we have nothing else going on set sort_name to display_name.
268 elseif ($displayName) {
269 $
$value = $displayName;
272 //finally if we could not pass anything lets keep db.
273 if (!empty($
$value)) {
274 $contact->$name = $
$value;
278 $format = CRM_Utils_Date
::getDateFormat('birth');
279 if ($date = CRM_Utils_Array
::value('birth_date', $params)) {
280 if (in_array($format, array(
285 if ($format == 'dd-mm') {
288 $date = $date . $separator . '1902';
290 elseif (in_array($format, array(
293 $date = $date . '-01';
295 elseif (in_array($format, array(
298 $date = $date . '-01';
300 elseif (in_array($format, array(
303 $date = $date . '-01-01';
305 $contact->birth_date
= CRM_Utils_Date
::processDate($date);
307 elseif ($contact->birth_date
) {
308 $contact->birth_date
= CRM_Utils_Date
::isoToMysql($contact->birth_date
);
311 if ($date = CRM_Utils_Array
::value('deceased_date', $params)) {
312 if (in_array($format, array(
317 if ($format == 'dd-mm') {
320 $date = $date . $separator . '1902';
322 elseif (in_array($format, array(
325 $date = $date . '-01';
327 elseif (in_array($format, array(
330 $date = $date . '-01';
332 elseif (in_array($format, array(
335 $date = $date . '-01-01';
338 $contact->deceased_date
= CRM_Utils_Date
::processDate($date);
340 elseif ($contact->deceased_date
) {
341 $contact->deceased_date
= CRM_Utils_Date
::isoToMysql($contact->deceased_date
);
344 if ($middle_name = CRM_Utils_Array
::value('middle_name', $params)) {
345 $contact->middle_name
= $middle_name;
352 * Regenerates display_name for contacts with given prefixes/suffixes.
355 * The array with the prefix/suffix id governing which contacts to regenerate.
357 * The action describing whether prefix/suffix was UPDATED or DELETED.
359 public static function updateDisplayNames(&$ids, $action) {
360 // get the proper field name (prefix_id or suffix_id) and its value
362 foreach ($ids as $key => $value) {
364 case 'individualPrefix':
365 $fieldName = 'prefix_id';
366 $fieldValue = $value;
369 case 'individualSuffix':
370 $fieldName = 'suffix_id';
371 $fieldValue = $value;
375 if ($fieldName == '') {
379 // query for the affected individuals
380 $fieldValue = CRM_Utils_Type
::escape($fieldValue, 'Integer');
381 $contact = new CRM_Contact_BAO_Contact();
382 $contact->$fieldName = $fieldValue;
385 // iterate through the affected individuals and rebuild their display_names
386 while ($contact->fetch()) {
387 $contact = new CRM_Contact_BAO_Contact();
388 $contact->id
= $contact->contact_id
;
389 if ($action == CRM_Core_Action
::DELETE
) {
390 $contact->$fieldName = 'NULL';
393 $contact->display_name
= $contact->displayName();
399 * Creates display name.
402 * the constructed display name
404 public function displayName() {
405 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
406 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
407 return str_replace(' ', ' ', trim($prefix[$this->prefix_id
] . ' ' . $this->first_name
. ' ' . $this->middle_name
. ' ' . $this->last_name
. ' ' . $suffix[$this->suffix_id
]));
411 * Check if there is data to create the object.
413 * @param array $params
417 public static function dataExists($params) {
418 if ($params['contact_type'] == 'Individual') {