INFRA-132 - CRM/Event - phpcbf
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 public function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Fetch object based on array of properties
46 *
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.
51 *
52 * @return CRM_Contact_BAO_RelationshipType object
53 * @static
54 */
55 public static function retrieve(&$params, &$defaults) {
56 $relationshipType = new CRM_Contact_DAO_RelationshipType();
57 $relationshipType->copyValues($params);
58 if ($relationshipType->find(TRUE)) {
59 CRM_Core_DAO::storeValues($relationshipType, $defaults);
60 $relationshipType->free();
61 return $relationshipType;
62 }
63 return NULL;
64 }
65
66 /**
67 * Update the is_active flag in the db
68 *
69 * @param int $id
70 * Id of the database record.
71 * @param bool $is_active
72 * Value we want to set the is_active field.
73 *
74 * @return Object DAO object on sucess, null otherwise
75 * @static
76 */
77 public static function setIsActive($id, $is_active) {
78 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
79 }
80
81 /**
82 * Add the relationship type in the db
83 *
84 * @param array $params
85 * (reference ) an assoc array of name/value pairs.
86 * @param array $ids
87 * The array that holds all the db ids.
88 *
89 * @return CRM_Contact_DAO_RelationshipType
90 * @static
91 *
92 */
93 public static function add(&$params, &$ids) {
94 //to change name, CRM-3336
95 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
96 $params['label_a_b'] = $params['name_a_b'];
97 }
98
99 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
100 $params['label_b_a'] = $params['name_b_a'];
101 }
102
103 // set label to name if it's not set - but *only* for
104 // ADD action. CRM-3336 as part from (CRM-3522)
105 if (empty($ids['relationshipType'])) {
106 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
107 $params['name_a_b'] = $params['label_a_b'];
108 }
109 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
110 $params['name_b_a'] = $params['label_b_a'];
111 }
112 }
113
114 // action is taken depending upon the mode
115 $relationshipType = new CRM_Contact_DAO_RelationshipType();
116
117 $relationshipType->copyValues($params);
118
119 // if label B to A is blank, insert the value label A to B for it
120 if (!strlen(trim($strName = CRM_Utils_Array::value('name_b_a', $params)))) {
121 $relationshipType->name_b_a = CRM_Utils_Array::value('name_a_b', $params);
122 }
123 if (!strlen(trim($strName = CRM_Utils_Array::value('label_b_a', $params)))) {
124 $relationshipType->label_b_a = CRM_Utils_Array::value('label_a_b', $params);
125 }
126
127 $relationshipType->id = CRM_Utils_Array::value('relationshipType', $ids);
128
129 $result = $relationshipType->save();
130
131 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
132 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
133 CRM_Case_XMLProcessor::flushStaticCaches();
134 return $result;
135 }
136
137 /**
138 * Delete Relationship Types
139 *
140 * @param int $relationshipTypeId
141 *
142 * @throws CRM_Core_Exception
143 * @return mixed
144 * @static
145 */
146 public static function del($relationshipTypeId) {
147 // make sure relationshipTypeId is an integer
148 // @todo review this as most delete functions rely on the api & form layer for this
149 // or do a find first & throw error if no find
150 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
151 throw new CRM_Core_Exception(ts('Invalid relationship type'));
152 }
153
154
155 //check dependencies
156
157 // delete all relationships
158 $relationship = new CRM_Contact_DAO_Relationship();
159 $relationship->relationship_type_id = $relationshipTypeId;
160 $relationship->delete();
161
162 // set all membership_type to null
163 $query = "
164 UPDATE civicrm_membership_type
165 SET relationship_type_id = NULL
166 WHERE relationship_type_id = %1
167 ";
168 $params = array(1 => array(CRM_Core_DAO::VALUE_SEPARATOR . $relationshipTypeId . CRM_Core_DAO::VALUE_SEPARATOR, 'String'));
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 }
183 }