3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
29 * This class provides the shared functionality for addToHousehold and addToOrganization.
31 class CRM_Contact_Form_Task_AddToParentClass
extends CRM_Contact_Form_Task
{
34 * Exported parameters from the form.
41 * Build the form object.
43 public function preProcess() {
47 public function buildQuickForm() {
48 $contactType = $this->get('contactType');
49 CRM_Utils_System
::setTitle(ts('Add Contacts to %1', array(1 => $contactType)));
50 $this->addElement('text', 'name', ts('Find Target %1', array(1 => $contactType)));
53 'relationship_type_id',
54 ts('Relationship Type'),
56 '' => ts('- select -'),
58 CRM_Contact_BAO_Relationship
::getRelationType($contactType), TRUE
61 $searchRows = $this->get('searchRows');
62 $searchCount = $this->get('searchCount');
64 $checkBoxes = array();
66 foreach ($searchRows as $id => $row) {
71 $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id);
74 $this->addGroup($checkBoxes, 'contact_check');
76 $checkBoxes[$chekFlag]->setChecked(TRUE);
78 $this->assign('searchRows', $searchRows);
81 $this->assign('searchCount', $searchCount);
82 $this->assign('searchDone', $this->get('searchDone'));
83 $this->assign('contact_type_display', ts($contactType));
84 $this->addElement('submit', $this->getButtonName('refresh'), ts('Search'), array('class' => 'crm-form-submit'));
85 $this->addElement('submit', $this->getButtonName('cancel'), ts('Cancel'), array('class' => 'crm-form-submit'));
86 $this->addButtons(array(
89 'name' => ts('Add to %1', array(1 => $contactType)),
94 'name' => ts('Cancel'),
100 * Add relationships from form.
102 public function addRelationships() {
104 if (!is_array($this->_contactIds
)) {
105 // Could this really happen?
108 $relationshipTypeParts = explode('_', $this->params
['relationship_type_id']);
110 'relationship_type_id' => $relationshipTypeParts[0],
113 $secondaryRelationshipSide = $relationshipTypeParts[1];
114 $primaryRelationshipSide = $relationshipTypeParts[2];
115 $primaryFieldName = 'contact_id_' . $primaryRelationshipSide;
116 $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide;
118 $relationshipLabel = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType',
119 $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}");
121 $params[$secondaryFieldName] = $this->_contactIds
;
122 $params[$primaryFieldName] = $this->params
['contact_check'];
123 $outcome = CRM_Contact_BAO_Relationship
::createMultiple($params, $primaryRelationshipSide);
125 $relatedContactName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$primaryFieldName],
129 ts('%count %2 %3 relationship created', array(
130 'count' => $outcome['valid'],
131 'plural' => '%count %2 %3 relationships created',
132 2 => $relationshipLabel,
133 3 => $relatedContactName,
136 if ($outcome['duplicate']) {
137 $status[] = ts('%count was skipped because the contact is already %2 %3', array(
138 'count' => $outcome['duplicate'],
139 'plural' => '%count were skipped because the contacts are already %2 %3',
140 2 => $relationshipLabel,
141 3 => $relatedContactName,
144 if ($outcome['invalid']) {
145 $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array(
146 'count' => $outcome['invalid'],
147 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship',
150 $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>';
151 CRM_Core_Session
::setStatus($status, ts('Relationship created.', array(
152 'count' => $outcome['valid'],
153 'plural' => 'Relationships created.',
154 )), 'success', array('expires' => 0));
159 * Get the result of the search for Add to * forms.
161 * @param CRM_Core_Form $form
162 * @param array $params
163 * This contains elements for search criteria.
165 public function search(&$form, &$params) {
166 //max records that will be listed
167 $searchValues = array();
168 if (!empty($params['rel_contact'])) {
169 if (isset($params['rel_contact_id']) &&
170 is_numeric($params['rel_contact_id'])
172 $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1);
175 $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1);
178 $contactTypeAdded = FALSE;
180 $excludedContactIds = array();
181 if (isset($form->_contactId
)) {
182 $excludedContactIds[] = $form->_contactId
;
185 if (!empty($params['relationship_type_id'])) {
186 $relationshipType = new CRM_Contact_DAO_RelationshipType();
187 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
189 $relationshipType->id
= $rid;
190 if ($relationshipType->find(TRUE)) {
191 if ($direction == 'a_b') {
192 $type = $relationshipType->contact_type_b
;
193 $subType = $relationshipType->contact_sub_type_b
;
196 $type = $relationshipType->contact_type_a
;
197 $subType = $relationshipType->contact_sub_type_a
;
200 $form->set('contact_type', $type);
201 $form->set('contact_sub_type', $subType);
202 if ($type == 'Individual' ||
$type == 'Organization' ||
$type == 'Household') {
203 $searchValues[] = array('contact_type', '=', $type, 0, 0);
204 $contactTypeAdded = TRUE;
208 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
213 if (!$contactTypeAdded && !empty($params['contact_type'])) {
214 $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0);
217 // get the count of contact
218 $contactBAO = new CRM_Contact_BAO_Contact();
219 $query = new CRM_Contact_BAO_Query($searchValues);
220 $searchCount = $query->searchQuery(0, 0, NULL, TRUE);
221 $form->set('searchCount', $searchCount);
222 if ($searchCount <= 50) {
223 // get the result of the search
224 $result = $query->searchQuery(0, 50, NULL);
226 $config = CRM_Core_Config
::singleton();
227 $searchRows = array();
229 //variable is set if only one record is foun and that record already has relationship with the contact
230 $duplicateRelationship = 0;
232 while ($result->fetch()) {
233 $query->convertToPseudoNames($result);
234 $contactID = $result->contact_id
;
235 if (in_array($contactID, $excludedContactIds)) {
236 $duplicateRelationship++
;
240 $duplicateRelationship = 0;
242 $searchRows[$contactID]['id'] = $contactID;
243 $searchRows[$contactID]['name'] = $result->sort_name
;
244 $searchRows[$contactID]['city'] = $result->city
;
245 $searchRows[$contactID]['state'] = $result->state_province
;
246 $searchRows[$contactID]['email'] = $result->email
;
247 $searchRows[$contactID]['phone'] = $result->phone
;
249 $contact_type = '<img src="' . $config->resourceBase
. 'i/contact_';
251 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
255 $form->set('searchRows', $searchRows);
256 $form->set('duplicateRelationship', $duplicateRelationship);
259 // resetting the session variables if many records are found
260 $form->set('searchRows', NULL);
261 $form->set('duplicateRelationship', NULL);
266 * Process the form after the input has been submitted and validated.
268 public function postProcess() {
269 // store the submitted values in an array
270 $this->params
= $this->controller
->exportValues($this->_name
);
271 $this->set('searchDone', 0);
272 $contactType = $this->get('contactType');
274 if (!empty($_POST["_qf_AddTo{$contactType}_refresh"])) {
275 $searchParams['contact_type'] = $contactType;
276 $searchParams['rel_contact'] = $this->params
['name'];
277 $this->search($this, $searchParams);
278 $this->set('searchDone', 1);
281 $this->addRelationships();