Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
6a488035 TO |
32 | */ |
33 | class 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); | |
57 | $relationshipType->free(); | |
58 | return $relationshipType; | |
59 | } | |
60 | return NULL; | |
61 | } | |
62 | ||
63 | /** | |
fe482240 | 64 | * Update the is_active flag in the db. |
6a488035 | 65 | * |
77c5b619 TO |
66 | * @param int $id |
67 | * Id of the database record. | |
68 | * @param bool $is_active | |
69 | * Value we want to set the is_active field. | |
6a488035 | 70 | * |
a6c01b45 | 71 | * @return Object |
b44e3f84 | 72 | * DAO object on success, null otherwise |
6a488035 | 73 | */ |
00be9182 | 74 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
75 | return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active); |
76 | } | |
77 | ||
78 | /** | |
fe482240 | 79 | * Add the relationship type in the db. |
6a488035 | 80 | * |
77c5b619 TO |
81 | * @param array $params |
82 | * (reference ) an assoc array of name/value pairs. | |
83 | * @param array $ids | |
84 | * The array that holds all the db ids. | |
6a488035 | 85 | * |
c490a46a | 86 | * @return CRM_Contact_DAO_RelationshipType |
6a488035 | 87 | */ |
00be9182 | 88 | public static function add(&$params, &$ids) { |
6a488035 | 89 | //to change name, CRM-3336 |
8cc574cf | 90 | if (empty($params['label_a_b']) && !empty($params['name_a_b'])) { |
6a488035 TO |
91 | $params['label_a_b'] = $params['name_a_b']; |
92 | } | |
93 | ||
8cc574cf | 94 | if (empty($params['label_b_a']) && !empty($params['name_b_a'])) { |
6a488035 TO |
95 | $params['label_b_a'] = $params['name_b_a']; |
96 | } | |
97 | ||
98 | // set label to name if it's not set - but *only* for | |
99 | // ADD action. CRM-3336 as part from (CRM-3522) | |
a7488080 | 100 | if (empty($ids['relationshipType'])) { |
8cc574cf | 101 | if (empty($params['name_a_b']) && !empty($params['label_a_b'])) { |
6a488035 TO |
102 | $params['name_a_b'] = $params['label_a_b']; |
103 | } | |
8cc574cf | 104 | if (empty($params['name_b_a']) && !empty($params['label_b_a'])) { |
6a488035 TO |
105 | $params['name_b_a'] = $params['label_b_a']; |
106 | } | |
107 | } | |
108 | ||
109 | // action is taken depending upon the mode | |
110 | $relationshipType = new CRM_Contact_DAO_RelationshipType(); | |
111 | ||
112 | $relationshipType->copyValues($params); | |
113 | ||
114 | // if label B to A is blank, insert the value label A to B for it | |
115 | if (!strlen(trim($strName = CRM_Utils_Array::value('name_b_a', $params)))) { | |
116 | $relationshipType->name_b_a = CRM_Utils_Array::value('name_a_b', $params); | |
117 | } | |
118 | if (!strlen(trim($strName = CRM_Utils_Array::value('label_b_a', $params)))) { | |
119 | $relationshipType->label_b_a = CRM_Utils_Array::value('label_a_b', $params); | |
120 | } | |
121 | ||
122 | $relationshipType->id = CRM_Utils_Array::value('relationshipType', $ids); | |
123 | ||
4bc6c777 TO |
124 | $result = $relationshipType->save(); |
125 | ||
126 | CRM_Core_PseudoConstant::relationshipType('label', TRUE); | |
127 | CRM_Core_PseudoConstant::relationshipType('name', TRUE); | |
128 | CRM_Case_XMLProcessor::flushStaticCaches(); | |
129 | return $result; | |
6a488035 TO |
130 | } |
131 | ||
132 | /** | |
fe482240 | 133 | * Delete Relationship Types. |
6a488035 TO |
134 | * |
135 | * @param int $relationshipTypeId | |
77b97be7 EM |
136 | * |
137 | * @throws CRM_Core_Exception | |
138 | * @return mixed | |
6a488035 | 139 | */ |
00be9182 | 140 | public static function del($relationshipTypeId) { |
6a488035 | 141 | // make sure relationshipTypeId is an integer |
b2402735 | 142 | // @todo review this as most delete functions rely on the api & form layer for this |
143 | // or do a find first & throw error if no find | |
6a488035 | 144 | if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) { |
b2402735 | 145 | throw new CRM_Core_Exception(ts('Invalid relationship type')); |
6a488035 TO |
146 | } |
147 | ||
6a488035 TO |
148 | //check dependencies |
149 | ||
150 | // delete all relationships | |
151 | $relationship = new CRM_Contact_DAO_Relationship(); | |
152 | $relationship->relationship_type_id = $relationshipTypeId; | |
153 | $relationship->delete(); | |
154 | ||
baccd59e CW |
155 | // remove this relationship type from membership types |
156 | $mems = civicrm_api3('MembershipType', 'get', array( | |
157 | 'relationship_type_id' => array('LIKE' => "%{$relationshipTypeId}%"), | |
158 | 'return' => array('id', 'relationship_type_id', 'relationship_direction'), | |
159 | )); | |
160 | foreach ($mems['values'] as $membershipTypeId => $membershipType) { | |
161 | $pos = array_search($relationshipTypeId, $membershipType['relationship_type_id']); | |
162 | // Api call may have returned false positives but currently the relationship_type_id uses | |
163 | // nonstandard serialization which makes anything more accurate impossible. | |
164 | if ($pos !== FALSE) { | |
165 | unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]); | |
166 | civicrm_api3('MembershipType', 'create', $membershipType); | |
167 | } | |
168 | } | |
6a488035 TO |
169 | |
170 | //fixed for CRM-3323 | |
171 | $mappingField = new CRM_Core_DAO_MappingField(); | |
172 | $mappingField->relationship_type_id = $relationshipTypeId; | |
173 | $mappingField->find(); | |
174 | while ($mappingField->fetch()) { | |
175 | $mappingField->delete(); | |
176 | } | |
177 | ||
178 | $relationshipType = new CRM_Contact_DAO_RelationshipType(); | |
179 | $relationshipType->id = $relationshipTypeId; | |
180 | return $relationshipType->delete(); | |
181 | } | |
96025800 | 182 | |
6a488035 | 183 | } |