예제 #1
0
 /**
  * where / qill clause for relationship
  *
  * @return void
  * @access public
  */
 function relationship(&$values)
 {
     list($name, $op, $value, $grouping, $wildcard) = $values;
     // also get values array for relation_target_name
     // for relatinship search we always do wildcard
     $targetName = $this->getWhereValues('relation_target_name', $grouping);
     $relStatus = $this->getWhereValues('relation_status', $grouping);
     $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
     $nameClause = $name = NULL;
     if ($targetName) {
         $name = trim($targetName[2]);
         if (substr($name, 0, 1) == '"' && substr($name, -1, 1) == '"') {
             $name = substr($name, 1, -1);
             $name = strtolower(CRM_Core_DAO::escapeString($name));
             $nameClause = "= '{$name}'";
         } else {
             $name = strtolower(CRM_Core_DAO::escapeString($name));
             $nameClause = "LIKE '%{$name}%'";
         }
     }
     $rel = explode('_', $value);
     self::$_relType = $rel[1];
     $params = array('id' => $rel[0]);
     $rTypeValues = array();
     $rType = CRM_Contact_BAO_RelationshipType::retrieve($params, $rTypeValues);
     if (!$rType) {
         return;
     }
     if ($rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) {
         self::$_relType = 'reciprocal';
     }
     if ($nameClause) {
         $this->_where[$grouping][] = "( contact_b.sort_name {$nameClause} AND contact_b.id != contact_a.id )";
     }
     $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Individual');
     $relTypeOrg = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Organization');
     $relTypeHou = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, 'Household');
     $allRelationshipType = array();
     $allRelationshipType = array_merge($relTypeInd, $relTypeOrg);
     $allRelationshipType = array_merge($allRelationshipType, $relTypeHou);
     if ($nameClause || !$targetGroup) {
         $this->_qill[$grouping][] = "{$allRelationshipType[$value]}  {$name}";
     }
     //check to see if the target contact is in specified group
     if ($targetGroup) {
         //add contacts from static groups
         $this->_tables['civicrm_relationship_group_contact'] = $this->_whereTables['civicrm_relationship_group_contact'] = " LEFT JOIN civicrm_group_contact civicrm_relationship_group_contact ON civicrm_relationship_group_contact.contact_id = contact_b.id";
         $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN  (" . implode(",", $targetGroup[2]) . ") )";
         //add contacts from saved searches
         $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", "contact_b");
         //set the group where clause
         if ($ssWhere) {
             $groupWhere[] = "( " . $ssWhere . " )";
         }
         $this->_where[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )";
         //Get the names of the target groups for the qill
         $groupNames =& CRM_Core_PseudoConstant::group();
         $qillNames = array();
         foreach ($targetGroup[2] as $groupId) {
             if (array_key_exists($groupId, $groupNames)) {
                 $qillNames[] = $groupNames[$groupId];
             }
         }
         $this->_qill[$grouping][] = "{$allRelationshipType[$value]}  ( " . implode(", ", $qillNames) . " )";
     }
     //check for active, inactive and all relation status
     $today = date('Ymd');
     if ($relStatus[2] == 0) {
         $this->_where[$grouping][] = "(\ncivicrm_relationship.is_active = 1 AND\n( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND\n( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )\n)";
         $this->_qill[$grouping][] = ts('Relationship - Active');
     } elseif ($relStatus[2] == 1) {
         $this->_where[$grouping][] = "(\ncivicrm_relationship.is_active = 0 OR\ncivicrm_relationship.end_date < {$today} OR\ncivicrm_relationship.start_date > {$today}\n)";
         $this->_qill[$grouping][] = ts('Relationship - Inactive');
     }
     $this->_where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rel[0];
     $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1;
     $this->_useDistinct = TRUE;
 }
예제 #2
0
 /**
  * Check the membership extended through relationship.
  *
  * @param int $membershipId
  *   Membership id.
  * @param int $contactId
  *   Contact id.
  *
  * @param int $action
  *
  * @return array
  *   array of contact_id of all related contacts.
  */
 public static function checkMembershipRelationship($membershipId, $contactId, $action = CRM_Core_Action::ADD)
 {
     $contacts = array();
     $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $membershipId, 'membership_type_id');
     $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipTypeID);
     $relationships = array();
     if (isset($membershipType['relationship_type_id'])) {
         $relationships = CRM_Contact_BAO_Relationship::getRelationship($contactId, CRM_Contact_BAO_Relationship::CURRENT);
         if ($action & CRM_Core_Action::UPDATE) {
             $pastRelationships = CRM_Contact_BAO_Relationship::getRelationship($contactId, CRM_Contact_BAO_Relationship::PAST);
             $relationships = array_merge($relationships, $pastRelationships);
         }
     }
     if (!empty($relationships)) {
         // check for each contact relationships
         foreach ($relationships as $values) {
             //get details of the relationship type
             $relType = array('id' => $values['civicrm_relationship_type_id']);
             $relValues = array();
             CRM_Contact_BAO_RelationshipType::retrieve($relType, $relValues);
             // Check if contact's relationship type exists in membership type
             $relTypeDirs = array();
             $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']);
             $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']);
             $bidirectional = FALSE;
             foreach ($relTypeIds as $key => $value) {
                 $relTypeDirs[] = $value . '_' . $relDirections[$key];
                 if (in_array($value, $relType) && $relValues['name_a_b'] == $relValues['name_b_a']) {
                     $bidirectional = TRUE;
                     break;
                 }
             }
             $relTypeDir = $values['civicrm_relationship_type_id'] . '_' . $values['rtype'];
             if ($bidirectional || in_array($relTypeDir, $relTypeDirs)) {
                 // $values['status'] is going to have value for
                 // current or past relationships.
                 $contacts[$values['cid']] = $values['status'];
             }
         }
     }
     // Sort by contact_id ascending
     ksort($contacts);
     return $contacts;
 }
