Merge pull request #4035 from eileenmcnaughton/CRM-14824
[civicrm-core.git] / CRM / Contact / BAO / GroupOrganization.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
232624b1 4 | CiviCRM version 4.4 |
6a488035
TO
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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
31 * @copyright CiviCRM LLC (c) 2004-2013
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 *
49 * @return void
50 * @access public
51 * @static
52 */
53 static function add(&$params) {
54 $formatedValues = array();
55 self::formatValues($params, $formatedValues);
56 $dataExists = self::dataExists($formatedValues);
57 if (!$dataExists) {
58 return NULL;
59 }
60 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
61 $groupOrganization->copyValues($formatedValues);
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) {
80 if (CRM_Utils_Array::value('group_organization', $params)) {
81 $formatedValues['id'] = $params['group_organization'];
82 }
83
84 if (CRM_Utils_Array::value('group_id', $params)) {
85 $formatedValues['group_id'] = $params['group_id'];
86 }
87
88 if (CRM_Utils_Array::value('organization_id', $params)) {
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
104 if (CRM_Utils_Array::value('organization_id', $params) &&
105 CRM_Utils_Array::value('group_id', $params)
106 ) {
107 return TRUE;
108 }
109 return FALSE;
110 }
111
112 static function retrieve($groupID, &$defaults) {
113 $dao = new CRM_Contact_DAO_GroupOrganization();
114 $dao->group_id = $groupID;
115 if ($dao->find(TRUE)) {
116 $defaults['group_organization'] = $dao->id;
117 $defaults['organization_id'] = $dao->organization_id;
118 }
119 }
120
121 /**
122 * Method to check group organization relationship exist
123 *
124 * @param int $contactId
125 *
126 * @return boolean
127 * @access public
128 * @static
129 */
130 static function hasGroupAssociated($contactID) {
131 $orgID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_GroupOrganization',
132 $contactID, 'group_id', 'organization_id'
133 );
134 if ($orgID) {
135 return TRUE;
136 }
137 return FALSE;
138 }
139
140 /**
141 * Function to delete Group Organization
142 *
143 * @param int $groupOrganizationID group organization id that needs to be deleted
144 *
145 * @return $results no of deleted group organization on success, false otherwise
146 * @access public
147 */
148 static function deleteGroupOrganization($groupOrganizationID) {
149 $results = NULL;
150 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
151 $groupOrganization->id = $groupOrganizationID;
152
153 $results = $groupOrganization->delete();
154
155 return $results;
156 }
157}
158