Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
dead2d66 | 12 | use Civi\Api4\Relationship; |
f00a0870 | 13 | use Civi\Api4\RelationshipType; |
dead2d66 | 14 | use Civi\Core\Event\PreEvent; |
f00a0870 | 15 | |
6a488035 TO |
16 | /** |
17 | * | |
18 | * @package CRM | |
ca5cec67 | 19 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 | 20 | */ |
6552bd20 | 21 | class CRM_Contact_BAO_RelationshipType extends CRM_Contact_DAO_RelationshipType implements \Civi\Test\HookInterface { |
6a488035 | 22 | |
6a488035 | 23 | /** |
4f940304 | 24 | * Retrieve DB object and copy to defaults array. |
6a488035 | 25 | * |
77c5b619 | 26 | * @param array $params |
4f940304 | 27 | * Array of criteria values. |
77c5b619 | 28 | * @param array $defaults |
4f940304 | 29 | * Array to be populated with found values. |
6a488035 | 30 | * |
4f940304 CW |
31 | * @return self|null |
32 | * The DAO object, if found. | |
33 | * | |
34 | * @deprecated | |
6a488035 | 35 | */ |
4f940304 CW |
36 | public static function retrieve($params, &$defaults) { |
37 | return self::commonRetrieve(self::class, $params, $defaults); | |
6a488035 TO |
38 | } |
39 | ||
40 | /** | |
fe482240 | 41 | * Update the is_active flag in the db. |
6a488035 | 42 | * |
77c5b619 TO |
43 | * @param int $id |
44 | * Id of the database record. | |
45 | * @param bool $is_active | |
46 | * Value we want to set the is_active field. | |
6a488035 | 47 | * |
8a4fede3 | 48 | * @return bool |
49 | * true if we found and updated the object, else false | |
6a488035 | 50 | */ |
00be9182 | 51 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
52 | return CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_RelationshipType', $id, 'is_active', $is_active); |
53 | } | |
54 | ||
55 | /** | |
fe482240 | 56 | * Add the relationship type in the db. |
6a488035 | 57 | * |
77c5b619 | 58 | * @param array $params |
6a488035 | 59 | * |
c490a46a | 60 | * @return CRM_Contact_DAO_RelationshipType |
6a488035 | 61 | */ |
780df37b CW |
62 | public static function add($params) { |
63 | if (empty($params['id'])) { | |
64 | // Set name to label if not set | |
65 | if (empty($params['label_a_b']) && !empty($params['name_a_b'])) { | |
66 | $params['label_a_b'] = $params['name_a_b']; | |
67 | } | |
68 | if (empty($params['label_b_a']) && !empty($params['name_b_a'])) { | |
69 | $params['label_b_a'] = $params['name_b_a']; | |
70 | } | |
6a488035 | 71 | |
780df37b | 72 | // set label to name if it's not set |
8cc574cf | 73 | if (empty($params['name_a_b']) && !empty($params['label_a_b'])) { |
6a488035 TO |
74 | $params['name_a_b'] = $params['label_a_b']; |
75 | } | |
8cc574cf | 76 | if (empty($params['name_b_a']) && !empty($params['label_b_a'])) { |
6a488035 TO |
77 | $params['name_b_a'] = $params['label_b_a']; |
78 | } | |
79 | } | |
80 | ||
81 | // action is taken depending upon the mode | |
0419bf7b | 82 | $relationshipType = self::writeRecord($params); |
4bc6c777 TO |
83 | |
84 | CRM_Core_PseudoConstant::relationshipType('label', TRUE); | |
85 | CRM_Core_PseudoConstant::relationshipType('name', TRUE); | |
1ac9bb56 | 86 | CRM_Core_PseudoConstant::flush(); |
4bc6c777 | 87 | CRM_Case_XMLProcessor::flushStaticCaches(); |
780df37b | 88 | return $relationshipType; |
6a488035 TO |
89 | } |
90 | ||
91 | /** | |
fe482240 | 92 | * Delete Relationship Types. |
6a488035 TO |
93 | * |
94 | * @param int $relationshipTypeId | |
77b97be7 | 95 | * |
6552bd20 | 96 | * @deprecated |
77b97be7 EM |
97 | * @throws CRM_Core_Exception |
98 | * @return mixed | |
6a488035 | 99 | */ |
00be9182 | 100 | public static function del($relationshipTypeId) { |
6a488035 | 101 | // make sure relationshipTypeId is an integer |
b2402735 | 102 | // @todo review this as most delete functions rely on the api & form layer for this |
103 | // or do a find first & throw error if no find | |
6a488035 | 104 | if (!CRM_Utils_Rule::positiveInteger($relationshipTypeId)) { |
b2402735 | 105 | throw new CRM_Core_Exception(ts('Invalid relationship type')); |
6a488035 | 106 | } |
6552bd20 CW |
107 | return static::deleteRecord(['id' => $relationshipTypeId]); |
108 | } | |
6a488035 | 109 | |
6552bd20 CW |
110 | /** |
111 | * Callback for hook_civicrm_pre(). | |
dead2d66 | 112 | * |
6552bd20 | 113 | * @param \Civi\Core\Event\PreEvent $event |
dead2d66 EM |
114 | * |
115 | * @throws \API_Exception | |
116 | * @throws \Civi\API\Exception\UnauthorizedException | |
6552bd20 | 117 | */ |
dead2d66 | 118 | public static function self_hook_civicrm_pre(PreEvent $event): void { |
6552bd20 CW |
119 | if ($event->action === 'delete') { |
120 | // need to delete all option value field before deleting group | |
dead2d66 | 121 | Relationship::delete(FALSE) |
6552bd20 CW |
122 | ->addWhere('relationship_type_id', '=', $event->id) |
123 | ->execute(); | |
6a488035 | 124 | } |
6a488035 | 125 | } |
96025800 | 126 | |
f00a0870 EM |
127 | /** |
128 | * Get the id of the employee relationship, checking it is valid. | |
129 | * | |
130 | * @return int|string | |
131 | * | |
132 | * @throws \CRM_Core_Exception | |
133 | */ | |
134 | public static function getEmployeeRelationshipTypeID(): int { | |
135 | try { | |
136 | if (!Civi::cache('metadata')->has(__CLASS__ . __FUNCTION__)) { | |
137 | $relationship = RelationshipType::get(FALSE) | |
138 | ->addWhere('name_a_b', '=', 'Employee of') | |
139 | ->addWhere('contact_type_a', '=', 'Individual') | |
140 | ->addWhere('contact_type_b', '=', 'Organization') | |
141 | ->addSelect('id')->execute()->first(); | |
142 | if (empty($relationship)) { | |
143 | throw new API_Exception('no valid relationship'); | |
144 | } | |
145 | Civi::cache('metadata')->set(__CLASS__ . __FUNCTION__, $relationship['id']); | |
146 | } | |
147 | } | |
148 | catch (API_Exception $e) { | |
149 | throw new CRM_Core_Exception(ts("You seem to have deleted the relationship type 'Employee of'")); | |
150 | } | |
151 | return Civi::cache('metadata')->get(__CLASS__ . __FUNCTION__); | |
152 | } | |
153 | ||
6a488035 | 154 | } |