예제 #3
0
 /**
  * Where / qill clause for relationship.
  *
  * @param array $values
  */
 public function relationship(&$values)
 {
     list($name, $op, $value, $grouping, $wildcard) = $values;
     if ($this->_relationshipValuesAdded) {
         return;
     }
     // also get values array for relation_target_name
     // for relationship search we always do wildcard
     $relationType = $this->getWhereValues('relation_type_id', $grouping);
     $targetName = $this->getWhereValues('relation_target_name', $grouping);
     $relStatus = $this->getWhereValues('relation_status', $grouping);
     $relPermission = $this->getWhereValues('relation_permission', $grouping);
     $targetGroup = $this->getWhereValues('relation_target_group', $grouping);
     $nameClause = $name = NULL;
     if ($targetName) {
         $name = trim($targetName[2]);
         if (substr($name, 0, 1) == '"' && substr($name, -1, 1) == '"') {
             $name = substr($name, 1, -1);
             $name = strtolower(CRM_Core_DAO::escapeString($name));
             $nameClause = "= '{$name}'";
         } else {
             $name = strtolower(CRM_Core_DAO::escapeString($name));
             $nameClause = "LIKE '%{$name}%'";
         }
     }
     $rTypeValues = array();
     if (!empty($relationType)) {
         $rel = explode('_', $relationType[2]);
         self::$_relType = $rel[1];
         $params = array('id' => $rel[0]);
         $rType = CRM_Contact_BAO_RelationshipType::retrieve($params, $rTypeValues);
     }
     if (!empty($rTypeValues) && $rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) {
         // if we don't know which end of the relationship we are dealing with we'll create a temp table
         //@todo unless we are dealing with a target group
         self::$_relType = 'reciprocal';
     }
     // if we are creating a temp table we build our own where for the relationship table
     $relationshipTempTable = NULL;
     if (self::$_relType == 'reciprocal' && empty($targetGroup)) {
         $where = array();
         self::$_relationshipTempTable = $relationshipTempTable = CRM_Core_DAO::createTempTableName('civicrm_rel');
         if ($nameClause) {
             $where[$grouping][] = " sort_name {$nameClause} ";
         }
     } else {
         $where =& $this->_where;
         if ($nameClause) {
             $where[$grouping][] = "( contact_b.sort_name {$nameClause} AND contact_b.id != contact_a.id )";
         }
     }
     $allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
     if ($nameClause || !$targetGroup) {
         if (!empty($relationType)) {
             $this->_qill[$grouping][] = $allRelationshipType[$relationType[2]] . " {$name}";
         } else {
             $this->_qill[$grouping][] = $name;
         }
     }
     //check to see if the target contact is in specified group
     if ($targetGroup) {
         //add contacts from static groups
         $this->_tables['civicrm_relationship_group_contact'] = $this->_whereTables['civicrm_relationship_group_contact'] = " LEFT JOIN civicrm_group_contact civicrm_relationship_group_contact ON civicrm_relationship_group_contact.contact_id = contact_b.id AND civicrm_relationship_group_contact.status = 'Added'";
         $groupWhere[] = "( civicrm_relationship_group_contact.group_id IN  (" . implode(",", $targetGroup[2]) . ") ) ";
         //add contacts from saved searches
         $ssWhere = $this->addGroupContactCache($targetGroup[2], "civicrm_relationship_group_contact_cache", "contact_b", $op);
         //set the group where clause
         if ($ssWhere) {
             $groupWhere[] = "( " . $ssWhere . " )";
         }
         $this->_where[$grouping][] = "( " . implode(" OR ", $groupWhere) . " )";
         //Get the names of the target groups for the qill
         $groupNames = CRM_Core_PseudoConstant::group();
         $qillNames = array();
         foreach ($targetGroup[2] as $groupId) {
             if (array_key_exists($groupId, $groupNames)) {
                 $qillNames[] = $groupNames[$groupId];
             }
         }
         if (!empty($relationType)) {
             $this->_qill[$grouping][] = $allRelationshipType[$relationType[2]] . " ( " . implode(", ", $qillNames) . " )";
         } else {
             $this->_qill[$grouping][] = implode(", ", $qillNames);
         }
     }
     // Note we do not currently set mySql to handle timezones, so doing this the old-fashioned way
     $today = date('Ymd');
     //check for active, inactive and all relation status
     if ($relStatus[2] == 0) {
         $where[$grouping][] = "(\ncivicrm_relationship.is_active = 1 AND\n( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND\n( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )\n)";
         $this->_qill[$grouping][] = ts('Relationship - Active and Current');
     } elseif ($relStatus[2] == 1) {
         $where[$grouping][] = "(\ncivicrm_relationship.is_active = 0 OR\ncivicrm_relationship.end_date < {$today} OR\ncivicrm_relationship.start_date > {$today}\n)";
         $this->_qill[$grouping][] = ts('Relationship - Inactive or not Current');
     }
     $onlyDeleted = 0;
     if (in_array(array('deleted_contacts', '=', '1', '0', '0'), $this->_params)) {
         $onlyDeleted = 1;
     }
     $where[$grouping][] = "(contact_b.is_deleted = {$onlyDeleted})";
     //check for permissioned, non-permissioned and all permissioned relations
     if ($relPermission[2] == 1) {
         $where[$grouping][] = "(\ncivicrm_relationship.is_permission_a_b = 1\n)";
         $this->_qill[$grouping][] = ts('Relationship - Permissioned');
     } elseif ($relPermission[2] == 2) {
         //non-allowed permission relationship.
         $where[$grouping][] = "(\ncivicrm_relationship.is_permission_a_b = 0\n)";
         $this->_qill[$grouping][] = ts('Relationship - Non-permissioned');
     }
     $this->addRelationshipDateClauses($grouping, $where);
     if (!empty($relationType) && !empty($rType) && isset($rType->id)) {
         $where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rType->id;
     }
     $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1;
     $this->_useDistinct = TRUE;
     $this->_relationshipValuesAdded = TRUE;
     // it could be a or b, using an OR creates an unindexed join - better to create a temp table &
     // join on that,
     // @todo creating a temp table could be expanded to group filter
     // as even creating a temp table of all relationships is much much more efficient than
     // an OR in the join
     if ($relationshipTempTable) {
         $whereClause = '';
         if (!empty($where[$grouping])) {
             $whereClause = ' WHERE ' . implode(' AND ', $where[$grouping]);
             $whereClause = str_replace('contact_b', 'c', $whereClause);
         }
         $sql = "\n        CREATE TEMPORARY TABLE {$relationshipTempTable}\n          (SELECT contact_id_b as contact_id, civicrm_relationship.id\n            FROM civicrm_relationship\n            INNER JOIN  civicrm_contact c ON civicrm_relationship.contact_id_a = c.id\n            {$whereClause} )\n          UNION\n            (SELECT contact_id_a as contact_id, civicrm_relationship.id\n            FROM civicrm_relationship\n            INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_b = c.id\n            {$whereClause} )\n      ";
         CRM_Core_DAO::executeQuery($sql);
     }
 }
