Esempio n. 1
0
 /**
  *  This function is called when the form is submitted 
  *
  * @access public
  * @return None
  */
 public function postProcess()
 {
     // store the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     $quickSave = false;
     if (CRM_Utils_Array::value('_qf_Relationship_refresh_save', $_POST) || CRM_Utils_Array::value('_qf_Relationship_refresh_savedetails', $_POST)) {
         $quickSave = true;
     }
     $this->set('searchDone', 0);
     $this->set('callAjax', false);
     if (CRM_Utils_Array::value('_qf_Relationship_refresh', $_POST) || $quickSave) {
         if (is_numeric($params['rel_contact_id'])) {
             if ($quickSave) {
                 $params['contact_check'] = array($params['rel_contact_id'] => 1);
             } else {
                 $this->search($params);
                 $quickSave = false;
             }
         } else {
             $this->set('callAjax', true);
             $this->set('relType', $params['relationship_type_id']);
             $this->set('relContact', $params['rel_contact']);
             $quickSave = false;
         }
         $this->set('searchDone', 1);
         if (!$quickSave) {
             return;
         }
     }
     // action is taken depending upon the mode
     $ids = array();
     $ids['contact'] = $this->_contactId;
     // modify params for ajax call
     $this->modifyParams($params);
     if ($this->_action & CRM_Core_Action::DELETE) {
         CRM_Contact_BAO_Relationship::del($this->_relationshipId);
         return;
     }
     $relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $params['relationship_type_id']);
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $ids['relationship'] = $this->_relationshipId;
         $relation = CRM_Contact_BAO_Relationship::getContactIds($this->_relationshipId);
         $ids['contactTarget'] = $relation->contact_id_a == $this->_contactId ? $relation->contact_id_b : $relation->contact_id_a;
         //if relationship type change and previously it was
         //employer / emplyee relationship with current employer
         //than clear the current employer. CRM-3235.
         //make sure we has to have employer id before firing queries, CRM-7306
         $employerId = CRM_Utils_Array::value('current_employee_id', $this->_values);
         $isDisabled = true;
         if (CRM_Utils_Array::value('is_active', $params)) {
             $isDisabled = false;
         }
         $relChanged = true;
         if ($relationshipTypeId == $this->_values['relationship_type_id']) {
             $relChanged = false;
         }
         if ($employerId && ($isDisabled || $relChanged)) {
             require_once 'CRM/Contact/BAO/Contact/Utils.php';
             CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['current_employee_id']);
         }
     } elseif ($quickSave) {
         if ($params['add_current_employee'] && $this->_allRelationshipNames[$relationshipTypeId]["name_a_b"] == 'Employee of') {
             $params['employee_of'] = $params['rel_contact_id'];
         } elseif ($params['add_current_employer'] && $this->_allRelationshipNames[$relationshipTypeId]["name_b_a"] == 'Employer of') {
             $params['employer_of'] = array($params['rel_contact_id'] => 1);
         }
         if (!$this->_rtype) {
             $this->_rtype = str_replace($relationshipTypeId . '_', '', $params['relationship_type_id']);
         }
     }
     $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], null, true);
     $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], null, true);
     //special case to handle if all checkboxes are unchecked
     $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', false, false, $relationshipTypeId);
     $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $this->_relationshipId, 'Relationship');
     list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
     // if this is called from case view,
     //create an activity for case role removal.CRM-4480
     if ($this->_caseId) {
         require_once "CRM/Case/BAO/Case.php";
         CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId);
     }
     $status = '';
     if ($valid) {
         $status .= ' ' . ts('%count new relationship record created.', array('count' => $valid, 'plural' => '%count new relationship records created.'));
     }
     if ($invalid) {
         $status .= ' ' . ts('%count relationship record not created due to invalid target contact type.', array('count' => $invalid, 'plural' => '%count relationship records not created due to invalid target contact type.'));
     }
     if ($duplicate) {
         $status .= ' ' . ts('%count relationship record not created - duplicate of existing relationship.', array('count' => $duplicate, 'plural' => '%count relationship records not created - duplicate of existing relationship.'));
     }
     if ($saved) {
         $status .= ts('Relationship record has been updated.');
     }
     $note = new CRM_Core_DAO_Note();
     $note->entity_id = $relationshipIds[0];
     $note->entity_table = 'civicrm_relationship';
     $noteIds = array();
     if ($note->find(true)) {
         $id = $note->id;
         $noteIds["id"] = $id;
     }
     $noteParams = array('entity_id' => $relationshipIds[0], 'entity_table' => 'civicrm_relationship', 'note' => $params['note'], 'contact_id' => $this->_contactId);
     CRM_Core_BAO_Note::add($noteParams, $noteIds);
     // Membership for related contacts CRM-1657
     if (CRM_Core_Permission::access('CiviMember') && !$duplicate) {
         CRM_Contact_BAO_Relationship::relatedMemberships($this->_contactId, $params, $ids, $this->_action);
     }
     //handle current employee/employer relationship, CRM-3532
     if ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') {
         $orgId = null;
         if (CRM_Utils_Array::value('employee_of', $params)) {
             $orgId = $params['employee_of'];
         } else {
             if ($this->_action & CRM_Core_Action::UPDATE) {
                 if (CRM_Utils_Array::value('is_current_employer', $params) && CRM_Utils_Array::value('is_active', $params)) {
                     if (CRM_Utils_Array::value('contactTarget', $ids) != CRM_Utils_Array::value('current_employer_id', $this->_values)) {
                         $orgId = CRM_Utils_Array::value('contactTarget', $ids);
                     }
                 } else {
                     if (CRM_Utils_Array::value('contactTarget', $ids) == CRM_Utils_Array::value('current_employer_id', $this->_values)) {
                         //clear current employer.
                         require_once 'CRM/Contact/BAO/Contact/Utils.php';
                         CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_contactId);
                     }
                 }
             }
         }
         //set current employer
         if ($orgId) {
             $currentEmpParams[$this->_contactId] = $orgId;
             require_once 'CRM/Contact/BAO/Contact/Utils.php';
             CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams);
         }
     } else {
         if ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of') {
             $individualIds = array();
             if (CRM_Utils_Array::value('employer_of', $params)) {
                 $individualIds = array_keys($params['employer_of']);
             } else {
                 if ($this->_action & CRM_Core_Action::UPDATE) {
                     if (CRM_Utils_Array::value('is_current_employer', $params)) {
                         if (CRM_Utils_Array::value('contactTarget', $ids) != CRM_Utils_Array::value('current_employee_id', $this->_values)) {
                             $individualIds[] = CRM_Utils_Array::value('contactTarget', $ids);
                         }
                     } else {
                         if (CRM_Utils_Array::value('contactTarget', $ids) == CRM_Utils_Array::value('current_employee_id', $this->_values)) {
                             // clear current employee
                             require_once 'CRM/Contact/BAO/Contact/Utils.php';
                             CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($ids['contactTarget']);
                         }
                     }
                 }
             }
             //set current employee
             if (!empty($individualIds)) {
                 //build the employee params.
                 foreach ($individualIds as $key => $Id) {
                     $currentEmpParams[$Id] = $this->_contactId;
                 }
                 require_once 'CRM/Contact/BAO/Contact/Utils.php';
                 CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams);
             }
         }
     }
     CRM_Core_Session::setStatus($status);
     if ($quickSave) {
         $session =& CRM_Core_Session::singleton();
         CRM_Utils_System::redirect($session->popUserContext());
     }
 }
