e229a03ea5718ee40692998d53cc479b0a43f18c
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33 class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType {
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 parent::__construct();
40 }
41
42 /**
43 * Fetch object based on array of properties.
44 *
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
47 * @param array $defaults
48 * (reference ) an assoc array to hold the flattened values.
49 *
50 * @return CRM_Contact_BAO_RelationshipType
51 */
52 public static function retrieve(&$params, &$defaults) {
53 $relationshipType = new CRM_Contact_DAO_RelationshipType();
54 $relationshipType->copyValues($params);
55 if ($relationshipType->find(TRUE)) {
56 CRM_Core_DAO::storeValues($relationshipType, $defaults);
57 $relationshipType->free();
58 return $relationshipType;
59 }
60 return NULL;
61 }
62
63 /**
64 * Update the is_active flag in the db.
65 *
66 * @param int $id
67 * Id of the database record.
68 * @param bool $is_active
69 * Value we want to set the is_active field.
70 *
71 * @return bool
72 * true if we found and updated the object, else false
73 */
74 public static function setIsActive($id, $is_active) {
75 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
76 }
77
78 /**
79 * Add the relationship type in the db.
80 *
81 * @param array $params
82 *
83 * @return CRM_Contact_DAO_RelationshipType
84 */
85 public static function add($params) {
86 if (empty($params['id'])) {
87 // Set name to label if not set
88 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
89 $params['label_a_b'] = $params['name_a_b'];
90 }
91 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
92 $params['label_b_a'] = $params['name_b_a'];
93 }
94
95 // set label to name if it's not set
96 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
97 $params['name_a_b'] = $params['label_a_b'];
98 }
99 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
100 $params['name_b_a'] = $params['label_b_a'];
101 }
102 }
103
104 // action is taken depending upon the mode
105 $relationshipType = new CRM_Contact_DAO_RelationshipType();
106
107 $hook = empty($params['id']) ? 'create' : 'edit';
108 CRM_Utils_Hook::pre($hook, 'RelationshipType', CRM_Utils_Array::value('id', $params), $params);
109
110 $relationshipType->copyValues($params);
111 $relationshipType->save();
112
113 CRM_Utils_Hook::post($hook, 'RelationshipType', $relationshipType->id, $relationshipType);
114
115 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
116 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
117 CRM_Case_XMLProcessor::flushStaticCaches();
118 return $relationshipType;
119 }
120
121 /**
122 * Delete Relationship Types.
123 *
124 * @param int $relationshipTypeId
125 *
126 * @throws CRM_Core_Exception
127 * @return mixed
128 */
129 public static function del($relationshipTypeId) {
130 // make sure relationshipTypeId is an integer
131 // @todo review this as most delete functions rely on the api & form layer for this
132 // or do a find first & throw error if no find
133 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
134 throw new CRM_Core_Exception(ts('Invalid relationship type'));
135 }
136
137 //check dependencies
138
139 // delete all relationships
140 $relationship = new CRM_Contact_DAO_Relationship();
141 $relationship->relationship_type_id = $relationshipTypeId;
142 $relationship->delete();
143
144 // remove this relationship type from membership types
145 $mems = civicrm_api3('MembershipType', 'get', [
146 'relationship_type_id' => ['LIKE' => "%{$relationshipTypeId}%"],
147 'return' => ['id', 'relationship_type_id', 'relationship_direction'],
148 ]);
149 foreach ($mems['values'] as $membershipTypeId => $membershipType) {
150 $pos = array_search($relationshipTypeId, $membershipType['relationship_type_id']);
151 // Api call may have returned false positives but currently the relationship_type_id uses
152 // nonstandard serialization which makes anything more accurate impossible.
153 if ($pos !== FALSE) {
154 unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]);
155 civicrm_api3('MembershipType', 'create', $membershipType);
156 }
157 }
158
159 //fixed for CRM-3323
160 $mappingField = new CRM_Core_DAO_MappingField();
161 $mappingField->relationship_type_id = $relationshipTypeId;
162 $mappingField->find();
163 while ($mappingField->fetch()) {
164 $mappingField->delete();
165 }
166
167 $relationshipType = new CRM_Contact_DAO_RelationshipType();
168 $relationshipType->id = $relationshipTypeId;
169 return $relationshipType->delete();
170 }
171
172 }