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