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