3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
{
42 * Build the form object
47 public function preProcess() {
49 * initialize the task and row fields
56 * Build the form object
61 public function buildQuickForm() {
63 CRM_Utils_System
::setTitle(ts('Add Members to Household'));
64 $this->addElement('text', 'name', ts('Find Target Household'));
66 $this->add('select', 'relationship_type_id', ts('Relationship Type'),
68 '' => ts('- select -')) +
69 CRM_Contact_BAO_Relationship
::getRelationType("Household"), TRUE
72 $searchRows = $this->get('searchRows');
73 $searchCount = $this->get('searchCount');
75 $checkBoxes = array();
77 foreach ($searchRows as $id => $row) {
81 $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id);
83 $this->addGroup($checkBoxes, 'contact_check');
85 $checkBoxes[$chekFlag]->setChecked(TRUE);
87 $this->assign('searchRows', $searchRows);
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'));
96 $this->addButtons(array(
99 'name' => ts('Add to Household'),
104 'name' => ts('Cancel'),
111 * Process the form after the input has been submitted and validated
116 public function postProcess() {
118 // store the submitted values in an array
119 $params = $this->controller
->exportValues($this->_name
);
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);
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) {
138 $ids['contact'] = $value;
139 //contact b --> household
140 // contact a -> individual
141 $errors = CRM_Contact_BAO_Relationship
::checkValidRelationship($params, $ids, $params['contact_check']);
147 if (CRM_Contact_BAO_Relationship
::checkDuplicateRelationship($params,
148 CRM_Utils_Array
::value('contact', $ids),
150 $params['contact_check']
155 CRM_Contact_BAO_Relationship
::add($data, $ids, $params['contact_check']);
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");
163 $status = array(ts('%count %2 %3 relationship created', array('count' => $valid, 'plural' => '%count %2 %3 relationships created', 2 => $relationship, 3 => $house)));
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));
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'));
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));
176 * This function is to get the result of the search for Add to * forms
178 * @param CRM_Core_Form $form
179 * @param array $params This contains elements for search criteria
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'])
191 $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1);
194 $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1);
197 $contactTypeAdded = FALSE;
199 $excludedContactIds = array();
200 if (isset($form->_contactId
)) {
201 $excludedContactIds[] = $form->_contactId
;
204 if (!empty($params['relationship_type_id'])) {
205 $relationshipType = new CRM_Contact_DAO_RelationshipType();
206 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
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
;
215 $type = $relationshipType->contact_type_a
;
216 $subType = $relationshipType->contact_sub_type_a
;
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;
227 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
232 if (!$contactTypeAdded && !empty($params['contact_type'])) {
233 $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0);
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);
245 $config = CRM_Core_Config
::singleton();
246 $searchRows = array();
248 //variable is set if only one record is foun and that record already has relationship with the contact
249 $duplicateRelationship = 0;
251 while ($result->fetch()) {
252 $query->convertToPseudoNames($result);
253 $contactID = $result->contact_id
;
254 if (in_array($contactID, $excludedContactIds)) {
255 $duplicateRelationship++
;
259 $duplicateRelationship = 0;
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
;
268 $contact_type = '<img src="' . $config->resourceBase
. 'i/contact_';
270 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
271 $result->contact_sub_type
: $result->contact_type
275 $form->set('searchRows', $searchRows);
276 $form->set('duplicateRelationship', $duplicateRelationship);
279 // resetting the session variables if many records are found
280 $form->set('searchRows', NULL);
281 $form->set('duplicateRelationship', NULL);