Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Contact / BAO / GroupOrganization.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7e9e8871 4 | CiviCRM version 4.7 |
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 */
33class CRM_Contact_BAO_GroupOrganization extends CRM_Contact_DAO_GroupOrganization {
34
35 /**
fe482240 36 * Class constructor.
6a488035 37 */
00be9182 38 public function __construct() {
6a488035
TO
39 parent::__construct();
40 }
41
42 /**
fe482240 43 * Takes an associative array and creates a groupOrganization object.
6a488035 44 *
77c5b619
TO
45 * @param array $params
46 * (reference ) an assoc array of name/value pairs.
6a488035 47 *
f8d78294 48 * @return CRM_Contact_DAO_GroupOrganization
6a488035 49 */
00be9182 50 public static function add(&$params) {
f8d78294
EM
51 $formattedValues = array();
52 self::formatValues($params, $formattedValues);
53 $dataExists = self::dataExists($formattedValues);
6a488035
TO
54 if (!$dataExists) {
55 return NULL;
56 }
57 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
f8d78294 58 $groupOrganization->copyValues($formattedValues);
c69fdfbb
E
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);
6a488035
TO
62 $groupOrganization->save();
63 return $groupOrganization;
64 }
65
66 /**
fe482240 67 * Format the params.
6a488035 68 *
77c5b619
TO
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.
6a488035 73 */
00be9182 74 public static function formatValues(&$params, &$formatedValues) {
a7488080 75 if (!empty($params['group_organization'])) {
6a488035
TO
76 $formatedValues['id'] = $params['group_organization'];
77 }
78
a7488080 79 if (!empty($params['group_id'])) {
6a488035
TO
80 $formatedValues['group_id'] = $params['group_id'];
81 }
82
a7488080 83 if (!empty($params['organization_id'])) {
6a488035
TO
84 $formatedValues['organization_id'] = $params['organization_id'];
85 }
86 }
87
88 /**
fe482240 89 * Check if there is data to create the object.
6a488035 90 *
77c5b619
TO
91 * @param array $params
92 * (reference ) an assoc array of name/value pairs.
6a488035 93 *
bed98343 94 * @return bool
6a488035 95 */
00be9182 96 public static function dataExists($params) {
6a488035 97 // return if no data present
8cc574cf 98 if (!empty($params['organization_id']) && !empty($params['group_id'])) {
6a488035
TO
99 return TRUE;
100 }
101 return FALSE;
102 }
103
86538308 104 /**
100fef9d 105 * @param int $groupID
86538308
EM
106 * @param $defaults
107 */
00be9182 108 public static function retrieve($groupID, &$defaults) {
6a488035
TO
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 /**
fe482240 118 * Method to check group organization relationship exist.
6a488035 119 *
c490a46a 120 * @param int $contactID
6a488035 121 *
bed98343 122 * @return bool
6a488035 123 */
00be9182 124 public static function hasGroupAssociated($contactID) {
6a488035
TO
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 /**
fe482240 135 * Delete Group Organization.
6a488035 136 *
77c5b619
TO
137 * @param int $groupOrganizationID
138 * Group organization id that needs to be deleted.
6a488035 139 *
72b3a70c
CW
140 * @return int|null
141 * no of deleted group organization on success, false otherwise
6a488035 142 */
00be9182 143 public static function deleteGroupOrganization($groupOrganizationID) {
6a488035
TO
144 $results = NULL;
145 $groupOrganization = new CRM_Contact_DAO_GroupOrganization();
146 $groupOrganization->id = $groupOrganizationID;
147
148 $results = $groupOrganization->delete();
149
150 return $results;
151 }
96025800 152
6a488035 153}