Style - Remove @access
[civicrm-core.git] / CRM / Contact / BAO / GroupOrganization.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35 class CRM_Contact_BAO_GroupOrganization extends CRM_Contact_DAO_GroupOrganization {
36
37 /**
38 * Class constructor
39 */
40 public 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 CRM_Contact_DAO_GroupOrganization
50 * @static
51 */
52 public static function add(&$params) {
53 $formattedValues = array();
54 self::formatValues($params, $formattedValues);
55 $dataExists = self::dataExists($formattedValues);
56 if (!$dataExists) {
57 return NULL;
58 }
59 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
60 $groupOrganization->copyValues($formattedValues);
61 // we have ensured we have group_id & organization_id so we can do a find knowing that
62 // this can only find a matching record
63 $groupOrganization->find(TRUE);
64 $groupOrganization->save();
65 return $groupOrganization;
66 }
67
68 /**
69 * Format the params
70 *
71 * @param array $params (reference ) an assoc array of name/value pairs
72 * @param array $formatedValues (reference ) an assoc array of name/value pairs
73 *
74 * @return void
75 * @static
76 */
77 public static function formatValues(&$params, &$formatedValues) {
78 if (!empty($params['group_organization'])) {
79 $formatedValues['id'] = $params['group_organization'];
80 }
81
82 if (!empty($params['group_id'])) {
83 $formatedValues['group_id'] = $params['group_id'];
84 }
85
86 if (!empty($params['organization_id'])) {
87 $formatedValues['organization_id'] = $params['organization_id'];
88 }
89 }
90
91 /**
92 * Check if there is data to create the object
93 *
94 * @param array $params (reference ) an assoc array of name/value pairs
95 *
96 * @return boolean
97 * @static
98 */
99 public static function dataExists($params) {
100 // return if no data present
101 if (!empty($params['organization_id']) && !empty($params['group_id'])) {
102 return TRUE;
103 }
104 return FALSE;
105 }
106
107 /**
108 * @param int $groupID
109 * @param $defaults
110 */
111 public static function retrieve($groupID, &$defaults) {
112 $dao = new CRM_Contact_DAO_GroupOrganization();
113 $dao->group_id = $groupID;
114 if ($dao->find(TRUE)) {
115 $defaults['group_organization'] = $dao->id;
116 $defaults['organization_id'] = $dao->organization_id;
117 }
118 }
119
120 /**
121 * Method to check group organization relationship exist
122 *
123 * @param int $contactID
124 *
125 * @return boolean
126 * @static
127 */
128 public static function hasGroupAssociated($contactID) {
129 $orgID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_GroupOrganization',
130 $contactID, 'group_id', 'organization_id'
131 );
132 if ($orgID) {
133 return TRUE;
134 }
135 return FALSE;
136 }
137
138 /**
139 * Delete Group Organization
140 *
141 * @param int $groupOrganizationID group organization id that needs to be deleted
142 *
143 * @return mixed|null $results no of deleted group organization on success, false otherwise@access public
144 */
145 public static function deleteGroupOrganization($groupOrganizationID) {
146 $results = NULL;
147 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
148 $groupOrganization->id = $groupOrganizationID;
149
150 $results = $groupOrganization->delete();
151
152 return $results;
153 }
154 }
155