Style - Remove @access
[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 (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 * @static
52 */
53 public static function retrieve(&$params, &$defaults) {
54 $relationshipType = new CRM_Contact_DAO_RelationshipType();
55 $relationshipType->copyValues($params);
56 if ($relationshipType->find(TRUE)) {
57 CRM_Core_DAO::storeValues($relationshipType, $defaults);
58 $relationshipType->free();
59 return $relationshipType;
60 }
61 return NULL;
62 }
63
64 /**
65 * Update the is_active flag in the db
66 *
67 * @param int $id id of the database record
68 * @param boolean $is_active value we want to set the is_active field
69 *
70 * @return Object DAO object on sucess, null otherwise
71 * @static
72 */
73 public static function setIsActive($id, $is_active) {
74 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
75 }
76
77 /**
78 * Add the relationship type in the db
79 *
80 * @param array $params (reference ) an assoc array of name/value pairs
81 * @param array $ids the array that holds all the db ids
82 *
83 * @return CRM_Contact_DAO_RelationshipType
84 * @static
85 *
86 */
87 public static function add(&$params, &$ids) {
88 //to change name, CRM-3336
89 if (empty($params['label_a_b']) && !empty($params['name_a_b'])) {
90 $params['label_a_b'] = $params['name_a_b'];
91 }
92
93 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
94 $params['label_b_a'] = $params['name_b_a'];
95 }
96
97 // set label to name if it's not set - but *only* for
98 // ADD action. CRM-3336 as part from (CRM-3522)
99 if (empty($ids['relationshipType'])) {
100 if (empty($params['name_a_b']) && !empty($params['label_a_b'])) {
101 $params['name_a_b'] = $params['label_a_b'];
102 }
103 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
104 $params['name_b_a'] = $params['label_b_a'];
105 }
106 }
107
108 // action is taken depending upon the mode
109 $relationshipType = new CRM_Contact_DAO_RelationshipType();
110
111 $relationshipType->copyValues($params);
112
113 // if label B to A is blank, insert the value label A to B for it
114 if (!strlen(trim($strName = CRM_Utils_Array::value('name_b_a', $params)))) {
115 $relationshipType->name_b_a = CRM_Utils_Array::value('name_a_b', $params);
116 }
117 if (!strlen(trim($strName = CRM_Utils_Array::value('label_b_a', $params)))) {
118 $relationshipType->label_b_a = CRM_Utils_Array::value('label_a_b', $params);
119 }
120
121 $relationshipType->id = CRM_Utils_Array::value('relationshipType', $ids);
122
123 $result = $relationshipType->save();
124
125 CRM_Core_PseudoConstant::relationshipType('label', TRUE);
126 CRM_Core_PseudoConstant::relationshipType('name', TRUE);
127 CRM_Case_XMLProcessor::flushStaticCaches();
128 return $result;
129 }
130
131 /**
132 * Delete Relationship Types
133 *
134 * @param int $relationshipTypeId
135 *
136 * @throws CRM_Core_Exception
137 * @return mixed
138 * @static
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
149 //check dependencies
150
151 // delete all relationships
152 $relationship = new CRM_Contact_DAO_Relationship();
153 $relationship->relationship_type_id = $relationshipTypeId;
154 $relationship->delete();
155
156 // set all membership_type to null
157 $query = "
158 UPDATE civicrm_membership_type
159 SET relationship_type_id = NULL
160 WHERE relationship_type_id = %1
161 ";
162 $params = array(1 => array(CRM_Core_DAO::VALUE_SEPARATOR . $relationshipTypeId . CRM_Core_DAO::VALUE_SEPARATOR, 'String'));
163 CRM_Core_DAO::executeQuery($query, $params);
164
165 //fixed for CRM-3323
166 $mappingField = new CRM_Core_DAO_MappingField();
167 $mappingField->relationship_type_id = $relationshipTypeId;
168 $mappingField->find();
169 while ($mappingField->fetch()) {
170 $mappingField->delete();
171 }
172
173 $relationshipType = new CRM_Contact_DAO_RelationshipType();
174 $relationshipType->id = $relationshipTypeId;
175 return $relationshipType->delete();
176 }
177 }
178