-
Notifications
You must be signed in to change notification settings - Fork 9
/
tawk_to.admin.inc
338 lines (304 loc) · 10.4 KB
/
tawk_to.admin.inc
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
/**
* @file
* @package tawk.to module for Drupal CMS
* @copyright (C) 2021 tawk.to
* @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
require_once 'includes/tawk_to.helper.php';
/**
* tawk.to main admin form
*/
function tawk_to_admin_form($form, &$form_state) {
global $user;
$same_user = TawkHelper::check_same_user($user->uid);
$widget = TawkHelper::get_widget();
$base_url = TawkHelper::get_base_url();
$iframe_url = TawkHelper::get_iframe_url();
$display_opts = variable_get(TawkHelper::TAWK_TO_WIDGET_OPTIONS, '');
if ($display_opts && !empty($display_opts)) {
$display_opts = json_decode($display_opts);
}
else {
$display_opts = null;
}
// css and js files
$module_path = TawkHelper::get_module_path();
$form['#attached']['css'][] = array(
'data' => $module_path . '/css/tawk_to.admin.css',
'type' => 'file',
);
$form['#attached']['js'][] = array(
'data' => $module_path . '/js/tawk_to.admin.js',
'type' => 'file',
);
$form['#attached']['js'][] = array(
'data' => array(
'tawkto' => array(
'iframeUrl' => $iframe_url,
'baseUrl' => $base_url,
),
),
'type' => 'setting',
);
$form['widget_selection'] = build_widget_selection($same_user, $widget);
$form['visibility_settings'] = build_visibility_opts_form($display_opts);
$form['privacy_settings'] = build_privacy_settings_form($display_opts);
// submit button
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
$form['#submit'][] = 'tawk_to_admin_form_submit';
return $form;
}
/**
* builds tawk.to widget selection area
*/
function build_widget_selection($same_user, $widget) {
$form = array(
'#type' => 'fieldset',
'#title' => t('Property and Widget Selection'),
'#description' => t('Select the property and widget from your tawk.to account')
);
if(!$same_user) {
$form['already_set'] = array(
'#type' => 'markup',
'#markup' => '<div id="widget_already_set" class="alert alert-warning">Notice: Widget already set by other user</div>',
);
}
$form['iframe'] = array(
'#type' => 'markup',
'#markup' => '
<iframe id="tawkIframe" class="tawkto-property-widget-selection" src="" ></iframe>
<input type="hidden" class="hidden" name="page_id" value="' . $widget['page_id'] . '">
<input type="hidden" class="hidden" name="widget_id" value="' . $widget['widget_id'] . '">
',
);
return $form;
}
/**
* builds tawk.to widget visibility options form.
*/
function build_visibility_opts_form($display_opts) {
// visibility settings
$form = array(
'#type' => 'fieldset',
'#title' => t('Visibility Settings'),
'#description' => t('Define where the tawk.to widget will and won\'t show'),
);
// always display config
$always_display_attr = array();
if (!is_null($display_opts) && $display_opts->always_display) {
$always_display_attr['checked'] = true;
}
$form['always_display'] = array(
'#type' => 'checkbox',
'#title' => t('Always show Tawk.To widget on every page'),
'#description' => t('Select to show on all except the exceptions
De-select to select the specific pages'),
'#id' => t('always_display'),
'#attributes' => $always_display_attr,
);
// exclude url config
$form['hide_oncustom'] = array(
'#type' => 'textarea',
'#title' => t('Except on pages:'),
'#description' => t('
Add URLs/paths to pages in which you would like to hide the widget.<br>
Put each URL/path in a new line. Paths should have a leading \'/\'.
<br>
<div class="tooltip">
Examples of accepted path patterns
<ul class="tooltiptext">
<li>*</li>
<li>*/to/somewhere</li>
<li>/*/to/somewhere</li>
<li>/path/*/somewhere</li>
<li>/path/*/lead/*/somewhere</li>
<li>/path/*/*/somewhere</li>
<li>/path/to/*</li>
<li>/path/to/*/</li>
<li>*/to/*/page</li>
<li>/*/to/*/page</li>
<li>/path/*/other/*</li>
<li>/path/*/other/*/</li>
<li>http://www.example.com/</li>
<li>http://www.example.com/*</li>
<li>http://www.example.com/*/to/somewhere</li>
<li>http://www.example.com/path/*/somewhere</li>
<li>http://www.example.com/path/*/lead/*/somewhere</li>
<li>http://www.example.com/path/*/*/somewhere</li>
<li>http://www.example.com/path/to/*</li>
<li>http://www.example.com/path/to/*/</li>
<li>http://www.example.com/*/to/*/page</li>
<li>http://www.example.com/path/*/other/*</li>
<li>http://www.example.com/path/*/other/*/</li>
</ul>
</div>
'),
'#prefix' => '<div class="div_hide_specific">',
'#suffix' => '</div>',
'#id' => t('hide_oncustom'),
'#default_value' => !is_null($display_opts) && $display_opts->hide_oncustom ?
format_multiline_url_paths($display_opts->hide_oncustom) : '',
);
// show on front page config
$show_onfrontpage_attr = array();
if (!is_null($display_opts) && $display_opts->show_onfrontpage) {
$show_onfrontpage_attr['checked'] = true;
}
$form['show_onfrontpage'] = array(
'#type' => 'checkbox',
'#title' => t('Show on front page'),
'#prefix' => '<div class="div_show_specific">',
'#suffix' => '</div>',
'#id' => t('show_onfrontpage'),
'#attributes' => $show_onfrontpage_attr,
);
// show on category config
$show_oncategory_attr = array();
if (!is_null($display_opts) && $display_opts->show_oncategory) {
$show_oncategory_attr['checked'] = true;
}
$form['show_oncategory'] = array(
'#type' => 'checkbox',
'#title' => t('Show on category pages'),
'#description' => t('Select to show on pages for category terms'),
'#prefix' => '<div class="div_show_specific">',
'#suffix' => '</div>',
'#id' => t('show_oncategory'),
'#attributes' => $show_oncategory_attr,
);
// include urls config
$show_oncustom_default_value = '';
if (!is_null($display_opts) && $display_opts->show_oncustom) {
$show_oncustom_default_value = format_multiline_url_paths($display_opts->show_oncustom);
}
$form['show_oncustom'] = array(
'#type' => 'textarea',
'#title' => t('Show on pages:'),
'#description' => t('
Add URLs/paths to pages in which you would like to hide the widget.<br>
Put each URL/path in a new line. Paths should have a leading \'/\'.
<br>
<div class="tooltip">
Examples of accepted path patterns
<ul class="tooltiptext">
<li>*</li>
<li>*/to/somewhere</li>
<li>/*/to/somewhere</li>
<li>/path/*/somewhere</li>
<li>/path/*/lead/*/somewhere</li>
<li>/path/*/*/somewhere</li>
<li>/path/to/*</li>
<li>/path/to/*/</li>
<li>*/to/*/page</li>
<li>/*/to/*/page</li>
<li>/path/*/other/*</li>
<li>/path/*/other/*/</li>
<li>http://www.example.com/</li>
<li>http://www.example.com/*</li>
<li>http://www.example.com/*/to/somewhere</li>
<li>http://www.example.com/path/*/somewhere</li>
<li>http://www.example.com/path/*/lead/*/somewhere</li>
<li>http://www.example.com/path/*/*/somewhere</li>
<li>http://www.example.com/path/to/*</li>
<li>http://www.example.com/path/to/*/</li>
<li>http://www.example.com/*/to/*/page</li>
<li>http://www.example.com/path/*/other/*</li>
<li>http://www.example.com/path/*/other/*/</li>
</ul>
</div>
'),
'#prefix' => '<div class="div_show_specific">',
'#suffix' => '</div>',
'#id' => t('show_oncustom'),
'#default_value' => $show_oncustom_default_value,
);
return $form;
}
/**
* builds tawk.to privacy settings form
*/
function build_privacy_settings_form($display_opts) {
// privacy settings
$form = array(
'#type' => 'fieldset',
'#title' => t('Privacy Settings'),
'#description' => t('Define whether or not user information can be used'),
);
// enable visitor recognition config
$enable_visitor_recognition_attr = array();
if (!is_null($display_opts) && isset($display_opts->enable_visitor_recognition) && $display_opts->enable_visitor_recognition) {
$enable_visitor_recognition_attr['checked'] = true;
}
$form['enable_visitor_recognition'] = array(
'#type' => 'checkbox',
'#title' => t('Enable visitor recognition'),
'#description' => t('If selected, name and email address from logged in users will be used to identify the user to you when a chat comes in via tawk.to'),
'#id' => t('enable_visitor_recognition'),
'#attributes' => $enable_visitor_recognition_attr,
);
return $form;
}
/**
* tawk.to admin form submit handler
*/
function tawk_to_admin_form_submit($form, &$form_state) {
$jsonOpts = array(
'always_display' => false,
'hide_oncustom' => array(),
'show_onfrontpage' => false,
'show_oncategory' => false,
'show_oncustom' => array(),
'enable_visitor_recognition' => false,
);
$options = $form_state['values'];
foreach ($options as $column => $value) {
switch ($column) {
case 'hide_oncustom':
case 'show_oncustom':
// split by newlines, then remove empty lines
$value = str_replace("\r", "\n", $value);
$value = explode("\n", $value);
$non_empty_values = array();
foreach ($value as $str) {
$trimmed = trim($str);
if ($trimmed !== '') {
$non_empty_values[] = $trimmed;
}
}
$jsonOpts[$column] = json_encode($non_empty_values);
break;
case 'show_onfrontpage':
case 'show_oncategory':
case 'always_display':
case 'enable_visitor_recognition':
$jsonOpts[$column] = $value == 1;
break;
}
}
variable_set(
TawkHelper::TAWK_TO_WIDGET_OPTIONS,
json_encode($jsonOpts)
);
// Flush page cache so widget options will update at next page load
cache_clear_all(NULL, 'cache_page');
drupal_set_message('Successfully set widget options to your site');
}
/**
* Helper function to add new lines to url paths for displaying in textarea fields
*/
function format_multiline_url_paths($url_paths) {
$paths = json_decode($url_paths);
$formatted_paths = '';
foreach($paths as $path) {
if (!empty($formatted_paths)) {
$formatted_paths .= "\r\n";
}
$formatted_paths .= $path;
}
return $formatted_paths;
}