Esempio n. 2
0
 /**
  * Delete current employer relationship.
  *
  * @param int $id
  * @param int $action
  *
  * @return CRM_Contact_DAO_Relationship
  */
 public static function clearCurrentEmployer($id, $action)
 {
     $relationship = new CRM_Contact_DAO_Relationship();
     $relationship->id = $id;
     $relationship->find(TRUE);
     //to delete relationship between household and individual                                                                                          \
     //or between individual and organization
     if ($action & CRM_Core_Action::DISABLE || $action & CRM_Core_Action::DELETE) {
         $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
         if (isset($relTypes['Employee of']) && $relationship->relationship_type_id == $relTypes['Employee of']['id'] || isset($relTypes['Household Member of']) && $relationship->relationship_type_id == $relTypes['Household Member of']['id']) {
             $sharedContact = new CRM_Contact_DAO_Contact();
             $sharedContact->id = $relationship->contact_id_a;
             $sharedContact->find(TRUE);
             // CRM-15881 UPDATES
             // changed FROM "...relationship->relationship_type_id == 4..." TO "...relationship->relationship_type_id == 5..."
             // As the system should be looking for type "employer of" (id 5) and not "sibling of" (id 4)
             // As suggested by @davecivicrm, the employee relationship type id is fetched using the CRM_Core_DAO::getFieldValue() class and method, since these ids differ from system to system.
             $employerRelTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
             if ($relationship->relationship_type_id == $employerRelTypeId && $relationship->contact_id_b == $sharedContact->employer_id) {
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a);
             }
         }
     }
     return $relationship;
 }
Esempio n. 3
0
 static function clearCurrentEmployer($id, $action)
 {
     $relationship = new CRM_Contact_DAO_Relationship();
     $relationship->id = $id;
     $relationship->find(TRUE);
     //to delete relationship between household and individual                                                                                          \
     //or between individual and orgnization
     if ($action & CRM_Core_Action::DISABLE || $action & CRM_Core_Action::DELETE) {
         $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name'));
         if ($relationship->relationship_type_id == $relTypes['Employee of']['id'] || $relationship->relationship_type_id == $relTypes['Household Member of']['id']) {
             $sharedContact = new CRM_Contact_DAO_Contact();
             $sharedContact->id = $relationship->contact_id_a;
             $sharedContact->find(TRUE);
             if ($relationship->relationship_type_id == 4 && $relationship->contact_id_b == $sharedContact->employer_id) {
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a);
             }
         }
     }
     return $relationship;
 }
