Merge pull request #18704 from mlutfy/partListingOutput
[civicrm-core.git] / CRM / Contact / BAO / GroupOrganization.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Contact_BAO_GroupOrganization extends CRM_Contact_DAO_GroupOrganization {
18
19 /**
20 * Class constructor.
21 */
22 public function __construct() {
23 parent::__construct();
24 }
25
26 /**
27 * Takes an associative array and creates a groupOrganization object.
28 *
29 * @param array $params
30 * (reference ) an assoc array of name/value pairs.
31 *
32 * @return CRM_Contact_DAO_GroupOrganization
33 */
34 public static function add(&$params) {
35 if (!empty($params['group_organization'])) {
36 $params['id'] = $params['group_organization'];
37 }
38 $dataExists = self::dataExists($params);
39 if (!$dataExists && empty($params['id'])) {
40 return NULL;
41 }
42 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
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 }
49 $groupOrganization->save();
50 return $groupOrganization;
51 }
52
53 /**
54 * Check if there is data to create the object.
55 *
56 * @param array $params
57 * (reference ) an assoc array of name/value pairs.
58 *
59 * @return bool
60 */
61 public static function dataExists($params) {
62 // return if no data present
63 if (!empty($params['organization_id']) && !empty($params['group_id'])) {
64 return TRUE;
65 }
66 return FALSE;
67 }
68
69 /**
70 * @param int $groupID
71 * @param $defaults
72 */
73 public static function retrieve($groupID, &$defaults) {
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 /**
83 * Method to check group organization relationship exist.
84 *
85 * @param int $contactID
86 *
87 * @return bool
88 */
89 public static function hasGroupAssociated($contactID) {
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 /**
100 * Delete Group Organization.
101 *
102 * @param int $groupOrganizationID
103 * Group organization id that needs to be deleted.
104 *
105 * @return int|null
106 * no of deleted group organization on success, false otherwise
107 */
108 public static function deleteGroupOrganization($groupOrganizationID) {
109 $results = NULL;
110 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
111 $groupOrganization->id = $groupOrganizationID;
112
113 $results = $groupOrganization->delete();
114
115 return $results;
116 }
117
118 }