Merge pull request #5101 from agh1/context-abbrev
[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 * Call preprocess.
53 */
54 public function preProcess() {
55 parent::preProcess();
56
57 //get all the existing email addresses
58 $email = new CRM_Core_BAO_Email();
59 $email->contact_id = $this->_contactId;
60
61 $this->_emails = CRM_Core_BAO_Block::retrieveBlock($email, NULL);
62 }
63
64 /**
65 * Build the form object elements for an email object.
66 *
67 * @return void
68 */
69 public function buildQuickForm() {
70 parent::buildQuickForm();
71
72 $totalBlocks = $this->_blockCount;
73 $actualBlockCount = 1;
74 if (count($this->_emails) > 1) {
75 $actualBlockCount = $totalBlocks = count($this->_emails);
76 if ($totalBlocks < $this->_blockCount) {
77 $additionalBlocks = $this->_blockCount - $totalBlocks;
78 $totalBlocks += $additionalBlocks;
79 }
80 else {
81 $actualBlockCount++;
82 $totalBlocks++;
83 }
84 }
85
86 $this->assign('actualBlockCount', $actualBlockCount);
87 $this->assign('totalBlocks', $totalBlocks);
88
89 $this->applyFilter('__ALL__', 'trim');
90
91 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
92 CRM_Contact_Form_Edit_Email::buildQuickForm($this, $blockId, TRUE);
93 }
94
95 $this->addFormRule(array('CRM_Contact_Form_Inline_Email', 'formRule'));
96 }
97
98 /**
99 * Global validation rules for the form.
100 *
101 * @param array $fields
102 * Posted values of the form.
103 * @param array $errors
104 * List of errors to be posted back to the form.
105 *
106 * @return array
107 */
108 public static function formRule($fields, $errors) {
109 $hasData = $hasPrimary = $errors = array();
110 if (!empty($fields['email']) && is_array($fields['email'])) {
111 foreach ($fields['email'] as $instance => $blockValues) {
112 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
113
114 if ($dataExists) {
115 $hasData[] = $instance;
116 if (!empty($blockValues['is_primary'])) {
117 $hasPrimary[] = $instance;
118 }
119 }
120 }
121
122 if (empty($hasPrimary) && !empty($hasData)) {
123 $errors["email[1][is_primary]"] = ts('One email should be marked as primary.');
124 }
125
126 if (count($hasPrimary) > 1) {
127 $errors["email[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one email can be marked as primary.');
128 }
129 }
130 return $errors;
131 }
132
133 /**
134 * Set defaults for the form.
135 *
136 * @return array
137 */
138 public function setDefaultValues() {
139 $defaults = array();
140 if (!empty($this->_emails)) {
141 foreach ($this->_emails as $id => $value) {
142 $defaults['email'][$id] = $value;
143 }
144 }
145 else {
146 // get the default location type
147 $locationType = CRM_Core_BAO_LocationType::getDefault();
148 $defaults['email'][1]['location_type_id'] = $locationType->id;
149 }
150
151 return $defaults;
152 }
153
154 /**
155 * Process the form.
156 *
157 * @return void
158 */
159 public function postProcess() {
160 $params = $this->exportValues();
161
162 // Process / save emails
163 $params['contact_id'] = $this->_contactId;
164 $params['updateBlankLocInfo'] = TRUE;
165 CRM_Core_BAO_Block::create('email', $params);
166
167 $this->log();
168 $this->response();
169 }
170
171 }