Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
5a409b50 | 35 | * Form helper class for an IM object. |
6a488035 TO |
36 | */ |
37 | class CRM_Contact_Form_Inline_IM extends CRM_Contact_Form_Inline { | |
38 | ||
39 | /** | |
fe482240 | 40 | * Ims of the contact that is been viewed. |
69078420 | 41 | * @var array |
6a488035 | 42 | */ |
be2fb01f | 43 | private $_ims = []; |
6a488035 TO |
44 | |
45 | /** | |
fe482240 | 46 | * No of im blocks for inline edit. |
69078420 | 47 | * @var int |
6a488035 TO |
48 | */ |
49 | private $_blockCount = 6; | |
50 | ||
51 | /** | |
fe482240 | 52 | * Call preprocess. |
6a488035 TO |
53 | */ |
54 | public function preProcess() { | |
55 | parent::preProcess(); | |
56 | ||
57 | //get all the existing ims | |
58 | $im = new CRM_Core_BAO_IM(); | |
59 | $im->contact_id = $this->_contactId; | |
60 | ||
61 | $this->_ims = CRM_Core_BAO_Block::retrieveBlock($im, NULL); | |
62 | } | |
63 | ||
64 | /** | |
fe482240 | 65 | * Build the form object elements for im object. |
6a488035 TO |
66 | */ |
67 | public function buildQuickForm() { | |
68 | parent::buildQuickForm(); | |
69 | ||
70 | $totalBlocks = $this->_blockCount; | |
71 | $actualBlockCount = 1; | |
72 | if (count($this->_ims) > 1) { | |
73 | $actualBlockCount = $totalBlocks = count($this->_ims); | |
74 | if ($totalBlocks < $this->_blockCount) { | |
75 | $additionalBlocks = $this->_blockCount - $totalBlocks; | |
76 | $totalBlocks += $additionalBlocks; | |
77 | } | |
78 | else { | |
79 | $actualBlockCount++; | |
80 | $totalBlocks++; | |
81 | } | |
82 | } | |
83 | ||
84 | $this->assign('actualBlockCount', $actualBlockCount); | |
85 | $this->assign('totalBlocks', $totalBlocks); | |
86 | ||
87 | $this->applyFilter('__ALL__', 'trim'); | |
88 | ||
89 | for ($blockId = 1; $blockId < $totalBlocks; $blockId++) { | |
90 | CRM_Contact_Form_Edit_IM::buildQuickForm($this, $blockId, TRUE); | |
91 | } | |
92 | ||
be2fb01f | 93 | $this->addFormRule(['CRM_Contact_Form_Inline_IM', 'formRule']); |
6a488035 TO |
94 | } |
95 | ||
96 | /** | |
fe482240 | 97 | * Global validation rules for the form. |
6a488035 | 98 | * |
77c5b619 TO |
99 | * @param array $fields |
100 | * Posted values of the form. | |
101 | * @param array $errors | |
102 | * List of errors to be posted back to the form. | |
6a488035 | 103 | * |
a6c01b45 | 104 | * @return array |
6a488035 | 105 | */ |
00be9182 | 106 | public static function formRule($fields, $errors) { |
be2fb01f | 107 | $hasData = $hasPrimary = $errors = []; |
a7488080 | 108 | if (!empty($fields['im']) && is_array($fields['im'])) { |
6a488035 TO |
109 | foreach ($fields['im'] as $instance => $blockValues) { |
110 | $dataExists = CRM_Contact_Form_Contact::blockDataExists($blockValues); | |
111 | ||
112 | if ($dataExists) { | |
113 | $hasData[] = $instance; | |
a7488080 | 114 | if (!empty($blockValues['is_primary'])) { |
6a488035 | 115 | $hasPrimary[] = $instance; |
8cc574cf | 116 | if (!$primaryID && !empty($blockValues['im'])) { |
353ffa53 | 117 | $primaryID = $blockValues['im']; |
6a488035 TO |
118 | } |
119 | } | |
120 | } | |
121 | } | |
122 | ||
123 | if (empty($hasPrimary) && !empty($hasData)) { | |
124 | $errors["im[1][is_primary]"] = ts('One IM should be marked as primary.'); | |
125 | } | |
126 | ||
127 | if (count($hasPrimary) > 1) { | |
92fcb95f | 128 | $errors["im[" . array_pop($hasPrimary) . "][is_primary]"] = ts('Only one IM can be marked as primary.'); |
6a488035 TO |
129 | } |
130 | } | |
131 | return $errors; | |
132 | } | |
133 | ||
134 | /** | |
fe482240 | 135 | * Set defaults for the form. |
6a488035 TO |
136 | * |
137 | * @return array | |
6a488035 TO |
138 | */ |
139 | public function setDefaultValues() { | |
be2fb01f | 140 | $defaults = []; |
6a488035 TO |
141 | if (!empty($this->_ims)) { |
142 | foreach ($this->_ims as $id => $value) { | |
143 | $defaults['im'][$id] = $value; | |
144 | } | |
145 | } | |
146 | else { | |
147 | // get the default location type | |
148 | $locationType = CRM_Core_BAO_LocationType::getDefault(); | |
149 | $defaults['im'][1]['location_type_id'] = $locationType->id; | |
150 | } | |
151 | return $defaults; | |
152 | } | |
153 | ||
154 | /** | |
fe482240 | 155 | * Process the form. |
6a488035 TO |
156 | */ |
157 | public function postProcess() { | |
158 | $params = $this->exportValues(); | |
159 | ||
160 | // Process / save IMs | |
161 | $params['contact_id'] = $this->_contactId; | |
162 | $params['updateBlankLocInfo'] = TRUE; | |
a3b489b9 | 163 | $params['im']['isIdSet'] = TRUE; |
85c882c5 | 164 | foreach ($this->_ims as $count => $value) { |
165 | if (!empty($value['id']) && isset($params['im'][$count])) { | |
166 | $params['im'][$count]['id'] = $value['id']; | |
167 | } | |
168 | } | |
6a488035 TO |
169 | CRM_Core_BAO_Block::create('im', $params); |
170 | ||
171 | $this->log(); | |
172 | $this->response(); | |
173 | } | |
96025800 | 174 | |
6a488035 | 175 | } |