3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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() {
48 * Add relationships from form.
50 public function addRelationships() {
52 if (!is_array($this->_contactIds
)) {
53 // Could this really happen?
56 $relationshipTypeParts = explode('_', $this->params
['relationship_type_id']);
58 'relationship_type_id' => $relationshipTypeParts[0],
61 $secondaryRelationshipSide = $relationshipTypeParts[1];
62 $primaryRelationshipSide = $relationshipTypeParts[2];
63 $primaryFieldName = 'contact_id_' . $primaryRelationshipSide;
64 $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide;
66 $relationshipLabel = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_RelationshipType',
67 $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}");
69 $params[$secondaryFieldName] = $this->_contactIds
;
70 $params[$primaryFieldName] = $this->params
['contact_check'];
71 $outcome = CRM_Contact_BAO_Relationship
::createMultiple($params, $primaryRelationshipSide);
73 $relatedContactName = CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $params[$primaryFieldName],
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,
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,
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',
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));
107 * Get the result of the search for Add to * forms.
109 * @param CRM_Core_Form $form
110 * @param array $params
111 * This contains elements for search criteria.
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'])
120 $searchValues[] = array('contact_id', '=', $params['rel_contact_id'], 0, 1);
123 $searchValues[] = array('sort_name', 'LIKE', $params['rel_contact'], 0, 1);
126 $contactTypeAdded = FALSE;
128 $excludedContactIds = array();
129 if (isset($form->_contactId
)) {
130 $excludedContactIds[] = $form->_contactId
;
133 if (!empty($params['relationship_type_id'])) {
134 $relationshipType = new CRM_Contact_DAO_RelationshipType();
135 list($rid, $direction) = explode('_', $params['relationship_type_id'], 2);
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
;
144 $type = $relationshipType->contact_type_a
;
145 $subType = $relationshipType->contact_sub_type_a
;
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;
156 $searchValues[] = array('contact_sub_type', '=', $subType, 0, 0);
161 if (!$contactTypeAdded && !empty($params['contact_type'])) {
162 $searchValues[] = array('contact_type', '=', $params['contact_type'], 0, 0);
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);
174 $config = CRM_Core_Config
::singleton();
175 $searchRows = array();
177 //variable is set if only one record is foun and that record already has relationship with the contact
178 $duplicateRelationship = 0;
180 while ($result->fetch()) {
181 $query->convertToPseudoNames($result);
182 $contactID = $result->contact_id
;
183 if (in_array($contactID, $excludedContactIds)) {
184 $duplicateRelationship++
;
188 $duplicateRelationship = 0;
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
;
197 $contact_type = '<img src="' . $config->resourceBase
. 'i/contact_';
199 $searchRows[$contactID]['type'] = CRM_Contact_BAO_Contact_Utils
::getImage($result->contact_sub_type ?
$result->contact_sub_type
: $result->contact_type
203 $form->set('searchRows', $searchRows);
204 $form->set('duplicateRelationship', $duplicateRelationship);
207 // resetting the session variables if many records are found
208 $form->set('searchRows', NULL);
209 $form->set('duplicateRelationship', NULL);