Commit | Line | Data |
---|---|---|
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 IM object. |
6a488035 TO |
20 | */ |
21 | class CRM_Contact_Form_Inline_IM extends CRM_Contact_Form_Inline { | |
22 | ||
23 | /** | |
fe482240 | 24 | * Ims of the contact that is been viewed. |
69078420 | 25 | * @var array |
6a488035 | 26 | */ |
be2fb01f | 27 | private $_ims = []; |
6a488035 TO |
28 | |
29 | /** | |
fe482240 | 30 | * No of im 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 ims | |
42 | $im = new CRM_Core_BAO_IM(); | |
43 | $im->contact_id = $this->_contactId; | |
44 | ||
45 | $this->_ims = CRM_Core_BAO_Block::retrieveBlock($im, NULL); | |
46 | } | |
47 | ||
48 | /** | |
fe482240 | 49 | * Build the form object elements for im object. |
6a488035 TO |
50 | */ |
51 | public function buildQuickForm() { | |
52 | parent::buildQuickForm(); | |
53 | ||
54 | $totalBlocks = $this->_blockCount; | |
55 | $actualBlockCount = 1; | |
56 | if (count($this->_ims) > 1) { | |
57 | $actualBlockCount = $totalBlocks = count($this->_ims); | |
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_IM::buildQuickForm($this, $blockId, TRUE); | |
75 | } | |
76 | ||
be2fb01f | 77 | $this->addFormRule(['CRM_Contact_Form_Inline_IM', '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['im']) && is_array($fields['im'])) { |
6a488035 TO |
93 | foreach ($fields['im'] as $instance => $blockValues) { |
94 | $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues); | |
95 | ||
96 | if ($dataExists) { | |
97 | $hasData[] = $instance; | |
a7488080 | 98 | if (!empty($blockValues['is_primary'])) { |
6a488035 | 99 | $hasPrimary[] = $instance; |
8cc574cf | 100 | if (!$primaryID && !empty($blockValues['im'])) { |
353ffa53 | 101 | $primaryID = $blockValues['im']; |
6a488035 TO |
102 | } |
103 | } | |
104 | } | |
105 | } | |
106 | ||
107 | if (empty($hasPrimary) && !empty($hasData)) { | |
108 | $errors["im[1][is_primary]"] = ts('One IM should be marked as primary.'); | |
109 | } | |
110 | ||
111 | if (count($hasPrimary) > 1) { | |
92fcb95f | 112 | $errors["im[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one IM can be marked as primary.'); |
6a488035 TO |
113 | } |
114 | } | |
115 | return $errors; | |
116 | } | |
117 | ||
118 | /** | |
fe482240 | 119 | * Set defaults for the form. |
6a488035 TO |
120 | * |
121 | * @return array | |
6a488035 TO |
122 | */ |
123 | public function setDefaultValues() { | |
be2fb01f | 124 | $defaults = []; |
6a488035 TO |
125 | if (!empty($this->_ims)) { |
126 | foreach ($this->_ims as $id => $value) { | |
127 | $defaults['im'][$id] = $value; | |
128 | } | |
129 | } | |
130 | else { | |
131 | // get the default location type | |
132 | $locationType = CRM_Core_BAO_LocationType::getDefault(); | |
133 | $defaults['im'][1]['location_type_id'] = $locationType->id; | |
134 | } | |
135 | return $defaults; | |
136 | } | |
137 | ||
138 | /** | |
fe482240 | 139 | * Process the form. |
6a488035 TO |
140 | */ |
141 | public function postProcess() { | |
142 | $params = $this->exportValues(); | |
143 | ||
144 | // Process / save IMs | |
145 | $params['contact_id'] = $this->_contactId; | |
146 | $params['updateBlankLocInfo'] = TRUE; | |
a3b489b9 | 147 | $params['im']['isIdSet'] = TRUE; |
85c882c5 | 148 | foreach ($this->_ims as $count => $value) { |
149 | if (!empty($value['id']) && isset($params['im'][$count])) { | |
150 | $params['im'][$count]['id'] = $value['id']; | |
151 | } | |
152 | } | |
6a488035 TO |
153 | CRM_Core_BAO_Block::create('im', $params); |
154 | ||
155 | $this->log(); | |
156 | $this->response(); | |
157 | } | |
96025800 | 158 | |
6a488035 | 159 | } |