Merge pull request #6680 from colemanw/cleanup
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 $relationshipType->free();
58 return $relationshipType;
59 }
60 return NULL;
61 }
62
63 /**
64 * Update the is_active flag in the db.
65 *
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.
70 *
71 * @return Object
72 * DAO object on success, null otherwise
73 */
74 public 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
82 * (reference ) an assoc array of name/value pairs.
83 * @param array $ids
84 * The array that holds all the db ids.
85 *
86 * @return CRM_Contact_DAO_RelationshipType
87 */
88 public static function add(&$params, &$ids) {
89 //to change name, CRM-3336
90 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
91 $params['label_a_b'] = $params['name_a_b'];
92 }
93
94 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
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)
100 if (empty($ids['relationshipType'])) {
101 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
102 $params['name_a_b'] = $params['label_a_b'];
103 }
104 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
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
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;
130 }
131
132 /**
133 * Delete Relationship Types.
134 *
135 * @param int $relationshipTypeId
136 *
137 * @throws CRM_Core_Exception
138 * @return mixed
139 */
140 public static function del($relationshipTypeId) {
141 // make sure relationshipTypeId is an integer
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
144 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
145 throw new CRM_Core_Exception(ts('Invalid relationship type'));
146 }
147
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
155 // set all membership_type to null
156 $query = "
157 UPDATE civicrm_membership_type
158 SET relationship_type_id = NULL
159 WHERE relationship_type_id = %1
160 ";
161 $params = array(
162 1 => array(
163 CRM_Core_DAO::VALUE_SEPARATOR . $relationshipTypeId . CRM_Core_DAO::VALUE_SEPARATOR,
164 'String',
165 ),
166 );
167 CRM_Core_DAO::executeQuery($query, $params);
168
169 //fixed for CRM-3323
170 $mappingField = new CRM_Core_DAO_MappingField();
171 $mappingField->relationship_type_id = $relationshipTypeId;
172 $mappingField->find();
173 while ($mappingField->fetch()) {
174 $mappingField->delete();
175 }
176
177 $relationshipType = new CRM_Contact_DAO_RelationshipType();
178 $relationshipType->id = $relationshipTypeId;
179 return $relationshipType->delete();
180 }
181
182 }