3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2015
35 * Form helper class for an Email object.
37 class CRM_Contact_Form_Inline_Email
extends CRM_Contact_Form_Inline
{
40 * Email addresses of the contact that is been viewed.
42 private $_emails = array();
45 * No of email blocks for inline edit.
47 private $_blockCount = 6;
50 * Whether this contact has a first/last/organization/household name
54 public $contactHasName;
59 public function preProcess() {
62 //get all the existing email addresses
63 $email = new CRM_Core_BAO_Email();
64 $email->contact_id
= $this->_contactId
;
66 $this->_emails
= CRM_Core_BAO_Block
::retrieveBlock($email, NULL);
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'));
74 $this->contactHasName
= (bool) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, strtolower($this->_contactType
) . '_name');
79 * Build the form object elements for an email object.
81 public function buildQuickForm() {
82 parent
::buildQuickForm();
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;
98 $this->assign('actualBlockCount', $actualBlockCount);
99 $this->assign('totalBlocks', $totalBlocks);
101 $this->applyFilter('__ALL__', 'trim');
103 for ($blockId = 1; $blockId < $totalBlocks; $blockId++
) {
104 CRM_Contact_Form_Edit_Email
::buildQuickForm($this, $blockId, TRUE);
107 $this->addFormRule(array('CRM_Contact_Form_Inline_Email', 'formRule'), $this);
111 * Global validation rules for the form.
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
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);
128 $hasData[] = $instance;
129 if (!empty($blockValues['is_primary'])) {
130 $hasPrimary[] = $instance;
135 if (empty($hasPrimary) && !empty($hasData)) {
136 $errors["email[1][is_primary]"] = ts('One email should be marked as primary.');
139 if (count($hasPrimary) > 1) {
140 $errors["email[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one email can be marked as primary.');
143 if (!$hasData && !$form->contactHasName
) {
144 $errors["email[1][email]"] = ts('Contact with no name must have an email.');
150 * Set defaults for the form.
154 public function setDefaultValues() {
156 if (!empty($this->_emails
)) {
157 foreach ($this->_emails
as $id => $value) {
158 $defaults['email'][$id] = $value;
162 // get the default location type
163 $locationType = CRM_Core_BAO_LocationType
::getDefault();
164 $defaults['email'][1]['location_type_id'] = $locationType->id
;
173 public function postProcess() {
174 $params = $this->exportValues();
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'];
185 CRM_Core_BAO_Block
::create('email', $params);
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');