Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class provides the functionality to add contact(s) to Household | |
38 | */ | |
39 | class CRM_Contact_Form_Task_AddToHousehold extends CRM_Contact_Form_Task { | |
40 | ||
41 | /** | |
c490a46a | 42 | * Build the form object |
6a488035 | 43 | * |
6a488035 TO |
44 | * |
45 | * @return void | |
46 | */ | |
00be9182 | 47 | public function preProcess() { |
d424ffde | 48 | // initialize the task and row fields |
6a488035 TO |
49 | parent::preProcess(); |
50 | } | |
51 | ||
52 | /** | |
c490a46a | 53 | * Build the form object |
6a488035 | 54 | * |
6a488035 | 55 | * |
355ba699 | 56 | * @return void |
6a488035 | 57 | */ |
00be9182 | 58 | public function buildQuickForm() { |
6a488035 TO |
59 | |
60 | CRM_Utils_System::setTitle(ts('Add Members to Household')); | |
61 | $this->addElement('text', 'name', ts('Find Target Household')); | |
62 | ||
63 | $this->add('select', 'relationship_type_id', ts('Relationship Type'), | |
64 | array( | |
d5cc0fc2 | 65 | '' => ts('- select -'), |
353ffa53 | 66 | ) + |
6a488035 TO |
67 | CRM_Contact_BAO_Relationship::getRelationType("Household"), TRUE |
68 | ); | |
69 | ||
70 | $searchRows = $this->get('searchRows'); | |
71 | $searchCount = $this->get('searchCount'); | |
72 | if ($searchRows) { | |
73 | $checkBoxes = array(); | |
74 | $chekFlag = 0; | |
75 | foreach ($searchRows as $id => $row) { | |
76 | if (!$chekFlag) { | |
77 | $chekFlag = $id; | |
78 | } | |
79 | $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id); | |
80 | } | |
81 | $this->addGroup($checkBoxes, 'contact_check'); | |
82 | if ($chekFlag) { | |
83 | $checkBoxes[$chekFlag]->setChecked(TRUE); | |
84 | } | |
85 | $this->assign('searchRows', $searchRows); | |
86 | } | |
87 | ||
88 | $this->assign('searchCount', $searchCount); | |
89 | $this->assign('searchDone', $this->get('searchDone')); | |
90 | $this->assign('contact_type_display', ts('Household')); | |
97e557d7 CW |
91 | $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), array('class' => 'crm-form-submit')); |
92 | $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'crm-form-submit')); | |
6a488035 TO |
93 | |
94 | $this->addButtons(array( | |
95 | array( | |
96 | 'type' => 'next', | |
97 | 'name' => ts('Add to Household'), | |
98 | 'isDefault' => TRUE, | |
99 | ), | |
100 | array( | |
101 | 'type' => 'cancel', | |
102 | 'name' => ts('Cancel'), | |
103 | ), | |
104 | ) | |
105 | ); | |
106 | } | |
107 | ||
108 | /** | |
100fef9d | 109 | * Process the form after the input has been submitted and validated |
6a488035 | 110 | * |
6a488035 | 111 | * |
355ba699 | 112 | * @return void |
6a488035 TO |
113 | */ |
114 | public function postProcess() { | |
115 | ||
116 | // store the submitted values in an array | |
117 | $params = $this->controller->exportValues($this->_name); | |
118 | ||
119 | $this->set('searchDone', 0); | |
a7488080 | 120 | if (!empty($_POST['_qf_AddToHousehold_refresh'])) { |
6a488035 TO |
121 | $searchParams['contact_type'] = array('Household' => 'Household'); |
122 | $searchParams['rel_contact'] = $params['name']; | |
123 | self::search($this, $searchParams); | |
124 | $this->set('searchDone', 1); | |
125 | return; | |
126 | } | |
127 | ||
128 | $data = array(); | |
129 | //$params['relationship_type_id']='4_a_b'; | |
130 | $data['relationship_type_id'] = $params['relationship_type_id']; | |
131 | $data['is_active'] = 1; | |
132 | $invalid = $valid = $duplicate = 0; | |
133 | if (is_array($this->_contactIds)) { | |
134 | foreach ($this->_contactIds as $value) { | |
135 | $ids = array(); | |
136 | $ids['contact'] = $value; | |
137 | //contact b --> household | |
138 | // contact a -> individual | |
139 | $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']); | |
140 | if ($errors) { | |
141 | $invalid++; | |
142 | continue; | |
143 | } | |
144 | ||
145 | if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, | |
353ffa53 TO |
146 | CRM_Utils_Array::value('contact', $ids), |
147 | // step 2 | |
148 | $params['contact_check'] | |
149 | ) | |
150 | ) { | |
6a488035 TO |
151 | $duplicate++; |
152 | continue; | |
153 | } | |
154 | CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']); | |
155 | $valid++; | |
156 | } | |
157 | ||
158 | $house = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['contact_check'], 'display_name'); | |
159 | list($rtype, $a_b) = explode('_', $data['relationship_type_id'], 2); | |
160 | $relationship = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $rtype, "label_$a_b"); | |
161 | ||
353ffa53 TO |
162 | $status = array( |
163 | ts('%count %2 %3 relationship created', array( | |
164 | 'count' => $valid, | |
165 | 'plural' => '%count %2 %3 relationships created', | |
166 | 2 => $relationship, | |
d5cc0fc2 | 167 | 3 => $house, |
168 | )), | |
353ffa53 | 169 | ); |
6a488035 | 170 | if ($duplicate) { |
353ffa53 TO |
171 | $status[] = ts('%count was skipped because the contact is already %2 %3', array( |
172 | 'count' => $duplicate, | |
173 | 'plural' => '%count were skipped because the contacts are already %2 %3', | |
174 | 2 => $relationship, | |
d5cc0fc2 | 175 | 3 => $house, |
353ffa53 | 176 | )); |
6a488035 TO |
177 | } |
178 | if ($invalid) { | |
353ffa53 TO |
179 | $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array( |
180 | 'count' => $invalid, | |
d5cc0fc2 | 181 | 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship', |
353ffa53 | 182 | )); |
6a488035 TO |
183 | } |
184 | $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; | |
353ffa53 TO |
185 | CRM_Core_Session::setStatus($status, ts('Relationship created.', array( |
186 | 'count' => $valid, | |
d5cc0fc2 | 187 | 'plural' => 'Relationships created.', |
353ffa53 | 188 | )), 'success', array('expires' => 0)); |
6a488035 TO |
189 | } |
190 | } | |
191 | ||
192 | /** | |
dc195289 | 193 | * get the result of the search for Add to * forms |
6a488035 | 194 | * |
c490a46a | 195 | * @param CRM_Core_Form $form |
77c5b619 TO |
196 | * @param array $params |
197 | * This contains elements for search criteria. | |
6a488035 | 198 | * |
6a488035 | 199 | * |
355ba699 | 200 | * @return void |
6a488035 | 201 | */ |
00be9182 | 202 | public function search(&$form, &$params) { |
6a488035 TO |
203 | //max records that will be listed |
204 | $searchValues = array(); | |
a7488080 | 205 | if (!empty($params['rel_contact'])) { |
6a488035 TO |
206 | if (isset($params['rel_contact_id']) && |
207 | is_numeric($params['rel_contact_id']) | |
208 | ) { | |
209 | $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1); | |
210 | } | |
211 | else { | |
212 | $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1); | |
213 | } | |
214 | } | |
215 | $contactTypeAdded = FALSE; | |
216 | ||
217 | $excludedContactIds = array(); | |
218 | if (isset($form->_contactId)) { | |
219 | $excludedContactIds[] = $form->_contactId; | |
220 | } | |
221 | ||
a7488080 | 222 | if (!empty($params['relationship_type_id'])) { |
6a488035 TO |
223 | $relationshipType = new CRM_Contact_DAO_RelationshipType(); |
224 | list($rid, $direction) = explode('_', $params['relationship_type_id'], 2); | |
225 | ||
226 | $relationshipType->id = $rid; | |
227 | if ($relationshipType->find(TRUE)) { | |
228 | if ($direction == 'a_b') { | |
229 | $type = $relationshipType->contact_type_b; | |
230 | $subType = $relationshipType->contact_sub_type_b; | |
231 | } | |
232 | else { | |
233 | $type = $relationshipType->contact_type_a; | |
234 | $subType = $relationshipType->contact_sub_type_a; | |
235 | } | |
236 | ||
237 | $form->set('contact_type', $type); | |
238 | $form->set('contact_sub_type', $subType); | |
239 | if ($type == 'Individual' || $type == 'Organization' || $type == 'Household') { | |
240 | $searchValues[] = array('contact_type', '=', $type, 0, 0); | |
241 | $contactTypeAdded = TRUE; | |
242 | } | |
243 | ||
244 | if ($subType) { | |
245 | $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0); | |
246 | } | |
247 | } | |
248 | } | |
249 | ||
8cc574cf | 250 | if (!$contactTypeAdded && !empty($params['contact_type'])) { |
6a488035 TO |
251 | $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0); |
252 | } | |
253 | ||
254 | // get the count of contact | |
353ffa53 TO |
255 | $contactBAO = new CRM_Contact_BAO_Contact(); |
256 | $query = new CRM_Contact_BAO_Query($searchValues); | |
6a488035 TO |
257 | $searchCount = $query->searchQuery(0, 0, NULL, TRUE); |
258 | $form->set('searchCount', $searchCount); | |
259 | if ($searchCount <= 50) { | |
260 | // get the result of the search | |
261 | $result = $query->searchQuery(0, 50, NULL); | |
262 | ||
263 | $config = CRM_Core_Config::singleton(); | |
264 | $searchRows = array(); | |
265 | ||
266 | //variable is set if only one record is foun and that record already has relationship with the contact | |
267 | $duplicateRelationship = 0; | |
268 | ||
269 | while ($result->fetch()) { | |
d9ab802d | 270 | $query->convertToPseudoNames($result); |
6a488035 TO |
271 | $contactID = $result->contact_id; |
272 | if (in_array($contactID, $excludedContactIds)) { | |
273 | $duplicateRelationship++; | |
274 | continue; | |
275 | } | |
276 | ||
277 | $duplicateRelationship = 0; | |
278 | ||
279 | $searchRows[$contactID]['id'] = $contactID; | |
280 | $searchRows[$contactID]['name'] = $result->sort_name; | |
281 | $searchRows[$contactID]['city'] = $result->city; | |
282 | $searchRows[$contactID]['state'] = $result->state_province; | |
283 | $searchRows[$contactID]['email'] = $result->email; | |
284 | $searchRows[$contactID]['phone'] = $result->phone; | |
285 | ||
286 | $contact_type = '<img src="' . $config->resourceBase . 'i/contact_'; | |
287 | ||
d5cc0fc2 | 288 | $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type |
6a488035 TO |
289 | ); |
290 | } | |
291 | ||
292 | $form->set('searchRows', $searchRows); | |
293 | $form->set('duplicateRelationship', $duplicateRelationship); | |
294 | } | |
295 | else { | |
296 | // resetting the session variables if many records are found | |
297 | $form->set('searchRows', NULL); | |
298 | $form->set('duplicateRelationship', NULL); | |
299 | } | |
300 | } | |
96025800 | 301 | |
6a488035 | 302 | } |