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