3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Form helper class for an Phone object.
21 class CRM_Contact_Form_Inline_Phone
extends CRM_Contact_Form_Inline
{
24 * Phones of the contact that is been viewed
27 private $_phones = [];
30 * No of phone blocks for inline edit
33 private $_blockCount = 6;
38 public function preProcess() {
41 //get all the existing phones
42 $phone = new CRM_Core_BAO_Phone();
43 $phone->contact_id
= $this->_contactId
;
45 $this->_phones
= CRM_Core_BAO_Block
::retrieveBlock($phone, NULL);
49 * Build the form object elements for phone object.
51 public function buildQuickForm() {
52 parent
::buildQuickForm();
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;
68 $this->assign('actualBlockCount', $actualBlockCount);
69 $this->assign('totalBlocks', $totalBlocks);
71 $this->applyFilter('__ALL__', 'trim');
73 for ($blockId = 1; $blockId < $totalBlocks; $blockId++
) {
74 CRM_Contact_Form_Edit_Phone
::buildQuickForm($this, $blockId, TRUE);
77 $this->addFormRule(['CRM_Contact_Form_Inline_Phone', 'formRule']);
81 * Global validation rules for the form.
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.
90 public static function formRule($fields, $errors) {
91 $hasData = $hasPrimary = $errors = [];
92 if (!empty($fields['phone']) && is_array($fields['phone'])) {
94 foreach ($fields['phone'] as $instance => $blockValues) {
95 $dataExists = CRM_Contact_Form_Contact
::blockDataExists($blockValues);
98 $hasData[] = $instance;
99 if (!empty($blockValues['is_primary'])) {
100 $hasPrimary[] = $instance;
101 if (!$primaryID && !empty($blockValues['phone'])) {
102 $primaryID = $blockValues['phone'];
108 if (empty($hasPrimary) && !empty($hasData)) {
109 $errors["phone[1][is_primary]"] = ts('One phone should be marked as primary.');
112 if (count($hasPrimary) > 1) {
113 $errors["phone[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one phone can be marked as primary.');
120 * Set defaults for the form.
124 public function setDefaultValues() {
126 if (!empty($this->_phones
)) {
127 foreach ($this->_phones
as $id => $value) {
128 $defaults['phone'][$id] = $value;
132 // get the default location type
133 $locationType = CRM_Core_BAO_LocationType
::getDefault();
134 $defaults['phone'][1]['location_type_id'] = $locationType->id
;
142 public function postProcess() {
143 $params = $this->exportValues();
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'];
154 CRM_Core_BAO_Block
::create('phone', $params);