forked from civicrm/civicrm-drupal-8
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Civicrm.php
133 lines (114 loc) · 4.12 KB
/
Civicrm.php
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
<?php
namespace Drupal\civicrm;
use Drupal\civicrm\Exception\CiviCRMConfigException;
use Drupal\Core\Config\ConfigException;
use Drupal\Core\Session\AccountInterface;
/**
* Connects the Drupal instance to CiviCRM.
*/
class Civicrm {
/**
* Static cache.
*
* @var bool
*/
protected $initialized = FALSE;
/**
* Initialize CiviCRM.
*
* Call this function from other modules too if they use the CiviCRM API.
*/
public function initialize() {
if ($this->initialized) {
return;
}
// Get ready for problems.
$docLinkInstall = "http://wiki.civicrm.org/confluence/display/CRMDOC/Drupal+Installation+Guide";
$docLinkTrouble = "http://wiki.civicrm.org/confluence/display/CRMDOC/Installation+and+Configuration+Trouble-shooting";
$forumLink = "http://forum.civicrm.org/index.php/board,6.0.html";
$errorMsgAdd = t("Please review the <a href='!1'>Drupal Installation Guide</a> and the <a href='!2'>Trouble-shooting page</a> for assistance. If you still need help installing, you can often find solutions to your issue by searching for the error message in the <a href='!3'>installation support section of the community forum</a>.</strong></p>",
['!1' => $docLinkInstall, '!2' => $docLinkTrouble, '!3' => $forumLink]
);
$settingsFile = \Drupal::service('kernel')->getSitePath() . '/civicrm.settings.php';
if (!defined('CIVICRM_SETTINGS_PATH')) {
define('CIVICRM_SETTINGS_PATH', $settingsFile);
}
$output = include_once $settingsFile;
if ($output == FALSE) {
$msg = t("The CiviCRM settings file (civicrm.settings.php) was not found in the expected location: %location", ['%location' => $settingsFile]) . ' ' . $errorMsgAdd;
throw new CiviCRMConfigException($msg);
}
// This does pretty much all of the civicrm initialization.
$output = include_once 'CRM/Core/Config.php';
if ($output == FALSE) {
$msg = t("The path for including CiviCRM code files is not set properly. Most likely there is an error in the <em>civicrm_root</em> setting in your CiviCRM settings file (!1).",
['!1' => $settingsFile]
) . t("civicrm_root is currently set to: <em>!1</em>.", ['!1' => $civicrm_root]) . $errorMsgAdd;
throw new CiviCRMConfigException($msg);
}
// Initialize the system by creating a config object.
\CRM_Core_Config::singleton()->userSystem->setMySQLTimeZone();
// Mark CiviCRM as initialized.
$this->initialized = TRUE;
}
/**
* Checks if the CiviCRM link is already initialized.
*/
public function isInitialized() {
return $this->initialized;
}
/**
* Invoke a CiviCRM method from Drupal.
*
* Wraps around \CRM_Core_Invoke::invoke.
*/
public function invoke($args) {
$this->initialize();
// CiviCRM will echo/print directly to stdout. We need to capture it so that
// we can return the output as a renderable array.
ob_start();
$content = \CRM_Core_Invoke::invoke($args);
$output = ob_get_clean();
return !empty($content) ? $content : $output;
}
/**
* Synchronize a Drupal account with CiviCRM.
*
* This is a wrapper for CRM_Core_BAO_UFMatch::synchronize().
*
* @param \Drupal\Core\Session\AccountInterface $account
* The drupal user.
* @param string $contact_type
* The user contact type.
*/
public function synchronizeUser(AccountInterface $account, $contact_type = 'Individual') {
$this->initialize();
\CRM_Core_BAO_UFMatch::synchronize($account, FALSE, 'Drupal', $this->getCtype($contact_type));
}
/**
* Function to get the contact type.
*
* @param string $default
* Default contact type.
*
* @return string
* The contact type.
*
* @Todo: Document what this function is doing and why.
*/
public function getCtype($default = 'Individual') {
if (!empty($_REQUEST['ctype'])) {
$ctype = $_REQUEST['ctype'];
}
elseif (!empty($_REQUEST['edit']['ctype'])) {
$ctype = $_REQUEST['edit']['ctype'];
}
else {
$ctype = $default;
}
if (!in_array($ctype, ['Individual', 'Organization', 'Household'])) {
$ctype = $default;
}
return $ctype;
}
}