/** * takes an associative array and creates a relationship object * * * @param array $params (reference ) an assoc array of name/value pairs * @param array $ids the array that holds all the db ids * * @return object CRM_Contact_BAO_Relationship object * @access public * @static */ function create(&$params, &$ids) { $valid = $invalid = $duplicate = $saved = 0; $relationshipId = CRM_Utils_Array::value('relationship', $ids); if (!$relationshipId) { // creating a new relationship $dataExists = CRM_Contact_BAO_Relationship::dataExists($params); if (!$dataExists) { return null; } foreach ($params['contact_check'] as $key => $value) { $errors = ''; // check if the realtionship is valid between contacts. // step 1: check if the relationship is valid if not valid skip and keep the count // step 2: check the if two contacts already have a relationship if yes skip and keep the count // step 3: if valid relationship then add the relation and keep the count $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $key); // step 1 if ($errors) { $invalid++; continue; } if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, CRM_Utils_Array::value('contact', $ids), $key)) { // step 2 $duplicate++; continue; } $relationship = CRM_Contact_BAO_Relationship::add($params, $ids, $key); $valid++; } //return array( $valid, $invalid, $duplicate, $saved ); } else { //editing the relationship // check for duplicate relationship if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, CRM_Utils_Array::value('contact', $ids), $ids['contactTarget'], $relationshipId)) { $duplicate++; return array($valid, $invalid, $duplicate); } // editing an existing relationship CRM_Contact_BAO_Relationship::add($params, $ids, $ids['contactTarget']); $saved++; //return array( $valid, $invalid, $duplicate, $saved ); } return array($valid, $invalid, $duplicate, $saved); }
/** * process the form after the input has been submitted and validated * * @access public * @return None */ public function postProcess() { require_once 'CRM/Contact/Form/Relationship.php'; // store the submitted values in an array $params = $this->controller->exportValues($this->_name); $this->set('searchDone', 0); if (CRM_Utils_Array::value('_qf_AddToOrganization_refresh', $_POST)) { $searchParams['contact_type'] = array('Organization' => 'Organization'); $searchParams['rel_contact'] = $params['name']; CRM_Contact_Form_Relationship::search($searchParams); $this->set('searchDone', 1); return; } $data = array(); //$params['relationship_type_id']='4_a_b'; $data['relationship_type_id'] = $params['relationship_type_id']; $data['is_active'] = 1; $invalid = 0; $valid = 0; $duplicate = 0; if (is_array($this->_contactIds)) { foreach ($this->_contactIds as $value) { $ids = array(); $ids['contact'] = $value; //contact b --> organization // contact a -> individual $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']); if ($errors) { $invalid = $invalid + 1; continue; } if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, CRM_Utils_Array::value('contact', $ids), $params['contact_check'])) { // step 2 $duplicate++; continue; } CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']); $valid++; } $status = array(ts('Added Contact(s) to Organization'), ts('Total Selected Contact(s): %1', array(1 => $valid + $invalid + $duplicate))); if ($valid) { $status[] = ts('New relationship record(s) created: %1.', array(1 => $valid)) . '<br/>'; } if ($invalid) { $status[] = ts('Relationship record(s) not created due to invalid target contact type: %1.', array(1 => $invalid)) . '<br/>'; } if ($duplicate) { $status[] = ts('Relationship record(s) not created - duplicate of existing relationship: %1.', array(1 => $duplicate)) . '<br/>'; } CRM_Core_Session::setStatus($status); } }
/** * function for validation * * @param array $params (reference ) an assoc array of name/value pairs * * @return mixed true or array of errors * @access public * @static */ function formRule(&$params) { // hack, no error check for refresh if (CRM_Utils_Array::value('_qf_Relationship_refresh', $_POST)) { return true; } $ids = array(); $session =& CRM_Core_Session::singleton(); $ids['contact'] = $session->get('contactId', 'CRM_Contact_Form_Relationship'); $ids['relationship'] = $session->get('relationshipId', 'CRM_Contact_Form_Relationship'); $errors = array(); if (CRM_Utils_Array::value('contact_check', $params) && is_array($params['contact_check'])) { foreach ($params['contact_check'] as $cid => $dontCare) { $message = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $cid); if ($message) { $errors['relationship_type_id'] = $message; break; } } } else { $errors['contact_check'] = ts('Please select at least one contact.'); } return empty($errors) ? true : $errors; }
/** * Function to create new retaionship * * @param object $contact A valid Contact object. * * @param object $target_contact A valid Contact object * @param String $relationship_type_name A valid Relationship_type eg. Parent of etc. * @param array $ params Associative array of property name/value pairs to be inserted. See Data Model for available properties. * * @return newly created 'relationship object' object * * @access public * */ function crm_create_relationship($contact = null, $target_contact = null, $relationship_type_name, $params) { $relationTypeID = null; if (!isset($contact->id) and !isset($target_contact->id)) { return _crm_error('source or target contact object does not have contact ID'); } $sourceContact = $contact->id; $targetContact = $target_contact->id; require_once 'CRM/Contact/DAO/RelationshipType.php'; $reletionType =& new CRM_Contact_DAO_RelationshipType(); $reletionType->name_a_b = $relationship_type_name; $reletionType->find(); if ($reletionType->fetch()) { $relationTypeID = $reletionType->id; $relationTypeID .= '_a_b'; } if (!$relationTypeID) { $reletionType =& new CRM_Contact_DAO_RelationshipType(); $reletionType->name_b_a = $relationship_type_name; $reletionType->find(); if ($reletionType->fetch()) { $relationTypeID = $reletionType->id; $relationTypeID .= '_b_a'; } } if (!$relationTypeID) { return _crm_error('$relationship_type_ is not valid relationship type '); } $params['relationship_type_id'] = $relationTypeID; $ids['contact'] = $sourceContact; $params['contact_check'] = array($targetContact => $targetContact); require_once 'CRM/Contact/BAO/Relationship.php'; $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $targetContact); if ($errors) { return _crm_error($errors); } if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, $sourceContact, $targetContact)) { return _crm_error('Duplicate relationship'); } return CRM_Contact_BAO_Relationship::add($params, $ids, $targetContact); }
/** * function for validation * * @param array $params (reference ) an assoc array of name/value pairs * * @return mixed true or array of errors * @access public * @static */ static function formRule($params, $files, $form) { // hack, no error check for refresh if (CRM_Utils_Array::value('_qf_Relationship_refresh', $_POST) || CRM_Utils_Array::value('_qf_Relationship_refresh_save', $_POST) || CRM_Utils_Array::value('_qf_Relationship_refresh_savedetails', $_POST)) { return true; } $form->modifyParams($params); $ids = array(); $session = CRM_Core_Session::singleton(); $ids['contact'] = $form->get('contactId'); $ids['relationship'] = $form->get('relationshipId'); $errors = array(); $employerId = null; if (CRM_Utils_Array::value('contact_check', $params) && is_array($params['contact_check'])) { foreach ($params['contact_check'] as $cid => $dontCare) { $message = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $cid); if ($message) { $errors['relationship_type_id'] = $message; break; } if ($cid == CRM_Utils_Array::value('employee_of', $params)) { $employerId = $cid; } } } else { if ($form->_callAjax) { $errors['store_contacts'] = ts('Select select at least one contact from Target Contact(s).'); } else { $errors['contact_check'] = ts('Please select at least one contact.'); } } if (CRM_Utils_Array::value('employee_of', $params) && !$employerId) { if ($form->_callAjax) { $errors['store_employer'] = ts('Current employer should be one of the selected contacts.'); } else { $errors['employee_of'] = ts('Current employer should be one of the selected contacts.'); } } if (CRM_Utils_Array::value('employer_of', $params) && CRM_Utils_Array::value('contact_check', $params) && array_diff(array_keys($params['employer_of']), array_keys($params['contact_check']))) { if ($form->_callAjax) { $errors['store_employer'] = ts('Current employee should be among the selected contacts.'); } else { $errors['employer_of'] = ts('Current employee should be among the selected contacts.'); } } return empty($errors) ? true : $errors; }
/** * process the form after the input has been submitted and validated * * @access public * * @return None */ public function postProcess() { // store the submitted values in an array $params = $this->controller->exportValues($this->_name); $this->set('searchDone', 0); if (CRM_Utils_Array::value('_qf_AddToOrganization_refresh', $_POST)) { $searchParams['contact_type'] = array('Organization' => 'Organization'); $searchParams['rel_contact'] = $params['name']; CRM_Contact_Form_Task_AddToHousehold::search($this, $searchParams); $this->set('searchDone', 1); return; } $data = array(); $data['relationship_type_id'] = $params['relationship_type_id']; $data['is_active'] = 1; $invalid = 0; $valid = 0; $duplicate = 0; if (is_array($this->_contactIds)) { foreach ($this->_contactIds as $value) { $ids = array(); $ids['contact'] = $value; $errors = CRM_Contact_BAO_Relationship::checkValidRelationship($params, $ids, $params['contact_check']); if ($errors) { $invalid++; continue; } if (CRM_Contact_BAO_Relationship::checkDuplicateRelationship($params, CRM_Utils_Array::value('contact', $ids), $params['contact_check'])) { $duplicate++; continue; } CRM_Contact_BAO_Relationship::add($data, $ids, $params['contact_check']); $valid++; } $org = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['contact_check'], 'display_name'); list($rtype, $a_b) = explode('_', $data['relationship_type_id'], 2); $relationship = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $rtype, "label_{$a_b}"); $status = array(ts('%count %2 %3 relationship created', array('count' => $valid, 'plural' => '%count %2 %3 relationships created', 2 => $relationship, 3 => $org))); if ($duplicate) { $status[] = ts('%count was skipped because the contact is already %2 %3', array('count' => $duplicate, 'plural' => '%count were skipped because the contacts are already %2 %3', 2 => $relationship, 3 => $org)); } if ($invalid) { $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', array('count' => $invalid, 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship')); } $status = '<ul><li>' . implode('</li><li>', $status) . '</li></ul>'; CRM_Core_Session::setStatus($status, ts('Relationship Created', array('count' => $valid, 'plural' => 'Relationships Created')), 'success', array('expires' => 0)); } }