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.
63 foreach (array('first_name', 'middle_name', 'last_name', 'nick_name') as $displayField) {
64 if (CRM_Utils_Array
::value($displayField, $params) == "null") {
65 $params[$displayField] = '';
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, '');
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 && CRM_Utils_Array
::value('preserveDBName', $params)) {
104 $useDBNames[] = $name;
108 foreach (array('prefix', 'suffix') as $name) {
109 $dbName = "{$name}_id";
110 $value = $individual->$dbName;
111 if ($value && CRM_Utils_Array
::value('preserveDBName', $params)) {
112 $useDBNames[] = $name;
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.
121 foreach (array('last', 'middle', 'first', 'nick') as $name) {
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;
130 elseif (array_key_exists($dbName, $params)) {
131 $
$phpName = $params[$dbName];
138 foreach (array('prefix', 'suffix') as $name) {
139 $dbName = "{$name}_id";
141 $value = $individual->$dbName;
142 if (in_array($name, $useDBNames)) {
143 $params[$dbName] = $value;
144 $contact->$dbName = $value;
146 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $name, $value);
152 elseif (array_key_exists($dbName, $params)) {
154 if (!empty($params[$dbName])) {
155 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $dbName, $params[$dbName]);
159 $
$name = CRM_Core_PseudoConstant
::getLabel('CRM_Contact_DAO_Contact', $name, $value);
165 //first trim before further processing.
166 foreach (array('lastName', 'firstName', 'middleName') as $fld) {
170 if ($lastName ||
$firstName ||
$middleName) {
171 // make sure we have values for all the name fields.
172 $formatted = $params;
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,
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;
191 CRM_Utils_Hook
::tokens($tokens);
192 $tokenFields = array();
193 foreach ($tokens as $catTokens) {
194 foreach ($catTokens as $token => $label) {
195 $tokenFields[] = $token;
199 //build the sort name.
200 $format = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
203 $sortName = CRM_Utils_Address
::format($formatted, $format,
204 FALSE, FALSE, TRUE, $tokenFields
206 $sortName = trim($sortName);
208 //build the display name.
209 $format = CRM_Core_BAO_Setting
::getItem(CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
210 'display_name_format'
212 $displayName = CRM_Utils_Address
::format($formatted, $format,
213 FALSE, FALSE, TRUE, $tokenFields
215 $displayName = trim($displayName);
218 //start further check for email.
219 if (empty($sortName) ||
empty($displayName)) {
221 if (CRM_Utils_Array
::value('email', $params) &&
222 is_array($params['email'])
224 foreach ($params['email'] as $emailBlock) {
225 if (isset($emailBlock['is_primary'])) {
226 $email = $emailBlock['email'];
231 $uniqId = CRM_Utils_Array
::value('user_unique_id', $params);
232 if (!$email && $contact->id
) {
233 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contact->id
);
238 $names = array('sortName' => 'sort_name', 'displayName' => 'display_name');
239 foreach ($names as $value => $name) {
240 if (empty($
$value)) {
248 //finally if we could not pass anything lets keep db.
249 if (!empty($
$value)) {
250 $contact->$name = $
$value;
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'))) {
259 if ($format == 'dd-mm') {
262 $date = $date . $separator . '1902';
264 elseif (in_array($format, array(
266 $date = $date . '-01';
268 elseif (in_array($format, array(
270 $date = $date . '-01';
272 elseif (in_array($format, array(
274 $date = $date . '-01-01';
276 $contact->birth_date
= CRM_Utils_Date
::processDate($date);
278 elseif ($contact->birth_date
) {
279 $contact->birth_date
= CRM_Utils_Date
::isoToMysql($contact->birth_date
);
282 if ($date = CRM_Utils_Array
::value('deceased_date', $params)) {
283 if (in_array($format, array(
284 'dd-mm', 'mm/dd'))) {
286 if ($format == 'dd-mm') {
289 $date = $date . $separator . '1902';
291 elseif (in_array($format, array(
293 $date = $date . '-01';
295 elseif (in_array($format, array(
297 $date = $date . '-01';
299 elseif (in_array($format, array(
301 $date = $date . '-01-01';
304 $contact->deceased_date
= CRM_Utils_Date
::processDate($date);
306 elseif ($contact->deceased_date
) {
307 $contact->deceased_date
= CRM_Utils_Date
::isoToMysql($contact->deceased_date
);
310 if ($middle_name = CRM_Utils_Array
::value('middle_name', $params)) {
311 $contact->middle_name
= $middle_name;
318 * regenerates display_name for contacts with given prefixes/suffixes
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
325 static function updateDisplayNames(&$ids, $action) {
326 // get the proper field name (prefix_id or suffix_id) and its value
328 foreach ($ids as $key => $value) {
330 case 'individualPrefix':
331 $fieldName = 'prefix_id';
332 $fieldValue = $value;
335 case 'individualSuffix':
336 $fieldName = 'suffix_id';
337 $fieldValue = $value;
341 if ($fieldName == '') {
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;
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';
359 $contact->display_name
= $contact->displayName();
365 * creates display name
367 * @return string the constructed display name
369 function displayName() {
370 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
371 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
372 return str_replace(' ', ' ', trim($prefix[$this->prefix_id
] . ' ' . $this->first_name
. ' ' . $this->middle_name
. ' ' . $this->last_name
. ' ' . $suffix[$this->suffix_id
]));
376 * Check if there is data to create the object
378 * @param array $params (reference ) an assoc array of name/value pairs
384 static function dataExists(&$params) {
385 if ($params['contact_type'] == 'Individual') {