Merge pull request #16005 from magnolia61/Contribution_Invoice_Privacy
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType {
18
19 /**
20 * Class constructor.
21 */
22 public function __construct() {
23 parent::__construct();
24 }
25
26 /**
27 * Fetch object based on array of properties.
28 *
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.
33 *
34 * @return CRM_Contact_BAO_RelationshipType
35 */
36 public static function retrieve(&$params, &$defaults) {
37 $relationshipType = new CRM_Contact_DAO_RelationshipType();
38 $relationshipType->copyValues($params);
39 if ($relationshipType->find(TRUE)) {
40 CRM_Core_DAO::storeValues($relationshipType, $defaults);
41 return $relationshipType;
42 }
43 return NULL;
44 }
45
46 /**
47 * Update the is_active flag in the db.
48 *
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.
53 *
54 * @return bool
55 * true if we found and updated the object, else false
56 */
57 public static function setIsActive($id, $is_active) {
58 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
59 }
60
61 /**
62 * Add the relationship type in the db.
63 *
64 * @param array $params
65 *
66 * @return CRM_Contact_DAO_RelationshipType
67 */
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 }
77
78 // set label to name if it's not set
79 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
80 $params['name_a_b'] = $params['label_a_b'];
81 }
82 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
83 $params['name_b_a'] = $params['label_b_a'];
84 }
85 }
86
87 // action is taken depending upon the mode
88 $relationshipType = new CRM_Contact_DAO_RelationshipType();
89
90 $hook = empty($params['id']) ? 'create' : 'edit';
91 CRM_Utils_Hook::pre($hook, 'RelationshipType', CRM_Utils_Array::value('id', $params), $params);
92
93 $relationshipType->copyValues($params);
94 $relationshipType->save();
95
96 CRM_Utils_Hook::post($hook, 'RelationshipType', $relationshipType->id, $relationshipType);
97
98 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
99 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
100 CRM_Case_XMLProcessor::flushStaticCaches();
101 return $relationshipType;
102 }
103
104 /**
105 * Delete Relationship Types.
106 *
107 * @param int $relationshipTypeId
108 *
109 * @throws CRM_Core_Exception
110 * @return mixed
111 */
112 public static function del($relationshipTypeId) {
113 // make sure relationshipTypeId is an integer
114 // @todo review this as most delete functions rely on the api & form layer for this
115 // or do a find first & throw error if no find
116 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
117 throw new CRM_Core_Exception(ts('Invalid relationship type'));
118 }
119
120 //check dependencies
121
122 // delete all relationships
123 $relationship = new CRM_Contact_DAO_Relationship();
124 $relationship->relationship_type_id = $relationshipTypeId;
125 $relationship->delete();
126
127 // remove this relationship type from membership types
128 $mems = civicrm_api3('MembershipType', 'get', [
129 'relationship_type_id' => ['LIKE' => "%{$relationshipTypeId}%"],
130 'return' => ['id', 'relationship_type_id', 'relationship_direction'],
131 ]);
132 foreach ($mems['values'] as $membershipTypeId => $membershipType) {
133 $pos = array_search($relationshipTypeId, $membershipType['relationship_type_id']);
134 // Api call may have returned false positives but currently the relationship_type_id uses
135 // nonstandard serialization which makes anything more accurate impossible.
136 if ($pos !== FALSE) {
137 unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]);
138 civicrm_api3('MembershipType', 'create', $membershipType);
139 }
140 }
141
142 //fixed for CRM-3323
143 $mappingField = new CRM_Core_DAO_MappingField();
144 $mappingField->relationship_type_id = $relationshipTypeId;
145 $mappingField->find();
146 while ($mappingField->fetch()) {
147 $mappingField->delete();
148 }
149
150 $relationshipType = new CRM_Contact_DAO_RelationshipType();
151 $relationshipType->id = $relationshipTypeId;
152 return $relationshipType->delete();
153 }
154
155 }