Merge branch 4.5 into master
[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 * get the result of the search for Add to * forms
177 *
178 * @param CRM_Core_Form $form
179 * @param array $params
180 * This contains elements for search criteria.
181 *
182 *
183 * @return void
184 */
185 public function search(&$form, &$params) {
186 //max records that will be listed
187 $searchValues = array();
188 if (!empty($params['rel_contact'])) {
189 if (isset($params['rel_contact_id']) &&
190 is_numeric($params['rel_contact_id'])
191 ) {
192 $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1);
193 }
194 else {
195 $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1);
196 }
197 }
198 $contactTypeAdded = FALSE;
199
200 $excludedContactIds = array();
201 if (isset($form->_contactId)) {
202 $excludedContactIds[] = $form->_contactId;
203 }
204
205 if (!empty($params['relationship_type_id'])) {
206 $relationshipType = new CRM_Contact_DAO_RelationshipType();
207 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
208
209 $relationshipType->id = $rid;
210 if ($relationshipType->find(TRUE)) {
211 if ($direction == 'a_b') {
212 $type = $relationshipType->contact_type_b;
213 $subType = $relationshipType->contact_sub_type_b;
214 }
215 else {
216 $type = $relationshipType->contact_type_a;
217 $subType = $relationshipType->contact_sub_type_a;
218 }
219
220 $form->set('contact_type', $type);
221 $form->set('contact_sub_type', $subType);
222 if ($type == 'Individual' || $type == 'Organization' || $type == 'Household') {
223 $searchValues[] = array('contact_type', '=', $type, 0, 0);
224 $contactTypeAdded = TRUE;
225 }
226
227 if ($subType) {
228 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
229 }
230 }
231 }
232
233 if (!$contactTypeAdded && !empty($params['contact_type'])) {
234 $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0);
235 }
236
237 // get the count of contact
238 $contactBAO = new CRM_Contact_BAO_Contact();
239 $query = new CRM_Contact_BAO_Query($searchValues);
240 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
241 $form->set('searchCount', $searchCount);
242 if ($searchCount <= 50) {
243 // get the result of the search
244 $result = $query->searchQuery(0, 50, NULL);
245
246 $config = CRM_Core_Config::singleton();
247 $searchRows = array();
248
249 //variable is set if only one record is foun and that record already has relationship with the contact
250 $duplicateRelationship = 0;
251
252 while ($result->fetch()) {
253 $query->convertToPseudoNames($result);
254 $contactID = $result->contact_id;
255 if (in_array($contactID, $excludedContactIds)) {
256 $duplicateRelationship++;
257 continue;
258 }
259
260 $duplicateRelationship = 0;
261
262 $searchRows[$contactID]['id'] = $contactID;
263 $searchRows[$contactID]['name'] = $result->sort_name;
264 $searchRows[$contactID]['city'] = $result->city;
265 $searchRows[$contactID]['state'] = $result->state_province;
266 $searchRows[$contactID]['email'] = $result->email;
267 $searchRows[$contactID]['phone'] = $result->phone;
268
269 $contact_type = '<img src="' . $config->resourceBase . 'i/contact_';
270
271 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ?
272 $result->contact_sub_type : $result->contact_type
273 );
274 }
275
276 $form->set('searchRows', $searchRows);
277 $form->set('duplicateRelationship', $duplicateRelationship);
278 }
279 else {
280 // resetting the session variables if many records are found
281 $form->set('searchRows', NULL);
282 $form->set('duplicateRelationship', NULL);
283 }
284 }
285 }