Merge pull request #4955 from atif-shaikh/master-cleanup
[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
48 * (reference ) an assoc array of name/value pairs.
49 *
50 * @return CRM_Contact_DAO_GroupOrganization
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
72 * (reference ) an assoc array of name/value pairs.
73 * @param array $formatedValues
74 * (reference ) an assoc array of name/value pairs.
75 *
76 * @return void
77 */
78 public static function formatValues(&$params, &$formatedValues) {
79 if (!empty($params['group_organization'])) {
80 $formatedValues['id'] = $params['group_organization'];
81 }
82
83 if (!empty($params['group_id'])) {
84 $formatedValues['group_id'] = $params['group_id'];
85 }
86
87 if (!empty($params['organization_id'])) {
88 $formatedValues['organization_id'] = $params['organization_id'];
89 }
90 }
91
92 /**
93 * Check if there is data to create the object
94 *
95 * @param array $params
96 * (reference ) an assoc array of name/value pairs.
97 *
98 * @return boolean
99 */
100 public static function dataExists($params) {
101 // return if no data present
102 if (!empty($params['organization_id']) && !empty($params['group_id'])) {
103 return TRUE;
104 }
105 return FALSE;
106 }
107
108 /**
109 * @param int $groupID
110 * @param $defaults
111 */
112 public 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 */
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
142 * Group organization id that needs to be deleted.
143 *
144 * @return int|null
145 * no of deleted group organization on success, false otherwise
146 */
147 public static function deleteGroupOrganization($groupOrganizationID) {
148 $results = NULL;
149 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
150 $groupOrganization->id = $groupOrganizationID;
151
152 $results = $groupOrganization->delete();
153
154 return $results;
155 }
156 }