예제 #4
0
 /**
  * Create / update / delete membership for related contacts.
  *
  * This function will create/update/delete membership for related
  * contact based on 1) contact have active membership 2) that
  * membership is is extedned by the same relationship type to that
  * of the existing relationship.
  *
  * @param int $contactId
  *   contact id.
  * @param array $params
  *   array of values submitted by POST.
  * @param array $ids
  *   array of ids.
  * @param \const|int $action which action called this function
  *
  * @param bool $active
  *
  * @throws \CRM_Core_Exception
  */
 public static function relatedMemberships($contactId, &$params, $ids, $action = CRM_Core_Action::ADD, $active = TRUE)
 {
     // Check the end date and set the status of the relationship
     // accordingly.
     $status = self::CURRENT;
     $targetContact = $targetContact = CRM_Utils_Array::value('contact_check', $params, array());
     $today = date('Ymd');
     // If a relationship hasn't yet started, just return for now
     // TODO: handle edge-case of updating start_date of an existing relationship
     if (!empty($params['start_date'])) {
         $startDate = substr(CRM_Utils_Date::format($params['start_date']), 0, 8);
         if ($today < $startDate) {
             return;
         }
     }
     if (!empty($params['end_date'])) {
         $endDate = substr(CRM_Utils_Date::format($params['end_date']), 0, 8);
         if ($today > $endDate) {
             $status = self::PAST;
         }
     }
     if ($action & CRM_Core_Action::ADD && $status & self::PAST) {
         // If relationship is PAST and action is ADD, do nothing.
         return;
     }
     $rel = explode('_', $params['relationship_type_id']);
     $relTypeId = $rel[0];
     if (!empty($rel[1])) {
         $relDirection = "_{$rel[1]}_{$rel[2]}";
     } else {
         // this call is coming from somewhere where the direction was resolved early on (e.g an api call)
         // so we can assume _a_b
         $relDirection = "_a_b";
         $targetContact = array($params['contact_id_b'] => 1);
     }
     if ($action & CRM_Core_Action::ADD || $action & CRM_Core_Action::DELETE) {
         $contact = $contactId;
     } elseif ($action & CRM_Core_Action::UPDATE) {
         $contact = $ids['contact'];
         $targetContact = array($ids['contactTarget'] => 1);
     }
     // Build the 'values' array for
     // 1. ContactA
     // 2. ContactB
     // This will allow us to check if either of the contacts in
     // relationship have active memberships.
     $values = array();
     // 1. ContactA
     $values[$contact] = array('relatedContacts' => $targetContact, 'relationshipTypeId' => $relTypeId, 'relationshipTypeDirection' => $relDirection);
     // 2. ContactB
     if (!empty($targetContact)) {
         foreach ($targetContact as $cid => $donCare) {
             $values[$cid] = array('relatedContacts' => array($contact => 1), 'relationshipTypeId' => $relTypeId);
             $relTypeParams = array('id' => $relTypeId);
             $relTypeValues = array();
             CRM_Contact_BAO_RelationshipType::retrieve($relTypeParams, $relTypeValues);
             if (CRM_Utils_Array::value('name_a_b', $relTypeValues) == CRM_Utils_Array::value('name_b_a', $relTypeValues)) {
                 $values[$cid]['relationshipTypeDirection'] = '_a_b';
             } else {
                 $values[$cid]['relationshipTypeDirection'] = $relDirection == '_a_b' ? '_b_a' : '_a_b';
             }
         }
     }
     // CRM-15829 UPDATES
     // If we're looking for active memberships we must consider pending (id: 5) ones too.
     // Hence we can't just call CRM_Member_BAO_Membership::getValues below with the active flag, is it would completely miss pending relatioships.
     // As suggested by @davecivicrm, the pending status id is fetched using the CRM_Member_PseudoConstant::membershipStatus() class and method, since these ids differ from system to system.
     $pendingStatusId = array_search('Pending', CRM_Member_PseudoConstant::membershipStatus());
     $query = 'SELECT * FROM `civicrm_membership_status`';
     if ($active) {
         $query .= ' WHERE `is_current_member` = 1 OR `id` = %1 ';
     }
     $dao = CRM_Core_DAO::executeQuery($query, array(1 => array($pendingStatusId, 'Integer')));
     while ($dao->fetch()) {
         $membershipStatusRecordIds[$dao->id] = $dao->id;
     }
     // Now get the active memberships for all the contacts.
     // If contact have any valid membership(s), then add it to
     // 'values' array.
     foreach ($values as $cid => $subValues) {
         $memParams = array('contact_id' => $cid);
         $memberships = array();
         // CRM-15829 UPDATES
         // Since we want PENDING memberships as well, the $active flag needs to be set to false so that this will return all memberships and we can then filter the memberships based on the status IDs recieved above.
         CRM_Member_BAO_Membership::getValues($memParams, $memberships, FALSE, TRUE);
         // CRM-15829 UPDATES
         // filter out the memberships returned by CRM_Member_BAO_Membership::getValues based on the status IDs fetched on line ~1462
         foreach ($memberships as $key => $membership) {
             if (!isset($memberships[$key]['status_id'])) {
                 continue;
             }
             $membershipStatusId = $memberships[$key]['status_id'];
             if (!isset($membershipStatusRecordIds[$membershipStatusId])) {
                 unset($memberships[$key]);
             }
         }
         if (empty($memberships)) {
             continue;
         }
         //get ownerMembershipIds for related Membership
         //this is to handle memberships being deleted and recreated
         if (!empty($memberships['owner_membership_ids'])) {
             $ownerMemIds[$cid] = $memberships['owner_membership_ids'];
             unset($memberships['owner_membership_ids']);
         }
         $values[$cid]['memberships'] = $memberships;
     }
     $deceasedStatusId = array_search('Deceased', CRM_Member_PseudoConstant::membershipStatus());
     // done with 'values' array.
     // Finally add / edit / delete memberships for the related contacts
     foreach ($values as $cid => $details) {
         if (!array_key_exists('memberships', $details)) {
             continue;
         }
         $relatedContacts = array_keys(CRM_Utils_Array::value('relatedContacts', $details, array()));
         $mainRelatedContactId = reset($relatedContacts);
         foreach ($details['memberships'] as $membershipId => $membershipValues) {
             $relTypeIds = array();
             if ($action & CRM_Core_Action::DELETE) {
                 // Delete memberships of the related contacts only if relationship type exists for membership type
                 $query = "\nSELECT relationship_type_id, relationship_direction\n  FROM civicrm_membership_type\n WHERE id = {$membershipValues['membership_type_id']}";
                 $dao = CRM_Core_DAO::executeQuery($query);
                 $relTypeDirs = array();
                 while ($dao->fetch()) {
                     $relTypeId = $dao->relationship_type_id;
                     $relDirection = $dao->relationship_direction;
                 }
                 $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $relTypeId);
                 if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds) && !empty($membershipValues['owner_membership_id']) && !empty($values[$mainRelatedContactId]['memberships'][$membershipValues['owner_membership_id']])) {
                     CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
                 }
                 continue;
             }
             if ($action & CRM_Core_Action::UPDATE && $status & self::PAST && $membershipValues['owner_membership_id']) {
                 // If relationship is PAST and action is UPDATE
                 // then delete the RELATED membership
                 CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
                 continue;
             }
             // add / edit the memberships for related
             // contacts.
             // Get the Membership Type Details.
             $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipValues['membership_type_id']);
             // Check if contact's relationship type exists in membership type
             $relTypeDirs = array();
             if (!empty($membershipType['relationship_type_id'])) {
                 $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']);
             }
             if (!empty($membershipType['relationship_direction'])) {
                 $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']);
             }
             foreach ($relTypeIds as $key => $value) {
                 $relTypeDirs[] = $value . '_' . $relDirections[$key];
             }
             $relTypeDir = $details['relationshipTypeId'] . $details['relationshipTypeDirection'];
             if (in_array($relTypeDir, $relTypeDirs)) {
                 // Check if relationship being created/updated is
                 // similar to that of membership type's
                 // relationship.
                 $membershipValues['owner_membership_id'] = $membershipId;
                 unset($membershipValues['id']);
                 unset($membershipValues['membership_contact_id']);
                 unset($membershipValues['contact_id']);
                 unset($membershipValues['membership_id']);
                 foreach ($details['relatedContacts'] as $relatedContactId => $donCare) {
                     $membershipValues['contact_id'] = $relatedContactId;
                     if ($deceasedStatusId && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $relatedContactId, 'is_deceased')) {
                         $membershipValues['status_id'] = $deceasedStatusId;
                         $membershipValues['skipStatusCal'] = TRUE;
                     }
                     foreach (array('join_date', 'start_date', 'end_date') as $dateField) {
                         if (!empty($membershipValues[$dateField])) {
                             $membershipValues[$dateField] = CRM_Utils_Date::processDate($membershipValues[$dateField]);
                         }
                     }
                     if ($action & CRM_Core_Action::UPDATE) {
                         //if updated relationship is already related to contact don't delete existing inherited membership
                         if (in_array($relTypeId, $relTypeIds) && !empty($values[$relatedContactId]['memberships']) && !empty($ownerMemIds) && in_array($membershipValues['owner_membership_id'], $ownerMemIds[$relatedContactId])) {
                             continue;
                         }
                         //delete the membership record for related
                         //contact before creating new membership record.
                         CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $relatedContactId);
                     }
                     // check whether we have some related memberships still available
                     $query = "\nSELECT count(*)\n  FROM civicrm_membership\n    LEFT JOIN civicrm_membership_status ON (civicrm_membership_status.id = civicrm_membership.status_id)\n WHERE membership_type_id = {$membershipValues['membership_type_id']} AND owner_membership_id = {$membershipValues['owner_membership_id']}\n    AND is_current_member = 1";
                     $result = CRM_Core_DAO::singleValueQuery($query);
                     if ($result < CRM_Utils_Array::value('max_related', $membershipValues, PHP_INT_MAX)) {
                         CRM_Member_BAO_Membership::create($membershipValues, CRM_Core_DAO::$_nullArray);
                     }
                 }
             } elseif ($action & CRM_Core_Action::UPDATE) {
                 // if action is update and updated relationship do
                 // not match with the existing
                 // membership=>relationship then we need to
                 // change the status of the membership record to expired for
                 // previous relationship -- CRM-12078.
                 // CRM-16087 we need to pass ownerMembershipId to isRelatedMembershipExpired function
                 if (empty($params['relationship_ids']) && !empty($params['id'])) {
                     $relIds = array($params['id']);
                 } else {
                     $relIds = CRM_Utils_Array::value('relationship_ids', $params);
                 }
                 if (self::isRelatedMembershipExpired($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId, $relIds) && !empty($membershipValues['owner_membership_id']) && !empty($values[$mainRelatedContactId]['memberships'][$membershipValues['owner_membership_id']])) {
                     $membershipValues['status_id'] = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipStatus', 'Expired', 'id', 'label');
                     $type = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $membershipValues['membership_type_id'], 'name', 'id');
                     CRM_Member_BAO_Membership::add($membershipValues);
                     CRM_Core_Session::setStatus(ts("Inherited membership {$type} status was changed to Expired due to the change in relationship type."), ts('Record Updated'), 'alert');
                 }
             }
         }
     }
 }
