get('contactType'); CRM_Utils_System::setTitle(ts('Add Contacts to %1', [1 => $contactType])); $this->addElement('text', 'name', ts('Find Target %1', [1 => $contactType])); $this->add('select', 'relationship_type_id', ts('Relationship Type'), [ '' => ts('- select -'), ] + CRM_Contact_BAO_Relationship::getRelationType($contactType), TRUE ); $searchRows = $this->get('searchRows'); $searchCount = $this->get('searchCount'); if ($searchRows) { $checkBoxes = []; $chekFlag = 0; foreach ($searchRows as $id => $row) { if (!$chekFlag) { $chekFlag = $id; } $checkBoxes[$id] = $this->createElement('radio', NULL, NULL, NULL, $id); } $this->addGroup($checkBoxes, 'contact_check'); if ($chekFlag) { $checkBoxes[$chekFlag]->setChecked(TRUE); } $this->assign('searchRows', $searchRows); } $this->assign('searchCount', $searchCount); $this->assign('searchDone', $this->get('searchDone')); $this->assign('contact_type_display', $contactType); $buttonAttrs = [ 'type' => 'submit', 'class' => 'crm-form-submit', 'value' => 1, ]; $this->addElement('xbutton', $this->getButtonName('refresh'), ts('Search'), $buttonAttrs); $this->addElement('xbutton', $this->getButtonName('cancel'), ts('Cancel'), $buttonAttrs); $this->addButtons([ [ 'type' => 'next', 'name' => ts('Add to %1', [1 => $contactType]), 'isDefault' => TRUE, ], [ 'type' => 'cancel', 'name' => ts('Cancel'), ], ]); } /** * Add relationships from form. */ public function addRelationships() { if (!is_array($this->_contactIds)) { // Could this really happen? return; } $relationshipTypeParts = explode('_', $this->params['relationship_type_id']); $params = [ 'relationship_type_id' => $relationshipTypeParts[0], 'is_active' => 1, ]; $secondaryRelationshipSide = $relationshipTypeParts[1]; $primaryRelationshipSide = $relationshipTypeParts[2]; $primaryFieldName = 'contact_id_' . $primaryRelationshipSide; $secondaryFieldName = 'contact_id_' . $secondaryRelationshipSide; $relationshipLabel = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $params['relationship_type_id'], "label_{$secondaryRelationshipSide}_{$primaryRelationshipSide}"); $params[$secondaryFieldName] = $this->_contactIds; $params[$primaryFieldName] = $this->params['contact_check']; $outcome = CRM_Contact_BAO_Relationship::createMultiple($params, $primaryRelationshipSide); $relatedContactName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $params[$primaryFieldName], 'display_name'); $status = [ ts('%count %2 %3 relationship created', [ 'count' => $outcome['valid'], 'plural' => '%count %2 %3 relationships created', 2 => $relationshipLabel, 3 => $relatedContactName, ]), ]; if ($outcome['duplicate']) { $status[] = ts('%count was skipped because the contact is already %2 %3', [ 'count' => $outcome['duplicate'], 'plural' => '%count were skipped because the contacts are already %2 %3', 2 => $relationshipLabel, 3 => $relatedContactName, ]); } if ($outcome['invalid']) { $status[] = ts('%count relationship was not created because the contact is not of the right type for this relationship', [ 'count' => $outcome['invalid'], 'plural' => '%count relationships were not created because the contact is not of the right type for this relationship', ]); } $status = '