/** * Trigger a rule for this trigger * * @param $op * @param $objectName * @param $objectId * @param $objectRef */ public function triggerTrigger($op, $objectName, $objectId, $objectRef) { $t = $this->getTriggerDataFromPost($op, $objectName, $objectId, $objectRef); //trigger for each client $clients = CRM_Case_BAO_Case::getCaseClients($objectId); foreach ($clients as $client) { $triggerData = clone $t; $triggerData->setEntityData('Relationship', null); $triggerData->setContactId($client); CRM_Civirules_Engine::triggerRule($this, $triggerData); } //trigger for each case role $relatedContacts = CRM_Case_BAO_Case::getRelatedContacts($objectId); foreach ($relatedContacts as $contact) { $triggerData = clone $t; $relationshipData = null; $relationship = new CRM_Contact_BAO_Relationship(); $relationship->contact_id_b = $contact['contact_id']; $relationship->case_id = $objectId; if ($relationship->find(true)) { CRM_Core_DAO::storeValues($relationship, $relationshipData); } $triggerData->setEntityData('Relationship', $relationshipData); $triggerData->setContactId($contact['contact_id']); CRM_Civirules_Engine::triggerRule($this, $triggerData); } }
/** * @throws \CiviCRM_API3_Exception */ public static function relationship() { $relType = CRM_Utils_Request::retrieve('rel_type', 'String', CRM_Core_DAO::$_nullObject, TRUE); $relContactID = CRM_Utils_Request::retrieve('rel_contact', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); $originalCid = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject); $relationshipID = CRM_Utils_Request::retrieve('rel_id', 'Positive', CRM_Core_DAO::$_nullObject); $caseID = CRM_Utils_Request::retrieve('case_id', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); if (!CRM_Case_BAO_Case::accessCase($caseID)) { CRM_Utils_System::permissionDenied(); } $ret = array('is_error' => 0); list($relTypeId, $b, $a) = explode('_', $relType); if ($relationshipID && $originalCid) { CRM_Case_BAO_Case::endCaseRole($caseID, $a, $originalCid, $relTypeId); } $clientList = CRM_Case_BAO_Case::getCaseClients($caseID); // Loop through multiple case clients foreach ($clientList as $i => $sourceContactID) { $result = civicrm_api3('relationship', 'create', array('case_id' => $caseID, 'relationship_type_id' => $relTypeId, "contact_id_{$a}" => $relContactID, "contact_id_{$b}" => $sourceContactID, 'start_date' => 'now')); // Save activity only for the primary (first) client if ($i == 0 && empty($result['is_error'])) { CRM_Case_BAO_Case::createCaseRoleActivity($caseID, $result['id'], $relContactID); } } CRM_Utils_JSON::output($ret); }
public static function relationship() { $relType = CRM_Utils_Request::retrieve('rel_type', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); $relContactID = CRM_Utils_Request::retrieve('rel_contact', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); $relationshipID = CRM_Utils_Request::retrieve('rel_id', 'Positive', CRM_Core_DAO::$_nullObject); // this used only to determine add or update mode $caseID = CRM_Utils_Request::retrieve('case_id', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); // check if there are multiple clients for this case, if so then we need create // relationship and also activities for each contacts // get case client list $clientList = CRM_Case_BAO_Case::getCaseClients($caseID); $ret = array('is_error' => 0); foreach ($clientList as $sourceContactID) { $relationParams = array('relationship_type_id' => $relType . '_a_b', 'contact_check' => array($relContactID => 1), 'is_active' => 1, 'case_id' => $caseID, 'start_date' => date("Ymd")); $relationIds = array('contact' => $sourceContactID); // check if we are editing/updating existing relationship if ($relationshipID && $relationshipID != 'null') { // here we need to retrieve appropriate relationshipID based on client id and relationship type id $caseRelationships = new CRM_Contact_DAO_Relationship(); $caseRelationships->case_id = $caseID; $caseRelationships->relationship_type_id = $relType; $caseRelationships->contact_id_a = $sourceContactID; $caseRelationships->find(); while ($caseRelationships->fetch()) { $relationIds['relationship'] = $caseRelationships->id; $relationIds['contactTarget'] = $relContactID; } $caseRelationships->free(); } // create new or update existing relationship $return = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds); if (!empty($return[4][0])) { $relationshipID = $return[4][0]; //create an activity for case role assignment.CRM-4480 CRM_Case_BAO_Case::createCaseRoleActivity($caseID, $relationshipID, $relContactID); } else { $ret = array('is_error' => 1, 'error_message' => ts('The relationship type definition for the case role is not valid for the client and / or staff contact types. You can review and edit relationship types at <a href="%1">Administer >> Option Lists >> Relationship Types</a>.', array(1 => CRM_Utils_System::url('civicrm/admin/reltype', 'reset=1')))); } } CRM_Utils_JSON::output($ret); }
/** * Delete a contact and all its associated records. * * @param int $id * Id of the contact to delete. * @param bool $restore * Whether to actually restore, not delete. * @param bool $skipUndelete * Whether to force contact delete or not. * * @return bool * Was contact deleted? */ public static function deleteContact($id, $restore = FALSE, $skipUndelete = FALSE) { if (!$id) { return FALSE; } // If trash is disabled in system settings then we always skip if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete', NULL, 1)) { $skipUndelete = TRUE; } // make sure we have edit permission for this contact // before we delete if ($skipUndelete && !CRM_Core_Permission::check('delete contacts') || $restore && !CRM_Core_Permission::check('access deleted contacts')) { return FALSE; } // CRM-12929 // Restrict contact to be delete if contact has financial trxns $error = NULL; if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($id), $error)) { return FALSE; } // make sure this contact_id does not have any membership types $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $id, 'id', 'member_of_contact_id'); if ($membershipTypeID) { return FALSE; } $contact = new CRM_Contact_DAO_Contact(); $contact->id = $id; if (!$contact->find(TRUE)) { return FALSE; } $contactType = $contact->contact_type; // currently we only clear employer cache. // we are now deleting inherited membership if any. if ($contact->contact_type == 'Organization') { $action = $restore ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE; $relationshipDtls = CRM_Contact_BAO_Relationship::getRelationship($id); if (!empty($relationshipDtls)) { foreach ($relationshipDtls as $rId => $details) { CRM_Contact_BAO_Relationship::disableEnableRelationship($rId, $action); } } CRM_Contact_BAO_Contact_Utils::clearAllEmployee($id); } if ($restore) { return self::contactTrashRestore($contact, TRUE); } // start a new transaction $transaction = new CRM_Core_Transaction(); if ($skipUndelete) { CRM_Utils_Hook::pre('delete', $contactType, $id, CRM_Core_DAO::$_nullArray); //delete billing address if exists. CRM_Contribute_BAO_Contribution::deleteAddress(NULL, $id); // delete the log entries since we dont have triggers enabled as yet $logDAO = new CRM_Core_DAO_Log(); $logDAO->entity_table = 'civicrm_contact'; $logDAO->entity_id = $id; $logDAO->delete(); // delete contact participants CRM-12155 CRM_Event_BAO_Participant::deleteContactParticipant($id); // delete contact contributions CRM-12155 CRM_Contribute_BAO_Contribution::deleteContactContribution($id); // do activity cleanup, CRM-5604 CRM_Activity_BAO_Activity::cleanupActivity($id); // delete all notes related to contact CRM_Core_BAO_Note::cleanContactNotes($id); // delete cases related to contact $contactCases = CRM_Case_BAO_Case::retrieveCaseIdsByContactId($id); if (!empty($contactCases)) { foreach ($contactCases as $caseId) { //check if case is associate with other contact or not. $caseContactId = CRM_Case_BAO_Case::getCaseClients($caseId); if (count($caseContactId) <= 1) { CRM_Case_BAO_Case::deleteCase($caseId); } } } $contact->delete(); } else { self::contactTrashRestore($contact); } //delete the contact id from recently view CRM_Utils_Recent::delContact($id); // Update the group contact cache if ($restore) { CRM_Contact_BAO_GroupContactCache::remove(); } else { CRM_Contact_BAO_GroupContactCache::removeContact($id); } // delete any dupe cache entry CRM_Core_BAO_PrevNextCache::deleteItem($id); $transaction->commit(); if ($skipUndelete) { CRM_Utils_Hook::post('delete', $contactType, $contact->id, $contact); } // also reset the DB_DO global array so we can reuse the memory // http://issues.civicrm.org/jira/browse/CRM-4387 CRM_Core_DAO::freeResult(); return TRUE; }
static function relationship() { $relType = CRM_Utils_Array::value('rel_type', $_REQUEST); $relContactID = CRM_Utils_Array::value('rel_contact', $_REQUEST); $sourceContactID = CRM_Utils_Array::value('contact_id', $_REQUEST); // we no longer need this. $relationshipID = CRM_Utils_Array::value('rel_id', $_REQUEST); // this used only to determine add or update mode $caseID = CRM_Utils_Array::value('case_id', $_REQUEST); // check if there are multiple clients for this case, if so then we need create // relationship and also activities for each contacts // get case client list $clientList = CRM_Case_BAO_Case::getCaseClients($caseID); foreach ($clientList as $sourceContactID) { $relationParams = array('relationship_type_id' => $relType . '_a_b', 'contact_check' => array($relContactID => 1), 'is_active' => 1, 'case_id' => $caseID, 'start_date' => date("Ymd")); $relationIds = array('contact' => $sourceContactID); // check if we are editing/updating existing relationship if ($relationshipID && $relationshipID != 'null') { // here we need to retrieve appropriate relationshipID based on client id and relationship type id $caseRelationships = new CRM_Contact_DAO_Relationship(); $caseRelationships->case_id = $caseID; $caseRelationships->relationship_type_id = $relType; $caseRelationships->contact_id_a = $sourceContactID; $caseRelationships->find(); while ($caseRelationships->fetch()) { $relationIds['relationship'] = $caseRelationships->id; $relationIds['contactTarget'] = $relContactID; } $caseRelationships->free(); } // create new or update existing relationship $return = CRM_Contact_BAO_Relationship::create($relationParams, $relationIds); $status = 'process-relationship-fail'; if (CRM_Utils_Array::value(0, $return[4])) { $relationshipID = $return[4][0]; $status = 'process-relationship-success'; //create an activity for case role assignment.CRM-4480 CRM_Case_BAO_Case::createCaseRoleActivity($caseID, $relationshipID, $relContactID); } } $relation['status'] = $status; echo json_encode($relation); CRM_Utils_System::civiExit(); }