예제 #5
0
 /**
  * Function to create / update / delete membership for related contacts.
  *
  * This function will create/update/delete membership for related
  * contact based on 1) contact have active membership 2) that
  * membership is is extedned by the same relationship type to that
  * of the existing relationship.
  *
  * @param $contactId  Int     contact id
  * @param $params     array   array of values submitted by POST
  * @param $ids        array   array of ids
  * @param $action             which action called this function
  *
  * @static
  *
  */
 static function relatedMemberships($contactId, &$params, $ids, $action = CRM_Core_Action::ADD, $active = TRUE)
 {
     // Check the end date and set the status of the relationship
     // accrodingly.
     $status = self::CURRENT;
     if (!empty($params['end_date'])) {
         $endDate = CRM_Utils_Date::setDateDefaults(CRM_Utils_Date::format($params['end_date']), NULL, 'Ymd');
         $today = date('Ymd');
         if ($today > $endDate) {
             $status = self::PAST;
         }
     }
     if ($action & CRM_Core_Action::ADD && $status & self::PAST) {
         // if relationship is PAST and action is ADD, no qustion
         // of creating RELATED membership and return back to
         // calling method
         return;
     }
     $rel = explode('_', $params['relationship_type_id']);
     $relTypeId = $rel[0];
     $relDirection = "_{$rel[1]}_{$rel[2]}";
     $targetContact = array();
     if ($action & CRM_Core_Action::ADD || $action & CRM_Core_Action::DELETE) {
         $contact = $contactId;
         $targetContact = CRM_Utils_Array::value('contact_check', $params);
     } elseif ($action & CRM_Core_Action::UPDATE) {
         $contact = $ids['contact'];
         $targetContact = array($ids['contactTarget'] => 1);
     }
     // Build the 'values' array for
     // 1. ContactA
     // 2. ContactB
     // This will allow us to check if either of the contacts in
     // relationship have active memberships.
     $values = array();
     // 1. ContactA
     $values[$contact] = array('relatedContacts' => $targetContact, 'relationshipTypeId' => $relTypeId, 'relationshipTypeDirection' => $relDirection);
     // 2. ContactB
     if (!empty($targetContact)) {
         foreach ($targetContact as $cid => $donCare) {
             $values[$cid] = array('relatedContacts' => array($contact => 1), 'relationshipTypeId' => $relTypeId);
             $relTypeParams = array('id' => $relTypeId);
             $relTypeValues = array();
             CRM_Contact_BAO_RelationshipType::retrieve($relTypeParams, $relTypeValues);
             if (CRM_Utils_Array::value('name_a_b', $relTypeValues) == CRM_Utils_Array::value('name_b_a', $relTypeValues)) {
                 $values[$cid]['relationshipTypeDirection'] = '_a_b';
             } else {
                 $values[$cid]['relationshipTypeDirection'] = $relDirection == '_a_b' ? '_b_a' : '_a_b';
             }
         }
     }
     // Now get the active memberships for all the contacts.
     // If contact have any valid membership(s), then add it to
     // 'values' array.
     foreach ($values as $cid => $subValues) {
         $memParams = array('contact_id' => $cid);
         $memberships = array();
         CRM_Member_BAO_Membership::getValues($memParams, $memberships, $active);
         if (empty($memberships)) {
             continue;
         }
         $values[$cid]['memberships'] = $memberships;
     }
     $deceasedStatusId = array_search('Deceased', CRM_Member_PseudoConstant::membershipStatus());
     // done with 'values' array.
     // Finally add / edit / delete memberships for the related contacts
     foreach ($values as $cid => $details) {
         if (!array_key_exists('memberships', $details)) {
             continue;
         }
         $mainRelatedContactId = key(CRM_Utils_Array::value('relatedContacts', $details, array()));
         foreach ($details['memberships'] as $membershipId => $membershipValues) {
             $relTypeIds = array();
             if ($action & CRM_Core_Action::DELETE) {
                 // Delete memberships of the related contacts only if relationship type exists for membership type
                 $query = "\nSELECT relationship_type_id, relationship_direction\n  FROM civicrm_membership_type\n WHERE id = {$membershipValues['membership_type_id']}";
                 $dao = CRM_Core_DAO::executeQuery($query);
                 $relTypeDirs = array();
                 while ($dao->fetch()) {
                     $relTypeId = $dao->relationship_type_id;
                     $relDirection = $dao->relationship_direction;
                 }
                 $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $relTypeId);
                 if (in_array($values[$cid]['relationshipTypeId'], $relTypeIds)) {
                     CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $mainRelatedContactId);
                 }
                 continue;
             }
             if ($action & CRM_Core_Action::UPDATE && $status & self::PAST && $membershipValues['owner_membership_id']) {
                 // If relationship is PAST and action is UPDATE
                 // then delete the RELATED membership
                 CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
                 continue;
             }
             // add / edit the memberships for related
             // contacts.
             // Get the Membership Type Details.
             $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipValues['membership_type_id']);
             // Check if contact's relationship type exists in membership type
             $relTypeDirs = array();
             if (CRM_Utils_Array::value('relationship_type_id', $membershipType)) {
                 $relTypeIds = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_type_id']);
             }
             if (CRM_Utils_Array::value('relationship_direction', $membershipType)) {
                 $relDirections = explode(CRM_Core_DAO::VALUE_SEPARATOR, $membershipType['relationship_direction']);
             }
             foreach ($relTypeIds as $key => $value) {
                 $relTypeDirs[] = $value . '_' . $relDirections[$key];
             }
             $relTypeDir = $details['relationshipTypeId'] . $details['relationshipTypeDirection'];
             if (in_array($relTypeDir, $relTypeDirs)) {
                 // Check if relationship being created/updated is
                 // similar to that of membership type's
                 // relationship.
                 $membershipValues['owner_membership_id'] = $membershipId;
                 unset($membershipValues['id']);
                 unset($membershipValues['membership_contact_id']);
                 unset($membershipValues['contact_id']);
                 unset($membershipValues['membership_id']);
                 foreach ($details['relatedContacts'] as $relatedContactId => $donCare) {
                     $membershipValues['contact_id'] = $relatedContactId;
                     if ($deceasedStatusId && CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $relatedContactId, 'is_deceased')) {
                         $membershipValues['status_id'] = $deceasedStatusId;
                         $membershipValues['skipStatusCal'] = TRUE;
                     }
                     foreach (array('join_date', 'start_date', 'end_date') as $dateField) {
                         if (CRM_Utils_Array::value($dateField, $membershipValues)) {
                             $membershipValues[$dateField] = CRM_Utils_Date::processDate($membershipValues[$dateField]);
                         }
                     }
                     if ($action & CRM_Core_Action::UPDATE) {
                         //delete the membership record for related
                         //contact before creating new membership record.
                         CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $relatedContactId);
                     }
                     // check whether we have some related memberships still available
                     $query = "\nSELECT count(*)\n  FROM civicrm_membership\n    LEFT JOIN civicrm_membership_status ON (civicrm_membership_status.id = civicrm_membership.status_id)\n WHERE membership_type_id = {$membershipValues['membership_type_id']} AND owner_membership_id = {$membershipValues['owner_membership_id']}\n    AND is_current_member = 1";
                     $result = CRM_Core_DAO::singleValueQuery($query);
                     if ($result < CRM_Utils_Array::value('max_related', $membershipValues, PHP_INT_MAX)) {
                         CRM_Member_BAO_Membership::create($membershipValues, CRM_Core_DAO::$_nullArray);
                     }
                 }
             } elseif ($action & CRM_Core_Action::UPDATE) {
                 // if action is update and updated relationship do
                 // not match with the existing
                 // membership=>relationship then we need to
                 // delete the membership record created for
                 // previous relationship.
                 if (self::isDeleteRelatedMembership($relTypeIds, $contactId, $mainRelatedContactId, $relTypeId, CRM_Utils_Array::value('relationship_ids', $params))) {
                     CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $mainRelatedContactId);
                 }
             }
         }
     }
 }
