Merge pull request #17305 from mlutfy/core1755
[civicrm-core.git] / CRM / Bridge / OG / CiviCRM.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Bridge_OG_CiviCRM {
18
19 /**
20 * @param int $groupID
21 * @param $group
22 * @param $op
23 */
24 public static function group($groupID, $group, $op) {
25 if ($op == 'add') {
26 self::groupAdd($groupID, $group);
27 }
28 else {
29 self::groupDelete($groupID, $group);
30 }
31 }
32
33 /**
34 * @param int $groupID
35 * @param $group
36 */
37 public static function groupAdd($groupID, $group) {
38 $ogID = CRM_Bridge_OG_Utils::ogID($groupID);
39
40 $node = new StdClass();
41 if ($ogID) {
42 $node->nid = $ogID;
43 }
44
45 global $user;
46 $node->uid = $user->uid;
47 $node->title = $group->title;
48 $node->type = 'og';
49 $node->status = 1;
50
51 // set the og values
52 $node->og_description = $group->description;
53 $node->og_selective = OF_OPEN;
54 $node->og_register = 0;
55 $node->og_directory = 1;
56
57 node_save($node);
58
59 // also change the source field of the group
60 CRM_Core_DAO::setFieldValue('CRM_Contact_DAO_Group',
61 $groupID,
62 'source',
63 CRM_Bridge_OG_Utils::ogSyncName($node->nid)
64 );
65 }
66
67 /**
68 * @param int $groupID
69 * @param $group
70 */
71 public static function groupDelete($groupID, $group) {
72 $ogID = CRM_Bridge_OG_Utils::ogID($groupID);
73 if (!$ogID) {
74 return;
75 }
76
77 node_delete($ogID);
78 }
79
80 /**
81 * @param int $groupID
82 * @param $contactIDs
83 * @param $op
84 */
85 public static function groupContact($groupID, $contactIDs, $op) {
86 $config = CRM_Core_Config::singleton();
87 $ogID = CRM_Bridge_OG_Utils::ogID($groupID);
88
89 if (!$ogID) {
90 return;
91 }
92
93 foreach ($contactIDs as $contactID) {
94 $drupalID = CRM_Core_BAO_UFMatch::getUFId($contactID);
95 if ($drupalID) {
96 if ($op == 'add') {
97 $group_membership = $config->userSystem->og_membership_create($ogID, $drupalID);
98 }
99 else {
100 $group_membership = $config->userSystem->og_membership_delete($ogID, $drupalID);
101 }
102 }
103 }
104 }
105
106 }