From ed645ef976a94620079b30536b4a9c62ba9c6387 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 11 Mar 2015 21:58:48 -0400 Subject: [PATCH] CRM-16085 - Contact inline edit - Fix handling of empty name/email --- CRM/Contact/Form/Inline/ContactName.php | 25 +++++++++++++++++ CRM/Contact/Form/Inline/Email.php | 37 +++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/Form/Inline/ContactName.php b/CRM/Contact/Form/Inline/ContactName.php index 0a874a1113..95ffa8b44c 100644 --- a/CRM/Contact/Form/Inline/ContactName.php +++ b/CRM/Contact/Form/Inline/ContactName.php @@ -49,6 +49,31 @@ class CRM_Contact_Form_Inline_ContactName extends CRM_Contact_Form_Inline { // Build contact type specific fields $class = 'CRM_Contact_Form_Edit_' . $this->_contactType; $class::buildQuickForm($this, 1); + $this->addFormRule(array('CRM_Contact_Form_Inline_ContactName', 'formRule'), $this); + } + + /** + * Global validation rules for the form. + * + * @param array $fields + * Posted values of the form. + * @param array $errors + * List of errors to be posted back to the form. + * @param CRM_Contact_Form_Inline_ContactName $form + * + * @return array + */ + public static function formRule($fields, $errors, $form) { + if (empty($fields['first_name']) && empty($fields['last_name']) + && empty($fields['organization_name']) + && empty($fields['household_name'])) { + $emails = civicrm_api3('Email', 'getcount', array('contact_id' => $form->_contactId)); + if (!$emails) { + $errorField = $form->_contactType == 'Individual' ? 'last' : strtolower($form->_contactType); + $errors[$errorField . '_name'] = ts('Contact with no email must have a name.'); + } + } + return $errors; } /** diff --git a/CRM/Contact/Form/Inline/Email.php b/CRM/Contact/Form/Inline/Email.php index 8c1b2cab35..79ba69f624 100644 --- a/CRM/Contact/Form/Inline/Email.php +++ b/CRM/Contact/Form/Inline/Email.php @@ -48,6 +48,13 @@ class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline { */ private $_blockCount = 6; + /** + * Whether this contact has a first/last/organization/household name + * + * @var bool + */ + public $contactHasName; + /** * Call preprocess. */ @@ -59,6 +66,15 @@ class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline { $email->contact_id = $this->_contactId; $this->_emails = CRM_Core_BAO_Block::retrieveBlock($email, NULL); + + // Check if this contact has a first/last/organization/household name + if ($this->_contactType == 'Individual') { + $this->contactHasName = (bool) (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'last_name') + || CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'first_name')); + } + else { + $this->contactHasName = (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, strtolower($this->_contactType) . '_name'); + } } /** @@ -92,7 +108,7 @@ class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline { CRM_Contact_Form_Edit_Email::buildQuickForm($this, $blockId, TRUE); } - $this->addFormRule(array('CRM_Contact_Form_Inline_Email', 'formRule')); + $this->addFormRule(array('CRM_Contact_Form_Inline_Email', 'formRule'), $this); } /** @@ -102,10 +118,11 @@ class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline { * Posted values of the form. * @param array $errors * List of errors to be posted back to the form. + * @param CRM_Contact_Form_Inline_Email $form * * @return array */ - public static function formRule($fields, $errors) { + public static function formRule($fields, $errors, $form) { $hasData = $hasPrimary = $errors = array(); if (!empty($fields['email']) && is_array($fields['email'])) { foreach ($fields['email'] as $instance => $blockValues) { @@ -127,6 +144,9 @@ class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline { $errors["email[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one email can be marked as primary.'); } } + if (!$hasData && !$form->contactHasName) { + $errors["email[1][email]"] = ts('Contact with no name must have an email.'); + } return $errors; } @@ -164,6 +184,19 @@ class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline { $params['updateBlankLocInfo'] = TRUE; CRM_Core_BAO_Block::create('email', $params); + // If contact has no name, set primary email as display name + // TODO: This should be handled in the BAO for the benefit of the api, etc. + if (!$this->contactHasName) { + foreach ($params['email'] as $email) { + if ($email['is_primary']) { + CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name', $email['email']); + CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'sort_name', $email['email']); + $this->ajaxResponse['reloadBlocks'] = array('#crm-contactname-content'); + break; + } + } + } + $this->log(); $this->response(); } -- 2.25.1