Merge pull request #9325 from colemanw/multicurrency
[civicrm-core.git] / CRM / Contact / Form / Inline / Email.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
32 */
33
34 /**
35 * Form helper class for an Email object.
36 */
37 class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline {
38
39 /**
40 * Email addresses of the contact that is been viewed.
41 */
42 private $_emails = array();
43
44 /**
45 * No of email blocks for inline edit.
46 */
47 private $_blockCount = 6;
48
49 /**
50 * Whether this contact has a first/last/organization/household name
51 *
52 * @var bool
53 */
54 public $contactHasName;
55
56 /**
57 * Call preprocess.
58 */
59 public function preProcess() {
60 parent::preProcess();
61
62 //get all the existing email addresses
63 $email = new CRM_Core_BAO_Email();
64 $email->contact_id = $this->_contactId;
65
66 $this->_emails = CRM_Core_BAO_Block::retrieveBlock($email, NULL);
67
68 // Check if this contact has a first/last/organization/household name
69 if ($this->_contactType == 'Individual') {
70 $this->contactHasName = (bool) (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'last_name')
71 || CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'first_name'));
72 }
73 else {
74 $this->contactHasName = (bool) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, strtolower($this->_contactType) . '_name');
75 }
76 }
77
78 /**
79 * Build the form object elements for an email object.
80 */
81 public function buildQuickForm() {
82 parent::buildQuickForm();
83
84 $totalBlocks = $this->_blockCount;
85 $actualBlockCount = 1;
86 if (count($this->_emails) > 1) {
87 $actualBlockCount = $totalBlocks = count($this->_emails);
88 if ($totalBlocks < $this->_blockCount) {
89 $additionalBlocks = $this->_blockCount - $totalBlocks;
90 $totalBlocks += $additionalBlocks;
91 }
92 else {
93 $actualBlockCount++;
94 $totalBlocks++;
95 }
96 }
97
98 $this->assign('actualBlockCount', $actualBlockCount);
99 $this->assign('totalBlocks', $totalBlocks);
100
101 $this->applyFilter('__ALL__', 'trim');
102
103 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
104 CRM_Contact_Form_Edit_Email::buildQuickForm($this, $blockId, TRUE);
105 }
106
107 $this->addFormRule(array('CRM_Contact_Form_Inline_Email', 'formRule'), $this);
108 }
109
110 /**
111 * Global validation rules for the form.
112 *
113 * @param array $fields
114 * Posted values of the form.
115 * @param array $errors
116 * List of errors to be posted back to the form.
117 * @param CRM_Contact_Form_Inline_Email $form
118 *
119 * @return array
120 */
121 public static function formRule($fields, $errors, $form) {
122 $hasData = $hasPrimary = $errors = array();
123 if (!empty($fields['email']) && is_array($fields['email'])) {
124 foreach ($fields['email'] as $instance => $blockValues) {
125 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
126
127 if ($dataExists) {
128 $hasData[] = $instance;
129 if (!empty($blockValues['is_primary'])) {
130 $hasPrimary[] = $instance;
131 }
132 }
133 }
134
135 if (empty($hasPrimary) && !empty($hasData)) {
136 $errors["email[1][is_primary]"] = ts('One email should be marked as primary.');
137 }
138
139 if (count($hasPrimary) > 1) {
140 $errors["email[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one email can be marked as primary.');
141 }
142 }
143 if (!$hasData && !$form->contactHasName) {
144 $errors["email[1][email]"] = ts('Contact with no name must have an email.');
145 }
146 return $errors;
147 }
148
149 /**
150 * Set defaults for the form.
151 *
152 * @return array
153 */
154 public function setDefaultValues() {
155 $defaults = array();
156 if (!empty($this->_emails)) {
157 foreach ($this->_emails as $id => $value) {
158 $defaults['email'][$id] = $value;
159 }
160 }
161 else {
162 // get the default location type
163 $locationType = CRM_Core_BAO_LocationType::getDefault();
164 $defaults['email'][1]['location_type_id'] = $locationType->id;
165 }
166
167 return $defaults;
168 }
169
170 /**
171 * Process the form.
172 */
173 public function postProcess() {
174 $params = $this->exportValues();
175
176 // Process / save emails
177 $params['contact_id'] = $this->_contactId;
178 $params['updateBlankLocInfo'] = TRUE;
179 $params['email']['isIdSet'] = TRUE;
180 foreach ($this->_emails as $count => $value) {
181 if (!empty($value['id']) && isset($params['email'][$count])) {
182 $params['email'][$count]['id'] = $value['id'];
183 }
184 }
185 CRM_Core_BAO_Block::create('email', $params);
186
187 // If contact has no name, set primary email as display name
188 // TODO: This should be handled in the BAO for the benefit of the api, etc.
189 if (!$this->contactHasName) {
190 foreach ($params['email'] as $email) {
191 if ($email['is_primary']) {
192 CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name', $email['email']);
193 CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'sort_name', $email['email']);
194 $this->ajaxResponse['reloadBlocks'] = array('#crm-contactname-content');
195 break;
196 }
197 }
198 }
199
200 $this->log();
201 $this->response();
202 }
203
204 }