Merge pull request #6963 from agileware/4.6-CRM-17402
[civicrm-core.git] / CRM / Contact / BAO / GroupOrganization.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35class CRM_Contact_BAO_GroupOrganization extends CRM_Contact_DAO_GroupOrganization {
36
37 /**
fe482240 38 * Class constructor.
6a488035 39 */
00be9182 40 public function __construct() {
6a488035
TO
41 parent::__construct();
42 }
43
44 /**
fe482240 45 * Takes an associative array and creates a groupOrganization object.
6a488035 46 *
77c5b619
TO
47 * @param array $params
48 * (reference ) an assoc array of name/value pairs.
6a488035 49 *
f8d78294 50 * @return CRM_Contact_DAO_GroupOrganization
6a488035 51 */
00be9182 52 public static function add(&$params) {
f8d78294
EM
53 $formattedValues = array();
54 self::formatValues($params, $formattedValues);
55 $dataExists = self::dataExists($formattedValues);
6a488035
TO
56 if (!$dataExists) {
57 return NULL;
58 }
59 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
f8d78294 60 $groupOrganization->copyValues($formattedValues);
c69fdfbb
E
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);
6a488035
TO
64 $groupOrganization->save();
65 return $groupOrganization;
66 }
67
68 /**
fe482240 69 * Format the params.
6a488035 70 *
77c5b619
TO
71 * @param array $params
72 * (reference ) an assoc array of name/value pairs.
73 * @param array $formatedValues
74 * (reference ) an assoc array of name/value pairs.
6a488035
TO
75 *
76 * @return void
6a488035 77 */
00be9182 78 public static function formatValues(&$params, &$formatedValues) {
a7488080 79 if (!empty($params['group_organization'])) {
6a488035
TO
80 $formatedValues['id'] = $params['group_organization'];
81 }
82
a7488080 83 if (!empty($params['group_id'])) {
6a488035
TO
84 $formatedValues['group_id'] = $params['group_id'];
85 }
86
a7488080 87 if (!empty($params['organization_id'])) {
6a488035
TO
88 $formatedValues['organization_id'] = $params['organization_id'];
89 }
90 }
91
92 /**
fe482240 93 * Check if there is data to create the object.
6a488035 94 *
77c5b619
TO
95 * @param array $params
96 * (reference ) an assoc array of name/value pairs.
6a488035 97 *
bed98343 98 * @return bool
6a488035 99 */
00be9182 100 public static function dataExists($params) {
6a488035 101 // return if no data present
8cc574cf 102 if (!empty($params['organization_id']) && !empty($params['group_id'])) {
6a488035
TO
103 return TRUE;
104 }
105 return FALSE;
106 }
107
86538308 108 /**
100fef9d 109 * @param int $groupID
86538308
EM
110 * @param $defaults
111 */
00be9182 112 public static function retrieve($groupID, &$defaults) {
6a488035
TO
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 /**
fe482240 122 * Method to check group organization relationship exist.
6a488035 123 *
c490a46a 124 * @param int $contactID
6a488035 125 *
bed98343 126 * @return bool
6a488035 127 */
00be9182 128 public static function hasGroupAssociated($contactID) {
6a488035
TO
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 /**
fe482240 139 * Delete Group Organization.
6a488035 140 *
77c5b619
TO
141 * @param int $groupOrganizationID
142 * Group organization id that needs to be deleted.
6a488035 143 *
72b3a70c
CW
144 * @return int|null
145 * no of deleted group organization on success, false otherwise
6a488035 146 */
00be9182 147 public static function deleteGroupOrganization($groupOrganizationID) {
6a488035
TO
148 $results = NULL;
149 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
150 $groupOrganization->id = $groupOrganizationID;
151
152 $results = $groupOrganization->delete();
153
154 return $results;
155 }
96025800 156
6a488035 157}