-
Notifications
You must be signed in to change notification settings - Fork 0
/
sogintegration.module
314 lines (242 loc) · 10.7 KB
/
sogintegration.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
<?php
// $Id: sogintegration.module
/**
* @file
* Custom hooks for SOG member signup/creation
*/
define("DASHBOARD_DIR", "/var/www/services/dashboard");
define("CIVICRM_DIR", dirname(__FILE__)."/../../../..");
require_once CIVICRM_DIR.'/all/modules/civicrm/api/class.api.php' ;
/**
* Hook into CiviCRM Contribution page (Online Mitgliederformular)
*/
function sogintegration_civicrm_postProcess($formName, &$form ) {
if ($formName != "CRM_Contribute_Form_Contribution_Confirm" || !sogintegration_isMembershipSignup($form))
return;
sogintegration_saveDirectDebit($form);
if(sogintegration_isNewAktiveMitgliedschaft($form)) {
sogintegration_createSogUser($form);
}
}
function sogintegration_isMembershipSignup($form) {
return array_key_exists("membershipID", $form->_params);
}
function sogintegration_isNewAktiveMitgliedschaft($form) {
return ($form->_values['title'] == "Aktive Mitgliedschaft");
}
function sogintegration_isPatenschaft($form) {
return (strpos($form->_values['title'], 'Pate') !== false);
}
function sogintegration_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName != 'CRM_Contribute_Form_Contribution_Main') return;
$data = &$form->controller->container();
// Check IBAN
if (isset($fields['custom_63'])) {
$iban = $fields['custom_63'];
// Validate IBAN
if(!verify_iban($iban)) {
$errors['custom_63'] = 'Inkorrekte IBAN';
}else{
// sanitize IBAN
$data['values']['Main']['custom_63'] = iban_to_machine_format($iban);
}
}
// Check BIC
if (isset($fields['custom_64'])) {
$bic = $fields['custom_64'];
if (strlen($bic) !== 11 && strlen($bic) !== 8) {
$errors['custom_64'] = 'Inkorrektes Format';
}
else if (strlen($bic) === 8) {
// sanitize BIC
$data['values']['Main']['custom_64'] = strtoupper($bic).'XXX';
}
}
}
/**
* Add the direct debit (Lastschrifteinzug) data like amount and period to the CiviCRM membership
* because this is membership specific and not general for the contact overall.
*/
function sogintegration_saveDirectDebit(&$form) {
$directDebitSaver = new sogintegration_DirectDebitSaver($form, sogintegration_isPatenschaft($form));
$directDebitSaver->saveInformationToCiviCRM();
}
/**
* Create a new SOG user account through the SOG Dashboard API
*/
function sogintegration_createSogUser(&$form) {
require_once DASHBOARD_DIR . '/vendor/autoload.php';
require_once(DASHBOARD_DIR . '/app/config.php');
// Umlaute durch Laute ersetzen
$replace = array(
'Ä' => 'Ae',
'Ö' => 'Oe',
'Ü' => 'Ue',
'ä' => 'ae',
'ö' => 'oe',
'ü' => 'ue',
'ß' => 'ss',
);
// get parameters from form
$firstName = t($form->_submitValues['first_name']);
$firstName = strtr($firstName,$replace);
$lastName = t($form->_submitValues['last_name']);
$lastName = strtr($lastName,$replace);
$email = t($form->_params['email-5']);
$contactId = t($form->_params['contactID']);
$lokalgruppe = t($form->_params['custom_11']);
$lokalgruppe = "lg_" . strtolower($lokalgruppe);
$api = new SOG\Api\SogDashboardApi($dashboard_config);
$username = $api->createUser($firstName, $lastName, $email, $lokalgruppe);
$userSogMail = $username . "@studieren-ohne-grenzen.org";
sogintegration_addSogMail2Civicrm($contactId, $userSogMail);
drupal_set_message("Account '".$username."' erstellt");
}
/**
* Add its new SOG e-mail id to the CiviCRM contact.
* This is essential to match the user account to its CiviCRM contact when the user is logging in.
*/
function sogintegration_addSogMail2Civicrm($contactId, $mail) {
$params = array(
'contact_id' => $contactId,
'location_type_id' => 'Main',
'email' => $mail
);
$api = new civicrm_api3( array( ' conf_path' => CIVICRM_DIR ) );
$api->Email->Create($params);
}
class sogintegration_DirectDebitSaver
{
//TODO: get custom field names through the api somehow?
const CUSTOM_FIELD_AMOUNT = "custom_37";
const CUSTOM_FIELD_MANDATSREF = "custom_43";
const CUSTOM_FIELD_ZAHLUNGSINTERVAL = 'custom_36';
const CUSTOM_FIELD_ZAHLUNGSWEISE = 'custom_38';
public function __construct(&$form, $isPatenschaft) {
$this->api = new civicrm_api3( array( 'conf_path' => CIVICRM_DIR ) );
$this->isPatenschaft = $isPatenschaft;
// get parameters from form
$this->contributionId = t($form->_values['contribution_id']);
$this->membershipId = t($form->_params['membershipID']);
$this->contactId = t($form->_params['contactID']);
$this->receiveDate = $this->getReceiveDate($form);
$this->zahlungsweise = $this->getZahlungsweise($form);
$this->zahlungsinterval = $this->getZahlungsinterval($form);
$this->betrag = $this->getBetrag($form);
}
public function saveInformationToCiviCRM() {
$this->updateMembership();
$this->updateContribution();
}
/**
* update the created membership:
* - set amount for Mitgliedsbeitrag
* - set automatically generated Mandatsreferenz
* ?? update the membership description ??
*/
function updateMembership() {
$api_call_params = array('version' => 3,
'id' => $this->membershipId,
self::CUSTOM_FIELD_AMOUNT => $this->betrag,
self::CUSTOM_FIELD_MANDATSREF => $this->generateMandatsreferenz()
);
$result = $this->api->Membership->Create ( $api_call_params );
}
/**
* update the created contribution of the Mitgliedsbeitrag:
* - set 'Paid by' (Zahlungsweise = Payment Instrument)
* - set 'Source' -> "Mitgliedsbeitrag 20XX"
*/
function updateContribution() {
//TODO: better delete this contribution completely? (we are importing the real contribution later on anyway)
$contribution_source_text = "[Formular] Mitgliedsbeitrag " . substr($this->receiveDate, 0, 4);
$api_call_params = array('version' => 3, 'sequential' => 1,
'id' => $this->contributionId,
'contribution_payment_instrument_id' => $this->zahlungsweise,
'contribution_source' => $contribution_source_text,
'contribution_status_id' => 2,
'total_amount' => $this->betrag //amount like 3,33€ screws up
);
$result = $this->api->Contribution->Create ( $api_call_params );
//TODO: generate all monthly payments?!?
}
private function getReceiveDate(&$form) {
return t(substr($form->_params['receive_date'], 0, 8)); //format: YYYYMMDDTTTTTT (e.g. 20140317093001)
}
private function getZahlungsweise(&$form) {
//TODO: use API to determine the correct payment type id?!
return t( $form->_params[self::CUSTOM_FIELD_ZAHLUNGSWEISE] == "Lastschrifteinzug" ? "2" : "4" );
}
private function getZahlungsinterval(&$form) {
return t( $form->_params[self::CUSTOM_FIELD_ZAHLUNGSINTERVAL] );
}
private function getBetrag(&$form) {
$amount = $form->_params['amount'];
$zahlungsinterval = $this->getZahlungsinterval($form);
if($this->isPatenschaft) {
//Patenschaft: amount in form is monthly amount
if($zahlungsinterval == "jährlich") {
$amount = $amount*12;
}
} else {
//Mitgliedschaft: amount in form is annual amount
if ($zahlungsinterval == "monatlich") {
$amount = $amount/12.0;
} else if ($zahlungsinterval == "vierteljährlich") {
$amount = $amount/4.0;
}
}
$amount = round($amount, 2);
return $amount;
}
private function generateMandatsreferenz() {
// format of Mandatsreferenz see https://studieren-ohne-grenzen.org/atrium/it/node/5425
// <Datum>K<Civi-Kontakt-ID>(M/P)<Membership-ID>
return $this->receiveDate . "K" . $this->contactId . "M" . $this->membershipId;
}
/* Example of $form->_params array:
Array ( [qfKey] => 442870d528eb09f040273fa27480e74e_8647 [entryURL] => http://localhost/civi6/civicrm/contribute/transact?reset=1&id=1 [first_name] => Test [last_name] => Mustermann [gender_id] => [street_address-1] => sfdfsad [city-1] => afsfdsa [postal_code-1] => 21321 [state_province-Primary] => 2243 [country-1] => 1082 [14] => [group] => Array ( [14] => ) [custom_38] => Lastschrifteinzug [custom_36] => jährlich [custom_42] => Max Mustermann [custom_63] => 29993939 [custom_64] => 298298ddls [custom_40] => GLS [custom_11] => welt [job_title] => [birth_date] => [email-5] => [email protected] [priceSetId] => 4 [price_22] => 43 [price_23] => 3 [selectProduct] => [MAX_FILE_SIZE] => 52428800 [ip_address] => 127.0.0.1 [amount] => 15 [currencyID] => EUR [payment_action] => Sale [is_pay_later] => 1 [invoiceID] => af2bb7f85c1776abbc81606972c1d771 [description] => Online Contribution: Aktive Mitgliedschaft [accountingCode] => [contributionType_name] => Mitgliedsbeitrag [contributionType_accounting_code] => [contributionPageID] => 1 [contactID] => 2003 [contributionID] => 3760 [contributionTypeID] => 2 [item_name] => Online Contribution: Aktive Mitgliedschaft [receive_date] => 20140317093001 [membershipID] => 1290 )
*/
}
/**
* Write update of e-mail field in CiviCRM Profile to LDAP
*/
function sogintegration_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if ($op == 'edit' && $objectName == 'Individual') {
$uid = "";
$mail = "";
$backoff_mail ="";
foreach($objectRef->email as $m) {
if($m->is_primary) {
$mail = $m->email;
}
//save mail that is not SOG domain as backoff (because alternative mail can't be SOG)
if($backoff_mail === "" && !sogintegration_isSogMail($m->email)) {
$backoff_mail = $m->email;
}
// get uid from SOG mail
if(sogintegration_isSogMail($m->email)) {
$s = split('@', $m->email);
$uid = $s[0];
}
}
if(sogintegration_isSogMail($mail)) {
// don't use a SOG mail as alternative mail
$mail = $backoff_mail;
}
if($uid === "" || $mail === "") {
// no SOG user or no suitable mail at all
return;
}
require_once DASHBOARD_DIR . '/vendor/autoload.php';
require_once(DASHBOARD_DIR . '/app/config.php');
$api = new SOG\Api\SogDashboardApi($dashboard_config);
$api->updateAlternativeMail($uid, $mail);
drupal_set_message("Alternative E-Mail-Adresse für SOG-Systeme aktualisiert: Vergessenes Passwort wird ggf. an $mail gesendet.");
}
}
function sogintegration_isSogMail($mail) {
$SOG_DOMAIN = "@studieren-ohne-grenzen.org";
$length = strlen($SOG_DOMAIN);
return (substr($mail, -$length) === $SOG_DOMAIN);
}