forked from mlutfy/hosting_civicrm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhosting_civicrm.module
201 lines (173 loc) · 6.24 KB
/
hosting_civicrm.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* @file
* Hosting CiviCRM API functions, and Drupal hooks.
*/
/**
* Implements hook_form_FORM_ID_alter().
*/
function hosting_civicrm_form_site_node_form_alter(&$form, &$form_state) {
if (! empty($form['#node']->nid)) {
if (isset($form['#node']->civicrm_version)) {
// Display the CiviCRM version
$form['info']['civicrm_version'] = array(
'#type' => 'item',
'#title' => t('CiviCRM version'),
'#markup' => $form['#node']->civicrm_version,
'#required' => FALSE,
);
// Allow editing the CiviCRM site key
$form['civicrm_sitekey'] = array(
'#type' => 'textfield',
'#title' => t('CiviCRM site key'),
'#description' => t('The site key enables access to the CiviCRM REST API, in combination with a user API key. Leave this empty to generate a new random API site key. For more information, refer to the <a href="!url">CiviCRM REST interface documentation</a>.', array('!url' => 'http://wiki.civicrm.org/confluence/display/CRMDOC/REST+interface')),
'#default_value' => $form['#node']->civicrm_sitekey,
'#required' => FALSE,
'#weight' => 11,
);
}
}
}
/**
* Implements hook_nodeapi_load().
*/
function hosting_civicrm_node_load($nodes, $types) {
if (! in_array('site', $types)) {
return;
}
foreach ($nodes as $node) {
if ($node->type != 'site') {
continue;
}
// Get the CiviCRM package version from the platform.
//
// The presence of the version is also a check used later to know
// if a site has CiviCRM installed (ex: whether to show the sitekey).
$query = 'SELECT version as civicrm_version
FROM hosting_package_instance hpi
LEFT JOIN hosting_package pkg ON (pkg.nid = hpi.package_id)
WHERE platform = :platform
and short_name = :short_name
and status = 1
ORDER BY iid DESC';
if ($result = db_query($query, array(':platform' => $node->platform, ':short_name' => 'civicrm'))->fetchField()) {
$node->civicrm_version = $result;
// Get the CiviCRM site key
$node->civicrm_sitekey = db_query('SELECT civicrm_sitekey FROM hosting_site WHERE nid = :nid', array(':nid' => $node->nid))->fetchField();
}
}
}
function hosting_civicrm_node_view($node, $view_mode, $langcode) {
if (isset($node->civicrm_version)) {
$node->content['info']['civicrm_version'] = array(
'#type' => 'item',
'#title' => t('CiviCRM version'),
'#markup' => $node->civicrm_version,
);
}
if (isset($node->civicrm_version) && isset($node->civicrm_sitekey)) {
$node->content['info']['civicrm_sitekey'] = array(
'#type' => 'item',
'#title' => t('CiviCRM site key'),
'#markup' => $node->civicrm_sitekey,
);
}
}
/**
* Implements hook_node_update().
*/
function hosting_civicrm_node_update($node) {
hosting_civicrm_node_update_or_insert($node);
}
/**
* Implements hook_node_insert().
*/
function hosting_civicrm_node_insert($node) {
hosting_civicrm_node_update_or_insert($node);
}
function hosting_civicrm_node_update_or_insert($node) {
// Avoid creating a site key for Drupal-only site.
if (isset($node->civicrm_sitekey)) {
$sitekey = $node->civicrm_sitekey;
if (empty($sitekey)) {
// Adapted from civicrm/install/civicrm.php
$sitekey = md5(rand() . mt_rand() . rand() . uniqid('', TRUE) . $params['baseURL']);
}
db_query('UPDATE {hosting_site} SET civicrm_sitekey = :sitekey WHERE nid = :nid', array(':sitekey' => $sitekey, ':nid' => $node->nid));
}
}
/**
* Return the number of sites where CiviCRM is available
*/
function hosting_civicrm_site_count() {
$platforms = hosting_civicrm_get_platforms();
$count = 0;
foreach ($platforms as $key => $nid) {
$count += hosting_site_count($nid);
}
return $count;
}
/**
* Return an array of active platforms that contain CiviCRM.
*/
function hosting_civicrm_get_platforms() {
// Find the package ID for CiviCRM, which is unique accros all platforms.
$package_id = db_query('SELECT nid FROM {hosting_package} WHERE short_name = :short_name', array(':short_name' => 'civicrm'))->fetchField();
// Find active platforms that have the package_id.
$result = db_query('SELECT DISTINCT nid FROM {hosting_platform} pl
INNER JOIN {hosting_package_instance} pkg ON (pl.nid = pkg.platform)
WHERE pkg.package_id = :package_id
AND pl.status = :status', array(
':package_id' => $package_id,
':status' => HOSTING_PLATFORM_ENABLED,
));
$platforms = array();
foreach ($result as $record) {
$platforms[] = $record->nid;
}
return $platforms;
}
/**
* Return an array of enabled sites capable of running CiviCRM
*
* @param $order_by
* Field to order the returned sites by.
*/
function hosting_civicrm_get_sites($order_by = null) {
$platforms = hosting_civicrm_get_platforms();
$sites = array();
foreach ($platforms as $platform) {
//get all the enabled sites on the platform
if (isset($order_by)) {
$result = db_query('SELECT n.nid FROM {node} n LEFT JOIN {hosting_site} s ON n.nid=s.nid WHERE n.type="site" and s.status = :status and s.platform = :platform ORDER BY :orderby ASC', array(
':platform' => $platform,
':status' => HOSTING_SITE_ENABLED,
':orderby' => $order_by
));
}
else {
$result = db_query('SELECT n.nid FROM {node} n LEFT JOIN {hosting_site} s ON n.nid=s.nid WHERE n.type="site" and s.status = %d and s.platform=%d', array(':platform' => $platform, ':status' => HOSTING_SITE_ENABLED));
}
foreach ($result as $record) {
$sites[] = $record->nid;
}
}
return $sites;
}
/**
* Helper function to determine whether a platform contains CiviCRM
*/
function _is_civicrm_platform($nid) {
return in_array($nid, hosting_civicrm_get_platforms());
}
/**
* Helper function to determine the version of CiviCRM available.
* DEPRECATED c.f. #2335909
* Remove after 2015-05-01.
*/
function hosting_civicrm_get_version() {
//TODO: confirm this is returning the proper version for our platform, etc.
// @see: hosting_civicrm_form_site_node_form_alter()
$package = hosting_package_instance_load(array('short_name' => 'civicrm'));
return $package->version;
}