comment fixes
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 */
33class 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
155 // set all membership_type to null
156 $query = "
157UPDATE civicrm_membership_type
158 SET relationship_type_id = NULL
159 WHERE relationship_type_id = %1
160";
353ffa53
TO
161 $params = array(
162 1 => array(
163 CRM_Core_DAO::VALUE_SEPARATOR . $relationshipTypeId . CRM_Core_DAO::VALUE_SEPARATOR,
28a04ea9 164 'String',
165 ),
353ffa53 166 );
6a488035
TO
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 }
96025800 181
6a488035 182}