Merge pull request #17380 from seamuslee001/symfony_4_4
[civicrm-core.git] / CRM / Contact / Form / Inline / Phone.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Form helper class for an Phone object.
20 */
21 class CRM_Contact_Form_Inline_Phone extends CRM_Contact_Form_Inline {
22
23 /**
24 * Phones of the contact that is been viewed
25 * @var array
26 */
27 private $_phones = [];
28
29 /**
30 * No of phone blocks for inline edit
31 * @var int
32 */
33 private $_blockCount = 6;
34
35 /**
36 * Call preprocess.
37 */
38 public function preProcess() {
39 parent::preProcess();
40
41 //get all the existing phones
42 $phone = new CRM_Core_BAO_Phone();
43 $phone->contact_id = $this->_contactId;
44
45 $this->_phones = CRM_Core_BAO_Block::retrieveBlock($phone, NULL);
46 }
47
48 /**
49 * Build the form object elements for phone object.
50 */
51 public function buildQuickForm() {
52 parent::buildQuickForm();
53
54 $totalBlocks = $this->_blockCount;
55 $actualBlockCount = 1;
56 if (count($this->_phones) > 1) {
57 $actualBlockCount = $totalBlocks = count($this->_phones);
58 if ($totalBlocks < $this->_blockCount) {
59 $additionalBlocks = $this->_blockCount - $totalBlocks;
60 $totalBlocks += $additionalBlocks;
61 }
62 else {
63 $actualBlockCount++;
64 $totalBlocks++;
65 }
66 }
67
68 $this->assign('actualBlockCount', $actualBlockCount);
69 $this->assign('totalBlocks', $totalBlocks);
70
71 $this->applyFilter('__ALL__', 'trim');
72
73 for ($blockId = 1; $blockId < $totalBlocks; $blockId++) {
74 CRM_Contact_Form_Edit_Phone::buildQuickForm($this, $blockId, TRUE);
75 }
76
77 $this->addFormRule(['CRM_Contact_Form_Inline_Phone', 'formRule']);
78 }
79
80 /**
81 * Global validation rules for the form.
82 *
83 * @param array $fields
84 * Posted values of the form.
85 * @param array $errors
86 * List of errors to be posted back to the form.
87 *
88 * @return array
89 */
90 public static function formRule($fields, $errors) {
91 $hasData = $hasPrimary = $errors = [];
92 if (!empty($fields['phone']) && is_array($fields['phone'])) {
93 $primaryID = NULL;
94 foreach ($fields['phone'] as $instance => $blockValues) {
95 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
96
97 if ($dataExists) {
98 $hasData[] = $instance;
99 if (!empty($blockValues['is_primary'])) {
100 $hasPrimary[] = $instance;
101 if (!$primaryID && !empty($blockValues['phone'])) {
102 $primaryID = $blockValues['phone'];
103 }
104 }
105 }
106 }
107
108 if (empty($hasPrimary) && !empty($hasData)) {
109 $errors["phone[1][is_primary]"] = ts('One phone should be marked as primary.');
110 }
111
112 if (count($hasPrimary) > 1) {
113 $errors["phone[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one phone can be marked as primary.');
114 }
115 }
116 return $errors;
117 }
118
119 /**
120 * Set defaults for the form.
121 *
122 * @return array
123 */
124 public function setDefaultValues() {
125 $defaults = [];
126 if (!empty($this->_phones)) {
127 foreach ($this->_phones as $id => $value) {
128 $defaults['phone'][$id] = $value;
129 }
130 }
131 else {
132 // get the default location type
133 $locationType = CRM_Core_BAO_LocationType::getDefault();
134 $defaults['phone'][1]['location_type_id'] = $locationType->id;
135 }
136 return $defaults;
137 }
138
139 /**
140 * Process the form.
141 */
142 public function postProcess() {
143 $params = $this->exportValues();
144
145 // Process / save phones
146 $params['contact_id'] = $this->_contactId;
147 $params['updateBlankLocInfo'] = TRUE;
148 $params['phone']['isIdSet'] = TRUE;
149 foreach ($this->_phones as $count => $value) {
150 if (!empty($value['id']) && isset($params['phone'][$count])) {
151 $params['phone'][$count]['id'] = $value['id'];
152 }
153 }
154 CRM_Core_BAO_Block::create('phone', $params);
155
156 $this->log();
157 $this->response();
158 }
159
160 }