예제 #6
0
 /**
  * Function to check the membership extended through relationship
  * 
  * @param int $membershipId membership id
  * @param int $contactId    contact id
  *
  * @return Array    array of contact_id of all related contacts.
  * @static
  */
 static function checkMembershipRelationship($membershipId, $contactId, $action = CRM_Core_Action::ADD)
 {
     $contacts = array();
     $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_Membership', $membershipId, 'membership_type_id');
     require_once 'CRM/Member/BAO/MembershipType.php';
     $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipTypeID);
     require_once 'CRM/Contact/BAO/Relationship.php';
     $relationships = array();
     if (isset($membershipType['relationship_type_id'])) {
         $relationships = CRM_Contact_BAO_Relationship::getRelationship($contactId, CRM_Contact_BAO_Relationship::CURRENT);
         if ($action & CRM_Core_Action::UPDATE) {
             $pastRelationships = CRM_Contact_BAO_Relationship::getRelationship($contactId, CRM_Contact_BAO_Relationship::PAST);
             $relationships = array_merge($relationships, $pastRelationships);
         }
     }
     if (!empty($relationships)) {
         require_once "CRM/Contact/BAO/RelationshipType.php";
         // check for each contact relationships
         foreach ($relationships as $values) {
             //get details of the relationship type
             $relType = array('id' => $values['civicrm_relationship_type_id']);
             $relValues = array();
             CRM_Contact_BAO_RelationshipType::retrieve($relType, $relValues);
             // 1. Check if contact and membership type relationship type are same
             // 2. Check if relationship direction is same or name_a_b = name_b_a
             if ($values['civicrm_relationship_type_id'] == $membershipType['relationship_type_id'] && ($values['rtype'] == $membershipType['relationship_direction'] || $relValues['name_a_b'] == $relValues['name_b_a'])) {
                 // $values['status'] is going to have value for
                 // current or past relationships.
                 $contacts[$values['cid']] = $values['status'];
             }
         }
     }
     return $contacts;
 }
 /**
  * Where / qill clause for relationship_type
  *
  * @param $values
  *
  * @return void
  */
 public function relationshipType(&$values)
 {
     list($name, $op, $value, $grouping, $wildcard) = $values;
     $clause = array();
     foreach ($value as $k => $relationship_type_value) {
         // we gebruiken de key om relaties in 2 richtingen op te vangen.
         $relationship_type_id = substr_replace($relationship_type_value, "", -4);
         $relationship_type_params = array('id' => $relationship_type_id);
         CRM_Contact_BAO_RelationshipType::retrieve($relationship_type_params, $relationship_type);
         $clause[$relationship_type['label_a_b']] = $relationship_type_id;
     }
     $this->_where[$grouping][] = "relationship.relationship_type_id {$op} ('" . implode("', '", $clause) . "')";
     $this->_qill[$grouping][] = ts('Relationship Type') . " {$op} " . implode(' ' . ts('or') . ' ', array_keys($clause));
 }
