3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
13 * This class provides the shared functionality for addToHousehold and addToOrganization.
15 class CRM_Contact_Form_Task_AddToParentClass
extends CRM_Contact_Form_Task
{
18 * Exported parameters from the form.
25 * Build the form object.
27 public function preProcess() {
31 public function buildQuickForm() {
32 $contactType = $this->get('contactType');
33 CRM_Utils_System
::setTitle(ts('Add Contacts to %1', [1 => $contactType]));
34 $this->addElement('text', 'name', ts('Find Target %1', [1 => $contactType]));
37 'relationship_type_id',
38 ts('Relationship Type'),
40 '' => ts('- select -'),
42 CRM_Contact_BAO_Relationship
::getRelationType($contactType), TRUE
45 $searchRows = $this->get('searchRows');
46 $searchCount = $this->get('searchCount');
50 foreach ($searchRows as $id => $row) {
55 $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id);
58 $this->addGroup($checkBoxes, 'contact_check');
60 $checkBoxes[$chekFlag]->setChecked(TRUE);
62 $this->assign('searchRows', $searchRows);
65 $this->assign('searchCount', $searchCount);
66 $this->assign('searchDone', $this->get('searchDone'));
67 $this->assign('contact_type_display', $contactType);
70 'class' => 'crm-form-submit',
73 $this->addElement('xbutton', $this->getButtonName('refresh'), ts('Search'), $buttonAttrs);
74 $this->addElement('xbutton', $this->getButtonName('cancel'), ts('Cancel'), $buttonAttrs);
78 'name' => ts('Add to %1', [1 => $contactType]),
83 'name' => ts('Cancel'),
89 * Add relationships from form.
91 public function addRelationships() {
93 if (!is_array($this->_contactIds
)) {
94 // Could this really happen?
97 $relationshipTypeParts = explode('_', $this->params
['relationship_type_id']);
99 'relationship_type_id' => $relationshipTypeParts[0],
102 $secondaryRelationshipSide = $relationshipTypeParts[1];
103 $primaryRelationshipSide = $relationshipTypeParts[2];
104 $primaryFieldName = 'contact_id_' . $primaryRelationshipSide;
105 $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide;
107 $relationshipLabel = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType',
108 $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}");
110 $params[$secondaryFieldName] = $this->_contactIds
;
111 $params[$primaryFieldName] = $this->params
['contact_check'];
112 $outcome = CRM_Contact_BAO_Relationship
::createMultiple($params, $primaryRelationshipSide);
114 $relatedContactName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$primaryFieldName],
118 ts('%count %2 %3 relationship created', [
119 'count' => $outcome['valid'],
120 'plural' => '%count %2 %3 relationships created',
121 2 => $relationshipLabel,
122 3 => $relatedContactName,
125 if ($outcome['duplicate']) {
126 $status[] = ts('%count was skipped because the contact is already %2 %3', [
127 'count' => $outcome['duplicate'],
128 'plural' => '%count were skipped because the contacts are already %2 %3',
129 2 => $relationshipLabel,
130 3 => $relatedContactName,
133 if ($outcome['invalid']) {
134 $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', [
135 'count' => $outcome['invalid'],
136 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship',
139 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
140 CRM_Core_Session
::setStatus($status, ts('Relationship created.', [
141 'count' => $outcome['valid'],
142 'plural' => 'Relationships created.',
143 ]), 'success', ['expires' => 0]);
148 * Get the result of the search for Add to * forms.
150 * @param CRM_Core_Form $form
151 * @param array $params
152 * This contains elements for search criteria.
154 public function search(&$form, &$params) {
155 //max records that will be listed
157 if (!empty($params['rel_contact'])) {
158 if (isset($params['rel_contact_id']) &&
159 is_numeric($params['rel_contact_id'])
161 $searchValues[] = ['contact_id', '=', $params['rel_contact_id'], 0, 1];
164 $searchValues[] = ['sort_name', 'LIKE', $params['rel_contact'], 0, 1];
167 $contactTypeAdded = FALSE;
169 $excludedContactIds = [];
170 if (isset($form->_contactId
)) {
171 $excludedContactIds[] = $form->_contactId
;
174 if (!empty($params['relationship_type_id'])) {
175 $relationshipType = new CRM_Contact_DAO_RelationshipType();
176 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
178 $relationshipType->id
= $rid;
179 if ($relationshipType->find(TRUE)) {
180 if ($direction == 'a_b') {
181 $type = $relationshipType->contact_type_b
;
182 $subType = $relationshipType->contact_sub_type_b
;
185 $type = $relationshipType->contact_type_a
;
186 $subType = $relationshipType->contact_sub_type_a
;
189 $form->set('contact_type', $type);
190 $form->set('contact_sub_type', $subType);
191 if ($type == 'Individual' ||
$type == 'Organization' ||
$type == 'Household') {
192 $searchValues[] = ['contact_type', '=', $type, 0, 0];
193 $contactTypeAdded = TRUE;
197 $searchValues[] = ['contact_sub_type', '=', $subType, 0, 0];
202 if (!$contactTypeAdded && !empty($params['contact_type'])) {
203 $searchValues[] = ['contact_type', '=', $params['contact_type'], 0, 0];
206 // get the count of contact
207 $contactBAO = new CRM_Contact_BAO_Contact();
208 $query = new CRM_Contact_BAO_Query($searchValues);
209 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
210 $form->set('searchCount', $searchCount);
211 if ($searchCount <= 50) {
212 // get the result of the search
213 $result = $query->searchQuery(0, 50, NULL);
215 $config = CRM_Core_Config
::singleton();
218 //variable is set if only one record is foun and that record already has relationship with the contact
219 $duplicateRelationship = 0;
221 while ($result->fetch()) {
222 $query->convertToPseudoNames($result);
223 $contactID = $result->contact_id
;
224 if (in_array($contactID, $excludedContactIds)) {
225 $duplicateRelationship++
;
229 $duplicateRelationship = 0;
231 $searchRows[$contactID]['id'] = $contactID;
232 $searchRows[$contactID]['name'] = $result->sort_name
;
233 $searchRows[$contactID]['city'] = $result->city
;
234 $searchRows[$contactID]['state'] = $result->state_province
;
235 $searchRows[$contactID]['email'] = $result->email
;
236 $searchRows[$contactID]['phone'] = $result->phone
;
238 $contact_type = '<img src="' . $config->resourceBase
. 'i/contact_';
240 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
244 $form->set('searchRows', $searchRows);
245 $form->set('duplicateRelationship', $duplicateRelationship);
248 // resetting the session variables if many records are found
249 $form->set('searchRows', NULL);
250 $form->set('duplicateRelationship', NULL);
255 * Process the form after the input has been submitted and validated.
257 public function postProcess() {
258 // store the submitted values in an array
259 $this->params
= $this->controller
->exportValues($this->_name
);
260 $this->set('searchDone', 0);
261 $contactType = $this->get('contactType');
263 if (!empty($_POST["_qf_AddTo{$contactType}_refresh"])) {
264 $searchParams['contact_type'] = $contactType;
265 $searchParams['rel_contact'] = $this->params
['name'];
266 $this->search($this, $searchParams);
267 $this->set('searchDone', 1);
270 $this->addRelationships();