Merge pull request #20487 from eileenmcnaughton/cust_value
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType {
18
19 /**
fe482240 20 * Class constructor.
6a488035 21 */
00be9182 22 public function __construct() {
6a488035
TO
23 parent::__construct();
24 }
25
26 /**
fe482240 27 * Fetch object based on array of properties.
6a488035 28 *
77c5b619
TO
29 * @param array $params
30 * (reference ) an assoc array of name/value pairs.
31 * @param array $defaults
32 * (reference ) an assoc array to hold the flattened values.
6a488035 33 *
16b10e64 34 * @return CRM_Contact_BAO_RelationshipType
6a488035 35 */
00be9182 36 public static function retrieve(&$params, &$defaults) {
6a488035
TO
37 $relationshipType = new CRM_Contact_DAO_RelationshipType();
38 $relationshipType->copyValues($params);
39 if ($relationshipType->find(TRUE)) {
40 CRM_Core_DAO::storeValues($relationshipType, $defaults);
6a488035
TO
41 return $relationshipType;
42 }
43 return NULL;
44 }
45
46 /**
fe482240 47 * Update the is_active flag in the db.
6a488035 48 *
77c5b619
TO
49 * @param int $id
50 * Id of the database record.
51 * @param bool $is_active
52 * Value we want to set the is_active field.
6a488035 53 *
8a4fede3 54 * @return bool
55 * true if we found and updated the object, else false
6a488035 56 */
00be9182 57 public static function setIsActive($id, $is_active) {
6a488035
TO
58 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
59 }
60
61 /**
fe482240 62 * Add the relationship type in the db.
6a488035 63 *
77c5b619 64 * @param array $params
6a488035 65 *
c490a46a 66 * @return CRM_Contact_DAO_RelationshipType
6a488035 67 */
780df37b
CW
68 public static function add($params) {
69 if (empty($params['id'])) {
70 // Set name to label if not set
71 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
72 $params['label_a_b'] = $params['name_a_b'];
73 }
74 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
75 $params['label_b_a'] = $params['name_b_a'];
76 }
6a488035 77
780df37b 78 // set label to name if it's not set
8cc574cf 79 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
6a488035
TO
80 $params['name_a_b'] = $params['label_a_b'];
81 }
8cc574cf 82 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
6a488035
TO
83 $params['name_b_a'] = $params['label_b_a'];
84 }
85 }
86
87 // action is taken depending upon the mode
0419bf7b 88 $relationshipType = self::writeRecord($params);
4bc6c777
TO
89
90 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
91 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
1ac9bb56 92 CRM_Core_PseudoConstant::flush();
4bc6c777 93 CRM_Case_XMLProcessor::flushStaticCaches();
780df37b 94 return $relationshipType;
6a488035
TO
95 }
96
97 /**
fe482240 98 * Delete Relationship Types.
6a488035
TO
99 *
100 * @param int $relationshipTypeId
77b97be7
EM
101 *
102 * @throws CRM_Core_Exception
103 * @return mixed
6a488035 104 */
00be9182 105 public static function del($relationshipTypeId) {
6a488035 106 // make sure relationshipTypeId is an integer
b2402735 107 // @todo review this as most delete functions rely on the api & form layer for this
108 // or do a find first & throw error if no find
6a488035 109 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
b2402735 110 throw new CRM_Core_Exception(ts('Invalid relationship type'));
6a488035
TO
111 }
112
6a488035
TO
113 //check dependencies
114
115 // delete all relationships
116 $relationship = new CRM_Contact_DAO_Relationship();
117 $relationship->relationship_type_id = $relationshipTypeId;
118 $relationship->delete();
119
baccd59e 120 // remove this relationship type from membership types
be2fb01f
CW
121 $mems = civicrm_api3('MembershipType', 'get', [
122 'relationship_type_id' => ['LIKE' => "%{$relationshipTypeId}%"],
123 'return' => ['id', 'relationship_type_id', 'relationship_direction'],
124 ]);
baccd59e
CW
125 foreach ($mems['values'] as $membershipTypeId => $membershipType) {
126 $pos = array_search($relationshipTypeId, $membershipType['relationship_type_id']);
127 // Api call may have returned false positives but currently the relationship_type_id uses
128 // nonstandard serialization which makes anything more accurate impossible.
129 if ($pos !== FALSE) {
130 unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]);
131 civicrm_api3('MembershipType', 'create', $membershipType);
132 }
133 }
6a488035
TO
134
135 //fixed for CRM-3323
136 $mappingField = new CRM_Core_DAO_MappingField();
137 $mappingField->relationship_type_id = $relationshipTypeId;
138 $mappingField->find();
139 while ($mappingField->fetch()) {
140 $mappingField->delete();
141 }
142
143 $relationshipType = new CRM_Contact_DAO_RelationshipType();
144 $relationshipType->id = $relationshipTypeId;
145 return $relationshipType->delete();
146 }
96025800 147
6a488035 148}