Merge pull request #13258 from eileenmcnaughton/isam
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
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 *
8a4fede3 71 * @return bool
72 * true if we found and updated the object, else false
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 81 * @param array $params
6a488035 82 *
c490a46a 83 * @return CRM_Contact_DAO_RelationshipType
6a488035 84 */
780df37b
CW
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 }
6a488035 94
780df37b 95 // set label to name if it's not set
8cc574cf 96 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
6a488035
TO
97 $params['name_a_b'] = $params['label_a_b'];
98 }
8cc574cf 99 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
6a488035
TO
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
780df37b
CW
107 $hook = empty($params['id']) ? 'create' : 'edit';
108 CRM_Utils_Hook::pre($hook, 'RelationshipType', CRM_Utils_Array::value('id', $params), $params);
6a488035 109
780df37b
CW
110 $relationshipType->copyValues($params);
111 $relationshipType->save();
6a488035 112
780df37b 113 CRM_Utils_Hook::post($hook, 'RelationshipType', $relationshipType->id, $relationshipType);
4bc6c777
TO
114
115 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
116 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
117 CRM_Case_XMLProcessor::flushStaticCaches();
780df37b 118 return $relationshipType;
6a488035
TO
119 }
120
121 /**
fe482240 122 * Delete Relationship Types.
6a488035
TO
123 *
124 * @param int $relationshipTypeId
77b97be7
EM
125 *
126 * @throws CRM_Core_Exception
127 * @return mixed
6a488035 128 */
00be9182 129 public static function del($relationshipTypeId) {
6a488035 130 // make sure relationshipTypeId is an integer
b2402735 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
6a488035 133 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
b2402735 134 throw new CRM_Core_Exception(ts('Invalid relationship type'));
6a488035
TO
135 }
136
6a488035
TO
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
baccd59e
CW
144 // remove this relationship type from membership types
145 $mems = civicrm_api3('MembershipType', 'get', array(
146 'relationship_type_id' => array('LIKE' => "%{$relationshipTypeId}%"),
147 'return' => array('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 }
6a488035
TO
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 }
96025800 171
6a488035 172}