INFRA-132 - Drupal.Classes.ClassDeclaration
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType {
36
37 /**
100fef9d 38 * Class constructor
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
43
44 /**
c490a46a 45 * Fetch object based on array of properties
6a488035 46 *
77c5b619
TO
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
49 * @param array $defaults
50 * (reference ) an assoc array to hold the flattened values.
6a488035 51 *
16b10e64 52 * @return CRM_Contact_BAO_RelationshipType
6a488035 53 */
00be9182 54 public static function retrieve(&$params, &$defaults) {
6a488035
TO
55 $relationshipType = new CRM_Contact_DAO_RelationshipType();
56 $relationshipType->copyValues($params);
57 if ($relationshipType->find(TRUE)) {
58 CRM_Core_DAO::storeValues($relationshipType, $defaults);
59 $relationshipType->free();
60 return $relationshipType;
61 }
62 return NULL;
63 }
64
65 /**
100fef9d 66 * Update the is_active flag in the db
6a488035 67 *
77c5b619
TO
68 * @param int $id
69 * Id of the database record.
70 * @param bool $is_active
71 * Value we want to set the is_active field.
6a488035 72 *
a6c01b45
CW
73 * @return Object
74 * DAO object on sucess, null otherwise
6a488035 75 */
00be9182 76 public static function setIsActive($id, $is_active) {
6a488035
TO
77 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
78 }
79
80 /**
100fef9d 81 * Add the relationship type in the db
6a488035 82 *
77c5b619
TO
83 * @param array $params
84 * (reference ) an assoc array of name/value pairs.
85 * @param array $ids
86 * The array that holds all the db ids.
6a488035 87 *
c490a46a 88 * @return CRM_Contact_DAO_RelationshipType
6a488035 89 */
00be9182 90 public static function add(&$params, &$ids) {
6a488035 91 //to change name, CRM-3336
8cc574cf 92 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
6a488035
TO
93 $params['label_a_b'] = $params['name_a_b'];
94 }
95
8cc574cf 96 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
6a488035
TO
97 $params['label_b_a'] = $params['name_b_a'];
98 }
99
100 // set label to name if it's not set - but *only* for
101 // ADD action. CRM-3336 as part from (CRM-3522)
a7488080 102 if (empty($ids['relationshipType'])) {
8cc574cf 103 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
6a488035
TO
104 $params['name_a_b'] = $params['label_a_b'];
105 }
8cc574cf 106 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
6a488035
TO
107 $params['name_b_a'] = $params['label_b_a'];
108 }
109 }
110
111 // action is taken depending upon the mode
112 $relationshipType = new CRM_Contact_DAO_RelationshipType();
113
114 $relationshipType->copyValues($params);
115
116 // if label B to A is blank, insert the value label A to B for it
117 if (!strlen(trim($strName = CRM_Utils_Array::value('name_b_a', $params)))) {
118 $relationshipType->name_b_a = CRM_Utils_Array::value('name_a_b', $params);
119 }
120 if (!strlen(trim($strName = CRM_Utils_Array::value('label_b_a', $params)))) {
121 $relationshipType->label_b_a = CRM_Utils_Array::value('label_a_b', $params);
122 }
123
124 $relationshipType->id = CRM_Utils_Array::value('relationshipType', $ids);
125
4bc6c777
TO
126 $result = $relationshipType->save();
127
128 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
129 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
130 CRM_Case_XMLProcessor::flushStaticCaches();
131 return $result;
6a488035
TO
132 }
133
134 /**
100fef9d 135 * Delete Relationship Types
6a488035
TO
136 *
137 * @param int $relationshipTypeId
77b97be7
EM
138 *
139 * @throws CRM_Core_Exception
140 * @return mixed
6a488035 141 */
00be9182 142 public static function del($relationshipTypeId) {
6a488035 143 // make sure relationshipTypeId is an integer
b2402735 144 // @todo review this as most delete functions rely on the api & form layer for this
145 // or do a find first & throw error if no find
6a488035 146 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
b2402735 147 throw new CRM_Core_Exception(ts('Invalid relationship type'));
6a488035
TO
148 }
149
6a488035
TO
150 //check dependencies
151
152 // delete all relationships
153 $relationship = new CRM_Contact_DAO_Relationship();
154 $relationship->relationship_type_id = $relationshipTypeId;
155 $relationship->delete();
156
157 // set all membership_type to null
158 $query = "
159UPDATE civicrm_membership_type
160 SET relationship_type_id = NULL
161 WHERE relationship_type_id = %1
162";
353ffa53
TO
163 $params = array(
164 1 => array(
165 CRM_Core_DAO::VALUE_SEPARATOR . $relationshipTypeId . CRM_Core_DAO::VALUE_SEPARATOR,
28a04ea9 166 'String',
167 ),
353ffa53 168 );
6a488035
TO
169 CRM_Core_DAO::executeQuery($query, $params);
170
171 //fixed for CRM-3323
172 $mappingField = new CRM_Core_DAO_MappingField();
173 $mappingField->relationship_type_id = $relationshipTypeId;
174 $mappingField->find();
175 while ($mappingField->fetch()) {
176 $mappingField->delete();
177 }
178
179 $relationshipType = new CRM_Contact_DAO_RelationshipType();
180 $relationshipType->id = $relationshipTypeId;
181 return $relationshipType->delete();
182 }
96025800 183
6a488035 184}