3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
37 * This class provides the functionality to add contact(s) to Household
39 class CRM_Contact_Form_Task_AddToHousehold
extends CRM_Contact_Form_Task
{
48 function preProcess() {
50 * initialize the task and row fields
57 * Function to build the form
63 function buildQuickForm() {
65 CRM_Utils_System
::setTitle(ts('Add Members to Household'));
66 $this->addElement('text', 'name', ts('Find Target Household'));
68 $this->add('select', 'relationship_type_id', ts('Relationship Type'),
70 '' => ts('- select -')) +
71 CRM_Contact_BAO_Relationship
::getRelationType("Household"), TRUE
74 $searchRows = $this->get('searchRows');
75 $searchCount = $this->get('searchCount');
77 $checkBoxes = array();
79 foreach ($searchRows as $id => $row) {
83 $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id);
85 $this->addGroup($checkBoxes, 'contact_check');
87 $checkBoxes[$chekFlag]->setChecked(TRUE);
89 $this->assign('searchRows', $searchRows);
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' => 'crm-form-submit'));
96 $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'crm-form-submit'));
98 $this->addButtons(array(
101 'name' => ts('Add to Household'),
106 'name' => ts('Cancel'),
113 * process the form after the input has been submitted and validated
119 public function postProcess() {
121 // store the submitted values in an array
122 $params = $this->controller
->exportValues($this->_name
);
124 $this->set('searchDone', 0);
125 if (!empty($_POST['_qf_AddToHousehold_refresh'])) {
126 $searchParams['contact_type'] = array('Household' => 'Household');
127 $searchParams['rel_contact'] = $params['name'];
128 self
::search($this, $searchParams);
129 $this->set('searchDone', 1);
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) {
141 $ids['contact'] = $value;
142 //contact b --> household
143 // contact a -> individual
144 $errors = CRM_Contact_BAO_Relationship
::checkValidRelationship($params, $ids, $params['contact_check']);
150 if (CRM_Contact_BAO_Relationship
::checkDuplicateRelationship($params,
151 CRM_Utils_Array
::value('contact', $ids),
153 $params['contact_check']
158 CRM_Contact_BAO_Relationship
::add($data, $ids, $params['contact_check']);
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");
166 $status = array(ts('%count %2 %3 relationship created', array('count' => $valid, 'plural' => '%count %2 %3 relationships created', 2 => $relationship, 3 => $house)));
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));
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'));
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));
179 * This function is to get the result of the search for Add to * forms
182 * @param array $params This contains elements for search criteria
188 function search(&$form, &$params) {
189 //max records that will be listed
190 $searchValues = array();
191 if (!empty($params['rel_contact'])) {
192 if (isset($params['rel_contact_id']) &&
193 is_numeric($params['rel_contact_id'])
195 $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1);
198 $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1);
201 $contactTypeAdded = FALSE;
203 $excludedContactIds = array();
204 if (isset($form->_contactId
)) {
205 $excludedContactIds[] = $form->_contactId
;
208 if (!empty($params['relationship_type_id'])) {
209 $relationshipType = new CRM_Contact_DAO_RelationshipType();
210 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
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
;
219 $type = $relationshipType->contact_type_a
;
220 $subType = $relationshipType->contact_sub_type_a
;
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;
231 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
236 if (!$contactTypeAdded && !empty($params['contact_type'])) {
237 $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0);
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);
249 $config = CRM_Core_Config
::singleton();
250 $searchRows = array();
252 //variable is set if only one record is foun and that record already has relationship with the contact
253 $duplicateRelationship = 0;
255 while ($result->fetch()) {
256 $query->convertToPseudoNames($result);
257 $contactID = $result->contact_id
;
258 if (in_array($contactID, $excludedContactIds)) {
259 $duplicateRelationship++
;
263 $duplicateRelationship = 0;
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
;
272 $contact_type = '<img src="' . $config->resourceBase
. 'i/contact_';
274 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
275 $result->contact_sub_type
: $result->contact_type
279 $form->set('searchRows', $searchRows);
280 $form->set('duplicateRelationship', $duplicateRelationship);
283 // resetting the session variables if many records are found
284 $form->set('searchRows', NULL);
285 $form->set('duplicateRelationship', NULL);