3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Contact_BAO_RelationshipType
extends CRM_Contact_DAO_RelationshipType
{
38 public function __construct() {
39 parent
::__construct();
43 * Fetch object based on array of properties.
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.
50 * @return CRM_Contact_BAO_RelationshipType
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;
63 * Update the is_active flag in the db.
66 * Id of the database record.
67 * @param bool $is_active
68 * Value we want to set the is_active field.
71 * true if we found and updated the object, else false
73 public static function setIsActive($id, $is_active) {
74 return CRM_Core_DAO
::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
78 * Add the relationship type in the db.
80 * @param array $params
82 * @return CRM_Contact_DAO_RelationshipType
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'];
90 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
91 $params['label_b_a'] = $params['name_b_a'];
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'];
98 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
99 $params['name_b_a'] = $params['label_b_a'];
103 // action is taken depending upon the mode
104 $relationshipType = new CRM_Contact_DAO_RelationshipType();
106 $hook = empty($params['id']) ?
'create' : 'edit';
107 CRM_Utils_Hook
::pre($hook, 'RelationshipType', CRM_Utils_Array
::value('id', $params), $params);
109 $relationshipType->copyValues($params);
110 $relationshipType->save();
112 CRM_Utils_Hook
::post($hook, 'RelationshipType', $relationshipType->id
, $relationshipType);
114 CRM_Core_PseudoConstant
::relationshipType('label', TRUE);
115 CRM_Core_PseudoConstant
::relationshipType('name', TRUE);
116 CRM_Case_XMLProcessor
::flushStaticCaches();
117 return $relationshipType;
121 * Delete Relationship Types.
123 * @param int $relationshipTypeId
125 * @throws CRM_Core_Exception
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'));
138 // delete all relationships
139 $relationship = new CRM_Contact_DAO_Relationship();
140 $relationship->relationship_type_id
= $relationshipTypeId;
141 $relationship->delete();
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'],
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);
159 $mappingField = new CRM_Core_DAO_MappingField();
160 $mappingField->relationship_type_id
= $relationshipTypeId;
161 $mappingField->find();
162 while ($mappingField->fetch()) {
163 $mappingField->delete();
166 $relationshipType = new CRM_Contact_DAO_RelationshipType();
167 $relationshipType->id
= $relationshipTypeId;
168 return $relationshipType->delete();