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