3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
37 * form helper class for an Email object
39 class CRM_Contact_Form_Inline_Email
extends CRM_Contact_Form_Inline
{
42 * Email addresses of the contact that is been viewed.
44 private $_emails = array();
47 * No of email blocks for inline edit.
49 private $_blockCount = 6;
52 * Whether this contact has a first/last/organization/household name
56 public $contactHasName;
61 public function preProcess() {
64 //get all the existing email addresses
65 $email = new CRM_Core_BAO_Email();
66 $email->contact_id
= $this->_contactId
;
68 $this->_emails
= CRM_Core_BAO_Block
::retrieveBlock($email, NULL);
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'));
76 $this->contactHasName
= (bool) CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId
, strtolower($this->_contactType
) . '_name');
81 * Build the form object elements for an email object.
85 public function buildQuickForm() {
86 parent
::buildQuickForm();
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;
102 $this->assign('actualBlockCount', $actualBlockCount);
103 $this->assign('totalBlocks', $totalBlocks);
105 $this->applyFilter('__ALL__', 'trim');
107 for ($blockId = 1; $blockId < $totalBlocks; $blockId++
) {
108 CRM_Contact_Form_Edit_Email
::buildQuickForm($this, $blockId, TRUE);
111 $this->addFormRule(array('CRM_Contact_Form_Inline_Email', 'formRule'), $this);
115 * Global validation rules for the form.
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
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);
132 $hasData[] = $instance;
133 if (!empty($blockValues['is_primary'])) {
134 $hasPrimary[] = $instance;
139 if (empty($hasPrimary) && !empty($hasData)) {
140 $errors["email[1][is_primary]"] = ts('One email should be marked as primary.');
143 if (count($hasPrimary) > 1) {
144 $errors["email[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one email can be marked as primary.');
147 if (!$hasData && !$form->contactHasName
) {
148 $errors["email[1][email]"] = ts('Contact with no name must have an email.');
154 * Set defaults for the form.
158 public function setDefaultValues() {
160 if (!empty($this->_emails
)) {
161 foreach ($this->_emails
as $id => $value) {
162 $defaults['email'][$id] = $value;
166 // get the default location type
167 $locationType = CRM_Core_BAO_LocationType
::getDefault();
168 $defaults['email'][1]['location_type_id'] = $locationType->id
;
179 public function postProcess() {
180 $params = $this->exportValues();
182 // Process / save emails
183 $params['contact_id'] = $this->_contactId
;
184 $params['updateBlankLocInfo'] = TRUE;
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');