Merge pull request #12364 from JMAConsulting/dev-core-210
[civicrm-core.git] / CRM / Contact / Form / Inline / Phone.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
32 */
33
34 /**
35 * Form helper class for an Phone object.
36 */
37 class CRM_Contact_Form_Inline_Phone extends CRM_Contact_Form_Inline {
38
39 /**
40 * Phones of the contact that is been viewed
41 */
42 private $_phones = array();
43
44 /**
45 * No of phone blocks for inline edit
46 */
47 private $_blockCount = 6;
48
49 /**
50 * Call preprocess.
51 */
52 public function preProcess() {
53 parent::preProcess();
54
55 //get all the existing phones
56 $phone = new CRM_Core_BAO_Phone();
57 $phone->contact_id = $this->_contactId;
58
59 $this->_phones = CRM_Core_BAO_Block::retrieveBlock($phone, NULL);
60 }
61
62 /**
63 * Build the form object elements for phone object.
64 */
65 public function buildQuickForm() {
66 parent::buildQuickForm();
67
68 $totalBlocks = $this->_blockCount;
69 $actualBlockCount = 1;
70 if (count($this->_phones) > 1) {
71 $actualBlockCount = $totalBlocks = count($this->_phones);
72 if ($totalBlocks < $this->_blockCount) {
73 $additionalBlocks = $this->_blockCount - $totalBlocks;
74 $totalBlocks += $additionalBlocks;
75 }
76 else {
77 $actualBlockCount++;
78 $totalBlocks++;
79 }
80 }
81
82 $this->assign('actualBlockCount', $actualBlockCount);
83 $this->assign('totalBlocks', $totalBlocks);
84
85 $this->applyFilter('__ALL__', 'trim');
86
87 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
88 CRM_Contact_Form_Edit_Phone::buildQuickForm($this, $blockId, TRUE);
89 }
90
91 $this->addFormRule(array('CRM_Contact_Form_Inline_Phone', 'formRule'));
92 }
93
94 /**
95 * Global validation rules for the form.
96 *
97 * @param array $fields
98 * Posted values of the form.
99 * @param array $errors
100 * List of errors to be posted back to the form.
101 *
102 * @return array
103 */
104 public static function formRule($fields, $errors) {
105 $hasData = $hasPrimary = $errors = array();
106 if (!empty($fields['phone']) && is_array($fields['phone'])) {
107 $primaryID = NULL;
108 foreach ($fields['phone'] as $instance => $blockValues) {
109 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
110
111 if ($dataExists) {
112 $hasData[] = $instance;
113 if (!empty($blockValues['is_primary'])) {
114 $hasPrimary[] = $instance;
115 if (!$primaryID && !empty($blockValues['phone'])) {
116 $primaryID = $blockValues['phone'];
117 }
118 }
119 }
120 }
121
122 if (empty($hasPrimary) && !empty($hasData)) {
123 $errors["phone[1][is_primary]"] = ts('One phone should be marked as primary.');
124 }
125
126 if (count($hasPrimary) > 1) {
127 $errors["phone[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one phone 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->_phones)) {
141 foreach ($this->_phones as $id => $value) {
142 $defaults['phone'][$id] = $value;
143 }
144 }
145 else {
146 // get the default location type
147 $locationType = CRM_Core_BAO_LocationType::getDefault();
148 $defaults['phone'][1]['location_type_id'] = $locationType->id;
149 }
150 return $defaults;
151 }
152
153 /**
154 * Process the form.
155 */
156 public function postProcess() {
157 $params = $this->exportValues();
158
159 // Process / save phones
160 $params['contact_id'] = $this->_contactId;
161 $params['updateBlankLocInfo'] = TRUE;
162 $params['phone']['isIdSet'] = TRUE;
163 foreach ($this->_phones as $count => $value) {
164 if (!empty($value['id']) && isset($params['phone'][$count])) {
165 $params['phone'][$count]['id'] = $value['id'];
166 }
167 }
168 CRM_Core_BAO_Block::create('phone', $params);
169
170 $this->log();
171 $this->response();
172 }
173
174 }