Merge pull request #4724 from atif-shaikh/CRM-15598
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType {
36
37 /**
38 * Class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Fetch object based on array of properties
46 *
47 * @param array $params (reference ) an assoc array of name/value pairs
48 * @param array $defaults (reference ) an assoc array to hold the flattened values
49 *
50 * @return CRM_Contact_BAO_RelationshipType object
51 * @access public
52 * @static
53 */
54 static function retrieve(&$params, &$defaults) {
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 /**
66 * Update the is_active flag in the db
67 *
68 * @param int $id id of the database record
69 * @param boolean $is_active value we want to set the is_active field
70 *
71 * @return Object DAO object on sucess, null otherwise
72 * @static
73 */
74 static function setIsActive($id, $is_active) {
75 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
76 }
77
78 /**
79 * Add the relationship type in the db
80 *
81 * @param array $params (reference ) an assoc array of name/value pairs
82 * @param array $ids the array that holds all the db ids
83 *
84 * @return CRM_Contact_DAO_RelationshipType
85 * @access public
86 * @static
87 *
88 */
89 static function add(&$params, &$ids) {
90 //to change name, CRM-3336
91 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
92 $params['label_a_b'] = $params['name_a_b'];
93 }
94
95 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
96 $params['label_b_a'] = $params['name_b_a'];
97 }
98
99 // set label to name if it's not set - but *only* for
100 // ADD action. CRM-3336 as part from (CRM-3522)
101 if (empty($ids['relationshipType'])) {
102 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
103 $params['name_a_b'] = $params['label_a_b'];
104 }
105 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
106 $params['name_b_a'] = $params['label_b_a'];
107 }
108 }
109
110 // action is taken depending upon the mode
111 $relationshipType = new CRM_Contact_DAO_RelationshipType();
112
113 $relationshipType->copyValues($params);
114
115 // if label B to A is blank, insert the value label A to B for it
116 if (!strlen(trim($strName = CRM_Utils_Array::value('name_b_a', $params)))) {
117 $relationshipType->name_b_a = CRM_Utils_Array::value('name_a_b', $params);
118 }
119 if (!strlen(trim($strName = CRM_Utils_Array::value('label_b_a', $params)))) {
120 $relationshipType->label_b_a = CRM_Utils_Array::value('label_a_b', $params);
121 }
122
123 $relationshipType->id = CRM_Utils_Array::value('relationshipType', $ids);
124
125 $result = $relationshipType->save();
126
127 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
128 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
129 CRM_Case_XMLProcessor::flushStaticCaches();
130 return $result;
131 }
132
133 /**
134 * Delete Relationship Types
135 *
136 * @param int $relationshipTypeId
137 *
138 * @throws CRM_Core_Exception
139 * @return mixed
140 * @static
141 */
142 static function del($relationshipTypeId) {
143 // make sure relationshipTypeId is an integer
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
146 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
147 throw new CRM_Core_Exception(ts('Invalid relationship type'));
148 }
149
150
151 //check dependencies
152
153 // delete all relationships
154 $relationship = new CRM_Contact_DAO_Relationship();
155 $relationship->relationship_type_id = $relationshipTypeId;
156 $relationship->delete();
157
158 // set all membership_type to null
159 $query = "
160 UPDATE civicrm_membership_type
161 SET relationship_type_id = NULL
162 WHERE relationship_type_id = %1
163 ";
164 $params = array(1 => array(CRM_Core_DAO::VALUE_SEPARATOR . $relationshipTypeId . CRM_Core_DAO::VALUE_SEPARATOR, 'String'));
165 CRM_Core_DAO::executeQuery($query, $params);
166
167 //fixed for CRM-3323
168 $mappingField = new CRM_Core_DAO_MappingField();
169 $mappingField->relationship_type_id = $relationshipTypeId;
170 $mappingField->find();
171 while ($mappingField->fetch()) {
172 $mappingField->delete();
173 }
174
175 $relationshipType = new CRM_Contact_DAO_RelationshipType();
176 $relationshipType->id = $relationshipTypeId;
177 return $relationshipType->delete();
178 }
179 }
180