Merge pull request #17927 from monishdeb/core-785
[civicrm-core.git] / CRM / Contact / Form / Inline / Phone.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
5a409b50 19 * Form helper class for an Phone object.
6a488035
TO
20 */
21class CRM_Contact_Form_Inline_Phone extends CRM_Contact_Form_Inline {
22
23 /**
100fef9d 24 * Phones of the contact that is been viewed
69078420 25 * @var array
6a488035 26 */
be2fb01f 27 private $_phones = [];
6a488035
TO
28
29 /**
30 * No of phone blocks for inline edit
69078420 31 * @var int
6a488035
TO
32 */
33 private $_blockCount = 6;
34
35 /**
fe482240 36 * Call preprocess.
6a488035
TO
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 /**
fe482240 49 * Build the form object elements for phone object.
6a488035
TO
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) {
353ffa53
TO
59 $additionalBlocks = $this->_blockCount - $totalBlocks;
60 $totalBlocks += $additionalBlocks;
61 }
6a488035
TO
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
be2fb01f 77 $this->addFormRule(['CRM_Contact_Form_Inline_Phone', 'formRule']);
6a488035
TO
78 }
79
80 /**
fe482240 81 * Global validation rules for the form.
6a488035 82 *
77c5b619
TO
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.
6a488035 87 *
a6c01b45 88 * @return array
6a488035 89 */
00be9182 90 public static function formRule($fields, $errors) {
be2fb01f 91 $hasData = $hasPrimary = $errors = [];
a7488080 92 if (!empty($fields['phone']) && is_array($fields['phone'])) {
e60f24eb 93 $primaryID = NULL;
6a488035
TO
94 foreach ($fields['phone'] as $instance => $blockValues) {
95 $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues);
96
97 if ($dataExists) {
98 $hasData[] = $instance;
a7488080 99 if (!empty($blockValues['is_primary'])) {
6a488035 100 $hasPrimary[] = $instance;
8cc574cf 101 if (!$primaryID && !empty($blockValues['phone'])) {
353ffa53 102 $primaryID = $blockValues['phone'];
6a488035
TO
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) {
92fcb95f 113 $errors["phone[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one phone can be marked as primary.');
6a488035
TO
114 }
115 }
116 return $errors;
117 }
118
119 /**
fe482240 120 * Set defaults for the form.
6a488035
TO
121 *
122 * @return array
6a488035
TO
123 */
124 public function setDefaultValues() {
be2fb01f 125 $defaults = [];
6a488035
TO
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 /**
fe482240 140 * Process the form.
6a488035
TO
141 */
142 public function postProcess() {
143 $params = $this->exportValues();
144
145 // Process / save phones
146 $params['contact_id'] = $this->_contactId;
147 $params['updateBlankLocInfo'] = TRUE;
a3b489b9 148 $params['phone']['isIdSet'] = TRUE;
85c882c5 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 }
6a488035
TO
154 CRM_Core_BAO_Block::create('phone', $params);
155
156 $this->log();
157 $this->response();
158 }
96025800 159
6a488035 160}