Merge pull request #14254 from yashodha/add_dev_tab
[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 return $relationshipType;
58 }
59 return NULL;
60 }
61
62 /**
63 * Update the is_active flag in the db.
64 *
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.
69 *
70 * @return bool
71 * true if we found and updated the object, else false
72 */
73 public static function setIsActive($id, $is_active) {
74 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
75 }
76
77 /**
78 * Add the relationship type in the db.
79 *
80 * @param array $params
81 *
82 * @return CRM_Contact_DAO_RelationshipType
83 */
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 }
93
94 // set label to name if it's not set
95 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
96 $params['name_a_b'] = $params['label_a_b'];
97 }
98 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
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
106 $hook = empty($params['id']) ? 'create' : 'edit';
107 CRM_Utils_Hook::pre($hook, 'RelationshipType', CRM_Utils_Array::value('id', $params), $params);
108
109 $relationshipType->copyValues($params);
110 $relationshipType->save();
111
112 CRM_Utils_Hook::post($hook, 'RelationshipType', $relationshipType->id, $relationshipType);
113
114 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
115 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
116 CRM_Case_XMLProcessor::flushStaticCaches();
117 return $relationshipType;
118 }
119
120 /**
121 * Delete Relationship Types.
122 *
123 * @param int $relationshipTypeId
124 *
125 * @throws CRM_Core_Exception
126 * @return mixed
127 */
128 public static function del($relationshipTypeId) {
129 // make sure relationshipTypeId is an integer
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
132 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
133 throw new CRM_Core_Exception(ts('Invalid relationship type'));
134 }
135
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
143 // remove this relationship type from membership types
144 $mems = civicrm_api3('MembershipType', 'get', [
145 'relationship_type_id' => ['LIKE' => "%{$relationshipTypeId}%"],
146 'return' => ['id', 'relationship_type_id', 'relationship_direction'],
147 ]);
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 }
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 }
170
171 }