CRM-14106 - Regex targeting the first part of if statements
[civicrm-core.git] / CRM / Contact / BAO / RelationshipType.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
32 * $Id$
33 *
34 */
35class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType {
36
37 /**
38 * class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * Takes a bunch of params that are needed to match certain criteria and
46 * retrieves the relevant objects. Typically the valid params are only
47 * contact_id. We'll tweak this function to be more full featured over a period
48 * of time. This is the inverse function of create. It also stores all the retrieved
49 * values in the default array
50 *
51 * @param array $params (reference ) an assoc array of name/value pairs
52 * @param array $defaults (reference ) an assoc array to hold the flattened values
53 *
54 * @return object CRM_Contact_BAO_RelationshipType object
55 * @access public
56 * @static
57 */
58 static function retrieve(&$params, &$defaults) {
59 $relationshipType = new CRM_Contact_DAO_RelationshipType();
60 $relationshipType->copyValues($params);
61 if ($relationshipType->find(TRUE)) {
62 CRM_Core_DAO::storeValues($relationshipType, $defaults);
63 $relationshipType->free();
64 return $relationshipType;
65 }
66 return NULL;
67 }
68
69 /**
70 * update the is_active flag in the db
71 *
72 * @param int $id id of the database record
73 * @param boolean $is_active value we want to set the is_active field
74 *
75 * @return Object DAO object on sucess, null otherwise
76 * @static
77 */
78 static function setIsActive($id, $is_active) {
79 return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
80 }
81
82 /**
83 * Function to add the relationship type in the db
84 *
85 * @param array $params (reference ) an assoc array of name/value pairs
86 * @param array $ids the array that holds all the db ids
87 *
88 * @return object CRM_Contact_DAO_RelationshipType
89 * @access public
90 * @static
91 *
92 */
93 static function add(&$params, &$ids) {
94 //to change name, CRM-3336
a7488080 95 if (empty($params['label_a_b']) && CRM_Utils_Array::value('name_a_b', $params)) {
6a488035
TO
96 $params['label_a_b'] = $params['name_a_b'];
97 }
98
a7488080 99 if (empty($params['label_b_a']) && CRM_Utils_Array::value('name_b_a', $params)) {
6a488035
TO
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)
a7488080
CW
105 if (empty($ids['relationshipType'])) {
106 if (empty($params['name_a_b']) && CRM_Utils_Array::value('label_a_b', $params)) {
6a488035
TO
107 $params['name_a_b'] = $params['label_a_b'];
108 }
a7488080 109 if (empty($params['name_b_a']) && CRM_Utils_Array::value('label_b_a', $params)) {
6a488035
TO
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 return $relationshipType->save();
130 }
131
132 /**
133 * Function to delete Relationship Types
134 *
135 * @param int $relationshipTypeId
136 * @static
137 */
138 static function del($relationshipTypeId) {
139 // make sure relationshipTypeId is an integer
b2402735 140 // @todo review this as most delete functions rely on the api & form layer for this
141 // or do a find first & throw error if no find
6a488035 142 if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) {
b2402735 143 throw new CRM_Core_Exception(ts('Invalid relationship type'));
6a488035
TO
144 }
145
146
147 //check dependencies
148
149 // delete all relationships
150 $relationship = new CRM_Contact_DAO_Relationship();
151 $relationship->relationship_type_id = $relationshipTypeId;
152 $relationship->delete();
153
154 // set all membership_type to null
155 $query = "
156UPDATE civicrm_membership_type
157 SET relationship_type_id = NULL
158 WHERE relationship_type_id = %1
159";
69d1466d 160 $params = array(1 => array(CRM_Core_DAO::VALUE_SEPARATOR . $relationshipTypeId . CRM_Core_DAO::VALUE_SEPARATOR, 'String'));
6a488035
TO
161 CRM_Core_DAO::executeQuery($query, $params);
162
163 //fixed for CRM-3323
164 $mappingField = new CRM_Core_DAO_MappingField();
165 $mappingField->relationship_type_id = $relationshipTypeId;
166 $mappingField->find();
167 while ($mappingField->fetch()) {
168 $mappingField->delete();
169 }
170
171 $relationshipType = new CRM_Contact_DAO_RelationshipType();
172 $relationshipType->id = $relationshipTypeId;
173 return $relationshipType->delete();
174 }
175}
176