* @param int $relTypeId
* one or more relationship type id's.
* @param string $name
+ * @param string $contactType
*
* @return array
* Array of contacts
*/
- public static function getPermissionedContacts($contactID, $relTypeId = NULL, $name = NULL) {
+ public static function getPermissionedContacts($contactID, $relTypeId = NULL, $name = NULL, $contactType = NULL) {
$contacts = array();
$args = array(1 => array($contactID, 'Integer'));
- $relationshipTypeClause = '';
+ $relationshipTypeClause = $contactTypeClause = '';
if ($relTypeId) {
// @todo relTypeId is only ever passed in as an int. Change this to reflect that -
$relationshipTypeClause = 'AND cr.relationship_type_id IN (%2) ';
$args[2] = array($relTypeId, 'String');
}
+
+ if ($contactType) {
+ $contactTypeClause = ' AND cr.relationship_type_id = crt.id AND crt.contact_type_b = %3 ';
+ $args[3] = array($contactType, 'String');
+ }
+
$query = "
SELECT cc.id as id, cc.sort_name as name
-FROM civicrm_relationship cr, civicrm_contact cc
+FROM civicrm_relationship cr, civicrm_contact cc, civicrm_relationship_type crt
WHERE
cr.contact_id_a = %1 AND
cr.is_permission_a_b = 1 AND
cc.id = cr.contact_id_b AND
cc.is_deleted = 0
$relationshipTypeClause
+$contactTypeClause
";
if (!empty($name)) {
* Array of fields from the onbehalf profile relevant to the organization.
*/
public static function processOnBehalfOrganization(&$behalfOrganization, &$contactID, &$values, &$params, $fields = NULL) {
- $isCurrentEmployer = FALSE;
+ $isNotCurrentEmployer = FALSE;
$dupeIDs = array();
$orgID = NULL;
if (!empty($behalfOrganization['organization_id'])) {
$orgID = $behalfOrganization['organization_id'];
unset($behalfOrganization['organization_id']);
- $isCurrentEmployer = TRUE;
+ }
+ // create employer relationship with $contactID only when new organization is there
+ // else retain the existing relationship
+ else {
+ // get the Employee relationship type id
+ $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
+;
+ // keep relationship params ready
+ $relParams['relationship_type_id'] = $relTypeId . '_a_b';
+ $relParams['is_permission_a_b'] = 1;
+ $relParams['is_active'] = 1;
+ $isNotCurrentEmployer = TRUE;
}
// formalities for creating / editing organization.
$behalfOrganization['contact_type'] = 'Organization';
- // get the relationship type id
- $relType = new CRM_Contact_DAO_RelationshipType();
- $relType->name_a_b = 'Employee of';
- $relType->find(TRUE);
- $relTypeId = $relType->id;
-
- // keep relationship params ready
- $relParams['relationship_type_id'] = $relTypeId . '_a_b';
- $relParams['is_permission_a_b'] = 1;
- $relParams['is_active'] = 1;
-
if (!$orgID) {
// check if matching organization contact exists
$dedupeParams = CRM_Dedupe_Finder::formatParams($behalfOrganization, 'Organization');
NULL, NULL, 'Organization'
);
// create relationship
- $relParams['contact_check'][$orgID] = 1;
- $cid = array('contact' => $contactID);
- CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid);
+ if ($isNotCurrentEmployer) {
+ $relParams['contact_check'][$orgID] = 1;
+ $cid = array('contact' => $contactID);
+ CRM_Contact_BAO_Relationship::legacyCreateMultiple($relParams, $cid);
+ }
// if multiple match - send a duplicate alert
if ($dupeIDs && (count($dupeIDs) > 1)) {
//make this employee of relationship as current
//employer / employee relationship, CRM-3532
- if ($isCurrentEmployer &&
+ if ($isNotCurrentEmployer &&
($orgID != CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'employer_id'))
) {
- $isCurrentEmployer = FALSE;
+ $isNotCurrentEmployer = FALSE;
}
- if (!$isCurrentEmployer && $orgID) {
+ if (!$isNotCurrentEmployer && $orgID) {
//build current employer params
$currentEmpParams[$contactID] = $orgID;
CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams);
$this->_defaultMemTypeId = $membership->membership_type_id;
if ($membership->contact_id != $this->_contactID) {
$validMembership = FALSE;
- $employers = CRM_Contact_BAO_Relationship::getPermissionedContacts(
- $this->_userID,
- CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b')
- );
- if (!empty($employers) && array_key_exists($membership->contact_id, $employers)) {
+ $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($this->_userID, NULL, NULL, 'Organization');
+ if (!empty($organizations) && array_key_exists($membership->contact_id, $organizations)) {
$this->_membershipContactID = $membership->contact_id;
$this->assign('membershipContactID', $this->_membershipContactID);
- $this->assign('membershipContactName', $employers[$this->_membershipContactID]['name']);
+ $this->assign('membershipContactName', $organizations[$this->_membershipContactID]['name']);
$validMembership = TRUE;
}
else {
}
if ($contactID) {
- $employers = CRM_Contact_BAO_Relationship::getPermissionedContacts(
- $contactID,
- CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b')
- );
+ // retrieve all permissioned organizations of contact $contactID
+ $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($contactID, NULL, NULL, 'Organization');
- if (count($employers)) {
+ if (count($organizations)) {
// Related org url - pass checksum if needed
$args = array(
'ufId' => $form->_values['onbehalf_profile_id'],
$locDataURL = CRM_Utils_System::url('civicrm/ajax/permlocation', $args, FALSE, NULL, FALSE);
$form->assign('locDataURL', $locDataURL);
}
- if (count($employers) > 0) {
- $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $employers));
+ if (count($organizations) > 0) {
+ $form->add('select', 'onbehalfof_id', '', CRM_Utils_Array::collect('name', $organizations));
$orgOptions = array(
0 => ts('Select an existing organization'),
}
// Verify user permission on related contact
- $employers = CRM_Contact_BAO_Relationship::getPermissionedContacts(
- $user,
- CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b')
- );
- if (!isset($employers[$cid])) {
+ $organizations = CRM_Contact_BAO_Relationship::getPermissionedContacts($user, NULL, NULL, 'Organization');
+ if (!isset($organizations[$cid])) {
CRM_Utils_System::civiExit();
}