Esempio n. 4
0
 /**
  * Takes an associative array and creates a contact object.
  *
  * The function extracts all the params it needs to initialize the create a
  * contact object. the params array could contain additional unused name/value
  * pairs
  *
  * @param array $params
  *   (reference) an assoc array of name/value pairs.
  *
  * @return CRM_Contact_BAO_Contact|CRM_Core_Error|NULL
  *   Created or updated contact object or error object.
  *   (error objects are being phased out in favour of exceptions)
  */
 public static function add(&$params)
 {
     $contact = new CRM_Contact_DAO_Contact();
     if (empty($params)) {
         return NULL;
     }
     // Fix for validate contact sub type CRM-5143.
     if (isset($params['contact_sub_type'])) {
         if (empty($params['contact_sub_type'])) {
             $params['contact_sub_type'] = 'null';
         } else {
             if (!CRM_Contact_BAO_ContactType::isExtendsContactType($params['contact_sub_type'], $params['contact_type'], TRUE)) {
                 // we'll need to fix tests to handle this
                 // CRM-7925
                 CRM_Core_Error::fatal(ts('The Contact Sub Type does not match the Contact type for this record'));
             }
             if (is_array($params['contact_sub_type'])) {
                 $params['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, $params['contact_sub_type']) . CRM_Core_DAO::VALUE_SEPARATOR;
             } else {
                 $params['contact_sub_type'] = CRM_Core_DAO::VALUE_SEPARATOR . trim($params['contact_sub_type'], CRM_Core_DAO::VALUE_SEPARATOR) . CRM_Core_DAO::VALUE_SEPARATOR;
             }
         }
     } else {
         // Reset the value.
         // CRM-101XX.
         $params['contact_sub_type'] = 'null';
     }
     // Fixed contact source.
     if (isset($params['contact_source'])) {
         $params['source'] = $params['contact_source'];
     }
     // Fix for preferred communication method.
     $prefComm = CRM_Utils_Array::value('preferred_communication_method', $params);
     if ($prefComm && is_array($prefComm)) {
         unset($params['preferred_communication_method']);
         $newPref = array();
         foreach ($prefComm as $k => $v) {
             if ($v) {
                 $newPref[$k] = $v;
             }
         }
         $prefComm = $newPref;
         if (is_array($prefComm) && !empty($prefComm)) {
             $prefComm = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($prefComm)) . CRM_Core_DAO::VALUE_SEPARATOR;
             $contact->preferred_communication_method = $prefComm;
         } else {
             $contact->preferred_communication_method = '';
         }
     }
     $allNull = $contact->copyValues($params);
     $contact->id = CRM_Utils_Array::value('contact_id', $params);
     if ($contact->contact_type == 'Individual') {
         $allNull = FALSE;
         // Format individual fields.
         CRM_Contact_BAO_Individual::format($params, $contact);
     } elseif ($contact->contact_type == 'Household') {
         if (isset($params['household_name'])) {
             $allNull = FALSE;
             $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('household_name', $params, '');
         }
     } elseif ($contact->contact_type == 'Organization') {
         if (isset($params['organization_name'])) {
             $allNull = FALSE;
             $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('organization_name', $params, '');
         }
     }
     $privacy = CRM_Utils_Array::value('privacy', $params);
     if ($privacy && is_array($privacy) && !empty($privacy)) {
         $allNull = FALSE;
         foreach (self::$_commPrefs as $name) {
             $contact->{$name} = CRM_Utils_Array::value($name, $privacy, FALSE);
         }
     }
     // Since hash was required, make sure we have a 0 value for it (CRM-1063).
     // @todo - does this mean we can remove this block?
     // Fixed in 1.5 by making hash optional, only do this in create mode, not update.
     if ((!array_key_exists('hash', $contact) || !$contact->hash) && !$contact->id) {
         $allNull = FALSE;
         $contact->hash = md5(uniqid(rand(), TRUE));
     }
     // Even if we don't need $employerId, it's important to call getFieldValue() before
     // the contact is saved because we want the existing value to be cached.
     // createCurrentEmployerRelationship() needs the old value not the updated one. CRM-10788
     $employerId = empty($contact->id) ? NULL : CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id');
     if (!$allNull) {
         $contact->save();
         CRM_Core_BAO_Log::register($contact->id, 'civicrm_contact', $contact->id);
     }
     if ($contact->contact_type == 'Individual' && (isset($params['current_employer']) || isset($params['employer_id']))) {
         // Create current employer.
         $newEmployer = !empty($params['employer_id']) ? $params['employer_id'] : CRM_Utils_Array::value('current_employer', $params);
         $newContact = FALSE;
         if (empty($params['contact_id'])) {
             $newContact = TRUE;
         }
         if ($newEmployer) {
             CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $newEmployer, $employerId, $newContact);
         } else {
             if ($employerId) {
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($contact->id, $employerId);
             }
         }
     }
     // Update cached employer name.
     if ($contact->contact_type == 'Organization') {
         CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id);
     }
     return $contact;
 }
 /**
  * This function is called when the form is submitted.
  */
 public function postProcess()
 {
     // Store the submitted values in an array.
     $params = $this->controller->exportValues($this->_name);
     // CRM-14612 - Don't use adv-checkbox as it interferes with the form js
     $params['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $params, 0);
     $params['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $params, 0);
     // action is taken depending upon the mode
     if ($this->_action & CRM_Core_Action::DELETE) {
         CRM_Contact_BAO_Relationship::del($this->_relationshipId);
         // CRM-15881 UPDATES
         // Since the line above nullifies the organization_name and employer_id fiels in the contact record, we need to reload all blocks to reflect this chage on the user interface.
         $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
         return;
     }
     $relationshipTypeParts = explode('_', $params['relationship_type_id']);
     $params['relationship_type_id'] = $relationshipTypeParts[0];
     if (!$this->_rtype) {
         // Do we need to wrap this in an if - when is rtype used & is relationship_type_id always set then?
         $this->_rtype = $params['relationship_type_id'];
     }
     $params['contact_id_' . $relationshipTypeParts[1]] = $this->_contactId;
     // Update mode (always single)
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $update = TRUE;
         $params['id'] = $this->_relationshipId;
         $ids['relationship'] = $this->_relationshipId;
         $relation = CRM_Contact_BAO_Relationship::getRelationshipByID($this->_relationshipId);
         if ($relation->contact_id_a == $this->_contactId) {
             // I couldn't replicate this path in testing. See below.
             $params['contact_id_a'] = $this->_contactId;
             $params['contact_id_b'] = array($params['related_contact_id']);
             $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $relationshipTypeParts[1]);
             $relationshipIds = $outcome['relationship_ids'];
         } else {
             // The only reason we have changed this to use the api & not the above is that this was broken.
             // Recommend extracting all of update into a function that uses the api
             // and ensuring api / bao take care of 'other stuff' in this form
             // the contact_id_a & b can't be changed on this form so don't really need setting.
             $params['contact_id_b'] = $this->_contactId;
             $params['contact_id_a'] = $params['related_contact_id'];
             $result = civicrm_api3('relationship', 'create', $params);
             $relationshipIds = array($result['id']);
         }
         $ids['contactTarget'] = $relation->contact_id_a == $this->_contactId ? $relation->contact_id_b : $relation->contact_id_a;
         // @todo this belongs in the BAO.
         if ($this->_isCurrentEmployer) {
             // if relationship type changes, relationship is disabled, or "current employer" is unchecked,
             // clear the current employer. CRM-3235.
             $relChanged = $params['relationship_type_id'] != $this->_values['relationship_type_id'];
             if (!$params['is_active'] || !$params['is_current_employer'] || $relChanged) {
                 // CRM-15881 UPDATES
                 // If not is_active then is_current_employer needs to be set false as well! Logically a contact cannot be a current employee of a disabled employer relationship.
                 // If this is not done, then the below process will go ahead and disable the organization_name and employer_id fields in the contact record (which is what is wanted) but then further down will be re-enabled becuase is_current_employer is not false, therefore undoing what was done correctly.
                 if (!$params['is_active']) {
                     $params['is_current_employer'] = FALSE;
                 }
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']);
                 // Refresh contact summary if in ajax mode
                 $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
             }
         }
         if (empty($outcome['saved']) && !empty($update)) {
             $outcome['saved'] = $update;
         }
         $this->setMessage($outcome);
     } else {
         $params['contact_id_' . $relationshipTypeParts[2]] = explode(',', $params['related_contact_id']);
         $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $relationshipTypeParts[1]);
         $relationshipIds = $outcome['relationship_ids'];
         if (empty($outcome['saved']) && !empty($update)) {
             $outcome['saved'] = $update;
         }
         $this->setMessage($outcome);
     }
     // if this is called from case view,
     //create an activity for case role removal.CRM-4480
     // @todo this belongs in the BAO.
     if ($this->_caseId) {
         CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId);
     }
     // Save notes
     // @todo this belongs in the BAO.
     if ($this->_action & CRM_Core_Action::UPDATE || $params['note']) {
         foreach ($relationshipIds as $id) {
             $noteParams = array('entity_id' => $id, 'entity_table' => 'civicrm_relationship');
             $existing = civicrm_api3('note', 'get', $noteParams);
             if (!empty($existing['id'])) {
                 $noteParams['id'] = $existing['id'];
             }
             $noteParams['note'] = $params['note'];
             $noteParams['contact_id'] = $this->_contactId;
             if (!empty($existing['id']) || $params['note']) {
                 $action = $params['note'] ? 'create' : 'delete';
                 civicrm_api3('note', $action, $noteParams);
             }
         }
     }
     $params['relationship_ids'] = $relationshipIds;
     // Refresh contact tabs which might have been affected
     $this->ajaxResponse['updateTabs'] = array('#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId), '#tab_contribute' => CRM_Contact_BAO_Contact::getCountComponent('contribution', $this->_contactId));
     // Set current employee/employer relationship, CRM-3532
     if ($params['is_current_employer'] && $this->_allRelationshipNames[$params['relationship_type_id']]["name_a_b"] == 'Employee of') {
         $employerParams = array();
         foreach ($relationshipIds as $id) {
             // Fixme this is dumb why do we have to look this up again?
             $rel = CRM_Contact_BAO_Relationship::getRelationshipByID($id);
             $employerParams[$rel->contact_id_a] = $rel->contact_id_b;
         }
         // @todo this belongs in the BAO.
         CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams);
         // Refresh contact summary if in ajax mode
         $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
     }
 }
 static function clearCurrentEmployer($id, $action)
 {
     $relationship = new CRM_Contact_DAO_Relationship();
     $relationship->id = $id;
     $relationship->find(TRUE);
     //to delete relationship between household and individual                                                                                          \
     //or between individual and orgnization
     if ($action & CRM_Core_Action::DISABLE || $action & CRM_Core_Action::DELETE) {
         if ($relationship->relationship_type_id == 4 || $relationship->relationship_type_id == 7) {
             $sharedContact = new CRM_Contact_DAO_Contact();
             $sharedContact->id = $relationship->contact_id_a;
             $sharedContact->find(TRUE);
             if ($relationship->relationship_type_id == 4 && $relationship->contact_id_b == $sharedContact->employer_id) {
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a);
             }
         }
     }
     return $relationship;
 }
 /**
  *  This function is called when the form is submitted
  *
  * @access public
  *
  * @return None
  */
 public function postProcess()
 {
     $quickSave = FALSE;
     if (CRM_Utils_Array::value('_qf_Relationship_refresh_save', $_POST) || CRM_Utils_Array::value('_qf_Relationship_refresh_savedetails', $_POST)) {
         //CRM-15873
         //Parse custom file uploads
         $pageName = $this->getAttribute('name');
         $data =& $this->controller->container();
         foreach ($this->controller->get('uploadNames') as $name) {
             $this->controller->_actions['upload']->upload($this, $data, $pageName, $name);
         }
         $quickSave = TRUE;
     }
     // store the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     $this->set('searchDone', 0);
     $this->set('callAjax', FALSE);
     if (CRM_Utils_Array::value('_qf_Relationship_refresh', $_POST) || $quickSave) {
         if (is_numeric($params['contact_select_id'][1])) {
             if ($quickSave) {
                 $params['contact_check'] = array($params['contact_select_id'][1] => 1);
             }
         } else {
             $this->set('callAjax', TRUE);
             $this->set('relType', $params['relationship_type_id']);
             $this->set('relContact', $params['contact'][1]);
             $quickSave = FALSE;
         }
         $this->set('searchDone', 1);
         if (!$quickSave) {
             return;
         }
     }
     // action is taken depending upon the mode
     $ids = array();
     $ids['contact'] = $this->_contactId;
     // modify params for ajax call
     $this->modifyParams($params);
     if ($this->_action & CRM_Core_Action::DELETE) {
         CRM_Contact_BAO_Relationship::del($this->_relationshipId);
         return;
     }
     $relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $params['relationship_type_id']);
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $ids['relationship'] = $this->_relationshipId;
         $relation = CRM_Contact_BAO_Relationship::getContactIds($this->_relationshipId);
         $ids['contactTarget'] = $relation->contact_id_a == $this->_contactId ? $relation->contact_id_b : $relation->contact_id_a;
         //if relationship type change and previously it was
         //employer / emplyee relationship with current employer
         //than clear the current employer. CRM-3235.
         //make sure we has to have employer id before firing queries, CRM-7306
         $employerId = CRM_Utils_Array::value('current_employee_id', $this->_values);
         $isDisabled = TRUE;
         if (CRM_Utils_Array::value('is_active', $params)) {
             $isDisabled = FALSE;
         }
         $relChanged = TRUE;
         if ($relationshipTypeId == $this->_values['relationship_type_id']) {
             $relChanged = FALSE;
         }
         if ($employerId && ($isDisabled || $relChanged)) {
             CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['current_employee_id']);
         }
         //if field key doesn't exists in params that means the user has unchecked checkbox,
         //hence fill FALSE to params
         $params['is_active'] = $isDisabled ? FALSE : TRUE;
         $params['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $params, FALSE);
         $params['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $params, FALSE);
     } elseif ($quickSave) {
         if (CRM_Utils_Array::value('add_current_employee', $params) && $this->_allRelationshipNames[$relationshipTypeId]['name_a_b'] == 'Employee of') {
             $params['employee_of'] = $params['contact_select_id'][1];
         } elseif (CRM_Utils_Array::value('add_current_employer', $params) && $this->_allRelationshipNames[$relationshipTypeId]['name_b_a'] == 'Employer of') {
             $params['employer_of'] = array($params['contact_select_id'][1] => 1);
         }
         if (!$this->_rtype) {
             $this->_rtype = str_replace($relationshipTypeId . '_', '', $params['relationship_type_id']);
         }
     }
     if (!$params['note']) {
         $params['note'] = 'null';
     }
     $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], NULL, TRUE);
     $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], NULL, TRUE);
     //special case to handle if all checkboxes are unchecked
     $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', FALSE, FALSE, $relationshipTypeId);
     $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $this->_relationshipId, 'Relationship');
     list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
     // if this is called from case view,
     //create an activity for case role removal.CRM-4480
     if ($this->_caseId) {
         CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId);
     }
     $status = '';
     if ($valid) {
         CRM_Core_Session::setStatus(ts('New relationship created.', array('count' => $valid, 'plural' => '%count new relationships created.')), ts('Saved'), 'success');
     }
     if ($invalid) {
         CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid target contact type.', array('count' => $invalid, 'plural' => '%count relationship records were not created due to invalid target contact types.')), ts('%count invalid relationship record', array('count' => $invalid, 'plural' => '%count invalid relationship records')));
     }
     if ($duplicate) {
         CRM_Core_Session::setStatus(ts('One relationship was not created because it already exists.', array('count' => $duplicate, 'plural' => '%count relationships were not created because they already exist.')), ts('%count duplicate relationship', array('count' => $duplicate, 'plural' => '%count duplicate relationships')));
     }
     if ($saved) {
         CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
     }
     if (!empty($relationshipIds)) {
         $note = new CRM_Core_DAO_Note();
         $note->entity_id = $relationshipIds[0];
         $note->entity_table = 'civicrm_relationship';
         $noteIds = array();
         if ($note->find(TRUE)) {
             $id = $note->id;
             $noteIds['id'] = $id;
         }
         $noteParams = array('entity_id' => $relationshipIds[0], 'entity_table' => 'civicrm_relationship', 'note' => $params['note'], 'contact_id' => $this->_contactId);
         CRM_Core_BAO_Note::add($noteParams, $noteIds);
         $params['relationship_ids'] = $relationshipIds;
     }
     // Membership for related contacts CRM-1657
     if (CRM_Core_Permission::access('CiviMember') && !$duplicate) {
         if ($this->_action & CRM_Core_Action::ADD) {
             CRM_Contact_BAO_Relationship::relatedMemberships($this->_contactId, $params, $ids, $this->_action);
         } elseif ($this->_action & CRM_Core_Action::UPDATE) {
             //fixes for CRM-7985
             //only if the relationship has been toggled to enable /disable
             if (CRM_Utils_Array::value('is_active', $params) != $this->_enabled) {
                 $active = CRM_Utils_Array::value('is_active', $params) ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE;
                 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_relationshipId, $active);
             }
         }
     }
     //handle current employee/employer relationship, CRM-3532
     if ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employee of') {
         $orgId = NULL;
         if (CRM_Utils_Array::value('employee_of', $params)) {
             $orgId = $params['employee_of'];
         } elseif ($this->_action & CRM_Core_Action::UPDATE) {
             if (CRM_Utils_Array::value('is_current_employer', $params) && CRM_Utils_Array::value('is_active', $params)) {
                 if (CRM_Utils_Array::value('contactTarget', $ids) != CRM_Utils_Array::value('current_employer_id', $this->_values)) {
                     $orgId = CRM_Utils_Array::value('contactTarget', $ids);
                 }
             } elseif (CRM_Utils_Array::value('contactTarget', $ids) == CRM_Utils_Array::value('current_employer_id', $this->_values)) {
                 //clear current employer.
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_contactId);
             }
         }
         //set current employer
         if ($orgId) {
             $currentEmpParams[$this->_contactId] = $orgId;
             CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams);
         }
     } elseif ($this->_allRelationshipNames[$relationshipTypeId]["name_{$this->_rtype}"] == 'Employer of') {
         $individualIds = array();
         if (CRM_Utils_Array::value('employer_of', $params)) {
             $individualIds = array_keys($params['employer_of']);
         } elseif ($this->_action & CRM_Core_Action::UPDATE) {
             if (CRM_Utils_Array::value('is_current_employer', $params)) {
                 if (CRM_Utils_Array::value('contactTarget', $ids) != CRM_Utils_Array::value('current_employee_id', $this->_values)) {
                     $individualIds[] = CRM_Utils_Array::value('contactTarget', $ids);
                 }
             } elseif (CRM_Utils_Array::value('contactTarget', $ids) == CRM_Utils_Array::value('current_employee_id', $this->_values)) {
                 // clear current employee
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($ids['contactTarget']);
             }
         }
         //set current employee
         if (!empty($individualIds)) {
             //build the employee params.
             foreach ($individualIds as $key => $Id) {
                 $currentEmpParams[$Id] = $this->_contactId;
             }
             CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams);
         }
     }
     if ($quickSave) {
         $session = CRM_Core_Session::singleton();
         CRM_Utils_System::redirect($session->popUserContext());
     }
 }