예제 #8
0
 /**
  * where / qill clause for relationship
  *
  * @return void
  * @access public
  */
 function relationship(&$values)
 {
     list($name, $op, $value, $grouping, $wildcard) = $values;
     // also get values array for relation_target_name
     // for relatinship search we always do wildcard
     $targetName = $this->getWhereValues('relation_target_name', $grouping);
     $relStatus = $this->getWhereValues('relation_status', $grouping);
     $nameClause = null;
     if ($targetName) {
         $name = trim($targetName[2]);
         if (substr($name, 0, 1) == '"' && substr($name, -1, 1) == '"') {
             $name = substr($name, 1, -1);
             $name = strtolower(CRM_Core_DAO::escapeString($name));
             $nameClause = "= '{$name}'";
         } else {
             $name = strtolower(CRM_Core_DAO::escapeString($name));
             $nameClause = "LIKE '%{$name}%'";
         }
     }
     $rel = explode('_', $value);
     self::$_relType = $rel[1];
     if ($nameClause) {
         require_once 'CRM/Contact/BAO/RelationshipType.php';
         $params = array('id' => $rel[0]);
         $rTypeValues = array();
         require_once "CRM/Contact/BAO/RelationshipType.php";
         $rType =& CRM_Contact_BAO_RelationshipType::retrieve($params, $rTypeValues);
         if (!$rType) {
             return;
         }
         // for relatinship search we always do wildcard
         if ($rTypeValues['name_a_b'] == $rTypeValues['name_b_a']) {
             self::$_relType = 'reciprocal';
         }
         $this->_where[$grouping][] = "( contact_b.sort_name {$nameClause} AND contact_b.id != contact_a.id )";
     }
     require_once 'CRM/Contact/BAO/Relationship.php';
     $relTypeInd = CRM_Contact_BAO_Relationship::getContactRelationshipType(null, 'null', null, 'Individual');
     $relTypeOrg = CRM_Contact_BAO_Relationship::getContactRelationshipType(null, 'null', null, 'Organization');
     $relTypeHou = CRM_Contact_BAO_Relationship::getContactRelationshipType(null, 'null', null, 'Household');
     $allRelationshipType = array();
     $allRelationshipType = array_merge($relTypeInd, $relTypeOrg);
     $allRelationshipType = array_merge($allRelationshipType, $relTypeHou);
     $this->_qill[$grouping][] = "{$allRelationshipType[$value]}  {$name}";
     //check for active, inactive and all relation status
     $today = date('Ymd');
     if ($relStatus[2] == 0) {
         $this->_where[$grouping][] = "civicrm_relationship.is_active = 1 AND ( civicrm_relationship.end_date is NULL OR civicrm_relationship.end_date >= {$today} )";
         $this->_qill[$grouping][] = ts('Relationship - Active');
     } else {
         if ($relStatus[2] == 1) {
             $this->_where[$grouping][] = "(civicrm_relationship.is_active = 0 OR civicrm_relationship.end_date < {$today})";
             $this->_qill[$grouping][] = ts('Relationship - Inactive');
         }
     }
     $this->_where[$grouping][] = 'civicrm_relationship.relationship_type_id = ' . $rel[0];
     $this->_tables['civicrm_relationship'] = $this->_whereTables['civicrm_relationship'] = 1;
     $this->_useDistinct = true;
 }
