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