Esempio n. 8
0
 /**
  * takes an associative array and creates a contact object
  *
  * the function extract all the params it needs to initialize the create a
  * contact object. the params array could contain additional unused name/value
  * pairs
  *
  * @param array  $params (reference ) an assoc array of name/value pairs
  *
  * @return object CRM_Contact_BAO_Contact object
  * @access public
  * @static
  */
 static function add(&$params)
 {
     $contact =& new CRM_Contact_DAO_Contact();
     if (empty($params)) {
         return;
     }
     //fixed contact source
     if (isset($params['contact_source'])) {
         $params['source'] = $params['contact_source'];
     }
     //fix for preferred communication method
     $prefComm = CRM_Utils_Array::value('preferred_communication_method', $params);
     if ($prefComm && is_array($prefComm)) {
         unset($params['preferred_communication_method']);
         $newPref = array();
         foreach ($prefComm as $k => $v) {
             if ($v) {
                 $newPref[$k] = $v;
             }
         }
         $prefComm = $newPref;
         if (is_array($prefComm) && !empty($prefComm)) {
             $prefComm = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, array_keys($prefComm)) . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR;
             $contact->preferred_communication_method = $prefComm;
         } else {
             $contact->preferred_communication_method = '';
         }
     }
     $allNull = $contact->copyValues($params);
     $contact->id = CRM_Utils_Array::value('contact_id', $params);
     if ($contact->contact_type == 'Individual') {
         $allNull = false;
         //format individual fields
         require_once "CRM/Contact/BAO/Individual.php";
         CRM_Contact_BAO_Individual::format($params, $contact);
     } else {
         if ($contact->contact_type == 'Household') {
             if (isset($params['household_name'])) {
                 $allNull = false;
                 $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('household_name', $params, '');
             }
         } else {
             if ($contact->contact_type == 'Organization') {
                 if (isset($params['organization_name'])) {
                     $allNull = false;
                     $contact->display_name = $contact->sort_name = CRM_Utils_Array::value('organization_name', $params, '');
                 }
             }
         }
     }
     // privacy block
     $privacy = CRM_Utils_Array::value('privacy', $params);
     if ($privacy && is_array($privacy) && !empty($privacy)) {
         $allNull = false;
         foreach (self::$_commPrefs as $name) {
             $contact->{$name} = CRM_Utils_Array::value($name, $privacy, false);
         }
     }
     // since hash was required, make sure we have a 0 value for it, CRM-1063
     // fixed in 1.5 by making hash optional
     // only do this in create mode, not update
     if ((!array_key_exists('hash', $contact) || !$contact->hash) && !$contact->id) {
         $allNull = false;
         $contact->hash = md5(uniqid(rand(), true));
     }
     if (!$allNull) {
         $contact->save();
         require_once 'CRM/Core/BAO/Log.php';
         CRM_Core_BAO_Log::register($contact->id, 'civicrm_contact', $contact->id);
     }
     if ($contact->contact_type == 'Individual' && array_key_exists('current_employer', $params)) {
         // create current employer
         if ($params['current_employer']) {
             require_once 'CRM/Contact/BAO/Contact/Utils.php';
             CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contact->id, $params['current_employer']);
         } else {
             //unset if employer id exits
             if ($employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contact->id, 'employer_id')) {
                 require_once 'CRM/Contact/BAO/Contact/Utils.php';
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($contact->id, $employerId);
             }
         }
     }
     //update cached employee name
     if ($contact->contact_type == 'Organization') {
         require_once 'CRM/Contact/BAO/Contact/Utils.php';
         CRM_Contact_BAO_Contact_Utils::updateCurrentEmployer($contact->id);
     }
     // process greetings CRM-4575, cache greetings
     self::processGreetings($contact);
     return $contact;
 }