예제 #9
0
 /**
  * Function to create / update / delete membership for related contacts.
  * 
  * This function will create/update/delete membership for related
  * contact based on 1) contact have active membership 2) that
  * membership is is extedned by the same relationship type to that
  * of the existing relationship.
  * 
  * @param $contactId  Int     contact id
  * @param $params     array   array of values submitted by POST
  * @param $ids        array   array of ids
  * @param $action             which action called this function
  * 
  * @static
  *
  */
 static function relatedMemberships($contactId, &$params, $ids, $action = CRM_Core_Action::ADD, $active = true)
 {
     // Check the end date and set the status of the relationship
     // accrodingly.
     $status = self::CURRENT;
     if (!empty($params['end_date'])) {
         $endDate = CRM_Utils_Date::setDateDefaults($params['end_date'], null, 'Ymd');
         $today = date('Ymd');
         if ($today > $endDate) {
             $status = self::PAST;
         }
     }
     if ($action & CRM_Core_Action::ADD && $status & self::PAST) {
         // if relationship is PAST and action is ADD, no qustion
         // of creating RELATED membership and return back to
         // calling method
         return;
     }
     $rel = explode("_", $params['relationship_type_id']);
     $relTypeId = $rel[0];
     $relDirection = "_{$rel[1]}_{$rel[2]}";
     $targetContact = array();
     if ($action & CRM_Core_Action::ADD || $action & CRM_Core_Action::DELETE) {
         $contact = $contactId;
         $targetContact = CRM_Utils_Array::value('contact_check', $params);
     } else {
         if ($action & CRM_Core_Action::UPDATE) {
             $contact = $ids['contact'];
             $targetContact = array($ids['contactTarget'] => 1);
         }
     }
     // Build the 'values' array for
     // 1. ContactA
     // 2. ContactB
     // This will allow us to check if either of the contacts in
     // relationship have active memberships.
     $values = array();
     // 1. ContactA
     $values[$contact] = array('relatedContacts' => $targetContact, 'relationshipTypeId' => $relTypeId, 'relationshipTypeDirection' => $relDirection);
     // 2. ContactB
     if (!empty($targetContact)) {
         foreach ($targetContact as $cid => $donCare) {
             $values[$cid] = array('relatedContacts' => array($contact => 1), 'relationshipTypeId' => $relTypeId);
             $relTypeParams = array('id' => $relTypeId);
             $relTypeValues = array();
             require_once 'CRM/Contact/BAO/RelationshipType.php';
             CRM_Contact_BAO_RelationshipType::retrieve($relTypeParams, $relTypeValues);
             if (CRM_Utils_Array::value('name_a_b', $relTypeValues) == CRM_Utils_Array::value('name_b_a', $relTypeValues)) {
                 $values[$cid]['relationshipTypeDirection'] = '_a_b';
             } else {
                 $values[$cid]['relationshipTypeDirection'] = $relDirection == '_a_b' ? '_b_a' : '_a_b';
             }
         }
     }
     // Now get the active memberships for all the contacts.
     // If contact have any valid membership(s), then add it to
     // 'values' array.
     foreach ($values as $cid => $subValues) {
         $memParams = array('contact_id' => $cid);
         $memberships = array();
         require_once 'CRM/Member/BAO/Membership.php';
         CRM_Member_BAO_Membership::getValues($memParams, $memberships, $active);
         if (empty($memberships)) {
             continue;
         }
         $values[$cid]['memberships'] = $memberships;
     }
     // done with 'values' array.
     // Finally add / edit / delete memberships for the related contacts
     foreach ($values as $cid => $details) {
         if (!array_key_exists('memberships', $details)) {
             continue;
         }
         require_once 'CRM/Member/BAO/MembershipType.php';
         foreach ($details['memberships'] as $membershipId => $membershipValues) {
             if ($action & CRM_Core_Action::DELETE) {
                 // delete memberships of the related contacts.
                 CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId);
                 continue;
             }
             if ($action & CRM_Core_Action::UPDATE && $status & self::PAST && $membershipValues['owner_membership_id']) {
                 // If relationship is PAST and action is UPDATE
                 // then delete the RELATED membership
                 CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipValues['owner_membership_id'], $membershipValues['membership_contact_id']);
                 continue;
             }
             // add / edit the memberships for related
             // contacts.
             // Get the Membership Type Details.
             $membershipType = CRM_Member_BAO_MembershipType::getMembershipTypeDetails($membershipValues['membership_type_id']);
             if ("{$details['relationshipTypeId']}{$details['relationshipTypeDirection']}" == CRM_Utils_Array::value('relationship_type_id', $membershipType) . "_" . CRM_Utils_Array::value('relationship_direction', $membershipType)) {
                 // Check if relationship being created/updated is
                 // similar to that of membership type's
                 // relationship.
                 $membershipValues['owner_membership_id'] = $membershipId;
                 unset($membershipValues['id']);
                 unset($membershipValues['membership_contact_id']);
                 unset($membershipValues['contact_id']);
                 unset($membershipValues['membership_id']);
                 foreach ($details['relatedContacts'] as $relatedContactId => $donCare) {
                     $membershipValues['contact_id'] = $relatedContactId;
                     if ($action & CRM_Core_Action::UPDATE) {
                         //delete the membership record for related
                         //contact before creating new membership record.
                         CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $relatedContactId);
                     }
                     CRM_Member_BAO_Membership::create($membershipValues, CRM_Core_DAO::$_nullArray);
                 }
             } else {
                 if ($action & CRM_Core_Action::UPDATE) {
                     // if action is update and updated relationship do
                     // not match with the existing
                     // membership=>relationship then we need to
                     // delete the membership record created for
                     // previous relationship.
                     CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipId, $ids['contactTarget']);
                 }
             }
         }
     }
 }