commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / CRM / Contact / Form / Task / AddToParentClass.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * This class provides the shared functionality for addToHousehold and addToOrganization.
30 */
31 class CRM_Contact_Form_Task_AddToParentClass extends CRM_Contact_Form_Task {
32
33 /**
34 * Exported parameters from the form.
35 *
36 * @var array.
37 */
38 protected $params;
39
40 /**
41 * Build the form object.
42 */
43 public function preProcess() {
44 parent::preProcess();
45 }
46
47 /**
48 * Add relationships from form.
49 */
50 public function addRelationships() {
51
52 if (!is_array($this->_contactIds)) {
53 // Could this really happen?
54 return;
55 }
56 $relationshipTypeParts = explode('_', $this->params['relationship_type_id']);
57 $params = array(
58 'relationship_type_id' => $relationshipTypeParts[0],
59 'is_active' => 1,
60 );
61 $secondaryRelationshipSide = $relationshipTypeParts[1];
62 $primaryRelationshipSide = $relationshipTypeParts[2];
63 $primaryFieldName = 'contact_id_' . $primaryRelationshipSide;
64 $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide;
65
66 $relationshipLabel = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType',
67 $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}");
68
69 $params[$secondaryFieldName] = $this->_contactIds;
70 $params[$primaryFieldName] = $this->params['contact_check'];
71 $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $primaryRelationshipSide);
72
73 $relatedContactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$primaryFieldName],
74 'display_name');
75
76 $status = array(
77 ts('%count %2 %3 relationship created', array(
78 'count' => $outcome['valid'],
79 'plural' => '%count %2 %3 relationships created',
80 2 => $relationshipLabel,
81 3 => $relatedContactName,
82 )),
83 );
84 if ($outcome['duplicate']) {
85 $status[] = ts('%count was skipped because the contact is already %2 %3', array(
86 'count' => $outcome['duplicate'],
87 'plural' => '%count were skipped because the contacts are already %2 %3',
88 2 => $relationshipLabel,
89 3 => $relatedContactName,
90 ));
91 }
92 if ($outcome['invalid']) {
93 $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array(
94 'count' => $outcome['invalid'],
95 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship',
96 ));
97 }
98 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
99 CRM_Core_Session::setStatus($status, ts('Relationship created.', array(
100 'count' => $outcome['valid'],
101 'plural' => 'Relationships created.',
102 )), 'success', array('expires' => 0));
103
104 }
105
106 /**
107 * Get the result of the search for Add to * forms.
108 *
109 * @param CRM_Core_Form $form
110 * @param array $params
111 * This contains elements for search criteria.
112 */
113 public function search(&$form, &$params) {
114 //max records that will be listed
115 $searchValues = array();
116 if (!empty($params['rel_contact'])) {
117 if (isset($params['rel_contact_id']) &&
118 is_numeric($params['rel_contact_id'])
119 ) {
120 $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1);
121 }
122 else {
123 $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1);
124 }
125 }
126 $contactTypeAdded = FALSE;
127
128 $excludedContactIds = array();
129 if (isset($form->_contactId)) {
130 $excludedContactIds[] = $form->_contactId;
131 }
132
133 if (!empty($params['relationship_type_id'])) {
134 $relationshipType = new CRM_Contact_DAO_RelationshipType();
135 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
136
137 $relationshipType->id = $rid;
138 if ($relationshipType->find(TRUE)) {
139 if ($direction == 'a_b') {
140 $type = $relationshipType->contact_type_b;
141 $subType = $relationshipType->contact_sub_type_b;
142 }
143 else {
144 $type = $relationshipType->contact_type_a;
145 $subType = $relationshipType->contact_sub_type_a;
146 }
147
148 $form->set('contact_type', $type);
149 $form->set('contact_sub_type', $subType);
150 if ($type == 'Individual' || $type == 'Organization' || $type == 'Household') {
151 $searchValues[] = array('contact_type', '=', $type, 0, 0);
152 $contactTypeAdded = TRUE;
153 }
154
155 if ($subType) {
156 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
157 }
158 }
159 }
160
161 if (!$contactTypeAdded && !empty($params['contact_type'])) {
162 $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0);
163 }
164
165 // get the count of contact
166 $contactBAO = new CRM_Contact_BAO_Contact();
167 $query = new CRM_Contact_BAO_Query($searchValues);
168 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
169 $form->set('searchCount', $searchCount);
170 if ($searchCount <= 50) {
171 // get the result of the search
172 $result = $query->searchQuery(0, 50, NULL);
173
174 $config = CRM_Core_Config::singleton();
175 $searchRows = array();
176
177 //variable is set if only one record is foun and that record already has relationship with the contact
178 $duplicateRelationship = 0;
179
180 while ($result->fetch()) {
181 $query->convertToPseudoNames($result);
182 $contactID = $result->contact_id;
183 if (in_array($contactID, $excludedContactIds)) {
184 $duplicateRelationship++;
185 continue;
186 }
187
188 $duplicateRelationship = 0;
189
190 $searchRows[$contactID]['id'] = $contactID;
191 $searchRows[$contactID]['name'] = $result->sort_name;
192 $searchRows[$contactID]['city'] = $result->city;
193 $searchRows[$contactID]['state'] = $result->state_province;
194 $searchRows[$contactID]['email'] = $result->email;
195 $searchRows[$contactID]['phone'] = $result->phone;
196
197 $contact_type = '<img src="' . $config->resourceBase . 'i/contact_';
198
199 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils::getImage($result->contact_sub_type ? $result->contact_sub_type : $result->contact_type
200 );
201 }
202
203 $form->set('searchRows', $searchRows);
204 $form->set('duplicateRelationship', $duplicateRelationship);
205 }
206 else {
207 // resetting the session variables if many records are found
208 $form->set('searchRows', NULL);
209 $form->set('duplicateRelationship', NULL);
210 }
211 }
212
213 }