Esempio n. 9
0
 /**
  * Function to delete the relationship
  *
  * @param int $id relationship id
  *
  * @return null
  * @access public
  *
  * @static
  */
 static function del($id)
 {
     // delete from relationship table
     require_once 'CRM/Utils/Hook.php';
     CRM_Utils_Hook::pre('delete', 'Relationship', $id, CRM_Core_DAO::$_nullArray);
     $relationship =& new CRM_Contact_DAO_Relationship();
     $relationship->id = $id;
     $relationship->find(true);
     //to delete relationship between household and individual
     //or between individual and orgnization
     if ($relationship->relationship_type_id == 4 || $relationship->relationship_type_id == 7) {
         $sharedContact = new CRM_Contact_DAO_Contact();
         $sharedContact->id = $relationship->contact_id_a;
         $sharedContact->find(true);
         if ($relationship->relationship_type_id == 4 && $relationship->contact_id_b == $sharedContact->employer_id) {
             require_once 'CRM/Contact/BAO/Contact/Utils.php';
             CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a);
         } else {
             if ($sharedContact->mail_to_household_id == $relationship->contact_id_b) {
                 self::deleteSharedAddress($relationship->contact_id_a);
             }
         }
     }
     if (CRM_Core_Permission::access('CiviMember')) {
         // create $params array which isrequired to delete memberships
         // of the related contacts.
         $params = array('relationship_type_id' => "{$relationship->relationship_type_id}_a_b", 'contact_check' => array($relationship->contact_id_b => 1));
         $ids = array();
         // calling relatedMemberships to delete the memberships of
         // related contacts.
         self::relatedMemberships($relationship->contact_id_a, $params, $ids, CRM_Core_Action::DELETE);
     }
     $relationship->delete();
     CRM_Core_Session::setStatus(ts('Selected Relationship has been Deleted Successfuly.'));
     CRM_Utils_Hook::post('delete', 'Relationship', $relationship->id, $relationship);
     // delete the recently created Relationship
     require_once 'CRM/Utils/Recent.php';
     $relationshipRecent = array('id' => $id, 'type' => 'Relationship');
     CRM_Utils_Recent::del($relationshipRecent);
     return $relationship;
 }
