CIVICRM-1391 Update Smart Group form unsets the existing Group Types for the Group
[civicrm-core.git] / CRM / Contact / Form / Inline / Email.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
5a409b50 19 * Form helper class for an Email object.
6a488035
TO
20 */
21class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline {
22
23 /**
fe482240 24 * Email addresses of the contact that is been viewed.
69078420 25 * @var array
6a488035 26 */
be2fb01f 27 private $_emails = [];
6a488035
TO
28
29 /**
fe482240 30 * No of email blocks for inline edit.
69078420 31 * @var int
6a488035
TO
32 */
33 private $_blockCount = 6;
34
ed645ef9
CW
35 /**
36 * Whether this contact has a first/last/organization/household name
37 *
38 * @var bool
39 */
40 public $contactHasName;
41
6a488035 42 /**
fe482240 43 * Call preprocess.
6a488035
TO
44 */
45 public function preProcess() {
46 parent::preProcess();
47
48 //get all the existing email addresses
49 $email = new CRM_Core_BAO_Email();
50 $email->contact_id = $this->_contactId;
51
52 $this->_emails = CRM_Core_BAO_Block::retrieveBlock($email, NULL);
ed645ef9
CW
53
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'));
58 }
59 else {
60 $this->contactHasName = (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, strtolower($this->_contactType) . '_name');
61 }
6a488035
TO
62 }
63
64 /**
fe482240 65 * Build the form object elements for an email object.
6a488035
TO
66 */
67 public function buildQuickForm() {
68 parent::buildQuickForm();
69
70 $totalBlocks = $this->_blockCount;
71 $actualBlockCount = 1;
72 if (count($this->_emails) > 1) {
73 $actualBlockCount = $totalBlocks = count($this->_emails);
74 if ($totalBlocks < $this->_blockCount) {
353ffa53
TO
75 $additionalBlocks = $this->_blockCount - $totalBlocks;
76 $totalBlocks += $additionalBlocks;
77 }
6a488035
TO
78 else {
79 $actualBlockCount++;
80 $totalBlocks++;
81 }
82 }
83
84 $this->assign('actualBlockCount', $actualBlockCount);
85 $this->assign('totalBlocks', $totalBlocks);
86
87 $this->applyFilter('__ALL__', 'trim');
88
89 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
90 CRM_Contact_Form_Edit_Email::buildQuickForm($this, $blockId, TRUE);
91 }
92
be2fb01f 93 $this->addFormRule(['CRM_Contact_Form_Inline_Email', 'formRule'], $this);
6a488035
TO
94 }
95
96 /**
fe482240 97 * Global validation rules for the form.
6a488035 98 *
77c5b619
TO
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.
ed645ef9 103 * @param CRM_Contact_Form_Inline_Email $form
6a488035 104 *
a6c01b45 105 * @return array
6a488035 106 */
ed645ef9 107 public static function formRule($fields, $errors, $form) {
be2fb01f 108 $hasData = $hasPrimary = $errors = [];
a7488080 109 if (!empty($fields['email']) && is_array($fields['email'])) {
6a488035
TO
110 foreach ($fields['email'] as $instance => $blockValues) {
111 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
112
113 if ($dataExists) {
114 $hasData[] = $instance;
a7488080 115 if (!empty($blockValues['is_primary'])) {
6a488035 116 $hasPrimary[] = $instance;
6a488035
TO
117 }
118 }
353ffa53 119 }
6a488035 120
6a488035
TO
121 if (empty($hasPrimary) && !empty($hasData)) {
122 $errors["email[1][is_primary]"] = ts('One email should be marked as primary.');
123 }
124
125 if (count($hasPrimary) > 1) {
92fcb95f 126 $errors["email[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one email can be marked as primary.');
6a488035
TO
127 }
128 }
ed645ef9
CW
129 if (!$hasData && !$form->contactHasName) {
130 $errors["email[1][email]"] = ts('Contact with no name must have an email.');
131 }
6a488035
TO
132 return $errors;
133 }
134
135 /**
fe482240 136 * Set defaults for the form.
6a488035
TO
137 *
138 * @return array
6a488035
TO
139 */
140 public function setDefaultValues() {
be2fb01f 141 $defaults = [];
6a488035
TO
142 if (!empty($this->_emails)) {
143 foreach ($this->_emails as $id => $value) {
144 $defaults['email'][$id] = $value;
145 }
146 }
147 else {
148 // get the default location type
149 $locationType = CRM_Core_BAO_LocationType::getDefault();
150 $defaults['email'][1]['location_type_id'] = $locationType->id;
151 }
152
153 return $defaults;
154 }
155
156 /**
fe482240 157 * Process the form.
6a488035
TO
158 */
159 public function postProcess() {
160 $params = $this->exportValues();
161
162 // Process / save emails
163 $params['contact_id'] = $this->_contactId;
164 $params['updateBlankLocInfo'] = TRUE;
a3b489b9 165 $params['email']['isIdSet'] = TRUE;
85c882c5 166 foreach ($this->_emails as $count => $value) {
167 if (!empty($value['id']) && isset($params['email'][$count])) {
168 $params['email'][$count]['id'] = $value['id'];
169 }
170 }
6a488035
TO
171 CRM_Core_BAO_Block::create('email', $params);
172
ed645ef9
CW
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']);
be2fb01f 180 $this->ajaxResponse['reloadBlocks'] = ['#crm-contactname-content'];
ed645ef9
CW
181 break;
182 }
183 }
184 }
185
6a488035
TO
186 $this->log();
187 $this->response();
188 }
96025800 189
6a488035 190}