3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Form helper class for an Email object.
21 class CRM_Contact_Form_Inline_Email
extends CRM_Contact_Form_Inline
{
24 * Email addresses of the contact that is been viewed.
27 private $_emails = [];
30 * No of email blocks for inline edit.
33 private $_blockCount = 6;
36 * Whether this contact has a first/last/organization/household name
40 public $contactHasName;
45 public function preProcess() {
48 //get all the existing email addresses
49 $email = new CRM_Core_BAO_Email();
50 $email->contact_id
= $this->_contactId
;
52 $this->_emails
= CRM_Core_BAO_Block
::retrieveBlock($email, NULL);
54 // Check if this contact has a first/last/organization/household name
55 if ($this->_contactType
== 'Individual') {
56 $this->contactHasName
= (bool) (CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'last_name')
57 || CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'first_name'));
60 $this->contactHasName
= (bool) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, strtolower($this->_contactType
) . '_name');
65 * Build the form object elements for an email object.
67 public function buildQuickForm() {
68 parent
::buildQuickForm();
70 $totalBlocks = $this->_blockCount
;
71 $actualBlockCount = 1;
72 if (count($this->_emails
) > 1) {
73 $actualBlockCount = $totalBlocks = count($this->_emails
);
74 if ($totalBlocks < $this->_blockCount
) {
75 $additionalBlocks = $this->_blockCount
- $totalBlocks;
76 $totalBlocks +
= $additionalBlocks;
84 $this->assign('actualBlockCount', $actualBlockCount);
85 $this->assign('totalBlocks', $totalBlocks);
87 $this->applyFilter('__ALL__', 'trim');
89 for ($blockId = 1; $blockId < $totalBlocks; $blockId++
) {
90 CRM_Contact_Form_Edit_Email
::buildQuickForm($this, $blockId, TRUE);
93 $this->addFormRule(['CRM_Contact_Form_Inline_Email', 'formRule'], $this);
97 * Global validation rules for the form.
99 * @param array $fields
100 * Posted values of the form.
101 * @param array $errors
102 * List of errors to be posted back to the form.
103 * @param CRM_Contact_Form_Inline_Email $form
107 public static function formRule($fields, $errors, $form) {
108 $hasData = $hasPrimary = $errors = [];
109 if (!empty($fields['email']) && is_array($fields['email'])) {
110 foreach ($fields['email'] as $instance => $blockValues) {
111 $dataExists = CRM_Contact_Form_Contact
::blockDataExists($blockValues);
114 $hasData[] = $instance;
115 if (!empty($blockValues['is_primary'])) {
116 $hasPrimary[] = $instance;
121 if (empty($hasPrimary) && !empty($hasData)) {
122 $errors["email[1][is_primary]"] = ts('One email should be marked as primary.');
125 if (count($hasPrimary) > 1) {
126 $errors["email[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one email can be marked as primary.');
129 if (!$hasData && !$form->contactHasName
) {
130 $errors["email[1][email]"] = ts('Contact with no name must have an email.');
136 * Set defaults for the form.
140 public function setDefaultValues() {
142 if (!empty($this->_emails
)) {
143 foreach ($this->_emails
as $id => $value) {
144 $defaults['email'][$id] = $value;
148 // get the default location type
149 $locationType = CRM_Core_BAO_LocationType
::getDefault();
150 $defaults['email'][1]['location_type_id'] = $locationType->id
;
159 public function postProcess() {
160 $params = $this->exportValues();
162 // Process / save emails
163 $params['contact_id'] = $this->_contactId
;
164 $params['updateBlankLocInfo'] = TRUE;
165 $params['email']['isIdSet'] = TRUE;
166 foreach ($this->_emails
as $count => $value) {
167 if (!empty($value['id']) && isset($params['email'][$count])) {
168 $params['email'][$count]['id'] = $value['id'];
171 CRM_Core_BAO_Block
::create('email', $params);
173 // If contact has no name, set primary email as display name
174 // TODO: This should be handled in the BAO for the benefit of the api, etc.
175 if (!$this->contactHasName
) {
176 foreach ($params['email'] as $email) {
177 if ($email['is_primary']) {
178 CRM_Core_DAO
::setFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'display_name', $email['email']);
179 CRM_Core_DAO
::setFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, 'sort_name', $email['email']);
180 $this->ajaxResponse
['reloadBlocks'] = ['#crm-contactname-content'];