Esempio n. 10
0
 /**
  * Function to disable/enable the relationship
  *
  * @param int $id relationship id
  *
  * @return null
  * @access public
  * @static
  */
 static function disableEnableRelationship($id, $action)
 {
     $relationship = new CRM_Contact_DAO_Relationship();
     $relationship->id = $id;
     $relationship->find(true);
     //get the relationship type id of "Employee of"
     $relTypeId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', 'Employee of', 'id', 'name_a_b');
     if ($relTypeId && $action & CRM_Core_Action::DISABLE) {
         require_once 'CRM/Contact/BAO/Contact/Utils.php';
         CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($relationship->contact_id_a);
     }
     if (CRM_Core_Permission::access('CiviMember')) {
         // create $params array which isrequired to delete memberships
         // of the related contacts.
         $params = array('relationship_type_id' => "{$relationship->relationship_type_id}_a_b", 'contact_check' => array($relationship->contact_id_b => 1));
         $ids = array();
         // calling relatedMemberships to delete/add the memberships of
         // related contacts.
         if ($action & CRM_Core_Action::DISABLE) {
             CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a, $params, $ids, CRM_Core_Action::DELETE, false);
         } else {
             if ($action & CRM_Core_Action::ENABLE) {
                 $ids['contact'] = $relationship->contact_id_a;
                 CRM_Contact_BAO_Relationship::relatedMemberships($relationship->contact_id_a, $params, $ids, CRM_Core_Action::ADD, false);
             }
         }
     }
 }
