-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
civicrm.install
209 lines (180 loc) · 5.56 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
/**
* @file
* Contains install-time code for the CiviCRM module.
*/
use Drupal\Core\Database\Database;
/**
* Implements hook_install().
*
* Contains things that need to be installed when the module is installed.
*/
function civicrm_install() {
/** @var \Civi\Setup $setup */
$setup = _civicrm_setup();
if ($setup->getPendingAction() !== NULL) {
// Ex: If you do an advanced installation with `cv core:install`, then
// `hook_install` shouldn't activate the default/unconfigured install.
return;
}
$installed = $setup->checkInstalled();
if ($installed->isSettingInstalled() || $installed->isDatabaseInstalled()) {
throw new \Exception("CiviCRM appears to have already been installed. Skipping full installation.");
}
$setup->installFiles();
$setup->installDatabase();
}
/**
* Implements hook_uninstall().
*/
function civicrm_uninstall() {
/** @var \Civi\Setup $setup */
$setup = _civicrm_setup();
$setup->uninstallFiles();
$setup->uninstallDatabase();
}
/**
* Implements hook_requirements().
*/
function civicrm_requirements($phase) {
$requirements = [];
$civicrm_base = _civicrm_find_civicrm();
if ($civicrm_base) {
$requirements['civicrm.location'] = [
'title' => 'CiviCRM location',
'severity' => REQUIREMENT_OK,
'description' => 'CiviCRM core directory',
];
}
else {
$requirements['civicrm.location'] = [
'title' => 'CiviCRM location',
'severity' => REQUIREMENT_ERROR,
'description' => 'CiviCRM must be installed via composer.',
];
return $requirements;
}
/** @var \Civi\Setup $setup */
$setup = _civicrm_setup();
if ($phase === 'install' && !$setup->checkAuthorized()->isAuthorized()) {
$requirements['civicrm.checkAuthorized'] = [
'title' => 'CiviCRM Installation Not Authorized',
'description' => 'The current user does not have sufficient permissions to perform installation.',
'severity' => REQUIREMENT_WARNING,
];
return $requirements;
}
$severityMap = [
'info' => REQUIREMENT_OK,
'warning' => REQUIREMENT_WARNING,
'error' => REQUIREMENT_ERROR,
];
$sections = [
'system' => 'CiviCRM: System',
'database' => 'CiviCRM: Database',
'other' => 'CiviCRM: Other',
];
if ($phase !== 'runtime') {
foreach ($setup->checkRequirements()->getMessages() as $msg) {
$section = isset($sections[$msg['section']]) ? $sections[$msg['section']] : $sections['other'];
$key = 'civicrm.' . $msg['section'] . '.' . $msg['name'];
$requirements[$key] = [
'title' => $section . ': ' . $msg['message'],
'description' => $section . ': ' . $msg['message'],
'severity' => $severityMap[$msg['severity']],
];
}
}
ksort($requirements);
return $requirements;
}
/**
* @return \Civi\Setup
*/
function _civicrm_setup() {
if (defined('CIVI_SETUP')) {
return Civi\Setup::instance();
}
$civicrm_base = _civicrm_find_civicrm();
require_once $civicrm_base . '/CRM/Core/ClassLoader.php';
CRM_Core_ClassLoader::singleton()->register();
\Civi\Setup::assertProtocolCompatibility(1.0);
\Civi\Setup::init([
'cms' => 'Drupal8',
'srcPath' => $civicrm_base,
]);
$setup = Civi\Setup::instance();
// FIXME: Move more of this to plugins/init/Drupal8.civi-setup.php.
$civicrm_db = _civicrm_get_db_config()['info'];
$setup->getModel()->db = [
'server' => \Civi\Setup\DbUtil::encodeHostPort($civicrm_db['host'], $civicrm_db['port'] ?? NULL),
'username' => $civicrm_db['username'],
'password' => $civicrm_db['password'],
'database' => $civicrm_db['database'],
];
return $setup;
}
/**
* 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() {
// TODO: This works with standard composer layouts but would be easy to break
// via installer-paths. However, it should be possible to replace this - since we
// can count on composer's autoloader being setup already. Some ideas:
// - Use ReflectionClass('Civi') to find the path
// - Update `Civi.php` to provide a function with its __DIR__
$possible_paths = [];
if ($path = \Drupal::service('extension.list.module')->getPath('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'],
];
}
}