Merge pull request #4695 from colemanw/CRM-15550
[civicrm-core.git] / CRM / Contact / BAO / GroupOrganization.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_BAO_GroupOrganization extends CRM_Contact_DAO_GroupOrganization {
36
37 /**
38 * class constructor
39 */
40 function __construct() {
41 parent::__construct();
42 }
43
44 /**
45 * takes an associative array and creates a groupOrganization object
46 *
47 * @param array $params (reference ) an assoc array of name/value pairs
48 *
f8d78294 49 * @return CRM_Contact_DAO_GroupOrganization
6a488035
TO
50 * @access public
51 * @static
52 */
53 static function add(&$params) {
f8d78294
EM
54 $formattedValues = array();
55 self::formatValues($params, $formattedValues);
56 $dataExists = self::dataExists($formattedValues);
6a488035
TO
57 if (!$dataExists) {
58 return NULL;
59 }
60 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
f8d78294 61 $groupOrganization->copyValues($formattedValues);
c69fdfbb
E
62 // we have ensured we have group_id & organization_id so we can do a find knowing that
63 // this can only find a matching record
64 $groupOrganization->find(TRUE);
6a488035
TO
65 $groupOrganization->save();
66 return $groupOrganization;
67 }
68
69 /**
70 * Format the params
71 *
72 * @param array $params (reference ) an assoc array of name/value pairs
73 * @param array $formatedValues (reference ) an assoc array of name/value pairs
74 *
75 * @return void
76 * @access public
77 * @static
78 */
79 static function formatValues(&$params, &$formatedValues) {
a7488080 80 if (!empty($params['group_organization'])) {
6a488035
TO
81 $formatedValues['id'] = $params['group_organization'];
82 }
83
a7488080 84 if (!empty($params['group_id'])) {
6a488035
TO
85 $formatedValues['group_id'] = $params['group_id'];
86 }
87
a7488080 88 if (!empty($params['organization_id'])) {
6a488035
TO
89 $formatedValues['organization_id'] = $params['organization_id'];
90 }
91 }
92
93 /**
94 * Check if there is data to create the object
95 *
96 * @param array $params (reference ) an assoc array of name/value pairs
97 *
98 * @return boolean
99 * @access public
100 * @static
101 */
102 static function dataExists($params) {
103 // return if no data present
8cc574cf 104 if (!empty($params['organization_id']) && !empty($params['group_id'])) {
6a488035
TO
105 return TRUE;
106 }
107 return FALSE;
108 }
109
86538308
EM
110 /**
111 * @param $groupID
112 * @param $defaults
113 */
6a488035
TO
114 static function retrieve($groupID, &$defaults) {
115 $dao = new CRM_Contact_DAO_GroupOrganization();
116 $dao->group_id = $groupID;
117 if ($dao->find(TRUE)) {
118 $defaults['group_organization'] = $dao->id;
119 $defaults['organization_id'] = $dao->organization_id;
120 }
121 }
122
123 /**
124 * Method to check group organization relationship exist
125 *
6c8f6e67
EM
126 * @param $contactID
127 *
128 * @internal param int $contactId
6a488035
TO
129 *
130 * @return boolean
131 * @access public
132 * @static
133 */
134 static function hasGroupAssociated($contactID) {
135 $orgID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_GroupOrganization',
136 $contactID, 'group_id', 'organization_id'
137 );
138 if ($orgID) {
139 return TRUE;
140 }
141 return FALSE;
142 }
143
144 /**
145 * Function to delete Group Organization
146 *
147 * @param int $groupOrganizationID group organization id that needs to be deleted
148 *
fd31fa4c 149 * @return mixed|null $results no of deleted group organization on success, false otherwise@access public
6a488035
TO
150 */
151 static function deleteGroupOrganization($groupOrganizationID) {
152 $results = NULL;
153 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
154 $groupOrganization->id = $groupOrganizationID;
155
156 $results = $groupOrganization->delete();
157
158 return $results;
159 }
160}
161