Merge pull request #11377 from lcdservices/CRM-21523
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
8c9251b3 31 * @copyright CiviCRM LLC (c) 2004-2018
6a488035
TO
32 */
33class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType {
34
35 /**
fe482240 36 * Class constructor.
6a488035 37 */
00be9182 38 public function __construct() {
6a488035
TO
39 parent::__construct();
40 }
41
42 /**
fe482240 43 * Fetch object based on array of properties.
6a488035 44 *
77c5b619
TO
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.
6a488035 49 *
16b10e64 50 * @return CRM_Contact_BAO_RelationshipType
6a488035 51 */
00be9182 52 public static function retrieve(&$params, &$defaults) {
6a488035
TO
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 /**
fe482240 64 * Update the is_active flag in the db.
6a488035 65 *
77c5b619
TO
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.
6a488035 70 *
a6c01b45 71 * @return Object
b44e3f84 72 * DAO object on success, null otherwise
6a488035 73 */
00be9182 74 public static function setIsActive($id, $is_active) {
6a488035
TO
75 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
76 }
77
78 /**
fe482240 79 * Add the relationship type in the db.
6a488035 80 *
77c5b619
TO
81 * @param array $params
82 * (reference ) an assoc array of name/value pairs.
83 * @param array $ids
84 * The array that holds all the db ids.
6a488035 85 *
c490a46a 86 * @return CRM_Contact_DAO_RelationshipType
6a488035 87 */
0782c7b9 88 public static function add(&$params, $ids = []) {
89 $params['id'] = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('relationshipType', $ids));
6a488035 90 //to change name, CRM-3336
8cc574cf 91 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
6a488035
TO
92 $params['label_a_b'] = $params['name_a_b'];
93 }
94
8cc574cf 95 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
6a488035
TO
96 $params['label_b_a'] = $params['name_b_a'];
97 }
98
99 // set label to name if it's not set - but *only* for
100 // ADD action. CRM-3336 as part from (CRM-3522)
0782c7b9 101 if (!$params['id']) {
8cc574cf 102 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
6a488035
TO
103 $params['name_a_b'] = $params['label_a_b'];
104 }
8cc574cf 105 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
6a488035
TO
106 $params['name_b_a'] = $params['label_b_a'];
107 }
108 }
109
110 // action is taken depending upon the mode
111 $relationshipType = new CRM_Contact_DAO_RelationshipType();
112
113 $relationshipType->copyValues($params);
114
115 // if label B to A is blank, insert the value label A to B for it
116 if (!strlen(trim($strName = CRM_Utils_Array::value('name_b_a', $params)))) {
117 $relationshipType->name_b_a = CRM_Utils_Array::value('name_a_b', $params);
118 }
119 if (!strlen(trim($strName = CRM_Utils_Array::value('label_b_a', $params)))) {
120 $relationshipType->label_b_a = CRM_Utils_Array::value('label_a_b', $params);
121 }
122
4bc6c777
TO
123 $result = $relationshipType->save();
124
125 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
126 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
127 CRM_Case_XMLProcessor::flushStaticCaches();
128 return $result;
6a488035
TO
129 }
130
131 /**
fe482240 132 * Delete Relationship Types.
6a488035
TO
133 *
134 * @param int $relationshipTypeId
77b97be7
EM
135 *
136 * @throws CRM_Core_Exception
137 * @return mixed
6a488035 138 */
00be9182 139 public static function del($relationshipTypeId) {
6a488035 140 // make sure relationshipTypeId is an integer
b2402735 141 // @todo review this as most delete functions rely on the api & form layer for this
142 // or do a find first & throw error if no find
6a488035 143 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
b2402735 144 throw new CRM_Core_Exception(ts('Invalid relationship type'));
6a488035
TO
145 }
146
6a488035
TO
147 //check dependencies
148
149 // delete all relationships
150 $relationship = new CRM_Contact_DAO_Relationship();
151 $relationship->relationship_type_id = $relationshipTypeId;
152 $relationship->delete();
153
baccd59e
CW
154 // remove this relationship type from membership types
155 $mems = civicrm_api3('MembershipType', 'get', array(
156 'relationship_type_id' => array('LIKE' => "%{$relationshipTypeId}%"),
157 'return' => array('id', 'relationship_type_id', 'relationship_direction'),
158 ));
159 foreach ($mems['values'] as $membershipTypeId => $membershipType) {
160 $pos = array_search($relationshipTypeId, $membershipType['relationship_type_id']);
161 // Api call may have returned false positives but currently the relationship_type_id uses
162 // nonstandard serialization which makes anything more accurate impossible.
163 if ($pos !== FALSE) {
164 unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]);
165 civicrm_api3('MembershipType', 'create', $membershipType);
166 }
167 }
6a488035
TO
168
169 //fixed for CRM-3323
170 $mappingField = new CRM_Core_DAO_MappingField();
171 $mappingField->relationship_type_id = $relationshipTypeId;
172 $mappingField->find();
173 while ($mappingField->fetch()) {
174 $mappingField->delete();
175 }
176
177 $relationshipType = new CRM_Contact_DAO_RelationshipType();
178 $relationshipType->id = $relationshipTypeId;
179 return $relationshipType->delete();
180 }
96025800 181
6a488035 182}