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);
68 $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), ['class' => 'crm-form-submit']);
69 $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), ['class' => 'crm-form-submit']);
73 'name' => ts('Add to %1', [1 => $contactType]),
78 'name' => ts('Cancel'),
84 * Add relationships from form.
86 public function addRelationships() {
88 if (!is_array($this->_contactIds
)) {
89 // Could this really happen?
92 $relationshipTypeParts = explode('_', $this->params
['relationship_type_id']);
94 'relationship_type_id' => $relationshipTypeParts[0],
97 $secondaryRelationshipSide = $relationshipTypeParts[1];
98 $primaryRelationshipSide = $relationshipTypeParts[2];
99 $primaryFieldName = 'contact_id_' . $primaryRelationshipSide;
100 $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide;
102 $relationshipLabel = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType',
103 $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}");
105 $params[$secondaryFieldName] = $this->_contactIds
;
106 $params[$primaryFieldName] = $this->params
['contact_check'];
107 $outcome = CRM_Contact_BAO_Relationship
::createMultiple($params, $primaryRelationshipSide);
109 $relatedContactName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$primaryFieldName],
113 ts('%count %2 %3 relationship created', [
114 'count' => $outcome['valid'],
115 'plural' => '%count %2 %3 relationships created',
116 2 => $relationshipLabel,
117 3 => $relatedContactName,
120 if ($outcome['duplicate']) {
121 $status[] = ts('%count was skipped because the contact is already %2 %3', [
122 'count' => $outcome['duplicate'],
123 'plural' => '%count were skipped because the contacts are already %2 %3',
124 2 => $relationshipLabel,
125 3 => $relatedContactName,
128 if ($outcome['invalid']) {
129 $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', [
130 'count' => $outcome['invalid'],
131 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship',
134 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
135 CRM_Core_Session
::setStatus($status, ts('Relationship created.', [
136 'count' => $outcome['valid'],
137 'plural' => 'Relationships created.',
138 ]), 'success', ['expires' => 0]);
143 * Get the result of the search for Add to * forms.
145 * @param CRM_Core_Form $form
146 * @param array $params
147 * This contains elements for search criteria.
149 public function search(&$form, &$params) {
150 //max records that will be listed
152 if (!empty($params['rel_contact'])) {
153 if (isset($params['rel_contact_id']) &&
154 is_numeric($params['rel_contact_id'])
156 $searchValues[] = ['contact_id', '=', $params['rel_contact_id'], 0, 1];
159 $searchValues[] = ['sort_name', 'LIKE', $params['rel_contact'], 0, 1];
162 $contactTypeAdded = FALSE;
164 $excludedContactIds = [];
165 if (isset($form->_contactId
)) {
166 $excludedContactIds[] = $form->_contactId
;
169 if (!empty($params['relationship_type_id'])) {
170 $relationshipType = new CRM_Contact_DAO_RelationshipType();
171 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
173 $relationshipType->id
= $rid;
174 if ($relationshipType->find(TRUE)) {
175 if ($direction == 'a_b') {
176 $type = $relationshipType->contact_type_b
;
177 $subType = $relationshipType->contact_sub_type_b
;
180 $type = $relationshipType->contact_type_a
;
181 $subType = $relationshipType->contact_sub_type_a
;
184 $form->set('contact_type', $type);
185 $form->set('contact_sub_type', $subType);
186 if ($type == 'Individual' ||
$type == 'Organization' ||
$type == 'Household') {
187 $searchValues[] = ['contact_type', '=', $type, 0, 0];
188 $contactTypeAdded = TRUE;
192 $searchValues[] = ['contact_sub_type', '=', $subType, 0, 0];
197 if (!$contactTypeAdded && !empty($params['contact_type'])) {
198 $searchValues[] = ['contact_type', '=', $params['contact_type'], 0, 0];
201 // get the count of contact
202 $contactBAO = new CRM_Contact_BAO_Contact();
203 $query = new CRM_Contact_BAO_Query($searchValues);
204 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
205 $form->set('searchCount', $searchCount);
206 if ($searchCount <= 50) {
207 // get the result of the search
208 $result = $query->searchQuery(0, 50, NULL);
210 $config = CRM_Core_Config
::singleton();
213 //variable is set if only one record is foun and that record already has relationship with the contact
214 $duplicateRelationship = 0;
216 while ($result->fetch()) {
217 $query->convertToPseudoNames($result);
218 $contactID = $result->contact_id
;
219 if (in_array($contactID, $excludedContactIds)) {
220 $duplicateRelationship++
;
224 $duplicateRelationship = 0;
226 $searchRows[$contactID]['id'] = $contactID;
227 $searchRows[$contactID]['name'] = $result->sort_name
;
228 $searchRows[$contactID]['city'] = $result->city
;
229 $searchRows[$contactID]['state'] = $result->state_province
;
230 $searchRows[$contactID]['email'] = $result->email
;
231 $searchRows[$contactID]['phone'] = $result->phone
;
233 $contact_type = '<img src="' . $config->resourceBase
. 'i/contact_';
235 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
239 $form->set('searchRows', $searchRows);
240 $form->set('duplicateRelationship', $duplicateRelationship);
243 // resetting the session variables if many records are found
244 $form->set('searchRows', NULL);
245 $form->set('duplicateRelationship', NULL);
250 * Process the form after the input has been submitted and validated.
252 public function postProcess() {
253 // store the submitted values in an array
254 $this->params
= $this->controller
->exportValues($this->_name
);
255 $this->set('searchDone', 0);
256 $contactType = $this->get('contactType');
258 if (!empty($_POST["_qf_AddTo{$contactType}_refresh"])) {
259 $searchParams['contact_type'] = $contactType;
260 $searchParams['rel_contact'] = $this->params
['name'];
261 $this->search($this, $searchParams);
262 $this->set('searchDone', 1);
265 $this->addRelationships();