Esempio n. 11
0
 /**
  *  This function is called when the form is submitted
  *
  * @access public
  *
  * @return void
  */
 public function postProcess()
 {
     // store the submitted values in an array
     $params = $this->controller->exportValues($this->_name);
     // action is taken depending upon the mode
     if ($this->_action & CRM_Core_Action::DELETE) {
         CRM_Contact_BAO_Relationship::del($this->_relationshipId);
         return;
     }
     $ids = array('contact' => $this->_contactId);
     $relationshipTypeId = str_replace(array('_', 'a', 'b'), '', $params['relationship_type_id']);
     // CRM-14612 - Don't use adv-checkbox as it interferes with the form js
     $params['is_permission_a_b'] = CRM_Utils_Array::value('is_permission_a_b', $params, 0);
     $params['is_permission_b_a'] = CRM_Utils_Array::value('is_permission_b_a', $params, 0);
     // Update mode (always single)
     if ($this->_action & CRM_Core_Action::UPDATE) {
         $ids['relationship'] = $this->_relationshipId;
         $relation = CRM_Contact_BAO_Relationship::getContactIds($this->_relationshipId);
         $ids['contactTarget'] = $relation->contact_id_a == $this->_contactId ? $relation->contact_id_b : $relation->contact_id_a;
         if ($this->_isCurrentEmployer) {
             // if relationship type changes, relationship is disabled, or "current employer" is unchecked,
             // clear the current employer. CRM-3235.
             $relChanged = $relationshipTypeId != $this->_values['relationship_type_id'];
             if (!$params['is_active'] || !$params['is_current_employer'] || $relChanged) {
                 CRM_Contact_BAO_Contact_Utils::clearCurrentEmployer($this->_values['contact_id_a']);
                 // Refresh contact summary if in ajax mode
                 $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
             }
         }
     } else {
         // Fill up this weird param with contact ids like the weird relationship bao expects
         $params['contact_check'] = array_fill_keys(explode(',', $params['related_contact_id']), 1);
         if (!$this->_rtype) {
             list(, $this->_rtype) = explode('_', $params['relationship_type_id'], 2);
         }
     }
     $params['start_date'] = CRM_Utils_Date::processDate($params['start_date'], NULL, TRUE);
     $params['end_date'] = CRM_Utils_Date::processDate($params['end_date'], NULL, TRUE);
     // Process custom data
     $customFields = CRM_Core_BAO_CustomField::getFields('Relationship', FALSE, FALSE, $relationshipTypeId);
     $params['custom'] = CRM_Core_BAO_CustomField::postProcess($params, $customFields, $this->_relationshipId, 'Relationship');
     // Save relationships
     list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($params, $ids);
     // if this is called from case view,
     //create an activity for case role removal.CRM-4480
     if ($this->_caseId) {
         CRM_Case_BAO_Case::createCaseRoleActivity($this->_caseId, $relationshipIds, $params['contact_check'], $this->_contactId);
     }
     if ($valid) {
         CRM_Core_Session::setStatus(ts('Relationship created.', array('count' => $valid, 'plural' => '%count relationships created.')), ts('Saved'), 'success');
     }
     if ($invalid) {
         CRM_Core_Session::setStatus(ts('%count relationship record was not created due to an invalid contact type.', array('count' => $invalid, 'plural' => '%count relationship records were not created due to invalid contact types.')), ts('%count invalid relationship record', array('count' => $invalid, 'plural' => '%count invalid relationship records')));
     }
     if ($duplicate) {
         CRM_Core_Session::setStatus(ts('One relationship was not created because it already exists.', array('count' => $duplicate, 'plural' => '%count relationships were not created because they already exist.')), ts('%count duplicate relationship', array('count' => $duplicate, 'plural' => '%count duplicate relationships')));
     }
     if ($saved) {
         CRM_Core_Session::setStatus(ts('Relationship record has been updated.'), ts('Saved'), 'success');
     }
     // Save notes
     if ($this->_action & CRM_Core_Action::UPDATE || $params['note']) {
         foreach ($relationshipIds as $id) {
             $noteParams = array('entity_id' => $id, 'entity_table' => 'civicrm_relationship');
             $existing = civicrm_api3('note', 'get', $noteParams);
             if (!empty($existing['id'])) {
                 $noteParams['id'] = $existing['id'];
             }
             $noteParams['note'] = $params['note'];
             $noteParams['contact_id'] = $this->_contactId;
             if (!empty($existing['id']) || $params['note']) {
                 $action = $params['note'] ? 'create' : 'delete';
                 civicrm_api3('note', $action, $noteParams);
             }
         }
     }
     // Membership for related contacts CRM-1657
     if (CRM_Core_Permission::access('CiviMember') && !$duplicate) {
         $params['relationship_ids'] = $relationshipIds;
         if ($this->_action & CRM_Core_Action::ADD) {
             CRM_Contact_BAO_Relationship::relatedMemberships($this->_contactId, $params, $ids, $this->_action);
         } elseif ($this->_action & CRM_Core_Action::UPDATE) {
             //fixes for CRM-7985
             //only if the relationship has been toggled to enable /disable
             if (CRM_Utils_Array::value('is_active', $params) != $this->_enabled) {
                 $active = !empty($params['is_active']) ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE;
                 CRM_Contact_BAO_Relationship::disableEnableRelationship($this->_relationshipId, $active);
             }
         }
         // Refresh contact tabs with related data
         $this->ajaxResponse['updateTabs'] = array('#tab_member' => CRM_Contact_BAO_Contact::getCountComponent('membership', $this->_contactId));
     }
     // Set current employee/employer relationship, CRM-3532
     if ($params['is_current_employer'] && $this->_allRelationshipNames[$relationshipTypeId]["name_a_b"] == 'Employee of') {
         $employerParams = array();
         foreach ($relationshipIds as $id) {
             // Fixme this is dumb why do we have to look this up again?
             $rel = CRM_Contact_BAO_Relationship::getContactIds($id);
             $employerParams[$rel->contact_id_a] = $rel->contact_id_b;
         }
         CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($employerParams);
         // Refresh contact summary if in ajax mode
         $this->ajaxResponse['reloadBlocks'] = array('#crm-contactinfo-content');
     }
 }