forked from civicrm/civicrm-drupal-8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
civicrm.install
194 lines (170 loc) · 5.7 KB
/
civicrm.install
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
<?php
/**
* @file
* Contains install-time code for the CiviCRM module.
*/
use Drupal\Core\StreamWrapper\PublicStream;
use Civi\Install\Requirements;
use Drupal\Core\Site\Settings;
use Drupal\Core\Database\Database;
/**
* Contains things that need to be installed when the module is installed.
*/
function civicrm_install() {
// If civicrm.settings.php file is already present, we assume CiviCRM is
// already installed and abort.
if (file_exists(\Drupal::service('kernel')->getSitePath() . '/civicrm.settings.php')) {
drupal_set_message(t("CiviCRM appears to have already been installed. Skipping full installation."));
return;
}
$civicrm_base = _civicrm_find_civicrm();
require_once $civicrm_base . '/CRM/Core/ClassLoader.php';
CRM_Core_ClassLoader::singleton()->register();
// The civicrm install process uses globals all over the place. Ideally these
// will go sometime soon and will be passed as explicit parameters.
// @codingStandardsIgnoreStart
global $crmPath, $cmsPath, $installType;
// @codingStandardsIgnoreEnd
$crmPath = $civicrm_base;
$cmsPath = \Drupal::root();
$installType = 'drupal';
// Get database connection details.
// We attempt to get a separate set of details for a civicrm database, but
// otherwise default to using the same database as Drupal.
$drupal_db = Database::getConnection('default')->getConnectionOptions();
$civicrm_db = _civicrm_get_db_config()['info'];
$config = [
'cms' => 'Drupal8',
'base_url' => $GLOBALS['base_url'],
// Remove leading 'sites/'.
'site_dir' => substr(\Drupal::service('kernel')->getSitePath(), 6),
'loadGenerated' => Settings::get('civicrm_load_generated', FALSE),
'mysql' => [
'username' => $civicrm_db['username'],
'password' => $civicrm_db['password'],
'server' => "{$civicrm_db['host']}:{$civicrm_db['port']}",
'database' => $civicrm_db['database'],
],
'cmsdb' => [
'username' => $drupal_db['username'],
'password' => $drupal_db['password'],
'server' => "{$drupal_db['host']}:{$drupal_db['port']}",
'database' => $drupal_db['database'],
],
];
require_once "{$civicrm_base}/install/civicrm.php";
// @TODO: Enable CiviCRM's CRM_Core_TemporaryErrorScope::useException() and
// possibly catch exceptions. At the moment, civicrm doesn't allow exceptions
// to bubble up to Drupal. See CRM-15022.
civicrm_main($config);
}
/**
* Implements hook_requirements().
*/
function civicrm_requirements($phase) {
$requirements = [];
$civicrm_base = _civicrm_find_civicrm();
if ($civicrm_base) {
require_once $civicrm_base . '/Civi/Install/Requirements.php';
$requirements['civicrm.location'] = [
'title' => 'CiviCRM location',
'value' => $civicrm_base,
'severity' => REQUIREMENT_OK,
'description' => 'CiviCRM core directory',
];
}
else {
$requirements['civicrm.location'] = [
'title' => 'CiviCRM location',
'value' => NULL,
'severity' => REQUIREMENT_ERROR,
'description' => 'CiviCRM must be installed via composer.',
];
return $requirements;
}
// Grab db connection info.
$db_config = _civicrm_get_db_config()['info'];
$install_requirements = new Requirements();
// Gather directories that need to be writable.
$file_paths = [];
if (!file_exists(\Drupal::service('kernel')->getSitePath() . '/civicrm.settings.php')) {
// eg. sites/default folder.
$file_paths[] = realpath(\Drupal::service('kernel')->getSitePath());
}
// eg. sites/default/files folder.
$file_paths[] = realpath(PublicStream::basePath());
// Attempt to make directories writable
// We don't bother checking if these attempts are actually successful as
// that will be checked by checkAll().
foreach ($file_paths as $path) {
@chmod($path, 0755);
}
foreach ($install_requirements->checkAll(['db_config' => $db_config, 'file_paths' => $file_paths]) as $key => $result) {
$requirements["civicrm.$key"] = [
'title' => $result['title'],
'value' => NULL,
'severity' => $result['severity'],
'description' => $result['details'],
];
}
return $requirements;
}
/**
* Returns the path to where CiviCRM is installed.
*
* Installation via composer is recommended. We also allow /modules/civicrm,
* which seems to work fine.
*
* @return string|null
* A string to the location if we can find where CiviCRM is installed, NULL
* if we can't.
*/
function _civicrm_find_civicrm() {
$possible_paths = [];
if ($path = drupal_get_path('module', 'civicrm')) {
$possible_paths[] = $path;
}
$possible_paths[] = 'vendor/civicrm/civicrm-core';
$possible_paths[] = '../vendor/civicrm/civicrm-core';
foreach ($possible_paths as $path) {
if (file_exists($path . '/CRM/Core/ClassLoader.php')) {
return \Drupal::service('file_system')->realpath($path);
}
}
return NULL;
}
/**
* Attempt to use a 'civicrm' labelled database connection if one exists.
*
* Otherwise default to using the same connection used by drupal.
* Also handle the special case where this is running as a test.
*
* @return string[]
* An array of what database-config to use.
*/
function _civicrm_get_db_config() {
if (drupal_valid_test_ua()) {
$config = Database::getConnectionInfo('civicrm_test');
if ($config) {
return [
'key' => 'civicrm_test',
'info' => $config['default'],
];
}
else {
throw new \RuntimeException("No civicrm_test database provided");
}
}
if ($config = Database::getConnectionInfo('civicrm')) {
return [
'key' => 'civicrm',
'info' => $config['default'],
];
}
else {
return [
'key' => 'default',
'info' => Database::getConnectionInfo('default')['default'],
];
}
}