-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration-civicrm-easyappointments.php
212 lines (196 loc) · 7.74 KB
/
integration-civicrm-easyappointments.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
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
<?php
/*
Plugin Name: Integration between Easy Appointments and CiviCRM
Description: Provides an integration between Easy Appointments and CiviCRM. A new appointment is send to a form processor in CiviCRM. You can use this plugin with Connector to CiviCRM with CiviMcRestFace (https://wordpress.org/plugins/connector-civicrm-mcrestface/)
Version: 1.0.2
Author: Jaap Jansma
License: AGPL3
License URI: https://www.gnu.org/licenses/agpl-3.0.html
Text Domain: integration-civicrm-easyappointments
*/
/**
* Copyright (C) 2023 Jaap Jansma ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
defined('ABSPATH') or die("Cannot access pages directly.");
define('INTEGRATION_EASYAPPAOINTMENTS_CIVICRM_ROOT_PATH', plugin_dir_path(__FILE__));
add_action('init', function() {
require_once INTEGRATION_EASYAPPAOINTMENTS_CIVICRM_ROOT_PATH . "CiviCRM/class.civicrm-api-local.php";
require_once INTEGRATION_EASYAPPAOINTMENTS_CIVICRM_ROOT_PATH . "CiviCRM/class.civicrm-api-wpcmrf.php";
});
/**
* Returns a list of possible connection profiles.
* @return array
*/
function integration_civicrm_easyappointments_get_profiles() {
static $profiles = null;
if (is_array($profiles)) {
return $profiles;
}
$profiles = array();
$profiles = CiviCRMEasyAppointmentsApiLocal::profiles($profiles);
$profiles = CiviCRMasyAppointmentsApiWpcmrf::profiles($profiles);
$profiles = apply_filters('integration_civicrm_easyappointments_get_profiles', $profiles);
return $profiles;
}
function integration_civicrm_easyappointments_api($entity, $action, $params, $options, $profile_id) {
$profiles = integration_civicrm_easyappointments_get_profiles();
if (!isset($profiles[$profile_id])) {
return ['error' => 'Invalid connection', 'is_error' => '1'];
}
$func = $profiles[$profile_id]['function'];
return call_user_func($func, $entity, $action, $params, $options, $profiles[$profile_id]['profile_id']);
}
add_action('ea_new_app', function($id, $appointment, $isFinal) {
if ($isFinal) {
integration_civicrm_easyappointments_submit_to_form_processor($id, 'new');
} elseif (is_admin() && is_user_logged_in() && !$isFinal) {
$data = json_decode(file_get_contents("php://input"), true);
if (!empty($data['id'])) {
integration_civicrm_easyappointments_submit_to_form_processor($id, 'edit');
} else {
integration_civicrm_easyappointments_submit_to_form_processor($id, 'new');
}
}
}, 10, 3);
add_action('ea_edit_app', function($id) {
if (is_admin() && is_user_logged_in()) {
integration_civicrm_easyappointments_submit_to_form_processor($id, 'edit');
}
}, 10, 1);
add_action('wp_ajax_ea_appointment', function() {
if (is_admin() && is_user_logged_in()) {
$method = $_SERVER['REQUEST_METHOD'];
if (!empty($_REQUEST['_method'])) {
$method = strtoupper($_REQUEST['_method']);
unset($_REQUEST['_method']);
}
if ($method == 'DELETE') {
$id = null;
if (isset($_REQUEST['id'])) {
$id = $_REQUEST['id'];
}
if ($id) {
integration_civicrm_easyappointments_submit_to_form_processor($id, 'delete');
}
}
}
}, 0);
function integration_civicrm_easyappointments_submit_to_form_processor($appointment_id, $action) {
global $ea_app;
$models = $ea_app->get_container()['db_models'];
$options = $ea_app->get_container()['options'];
$civicrm_ea_form_processor = $options->get_option_value('civicrm_ea_form_processor_'.$action, '');
if (!empty($civicrm_ea_form_processor)) {
$app_data = $models->get_appintment_by_id($appointment_id);
$options = [];
$profiles = integration_civicrm_easyappointments_get_profiles();
foreach($profiles as $profile => $profile_data) {
$result = integration_civicrm_easyappointments_api('FormProcessorInstance', 'list', [], ['cache' => '180 minutes'], $profile);
if (isset($result['values'] )) {
foreach ($result['values'] as $value) {
if ($profile.'__'.$value['name'] == $civicrm_ea_form_processor) {
integration_civicrm_easyappointments_api('FormProcessor', $value['name'], $app_data, $options,$profile);
}
}
}
}
}
}
function integration_civicrm_easyappointments_settings_page() {
global $ea_app;
$formProcessors = [];
$profiles = integration_civicrm_easyappointments_get_profiles();
foreach($profiles as $profile => $profile_data) {
$result = integration_civicrm_easyappointments_api('FormProcessorInstance', 'list', [], ['cache' => '180 minutes'], $profile);
if (isset($result['values'] )) {
foreach ($result['values'] as $value) {
$formProcessors[$profile.'__'.$value['name']] = $profile_data['title'].': '.$value['title'];
}
}
}
$models = $ea_app->get_container()['db_models'];
$options = $ea_app->get_container()['options'];
if (isset($_REQUEST['civicrm_ea_form_processor_new'])) {
$civicrm_ea_form_processor = sanitize_text_field($_REQUEST['civicrm_ea_form_processor_new']);
$option['ea_key'] = 'civicrm_ea_form_processor_new';
$option['ea_value'] = $civicrm_ea_form_processor;
$models->update_option($option);
}
if (isset($_REQUEST['civicrm_ea_form_processor_edit'])) {
$civicrm_ea_form_processor = sanitize_text_field($_REQUEST['civicrm_ea_form_processor_edit']);
$option['ea_key'] = 'civicrm_ea_form_processor_edit';
$option['ea_value'] = $civicrm_ea_form_processor;
$models->update_option($option);
}
if (isset($_REQUEST['civicrm_ea_form_processor_delete'])) {
$civicrm_ea_form_processor = sanitize_text_field($_REQUEST['civicrm_ea_form_processor_delete']);
$option['ea_key'] = 'civicrm_ea_form_processor_delete';
$option['ea_value'] = $civicrm_ea_form_processor;
$models->update_option($option);
}
$civicrm_ea_form_processor_new = $options->get_option_value('civicrm_ea_form_processor_new', '');
$civicrm_ea_form_processor_edit = $options->get_option_value('civicrm_ea_form_processor_edit', '');
$civicrm_ea_form_processor_delete = $options->get_option_value('civicrm_ea_form_processor_delete', '');
$fields = [
'id',
'location',
'service',
'worker',
'name',
'email',
'phone',
'date',
'start',
'end',
'end_date',
'description',
'status',
'user',
'created',
'price',
'ip',
'session',
'service_name',
'service_duration',
'service_price',
'worker_name',
'worker_email',
'worker_phone',
'location_name',
'location_address',
'location_location',
];
$metafields = $models->get_all_rows('ea_meta_fields');
foreach($metafields as $metafield) {
if (!in_array($metafield->slug, $fields)) {
$fields[] = $metafield->slug;
}
}
require_once INTEGRATION_EASYAPPAOINTMENTS_CIVICRM_ROOT_PATH . 'templates/admin_civicrm_settings.tpl.php';
}
if (is_admin()) {
add_action('admin_menu', function() {
// locations page
$integration_civicrm_settings = add_submenu_page(
'easy_app_top_level',
__('CiviCRM Settings', 'integration-civicrm-easyappointments'),
__('CiviCRM Settings', 'integration-civicrm-easyappointments'),
'manage_options',
'easy_app_civicrm_settings',
'integration_civicrm_easyappointments_settings_page'
);
});
}