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() {
48 // initialize the task and row fields
53 * Build the form object
58 public function buildQuickForm() {
60 CRM_Utils_System
::setTitle(ts('Add Members to Household'));
61 $this->addElement('text', 'name', ts('Find Target Household'));
63 $this->add('select', 'relationship_type_id', ts('Relationship Type'),
65 '' => ts('- select -'),
67 CRM_Contact_BAO_Relationship
::getRelationType("Household"), TRUE
70 $searchRows = $this->get('searchRows');
71 $searchCount = $this->get('searchCount');
73 $checkBoxes = array();
75 foreach ($searchRows as $id => $row) {
79 $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id);
81 $this->addGroup($checkBoxes, 'contact_check');
83 $checkBoxes[$chekFlag]->setChecked(TRUE);
85 $this->assign('searchRows', $searchRows);
88 $this->assign('searchCount', $searchCount);
89 $this->assign('searchDone', $this->get('searchDone'));
90 $this->assign('contact_type_display', ts('Household'));
91 $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), array('class' => 'crm-form-submit'));
92 $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'crm-form-submit'));
94 $this->addButtons(array(
97 'name' => ts('Add to Household'),
102 'name' => ts('Cancel'),
109 * Process the form after the input has been submitted and validated
114 public function postProcess() {
116 // store the submitted values in an array
117 $params = $this->controller
->exportValues($this->_name
);
119 $this->set('searchDone', 0);
120 if (!empty($_POST['_qf_AddToHousehold_refresh'])) {
121 $searchParams['contact_type'] = array('Household' => 'Household');
122 $searchParams['rel_contact'] = $params['name'];
123 self
::search($this, $searchParams);
124 $this->set('searchDone', 1);
129 //$params['relationship_type_id']='4_a_b';
130 $data['relationship_type_id'] = $params['relationship_type_id'];
131 $data['is_active'] = 1;
132 $invalid = $valid = $duplicate = 0;
133 if (is_array($this->_contactIds
)) {
134 foreach ($this->_contactIds
as $value) {
136 $ids['contact'] = $value;
137 //contact b --> household
138 // contact a -> individual
139 $errors = CRM_Contact_BAO_Relationship
::checkValidRelationship($params, $ids, $params['contact_check']);
145 if (CRM_Contact_BAO_Relationship
::checkDuplicateRelationship($params,
146 CRM_Utils_Array
::value('contact', $ids),
148 $params['contact_check']
154 CRM_Contact_BAO_Relationship
::add($data, $ids, $params['contact_check']);
158 $house = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params['contact_check'], 'display_name');
159 list($rtype, $a_b) = explode('_', $data['relationship_type_id'], 2);
160 $relationship = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType', $rtype, "label_$a_b");
163 ts('%count %2 %3 relationship created', array(
165 'plural' => '%count %2 %3 relationships created',
171 $status[] = ts('%count was skipped because the contact is already %2 %3', array(
172 'count' => $duplicate,
173 'plural' => '%count were skipped because the contacts are already %2 %3',
179 $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array(
181 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship',
184 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
185 CRM_Core_Session
::setStatus($status, ts('Relationship created.', array(
187 'plural' => 'Relationships created.',
188 )), 'success', array('expires' => 0));
193 * get the result of the search for Add to * forms
195 * @param CRM_Core_Form $form
196 * @param array $params
197 * This contains elements for search criteria.
202 public function search(&$form, &$params) {
203 //max records that will be listed
204 $searchValues = array();
205 if (!empty($params['rel_contact'])) {
206 if (isset($params['rel_contact_id']) &&
207 is_numeric($params['rel_contact_id'])
209 $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1);
212 $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1);
215 $contactTypeAdded = FALSE;
217 $excludedContactIds = array();
218 if (isset($form->_contactId
)) {
219 $excludedContactIds[] = $form->_contactId
;
222 if (!empty($params['relationship_type_id'])) {
223 $relationshipType = new CRM_Contact_DAO_RelationshipType();
224 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
226 $relationshipType->id
= $rid;
227 if ($relationshipType->find(TRUE)) {
228 if ($direction == 'a_b') {
229 $type = $relationshipType->contact_type_b
;
230 $subType = $relationshipType->contact_sub_type_b
;
233 $type = $relationshipType->contact_type_a
;
234 $subType = $relationshipType->contact_sub_type_a
;
237 $form->set('contact_type', $type);
238 $form->set('contact_sub_type', $subType);
239 if ($type == 'Individual' ||
$type == 'Organization' ||
$type == 'Household') {
240 $searchValues[] = array('contact_type', '=', $type, 0, 0);
241 $contactTypeAdded = TRUE;
245 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
250 if (!$contactTypeAdded && !empty($params['contact_type'])) {
251 $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0);
254 // get the count of contact
255 $contactBAO = new CRM_Contact_BAO_Contact();
256 $query = new CRM_Contact_BAO_Query($searchValues);
257 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
258 $form->set('searchCount', $searchCount);
259 if ($searchCount <= 50) {
260 // get the result of the search
261 $result = $query->searchQuery(0, 50, NULL);
263 $config = CRM_Core_Config
::singleton();
264 $searchRows = array();
266 //variable is set if only one record is foun and that record already has relationship with the contact
267 $duplicateRelationship = 0;
269 while ($result->fetch()) {
270 $query->convertToPseudoNames($result);
271 $contactID = $result->contact_id
;
272 if (in_array($contactID, $excludedContactIds)) {
273 $duplicateRelationship++
;
277 $duplicateRelationship = 0;
279 $searchRows[$contactID]['id'] = $contactID;
280 $searchRows[$contactID]['name'] = $result->sort_name
;
281 $searchRows[$contactID]['city'] = $result->city
;
282 $searchRows[$contactID]['state'] = $result->state_province
;
283 $searchRows[$contactID]['email'] = $result->email
;
284 $searchRows[$contactID]['phone'] = $result->phone
;
286 $contact_type = '<img src="' . $config->resourceBase
. 'i/contact_';
288 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
292 $form->set('searchRows', $searchRows);
293 $form->set('duplicateRelationship', $duplicateRelationship);
296 // resetting the session variables if many records are found
297 $form->set('searchRows', NULL);
298 $form->set('duplicateRelationship', NULL);