Merge pull request #19232 from eileenmcnaughton/friend
[civicrm-core.git] / CRM / Contact / BAO / GroupOrganization.php
CommitLineData
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
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17class CRM_Contact_BAO_GroupOrganization extends CRM_Contact_DAO_GroupOrganization {
18
19 /**
fe482240 20 * Class constructor.
6a488035 21 */
00be9182 22 public function __construct() {
6a488035
TO
23 parent::__construct();
24 }
25
26 /**
fe482240 27 * Takes an associative array and creates a groupOrganization object.
6a488035 28 *
77c5b619
TO
29 * @param array $params
30 * (reference ) an assoc array of name/value pairs.
6a488035 31 *
f8d78294 32 * @return CRM_Contact_DAO_GroupOrganization
6a488035 33 */
00be9182 34 public static function add(&$params) {
1237d8d7 35 if (!empty($params['group_organization'])) {
36 $params['id'] = $params['group_organization'];
37 }
38 $dataExists = self::dataExists($params);
39 if (!$dataExists && empty($params['id'])) {
6a488035
TO
40 return NULL;
41 }
42 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
1237d8d7 43 $groupOrganization->copyValues($params);
44 if (!isset($params['id'])) {
45 // we have ensured we have group_id & organization_id so we can do a find knowing that
46 // this can only find a matching record
47 $groupOrganization->find(TRUE);
48 }
6a488035
TO
49 $groupOrganization->save();
50 return $groupOrganization;
51 }
52
6a488035 53 /**
fe482240 54 * Check if there is data to create the object.
6a488035 55 *
77c5b619
TO
56 * @param array $params
57 * (reference ) an assoc array of name/value pairs.
6a488035 58 *
bed98343 59 * @return bool
6a488035 60 */
00be9182 61 public static function dataExists($params) {
6a488035 62 // return if no data present
8cc574cf 63 if (!empty($params['organization_id']) && !empty($params['group_id'])) {
6a488035
TO
64 return TRUE;
65 }
66 return FALSE;
67 }
68
86538308 69 /**
100fef9d 70 * @param int $groupID
86538308
EM
71 * @param $defaults
72 */
00be9182 73 public static function retrieve($groupID, &$defaults) {
6a488035
TO
74 $dao = new CRM_Contact_DAO_GroupOrganization();
75 $dao->group_id = $groupID;
76 if ($dao->find(TRUE)) {
77 $defaults['group_organization'] = $dao->id;
78 $defaults['organization_id'] = $dao->organization_id;
79 }
80 }
81
82 /**
fe482240 83 * Method to check group organization relationship exist.
6a488035 84 *
c490a46a 85 * @param int $contactID
6a488035 86 *
bed98343 87 * @return bool
6a488035 88 */
00be9182 89 public static function hasGroupAssociated($contactID) {
6a488035
TO
90 $orgID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_GroupOrganization',
91 $contactID, 'group_id', 'organization_id'
92 );
93 if ($orgID) {
94 return TRUE;
95 }
96 return FALSE;
97 }
98
99 /**
fe482240 100 * Delete Group Organization.
6a488035 101 *
77c5b619
TO
102 * @param int $groupOrganizationID
103 * Group organization id that needs to be deleted.
6a488035 104 *
72b3a70c
CW
105 * @return int|null
106 * no of deleted group organization on success, false otherwise
6a488035 107 */
00be9182 108 public static function deleteGroupOrganization($groupOrganizationID) {
6a488035
TO
109 $results = NULL;
110 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
111 $groupOrganization->id = $groupOrganizationID;
112
113 $results = $groupOrganization->delete();
114
115 return $results;
116 }
96025800 117
6a488035 118}