Merge pull request #14254 from yashodha/add_dev_tab
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
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);
6a488035
TO
57 return $relationshipType;
58 }
59 return NULL;
60 }
61
62 /**
fe482240 63 * Update the is_active flag in the db.
6a488035 64 *
77c5b619
TO
65 * @param int $id
66 * Id of the database record.
67 * @param bool $is_active
68 * Value we want to set the is_active field.
6a488035 69 *
8a4fede3 70 * @return bool
71 * true if we found and updated the object, else false
6a488035 72 */
00be9182 73 public static function setIsActive($id, $is_active) {
6a488035
TO
74 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
75 }
76
77 /**
fe482240 78 * Add the relationship type in the db.
6a488035 79 *
77c5b619 80 * @param array $params
6a488035 81 *
c490a46a 82 * @return CRM_Contact_DAO_RelationshipType
6a488035 83 */
780df37b
CW
84 public static function add($params) {
85 if (empty($params['id'])) {
86 // Set name to label if not set
87 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
88 $params['label_a_b'] = $params['name_a_b'];
89 }
90 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
91 $params['label_b_a'] = $params['name_b_a'];
92 }
6a488035 93
780df37b 94 // set label to name if it's not set
8cc574cf 95 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
6a488035
TO
96 $params['name_a_b'] = $params['label_a_b'];
97 }
8cc574cf 98 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
6a488035
TO
99 $params['name_b_a'] = $params['label_b_a'];
100 }
101 }
102
103 // action is taken depending upon the mode
104 $relationshipType = new CRM_Contact_DAO_RelationshipType();
105
780df37b
CW
106 $hook = empty($params['id']) ? 'create' : 'edit';
107 CRM_Utils_Hook::pre($hook, 'RelationshipType', CRM_Utils_Array::value('id', $params), $params);
6a488035 108
780df37b
CW
109 $relationshipType->copyValues($params);
110 $relationshipType->save();
6a488035 111
780df37b 112 CRM_Utils_Hook::post($hook, 'RelationshipType', $relationshipType->id, $relationshipType);
4bc6c777
TO
113
114 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
115 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
116 CRM_Case_XMLProcessor::flushStaticCaches();
780df37b 117 return $relationshipType;
6a488035
TO
118 }
119
120 /**
fe482240 121 * Delete Relationship Types.
6a488035
TO
122 *
123 * @param int $relationshipTypeId
77b97be7
EM
124 *
125 * @throws CRM_Core_Exception
126 * @return mixed
6a488035 127 */
00be9182 128 public static function del($relationshipTypeId) {
6a488035 129 // make sure relationshipTypeId is an integer
b2402735 130 // @todo review this as most delete functions rely on the api & form layer for this
131 // or do a find first & throw error if no find
6a488035 132 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
b2402735 133 throw new CRM_Core_Exception(ts('Invalid relationship type'));
6a488035
TO
134 }
135
6a488035
TO
136 //check dependencies
137
138 // delete all relationships
139 $relationship = new CRM_Contact_DAO_Relationship();
140 $relationship->relationship_type_id = $relationshipTypeId;
141 $relationship->delete();
142
baccd59e 143 // remove this relationship type from membership types
be2fb01f
CW
144 $mems = civicrm_api3('MembershipType', 'get', [
145 'relationship_type_id' => ['LIKE' => "%{$relationshipTypeId}%"],
146 'return' => ['id', 'relationship_type_id', 'relationship_direction'],
147 ]);
baccd59e
CW
148 foreach ($mems['values'] as $membershipTypeId => $membershipType) {
149 $pos = array_search($relationshipTypeId, $membershipType['relationship_type_id']);
150 // Api call may have returned false positives but currently the relationship_type_id uses
151 // nonstandard serialization which makes anything more accurate impossible.
152 if ($pos !== FALSE) {
153 unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]);
154 civicrm_api3('MembershipType', 'create', $membershipType);
155 }
156 }
6a488035
TO
157
158 //fixed for CRM-3323
159 $mappingField = new CRM_Core_DAO_MappingField();
160 $mappingField->relationship_type_id = $relationshipTypeId;
161 $mappingField->find();
162 while ($mappingField->fetch()) {
163 $mappingField->delete();
164 }
165
166 $relationshipType = new CRM_Contact_DAO_RelationshipType();
167 $relationshipType->id = $relationshipTypeId;
168 return $relationshipType->delete();
169 }
96025800 170
6a488035 171}