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