function __construct() { $this->fetchActivityTypes(); $this->fetchCaseStatusses(); $rels = CRM_Core_PseudoConstant::relationshipType(); $coach_rel_type_id = false; foreach ($rels as $relid => $v) { $this->rel_types[$relid] = $v['label_b_a']; if ($v['label_b_a'] == 'Coach') { $coach_rel_type_id = $relid; } } $coaches = array(); if ($coach_rel_type_id) { $coaches = $this->coaches($coach_rel_type_id); } $this->_groupFilter = FALSE; $this->_tagFilter = FALSE; $this->_columns = array('civicrm_activity`' => array('dao' => 'CRM_Activity_DAO_Activity', 'fields' => array(), 'filters' => array('activity_type_id' => array('title' => ts('Activity type'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_OptionGroup::values('activity_type'), 'pseudofield' => true, 'default' => $this->_activityTypes), 'case_status' => array('title' => ts('Status'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_OptionGroup::values('case_status'), 'pseudofield' => true, 'default' => $this->_openedCaseStatusses), 'case_role' => array('title' => ts('Role on case'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->rel_types), 'coach' => array('title' => ts('Coach'), 'type' => CRM_Utils_Type::T_INT, 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $coaches, 'pseudofield' => true), 'my_cases' => array('title' => ts('My cases'), 'type' => CRM_Utils_Type::T_BOOLEAN, 'operatorType' => CRM_Report_Form::OP_SELECT, 'options' => array('0' => ts('No'), '1' => ts('Yes')), 'default' => '1', 'pseudofield' => TRUE)))); parent::__construct(); $this->fetchCaseType(); $this->fetchChequenummer(); $this->fetchStatusGroup(); $this->fetchCoachIdentifier(); }
function __construct() { $this->case_statuses = CRM_Case_PseudoConstant::caseStatus(); $rels = CRM_Core_PseudoConstant::relationshipType(); foreach ($rels as $relid => $v) { $this->rel_types[$relid] = $v['label_b_a']; } $this->deleted_labels = array('' => ts('- select -'), 0 => ts('No'), 1 => ts('Yes')); $this->_columns = array('civicrm_c2' => array('dao' => 'CRM_Contact_DAO_Contact', 'fields' => array('display_name' => array('title' => ts('Client'), 'required' => true))), 'civicrm_case' => array('dao' => 'CRM_Case_DAO_Case', 'fields' => array('id' => array('title' => ts('Case ID'), 'required' => true), 'start_date' => array('title' => ts('Start Date'), 'default' => true), 'end_date' => array('title' => ts('End Date'), 'default' => true), 'status_id' => array('title' => ts('Status'), 'default' => true), 'duration' => array('title' => ts('Duration (Days)'), 'default' => false), 'is_deleted' => array('title' => ts('Deleted?'), 'default' => false, 'type' => CRM_Utils_Type::T_INT)), 'filters' => array('start_date' => array('title' => ts('Start Date'), 'operatorType' => CRM_Report_Form::OP_DATE), 'end_date' => array('title' => ts('End Date'), 'operatorType' => CRM_Report_Form::OP_DATE), 'status_id' => array('title' => ts('Status'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->case_statuses), 'is_deleted' => array('title' => ts('Deleted?'), 'type' => CRM_Report_Form::OP_INT, 'operatorType' => CRM_Report_Form::OP_SELECT, 'options' => $this->deleted_labels, 'default' => 0))), 'civicrm_contact' => array('dao' => 'CRM_Contact_DAO_Contact', 'fields' => array('sort_name' => array('title' => ts('Staff Member'), 'default' => true)), 'filters' => array('sort_name' => array('title' => ts('Staff Member')))), 'civicrm_relationship' => array('dao' => 'CRM_Contact_DAO_Relationship', 'filters' => array('relationship_type_id' => array('title' => ts('Staff Relationship'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->rel_types))), 'civicrm_relationship_type' => array('dao' => 'CRM_Contact_DAO_RelationshipType', 'fields' => array('label_b_a' => array('title' => ts('Relationship'), 'default' => true))), 'civicrm_case_contact' => array('dao' => 'CRM_Case_DAO_CaseContact')); parent::__construct(); }
public function __construct() { $this->case_types = CRM_Case_PseudoConstant::caseType(); $this->case_statuses = CRM_Core_OptionGroup::values('case_status'); $rels = CRM_Core_PseudoConstant::relationshipType(); foreach ($rels as $relid => $v) { $this->rel_types[$relid] = $v['label_b_a']; } $this->deleted_labels = array('' => ts('- select -'), 0 => ts('No'), 1 => ts('Yes')); $this->_columns = array('civicrm_c2' => array('dao' => 'CRM_Contact_DAO_Contact', 'fields' => array('client_name' => array('name' => 'sort_name', 'title' => ts('Client'), 'required' => TRUE), 'id' => array('no_display' => TRUE, 'required' => TRUE))), 'civicrm_case' => array('dao' => 'CRM_Case_DAO_Case', 'fields' => array('id' => array('title' => ts('Case ID'), 'required' => TRUE), 'subject' => array('title' => ts('Case Subject'), 'default' => FALSE), 'status_id' => array('title' => ts('Status'), 'default' => FALSE), 'case_type_id' => array('title' => ts('Case Type'), 'default' => FALSE), 'start_date' => array('title' => ts('Start Date'), 'default' => TRUE, 'type' => CRM_Utils_Type::T_DATE), 'end_date' => array('title' => ts('End Date'), 'default' => FALSE, 'type' => CRM_Utils_Type::T_DATE), 'duration' => array('title' => ts('Duration (Days)'), 'default' => FALSE), 'is_deleted' => array('title' => ts('Deleted?'), 'default' => FALSE, 'type' => CRM_Utils_Type::T_INT)), 'filters' => array('start_date' => array('title' => ts('Start Date'), 'operatorType' => CRM_Report_Form::OP_DATE, 'type' => CRM_Utils_Type::T_DATE), 'end_date' => array('title' => ts('End Date'), 'operatorType' => CRM_Report_Form::OP_DATE, 'type' => CRM_Utils_Type::T_DATE), 'case_type_id' => array('title' => ts('Case Type'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->case_types), 'status_id' => array('title' => ts('Status'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->case_statuses), 'is_deleted' => array('title' => ts('Deleted?'), 'type' => CRM_Report_Form::OP_INT, 'operatorType' => CRM_Report_Form::OP_SELECT, 'options' => $this->deleted_labels, 'default' => 0))), 'civicrm_contact' => array('dao' => 'CRM_Contact_DAO_Contact', 'fields' => array('sort_name' => array('title' => ts('Staff Member'), 'default' => FALSE)), 'filters' => array('my_cases' => array('title' => ts('My cases'), 'type' => CRM_Utils_Type::T_BOOLEAN, 'operatorType' => CRM_Report_Form::OP_SELECT, 'options' => array('0' => ts('No'), '1' => ts('Yes')), 'pseudofield' => TRUE))), 'civicrm_relationship' => array('dao' => 'CRM_Contact_DAO_Relationship', 'filters' => array('relationship_type_id' => array('title' => ts('Staff Relationship'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->rel_types))), 'civicrm_relationship_type' => array('dao' => 'CRM_Contact_DAO_RelationshipType', 'fields' => array('label_b_a' => array('title' => ts('Relationship'), 'default' => FALSE))), 'civicrm_case_contact' => array('dao' => 'CRM_Case_DAO_CaseContact'), 'civicrm_activity' => array('filters' => array('activity_type_id' => array('title' => ts('Activity Type'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_PseudoConstant::activityType(TRUE, TRUE, FALSE, 'label', TRUE), 'pseudofield' => TRUE)))); parent::__construct(); }
function __construct() { $this->case_statuses = CRM_Case_PseudoConstant::caseStatus(); $this->case_types = CRM_Case_PseudoConstant::caseType(); $rels = CRM_Core_PseudoConstant::relationshipType(); foreach ($rels as $relid => $v) { $this->rel_types[$relid] = $v['label_b_a']; } $this->_columns = array('civicrm_case' => array('dao' => 'CRM_Case_DAO_Case', 'fields' => array('id' => array('title' => ts('Case ID'), 'no_display' => true, 'required' => true), 'subject' => array('title' => ts('Subject'), 'required' => true), 'start_date' => array('title' => ts('Start Date')), 'end_date' => array('title' => ts('End Date')), 'status_id' => array('title' => ts('Case Status')), 'case_type_id' => array('title' => ts('Case Type'))), 'filters' => array('start_date' => array('title' => ts('Start Date'), 'operatorType' => CRM_Report_Form::OP_DATE, 'type' => CRM_Utils_Type::T_DATE), 'end_date' => array('title' => ts('End Date'), 'operatorType' => CRM_Report_Form::OP_DATE, 'type' => CRM_Utils_Type::T_DATE), 'status_id' => array('title' => ts('Case Status'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->case_statuses), 'case_type_id' => array('title' => ts('Case Type'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->case_types))), 'civicrm_contact' => array('dao' => 'CRM_Contact_DAO_Contact', 'fields' => array('display_name' => array('title' => ts('Client Name'), 'required' => true), 'id' => array('no_display' => true, 'required' => true)), 'filters' => array('display_name' => array('title' => ts('Client Name')))), 'civicrm_relationship' => array('dao' => 'CRM_Contact_DAO_Relationship', 'fields' => array('relationship_type_id' => array('title' => ts('Case Role'))), 'filters' => array('relationship_type_id' => array('title' => ts('Case Role'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->rel_types))), 'civicrm_relationship_type' => array('dao' => 'CRM_Contact_DAO_RelationshipType'), 'civicrm_email' => array('dao' => 'CRM_Core_DAO_Email', 'fields' => array('email' => array('title' => ts('Email'), 'no_repeat' => true)), 'grouping' => 'contact-fields'), 'civicrm_phone' => array('dao' => 'CRM_Core_DAO_Phone', 'fields' => array('phone' => array('title' => ts('Phone'), 'no_repeat' => true)), 'grouping' => 'contact-fields'), 'civicrm_address' => array('dao' => 'CRM_Core_DAO_Address', 'fields' => array('street_address' => null, 'state_province_id' => array('title' => ts('State/Province')), 'country_id' => array('title' => ts('Country'))), 'grouping' => 'contact-fields', 'filters' => array('country_id' => array('title' => ts('Country'), 'type' => CRM_Utils_Type::T_INT, 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_PseudoConstant::country()), 'state_province_id' => array('title' => ts('State/Province'), 'type' => CRM_Utils_Type::T_INT, 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_PseudoConstant::stateProvince()))), 'civicrm_worldregion' => array('dao' => 'CRM_Core_DAO_Worldregion', 'filters' => array('worldregion_id' => array('name' => 'id', 'title' => ts('WorldRegion'), 'type' => CRM_Utils_Type::T_INT, 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_PseudoConstant::worldRegion()))), 'civicrm_country' => array('dao' => 'CRM_Core_DAO_Country'), 'civicrm_activity' => array('dao' => 'CRM_Activity_DAO_Activity', 'fields' => array('activity_subject' => array('name' => 'subject', 'title' => ts('Activity Subject'), 'no_display' => true)), 'filters' => array('activity_date_time' => array('title' => ts('Last Action Date'), 'operatorType' => CRM_Report_Form::OP_DATE))), 'civicrm_case_contact' => array('dao' => 'CRM_Case_DAO_CaseContact')); $this->_options = array('my_cases' => array('title' => ts('My Cases'), 'type' => 'checkbox')); parent::__construct(); }
function &allRelationshipTypes() { static $relationshipTypes = array(); if (!$relationshipTypes) { $relationshipInfo = CRM_Core_PseudoConstant::relationshipType(); $relationshipTypes = array(); foreach ($relationshipInfo as $id => $info) { $relationshipTypes[$id] = $info['label_b_a']; } } return $relationshipTypes; }
/** * */ function __construct() { $this->case_statuses = CRM_Case_PseudoConstant::caseStatus(); $this->case_types = CRM_Case_PseudoConstant::caseType(); $rels = CRM_Core_PseudoConstant::relationshipType(); foreach ($rels as $relid => $v) { $this->rel_types[$relid] = $v['label_b_a']; } $this->deleted_labels = array('' => ts('- select -'), 0 => ts('No'), 1 => ts('Yes')); $this->caseActivityTypes = array(); foreach (CRM_Case_PseudoConstant::caseActivityType() as $typeDetail) { $this->caseActivityTypes[$typeDetail['id']] = $typeDetail['label']; } $this->_columns = array('civicrm_case' => array('dao' => 'CRM_Case_DAO_Case', 'fields' => array('id' => array('title' => ts('Case ID'), 'no_display' => TRUE, 'required' => TRUE), 'subject' => array('title' => ts('Subject'), 'required' => TRUE), 'start_date' => array('title' => ts('Start Date'), 'type' => CRM_Utils_Type::T_DATE), 'end_date' => array('title' => ts('End Date'), 'type' => CRM_Utils_Type::T_DATE), 'status_id' => array('title' => ts('Case Status')), 'case_type_id' => array('title' => ts('Case Type')), 'is_deleted' => array('title' => ts('Deleted?'), 'default' => FALSE, 'type' => CRM_Utils_Type::T_INT)), 'filters' => array('start_date' => array('title' => ts('Start Date'), 'operatorType' => CRM_Report_Form::OP_DATE, 'type' => CRM_Utils_Type::T_DATE), 'end_date' => array('title' => ts('End Date'), 'operatorType' => CRM_Report_Form::OP_DATE, 'type' => CRM_Utils_Type::T_DATE), 'status_id' => array('title' => ts('Case Status'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->case_statuses), 'case_type_id' => array('title' => ts('Case Type'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->case_types), 'is_deleted' => array('title' => ts('Deleted?'), 'type' => CRM_Utils_Type::T_INT, 'operatorType' => CRM_Report_Form::OP_SELECT, 'options' => $this->deleted_labels, 'default' => 0))), 'civicrm_contact' => array('dao' => 'CRM_Contact_DAO_Contact', 'fields' => array('client_sort_name' => array('name' => 'sort_name', 'title' => ts('Client Name'), 'required' => TRUE), 'id' => array('no_display' => TRUE, 'required' => TRUE)), 'filters' => array('sort_name' => array('title' => ts('Client Name')))), 'civicrm_relationship' => array('dao' => 'CRM_Contact_DAO_Relationship', 'fields' => array('case_role' => array('name' => 'relationship_type_id', 'title' => ts('Case Role(s)'))), 'filters' => array('case_role' => array('name' => 'relationship_type_id', 'title' => ts('Case Role(s)'), 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => $this->rel_types))), 'civicrm_email' => array('dao' => 'CRM_Core_DAO_Email', 'fields' => array('email' => array('title' => ts('Email'), 'no_repeat' => TRUE)), 'grouping' => 'contact-fields'), 'civicrm_phone' => array('dao' => 'CRM_Core_DAO_Phone', 'fields' => array('phone' => array('title' => ts('Phone'), 'no_repeat' => TRUE)), 'grouping' => 'contact-fields'), 'civicrm_address' => array('dao' => 'CRM_Core_DAO_Address', 'fields' => array('street_address' => NULL, 'state_province_id' => array('title' => ts('State/Province')), 'country_id' => array('title' => ts('Country'))), 'grouping' => 'contact-fields', 'filters' => array('country_id' => array('title' => ts('Country'), 'type' => CRM_Utils_Type::T_INT, 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_PseudoConstant::country()), 'state_province_id' => array('title' => ts('State/Province'), 'type' => CRM_Utils_Type::T_INT, 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_PseudoConstant::stateProvince()))), 'civicrm_worldregion' => array('dao' => 'CRM_Core_DAO_Worldregion', 'filters' => array('worldregion_id' => array('name' => 'id', 'title' => ts('WorldRegion'), 'type' => CRM_Utils_Type::T_INT, 'operatorType' => CRM_Report_Form::OP_MULTISELECT, 'options' => CRM_Core_PseudoConstant::worldRegion()))), 'civicrm_country' => array('dao' => 'CRM_Core_DAO_Country'), 'civicrm_activity_last' => array('dao' => 'CRM_Activity_DAO_Activity', 'filters' => array('last_activity_date_time' => array('name' => 'activity_date_time', 'title' => ts('Last Action Date'), 'operatorType' => CRM_Report_Form::OP_DATE)), 'alias' => 'civireport_activity_last'), 'civicrm_activity_last_completed' => array('dao' => 'CRM_Activity_DAO_Activity', 'fields' => array('last_completed_activity_subject' => array('name' => 'subject', 'title' => ts('Subject of the last completed activity in the case')), 'last_completed_activity_type' => array('name' => 'activity_type_id', 'title' => ts('Activity type of the last completed activity'))))); $this->_options = array('my_cases' => array('title' => ts('My Cases'), 'type' => 'checkbox')); parent::__construct(); }
/** * View details of a relationship. */ public function view() { $viewRelationship = CRM_Contact_BAO_Relationship::getRelationship($this->_contactId, NULL, NULL, NULL, $this->_id); //To check whether selected contact is a contact_id_a in //relationship type 'a_b' in relationship table, if yes then //revert the permissionship text in template $relationship = new CRM_Contact_DAO_Relationship(); $relationship->id = $viewRelationship[$this->_id]['id']; if ($relationship->find(TRUE)) { if ($viewRelationship[$this->_id]['rtype'] == 'a_b' && $this->_contactId == $relationship->contact_id_a) { $this->assign("is_contact_id_a", TRUE); } } $relType = $viewRelationship[$this->_id]['civicrm_relationship_type_id']; $this->assign('viewRelationship', $viewRelationship); $employerId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'employer_id'); $this->assign('isCurrentEmployer', FALSE); $relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name')); if ($viewRelationship[$this->_id]['employer_id'] == $this->_contactId) { $this->assign('isCurrentEmployer', TRUE); } elseif ($relType == $relTypes['Employee of']['id'] && $viewRelationship[$this->_id]['cid'] == $employerId) { // make sure we are viewing employee of relationship $this->assign('isCurrentEmployer', TRUE); } $viewNote = CRM_Core_BAO_Note::getNote($this->_id); $this->assign('viewNote', $viewNote); $groupTree = CRM_Core_BAO_CustomGroup::getTree('Relationship', $this, $this->_id, 0, $relType); CRM_Core_BAO_CustomGroup::buildCustomDataView($this, $groupTree, FALSE, NULL, NULL, NULL, $this->_id); $rType = CRM_Utils_Array::value('rtype', $viewRelationship[$this->_id]); // add viewed contribution to recent items list $url = CRM_Utils_System::url('civicrm/contact/view/rel', "action=view&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&context=home"); $session = CRM_Core_Session::singleton(); $recentOther = array(); if ($session->get('userID') == $this->_contactId || CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) { $recentOther = array('editUrl' => CRM_Utils_System::url('civicrm/contact/view/rel', "action=update&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home"), 'deleteUrl' => CRM_Utils_System::url('civicrm/contact/view/rel', "action=delete&reset=1&id={$viewRelationship[$this->_id]['id']}&cid={$this->_contactId}&rtype={$rType}&context=home")); } $displayName = CRM_Contact_BAO_Contact::displayName($this->_contactId); $this->assign('displayName', $displayName); CRM_Utils_System::setTitle(ts('View Relationship for') . ' ' . $displayName); $title = $displayName . ' (' . $viewRelationship[$this->_id]['relation'] . ' ' . CRM_Contact_BAO_Contact::displayName($viewRelationship[$this->_id]['cid']) . ')'; // add the recently viewed Relationship CRM_Utils_Recent::add($title, $url, $viewRelationship[$this->_id]['id'], 'Relationship', $this->_contactId, NULL, $recentOther); }
/** * Get a list of managed relationship-types by searching CiviCase XML files. * * @param \CRM_Case_XMLRepository $xmlRepo * @param \CRM_Core_ManagedEntities $me * * @return array * @see CRM_Utils_Hook::managed */ public static function createManagedRelationshipTypes(CRM_Case_XMLRepository $xmlRepo, CRM_Core_ManagedEntities $me) { $result = array(); if (!isset(Civi::$statics[__CLASS__]['reltypes'])) { $relationshipInfo = CRM_Core_PseudoConstant::relationshipType('label', TRUE, NULL); Civi::$statics[__CLASS__]['reltypes'] = CRM_Utils_Array::collect(CRM_Case_XMLProcessor::REL_TYPE_CNAME, $relationshipInfo); } $validRelTypes = Civi::$statics[__CLASS__]['reltypes']; $relTypes = $xmlRepo->getAllDeclaredRelationshipTypes(); foreach ($relTypes as $relType) { $managed = array('module' => 'civicrm', 'name' => "civicase:rel:{$relType}", 'entity' => 'RelationshipType', 'update' => 'never', 'cleanup' => 'unused', 'params' => array('version' => 3, 'name_a_b' => "{$relType} is", 'name_b_a' => $relType, 'label_a_b' => "{$relType} is", 'label_b_a' => $relType, 'description' => $relType, 'contact_type_a' => 'Individual', 'contact_type_b' => 'Individual', 'contact_sub_type_a' => NULL, 'contact_sub_type_b' => NULL)); // We'll create managed-entity if this record doesn't exist yet // or if we previously decided to manage this record. if (!in_array($relType, $validRelTypes)) { $result[] = $managed; } elseif ($me->get($managed['module'], $managed['name'])) { $result[] = $managed; } } return $result; }
function preProcess() { //custom data related code $this->_cdType = CRM_Utils_Array::value('type', $_GET); $this->assign('cdType', false); if ($this->_cdType) { $this->assign('cdType', true); return CRM_Custom_Form_CustomData::preProcess($this); } $this->_contactId = $this->get('contactId'); $this->_relationshipId = $this->get('id'); $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this); $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); $this->assign('sort_name_a', $this->_display_name_a); $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); //get the relationship values. $this->_values = array(); if ($this->_relationshipId) { $params = array('id' => $this->_relationshipId); CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values); } if (!$this->_rtypeId) { $params = $this->controller->exportValues($this->_name); if (isset($params['relationship_type_id'])) { $this->_rtypeId = $params['relationship_type_id']; } else { if (!empty($this->_values)) { $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype; } } } //get the relationship type id $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId); //get the relationship type if (!$this->_rtype) { $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); } $this->assign("rtype", $this->_rtype); require_once 'CRM/Core/PseudoConstant.php'; //use name as it remain constant, CRM-3336 $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); // when custom data is included in this page if (CRM_Utils_Array::value("hidden_custom", $_POST)) { CRM_Custom_Form_Customdata::preProcess($this); CRM_Custom_Form_Customdata::buildQuickForm($this); CRM_Custom_Form_Customdata::setDefaultValues($this); } }
public function preProcess() { $this->_contactId = $this->get('contactId'); $this->_contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'contact_type'); $this->_relationshipId = $this->get('id'); $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this); $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); $this->assign('display_name_a', $this->_display_name_a); // Set page title based on action switch ($this->_action) { case CRM_Core_Action::VIEW: CRM_Utils_System::setTitle(ts('View Relationship for %1', array(1 => $this->_display_name_a))); break; case CRM_Core_Action::ADD: CRM_Utils_System::setTitle(ts('Add Relationship for %1', array(1 => $this->_display_name_a))); break; case CRM_Core_Action::UPDATE: CRM_Utils_System::setTitle(ts('Edit Relationship for %1', array(1 => $this->_display_name_a))); break; case CRM_Core_Action::DELETE: CRM_Utils_System::setTitle(ts('Delete Relationship for %1', array(1 => $this->_display_name_a))); break; } $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); //get the relationship values. $this->_values = array(); if ($this->_relationshipId) { $params = array('id' => $this->_relationshipId); CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values); } if (!$this->_rtypeId) { $params = $this->controller->exportValues($this->_name); if (isset($params['relationship_type_id'])) { $this->_rtypeId = $params['relationship_type_id']; } elseif (!empty($this->_values)) { $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype; } } //get the relationship type id $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId); //get the relationship type if (!$this->_rtype) { $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); } //need to assign custom data type and subtype to the template - FIXME: explain why $this->assign('customDataType', 'Relationship'); $this->assign('customDataSubType', $this->_relationshipTypeId); $this->assign('entityID', $this->_relationshipId); //use name as it remain constant, CRM-3336 $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); // Current employer? if ($this->_action & CRM_Core_Action::UPDATE) { if ($this->_allRelationshipNames[$this->_relationshipTypeId]["name_a_b"] == 'Employee of') { $this->_isCurrentEmployer = $this->_values['contact_id_b'] == CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_values['contact_id_a'], 'employer_id'); } } // when custom data is included in this page if (!empty($_POST['hidden_custom'])) { CRM_Custom_Form_CustomData::preProcess($this); CRM_Custom_Form_CustomData::buildQuickForm($this); CRM_Custom_Form_CustomData::setDefaultValues($this); } }
/** * @return array */ function &allRelationshipTypes() { if (self::$relationshipTypes === NULL) { $relationshipInfo = CRM_Core_PseudoConstant::relationshipType('label', TRUE); self::$relationshipTypes = array(); foreach ($relationshipInfo as $id => $info) { self::$relationshipTypes[$id] = $info[CRM_Case_XMLProcessor::REL_TYPE_CNAME]; } } return self::$relationshipTypes; }
/** * Class constructor */ public function __construct() { // initialize all the vars $this->numIndividual = self::INDIVIDUAL_PERCENT * self::NUM_CONTACT / 100; $this->numHousehold = self::HOUSEHOLD_PERCENT * self::NUM_CONTACT / 100; $this->numOrganization = self::ORGANIZATION_PERCENT * self::NUM_CONTACT / 100; $this->numStrictIndividual = $this->numIndividual - $this->numHousehold * self::NUM_INDIVIDUAL_PER_HOUSEHOLD; // Parse data file foreach ((array) simplexml_load_file(self::DATA_FILENAME) as $key => $val) { $val = (array) $val; $this->sampleData[$key] = (array) $val['item']; } // Init DB $config = CRM_Core_Config::singleton(); // Relationship types indexed by name_a_b from the table civicrm_relationship_type $this->relTypes = CRM_Utils_Array::index(array('name_a_b'), CRM_Core_PseudoConstant::relationshipType('name')); }
/** * Function to get individual id for onbehalf contribution * @param int $contributionId contribution id * @param int $contributorId contributer id * @return array $ids containing organization id and individual id * @access public */ function getOnbehalfIds($contributionId, $contributorId = null) { $ids = array(); if (!$contributionId) { return $ids; } // fetch contributor id if null if (!$contributorId) { require_once 'CRM/Core/DAO.php'; $contributorId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'contact_id'); } require_once 'CRM/Core/PseudoConstant.php'; $activityTypeIds = CRM_Core_PseudoConstant::activityType(true, false, false, 'name'); $activityTypeId = array_search("Contribution", $activityTypeIds); if ($activityTypeId && $contributorId) { $activityQuery = "\nSELECT source_contact_id \n FROM civicrm_activity \n WHERE activity_type_id = %1 \n AND source_record_id = %2"; $params = array(1 => array($activityTypeId, 'Integer'), 2 => array($contributionId, 'Integer')); $sourceContactId = CRM_Core_DAO::singleValueQuery($activityQuery, $params); // for on behalf contribution source is individual and contributor is organization if ($sourceContactId && $sourceContactId != $contributorId) { $relationshipTypeIds = CRM_Core_PseudoConstant::relationshipType('name'); // get rel type id for employee of relation foreach ($relationshipTypeIds as $id => $typeVals) { if ($typeVals['name_a_b'] == 'Employee of') { $relationshipTypeId = $id; break; } } require_once 'CRM/Contact/DAO/Relationship.php'; $rel = new CRM_Contact_DAO_Relationship(); $rel->relationship_type_id = $relationshipTypeId; $rel->contact_id_a = $sourceContactId; $rel->contact_id_b = $contributorId; if ($rel->find(true)) { $ids['individual_id'] = $rel->contact_id_a; $ids['organization_id'] = $rel->contact_id_b; } } } return $ids; }
static function getValidContactTypeList($relType) { // string looks like 4_a_b $rel_parts = explode('_', $relType); $allRelationshipType = CRM_Core_PseudoConstant::relationshipType('label'); $contactProfiles = CRM_Core_BAO_UFGroup::getReservedProfiles('Contact', NULL); if ($rel_parts[1] == 'a') { $leftType = $allRelationshipType[$rel_parts[0]]['contact_type_b']; } else { $leftType = $allRelationshipType[$rel_parts[0]]['contact_type_a']; } // Handle 'All Contacts' contact type for left side of relationship ($leftType is empty in this case) // In this case all reserved profiles are available if ($leftType == '') { $contactTypes = $contactProfiles; } else { $contactTypes = array(); foreach ($contactProfiles as $key => $value) { $groupTypes = CRM_Core_BAO_UFGroup::profileGroups($key); if (in_array($leftType, $groupTypes)) { $contactTypes = array($key => $value); } } } return $contactTypes; }
static function basic(&$form) { $form->addElement('hidden', 'hidden_basic', 1); if ($form->_searchOptions['contactType']) { // add checkboxes for contact type $contact_type = array(); $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(); if ($contactTypes) { $form->add('select', 'contact_type', ts('Contact Type(s)'), $contactTypes, FALSE, array('id' => 'contact_type', 'multiple' => 'multiple', 'title' => ts('- select -'))); } } if ($form->_searchOptions['groups']) { // multiselect for groups if ($form->_group) { $form->add('select', 'group', ts('Groups'), $form->_group, FALSE, array('id' => 'group', 'multiple' => 'multiple', 'title' => ts('- select -'))); } } if ($form->_searchOptions['tags']) { // multiselect for categories $contactTags = CRM_Core_BAO_Tag::getTags(); if ($contactTags) { $form->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE, array('id' => 'contact_tags', 'multiple' => 'multiple', 'title' => ts('- select -'))); } $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE, TRUE); } // add text box for last name, first name, street name, city $form->addElement('text', 'sort_name', ts('Find...'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); // add text box for last name, first name, street name, city $form->add('text', 'email', ts('Contact Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); //added contact source $form->add('text', 'contact_source', ts('Contact Source'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'source')); //added job title $attributes['job_title']['size'] = 30; $form->addElement('text', 'job_title', ts('Job Title'), $attributes['job_title'], 'size="30"'); //added internal ID $attributes['id']['size'] = 30; $form->addElement('text', 'id', ts('Contact ID'), $attributes['id'], 'size="30"'); //added external ID $attributes['external_identifier']['size'] = 30; $form->addElement('text', 'external_identifier', ts('External ID'), $attributes['external_identifier'], 'size="30"'); $config = CRM_Core_Config::singleton(); if (CRM_Core_Permission::check('access deleted contacts') and $config->contactUndelete) { $form->add('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)')); } // add checkbox for cms users only $form->addYesNo('uf_user', ts('CMS User?')); // tag all search $form->add('text', 'tag_search', ts('All Tags')); // add search profiles // FIXME: This is probably a part of profiles - need to be // FIXME: eradicated from here when profiles are reworked. $types = array('Participant', 'Contribution', 'Membership'); // get component profiles $componentProfiles = array(); $componentProfiles = CRM_Core_BAO_UFGroup::getProfiles($types); $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('Search Profile', 1); $accessibleUfGroups = CRM_Core_Permission::ufGroup(CRM_Core_Permission::VIEW); $searchProfiles = array(); foreach ($ufGroups as $key => $var) { if (!array_key_exists($key, $componentProfiles) && in_array($key, $accessibleUfGroups)) { $searchProfiles[$key] = $var['title']; } } $form->addElement('select', 'uf_group_id', ts('Search Views'), array('0' => ts('- default view -')) + $searchProfiles); $componentModes = CRM_Contact_Form_Search::getModeSelect(); // unset contributions or participants if user does not have // permission on them if (!CRM_Core_Permission::access('CiviContribute')) { unset($componentModes['2']); } if (!CRM_Core_Permission::access('CiviEvent')) { unset($componentModes['3']); } if (!CRM_Core_Permission::access('CiviMember')) { unset($componentModes['5']); } if (!CRM_Core_Permission::check('view all activities')) { unset($componentModes['4']); } if (count($componentModes) > 1) { $form->addElement('select', 'component_mode', ts('Display Results As'), $componentModes); } $form->addElement('select', 'operator', ts('Search Operator'), array('AND' => ts('AND'), 'OR' => ts('OR'))); // add the option to display relationships $rTypes = CRM_Core_PseudoConstant::relationshipType(); $rSelect = array('' => ts('- Select Relationship Type-')); foreach ($rTypes as $rid => $rValue) { if ($rValue['label_a_b'] == $rValue['label_b_a']) { $rSelect[$rid] = $rValue['label_a_b']; } else { $rSelect["{$rid}_a_b"] = $rValue['label_a_b']; $rSelect["{$rid}_b_a"] = $rValue['label_b_a']; } } $form->addElement('select', 'display_relationship_type', ts('Display Results as Relationship'), $rSelect); // checkboxes for DO NOT phone, email, mail // we take labels from SelectValues $t = CRM_Core_SelectValues::privacy(); $form->add('select', 'privacy_options', ts('Privacy'), $t, FALSE, array('id' => 'privacy_options', 'multiple' => 'multiple', 'title' => ts('- select -'))); $form->addElement('select', 'privacy_operator', ts('Operator'), array('OR' => ts('OR'), 'AND' => ts('AND'))); $toggleChoice = array(); $toggleChoice[] = $form->createElement('radio', NULL, '', ' ' . ts('Exclude'), '1'); $toggleChoice[] = $form->createElement('radio', NULL, '', ' ' . ts('Include by Privacy Option(s)'), '2'); $form->addGroup($toggleChoice, 'privacy_toggle', 'Privacy Options'); // preferred communication method $comm = CRM_Core_PseudoConstant::pcm(); $commPreff = array(); foreach ($comm as $k => $v) { $commPreff[] = $form->createElement('advcheckbox', $k, NULL, $v); } $onHold[] = $form->createElement('advcheckbox', 'on_hold', NULL, ts('')); $form->addGroup($onHold, 'email_on_hold', ts('Email On Hold')); $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Communication Method')); //CRM-6138 Preferred Language $langPreff = CRM_Core_PseudoConstant::languages(); $form->add('select', 'preferred_language', ts('Preferred Language'), array('' => ts('- select language -')) + $langPreff); }
/** * @param CRM_Core_Form $form */ public static function basic(&$form) { $form->addElement('hidden', 'hidden_basic', 1); if ($form->_searchOptions['contactType']) { // add checkboxes for contact type //@todo FIXME - using the CRM_Core_DAO::VALUE_SEPARATOR creates invalid html - if you can find the form // this is loaded onto then replace with something like '__' & test $separator = CRM_Core_DAO::VALUE_SEPARATOR; $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(FALSE, TRUE, $separator); if ($contactTypes) { $form->add('select', 'contact_type', ts('Contact Type(s)'), $contactTypes, FALSE, array('id' => 'contact_type', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;')); } } if ($form->_searchOptions['groups']) { // multiselect for groups if ($form->_group) { // Arrange groups into hierarchical listing (child groups follow their parents and have indentation spacing in title) $groupHierarchy = CRM_Contact_BAO_Group::getGroupsHierarchy($form->_group, NULL, ' ', TRUE); $form->add('select', 'group', ts('Groups'), $groupHierarchy, FALSE, array('id' => 'group', 'multiple' => 'multiple', 'class' => 'crm-select2')); $groupOptions = CRM_Core_BAO_OptionValue::getOptionValuesAssocArrayFromName('group_type'); $form->add('select', 'group_type', ts('Group Types'), $groupOptions, FALSE, array('id' => 'group_type', 'multiple' => 'multiple', 'class' => 'crm-select2')); $form->add('hidden', 'group_search_selected', 'group'); } } if ($form->_searchOptions['tags']) { // multiselect for categories $contactTags = CRM_Core_BAO_Tag::getTags(); if ($contactTags) { $form->add('select', 'contact_tags', ts('Tags'), $contactTags, FALSE, array('id' => 'contact_tags', 'multiple' => 'multiple', 'class' => 'crm-select2', 'style' => 'width: 100%;')); } $parentNames = CRM_Core_BAO_Tag::getTagSet('civicrm_contact'); CRM_Core_Form_Tag::buildQuickForm($form, $parentNames, 'civicrm_contact', NULL, TRUE, FALSE); $used_for = CRM_Core_OptionGroup::values('tag_used_for'); $tagsTypes = array(); $showAllTagTypes = FALSE; foreach ($used_for as $key => $value) { //check tags for every type and find if there are any defined $tags = CRM_Core_BAO_Tag::getTagsUsedFor($key, FALSE, TRUE, NULL); // check if there are tags other than contact type, if no - keep checkbox hidden on adv search // we will hide searching contact by attachments tags until it will be implemented in core if (count($tags) && $key != 'civicrm_file' && $key != 'civicrm_contact') { //if tags exists then add type to display in adv search form help text $tagsTypes[] = ts($value); $showAllTagTypes = TRUE; } } $tagTypesText = implode(" or ", $tagsTypes); if ($showAllTagTypes) { $form->add('checkbox', 'all_tag_types', ts('Include tags used for %1', array(1 => $tagTypesText))); $form->add('hidden', 'tag_types_text', $tagTypesText); } } // add text box for last name, first name, street name, city $form->addElement('text', 'sort_name', ts('Find...'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); // add text box for last name, first name, street name, city $form->add('text', 'email', ts('Contact Email'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'sort_name')); //added contact source $form->add('text', 'contact_source', ts('Contact Source'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'contact_source')); //added job title $form->addElement('text', 'job_title', ts('Job Title'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'job_title')); //added internal ID $form->addElement('text', 'contact_id', ts('Contact ID'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'id')); $form->addRule('contact_id', ts('Please enter valid Contact ID'), 'positiveInteger'); //added external ID $form->addElement('text', 'external_identifier', ts('External ID'), CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Contact', 'external_identifier')); if (CRM_Core_Permission::check('access deleted contacts') and CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete', NULL)) { $form->add('checkbox', 'deleted_contacts', ts('Search in Trash') . '<br />' . ts('(deleted contacts)')); } // add checkbox for cms users only $form->addYesNo('uf_user', ts('CMS User?'), TRUE); // tag all search $form->add('text', 'tag_search', ts('All Tags')); // add search profiles // FIXME: This is probably a part of profiles - need to be // FIXME: eradicated from here when profiles are reworked. $types = array('Participant', 'Contribution', 'Membership'); // get component profiles $componentProfiles = array(); $componentProfiles = CRM_Core_BAO_UFGroup::getProfiles($types); $ufGroups = CRM_Core_BAO_UFGroup::getModuleUFGroup('Search Profile', 1); $accessibleUfGroups = CRM_Core_Permission::ufGroup(CRM_Core_Permission::VIEW); $searchProfiles = array(); foreach ($ufGroups as $key => $var) { if (!array_key_exists($key, $componentProfiles) && in_array($key, $accessibleUfGroups)) { $searchProfiles[$key] = $var['title']; } } $form->add('select', 'uf_group_id', ts('Search Views'), array('0' => ts('- default view -')) + $searchProfiles, FALSE, array('class' => 'crm-select2')); $componentModes = CRM_Contact_Form_Search::getModeSelect(); // unset contributions or participants if user does not have // permission on them if (!CRM_Core_Permission::access('CiviContribute')) { unset($componentModes['2']); } if (!CRM_Core_Permission::access('CiviEvent')) { unset($componentModes['3']); } if (!CRM_Core_Permission::access('CiviMember')) { unset($componentModes['5']); } if (!CRM_Core_Permission::check('view all activities')) { unset($componentModes['4']); } if (count($componentModes) > 1) { $form->add('select', 'component_mode', ts('Display Results As'), $componentModes, FALSE, array('class' => 'crm-select2')); } $form->addRadio('operator', ts('Search Operator'), array('AND' => ts('AND'), 'OR' => ts('OR')), array('allowClear' => FALSE)); // add the option to display relationships $rTypes = CRM_Core_PseudoConstant::relationshipType(); $rSelect = array('' => ts('- Select Relationship Type-')); foreach ($rTypes as $rid => $rValue) { if ($rValue['label_a_b'] == $rValue['label_b_a']) { $rSelect[$rid] = $rValue['label_a_b']; } else { $rSelect["{$rid}_a_b"] = $rValue['label_a_b']; $rSelect["{$rid}_b_a"] = $rValue['label_b_a']; } } $form->addElement('select', 'display_relationship_type', ts('Display Results as Relationship'), $rSelect, array('class' => 'crm-select2')); // checkboxes for DO NOT phone, email, mail // we take labels from SelectValues $t = CRM_Core_SelectValues::privacy(); $form->add('select', 'privacy_options', ts('Privacy'), $t, FALSE, array('id' => 'privacy_options', 'multiple' => 'multiple', 'class' => 'crm-select2')); $form->addElement('select', 'privacy_operator', ts('Operator'), array('OR' => ts('OR'), 'AND' => ts('AND'))); $options = array(1 => ts('Exclude'), 2 => ts('Include by Privacy Option(s)')); $form->addRadio('privacy_toggle', ts('Privacy Options'), $options, array('allowClear' => FALSE)); // preferred communication method $comm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); $commPreff = array(); foreach ($comm as $k => $v) { $commPreff[] = $form->createElement('advcheckbox', $k, NULL, $v); } $onHold[] = $form->createElement('advcheckbox', 'on_hold', NULL, ''); $form->addGroup($onHold, 'email_on_hold', ts('Email On Hold')); $form->addGroup($commPreff, 'preferred_communication_method', ts('Preferred Communication Method')); //CRM-6138 Preferred Language $form->addSelect('preferred_language', array('class' => 'twenty', 'context' => 'search')); // Phone search $form->addElement('text', 'phone_numeric', ts('Phone Number'), CRM_Core_DAO::getAttribute('CRM_Core_DAO_Phone', 'phone')); $locationType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Address', 'location_type_id'); $phoneType = CRM_Core_PseudoConstant::get('CRM_Core_DAO_Phone', 'phone_type_id'); $form->add('select', 'phone_location_type_id', ts('Phone Location'), array('' => ts('- any -')) + $locationType, FALSE, array('class' => 'crm-select2')); $form->add('select', 'phone_phone_type_id', ts('Phone Type'), array('' => ts('- any -')) + $phoneType, FALSE, array('class' => 'crm-select2')); }
static function getValidContactTypeList($relType) { // string looks like 4_a_b $rel_parts = explode('_', $relType); $allRelationshipType = CRM_Core_PseudoConstant::relationshipType('label'); $contactProfiles = CRM_Core_BAO_UFGroup::getReservedProfiles('Contact', NULL); if ($rel_parts[1] == 'a') { $leftType = $allRelationshipType[$rel_parts[0]]['contact_type_b']; } else { $leftType = $allRelationshipType[$rel_parts[0]]['contact_type_a']; } $contactTypes = array(); foreach ($contactProfiles as $key => $value) { if (strpos($value, $leftType) !== FALSE) { $contactTypes = array($key => $value); } } return $contactTypes; }
/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params Associative array of property name/value * pairs to insert in new contact. * @param array $values The reformatted properties that we can use internally * ' * * @throws Exception * @return array|CRM_Error * @access public */ function _civicrm_api3_relationship_format_params($params, &$values) { // copy all the relationship fields as is $fields = CRM_Contact_DAO_Relationship::fields(); _civicrm_api3_store_values($fields, $params, $values); $relationTypes = CRM_Core_PseudoConstant::relationshipType('name'); if (!empty($params['id'])) { $relation = new CRM_Contact_BAO_Relationship(); $relation->id = $params['id']; if (!$relation->find(TRUE)) { throw new Exception('Relationship id is not valid'); } else { if (isset($params['contact_id_a']) && $params['contact_id_a'] != $relation->contact_id_a || isset($params['contact_id_b']) && $params['contact_id_b'] != $relation->contact_id_b) { throw new Exception('Cannot change the contacts once relationship has been created'); } else { // since the BAO function is not std & won't accept just 'id' (aargh) let's // at least return our BAO here $values = array(); _civicrm_api3_object_to_array($relation, $values); $values = array_merge($values, $params); // and we need to reformat our date fields.... $dateFields = array('start_date', 'end_date'); foreach ($dateFields as $dateField) { $values[$dateField] = CRM_Utils_Date::processDate($values[$dateField]); } } } } foreach ($params as $key => $value) { // ignore empty values or empty arrays etc if (CRM_Utils_System::isNull($value)) { continue; } switch ($key) { case 'contact_id_a': case 'contact_id_b': if (!CRM_Utils_Rule::integer($value)) { throw new Exception("contact_id not valid: {$value}"); } $dao = new CRM_Core_DAO(); $qParams = array(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}", $qParams); if (!$svq) { throw new Exception("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } break; case 'relationship_type': foreach ($relationTypes as $relTypId => $relValue) { if (CRM_Utils_Array::key(ucfirst($value), $relValue)) { $relationshipTypeId = $relTypId; break; } } if ($relationshipTypeId) { if (!empty($values['relationship_type_id']) && $relationshipTypeId != $values['relationship_type_id']) { throw new Exception('Mismatched Relationship Type and Relationship Type Id'); } $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId; } else { throw new Exception('Invalid Relationship Type'); } case 'relationship_type_id': if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) { throw new Exception("{$key} not a valid: {$value}"); } // execute for both relationship_type and relationship_type_id $relation = $relationTypes[$params['relationship_type_id']]; if (!empty($params['contact_id_a']) && $relation['contact_type_a'] && $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) { throw new Exception("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}"); } if (!empty($params['contact_id_b']) && $relation['contact_type_b'] && $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])) { throw new Exception("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}"); } break; default: break; } } if (array_key_exists('note', $params)) { $values['note'] = $params['note']; } _civicrm_api3_custom_format_params($params, $values, 'Relationship'); return array(); }
/** * take the input parameter list as specified in the data model and * convert it into the same format that we use in QF and BAO object * * @param array $params Associative array of property name/value * pairs to insert in new contact. * @param array $values The reformatted properties that we can use internally * ' * * @return array|CRM_Error * @access public */ function _civicrm_relationship_format_params(&$params, &$values) { // copy all the relationship fields as is $fields = CRM_Contact_DAO_Relationship::fields(); _civicrm_store_values($fields, $params, $values); $relationTypes = CRM_Core_PseudoConstant::relationshipType('name', TRUE); foreach ($params as $key => $value) { // ignore empty values or empty arrays etc require_once 'CRM/Utils/System.php'; if (CRM_Utils_System::isNull($value)) { continue; } switch ($key) { case 'contact_id_a': case 'contact_id_b': require_once 'CRM/Utils/Rule.php'; if (!CRM_Utils_Rule::integer($value)) { return civicrm_create_error("contact_id not valid: {$value}"); } $dao = new CRM_Core_DAO(); $qParams = array(); $svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = {$value}", $qParams); if (!$svq) { return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = {$value}."); } break; case 'start_date': case 'end_date': if (!CRM_Utils_Rule::qfDate($value)) { return civicrm_create_error("{$key} not a valid date: {$value}"); } break; case 'relationship_type': foreach ($relationTypes as $relTypId => $relValue) { if (CRM_Utils_Array::key(ucfirst($value), $relValue)) { $relationshipTypeId = $relTypId; break; } } if ($relationshipTypeId) { if (CRM_Utils_Array::value('relationship_type_id', $values) && $relationshipTypeId != $values['relationship_type_id']) { return civicrm_create_error('Mismatched Relationship Type and Relationship Type Id'); } $values['relationship_type_id'] = $params['relationship_type_id'] = $relationshipTypeId; } else { return civicrm_create_error('Invalid Relationship Type'); } case 'relationship_type_id': if ($key == 'relationship_type_id' && !array_key_exists($value, $relationTypes)) { return civicrm_create_error("{$key} not a valid: {$value}"); } // execute for both relationship_type and relationship_type_id $relation = $relationTypes[$params['relationship_type_id']]; require_once 'CRM/Contact/BAO/Contact.php'; if ($relation['contact_type_a'] && $relation['contact_type_a'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_a'])) { return civicrm_create_error("Contact ID :{$params['contact_id_a']} is not of contact type {$relation['contact_type_a']}"); } if ($relation['contact_type_b'] && $relation['contact_type_b'] != CRM_Contact_BAO_Contact::getContactType($params['contact_id_b'])) { return civicrm_create_error("Contact ID :{$params['contact_id_b']} is not of contact type {$relation['contact_type_b']}"); } break; default: break; } } if (array_key_exists('note', $params)) { $values['note'] = $params['note']; } _civicrm_custom_format_params($params, $values, 'Relationship'); return array(); }
/** * Handle the values in import mode. * * @param int $onDuplicate * The code for what action to take on duplicates. * @param array $values * The array of values belonging to this line. * * @param bool $doGeocodeAddress * * @return bool * the result of this processing */ public function import($onDuplicate, &$values, $doGeocodeAddress = FALSE) { $config = CRM_Core_Config::singleton(); $this->_unparsedStreetAddressContacts = array(); if (!$doGeocodeAddress) { // CRM-5854, reset the geocode method to null to prevent geocoding $config->geocodeMethod = NULL; } // first make sure this is a valid line //$this->_updateWithId = false; $response = $this->summary($values); $statusFieldName = $this->_statusFieldName; if ($response != CRM_Import_Parser::VALID) { $importRecordParams = array($statusFieldName => 'INVALID', "{$statusFieldName}Msg" => "Invalid (Error Code: {$response})"); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return $response; } $params =& $this->getActiveFieldParams(); $formatted = array('contact_type' => $this->_contactType); static $contactFields = NULL; if ($contactFields == NULL) { $contactFields = CRM_Contact_DAO_Contact::import(); } //check if external identifier exists in database if (!empty($params['external_identifier']) && (!empty($params['id']) || in_array($onDuplicate, array(CRM_Import_Parser::DUPLICATE_SKIP, CRM_Import_Parser::DUPLICATE_NOCHECK)))) { if ($internalCid = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['external_identifier'], 'id', 'external_identifier')) { if ($internalCid != CRM_Utils_Array::value('id', $params)) { $errorMessage = ts('External ID already exists in Database.'); array_unshift($values, $errorMessage); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::DUPLICATE; } } } if (!empty($this->_contactSubType)) { $params['contact_sub_type'] = $this->_contactSubType; } if ($subType = CRM_Utils_Array::value('contact_sub_type', $params)) { if (CRM_Contact_BAO_ContactType::isExtendsContactType($subType, $this->_contactType, FALSE, 'label')) { $subTypes = CRM_Contact_BAO_ContactType::subTypePairs($this->_contactType, FALSE, NULL); $params['contact_sub_type'] = array_search($subType, $subTypes); } elseif (!CRM_Contact_BAO_ContactType::isExtendsContactType($subType, $this->_contactType)) { $message = "Mismatched or Invalid Contact Subtype."; array_unshift($values, $message); return CRM_Import_Parser::NO_MATCH; } } // Get contact id to format common data in update/fill mode, // prioritising a dedupe rule check over an external_identifier check, but falling back on ext id. if ($this->_updateWithId && empty($params['id'])) { $possibleMatches = $this->getPossibleContactMatches($params); foreach ($possibleMatches as $possibleID) { $params['id'] = $formatted['id'] = $possibleID; } } //format common data, CRM-4062 $this->formatCommonData($params, $formatted, $contactFields); $relationship = FALSE; $createNewContact = TRUE; // Support Match and Update Via Contact ID if ($this->_updateWithId && isset($params['id'])) { $createNewContact = FALSE; // @todo - it feels like all the rows from here to the end of the IF // could be removed in favour of a simple check for whether the contact_type & id match // the call to the deprecated function seems to add no value other that to do an additional // check for the contact_id & type. $error = _civicrm_api3_deprecated_duplicate_formatted_contact($formatted); if (CRM_Core_Error::isAPIError($error, CRM_Core_ERROR::DUPLICATE_CONTACT)) { $matchedIDs = explode(',', $error['error_message']['params'][0]); if (count($matchedIDs) >= 1) { $updateflag = TRUE; foreach ($matchedIDs as $contactId) { if ($params['id'] == $contactId) { $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type'); if ($formatted['contact_type'] == $contactType) { //validation of subtype for update mode //CRM-5125 $contactSubType = NULL; if (!empty($params['contact_sub_type'])) { $contactSubType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type'); } if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array::value('contact_sub_type', $formatted))) { $message = "Mismatched contact SubTypes :"; array_unshift($values, $message); $updateflag = FALSE; $this->_retCode = CRM_Import_Parser::NO_MATCH; } else { $updateflag = FALSE; $this->_retCode = CRM_Import_Parser::VALID; } } else { $message = "Mismatched contact Types :"; array_unshift($values, $message); $updateflag = FALSE; $this->_retCode = CRM_Import_Parser::NO_MATCH; } } } if ($updateflag) { $message = "Mismatched contact IDs OR Mismatched contact Types :"; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } } } else { $contactType = NULL; if (!empty($params['id'])) { $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type'); if ($contactType) { if ($formatted['contact_type'] == $contactType) { //validation of subtype for update mode //CRM-5125 $contactSubType = NULL; if (!empty($params['contact_sub_type'])) { $contactSubType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type'); } if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType) && $contactSubType != CRM_Utils_Array::value('contact_sub_type', $formatted))) { $message = "Mismatched contact SubTypes :"; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } else { $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $params['id'], FALSE, $this->_dedupeRuleGroupID); $this->_retCode = CRM_Import_Parser::VALID; } } else { $message = "Mismatched contact Types :"; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } } else { // we should avoid multiple errors for single record // since we have already retCode and we trying to force again. if ($this->_retCode != CRM_Import_Parser::NO_MATCH) { $message = "No contact found for this contact ID:" . $params['id']; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } } } else { //CRM-4148 //now we want to create new contact on update/fill also. $createNewContact = TRUE; } } if (isset($newContact) && is_a($newContact, 'CRM_Contact_BAO_Contact')) { $relationship = TRUE; } elseif (is_a($error, 'CRM_Core_Error')) { $newContact = $error; $relationship = TRUE; } } //fixed CRM-4148 //now we create new contact in update/fill mode also. $contactID = NULL; if ($createNewContact || $this->_retCode != CRM_Import_Parser::NO_MATCH && $this->_updateWithId) { //CRM-4430, don't carry if not submitted. foreach (array('prefix_id', 'suffix_id', 'gender_id') as $name) { if (!empty($formatted[$name])) { $options = CRM_Contact_BAO_Contact::buildOptions($name, 'get'); if (!isset($options[$formatted[$name]])) { $formatted[$name] = CRM_Utils_Array::key((string) $formatted[$name], $options); } } } if ($this->_updateWithId && !empty($params['id'])) { $contactID = $params['id']; } $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactID, TRUE, $this->_dedupeRuleGroupID); } if (isset($newContact) && is_object($newContact) && $newContact instanceof CRM_Contact_BAO_Contact) { $relationship = TRUE; $newContact = clone $newContact; $contactID = $newContact->id; $this->_newContacts[] = $contactID; //get return code if we create new contact in update mode, CRM-4148 if ($this->_updateWithId) { $this->_retCode = CRM_Import_Parser::VALID; } } elseif (isset($newContact) && CRM_Core_Error::isAPIError($newContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { // if duplicate, no need of further processing if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { $errorMessage = "Skipping duplicate record"; array_unshift($values, $errorMessage); $importRecordParams = array($statusFieldName => 'DUPLICATE', "{$statusFieldName}Msg" => $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::DUPLICATE; } $relationship = TRUE; # see CRM-10433 - might return comma separate list of all dupes $dupeContactIDs = explode(',', $newContact['error_message']['params'][0]); $dupeCount = count($dupeContactIDs); $contactID = array_pop($dupeContactIDs); // check to see if we had more than one duplicate contact id. // if we have more than one, the record will be rejected below if ($dupeCount == 1) { // there was only one dupe, we will continue normally... if (!in_array($contactID, $this->_newContacts)) { $this->_newContacts[] = $contactID; } } } if ($contactID) { // call import hook $currentImportID = end($values); $hookParams = array('contactID' => $contactID, 'importID' => $currentImportID, 'importTempTable' => $this->_tableName, 'fieldHeaders' => $this->_mapperKeys, 'fields' => $this->_activeFields); CRM_Utils_Hook::import('Contact', 'process', $this, $hookParams); } if ($relationship) { $primaryContactId = NULL; if (CRM_Core_Error::isAPIError($newContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { if (CRM_Utils_Rule::integer($newContact['error_message']['params'][0])) { $primaryContactId = $newContact['error_message']['params'][0]; } } else { $primaryContactId = $newContact->id; } if ((CRM_Core_Error::isAPIError($newContact, CRM_Core_ERROR::DUPLICATE_CONTACT) || is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) { //relationship contact insert foreach ($params as $key => $field) { list($id, $first, $second) = CRM_Utils_System::explode('_', $key, 3); if (!($first == 'a' && $second == 'b') && !($first == 'b' && $second == 'a')) { continue; } $relationType = new CRM_Contact_DAO_RelationshipType(); $relationType->id = $id; $relationType->find(TRUE); $direction = "contact_sub_type_{$second}"; $formatting = array('contact_type' => $params[$key]['contact_type']); //set subtype for related contact CRM-5125 if (isset($relationType->{$direction})) { //validation of related contact subtype for update mode if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $params[$key]) && $relCsType != $relationType->{$direction}) { $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact."); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } else { $formatting['contact_sub_type'] = $relationType->{$direction}; } } $relationType->free(); $contactFields = NULL; $contactFields = CRM_Contact_DAO_Contact::import(); //Relation on the basis of External Identifier. if (empty($params[$key]['id']) && !empty($params[$key]['external_identifier'])) { $params[$key]['id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['external_identifier'], 'id', 'external_identifier'); } // check for valid related contact id in update/fill mode, CRM-4424 if (in_array($onDuplicate, array(CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::DUPLICATE_FILL)) && !empty($params[$key]['id'])) { $relatedContactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type'); if (!$relatedContactType) { $errorMessage = ts("No contact found for this related contact ID: %1", array(1 => $params[$key]['id'])); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } else { //validation of related contact subtype for update mode //CRM-5125 $relatedCsType = NULL; if (!empty($formatting['contact_sub_type'])) { $relatedCsType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_sub_type'); } if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params[$key]['id'], $relatedCsType) && $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting))) { $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact.") . ' ' . ts("ID: %1", array(1 => $params[$key]['id'])); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } else { // get related contact id to format data in update/fill mode, //if external identifier is present, CRM-4423 $formatting['id'] = $params[$key]['id']; } } } //format common data, CRM-4062 $this->formatCommonData($field, $formatting, $contactFields); //do we have enough fields to create related contact. $allowToCreate = $this->checkRelatedContactFields($key, $formatting); if (!$allowToCreate) { $errorMessage = ts('Related contact required fields are missing.'); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } //fixed for CRM-4148 if (!empty($params[$key]['id'])) { $contact = array('contact_id' => $params[$key]['id']); $defaults = array(); $relatedNewContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); } else { $relatedNewContact = $this->createContact($formatting, $contactFields, $onDuplicate, NULL, FALSE); } if (is_object($relatedNewContact) || $relatedNewContact instanceof CRM_Contact_BAO_Contact) { $relatedNewContact = clone $relatedNewContact; } $matchedIDs = array(); // To update/fill contact, get the matching contact Ids if duplicate contact found // otherwise get contact Id from object of related contact if (is_array($relatedNewContact) && civicrm_error($relatedNewContact)) { if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { $matchedIDs = explode(',', $relatedNewContact['error_message']['params'][0]); } else { $errorMessage = $relatedNewContact['error_message']; array_unshift($values, $errorMessage); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::ERROR; } } else { $matchedIDs[] = $relatedNewContact->id; } // update/fill related contact after getting matching Contact Ids, CRM-4424 if (in_array($onDuplicate, array(CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::DUPLICATE_FILL))) { //validation of related contact subtype for update mode //CRM-5125 $relatedCsType = NULL; if (!empty($formatting['contact_sub_type'])) { $relatedCsType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $matchedIDs[0], 'contact_sub_type'); } if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($matchedIDs[0], $relatedCsType) && $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting))) { $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact."); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } else { $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]); } } static $relativeContact = array(); if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT)) { if (count($matchedIDs) >= 1) { $relContactId = $matchedIDs[0]; //add relative contact to count during update & fill mode. //logic to make count distinct by contact id. if ($this->_newRelatedContacts || !empty($relativeContact)) { $reContact = array_keys($relativeContact, $relContactId); if (empty($reContact)) { $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; } } else { $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; } } } else { $relContactId = $relatedNewContact->id; $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; } if (CRM_Core_Error::isAPIError($relatedNewContact, CRM_Core_ERROR::DUPLICATE_CONTACT) || $relatedNewContact instanceof CRM_Contact_BAO_Contact) { //fix for CRM-1993.Checks for duplicate related contacts if (count($matchedIDs) >= 1) { //if more than one duplicate contact //found, create relationship with first contact // now create the relationship record $relationParams = array(); $relationParams = array('relationship_type_id' => $key, 'contact_check' => array($relContactId => 1), 'is_active' => 1, 'skipRecentView' => TRUE); // we only handle related contact success, we ignore failures for now // at some point wold be nice to have related counts as separate $relationIds = array('contact' => $primaryContactId); list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::legacyCreateMultiple($relationParams, $relationIds); if ($valid || $duplicate) { $relationIds['contactTarget'] = $relContactId; $action = $duplicate ? CRM_Core_Action::UPDATE : CRM_Core_Action::ADD; CRM_Contact_BAO_Relationship::relatedMemberships($primaryContactId, $relationParams, $relationIds, $action); } //handle current employer, CRM-3532 if ($valid) { $allRelationships = CRM_Core_PseudoConstant::relationshipType('name'); $relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $key); $relationshipType = str_replace($relationshipTypeId . '_', '', $key); $orgId = $individualId = NULL; if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') { $orgId = $relContactId; $individualId = $primaryContactId; } elseif ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') { $orgId = $primaryContactId; $individualId = $relContactId; } if ($orgId && $individualId) { $currentEmpParams[$individualId] = $orgId; CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); } } } } } } } if ($this->_updateWithId) { //return warning if street address is unparsed, CRM-5886 return $this->processMessage($values, $statusFieldName, $this->_retCode); } //dupe checking if (is_array($newContact) && civicrm_error($newContact)) { $code = NULL; if (($code = CRM_Utils_Array::value('code', $newContact['error_message'])) && $code == CRM_Core_Error::DUPLICATE_CONTACT) { $urls = array(); // need to fix at some stage and decide if the error will return an // array or string, crude hack for now if (is_array($newContact['error_message']['params'][0])) { $cids = $newContact['error_message']['params'][0]; } else { $cids = explode(',', $newContact['error_message']['params'][0]); } foreach ($cids as $cid) { $urls[] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $cid, TRUE); } $url_string = implode("\n", $urls); // If we duplicate more than one record, skip no matter what if (count($cids) > 1) { $errorMessage = ts('Record duplicates multiple contacts'); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); //combine error msg to avoid mismatch between error file columns. $errorMessage .= "\n" . $url_string; array_unshift($values, $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::ERROR; } // Params only had one id, so shift it out $contactId = array_shift($cids); $cid = NULL; $vals = array('contact_id' => $contactId); if ($onDuplicate == CRM_Import_Parser::DUPLICATE_REPLACE) { civicrm_api('contact', 'delete', $vals); $cid = CRM_Contact_BAO_Contact::createProfileContact($formatted, $contactFields, $contactId, NULL, NULL, $formatted['contact_type']); } elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId); } elseif ($onDuplicate == CRM_Import_Parser::DUPLICATE_FILL) { $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId); } // else skip does nothing and just returns an error code. if ($cid) { $contact = array('contact_id' => $cid); $defaults = array(); $newContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); } if (civicrm_error($newContact)) { if (empty($newContact['error_message']['params'])) { // different kind of error other than DUPLICATE $errorMessage = $newContact['error_message']; array_unshift($values, $errorMessage); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::ERROR; } $contactID = $newContact['error_message']['params'][0]; if (!in_array($contactID, $this->_newContacts)) { $this->_newContacts[] = $contactID; } } //CRM-262 No Duplicate Checking if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { array_unshift($values, $url_string); $importRecordParams = array($statusFieldName => 'DUPLICATE', "{$statusFieldName}Msg" => "Skipping duplicate record"); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::DUPLICATE; } $importRecordParams = array($statusFieldName => 'IMPORTED'); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); //return warning if street address is not parsed, CRM-5886 return $this->processMessage($values, $statusFieldName, CRM_Import_Parser::VALID); } else { // Not a dupe, so we had an error $errorMessage = $newContact['error_message']; array_unshift($values, $errorMessage); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::ERROR; } } // sleep(3); return $this->processMessage($values, $statusFieldName, CRM_Import_Parser::VALID); }
/** * Get get list of relationship type based on the target contact type. * * @param string $targetContactType * It's valid contact tpye(may be Individual , Organization , Household). * * @return array * array reference of all relationship types with context to current contact type . */ public static function getRelationType($targetContactType) { $relationshipType = array(); $allRelationshipType = CRM_Core_PseudoConstant::relationshipType(); foreach ($allRelationshipType as $key => $type) { if ($type['contact_type_b'] == $targetContactType) { $relationshipType[$key . '_a_b'] = $type['label_a_b']; } } return $relationshipType; }
/** * @param $relationshipList * @return array */ public static function getRelationshipTypeMetadata($relationshipList) { $contactTypes = CRM_Contact_BAO_ContactType::contactTypeInfo(TRUE); $allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); $jsData = array(); // Get just what we need to keep the dom small $whatWeWant = array_flip(array('contact_type_a', 'contact_type_b', 'contact_sub_type_a', 'contact_sub_type_b')); foreach ($allRelationshipNames as $id => $vals) { if (isset($relationshipList["{$id}_a_b"]) || isset($relationshipList["{$id}_b_a"])) { $jsData[$id] = array_filter(array_intersect_key($allRelationshipNames[$id], $whatWeWant)); // Add user-friendly placeholder foreach (array('a', 'b') as $x) { $type = !empty($jsData[$id]["contact_sub_type_{$x}"]) ? $jsData[$id]["contact_sub_type_{$x}"] : CRM_Utils_Array::value("contact_type_{$x}", $jsData[$id]); $jsData[$id]["placeholder_{$x}"] = $type ? ts('- select %1 -', array(strtolower($contactTypes[$type]['label']))) : ts('- select contact -'); } } } return $jsData; }
function filterRelatedContacts(&$from, &$where, &$having) { static $_rTypeProcessed = NULL; static $_rTypeFrom = NULL; static $_rTypeWhere = NULL; if (!$_rTypeProcessed) { $_rTypeProcessed = TRUE; // create temp table with contact ids $tableName = CRM_Core_DAO::createTempTableName('civicrm_transform', TRUE); $sql = "CREATE TEMPORARY TABLE {$tableName} ( contact_id int primary key) ENGINE=HEAP"; CRM_Core_DAO::executeQuery($sql); $sql = "\nREPLACE INTO {$tableName} ( contact_id )\nSELECT contact_a.id\n {$from}\n {$where}\n {$having}\n"; CRM_Core_DAO::executeQuery($sql); $qillMessage = ts('Contacts with a Relationship Type of: '); $rTypes = CRM_Core_PseudoConstant::relationshipType(); if (is_numeric($this->_displayRelationshipType)) { $relationshipTypeLabel = $rTypes[$this->_displayRelationshipType]['label_a_b']; $_rTypeFrom = "\nINNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id OR displayRelType.contact_id_b = contact_a.id )\nINNER JOIN {$tableName} transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a OR transform_temp.contact_id = displayRelType.contact_id_b )\n"; $_rTypeWhere = "\nWHERE displayRelType.relationship_type_id = {$this->_displayRelationshipType}\nAND displayRelType.is_active = 1\n"; } else { list($relType, $dirOne, $dirTwo) = explode('_', $this->_displayRelationshipType); if ($dirOne == 'a') { $relationshipTypeLabel = $rTypes[$relType]['label_a_b']; $_rTypeFrom .= "\nINNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_a = contact_a.id )\nINNER JOIN {$tableName} transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_b )\n"; } else { $relationshipTypeLabel = $rTypes[$relType]['label_b_a']; $_rTypeFrom .= "\nINNER JOIN civicrm_relationship displayRelType ON ( displayRelType.contact_id_b = contact_a.id )\nINNER JOIN {$tableName} transform_temp ON ( transform_temp.contact_id = displayRelType.contact_id_a )\n"; } $_rTypeWhere = "\nWHERE displayRelType.relationship_type_id = {$relType}\nAND displayRelType.is_active = 1\n"; } $this->_qill[0][] = $qillMessage . "'" . $relationshipTypeLabel . "'"; } if (strpos($from, $_rTypeFrom) === FALSE) { // lets replace all the INNER JOIN's in the $from so we dont exclude other data // this happens when we have an event_type in the quert (CRM-7969) $from = str_replace("INNER JOIN", "LEFT JOIN", $from); $from .= $_rTypeFrom; $where = $_rTypeWhere; } $having = NULL; }
/** * @param $rows * * @return array */ public function statistics(&$rows) { $statistics = parent::statistics($rows); $isStatusFilter = FALSE; $relStatus = NULL; if (CRM_Utils_Array::value('is_active_value', $this->_params) == '1') { $relStatus = 'Is equal to Active'; } elseif (CRM_Utils_Array::value('is_active_value', $this->_params) == '0') { $relStatus = 'Is equal to Inactive'; } if (!empty($statistics['filters'])) { foreach ($statistics['filters'] as $id => $value) { //for displaying relationship type filter if ($value['title'] == 'Relationship') { $relTypes = CRM_Core_PseudoConstant::relationshipType(); $op = CRM_Utils_array::value('relationship_type_id_op', $this->_params) == 'in' ? ts('Is one of') . ' ' : ts('Is not one of') . ' '; $relationshipTypes = array(); foreach ($this->_params['relationship_type_id_value'] as $relationship) { $relationshipTypes[] = $relTypes[$relationship]['label_' . $this->relationType]; } $statistics['filters'][$id]['value'] = $op . implode(', ', $relationshipTypes); } //for displaying relationship status if ($value['title'] == 'Relationship Status') { $isStatusFilter = TRUE; $statistics['filters'][$id]['value'] = $relStatus; } } } //for displaying relationship status if (!$isStatusFilter && $relStatus) { $statistics['filters'][] = array('title' => 'Relationship Status', 'value' => $relStatus); } return $statistics; }
/** * Get individual id for onbehalf contribution. * * @param int $contributionId * Contribution id. * @param int $contributorId * Contributor id. * * @return array * containing organization id and individual id */ public static function getOnbehalfIds($contributionId, $contributorId = NULL) { $ids = array(); if (!$contributionId) { return $ids; } // fetch contributor id if null if (!$contributorId) { $contributorId = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution', $contributionId, 'contact_id'); } $activityTypeIds = CRM_Core_PseudoConstant::activityType(TRUE, FALSE, FALSE, 'name'); $activityTypeId = array_search('Contribution', $activityTypeIds); if ($activityTypeId && $contributorId) { $activityQuery = "\nSELECT civicrm_activity_contact.contact_id\n FROM civicrm_activity_contact\nINNER JOIN civicrm_activity ON civicrm_activity_contact.activity_id = civicrm_activity.id\n WHERE civicrm_activity.activity_type_id = %1\n AND civicrm_activity.source_record_id = %2\n AND civicrm_activity_contact.record_type_id = %3\n"; $activityContacts = CRM_Core_OptionGroup::values('activity_contacts', FALSE, FALSE, FALSE, NULL, 'name'); $sourceID = CRM_Utils_Array::key('Activity Source', $activityContacts); $params = array(1 => array($activityTypeId, 'Integer'), 2 => array($contributionId, 'Integer'), 3 => array($sourceID, 'Integer')); $sourceContactId = CRM_Core_DAO::singleValueQuery($activityQuery, $params); // for on behalf contribution source is individual and contributor is organization if ($sourceContactId && $sourceContactId != $contributorId) { $relationshipTypeIds = CRM_Core_PseudoConstant::relationshipType('name'); // get rel type id for employee of relation foreach ($relationshipTypeIds as $id => $typeVals) { if ($typeVals['name_a_b'] == 'Employee of') { $relationshipTypeId = $id; break; } } $rel = new CRM_Contact_DAO_Relationship(); $rel->relationship_type_id = $relationshipTypeId; $rel->contact_id_a = $sourceContactId; $rel->contact_id_b = $contributorId; if ($rel->find(TRUE)) { $ids['individual_id'] = $rel->contact_id_a; $ids['organization_id'] = $rel->contact_id_b; } } } return $ids; }
/** * @param $contactIds * * @return array */ public function buildRelationshipRows($contactIds) { $relationshipRows = $relatedContactIds = array(); if (empty($contactIds)) { return array($relationshipRows, $relatedContactIds); } $relContactAlias = 'contact_relationship'; $addRelSelect = ''; if (!empty($this->_relationshipColumns)) { $addRelSelect = ', ' . implode(', ', $this->_relationshipColumns); } $sqlRelationship = "SELECT {$this->_aliases['civicrm_relationship']}.relationship_type_id as relationship_type_id, {$this->_aliases['civicrm_relationship']}.contact_id_a as contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b as contact_id_b {$addRelSelect} FROM civicrm_contact {$relContactAlias} {$this->_relationshipFrom} WHERE {$relContactAlias}.id IN (" . implode(',', $contactIds) . ") AND {$this->_aliases['civicrm_relationship']}.is_active = 1 {$this->_relationshipWhere} GROUP BY {$this->_aliases['civicrm_relationship']}.contact_id_a, {$this->_aliases['civicrm_relationship']}.contact_id_b"; $relationshipTypes = CRM_Core_PseudoConstant::relationshipType(); $dao = CRM_Core_DAO::executeQuery($sqlRelationship); while ($dao->fetch()) { $row = array(); foreach (array_keys($this->_relationshipColumns) as $rel_column) { $row[$rel_column] = $dao->{$rel_column}; } if (in_array($dao->contact_id_a, $contactIds)) { $row['civicrm_relationship_relationship_type_id'] = $relationshipTypes[$dao->relationship_type_id]['label_a_b']; $row['civicrm_relationship_contact_id'] = $dao->contact_id_b; $relationshipRows[$dao->contact_id_a][$dao->contact_id_b] = $row; $relatedContactIds[$dao->contact_id_b] = $dao->contact_id_b; } if (in_array($dao->contact_id_b, $contactIds)) { $row['civicrm_relationship_contact_id'] = $dao->contact_id_a; $row['civicrm_relationship_relationship_type_id'] = $relationshipTypes[$dao->relationship_type_id]['label_b_a']; $relationshipRows[$dao->contact_id_b][$dao->contact_id_a] = $row; $relatedContactIds[$dao->contact_id_a] = $dao->contact_id_a; } } $dao->free(); return array($relationshipRows, $relatedContactIds); }
/** * handle the values in import mode * * @param int $onDuplicate the code for what action to take on duplicates * @param array $values the array of values belonging to this line * * @return boolean the result of this processing * @access public */ function import($onDuplicate, &$values, $doGeocodeAddress = false) { $config =& CRM_Core_Config::singleton(); $this->_unparsedStreetAddressContacts = array(); if (!$doGeocodeAddress) { // CRM-5854, reset the geocode method to null to prevent geocoding $config->geocodeMethod = null; } // first make sure this is a valid line //$this->_updateWithId = false; $response = $this->summary($values); $statusFieldName = $this->_statusFieldName; if ($response != CRM_Import_Parser::VALID) { $importRecordParams = array($statusFieldName => 'INVALID', "{$statusFieldName}Msg" => "Invalid (Error Code: {$response})"); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return $response; } $params =& $this->getActiveFieldParams(); $formatted = array('contact_type' => $this->_contactType); static $contactFields = null; if ($contactFields == null) { require_once "CRM/Contact/DAO/Contact.php"; $contactFields =& CRM_Contact_DAO_Contact::import(); } //check if external identifier exists in database if (CRM_Utils_Array::value('external_identifier', $params) && (CRM_Utils_Array::value('id', $params) || in_array($onDuplicate, array(CRM_Import_Parser::DUPLICATE_SKIP, CRM_Import_Parser::DUPLICATE_NOCHECK)))) { require_once "CRM/Contact/BAO/Contact.php"; if ($internalCid = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['external_identifier'], 'id', 'external_identifier')) { if ($internalCid != CRM_Utils_Array::value('id', $params)) { $errorMessage = ts('External Identifier already exists in database.'); array_unshift($values, $errorMessage); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::ERROR; } } } if (!empty($this->_contactSubType)) { $params['contact_sub_type'] = $this->_contactSubType; } if ($subType = CRM_Utils_Array::value('contact_sub_type', $params)) { if (CRM_Contact_BAO_ContactType::isExtendsContactType($subType, $this->_contactType, false, 'label')) { $subTypes = CRM_Contact_BAO_ContactType::subTypePairs($this->_contactType, false, null); $params['contact_sub_type'] = array_search($subType, $subTypes); } elseif (!CRM_Contact_BAO_ContactType::isExtendsContactType($subType, $this->_contactType)) { $message = "Mismatched or Invalid Contact SubType."; array_unshift($values, $message); return CRM_Import_Parser::NO_MATCH; } } //get contact id to format common data in update/fill mode, //if external identifier is present, CRM-4423 if ($this->_updateWithId && !CRM_Utils_Array::value('id', $params) && CRM_Utils_Array::value('external_identifier', $params)) { if ($cid = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['external_identifier'], 'id', 'external_identifier')) { $formatted['id'] = $cid; } } //format common data, CRM-4062 $this->formatCommonData($params, $formatted, $contactFields); $relationship = false; $createNewContact = true; // Support Match and Update Via Contact ID if ($this->_updateWithId) { $createNewContact = false; if (!CRM_Utils_Array::value('id', $params) && CRM_Utils_Array::value('external_identifier', $params)) { if ($cid) { $params['id'] = $cid; } else { //update contact if dedupe found contact id, CRM-4148 $dedupeParams = $formatted; //special case to check dedupe if external id present. //if we send external id dedupe will stop. unset($dedupeParams['external_identifier']); $checkDedupe = _civicrm_duplicate_formatted_contact($dedupeParams); if (civicrm_duplicate($checkDedupe)) { $matchingContactIds = explode(',', $checkDedupe['error_message']['params'][0]); if (count($matchingContactIds) == 1) { $params['id'] = array_pop($matchingContactIds); } else { $message = "More than one matching contact found for given criteria."; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } } else { $createNewContact = true; } } } $error = _civicrm_duplicate_formatted_contact($formatted); if (civicrm_duplicate($error)) { $matchedIDs = explode(',', $error['error_message']['params'][0]); if (count($matchedIDs) >= 1) { $updateflag = true; foreach ($matchedIDs as $contactId) { if ($params['id'] == $contactId) { $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type'); if ($formatted['contact_type'] == $contactType) { //validation of subtype for update mode //CRM-5125 $contactSubType = null; if (CRM_Utils_Array::value('contact_sub_type', $params)) { $contactSubType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type'); } if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType) || $contactSubType != CRM_Utils_Array::value('contact_sub_type', $formatted))) { $message = "Mismatched contact SubTypes :"; array_unshift($values, $message); $updateflag = false; $this->_retCode = CRM_Import_Parser::NO_MATCH; } else { $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId, false); $updateflag = false; $this->_retCode = CRM_Import_Parser::VALID; } } else { $message = "Mismatched contact Types :"; array_unshift($values, $message); $updateflag = false; $this->_retCode = CRM_Import_Parser::NO_MATCH; } } } if ($updateflag) { $message = "Mismatched contact IDs OR Mismatched contact Types :"; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } } } else { $contactType = null; if (CRM_Utils_Array::value('id', $params)) { $contactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_type'); if ($contactType) { if ($formatted['contact_type'] == $contactType) { //validation of subtype for update mode //CRM-5125 $contactSubType = null; if (CRM_Utils_Array::value('contact_sub_type', $params)) { $contactSubType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params['id'], 'contact_sub_type'); } if (!empty($contactSubType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params['id'], $contactSubType) || $contactSubType != CRM_Utils_Array::value('contact_sub_type', $formatted))) { $message = "Mismatched contact SubTypes :"; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } else { $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $params['id'], false); $this->_retCode = CRM_Import_Parser::VALID; } } else { $message = "Mismatched contact Types :"; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } } else { // we should avoid multiple errors for single record // since we have already retCode and we trying to force again. if ($this->_retCode != CRM_Import_Parser::NO_MATCH) { $message = "No contact found for this contact ID:" . $params['id']; array_unshift($values, $message); $this->_retCode = CRM_Import_Parser::NO_MATCH; } } } else { //CRM-4148 //now we want to create new contact on update/fill also. $createNewContact = true; } } if (is_a($newContact, 'CRM_Contact_BAO_Contact')) { $relationship = true; } else { if (is_a($error, 'CRM_Core_Error')) { $newContact = $error; $relationship = true; } } } //fixed CRM-4148 //now we create new contact in update/fill mode also. if ($createNewContact) { //CRM-4430, don't carry if not submitted. foreach (array('prefix', 'suffix', 'gender') as $name) { if (array_key_exists($name, $formatted)) { if (in_array($name, array('prefix', 'suffix'))) { $formattedName = "individual_{$name}"; $formatted[$formattedName] = CRM_Core_OptionGroup::getValue($formattedName, (string) $formatted[$name]); } else { $formatted[$name] = CRM_Core_OptionGroup::getValue($name, (string) $formatted[$name]); } } } $newContact = $this->createContact($formatted, $contactFields, $onDuplicate); } if (is_object($newContact) || $newContact instanceof CRM_Contact_BAO_Contact) { $relationship = true; $newContact = clone $newContact; $this->_newContacts[] = $newContact->id; //get return code if we create new contact in update mode, CRM-4148 if ($this->_updateWithId) { $this->_retCode = CRM_Import_Parser::VALID; } } else { if (civicrm_duplicate($newContact)) { $relationship = true; $contactID = $newContact['error_message']['params'][0]; if (!in_array($contactID, $this->_newContacts)) { $this->_newContacts[] = $contactID; } } } if ($relationship) { $primaryContactId = null; if (civicrm_duplicate($newContact)) { if (CRM_Utils_Rule::integer($newContact['error_message']['params'][0])) { $primaryContactId = $newContact['error_message']['params'][0]; } } else { $primaryContactId = $newContact->id; } if ((civicrm_duplicate($newContact) || is_a($newContact, 'CRM_Contact_BAO_Contact')) && $primaryContactId) { //relationship contact insert foreach ($params as $key => $field) { list($id, $first, $second) = CRM_Utils_System::explode('_', $key, 3); if (!($first == 'a' && $second == 'b') && !($first == 'b' && $second == 'a')) { continue; } $relationType = new CRM_Contact_DAO_RelationshipType(); $relationType->id = $id; $relationType->find(true); $direction = "contact_sub_type_{$second}"; $formatting = array('contact_type' => $params[$key]['contact_type']); //set subtype for related contact CRM-5125 if (isset($relationType->{$direction})) { //validation of related contact subtype for update mode if ($relCsType = CRM_Utils_Array::value('contact_sub_type', $params[$key]) && $relCsType != $relationType->{$direction}) { $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact"); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } else { $formatting['contact_sub_type'] = $relationType->{$direction}; } } $relationType->free(); $contactFields = null; $contactFields = CRM_Contact_DAO_Contact::import(); //Relation on the basis of External Identifier. if (!CRM_Utils_Array::value('id', $params[$key]) && isset($params[$key]['external_identifier'])) { $params[$key]['id'] = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['external_identifier'], 'id', 'external_identifier'); } // check for valid related contact id in update/fill mode, CRM-4424 if (in_array($onDuplicate, array(CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::DUPLICATE_FILL)) && CRM_Utils_Array::value('id', $params[$key])) { $relatedContactType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_type'); if (!$relatedContactType) { $errorMessage = ts("No contact found for this related contact ID: %1", array(1 => $params[$key]['id'])); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } else { //validation of related contact subtype for update mode //CRM-5125 $relatedCsType = null; if (CRM_Utils_Array::value('contact_sub_type', $formatting)) { $relatedCsType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$key]['id'], 'contact_sub_type'); } if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($params[$key]['id'], $relatedCsType) || $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting))) { $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact ID: %1", array(1 => $params[$key]['id'])); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } else { // get related contact id to format data in update/fill mode, //if external identifier is present, CRM-4423 $formatting['id'] = $params[$key]['id']; } } } //format common data, CRM-4062 $this->formatCommonData($field, $formatting, $contactFields); //fixed for CRM-4148 if ($params[$key]['id']) { $contact = array('contact_id' => $params[$key]['id']); $defaults = array(); $relatedNewContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); } else { $relatedNewContact = $this->createContact($formatting, $contactFields, $onDuplicate, null, false); } if (is_object($relatedNewContact) || $relatedNewContact instanceof CRM_Contact_BAO_Contact) { $relatedNewContact = clone $relatedNewContact; } $matchedIDs = array(); // To update/fill contact, get the matching contact Ids if duplicate contact found // otherwise get contact Id from object of related contact if (is_array($relatedNewContact) && civicrm_error($relatedNewContact)) { if (civicrm_duplicate($relatedNewContact)) { $matchedIDs = explode(',', $relatedNewContact['error_message']['params'][0]); } else { $errorMessage = $relatedNewContact['error_message']; array_unshift($values, $errorMessage); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::ERROR; } } else { $matchedIDs[] = $relatedNewContact->id; } // update/fill related contact after getting matching Contact Ids, CRM-4424 if (in_array($onDuplicate, array(CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::DUPLICATE_FILL))) { //validation of related contact subtype for update mode //CRM-5125 $relatedCsType = null; if (CRM_Utils_Array::value('contact_sub_type', $formatting)) { $relatedCsType = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $matchedIDs[0], 'contact_sub_type'); } if (!empty($relatedCsType) && (!CRM_Contact_BAO_ContactType::isAllowEdit($matchedIDs[0], $relatedCsType) || $relatedCsType != CRM_Utils_Array::value('contact_sub_type', $formatting))) { $errorMessage = ts("Mismatched or Invalid contact subtype found for this related contact."); array_unshift($values, $errorMessage); return CRM_Import_Parser::NO_MATCH; } else { $updatedContact = $this->createContact($formatting, $contactFields, $onDuplicate, $matchedIDs[0]); } } static $relativeContact = array(); if (civicrm_duplicate($relatedNewContact)) { if (count($matchedIDs) >= 1) { $relContactId = $matchedIDs[0]; //add relative contact to count during update & fill mode. //logic to make count distinct by contact id. if ($this->_newRelatedContacts || !empty($relativeContact)) { $reContact = array_keys($relativeContact, $relContactId); if (empty($reContact)) { $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; } } else { $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; } } } else { $relContactId = $relatedNewContact->id; $this->_newRelatedContacts[] = $relativeContact[] = $relContactId; } if (civicrm_duplicate($relatedNewContact) || $relatedNewContact instanceof CRM_Contact_BAO_Contact) { //fix for CRM-1993.Checks for duplicate related contacts if (count($matchedIDs) >= 1) { //if more than one duplicate contact //found, create relationship with first contact // now create the relationship record $relationParams = array(); $relationParams = array('relationship_type_id' => $key, 'contact_check' => array($relContactId => 1), 'is_active' => 1, 'skipRecentView' => true); // we only handle related contact success, we ignore failures for now // at some point wold be nice to have related counts as separate $relationIds = array('contact' => $primaryContactId); list($valid, $invalid, $duplicate, $saved, $relationshipIds) = CRM_Contact_BAO_Relationship::create($relationParams, $relationIds); CRM_Contact_BAO_Relationship::relatedMemberships($primaryContactId, $relationParams, $relationIds); //handle current employer, CRM-3532 if ($valid) { require_once 'CRM/Core/PseudoConstant.php'; $allRelationships = CRM_Core_PseudoConstant::relationshipType('name'); $relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $key); $relationshipType = str_replace($relationshipTypeId . '_', '', $key); $orgId = $individualId = null; if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employee of') { $orgId = $relContactId; $individualId = $primaryContactId; } else { if ($allRelationships[$relationshipTypeId]["name_{$relationshipType}"] == 'Employer of') { $orgId = $primaryContactId; $individualId = $relContactId; } } if ($orgId && $individualId) { $currentEmpParams[$individualId] = $orgId; require_once 'CRM/Contact/BAO/Contact/Utils.php'; CRM_Contact_BAO_Contact_Utils::setCurrentEmployer($currentEmpParams); } } } } } } } if ($this->_updateWithId) { //return warning if street address is unparsed, CRM-5886 return $this->processMessage($values, $statusFieldName, $this->_retCode); } //dupe checking if (is_array($newContact) && civicrm_error($newContact)) { $code = null; if (($code = CRM_Utils_Array::value('code', $newContact['error_message'])) && $code == CRM_Core_Error::DUPLICATE_CONTACT) { $urls = array(); // need to fix at some stage and decide if the error will return an // array or string, crude hack for now if (is_array($newContact['error_message']['params'][0])) { $cids = $newContact['error_message']['params'][0]; } else { $cids = explode(',', $newContact['error_message']['params'][0]); } foreach ($cids as $cid) { $urls[] = CRM_Utils_System::url('civicrm/contact/view', 'reset=1&cid=' . $cid, true); } $url_string = implode("\n", $urls); // If we duplicate more than one record, skip no matter what if (count($cids) > 1) { $errorMessage = ts('Record duplicates multiple contacts'); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); //combine error msg to avoid mismatch between error file columns. $errorMessage .= "\n" . $url_string; array_unshift($values, $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::ERROR; } // Params only had one id, so shift it out $contactId = array_shift($cids); $cid = null; $vals = array('contact_id' => $contactId); if ($onDuplicate == CRM_Import_Parser::DUPLICATE_REPLACE) { $result = civicrm_replace_contact_formatted($contactId, $formatted, $contactFields); $cid = $result['result']; } else { if ($onDuplicate == CRM_Import_Parser::DUPLICATE_UPDATE) { $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId); } else { if ($onDuplicate == CRM_Import_Parser::DUPLICATE_FILL) { $newContact = $this->createContact($formatted, $contactFields, $onDuplicate, $contactId); } } } // else skip does nothing and just returns an error code. if ($cid) { $contact = array('contact_id' => $cid); $defaults = array(); $newContact = CRM_Contact_BAO_Contact::retrieve($contact, $defaults); } if (civicrm_error($newContact)) { $contactID = $newContact['error_message']['params'][0]; if (!in_array($contactID, $this->_newContacts)) { $this->_newContacts[] = $contactID; } } //CRM-262 No Duplicate Checking if ($onDuplicate == CRM_Import_Parser::DUPLICATE_SKIP) { array_unshift($values, $url_string); $importRecordParams = array($statusFieldName => 'DUPLICATE', "{$statusFieldName}Msg" => "Skipping duplicate record"); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::DUPLICATE; } $importRecordParams = array($statusFieldName => 'IMPORTED'); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); //return warning if street address is not parsed, CRM-5886 return $this->processMessage($values, $statusFieldName, CRM_Import_Parser::VALID); } else { // Not a dupe, so we had an error $errorMessage = $newContact['error_message']; array_unshift($values, $errorMessage); $importRecordParams = array($statusFieldName => 'ERROR', "{$statusFieldName}Msg" => $errorMessage); $this->updateImportRecord($values[count($values) - 1], $importRecordParams); return CRM_Import_Parser::ERROR; } } // sleep(3); return $this->processMessage($values, $statusFieldName, CRM_Import_Parser::VALID); }
function statistics(&$rows) { $statistics = parent::statistics($rows); $isStatusFilter = false; $relStatus = null; if (CRM_Utils_Array::value('is_active_value', $this->_params) == '1') { $relStatus = 'Is equal to Active'; } elseif (CRM_Utils_Array::value('is_active_value', $this->_params) == '0') { $relStatus = 'Is equal to Inactive'; } if (CRM_Utils_Array::value('filters', $statistics)) { foreach ($statistics['filters'] as $id => $value) { //for displaying relationship type filter if ($value['title'] == 'Relationship') { $relTypes = CRM_Core_PseudoConstant::relationshipType(); $statistics['filters'][$id]['value'] = 'Is equal to ' . $relTypes[$this->_params['relationship_type_id_value']]['label_' . $this->relationType]; } //for displaying relationship status if ($value['title'] == 'Relationship Status') { $isStatusFilter = true; $statistics['filters'][$id]['value'] = $relStatus; } } } //for displaying relationship status if (!$isStatusFilter && $relStatus) { $statistics['filters'][] = array('title' => 'Relationship Status', 'value' => $relStatus); } return $statistics; }
/** * Add the relationship type in the db. * * @param array $params * (reference ) an assoc array of name/value pairs. * @param array $ids * The array that holds all the db ids. * * @return CRM_Contact_DAO_RelationshipType */ public static function add(&$params, &$ids) { //to change name, CRM-3336 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) { $params['label_a_b'] = $params['name_a_b']; } if (empty($params['label_b_a']) && !empty($params['name_b_a'])) { $params['label_b_a'] = $params['name_b_a']; } // set label to name if it's not set - but *only* for // ADD action. CRM-3336 as part from (CRM-3522) if (empty($ids['relationshipType'])) { if (empty($params['name_a_b']) && !empty($params['label_a_b'])) { $params['name_a_b'] = $params['label_a_b']; } if (empty($params['name_b_a']) && !empty($params['label_b_a'])) { $params['name_b_a'] = $params['label_b_a']; } } // action is taken depending upon the mode $relationshipType = new CRM_Contact_DAO_RelationshipType(); $relationshipType->copyValues($params); // if label B to A is blank, insert the value label A to B for it if (!strlen(trim($strName = CRM_Utils_Array::value('name_b_a', $params)))) { $relationshipType->name_b_a = CRM_Utils_Array::value('name_a_b', $params); } if (!strlen(trim($strName = CRM_Utils_Array::value('label_b_a', $params)))) { $relationshipType->label_b_a = CRM_Utils_Array::value('label_a_b', $params); } $relationshipType->id = CRM_Utils_Array::value('relationshipType', $ids); $result = $relationshipType->save(); CRM_Core_PseudoConstant::relationshipType('label', TRUE); CRM_Core_PseudoConstant::relationshipType('name', TRUE); CRM_Case_XMLProcessor::flushStaticCaches(); return $result; }
function preProcess() { //custom data related code $this->_cdType = CRM_Utils_Array::value('type', $_GET); $this->assign('cdType', FALSE); if ($this->_cdType) { $this->assign('cdType', TRUE); return CRM_Custom_Form_CustomData::preProcess($this); } $this->_contactId = $this->get('contactId'); // Check permissions if (in_array($this->_action, array(CRM_Core_Action::ADD, CRM_Core_Action::UPDATE, CRM_Core_Action::DELETE))) { if (!CRM_Contact_BAO_Contact_Permission::allow($this->_contactId, CRM_Core_Permission::EDIT)) { CRM_Core_Error::statusBounce(ts('You do not have permission to edit relationships for this contact.')); } } $this->_relationshipId = $this->get('id'); $this->_rtype = CRM_Utils_Request::retrieve('rtype', 'String', $this); $this->_rtypeId = CRM_Utils_Request::retrieve('relTypeId', 'String', $this); $this->_display_name_a = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $this->_contactId, 'display_name'); $this->assign('sort_name_a', $this->_display_name_a); CRM_Utils_System::setTitle(ts('Relationships for') . ' ' . $this->_display_name_a); $this->_caseId = CRM_Utils_Request::retrieve('caseID', 'Integer', $this); //get the relationship values. $this->_values = array(); if ($this->_relationshipId) { $params = array('id' => $this->_relationshipId); CRM_Core_DAO::commonRetrieve('CRM_Contact_DAO_Relationship', $params, $this->_values); } if (!$this->_rtypeId) { $params = $this->controller->exportValues($this->_name); if (isset($params['relationship_type_id'])) { $this->_rtypeId = $params['relationship_type_id']; } elseif (!empty($this->_values)) { $this->_rtypeId = $this->_values['relationship_type_id'] . '_' . $this->_rtype; } } //get the relationship type id $this->_relationshipTypeId = str_replace(array('_a_b', '_b_a'), array('', ''), $this->_rtypeId); //get the relationship type if (!$this->_rtype) { $this->_rtype = str_replace($this->_relationshipTypeId . '_', '', $this->_rtypeId); } $this->assign('rtype', $this->_rtype); //use name as it remain constant, CRM-3336 $this->_allRelationshipNames = CRM_Core_PseudoConstant::relationshipType('name'); // when custom data is included in this page if (CRM_Utils_Array::value('hidden_custom', $_POST)) { CRM_Custom_Form_CustomData::preProcess($this); CRM_Custom_Form_CustomData::buildQuickForm($this); CRM_Custom_Form_CustomData::setDefaultValues($this); } }