3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2017
33 class CRM_Contact_BAO_RelationshipType
extends CRM_Contact_DAO_RelationshipType
{
38 public function __construct() {
39 parent
::__construct();
43 * Fetch object based on array of properties.
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.
50 * @return CRM_Contact_BAO_RelationshipType
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;
64 * Update the is_active flag in the db.
67 * Id of the database record.
68 * @param bool $is_active
69 * Value we want to set the is_active field.
72 * DAO object on success, null otherwise
74 public static function setIsActive($id, $is_active) {
75 return CRM_Core_DAO
::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active);
79 * Add the relationship type in the db.
81 * @param array $params
82 * (reference ) an assoc array of name/value pairs.
84 * The array that holds all the db ids.
86 * @return CRM_Contact_DAO_RelationshipType
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'];
94 if (empty($params['label_b_a']) && !empty($params['name_b_a'])) {
95 $params['label_b_a'] = $params['name_b_a'];
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'];
104 if (empty($params['name_b_a']) && !empty($params['label_b_a'])) {
105 $params['name_b_a'] = $params['label_b_a'];
109 // action is taken depending upon the mode
110 $relationshipType = new CRM_Contact_DAO_RelationshipType();
112 $relationshipType->copyValues($params);
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);
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);
122 $relationshipType->id
= CRM_Utils_Array
::value('relationshipType', $ids);
124 $result = $relationshipType->save();
126 CRM_Core_PseudoConstant
::relationshipType('label', TRUE);
127 CRM_Core_PseudoConstant
::relationshipType('name', TRUE);
128 CRM_Case_XMLProcessor
::flushStaticCaches();
133 * Delete Relationship Types.
135 * @param int $relationshipTypeId
137 * @throws CRM_Core_Exception
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'));
150 // delete all relationships
151 $relationship = new CRM_Contact_DAO_Relationship();
152 $relationship->relationship_type_id
= $relationshipTypeId;
153 $relationship->delete();
155 // remove this relationship type from membership types
156 $mems = civicrm_api3('MembershipType', 'get', array(
157 'relationship_type_id' => array('LIKE' => "%{$relationshipTypeId}%"),
158 'return' => array('id', 'relationship_type_id', 'relationship_direction'),
160 foreach ($mems['values'] as $membershipTypeId => $membershipType) {
161 $pos = array_search($relationshipTypeId, $membershipType['relationship_type_id']);
162 // Api call may have returned false positives but currently the relationship_type_id uses
163 // nonstandard serialization which makes anything more accurate impossible.
164 if ($pos !== FALSE) {
165 unset($membershipType['relationship_type_id'][$pos], $membershipType['relationship_direction'][$pos]);
166 civicrm_api3('MembershipType', 'create', $membershipType);
171 $mappingField = new CRM_Core_DAO_MappingField();
172 $mappingField->relationship_type_id
= $relationshipTypeId;
173 $mappingField->find();
174 while ($mappingField->fetch()) {
175 $mappingField->delete();
178 $relationshipType = new CRM_Contact_DAO_RelationshipType();
179 $relationshipType->id
= $relationshipTypeId;
180 return $relationshipType->delete();