INFRA-132 - Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines
[civicrm-core.git] / CRM / Contact / Form / Contact.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class generates form components generic to all the contact types.
38 *
39 * It delegates the work to lower level subclasses and integrates the changes
40 * back in. It also uses a lot of functionality with the CRM API's, so any change
41 * made here could potentially affect the API etc. Be careful, be aware, use unit tests.
42 *
43 */
44 class CRM_Contact_Form_Contact extends CRM_Core_Form {
45
46 /**
47 * The contact type of the form
48 *
49 * @var string
50 */
51 public $_contactType;
52
53 /**
54 * The contact type of the form
55 *
56 * @var string
57 */
58 public $_contactSubType;
59
60 /**
61 * The contact id, used when editing the form
62 *
63 * @var int
64 */
65 public $_contactId;
66
67 /**
68 * The default group id passed in via the url
69 *
70 * @var int
71 */
72 public $_gid;
73
74 /**
75 * The default tag id passed in via the url
76 *
77 * @var int
78 */
79 public $_tid;
80
81 /**
82 * Name of de-dupe button
83 *
84 * @var string
85 */
86 protected $_dedupeButtonName;
87
88 /**
89 * Name of optional save duplicate button
90 *
91 * @var string
92 */
93 protected $_duplicateButtonName;
94
95 protected $_editOptions = array();
96
97 protected $_oldSubtypes = array();
98
99 public $_blocks;
100
101 public $_values = array();
102
103 public $_action;
104
105 public $_customValueCount;
106 /**
107 * The array of greetings with option group and filed names
108 *
109 * @var array
110 */
111 public $_greetings;
112
113 /**
114 * Do we want to parse street address.
115 */
116 public $_parseStreetAddress;
117
118 /**
119 * Check contact has a subtype or not
120 */
121 public $_isContactSubType;
122
123 /**
124 * Lets keep a cache of all the values that we retrieved.
125 * THis is an attempt to avoid the number of update statements
126 * during the write phase
127 */
128 public $_preEditValues;
129
130 /**
131 * Build all the data structures needed to build the form
132 *
133 * @return void
134 */
135 public function preProcess() {
136 $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this, FALSE, 'add');
137
138 $this->_dedupeButtonName = $this->getButtonName('refresh', 'dedupe');
139 $this->_duplicateButtonName = $this->getButtonName('upload', 'duplicate');
140
141 $session = CRM_Core_Session::singleton();
142 if ($this->_action == CRM_Core_Action::ADD) {
143 // check for add contacts permissions
144 if (!CRM_Core_Permission::check('add contacts')) {
145 CRM_Utils_System::permissionDenied();
146 CRM_Utils_System::civiExit();
147 }
148 $this->_contactType = CRM_Utils_Request::retrieve('ct', 'String',
149 $this, TRUE, NULL, 'REQUEST'
150 );
151 if (!in_array($this->_contactType,
152 array('Individual', 'Household', 'Organization')
153 )
154 ) {
155 CRM_Core_Error::statusBounce(ts('Could not get a contact id and/or contact type'));
156 }
157
158 $this->_isContactSubType = FALSE;
159 if ($this->_contactSubType = CRM_Utils_Request::retrieve('cst', 'String', $this)) {
160 $this->_isContactSubType = TRUE;
161 }
162
163 if (
164 $this->_contactSubType &&
165 !(CRM_Contact_BAO_ContactType::isExtendsContactType($this->_contactSubType, $this->_contactType, TRUE))
166 ) {
167 CRM_Core_Error::statusBounce(ts("Could not get a valid contact subtype for contact type '%1'", array(1 => $this->_contactType)));
168 }
169
170 $this->_gid = CRM_Utils_Request::retrieve('gid', 'Integer',
171 CRM_Core_DAO::$_nullObject,
172 FALSE, NULL, 'GET'
173 );
174 $this->_tid = CRM_Utils_Request::retrieve('tid', 'Integer',
175 CRM_Core_DAO::$_nullObject,
176 FALSE, NULL, 'GET'
177 );
178 $typeLabel = CRM_Contact_BAO_ContactType::contactTypePairs(TRUE, $this->_contactSubType ?
179 $this->_contactSubType : $this->_contactType
180 );
181 $typeLabel = implode(' / ', $typeLabel);
182
183 CRM_Utils_System::setTitle(ts('New %1', array(1 => $typeLabel)));
184 $session->pushUserContext(CRM_Utils_System::url('civicrm/dashboard', 'reset=1'));
185 $this->_contactId = NULL;
186 }
187 else {
188 //update mode
189 if (!$this->_contactId) {
190 $this->_contactId = CRM_Utils_Request::retrieve('cid', 'Positive', $this, TRUE);
191 }
192
193 if ($this->_contactId) {
194 $defaults = array();
195 $params = array('id' => $this->_contactId);
196 $returnProperities = array('id', 'contact_type', 'contact_sub_type', 'modified_date');
197 CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Contact', $params, $defaults, $returnProperities);
198
199 if (empty($defaults['id'])) {
200 CRM_Core_Error::statusBounce(ts('A Contact with that ID does not exist: %1', array(1 => $this->_contactId)));
201 }
202
203 $this->_contactType = CRM_Utils_Array::value('contact_type', $defaults);
204 $this->_contactSubType = CRM_Utils_Array::value('contact_sub_type', $defaults);
205
206 // check for permissions
207 $session = CRM_Core_Session::singleton();
208 if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) {
209 CRM_Core_Error::statusBounce(ts('You do not have the necessary permission to edit this contact.'));
210 }
211
212 $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId);
213 $displayName = ts('Edit %1', array(1 => $displayName));
214
215 // Check if this is default domain contact CRM-10482
216 if (CRM_Contact_BAO_Contact::checkDomainContact($this->_contactId)) {
217 $displayName .= ' (' . ts('default organization') . ')';
218 }
219
220 // omitting contactImage from title for now since the summary overlay css doesn't work outside of our crm-container
221 CRM_Utils_System::setTitle($displayName);
222 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
223 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
224
225 $urlParams = 'reset=1&cid=' . $this->_contactId;
226 if ($context) {
227 $urlParams .= "&context=$context";
228 }
229
230 if (CRM_Utils_Rule::qfKey($qfKey)) {
231
232 $urlParams .= "&key=$qfKey";
233
234 }
235 $session->pushUserContext(CRM_Utils_System::url('civicrm/contact/view', $urlParams));
236
237 $values = $this->get('values');
238 // get contact values.
239 if (!empty($values)) {
240 $this->_values = $values;
241 }
242 else {
243 $params = array(
244 'id' => $this->_contactId,
245 'contact_id' => $this->_contactId,
246 'noRelationships' => TRUE,
247 'noNotes' => TRUE,
248 'noGroups' => TRUE,
249 );
250
251 $contact = CRM_Contact_BAO_Contact::retrieve($params, $this->_values, TRUE);
252 $this->set('values', $this->_values);
253 }
254 }
255 else {
256 CRM_Core_Error::statusBounce(ts('Could not get a contact_id and/or contact_type'));
257 }
258 }
259
260 // parse street address, CRM-5450
261 $this->_parseStreetAddress = $this->get('parseStreetAddress');
262 if (!isset($this->_parseStreetAddress)) {
263 $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
264 'address_options'
265 );
266 $this->_parseStreetAddress = FALSE;
267 if (!empty($addressOptions['street_address']) && !empty($addressOptions['street_address_parsing'])) {
268 $this->_parseStreetAddress = TRUE;
269 }
270 $this->set('parseStreetAddress', $this->_parseStreetAddress);
271 }
272 $this->assign('parseStreetAddress', $this->_parseStreetAddress);
273
274 $this->_editOptions = $this->get('contactEditOptions');
275 if (CRM_Utils_System::isNull($this->_editOptions)) {
276 $this->_editOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
277 'contact_edit_options', TRUE, NULL,
278 FALSE, 'name', TRUE, 'AND v.filter = 0'
279 );
280 $this->set('contactEditOptions', $this->_editOptions);
281 }
282
283 // build demographics only for Individual contact type
284 if ($this->_contactType != 'Individual' &&
285 array_key_exists('Demographics', $this->_editOptions)
286 ) {
287 unset($this->_editOptions['Demographics']);
288 }
289
290 // in update mode don't show notes
291 if ($this->_contactId && array_key_exists('Notes', $this->_editOptions)) {
292 unset($this->_editOptions['Notes']);
293 }
294
295 $this->assign('editOptions', $this->_editOptions);
296 $this->assign('contactType', $this->_contactType);
297 $this->assign('contactSubType', $this->_contactSubType);
298
299 //build contact subtype form element, CRM-6864
300 $buildContactSubType = TRUE;
301 if ($this->_contactSubType && ($this->_action & CRM_Core_Action::ADD)) {
302 $buildContactSubType = FALSE;
303 }
304 $this->assign('buildContactSubType', $buildContactSubType);
305
306 // get the location blocks.
307 $this->_blocks = $this->get('blocks');
308 if (CRM_Utils_System::isNull($this->_blocks)) {
309 $this->_blocks = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
310 'contact_edit_options', TRUE, NULL,
311 FALSE, 'name', TRUE, 'AND v.filter = 1'
312 );
313 $this->set('blocks', $this->_blocks);
314 }
315 $this->assign('blocks', $this->_blocks);
316
317 // this is needed for custom data.
318 $this->assign('entityID', $this->_contactId);
319
320 // also keep the convention.
321 $this->assign('contactId', $this->_contactId);
322
323 // location blocks.
324 CRM_Contact_Form_Location::preProcess($this);
325
326 // retain the multiple count custom fields value
327 if (!empty($_POST['hidden_custom'])) {
328 $customGroupCount = CRM_Utils_Array::value('hidden_custom_group_count', $_POST);
329
330 if ($contactSubType = CRM_Utils_Array::value('contact_sub_type', $_POST)) {
331 $paramSubType = implode(',', $contactSubType);
332 }
333
334 $this->_getCachedTree = FALSE;
335 unset($customGroupCount[0]);
336 foreach ($customGroupCount as $groupID => $groupCount) {
337 if ($groupCount > 1) {
338 $this->set('groupID', $groupID);
339 //loop the group
340 for ($i = 0; $i <= $groupCount; $i++) {
341 CRM_Custom_Form_CustomData::preProcess($this, NULL, $contactSubType,
342 $i, $this->_contactType
343 );
344 CRM_Contact_Form_Edit_CustomData::buildQuickForm($this);
345 }
346 }
347 }
348
349 //reset all the ajax stuff, for normal processing
350 if (isset($this->_groupTree)) {
351 $this->_groupTree = NULL;
352 }
353 $this->set('groupID', NULL);
354 $this->_getCachedTree = TRUE;
355 }
356
357 // execute preProcess dynamically by js else execute normal preProcess
358 if (array_key_exists('CustomData', $this->_editOptions)) {
359 //assign a parameter to pass for sub type multivalue
360 //custom field to load
361 if ($this->_contactSubType || isset($paramSubType)) {
362 $paramSubType = (isset($paramSubType)) ? $paramSubType :
363 str_replace(CRM_Core_DAO::VALUE_SEPARATOR, ',', trim($this->_contactSubType, CRM_Core_DAO::VALUE_SEPARATOR));
364
365 $this->assign('paramSubType', $paramSubType);
366 }
367
368 if (CRM_Utils_Request::retrieve('type', 'String', CRM_Core_DAO::$_nullObject)) {
369 CRM_Contact_Form_Edit_CustomData::preProcess($this);
370 }
371 else {
372 $contactSubType = $this->_contactSubType;
373 // need contact sub type to build related grouptree array during post process
374 if (!empty($_POST['contact_sub_type'])) {
375 $contactSubType = $_POST['contact_sub_type'];
376 }
377 //only custom data has preprocess hence directly call it
378 CRM_Custom_Form_CustomData::preProcess($this, NULL, $contactSubType,
379 1, $this->_contactType, $this->_contactId
380 );
381 $this->assign('customValueCount', $this->_customValueCount);
382 }
383 }
384 }
385
386 /**
387 * Set default values for the form. Note that in edit/view mode
388 * the default values are retrieved from the database
389 *
390 *
391 * @return void
392 */
393 public function setDefaultValues() {
394 $defaults = $this->_values;
395 $params = array();
396
397 if ($this->_action & CRM_Core_Action::ADD) {
398 if (array_key_exists('TagsAndGroups', $this->_editOptions)) {
399 // set group and tag defaults if any
400 if ($this->_gid) {
401 $defaults['group'][] = $this->_gid;
402 }
403 if ($this->_tid) {
404 $defaults['tag'][$this->_tid] = 1;
405 }
406 }
407 if ($this->_contactSubType) {
408 $defaults['contact_sub_type'] = $this->_contactSubType;
409 }
410 }
411 else {
412 foreach ($defaults['email'] as $dontCare => & $val) {
413 if (isset($val['signature_text'])) {
414 $val['signature_text_hidden'] = $val['signature_text'];
415 }
416 if (isset($val['signature_html'])) {
417 $val['signature_html_hidden'] = $val['signature_html'];
418 }
419 }
420
421 if (!empty($defaults['contact_sub_type'])) {
422 $defaults['contact_sub_type'] = $this->_oldSubtypes;
423 }
424 }
425 // set defaults for blocks ( custom data, address, communication preference, notes, tags and groups )
426 foreach ($this->_editOptions as $name => $label) {
427 if (!in_array($name, array('Address', 'Notes'))) {
428 $className = 'CRM_Contact_Form_Edit_' . $name;
429 $className::setDefaultValues($this, $defaults);
430 }
431 }
432
433 //set address block defaults
434 CRM_Contact_Form_Edit_Address::setDefaultValues($defaults, $this);
435
436 if (!empty($defaults['image_URL'])) {
437 list($imageWidth, $imageHeight) = getimagesize(CRM_Utils_String::unstupifyUrl($defaults['image_URL']));
438 list($imageThumbWidth, $imageThumbHeight) = CRM_Contact_BAO_Contact::getThumbSize($imageWidth, $imageHeight);
439 $this->assign('imageWidth', $imageWidth);
440 $this->assign('imageHeight', $imageHeight);
441 $this->assign('imageThumbWidth', $imageThumbWidth);
442 $this->assign('imageThumbHeight', $imageThumbHeight);
443 $this->assign('imageURL', $defaults['image_URL']);
444 }
445
446 //set location type and country to default for each block
447 $this->blockSetDefaults($defaults);
448
449 $this->_preEditValues = $defaults;
450 return $defaults;
451 }
452
453 /**
454 * Do the set default related to location type id,
455 * primary location, default country
456 */
457 public function blockSetDefaults(&$defaults) {
458 $locationTypeKeys = array_filter(array_keys(CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id')), 'is_int');
459 sort($locationTypeKeys);
460
461 // get the default location type
462 $locationType = CRM_Core_BAO_LocationType::getDefault();
463
464 // unset primary location type
465 $primaryLocationTypeIdKey = CRM_Utils_Array::key($locationType->id, $locationTypeKeys);
466 unset($locationTypeKeys[$primaryLocationTypeIdKey]);
467
468 // reset the array sequence
469 $locationTypeKeys = array_values($locationTypeKeys);
470
471 // get default phone and im provider id.
472 $defPhoneTypeId = key(CRM_Core_OptionGroup::values('phone_type', FALSE, FALSE, FALSE, ' AND is_default = 1'));
473 $defIMProviderId = key(CRM_Core_OptionGroup::values('instant_messenger_service',
474 FALSE, FALSE, FALSE, ' AND is_default = 1'
475 ));
476 $defWebsiteTypeId = key(CRM_Core_OptionGroup::values('website_type',
477 FALSE, FALSE, FALSE, ' AND is_default = 1'
478 ));
479
480 $allBlocks = $this->_blocks;
481 if (array_key_exists('Address', $this->_editOptions)) {
482 $allBlocks['Address'] = $this->_editOptions['Address'];
483 }
484
485 $config = CRM_Core_Config::singleton();
486 foreach ($allBlocks as $blockName => $label) {
487 $name = strtolower($blockName);
488 $hasPrimary = $updateMode = FALSE;
489
490 // user is in update mode.
491 if (array_key_exists($name, $defaults) &&
492 !CRM_Utils_System::isNull($defaults[$name])
493 ) {
494 $updateMode = TRUE;
495 }
496
497 for ($instance = 1; $instance <= $this->get($blockName . '_Block_Count'); $instance++) {
498 // make we require one primary block, CRM-5505
499 if ($updateMode) {
500 if (!$hasPrimary) {
501 $hasPrimary =
502 CRM_Utils_Array::value(
503 'is_primary',
504 CRM_Utils_Array::value($instance, $defaults[$name])
505 );
506 }
507 continue;
508 }
509
510 //set location to primary for first one.
511 if ($instance == 1) {
512 $hasPrimary = TRUE;
513 $defaults[$name][$instance]['is_primary'] = TRUE;
514 $defaults[$name][$instance]['location_type_id'] = $locationType->id;
515 }
516 else {
517 $locTypeId = isset($locationTypeKeys[$instance - 1]) ? $locationTypeKeys[$instance - 1] : $locationType->id;
518 $defaults[$name][$instance]['location_type_id'] = $locTypeId;
519 }
520
521 //set default country
522 if ($name == 'address' && $config->defaultContactCountry) {
523 $defaults[$name][$instance]['country_id'] = $config->defaultContactCountry;
524 }
525
526 //set default state/province
527 if ($name == 'address' && $config->defaultContactStateProvince) {
528 $defaults[$name][$instance]['state_province_id'] = $config->defaultContactStateProvince;
529 }
530
531 //set default phone type.
532 if ($name == 'phone' && $defPhoneTypeId) {
533 $defaults[$name][$instance]['phone_type_id'] = $defPhoneTypeId;
534 }
535 //set default website type.
536 if ($name == 'website' && $defWebsiteTypeId) {
537 $defaults[$name][$instance]['website_type_id'] = $defWebsiteTypeId;
538 }
539
540 //set default im provider.
541 if ($name == 'im' && $defIMProviderId) {
542 $defaults[$name][$instance]['provider_id'] = $defIMProviderId;
543 }
544 }
545
546 if (!$hasPrimary) {
547 $defaults[$name][1]['is_primary'] = TRUE;
548 }
549 }
550 }
551
552 /**
553 * add the rules (mainly global rules) for form.
554 * All local rules are added near the element
555 *
556 * @return void
557 * @see valid_date
558 */
559 public function addRules() {
560 // skip adding formRules when custom data is build
561 if ($this->_addBlockName || ($this->_action & CRM_Core_Action::DELETE)) {
562 return;
563 }
564
565 $this->addFormRule(array('CRM_Contact_Form_Edit_' . $this->_contactType, 'formRule'), $this->_contactId);
566
567 // Call Locking check if editing existing contact
568 if ($this->_contactId) {
569 $this->addFormRule(array('CRM_Contact_Form_Edit_Lock', 'formRule'), $this->_contactId);
570 }
571
572 if (array_key_exists('Address', $this->_editOptions)) {
573 $this->addFormRule(array('CRM_Contact_Form_Edit_Address', 'formRule'), $this);
574 }
575
576 if (array_key_exists('CommunicationPreferences', $this->_editOptions)) {
577 $this->addFormRule(array('CRM_Contact_Form_Edit_CommunicationPreferences', 'formRule'), $this);
578 }
579 }
580
581 /**
582 * Global validation rules for the form
583 *
584 * @param array $fields
585 * Posted values of the form.
586 * @param array $errors
587 * List of errors to be posted back to the form.
588 * @param int $contactId
589 * Contact id if doing update.
590 *
591 * @return bool
592 * email/openId
593 */
594 public static function formRule($fields, &$errors, $contactId = NULL) {
595 $config = CRM_Core_Config::singleton();
596
597 // validations.
598 //1. for each block only single value can be marked as is_primary = true.
599 //2. location type id should be present if block data present.
600 //3. check open id across db and other each block for duplicate.
601 //4. at least one location should be primary.
602 //5. also get primaryID from email or open id block.
603
604 // take the location blocks.
605 $blocks = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
606 'contact_edit_options', TRUE, NULL,
607 FALSE, 'name', TRUE, 'AND v.filter = 1'
608 );
609
610 $otherEditOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
611 'contact_edit_options', TRUE, NULL,
612 FALSE, 'name', TRUE, 'AND v.filter = 0'
613 );
614 //get address block inside.
615 if (array_key_exists('Address', $otherEditOptions)) {
616 $blocks['Address'] = $otherEditOptions['Address'];
617 }
618
619 $openIds = array();
620 $primaryID = FALSE;
621 foreach ($blocks as $name => $label) {
622 $hasData = $hasPrimary = array();
623 $name = strtolower($name);
624 if (!empty($fields[$name]) && is_array($fields[$name])) {
625 foreach ($fields[$name] as $instance => $blockValues) {
626 $dataExists = self::blockDataExists($blockValues);
627
628 if (!$dataExists && $name == 'address') {
629 $dataExists = CRM_Utils_Array::value('use_shared_address', $fields['address'][$instance]);
630 }
631
632 if ($dataExists) {
633 // skip remaining checks for website
634 if ($name == 'website') {
635 continue;
636 }
637
638 $hasData[] = $instance;
639 if (!empty($blockValues['is_primary'])) {
640 $hasPrimary[] = $instance;
641 if (!$primaryID &&
642 in_array($name, array(
643 'email',
644 'openid',
645 )) && !empty($blockValues[$name])
646 ) {
647 $primaryID = $blockValues[$name];
648 }
649 }
650
651 if (empty($blockValues['location_type_id'])) {
652 $errors["{$name}[$instance][location_type_id]"] = ts('The Location Type should be set if there is %1 information.', array(1 => $label));
653 }
654 }
655
656 if ($name == 'openid' && !empty($blockValues[$name])) {
657 $oid = new CRM_Core_DAO_OpenID();
658 $oid->openid = $openIds[$instance] = CRM_Utils_Array::value($name, $blockValues);
659 $cid = isset($contactId) ? $contactId : 0;
660 if ($oid->find(TRUE) && ($oid->contact_id != $cid)) {
661 $errors["{$name}[$instance][openid]"] = ts('%1 already exist.', array(1 => $blocks['OpenID']));
662 }
663 }
664 }
665
666 if (empty($hasPrimary) && !empty($hasData)) {
667 $errors["{$name}[1][is_primary]"] = ts('One %1 should be marked as primary.', array(1 => $label));
668 }
669
670 if (count($hasPrimary) > 1) {
671 $errors["{$name}[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one %1 can be marked as primary.',
672 array(1 => $label)
673 );
674 }
675 }
676 }
677
678 //do validations for all opend ids they should be distinct.
679 if (!empty($openIds) && (count(array_unique($openIds)) != count($openIds))) {
680 foreach ($openIds as $instance => $value) {
681 if (!array_key_exists($instance, array_unique($openIds))) {
682 $errors["openid[$instance][openid]"] = ts('%1 already used.', array(1 => $blocks['OpenID']));
683 }
684 }
685 }
686
687 // street number should be digit + suffix, CRM-5450
688 $parseStreetAddress = CRM_Utils_Array::value('street_address_parsing',
689 CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
690 'address_options'
691 )
692 );
693 if ($parseStreetAddress) {
694 if (isset($fields['address']) &&
695 is_array($fields['address'])
696 ) {
697 $invalidStreetNumbers = array();
698 foreach ($fields['address'] as $cnt => $address) {
699 if ($streetNumber = CRM_Utils_Array::value('street_number', $address)) {
700 $parsedAddress = CRM_Core_BAO_Address::parseStreetAddress($address['street_number']);
701 if (empty($parsedAddress['street_number'])) {
702 $invalidStreetNumbers[] = $cnt;
703 }
704 }
705 }
706
707 if (!empty($invalidStreetNumbers)) {
708 $first = $invalidStreetNumbers[0];
709 foreach ($invalidStreetNumbers as & $num) {
710 $num = CRM_Contact_Form_Contact::ordinalNumber($num);
711 }
712 $errors["address[$first][street_number]"] = ts('The street number you entered for the %1 address block(s) is not in an expected format. Street numbers may include numeric digit(s) followed by other characters. You can still enter the complete street address (unparsed) by clicking "Edit Complete Street Address".', array(1 => implode(', ', $invalidStreetNumbers)));
713 }
714 }
715 }
716
717 return $primaryID;
718 }
719
720 /**
721 * Build the form object
722 *
723 * @return void
724 */
725 public function buildQuickForm() {
726 //load form for child blocks
727 if ($this->_addBlockName) {
728 $className = 'CRM_Contact_Form_Edit_' . $this->_addBlockName;
729 return $className::buildQuickForm($this);
730 }
731
732 if ($this->_action == CRM_Core_Action::UPDATE) {
733 $deleteExtra = ts('Are you sure you want to delete contact image.');
734 $deleteURL = array(
735 CRM_Core_Action::DELETE => array(
736 'name' => ts('Delete Contact Image'),
737 'url' => 'civicrm/contact/image',
738 'qs' => 'reset=1&cid=%%id%%&action=delete',
739 'extra' =>
740 'onclick = "if (confirm( \'' . $deleteExtra . '\' ) ) this.href+=\'&amp;confirmed=1\'; else return false;"',
741 ),
742 );
743 $deleteURL = CRM_Core_Action::formLink($deleteURL,
744 CRM_Core_Action::DELETE,
745 array(
746 'id' => $this->_contactId,
747 ),
748 ts('more'),
749 FALSE,
750 'contact.image.delete',
751 'Contact',
752 $this->_contactId
753 );
754 $this->assign('deleteURL', $deleteURL);
755 }
756
757 //build contact type specific fields
758 $className = 'CRM_Contact_Form_Edit_' . $this->_contactType;
759 $className::buildQuickForm($this);
760
761 // build Custom data if Custom data present in edit option
762 $buildCustomData = 'noCustomDataPresent';
763 if (array_key_exists('CustomData', $this->_editOptions)) {
764 $buildCustomData = "customDataPresent";
765 }
766
767 // subtype is a common field. lets keep it here
768 $subtypes = CRM_Contact_BAO_Contact::buildOptions('contact_sub_type', 'create', array('contact_type' => $this->_contactType));
769 if (!empty($subtypes)) {
770 $sel = $this->add('select', 'contact_sub_type', ts('Contact Type'),
771 $subtypes, FALSE,
772 array(
773 'id' => 'contact_sub_type',
774 'multiple' => 'multiple',
775 'class' => $buildCustomData . ' crm-select2',
776 )
777 );
778 }
779
780 // build edit blocks ( custom data, demographics, communication preference, notes, tags and groups )
781 foreach ($this->_editOptions as $name => $label) {
782 if ($name == 'Address') {
783 $this->_blocks['Address'] = $this->_editOptions['Address'];
784 continue;
785 }
786 if ($name == 'TagsAndGroups') {
787 continue;
788 }
789 $className = 'CRM_Contact_Form_Edit_' . $name;
790 $className::buildQuickForm($this);
791 }
792
793 // build tags and groups
794 CRM_Contact_Form_Edit_TagsAndGroups::buildQuickForm($this, 0, CRM_Contact_Form_Edit_TagsAndGroups::ALL,
795 FALSE, NULL, 'Group(s)', 'Tag(s)', NULL, 'select');
796
797 // build location blocks.
798 CRM_Contact_Form_Edit_Lock::buildQuickForm($this);
799 CRM_Contact_Form_Location::buildQuickForm($this);
800
801 // add attachment
802 $this->addElement('file', 'image_URL', ts('Browse/Upload Image'), 'size=30 maxlength=60');
803 $this->addUploadElement('image_URL');
804
805 // add the dedupe button
806 $this->addElement('submit',
807 $this->_dedupeButtonName,
808 ts('Check for Matching Contact(s)')
809 );
810 $this->addElement('submit',
811 $this->_duplicateButtonName,
812 ts('Save Matching Contact')
813 );
814 $this->addElement('submit',
815 $this->getButtonName('next', 'sharedHouseholdDuplicate'),
816 ts('Save With Duplicate Household')
817 );
818
819 $buttons = array(
820 array(
821 'type' => 'upload',
822 'name' => ts('Save'),
823 'subName' => 'view',
824 'isDefault' => TRUE,
825 ),
826 );
827 if (CRM_Core_Permission::check('add contacts')) {
828 $buttons[] = array(
829 'type' => 'upload',
830 'name' => ts('Save and New'),
831 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
832 'subName' => 'new',
833 );
834 }
835 $buttons[] = array(
836 'type' => 'cancel',
837 'name' => ts('Cancel'),
838 );
839
840 if (!empty($this->_values['contact_sub_type'])) {
841 $this->_oldSubtypes = explode(CRM_Core_DAO::VALUE_SEPARATOR,
842 trim($this->_values['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR)
843 );
844 }
845 $this->assign('oldSubtypes', json_encode($this->_oldSubtypes));
846
847 $this->addButtons($buttons);
848 }
849
850 /**
851 * Form submission of new/edit contact is processed.
852 *
853 *
854 * @return void
855 */
856 public function postProcess() {
857 // check if dedupe button, if so return.
858 $buttonName = $this->controller->getButtonName();
859 if ($buttonName == $this->_dedupeButtonName) {
860 return;
861 }
862
863 //get the submitted values in an array
864 $params = $this->controller->exportValues($this->_name);
865
866 $group = CRM_Utils_Array::value('group', $params);
867 if (!empty($group) && is_array($group)) {
868 unset($params['group']);
869 foreach ($group as $key => $value) {
870 $params['group'][$value] = 1;
871 }
872 }
873
874 CRM_Contact_BAO_Contact_Optimizer::edit($params, $this->_preEditValues);
875
876 if (!empty($params['image_URL'])) {
877 CRM_Contact_BAO_Contact::processImageParams($params);
878 }
879
880 if (is_numeric(CRM_Utils_Array::value('current_employer_id', $params)) && !empty($params['current_employer'])) {
881 $params['current_employer'] = $params['current_employer_id'];
882 }
883
884 // don't carry current_employer_id field,
885 // since we don't want to directly update DAO object without
886 // handling related business logic ( eg related membership )
887 if (isset($params['current_employer_id'])) {
888 unset($params['current_employer_id']);
889 }
890
891 $params['contact_type'] = $this->_contactType;
892 if (empty($params['contact_sub_type']) && $this->_isContactSubType) {
893 $params['contact_sub_type'] = array($this->_contactSubType);
894 }
895
896 if ($this->_contactId) {
897 $params['contact_id'] = $this->_contactId;
898 }
899
900 //make deceased date null when is_deceased = false
901 if ($this->_contactType == 'Individual' && !empty($this->_editOptions['Demographics']) && empty($params['is_deceased'])) {
902 $params['is_deceased'] = FALSE;
903 $params['deceased_date'] = NULL;
904 }
905
906 if (isset($params['contact_id'])) {
907 // process membership status for deceased contact
908 $deceasedParams = array(
909 'contact_id' => CRM_Utils_Array::value('contact_id', $params),
910 'is_deceased' => CRM_Utils_Array::value('is_deceased', $params, FALSE),
911 'deceased_date' => CRM_Utils_Array::value('deceased_date', $params, NULL),
912 );
913 $updateMembershipMsg = $this->updateMembershipStatus($deceasedParams);
914 }
915
916 // action is taken depending upon the mode
917 if ($this->_action & CRM_Core_Action::UPDATE) {
918 CRM_Utils_Hook::pre('edit', $params['contact_type'], $params['contact_id'], $params);
919 }
920 else {
921 CRM_Utils_Hook::pre('create', $params['contact_type'], NULL, $params);
922 }
923
924 $customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, TRUE);
925
926 //CRM-5143
927 //if subtype is set, send subtype as extend to validate subtype customfield
928 $customFieldExtends = (CRM_Utils_Array::value('contact_sub_type', $params)) ? $params['contact_sub_type'] : $params['contact_type'];
929
930 $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params,
931 $customFields,
932 $this->_contactId,
933 $customFieldExtends,
934 TRUE
935 );
936 if ($this->_contactId && !empty($this->_oldSubtypes)) {
937 CRM_Contact_BAO_ContactType::deleteCustomSetForSubtypeMigration($this->_contactId,
938 $params['contact_type'],
939 $this->_oldSubtypes,
940 $params['contact_sub_type']
941 );
942 }
943
944 if (array_key_exists('CommunicationPreferences', $this->_editOptions)) {
945 // this is a chekbox, so mark false if we dont get a POST value
946 $params['is_opt_out'] = CRM_Utils_Array::value('is_opt_out', $params, FALSE);
947 }
948
949 // process shared contact address.
950 CRM_Contact_BAO_Contact_Utils::processSharedAddress($params['address']);
951
952 if (!array_key_exists('TagsAndGroups', $this->_editOptions) && !empty($params['group'])) {
953 unset($params['group']);
954 }
955
956 if (!empty($params['contact_id']) && ($this->_action & CRM_Core_Action::UPDATE) && !empty($params['group'])) {
957 // figure out which all groups are intended to be removed
958 $contactGroupList = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], 'Added');
959 if (is_array($contactGroupList)) {
960 foreach ($contactGroupList as $key) {
961 if ((!array_key_exists($key['group_id'], $params['group']) || $params['group'][$key['group_id']] != 1) && empty($key['is_hidden'])) {
962 $params['group'][$key['group_id']] = -1;
963 }
964 }
965 }
966 }
967
968 // parse street address, CRM-5450
969 $parseStatusMsg = NULL;
970 if ($this->_parseStreetAddress) {
971 $parseResult = self::parseAddress($params);
972 $parseStatusMsg = self::parseAddressStatusMsg($parseResult);
973 }
974
975 // Allow un-setting of location info, CRM-5969
976 $params['updateBlankLocInfo'] = TRUE;
977
978 $contact = CRM_Contact_BAO_Contact::create($params, TRUE, FALSE, TRUE);
979
980 // status message
981 if ($this->_contactId) {
982 $message = ts('%1 has been updated.', array(1 => $contact->display_name));
983 }
984 else {
985 $message = ts('%1 has been created.', array(1 => $contact->display_name));
986 }
987
988 // set the contact ID
989 $this->_contactId = $contact->id;
990
991 if (array_key_exists('TagsAndGroups', $this->_editOptions)) {
992 //add contact to tags
993 CRM_Core_BAO_EntityTag::create($params['tag'], 'civicrm_contact', $params['contact_id']);
994
995 //save free tags
996 if (isset($params['contact_taglist']) && !empty($params['contact_taglist'])) {
997 CRM_Core_Form_Tag::postProcess($params['contact_taglist'], $params['contact_id'], 'civicrm_contact', $this);
998 }
999 }
1000
1001 if (!empty($parseStatusMsg)) {
1002 $message .= "<br />$parseStatusMsg";
1003 }
1004 if (!empty($updateMembershipMsg)) {
1005 $message .= "<br />$updateMembershipMsg";
1006 }
1007
1008 $session = CRM_Core_Session::singleton();
1009 $session->setStatus($message, ts('Contact Saved'), 'success');
1010
1011 // add the recently viewed contact
1012 $recentOther = array();
1013 if (($session->get('userID') == $contact->id) ||
1014 CRM_Contact_BAO_Contact_Permission::allow($contact->id, CRM_Core_Permission::EDIT)
1015 ) {
1016 $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/contact/add', 'reset=1&action=update&cid=' . $contact->id);
1017 }
1018
1019 if (($session->get('userID') != $this->_contactId) && CRM_Core_Permission::check('delete contacts')) {
1020 $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/contact/view/delete', 'reset=1&delete=1&cid=' . $contact->id);
1021 }
1022
1023 CRM_Utils_Recent::add($contact->display_name,
1024 CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $contact->id),
1025 $contact->id,
1026 $this->_contactType,
1027 $contact->id,
1028 $contact->display_name,
1029 $recentOther
1030 );
1031
1032 // here we replace the user context with the url to view this contact
1033 $buttonName = $this->controller->getButtonName();
1034 if ($buttonName == $this->getButtonName('upload', 'new')) {
1035 $resetStr = "reset=1&ct={$contact->contact_type}";
1036 $resetStr .= $this->_contactSubType ? "&cst={$this->_contactSubType}" : '';
1037 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/add', $resetStr));
1038 }
1039 else {
1040 $context = CRM_Utils_Request::retrieve('context', 'String', $this);
1041 $qfKey = CRM_Utils_Request::retrieve('key', 'String', $this);
1042 //validate the qfKey
1043 $urlParams = 'reset=1&cid=' . $contact->id;
1044 if ($context) {
1045 $urlParams .= "&context=$context";
1046 }
1047 if (CRM_Utils_Rule::qfKey($qfKey)) {
1048 $urlParams .= "&key=$qfKey";
1049 }
1050
1051 $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', $urlParams));
1052 }
1053
1054 // now invoke the post hook
1055 if ($this->_action & CRM_Core_Action::UPDATE) {
1056 CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact);
1057 }
1058 else {
1059 CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact);
1060 }
1061 }
1062
1063 /**
1064 * Is there any real significant data in the hierarchical location array
1065 *
1066 * @param array $fields
1067 * The hierarchical value representation of this location.
1068 *
1069 * @return boolean
1070 * true if data exists, false otherwise
1071 */
1072 public static function blockDataExists(&$fields) {
1073 if (!is_array($fields)) {
1074 return FALSE;
1075 }
1076
1077 static $skipFields = array(
1078 'location_type_id',
1079 'is_primary',
1080 'phone_type_id',
1081 'provider_id',
1082 'country_id',
1083 'website_type_id',
1084 'master_id',
1085 );
1086 foreach ($fields as $name => $value) {
1087 $skipField = FALSE;
1088 foreach ($skipFields as $skip) {
1089 if (strpos("[$skip]", $name) !== FALSE) {
1090 if ($name == 'phone') {
1091 continue;
1092 }
1093 $skipField = TRUE;
1094 break;
1095 }
1096 }
1097 if ($skipField) {
1098 continue;
1099 }
1100 if (is_array($value)) {
1101 if (self::blockDataExists($value)) {
1102 return TRUE;
1103 }
1104 }
1105 else {
1106 if (!empty($value)) {
1107 return TRUE;
1108 }
1109 }
1110 }
1111
1112 return FALSE;
1113 }
1114
1115 /**
1116 * That checks for duplicate contacts
1117 *
1118 * @param array $fields
1119 * Fields array which are submitted.
1120 * @param $errors
1121 * @param int $contactID
1122 * Contact id.
1123 * @param string $contactType
1124 * Contact type.
1125 */
1126 public static function checkDuplicateContacts(&$fields, &$errors, $contactID, $contactType) {
1127 // if this is a forced save, ignore find duplicate rule
1128 if (empty($fields['_qf_Contact_upload_duplicate'])) {
1129
1130 $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, $contactType);
1131 $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $contactType, 'Supervised', array($contactID));
1132 if ($ids) {
1133
1134 $contactLinks = CRM_Contact_BAO_Contact_Utils::formatContactIDSToLinks($ids, TRUE, TRUE, $contactID);
1135
1136 $duplicateContactsLinks = '<div class="matching-contacts-found">';
1137 $duplicateContactsLinks .= ts('One matching contact was found. ', array(
1138 'count' => count($contactLinks['rows']),
1139 'plural' => '%count matching contacts were found.<br />',
1140 ));
1141 if ($contactLinks['msg'] == 'view') {
1142 $duplicateContactsLinks .= ts('You can View the existing contact', array(
1143 'count' => count($contactLinks['rows']),
1144 'plural' => 'You can View the existing contacts',
1145 ));
1146 }
1147 else {
1148 $duplicateContactsLinks .= ts('You can View or Edit the existing contact', array(
1149 'count' => count($contactLinks['rows']),
1150 'plural' => 'You can View or Edit the existing contacts',
1151 ));
1152 }
1153 if ($contactLinks['msg'] == 'merge') {
1154 // We should also get a merge link if this is for an existing contact
1155 $duplicateContactsLinks .= ts(', or Merge this contact with an existing contact');
1156 }
1157 $duplicateContactsLinks .= '.';
1158 $duplicateContactsLinks .= '</div>';
1159 $duplicateContactsLinks .= '<table class="matching-contacts-actions">';
1160 $row = '';
1161 for ($i = 0; $i < count($contactLinks['rows']); $i++) {
1162 $row .= ' <tr> ';
1163 $row .= ' <td class="matching-contacts-name"> ';
1164 $row .= $contactLinks['rows'][$i]['display_name'];
1165 $row .= ' </td>';
1166 $row .= ' <td class="matching-contacts-email"> ';
1167 $row .= $contactLinks['rows'][$i]['primary_email'];
1168 $row .= ' </td>';
1169 $row .= ' <td class="action-items"> ';
1170 $row .= $contactLinks['rows'][$i]['view'];
1171 $row .= $contactLinks['rows'][$i]['edit'];
1172 $row .= CRM_Utils_Array::value('merge', $contactLinks['rows'][$i]);
1173 $row .= ' </td>';
1174 $row .= ' </tr> ';
1175 }
1176
1177 $duplicateContactsLinks .= $row . '</table>';
1178 $duplicateContactsLinks .= ts("If you're sure this record is not a duplicate, click the 'Save Matching Contact' button below.");
1179
1180 $errors['_qf_default'] = $duplicateContactsLinks;
1181
1182 // let smarty know that there are duplicates
1183 $template = CRM_Core_Smarty::singleton();
1184 $template->assign('isDuplicate', 1);
1185 }
1186 elseif (!empty($fields['_qf_Contact_refresh_dedupe'])) {
1187 // add a session message for no matching contacts
1188 CRM_Core_Session::setStatus(ts('No matching contact found.'), ts('None Found'), 'info');
1189 }
1190 }
1191 }
1192
1193 /**
1194 * Use the form name to create the tpl file name
1195 *
1196 * @return string
1197 */
1198 public function getTemplateFileName() {
1199 if ($this->_contactSubType) {
1200 $templateFile = "CRM/Contact/Form/Edit/SubType/{$this->_contactSubType}.tpl";
1201 $template = CRM_Core_Form::getTemplate();
1202 if ($template->template_exists($templateFile)) {
1203 return $templateFile;
1204 }
1205 }
1206 return parent::getTemplateFileName();
1207 }
1208
1209 /**
1210 * Parse all address blocks present in given params
1211 * and return parse result for all address blocks,
1212 * This function either parse street address in to child
1213 * elements or build street address from child elements.
1214 *
1215 * @param array $params
1216 * of key value consist of address blocks.
1217 *
1218 * @return array
1219 * as array of sucess/fails for each address block
1220 */
1221 public function parseAddress(&$params) {
1222 $parseSuccess = $parsedFields = array();
1223 if (!is_array($params['address']) ||
1224 CRM_Utils_System::isNull($params['address'])
1225 ) {
1226 return $parseSuccess;
1227 }
1228
1229 foreach ($params['address'] as $instance => & $address) {
1230 $buildStreetAddress = FALSE;
1231 $parseFieldName = 'street_address';
1232 foreach (array(
1233 'street_number',
1234 'street_name',
1235 'street_unit',
1236 ) as $fld) {
1237 if (!empty($address[$fld])) {
1238 $parseFieldName = 'street_number';
1239 $buildStreetAddress = TRUE;
1240 break;
1241 }
1242 }
1243
1244 // main parse string.
1245 $parseString = CRM_Utils_Array::value($parseFieldName, $address);
1246
1247 // parse address field.
1248 $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($parseString);
1249
1250 if ($buildStreetAddress) {
1251 //hack to ignore spaces between number and suffix.
1252 //here user gives input as street_number so it has to
1253 //be street_number and street_number_suffix, but
1254 //due to spaces though preg detect string as street_name
1255 //consider it as 'street_number_suffix'.
1256 $suffix = $parsedFields['street_number_suffix'];
1257 if (!$suffix) {
1258 $suffix = $parsedFields['street_name'];
1259 }
1260 $address['street_number_suffix'] = $suffix;
1261 $address['street_number'] = $parsedFields['street_number'];
1262
1263 $streetAddress = NULL;
1264 foreach (array(
1265 'street_number',
1266 'street_number_suffix',
1267 'street_name',
1268 'street_unit',
1269 ) as $fld) {
1270 if (in_array($fld, array(
1271 'street_name',
1272 'street_unit',
1273 ))) {
1274 $streetAddress .= ' ';
1275 }
1276 $streetAddress .= CRM_Utils_Array::value($fld, $address);
1277 }
1278 $address['street_address'] = trim($streetAddress);
1279 $parseSuccess[$instance] = TRUE;
1280 }
1281 else {
1282 $success = TRUE;
1283 // consider address is automatically parseable,
1284 // when we should found street_number and street_name
1285 if (empty($parsedFields['street_name']) || empty($parsedFields['street_number'])) {
1286 $success = FALSE;
1287 }
1288
1289 // check for original street address string.
1290 if (empty($parseString)) {
1291 $success = TRUE;
1292 }
1293
1294 $parseSuccess[$instance] = $success;
1295
1296 // we do not reset element values, but keep what we've parsed
1297 // in case of partial matches: CRM-8378
1298
1299 // merge parse address in to main address block.
1300 $address = array_merge($address, $parsedFields);
1301 }
1302 }
1303
1304 return $parseSuccess;
1305 }
1306
1307 /**
1308 * Check parse result and if some address block fails then this
1309 * function return the status message for all address blocks.
1310 *
1311 * @param array $parseResult
1312 * An array of address blk instance and its status.
1313 *
1314 * @return null|string
1315 * $statusMsg string status message for all address blocks.
1316 */
1317 public static function parseAddressStatusMsg($parseResult) {
1318 $statusMsg = NULL;
1319 if (!is_array($parseResult) || empty($parseResult)) {
1320 return $statusMsg;
1321 }
1322
1323 $parseFails = array();
1324 foreach ($parseResult as $instance => $success) {
1325 if (!$success) {
1326 $parseFails[] = self::ordinalNumber($instance);
1327 }
1328 }
1329
1330 if (!empty($parseFails)) {
1331 $statusMsg = ts("Complete street address(es) have been saved. However we were unable to split the address in the %1 address block(s) into address elements (street number, street name, street unit) due to an unrecognized address format. You can set the address elements manually by clicking 'Edit Address Elements' next to the Street Address field while in edit mode.",
1332 array(1 => implode(', ', $parseFails))
1333 );
1334 }
1335
1336 return $statusMsg;
1337 }
1338
1339 /**
1340 * Convert normal number to ordinal number format.
1341 * like 1 => 1st, 2 => 2nd and so on...
1342 *
1343 * @param int $number
1344 * number to convert in to ordinal number.
1345 *
1346 * @return string
1347 * ordinal number for given number.
1348 */
1349 public static function ordinalNumber($number) {
1350 if (empty($number)) {
1351 return NULL;
1352 }
1353
1354 $str = 'th';
1355 switch (floor($number / 10) % 10) {
1356 case 1:
1357 default:
1358 switch ($number % 10) {
1359 case 1:
1360 $str = 'st';
1361 break;
1362
1363 case 2:
1364 $str = 'nd';
1365 break;
1366
1367 case 3:
1368 $str = 'rd';
1369 break;
1370 }
1371 }
1372
1373 return "$number$str";
1374 }
1375
1376 /**
1377 * Update membership status to deceased
1378 * function return the status message for updated membership.
1379 *
1380 * @param array $deceasedParams
1381 * having contact id and deceased value.
1382 *
1383 * @return null|string
1384 * $updateMembershipMsg string status message for updated membership.
1385 */
1386 public function updateMembershipStatus($deceasedParams) {
1387 $updateMembershipMsg = NULL;
1388 $contactId = CRM_Utils_Array::value('contact_id', $deceasedParams);
1389 $deceasedDate = CRM_Utils_Array::value('deceased_date', $deceasedParams);
1390
1391 // process to set membership status to deceased for both active/inactive membership
1392 if ($contactId &&
1393 $this->_contactType == 'Individual' && !empty($deceasedParams['is_deceased'])
1394 ) {
1395
1396 $session = CRM_Core_Session::singleton();
1397 $userId = $session->get('userID');
1398 if (!$userId) {
1399 $userId = $contactId;
1400 }
1401
1402 // get deceased status id
1403 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
1404 $deceasedStatusId = array_search('Deceased', $allStatus);
1405 if (!$deceasedStatusId) {
1406 return $updateMembershipMsg;
1407 }
1408
1409 $today = time();
1410 if ($deceasedDate && strtotime($deceasedDate) > $today) {
1411 return $updateMembershipMsg;
1412 }
1413
1414 // get non deceased membership
1415 $dao = new CRM_Member_DAO_Membership();
1416 $dao->contact_id = $contactId;
1417 $dao->whereAdd("status_id != $deceasedStatusId");
1418 $dao->find();
1419 $activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name');
1420 $allStatus = CRM_Member_PseudoConstant::membershipStatus();
1421 $memCount = 0;
1422 while ($dao->fetch()) {
1423 // update status to deceased (for both active/inactive membership )
1424 CRM_Core_DAO::setFieldValue('CRM_Member_DAO_Membership', $dao->id,
1425 'status_id', $deceasedStatusId
1426 );
1427
1428 // add membership log
1429 $membershipLog = array(
1430 'membership_id' => $dao->id,
1431 'status_id' => $deceasedStatusId,
1432 'start_date' => CRM_Utils_Date::isoToMysql($dao->start_date),
1433 'end_date' => CRM_Utils_Date::isoToMysql($dao->end_date),
1434 'modified_id' => $userId,
1435 'modified_date' => date('Ymd'),
1436 'membership_type_id' => $dao->membership_type_id,
1437 'max_related' => $dao->max_related,
1438 );
1439
1440 CRM_Member_BAO_MembershipLog::add($membershipLog, CRM_Core_DAO::$_nullArray);
1441
1442 //create activity when membership status is changed
1443 $activityParam = array(
1444 'subject' => "Status changed from {$allStatus[$dao->status_id]} to {$allStatus[$deceasedStatusId]}",
1445 'source_contact_id' => $userId,
1446 'target_contact_id' => $dao->contact_id,
1447 'source_record_id' => $dao->id,
1448 'activity_type_id' => array_search('Change Membership Status', $activityTypes),
1449 'status_id' => 2,
1450 'version' => 3,
1451 'priority_id' => 2,
1452 'activity_date_time' => date('Y-m-d H:i:s'),
1453 'is_auto' => 0,
1454 'is_current_revision' => 1,
1455 'is_deleted' => 0,
1456 );
1457 $activityResult = civicrm_api('activity', 'create', $activityParam);
1458
1459 $memCount++;
1460 }
1461
1462 // set status msg
1463 if ($memCount) {
1464 $updateMembershipMsg = ts("%1 Current membership(s) for this contact have been set to 'Deceased' status.",
1465 array(1 => $memCount)
1466 );
1467 }
1468 }
1469
1470 return $updateMembershipMsg;
1471 }
1472
1473 }